From 2db0692f5031ac793182c16cfbf8746be623c763 Mon Sep 17 00:00:00 2001 From: "jihwan.seo" Date: Mon, 23 Jan 2017 22:15:21 +0900 Subject: [PATCH] To support BLE MTU negotiation (android to android)-> clean up LE state - move common codes related device state calestate for android BLE Change-Id: Iccfc0250fd1522d098851ad072c067bd72012d3c Signed-off-by: jihwan.seo Reviewed-on: https://gerrit.iotivity.org/gerrit/16733 Tested-by: jenkins-iotivity Reviewed-by: Jaehong Jo Reviewed-by: Ashok Babu Channa --- .../src/bt_le_adapter/android/SConscript | 3 +- .../src/bt_le_adapter/android/caleclient.c | 626 ++++----------------- .../src/bt_le_adapter/android/caleclient.h | 102 +--- .../src/bt_le_adapter/android/caleserver.c | 3 + .../src/bt_le_adapter/android/calestate.c | 470 ++++++++++++++++ .../src/bt_le_adapter/android/calestate.h | 178 ++++++ .../src/bt_le_adapter/android/caleutils.h | 9 + 7 files changed, 801 insertions(+), 590 deletions(-) create mode 100644 resource/csdk/connectivity/src/bt_le_adapter/android/calestate.c create mode 100644 resource/csdk/connectivity/src/bt_le_adapter/android/calestate.h diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript b/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript index 7ee11e6..a12b289 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript @@ -10,6 +10,7 @@ env.PrependUnique(CPPPATH = [ os.path.join(src_dir, 'android') ]) src_files = [ 'caleclient.c', 'caleserver.c', 'calenwmonitor.c', - 'caleutils.c' ] + 'caleutils.c', + 'calestate.c' ] Return('src_files') diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c index fef544b..68314c7 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -23,6 +23,7 @@ #include #include +#include "calestate.h" #include "caleclient.h" #include "caleserver.h" #include "caleutils.h" @@ -555,10 +556,11 @@ void CALEClientTerminate() g_uuidList = NULL; } - ret = CALEClientRemoveAllDeviceState(); + ret = CALERemoveAllDeviceState(g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != ret) { - OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALERemoveAllDeviceState has failed"); } ret = CALEClientRemoveAllScanDevices(env); @@ -1006,14 +1008,18 @@ CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* oc_mutex_unlock(g_threadSendMutex); OIC_LOG(INFO, TAG, "unicast - send logic has finished"); - if (CALEClientIsValidState(address, CA_LE_SEND_STATE, - STATE_SEND_SUCCESS)) + if (CALEIsValidState(address, CA_LE_SEND_STATE, + STATE_SEND_SUCCESS, + g_deviceStateList, + g_deviceStateListMutex)) { OIC_LOG(INFO, TAG, "send success"); ret = CA_STATUS_OK; } - else if (CALEClientIsValidState(address, CA_LE_SEND_STATE, - STATE_SEND_MTU_NEGO_SUCCESS)) + else if (CALEIsValidState(address, CA_LE_SEND_STATE, + STATE_SEND_MTU_NEGO_SUCCESS, + g_deviceStateList, + g_deviceStateListMutex)) { OIC_LOG(INFO, TAG, "mtu nego success"); ret = CA_STATUS_OK; @@ -1025,11 +1031,13 @@ CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* } // reset send state - CAResult_t resetRet = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE, - STATE_SEND_NONE); + CAResult_t resetRet = CALEUpdateDeviceState(address, CA_LE_SEND_STATE, + STATE_SEND_NONE, + g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != resetRet) { - OIC_LOG_V(ERROR, TAG, "CALEClientUpdateDeviceState has failed (%d)", resetRet); + OIC_LOG_V(ERROR, TAG, "CALEUpdateDeviceState has failed (%d)", resetRet); ret = CA_SEND_FAILED; } @@ -1142,7 +1150,7 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device) return CA_STATUS_FAILED; } oc_mutex_lock(g_deviceStateListMutex); - state = CALEClientGetStateInfo(address); + state = CALEGetStateInfo(address, g_deviceStateList); oc_mutex_unlock(g_deviceStateListMutex); } @@ -1193,8 +1201,10 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device) } else { - if (CALEClientIsValidState(address, CA_LE_CONNECTION_STATE, - STATE_SERVICE_CONNECTED)) + if (CALEIsValidState(address, CA_LE_CONNECTION_STATE, + STATE_SERVICE_CONNECTED, + g_deviceStateList, + g_deviceStateListMutex)) { OIC_LOG(INFO, TAG, "GATT has already connected"); @@ -1215,13 +1225,17 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device) } (*env)->ReleaseStringUTFChars(env, jni_address, address); } - else if(CALEClientIsValidState(address, CA_LE_CONNECTION_STATE, - STATE_CONNECTED)) + else if(CALEIsValidState(address, CA_LE_CONNECTION_STATE, + STATE_CONNECTED, + g_deviceStateList, + g_deviceStateListMutex)) { OIC_LOG(INFO, TAG, "service connecting..."); } - else if(CALEClientIsValidState(address, CA_LE_CONNECTION_STATE, - STATE_DISCONNECTED)) + else if(CALEIsValidState(address, CA_LE_CONNECTION_STATE, + STATE_DISCONNECTED, + g_deviceStateList, + g_deviceStateListMutex)) { OIC_LOG(INFO, TAG, "STATE_DISCONNECTED - start to connect LE"); @@ -1243,8 +1257,10 @@ CAResult_t CALEClientSendData(JNIEnv *env, jobject device) OIC_LOG(DEBUG, TAG, "start to connect LE"); jobject gatt = CALEClientConnect(env, device, - CALEClientGetFlagFromState(env, jni_address, - CA_LE_AUTO_CONNECT_FLAG)); + CALEGetFlagFromState(env, jni_address, + CA_LE_AUTO_CONNECT_FLAG, + g_deviceStateList, + g_deviceStateListMutex)); if (NULL == gatt) { @@ -1686,98 +1702,6 @@ CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback) return CA_STATUS_OK; } -CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address, jint state_idx, jboolean flag) -{ - OIC_LOG(DEBUG, TAG, "IN - CALEClientSetFlagToState"); - VERIFY_NON_NULL(env, TAG, "env"); - VERIFY_NON_NULL(jni_address, TAG, "jni_address"); - - oc_mutex_lock(g_deviceStateListMutex); - - char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - OIC_LOG(ERROR, TAG, "address is not available"); - CACheckJNIException(env); - return CA_STATUS_FAILED; - } - - if (CALEClientIsDeviceInList(address)) - { - CALEState_t* curState = CALEClientGetStateInfo(address); - if(!curState) - { - OIC_LOG(ERROR, TAG, "curState is null"); - (*env)->ReleaseStringUTFChars(env, jni_address, address); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_STATUS_FAILED; - } - OIC_LOG_V(INFO, TAG, "%d flag is set : %d", state_idx, flag); - - switch(state_idx) - { - case CA_LE_AUTO_CONNECT_FLAG: - curState->autoConnectFlag = flag; - break; - case CA_LE_DESCRIPTOR_FOUND: - curState->isDescriptorFound = flag; - break; - default: - break; - } - } - - (*env)->ReleaseStringUTFChars(env, jni_address, address); - oc_mutex_unlock(g_deviceStateListMutex); - OIC_LOG(DEBUG, TAG, "OUT - CALEClientSetFlagToState"); - return CA_STATUS_OK; -} - -jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx) -{ - OIC_LOG(DEBUG, TAG, "IN - CALEClientGetFlagFromState"); - VERIFY_NON_NULL_RET(env, TAG, "env", false); - VERIFY_NON_NULL_RET(jni_address, TAG, "jni_address", false); - - oc_mutex_lock(g_deviceStateListMutex); - - char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - OIC_LOG(ERROR, TAG, "address is not available"); - CACheckJNIException(env); - oc_mutex_unlock(g_deviceStateListMutex); - return JNI_FALSE; - } - - CALEState_t* curState = CALEClientGetStateInfo(address); - (*env)->ReleaseStringUTFChars(env, jni_address, address); - if(!curState) - { - OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false"); - oc_mutex_unlock(g_deviceStateListMutex); - return JNI_FALSE; - } - - jboolean ret = JNI_FALSE; - switch(state_idx) - { - case CA_LE_AUTO_CONNECT_FLAG: - ret = curState->autoConnectFlag; - break; - case CA_LE_DESCRIPTOR_FOUND: - ret = curState->isDescriptorFound; - break; - default: - break; - } - oc_mutex_unlock(g_deviceStateListMutex); - - OIC_LOG_V(INFO, TAG, "%d flag is %d", state_idx, ret); - OIC_LOG(DEBUG, TAG, "OUT - CALEClientGetFlagFromState"); - return ret; -} - CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect) { OIC_LOG(DEBUG, TAG, "CALEClientDirectConnect"); @@ -1804,8 +1728,10 @@ CAResult_t CALEClientDirectConnect(JNIEnv *env, jobject bluetoothDevice, jboolea } CAResult_t res = CA_STATUS_OK; - if(CALEClientIsValidState(address, CA_LE_CONNECTION_STATE, - STATE_DISCONNECTED)) + if(CALEIsValidState(address, CA_LE_CONNECTION_STATE, + STATE_DISCONNECTED, + g_deviceStateList, + g_deviceStateListMutex)) { jobject newGatt = CALEClientConnect(env, bluetoothDevice, autoconnect); if (NULL == newGatt) @@ -1978,8 +1904,10 @@ jobject CALEClientGattConnect(JNIEnv *env, jobject bluetoothDevice, jboolean aut bool CALEClientIsConnected(const char* address) { - if (CALEClientIsValidState(address, CA_LE_CONNECTION_STATE, - STATE_SERVICE_CONNECTED)) + if (CALEIsValidState(address, CA_LE_CONNECTION_STATE, + STATE_SERVICE_CONNECTED, + g_deviceStateList, + g_deviceStateListMutex)) { OIC_LOG(DEBUG, TAG, "current state is connected"); return true; @@ -2325,7 +2253,9 @@ CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv* env, jobject gatt) oc_mutex_lock(g_threadSendStateMutex); - if (CALEClientIsValidState(address, CA_LE_SEND_STATE, STATE_SENDING)) + if (CALEIsValidState(address, CA_LE_SEND_STATE, STATE_SENDING, + g_deviceStateList, + g_deviceStateListMutex)) { OIC_LOG(INFO, TAG, "current state is SENDING"); (*env)->ReleaseStringUTFChars(env, jni_address, address); @@ -2333,10 +2263,12 @@ CAResult_t CALESetValueAndWriteCharacteristic(JNIEnv* env, jobject gatt) return CA_STATUS_OK; } - if (CA_STATUS_OK != CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE, - STATE_SENDING)) + if (CA_STATUS_OK != CALEUpdateDeviceState(address, CA_LE_SEND_STATE, + STATE_SENDING, + g_deviceStateList, + g_deviceStateListMutex)) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); (*env)->ReleaseStringUTFChars(env, jni_address, address); CALEClientSendFinish(env, gatt); oc_mutex_unlock(g_threadSendStateMutex); @@ -3668,17 +3600,21 @@ CAResult_t CALEClientUpdateDeviceStateWithBtDevice(JNIEnv *env, return CA_STATUS_FAILED; } - if (CALEClientIsValidState(address, state_type, target_state)) + if (CALEIsValidState(address, state_type, target_state, + g_deviceStateList, + g_deviceStateListMutex)) { (*env)->DeleteLocalRef(env, jni_Address); return CA_STATUS_OK; } - CAResult_t res = CALEClientUpdateDeviceState(address, state_type, - target_state); + CAResult_t res = CALEUpdateDeviceState(address, state_type, + target_state, + g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); } (*env)->ReleaseStringUTFChars(env, jni_Address, address); (*env)->DeleteLocalRef(env, jni_Address); @@ -3686,359 +3622,22 @@ CAResult_t CALEClientUpdateDeviceStateWithBtDevice(JNIEnv *env, return res; } -CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize) +CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address, + jint state_idx, jboolean flag) { - VERIFY_NON_NULL(address, TAG, "address is null"); - - oc_mutex_lock(g_deviceStateListMutex); - if (CALEClientIsDeviceInList(address)) - { - CALEState_t* curState = CALEClientGetStateInfo(address); - if(!curState) - { - OIC_LOG(ERROR, TAG, "curState is null"); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_STATUS_FAILED; - } - - curState->mtuSize = mtuSize; - OIC_LOG_V(INFO, TAG, "update state - addr: %s, mtu: %d", - curState->address, curState->mtuSize); - } - else - { - OIC_LOG(ERROR, TAG, "there is no state info in the list"); - } - oc_mutex_unlock(g_deviceStateListMutex); - return CA_STATUS_OK; + return CALESetFlagToState(env, jni_address, state_idx, flag, + g_deviceStateList, g_deviceStateListMutex); } -uint16_t CALEClientGetMtuSize(const char* address) -{ - VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE); - - oc_mutex_lock(g_deviceStateListMutex); - if (CALEClientIsDeviceInList(address)) - { - CALEState_t* curState = CALEClientGetStateInfo(address); - if(!curState) - { - OIC_LOG(ERROR, TAG, "curState is null"); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_DEFAULT_BLE_MTU_SIZE; - } - - OIC_LOG_V(INFO, TAG, "state - addr: %s, mtu: %d", - curState->address, curState->mtuSize); - oc_mutex_unlock(g_deviceStateListMutex); - return curState->mtuSize; - } - - oc_mutex_unlock(g_deviceStateListMutex); - return CA_DEFAULT_BLE_MTU_SIZE; -} - -CAResult_t CALEClientUpdateDeviceState(const char* address, uint16_t state_type, - uint16_t target_state) -{ - VERIFY_NON_NULL(address, TAG, "address is null"); - - if (!g_deviceStateList) - { - OIC_LOG(ERROR, TAG, "gdevice_list is null"); - return CA_STATUS_FAILED; - } - - oc_mutex_lock(g_deviceStateListMutex); - - if (CALEClientIsDeviceInList(address)) - { - CALEState_t* curState = CALEClientGetStateInfo(address); - if(!curState) - { - OIC_LOG(ERROR, TAG, "curState is null"); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_STATUS_FAILED; - } - - switch(state_type) - { - case CA_LE_CONNECTION_STATE: - curState->connectedState = target_state; - break; - case CA_LE_SEND_STATE: - curState->sendState = target_state; - break; - default: - break; - } - OIC_LOG_V(INFO, TAG, "update state - addr: %s, conn: %d, send: %d, ACFlag: %d, mtu: %d", - curState->address, curState->connectedState, curState->sendState, - curState->autoConnectFlag, curState->mtuSize); - } - else /** state is added newly **/ - { - if (strlen(address) > CA_MACADDR_SIZE) - { - OIC_LOG(ERROR, TAG, "address is not proper"); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_STATUS_INVALID_PARAM; - } - - CALEState_t *newstate = (CALEState_t*) OICCalloc(1, sizeof(*newstate)); - if (!newstate) - { - OIC_LOG(ERROR, TAG, "out of memory"); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_MEMORY_ALLOC_FAILED; - } - - OICStrcpy(newstate->address, sizeof(newstate->address), address); - newstate->mtuSize = CA_DEFAULT_BLE_MTU_SIZE; - switch(state_type) - { - case CA_LE_CONNECTION_STATE: - newstate->connectedState = target_state; - newstate->sendState = STATE_SEND_NONE; - break; - case CA_LE_SEND_STATE: - newstate->connectedState = STATE_DISCONNECTED; - newstate->sendState = target_state; - break; - default: - break; - } - OIC_LOG_V(INFO, TAG, "add a new state to List - addr : %s, " - "conn : %d, send : %d, ACFlag : %d", - newstate->address, newstate->connectedState, newstate->sendState, - newstate->autoConnectFlag); - u_arraylist_add(g_deviceStateList, newstate); // update new state - } - oc_mutex_unlock(g_deviceStateListMutex); - - return CA_STATUS_OK; -} - -bool CALEClientIsDeviceInList(const char* remoteAddress) -{ - VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false); - - if (!g_deviceStateList) - { - OIC_LOG(ERROR, TAG, "g_deviceStateList is null"); - return false; - } - - uint32_t length = u_arraylist_length(g_deviceStateList); - for (uint32_t index = 0; index < length; index++) - { - CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index); - if (!state) - { - OIC_LOG(ERROR, TAG, "CALEState_t object is null"); - return false; - } - - if (!strcmp(remoteAddress, state->address)) - { - OIC_LOG(DEBUG, TAG, "the device is already set"); - return true; - } - else - { - continue; - } - } - - OIC_LOG(DEBUG, TAG, "there are no the device in list."); - return false; -} - -CAResult_t CALEClientRemoveAllDeviceState() -{ - OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllDeviceState"); - - oc_mutex_lock(g_deviceStateListMutex); - if (!g_deviceStateList) - { - OIC_LOG(ERROR, TAG, "g_deviceStateList is null"); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_STATUS_FAILED; - } - - uint32_t length = u_arraylist_length(g_deviceStateList); - for (uint32_t index = 0; index < length; index++) - { - CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index); - if (!state) - { - OIC_LOG(ERROR, TAG, "jarrayObj is null"); - continue; - } - OICFree(state); - } - - OICFree(g_deviceStateList); - g_deviceStateList = NULL; - oc_mutex_unlock(g_deviceStateListMutex); - - return CA_STATUS_OK; -} - -CAResult_t CALEClientResetDeviceStateForAll() -{ - OIC_LOG(DEBUG, TAG, "CALEClientResetDeviceStateForAll"); - - oc_mutex_lock(g_deviceStateListMutex); - if (!g_deviceStateList) - { - OIC_LOG(ERROR, TAG, "g_deviceStateList is null"); - oc_mutex_unlock(g_deviceStateListMutex); - return CA_STATUS_FAILED; - } - - size_t length = u_arraylist_length(g_deviceStateList); - for (size_t index = 0; index < length; index++) - { - CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index); - if (!state) - { - OIC_LOG(ERROR, TAG, "jarrayObj is null"); - continue; - } - - // autoConnectFlag value will be not changed, - // since it has reset only termination case. - state->connectedState = STATE_DISCONNECTED; - state->sendState = STATE_SEND_NONE; - } - oc_mutex_unlock(g_deviceStateListMutex); - - return CA_STATUS_OK; -} - -CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress) -{ - OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState"); - VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null"); - - if (!g_deviceStateList) - { - OIC_LOG(ERROR, TAG, "g_deviceStateList is null"); - return CA_STATUS_FAILED; - } - - uint32_t length = u_arraylist_length(g_deviceStateList); - for (uint32_t index = 0; index < length; index++) - { - CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index); - if (!state) - { - OIC_LOG(ERROR, TAG, "CALEState_t object is null"); - continue; - } - - if (!strcmp(state->address, remoteAddress)) - { - OIC_LOG_V(DEBUG, TAG, "remove state : %s", state->address); - - CALEState_t* targetState = (CALEState_t*)u_arraylist_remove(g_deviceStateList, - index); - if (NULL == targetState) - { - OIC_LOG(ERROR, TAG, "List removal failed."); - return CA_STATUS_FAILED; - } - - OICFree(targetState); - return CA_STATUS_OK; - } - } - - return CA_STATUS_OK; -} - -CALEState_t* CALEClientGetStateInfo(const char* remoteAddress) +jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx) { - VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL); - - if (!g_deviceStateList) - { - OIC_LOG(ERROR, TAG, "g_deviceStateList is null"); - return NULL; - } - - uint32_t length = u_arraylist_length(g_deviceStateList); - OIC_LOG_V(DEBUG, TAG, "length of deviceStateList : %d", length); - OIC_LOG_V(DEBUG, TAG, "target address : %s", remoteAddress); - - for (uint32_t index = 0; index < length; index++) - { - CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index); - if (!state) - { - OIC_LOG(ERROR, TAG, "CALEState_t object is null"); - continue; - } - - OIC_LOG_V(DEBUG, TAG, "state address : %s (idx: %d)", state->address, index); - - if (!strcmp(state->address, remoteAddress)) - { - OIC_LOG(DEBUG, TAG, "found state"); - return state; - } - } - - OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in deviceStateList", remoteAddress); - return NULL; + return CALEGetFlagFromState(env, jni_address, state_idx, g_deviceStateList, + g_deviceStateListMutex); } -bool CALEClientIsValidState(const char* remoteAddress, uint16_t state_type, - uint16_t target_state) +uint16_t CALEClientGetMtuSize(const char* address) { - OIC_LOG_V(DEBUG, TAG, "CALEClientIsValidState : type[%d], target state[%d]", - state_type, target_state); - VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false); - - oc_mutex_lock(g_deviceStateListMutex); - if (!g_deviceStateList) - { - OIC_LOG(ERROR, TAG, "g_deviceStateList is null"); - oc_mutex_unlock(g_deviceStateListMutex); - return false; - } - - CALEState_t* state = CALEClientGetStateInfo(remoteAddress); - if (NULL == state) - { - OIC_LOG(DEBUG, TAG, "state is not updated yet"); - oc_mutex_unlock(g_deviceStateListMutex); - return false; - } - - uint16_t curValue = 0; - switch(state_type) - { - case CA_LE_CONNECTION_STATE: - curValue = state->connectedState; - break; - case CA_LE_SEND_STATE: - curValue = state->sendState; - break; - default: - break; - } - oc_mutex_unlock(g_deviceStateListMutex); - - if (target_state == curValue) - { - return true; - } - else - { - return false; - } + return CALEGetMtuSize(address, g_deviceStateList, g_deviceStateListMutex); } void CALEClientCreateDeviceList() @@ -4068,6 +3667,11 @@ void CALEClientCreateDeviceList() } } +CAResult_t CALEClientResetDeviceStateForAll() +{ + return CALEResetDeviceStateForAll(g_deviceStateList, g_deviceStateListMutex); +} + /** * Check Sent Count for remove g_sendBuffer */ @@ -4530,13 +4134,15 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(J OIC_LOG(DEBUG, TAG, "LE is connected"); if (GATT_SUCCESS == status) { - CAResult_t res = CALEClientUpdateDeviceState(address, - CA_LE_CONNECTION_STATE, - STATE_CONNECTED); + CAResult_t res = CALEUpdateDeviceState(address, + CA_LE_CONNECTION_STATE, + STATE_CONNECTED, + g_deviceStateList, + g_deviceStateListMutex); (*env)->ReleaseStringUTFChars(env, jni_address, address); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); goto error_exit; } @@ -4564,24 +4170,29 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(J { OIC_LOG(DEBUG, TAG, "LE is disconnected"); - if (CALEClientIsValidState(address, CA_LE_SEND_STATE, STATE_SEND_PREPARING)) + if (CALEIsValidState(address, CA_LE_SEND_STATE, STATE_SEND_PREPARING, + g_deviceStateList, g_deviceStateListMutex)) { OIC_LOG(INFO, TAG, "current state is STATE_SEND_PREPARING"); - CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE, - STATE_SEND_FAIL); + CAResult_t res = CALEUpdateDeviceState(address, CA_LE_SEND_STATE, + STATE_SEND_FAIL, + g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); } } - CAResult_t res = CALEClientUpdateDeviceState(address, - CA_LE_CONNECTION_STATE, - STATE_DISCONNECTED); + CAResult_t res = CALEUpdateDeviceState(address, + CA_LE_CONNECTION_STATE, + STATE_DISCONNECTED, + g_deviceStateList, + g_deviceStateListMutex); (*env)->ReleaseStringUTFChars(env, jni_address, address); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); goto error_exit; } @@ -4679,18 +4290,21 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIE { OIC_LOG_V(INFO, TAG, "Descriptor is not found : %d", res); - res = CALEClientSetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_FALSE); + res = CALESetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_FALSE, + g_deviceStateList, g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientSetFlagToState has failed"); + OIC_LOG(ERROR, TAG, "CALESetFlagToState has failed"); goto error_exit; } - res = CALEClientUpdateDeviceState(address, CA_LE_CONNECTION_STATE, - STATE_SERVICE_CONNECTED); + res = CALEUpdateDeviceState(address, CA_LE_CONNECTION_STATE, + STATE_SERVICE_CONNECTED, + g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); goto error_exit; } @@ -4703,10 +4317,11 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIE } else { - res = CALEClientSetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_TRUE); + res = CALESetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_TRUE, + g_deviceStateList, g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientSetFlagToState has failed"); + OIC_LOG(ERROR, TAG, "CALESetFlagToState has failed"); goto error_exit; } } @@ -4770,11 +4385,13 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback( oc_cond_signal(g_threadWriteCharacteristicCond); oc_mutex_unlock(g_threadWriteCharacteristicMutex); - CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE, - STATE_SEND_FAIL); + CAResult_t res = CALEUpdateDeviceState(address, CA_LE_SEND_STATE, + STATE_SEND_FAIL, + g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); } if (g_clientErrorCallback) @@ -4793,11 +4410,13 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback( else { OIC_LOG(DEBUG, TAG, "send success"); - CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE, - STATE_SEND_SUCCESS); + CAResult_t res = CALEUpdateDeviceState(address, CA_LE_SEND_STATE, + STATE_SEND_SUCCESS, + g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); } oc_mutex_lock(g_threadWriteCharacteristicMutex); @@ -4917,12 +4536,14 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv goto error_exit; } - CAResult_t res = CALEClientUpdateDeviceState(address, CA_LE_CONNECTION_STATE, - STATE_SERVICE_CONNECTED); + CAResult_t res = CALEUpdateDeviceState(address, CA_LE_CONNECTION_STATE, + STATE_SERVICE_CONNECTED, + g_deviceStateList, + g_deviceStateListMutex); (*env)->ReleaseStringUTFChars(env, jni_address, address); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); goto error_exit; } @@ -4983,17 +4604,20 @@ Java_org_iotivity_ca_CaLeClientInterface_caLeGattMtuChangedCallback(JNIEnv *env, } // update mtu size - CAResult_t res = CALEClientSetMtuSize(address, mtu - CA_BLE_MTU_HEADER_SIZE); + CAResult_t res = CALESetMtuSize(address, mtu - CA_BLE_MTU_HEADER_SIZE, + g_deviceStateList, g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientSetMtuSize has failed"); + OIC_LOG(ERROR, TAG, "CALESetMtuSize has failed"); } - res = CALEClientUpdateDeviceState(address, CA_LE_SEND_STATE, - STATE_SEND_MTU_NEGO_SUCCESS); + res = CALEUpdateDeviceState(address, CA_LE_SEND_STATE, + STATE_SEND_MTU_NEGO_SUCCESS, + g_deviceStateList, + g_deviceStateListMutex); if (CA_STATUS_OK != res) { - OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed"); + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); } CALEClientUpdateSendCnt(env); (*env)->ReleaseStringUTFChars(env, jni_address, address); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h index a9f62d4..5e7fdbf 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h @@ -25,6 +25,7 @@ #ifndef CA_LECLIENT_H_ #define CA_LECLIENT_H_ +#include "calestate.h" #include "cacommon.h" #include "cathreadpool.h" #include "uarraylist.h" @@ -35,25 +36,6 @@ extern "C" { #endif -static const uint16_t GATT_ERROR = 133; - -static const uint16_t STATE_SEND_NONE = 1; -static const uint16_t STATE_SEND_SUCCESS = 2; -static const uint16_t STATE_SEND_FAIL = 3; -static const uint16_t STATE_SENDING = 4; -static const uint16_t STATE_SEND_PREPARING = 5; -static const uint16_t STATE_SEND_MTU_NEGO_SUCCESS = 6; - -typedef struct le_state_info -{ - char address[CA_MACADDR_SIZE]; - uint16_t connectedState; - uint16_t sendState; - jboolean autoConnectFlag; - jboolean isDescriptorFound; - uint16_t mtuSize; -} CALEState_t; - /** * BLE Scanning State. */ @@ -276,25 +258,6 @@ CAResult_t CALEClientStopScan(); CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback); /** - * set flag into State List. - * @param[in] env JNI interface pointer. - * @param[in] jni_address remote address. - * @param[in] state_idx state index. - * @param[in] flag auto connect flag. - */ -CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address, - jint state_idx, jboolean flag); - -/** - * get flag from State List. - * @param[in] env JNI interface pointer. - * @param[in] jni_address remote address. - * @param[in] state_idx state index. - * @return current flag; - */ -jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx); - -/** * connect to gatt server hosted. * @param[in] env JNI interface pointer. * @param[in] bluetoothDevice bluetooth Device object. @@ -531,23 +494,23 @@ CAResult_t CALEClientUpdateDeviceStateWithBtDevice(JNIEnv *env, uint16_t target_state); /** - * update new state information. - * @param[in] address remote address. - * @param[in] state_type state type. - * @param[in] target_state state index to update. - * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + * set flag into State List. + * @param[in] env JNI interface pointer. + * @param[in] jni_address remote address. + * @param[in] state_idx state index. + * @param[in] flag auto connect flag. */ -CAResult_t CALEClientUpdateDeviceState(const char* address, uint16_t state_type, - uint16_t target_state); +CAResult_t CALEClientSetFlagToState(JNIEnv *env, jstring jni_address, + jint state_idx, jboolean flag); /** - * This function is used to set MTU size - * which negotiated between client and server device. - * @param[in] address remote address. - * @param[in] mtuSize MTU size. - * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + * get flag from State List. + * @param[in] env JNI interface pointer. + * @param[in] jni_address remote address. + * @param[in] state_idx state index. + * @return current flag; */ -CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize); +jboolean CALEClientGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx); /** * get MTU size. @@ -557,19 +520,6 @@ CAResult_t CALEClientSetMtuSize(const char* address, uint16_t mtuSize); uint16_t CALEClientGetMtuSize(const char* address); /** - * check whether the remote address is existed or not. - * @param[in] address remote address. - * @return true or false. - */ -bool CALEClientIsDeviceInList(const char *remoteAddress); - -/** - * remove all device states. - * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). - */ -CAResult_t CALEClientRemoveAllDeviceState(); - -/** * Reset values of device state for all of devices. * this method has to be invoked when BT adapter is disabled. * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). @@ -577,30 +527,6 @@ CAResult_t CALEClientRemoveAllDeviceState(); CAResult_t CALEClientResetDeviceStateForAll(); /** - * remove the device state for a remote device. - * @param[in] remoteAddress remote address. - * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). - */ -CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress); - -/** - * get state information for a remote device. - * @param[in] remoteAddress remote address. - * @return CALEState_t. - */ -CALEState_t* CALEClientGetStateInfo(const char* remoteAddress); - -/** - * check whether the remote address has same state with target state. - * @param[in] remoteAddress remote address. - * @param[in] state_type state_type. - * @param[in] target_state state index to check. - * @return true or false. - */ -bool CALEClientIsValidState(const char* remoteAddress, uint16_t state_type, - uint16_t target_state); - -/** * create scan device list. */ void CALEClientCreateDeviceList(); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c index 8350f4a..977c356 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c @@ -51,6 +51,7 @@ static CAPacketReceiveCallback g_packetReceiveCallback = NULL; static CABLEErrorHandleCallback g_serverErrorCallback; static u_arraylist_t *g_connectedDeviceList = NULL; +static u_arraylist_t *g_deviceStateList = NULL; static bool g_isStartServer = false; static bool g_isInitializedServer = false; @@ -68,6 +69,8 @@ static oc_mutex g_threadSendNotifyMutex = NULL; static oc_cond g_threadSendNotifyCond = NULL; static bool g_isSignalSetFlag = false; +static oc_mutex g_deviceStateListMutex = NULL; + static jint g_state_connected = INVALID_STATE; static jint g_state_disconnected = INVALID_STATE; diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/calestate.c b/resource/csdk/connectivity/src/bt_le_adapter/android/calestate.c new file mode 100644 index 0000000..206f46c --- /dev/null +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/calestate.c @@ -0,0 +1,470 @@ +/****************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include +#include +#include +#include + +#include "calestate.h" +#include "caleinterface.h" +#include "caadapterutils.h" +#include "caleutils.h" + +#include "logger.h" +#include "oic_malloc.h" +#include "oic_string.h" +#include "cathreadpool.h" /* for thread pool */ +#include "octhread.h" +#include "uarraylist.h" + +#define TAG PCF("OIC_CA_LE_STATE") + +CAResult_t CALEUpdateDeviceState(const char* address, + uint16_t state_type, + uint16_t target_state, + u_arraylist_t *deviceList, + oc_mutex deviceListMutex) +{ + VERIFY_NON_NULL(address, TAG, "address is null"); + VERIFY_NON_NULL(deviceList, TAG, "deviceList is null"); + + oc_mutex_lock(deviceListMutex); + + if (CALEIsDeviceInList(address, deviceList)) + { + CALEState_t* curState = CALEGetStateInfo(address, deviceList); + if(!curState) + { + OIC_LOG(ERROR, TAG, "curState is null"); + oc_mutex_unlock(deviceListMutex); + return CA_STATUS_FAILED; + } + + switch(state_type) + { + case CA_LE_CONNECTION_STATE: + curState->connectedState = target_state; + break; + case CA_LE_SEND_STATE: + curState->sendState = target_state; + break; + default: + break; + } + OIC_LOG_V(INFO, TAG, "update state - addr: %s, conn: %d, send: %d, ACFlag: %d, mtu: %d", + curState->address, curState->connectedState, curState->sendState, + curState->autoConnectFlag, curState->mtuSize); + } + else /** state is added newly **/ + { + if (strlen(address) > CA_MACADDR_SIZE) + { + OIC_LOG(ERROR, TAG, "address is not proper"); + oc_mutex_unlock(deviceListMutex); + return CA_STATUS_INVALID_PARAM; + } + + CALEState_t *newstate = (CALEState_t*) OICCalloc(1, sizeof(*newstate)); + if (!newstate) + { + OIC_LOG(ERROR, TAG, "out of memory"); + oc_mutex_unlock(deviceListMutex); + return CA_MEMORY_ALLOC_FAILED; + } + + OICStrcpy(newstate->address, sizeof(newstate->address), address); + newstate->mtuSize = CA_DEFAULT_BLE_MTU_SIZE; + switch(state_type) + { + case CA_LE_CONNECTION_STATE: + newstate->connectedState = target_state; + newstate->sendState = STATE_SEND_NONE; + break; + case CA_LE_SEND_STATE: + newstate->connectedState = STATE_DISCONNECTED; + newstate->sendState = target_state; + break; + default: + break; + } + OIC_LOG_V(INFO, TAG, "add a new state to List - addr : %s, " + "conn : %d, send : %d, ACFlag : %d", + newstate->address, newstate->connectedState, newstate->sendState, + newstate->autoConnectFlag); + u_arraylist_add(deviceList, newstate); // update new state + } + oc_mutex_unlock(deviceListMutex); + + return CA_STATUS_OK; +} + +bool CALEIsDeviceInList(const char* remoteAddress, + u_arraylist_t *deviceList) +{ + VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false); + VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", false); + + uint32_t length = u_arraylist_length(deviceList); + for (uint32_t index = 0; index < length; index++) + { + CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index); + if (!state) + { + OIC_LOG(ERROR, TAG, "CALEState_t object is null"); + return false; + } + + if (!strcmp(remoteAddress, state->address)) + { + OIC_LOG(DEBUG, TAG, "the device is already set"); + return true; + } + else + { + continue; + } + } + + OIC_LOG(DEBUG, TAG, "there are no the device in list."); + return false; +} + +CAResult_t CALERemoveAllDeviceState(u_arraylist_t *deviceList, + oc_mutex deviceListMutex) +{ + OIC_LOG(DEBUG, TAG, "CALERemoveAllDeviceState"); + VERIFY_NON_NULL(deviceList, TAG, "deviceList is null"); + + oc_mutex_lock(deviceListMutex); + uint32_t length = u_arraylist_length(deviceList); + for (uint32_t index = 0; index < length; index++) + { + CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index); + if (!state) + { + OIC_LOG(ERROR, TAG, "jarrayObj is null"); + continue; + } + OICFree(state); + } + + OICFree(deviceList); + deviceList = NULL; + oc_mutex_unlock(deviceListMutex); + + return CA_STATUS_OK; +} + +CAResult_t CALEResetDeviceStateForAll(u_arraylist_t *deviceList, + oc_mutex deviceListMutex) +{ + OIC_LOG(DEBUG, TAG, "CALEClientResetDeviceStateForAll"); + VERIFY_NON_NULL(deviceList, TAG, "deviceList is null"); + + oc_mutex_lock(deviceListMutex); + size_t length = u_arraylist_length(deviceList); + for (size_t index = 0; index < length; index++) + { + CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index); + if (!state) + { + OIC_LOG(ERROR, TAG, "jarrayObj is null"); + continue; + } + + // autoConnectFlag value will be not changed, + // since it has reset only termination case. + state->connectedState = STATE_DISCONNECTED; + state->sendState = STATE_SEND_NONE; + } + oc_mutex_unlock(deviceListMutex); + + return CA_STATUS_OK; +} + +CAResult_t CALERemoveDeviceState(const char* remoteAddress, + u_arraylist_t *deviceList) +{ + OIC_LOG(DEBUG, TAG, "CALERemoveDeviceState"); + VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null"); + VERIFY_NON_NULL(deviceList, TAG, "deviceList is null"); + + uint32_t length = u_arraylist_length(deviceList); + for (uint32_t index = 0; index < length; index++) + { + CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index); + if (!state) + { + OIC_LOG(ERROR, TAG, "CALEState_t object is null"); + continue; + } + + if (!strcmp(state->address, remoteAddress)) + { + OIC_LOG_V(DEBUG, TAG, "remove state : %s", state->address); + + CALEState_t* targetState = (CALEState_t*)u_arraylist_remove(deviceList, + index); + if (NULL == targetState) + { + OIC_LOG(ERROR, TAG, "List removal failed."); + return CA_STATUS_FAILED; + } + + OICFree(targetState); + return CA_STATUS_OK; + } + } + return CA_STATUS_OK; +} + +CALEState_t* CALEGetStateInfo(const char* remoteAddress, + u_arraylist_t *deviceList) +{ + VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL); + VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", NULL); + + uint32_t length = u_arraylist_length(deviceList); + OIC_LOG_V(DEBUG, TAG, "length of deviceStateList : %d", length); + OIC_LOG_V(DEBUG, TAG, "target address : %s", remoteAddress); + + for (uint32_t index = 0; index < length; index++) + { + CALEState_t* state = (CALEState_t*) u_arraylist_get(deviceList, index); + if (!state) + { + OIC_LOG(ERROR, TAG, "CALEState_t object is null"); + continue; + } + + OIC_LOG_V(DEBUG, TAG, "state address : %s (idx: %d)", state->address, index); + + if (!strcmp(state->address, remoteAddress)) + { + OIC_LOG(DEBUG, TAG, "found state"); + return state; + } + } + + OIC_LOG_V(DEBUG, TAG, "[%s] doesn't exist in deviceStateList", remoteAddress); + return NULL; +} + +bool CALEIsValidState(const char* remoteAddress, + uint16_t state_type, + uint16_t target_state, + u_arraylist_t *deviceList, + oc_mutex deviceListMutex) +{ + OIC_LOG_V(DEBUG, TAG, "CALEIsValidState : type[%d], target state[%d]", + state_type, target_state); + VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false); + VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", false); + + oc_mutex_lock(deviceListMutex); + CALEState_t* state = CALEGetStateInfo(remoteAddress, deviceList); + if (NULL == state) + { + OIC_LOG(DEBUG, TAG, "state is not updated yet"); + oc_mutex_unlock(deviceListMutex); + return false; + } + + uint16_t curValue = 0; + switch(state_type) + { + case CA_LE_CONNECTION_STATE: + curValue = state->connectedState; + break; + case CA_LE_SEND_STATE: + curValue = state->sendState; + break; + default: + break; + } + + if (target_state == curValue) + { + oc_mutex_unlock(deviceListMutex); + return true; + } + else + { + oc_mutex_unlock(deviceListMutex); + return false; + } + + oc_mutex_unlock(deviceListMutex); + return false; +} + + +CAResult_t CALESetFlagToState(JNIEnv *env, jstring jni_address, jint state_idx, jboolean flag, + u_arraylist_t *deviceList, oc_mutex deviceListMutex) +{ + OIC_LOG(DEBUG, TAG, "IN - CALESetFlagToState"); + VERIFY_NON_NULL(env, TAG, "env"); + VERIFY_NON_NULL(jni_address, TAG, "jni_address"); + VERIFY_NON_NULL(deviceList, TAG, "deviceList"); + + oc_mutex_lock(deviceListMutex); + + char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + OIC_LOG(ERROR, TAG, "address is not available"); + CACheckJNIException(env); + return CA_STATUS_FAILED; + } + + if (CALEIsDeviceInList(address, deviceList)) + { + CALEState_t* curState = CALEGetStateInfo(address, deviceList); + if(!curState) + { + OIC_LOG(ERROR, TAG, "curState is null"); + (*env)->ReleaseStringUTFChars(env, jni_address, address); + oc_mutex_unlock(deviceListMutex); + return CA_STATUS_FAILED; + } + OIC_LOG_V(INFO, TAG, "%d flag is set : %d", state_idx, flag); + + switch(state_idx) + { + case CA_LE_AUTO_CONNECT_FLAG: + curState->autoConnectFlag = flag; + break; + case CA_LE_DESCRIPTOR_FOUND: + curState->isDescriptorFound = flag; + break; + default: + break; + } + } + + (*env)->ReleaseStringUTFChars(env, jni_address, address); + oc_mutex_unlock(deviceListMutex); + OIC_LOG(DEBUG, TAG, "OUT - CALESetFlagToState"); + return CA_STATUS_OK; +} + +jboolean CALEGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx, + u_arraylist_t *deviceList, oc_mutex deviceListMutex) +{ + OIC_LOG(DEBUG, TAG, "IN - CALEGetFlagFromState"); + VERIFY_NON_NULL_RET(env, TAG, "env", false); + VERIFY_NON_NULL_RET(jni_address, TAG, "jni_address", false); + VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList", false); + + oc_mutex_lock(deviceListMutex); + + char* address = (char*)(*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + OIC_LOG(ERROR, TAG, "address is not available"); + CACheckJNIException(env); + oc_mutex_unlock(deviceListMutex); + return JNI_FALSE; + } + + CALEState_t* curState = CALEGetStateInfo(address, deviceList); + (*env)->ReleaseStringUTFChars(env, jni_address, address); + if(!curState) + { + OIC_LOG(INFO, TAG, "there is no information. auto connect flag is false"); + oc_mutex_unlock(deviceListMutex); + return JNI_FALSE; + } + + jboolean ret = JNI_FALSE; + switch(state_idx) + { + case CA_LE_AUTO_CONNECT_FLAG: + ret = curState->autoConnectFlag; + break; + case CA_LE_DESCRIPTOR_FOUND: + ret = curState->isDescriptorFound; + break; + default: + break; + } + oc_mutex_unlock(deviceListMutex); + + OIC_LOG_V(INFO, TAG, "%d flag is %d", state_idx, ret); + OIC_LOG(DEBUG, TAG, "OUT - CALEGetFlagFromState"); + return ret; +} + +CAResult_t CALESetMtuSize(const char* address, uint16_t mtuSize, + u_arraylist_t *deviceList, oc_mutex deviceListMutex) + +{ + VERIFY_NON_NULL(address, TAG, "address is null"); + VERIFY_NON_NULL(deviceList, TAG, "deviceList is null"); + + oc_mutex_lock(deviceListMutex); + if (CALEIsDeviceInList(address, deviceList)) + { + CALEState_t* curState = CALEGetStateInfo(address, deviceList); + if(!curState) + { + OIC_LOG(ERROR, TAG, "curState is null"); + oc_mutex_unlock(deviceListMutex); + return CA_STATUS_FAILED; + } + + curState->mtuSize = mtuSize; + OIC_LOG_V(INFO, TAG, "update state - addr: %s, mtu: %d", + curState->address, curState->mtuSize); + } + else + { + OIC_LOG(ERROR, TAG, "there is no state info in the list"); + } + oc_mutex_unlock(deviceListMutex); + return CA_STATUS_OK; +} + +uint16_t CALEGetMtuSize(const char* address, u_arraylist_t *deviceList, oc_mutex deviceListMutex) +{ + VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE); + VERIFY_NON_NULL_RET(deviceList, TAG, "deviceList is null", CA_DEFAULT_BLE_MTU_SIZE); + + oc_mutex_lock(deviceListMutex); + if (CALEIsDeviceInList(address, deviceList)) + { + CALEState_t* curState = CALEGetStateInfo(address, deviceList); + if(!curState) + { + OIC_LOG(ERROR, TAG, "curState is null"); + oc_mutex_unlock(deviceListMutex); + return CA_DEFAULT_BLE_MTU_SIZE; + } + + OIC_LOG_V(INFO, TAG, "state - addr: %s, mtu: %d", + curState->address, curState->mtuSize); + oc_mutex_unlock(deviceListMutex); + return curState->mtuSize; + } + + oc_mutex_unlock(deviceListMutex); + return CA_DEFAULT_BLE_MTU_SIZE; +} diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/calestate.h b/resource/csdk/connectivity/src/bt_le_adapter/android/calestate.h new file mode 100644 index 0000000..7a72aa3 --- /dev/null +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/calestate.h @@ -0,0 +1,178 @@ +/* **************************************************************** + * + * Copyright 2017 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** + * @file + * This file contains the APIs for BT LE communications. + */ +#ifndef CA_LE_LIST_H_ +#define CA_LE_LIST_H_ + +#include "cacommon.h" +#include "cathreadpool.h" /* for thread pool */ +#include "octhread.h" +#include "uarraylist.h" +#include "jni.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +typedef struct le_state_info +{ + char address[CA_MACADDR_SIZE]; + uint16_t connectedState; + uint16_t sendState; + jboolean autoConnectFlag; + jboolean isDescriptorFound; + uint16_t mtuSize; +} CALEState_t; + +/** + * update new state information. + * @param[in] address remote address. + * @param[in] state_type state type. + * @param[in] target_state state index to update. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CALEUpdateDeviceState(const char* address, + uint16_t state_type, + uint16_t target_state, + u_arraylist_t *deviceList, + oc_mutex deviceListMutex); + +/** + * check whether the remote address is existed or not. + * @param[in] address remote address. + * @param[in] deviceList target device list. + * @return true or false. + */ +bool CALEIsDeviceInList(const char* remoteAddress, + u_arraylist_t *deviceList); + +/** + * remove all device states. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CALERemoveAllDeviceState(u_arraylist_t *deviceList, + oc_mutex deviceListMutex); + +/** + * Reset values of device state for all of devices. + * this method has to be invoked when BT adapter is disabled. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CALEResetDeviceStateForAll(u_arraylist_t *deviceList, + oc_mutex deviceListMutex); + +/** + * remove the device state for a remote device. + * @param[in] remoteAddress remote address. + * @param[in] deviceList target device list. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CALERemoveDeviceState(const char* remoteAddress, + u_arraylist_t *deviceList); + +/** + * get state information for a remote device. + * @param[in] remoteAddress remote address. + * @param[in] deviceList target device list. + * @return CALEState_t. + */ +CALEState_t* CALEGetStateInfo(const char* remoteAddressm, + u_arraylist_t *deviceList); + +/** + * check whether the remote address has same state with target state. + * @param[in] remoteAddress remote address. + * @param[in] state_type state_type. + * @param[in] target_state state index to check. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + * @return true or false. + */ +bool CALEIsValidState(const char* remoteAddress, + uint16_t state_type, + uint16_t target_state, + u_arraylist_t *deviceList, + oc_mutex deviceListMutex); + + +/** + * set flag into State List. + * @param[in] env JNI interface pointer. + * @param[in] jni_address remote address. + * @param[in] state_idx state index. + * @param[in] flag auto connect flag. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + */ +CAResult_t CALESetFlagToState(JNIEnv *env, jstring jni_address, + jint state_idx, jboolean flag, + u_arraylist_t *deviceList, + oc_mutex deviceListMutex); + +/** + * get flag from State List. + * @param[in] env JNI interface pointer. + * @param[in] jni_address remote address. + * @param[in] state_idx state index. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + * @return current flag; + */ +jboolean CALEGetFlagFromState(JNIEnv *env, jstring jni_address, jint state_idx, + u_arraylist_t *deviceList, oc_mutex deviceListMutex); + +/** + * This function is used to set MTU size + * which negotiated between client and server device. + * @param[in] address remote address. + * @param[in] mtuSize MTU size. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + * @return ::CA_STATUS_OK or ERROR CODES (::CAResult_t error codes in cacommon.h). + */ +CAResult_t CALESetMtuSize(const char* address, uint16_t mtuSize, + u_arraylist_t *deviceList, oc_mutex deviceListMutex); + +/** + * get MTU size. + * @param[in] address the address of the remote device. + * @param[in] deviceList target device list. + * @param[in] deviceListMutex target device list mutex. + * @return mtu size negotiated from remote device. + */ +uint16_t CALEGetMtuSize(const char* address, + u_arraylist_t *deviceList, + oc_mutex deviceListMutex); +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* CA_LESERVER_H_ */ diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h index dd01468..b7812e9 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h @@ -69,6 +69,15 @@ static const uint16_t STATE_CONNECTED = 3; static const uint16_t STATE_SERVICE_CONNECTED = 2; static const uint16_t STATE_DISCONNECTED = 1; +static const uint16_t GATT_ERROR = 133; + +static const uint16_t STATE_SEND_NONE = 1; +static const uint16_t STATE_SEND_SUCCESS = 2; +static const uint16_t STATE_SEND_FAIL = 3; +static const uint16_t STATE_SENDING = 4; +static const uint16_t STATE_SEND_PREPARING = 5; +static const uint16_t STATE_SEND_MTU_NEGO_SUCCESS = 6; + /** * get uuid(jni object) from uuid(character). * @param[in] env JNI interface pointer. -- 2.7.4