Direct-BT  2.3.1
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 
34 #include "direct_bt/BTDevice.hpp"
35 #include "direct_bt/BTAdapter.hpp"
36 
37 using namespace direct_bt;
38 using namespace jau;
39 
40 jstring Java_jau_direct_1bt_DBTGattService_toStringImpl(JNIEnv *env, jobject obj) {
41  try {
42  BTGattService *nativePtr = getJavaUplinkObject<BTGattService>(env, obj);
43  JavaGlobalObj::check(nativePtr->getJavaObject(), E_FILE_LINE);
44  return from_string_to_jstring(env, nativePtr->toString());
45  } catch(...) {
47  }
48  return nullptr;
49 }
50 
51 
52 void Java_jau_direct_1bt_DBTGattService_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) {
53  (void)obj;
54  try {
55  BTGattService *service = castInstance<BTGattService>(nativeInstance);
56  (void)service;
57  // No delete: Service instance owned by BTDevice
58  } catch(...) {
60  }
61 }
62 
63 static const std::string _characteristicClazzCtorArgs("(JLjau/direct_bt/DBTGattService;SLorg/direct_bt/GattCharPropertySet;Ljava/lang/String;SI)V");
64 static const std::string _gattCharPropSetClassName("org/direct_bt/GattCharPropertySet");
65 static const std::string _gattCharPropSetClazzCtorArgs("(B)V");
66 
67 jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj) {
68  try {
69  BTGattService *service = getJavaUplinkObject<BTGattService>(env, obj);
70  JavaGlobalObj::check(service->getJavaObject(), E_FILE_LINE);
71 
72  jau::darray<std::shared_ptr<BTGattChar>> & characteristics = service->characteristicList;
73 
74  jclass gattCharPropSetClazz;
75  jmethodID gattCharPropSetClazzCtor;
76  // gattCharPropSetClazzRef, gattCharPropSetClazzCtor
77  {
78  gattCharPropSetClazz = jau::search_class(env, _gattCharPropSetClassName.c_str());
80  if( nullptr == gattCharPropSetClazz ) {
81  throw jau::InternalError("BTDevice::java_class not found: "+_gattCharPropSetClassName, E_FILE_LINE);
82  }
83  }
84  gattCharPropSetClazzCtor = jau::search_method(env, gattCharPropSetClazz, "<init>", _gattCharPropSetClazzCtorArgs.c_str(), false);
86  if( nullptr == gattCharPropSetClazzCtor ) {
87  throw jau::InternalError("GattCharPropertySet ctor not found: "+_gattCharPropSetClassName+".<init>"+_gattCharPropSetClazzCtorArgs, E_FILE_LINE);
88  }
89 
90  /**
91  DBTGattChar(final long nativeInstance, final DBTGattService service,
92  final short handle, final GattCharPropertySet properties,
93  final String value_type_uuid, final short value_handle,
94  final int clientCharacteristicsConfigIndex)
95  */
96  std::function<jobject(JNIEnv*, jclass, jmethodID, BTGattChar *)> ctor_char =
97  [&gattCharPropSetClazz, &gattCharPropSetClazzCtor](JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, BTGattChar *characteristic)->jobject {
98  // prepare adapter ctor
99  std::shared_ptr<BTGattService> _service = characteristic->getServiceChecked();
100  JavaGlobalObj::check(_service->getJavaObject(), E_FILE_LINE);
101  jobject jservice = JavaGlobalObj::GetObject(_service->getJavaObject());
102 
103  jobject jGattCharPropSet = env_->NewObject(gattCharPropSetClazz, gattCharPropSetClazzCtor, (jbyte)characteristic->properties);
105  JNIGlobalRef::check(jGattCharPropSet, E_FILE_LINE);
107 
108  const jstring uuid = from_string_to_jstring(env_,
109  directBTJNISettings.getUnifyUUID128Bit() ? characteristic->value_type->toUUID128String() :
110  characteristic->value_type->toString());
112 
113  jobject jcharVal = env_->NewObject(clazz, clazz_ctor, (jlong)characteristic, jservice,
114  characteristic->handle, jGattCharPropSet,
115  uuid, characteristic->value_handle, characteristic->clientCharConfigIndex);
117  JNIGlobalRef::check(jcharVal, E_FILE_LINE);
118  std::shared_ptr<JavaAnon> jCharRef = characteristic->getJavaObject(); // GlobalRef
119  JavaGlobalObj::check(jCharRef, E_FILE_LINE);
120  env_->DeleteLocalRef(jGattCharPropSet);
121  env_->DeleteLocalRef(jcharVal);
122  return JavaGlobalObj::GetObject(jCharRef);
123  };
124  jobject jres = convert_vector_sharedptr_to_jarraylist<jau::darray<std::shared_ptr<BTGattChar>>, BTGattChar>(
125  env, characteristics, _characteristicClazzCtorArgs.c_str(), ctor_char);
126  env->DeleteLocalRef(gattCharPropSetClazz);
127  return jres;
128  } catch(...) {
130  }
131  return nullptr;
132 }
133 
direct_bt::BTGattService::characteristicList
jau::darray< BTGattCharRef > characteristicList
List of Characteristic Declarations as shared reference.
Definition: BTGattService.hpp:97
jau::from_string_to_jstring
jstring from_string_to_jstring(JNIEnv *env, const std::string &str)
helper_dbt.hpp
helper_base.hpp
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::BTGattChar
Definition: BTGattChar.hpp:75
jau::InternalError
Definition: basic_types.hpp:93
direct_bt::DirectBTJNISettings::getUnifyUUID128Bit
bool getUnifyUUID128Bit()
Enables or disables uuid128_t consolidation for native uuid16_t and uuid32_t values before string con...
Definition: helper_dbt.hpp:47
Java_jau_direct_1bt_DBTGattService_deleteImpl
void Java_jau_direct_1bt_DBTGattService_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance)
Definition: DBTGattService.cxx:52
_gattCharPropSetClassName
static const std::string _gattCharPropSetClassName("org/direct_bt/GattCharPropertySet")
jau
Definition: basic_algos.hpp:34
direct_bt::BTGattService
Representing a complete [Primary] Service Declaration including its list of Characteristic Declaratio...
Definition: BTGattService.hpp:67
Java_jau_direct_1bt_DBTGattService_toStringImpl
jstring Java_jau_direct_1bt_DBTGattService_toStringImpl(JNIEnv *env, jobject obj)
Definition: DBTGattService.cxx:40
jau::search_class
jclass search_class(JNIEnv *env, const char *clazz_name)
jau::java_exception_check_and_throw
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.
_gattCharPropSetClazzCtorArgs
static const std::string _gattCharPropSetClazzCtorArgs("(B)V")
E_FILE_LINE
#define E_FILE_LINE
Definition: basic_types.hpp:64
direct_bt::directBTJNISettings
DirectBTJNISettings directBTJNISettings
Definition: helper_dbt.cxx:35
jau::darray
Implementation of a dynamic linear array storage, aka vector.
Definition: darray.hpp:102
jau::search_method
jmethodID search_method(JNIEnv *env, jclass clazz, const char *method_name, const char *prototype, bool is_static)
BTAdapter.hpp
JNIGlobalRef::check
static void check(jobject object, const char *file, int line)
Definition: jni_mem.hpp:80
debug.hpp
rethrow_and_raise_java_exception
#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:55
direct_bt::BTGattService::toString
std::string toString() const noexcept override
Definition: BTGattService.cpp:66
_characteristicClazzCtorArgs
static const std::string _characteristicClazzCtorArgs("(JLjau/direct_bt/DBTGattService;SLorg/direct_bt/GattCharPropertySet;Ljava/lang/String;SI)V")
BTDevice.hpp
Java_jau_direct_1bt_DBTGattService_getCharsImpl
jobject Java_jau_direct_1bt_DBTGattService_getCharsImpl(JNIEnv *env, jobject obj)
Definition: DBTGattService.cxx:67