Clean up log messages in CA Layer.
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / android / calestate.c
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 <string.h>
23 #include <jni.h>
24 #include <unistd.h>
25
26 #include "calestate.h"
27 #include "caleinterface.h"
28 #include "caadapterutils.h"
29 #include "caleutils.h"
30
31 #include "logger.h"
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "cathreadpool.h" /* for thread pool */
35 #include "octhread.h"
36 #include "uarraylist.h"
37
38 #define TAG PCF("OIC_CA_LE_STATE")
39
40 CAResult_t CALEUpdateDeviceState(const char* address,
41                                  uint16_t state_type,
42                                  uint16_t target_state,
43                                  u_arraylist_t *deviceList,
44                                  oc_mutex deviceListMutex)
45 {
46     VERIFY_NON_NULL(address, TAG, "address is null");
47     VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
48
49     oc_mutex_lock(deviceListMutex);
50
51     if (CALEIsDeviceInList(address, deviceList))
52     {
53         CALEState_t* curState = CALEGetStateInfo(address, deviceList);
54         if(!curState)
55         {
56             OIC_LOG(ERROR, TAG, "curState is null");
57             oc_mutex_unlock(deviceListMutex);
58             return CA_STATUS_FAILED;
59         }
60
61         switch(state_type)
62         {
63             case CA_LE_CONNECTION_STATE:
64                 curState->connectedState = target_state;
65                 break;
66             case CA_LE_SEND_STATE:
67                 curState->sendState = target_state;
68                 break;
69             default:
70                 break;
71         }
72         OIC_LOG_V(INFO, TAG, "update state - addr: %s, conn: %d, send: %d, ACFlag: %d, mtu: %d",
73                   curState->address, curState->connectedState, curState->sendState,
74                   curState->autoConnectFlag, curState->mtuSize);
75     }
76     else /** state is added newly **/
77     {
78         if (strlen(address) > CA_MACADDR_SIZE)
79         {
80             OIC_LOG(ERROR, TAG, "address is not proper");
81             oc_mutex_unlock(deviceListMutex);
82             return CA_STATUS_INVALID_PARAM;
83         }
84
85         CALEState_t *newstate = (CALEState_t*) OICCalloc(1, sizeof(*newstate));
86         if (!newstate)
87         {
88             OIC_LOG(ERROR, TAG, "out of memory");
89             oc_mutex_unlock(deviceListMutex);
90             return CA_MEMORY_ALLOC_FAILED;
91         }
92
93         OICStrcpy(newstate->address, sizeof(newstate->address), address);
94         newstate->mtuSize = CA_DEFAULT_BLE_MTU_SIZE;
95         switch(state_type)
96         {
97             case CA_LE_CONNECTION_STATE:
98                 newstate->connectedState = target_state;
99                 newstate->sendState = STATE_SEND_NONE;
100                 break;
101             case CA_LE_SEND_STATE:
102                 newstate->connectedState = STATE_DISCONNECTED;
103                 newstate->sendState = target_state;
104                 break;
105             default:
106                 break;
107         }
108         OIC_LOG_V(INFO, TAG, "add a new state to List - addr : %s, "
109                   "conn : %d, send : %d, ACFlag : %d",
110                   newstate->address, newstate->connectedState, newstate->sendState,
111                   newstate->autoConnectFlag);
112         u_arraylist_add(deviceList, newstate); // update new state
113     }
114     oc_mutex_unlock(deviceListMutex);
115
116     return CA_STATUS_OK;
117 }
118
119 bool CALEIsDeviceInList(const char* remoteAddress,
120                         u_arraylist_t *deviceList)
121 {
122     VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
123     VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", false);
124
125     uint32_t length = u_arraylist_length(deviceList);
126     for (uint32_t index = 0; index < length; index++)
127     {
128         CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
129         if (!state)
130         {
131             OIC_LOG(ERROR, TAG, "CALEState_t object is null");
132             return false;
133         }
134
135         if (!strcmp(remoteAddress, state->address))
136         {
137             return true;
138         }
139         else
140         {
141             continue;
142         }
143     }
144
145     OIC_LOG(INFO, TAG, "there are no the device in list.");
146     return false;
147 }
148
149 CAResult_t CALERemoveAllDeviceState(u_arraylist_t *deviceList,
150                                     oc_mutex deviceListMutex)
151 {
152     OIC_LOG(DEBUG, TAG, "CALERemoveAllDeviceState");
153     VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
154
155     oc_mutex_lock(deviceListMutex);
156     uint32_t length = u_arraylist_length(deviceList);
157     for (uint32_t index = 0; index < length; index++)
158     {
159         CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
160         if (!state)
161         {
162             OIC_LOG(ERROR, TAG, "jarrayObj is null");
163             continue;
164         }
165         OICFree(state);
166     }
167
168     OICFree(deviceList);
169     deviceList = NULL;
170     oc_mutex_unlock(deviceListMutex);
171
172     return CA_STATUS_OK;
173 }
174
175 CAResult_t CALEResetDeviceStateForAll(u_arraylist_t *deviceList,
176                                       oc_mutex deviceListMutex)
177 {
178     OIC_LOG(DEBUG, TAG, "CALEResetDeviceStateForAll");
179     VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
180
181     oc_mutex_lock(deviceListMutex);
182     size_t length = u_arraylist_length(deviceList);
183     for (size_t index = 0; index < length; index++)
184     {
185         CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
186         if (!state)
187         {
188             OIC_LOG(ERROR, TAG, "jarrayObj is null");
189             continue;
190         }
191
192         // autoConnectFlag value will be not changed,
193         // since it has reset only termination case.
194         state->connectedState = STATE_DISCONNECTED;
195         state->sendState = STATE_SEND_NONE;
196     }
197     oc_mutex_unlock(deviceListMutex);
198
199     return CA_STATUS_OK;
200 }
201
202 CAResult_t CALERemoveDeviceState(const char* remoteAddress,
203                                  u_arraylist_t *deviceList)
204 {
205     OIC_LOG(DEBUG, TAG, "CALERemoveDeviceState");
206     VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
207     VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
208
209     uint32_t length = u_arraylist_length(deviceList);
210     for (uint32_t index = 0; index < length; index++)
211     {
212         CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
213         if (!state)
214         {
215             OIC_LOG(ERROR, TAG, "CALEState_t object is null");
216             continue;
217         }
218
219         if (!strcmp(state->address, remoteAddress))
220         {
221             OIC_LOG_V(DEBUG, TAG, "remove state : %s", state->address);
222
223             CALEState_t* targetState  = (CALEState_t*)u_arraylist_remove(deviceList,
224                                                                          index);
225             if (NULL == targetState)
226             {
227                 OIC_LOG(ERROR, TAG, "List removal failed.");
228                 return CA_STATUS_FAILED;
229             }
230
231             OICFree(targetState);
232             return CA_STATUS_OK;
233         }
234     }
235     return CA_STATUS_OK;
236 }
237
238 CALEState_t* CALEGetStateInfo(const char* remoteAddress,
239                               u_arraylist_t *deviceList)
240 {
241     VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
242     VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", NULL);
243
244     uint32_t length = u_arraylist_length(deviceList);
245
246     for (uint32_t index = 0; index < length; index++)
247     {
248         CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
249         if (!state)
250         {
251             OIC_LOG(ERROR, TAG, "CALEState_t object is null");
252             continue;
253         }
254
255         if (!strcmp(state->address, remoteAddress))
256         {
257             return state;
258         }
259         OIC_LOG_V(DEBUG, TAG, "state addr[%s, %d]", state->address, index);
260     }
261
262     OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in deviceStateList", remoteAddress, length);
263     return NULL;
264 }
265
266 bool CALEIsValidState(const char* remoteAddress,
267                       uint16_t state_type,
268                       uint16_t target_state,
269                       u_arraylist_t *deviceList,
270                       oc_mutex deviceListMutex)
271 {
272     OIC_LOG_V(DEBUG, TAG, "CALEIsValidState : type[%d], target state[%d]",
273               state_type, target_state);
274     VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
275     VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", false);
276
277     oc_mutex_lock(deviceListMutex);
278     CALEState_t* state = CALEGetStateInfo(remoteAddress, deviceList);
279     if (NULL == state)
280     {
281         OIC_LOG(DEBUG, TAG, "state is not updated yet");
282         oc_mutex_unlock(deviceListMutex);
283         return false;
284     }
285
286     uint16_t curValue = 0;
287     switch(state_type)
288     {
289         case CA_LE_CONNECTION_STATE:
290             curValue = state->connectedState;
291             break;
292         case CA_LE_SEND_STATE:
293             curValue = state->sendState;
294             break;
295         default:
296             break;
297     }
298
299     if (target_state == curValue)
300     {
301         oc_mutex_unlock(deviceListMutex);
302         return true;
303     }
304     else
305     {
306         oc_mutex_unlock(deviceListMutex);
307         return false;
308     }
309
310     oc_mutex_unlock(deviceListMutex);
311     return false;
312 }
313
314
315 CAResult_t CALESetFlagToState(JNIEnv *env, jstring jni_address, jint state_idx, jboolean flag,
316                               u_arraylist_t *deviceList, oc_mutex deviceListMutex)
317 {
318     OIC_LOG(DEBUG, TAG, "IN - CALESetFlagToState");
319     VERIFY_NON_NULL(env, TAG, "env");
320     VERIFY_NON_NULL(jni_address, TAG, "jni_address");
321     VERIFY_NON_NULL(deviceList, TAG, "deviceList");
322
323     oc_mutex_lock(deviceListMutex);
324
325     char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
326     if (!address)
327     {
328         OIC_LOG(ERROR, TAG, "address is not available");
329         CACheckJNIException(env);
330         return CA_STATUS_FAILED;
331     }
332
333     if (CALEIsDeviceInList(address, deviceList))
334     {
335         CALEState_t* curState = CALEGetStateInfo(address, deviceList);
336         if(!curState)
337         {
338             OIC_LOG(ERROR, TAG, "curState is null");
339             (*env)->ReleaseStringUTFChars(env, jni_address, address);
340             oc_mutex_unlock(deviceListMutex);
341             return CA_STATUS_FAILED;
342         }
343         OIC_LOG_V(INFO, TAG, "%d flag is set : %d", state_idx, flag);
344
345         switch(state_idx)
346         {
347             case CA_LE_AUTO_CONNECT_FLAG:
348                 curState->autoConnectFlag = flag;
349                 break;
350             case CA_LE_DESCRIPTOR_FOUND:
351                 curState->isDescriptorFound = flag;
352                 break;
353             default:
354                 break;
355         }
356     }
357
358     (*env)->ReleaseStringUTFChars(env, jni_address, address);
359     oc_mutex_unlock(deviceListMutex);
360     OIC_LOG(DEBUG, TAG, "OUT - CALESetFlagToState");
361     return CA_STATUS_OK;
362 }
363
364 jboolean CALEGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx,
365                               u_arraylist_t *deviceList, oc_mutex deviceListMutex)
366 {
367     OIC_LOG(DEBUG, TAG, "IN - CALEGetFlagFromState");
368     VERIFY_NON_NULL_RET(env, TAG, "env", false);
369     VERIFY_NON_NULL_RET(jni_address, TAG, "jni_address", false);
370     VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList", false);
371
372     oc_mutex_lock(deviceListMutex);
373
374     char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
375     if (!address)
376     {
377         OIC_LOG(ERROR, TAG, "address is not available");
378         CACheckJNIException(env);
379         oc_mutex_unlock(deviceListMutex);
380         return JNI_FALSE;
381     }
382
383     CALEState_t* curState = CALEGetStateInfo(address, deviceList);
384     (*env)->ReleaseStringUTFChars(env, jni_address, address);
385     if(!curState)
386     {
387         OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false");
388         oc_mutex_unlock(deviceListMutex);
389         return JNI_FALSE;
390     }
391
392     jboolean ret = JNI_FALSE;
393     switch(state_idx)
394     {
395         case CA_LE_AUTO_CONNECT_FLAG:
396             ret = curState->autoConnectFlag;
397             break;
398         case CA_LE_DESCRIPTOR_FOUND:
399             ret = curState->isDescriptorFound;
400             break;
401         default:
402             break;
403     }
404     oc_mutex_unlock(deviceListMutex);
405
406     OIC_LOG_V(INFO, TAG, "%d flag is %d", state_idx, ret);
407     OIC_LOG(DEBUG, TAG, "OUT - CALEGetFlagFromState");
408     return ret;
409 }
410
411 CAResult_t CALESetMtuSize(const char* address, uint16_t mtuSize,
412                           u_arraylist_t *deviceList, oc_mutex deviceListMutex)
413
414 {
415     VERIFY_NON_NULL(address, TAG, "address is null");
416     VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
417
418     oc_mutex_lock(deviceListMutex);
419     if (CALEIsDeviceInList(address, deviceList))
420     {
421         CALEState_t* curState = CALEGetStateInfo(address, deviceList);
422         if(!curState)
423         {
424             OIC_LOG(ERROR, TAG, "curState is null");
425             oc_mutex_unlock(deviceListMutex);
426             return CA_STATUS_FAILED;
427         }
428
429         curState->mtuSize = mtuSize;
430         OIC_LOG_V(INFO, TAG, "update state - addr: %s, mtu: %d",
431                   curState->address, curState->mtuSize);
432     }
433     else
434     {
435         OIC_LOG(ERROR, TAG, "there is no state info in the list");
436     }
437     oc_mutex_unlock(deviceListMutex);
438     return CA_STATUS_OK;
439 }
440
441 uint16_t CALEGetMtuSize(const char* address, u_arraylist_t *deviceList, oc_mutex deviceListMutex)
442 {
443     VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE);
444     VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", CA_DEFAULT_BLE_MTU_SIZE);
445
446     oc_mutex_lock(deviceListMutex);
447     if (CALEIsDeviceInList(address, deviceList))
448     {
449         CALEState_t* curState = CALEGetStateInfo(address, deviceList);
450         if(!curState)
451         {
452             OIC_LOG(ERROR, TAG, "curState is null");
453             oc_mutex_unlock(deviceListMutex);
454             return CA_DEFAULT_BLE_MTU_SIZE;
455         }
456
457         OIC_LOG_V(INFO, TAG, "state - addr: %s, mtu: %d",
458                   curState->address, curState->mtuSize);
459         oc_mutex_unlock(deviceListMutex);
460         return curState->mtuSize;
461     }
462
463     oc_mutex_unlock(deviceListMutex);
464     return CA_DEFAULT_BLE_MTU_SIZE;
465 }