replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / bt_le_manager / ios / camanagerdevice.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 <string.h>
22
23 #include "cacommon.h"
24 #include "logger.h"
25 #include "cacommonutil.h"
26 #include "camanagerleutil.h"
27 #include "uarraylist.h"
28 #include "octhread.h"
29 #include "camanagerdevice.h"
30 #include "oic_malloc.h"
31 #include "oic_string.h"
32
33 #define TAG "OIC_CA_MANAGER_DEVICE"
34
35 static u_arraylist_t *g_deviceACDataList = NULL;
36 static oc_mutex g_deviceACDataListMutex = NULL;
37 static bool g_isBTRecovery = false;
38
39 char* CAManagerGetLEAddressFromACData(size_t idx)
40 {
41     OIC_LOG(DEBUG, TAG, "CAManagerGetLEAddressFromACData");
42     OIC_LOG_V(DEBUG, TAG, "idx : %d", idx);
43     if (idx <= u_arraylist_length(g_deviceACDataList))
44     {
45         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(
46                 g_deviceACDataList, idx);
47         if (!curData)
48         {
49             OIC_LOG(ERROR, TAG, "curData is null");
50             return NULL;
51         }
52
53         const char* address = curData->address;
54         if (!address)
55         {
56             OIC_LOG(ERROR, TAG, "address is null");
57             return NULL;
58         }
59         OIC_LOG_V(INFO, TAG, "found out target address : %s", address);
60
61         return curData->address;
62     }
63     return NULL;
64 }
65
66 void CAManagerCreateACDataList()
67 {
68     OIC_LOG(DEBUG, TAG, "CAManagerCreateACDataList");
69
70     oc_mutex_lock(g_deviceACDataListMutex);
71     // create new object array
72     if (g_deviceACDataList == NULL)
73     {
74         OIC_LOG(DEBUG, TAG, "Create AC Data list");
75
76         g_deviceACDataList = u_arraylist_create();
77     }
78     oc_mutex_unlock(g_deviceACDataListMutex);
79 }
80
81 CAResult_t CAManagerInitMutexVaraibles()
82 {
83     if (NULL == g_deviceACDataListMutex)
84     {
85         g_deviceACDataListMutex = oc_mutex_new();
86         if (NULL == g_deviceACDataListMutex)
87         {
88             OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
89             return CA_STATUS_FAILED;
90         }
91     }
92     return CA_STATUS_OK;
93 }
94
95 void CAManagerTerminateMutexVariables()
96 {
97     if (g_deviceACDataListMutex)
98     {
99         oc_mutex_free(g_deviceACDataListMutex);
100         g_deviceACDataListMutex = NULL;
101     }
102 }
103
104 static CAManagerACData_t *CAManagerCreateACData(char *address)
105 {
106     OIC_LOG(DEBUG, TAG, "IN-CAManagerCreateACData");
107     VERIFY_NON_NULL_RET(address, TAG, "address", NULL);
108
109     // create block data
110     CAManagerACData_t *data = (CAManagerACData_t *) OICCalloc(1, sizeof(*data));
111     if (!data)
112     {
113         OIC_LOG(ERROR, TAG, "memory alloc has failed");
114         return NULL;
115     }
116
117     OICStrcpy(data->address, strlen(address)+1, address);
118     data->isAutoConnect = false;
119
120     OIC_LOG(DEBUG, TAG, "OUT-CAManagerCreateACData");
121     return data;
122 }
123
124 bool CAManagerIsMatchedACData(const char* address)
125 {
126     VERIFY_NON_NULL_RET(address, TAG, "address", false);
127
128     size_t length = u_arraylist_length(g_deviceACDataList);
129     for (size_t idx = 0; idx < length; idx++)
130     {
131         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
132                                                                            idx);
133         if (!curData)
134         {
135             OIC_LOG(ERROR, TAG, "curData is null");
136             return false;
137         }
138
139         const char* setAddress = curData->address;
140         if (!setAddress)
141         {
142             OIC_LOG(ERROR, TAG, "address is null");
143             return false;
144         }
145
146         if (!strcmp(setAddress, address))
147         {
148             return true;
149         }
150     }
151
152     return false;
153 }
154
155 void CAManagerAddACData(const char* address)
156 {
157     OIC_LOG(DEBUG, TAG, "IN-CAManagerAddACData");
158
159     VERIFY_NON_NULL_VOID(address, TAG, "address");
160
161     oc_mutex_lock(g_deviceACDataListMutex);
162
163     if(!CAManagerIsMatchedACData(address))
164     {
165         OIC_LOG(DEBUG, TAG, "ACdata will be added");
166
167         CAManagerACData_t *data = CAManagerCreateACData(address);
168         u_arraylist_add(g_deviceACDataList, data);
169     }
170     oc_mutex_unlock(g_deviceACDataListMutex);
171
172     OIC_LOG(DEBUG, TAG, "OUT-CAManagerAddACData");
173 }
174
175 CAResult_t CAManagerRemoveData(const char* address)
176 {
177     OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveData");
178     VERIFY_NON_NULL(address, TAG, "address");
179
180     oc_mutex_lock(g_deviceACDataListMutex);
181
182     OIC_LOG_V(DEBUG, TAG, "(%s) will be removed", address);
183
184     size_t length = u_arraylist_length(g_deviceACDataList);
185     for (size_t idx = 0; idx < length; idx++)
186     {
187         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
188                                                                            idx);
189         if (!curData)
190         {
191             OIC_LOG(ERROR, TAG, "curData is null");
192             oc_mutex_unlock(g_deviceACDataListMutex);
193             return CA_STATUS_FAILED;
194         }
195
196         const char* setAddress = curData->address;
197         if (!setAddress)
198         {
199             OIC_LOG(ERROR, TAG, "address is null");
200             oc_mutex_unlock(g_deviceACDataListMutex);
201             return CA_STATUS_FAILED;
202         }
203
204         if (!strcmp(setAddress, address))
205         {
206             if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
207             {
208                 OIC_LOG(ERROR, TAG, "removal has failed.");
209                 oc_mutex_unlock(g_deviceACDataListMutex);
210                 return CA_STATUS_FAILED;
211             }
212
213             OICFree(curData);
214             oc_mutex_unlock(g_deviceACDataListMutex);
215             OIC_LOG(DEBUG, TAG, "remove done");
216             return CA_STATUS_OK;
217         }
218     }
219
220     oc_mutex_unlock(g_deviceACDataListMutex);
221     OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveData");
222     return CA_STATUS_OK;
223 }
224
225 CAResult_t CAManagerRemoveAllData()
226 {
227     OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveAllData");
228
229     oc_mutex_lock(g_deviceACDataListMutex);
230
231     size_t length = u_arraylist_length(g_deviceACDataList);
232     for (size_t idx = 0; idx < length; idx++)
233     {
234         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
235                                                                            idx);
236         if (!curData)
237         {
238             OIC_LOG(ERROR, TAG, "curData is null");
239             oc_mutex_unlock(g_deviceACDataListMutex);
240             return CA_STATUS_FAILED;
241         }
242
243         if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
244         {
245             OIC_LOG(ERROR, TAG, "removal has failed.");
246             oc_mutex_unlock(g_deviceACDataListMutex);
247             return CA_STATUS_FAILED;
248         }
249
250         OICFree(curData);
251     }
252     oc_mutex_unlock(g_deviceACDataListMutex);
253     OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveAllData");
254     return CA_STATUS_OK;
255 }
256
257 bool CAManagerGetAutoConnectionFlag(const char* address)
258 {
259     OIC_LOG(DEBUG, TAG, "IN-CAManagerGetAutoConnectionFlag");
260     VERIFY_NON_NULL_RET(address, TAG, "address", NULL);
261
262     oc_mutex_lock(g_deviceACDataListMutex);
263
264     size_t length = u_arraylist_length(g_deviceACDataList);
265     for (size_t idx = 0; idx < length; idx++)
266     {
267         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
268                                                                            idx);
269         if (!curData)
270         {
271             OIC_LOG(ERROR, TAG, "curData is null");
272             oc_mutex_unlock(g_deviceACDataListMutex);
273             return CA_STATUS_FAILED;
274         }
275
276         const char* setAddress = curData->address;
277         if (!setAddress)
278         {
279             OIC_LOG(ERROR, TAG, "setAddress is null");
280             oc_mutex_unlock(g_deviceACDataListMutex);
281             return CA_STATUS_FAILED;
282         }
283
284         if (!strcmp(setAddress, address))
285         {
286             oc_mutex_unlock(g_deviceACDataListMutex);
287             OIC_LOG_V(DEBUG, TAG, "flag is %d", curData->isAutoConnect);
288             return curData->isAutoConnect;
289         }
290     }
291     oc_mutex_unlock(g_deviceACDataListMutex);
292
293     OIC_LOG(DEBUG, TAG, "OUT-CAManagerGetAutoConnectionFlag");
294     return false;
295 }
296
297 void CAManagerSetAutoConnectionFlag(const char* address, bool flag)
298 {
299     OIC_LOG(DEBUG, TAG, "IN-CAManagerSetAutoConnectionFlag");
300     VERIFY_NON_NULL_VOID(address, TAG, "address");
301
302     oc_mutex_lock(g_deviceACDataListMutex);
303
304     size_t length = u_arraylist_length(g_deviceACDataList);
305     for (size_t idx = 0; idx < length; idx++)
306     {
307         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
308                                                                            idx);
309         if (!curData)
310         {
311             OIC_LOG(ERROR, TAG, "curData is null");
312             oc_mutex_unlock(g_deviceACDataListMutex);
313             return;
314         }
315
316         const char* setAddress = curData->address;
317         if (!setAddress)
318         {
319             OIC_LOG(ERROR, TAG, "address is null");
320             oc_mutex_unlock(g_deviceACDataListMutex);
321             return;
322         }
323
324         if (!strcmp(setAddress, address))
325         {
326             OIC_LOG_V(DEBUG, TAG, "flag is set to %d", flag);
327             curData->isAutoConnect = flag;
328             oc_mutex_unlock(g_deviceACDataListMutex);
329             return;
330         }
331     }
332     oc_mutex_unlock(g_deviceACDataListMutex);
333
334     OIC_LOG(DEBUG, TAG, "OUT-CAManagerSetAutoConnectionFlag");
335 }
336
337 size_t CAManagerGetACDataLength()
338 {
339     return u_arraylist_length(g_deviceACDataList);
340 }
341
342 void CAManagerSetBTRecovery(bool flag)
343 {
344     g_isBTRecovery = flag;
345     OIC_LOG_V(DEBUG, TAG, "BT recovery flag : %d", g_isBTRecovery);
346 }
347
348 bool CAManagerIsRecoveryFlagSet()
349 {
350     return g_isBTRecovery;
351 }