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 ******************************************************************/
26 #include "calestate.h"
27 #include "caleinterface.h"
28 #include "caadapterutils.h"
29 #include "caleutils.h"
32 #include "oic_malloc.h"
33 #include "oic_string.h"
34 #include "cathreadpool.h" /* for thread pool */
36 #include "uarraylist.h"
38 #define TAG PCF("OIC_CA_LE_STATE")
40 CAResult_t CALEUpdateDeviceState(const char* address,
42 uint16_t target_state,
43 u_arraylist_t *deviceList,
44 oc_mutex deviceListMutex)
46 VERIFY_NON_NULL(address, TAG, "address is null");
47 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
49 oc_mutex_lock(deviceListMutex);
51 if (CALEIsDeviceInList(address, deviceList))
53 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
56 OIC_LOG(ERROR, TAG, "curState is null");
57 oc_mutex_unlock(deviceListMutex);
58 return CA_STATUS_FAILED;
63 case CA_LE_CONNECTION_STATE:
64 curState->connectedState = target_state;
66 case CA_LE_SEND_STATE:
67 curState->sendState = target_state;
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);
76 else /** state is added newly **/
78 if (strlen(address) > CA_MACADDR_SIZE)
80 OIC_LOG(ERROR, TAG, "address is not proper");
81 oc_mutex_unlock(deviceListMutex);
82 return CA_STATUS_INVALID_PARAM;
85 CALEState_t *newstate = (CALEState_t*) OICCalloc(1, sizeof(*newstate));
88 OIC_LOG(ERROR, TAG, "out of memory");
89 oc_mutex_unlock(deviceListMutex);
90 return CA_MEMORY_ALLOC_FAILED;
93 OICStrcpy(newstate->address, sizeof(newstate->address), address);
94 newstate->mtuSize = CA_DEFAULT_BLE_MTU_SIZE;
97 case CA_LE_CONNECTION_STATE:
98 newstate->connectedState = target_state;
99 newstate->sendState = STATE_SEND_NONE;
101 case CA_LE_SEND_STATE:
102 newstate->connectedState = STATE_DISCONNECTED;
103 newstate->sendState = target_state;
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
114 oc_mutex_unlock(deviceListMutex);
119 bool CALEIsDeviceInList(const char* remoteAddress,
120 u_arraylist_t *deviceList)
122 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
123 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", false);
125 uint32_t length = u_arraylist_length(deviceList);
126 for (uint32_t index = 0; index < length; index++)
128 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
131 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
135 if (!strcasecmp(remoteAddress, state->address))
145 OIC_LOG(INFO, TAG, "there are no the device in list.");
149 CAResult_t CALERemoveAllDeviceState(u_arraylist_t *deviceList,
150 oc_mutex deviceListMutex)
152 OIC_LOG(DEBUG, TAG, "CALERemoveAllDeviceState");
153 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
155 oc_mutex_lock(deviceListMutex);
156 uint32_t length = u_arraylist_length(deviceList);
157 for (uint32_t index = 0; index < length; index++)
159 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
162 OIC_LOG(ERROR, TAG, "jarrayObj is null");
168 oc_mutex_unlock(deviceListMutex);
173 CAResult_t CALEResetDeviceStateForAll(u_arraylist_t *deviceList,
174 oc_mutex deviceListMutex)
176 OIC_LOG(DEBUG, TAG, "CALEResetDeviceStateForAll");
177 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
179 oc_mutex_lock(deviceListMutex);
180 size_t length = u_arraylist_length(deviceList);
181 for (size_t index = 0; index < length; index++)
183 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
186 OIC_LOG(ERROR, TAG, "jarrayObj is null");
190 // autoConnectFlag value will be not changed,
191 // since it has reset only termination case.
192 state->connectedState = STATE_DISCONNECTED;
193 state->sendState = STATE_SEND_NONE;
195 oc_mutex_unlock(deviceListMutex);
200 CAResult_t CALERemoveDeviceState(const char* remoteAddress,
201 u_arraylist_t *deviceList)
203 OIC_LOG(DEBUG, TAG, "CALERemoveDeviceState");
204 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
205 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
207 uint32_t length = u_arraylist_length(deviceList);
208 for (uint32_t index = 0; index < length; index++)
210 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
213 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
217 if (!strcasecmp(state->address, remoteAddress))
219 OIC_LOG_V(DEBUG, TAG, "remove state : %s", state->address);
221 CALEState_t* targetState = (CALEState_t*)u_arraylist_remove(deviceList,
223 if (NULL == targetState)
225 OIC_LOG(ERROR, TAG, "List removal failed.");
226 return CA_STATUS_FAILED;
229 OICFree(targetState);
236 CALEState_t* CALEGetStateInfo(const char* remoteAddress,
237 u_arraylist_t *deviceList)
239 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
240 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", NULL);
242 uint32_t length = u_arraylist_length(deviceList);
244 for (uint32_t index = 0; index < length; index++)
246 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
249 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
253 if (!strcasecmp(state->address, remoteAddress))
257 OIC_LOG_V(DEBUG, TAG, "state addr[%s, %d]", state->address, index);
260 OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in deviceStateList", remoteAddress, length);
264 bool CALEIsValidState(const char* remoteAddress,
266 uint16_t target_state,
267 u_arraylist_t *deviceList,
268 oc_mutex deviceListMutex)
270 OIC_LOG_V(DEBUG, TAG, "CALEIsValidState : type[%d], target state[%d]",
271 state_type, target_state);
272 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
273 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", false);
275 oc_mutex_lock(deviceListMutex);
276 CALEState_t* state = CALEGetStateInfo(remoteAddress, deviceList);
279 OIC_LOG(DEBUG, TAG, "state is not updated yet");
280 oc_mutex_unlock(deviceListMutex);
284 uint16_t curValue = 0;
287 case CA_LE_CONNECTION_STATE:
288 curValue = state->connectedState;
290 case CA_LE_SEND_STATE:
291 curValue = state->sendState;
297 if (target_state == curValue)
299 oc_mutex_unlock(deviceListMutex);
304 oc_mutex_unlock(deviceListMutex);
308 oc_mutex_unlock(deviceListMutex);
313 CAResult_t CALESetFlagToState(JNIEnv *env, jstring jni_address, jint state_idx, jboolean flag,
314 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
316 OIC_LOG(DEBUG, TAG, "IN - CALESetFlagToState");
317 VERIFY_NON_NULL(env, TAG, "env");
318 VERIFY_NON_NULL(jni_address, TAG, "jni_address");
319 VERIFY_NON_NULL(deviceList, TAG, "deviceList");
321 oc_mutex_lock(deviceListMutex);
323 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
326 OIC_LOG(ERROR, TAG, "address is not available");
327 CACheckJNIException(env);
328 return CA_STATUS_FAILED;
331 if (CALEIsDeviceInList(address, deviceList))
333 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
336 OIC_LOG(ERROR, TAG, "curState is null");
337 (*env)->ReleaseStringUTFChars(env, jni_address, address);
338 oc_mutex_unlock(deviceListMutex);
339 return CA_STATUS_FAILED;
341 OIC_LOG_V(INFO, TAG, "%d flag is set : %d", state_idx, flag);
345 case CA_LE_AUTO_CONNECT_FLAG:
346 curState->autoConnectFlag = flag;
348 case CA_LE_DESCRIPTOR_FOUND:
349 curState->isDescriptorFound = flag;
356 (*env)->ReleaseStringUTFChars(env, jni_address, address);
357 oc_mutex_unlock(deviceListMutex);
358 OIC_LOG(DEBUG, TAG, "OUT - CALESetFlagToState");
362 jboolean CALEGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx,
363 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
365 OIC_LOG(DEBUG, TAG, "IN - CALEGetFlagFromState");
366 VERIFY_NON_NULL_RET(env, TAG, "env", false);
367 VERIFY_NON_NULL_RET(jni_address, TAG, "jni_address", false);
368 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList", false);
370 oc_mutex_lock(deviceListMutex);
372 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
375 OIC_LOG(ERROR, TAG, "address is not available");
376 CACheckJNIException(env);
377 oc_mutex_unlock(deviceListMutex);
381 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
382 (*env)->ReleaseStringUTFChars(env, jni_address, address);
385 OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false");
386 oc_mutex_unlock(deviceListMutex);
390 jboolean ret = JNI_FALSE;
393 case CA_LE_AUTO_CONNECT_FLAG:
394 ret = curState->autoConnectFlag;
396 case CA_LE_DESCRIPTOR_FOUND:
397 ret = curState->isDescriptorFound;
402 oc_mutex_unlock(deviceListMutex);
404 OIC_LOG_V(INFO, TAG, "%d flag is %d", state_idx, ret);
405 OIC_LOG(DEBUG, TAG, "OUT - CALEGetFlagFromState");
409 CAResult_t CALESetMtuSize(const char* address, uint16_t mtuSize,
410 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
413 VERIFY_NON_NULL(address, TAG, "address is null");
414 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
416 oc_mutex_lock(deviceListMutex);
417 if (CALEIsDeviceInList(address, deviceList))
419 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
422 OIC_LOG(ERROR, TAG, "curState is null");
423 oc_mutex_unlock(deviceListMutex);
424 return CA_STATUS_FAILED;
427 curState->mtuSize = mtuSize;
428 OIC_LOG_V(INFO, TAG, "update state - addr: %s, mtu: %d",
429 curState->address, curState->mtuSize);
433 OIC_LOG(ERROR, TAG, "there is no state info in the list");
435 oc_mutex_unlock(deviceListMutex);
439 uint16_t CALEGetMtuSize(const char* address, u_arraylist_t *deviceList, oc_mutex deviceListMutex)
441 VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE);
442 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", CA_DEFAULT_BLE_MTU_SIZE);
444 oc_mutex_lock(deviceListMutex);
445 if (CALEIsDeviceInList(address, deviceList))
447 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
450 OIC_LOG(ERROR, TAG, "curState is null");
451 oc_mutex_unlock(deviceListMutex);
452 return CA_DEFAULT_BLE_MTU_SIZE;
455 OIC_LOG_V(INFO, TAG, "state - addr: %s, mtu: %d",
456 curState->address, curState->mtuSize);
457 oc_mutex_unlock(deviceListMutex);
458 return curState->mtuSize;
461 oc_mutex_unlock(deviceListMutex);
462 return CA_DEFAULT_BLE_MTU_SIZE;