Direct-BT v3.3.0-1-gc2d430c
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
36
37using namespace direct_bt;
38using namespace jau::jni;
39
40void Java_jau_direct_1bt_DBTGattDesc_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance) {
41 (void)obj;
42 try {
43 shared_ptr_ref<BTGattDesc> sref(nativeInstance, false /* throw_on_nullptr */); // hold copy until done
44 if( nullptr != sref.pointer() ) {
45 std::shared_ptr<BTGattDesc>* sref_ptr = castInstance<BTGattDesc>(nativeInstance);
46 delete sref_ptr;
47 }
48 } catch(...) {
50 }
51}
52
53jstring Java_jau_direct_1bt_DBTGattDesc_toStringImpl(JNIEnv *env, jobject obj) {
54 (void)obj;
55 try {
56 shared_ptr_ref<BTGattDesc> descriptor(env, obj); // hold until done
57 JavaAnonRef descriptor_java = descriptor->getJavaObject(); // hold until done!
58 JavaGlobalObj::check(descriptor_java, E_FILE_LINE);
59 return from_string_to_jstring(env, descriptor->toString());
60 } catch(...) {
62 }
63 return nullptr;
64}
65
66jbyteArray Java_jau_direct_1bt_DBTGattDesc_readValueImpl(JNIEnv *env, jobject obj) {
67 try {
68 shared_ptr_ref<BTGattDesc> descriptor(env, obj); // hold until done
69 JavaAnonRef descriptor_java = descriptor->getJavaObject(); // hold until done!
70 JavaGlobalObj::check(descriptor_java, E_FILE_LINE);
71
72 if( !descriptor->readValue() ) {
73 ERR_PRINT("Characteristic readValue failed: %s", descriptor->toString().c_str());
74 return env->NewByteArray((jsize)0);
75 }
76 const size_t value_size = descriptor->value.size();
77 jbyteArray jres = env->NewByteArray((jsize)value_size);
78 env->SetByteArrayRegion(jres, 0, (jsize)value_size, (const jbyte *)descriptor->value.get_ptr());
80 return jres;
81
82 } catch(...) {
84 }
85 return nullptr;
86}
87
88jboolean Java_jau_direct_1bt_DBTGattDesc_writeValueImpl(JNIEnv *env, jobject obj, jbyteArray jval) {
89 try {
90 shared_ptr_ref<BTGattDesc> descriptor(env, obj); // hold until done
91 JavaAnonRef descriptor_java = descriptor->getJavaObject(); // hold until done!
92 JavaGlobalObj::check(descriptor_java, E_FILE_LINE);
93
94 if( nullptr == jval ) {
95 throw jau::IllegalArgumentException("byte array null", E_FILE_LINE);
96 }
97 const size_t value_size = (size_t)env->GetArrayLength(jval);
98 if( 0 == value_size ) {
99 return JNI_TRUE;
100 }
101 JNICriticalArray<uint8_t, jbyteArray> criticalArray(env); // RAII - release
102 uint8_t * value_ptr = criticalArray.get(jval, criticalArray.Mode::NO_UPDATE_AND_RELEASE);
103 if( nullptr == value_ptr ) {
104 throw jau::InternalError("GetPrimitiveArrayCritical(byte array) is null", E_FILE_LINE);
105 }
106 jau::TROOctets value(value_ptr, value_size, jau::lb_endian_t::little);
107 descriptor->value = value; // copy data
108
109 if( !descriptor->writeValue() ) {
110 ERR_PRINT("Descriptor writeValue failed: %s", descriptor->toString().c_str());
111 return JNI_FALSE;
112 }
113 return JNI_TRUE;
114 } catch(...) {
116 }
117 return JNI_FALSE;
118}
119
120
jbyteArray Java_jau_direct_1bt_DBTGattDesc_readValueImpl(JNIEnv *env, jobject obj)
Definition: DBTGattDesc.cxx:66
jstring Java_jau_direct_1bt_DBTGattDesc_toStringImpl(JNIEnv *env, jobject obj)
Definition: DBTGattDesc.cxx:53
jboolean Java_jau_direct_1bt_DBTGattDesc_writeValueImpl(JNIEnv *env, jobject obj, jbyteArray jval)
Definition: DBTGattDesc.cxx:88
void Java_jau_direct_1bt_DBTGattDesc_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance)
Definition: DBTGattDesc.cxx:40
#define E_FILE_LINE
Transient read only and endian aware octet data, i.e.
Definition: octets.hpp:67
T * get(U jarray_val, Mode mode_val=UPDATE_AND_RELEASE)
Acquired the primitive array.
Definition: jni_mem.hpp:193
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
#define ERR_PRINT(...)
Use for unconditional error messages, prefix '[elapsed_time] Error @ FILE:LINE FUNC: '.
Definition: debug.hpp:109
@ little
Identifier for little endian, equivalent to endian::little.
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)
std::shared_ptr< JavaAnon > JavaAnonRef
Definition: java_uplink.hpp:55
#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