replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / ios / calenwmonitor.m
1 /******************************************************************
2  *
3  * Copyright 2017 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <stdio.h>
22 #include "logger.h"
23 #include "calenwmonitor.h"
24 #include "caleclient.h"
25 #include "caleserver.h"
26 #include "caleutils.h"
27 #include "caleinterface.h"
28 #include "caadapterutils.h"
29
30 #include "octhread.h"
31
32 #define TAG PCF("OIC_CA_LE_MONITOR")
33
34 static oc_cond g_deviceStateCbCond = NULL;
35 static oc_mutex g_deviceStateCbMutex = NULL;
36
37 /**
38  * @var g_bleDeviceStateChangedCallback
39  * @brief Maintains the callback to be notified on device state changed.
40  */
41 static CALEDeviceStateChangedCallback g_bleDeviceStateChangedCallback = NULL;
42
43 /**
44  * @var g_bleConnectionStateChangedCallback
45  * @brief Maintains the callback to be notified on device state changed.
46  */
47 static CALEConnectionStateChangedCallback g_bleConnectionStateChangedCallback = NULL;
48
49 /**
50  * @var g_bleDeviceStateChangedCbMutex
51  * @brief Mutex to synchronize access to the deviceStateChanged Callback when the state
52  *           of the LE adapter gets change.
53  */
54 static oc_mutex g_bleDeviceStateChangedCbMutex = NULL;
55
56 /**
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.
60  */
61 static oc_mutex g_bleConnectionStateChangedCbMutex = NULL;
62
63 void CALESetAdapterStateCallback(CALEDeviceStateChangedCallback callback)
64 {
65     OIC_LOG(DEBUG, TAG, "CALESetAdapterStateCallback");
66     g_bleDeviceStateChangedCallback = callback;
67
68     oc_cond_signal(g_deviceStateCbCond);
69 }
70
71 CAResult_t CAInitializeLEAdapter(const ca_thread_pool_t threadPool)
72 {
73     OIC_LOG(DEBUG, TAG, "IN - CAInitializeLEAdapter");
74     (void)threadPool;
75     OIC_LOG(DEBUG, TAG, "OUT - CAInitializeLEAdapter");
76     return CA_STATUS_OK;
77 }
78
79 CAResult_t CAStartLEAdapter()
80 {
81     // Nothing to do.
82
83     return CA_STATUS_OK;
84 }
85
86 CAResult_t CAStopLEAdapter()
87 {
88     // Nothing to do.
89
90     return CA_STATUS_OK;
91 }
92
93 CAResult_t CAInitLENwkMonitorMutexVaraibles()
94 {
95     OIC_LOG(DEBUG, TAG, "IN - CAInitLENwkMonitorMutexVaraibles");
96     if (NULL == g_deviceStateCbMutex)
97     {
98         g_deviceStateCbMutex = oc_mutex_new();
99         if (NULL == g_deviceStateCbMutex)
100         {
101             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
102             return CA_STATUS_FAILED;
103         }
104     }
105
106     if (NULL == g_bleDeviceStateChangedCbMutex)
107     {
108         g_bleDeviceStateChangedCbMutex = oc_mutex_new();
109         if (NULL == g_bleDeviceStateChangedCbMutex)
110         {
111             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
112             return CA_STATUS_FAILED;
113         }
114     }
115
116     if (NULL == g_bleConnectionStateChangedCbMutex)
117     {
118         g_bleConnectionStateChangedCbMutex = oc_mutex_new();
119         if (NULL == g_bleConnectionStateChangedCbMutex)
120         {
121             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
122             oc_mutex_free(g_bleConnectionStateChangedCbMutex);
123             return CA_STATUS_FAILED;
124         }
125     }
126
127     OIC_LOG(DEBUG, TAG, "OUT - CAInitLENwkMonitorMutexVaraibles");
128     return CA_STATUS_OK;
129 }
130
131 void CATerminateLENwkMonitorMutexVaraibles()
132 {
133     OIC_LOG(DEBUG, TAG, "IN - CATerminateLENwkMonitorMutexVaraibles");
134
135     oc_mutex_free(g_deviceStateCbMutex);
136     g_deviceStateCbMutex = NULL;
137
138     oc_mutex_free(g_bleDeviceStateChangedCbMutex);
139     g_bleDeviceStateChangedCbMutex = NULL;
140
141     oc_mutex_free(g_bleConnectionStateChangedCbMutex);
142     g_bleConnectionStateChangedCbMutex = NULL;
143
144     OIC_LOG(DEBUG, TAG, "OUT - CATerminateLENwkMonitorMutexVaraibles");
145 }
146
147 CAResult_t CAGetLEAdapterState()
148 {
149     OIC_LOG(DEBUG, TAG, "IN - CAGetLEAdapterState");
150
151     if (!CALEClientIsEnableBTAdapter() || !CALEServerIsEnableBTAdapter())
152     {
153         OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
154     }
155
156     OIC_LOG(DEBUG, TAG, "OUT - CAGetLEAdapterState");
157     return CA_STATUS_OK;
158 }
159
160 CAResult_t CAInitializeLENetworkMonitor()
161 {
162     OIC_LOG(DEBUG, TAG, "IN - CAInitializeLENetworkMonitor");
163
164     if (NULL == g_deviceStateCbCond){
165         g_deviceStateCbCond = oc_cond_new();
166     }
167
168     CAResult_t res = CAInitLENwkMonitorMutexVaraibles();
169     if (CA_STATUS_OK != res)
170     {
171         OIC_LOG(ERROR, TAG, "CAInitLENwkMonitorMutexVaraibles has failed");
172         return CA_STATUS_FAILED;
173     }
174
175     OIC_LOG(DEBUG, TAG, "OUT- CAInitializeLENetworkMonitor");
176
177     return CA_STATUS_OK;
178
179 }
180
181 void CATerminateLENetworkMonitor()
182 {
183     OIC_LOG(DEBUG, TAG, "IN");
184
185     oc_cond_free(g_deviceStateCbCond);
186     g_deviceStateCbCond = NULL;
187
188     CATerminateLENwkMonitorMutexVaraibles();
189
190     OIC_LOG(DEBUG, TAG, "OUT");
191 }
192
193 CAResult_t CASetLEAdapterStateChangedCb(CALEDeviceStateChangedCallback callback)
194 {
195     OIC_LOG(DEBUG, TAG, "IN");
196
197     OIC_LOG(DEBUG, TAG, "Setting CALEDeviceStateChangedCallback");
198
199     oc_mutex_lock(g_bleDeviceStateChangedCbMutex);
200     CALESetAdapterStateCallback(callback);
201     oc_mutex_unlock(g_bleDeviceStateChangedCbMutex);
202
203     OIC_LOG(DEBUG, TAG, "OUT");
204     return CA_STATUS_OK;
205 }
206
207 CAResult_t CAUnSetLEAdapterStateChangedCb()
208 {
209     OIC_LOG(DEBUG, TAG, "it is not required in this platform");
210     return CA_STATUS_OK;
211 }
212
213 CAResult_t CASetLENWConnectionStateChangedCb(CALEConnectionStateChangedCallback callback)
214 {
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");
220     return CA_STATUS_OK;
221 }
222
223 CAResult_t CAUnSetLENWConnectionStateChangedCb()
224 {
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");
230     return CA_STATUS_OK;
231 }
232
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"));
236
237     if (!g_bleDeviceStateChangedCallback)
238     {
239         OIC_LOG(ERROR, TAG, "gNetworkChangeCb is null --> wait~");
240         if (NULL == g_deviceStateCbMutex){
241             g_deviceStateCbMutex = oc_mutex_new();
242         }
243         if (NULL == g_deviceStateCbCond){
244             g_deviceStateCbCond = oc_cond_new();
245         }
246         oc_mutex_lock(g_deviceStateCbMutex);
247         oc_cond_wait(g_deviceStateCbCond, g_deviceStateCbMutex);
248         oc_mutex_unlock(g_deviceStateCbMutex);
249     }
250
251     CAAdapterState_t newStatus = CA_ADAPTER_ENABLED;
252     CAResult_t res = CA_STATUS_OK;
253     switch(state){
254         case CALE_STATE_ON:
255             newStatus = CA_ADAPTER_ENABLED;
256             CALEClientCreateDeviceList();
257             CALEServerCreateCachedDeviceList();
258
259             g_bleDeviceStateChangedCallback(newStatus);
260             break;
261         case CALE_STATE_TURNING_OFF:
262             CAStopLEGattClient();
263             break;
264         default:
265         case CALE_STATE_OFF:
266             res = CALEClientRemoveAllGattObjs();
267             if (CA_STATUS_OK != res)
268             {
269                 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
270             }
271
272             res = CALEClientResetDeviceStateForAll();
273             if (CA_STATUS_OK != res)
274             {
275                 OIC_LOG(ERROR, TAG, "CALEClientResetDeviceStateForAll has failed");
276             }
277             res = CALEServerRemoveAllDevices();
278             if (CA_STATUS_OK != res)
279             {
280                 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
281             }
282             CALEClientSetScanFlag(false);
283
284             newStatus = CA_ADAPTER_DISABLED;
285             g_bleDeviceStateChangedCallback(newStatus);
286             break;
287     }
288 }
289
290 void CALEClientDisconnectCallabck(const char *address){
291     OIC_LOG(DEBUG, TAG, "[CALEClientDisconnectCallabck] Central: disconnect");
292     if (g_bleConnectionStateChangedCallback)
293     {
294         g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
295     }
296 }
297
298 void CALEServerDisconnectCallback(const char *address){
299     OIC_LOG(DEBUG, TAG, "[CALEServerDisconnectCallback] Peripheral: disconnect");
300     if (g_bleConnectionStateChangedCallback)
301     {
302         g_bleConnectionStateChangedCallback(CA_ADAPTER_GATT_BTLE, address, false);
303     }
304 }
305