1 /* ****************************************************************
3 * Copyright 2016 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 #include "cautilinterface.h"
24 #include "camanagerleinterface.h"
25 #include "camanagerleutil.h"
26 #include "caleautoconnector.h"
28 #include "cacommonutil.h"
29 #include "camanagerdevice.h"
30 #include "caleclient.h"
31 #include "caleutils.h"
33 #define TAG "OIC_CA_MANAGER_LE"
35 static const jint SUPPORT_ADNROID_API_LEVEL = 18;
36 static const jint AUTH_FAIL = 5;
37 static const jint LINK_LOSS = 8;
38 static const jint ACCEPT_TIMEOUT_EXCEPTION = 16;
39 static const jint REMOTE_DISCONNECT = 19;
40 static const jint LOCAL_DISCONNECT = 22;
41 static const jint USER_REMOVED_BOND = 68;
42 static JavaVM *g_jvm = NULL;
43 static jobject g_context = NULL;
44 static jobject g_connectedDeviceSet = NULL;
47 CAResult_t CASetLEClientAutoConnectionDeviceInfo(const char* address)
49 OIC_LOG(DEBUG, TAG, "CASetClientAutoConnectionDeviceInfo");
50 VERIFY_NON_NULL(address, TAG, "address");
52 bool isAttached = false;
54 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
57 OIC_LOG(DEBUG, TAG, "AttachCurrentThread will be called for JNIEnv pointer");
58 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
62 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
63 return CA_STATUS_FAILED;
67 OIC_LOG_V(DEBUG, TAG, "set [%s] for Auto Connection", address);
69 jstring jni_leAddress = (*env)->NewStringUTF(env, address);
72 OIC_LOG(ERROR, TAG, "jni_leAddress is null");
76 if (!CAManagerCheckBTAddress(env, jni_leAddress))
78 OIC_LOG(ERROR, TAG, "this address is not BT address string format");
82 // if there is target address in SharedPreference, it will be reset.
83 if (CAManagerIsConnectedDeviceAddress(env, g_context, jni_leAddress, g_connectedDeviceSet))
85 if (!CAManagerRemoveConnectedDeviceAddress(env, g_context, jni_leAddress,
86 g_connectedDeviceSet))
88 OIC_LOG(ERROR, TAG, "Preference - remove has failed");
92 OIC_LOG(INFO, TAG, "Preference - remove success");
96 // it will be added new target address.
97 if (!CAManagerAddConnectedDeviceAddress(env, g_context, jni_leAddress, g_connectedDeviceSet))
99 OIC_LOG(ERROR, TAG, "Preference - putting has failed");
103 OIC_LOG(INFO, TAG, "Preference - putting success");
108 (*g_jvm)->DetachCurrentThread(g_jvm);
117 (*g_jvm)->DetachCurrentThread(g_jvm);
120 return CA_STATUS_FAILED;
123 CAResult_t CAUnsetLEClientAutoConnectionDeviceInfo(const char* address)
125 OIC_LOG(DEBUG, TAG, "CAUnsetClientAutoConnectionDeviceInfo");
126 VERIFY_NON_NULL(address, TAG, "address");
128 bool isAttached = false;
130 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
133 OIC_LOG(DEBUG, TAG, "AttachCurrentThread will be called for JNIEnv pointer");
134 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
138 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
139 return CA_STATUS_FAILED;
143 OIC_LOG_V(DEBUG, TAG, "unset [%s] for Auto Connection", address);
145 jstring jni_leAddress = (*env)->NewStringUTF(env, address);
148 OIC_LOG(ERROR, TAG, "jni_leAddress is null");
152 if (!CAManagerCheckBTAddress(env, jni_leAddress))
154 OIC_LOG(ERROR, TAG, "this address is not BT address string format");
158 // if there is target address in SharedPreference, it will be removed
159 if (CAManagerIsConnectedDeviceAddress(env, g_context, jni_leAddress, g_connectedDeviceSet))
161 if (!CAManagerRemoveConnectedDeviceAddress(env, g_context, jni_leAddress,
162 g_connectedDeviceSet))
164 OIC_LOG(ERROR, TAG, "Preference - remove has failed");
168 OIC_LOG(INFO, TAG, "Preference - remove success");
172 // remove target device for auto connection
173 CAResult_t ret = CAManagerRemoveACData(env, jni_leAddress);
174 if (CA_STATUS_OK != ret)
176 OIC_LOG(ERROR, TAG, "CAManagerRemoveACData has failed");
181 (*g_jvm)->DetachCurrentThread(g_jvm);
190 (*g_jvm)->DetachCurrentThread(g_jvm);
193 return CA_STATUS_FAILED;
196 CAResult_t CAManagerLEClientInitialize(JNIEnv *env, JavaVM *jvm, jobject context)
198 OIC_LOG(DEBUG, TAG, "CAManagerClientInitialize");
199 VERIFY_NON_NULL(env, TAG, "env");
200 VERIFY_NON_NULL(jvm, TAG, "jvm");
201 VERIFY_NON_NULL(context, TAG, "context");
203 jint jni_int_sdk = CALEGetBuildVersion(env);
204 if (jni_int_sdk < SUPPORT_ADNROID_API_LEVEL)
206 OIC_LOG_V(ERROR, TAG, "it is not supported (%d)", jni_int_sdk);
207 return CA_STATUS_FAILED;
211 g_context = (*env)->NewGlobalRef(env, context);;
213 CAManagerInitMutexVaraibles();
214 CAManagerInitLEAutoConnection();
215 CAManagerCreateACDataList();
217 // get last connected device list
219 set = CAManagerGetConnectedDeviceAddress(env, g_context);
222 // create new set<String> object
223 set = CAManagerCreateSetString(env);
226 OIC_LOG(ERROR, TAG, "CAManagerCreateSetString has failed");
227 return CA_STATUS_FAILED;
229 OIC_LOG(DEBUG, TAG, "created new SetString");
233 OIC_LOG(DEBUG, TAG, "get previous Set<String> object");
236 g_connectedDeviceSet = (jobject)(*env)->NewGlobalRef(env, set);
237 if (!g_connectedDeviceSet)
239 OIC_LOG(ERROR, TAG, "g_connectedDeviceSet is null");
240 return CA_STATUS_FAILED;
246 CAResult_t CAManagerLEClientTerminate(JNIEnv *env)
248 OIC_LOG(DEBUG, TAG, "CAManagerClientTerminate");
249 VERIFY_NON_NULL(env, TAG, "env");
251 // stop gatt connection
252 CAResult_t res = CALEClientDisconnectAll(env);
253 if (CA_STATUS_OK != res)
255 OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
258 res = CAManagerRemoveAllACData(env);
259 if (CA_STATUS_OK != res)
261 OIC_LOG(ERROR, TAG, "CAManagerRemoveAllACData has failed");
264 CAManagerDestroyACDataList();
265 CAManagerTerminateLEAutoConnection();
266 CAManagerTerminateMutexVariables();
270 (*env)->DeleteGlobalRef(env, g_context);
274 if (g_connectedDeviceSet)
276 (*env)->DeleteGlobalRef(env, g_connectedDeviceSet);
277 g_connectedDeviceSet = NULL;
283 void CAManagerLESetScanInterval(jint interval, jint count)
285 OIC_LOG(DEBUG, TAG, "CAManagerLESetScanInterval");
286 CALERestartScanWithInterval(interval, count, BLE_SCAN_ENABLE);
289 void CAManagerLEStopScan()
291 OIC_LOG(DEBUG, TAG, "CAManagerLEStopScan");
292 CALERestartScanWithInterval(0, 0, BLE_SCAN_DISABLE);
295 JNIEXPORT void JNICALL
296 Java_org_iotivity_ca_CaLeClientInterface_caManagerAdapterStateChangedCallback(
297 JNIEnv *env, jobject obj, jint state)
299 OIC_LOG_V(INFO, TAG, "caManagerAdapterStateChangedCallback - state %d", state);
300 VERIFY_NON_NULL_VOID(env, TAG, "env");
301 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
303 jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
304 jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
305 jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");
307 if (state_on == state)
309 OIC_LOG(DEBUG, TAG, "AdapterStateChangedCallback : state_on");
311 // when BT state is on. recovery flag has to be reset.
312 CAManagerSetBTRecovery(false);
314 // find target device for autoconnect
315 size_t length = CAManagerGetACDataLength();
316 OIC_LOG_V(DEBUG, TAG, "length of ACDataList : %d", length);
317 for (size_t idx = 0; idx < length; idx++)
319 jstring leAddress = CAManagerGetLEAddressFromACData(env, idx);
322 CAResult_t res = CAManagerStartAutoConnection(env, leAddress);
323 if (CA_STATUS_OK != res)
325 OIC_LOG(ERROR, TAG, "CAManagerStartAutoConnection has failed");
331 else if (state_off == state)
333 OIC_LOG(DEBUG, TAG, "AdapterStateChangedCallback : state_off");
335 // reset isAutoConnecting flag for all target devices
336 size_t length = CAManagerGetACDataLength();
337 OIC_LOG_V(DEBUG, TAG, "length of ACDataList : %d", length);
338 for (size_t idx = 0; idx < length; idx++)
340 jstring address = CAManagerGetLEAddressFromACData(env, idx);
343 CAManagerSetAutoConnectingFlag(env, address, false);
347 // check whether BT recovery is needed or not
348 if (CAManagerIsRecoveryFlagSet())
350 CAManagerProcessRecovery(env, STATE_OFF);
353 else if (state_turning_off == state)
355 OIC_LOG(DEBUG, TAG, "AdapterStateChangedCallback : state_turning_off");
359 OIC_LOG(INFO, TAG, "AdapterStateChangedCallback state is not available");
363 JNIEXPORT void JNICALL
364 Java_org_iotivity_ca_CaLeClientInterface_caManagerBondStateChangedCallback(
365 JNIEnv *env, jobject obj, jobject device)
367 OIC_LOG(INFO, TAG, "caManagerBondStateChangedCallback");
368 // this callback is called by CaLeClientInterface
369 // only when bond state is changed from BOND_BONDED to BOND_NONE
370 OIC_LOG(DEBUG, TAG, "bond state is changed from BOND_BONDED to BOND_NONE");
371 VERIFY_NON_NULL_VOID(env, TAG, "env");
372 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
373 VERIFY_NON_NULL_VOID(device, TAG, "device");
375 // get ble address from Bluetooth Device object
376 jstring jni_leAddress = CALEClientGetLEAddressFromBTDevice(env, device);
379 OIC_LOG(INFO, TAG, "unbonded : it isn't same device type");
383 char* leAddress = (char*)(*env)->GetStringUTFChars(env, jni_leAddress, NULL);
386 OIC_LOG(ERROR, TAG, "leAddress is null");
390 // if there is no data, CAData will be created.
391 OIC_LOG_V(DEBUG, TAG, "bond none device : %s", leAddress);
393 CAResult_t res = CAManagerRemoveACData(env, jni_leAddress);
394 if (CA_STATUS_OK != res)
396 OIC_LOG(ERROR, TAG, "CAManagerRemoveACData has failed");
399 (*env)->ReleaseStringUTFChars(env, jni_leAddress, leAddress);
401 if (!CAManagerRemoveConnectedDeviceAddress(env, g_context, jni_leAddress,
402 g_connectedDeviceSet))
404 OIC_LOG(ERROR, TAG, "CAManagerRemoveConnectedDeviceAddress has failed");
408 JNIEXPORT void JNICALL
409 Java_org_iotivity_ca_CaLeClientInterface_caManagerLeGattConnectionStateChangeCB(
410 JNIEnv *env, jobject obj, jobject gatt, jint status, jint newState)
412 OIC_LOG_V(INFO, TAG, "caManagerLeGattConnectionStateChangeCB - status %d, newState %d",
415 VERIFY_NON_NULL_VOID(env, TAG, "env");
416 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
417 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
419 jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, "STATE_CONNECTED");
420 jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, "STATE_DISCONNECTED");
422 jstring jni_address = CALEGetAddressFromGatt(env, gatt);
425 OIC_LOG(ERROR, TAG, "CALEGetAddressFromGatt is null");
429 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
432 OIC_LOG(ERROR, TAG, "address is null");
433 (*env)->DeleteLocalRef(env, jni_address);
437 OIC_LOG_V(DEBUG, TAG, "caManagerLeGattConnectionStateChangeCB - address [%s]", address);
439 if (GATT_SUCCESS == status && state_connected == newState) // le connected
441 OIC_LOG(DEBUG, TAG, "LE is connected");
443 CAResult_t res = CAManagerReadRemoteRssi(env, gatt);
444 if (CA_STATUS_OK != res)
446 OIC_LOG(ERROR, TAG, "CAManagerReadRemoteRssi has failed");
450 else if (state_disconnected == newState)// le disconnected
452 OIC_LOG(DEBUG, TAG, "LE is disconnected");
454 if (LINK_LOSS == status || REMOTE_DISCONNECT == status)
456 if (!CAManagerIsInACDataList(env, jni_address))
458 OIC_LOG_V(DEBUG, TAG, "this[%s] is not target address for Auto Connection",
463 CAManagerSetAutoConnectingFlag(env, jni_address, false);
465 CAResult_t res = CAManagerStartAutoConnection(env, jni_address);
466 if (CA_STATUS_OK != res)
468 OIC_LOG(ERROR, TAG, "CAManagerStartAutoConnection has failed");
472 else if (ACCEPT_TIMEOUT_EXCEPTION == status)
474 CAManagerProcessRecovery(env, START_RECOVERY);
479 (*env)->ReleaseStringUTFChars(env, jni_address, address);
480 (*env)->DeleteLocalRef(env, jni_address);
484 * Class: org_iotivity_ca_jar_caleinterface
485 * Method: caManagerLeServicesDiscoveredCallback
486 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
488 JNIEXPORT void JNICALL
489 Java_org_iotivity_ca_CaLeClientInterface_caManagerLeServicesDiscoveredCallback(JNIEnv *env,
494 OIC_LOG_V(INFO, TAG, "caManagerLeServicesDiscoveredCallback - status %d", status);
495 VERIFY_NON_NULL_VOID(env, TAG, "env");
496 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
497 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
499 if (GATT_SUCCESS == status)
501 if (!g_connectedDeviceSet)
503 OIC_LOG(ERROR, TAG, "g_connectedDeviceSet is null");
507 jstring jni_address = CALEGetAddressFromGatt(env, gatt);
510 OIC_LOG(ERROR, TAG, "CALEGetAddressFromGatt is null");
514 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
517 OIC_LOG(ERROR, TAG, "address is null");
518 (*env)->DeleteLocalRef(env, jni_address);
522 OIC_LOG_V(DEBUG, TAG, "ServicesDiscovered device : %s", address);
524 if (CAManagerIsConnectedDeviceAddress(env, g_context, jni_address, g_connectedDeviceSet))
526 OIC_LOG(INFO, TAG, "AC list - the address will be added to ACData list");
527 CAManagerAddACData(env, jni_address);
528 CAManagerSetAutoConnectingFlag(env, jni_address, false);
530 // next connection will be requested with JNI_TRUE on autoConnect flag
531 // after first connection
532 CALEClientSetFlagToState(env, jni_address, CA_LE_AUTO_CONNECT_FLAG, JNI_TRUE);
536 OIC_LOG(DEBUG, TAG, "AC list - the address is not set to AutoConnect");
539 (*env)->ReleaseStringUTFChars(env, jni_address, address);
540 (*env)->DeleteLocalRef(env, jni_address);
542 OIC_LOG(INFO, TAG, "ServicesDiscovery is successful");
546 OIC_LOG(ERROR, TAG, "ServicesDiscovery has failed");
551 * Class: org_iotivity_ca_jar_caleinterface
552 * Method: caManagerLeRemoteRssiCallback
553 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
555 JNIEXPORT void JNICALL
556 Java_org_iotivity_ca_CaLeClientInterface_caManagerLeRemoteRssiCallback(JNIEnv *env,
566 OIC_LOG_V(DEBUG, TAG, "caManagerLeRemoteRssiCallback - rssi : %d: ", rssi);
567 OIC_LOG_V(DEBUG, TAG, "caManagerLeRemoteRssiCallback - status : %d: ", status);
568 VERIFY_NON_NULL_VOID(env, TAG, "env");
569 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
570 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");