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 (!strcmp(remoteAddress, state->address))
137 OIC_LOG(DEBUG, TAG, "the device is already set");
146 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
150 CAResult_t CALERemoveAllDeviceState(u_arraylist_t *deviceList,
151 oc_mutex deviceListMutex)
153 OIC_LOG(DEBUG, TAG, "CALERemoveAllDeviceState");
154 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
156 oc_mutex_lock(deviceListMutex);
157 uint32_t length = u_arraylist_length(deviceList);
158 for (uint32_t index = 0; index < length; index++)
160 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
163 OIC_LOG(ERROR, TAG, "jarrayObj is null");
171 oc_mutex_unlock(deviceListMutex);
176 CAResult_t CALEResetDeviceStateForAll(u_arraylist_t *deviceList,
177 oc_mutex deviceListMutex)
179 OIC_LOG(DEBUG, TAG, "CALEClientResetDeviceStateForAll");
180 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
182 oc_mutex_lock(deviceListMutex);
183 size_t length = u_arraylist_length(deviceList);
184 for (size_t index = 0; index < length; index++)
186 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
189 OIC_LOG(ERROR, TAG, "jarrayObj is null");
193 // autoConnectFlag value will be not changed,
194 // since it has reset only termination case.
195 state->connectedState = STATE_DISCONNECTED;
196 state->sendState = STATE_SEND_NONE;
198 oc_mutex_unlock(deviceListMutex);
203 CAResult_t CALERemoveDeviceState(const char* remoteAddress,
204 u_arraylist_t *deviceList)
206 OIC_LOG(DEBUG, TAG, "CALERemoveDeviceState");
207 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
208 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
210 uint32_t length = u_arraylist_length(deviceList);
211 for (uint32_t index = 0; index < length; index++)
213 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
216 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
220 if (!strcmp(state->address, remoteAddress))
222 OIC_LOG_V(DEBUG, TAG, "remove state : %s", state->address);
224 CALEState_t* targetState = (CALEState_t*)u_arraylist_remove(deviceList,
226 if (NULL == targetState)
228 OIC_LOG(ERROR, TAG, "List removal failed.");
229 return CA_STATUS_FAILED;
232 OICFree(targetState);
239 CALEState_t* CALEGetStateInfo(const char* remoteAddress,
240 u_arraylist_t *deviceList)
242 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
243 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", NULL);
245 uint32_t length = u_arraylist_length(deviceList);
246 OIC_LOG_V(DEBUG, TAG, "length of deviceStateList : %d", length);
247 OIC_LOG_V(DEBUG, TAG, "target address : %s", remoteAddress);
249 for (uint32_t index = 0; index < length; index++)
251 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
254 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
258 OIC_LOG_V(DEBUG, TAG, "state address : %s (idx: %d)", state->address, index);
260 if (!strcmp(state->address, remoteAddress))
262 OIC_LOG(DEBUG, TAG, "found state");
267 OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in deviceStateList", remoteAddress);
271 bool CALEIsValidState(const char* remoteAddress,
273 uint16_t target_state,
274 u_arraylist_t *deviceList,
275 oc_mutex deviceListMutex)
277 OIC_LOG_V(DEBUG, TAG, "CALEIsValidState : type[%d], target state[%d]",
278 state_type, target_state);
279 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
280 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", false);
282 oc_mutex_lock(deviceListMutex);
283 CALEState_t* state = CALEGetStateInfo(remoteAddress, deviceList);
286 OIC_LOG(DEBUG, TAG, "state is not updated yet");
287 oc_mutex_unlock(deviceListMutex);
291 uint16_t curValue = 0;
294 case CA_LE_CONNECTION_STATE:
295 curValue = state->connectedState;
297 case CA_LE_SEND_STATE:
298 curValue = state->sendState;
304 if (target_state == curValue)
306 oc_mutex_unlock(deviceListMutex);
311 oc_mutex_unlock(deviceListMutex);
315 oc_mutex_unlock(deviceListMutex);
320 CAResult_t CALESetFlagToState(JNIEnv *env, jstring jni_address, jint state_idx, jboolean flag,
321 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
323 OIC_LOG(DEBUG, TAG, "IN - CALESetFlagToState");
324 VERIFY_NON_NULL(env, TAG, "env");
325 VERIFY_NON_NULL(jni_address, TAG, "jni_address");
326 VERIFY_NON_NULL(deviceList, TAG, "deviceList");
328 oc_mutex_lock(deviceListMutex);
330 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
333 OIC_LOG(ERROR, TAG, "address is not available");
334 CACheckJNIException(env);
335 return CA_STATUS_FAILED;
338 if (CALEIsDeviceInList(address, deviceList))
340 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
343 OIC_LOG(ERROR, TAG, "curState is null");
344 (*env)->ReleaseStringUTFChars(env, jni_address, address);
345 oc_mutex_unlock(deviceListMutex);
346 return CA_STATUS_FAILED;
348 OIC_LOG_V(INFO, TAG, "%d flag is set : %d", state_idx, flag);
352 case CA_LE_AUTO_CONNECT_FLAG:
353 curState->autoConnectFlag = flag;
355 case CA_LE_DESCRIPTOR_FOUND:
356 curState->isDescriptorFound = flag;
363 (*env)->ReleaseStringUTFChars(env, jni_address, address);
364 oc_mutex_unlock(deviceListMutex);
365 OIC_LOG(DEBUG, TAG, "OUT - CALESetFlagToState");
369 jboolean CALEGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx,
370 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
372 OIC_LOG(DEBUG, TAG, "IN - CALEGetFlagFromState");
373 VERIFY_NON_NULL_RET(env, TAG, "env", false);
374 VERIFY_NON_NULL_RET(jni_address, TAG, "jni_address", false);
375 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList", false);
377 oc_mutex_lock(deviceListMutex);
379 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
382 OIC_LOG(ERROR, TAG, "address is not available");
383 CACheckJNIException(env);
384 oc_mutex_unlock(deviceListMutex);
388 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
389 (*env)->ReleaseStringUTFChars(env, jni_address, address);
392 OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false");
393 oc_mutex_unlock(deviceListMutex);
397 jboolean ret = JNI_FALSE;
400 case CA_LE_AUTO_CONNECT_FLAG:
401 ret = curState->autoConnectFlag;
403 case CA_LE_DESCRIPTOR_FOUND:
404 ret = curState->isDescriptorFound;
409 oc_mutex_unlock(deviceListMutex);
411 OIC_LOG_V(INFO, TAG, "%d flag is %d", state_idx, ret);
412 OIC_LOG(DEBUG, TAG, "OUT - CALEGetFlagFromState");
416 CAResult_t CALESetMtuSize(const char* address, uint16_t mtuSize,
417 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
420 VERIFY_NON_NULL(address, TAG, "address is null");
421 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
423 oc_mutex_lock(deviceListMutex);
424 if (CALEIsDeviceInList(address, deviceList))
426 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
429 OIC_LOG(ERROR, TAG, "curState is null");
430 oc_mutex_unlock(deviceListMutex);
431 return CA_STATUS_FAILED;
434 curState->mtuSize = mtuSize;
435 OIC_LOG_V(INFO, TAG, "update state - addr: %s, mtu: %d",
436 curState->address, curState->mtuSize);
440 OIC_LOG(ERROR, TAG, "there is no state info in the list");
442 oc_mutex_unlock(deviceListMutex);
446 uint16_t CALEGetMtuSize(const char* address, u_arraylist_t *deviceList, oc_mutex deviceListMutex)
448 VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE);
449 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", CA_DEFAULT_BLE_MTU_SIZE);
451 oc_mutex_lock(deviceListMutex);
452 if (CALEIsDeviceInList(address, deviceList))
454 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
457 OIC_LOG(ERROR, TAG, "curState is null");
458 oc_mutex_unlock(deviceListMutex);
459 return CA_DEFAULT_BLE_MTU_SIZE;
462 OIC_LOG_V(INFO, TAG, "state - addr: %s, mtu: %d",
463 curState->address, curState->mtuSize);
464 oc_mutex_unlock(deviceListMutex);
465 return curState->mtuSize;
468 oc_mutex_unlock(deviceListMutex);
469 return CA_DEFAULT_BLE_MTU_SIZE;