1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2020 Gothel Software e.K.
* Copyright (c) 2020 ZAFENA AB
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "direct_bt_tinyb_DBTManager.h"
// #define VERBOSE_ON 1
#include <jau/debug.hpp>
#include "helper_base.hpp"
#include "helper_dbt.hpp"
#include "direct_bt/DBTDevice.hpp"
#include "direct_bt/DBTAdapter.hpp"
#include "direct_bt/DBTManager.hpp"
using namespace direct_bt;
using namespace jau;
static const std::string _removeAdapterCBMethodName("removeAdapterCB");
static const std::string _removeAdapterCBMethodArgs("(II)V");
static const std::string _updatedAdapterCBMethodName("updatedAdapterCB");
static const std::string _updatedAdapterCBMethodArgs("(II)V");
struct BooleanMgmtCBContext {
MgmtEvent::Opcode opc;
JNIGlobalRef jmgmtRef;
jmethodID mid;
BooleanMgmtCBContext(MgmtEvent::Opcode opc_, JNIGlobalRef jmgmtRef_, jmethodID mid_)
: opc(opc_), jmgmtRef(jmgmtRef_), mid(mid_) { }
bool operator==(const BooleanMgmtCBContext& rhs) const
{
if( &rhs == this ) {
return true;
}
return rhs.opc == opc &&
rhs.jmgmtRef == jmgmtRef;
}
bool operator!=(const BooleanMgmtCBContext& rhs) const
{ return !( *this == rhs ); }
};
typedef std::shared_ptr<BooleanMgmtCBContext> BooleanMgmtCBContextRef;
static void _addMgmtCBOnce(JNIEnv *env, DBTManager & mgmt, JNIGlobalRef jmgmtRef, MgmtEvent::Opcode opc,
const std::string &jmethodName, const std::string &jmethodArgs)
{
try {
bool(*nativeCallback)(BooleanMgmtCBContextRef&, const MgmtEvent&) =
[](BooleanMgmtCBContextRef& ctx_ref, const MgmtEvent& e)->bool {
JNIEnv *env_ = *jni_env;
const int dev_id = e.getDevID();
if( MgmtEvent::Opcode::INDEX_REMOVED == ctx_ref->opc ) {
env_->CallVoidMethod(*(ctx_ref->jmgmtRef), ctx_ref->mid, dev_id, ctx_ref->opc); // remove
} else if( MgmtEvent::Opcode::INDEX_ADDED == ctx_ref->opc ) {
env_->CallVoidMethod(*(ctx_ref->jmgmtRef), ctx_ref->mid, dev_id, ctx_ref->opc); // updated
} else if( MgmtEvent::Opcode::NEW_SETTINGS == ctx_ref->opc ) {
const MgmtEvtNewSettings &event = *static_cast<const MgmtEvtNewSettings *>(&e);
if( isAdapterSettingBitSet(event.getSettings(), AdapterSetting::POWERED) ) {
// probably newly POWERED on
env_->CallVoidMethod(*(ctx_ref->jmgmtRef), ctx_ref->mid, dev_id, ctx_ref->opc); // updated
}
}
return true;
};
jclass mgmtClazz = jau::search_class(env, jmgmtRef.getObject());
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mgmtClazz ) {
throw jau::InternalError("DBTManager not found", E_FILE_LINE);
}
jmethodID mid = jau::search_method(env, mgmtClazz, jmethodName.c_str(), jmethodArgs.c_str(), false);
jau::java_exception_check_and_throw(env, E_FILE_LINE);
if( nullptr == mid ) {
throw jau::InternalError("DBTManager has no "+jmethodName+"."+jmethodArgs+" method, for "+mgmt.toString(), E_FILE_LINE);
}
// move BooleanDeviceCBContextRef into CaptureInvocationFunc and operator== includes javaCallback comparison
FunctionDef<bool, const MgmtEvent&> funcDef = bindCaptureFunc(std::make_shared<BooleanMgmtCBContext>(opc, jmgmtRef, mid), nativeCallback);
mgmt.addMgmtEventCallback(-1, opc, funcDef);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void Java_direct_1bt_tinyb_DBTManager_initImpl(JNIEnv *env, jobject obj, jboolean unifyUUID128Bit, jint jbtMode)
{
directBTJNISettings.setUnifyUUID128Bit(unifyUUID128Bit);
try {
BTMode btMode = static_cast<BTMode>(jbtMode);
DBTManager *manager = &DBTManager::get(btMode); // special: static singleton
setInstance<DBTManager>(env, obj, manager);
java_exception_check_and_throw(env, E_FILE_LINE);
manager->setJavaObject( std::shared_ptr<JavaAnon>( new JavaGlobalObj(obj, nullptr) ) );
JavaGlobalObj::check(manager->getJavaObject(), E_FILE_LINE);
JNIGlobalRef jmgmtRef = JavaGlobalObj::GetJavaObject(manager->getJavaObject());
_addMgmtCBOnce(env, *manager, jmgmtRef, MgmtEvent::Opcode::INDEX_REMOVED, _removeAdapterCBMethodName, _removeAdapterCBMethodArgs);
_addMgmtCBOnce(env, *manager, jmgmtRef, MgmtEvent::Opcode::INDEX_ADDED, _updatedAdapterCBMethodName, _updatedAdapterCBMethodArgs);
_addMgmtCBOnce(env, *manager, jmgmtRef, MgmtEvent::Opcode::NEW_SETTINGS, _updatedAdapterCBMethodName, _updatedAdapterCBMethodArgs);
DBG_PRINT("Java_direct_1bt_tinyb_DBTManager_init: Manager %s", manager->toString().c_str());
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
void Java_direct_1bt_tinyb_DBTManager_deleteImpl(JNIEnv *env, jobject obj, jlong nativeInstance)
{
(void)obj;
try {
DBTManager *manager = castInstance<DBTManager>(nativeInstance); // special: static singleton
manager->close();
manager->setJavaObject();
(void) manager;
} catch(...) {
rethrow_and_raise_java_exception(env);
}
}
static const std::string _adapterClazzCtorArgs("(J[BLjava/lang/String;I)V");
static jobject _createJavaAdapter(JNIEnv *env_, jclass clazz, jmethodID clazz_ctor, DBTAdapter* adapter) {
// prepare adapter ctor
const EUI48 addr = adapter->getAddress();
jbyteArray jaddr = env_->NewByteArray(sizeof(addr));
env_->SetByteArrayRegion(jaddr, 0, sizeof(addr), (const jbyte*)(addr.b));
jau::java_exception_check_and_throw(env_, E_FILE_LINE);
const jstring name = from_string_to_jstring(env_, adapter->getName());
java_exception_check_and_throw(env_, E_FILE_LINE);
jobject jAdapter = env_->NewObject(clazz, clazz_ctor, (jlong)adapter, jaddr, name, adapter->dev_id);
java_exception_check_and_throw(env_, E_FILE_LINE);
JNIGlobalRef::check(jAdapter, E_FILE_LINE);
std::shared_ptr<JavaAnon> jAdapterRef = adapter->getJavaObject(); // GlobalRef
JavaGlobalObj::check(jAdapterRef, E_FILE_LINE);
env_->DeleteLocalRef(jaddr);
env_->DeleteLocalRef(name);
env_->DeleteLocalRef(jAdapter);
DBG_PRINT("Java_direct_1bt_tinyb_DBTManager_createJavaAdapter: New Adapter %p %s", adapter, adapter->toString().c_str());
return JavaGlobalObj::GetObject(jAdapterRef);
};
jobject Java_direct_1bt_tinyb_DBTManager_getAdapterListImpl(JNIEnv *env, jobject obj)
{
try {
DBTManager *manager = getInstance<DBTManager>(env, obj);
DBG_PRINT("Java_direct_1bt_tinyb_DBTManager_getAdapterListImpl: Manager %s", manager->toString().c_str());
jau::darray<std::shared_ptr<DBTAdapter>> adapters = manager->getAdapters();
return convert_vector_sharedptr_to_jarraylist<jau::darray<std::shared_ptr<DBTAdapter>>, DBTAdapter>(
env, adapters, _adapterClazzCtorArgs.c_str(), _createJavaAdapter);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
jobject Java_direct_1bt_tinyb_DBTManager_getAdapterImpl(JNIEnv *env, jobject obj, jint dev_id)
{
try {
DBTManager *manager = getInstance<DBTManager>(env, obj);
std::shared_ptr<DBTAdapter> adapter = manager->getAdapter(dev_id);
if( nullptr == adapter ) {
ERR_PRINT("DBTManager::getAdapterImpl: Adapter dev_id %d: Not found", dev_id);
return nullptr;
}
DBG_PRINT("DBTManager::getAdapterImpl: Adapter dev_id %d: %s", dev_id, adapter->toString().c_str());
return jau::convert_instance_to_jobject<DBTAdapter>(env, adapter.get(), _adapterClazzCtorArgs.c_str(), _createJavaAdapter);
} catch(...) {
rethrow_and_raise_java_exception(env);
}
return nullptr;
}
|