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")
42 * @var g_bleDeviceStateChangedCallback
43 * @brief Maintains the callback to be notified on device state changed.
45 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
48 * @var g_bleConnectionStateChangedCallback
49 * @brief Maintains the callback to be notified on device state changed.
51 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
54 * @var g_bleDeviceStateChangedCbMutex
55 * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
56 * of the LE adapter gets change.
58 static ca_mutex g_bleDeviceStateChangedCbMutex = NULL;
61 * @var g_bleConnectionStateChangedCbMutex
62 * @brief Mutex to synchronize access to the LE ConnectionStateChanged Callback when the state
63 * of the LE adapter gets change.
65 static ca_mutex g_bleConnectionStateChangedCbMutex = NULL;
68 void CALENetworkMonitorJNISetContext()
70 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJNISetContext - it is not supported");
74 void CALENetworkMonitorJniInit()
76 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJniInit");
77 g_jvm = CANativeJNIGetJavaVM();
80 void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback)
82 OIC_LOG(DEBUG, TAG, "CALESetAdapterStateCallback");
83 g_bleDeviceStateChangedCallback = callback;
86 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
88 OIC_LOG(DEBUG, TAG, "IN");
90 OIC_LOG(DEBUG, TAG, "OUT");
94 CAResult_t CAStartLEAdapter()
101 CAResult_t CAStopLEAdapter()
108 CAResult_t CAInitLENwkMonitorMutexVaraibles()
110 OIC_LOG(DEBUG, TAG, "IN");
111 if (NULL == g_bleDeviceStateChangedCbMutex)
113 g_bleDeviceStateChangedCbMutex = ca_mutex_new();
114 if (NULL == g_bleDeviceStateChangedCbMutex)
116 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
117 return CA_STATUS_FAILED;
121 if (NULL == g_bleConnectionStateChangedCbMutex)
123 g_bleConnectionStateChangedCbMutex = ca_mutex_new();
124 if (NULL == g_bleConnectionStateChangedCbMutex)
126 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
127 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
128 return CA_STATUS_FAILED;
132 OIC_LOG(DEBUG, TAG, "OUT");
136 void CATerminateLENwkMonitorMutexVaraibles()
138 OIC_LOG(DEBUG, TAG, "IN");
140 ca_mutex_free(g_bleDeviceStateChangedCbMutex);
141 g_bleDeviceStateChangedCbMutex = NULL;
143 ca_mutex_free(g_bleConnectionStateChangedCbMutex);
144 g_bleConnectionStateChangedCbMutex = NULL;
146 OIC_LOG(DEBUG, TAG, "OUT");
149 CAResult_t CAGetLEAdapterState()
151 OIC_LOG(DEBUG, TAG, "IN");
155 OIC_LOG(ERROR, TAG, "g_jvm is null");
156 return CA_STATUS_FAILED;
159 bool isAttached = false;
161 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
164 OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
165 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
169 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
170 return CA_STATUS_FAILED;
175 if (!CALEIsEnableBTAdapter(env))
177 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
180 (*g_jvm)->DetachCurrentThread(g_jvm);
182 return CA_ADAPTER_NOT_ENABLED;
187 (*g_jvm)->DetachCurrentThread(g_jvm);
190 OIC_LOG(DEBUG, TAG, "OUT");
194 CAResult_t CAInitializeLENetworkMonitor()
196 OIC_LOG(DEBUG, TAG, "IN");
198 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
199 if (CA_STATUS_OK != res)
201 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
202 return CA_STATUS_FAILED;
205 CALENetworkMonitorJNISetContext();
206 CALENetworkMonitorJniInit();
208 OIC_LOG(DEBUG, TAG, "OUT");
214 void CATerminateLENetworkMonitor()
216 OIC_LOG(DEBUG, TAG, "IN");
218 CATerminateLENwkMonitorMutexVaraibles();
220 OIC_LOG(DEBUG, TAG, "OUT");
223 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
225 OIC_LOG(DEBUG, TAG, "IN");
227 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
229 ca_mutex_lock(g_bleDeviceStateChangedCbMutex);
230 CALESetAdapterStateCallback(callback);
231 ca_mutex_unlock(g_bleDeviceStateChangedCbMutex);
233 OIC_LOG(DEBUG, TAG, "OUT");
237 CAResult_t CAUnSetLEAdapterStateChangedCb()
239 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
243 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
245 OIC_LOG(DEBUG, TAG, "IN");
246 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
247 g_bleConnectionStateChangedCallback = callback;
248 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
249 OIC_LOG(DEBUG, TAG, "OUT");
253 CAResult_t CAUnsetLENWConnectionStateChangedCb()
255 OIC_LOG(DEBUG, TAG, "IN");
256 ca_mutex_lock(g_bleConnectionStateChangedCbMutex);
257 g_bleConnectionStateChangedCallback = NULL;
258 ca_mutex_unlock(g_bleConnectionStateChangedCbMutex);
259 OIC_LOG(DEBUG, TAG, "OUT");
263 JNIEXPORT void JNICALL
264 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
267 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
268 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
270 OIC_LOG_V(DEBUG, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);
272 if (!g_bleDeviceStateChangedCallback)
274 OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
278 jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
279 jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
280 jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");
282 if (state_on == status) // STATE_ON:12
284 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
285 CALEClientCreateDeviceList();
286 CALEServerCreateCachedDeviceList();
288 g_bleDeviceStateChangedCallback(newStatus);
290 else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
292 // gatt Device list will be removed.
293 // so it is need to create list again when adapter is enabled.
294 CAStopLEGattClient();
296 else if (state_off == status) // STATE_OFF:10
298 CALEClientStopMulticastServer();
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");