Change file mode.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / android / camanagerdevice.c
1 /* ****************************************************************
2  *
3  * Copyright 2016 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 <jni.h>
22 #include "cacommon.h"
23 #include "logger.h"
24 #include "cacommonutil.h"
25 #include "camanagerleutil.h"
26 #include "uarraylist.h"
27 #include "camutex.h"
28 #include "camanagerdevice.h"
29 #include "oic_malloc.h"
30
31 #define TAG "OIC_CA_MANAGER_DEVICE"
32
33 static u_arraylist_t *g_deviceACDataList = NULL;
34 static ca_mutex g_deviceACDataListMutex = NULL;
35 static bool g_isBTRecovery = false;
36
37 jstring CAManagerGetLEAddressFromACData(JNIEnv *env, size_t idx)
38 {
39     OIC_LOG(DEBUG, TAG, "CAManagerGetLEAddressFromACData");
40     OIC_LOG_V(DEBUG, TAG, "idx : %d", idx);
41     if (idx <= u_arraylist_length(g_deviceACDataList))
42     {
43         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(
44                 g_deviceACDataList, idx);
45         if (!curData)
46         {
47             OIC_LOG(ERROR, TAG, "curData is null");
48             return NULL;
49         }
50
51         const char* address = (*env)->GetStringUTFChars(env, curData->address, NULL);
52         if (!address)
53         {
54             OIC_LOG(ERROR, TAG, "address is null");
55             return NULL;
56         }
57         OIC_LOG_V(INFO, TAG, "found out target address : %s", address);
58         (*env)->ReleaseStringUTFChars(env, curData->address, address);
59
60         return curData->address;
61     }
62     return NULL;
63 }
64
65 void CAManagerCreateACDataList(JNIEnv *env)
66 {
67     OIC_LOG(DEBUG, TAG, "CAManagerCreateACDataList");
68     VERIFY_NON_NULL_VOID(env, TAG, "env");
69
70     ca_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     ca_mutex_unlock(g_deviceACDataListMutex);
79 }
80
81 CAResult_t CAManagerInitMutexVaraibles()
82 {
83     if (NULL == g_deviceACDataListMutex)
84     {
85         g_deviceACDataListMutex = ca_mutex_new();
86         if (NULL == g_deviceACDataListMutex)
87         {
88             OIC_LOG(ERROR, TAG, "ca_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         ca_mutex_free(g_deviceACDataListMutex);
100         g_deviceACDataListMutex = NULL;
101     }
102 }
103
104 static CAManagerACData_t *CAManagerCreateACData(jstring jaddress)
105 {
106     OIC_LOG(DEBUG, TAG, "IN-CAManagerCreateACData");
107     VERIFY_NON_NULL_RET(jaddress, TAG, "jaddress", 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     data->address = jaddress;
118     data->isAutoConnect = false;
119
120     OIC_LOG(DEBUG, TAG, "OUT-CAManagerCreateACData");
121     return data;
122 }
123
124 bool CAManagerIsMatchedACData(JNIEnv *env, jstring jaddress)
125 {
126     VERIFY_NON_NULL_RET(env, TAG, "env", NULL);
127     VERIFY_NON_NULL_RET(jaddress, TAG, "jaddress", false);
128
129     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
130     if (!address)
131     {
132         OIC_LOG(ERROR, TAG, "address is null");
133         return false;
134     }
135
136     size_t length = u_arraylist_length(g_deviceACDataList);
137     for (size_t idx = 0; idx < length; idx++)
138     {
139         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
140                                                                            idx);
141         if (!curData)
142         {
143             OIC_LOG(ERROR, TAG, "curData is null");
144             (*env)->ReleaseStringUTFChars(env, jaddress, address);
145             return false;
146         }
147
148         const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
149         if (!setAddress)
150         {
151             OIC_LOG(ERROR, TAG, "address is null");
152             (*env)->ReleaseStringUTFChars(env, jaddress, address);
153             return false;
154         }
155
156         if (!strcmp(setAddress, address))
157         {
158             (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
159             (*env)->ReleaseStringUTFChars(env, jaddress, address);
160             return true;
161         }
162         (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
163     }
164     (*env)->ReleaseStringUTFChars(env, jaddress, address);
165
166     return false;
167 }
168
169 void CAManagerAddACData(JNIEnv *env, jstring jaddress)
170 {
171     OIC_LOG(DEBUG, TAG, "IN-CAManagerAddACData");
172
173     VERIFY_NON_NULL_VOID(env, TAG, "env");
174     VERIFY_NON_NULL_VOID(jaddress, TAG, "jaddress");
175
176     ca_mutex_lock(g_deviceACDataListMutex);
177
178     if(!CAManagerIsMatchedACData(env, jaddress))
179     {
180         OIC_LOG(DEBUG, TAG, "ACdata will be added");
181         // add CAManagerACData
182         jobject gaddress = (*env)->NewGlobalRef(env, jaddress);
183
184         CAManagerACData_t *data = CAManagerCreateACData(gaddress);
185         u_arraylist_add(g_deviceACDataList, data);
186     }
187     ca_mutex_unlock(g_deviceACDataListMutex);
188
189     OIC_LOG(DEBUG, TAG, "OUT-CAManagerAddACData");
190 }
191
192 CAResult_t CAManagerRemoveData(JNIEnv *env, jstring jaddress)
193 {
194     OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveData");
195     VERIFY_NON_NULL(env, TAG, "env");
196     VERIFY_NON_NULL(jaddress, TAG, "jaddress");
197
198     ca_mutex_lock(g_deviceACDataListMutex);
199
200     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
201     if (!address)
202     {
203         OIC_LOG(ERROR, TAG, "address is null");
204         ca_mutex_unlock(g_deviceACDataListMutex);
205         return CA_STATUS_FAILED;
206     }
207
208     OIC_LOG_V(DEBUG, TAG, "(%s) will be removed", address);
209
210     size_t length = u_arraylist_length(g_deviceACDataList);
211     for (size_t idx = 0; idx < length; idx++)
212     {
213         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
214                                                                            idx);
215         if (!curData)
216         {
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;
221         }
222
223         const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
224         if (!setAddress)
225         {
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;
230         }
231
232         if (!strcmp(setAddress, address))
233         {
234             if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
235             {
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;
241             }
242
243             (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
244
245             if (curData->address)
246             {
247                 (*env)->DeleteGlobalRef(env, curData->address);
248             }
249
250             OICFree(curData);
251             (*env)->ReleaseStringUTFChars(env, jaddress, address);
252             ca_mutex_unlock(g_deviceACDataListMutex);
253             OIC_LOG(DEBUG, TAG, "remove done");
254             return CA_STATUS_OK;
255         }
256         (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
257     }
258
259     (*env)->ReleaseStringUTFChars(env, jaddress, address);
260     ca_mutex_unlock(g_deviceACDataListMutex);
261     OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveData");
262     return CA_STATUS_OK;
263 }
264
265 CAResult_t CAManagerRemoveAllData(JNIEnv *env)
266 {
267     OIC_LOG(DEBUG, TAG, "IN-CAManagerRemoveAllData");
268     VERIFY_NON_NULL(env, TAG, "env");
269
270     ca_mutex_lock(g_deviceACDataListMutex);
271
272     size_t length = u_arraylist_length(g_deviceACDataList);
273     for (size_t idx = 0; idx < length; idx++)
274     {
275         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
276                                                                            idx);
277         if (!curData)
278         {
279             OIC_LOG(ERROR, TAG, "curData is null");
280             ca_mutex_unlock(g_deviceACDataListMutex);
281             return CA_STATUS_FAILED;
282         }
283
284         if (NULL == u_arraylist_remove(g_deviceACDataList, idx))
285         {
286             OIC_LOG(ERROR, TAG, "removal has failed.");
287             ca_mutex_unlock(g_deviceACDataListMutex);
288             return CA_STATUS_FAILED;
289         }
290
291         if (curData->address)
292         {
293             (*env)->DeleteGlobalRef(env, curData->address);
294         }
295
296         OICFree(curData);
297     }
298     ca_mutex_unlock(g_deviceACDataListMutex);
299     OIC_LOG(DEBUG, TAG, "OUT-CAManagerRemoveAllData");
300     return CA_STATUS_OK;
301 }
302
303 bool CAManagerGetAutoConnectionFlag(JNIEnv *env, jstring jaddress)
304 {
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);
308
309     ca_mutex_lock(g_deviceACDataListMutex);
310
311     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
312     if (!address)
313     {
314         OIC_LOG(ERROR, TAG, "address is null");
315         ca_mutex_unlock(g_deviceACDataListMutex);
316         return CA_STATUS_FAILED;
317     }
318
319     size_t length = u_arraylist_length(g_deviceACDataList);
320     for (size_t idx = 0; idx < length; idx++)
321     {
322         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
323                                                                            idx);
324         if (!curData)
325         {
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;
330         }
331
332         const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
333         if (!setAddress)
334         {
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;
339         }
340
341         if (!strcmp(setAddress, address))
342         {
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;
348         }
349         (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
350     }
351     (*env)->ReleaseStringUTFChars(env, jaddress, address);
352     ca_mutex_unlock(g_deviceACDataListMutex);
353
354     OIC_LOG(DEBUG, TAG, "OUT-CAManagerGetAutoConnectionFlag");
355     return false;
356 }
357
358 void CAManagerSetAutoConnectionFlag(JNIEnv *env, jstring jaddress, bool flag)
359 {
360     OIC_LOG(DEBUG, TAG, "IN-CAManagerSetAutoConnectionFlag");
361     VERIFY_NON_NULL_VOID(env, TAG, "env");
362     VERIFY_NON_NULL_VOID(jaddress, TAG, "jaddress");
363
364     ca_mutex_lock(g_deviceACDataListMutex);
365
366     const char* address = (*env)->GetStringUTFChars(env, jaddress, NULL);
367     if (!address)
368     {
369         OIC_LOG(ERROR, TAG, "address is null");
370         ca_mutex_unlock(g_deviceACDataListMutex);
371         return;
372     }
373
374     size_t length = u_arraylist_length(g_deviceACDataList);
375     for (size_t idx = 0; idx < length; idx++)
376     {
377         CAManagerACData_t *curData = (CAManagerACData_t *) u_arraylist_get(g_deviceACDataList,
378                                                                            idx);
379         if (!curData)
380         {
381             OIC_LOG(ERROR, TAG, "curData is null");
382             (*env)->ReleaseStringUTFChars(env, jaddress, address);
383             ca_mutex_unlock(g_deviceACDataListMutex);
384             return;
385         }
386
387         const char* setAddress = (*env)->GetStringUTFChars(env, curData->address, NULL);
388         if (!setAddress)
389         {
390             OIC_LOG(ERROR, TAG, "address is null");
391             (*env)->ReleaseStringUTFChars(env, jaddress, address);
392             ca_mutex_unlock(g_deviceACDataListMutex);
393             return;
394         }
395
396         if (!strcmp(setAddress, address))
397         {
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);
403             return;
404         }
405         (*env)->ReleaseStringUTFChars(env, curData->address, setAddress);
406     }
407     (*env)->ReleaseStringUTFChars(env, jaddress, address);
408     ca_mutex_unlock(g_deviceACDataListMutex);
409
410     OIC_LOG(DEBUG, TAG, "OUT-CAManagerSetAutoConnectionFlag");
411 }
412
413 size_t CAManagerGetACDataLength()
414 {
415     return u_arraylist_length(g_deviceACDataList);
416 }
417
418 void CAManagerSetBTRecovery(bool flag)
419 {
420     g_isBTRecovery = flag;
421     OIC_LOG_V(DEBUG, TAG, "BT recovery flag : %d", g_isBTRecovery);
422 }
423
424 bool CAManagerIsRecoveryFlagSet()
425 {
426     return g_isBTRecovery;
427 }