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(const ca_thread_pool_t threadPool)
90 OIC_LOG(DEBUG, TAG, "IN");
92 OIC_LOG(DEBUG, TAG, "OUT");
96 CAResult_t CAStartLEAdapter()
103 CAResult_t CAStopLEAdapter()
110 CAResult_t CAInitLENwkMonitorMutexVaraibles()
112 OIC_LOG(DEBUG, TAG, "IN");
113 if (NULL == g_bleDeviceStateChangedCbMutex)
115 g_bleDeviceStateChangedCbMutex = ca_mutex_new();
116 if (NULL == g_bleDeviceStateChangedCbMutex)
118 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
119 return CA_STATUS_FAILED;
123 if (NULL == g_bleConnectionStateChangedCbMutex)
125 g_bleConnectionStateChangedCbMutex = ca_mutex_new();
126 if (NULL == g_bleConnectionStateChangedCbMutex)
128 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
129 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
130 return CA_STATUS_FAILED;
134 OIC_LOG(DEBUG, TAG, "OUT");
138 void CATerminateLENwkMonitorMutexVaraibles()
140 OIC_LOG(DEBUG, TAG, "IN");
142 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
143 g_bleDeviceStateChangedCbMutex = NULL;
145 ca_mutex_free(g_bleConnectionStateChangedCbMutex);
146 g_bleConnectionStateChangedCbMutex = NULL;
148 OIC_LOG(DEBUG, TAG, "OUT");
151 CAResult_t CAGetLEAdapterState()
153 OIC_LOG(DEBUG, TAG, "IN");
157 OIC_LOG(ERROR, TAG, "g_jvm is null");
158 return CA_STATUS_FAILED;
161 bool isAttached = false;
163 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
166 OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
167 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
171 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
172 return CA_STATUS_FAILED;
177 if (!CALEIsEnableBTAdapter(env))
179 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
182 (*g_jvm)->DetachCurrentThread(g_jvm);
184 return CA_ADAPTER_NOT_ENABLED;
189 (*g_jvm)->DetachCurrentThread(g_jvm);
192 OIC_LOG(DEBUG, TAG, "OUT");
196 CAResult_t CAInitializeLENetworkMonitor()
198 OIC_LOG(DEBUG, TAG, "IN");
200 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
201 if (CA_STATUS_OK != res)
203 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
204 return CA_STATUS_FAILED;
207 CALENetworkMonitorJNISetContext();
208 CALENetworkMonitorJniInit();
210 OIC_LOG(DEBUG, TAG, "OUT");
216 void CATerminateLENetworkMonitor()
218 OIC_LOG(DEBUG, TAG, "IN");
220 CATerminateLENwkMonitorMutexVaraibles();
222 OIC_LOG(DEBUG, TAG, "OUT");
225 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
227 OIC_LOG(DEBUG, TAG, "IN");
229 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
231 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
232 CALESetAdapterStateCallback(callback);
233 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
235 OIC_LOG(DEBUG, TAG, "OUT");
239 CAResult_t CAUnSetLEAdapterStateChangedCb()
241 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
245 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
247 OIC_LOG(DEBUG, TAG, "IN");
248 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
249 g_bleConnectionStateChangedCallback = callback;
250 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
251 OIC_LOG(DEBUG, TAG, "OUT");
255 CAResult_t CAUnsetLENWConnectionStateChangedCb()
257 OIC_LOG(DEBUG, TAG, "IN");
258 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
259 g_bleConnectionStateChangedCallback = NULL;
260 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
261 OIC_LOG(DEBUG, TAG, "OUT");
265 JNIEXPORT void JNICALL
266 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
269 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
270 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
272 OIC_LOG_V(DEBUG, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);
274 if (!g_bleDeviceStateChangedCallback)
276 OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
280 jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
281 jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
282 jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");
284 if (state_on == status) // STATE_ON:12
286 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
287 CALEClientCreateDeviceList();
288 CALEServerCreateCachedDeviceList();
290 g_bleDeviceStateChangedCallback(newStatus);
292 else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
294 // gatt Device list will be removed.
295 // so it is need to create list again when adapter is enabled.
296 CAStopLEGattClient();
298 else if (state_off == status) // STATE_OFF:10
300 // remove obj for client
301 CAResult_t res = CALEClientRemoveAllGattObjs(env);
302 if (CA_STATUS_OK != res)
304 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
307 res = CALEClientResetDeviceStateForAll();
308 if (CA_STATUS_OK != res)
310 OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
313 // remove obj for server
314 res = CALEServerRemoveAllDevices(env);
315 if (CA_STATUS_OK != res)
317 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
320 CALEClientSetScanFlag(false);
322 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
323 g_bleDeviceStateChangedCallback(newStatus);
327 JNIEXPORT void JNICALL
328 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
331 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
332 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
333 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
334 VERIFY_NON_NULL_VOID(jaddr, TAG, "jaddr is null");
336 // geneally 'addr' parameter will be not ble address, if you didn't bond for BLE.
337 // below logics will be needed when ble pairing is set.
339 CAResult_t res = CALEClientDisconnectforAddress(env, jaddr);
340 if (CA_STATUS_OK != res)
342 OIC_LOG(ERROR, TAG, "CALEClientDisconnectforAddress has failed");
345 // remove obj for client
346 res = CALEClientRemoveGattObjForAddr(env, jaddr);
347 if (CA_STATUS_OK != res)
349 OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
352 res = CALEClientRemoveDeviceInScanDeviceList(env, jaddr);
353 if (CA_STATUS_OK != res)
355 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
358 // remove obej for server
359 res = CALEServerRemoveDevice(env, jaddr);
360 if (CA_STATUS_OK != res)
362 OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");
366 JNIEXPORT void JNICALL
367 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWConnectionStateChangeCallback(JNIEnv *env,
373 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
374 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
375 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
377 OIC_LOG_V(DEBUG, TAG, "CALeGattNWConnectionStateChangeCallback - status %d, newstate %d",
380 jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
381 "STATE_DISCONNECTED");
382 if (state_disconnected == newState)
384 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
387 OIC_LOG(ERROR, TAG, "jni_address is null");
391 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
394 OIC_LOG(ERROR, TAG, "address is null");
398 if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
400 if (g_bleConnectionStateChangedCallback)
402 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
403 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
407 (*env)->ReleaseStringUTFChars(env, jni_address, address);
411 JNIEXPORT void JNICALL
412 Java_org_iotivity_ca_CaLeServerInterface_caLeGattServerNWConnectionStateChangeCallback(
413 JNIEnv *env, jobject obj, jobject device, jint status, jint newState)
415 OIC_LOG(DEBUG, TAG, " Gatt Server NWConnectionStateChange Callback");
417 VERIFY_NON_NULL_VOID(env, TAG, "env");
418 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
419 VERIFY_NON_NULL_VOID(device, TAG, "device");
422 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
423 if (!jni_remoteAddress)
425 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
429 const char* address = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
432 OIC_LOG(ERROR, TAG, "address is null");
436 // STATE_DISCONNECTED
437 jint state_disconnected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
438 "STATE_DISCONNECTED");
441 jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE,
444 if (CONNECTION_FAILED_TO_BE_EASTABLISHED != status)
446 if (g_bleConnectionStateChangedCallback)
448 if (state_disconnected == newState)
450 OIC_LOG_V(DEBUG, TAG, "LE Disconnected state is %d, %s", newState, address);
451 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
453 else if (state_connected == newState)
455 OIC_LOG_V(DEBUG, TAG, "LE Connected state is %d, %s", newState, address);
456 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
460 OIC_LOG_V(DEBUG, TAG, "Unknown state : %d, %s", newState, address);
464 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, address);
467 JNIEXPORT void JNICALL
468 Java_org_iotivity_ca_CaLeClientInterface_caLeGattNWServicesDiscoveredCallback(JNIEnv *env,
473 OIC_LOG_V(DEBUG, TAG, "caLeGattNWServicesDiscoveredCallback - status %d: ", status);
474 VERIFY_NON_NULL_VOID(env, TAG, "env");
475 VERIFY_NON_NULL_VOID(obj, TAG, "obj");
476 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt");
478 if (GATT_SUCCESS == status)
480 jstring jni_address = CALEGetAddressFromGatt(env, gatt);
483 OIC_LOG(ERROR, TAG, "CALEGetAddressFromGatt is null");
487 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
490 OIC_LOG(ERROR, TAG, "address is null");
491 (*env)->DeleteLocalRef(env, jni_address);
495 if (g_bleConnectionStateChangedCallback)
497 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, true);
498 OIC_LOG(DEBUG, TAG, "BLE is connected");
501 (*env)->ReleaseStringUTFChars(env, jni_address, address);