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 CALENetworkMonitorJNISetContext();
80 CALENetworkMonitorJniInit();
82 OIC_LOG(DEBUG, TAG, "OUT");
86 CAResult_t CAStartLEAdapter()
93 CAResult_t CAInitLENwkMonitorMutexVaraibles()
95 OIC_LOG(DEBUG, TAG, "IN");
96 if (NULL == gCALEDeviceStateChangedCbMutex)
98 gCALEDeviceStateChangedCbMutex = ca_mutex_new();
99 if (NULL == gCALEDeviceStateChangedCbMutex)
101 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
102 return CA_STATUS_FAILED;
106 OIC_LOG(DEBUG, TAG, "OUT");
111 void CATerminateLENwkMonitorMutexVaraibles()
113 OIC_LOG(DEBUG, TAG, "IN");
115 ca_mutex_free(gCALEDeviceStateChangedCbMutex);
116 gCALEDeviceStateChangedCbMutex = NULL;
118 OIC_LOG(DEBUG, TAG, "OUT");
121 CAResult_t CAGetLEAdapterState()
123 OIC_LOG(DEBUG, TAG, "IN");
127 OIC_LOG(ERROR, TAG, "g_jvm is null");
128 return CA_STATUS_FAILED;
131 bool isAttached = false;
133 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
136 OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
137 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
141 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
142 return CA_STATUS_FAILED;
147 if (!CALEIsEnableBTAdapter(env))
149 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
152 (*g_jvm)->DetachCurrentThread(g_jvm);
154 return CA_ADAPTER_NOT_ENABLED;
159 (*g_jvm)->DetachCurrentThread(g_jvm);
162 OIC_LOG(DEBUG, TAG, "OUT");
166 CAResult_t CAInitializeLENetworkMonitor()
168 OIC_LOG(DEBUG, TAG, "IN");
170 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
171 if (CA_STATUS_OK != res)
173 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
174 return CA_STATUS_FAILED;
177 OIC_LOG(DEBUG, TAG, "OUT");
183 void CATerminateLENetworkMonitor()
185 OIC_LOG(DEBUG, TAG, "IN");
187 CATerminateLENwkMonitorMutexVaraibles();
189 OIC_LOG(DEBUG, TAG, "OUT");
192 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
194 OIC_LOG(DEBUG, TAG, "IN");
196 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
198 ca_mutex_lock(gCALEDeviceStateChangedCbMutex);
199 CALESetNetStateCallback(callback);
200 ca_mutex_unlock(gCALEDeviceStateChangedCbMutex);
202 OIC_LOG(DEBUG, TAG, "OUT");
206 CAResult_t CAUnSetLEAdapterStateChangedCb()
208 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
212 JNIEXPORT void JNICALL
213 Java_org_iotivity_ca_CaLeClientInterface_caLeStateChangedCallback(JNIEnv *env, jobject obj,
216 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
219 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Network State Changed");
221 if (!gCALEDeviceStateChangedCallback)
223 OIC_LOG_V(ERROR, TAG, "gNetworkChangeCb is null", status);
226 if (BT_STATE_ON == status) // STATE_ON:12
228 CANetworkStatus_t newStatus = CA_INTERFACE_UP;
229 gCALEDeviceStateChangedCallback(newStatus);
231 else if (BT_STATE_OFF == status) // STATE_OFF:10
233 // remove obj for client
234 CAResult_t res = CALEClientRemoveAllGattObjs(env);
235 if (CA_STATUS_OK != res)
237 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
240 res = CALEClientRemoveAllScanDevices(env);
241 if (CA_STATUS_OK != res)
243 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
246 // remove obej for server
247 res = CALEServerRemoveAllDevices(env);
248 if (CA_STATUS_OK != res)
250 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
253 CANetworkStatus_t newStatus = CA_INTERFACE_DOWN;
254 gCALEDeviceStateChangedCallback(newStatus);
258 JNIEXPORT void JNICALL
259 Java_org_iotivity_ca_CaLeClientInterface_caLeBondStateChangedCallback(JNIEnv *env, jobject obj,
262 OIC_LOG(DEBUG, TAG, "CaLeClientInterface - Bond State Changed");
263 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
264 VERIFY_NON_NULL_VOID(addr, TAG, "addr is null");
266 // remove obj for client
267 CAResult_t res = CALEClientRemoveGattObjForAddr(env, addr);
268 if (CA_STATUS_OK != res)
270 OIC_LOG(ERROR, TAG, "CANativeRemoveGattObjForAddr has failed");
273 res = CALEClientRemoveDeviceInScanDeviceList(env, addr);
274 if (CA_STATUS_OK != res)
276 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceInScanDeviceList has failed");
279 // remove obej for server
280 res = CALEServerRemoveDevice(env, addr);
281 if (CA_STATUS_OK != res)
283 OIC_LOG(ERROR, TAG, "CALEServerRemoveDevice has failed");