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 oc_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 oc_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 if (NULL == g_bleDeviceStateChangedCbMutex)
113 g_bleDeviceStateChangedCbMutex = oc_mutex_new();
114 if (NULL == g_bleDeviceStateChangedCbMutex)
116 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
117 return CA_STATUS_FAILED;
121 if (NULL == g_bleConnectionStateChangedCbMutex)
123 g_bleConnectionStateChangedCbMutex = oc_mutex_new();
124 if (NULL == g_bleConnectionStateChangedCbMutex)
126 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
127 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
128 return CA_STATUS_FAILED;
135 void CATerminateLENwkMonitorMutexVaraibles()
137 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
138 g_bleDeviceStateChangedCbMutex = NULL;
140 oc_mutex_free(g_bleConnectionStateChangedCbMutex);
141 g_bleConnectionStateChangedCbMutex = NULL;
144 CAResult_t CAGetLEAdapterState()
148 OIC_LOG(ERROR, TAG, "g_jvm is null");
149 return CA_STATUS_FAILED;
152 bool isAttached = false;
154 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
157 OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
158 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
162 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
163 return CA_STATUS_FAILED;
168 if (!CALEIsEnableBTAdapter(env))
170 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
173 (*g_jvm)->DetachCurrentThread(g_jvm);
175 return CA_ADAPTER_NOT_ENABLED;
180 (*g_jvm)->DetachCurrentThread(g_jvm);
186 CAResult_t CAInitializeLENetworkMonitor()
188 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
189 if (CA_STATUS_OK != res)
191 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
192 return CA_STATUS_FAILED;
195 CALENetworkMonitorJNISetContext();
196 CALENetworkMonitorJniInit();
201 void CATerminateLENetworkMonitor()
203 OIC_LOG(DEBUG, TAG, "CATerminateLENetworkMonitor");
205 CATerminateLENwkMonitorMutexVaraibles();
208 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
210 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
212 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
213 CALESetAdapterStateCallback(callback);
214 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
219 CAResult_t CAUnSetLEAdapterStateChangedCb()
221 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
225 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
227 OIC_LOG(DEBUG, TAG, "CASetLENWConnectionStateChangedCb");
228 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
229 g_bleConnectionStateChangedCallback = callback;
230 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
234 CAResult_t CAUnsetLENWConnectionStateChangedCb()
236 OIC_LOG(DEBUG, TAG, "CAUnsetLENWConnectionStateChangedCb");
237 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
238 g_bleConnectionStateChangedCallback = NULL;
239 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
243 static CAResult_t CALEStateConnectedCallback(JNIEnv *env, jstring jni_address,
244 jboolean isDescriptorFound)
246 VERIFY_NON_NULL(env, TAG, "env");
247 VERIFY_NON_NULL(jni_address, TAG, "jni_address");
249 if (CALEClientGetFlagFromState(env, jni_address, CA_LE_DESCRIPTOR_FOUND) == isDescriptorFound)
251 if (g_bleConnectionStateChangedCallback)
253 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
256 OIC_LOG(ERROR, TAG, "address is null");
257 CACheckJNIException(env);
258 return CA_STATUS_FAILED;
261 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
262 OIC_LOG(DEBUG, TAG, "BLE is connected");
264 (*env)->ReleaseStringUTFChars(env, jni_address, address);
271 JNIEXPORT void JNICALL
272 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
275 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
276 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
278 OIC_LOG_V(INFO, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);
280 if (!g_bleDeviceStateChangedCallback)
282 OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
286 jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
287 jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
288 jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");
290 if (state_on == status) // STATE_ON:12
292 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
293 CALEClientCreateDeviceList();
295 if (!(caglobals.bleFlags & CA_LE_SERVER_DISABLE))
297 CALEServerCreateCachedDeviceList();
300 g_bleDeviceStateChangedCallback(newStatus);
302 else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
304 // gatt Device list will be removed.
305 // so it is need to create list again when adapter is enabled.
306 CAStopLEGattClient();
308 else if (state_off == status) // STATE_OFF:10
310 // remove obj for client
311 CAResult_t res = CALEClientRemoveAllGattObjs(env);
312 if (CA_STATUS_OK != res)
314 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
317 res = CALEClientResetDeviceStateForAll();
318 if (CA_STATUS_OK != res)
320 OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
323 // remove obj for server
324 res = CALEServerRemoveAllDevices(env);
325 if (CA_STATUS_OK != res)
327 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
330 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
331 g_bleDeviceStateChangedCallback(newStatus);
335 JNIEXPORT void JNICALL
336 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
339 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
340 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
341 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
342 VERIFY_NON_NULL_VOID(jaddr, TAG, "jaddr is null");
344 // geneally 'addr' parameter will be not ble address, if you didn't bond for BLE.
345 // below logics will be needed when ble pairing is set.
347 CAResult_t res = CALEClientDisconnectforAddress(env, jaddr);
348 if (CA_STATUS_OK != res)
350 OIC_LOG(ERROR, TAG, "CALEClientDisconnectforAddress has failed");
353 // remove obj for client
354 res = CALEClientRemoveGattObjForAddr(env, jaddr);
355 if (CA_STATUS_OK != res)
357 OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
360 res = CALEClientRemoveDeviceInScanDeviceList(env, jaddr);
361 if (CA_STATUS_OK != res)
363 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
366 // remove obej for server
367 res = CALEServerRemoveDevice(env, jaddr);
368 if (CA_STATUS_OK != res)
370 OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
374 JNIEXPORT void JNICALL
375 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWConnectionStateChangeCallback(JNIEnv *env,
381 OIC_LOG_V(INFO, TAG, "CALeGattNWConnectionStateChangeCallback - status %d, newstate %d",
383 VERIFY_NON_NULL_VOID(env, TAG, "env");
384 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
385 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
387 jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
388 "STATE_DISCONNECTED");
389 if (state_disconnected == newState)
391 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
394 OIC_LOG(ERROR, TAG, "jni_address is null");
398 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
401 OIC_LOG(ERROR, TAG, "address is null");
402 CACheckJNIException(env);
406 if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
408 if (g_bleConnectionStateChangedCallback)
410 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %s", address);
411 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
415 (*env)->ReleaseStringUTFChars(env, jni_address, address);
419 JNIEXPORT void JNICALL
420 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback(
421 JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
423 OIC_LOG_V(DEBUG, TAG, "caLeGattServerNWConnectionStateChangeCallback - status %d, newstate %d",
425 VERIFY_NON_NULL_VOID(env, TAG, "env");
426 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
427 VERIFY_NON_NULL_VOID(device, TAG, "device");
429 if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
431 if (g_bleConnectionStateChangedCallback)
433 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
434 if (!jni_remoteAddress)
436 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
440 const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
443 OIC_LOG(ERROR, TAG, "address is null");
444 CACheckJNIException(env);
448 // STATE_DISCONNECTED
449 jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
450 "STATE_DISCONNECTED");
453 jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
456 if (state_disconnected == newState)
458 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
459 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
461 else if (state_connected == newState)
463 OIC_LOG_V(DEBUG, TAG, "LE Connected state is %d, %s", newState, address);
464 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
468 OIC_LOG_V(DEBUG, TAG, "Unknown state : %d, %s", newState, address);
471 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address);
476 JNIEXPORT void JNICALL
477 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWServicesDiscoveredCallback(JNIEnv *env,
482 OIC_LOG_V(INFO, TAG, "caLeGattNWServicesDiscoveredCallback - status %d", status);
483 VERIFY_NON_NULL_VOID(env, TAG, "env");
484 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
485 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
487 if (GATT_SUCCESS == status)
489 jstring jni_address = CALEGetAddressFromGatt(env, gatt);
492 OIC_LOG(ERROR, TAG, "jni_address is null");
496 if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_FALSE))
498 OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
501 (*env)->DeleteLocalRef(env, jni_address);
505 JNIEXPORT void JNICALL
506 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWDescriptorWriteCallback(JNIEnv *env,
511 OIC_LOG_V(INFO, TAG, "caLeGattNWDescriptorWriteCallback - status %d", status);
512 VERIFY_NON_NULL_VOID(env, TAG, "env");
513 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
514 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
516 if (GATT_SUCCESS == status)
518 jstring jni_address = CALEGetAddressFromGatt(env, gatt);
521 OIC_LOG(ERROR, TAG, "jni_address is null");
525 if (CA_STATUS_OK != CALEStateConnectedCallback(env, jni_address, JNI_TRUE))
527 OIC_LOG(ERROR, TAG, "CALEStateConnectedCallback has failed");
530 (*env)->DeleteLocalRef(env, jni_address);