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")
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(const ca_thread_pool_t threadPool)
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 gCALEDeviceStateChangedCallback(newStatus);
242 else if (BT_STATE_OFF == status) // STATE_OFF:10
244 CALEClientStopMulticastServer();
246 // remove obj for client
247 CAResult_t res = CALEClientRemoveAllGattObjs(env);
248 if (CA_STATUS_OK != res)
250 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
253 res = CALEClientRemoveAllScanDevices(env);
254 if (CA_STATUS_OK != res)
256 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
259 res = CALEClientRemoveAllDeviceState();
260 if (CA_STATUS_OK != res)
262 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
265 // remove obej for server
266 res = CALEServerRemoveAllDevices(env);
267 if (CA_STATUS_OK != res)
269 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
272 CALEClientSetScanFlag(false);
274 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
275 gCALEDeviceStateChangedCallback(newStatus);
279 JNIEXPORT void JNICALL
280 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
283 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
284 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
285 VERIFY_NON_NULL_VOID(obj, TAG, "obj is null");
286 VERIFY_NON_NULL_VOID(addr, TAG, "addr is null");
288 // remove obj for client
289 CAResult_t res = CALEClientRemoveGattObjForAddr(env, addr);
290 if (CA_STATUS_OK != res)
292 OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
295 res = CALEClientRemoveDeviceInScanDeviceList(env, addr);
296 if (CA_STATUS_OK != res)
298 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
301 // remove obej for server
302 res = CALEServerRemoveDevice(env, addr);
303 if (CA_STATUS_OK != res)
305 OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");