Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBTGattService.cxx
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2020 ZAFENA AB
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26#include "jau_direct_bt_DBTGattService.h"
27
28// #define VERBOSE_ON 1
29#include <jau/debug.hpp>
30
31#include "helper_base.hpp"
32#include "helper_dbt.hpp"
33
37
38using namespace direct_bt;
39using namespace jau::jni;
40
41void Java_jau_direct_1bt_DBTGattService_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) {
42 (void)obj;
43 try {
44 shared_ptr_ref<BTGattService> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done
45 if( nullptr != sref.pointer() ) {
46 std::shared_ptr<BTGattService>* sref_ptr = castInstance<BTGattService>(nativeInstance);
47 delete sref_ptr;
48 }
49 } catch(...) {
51 }
52}
53
54jstring Java_jau_direct_1bt_DBTGattService_toStringImpl(JNIEnv *env, jobject obj) {
55 try {
56 shared_ptr_ref<BTGattService> service(env, obj); // hold until done
57 JavaAnonRef service_java = service->getJavaObject(); // hold until done!
58 JavaGlobalObj::check(service_java, E_FILE_LINE);
59 return from_string_to_jstring(env, service->toString());
60 } catch(...) {
62 }
63 return nullptr;
64}
65
66
67static const std::string _characteristicClazzCtorArgs("(JLjau/direct_bt/DBTGattService;SLorg/direct_bt/GattCharPropertySet;Ljava/lang/String;SII)V");
68static const std::string _gattCharPropSetClassName("org/direct_bt/GattCharPropertySet");
69static const std::string _gattCharPropSetClazzCtorArgs("(B)V");
70
71jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj) {
72 try {
73 shared_ptr_ref<BTGattService> service(env, obj); // hold until done
74 JavaAnonRef service_java = service->getJavaObject(); // hold until done!
75 JavaGlobalObj::check(service_java, E_FILE_LINE);
76
77 jau::darray<std::shared_ptr<BTGattChar>> & characteristics = service->characteristicList;
78
79 jclass gattCharPropSetClazz;
80 jmethodID gattCharPropSetClazzCtor;
81 // gattCharPropSetClazzRef, gattCharPropSetClazzCtor
82 {
83 gattCharPropSetClazz = search_class(env, _gattCharPropSetClassName.c_str());
85 if( nullptr == gattCharPropSetClazz ) {
86 throw jau::InternalError("BTDevice::java_class not found: "+_gattCharPropSetClassName, E_FILE_LINE);
87 }
88 }
89 gattCharPropSetClazzCtor = search_method(env, gattCharPropSetClazz, "<init>", _gattCharPropSetClazzCtorArgs.c_str(), false);
91 if( nullptr == gattCharPropSetClazzCtor ) {
92 throw jau::InternalError("GattCharPropertySet ctor not found: "+_gattCharPropSetClassName+".<init>"+_gattCharPropSetClazzCtorArgs, E_FILE_LINE);
93 }
94
95 /**
96 DBTGattChar(final long nativeInstance, final DBTGattService service,
97 final short handle, final GattCharPropertySet properties,
98 final String value_type_uuid, final short value_handle,
99 final int clientCharacteristicsConfigIndex,
100 final int userDescriptionIndex)
101 */
102 jau::function<jobject(JNIEnv*, jclass, jmethodID, const BTGattCharRef&)> ctor_char =
103 [&gattCharPropSetClazz, &gattCharPropSetClazzCtor](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, const BTGattCharRef& characteristic)->jobject {
104 // prepare adapter ctor
105 std::shared_ptr<BTGattService> _service = characteristic->getServiceUnchecked();
106 if( nullptr == _service ) {
107 throw jau::RuntimeException("Characteristic's service null: "+characteristic->toString(), E_FILE_LINE);
108 }
109 JavaAnonRef _service_java = _service->getJavaObject(); // hold until done!
110 JavaGlobalObj::check(_service_java, E_FILE_LINE);
111
112 jobject jservice = JavaGlobalObj::GetObject(_service_java);
113
114 jobject jGattCharPropSet = env_->NewObject(gattCharPropSetClazz, gattCharPropSetClazzCtor, (jbyte)characteristic->properties);
116 JNIGlobalRef::check(jGattCharPropSet, E_FILE_LINE);
118
119 const jstring uuid = from_string_to_jstring(env_, characteristic->value_type->toUUID128String());
121
122 shared_ptr_ref<BTGattChar> characteristic_sref(characteristic); // new instance to be released into new jobject
123 jobject jcharVal = env_->NewObject(clazz, clazz_ctor, characteristic_sref.release_to_jlong(), jservice,
124 characteristic->handle, jGattCharPropSet,
125 uuid, characteristic->value_handle,
126 characteristic->clientCharConfigIndex,
127 characteristic->userDescriptionIndex);
129 JNIGlobalRef::check(jcharVal, E_FILE_LINE);
130 JavaAnonRef jCharRef = characteristic->getJavaObject(); // GlobalRef
131 JavaGlobalObj::check(jCharRef, E_FILE_LINE);
132 env_->DeleteLocalRef(jGattCharPropSet);
133 env_->DeleteLocalRef(jcharVal);
134 return JavaGlobalObj::GetObject(jCharRef);
135 };
136 jobject jres = convert_vector_sharedptr_to_jarraylist<jau::darray<std::shared_ptr<BTGattChar>>, BTGattChar>(
137 env, characteristics, _characteristicClazzCtorArgs.c_str(), ctor_char);
138 env->DeleteLocalRef(gattCharPropSetClazz);
139 return jres;
140 } catch(...) {
142 }
143 return nullptr;
144}
145
void Java_jau_direct_1bt_DBTGattService_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance)
jstring Java_jau_direct_1bt_DBTGattService_toStringImpl(JNIEnv *env, jobject obj)
static const std::string _gattCharPropSetClazzCtorArgs("(B)V")
static const std::string _gattCharPropSetClassName("org/direct_bt/GattCharPropertySet")
static const std::string _characteristicClazzCtorArgs("(JLjau/direct_bt/DBTGattService;SLorg/direct_bt/GattCharPropertySet;Ljava/lang/String;SII)V")
jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj)
#define E_FILE_LINE
Representing a Gatt Characteristic object from the GATTRole::Client perspective.
Definition: BTGattChar.hpp:94
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:148
Class template jau::function is a general-purpose static-polymorphic function wrapper.
A std::shared_ptr<T> storage instance to be copied from and released into a java object's long native...
Definition: helper_jni.hpp:393
std::shared_ptr< T > * pointer() noexcept
Provides access to the shared_ptr<T> pointer, l-value of storage.
Definition: helper_jni.hpp:621
std::string toString() const noexcept
Definition: helper_jni.hpp:657
jlong release_to_jlong() noexcept
Release ownership and return the jlong representation of the shared_ptr<T> storage.
Definition: helper_jni.hpp:574
std::shared_ptr< BTGattChar > BTGattCharRef
Definition: BTGattChar.hpp:410
void java_exception_check_and_throw(JNIEnv *env, const char *file, int line)
Throws a C++ exception if a java exception occurred, otherwise do nothing.
jstring from_string_to_jstring(JNIEnv *env, const std::string &str)
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, const char *prototype, bool is_static)
std::shared_ptr< JavaAnon > JavaAnonRef
Definition: java_uplink.hpp:55
jclass search_class(JNIEnv *env, const char *clazz_name)
#define rethrow_and_raise_java_exception(E)
Re-throw current exception and raise respective java exception using any matching function above.
Definition: helper_base.hpp:52