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))
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");
170 oc_mutex_unlock(deviceListMutex);
175 CAResult_t CALEResetDeviceStateForAll(u_arraylist_t *deviceList,
176 oc_mutex deviceListMutex)
178 OIC_LOG(DEBUG, TAG, "CALEResetDeviceStateForAll");
179 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
181 oc_mutex_lock(deviceListMutex);
182 size_t length = u_arraylist_length(deviceList);
183 for (size_t index = 0; index < length; index++)
185 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
188 OIC_LOG(ERROR, TAG, "jarrayObj is null");
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;
197 oc_mutex_unlock(deviceListMutex);
202 CAResult_t CALERemoveDeviceState(const char* remoteAddress,
203 u_arraylist_t *deviceList)
205 OIC_LOG(DEBUG, TAG, "CALERemoveDeviceState");
206 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
207 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
209 uint32_t length = u_arraylist_length(deviceList);
210 for (uint32_t index = 0; index < length; index++)
212 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
215 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
219 if (!strcmp(state->address, remoteAddress))
221 OIC_LOG_V(DEBUG, TAG, "remove state : %s", state->address);
223 CALEState_t* targetState = (CALEState_t*)u_arraylist_remove(deviceList,
225 if (NULL == targetState)
227 OIC_LOG(ERROR, TAG, "List removal failed.");
228 return CA_STATUS_FAILED;
231 OICFree(targetState);
238 CALEState_t* CALEGetStateInfo(const char* remoteAddress,
239 u_arraylist_t *deviceList)
241 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
242 VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", NULL);
244 uint32_t length = u_arraylist_length(deviceList);
246 for (uint32_t index = 0; index < length; index++)
248 CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index);
251 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
255 if (!strcmp(state->address, remoteAddress))
259 OIC_LOG_V(DEBUG, TAG, "state addr[%s, %d]", state->address, index);
262 OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in deviceStateList", remoteAddress, length);
266 bool CALEIsValidState(const char* remoteAddress,
268 uint16_t target_state,
269 u_arraylist_t *deviceList,
270 oc_mutex deviceListMutex)
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);
277 oc_mutex_lock(deviceListMutex);
278 CALEState_t* state = CALEGetStateInfo(remoteAddress, deviceList);
281 OIC_LOG(DEBUG, TAG, "state is not updated yet");
282 oc_mutex_unlock(deviceListMutex);
286 uint16_t curValue = 0;
289 case CA_LE_CONNECTION_STATE:
290 curValue = state->connectedState;
292 case CA_LE_SEND_STATE:
293 curValue = state->sendState;
299 if (target_state == curValue)
301 oc_mutex_unlock(deviceListMutex);
306 oc_mutex_unlock(deviceListMutex);
310 oc_mutex_unlock(deviceListMutex);
315 CAResult_t CALESetFlagToState(JNIEnv *env, jstring jni_address, jint state_idx, jboolean flag,
316 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
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");
323 oc_mutex_lock(deviceListMutex);
325 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
328 OIC_LOG(ERROR, TAG, "address is not available");
329 CACheckJNIException(env);
330 return CA_STATUS_FAILED;
333 if (CALEIsDeviceInList(address, deviceList))
335 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
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;
343 OIC_LOG_V(INFO, TAG, "%d flag is set : %d", state_idx, flag);
347 case CA_LE_AUTO_CONNECT_FLAG:
348 curState->autoConnectFlag = flag;
350 case CA_LE_DESCRIPTOR_FOUND:
351 curState->isDescriptorFound = flag;
358 (*env)->ReleaseStringUTFChars(env, jni_address, address);
359 oc_mutex_unlock(deviceListMutex);
360 OIC_LOG(DEBUG, TAG, "OUT - CALESetFlagToState");
364 jboolean CALEGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx,
365 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
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);
372 oc_mutex_lock(deviceListMutex);
374 char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL);
377 OIC_LOG(ERROR, TAG, "address is not available");
378 CACheckJNIException(env);
379 oc_mutex_unlock(deviceListMutex);
383 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
384 (*env)->ReleaseStringUTFChars(env, jni_address, address);
387 OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false");
388 oc_mutex_unlock(deviceListMutex);
392 jboolean ret = JNI_FALSE;
395 case CA_LE_AUTO_CONNECT_FLAG:
396 ret = curState->autoConnectFlag;
398 case CA_LE_DESCRIPTOR_FOUND:
399 ret = curState->isDescriptorFound;
404 oc_mutex_unlock(deviceListMutex);
406 OIC_LOG_V(INFO, TAG, "%d flag is %d", state_idx, ret);
407 OIC_LOG(DEBUG, TAG, "OUT - CALEGetFlagFromState");
411 CAResult_t CALESetMtuSize(const char* address, uint16_t mtuSize,
412 u_arraylist_t *deviceList, oc_mutex deviceListMutex)
415 VERIFY_NON_NULL(address, TAG, "address is null");
416 VERIFY_NON_NULL(deviceList, TAG, "deviceList is null");
418 oc_mutex_lock(deviceListMutex);
419 if (CALEIsDeviceInList(address, deviceList))
421 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
424 OIC_LOG(ERROR, TAG, "curState is null");
425 oc_mutex_unlock(deviceListMutex);
426 return CA_STATUS_FAILED;
429 curState->mtuSize = mtuSize;
430 OIC_LOG_V(INFO, TAG, "update state - addr: %s, mtu: %d",
431 curState->address, curState->mtuSize);
435 OIC_LOG(ERROR, TAG, "there is no state info in the list");
437 oc_mutex_unlock(deviceListMutex);
441 uint16_t CALEGetMtuSize(const char* address, u_arraylist_t *deviceList, oc_mutex deviceListMutex)
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);
446 oc_mutex_lock(deviceListMutex);
447 if (CALEIsDeviceInList(address, deviceList))
449 CALEState_t* curState = CALEGetStateInfo(address, deviceList);
452 OIC_LOG(ERROR, TAG, "curState is null");
453 oc_mutex_unlock(deviceListMutex);
454 return CA_DEFAULT_BLE_MTU_SIZE;
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;
463 oc_mutex_unlock(deviceListMutex);
464 return CA_DEFAULT_BLE_MTU_SIZE;