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("CA_LE_MONITOR")
38 #define BT_STATE_ON (12)
39 #define BT_STATE_OFF (10)
44 * @var gCALEDeviceStateChangedCallback
45 * @brief Maintains the callback to be notified on device state changed.
47 static CALEDeviceStateChangedCallback gCALEDeviceStateChangedCallback = NULL;
50 * @var gCALEDeviceStateChangedCbMutex
51 * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
52 * of the LE adapter gets change.
54 static ca_mutex gCALEDeviceStateChangedCbMutex = NULL;
57 void CALENetworkMonitorJNISetContext()
59 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJNISetContext - it is not supported");
63 void CALENetworkMonitorJniInit()
65 OIC_LOG(DEBUG, TAG, "CALENetworkMonitorJniInit");
66 g_jvm = CANativeJNIGetJavaVM();
69 void CALESetNetStateCallback(CALEDeviceStateChangedCallback callback)
71 OIC_LOG(DEBUG, TAG, "CALESetNetStateCallback");
72 gCALEDeviceStateChangedCallback = callback;
75 CAResult_t CAInitializeLEAdapter()
77 OIC_LOG(DEBUG, TAG, "IN");
79 OIC_LOG(DEBUG, TAG, "OUT");
83 CAResult_t CAStartLEAdapter()
90 CAResult_t CAStopLEAdapter()
97 CAResult_t CAInitLENwkMonitorMutexVaraibles()
99 OIC_LOG(DEBUG, TAG, "IN");
100 if (NULL == gCALEDeviceStateChangedCbMutex)
102 gCALEDeviceStateChangedCbMutex = ca_mutex_new();
103 if (NULL == gCALEDeviceStateChangedCbMutex)
105 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
106 return CA_STATUS_FAILED;
110 OIC_LOG(DEBUG, TAG, "OUT");
115 void CATerminateLENwkMonitorMutexVaraibles()
117 OIC_LOG(DEBUG, TAG, "IN");
119 ca_mutex_free(gCALEDeviceStateChangedCbMutex);
120 gCALEDeviceStateChangedCbMutex = NULL;
122 OIC_LOG(DEBUG, TAG, "OUT");
125 CAResult_t CAGetLEAdapterState()
127 OIC_LOG(DEBUG, TAG, "IN");
131 OIC_LOG(ERROR, TAG, "g_jvm is null");
132 return CA_STATUS_FAILED;
135 bool isAttached = false;
137 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
140 OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
141 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
145 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
146 return CA_STATUS_FAILED;
151 if (!CALEIsEnableBTAdapter(env))
153 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
156 (*g_jvm)->DetachCurrentThread(g_jvm);
158 return CA_ADAPTER_NOT_ENABLED;
163 (*g_jvm)->DetachCurrentThread(g_jvm);
166 OIC_LOG(DEBUG, TAG, "OUT");
170 CAResult_t CAInitializeLENetworkMonitor()
172 OIC_LOG(DEBUG, TAG, "IN");
174 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
175 if (CA_STATUS_OK != res)
177 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
178 return CA_STATUS_FAILED;
181 CALENetworkMonitorJNISetContext();
182 CALENetworkMonitorJniInit();
184 OIC_LOG(DEBUG, TAG, "OUT");
190 void CATerminateLENetworkMonitor()
192 OIC_LOG(DEBUG, TAG, "IN");
194 CATerminateLENwkMonitorMutexVaraibles();
196 OIC_LOG(DEBUG, TAG, "OUT");
199 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
201 OIC_LOG(DEBUG, TAG, "IN");
203 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
205 ca_mutex_lock(gCALEDeviceStateChangedCbMutex);
206 CALESetNetStateCallback(callback);
207 ca_mutex_unlock(gCALEDeviceStateChangedCbMutex);
209 OIC_LOG(DEBUG, TAG, "OUT");
213 CAResult_t CAUnSetLEAdapterStateChangedCb()
215 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
219 JNIEXPORT void JNICALL
220 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
223 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
224 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
226 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Network State Changed");
228 if (!gCALEDeviceStateChangedCallback)
230 OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null");
234 if (BT_STATE_ON == status) // STATE_ON:12
236 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
237 CALEClientCreateDeviceList();
238 CALEServerCreateCachedDeviceList();
240 CAResult_t res = CALEClientStartScan();
241 if (CA_STATUS_OK != res)
243 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
246 res = CALEStartAdvertise();
247 if (CA_STATUS_OK != res)
249 OIC_LOG(ERROR, TAG, "CALEStartAdvertise has failed");
252 gCALEDeviceStateChangedCallback(newStatus);
254 else if (BT_STATE_OFF == status) // STATE_OFF:10
256 // remove obj for client
257 CAResult_t res = CALEClientRemoveAllGattObjs(env);
258 if (CA_STATUS_OK != res)
260 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
263 res = CALEClientRemoveAllScanDevices(env);
264 if (CA_STATUS_OK != res)
266 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
269 res = CALEClientRemoveAllDeviceState();
270 if (CA_STATUS_OK != res)
272 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
275 // remove obej for server
276 res = CALEServerRemoveAllDevices(env);
277 if (CA_STATUS_OK != res)
279 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
282 CALEClientSetScanFlag(false);
284 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
285 gCALEDeviceStateChangedCallback(newStatus);
289 JNIEXPORT void JNICALL
290 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
293 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
294 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
295 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
296 VERIFY_NON_NULL_VOID(addr, TAG, "addr is null");
298 // remove obj for client
299 CAResult_t 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");