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"
36 #define TAG PCF("OIC_CA_LE_MONITOR")
41 * @var gCALEDeviceStateChangedCallback
42 * @brief Maintains the callback to be notified on device state changed.
44 static CALEDeviceStateChangedCallback gCALEDeviceStateChangedCallback = NULL;
47 * @var gCALEDeviceStateChangedCbMutex
48 * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
49 * of the LE adapter gets change.
51 static ca_mutex gCALEDeviceStateChangedCbMutex = NULL;
54 void CALENetworkMonitorJNISetContext()
56 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJNISetContext - it is not supported");
60 void CALENetworkMonitorJniInit()
62 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJniInit");
63 g_jvm = CANativeJNIGetJavaVM();
66 void CALESetNetStateCallback(CALEDeviceStateChangedCallback callback)
68 OIC_LOG(DEBUG, TAG, "CALESetNetStateCallback");
69 gCALEDeviceStateChangedCallback = callback;
72 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
74 OIC_LOG(DEBUG, TAG, "IN");
76 OIC_LOG(DEBUG, TAG, "OUT");
80 CAResult_t CAStartLEAdapter()
87 CAResult_t CAStopLEAdapter()
94 CAResult_t CAInitLENwkMonitorMutexVaraibles()
96 OIC_LOG(DEBUG, TAG, "IN");
97 if (NULL == gCALEDeviceStateChangedCbMutex)
99 gCALEDeviceStateChangedCbMutex = ca_mutex_new();
100 if (NULL == gCALEDeviceStateChangedCbMutex)
102 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
103 return CA_STATUS_FAILED;
107 OIC_LOG(DEBUG, TAG, "OUT");
112 void CATerminateLENwkMonitorMutexVaraibles()
114 OIC_LOG(DEBUG, TAG, "IN");
116 ca_mutex_free(gCALEDeviceStateChangedCbMutex);
117 gCALEDeviceStateChangedCbMutex = NULL;
119 OIC_LOG(DEBUG, TAG, "OUT");
122 CAResult_t CAGetLEAdapterState()
124 OIC_LOG(DEBUG, TAG, "IN");
128 OIC_LOG(ERROR, TAG, "g_jvm is null");
129 return CA_STATUS_FAILED;
132 bool isAttached = false;
134 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
137 OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
138 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
142 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
143 return CA_STATUS_FAILED;
148 if (!CALEIsEnableBTAdapter(env))
150 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
153 (*g_jvm)->DetachCurrentThread(g_jvm);
155 return CA_ADAPTER_NOT_ENABLED;
160 (*g_jvm)->DetachCurrentThread(g_jvm);
163 OIC_LOG(DEBUG, TAG, "OUT");
167 CAResult_t CAInitializeLENetworkMonitor()
169 OIC_LOG(DEBUG, TAG, "IN");
171 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
172 if (CA_STATUS_OK != res)
174 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
175 return CA_STATUS_FAILED;
178 CALENetworkMonitorJNISetContext();
179 CALENetworkMonitorJniInit();
181 OIC_LOG(DEBUG, TAG, "OUT");
187 void CATerminateLENetworkMonitor()
189 OIC_LOG(DEBUG, TAG, "IN");
191 CATerminateLENwkMonitorMutexVaraibles();
193 OIC_LOG(DEBUG, TAG, "OUT");
196 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
198 OIC_LOG(DEBUG, TAG, "IN");
200 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
202 ca_mutex_lock(gCALEDeviceStateChangedCbMutex);
203 CALESetNetStateCallback(callback);
204 ca_mutex_unlock(gCALEDeviceStateChangedCbMutex);
206 OIC_LOG(DEBUG, TAG, "OUT");
210 CAResult_t CAUnSetLEAdapterStateChangedCb()
212 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
216 JNIEXPORT void JNICALL
217 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
220 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
221 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
223 OIC_LOG_V(DEBUG, TAG, "CaLeClientInterface - Network State Changed : status(%d)", status);
225 if (!gCALEDeviceStateChangedCallback)
227 OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
231 jint state_on = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_ON");
232 jint state_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_OFF");
233 jint state_turning_off = CALEGetConstantsValue(env, CLASSPATH_BT_ADAPTER, "STATE_TURNING_OFF");
235 if (state_on == status) // STATE_ON:12
237 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
238 CALEClientCreateDeviceList();
239 CALEServerCreateCachedDeviceList();
241 gCALEDeviceStateChangedCallback(newStatus);
243 else if (state_turning_off == status) // BT_STATE_TURNING_OFF:13
245 // gatt Device list will be removed.
246 // so it is need to create list again when adapter is enabled.
247 CAStopLEGattClient();
249 else if (state_off == status) // STATE_OFF:10
251 CALEClientStopMulticastServer();
253 // remove obj for client
254 CAResult_t res = CALEClientRemoveAllGattObjs(env);
255 if (CA_STATUS_OK != res)
257 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
260 res = CALEClientRemoveAllDeviceState();
261 if (CA_STATUS_OK != res)
263 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
266 // remove obj for server
267 res = CALEServerRemoveAllDevices(env);
268 if (CA_STATUS_OK != res)
270 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
273 CALEClientSetScanFlag(false);
275 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
276 gCALEDeviceStateChangedCallback(newStatus);
280 JNIEXPORT void JNICALL
281 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
284 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
285 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
286 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
287 VERIFY_NON_NULL_VOID(addr, TAG, "addr is null");
289 // geneally 'addr' parameter will be not ble address, if you didn't bond for BLE.
290 // below logics will be needed when ble pairing is set.
292 CAResult_t res = CALEClientDisconnectforAddress(env, addr);
293 if (CA_STATUS_OK != res)
295 OIC_LOG(ERROR, TAG, "CALEClientDisconnectforAddress has failed");
298 // remove obj for client
299 res = CALEClientRemoveGattObjForAddr(env, addr);
300 if (CA_STATUS_OK != res)
302 OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
305 res = CALEClientRemoveDeviceInScanDeviceList(env, addr);
306 if (CA_STATUS_OK != res)
308 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
311 // remove obej for server
312 res = CALEServerRemoveDevice(env, addr);
313 if (CA_STATUS_OK != res)
315 OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");