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 ******************************************************************/
25 #include "cacommonutil.h"
26 #include "camanagerleutil.h"
27 #include "uarraylist.h"
29 #include "camanagerdevice.h"
30 #include "oic_malloc.h"
31 #include "oic_string.h"
33 #define TAG "OIC_CA_MANAGER_DEVICE"
35 static u_arraylist_t *g_deviceACDataList = NULL;
36 static oc_mutex g_deviceACDataListMutex = NULL;
37 static bool g_isBTRecovery = false;
39 char* CAManagerGetLEAddressFromACData(size_t idx)
41 OIC_LOG(DEBUG, TAG, "CAManagerGetLEAddressFromACData");
42 OIC_LOG_V(DEBUG, TAG, "idx : %d", idx);
43 if (idx <= u_arraylist_length(g_deviceACDataList))
45 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(
46 g_deviceACDataList, idx);
49 OIC_LOG(ERROR, TAG, "curData is null");
53 const char* address = curData->address;
56 OIC_LOG(ERROR, TAG, "address is null");
59 OIC_LOG_V(INFO, TAG, "found out target address : %s", address);
61 return curData->address;
66 void CAManagerCreateACDataList()
68 OIC_LOG(DEBUG, TAG, "CAManagerCreateACDataList");
70 oc_mutex_lock(g_deviceACDataListMutex);
71 // create new object array
72 if (g_deviceACDataList == NULL)
74 OIC_LOG(DEBUG, TAG, "Create AC Data list");
76 g_deviceACDataList = u_arraylist_create();
78 oc_mutex_unlock(g_deviceACDataListMutex);
81 CAResult_t CAManagerInitMutexVaraibles()
83 if (NULL == g_deviceACDataListMutex)
85 g_deviceACDataListMutex = oc_mutex_new();
86 if (NULL == g_deviceACDataListMutex)
88 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
89 return CA_STATUS_FAILED;
95 void CAManagerTerminateMutexVariables()
97 if (g_deviceACDataListMutex)
99 oc_mutex_free(g_deviceACDataListMutex);
100 g_deviceACDataListMutex = NULL;
104 static CAManagerACData_t *CAManagerCreateACData(char *address)
106 OIC_LOG(DEBUG, TAG, "IN-CAManagerCreateACData");
107 VERIFY_NON_NULL_RET(address, TAG, "address", NULL);
110 CAManagerACData_t *data = (CAManagerACData_t *) OICCalloc(1, sizeof(*data));
113 OIC_LOG(ERROR, TAG, "memory alloc has failed");
117 OICStrcpy(data->address, strlen(address)+1, address);
118 data->isAutoConnect = false;
120 OIC_LOG(DEBUG, TAG, "OUT-CAManagerCreateACData");
124 bool CAManagerIsMatchedACData(const char* address)
126 VERIFY_NON_NULL_RET(address, TAG, "address", false);
128 size_t length = u_arraylist_length(g_deviceACDataList);
129 for (size_t idx = 0; idx < length; idx++)
131 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
135 OIC_LOG(ERROR, TAG, "curData is null");
139 const char* setAddress = curData->address;
142 OIC_LOG(ERROR, TAG, "address is null");
146 if (!strcmp(setAddress, address))
155 void CAManagerAddACData(const char* address)
157 OIC_LOG(DEBUG, TAG, "IN-CAManagerAddACData");
159 VERIFY_NON_NULL_VOID(address, TAG, "address");
161 oc_mutex_lock(g_deviceACDataListMutex);
163 if(!CAManagerIsMatchedACData(address))
165 OIC_LOG(DEBUG, TAG, "ACdata will be added");
167 CAManagerACData_t *data = CAManagerCreateACData(address);
168 u_arraylist_add(g_deviceACDataList, data);
170 oc_mutex_unlock(g_deviceACDataListMutex);
172 OIC_LOG(DEBUG, TAG, "OUT-CAManagerAddACData");
175 CAResult_t CAManagerRemoveData(const char* address)
177 OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveData");
178 VERIFY_NON_NULL(address, TAG, "address");
180 oc_mutex_lock(g_deviceACDataListMutex);
182 OIC_LOG_V(DEBUG, TAG, "(%s) will be removed", address);
184 size_t length = u_arraylist_length(g_deviceACDataList);
185 for (size_t idx = 0; idx < length; idx++)
187 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
191 OIC_LOG(ERROR, TAG, "curData is null");
192 oc_mutex_unlock(g_deviceACDataListMutex);
193 return CA_STATUS_FAILED;
196 const char* setAddress = curData->address;
199 OIC_LOG(ERROR, TAG, "address is null");
200 oc_mutex_unlock(g_deviceACDataListMutex);
201 return CA_STATUS_FAILED;
204 if (!strcmp(setAddress, address))
206 if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
208 OIC_LOG(ERROR, TAG, "removal has failed.");
209 oc_mutex_unlock(g_deviceACDataListMutex);
210 return CA_STATUS_FAILED;
214 oc_mutex_unlock(g_deviceACDataListMutex);
215 OIC_LOG(DEBUG, TAG, "remove done");
220 oc_mutex_unlock(g_deviceACDataListMutex);
221 OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveData");
225 CAResult_t CAManagerRemoveAllData()
227 OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveAllData");
229 oc_mutex_lock(g_deviceACDataListMutex);
231 size_t length = u_arraylist_length(g_deviceACDataList);
232 for (size_t idx = 0; idx < length; idx++)
234 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
238 OIC_LOG(ERROR, TAG, "curData is null");
239 oc_mutex_unlock(g_deviceACDataListMutex);
240 return CA_STATUS_FAILED;
243 if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
245 OIC_LOG(ERROR, TAG, "removal has failed.");
246 oc_mutex_unlock(g_deviceACDataListMutex);
247 return CA_STATUS_FAILED;
252 oc_mutex_unlock(g_deviceACDataListMutex);
253 OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveAllData");
257 bool CAManagerGetAutoConnectionFlag(const char* address)
259 OIC_LOG(DEBUG, TAG, "IN-CAManagerGetAutoConnectionFlag");
260 VERIFY_NON_NULL_RET(address, TAG, "address", NULL);
262 oc_mutex_lock(g_deviceACDataListMutex);
264 size_t length = u_arraylist_length(g_deviceACDataList);
265 for (size_t idx = 0; idx < length; idx++)
267 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
271 OIC_LOG(ERROR, TAG, "curData is null");
272 oc_mutex_unlock(g_deviceACDataListMutex);
273 return CA_STATUS_FAILED;
276 const char* setAddress = curData->address;
279 OIC_LOG(ERROR, TAG, "setAddress is null");
280 oc_mutex_unlock(g_deviceACDataListMutex);
281 return CA_STATUS_FAILED;
284 if (!strcmp(setAddress, address))
286 oc_mutex_unlock(g_deviceACDataListMutex);
287 OIC_LOG_V(DEBUG, TAG, "flag is %d", curData->isAutoConnect);
288 return curData->isAutoConnect;
291 oc_mutex_unlock(g_deviceACDataListMutex);
293 OIC_LOG(DEBUG, TAG, "OUT-CAManagerGetAutoConnectionFlag");
297 void CAManagerSetAutoConnectionFlag(const char* address, bool flag)
299 OIC_LOG(DEBUG, TAG, "IN-CAManagerSetAutoConnectionFlag");
300 VERIFY_NON_NULL_VOID(address, TAG, "address");
302 oc_mutex_lock(g_deviceACDataListMutex);
304 size_t length = u_arraylist_length(g_deviceACDataList);
305 for (size_t idx = 0; idx < length; idx++)
307 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
311 OIC_LOG(ERROR, TAG, "curData is null");
312 oc_mutex_unlock(g_deviceACDataListMutex);
316 const char* setAddress = curData->address;
319 OIC_LOG(ERROR, TAG, "address is null");
320 oc_mutex_unlock(g_deviceACDataListMutex);
324 if (!strcmp(setAddress, address))
326 OIC_LOG_V(DEBUG, TAG, "flag is set to %d", flag);
327 curData->isAutoConnect = flag;
328 oc_mutex_unlock(g_deviceACDataListMutex);
332 oc_mutex_unlock(g_deviceACDataListMutex);
334 OIC_LOG(DEBUG, TAG, "OUT-CAManagerSetAutoConnectionFlag");
337 size_t CAManagerGetACDataLength()
339 return u_arraylist_length(g_deviceACDataList);
342 void CAManagerSetBTRecovery(bool flag)
344 g_isBTRecovery = flag;
345 OIC_LOG_V(DEBUG, TAG, "BT recovery flag : %d", g_isBTRecovery);
348 bool CAManagerIsRecoveryFlagSet()
350 return g_isBTRecovery;