1 /******************************************************************
3 * Copyright 2014 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 <android/log.h>
25 #include "calenwmonitor.h"
26 #include "caleclient.h"
27 #include "caleserver.h"
28 #include "caleutils.h"
29 #include "caleinterface.h"
30 #include "caadapterutils.h"
34 #include "org_iotivity_ca_CaLeClientInterface.h"
35 #include "org_iotivity_ca_CaLeServerInterface.h"
37 #define TAG PCF("OIC_CA_LE_MONITOR")
39 static const jint CONNECTION_FAILED_TO_BE_EASTABLISHED = 62;
44 * @var g_bleDeviceStateChangedCallback
45 * @brief Maintains the callback to be notified on device state changed.
47 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
50 * @var g_bleConnectionStateChangedCallback
51 * @brief Maintains the callback to be notified on device state changed.
53 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
56 * @var g_bleDeviceStateChangedCbMutex
57 * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
58 * of the LE adapter gets change.
60 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
63 * @var g_bleConnectionStateChangedCbMutex
64 * @brief Mutex to synchronize access to the LE ConnectionStateChanged Callback when the state
65 * of the LE adapter gets change.
67 static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
70 void CALENetworkMonitorJNISetContext()
72 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJNISetContext - it is not supported");
76 void CALENetworkMonitorJniInit()
78 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJniInit");
79 g_jvm = CANativeJNIGetJavaVM();
82 void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback)
84 OIC_LOG(DEBUG, TAG, "CALESetAdapterStateCallback");
85 g_bleDeviceStateChangedCallback = callback;
88 CAResult_t CAInitializeLEAdapter()
95 CAResult_t CAStartLEAdapter()
102 CAResult_t CAStopLEAdapter()
109 CAResult_t CAInitLENwkMonitorMutexVaraibles()
111 OIC_LOG(DEBUG, TAG, "IN");
112 if (NULL == g_bleDeviceStateChangedCbMutex)
114 g_bleDeviceStateChangedCbMutex = ca_mutex_new();
115 if (NULL == g_bleDeviceStateChangedCbMutex)
117 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
118 return CA_STATUS_FAILED;
122 if (NULL == g_bleConnectionStateChangedCbMutex)
124 g_bleConnectionStateChangedCbMutex = ca_mutex_new();
125 if (NULL == g_bleConnectionStateChangedCbMutex)
127 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
128 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
129 return CA_STATUS_FAILED;
133 OIC_LOG(DEBUG, TAG, "OUT");
137 void CATerminateLENwkMonitorMutexVaraibles()
139 OIC_LOG(DEBUG, TAG, "IN");
141 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
142 g_bleDeviceStateChangedCbMutex = NULL;
144 ca_mutex_free(g_bleConnectionStateChangedCbMutex);
145 g_bleConnectionStateChangedCbMutex = NULL;
147 OIC_LOG(DEBUG, TAG, "OUT");
150 CAResult_t CAGetLEAdapterState()
152 OIC_LOG(DEBUG, TAG, "IN");
156 OIC_LOG(ERROR, TAG, "g_jvm is null");
157 return CA_STATUS_FAILED;
160 bool isAttached = false;
162 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
165 OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
166 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
170 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
171 return CA_STATUS_FAILED;
176 if (!CALEIsEnableBTAdapter(env))
178 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
181 (*g_jvm)->DetachCurrentThread(g_jvm);
183 return CA_ADAPTER_NOT_ENABLED;
188 (*g_jvm)->DetachCurrentThread(g_jvm);
191 OIC_LOG(DEBUG, TAG, "OUT");
195 CAResult_t CAInitializeLENetworkMonitor()
197 OIC_LOG(DEBUG, TAG, "IN");
199 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
200 if (CA_STATUS_OK != res)
202 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
203 return CA_STATUS_FAILED;
206 CALENetworkMonitorJNISetContext();
207 CALENetworkMonitorJniInit();
209 OIC_LOG(DEBUG, TAG, "OUT");
215 void CATerminateLENetworkMonitor()
217 OIC_LOG(DEBUG, TAG, "IN");
219 CATerminateLENwkMonitorMutexVaraibles();
221 OIC_LOG(DEBUG, TAG, "OUT");
224 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
226 OIC_LOG(DEBUG, TAG, "IN");
228 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
230 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
231 CALESetAdapterStateCallback(callback);
232 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
234 OIC_LOG(DEBUG, TAG, "OUT");
238 CAResult_t CAUnSetLEAdapterStateChangedCb()
240 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
244 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
246 OIC_LOG(DEBUG, TAG, "IN");
247 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
248 g_bleConnectionStateChangedCallback = callback;
249 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
250 OIC_LOG(DEBUG, TAG, "OUT");
254 CAResult_t CAUnsetLENWConnectionStateChangedCb()
256 OIC_LOG(DEBUG, TAG, "IN");
257 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
258 g_bleConnectionStateChangedCallback = NULL;
259 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
260 OIC_LOG(DEBUG, TAG, "OUT");
264 static CAResult_t CALEStateConnectedCallback(JNIEnv *env, jstring jni_address,
265 jboolean isDescriptorFound)
267 VERIFY_NON_NULL(env, TAG, "env");
268 VERIFY_NON_NULL(jni_address, TAG, "jni_address");
270 if (CALEClientGetFlagFromState(env, jni_address, CA_LE_DESCRIPTOR_FOUND) == isDescriptorFound)
272 if (g_bleConnectionStateChangedCallback)
274 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
277 OIC_LOG(ERROR, TAG, "address is null");
278 return CA_STATUS_FAILED;
281 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
282 OIC_LOG(DEBUG, TAG, "BLE is connected");
284 (*env)->ReleaseStringUTFChars(env, jni_address, address);
291 JNIEXPORT void JNICALL
292 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
295 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
296 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
298 OIC_LOG_V(DEBUG, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);
300 if (!g_bleDeviceStateChangedCallback)
302 OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
306 jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
307 jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
308 jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");
310 if (state_on == status) // STATE_ON:12
312 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
313 CALEClientCreateDeviceList();
314 CALEServerCreateCachedDeviceList();
316 g_bleDeviceStateChangedCallback(newStatus);
318 else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
320 // gatt Device list will be removed.
321 // so it is need to create list again when adapter is enabled.
322 CAStopLEGattClient();
324 else if (state_off == status) // STATE_OFF:10
326 // remove obj for client
327 CAResult_t res = CALEClientRemoveAllGattObjs(env);
328 if (CA_STATUS_OK != res)
330 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
333 res = CALEClientResetDeviceStateForAll();
334 if (CA_STATUS_OK != res)
336 OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
339 // remove obj for server
340 res = CALEServerRemoveAllDevices(env);
341 if (CA_STATUS_OK != res)
343 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
346 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
347 g_bleDeviceStateChangedCallback(newStatus);
351 JNIEXPORT void JNICALL
352 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
355 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
356 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
357 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
358 VERIFY_NON_NULL_VOID(jaddr, TAG, "jaddr is null");
360 // geneally 'addr' parameter will be not ble address, if you didn't bond for BLE.
361 // below logics will be needed when ble pairing is set.
363 CAResult_t res = CALEClientDisconnectforAddress(env, jaddr);
364 if (CA_STATUS_OK != res)
366 OIC_LOG(ERROR, TAG, "CALEClientDisconnectforAddress has failed");
369 // remove obj for client
370 res = CALEClientRemoveGattObjForAddr(env, jaddr);
371 if (CA_STATUS_OK != res)
373 OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
376 res = CALEClientRemoveDeviceInScanDeviceList(env, jaddr);
377 if (CA_STATUS_OK != res)
379 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
382 // remove obej for server
383 res = CALEServerRemoveDevice(env, jaddr);
384 if (CA_STATUS_OK != res)
386 OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
390 JNIEXPORT void JNICALL
391 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWConnectionStateChangeCallback(JNIEnv *env,
397 OIC_LOG_V(DEBUG, TAG, "CALeGattNWConnectionStateChangeCallback - status %d, newstate %d",
399 VERIFY_NON_NULL_VOID(env, TAG, "env");
400 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
401 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
403 jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
404 "STATE_DISCONNECTED");
405 if (state_disconnected == newState)
407 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
410 OIC_LOG(ERROR, TAG, "jni_address is null");
414 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
417 OIC_LOG(ERROR, TAG, "address is null");
421 if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
423 if (g_bleConnectionStateChangedCallback)
425 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
426 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
430 (*env)->ReleaseStringUTFChars(env, jni_address, address);
434 JNIEXPORT void JNICALL
435 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback(
436 JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
438 OIC_LOG_V(DEBUG, TAG, "caLeGattServerNWConnectionStateChangeCallback - status %d, newstate %d",
440 VERIFY_NON_NULL_VOID(env, TAG, "env");
441 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
442 VERIFY_NON_NULL_VOID(device, TAG, "device");
444 if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
446 if (g_bleConnectionStateChangedCallback)
448 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
449 if (!jni_remoteAddress)
451 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
455 const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
458 OIC_LOG(ERROR, TAG, "address is null");
462 // STATE_DISCONNECTED
463 jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
464 "STATE_DISCONNECTED");
467 jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
470 if (state_disconnected == newState)
472 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
473 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
475 else if (state_connected == newState)
477 OIC_LOG_V(DEBUG, TAG, "LE Connected state is %d, %s", newState, address);
478 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
482 OIC_LOG_V(DEBUG, TAG, "Unknown state : %d, %s", newState, address);
485 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address);
490 JNIEXPORT void JNICALL
491 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWServicesDiscoveredCallback(JNIEnv *env,
496 OIC_LOG_V(DEBUG, TAG, "caLeGattNWServicesDiscoveredCallback - status %d", status);
497 VERIFY_NON_NULL_VOID(env, TAG, "env");
498 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
499 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
501 if (GATT_SUCCESS == status)
503 jstring jni_address = CALEGetAddressFromGatt(env, gatt);
506 OIC_LOG(ERROR, TAG, "jni_address is null");
510 if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_FALSE))
512 OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
515 (*env)->DeleteLocalRef(env, jni_address);
519 JNIEXPORT void JNICALL
520 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWDescriptorWriteCallback(JNIEnv *env,
525 OIC_LOG_V(DEBUG, TAG, "caLeGattNWDescriptorWriteCallback - status %d", status);
526 VERIFY_NON_NULL_VOID(env, TAG, "env");
527 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
528 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
530 if (GATT_SUCCESS == status)
532 jstring jni_address = CALEGetAddressFromGatt(env, gatt);
535 OIC_LOG(ERROR, TAG, "jni_address is null");
539 if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_TRUE))
541 OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
544 (*env)->DeleteLocalRef(env, jni_address);