Direct-BT  2.3.1
Direct-BT - Direct Bluetooth Programming.
DBTGattDesc.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_DBTGattDesc.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 void Java_jau_direct_1bt_DBTGattDesc_deleteImpl(JNIEnv *env, jobject obj) {
41  try {
42  BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj);
43  (void)descriptor;
44  // No delete: Service instance owned by GATTService -> BTDevice
45  } catch(...) {
47  }
48 }
49 
50 jstring Java_jau_direct_1bt_DBTGattDesc_toStringImpl(JNIEnv *env, jobject obj) {
51  (void)obj;
52  try {
53  BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj);
54  JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE);
55  return from_string_to_jstring(env, descriptor->toString());
56  } catch(...) {
58  }
59  return nullptr;
60 }
61 
62 jbyteArray Java_jau_direct_1bt_DBTGattDesc_readValueImpl(JNIEnv *env, jobject obj) {
63  try {
64  BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj);
65  JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE);
66 
67  if( !descriptor->readValue() ) {
68  ERR_PRINT("Characteristic readValue failed: %s", descriptor->toString().c_str());
69  return env->NewByteArray((jsize)0);
70  }
71  const size_t value_size = descriptor->value.getSize();
72  jbyteArray jres = env->NewByteArray((jsize)value_size);
73  env->SetByteArrayRegion(jres, 0, (jsize)value_size, (const jbyte *)descriptor->value.get_ptr());
75  return jres;
76 
77  } catch(...) {
79  }
80  return nullptr;
81 }
82 
83 jboolean Java_jau_direct_1bt_DBTGattDesc_writeValueImpl(JNIEnv *env, jobject obj, jbyteArray jval) {
84  try {
85  if( nullptr == jval ) {
86  throw IllegalArgumentException("byte array null", E_FILE_LINE);
87  }
88  const size_t value_size = (size_t)env->GetArrayLength(jval);
89  if( 0 == value_size ) {
90  return JNI_TRUE;
91  }
92  BTGattDesc *descriptor = getJavaUplinkObject<BTGattDesc>(env, obj);
93  JavaGlobalObj::check(descriptor->getJavaObject(), E_FILE_LINE);
94 
95  JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
96  uint8_t * value_ptr = criticalArray.get(jval, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
97  if( NULL == value_ptr ) {
98  throw InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE);
99  }
100  TROOctets value(value_ptr, value_size);
101  descriptor->value = value; // copy data
102 
103  if( !descriptor->writeValue() ) {
104  ERR_PRINT("Descriptor writeValue failed: %s", descriptor->toString().c_str());
105  return JNI_FALSE;
106  }
107  return JNI_TRUE;
108  } catch(...) {
110  }
111  return JNI_FALSE;
112 }
113 
114 
direct_bt::TROOctets::getSize
constexpr jau::nsize_t getSize() const noexcept
Returns the used memory size for read and write operations, may be zero.
Definition: OctetTypes.hpp:118
jau::IllegalArgumentException
Definition: basic_types.hpp:111
jau::from_string_to_jstring
jstring from_string_to_jstring(JNIEnv *env, const std::string &str)
Java_jau_direct_1bt_DBTGattDesc_deleteImpl
void Java_jau_direct_1bt_DBTGattDesc_deleteImpl(JNIEnv *env, jobject obj)
Definition: DBTGattDesc.cxx:40
helper_dbt.hpp
helper_base.hpp
direct_bt
Definition: ATTPDUTypes.hpp:171
direct_bt::BTGattDesc::toString
virtual std::string toString() const noexcept override
Definition: BTGattDesc.cpp:82
direct_bt::BTGattDesc::value
POctets value
Definition: BTGattDesc.hpp:113
jau::InternalError
Definition: basic_types.hpp:93
direct_bt::BTGattDesc::writeValue
bool writeValue()
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.3 Write Characteristic Descriptors.
Definition: BTGattDesc.cpp:73
direct_bt::TROOctets::get_ptr
constexpr uint8_t const * get_ptr() const noexcept
Definition: OctetTypes.hpp:228
jau
Definition: basic_algos.hpp:34
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.
E_FILE_LINE
#define E_FILE_LINE
Definition: basic_types.hpp:64
BTAdapter.hpp
direct_bt::BTGattDesc::readValue
bool readValue(int expectedLength=-1)
BT Core Spec v5.2: Vol 3, Part G GATT: 4.12.1 Read Characteristic Descriptor.
Definition: BTGattDesc.cpp:64
JNICriticalArray::get
T * get(U jarray_val, Mode mode_val=UPDATE_AND_RELEASE)
Acquired the primitive array.
Definition: jni_mem.hpp:177
Java_jau_direct_1bt_DBTGattDesc_toStringImpl
jstring Java_jau_direct_1bt_DBTGattDesc_toStringImpl(JNIEnv *env, jobject obj)
Definition: DBTGattDesc.cxx:50
JNICriticalArray
Definition: jni_mem.hpp:126
debug.hpp
Java_jau_direct_1bt_DBTGattDesc_readValueImpl
jbyteArray Java_jau_direct_1bt_DBTGattDesc_readValueImpl(JNIEnv *env, jobject obj)
Definition: DBTGattDesc.cxx:62
direct_bt::TROOctets
Transient read only octet data, i.e.
Definition: OctetTypes.hpp:59
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
ERR_PRINT
#define ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE: '.
Definition: debug.hpp:132
Java_jau_direct_1bt_DBTGattDesc_writeValueImpl
jboolean Java_jau_direct_1bt_DBTGattDesc_writeValueImpl(JNIEnv *env, jobject obj, jbyteArray jval)
Definition: DBTGattDesc.cxx:83
BTDevice.hpp
direct_bt::BTGattDesc
BT Core Spec v5.2: Vol 3, Part G GATT: 3.3.3 Characteristic Descriptor.
Definition: BTGattDesc.hpp:62