1 /* ****************************************************************
3 * Copyright 2016 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 ******************************************************************/
24 #include "cacommonutil.h"
25 #include "camanagerleutil.h"
26 #include "uarraylist.h"
28 #include "camanagerdevice.h"
29 #include "oic_malloc.h"
31 #define TAG "OIC_CA_MANAGER_DEVICE"
33 static u_arraylist_t *g_deviceACDataList = NULL;
34 static ca_mutex g_deviceACDataListMutex = NULL;
35 static bool g_isBTRecovery = false;
37 jstring CAManagerGetLEAddressFromACData(JNIEnv *env, size_t idx)
39 OIC_LOG(DEBUG, TAG, "CAManagerGetLEAddressFromACData");
40 OIC_LOG_V(DEBUG, TAG, "idx : %d", idx);
41 if (idx <= u_arraylist_length(g_deviceACDataList))
43 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(
44 g_deviceACDataList, idx);
47 OIC_LOG(ERROR, TAG, "curData is null");
51 const char* address = (*env)->GetStringUTFChars(env, curData->address, NULL);
54 OIC_LOG(ERROR, TAG, "address is null");
57 OIC_LOG_V(INFO, TAG, "found out target address : %s", address);
58 (*env)->ReleaseStringUTFChars(env, curData->address, address);
60 return curData->address;
65 void CAManagerCreateACDataList(JNIEnv *env)
67 OIC_LOG(DEBUG, TAG, "CAManagerCreateACDataList");
68 VERIFY_NON_NULL_VOID(env, TAG, "env");
70 ca_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 ca_mutex_unlock(g_deviceACDataListMutex);
81 CAResult_t CAManagerInitMutexVaraibles()
83 if (NULL == g_deviceACDataListMutex)
85 g_deviceACDataListMutex = ca_mutex_new();
86 if (NULL == g_deviceACDataListMutex)
88 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
89 return CA_STATUS_FAILED;
95 void CAManagerTerminateMutexVariables()
97 if (g_deviceACDataListMutex)
99 ca_mutex_free(g_deviceACDataListMutex);
100 g_deviceACDataListMutex = NULL;
104 static CAManagerACData_t *CAManagerCreateACData(jstring jaddress)
106 OIC_LOG(DEBUG, TAG, "IN-CAManagerCreateACData");
107 VERIFY_NON_NULL_RET(jaddress, TAG, "jaddress", NULL);
110 CAManagerACData_t *data = (CAManagerACData_t *) OICCalloc(1, sizeof(*data));
113 OIC_LOG(ERROR, TAG, "memory alloc has failed");
117 data->address = jaddress;
118 data->isAutoConnect = false;
120 OIC_LOG(DEBUG, TAG, "OUT-CAManagerCreateACData");
124 bool CAManagerIsMatchedACData(JNIEnv *env, jstring jaddress)
126 VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
127 VERIFY_NON_NULL_RET(jaddress, TAG, "jaddress", false);
129 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
132 OIC_LOG(ERROR, TAG, "address is null");
136 size_t length = u_arraylist_length(g_deviceACDataList);
137 for (size_t idx = 0; idx < length; idx++)
139 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
143 OIC_LOG(ERROR, TAG, "curData is null");
144 (*env)->ReleaseStringUTFChars(env, jaddress, address);
148 const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
151 OIC_LOG(ERROR, TAG, "address is null");
152 (*env)->ReleaseStringUTFChars(env, jaddress, address);
156 if (!strcmp(setAddress, address))
158 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
159 (*env)->ReleaseStringUTFChars(env, jaddress, address);
162 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
164 (*env)->ReleaseStringUTFChars(env, jaddress, address);
169 void CAManagerAddACData(JNIEnv *env, jstring jaddress)
171 OIC_LOG(DEBUG, TAG, "IN-CAManagerAddACData");
173 VERIFY_NON_NULL_VOID(env, TAG, "env");
174 VERIFY_NON_NULL_VOID(jaddress, TAG, "jaddress");
176 ca_mutex_lock(g_deviceACDataListMutex);
178 if(!CAManagerIsMatchedACData(env, jaddress))
180 OIC_LOG(DEBUG, TAG, "ACdata will be added");
181 // add CAManagerACData
182 jobject gaddress = (*env)->NewGlobalRef(env, jaddress);
184 CAManagerACData_t *data = CAManagerCreateACData(gaddress);
185 u_arraylist_add(g_deviceACDataList, data);
187 ca_mutex_unlock(g_deviceACDataListMutex);
189 OIC_LOG(DEBUG, TAG, "OUT-CAManagerAddACData");
192 CAResult_t CAManagerRemoveData(JNIEnv *env, jstring jaddress)
194 OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveData");
195 VERIFY_NON_NULL(env, TAG, "env");
196 VERIFY_NON_NULL(jaddress, TAG, "jaddress");
198 ca_mutex_lock(g_deviceACDataListMutex);
200 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
203 OIC_LOG(ERROR, TAG, "address is null");
204 ca_mutex_unlock(g_deviceACDataListMutex);
205 return CA_STATUS_FAILED;
208 OIC_LOG_V(DEBUG, TAG, "(%s) will be removed", address);
210 size_t length = u_arraylist_length(g_deviceACDataList);
211 for (size_t idx = 0; idx < length; idx++)
213 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
217 OIC_LOG(ERROR, TAG, "curData is null");
218 (*env)->ReleaseStringUTFChars(env, jaddress, address);
219 ca_mutex_unlock(g_deviceACDataListMutex);
220 return CA_STATUS_FAILED;
223 const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
226 OIC_LOG(ERROR, TAG, "address is null");
227 (*env)->ReleaseStringUTFChars(env, jaddress, address);
228 ca_mutex_unlock(g_deviceACDataListMutex);
229 return CA_STATUS_FAILED;
232 if (!strcmp(setAddress, address))
234 if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
236 OIC_LOG(ERROR, TAG, "removal has failed.");
237 (*env)->ReleaseStringUTFChars(env, jaddress, address);
238 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
239 ca_mutex_unlock(g_deviceACDataListMutex);
240 return CA_STATUS_FAILED;
243 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
245 if (curData->address)
247 (*env)->DeleteGlobalRef(env, curData->address);
251 (*env)->ReleaseStringUTFChars(env, jaddress, address);
252 ca_mutex_unlock(g_deviceACDataListMutex);
253 OIC_LOG(DEBUG, TAG, "remove done");
256 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
259 (*env)->ReleaseStringUTFChars(env, jaddress, address);
260 ca_mutex_unlock(g_deviceACDataListMutex);
261 OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveData");
265 CAResult_t CAManagerRemoveAllData(JNIEnv *env)
267 OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveAllData");
268 VERIFY_NON_NULL(env, TAG, "env");
270 ca_mutex_lock(g_deviceACDataListMutex);
272 size_t length = u_arraylist_length(g_deviceACDataList);
273 for (size_t idx = 0; idx < length; idx++)
275 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
279 OIC_LOG(ERROR, TAG, "curData is null");
280 ca_mutex_unlock(g_deviceACDataListMutex);
281 return CA_STATUS_FAILED;
284 if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
286 OIC_LOG(ERROR, TAG, "removal has failed.");
287 ca_mutex_unlock(g_deviceACDataListMutex);
288 return CA_STATUS_FAILED;
291 if (curData->address)
293 (*env)->DeleteGlobalRef(env, curData->address);
298 ca_mutex_unlock(g_deviceACDataListMutex);
299 OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveAllData");
303 bool CAManagerGetAutoConnectionFlag(JNIEnv *env, jstring jaddress)
305 OIC_LOG(DEBUG, TAG, "IN-CAManagerGetAutoConnectionFlag");
306 VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
307 VERIFY_NON_NULL_RET(jaddress, TAG, "jaddress", NULL);
309 ca_mutex_lock(g_deviceACDataListMutex);
311 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
314 OIC_LOG(ERROR, TAG, "address is null");
315 ca_mutex_unlock(g_deviceACDataListMutex);
316 return CA_STATUS_FAILED;
319 size_t length = u_arraylist_length(g_deviceACDataList);
320 for (size_t idx = 0; idx < length; idx++)
322 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
326 OIC_LOG(ERROR, TAG, "curData is null");
327 (*env)->ReleaseStringUTFChars(env, jaddress, address);
328 ca_mutex_unlock(g_deviceACDataListMutex);
329 return CA_STATUS_FAILED;
332 const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
335 OIC_LOG(ERROR, TAG, "setAddress is null");
336 (*env)->ReleaseStringUTFChars(env, jaddress, address);
337 ca_mutex_unlock(g_deviceACDataListMutex);
338 return CA_STATUS_FAILED;
341 if (!strcmp(setAddress, address))
343 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
344 (*env)->ReleaseStringUTFChars(env, jaddress, address);
345 ca_mutex_unlock(g_deviceACDataListMutex);
346 OIC_LOG_V(DEBUG, TAG, "flag is %d", curData->isAutoConnect);
347 return curData->isAutoConnect;
349 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
351 (*env)->ReleaseStringUTFChars(env, jaddress, address);
352 ca_mutex_unlock(g_deviceACDataListMutex);
354 OIC_LOG(DEBUG, TAG, "OUT-CAManagerGetAutoConnectionFlag");
358 void CAManagerSetAutoConnectionFlag(JNIEnv *env, jstring jaddress, bool flag)
360 OIC_LOG(DEBUG, TAG, "IN-CAManagerSetAutoConnectionFlag");
361 VERIFY_NON_NULL_VOID(env, TAG, "env");
362 VERIFY_NON_NULL_VOID(jaddress, TAG, "jaddress");
364 ca_mutex_lock(g_deviceACDataListMutex);
366 const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
369 OIC_LOG(ERROR, TAG, "address is null");
370 ca_mutex_unlock(g_deviceACDataListMutex);
374 size_t length = u_arraylist_length(g_deviceACDataList);
375 for (size_t idx = 0; idx < length; idx++)
377 CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
381 OIC_LOG(ERROR, TAG, "curData is null");
382 (*env)->ReleaseStringUTFChars(env, jaddress, address);
383 ca_mutex_unlock(g_deviceACDataListMutex);
387 const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
390 OIC_LOG(ERROR, TAG, "address is null");
391 (*env)->ReleaseStringUTFChars(env, jaddress, address);
392 ca_mutex_unlock(g_deviceACDataListMutex);
396 if (!strcmp(setAddress, address))
398 OIC_LOG_V(DEBUG, TAG, "flag is set to %d", flag);
399 curData->isAutoConnect = flag;
400 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
401 (*env)->ReleaseStringUTFChars(env, jaddress, address);
402 ca_mutex_unlock(g_deviceACDataListMutex);
405 (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
407 (*env)->ReleaseStringUTFChars(env, jaddress, address);
408 ca_mutex_unlock(g_deviceACDataListMutex);
410 OIC_LOG(DEBUG, TAG, "OUT-CAManagerSetAutoConnectionFlag");
413 size_t CAManagerGetACDataLength()
415 return u_arraylist_length(g_deviceACDataList);
418 void CAManagerSetBTRecovery(bool flag)
420 g_isBTRecovery = flag;
421 OIC_LOG_V(DEBUG, TAG, "BT recovery flag : %d", g_isBTRecovery);
424 bool CAManagerIsRecoveryFlagSet()
426 return g_isBTRecovery;