1 /******************************************************************
3 * Copyright 2017 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 "calenwmonitor.h"
24 #include "caleclient.h"
25 #include "caleserver.h"
26 #include "caleutils.h"
27 #include "caleinterface.h"
28 #include "caadapterutils.h"
32 #define TAG PCF("OIC_CA_LE_MONITOR")
34 static oc_cond g_deviceStateCbCond = NULL;
35 static oc_mutex g_deviceStateCbMutex = NULL;
38 * @var g_bleDeviceStateChangedCallback
39 * @brief Maintains the callback to be notified on device state changed.
41 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
44 * @var g_bleConnectionStateChangedCallback
45 * @brief Maintains the callback to be notified on device state changed.
47 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
50 * @var g_bleDeviceStateChangedCbMutex
51 * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
52 * of the LE adapter gets change.
54 static oc_mutex g_bleDeviceStateChangedCbMutex = NULL;
57 * @var g_bleConnectionStateChangedCbMutex
58 * @brief Mutex to synchronize access to the LE ConnectionStateChanged Callback when the state
59 * of the LE adapter gets change.
61 static oc_mutex g_bleConnectionStateChangedCbMutex = NULL;
63 void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback)
65 OIC_LOG(DEBUG, TAG, "CALESetAdapterStateCallback");
66 g_bleDeviceStateChangedCallback = callback;
68 oc_cond_signal(g_deviceStateCbCond);
71 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
73 OIC_LOG(DEBUG, TAG, "IN - CAInitializeLEAdapter");
75 OIC_LOG(DEBUG, TAG, "OUT - CAInitializeLEAdapter");
79 CAResult_t CAStartLEAdapter()
86 CAResult_t CAStopLEAdapter()
93 CAResult_t CAInitLENwkMonitorMutexVaraibles()
95 OIC_LOG(DEBUG, TAG, "IN - CAInitLENwkMonitorMutexVaraibles");
96 if (NULL == g_deviceStateCbMutex)
98 g_deviceStateCbMutex = oc_mutex_new();
99 if (NULL == g_deviceStateCbMutex)
101 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
102 return CA_STATUS_FAILED;
106 if (NULL == g_bleDeviceStateChangedCbMutex)
108 g_bleDeviceStateChangedCbMutex = oc_mutex_new();
109 if (NULL == g_bleDeviceStateChangedCbMutex)
111 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
112 return CA_STATUS_FAILED;
116 if (NULL == g_bleConnectionStateChangedCbMutex)
118 g_bleConnectionStateChangedCbMutex = oc_mutex_new();
119 if (NULL == g_bleConnectionStateChangedCbMutex)
121 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
122 oc_mutex_free(g_bleConnectionStateChangedCbMutex);
123 return CA_STATUS_FAILED;
127 OIC_LOG(DEBUG, TAG, "OUT - CAInitLENwkMonitorMutexVaraibles");
131 void CATerminateLENwkMonitorMutexVaraibles()
133 OIC_LOG(DEBUG, TAG, "IN - CATerminateLENwkMonitorMutexVaraibles");
135 oc_mutex_free(g_deviceStateCbMutex);
136 g_deviceStateCbMutex = NULL;
138 oc_mutex_free(g_bleDeviceStateChangedCbMutex);
139 g_bleDeviceStateChangedCbMutex = NULL;
141 oc_mutex_free(g_bleConnectionStateChangedCbMutex);
142 g_bleConnectionStateChangedCbMutex = NULL;
144 OIC_LOG(DEBUG, TAG, "OUT - CATerminateLENwkMonitorMutexVaraibles");
147 CAResult_t CAGetLEAdapterState()
149 OIC_LOG(DEBUG, TAG, "IN - CAGetLEAdapterState");
151 if (!CALEClientIsEnableBTAdapter() || !CALEServerIsEnableBTAdapter())
153 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
156 OIC_LOG(DEBUG, TAG, "OUT - CAGetLEAdapterState");
160 CAResult_t CAInitializeLENetworkMonitor()
162 OIC_LOG(DEBUG, TAG, "IN - CAInitializeLENetworkMonitor");
164 if (NULL == g_deviceStateCbCond){
165 g_deviceStateCbCond = oc_cond_new();
168 CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
169 if (CA_STATUS_OK != res)
171 OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
172 return CA_STATUS_FAILED;
175 OIC_LOG(DEBUG, TAG, "OUT- CAInitializeLENetworkMonitor");
181 void CATerminateLENetworkMonitor()
183 OIC_LOG(DEBUG, TAG, "IN");
185 oc_cond_free(g_deviceStateCbCond);
186 g_deviceStateCbCond = NULL;
188 CATerminateLENwkMonitorMutexVaraibles();
190 OIC_LOG(DEBUG, TAG, "OUT");
193 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
195 OIC_LOG(DEBUG, TAG, "IN");
197 OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
199 oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
200 CALESetAdapterStateCallback(callback);
201 oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
203 OIC_LOG(DEBUG, TAG, "OUT");
207 CAResult_t CAUnSetLEAdapterStateChangedCb()
209 OIC_LOG(DEBUG, TAG, "it is not required in this platform");
213 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
215 OIC_LOG(DEBUG, TAG, "IN");
216 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
217 g_bleConnectionStateChangedCallback = callback;
218 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
219 OIC_LOG(DEBUG, TAG, "OUT");
223 CAResult_t CAUnSetLENWConnectionStateChangedCb()
225 OIC_LOG(DEBUG, TAG, "IN");
226 oc_mutex_lock(g_bleConnectionStateChangedCbMutex);
227 g_bleConnectionStateChangedCallback = NULL;
228 oc_mutex_unlock(g_bleConnectionStateChangedCbMutex);
229 OIC_LOG(DEBUG, TAG, "OUT");
233 void CALEClientNWStateChangeCallback(CALE_STATE_t state){
234 OIC_LOG_V(DEBUG, TAG, "[Network State Changed]: state(%s)",
235 state == 1 ? "STATE_ON": (state == 0 ? "STATE_OFF" : "TURN_OFF"));
237 if (!g_bleDeviceStateChangedCallback)
239 OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null --> wait~");
240 if (NULL == g_deviceStateCbMutex){
241 g_deviceStateCbMutex = oc_mutex_new();
243 if (NULL == g_deviceStateCbCond){
244 g_deviceStateCbCond = oc_cond_new();
246 oc_mutex_lock(g_deviceStateCbMutex);
247 oc_cond_wait(g_deviceStateCbCond, g_deviceStateCbMutex);
248 oc_mutex_unlock(g_deviceStateCbMutex);
251 CAAdapterState_t newStatus = CA_ADAPTER_ENABLED;
252 CAResult_t res = CA_STATUS_OK;
255 newStatus = CA_ADAPTER_ENABLED;
256 CALEClientCreateDeviceList();
257 CALEServerCreateCachedDeviceList();
259 g_bleDeviceStateChangedCallback(newStatus);
261 case CALE_STATE_TURNING_OFF:
262 CAStopLEGattClient();
266 res = CALEClientRemoveAllGattObjs();
267 if (CA_STATUS_OK != res)
269 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
272 res = CALEClientResetDeviceStateForAll();
273 if (CA_STATUS_OK != res)
275 OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
277 res = CALEServerRemoveAllDevices();
278 if (CA_STATUS_OK != res)
280 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
282 CALEClientSetScanFlag(false);
284 newStatus = CA_ADAPTER_DISABLED;
285 g_bleDeviceStateChangedCallback(newStatus);
290 void CALEClientDisconnectCallabck(const char *address){
291 OIC_LOG(DEBUG, TAG, "[CALEClientDisconnectCallabck] Central: disconnect");
292 if (g_bleConnectionStateChangedCallback)
294 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
298 void CALEServerDisconnectCallback(const char *address){
299 OIC_LOG(DEBUG, TAG, "[CALEServerDisconnectCallback] Peripheral: disconnect");
300 if (g_bleConnectionStateChangedCallback)
302 g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);