From: DoHyun Pyun Date: Thu, 28 Nov 2019 07:54:15 +0000 (+0900) Subject: Merge branch 'tizen' into tizen_5.5 X-Git-Tag: accepted/tizen/5.5/unified/20191203.011249^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0b3468590b08bfb8d617b4e207988dd27ddc7ad1;hp=0c9cc9f4ed3a0cd71a0043d6327e81d07207ea3f;p=platform%2Fupstream%2Fiotivity.git Merge branch 'tizen' into tizen_5.5 --- diff --git a/android/android_api/build.gradle b/android/android_api/build.gradle old mode 100755 new mode 100644 index cfcb3a2..81a0c8f --- a/android/android_api/build.gradle +++ b/android/android_api/build.gradle @@ -24,6 +24,7 @@ buildscript { repositories { + mavenCentral() jcenter() } dependencies { @@ -35,7 +36,8 @@ buildscript { } allprojects { - repositories { + repositories { + mavenCentral() jcenter() } } diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript b/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript index a12b289..1f68e5b 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/SConscript @@ -11,6 +11,7 @@ src_files = [ 'caleclient.c', 'caleserver.c', 'calenwmonitor.c', 'caleutils.c', - 'calestate.c' ] + 'calestate.c', + 'caleclient_jni.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 old mode 100644 new mode 100755 index 9bf9330..ebd8a30 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.c @@ -38,6 +38,7 @@ #include "octhread.h" #include "uarraylist.h" #include "org_iotivity_ca_CaLeClientInterface.h" +#include "caleclient_utils.h" //#define TAG PCF("OIC_CA_LE_CLIENT") #define TAG BLE_CLIENT_TAG @@ -68,19 +69,14 @@ static ca_thread_pool_t g_threadPoolHandle = NULL; JavaVM *g_jvm; static u_arraylist_t *g_deviceList = NULL; // device list to have same UUID static u_arraylist_t *g_gattObjectList = NULL; -static u_arraylist_t *g_deviceStateList = NULL; static CAPacketReceiveCallback g_packetReceiveCallback = NULL; -static CABLEErrorHandleCallback g_clientErrorCallback; -static jobject g_leScanCallback = NULL; -static jobject g_leGattCallback = NULL; static jobject g_context = NULL; static jobjectArray g_uuidList = NULL; // it will be prevent to start send logic when adapter has stopped. static bool g_isStartedLEClient = false; -static jbyteArray g_sendBuffer = NULL; static uint32_t g_targetCnt = 0; static uint32_t g_currentSentCnt = 0; static bool g_isFinishedSendData = false; @@ -90,15 +86,9 @@ static oc_cond g_threadCond = NULL; static oc_cond g_deviceDescCond = NULL; static oc_mutex g_threadSendMutex = NULL; -static oc_mutex g_threadWriteCharacteristicMutex = NULL; -static oc_cond g_threadWriteCharacteristicCond = NULL; -static bool g_isSignalSetFlag = false; - -static oc_mutex g_bleServerBDAddressMutex = NULL; static oc_mutex g_deviceListMutex = NULL; static oc_mutex g_gattObjectMutex = NULL; -static oc_mutex g_deviceStateListMutex = NULL; static oc_mutex g_deviceScanRetryDelayMutex = NULL; static oc_cond g_deviceScanRetryDelayCond = NULL; @@ -116,7 +106,6 @@ static bool g_isWorkingScanThread = false; static CALEScanState_t g_curScanningStep = BLE_SCAN_NONE; static CALEScanState_t g_nextScanningStep = BLE_SCAN_ENABLE; -static CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL; static int32_t g_jniIntSdk = -1; static bool g_setHighQoS = true; @@ -131,6 +120,25 @@ static u_arraylist_t* g_serviceUuidList = NULL; // List of Manufacture Data static u_arraylist_t* g_manufactureDataList = NULL; +//Variables are defined as extern in other file so using it as non-static here +u_arraylist_t *g_deviceStateList = NULL; +oc_mutex g_deviceStateListMutex = NULL; + +oc_mutex g_threadWriteCharacteristicMutex = NULL; +oc_cond g_threadWriteCharacteristicCond = NULL; +oc_mutex g_bleServerBDAddressMutex = NULL; + +CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL; + +jobject g_leScanCallback = NULL; +jobject g_leGattCallback = NULL; + +bool g_isSignalSetFlag = false; + +jbyteArray g_sendBuffer = NULL; + +CABLEErrorHandleCallback g_clientErrorCallback = NULL; + /** * check if retry logic for connection routine has to be stopped or not. * in case of error value including this method, connection routine has to be stopped. @@ -139,7 +147,7 @@ static u_arraylist_t* g_manufactureDataList = NULL; * @return true - waiting for background connection in BT platform. * false - connection routine has to be stopped. */ -static bool CALECheckConnectionStateValue(jint state) +bool CALECheckConnectionStateValue(jint state) { switch(state) { @@ -212,19 +220,10 @@ static void CALEScanThread(void* object) { (void)object; - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return; } oc_mutex_lock(g_threadScanIntervalMutex); @@ -350,31 +349,13 @@ CAResult_t CALECreateJniInterfaceObject() { OIC_LOG(DEBUG, TAG, "CALECreateJniInterfaceObject"); - if (!g_context) - { - OIC_LOG(ERROR, TAG, "g_context is null"); - return CA_STATUS_FAILED; - } - - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_context, TAG, "g_context is null", CA_STATUS_FAILED); + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } jmethodID mid_getApplicationContext = CAGetJNIMethodID(env, "android/content/Context", @@ -488,25 +469,12 @@ CAResult_t CALEClientInitialize() CALEClientAddUuid(OIC_GATT_CUSTOM_UUID2, CA_LE_TYPE_CUSTOM_UUID); CALEClientAddUuid(OIC_GATT_CUSTOM_UUID3, CA_LE_TYPE_CUSTOM_UUID); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } g_jniIntSdk = CALEGetBuildVersion(env); @@ -585,25 +553,12 @@ void CALEClientTerminate() { OIC_LOG(DEBUG, TAG, "CALEClientTerminate"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return; - } + VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null"); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return ; } // stop scan @@ -698,25 +653,12 @@ jobject CALEClientHiddenConnectGatt(jobject btDevice, const char* address, jbool { OIC_LOG(INFO, TAG, "IN - CALEClientHiddenConnectGatt"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return NULL; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", NULL); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return NULL; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return NULL; } jstring jni_address = (*env)->NewStringUTF(env, address); @@ -766,25 +708,12 @@ CAResult_t CALEClientDestroyJniInterface() { OIC_LOG(DEBUG, TAG, "CALEClientDestroyJniInterface"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } jclass jni_LeInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface"); @@ -871,25 +800,12 @@ CAResult_t CALEClientSendMulticastMessage(const uint8_t* data, OIC_LOG_V(DEBUG, TAG, "CALEClientSendMulticastMessage(%p)", data); VERIFY_NON_NULL(data, TAG, "data is null"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } CAResult_t ret = CALEClientSendMulticastMessageImpl(env, data, dataLen); @@ -1044,24 +960,12 @@ CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const uint8_t* data); VERIFY_NON_NULL(address, TAG, "address is null"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } oc_mutex_lock(g_threadSendMutex); @@ -1490,9 +1394,11 @@ CAResult_t CALEClientStartScan() return CA_STATUS_FAILED; } - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); + + JNIEnv* env = NULL; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ return CA_STATUS_FAILED; } @@ -1500,19 +1406,6 @@ CAResult_t CALEClientStartScan() { g_setFullScanFlag = true; } - bool isAttached = false; - JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; - } OIC_LOG(DEBUG, TAG, "CALEClientStartScan"); @@ -2481,24 +2374,12 @@ error_exit: CAResult_t CALEClientStopScan() { - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } CAResult_t ret = CA_STATUS_FAILED; @@ -3227,20 +3108,12 @@ CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt) static void CALEWriteCharacteristicThread(void* object) { VERIFY_NON_NULL_VOID(object, TAG, "object is null"); + VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null"); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return ; } jobject gatt = (jobject)object; @@ -3745,25 +3618,12 @@ jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteris CAResult_t CALEClientCreateUUIDList() { - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } // create new object array @@ -4881,7 +4741,7 @@ CAResult_t CALEClientInitGattMutexVaraibles() void CALEClientTerminateGattMutexVariables() { - + oc_mutex_free(g_bleServerBDAddressMutex); g_bleServerBDAddressMutex = NULL; @@ -4970,25 +4830,12 @@ void CAStopLEGattClient() { OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return; - } + VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null"); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return ; } CAResult_t ret = CALEClientDisconnectAll(env); @@ -5085,9 +4932,9 @@ CAResult_t CAUpdateCharacteristicsToAllGattServers(const uint8_t *data, uint32_t void CASetLEReqRespClientCallback(CABLEDataReceivedCallback callback) { - + g_CABLEClientDataReceivedCallback = callback; - + } void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle) @@ -5100,617 +4947,3 @@ CAResult_t CAGetLEAddress(char **local_address) VERIFY_NON_NULL(local_address, TAG, "local_address"); return CA_NOT_SUPPORTED; } - -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj, - jobject callback) -{ - OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback"); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); - - g_leScanCallback = (*env)->NewGlobalRef(env, callback); - CACheckJNIException(env); -} - -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallbackForV21(JNIEnv *env, - jobject obj, - jobject callback) -{ - OIC_LOG(DEBUG, TAG, "caLeRegisterLeScanCallbackForV21"); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); - - g_leScanCallback = (*env)->NewGlobalRef(env, callback); - CACheckJNIException(env); -} - -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj, - jobject callback) -{ - OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback"); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); - - g_leGattCallback = (*env)->NewGlobalRef(env, callback); - CACheckJNIException(env); -} - -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj, - jobject device) -{ - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(device, TAG, "device is null"); - - CAResult_t res = CALEClientAddScanDeviceToList(env, device); - if (CA_STATUS_OK != res) - { - OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res); - } -} - -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeScanFailedCallback(JNIEnv *env, jobject obj, - jint errorCode) -{ - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - - switch (errorCode) - { - case 1: - OIC_LOG(ERROR, TAG, "BLE scan has failed, error is SCAN_FAILED_ALREADY_STARTED"); - break; - - case 2: - OIC_LOG(ERROR, TAG, - "BLE scan has failed, error is SCAN_FAILED_APPLICATION_REGISTRATION_FAILED"); - break; - - case 3: - OIC_LOG(ERROR, TAG, "BLE scan has failed, error is SCAN_FAILED_INTERNAL_ERROR"); - break; - - case 4: - OIC_LOG(ERROR, TAG, "BLE scan has failed, error is SCAN_FAILED_FEATURE_UNSUPPORTED"); - break; - - default: - OIC_LOG(ERROR, TAG, "BLE scan has failed with unknown error"); - break; - } -} - -/* - * Class: org_iotivity_ca_jar_caleinterface - * Method: CALeGattConnectionStateChangeCallback - * Signature: (Landroid/bluetooth/BluetoothGatt;II)V - */ -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env, - jobject obj, - jobject gatt, - jint status, - jint newstate) -{ - OIC_LOG_V(INFO, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status, - newstate); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); - - jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, "STATE_CONNECTED"); - - jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); - if (!jni_address) - { - OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed"); - goto error_exit; - } - - const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - OIC_LOG(ERROR, TAG, "address is null"); - CACheckJNIException(env); - goto error_exit; - } - OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - address [%s]", address); - - if (state_connected == newstate) - { - OIC_LOG(DEBUG, TAG, "LE is connected"); - if (GATT_SUCCESS == status) - { - 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, "CALEUpdateDeviceState has failed"); - goto error_exit; - } - - res = CALEClientAddGattobjToList(env, gatt); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed"); - goto error_exit; - } - - res = CALEClientDiscoverServices(env, gatt); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed"); - goto error_exit; - } - } - else - { - OIC_LOG(INFO, TAG, "unknown status"); - (*env)->ReleaseStringUTFChars(env, jni_address, address); - } - } - else // STATE_DISCONNECTED == newstate - { - OIC_LOG(DEBUG, TAG, "LE is disconnected"); - - 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 = CALEUpdateDeviceState(address, CA_LE_SEND_STATE, - STATE_SEND_FAIL, - g_deviceStateList, - g_deviceStateListMutex); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); - } - } - - CAResult_t res = CALEUpdateDeviceState(address, - CA_LE_CONNECTION_STATE, - STATE_DISCONNECTED, - g_deviceStateList, - g_deviceStateListMutex); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); - } - (*env)->ReleaseStringUTFChars(env, jni_address, address); - - res = CALEClientGattClose(env, gatt); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed"); - } - - if (CALECheckConnectionStateValue(status)) - { - // this state is unexpected reason to disconnect - // if the reason is suitable, connection logic of the device will be destroyed. - OIC_LOG(INFO, TAG, "connection logic destroy"); - } - else - { - // other reason except for gatt_success is expected to running - // background connection in BT platform. - OIC_LOG(INFO, TAG, "unknown status or manual disconnected state"); - } - CALEClientUpdateSendCnt(env); - } - return; - - // error label. -error_exit: - CALEClientSendFinish(env, gatt); - return; -} - -/* - * Class: org_iotivity_ca_jar_caleinterface - * Method: CALeGattServicesDiscoveredCallback - * Signature: (Landroid/bluetooth/BluetoothGatt;I)V - */ -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env, - jobject obj, - jobject gatt, - jint status) -{ - OIC_LOG_V(INFO, TAG, "CALeGattServicesDiscoveredCallback - status %d", status); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); - - if (GATT_SUCCESS != status) // discovery error - { - CALEClientSendFinish(env, gatt); - return; - } - - jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); - if (!jni_address) - { - CALEClientSendFinish(env, gatt); - return; - } - - const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - CACheckJNIException(env); - CALEClientSendFinish(env, gatt); - return; - } - - jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID); - if (!jni_uuid) - { - OIC_LOG(ERROR, TAG, "jni_uuid is null"); - goto error_exit; - } - - jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid); - if (!jni_obj_GattCharacteristic) - { - OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null"); - goto error_exit; - } - - CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt, - jni_obj_GattCharacteristic); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed"); - goto error_exit; - } - - res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic); - if (CA_STATUS_OK != res) - { - OIC_LOG_V(INFO, TAG, "Descriptor is not found : %d", res); - - res = CALESetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_FALSE, - g_deviceStateList, g_deviceStateListMutex); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALESetFlagToState has failed"); - goto error_exit; - } - - res = CALEUpdateDeviceState(address, CA_LE_CONNECTION_STATE, - STATE_SERVICE_CONNECTED, - g_deviceStateList, - g_deviceStateListMutex); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); - goto error_exit; - } - - res = CALEClientRequestMTU(env, gatt, CA_SUPPORTED_BLE_MTU_SIZE); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientRequestMTU has failed"); - goto error_exit; - } - } - else - { - res = CALESetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_TRUE, - g_deviceStateList, g_deviceStateListMutex); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALESetFlagToState has failed"); - goto error_exit; - } - } - -#ifdef SCAN_INTERVAL - // reset interval scan logic - CALERestartScanWithInterval(g_scanIntervalTimePrev, 0, BLE_SCAN_ENABLE); -#endif - - OIC_LOG(INFO, TAG, "ServicesDiscovery is successful"); - (*env)->ReleaseStringUTFChars(env, jni_address, address); - return; - - // error label. -error_exit: - OIC_LOG(ERROR, TAG, "ServicesDiscovery has failed"); - (*env)->ReleaseStringUTFChars(env, jni_address, address); - CALEClientSendFinish(env, gatt); - return; -} - -/* - * Class: org_iotivity_ca_jar_caleinterface - * Method: CALeGattCharacteristicWritjclasseCallback - * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V - */ -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback( - JNIEnv *env, jobject obj, jobject gatt, jbyteArray data, jint status) -{ - OIC_LOG_V(INFO, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); - - // send success & signal - jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); - if (!jni_address) - { - goto error_exit; - } - - const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - CACheckJNIException(env); - goto error_exit; - } - - if (GATT_SUCCESS != status) // error case - { - OIC_LOG(ERROR, TAG, "send failure"); - - // retry to write - CAResult_t res = CALEClientWriteCharacteristic(env, gatt); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "WriteCharacteristic has failed"); - oc_mutex_lock(g_threadWriteCharacteristicMutex); - g_isSignalSetFlag = true; - oc_cond_signal(g_threadWriteCharacteristicCond); - oc_mutex_unlock(g_threadWriteCharacteristicMutex); - - 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, "CALEUpdateDeviceState has failed"); - } - - if (g_clientErrorCallback) - { - jint length = (*env)->GetArrayLength(env, data); - CACheckJNIException(env); - g_clientErrorCallback(address, data, length, CA_SEND_FAILED); - CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, address, 0, length, - false, "writeChar failure"); - } - - (*env)->ReleaseStringUTFChars(env, jni_address, address); - goto error_exit; - } - } - else - { - OIC_LOG(DEBUG, TAG, "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, "CALEUpdateDeviceState has failed"); - } - - oc_mutex_lock(g_threadWriteCharacteristicMutex); - OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal"); - g_isSignalSetFlag = true; - oc_cond_signal(g_threadWriteCharacteristicCond); - oc_mutex_unlock(g_threadWriteCharacteristicMutex); - - CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, address, 0, - (*env)->GetArrayLength(env, data), - true, "writeChar success"); - } - - (*env)->ReleaseStringUTFChars(env, jni_address, address); - return; - - // error label. -error_exit: - - CALEClientSendFinish(env, gatt); - return; -} - -/* - * Class: org_iotivity_ca_jar_caleinterface - * Method: CALeGattCharacteristicChangedCallback - * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V - */ -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback( - JNIEnv *env, jobject obj, jobject gatt, jbyteArray data) -{ - OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback"); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); - VERIFY_NON_NULL_VOID(data, TAG, "data is null"); - - // get Byte Array and convert to uint8_t* - jint length = (*env)->GetArrayLength(env, data); - - jboolean isCopy; - jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy); - CACheckJNIException(env); - - uint8_t* receivedData = OICMalloc(length); - if (!receivedData) - { - OIC_LOG(ERROR, TAG, "receivedData is null"); - return; - } - - memcpy(receivedData, jni_byte_responseData, length); - (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT); - - jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); - if (!jni_address) - { - OIC_LOG(ERROR, TAG, "jni_address is null"); - OICFree(receivedData); - return; - } - - const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - OIC_LOG(ERROR, TAG, "address is null"); - CACheckJNIException(env); - OICFree(receivedData); - return; - } - - OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %p, %d", - receivedData, length); - - uint32_t sentLength = 0; - oc_mutex_lock(g_bleServerBDAddressMutex); - g_CABLEClientDataReceivedCallback(address, receivedData, length, &sentLength); - oc_mutex_unlock(g_bleServerBDAddressMutex); - - (*env)->ReleaseStringUTFChars(env, jni_address, address); -} - -/* - * Class: org_iotivity_ca_jar_caleinterface - * Method: CALeGattDescriptorWriteCallback - * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V - */ -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj, - jobject gatt, - jint status) -{ - OIC_LOG_V(INFO, TAG, "CALeGattDescriptorWriteCallback - status %d", status); - VERIFY_NON_NULL_VOID(env, TAG, "env is null"); - VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); - VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); - - if (GATT_SUCCESS != status) // error - { - goto error_exit; - } - - jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); - if (!jni_address) - { - goto error_exit; - } - - const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - CACheckJNIException(env); - goto error_exit; - } - - 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, "CALEUpdateDeviceState has failed"); - goto error_exit; - } - - res = CALEClientRequestMTU(env, gatt, CA_SUPPORTED_BLE_MTU_SIZE); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientRequestMTU has failed"); - goto error_exit; - } - return; - -// error label. -error_exit: - - CALEClientSendFinish(env, gatt); - return; -} - -JNIEXPORT void JNICALL -Java_org_iotivity_ca_CaLeClientInterface_caLeGattMtuChangedCallback(JNIEnv *env, - jobject obj, - jobject gatt, - jint mtu, - jint status) -{ - OIC_LOG_V(INFO, TAG, "caLeGattMtuChangedCallback - status %d, " - "mtu[%d-including Header size 3 byte]", status, mtu); - - (void)obj; - - if (0 == status || 133 == status) - { - if (g_sendBuffer) - { - CAResult_t res = CALEClientWriteCharacteristic(env, gatt); - if (CA_STATUS_OK != res) - { - OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed"); - } - } - else - { - OIC_LOG(INFO, TAG, "mtu nego is done"); - jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); - if (!jni_address) - { - CALEClientSendFinish(env, gatt); - return; - } - - const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); - if (!address) - { - CACheckJNIException(env); - (*env)->DeleteLocalRef(env, jni_address); - CALEClientSendFinish(env, gatt); - return; - } - - // update mtu 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, "CALESetMtuSize has failed"); - } - - 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, "CALEUpdateDeviceState has failed"); - } - CALEClientUpdateSendCnt(env); - (*env)->ReleaseStringUTFChars(env, jni_address, address); - (*env)->DeleteLocalRef(env, jni_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 f108cea..46d9649 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient.h @@ -648,6 +648,16 @@ CAResult_t CALEClientStartScanWithInterval(); */ void CALEClientStopScanWithInterval(); +/** + * check if retry logic for connection routine has to be stopped or not. + * in case of error value including this method, connection routine has to be stopped. + * since there is no retry logic for this error reason in this client. + * @param state constant value of bluetoothgatt. + * @return true - waiting for background connection in BT platform. + * false - connection routine has to be stopped. + */ +bool CALECheckConnectionStateValue(jint state); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient_jni.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient_jni.c new file mode 100644 index 0000000..13a1efa --- /dev/null +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient_jni.c @@ -0,0 +1,656 @@ +/****************************************************************** + * + * Copyright 2014 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 "caleclient.h" +#include "caleserver.h" +#include "caleutils.h" +#include "caleinterface.h" +#include "caadapterutils.h" + +#include "uarraylist.h" +#include "logger.h" +#include "oic_malloc.h" +#include "oic_string.h" +#include "cathreadpool.h" /* for thread pool */ +#include "octhread.h" +#include "uarraylist.h" +#include "org_iotivity_ca_CaLeClientInterface.h" +#include "caleclient_utils.h" +#include "caleclient.h" + +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj, + jobject callback) +{ + OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback"); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); + + g_leScanCallback = (*env)->NewGlobalRef(env, callback); + CACheckJNIException(env); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallbackForV21(JNIEnv *env, + jobject obj, + jobject callback) +{ + OIC_LOG(DEBUG, TAG, "caLeRegisterLeScanCallbackForV21"); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); + + g_leScanCallback = (*env)->NewGlobalRef(env, callback); + CACheckJNIException(env); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj, + jobject callback) +{ + OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback"); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(callback, TAG, "callback is null"); + + g_leGattCallback = (*env)->NewGlobalRef(env, callback); + CACheckJNIException(env); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj, + jobject device) +{ + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(device, TAG, "device is null"); + + CAResult_t res = CALEClientAddScanDeviceToList(env, device); + if (CA_STATUS_OK != res) + { + OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res); + } +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeScanFailedCallback(JNIEnv *env, jobject obj, + jint errorCode) +{ + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + + switch (errorCode) + { + case 1: + OIC_LOG(ERROR, TAG, "BLE scan has failed, error is SCAN_FAILED_ALREADY_STARTED"); + break; + + case 2: + OIC_LOG(ERROR, TAG, + "BLE scan has failed, error is SCAN_FAILED_APPLICATION_REGISTRATION_FAILED"); + break; + + case 3: + OIC_LOG(ERROR, TAG, "BLE scan has failed, error is SCAN_FAILED_INTERNAL_ERROR"); + break; + + case 4: + OIC_LOG(ERROR, TAG, "BLE scan has failed, error is SCAN_FAILED_FEATURE_UNSUPPORTED"); + break; + + default: + OIC_LOG(ERROR, TAG, "BLE scan has failed with unknown error"); + break; + } +} + +/* + * Class: org_iotivity_ca_jar_caleinterface + * Method: CALeGattConnectionStateChangeCallback + * Signature: (Landroid/bluetooth/BluetoothGatt;II)V + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env, + jobject obj, + jobject gatt, + jint status, + jint newstate) +{ + OIC_LOG_V(INFO, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status, + newstate); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); + + jint state_connected = CALEGetConstantsValue(env, CLASSPATH_BT_PROFILE, "STATE_CONNECTED"); + + jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); + if (!jni_address) + { + OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed"); + goto error_exit; + } + + const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); + goto error_exit; + } + OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - address [%s]", address); + + if (state_connected == newstate) + { + OIC_LOG(DEBUG, TAG, "LE is connected"); + if (GATT_SUCCESS == status) + { + 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, "CALEUpdateDeviceState has failed"); + goto error_exit; + } + + res = CALEClientAddGattobjToList(env, gatt); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed"); + goto error_exit; + } + + res = CALEClientDiscoverServices(env, gatt); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed"); + goto error_exit; + } + } + else + { + OIC_LOG(INFO, TAG, "unknown status"); + (*env)->ReleaseStringUTFChars(env, jni_address, address); + } + } + else // STATE_DISCONNECTED == newstate + { + OIC_LOG(DEBUG, TAG, "LE is disconnected"); + + 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 = CALEUpdateDeviceState(address, CA_LE_SEND_STATE, + STATE_SEND_FAIL, + g_deviceStateList, + g_deviceStateListMutex); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); + } + } + + CAResult_t res = CALEUpdateDeviceState(address, + CA_LE_CONNECTION_STATE, + STATE_DISCONNECTED, + g_deviceStateList, + g_deviceStateListMutex); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); + } + (*env)->ReleaseStringUTFChars(env, jni_address, address); + + res = CALEClientGattClose(env, gatt); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed"); + } + + if (CALECheckConnectionStateValue(status)) + { + // this state is unexpected reason to disconnect + // if the reason is suitable, connection logic of the device will be destroyed. + OIC_LOG(INFO, TAG, "connection logic destroy"); + } + else + { + // other reason except for gatt_success is expected to running + // background connection in BT platform. + OIC_LOG(INFO, TAG, "unknown status or manual disconnected state"); + } + CALEClientUpdateSendCnt(env); + } + return; + + // error label. +error_exit: + CALEClientSendFinish(env, gatt); + return; +} + +/* + * Class: org_iotivity_ca_jar_caleinterface + * Method: CALeGattServicesDiscoveredCallback + * Signature: (Landroid/bluetooth/BluetoothGatt;I)V + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env, + jobject obj, + jobject gatt, + jint status) +{ + OIC_LOG_V(INFO, TAG, "CALeGattServicesDiscoveredCallback - status %d", status); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); + + if (GATT_SUCCESS != status) // discovery error + { + CALEClientSendFinish(env, gatt); + return; + } + + jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); + if (!jni_address) + { + CALEClientSendFinish(env, gatt); + return; + } + + const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + CACheckJNIException(env); + CALEClientSendFinish(env, gatt); + return; + } + + jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID); + if (!jni_uuid) + { + OIC_LOG(ERROR, TAG, "jni_uuid is null"); + goto error_exit; + } + + jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid); + if (!jni_obj_GattCharacteristic) + { + OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null"); + goto error_exit; + } + + CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt, + jni_obj_GattCharacteristic); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed"); + goto error_exit; + } + + res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic); + if (CA_STATUS_OK != res) + { + OIC_LOG_V(INFO, TAG, "Descriptor is not found : %d", res); + + res = CALESetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_FALSE, + g_deviceStateList, g_deviceStateListMutex); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALESetFlagToState has failed"); + goto error_exit; + } + + res = CALEUpdateDeviceState(address, CA_LE_CONNECTION_STATE, + STATE_SERVICE_CONNECTED, + g_deviceStateList, + g_deviceStateListMutex); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEUpdateDeviceState has failed"); + goto error_exit; + } + + res = CALEClientRequestMTU(env, gatt, CA_SUPPORTED_BLE_MTU_SIZE); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientRequestMTU has failed"); + goto error_exit; + } + } + else + { + res = CALESetFlagToState(env, jni_address, CA_LE_DESCRIPTOR_FOUND, JNI_TRUE, + g_deviceStateList, g_deviceStateListMutex); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALESetFlagToState has failed"); + goto error_exit; + } + } + +#ifdef SCAN_INTERVAL + // reset interval scan logic + CALERestartScanWithInterval(g_scanIntervalTimePrev, 0, BLE_SCAN_ENABLE); +#endif + + OIC_LOG(INFO, TAG, "ServicesDiscovery is successful"); + (*env)->ReleaseStringUTFChars(env, jni_address, address); + return; + + // error label. +error_exit: + OIC_LOG(ERROR, TAG, "ServicesDiscovery has failed"); + (*env)->ReleaseStringUTFChars(env, jni_address, address); + CALEClientSendFinish(env, gatt); + return; +} + +/* + * Class: org_iotivity_ca_jar_caleinterface + * Method: CALeGattCharacteristicWritjclasseCallback + * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback( + JNIEnv *env, jobject obj, jobject gatt, jbyteArray data, jint status) +{ + OIC_LOG_V(INFO, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); + + // send success & signal + jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); + if (!jni_address) + { + goto error_exit; + } + + const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + CACheckJNIException(env); + goto error_exit; + } + + if (GATT_SUCCESS != status) // error case + { + OIC_LOG(ERROR, TAG, "send failure"); + + // retry to write + CAResult_t res = CALEClientWriteCharacteristic(env, gatt); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "WriteCharacteristic has failed"); + oc_mutex_lock(g_threadWriteCharacteristicMutex); + g_isSignalSetFlag = true; + oc_cond_signal(g_threadWriteCharacteristicCond); + oc_mutex_unlock(g_threadWriteCharacteristicMutex); + + 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, "CALEUpdateDeviceState has failed"); + } + + if (g_clientErrorCallback) + { + jint length = (*env)->GetArrayLength(env, data); + CACheckJNIException(env); + g_clientErrorCallback(address, data, length, CA_SEND_FAILED); + CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, address, 0, length, + false, "writeChar failure"); + } + + (*env)->ReleaseStringUTFChars(env, jni_address, address); + goto error_exit; + } + } + else + { + OIC_LOG(DEBUG, TAG, "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, "CALEUpdateDeviceState has failed"); + } + + oc_mutex_lock(g_threadWriteCharacteristicMutex); + OIC_LOG(DEBUG, TAG, "g_isSignalSetFlag is set true and signal"); + g_isSignalSetFlag = true; + oc_cond_signal(g_threadWriteCharacteristicCond); + oc_mutex_unlock(g_threadWriteCharacteristicMutex); + + CALogSendStateInfo(CA_ADAPTER_GATT_BTLE, address, 0, + (*env)->GetArrayLength(env, data), + true, "writeChar success"); + } + + (*env)->ReleaseStringUTFChars(env, jni_address, address); + return; + + // error label. +error_exit: + + CALEClientSendFinish(env, gatt); + return; +} + +/* + * Class: org_iotivity_ca_jar_caleinterface + * Method: CALeGattCharacteristicChangedCallback + * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback( + JNIEnv *env, jobject obj, jobject gatt, jbyteArray data) +{ + OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback"); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); + VERIFY_NON_NULL_VOID(data, TAG, "data is null"); + + // get Byte Array and convert to uint8_t* + jint length = (*env)->GetArrayLength(env, data); + + jboolean isCopy; + jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy); + CACheckJNIException(env); + + uint8_t* receivedData = OICMalloc(length); + if (!receivedData) + { + OIC_LOG(ERROR, TAG, "receivedData is null"); + return; + } + + memcpy(receivedData, jni_byte_responseData, length); + (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT); + + jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); + if (!jni_address) + { + OIC_LOG(ERROR, TAG, "jni_address is null"); + OICFree(receivedData); + return; + } + + const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + OIC_LOG(ERROR, TAG, "address is null"); + CACheckJNIException(env); + OICFree(receivedData); + return; + } + + OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %p, %d", + receivedData, length); + + uint32_t sentLength = 0; + oc_mutex_lock(g_bleServerBDAddressMutex); + g_CABLEClientDataReceivedCallback(address, receivedData, length, &sentLength); + oc_mutex_unlock(g_bleServerBDAddressMutex); + + (*env)->ReleaseStringUTFChars(env, jni_address, address); +} + +/* + * Class: org_iotivity_ca_jar_caleinterface + * Method: CALeGattDescriptorWriteCallback + * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj, + jobject gatt, + jint status) +{ + OIC_LOG_V(INFO, TAG, "CALeGattDescriptorWriteCallback - status %d", status); + VERIFY_NON_NULL_VOID(env, TAG, "env is null"); + VERIFY_NON_NULL_VOID(obj, TAG, "obj is null"); + VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null"); + + if (GATT_SUCCESS != status) // error + { + goto error_exit; + } + + jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); + if (!jni_address) + { + goto error_exit; + } + + const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + CACheckJNIException(env); + goto error_exit; + } + + 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, "CALEUpdateDeviceState has failed"); + goto error_exit; + } + + res = CALEClientRequestMTU(env, gatt, CA_SUPPORTED_BLE_MTU_SIZE); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientRequestMTU has failed"); + goto error_exit; + } + return; + +// error label. +error_exit: + + CALEClientSendFinish(env, gatt); + return; +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ca_CaLeClientInterface_caLeGattMtuChangedCallback(JNIEnv *env, + jobject obj, + jobject gatt, + jint mtu, + jint status) +{ + OIC_LOG_V(INFO, TAG, "caLeGattMtuChangedCallback - status %d, " + "mtu[%d-including Header size 3 byte]", status, mtu); + + (void)obj; + + if (0 == status || 133 == status) + { + if (g_sendBuffer) + { + CAResult_t res = CALEClientWriteCharacteristic(env, gatt); + if (CA_STATUS_OK != res) + { + OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed"); + } + } + else + { + OIC_LOG(INFO, TAG, "mtu nego is done"); + jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt); + if (!jni_address) + { + CALEClientSendFinish(env, gatt); + return; + } + + const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL); + if (!address) + { + CACheckJNIException(env); + (*env)->DeleteLocalRef(env, jni_address); + CALEClientSendFinish(env, gatt); + return; + } + + // update mtu 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, "CALESetMtuSize has failed"); + } + + 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, "CALEUpdateDeviceState has failed"); + } + CALEClientUpdateSendCnt(env); + (*env)->ReleaseStringUTFChars(env, jni_address, address); + (*env)->DeleteLocalRef(env, jni_address); + } + } +} diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient_utils.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient_utils.h new file mode 100644 index 0000000..4b5501c --- /dev/null +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleclient_utils.h @@ -0,0 +1,40 @@ +/* **************************************************************** + * + * Copyright 2014 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. + * + ******************************************************************/ + +#define TAG BLE_CLIENT_TAG + +extern u_arraylist_t *g_deviceStateList; +extern oc_mutex g_deviceStateListMutex; + +extern oc_mutex g_threadWriteCharacteristicMutex; +extern oc_cond g_threadWriteCharacteristicCond; + +extern oc_mutex g_bleServerBDAddressMutex; + +extern CABLEDataReceivedCallback g_CABLEClientDataReceivedCallback; + +extern jobject g_leScanCallback; +extern jobject g_leGattCallback; + +extern bool g_isSignalSetFlag; + +extern CABLEErrorHandleCallback g_clientErrorCallback; + +extern jbyteArray g_sendBuffer; diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c old mode 100644 new mode 100755 index 3cbb821..98bd82e --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleserver.c @@ -96,32 +96,13 @@ CAResult_t CALEServerCreateJniInterfaceObject() { OIC_LOG(DEBUG, TAG, "CALEServerCreateJniInterfaceObject"); - if (!g_context) - { - OIC_LOG(ERROR, TAG, "g_context is null"); - return CA_STATUS_FAILED; - } - - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_context, TAG, "g_context is null",CA_STATUS_FAILED); + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeServerInterface"); @@ -424,26 +405,12 @@ CAResult_t CALEServerStartAdvertise() return CA_STATUS_OK; } - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } // start advertise @@ -765,26 +732,12 @@ error_exit: CAResult_t CALEServerStopAdvertise() { - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } CAResult_t ret = CALEServerStopAdvertiseImpl(env, g_leAdvertiseCallback); @@ -1550,26 +1503,12 @@ CAResult_t CALEServerInitialize() CALeServerJniInit(); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } CAResult_t ret = CALECheckPlatformVersion(env, 21); @@ -1627,26 +1566,12 @@ void CALEServerTerminate() { OIC_LOG(DEBUG, TAG, "IN - CALEServerTerminate"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return; - } + VERIFY_NON_NULL_VOID(g_jvm, TAG, "g_jvm is null"); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return ; } if (g_sendBuffer) @@ -1683,26 +1608,12 @@ CAResult_t CALEServerSendUnicastMessage(const char* address, const uint8_t* data VERIFY_NON_NULL(address, TAG, "address is null"); VERIFY_NON_NULL(data, TAG, "data is null"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } CAResult_t ret = CALEServerSendUnicastMessageImpl(env, address, data, dataLen); @@ -1724,26 +1635,12 @@ CAResult_t CALEServerSendMulticastMessage(const uint8_t* data, uint32_t dataLen) OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage(%p)", data); VERIFY_NON_NULL(data, TAG, "data is null"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } CAResult_t ret = CALEServerSendMulticastMessageImpl(env, data, dataLen); @@ -1776,26 +1673,12 @@ CAResult_t CALEServerStartMulticastServer() return CA_STATUS_FAILED; } - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } g_isStartServer = true; @@ -2685,26 +2568,12 @@ CAResult_t CAStopLEGattServer() { OIC_LOG(DEBUG, TAG, "CAStopLEGattServer"); - if (!g_jvm) - { - OIC_LOG(ERROR, TAG, "g_jvm is null"); - return CA_STATUS_FAILED; - } + VERIFY_NON_NULL_RET(g_jvm, TAG, "g_jvm is null", CA_STATUS_FAILED); - bool isAttached = false; JNIEnv* env = NULL; - jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6); - if (JNI_OK != res) - { - OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer"); - res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL); - - if (JNI_OK != res) - { - OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); - return CA_STATUS_FAILED; - } - isAttached = true; + bool isAttached = false; + if (!CALEAttachCurrentThread(&env, g_jvm, &isAttached)){ + return CA_STATUS_FAILED; } CAResult_t ret = CALEServerGattClose(env, g_bluetoothGattServer); diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c old mode 100644 new mode 100755 index e41acfa..0d8b359 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.c @@ -581,3 +581,21 @@ jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt) return jni_address; } + +bool CALEAttachCurrentThread(JNIEnv **env, JavaVM *g_jvm, bool *attach_status) +{ + jint res = (*g_jvm)->GetEnv(g_jvm, (void**) env, JNI_VERSION_1_6); + if (JNI_OK != res) + { + res = (*g_jvm)->AttachCurrentThread(g_jvm, env, NULL); + + if (JNI_OK != res) + { + OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed"); + return false; + } + *attach_status = true; + } + + return true; +} \ No newline at end of file diff --git a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h old mode 100644 new mode 100755 index ee52303..b4764c3 --- a/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h +++ b/resource/csdk/connectivity/src/bt_le_adapter/android/caleutils.h @@ -202,6 +202,8 @@ jobject CALEGetRemoteDevice(JNIEnv *env, jstring address); */ jstring CALEGetAddressFromGatt(JNIEnv *env, jobject gatt); +bool CALEAttachCurrentThread(JNIEnv **env, JavaVM *g_jvm, bool *attach_status); + #ifdef __cplusplus } /* extern "C" */ #endif diff --git a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c index 225e1be..d3245f0 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/caleadapter.c @@ -50,6 +50,20 @@ #define CALEADAPTER_TAG "OIC_CA_LE_ADAP" /** + * Macro for checking CAResult_t + */ +#define VERIFY_CARESULT(res, func, arg1, arg2, arg3) \ + if (CA_STATUS_OK != res) \ + { \ + OIC_LOG_V(ERROR, CALEADAPTER_TAG, "%s failed, result [%d]", #func, res); \ + if (g_errorHandler) \ + { \ + g_errorHandler(arg1, arg2, arg3, res); \ + } \ + return; \ + } + +/** * Stores information of all the senders. * * This structure will be used to track and defragment all incoming data packet. @@ -1273,16 +1287,7 @@ static void CALEServerSendDataThread(void *threadData) &totalLength, g_mtuSize); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateVariableForFragmentation failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateVariableForFragmentation, bleData->remoteEndpoint, bleData->data, bleData->dataLen); OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Packet info: data size[%d] midPacketCount[%u] remainingLen[%zu] totalLength[%zu]", @@ -1318,32 +1323,14 @@ static void CALEServerSendDataThread(void *threadData) } OIC_LOG_V(INFO, CALEADAPTER_TAG, "header info: secureFlag[%X]", secureFlag); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeader failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateHeader, bleData->remoteEndpoint, bleData->data, bleData->dataLen); uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0}; result = CAGenerateHeaderPayloadLength(lengthHeader, CA_BLE_LENGTH_HEADER_SIZE, bleData->dataLen); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeaderPayloadLength failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateHeaderPayloadLength, bleData->remoteEndpoint, bleData->data, bleData->dataLen); uint32_t length = 0; uint32_t dataLen = 0; @@ -1362,16 +1349,7 @@ static void CALEServerSendDataThread(void *threadData) bleData->data, dataLen, dataHeader, lengthHeader); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAMakeFirstDataSegment, bleData->remoteEndpoint, bleData->data, bleData->dataLen); const uint32_t iter = midPacketCount; uint32_t index = 0; @@ -1384,21 +1362,7 @@ static void CALEServerSendDataThread(void *threadData) result = CAUpdateCharacteristicsToGattClient( bleData->remoteEndpoint->addr, dataSegment, length); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, - CALEADAPTER_TAG, - "Update characteristics failed, result [%d]", - result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, - bleData->data, - bleData->dataLen, - result); - } - return; - } + VERIFY_CARESULT(result, CAUpdateCharacteristicsToGattClient, bleData->remoteEndpoint, bleData->data, bleData->dataLen); OIC_LOG_V(DEBUG, CALEADAPTER_TAG, @@ -1411,16 +1375,7 @@ static void CALEServerSendDataThread(void *threadData) secureFlag, bleData->remoteEndpoint->port); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeader failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateHeader, bleData->remoteEndpoint, bleData->data, bleData->dataLen); for (index = 0; index < iter; index++) { @@ -1433,16 +1388,7 @@ static void CALEServerSendDataThread(void *threadData) dataHeader, g_mtuSize); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAMakeRemainDataSegment, bleData->remoteEndpoint, bleData->data, bleData->dataLen); result = CAUpdateCharacteristicsToGattClient( @@ -1450,16 +1396,8 @@ static void CALEServerSendDataThread(void *threadData) dataSegment, g_mtuSize); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Update characteristics failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAUpdateCharacteristicsToGattClient, bleData->remoteEndpoint, bleData->data, bleData->dataLen); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent data length [%d]", g_mtuSize); } @@ -1476,37 +1414,15 @@ static void CALEServerSendDataThread(void *threadData) dataHeader, g_mtuSize); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAMakeRemainDataSegment, bleData->remoteEndpoint, bleData->data, bleData->dataLen); result = CAUpdateCharacteristicsToGattClient( bleData->remoteEndpoint->addr, dataSegment, remainingLen + CA_BLE_HEADER_SIZE); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, - CALEADAPTER_TAG, - "Update characteristics failed, result [%d]", - result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, - bleData->data, - bleData->dataLen, - result); - } - return; - } + VERIFY_CARESULT(result, CAUpdateCharacteristicsToGattClient, bleData->remoteEndpoint, bleData->data, bleData->dataLen); + OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Server Sent Unicast Last Data - data length [%zu]", @@ -1547,21 +1463,7 @@ static void CALEClientSendDataThread(void *threadData) CAResult_t res = CALEClientSendNegotiationMessage( bleData->remoteEndpoint->addr); - if (CA_STATUS_OK != res) - { - OIC_LOG_V(ERROR, - CALEADAPTER_TAG, - "CALEClientSendNegotiationMessage has failed, result [%d]", - res); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, - bleData->data, - bleData->dataLen, - res); - } - return; - } + VERIFY_CARESULT(res, CALEClientSendNegotiationMessage, bleData->remoteEndpoint, bleData->data, bleData->dataLen); } #endif #if defined(__TIZEN__) || defined(__ANDROID__) @@ -1580,16 +1482,7 @@ static void CALEClientSendDataThread(void *threadData) &totalLength, g_mtuSize); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateVariableForFragmentation failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateVariableForFragmentation, bleData->remoteEndpoint, bleData->data, bleData->dataLen); OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Packet info: data size[%d] midPacketCount[%u] remainingLen[%zu] totalLength[%zu]", @@ -1621,32 +1514,14 @@ static void CALEClientSendDataThread(void *threadData) OIC_LOG_V(INFO, CALEADAPTER_TAG, "header info: secureFlag[%X]", secureFlag); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeader failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateHeader, bleData->remoteEndpoint, bleData->data, bleData->dataLen); uint8_t lengthHeader[CA_BLE_LENGTH_HEADER_SIZE] = {0}; result = CAGenerateHeaderPayloadLength(lengthHeader, CA_BLE_LENGTH_HEADER_SIZE, bleData->dataLen); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeaderPayloadLength failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateHeaderPayloadLength, bleData->remoteEndpoint, bleData->data, bleData->dataLen); uint32_t length = 0; uint32_t dataLen = 0; @@ -1665,16 +1540,7 @@ static void CALEClientSendDataThread(void *threadData) bleData->data, dataLen, dataHeader, lengthHeader); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAMakeFirstDataSegment, bleData->remoteEndpoint, bleData->data, bleData->dataLen); const uint32_t iter = midPacketCount; uint32_t index = 0; @@ -1690,21 +1556,7 @@ static void CALEClientSendDataThread(void *threadData) LE_UNICAST, 0); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, - CALEADAPTER_TAG, - "Update characteristics failed, result [%d]", - result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, - bleData->data, - bleData->dataLen, - result); - } - return; - } + VERIFY_CARESULT(result, CAUpdateCharacteristicsToGattServer, bleData->remoteEndpoint, bleData->data, bleData->dataLen); OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Unicast First Data - data length [%u]", @@ -1716,16 +1568,7 @@ static void CALEClientSendDataThread(void *threadData) secureFlag, bleData->remoteEndpoint->port); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "CAGenerateHeader failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAGenerateHeader, bleData->remoteEndpoint, bleData->data, bleData->dataLen); for (index = 0; index < iter; index++) { @@ -1737,16 +1580,7 @@ static void CALEClientSendDataThread(void *threadData) dataHeader, g_mtuSize); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAMakeRemainDataSegment, bleData->remoteEndpoint, bleData->data, bleData->dataLen); // Send the remaining header. result = CAUpdateCharacteristicsToGattServer( @@ -1755,18 +1589,7 @@ static void CALEClientSendDataThread(void *threadData) g_mtuSize, LE_UNICAST, 0); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, - CALEADAPTER_TAG, - "Update characteristics failed, result [%d]", - result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAUpdateCharacteristicsToGattServer, bleData->remoteEndpoint, bleData->data, bleData->dataLen); OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Unicast %d Data - data(mtu) length [%hu]", @@ -1786,16 +1609,7 @@ static void CALEClientSendDataThread(void *threadData) dataHeader, g_mtuSize); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, - "Making data segment failed, result [%d]", result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAMakeRemainDataSegment, bleData->remoteEndpoint, bleData->data, bleData->dataLen); result = CAUpdateCharacteristicsToGattServer( bleData->remoteEndpoint->addr, @@ -1803,16 +1617,7 @@ static void CALEClientSendDataThread(void *threadData) remainingLen + CA_BLE_HEADER_SIZE, LE_UNICAST, 0); - if (CA_STATUS_OK != result) - { - OIC_LOG_V(ERROR, CALEADAPTER_TAG, "Update characteristics failed, result [%d]", - result); - if (g_errorHandler) - { - g_errorHandler(bleData->remoteEndpoint, bleData->data, bleData->dataLen, result); - } - return; - } + VERIFY_CARESULT(result, CAUpdateCharacteristicsToGattServer, bleData->remoteEndpoint, bleData->data, bleData->dataLen); OIC_LOG_V(DEBUG, CALEADAPTER_TAG, "Client Sent Unicast Last Data - data length [%zu]", diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/SConscript b/resource/csdk/connectivity/src/bt_le_adapter/tizen/SConscript index 2166547..7560a78 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/SConscript +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/SConscript @@ -38,7 +38,8 @@ if 'MCD' in division: 'caleutil.c', 'calenwmonitor.c'] elif 'VD' in division: - src_files = [ 'caleclient_vd.c', + env.AppendUnique(CPPDEFINES = ['TIZEN_VD']) + src_files = [ 'caleclient.c', 'caleserver_vd.c', 'caleutil.c', 'calenwmonitor_vd.c'] diff --git a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c index fb5a920..561e2bf 100644 --- a/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c +++ b/resource/csdk/connectivity/src/bt_le_adapter/tizen/caleclient.c @@ -58,6 +58,7 @@ static int g_retrycount = 0; static int retry_flag = 0; uint64_t const TIMEOUT = 30 * MICROSECS_PER_SEC; +#ifndef TIZEN_VD /** * Mutex to call connect only after disconnect during retry */ @@ -67,6 +68,7 @@ static oc_mutex g_isDisconnectedMutex = NULL; * Condition for calling connect during connection retry */ static oc_cond g_LEDisconnectedCond = NULL; +#endif /** * Flag to check if scanning is in progress @@ -288,8 +290,10 @@ CAResult_t CALEGattInitiateConnection(const char *remoteAddress) return CA_STATUS_FAILED; } g_isConnectionInProgress = true; +#ifndef TIZEN_VD // Set gatt connect retry count g_retrycount = RETRY_COUNT; +#endif oc_mutex_unlock(g_isConnectionInProgressMutex); // Pause the scanning @@ -336,6 +340,11 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress) if (!connected) { OIC_LOG_V(DEBUG, TAG, "DisConnected from [%s] ", remoteAddress); +#ifdef TIZEN_VD + oc_mutex_lock(g_LEServerListMutex); + CARemoveLEServerInfoFromList(&g_LEServerList, remoteAddress); + oc_mutex_unlock(g_LEServerListMutex); +#else if(!retry_flag) { oc_mutex_lock(g_LEServerListMutex); @@ -349,6 +358,7 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress) oc_mutex_unlock(g_isDisconnectedMutex); } retry_flag = 0; +#endif } else { @@ -363,7 +373,7 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress) if (CA_STATUS_OK != CAGetLEServerInfo(g_LEServerList, remoteAddress, &serverInfo)) { oc_mutex_unlock(g_LEServerListMutex); - OIC_LOG_V(ERROR, TAG, "Could not get server info!"); + OIC_LOG_V(ERROR, TAG, "Could not get server info for [%s]", remoteAddress); return; } @@ -384,6 +394,7 @@ void CALEGattConnectionStateChanged(bool connected, const char *remoteAddress) OIC_LOG(ERROR, TAG, "addr is NULL"); return; } + CAResult_t ret = ca_thread_pool_add_task(g_LEClientThreadPool, CADiscoverLEServicesThread, addr, NULL); oc_mutex_unlock(g_LEClientThreadPoolMutex); @@ -431,13 +442,19 @@ static bool CALEIsHaveServiceImpl(bt_adapter_le_device_scan_result_info_s *scanI if (result == BT_ERROR_NONE && NULL != man_data) { - char *compare_man_data = OICMalloc((man_data_len*2)+1); int pos =0; + char *compare_man_data = OICCalloc(1, (man_data_len*2)+1); + if (!compare_man_data) + { + OIC_LOG(ERROR, TAG, "Memory allocation failed for compare_man_data"); + OICFree(man_data); + return false; + } + for(int i=0;iremoteAddress = addr; serverInfo->status = LE_STATUS_UNICAST_PENDING; diff --git a/resource/csdk/connectivity/src/ip_adapter/SConscript b/resource/csdk/connectivity/src/ip_adapter/SConscript index 8cb1cbb..0864b31 100644 --- a/resource/csdk/connectivity/src/ip_adapter/SConscript +++ b/resource/csdk/connectivity/src/ip_adapter/SConscript @@ -21,7 +21,10 @@ if target_os == 'arduino': else: common_files = [ os.path.join(src_dir, 'caipadapter.c'), - os.path.join(src_dir, 'caipserver.c') ] + os.path.join(src_dir, 'caipserver.c'), + os.path.join(src_dir, 'caipnwmonitor_common.c') ] + +env.AppendUnique(CPPPATH = [ src_dir ]) # Get list of target-specific source file base names, i.e. no parent # directories prepended to the path. diff --git a/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c b/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c index 2c451f6..05cddc7 100644 --- a/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c +++ b/resource/csdk/connectivity/src/ip_adapter/android/caipnwmonitor.c @@ -36,6 +36,7 @@ #include "caadapterutils.h" #include "caipnwmonitor.h" +#include "caipnwmonitor_common.h" #include "logger.h" #include "oic_malloc.h" #include "oic_string.h" @@ -127,71 +128,20 @@ CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter) return CA_STATUS_OK; } -int CAGetPollingInterval(int interval) -{ - return interval; -} - static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status) { - CAIPCBData_t *cbitem = NULL; - LL_FOREACH(g_adapterCallbackList, cbitem) - { - if (cbitem && cbitem->adapter) - { - cbitem->callback(cbitem->adapter, status); - } - } + CAIPPassNetworkChangesToAdapterInternal(status, g_adapterCallbackList, CA_IP_NW_COMMON_ANDROID); } CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback, CATransportAdapter_t adapter) { - if (!callback) - { - OIC_LOG(ERROR, TAG, "callback is null"); - return CA_STATUS_INVALID_PARAM; - } - - CAIPCBData_t *cbitem = NULL; - LL_FOREACH(g_adapterCallbackList, cbitem) - { - if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback) - { - OIC_LOG(DEBUG, TAG, "this callback is already added"); - return CA_STATUS_OK; - } - } - - cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem)); - if (!cbitem) - { - OIC_LOG(ERROR, TAG, "Malloc failed"); - return CA_STATUS_FAILED; - } - - cbitem->adapter = adapter; - cbitem->callback = callback; - LL_APPEND(g_adapterCallbackList, cbitem); - - return CA_STATUS_OK; + return CAIPSetNetworkMonitorCallbackInternal(callback, adapter, g_adapterCallbackList); } CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter) { - CAIPCBData_t *cbitem = NULL; - CAIPCBData_t *tmpCbitem = NULL; - LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem) - { - if (cbitem && adapter == cbitem->adapter) - { - OIC_LOG(DEBUG, TAG, "remove specific callback"); - LL_DELETE(g_adapterCallbackList, cbitem); - OICFree(cbitem); - return CA_STATUS_OK; - } - } - return CA_STATUS_OK; + return CAIPUnSetNetworkMonitorCallbackInternal(adapter, g_adapterCallbackList); } u_arraylist_t *CAFindInterfaceChange() diff --git a/resource/csdk/connectivity/src/ip_adapter/caipnwmonitor_common.c b/resource/csdk/connectivity/src/ip_adapter/caipnwmonitor_common.c new file mode 100644 index 0000000..1fcb957 --- /dev/null +++ b/resource/csdk/connectivity/src/ip_adapter/caipnwmonitor_common.c @@ -0,0 +1,105 @@ +/****************************************************************** +* +* Copyright 2019 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 "caipnwmonitor.h" +#include "caipnwmonitor_common.h" + +#include "utlist.h" +#include "logger.h" + +#define TAG "OIC_CA_IP_MONITOR_COMMON" + +/** + * @todo For Windows platform: Implement network interface monitoring. + * Not used in win32, but caipserver currently requires this function + * be defined. not critical. + */ +int CAGetPollingInterval(int interval) +{ + return interval; +} + +void CAIPPassNetworkChangesToAdapterInternal(CANetworkStatus_t status, + CAIPCBData_t *adapterCallbackList, CAIPAdapterPlatform_t platform) +{ + CAIPCBData_t *cbitem = NULL; + LL_FOREACH(adapterCallbackList, cbitem) + { + if (cbitem && cbitem->adapter) + { + cbitem->callback(cbitem->adapter, status); + if (platform == CA_IP_NW_COMMON_LINUX) + { + CALogAdapterStateInfo(cbitem->adapter, status); + } + } + } +} + +CAResult_t CAIPSetNetworkMonitorCallbackInternal(CAIPAdapterStateChangeCallback callback, + CATransportAdapter_t adapter, CAIPCBData_t *adapterCallbackList) +{ + if (!callback) + { + OIC_LOG(ERROR, TAG, "callback is null"); + return CA_STATUS_INVALID_PARAM; + } + + CAIPCBData_t *cbitem = NULL; + LL_FOREACH(adapterCallbackList, cbitem) + { + if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback) + { + OIC_LOG(DEBUG, TAG, "this callback is already added"); + return CA_STATUS_OK; + } + } + + cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem)); + if (!cbitem) + { + OIC_LOG(ERROR, TAG, "Malloc failed"); + return CA_STATUS_FAILED; + } + + cbitem->adapter = adapter; + cbitem->callback = callback; + LL_APPEND(adapterCallbackList, cbitem); + + return CA_STATUS_OK; +} + +CAResult_t CAIPUnSetNetworkMonitorCallbackInternal(CATransportAdapter_t adapter, + CAIPCBData_t *adapterCallbackList) +{ + CAIPCBData_t *cbitem = NULL; + CAIPCBData_t *tmpCbitem = NULL; + LL_FOREACH_SAFE(adapterCallbackList, cbitem, tmpCbitem) + { + if (cbitem && adapter == cbitem->adapter) + { + OIC_LOG(DEBUG, TAG, "remove specific callback"); + LL_DELETE(adapterCallbackList, cbitem); + OICFree(cbitem); + return CA_STATUS_OK; + } + } + return CA_STATUS_OK; +} diff --git a/resource/csdk/connectivity/src/ip_adapter/caipnwmonitor_common.h b/resource/csdk/connectivity/src/ip_adapter/caipnwmonitor_common.h new file mode 100644 index 0000000..949d0fa --- /dev/null +++ b/resource/csdk/connectivity/src/ip_adapter/caipnwmonitor_common.h @@ -0,0 +1,61 @@ +/****************************************************************** + * + * Copyright 2019 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 caipnwmonitor_common.h + * This file provides common APIs of IP network monitor modules. + */ + +#ifndef CA_IP_NW_INTERFACE_COMMON_H_ +#define CA_IP_NW_INTERFACE_COMMON_H_ + +#include "cacommon.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * Enum for defining different platforms for internal usage. + */ +typedef enum +{ + CA_IP_NW_COMMON_ANDROID = 0, + CA_IP_NW_COMMON_LINUX, + CA_IP_NW_COMMON_TIZEN, + CA_IP_NW_COMMON_TIZENRT, + CA_IP_NW_COMMON_WINDOWS +} CAIPAdapterPlatform_t; + +void CAIPPassNetworkChangesToAdapterInternal(CANetworkStatus_t status, + CAIPCBData_t *adapterCallbackList, CAIPAdapterPlatform_t platform); + +CAResult_t CAIPSetNetworkMonitorCallbackInternal(CAIPAdapterStateChangeCallback callback, + CATransportAdapter_t adapter, CAIPCBData_t *adapterCallbackList); + +CAResult_t CAIPUnSetNetworkMonitorCallbackInternal(CATransportAdapter_t adapter, + CAIPCBData_t *adapterCallbackList); + +#ifdef __cplusplus +} +#endif + +#endif /* CA_IP_NW_INTERFACE_COMMON_H_ */ diff --git a/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c b/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c index 1dcd851..39fc257 100644 --- a/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c +++ b/resource/csdk/connectivity/src/ip_adapter/linux/caipnwmonitor.c @@ -41,6 +41,7 @@ #include "octhread.h" #include "caipnwmonitor.h" +#include "caipnwmonitor_common.h" #include "caadapterutils.h" #include "logger.h" #include "oic_malloc.h" @@ -225,72 +226,20 @@ CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter) return CAIPUnSetNetworkMonitorCallback(adapter); } -int CAGetPollingInterval(int interval) -{ - return interval; -} - static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status) { - CAIPCBData_t *cbitem = NULL; - LL_FOREACH(g_adapterCallbackList, cbitem) - { - if (cbitem && cbitem->adapter) - { - cbitem->callback(cbitem->adapter, status); - CALogAdapterStateInfo(cbitem->adapter, status); - } - } + CAIPPassNetworkChangesToAdapterInternal(status, g_adapterCallbackList, CA_IP_NW_COMMON_LINUX); } CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback, CATransportAdapter_t adapter) { - if (!callback) - { - OIC_LOG(ERROR, TAG, "callback is null"); - return CA_STATUS_INVALID_PARAM; - } - - CAIPCBData_t *cbitem = NULL; - LL_FOREACH(g_adapterCallbackList, cbitem) - { - if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback) - { - OIC_LOG(DEBUG, TAG, "this callback is already added"); - return CA_STATUS_OK; - } - } - - cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem)); - if (!cbitem) - { - OIC_LOG(ERROR, TAG, "Malloc failed"); - return CA_STATUS_FAILED; - } - - cbitem->adapter = adapter; - cbitem->callback = callback; - LL_APPEND(g_adapterCallbackList, cbitem); - - return CA_STATUS_OK; + return CAIPSetNetworkMonitorCallbackInternal(callback, adapter, g_adapterCallbackList); } CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter) { - CAIPCBData_t *cbitem = NULL; - CAIPCBData_t *tmpCbitem = NULL; - LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem) - { - if (cbitem && adapter == cbitem->adapter) - { - OIC_LOG(DEBUG, TAG, "remove specific callback"); - LL_DELETE(g_adapterCallbackList, cbitem); - OICFree(cbitem); - return CA_STATUS_OK; - } - } - return CA_STATUS_OK; + return CAIPUnSetNetworkMonitorCallbackInternal(adapter, g_adapterCallbackList); } static CAInterface_t *CANewInterfaceItem(int index, const char *name, int family, diff --git a/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c b/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c index 079ecd5..036b0f1 100644 --- a/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c +++ b/resource/csdk/connectivity/src/ip_adapter/tizen/caipnwmonitor.c @@ -35,6 +35,7 @@ #include "caipinterface.h" #include "caipnwmonitor.h" +#include "caipnwmonitor_common.h" #include "caadapterutils.h" #include "logger.h" #include "oic_malloc.h" @@ -80,71 +81,20 @@ static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status); */ static void CAIPConnectionStateChangedCb(connection_type_e type, void* userData); -int CAGetPollingInterval(int interval) -{ - return interval; -} - static void CAIPPassNetworkChangesToAdapter(CANetworkStatus_t status) { - CAIPCBData_t *cbitem = NULL; - LL_FOREACH(g_adapterCallbackList, cbitem) - { - if (cbitem && cbitem->adapter) - { - cbitem->callback(cbitem->adapter, status); - } - } + CAIPPassNetworkChangesToAdapterInternal(status, g_adapterCallbackList, CA_IP_NW_COMMON_TIZEN); } CAResult_t CAIPSetNetworkMonitorCallback(CAIPAdapterStateChangeCallback callback, CATransportAdapter_t adapter) { - if (!callback) - { - OIC_LOG(ERROR, TAG, "callback is null"); - return CA_STATUS_INVALID_PARAM; - } - - CAIPCBData_t *cbitem = NULL; - LL_FOREACH(g_adapterCallbackList, cbitem) - { - if (cbitem && adapter == cbitem->adapter && callback == cbitem->callback) - { - OIC_LOG(DEBUG, TAG, "this callback is already added"); - return CA_STATUS_OK; - } - } - - cbitem = (CAIPCBData_t *)OICCalloc(1, sizeof(*cbitem)); - if (!cbitem) - { - OIC_LOG(ERROR, TAG, "Malloc failed"); - return CA_STATUS_FAILED; - } - - cbitem->adapter = adapter; - cbitem->callback = callback; - LL_APPEND(g_adapterCallbackList, cbitem); - - return CA_STATUS_OK; + return CAIPSetNetworkMonitorCallbackInternal(callback, adapter, g_adapterCallbackList); } CAResult_t CAIPUnSetNetworkMonitorCallback(CATransportAdapter_t adapter) { - CAIPCBData_t *cbitem = NULL; - CAIPCBData_t *tmpCbitem = NULL; - LL_FOREACH_SAFE(g_adapterCallbackList, cbitem, tmpCbitem) - { - if (cbitem && adapter == cbitem->adapter) - { - OIC_LOG(DEBUG, TAG, "remove specific callback"); - LL_DELETE(g_adapterCallbackList, cbitem); - OICFree(cbitem); - return CA_STATUS_OK; - } - } - return CA_STATUS_OK; + return CAIPUnSetNetworkMonitorCallbackInternal(adapter, g_adapterCallbackList); } u_arraylist_t *CAFindInterfaceChange() diff --git a/resource/csdk/connectivity/src/ip_adapter/windows/caipnwmonitor.c b/resource/csdk/connectivity/src/ip_adapter/windows/caipnwmonitor.c index e1902ec..18813d4 100644 --- a/resource/csdk/connectivity/src/ip_adapter/windows/caipnwmonitor.c +++ b/resource/csdk/connectivity/src/ip_adapter/windows/caipnwmonitor.c @@ -61,16 +61,6 @@ CAResult_t CAIPStopNetworkMonitor(CATransportAdapter_t adapter) * Not used in win32, but caipserver currently requires this function * be defined. not critical. */ -int CAGetPollingInterval(int interval) -{ - return interval; -} - -/** - * @todo Implement network interface monitoring. - * Not used in win32, but caipserver currently requires this function - * be defined. not critical. - */ u_arraylist_t *CAFindInterfaceChange() { u_arraylist_t *iflist = NULL; diff --git a/resource/csdk/connectivity/src/tcp_adapter/SConscript b/resource/csdk/connectivity/src/tcp_adapter/SConscript old mode 100644 new mode 100755 index a1b8806..cc3f8d3 --- a/resource/csdk/connectivity/src/tcp_adapter/SConscript +++ b/resource/csdk/connectivity/src/tcp_adapter/SConscript @@ -18,7 +18,8 @@ common_files = None if target_os in ['linux', 'tizen', 'android', 'ios', 'tizenrt']: common_files = [ os.path.join(src_dir, 'catcpadapter.c'), - os.path.join(src_dir, 'catcpserver.c') ] + os.path.join(src_dir, 'catcpserver.c'), + os.path.join(src_dir, 'catcpserver_mutex.c'),] else : common_files = [os.path.join(src_dir, 'catcpadapter.c')] diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c index f0ca652..2eb798c 100755 --- a/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver.c @@ -49,6 +49,7 @@ #include "caipnwmonitor.h" #include #include "caadapterutils.h" +#include "catcpserver_mutex.h" #include "octhread.h" #include "oic_malloc.h" #include "oic_string.h" @@ -94,26 +95,6 @@ static ca_thread_pool_t g_threadPool = NULL; static uint32_t g_taskId = 0; /** - * Mutex to synchronize device object list. - */ -static oc_mutex g_mutexObjectList = NULL; - -/** - * Conditional mutex to synchronize. - */ -static oc_cond g_condObjectList = NULL; - -/** - * Mutex to synchronize send. - */ -static oc_mutex g_mutexSend = NULL; - -/** - * Conditional mutex to synchronize send. - */ -static oc_cond g_condSend = NULL; - -/** * Maintains the callback to be notified when data received from remote device. */ static CATCPPacketReceivedCallback g_packetReceivedCallback = NULL; @@ -196,100 +177,6 @@ CAResult_t CASetTCPServerSocketBindIP(const char* ifname) FDS[COUNT].fd = caglobals.tcp.TYPE.fd; \ FDS[COUNT].events = POLLIN; -void CATCPDestroyMutex() -{ - if (g_mutexObjectList) - { - oc_mutex_free(g_mutexObjectList); - g_mutexObjectList = NULL; - } -} - -CAResult_t CATCPCreateMutex() -{ - if (!g_mutexObjectList) - { - g_mutexObjectList = oc_mutex_new_recursive(); - if (!g_mutexObjectList) - { - OIC_LOG(ERROR, TAG, "Failed to create mutex!"); - return CA_STATUS_FAILED; - } - } - - return CA_STATUS_OK; -} - -void CATCPDestroyCond() -{ - if (g_condObjectList) - { - oc_cond_free(g_condObjectList); - g_condObjectList = NULL; - } -} - -CAResult_t CATCPCreateCond() -{ - if (!g_condObjectList) - { - g_condObjectList = oc_cond_new(); - if (!g_condObjectList) - { - OIC_LOG(ERROR, TAG, "Failed to create cond!"); - return CA_STATUS_FAILED; - } - } - return CA_STATUS_OK; -} - -void CATCPDestroySendMutex() -{ - if (g_mutexSend) - { - oc_mutex_free(g_mutexSend); - g_mutexSend = NULL; - } -} - -CAResult_t CATCPCreateSendMutex() -{ - if (!g_mutexSend) - { - g_mutexSend = oc_mutex_new(); - if (!g_mutexSend) - { - OIC_LOG(ERROR, TAG, "Failed to create send mutex!"); - return CA_STATUS_FAILED; - } - } - - return CA_STATUS_OK; -} - -void CATCPDestroySendCond() -{ - if (g_condSend) - { - oc_cond_free(g_condSend); - g_condSend = NULL; - } -} - -CAResult_t CATCPCreateSendCond() -{ - if (!g_condSend) - { - g_condSend = oc_cond_new(); - if (!g_condSend) - { - OIC_LOG(ERROR, TAG, "Failed to create send cond!"); - return CA_STATUS_FAILED; - } - } - return CA_STATUS_OK; -} - static void CAReceiveHandler(void *data) { (void)data; diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpserver_mutex.c b/resource/csdk/connectivity/src/tcp_adapter/catcpserver_mutex.c new file mode 100755 index 0000000..100c64b --- /dev/null +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver_mutex.c @@ -0,0 +1,140 @@ +/* **************************************************************** + * + * Copyright 2019 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 "catcpserver_mutex.h" +/** + * Logging tag for module name. + */ +//#define TAG "OIC_CA_TCP_SERVER" +#define TAG TCP_SERVER_TAG + +/** + * Mutex to synchronize device object list. + */ +oc_mutex g_mutexObjectList = NULL; + +/** + * Conditional mutex to synchronize. + */ +oc_cond g_condObjectList= NULL; + +/** + * Mutex to synchronize send. + */ +oc_mutex g_mutexSend= NULL; + +/** + * Conditional mutex to synchronize send. + */ +oc_cond g_condSend= NULL; + +void CATCPDestroyMutex() +{ + if (g_mutexObjectList) + { + oc_mutex_free(g_mutexObjectList); + g_mutexObjectList = NULL; + } +} + +CAResult_t CATCPCreateMutex() +{ + if (!g_mutexObjectList) + { + g_mutexObjectList = oc_mutex_new_recursive(); + if (!g_mutexObjectList) + { + OIC_LOG(ERROR, TAG, "Failed to create mutex!"); + return CA_STATUS_FAILED; + } + } + + return CA_STATUS_OK; +} + +void CATCPDestroyCond() +{ + if (g_condObjectList) + { + oc_cond_free(g_condObjectList); + g_condObjectList = NULL; + } +} + +CAResult_t CATCPCreateCond() +{ + if (!g_condObjectList) + { + g_condObjectList = oc_cond_new(); + if (!g_condObjectList) + { + OIC_LOG(ERROR, TAG, "Failed to create cond!"); + return CA_STATUS_FAILED; + } + } + return CA_STATUS_OK; +} + +void CATCPDestroySendMutex() +{ + if (g_mutexSend) + { + oc_mutex_free(g_mutexSend); + g_mutexSend = NULL; + } +} + +CAResult_t CATCPCreateSendMutex() +{ + if (!g_mutexSend) + { + g_mutexSend = oc_mutex_new(); + if (!g_mutexSend) + { + OIC_LOG(ERROR, TAG, "Failed to create send mutex!"); + return CA_STATUS_FAILED; + } + } + + return CA_STATUS_OK; +} + +void CATCPDestroySendCond() +{ + if (g_condSend) + { + oc_cond_free(g_condSend); + g_condSend = NULL; + } +} + +CAResult_t CATCPCreateSendCond() +{ + if (!g_condSend) + { + g_condSend = oc_cond_new(); + if (!g_condSend) + { + OIC_LOG(ERROR, TAG, "Failed to create send cond!"); + return CA_STATUS_FAILED; + } + } + return CA_STATUS_OK; +} diff --git a/resource/csdk/connectivity/src/tcp_adapter/catcpserver_mutex.h b/resource/csdk/connectivity/src/tcp_adapter/catcpserver_mutex.h new file mode 100755 index 0000000..cedc7f0 --- /dev/null +++ b/resource/csdk/connectivity/src/tcp_adapter/catcpserver_mutex.h @@ -0,0 +1,68 @@ +/* **************************************************************** +* +* Copyright 2019 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. +* +******************************************************************/ + +#ifndef CA_TCP_SERVER_MUTEX_H_ +#define CA_TCP_SERVER_MUTEX_H_ + +#include "octhread.h" +#include "oic_malloc.h" +#include "oic_string.h" +#include "catcpinterface.h" +#include "caipnwmonitor.h" +#include +#include "caadapterutils.h" + +/** + * Mutex to synchronize device object list. + */ +extern oc_mutex g_mutexObjectList; + +/** + * Conditional mutex to synchronize. + */ +extern oc_cond g_condObjectList; + +/** + * Mutex to synchronize send. + */ +extern oc_mutex g_mutexSend; + +/** + * Conditional mutex to synchronize send. + */ +extern oc_cond g_condSend; + +void CATCPDestroyMutex(); + +CAResult_t CATCPCreateMutex(); + +void CATCPDestroyCond(); + +CAResult_t CATCPCreateCond(); + +void CATCPDestroySendMutex(); + +CAResult_t CATCPCreateSendMutex(); + +void CATCPDestroySendCond(); + +CAResult_t CATCPCreateSendCond(); + +#endif diff --git a/resource/csdk/routing/SConscript b/resource/csdk/routing/SConscript old mode 100644 new mode 100755 index cf18ade..b84399b --- a/resource/csdk/routing/SConscript +++ b/resource/csdk/routing/SConscript @@ -51,6 +51,7 @@ if env.get('ROUTING') == 'GW': './src/routingutility.c', './src/routingmanager.c', './src/routingtablemanager.c', + './src/routingtablemanager_endpoint.c', './src/routingmanagerinterface.c', './src/routingmessageparser.c', ] diff --git a/resource/csdk/routing/include/routingtablemanager.h b/resource/csdk/routing/include/routingtablemanager.h old mode 100644 new mode 100755 index 9497b48..0b24135 --- a/resource/csdk/routing/include/routingtablemanager.h +++ b/resource/csdk/routing/include/routingtablemanager.h @@ -142,13 +142,6 @@ OCStackResult RTMFreeGatewayRouteTable(u_linklist_t **gatewayTable); OCStackResult RTMFreeGatewayIdList(u_linklist_t **gatewayIdTable); /** - * Frees the endpoint table memory with nodes containing structute RTMEndpointEntry_t. - * @param[in/out] endpointTable Endpoint Routing Table. - * @return ::OC_STACK_OK or Appropriate error code. - */ -OCStackResult RTMFreeEndpointRouteTable(u_linklist_t **endpointTable); - -/** * Adds the entry to the routing table if the entry for Gateway id is * not preset in Routing table, Updates the Old entry if Entry for * Gateway Id is already present in Routing table i.e routeCost and NextHop @@ -165,16 +158,6 @@ OCStackResult RTMAddGatewayEntry(uint32_t gatewayId, uint32_t nextHop, uint32_t const RTMDestIntfInfo_t *destInterfaces, u_linklist_t **gatewayTable); /** - * Adds the endpoint entry to the routing table. - * @param[in/out] endpointId Endpoint Id. - * @param[in] destAddr Destination Address. - * @param[in/out] endpointTable Endpoint Routing Table. - * @return ::OC_STACK_OK or Appropriate error code. - */ -OCStackResult RTMAddEndpointEntry(uint16_t *endpointId, const CAEndpoint_t *destAddr, - u_linklist_t **endpointTable); - -/** * Removes the gateway entry from the routing table and also removes * corresponding entries having nexthop as removed gateway. * @param[in] gatewayId Gateway id of node need to be removed. @@ -187,14 +170,6 @@ OCStackResult RTMRemoveGatewayEntry(uint32_t gatewayId, u_linklist_t **removedGa u_linklist_t **gatewayTable); /** - * Removes the endpoint entry from the routing table. - * @param[in] endpointId Endpoint id of node need to be removed. - * @param[in/out] endpointTable Endpoint Routing Table. - * @return ::OC_STACK_OK or Appropriate error code. - */ -OCStackResult RTMRemoveEndpointEntry(uint16_t endpointId, u_linklist_t **endpointTable); - -/** * Removes the gateway entry from the routing table which has gateway id and nexthop as given. * @param[in] gatewayId Gateway Id. * @param[in] nextHop Next Hop address. @@ -214,13 +189,6 @@ OCStackResult RTMRemoveGatewayDestEntry(uint32_t gatewayId, uint32_t nextHop, OCStackResult RTMRemoveGateways(u_linklist_t **gatewayTable); /** - * Removes the endpoint nodes. - * @param[in/out] endpointTable Endpoint Routing Table. - * @return ::OC_STACK_OK or Appropriate error code. - */ -OCStackResult RTMRemoveEndpoints(u_linklist_t **endpointTable); - -/** * Gets the neighbor nodes i.e nodes with routecost 1. * @param[in/out] neighbourNodes link list containing neighbor nodes. this list will be pointer to GatewayIds @@ -238,14 +206,6 @@ void RTMGetNeighbours(u_linklist_t **neighbourNodes, const u_linklist_t *gateway RTMGatewayId_t *RTMGetNextHop(uint32_t gatewayId, const u_linklist_t *gatewayTable); /** - * Gets endpoint entry - * @param[in] endpointId Endpoint id of node need to be removed. - * @param[in] endpointTable Endpoint Routing Table. - * @return Endpoint Destination inteface address. - */ -CAEndpoint_t *RTMGetEndpointEntry(uint16_t endpointId, const u_linklist_t *endpointTable); - -/** * Updates destination interface address of an entry with provided gateway id * as destination. * @param[in] gatewayId Gateway Id of Hop need to be updated. diff --git a/resource/csdk/routing/include/routingtablemanager_endpoint.h b/resource/csdk/routing/include/routingtablemanager_endpoint.h new file mode 100755 index 0000000..4730458 --- /dev/null +++ b/resource/csdk/routing/include/routingtablemanager_endpoint.h @@ -0,0 +1,111 @@ +/* **************************************************************** + * + * Copyright 2015 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 routing table manager endpoint. + */ +#ifndef ROUTING_TABLE_MANAGER_ENDPOINT_H_ +#define ROUTING_TABLE_MANAGER_ENDPOINT_H_ + +#ifndef SINGLE_THREAD +#include +#include +#include +#endif + +#if defined(__ANDROID__) +#include +#endif +#include "ulinklist.h" +#include "uarraylist.h" +#include "octypes.h" + + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** + * Initialize the Routing Table Manager. + * @param[in/out] endpointTable Endpoint Routing Table. + * @return ::OC_STACK_OK or Appropriate error code. + */ +OCStackResult RTMEndpointInitialize(u_linklist_t **endpointTable); + +/** + * Terminates the Routing Table Manager.. + * @param[in/out] endpointTable Endpoint Routing Table. + * @return ::OC_STACK_OK or Appropriate error code. + */ +OCStackResult RTMEndpointTerminate(u_linklist_t **endpointTable); + +/** + * Frees the endpoint table memory with nodes containing structute RTMEndpointEntry_t. + * @param[in/out] endpointTable Endpoint Routing Table. + * @return ::OC_STACK_OK or Appropriate error code. + */ +OCStackResult RTMFreeEndpointRouteTable(u_linklist_t **endpointTable); + +/** + * Adds the endpoint entry to the routing table. + * @param[in/out] endpointId Endpoint Id. + * @param[in] destAddr Destination Address. + * @param[in/out] endpointTable Endpoint Routing Table. + * @return ::OC_STACK_OK or Appropriate error code. + */ +OCStackResult RTMAddEndpointEntry(uint16_t *endpointId, const CAEndpoint_t *destAddr, + u_linklist_t **endpointTable); + +/** + * Removes the endpoint entry from the routing table. + * @param[in] endpointId Endpoint id of node need to be removed. + * @param[in/out] endpointTable Endpoint Routing Table. + * @return ::OC_STACK_OK or Appropriate error code. + */ +OCStackResult RTMRemoveEndpointEntry(uint16_t endpointId, u_linklist_t **endpointTable); + +/** + * Removes the endpoint nodes. + * @param[in/out] endpointTable Endpoint Routing Table. + * @return ::OC_STACK_OK or Appropriate error code. + */ +OCStackResult RTMRemoveEndpoints(u_linklist_t **endpointTable); + +/** + * Gets endpoint entry + * @param[in] endpointId Endpoint id of node need to be removed. + * @param[in] endpointTable Endpoint Routing Table. + * @return Endpoint Destination inteface address. + */ +CAEndpoint_t *RTMGetEndpointEntry(uint16_t endpointId, const u_linklist_t *endpointTable); + +/** + * Prints the routing table + * @param[in] endpointTable Endpoint Routing Table. + */ +void RTMEndpointPrintTable(const u_linklist_t *endpointTable); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ROUTING_TABLE_MANAGER_ENDPOINT_H_ */ diff --git a/resource/csdk/routing/src/routingtablemanager.c b/resource/csdk/routing/src/routingtablemanager.c old mode 100644 new mode 100755 index f949c62..3a3cbb9 --- a/resource/csdk/routing/src/routingtablemanager.c +++ b/resource/csdk/routing/src/routingtablemanager.c @@ -21,6 +21,7 @@ #include #include #include "routingtablemanager.h" +#include "routingtablemanager_endpoint.h" #include "routingutility.h" #include "oic_malloc.h" #include "oic_string.h" @@ -60,16 +61,13 @@ OCStackResult RTMInitialize(u_linklist_t **gatewayTable, u_linklist_t **endpoint } } - if (NULL == *endpointTable) + if (OC_STACK_ERROR == RTMEndpointInitialize(endpointTable)) { - *endpointTable = u_linklist_create(); - if (NULL == *endpointTable) - { - OIC_LOG(ERROR, TAG, "Creating Routing Table failed"); - RTMTerminate(gatewayTable, endpointTable); - return OC_STACK_ERROR; - } + OIC_LOG(ERROR, TAG, "Creating EndPoint Routing Table failed"); + RTMTerminate(gatewayTable, endpointTable); + return OC_STACK_ERROR; } + OIC_LOG(DEBUG, TAG, "RTMInitialize OUT"); return OC_STACK_OK; } @@ -118,39 +116,6 @@ OCStackResult RTMFreeGatewayRouteTable(u_linklist_t **gatewayTable) return OC_STACK_OK; } -/* - * Freeing every char pointer of endpoint entry here frees the table. - */ -OCStackResult RTMFreeEndpointRouteTable(u_linklist_t **endpointTable) -{ - OIC_LOG(DEBUG, TAG, "IN"); - if (NULL == endpointTable || NULL == *endpointTable) - { - return OC_STACK_OK; - } - - u_linklist_iterator_t *iterTable = NULL; - u_linklist_init_iterator(*endpointTable, &iterTable); - while (NULL != iterTable) - { - RTMEndpointEntry_t *hop = u_linklist_get_data(iterTable); - if (NULL != hop) - { - OICFree(hop); - } - - OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable); - if (OC_STACK_OK != ret) - { - OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed"); - return OC_STACK_ERROR; - } - } - u_linklist_free(endpointTable); - OIC_LOG(DEBUG, TAG, "OUT"); - return OC_STACK_OK; -} - OCStackResult RTMFreeGatewayIdList(u_linklist_t **gatewayIdTable) { OIC_LOG(DEBUG, TAG, "IN"); @@ -212,15 +177,7 @@ OCStackResult RTMTerminate(u_linklist_t **gatewayTable, u_linklist_t **endpointT *gatewayTable = NULL; } - ret = RTMFreeEndpointRouteTable(endpointTable); - if (OC_STACK_OK != ret) - { - OIC_LOG(ERROR, TAG, "Deleting Endpoint Routing Table failed"); - } - if (NULL != *endpointTable) - { - *endpointTable = NULL; - } + RTMEndpointTerminate(endpointTable); OIC_LOG(DEBUG, TAG, "OUT"); return OC_STACK_OK; } @@ -499,65 +456,6 @@ OCStackResult RTMAddGatewayEntry(uint32_t gatewayId, uint32_t nextHop, uint32_t return OC_STACK_OK; } -OCStackResult RTMAddEndpointEntry(uint16_t *endpointId, const CAEndpoint_t *destAddr, - u_linklist_t **endpointTable) -{ - OIC_LOG(DEBUG, TAG, "IN"); - RM_NULL_CHECK_WITH_RET(endpointId, TAG, "endpointId"); - RM_NULL_CHECK_WITH_RET(destAddr, TAG, "destAddr"); - RM_NULL_CHECK_WITH_RET(endpointTable, TAG, "endpointTable"); - if (NULL == *endpointTable) - { - *endpointTable = u_linklist_create(); - if (NULL == *endpointTable) - { - OIC_LOG(ERROR, TAG, "u_linklist_create failed"); - return OC_STACK_NO_MEMORY; - } - } - - u_linklist_iterator_t *iterTable = NULL; - u_linklist_init_iterator(*endpointTable, &iterTable); - // Iterate over gateway list to find if already entry with this gatewayid is present. - while (NULL != iterTable) - { - RTMEndpointEntry_t *entry = - (RTMEndpointEntry_t *) u_linklist_get_data(iterTable); - - if (NULL != entry && (0 == memcmp(destAddr->addr, entry->destIntfAddr.addr, - strlen(entry->destIntfAddr.addr))) - && destAddr->port == entry->destIntfAddr.port) - { - *endpointId = entry->endpointId; - OIC_LOG(ERROR, TAG, "Adding failed as Enpoint Entry Already present in Table"); - return OC_STACK_DUPLICATE_REQUEST; - } - u_linklist_get_next(&iterTable); - } - - // Filling Entry. - RTMEndpointEntry_t *hopEntry = (RTMEndpointEntry_t *)OICCalloc(1, sizeof(RTMEndpointEntry_t)); - - if (NULL == hopEntry) - { - OIC_LOG(ERROR, TAG, "Malloc failed for hop entry"); - return OC_STACK_ERROR; - } - - hopEntry->endpointId = *endpointId; - hopEntry->destIntfAddr = *destAddr; - - OCStackResult ret = u_linklist_add(*endpointTable, (void *)hopEntry); - if (OC_STACK_OK != ret) - { - OIC_LOG(ERROR, TAG, "Adding Enpoint Entry to Routing Table failed"); - OICFree(hopEntry); - return OC_STACK_ERROR; - } - OIC_LOG(DEBUG, TAG, "OUT"); - return OC_STACK_OK; -} - OCStackResult RTMAddObserver(uint32_t obsID, CAEndpoint_t devAddr, u_linklist_t **gatewayTable) { OIC_LOG(DEBUG, TAG, "IN"); @@ -756,36 +654,6 @@ OCStackResult RTMRemoveGatewayDestEntry(uint32_t gatewayId, uint32_t nextHop, return OC_STACK_ERROR; } -OCStackResult RTMRemoveEndpointEntry(uint16_t endpointId, u_linklist_t **endpointTable) -{ - OIC_LOG(DEBUG, TAG, "IN"); - RM_NULL_CHECK_WITH_RET(endpointTable, TAG, "endpointTable"); - RM_NULL_CHECK_WITH_RET(*endpointTable, TAG, "*endpointTable"); - - u_linklist_iterator_t *iterTable = NULL; - u_linklist_init_iterator(*endpointTable, &iterTable); - while (NULL != iterTable) - { - RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable); - if (NULL != entry && endpointId == entry->endpointId) - { - OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable); - if (OC_STACK_OK != ret) - { - OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed"); - return OC_STACK_ERROR; - } - OICFree(entry); - } - else - { - u_linklist_get_next(&iterTable); - } - } - OIC_LOG(DEBUG, TAG, "OUT"); - return OC_STACK_OK; -} - OCStackResult RTMRemoveGateways(u_linklist_t **gatewayTable) { OIC_LOG(DEBUG, TAG, "IN"); @@ -806,25 +674,6 @@ OCStackResult RTMRemoveGateways(u_linklist_t **gatewayTable) return OC_STACK_OK; } -OCStackResult RTMRemoveEndpoints(u_linklist_t **endpointTable) -{ - OIC_LOG(DEBUG, TAG, "IN"); - if (NULL == endpointTable || NULL == *endpointTable) - { - OIC_LOG(DEBUG, TAG, "OUT"); - return OC_STACK_OK; - } - - OCStackResult ret = RTMFreeEndpointRouteTable(endpointTable); - if (OC_STACK_OK != ret) - { - OIC_LOG(ERROR, TAG, "Freeing Endpoints failed"); - return OC_STACK_ERROR; - } - OIC_LOG(DEBUG, TAG, "OUT"); - return OC_STACK_OK; -} - void RTMFreeGateway(RTMGatewayId_t *gateway, u_linklist_t **gatewayTable) { OIC_LOG(DEBUG, TAG, "IN"); @@ -909,32 +758,6 @@ RTMGatewayId_t *RTMGetNextHop(uint32_t gatewayId, const u_linklist_t *gatewayTab return NULL; } -CAEndpoint_t *RTMGetEndpointEntry(uint16_t endpointId, const u_linklist_t *endpointTable) -{ - OIC_LOG(DEBUG, TAG, "IN"); - if (NULL == endpointTable) - { - OIC_LOG(ERROR, TAG, "endpointTable is null"); - return NULL; - } - - u_linklist_iterator_t *iterTable = NULL; - u_linklist_init_iterator(endpointTable, &iterTable); - - while (NULL != iterTable) - { - RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable); - if (NULL != entry && (endpointId == entry->endpointId)) - { - OIC_LOG(DEBUG, TAG, "OUT"); - return &(entry->destIntfAddr); - } - u_linklist_get_next(&iterTable); - } - OIC_LOG(DEBUG, TAG, "OUT"); - return NULL; -} - void RTMGetObserverList(OCObservationId **obsList, uint8_t *obsListLen, const u_linklist_t *gatewayTable) { @@ -1350,24 +1173,5 @@ void RTMPrintTable(const u_linklist_t *gatewayTable, const u_linklist_t *endpoin u_linklist_get_next(&iterTable); } - OIC_LOG(DEBUG, RM_TAG, "=================Endpoint List table============================\n"); - u_linklist_iterator_t *iterEndpointTable = NULL; - u_linklist_init_iterator(endpointTable, &iterEndpointTable); - - // Iterate over endpoint list to find if already entry for gatewayid is present. - while (NULL != iterEndpointTable) - { - RTMEndpointEntry_t *hop = - (RTMEndpointEntry_t *) u_linklist_get_data(iterEndpointTable); - if (NULL == hop) - { - OIC_LOG(ERROR, RM_TAG, "Printing Table Failed"); - return; - } - OIC_LOG_V(DEBUG, RM_TAG, "EndpointId : %u\naddr : %s Port : %d", - hop->endpointId, hop->destIntfAddr.addr, hop->destIntfAddr.port); - - OIC_LOG(DEBUG, RM_TAG, "********************************************\n"); - u_linklist_get_next(&iterEndpointTable); - } + RTMEndpointPrintTable(endpointTable); } diff --git a/resource/csdk/routing/src/routingtablemanager_endpoint.c b/resource/csdk/routing/src/routingtablemanager_endpoint.c new file mode 100755 index 0000000..9b68c3a --- /dev/null +++ b/resource/csdk/routing/src/routingtablemanager_endpoint.c @@ -0,0 +1,267 @@ +/* **************************************************************** + * + * Copyright 2015 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 "routingtablemanager_endpoint.h" +#include "routingtablemanager.h" +#include "routingutility.h" +#include "oic_malloc.h" +#include "oic_string.h" +#include "include/logger.h" + +/** + * Logging tag for module name. + */ +#define TAG "OIC_RM_TM" + +/** + * Tag for printing the Routing table. + */ +#define RM_TAG "OIC_RM_RAP" + +OCStackResult RTMEndpointInitialize(u_linklist_t **endpointTable) +{ + OIC_LOG(DEBUG, TAG, "RTMEndpointInitialize IN"); + if (NULL == *endpointTable) + { + *endpointTable = u_linklist_create(); + if (NULL == *endpointTable) + { + OIC_LOG(ERROR, TAG, "Creating Routing Table failed"); + return OC_STACK_ERROR; + } + } + OIC_LOG(DEBUG, TAG, "RTMEndpointInitialize OUT"); + return OC_STACK_OK; +} + +/* + * Freeing every char pointer of endpoint entry here frees the table. + */ +OCStackResult RTMFreeEndpointRouteTable(u_linklist_t **endpointTable) +{ + OIC_LOG(DEBUG, TAG, "IN"); + if (NULL == endpointTable || NULL == *endpointTable) + { + return OC_STACK_OK; + } + + u_linklist_iterator_t *iterTable = NULL; + u_linklist_init_iterator(*endpointTable, &iterTable); + while (NULL != iterTable) + { + RTMEndpointEntry_t *hop = u_linklist_get_data(iterTable); + if (NULL != hop) + { + OICFree(hop); + } + + OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable); + if (OC_STACK_OK != ret) + { + OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed"); + return OC_STACK_ERROR; + } + } + u_linklist_free(endpointTable); + OIC_LOG(DEBUG, TAG, "OUT"); + return OC_STACK_OK; +} + +/* + * Freeing memory first and then Freeing linked list for gateway and endpoint. + */ +OCStackResult RTMEndpointTerminate(u_linklist_t **endpointTable) +{ + OIC_LOG(DEBUG, TAG, "IN"); + + OCStackResult ret = RTMFreeEndpointRouteTable(endpointTable); + if (OC_STACK_OK != ret) + { + OIC_LOG(ERROR, TAG, "Deleting Endpoint Routing Table failed"); + } + if (NULL != *endpointTable) + { + *endpointTable = NULL; + } + OIC_LOG(DEBUG, TAG, "OUT"); + return OC_STACK_OK; +} + +OCStackResult RTMAddEndpointEntry(uint16_t *endpointId, const CAEndpoint_t *destAddr, + u_linklist_t **endpointTable) +{ + OIC_LOG(DEBUG, TAG, "IN"); + RM_NULL_CHECK_WITH_RET(endpointId, TAG, "endpointId"); + RM_NULL_CHECK_WITH_RET(destAddr, TAG, "destAddr"); + RM_NULL_CHECK_WITH_RET(endpointTable, TAG, "endpointTable"); + if (NULL == *endpointTable) + { + *endpointTable = u_linklist_create(); + if (NULL == *endpointTable) + { + OIC_LOG(ERROR, TAG, "u_linklist_create failed"); + return OC_STACK_NO_MEMORY; + } + } + + u_linklist_iterator_t *iterTable = NULL; + u_linklist_init_iterator(*endpointTable, &iterTable); + // Iterate over gateway list to find if already entry with this gatewayid is present. + while (NULL != iterTable) + { + RTMEndpointEntry_t *entry = + (RTMEndpointEntry_t *) u_linklist_get_data(iterTable); + + if (NULL != entry && (0 == memcmp(destAddr->addr, entry->destIntfAddr.addr, + strlen(entry->destIntfAddr.addr))) + && destAddr->port == entry->destIntfAddr.port) + { + *endpointId = entry->endpointId; + OIC_LOG(ERROR, TAG, "Adding failed as Enpoint Entry Already present in Table"); + return OC_STACK_DUPLICATE_REQUEST; + } + u_linklist_get_next(&iterTable); + } + + // Filling Entry. + RTMEndpointEntry_t *hopEntry = (RTMEndpointEntry_t *)OICCalloc(1, sizeof(RTMEndpointEntry_t)); + + if (NULL == hopEntry) + { + OIC_LOG(ERROR, TAG, "Malloc failed for hop entry"); + return OC_STACK_ERROR; + } + + hopEntry->endpointId = *endpointId; + hopEntry->destIntfAddr = *destAddr; + + OCStackResult ret = u_linklist_add(*endpointTable, (void *)hopEntry); + if (OC_STACK_OK != ret) + { + OIC_LOG(ERROR, TAG, "Adding Enpoint Entry to Routing Table failed"); + OICFree(hopEntry); + return OC_STACK_ERROR; + } + OIC_LOG(DEBUG, TAG, "OUT"); + return OC_STACK_OK; +} + +OCStackResult RTMRemoveEndpointEntry(uint16_t endpointId, u_linklist_t **endpointTable) +{ + OIC_LOG(DEBUG, TAG, "IN"); + RM_NULL_CHECK_WITH_RET(endpointTable, TAG, "endpointTable"); + RM_NULL_CHECK_WITH_RET(*endpointTable, TAG, "*endpointTable"); + + u_linklist_iterator_t *iterTable = NULL; + u_linklist_init_iterator(*endpointTable, &iterTable); + while (NULL != iterTable) + { + RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable); + if (NULL != entry && endpointId == entry->endpointId) + { + OCStackResult ret = u_linklist_remove(*endpointTable, &iterTable); + if (OC_STACK_OK != ret) + { + OIC_LOG(ERROR, TAG, "Deleting Entry from Routing Table failed"); + return OC_STACK_ERROR; + } + OICFree(entry); + } + else + { + u_linklist_get_next(&iterTable); + } + } + OIC_LOG(DEBUG, TAG, "OUT"); + return OC_STACK_OK; +} + +OCStackResult RTMRemoveEndpoints(u_linklist_t **endpointTable) +{ + OIC_LOG(DEBUG, TAG, "IN"); + if (NULL == endpointTable || NULL == *endpointTable) + { + OIC_LOG(DEBUG, TAG, "OUT"); + return OC_STACK_OK; + } + + OCStackResult ret = RTMFreeEndpointRouteTable(endpointTable); + if (OC_STACK_OK != ret) + { + OIC_LOG(ERROR, TAG, "Freeing Endpoints failed"); + return OC_STACK_ERROR; + } + OIC_LOG(DEBUG, TAG, "OUT"); + return OC_STACK_OK; +} + +CAEndpoint_t *RTMGetEndpointEntry(uint16_t endpointId, const u_linklist_t *endpointTable) +{ + OIC_LOG(DEBUG, TAG, "IN"); + if (NULL == endpointTable) + { + OIC_LOG(ERROR, TAG, "endpointTable is null"); + return NULL; + } + + u_linklist_iterator_t *iterTable = NULL; + u_linklist_init_iterator(endpointTable, &iterTable); + + while (NULL != iterTable) + { + RTMEndpointEntry_t *entry = u_linklist_get_data(iterTable); + if (NULL != entry && (endpointId == entry->endpointId)) + { + OIC_LOG(DEBUG, TAG, "OUT"); + return &(entry->destIntfAddr); + } + u_linklist_get_next(&iterTable); + } + OIC_LOG(DEBUG, TAG, "OUT"); + return NULL; +} + +void RTMEndpointPrintTable(const u_linklist_t *endpointTable) +{ + RM_NULL_CHECK_VOID(endpointTable, TAG, "endpointTable"); + + OIC_LOG(DEBUG, RM_TAG, "=================Endpoint List table============================\n"); + u_linklist_iterator_t *iterEndpointTable = NULL; + u_linklist_init_iterator(endpointTable, &iterEndpointTable); + + // Iterate over endpoint list to find if already entry for gatewayid is present. + while (NULL != iterEndpointTable) + { + RTMEndpointEntry_t *hop = + (RTMEndpointEntry_t *) u_linklist_get_data(iterEndpointTable); + if (NULL == hop) + { + OIC_LOG(ERROR, RM_TAG, "Printing Table Failed"); + return; + } + OIC_LOG_V(DEBUG, RM_TAG, "EndpointId : %u\naddr : %s Port : %d Flags : %d", + hop->endpointId, hop->destIntfAddr.addr, hop->destIntfAddr.port, hop->destIntfAddr.flags); + + OIC_LOG(DEBUG, RM_TAG, "********************************************\n"); + u_linklist_get_next(&iterEndpointTable); + } +} diff --git a/resource/csdk/security/include/internal/aclresource.h b/resource/csdk/security/include/internal/aclresource.h index 0172ae4..42c75a9 100644 --- a/resource/csdk/security/include/internal/aclresource.h +++ b/resource/csdk/security/include/internal/aclresource.h @@ -184,6 +184,13 @@ OicSecAcl_t* CBORPayloadToAcl2(const uint8_t *cborPayload, const size_t size); */ void printACL(const OicSecAcl_t* acl); +/** + * Check if Security ACE permission property matched + * @param permission value for matching + * @return OC_STACK_OK if permissions matched, else::OC_STACK_ERROR. + */ +OCStackResult CheckSecurityACEPermission(uint16_t permission); + #ifdef __cplusplus } #endif diff --git a/resource/csdk/security/include/internal/psinterface.h b/resource/csdk/security/include/internal/psinterface.h index 73bc90e..0d7b179 100644 --- a/resource/csdk/security/include/internal/psinterface.h +++ b/resource/csdk/security/include/internal/psinterface.h @@ -36,6 +36,14 @@ typedef enum PS_NO_EXTERNAL_DB_SET = 99, // no external DB set (initial state) } PSStatus_t; +//SVR DB Validity status +typedef enum +{ + SVRDB_NOT_VALID = 0, + SVRDB_VALID = 1, + SVRDB_NOT_CHECKED = 255 +} SVRDBValidStatus_t; + /** * Reads the Secure Virtual Database from PS into dynamically allocated * memory buffer. @@ -123,6 +131,13 @@ OCStackResult CreateResetProfile(void); void SetPSStatus(PSStatus_t status); /** + * This function return SVR DB state: valid, not valid or was not checked yet + * + * @return Status of SVR DB + */ +SVRDBValidStatus_t GetSVRDBValidStatus(void); + +/** * This method prints debug log that displays persistent storage status. */ void PrintPSStatus(void); @@ -156,6 +171,18 @@ OCStackResult setSecurePSI(const unsigned char *key, const OCPersistentStorage * * * @return pointer on resetSVRDB structure for manipulating with callback */ -resetSVRDBCB_t* const GetResetSVRDBCB(void); +resetSVRDBCB_t* GetResetSVRDBCB(void); + +/** + * API to validate data in SVR DB. + * @return ::OC_STACK_OK on success and other value otherwise. + */ +OCStackResult CheckSVRDBValidity(void); + +/** + * This function check if ResetPF are created + * @return true in positive case, false otherwise. + */ +bool isResetPFExist(void); #endif //IOTVT_SRM_PSI_H diff --git a/resource/csdk/security/include/internal/pstatresource.h b/resource/csdk/security/include/internal/pstatresource.h index c021cb0..d158cff 100644 --- a/resource/csdk/security/include/internal/pstatresource.h +++ b/resource/csdk/security/include/internal/pstatresource.h @@ -122,6 +122,15 @@ bool GetPstatIsop(); */ OCStackResult SetPstatSelfOwnership(const OicUuid_t* newROwner); +/** + * Gets the cm value for pstat resource. + * + * @param[out] cm pointer for pstat.cm copy value. + * + * @retval ::OC_STACK_OK for Success, otherwise some error value. + */ +OCStackResult GetPstatCm(OicSecDpm_t *cm); + #ifdef __cplusplus } #endif diff --git a/resource/csdk/security/include/srmutility.h b/resource/csdk/security/include/srmutility.h index 22936b7..a48d322 100644 --- a/resource/csdk/security/include/srmutility.h +++ b/resource/csdk/security/include/srmutility.h @@ -199,4 +199,31 @@ bool IsNilUuid(const OicUuid_t *uuid); } #endif // __cplusplus +/** + * cbor default init + */ +#ifndef OC_DEFAULT_CBOR_VALUE +# if (__STDC_VERSION__ >= 199901L) +# define OC_DEFAULT_CBOR_VALUE {.parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0} +# else +# define OC_DEFAULT_CBOR_VALUE {NULL, NULL, 0, 0, 0, 0} +# endif +#endif + +#ifndef OC_DEFAULT_CBOR_PARSER +# if (__STDC_VERSION__ >= 199901L) +# define OC_DEFAULT_CBOR_PARSER {.end = NULL, .flags = 0} +# else +# define OC_DEFAULT_CBOR_PARSER {NULL, 0} +# endif +#endif + +#ifndef OC_DEFAULT_OICUUID +# if (__STDC_VERSION__ >= 199901L) +# define OC_DEFAULT_OICUUID {.id= { 0 } } +# else +# define OC_DEFAULT_OICUUID {{ 0 }} +# endif +#endif + #endif //IOTVT_SRM_UTILITY_H diff --git a/resource/csdk/security/src/aclresource.c b/resource/csdk/security/src/aclresource.c index dc4b874..b1a3bc8 100644 --- a/resource/csdk/security/src/aclresource.c +++ b/resource/csdk/security/src/aclresource.c @@ -2895,3 +2895,51 @@ OCStackResult GetAclRownerId(OicUuid_t *rowneruuid) } return retVal; } + +OCStackResult CheckSecurityACEPermission(uint16_t permission) +{ + OIC_LOG_V(DEBUG, TAG, "IN: %s", __func__); + + OCStackResult ret = OC_STACK_ERROR; + OicSecAce_t *ace = NULL; + OicSecAce_t *tempAce = NULL; + + if(NULL == gAcl) + { + OIC_LOG_V(ERROR, TAG, "%s - gAcl is NULL!", __func__); + ret = OC_STACK_ERROR; + goto exit; + } + + LL_FOREACH_SAFE(gAcl->aces, ace, tempAce) + { + if(0 != memcmp(&ace->subjectuuid, &WILDCARD_SUBJECT_ID, sizeof(OicUuid_t))) + { + continue; + } + + OicSecRsrc_t* rsrc = NULL; + int flag = 0; + LL_FOREACH(ace->resources, rsrc) + { + if(strncmp(rsrc->href, OIC_RSRC_DOXM_URI, strlen(OIC_RSRC_DOXM_URI) + 1) == 0 || + strncmp(rsrc->href, OIC_RSRC_CRED_URI, strlen(OIC_RSRC_CRED_URI) + 1) == 0 || + strncmp(rsrc->href, OIC_RSRC_PSTAT_URI, strlen(OIC_RSRC_PSTAT_URI) + 1) == 0) + { + flag = 1; + break; + } + } + + if (flag && !(permission & ace->permission) ) + { + ret = OC_STACK_ERROR; + goto exit; + } + } + ret = OC_STACK_OK; + + exit: + OIC_LOG_V(DEBUG, TAG, "OUT: %s", __func__); + return ret; +} diff --git a/resource/csdk/security/src/doxmresource.c b/resource/csdk/security/src/doxmresource.c index c7362fb..98c8405 100644 --- a/resource/csdk/security/src/doxmresource.c +++ b/resource/csdk/security/src/doxmresource.c @@ -2493,7 +2493,7 @@ OCStackResult GetDoxmDevOwnerId(OicUuid_t *devownerid) { OIC_LOG_V(DEBUG, TAG, "GetDoxmDevOwnerId(): gDoxm owned = %d.", \ gDoxm->owned); - if (gDoxm->owned) + //if (gDoxm->owned) { *devownerid = gDoxm->owner; retVal = OC_STACK_OK; @@ -2507,7 +2507,7 @@ OCStackResult GetDoxmRownerId(OicUuid_t *rowneruuid) OCStackResult retVal = OC_STACK_ERROR; if (gDoxm) { - if( gDoxm->owned ) + // if( gDoxm->owned ) { *rowneruuid = gDoxm->rownerID; retVal = OC_STACK_OK; diff --git a/resource/csdk/security/src/psinterface.c b/resource/csdk/security/src/psinterface.c index d6474ab..0964232 100644 --- a/resource/csdk/security/src/psinterface.c +++ b/resource/csdk/security/src/psinterface.c @@ -42,6 +42,8 @@ #include "psinterface.h" #include "doxmresource.h" #include "octhread.h" +#include "aclresource.h" +#include "credresource.h" #define TAG "OIC_SRM_PSI" @@ -59,12 +61,24 @@ static oc_mutex g_mutexDb = NULL; // Persistent Storage status static PSStatus_t g_psStatus = PS_NO_EXTERNAL_DB_SET; +//SVR DB Validation status +static SVRDBValidStatus_t g_svrdbValidStatus = SVRDB_NOT_CHECKED; + static resetSVRDBCB_t g_resetSVRDBCB = {0}; -resetSVRDBCB_t* const GetResetSVRDBCB(void) +resetSVRDBCB_t* GetResetSVRDBCB(void) { return &g_resetSVRDBCB; } + +/** + * Set status of SVR DB after call CheckSVRDBValidity() API + */ +static void SetSVRDBValidStatus(SVRDBValidStatus_t status) +{ + g_svrdbValidStatus = status; +} + /** * Update the Persistent Storage Database size. */ @@ -1217,6 +1231,11 @@ void SetPSStatus(PSStatus_t status) g_psStatus = status; } +SVRDBValidStatus_t GetSVRDBValidStatus() +{ + return g_svrdbValidStatus; +} + void PrintPSStatus(void) { switch(g_psStatus) @@ -1235,3 +1254,262 @@ void PrintPSStatus(void) } } + +OCStackResult CheckSVRDBValidity(void) +{ + OIC_LOG_V(DEBUG, TAG, "In %s", __func__); + + OCStackResult res = OC_STACK_ERROR; + OicUuid_t emptyUuid = OC_DEFAULT_OICUUID; + OicUuid_t devOwnerUuid = OC_DEFAULT_OICUUID; + OicUuid_t devRownerUuid = OC_DEFAULT_OICUUID; + OicUuid_t resRowneruuid = OC_DEFAULT_OICUUID; + bool isDevOwnerUuidEmpty = false; + bool isDevRownerUuidEmpty = false; + bool isPstatRownerUuidEmpty = false; + bool isAclRownerUuidEmpty = false; + bool isCredRownerUuidEmpty = false; + OicSecDpm_t cm = NORMAL; + + bool isOwned = false; + res = GetDoxmIsOwned(&isOwned); + + if( OC_STACK_OK != res) + { + OIC_LOG_V(ERROR, TAG, "%s - doxm is NULL!", __func__); + goto exit; + } + + if(OC_STACK_OK != GetDoxmDevOwnerId(&devOwnerUuid) || + OC_STACK_OK != GetDoxmRownerId(&devRownerUuid)) + { + res = OC_STACK_ERROR; + goto exit; + } + + if (0 == memcmp(&devOwnerUuid, &emptyUuid, sizeof(devOwnerUuid))) + { + isDevOwnerUuidEmpty = true; + } + else + { + isDevOwnerUuidEmpty = false; + } + + if (0 == memcmp(&devRownerUuid, &emptyUuid, sizeof(devRownerUuid))) + { + isDevRownerUuidEmpty = true; + } + else + { + isDevRownerUuidEmpty = false; + } + + if (OC_STACK_OK != GetPstatRownerId(&resRowneruuid)) + { + res = OC_STACK_ERROR; + goto exit; + } + + if (0 == memcmp(&resRowneruuid, &emptyUuid, sizeof(resRowneruuid))) + { + isPstatRownerUuidEmpty = true; + } + else + { + isPstatRownerUuidEmpty = false; + } + + VERIFY_SUCCESS(TAG, OC_STACK_OK == GetPstatCm(&cm), ERROR); + + if (OC_STACK_OK != GetAclRownerId(&resRowneruuid)) + { + res = OC_STACK_ERROR; + goto exit; + } + + if (0 == memcmp(&resRowneruuid, &emptyUuid, sizeof(resRowneruuid))) + { + isAclRownerUuidEmpty = true; + } + else + { + isAclRownerUuidEmpty = false; + } + + if (OC_STACK_OK != GetCredRownerId(&resRowneruuid)) + { + res = OC_STACK_ERROR; + goto exit; + } + + if (0 == memcmp(&resRowneruuid, &emptyUuid, sizeof(resRowneruuid))) + { + isCredRownerUuidEmpty = true; + } + else + { + isCredRownerUuidEmpty = false; + } + + if(true == isOwned) + { + if ( isDevOwnerUuidEmpty ||isDevRownerUuidEmpty ) + { + OIC_LOG_V(ERROR, TAG, "%s - uuid is empty for owned device!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if(!GetPstatIsop() || (cm & TAKE_OWNER)) + { + OIC_LOG_V(ERROR, TAG, "%s - isop or cm properties are incorrect!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if (isPstatRownerUuidEmpty) + { + OIC_LOG_V(ERROR, TAG, "%s - pstatRownerUuid is empty for owned device!", __func__); + res = OC_STACK_ERROR;; + goto exit; + } + + if (isAclRownerUuidEmpty) + { + OIC_LOG_V(ERROR, TAG, "%s - aclRownerUuid is empty for owned device!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if (isCredRownerUuidEmpty) + { + OIC_LOG_V(ERROR, TAG, "%s - credRownerUuid is empty for owned device!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if (OC_STACK_OK != CheckSecurityACEPermission(PERMISSION_READ)) + { + OIC_LOG_V(ERROR, TAG, "%s - ACE permission doesn't match to READ", __func__); + res = OC_STACK_ERROR; + goto exit; + } + } + else + { + if ( !isDevOwnerUuidEmpty ||!isDevRownerUuidEmpty ) + { + OIC_LOG_V(ERROR, TAG, "%s - uuid is not empty for unOwned device!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if (!isPstatRownerUuidEmpty) + { + OIC_LOG_V(ERROR, TAG, "%s - pstatRownerUuid is not empty for unOwned device!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if(GetPstatIsop() || !(cm & TAKE_OWNER)) + { + res = OC_STACK_ERROR; + goto exit; + } + + if (!isAclRownerUuidEmpty) + { + OIC_LOG_V(ERROR, TAG, "%s - aclRownerUuid is not empty for unOwned device!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if (!isCredRownerUuidEmpty) + { + OIC_LOG_V(ERROR, TAG, "%s - credRownerUuid is not empty for unOwned device!", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + if (OC_STACK_OK != CheckSecurityACEPermission(PERMISSION_READ | PERMISSION_WRITE)) + { + OIC_LOG_V(ERROR, TAG, "%s - ACE permission doesn't match to READ|WRITE", __func__); + res = OC_STACK_ERROR; + goto exit; + } + + } + res = OC_STACK_OK; + + exit: + + if (OC_STACK_OK != res) + { + if (isResetPFExist() && !isOwned) + { + res = ResetSecureResourceInPS(); + OIC_LOG_V(INFO, TAG, "%s - Secure resources reseted (%d)", __func__, res); + } + else + { + res = OC_STACK_INCONSISTENT_DB; + } + } + + if(OC_STACK_OK == res) + { + SetSVRDBValidStatus(SVRDB_VALID); + } + else + { + SetSVRDBValidStatus(SVRDB_NOT_VALID); + } + OIC_LOG_V(DEBUG, TAG, "Out %s", __func__); + + return res; +} + +bool isResetPFExist(void) +{ + OIC_LOG_V(DEBUG, TAG, "In %s", __func__); + + bool ret = false; + size_t dbSize = 0; + uint8_t *dbData = NULL; + + OCStackResult ocRes = OC_STACK_ERROR; + + ocRes = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize); + if (OC_STACK_OK != ocRes) + { + OIC_LOG_V(ERROR, TAG, "GetSecureVirtualDatabaseFromPS() is failed(%d)", ocRes); + } + if (dbData && dbSize) + { + ocRes = OC_STACK_ERROR; + { + CborParser parser = OC_DEFAULT_CBOR_PARSER; + CborValue cbor = OC_DEFAULT_CBOR_VALUE; + cbor_parser_init(dbData, dbSize, 0, &parser, &cbor); + CborValue curVal = OC_DEFAULT_CBOR_VALUE; + CborError cborFindResult = CborNoError; + + cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_RESET_PF_NAME, &curVal); + if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal)) + { + OIC_LOG(DEBUG, TAG, "Reset Profile already exists!"); + ret = true; + } + else + { + OIC_LOG(DEBUG, TAG, "Reset Profile doesn't exists!"); + ret = false; + } + } + } + OICFree(dbData); + OIC_LOG_V(DEBUG, TAG, "Out %s", __func__); + + return ret; +} diff --git a/resource/csdk/security/src/pstatresource.c b/resource/csdk/security/src/pstatresource.c index ceb8949..61e2713 100644 --- a/resource/csdk/security/src/pstatresource.c +++ b/resource/csdk/security/src/pstatresource.c @@ -1060,3 +1060,12 @@ exit: return ret; } +OCStackResult GetPstatCm(OicSecDpm_t *cm) +{ + if (gPstat && cm) + { + *cm = gPstat->cm; + return OC_STACK_OK; + } + return OC_STACK_ERROR; +} diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp index e9e481a..239cfc2 100755 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp @@ -1070,6 +1070,15 @@ int main(int argc, char* argv[]) OCSetRAInfo(&rainfo); #endif +#ifdef WITH_PROCESS_EVENT + processEvent = oc_event_new(); + if (!processEvent) + { + OIC_LOG(INFO, TAG, "oc_event_new failed!"); + exit (EXIT_FAILURE); + } +#endif + OIC_LOG(DEBUG, TAG, "OCServer is starting..."); if (OCInit(NULL, 0, OC_SERVER) != OC_STACK_OK) @@ -1123,14 +1132,8 @@ int main(int argc, char* argv[]) OIC_LOG(INFO, TAG, "Device Registration failed!"); exit (EXIT_FAILURE); } -#ifdef WITH_PROCESS_EVENT - processEvent = oc_event_new(); - if (!processEvent) - { - OIC_LOG(INFO, TAG, "oc_event_new failed!"); - exit (EXIT_FAILURE); - } +#ifdef WITH_PROCESS_EVENT OCRegisterProcessEvent(processEvent); #endif @@ -1197,6 +1200,11 @@ int main(int argc, char* argv[]) OIC_LOG(INFO, TAG, "Exiting ocserver main loop..."); + if (OCStop() != OC_STACK_OK) + { + OIC_LOG(ERROR, TAG, "OCStack process error"); + } + #ifdef WITH_PROCESS_EVENT if (processEvent) { @@ -1205,10 +1213,5 @@ int main(int argc, char* argv[]) } #endif - if (OCStop() != OC_STACK_OK) - { - OIC_LOG(ERROR, TAG, "OCStack process error"); - } - return 0; } diff --git a/resource/csdk/stack/src/ocresource.c b/resource/csdk/stack/src/ocresource.c index bf82aa2..287244b 100644 --- a/resource/csdk/stack/src/ocresource.c +++ b/resource/csdk/stack/src/ocresource.c @@ -555,6 +555,7 @@ OCStackResult BuildVirtualResourceResponse(const OCResource *resourcePtr, return OC_STACK_OK; } +// TODO: headResource needs to be accessed in a thread-safe manner OCResource *FindResourceByUri(const char* resourceUri) { if(!resourceUri) @@ -565,7 +566,7 @@ OCResource *FindResourceByUri(const char* resourceUri) OCResource * pointer = headResource; while (pointer) { - if (strcmp(resourceUri, pointer->uri) == 0) + if (pointer->uri && strcmp(resourceUri, pointer->uri) == 0) { return pointer; } diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index c5b6fd3..4c799b1 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -4666,6 +4666,14 @@ OCStackResult initResources() } #endif + //check SVR DB Validity + OCStackResult validRes = CheckSVRDBValidity(); + + if( OC_STACK_INCONSISTENT_DB == validRes) + { + OIC_LOG_V(ERROR, TAG,"%s - SVR DB is not valid!",__func__); + } + if(result == OC_STACK_OK) { CreateResetProfile(); @@ -5475,7 +5483,7 @@ OCResourceHandle OCGetResourceHandleAtUri(const char *uri) while (pointer) { - if (strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0) + if (pointer->uri && strncmp(uri, pointer->uri, MAX_URI_LENGTH) == 0) { OIC_LOG_V(DEBUG, TAG, "Found Resource %s", uri); return pointer; diff --git a/resource/src/InProcClientWrapper.cpp b/resource/src/InProcClientWrapper.cpp index 433d840..1062ea3 100755 --- a/resource/src/InProcClientWrapper.cpp +++ b/resource/src/InProcClientWrapper.cpp @@ -64,6 +64,17 @@ namespace OC if (m_cfg.mode == ModeType::Client) { +#ifdef WITH_PROCESS_EVENT + if (false == m_threadRun) + { + m_processEvent = oc_event_new(); + if (!m_processEvent) + { + OIC_LOG(INFO, TAG, "oc_event_new failed!"); + return OC_STACK_ERROR; + } + } +#endif OCTransportFlags serverFlags = static_cast(m_cfg.serverConnectivity & CT_MASK_FLAGS); OCTransportFlags clientFlags = @@ -80,12 +91,6 @@ namespace OC { m_threadRun = true; #ifdef WITH_PROCESS_EVENT - m_processEvent = oc_event_new(); - if (!m_processEvent) - { - OIC_LOG(INFO, TAG, "oc_event_new failed!"); - return OC_STACK_ERROR; - } OCRegisterProcessEvent(m_processEvent); #endif m_listeningThread = std::thread(&InProcClientWrapper::listeningFunc, this); @@ -113,6 +118,12 @@ namespace OC #endif m_listeningThread.join(); } + OCStackResult result = OCStop(); + + if (OC_STACK_OK != result) + { + throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, result); + } #ifdef WITH_PROCESS_EVENT if (m_processEvent) @@ -121,12 +132,6 @@ namespace OC m_processEvent = NULL; } #endif - OCStackResult result = OCStop(); - - if (OC_STACK_OK != result) - { - throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, result); - } } return OC_STACK_OK; } diff --git a/resource/src/InProcServerWrapper.cpp b/resource/src/InProcServerWrapper.cpp index f36a1f3..cd56218 100755 --- a/resource/src/InProcServerWrapper.cpp +++ b/resource/src/InProcServerWrapper.cpp @@ -288,6 +288,18 @@ namespace OC OC_STACK_INVALID_PARAM); } +#ifdef WITH_PROCESS_EVENT + if (false == m_threadRun) + { + m_processEvent = oc_event_new(); + if (!m_processEvent) + { + OIC_LOG(INFO, TAG, "oc_event_new failed!"); + return OC_STACK_ERROR; + } + } +#endif + OCTransportFlags serverFlags = static_cast(m_cfg.serverConnectivity & CT_MASK_FLAGS); OCTransportFlags clientFlags = @@ -304,12 +316,6 @@ namespace OC { m_threadRun = true; #ifdef WITH_PROCESS_EVENT - m_processEvent = oc_event_new(); - if (!m_processEvent) - { - OIC_LOG(INFO, TAG, "oc_event_new failed!"); - return OC_STACK_ERROR; - } OCRegisterProcessEvent(m_processEvent); #endif m_processThread = std::thread(&InProcServerWrapper::processFunc, this); @@ -333,6 +339,13 @@ namespace OC m_processThread.join(); } + OCStackResult res = OCStop(); + + if (OC_STACK_OK != res) + { + throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, res); + } + #ifdef WITH_PROCESS_EVENT if (m_processEvent) { @@ -341,13 +354,6 @@ namespace OC } #endif - OCStackResult res = OCStop(); - - if (OC_STACK_OK != res) - { - throw InitializeException(OC::InitException::STACK_TERMINATE_ERROR, res); - } - return OC_STACK_OK; } diff --git a/service/easy-setup/enrollee/src/resourcehandler.c b/service/easy-setup/enrollee/src/resourcehandler.c index 779354e..f33187d 100755 --- a/service/easy-setup/enrollee/src/resourcehandler.c +++ b/service/easy-setup/enrollee/src/resourcehandler.c @@ -559,6 +559,29 @@ void updateDevConfResource(OCRepPayload* input) OICFree(devConfData); } +static OCRepPayload *constructPolicy(OCEntityHandlerRequest *ehRequest, OCResourceHandle handle) +{ + OCResourceProperty p = OCGetResourceProperties(handle); + OCRepPayload *policy = OCRepPayloadCreate(); + if (!policy) + { + OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload"); + return NULL; + } + + OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, + ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE))); + if (p & OC_SECURE) + { + OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE); + uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter, + ehRequest->devAddr.flags); + OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort); + } + + return policy; +} + OCRepPayload* constructResponseOfWiFiConf(char *interface) { OCRepPayload* payload = OCRepPayloadCreate(); @@ -865,24 +888,8 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest) OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE, (const char **)resourceInterface, dimensions); - OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle); - OCRepPayload *policy = OCRepPayloadCreate(); - if (!policy) - { - OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload"); - return NULL; - } - - OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, - ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE))); - if (p & OC_SECURE) - { - OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE); - uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter, - ehRequest->devAddr.flags); - OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort); - } - + OCRepPayload *policy = constructPolicy(ehRequest, + (OCResourceHandle *)g_ESWiFiConfResource.handle); OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy); arrayPayload[childResCnt++] = add; @@ -919,24 +926,8 @@ OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest) OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE, (const char **)resourceInterface, dimensions); - OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle); - OCRepPayload *policy = OCRepPayloadCreate(); - if (!policy) - { - OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload"); - return NULL; - } - - OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP, - ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE))); - if (p & OC_SECURE) - { - OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE); - uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter, - ehRequest->devAddr.flags); - OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort); - } - + OCRepPayload *policy = constructPolicy(ehRequest, + (OCResourceHandle *)g_ESDevConfResource.handle); OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy); arrayPayload[childResCnt++] = add; diff --git a/service/easy-setup/mediator/richsdk/android/build.gradle b/service/easy-setup/mediator/richsdk/android/build.gradle old mode 100755 new mode 100644 index 31e5eed..b9bf4c4 --- a/service/easy-setup/mediator/richsdk/android/build.gradle +++ b/service/easy-setup/mediator/richsdk/android/build.gradle @@ -21,6 +21,7 @@ buildscript { repositories { + mavenCentral() jcenter() } dependencies { @@ -33,9 +34,8 @@ buildscript { allprojects { repositories { - jcenter { - url "http://jcenter.bintray.com/" - } + mavenCentral() + jcenter { url "http://jcenter.bintray.com/" } flatDir { dirs "../../../../../../android/android_api/base/build/outputs/aar/" } diff --git a/service/easy-setup/mediator/richsdk/inc/ESSCCommon.h b/service/easy-setup/mediator/richsdk/inc/ESSCCommon.h index d52cd26..65c6d47 100644 --- a/service/easy-setup/mediator/richsdk/inc/ESSCCommon.h +++ b/service/easy-setup/mediator/richsdk/inc/ESSCCommon.h @@ -109,6 +109,68 @@ namespace OIC std::string bssid; } SCCandidateAPInfo; + static std::vector getCandidateAPListHelper(OCRepresentation rep) + { + if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATEAPS)) + { + std::vector candidateInfo = + rep.getValue>(SC_RSRVD_ES_VENDOR_CANDIDATEAPS); + std::vector postcandidateInfo; + for(std::vector::iterator it = candidateInfo.begin(); + it != candidateInfo.end(); ++it) + { + OC::OCRepresentation rep = *it; + SCCandidateAPInfo candidateInfotemp; + if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_SSID)) + { + candidateInfotemp.ssid=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_SSID); + } + if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_PASSPHRASE)) + { + candidateInfotemp.passphrase=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_PASSPHRASE); + } + if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_CHANNEL)) + { + candidateInfotemp.channel=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_CHANNEL); + } + if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_BSSID)) + { + candidateInfotemp.bssid=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_BSSID); + } + postcandidateInfo.push_back(candidateInfotemp); + } + return postcandidateInfo; + } + return {}; + } + + static std::string getEasySetupPropHelper(OCRepresentation easySetupRep, std::string propName) + { + std::vector children = easySetupRep.getChildren(); + for(auto child = children.begin(); child != children.end(); ++child) + { + if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) + { + OCRepresentation rep; + if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) + { + rep = child->getValue(OC_RSRVD_REPRESENTATION); + } + else + { + return std::string(""); + } + + if(rep.hasAttribute(propName)) + { + return rep.getValue(propName); + } + } + } + return std::string(""); + } + + /** * @brief Properties of provisioning resource. It includes a provisioning status and last * error code. @@ -433,38 +495,7 @@ namespace OIC */ std::vector getCandidateAPList() { - if (m_rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATEAPS)) - { - std::vector candidateInfo= - m_rep.getValue>(SC_RSRVD_ES_VENDOR_CANDIDATEAPS); - std::vector postcandidateInfo; - for(std::vector::iterator it = candidateInfo.begin(); - it != candidateInfo.end(); ++it) - { - OC::OCRepresentation rep = *it; - SCCandidateAPInfo candidateInfotemp; - if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_SSID)) - { - candidateInfotemp.ssid=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_SSID); - } - if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_PASSPHRASE)) - { - candidateInfotemp.passphrase=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_PASSPHRASE); - } - if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_CHANNEL)) - { - candidateInfotemp.channel=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_CHANNEL); - - } - if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_BSSID)) - { - candidateInfotemp.bssid=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_BSSID); - } - postcandidateInfo.push_back(candidateInfotemp); - } - return postcandidateInfo; - } - return {}; + return getCandidateAPListHelper(m_rep); } /** @@ -828,28 +859,7 @@ namespace OIC */ std::string getDeviceType() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_DEVICE_TYPE)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_DEVICE_TYPE); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_DEVICE_TYPE); } /** @@ -860,28 +870,7 @@ namespace OIC */ std::string getDeviceSubType() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_DEVICE_SUBTYPE)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_DEVICE_SUBTYPE); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_DEVICE_SUBTYPE); } /** @@ -894,28 +883,7 @@ namespace OIC */ std::string getRegisterSetDevice() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_REGISTER_SET_DEV)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_REGISTER_SET_DEV); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_REGISTER_SET_DEV); } /** @@ -926,45 +894,19 @@ namespace OIC std::vector getCandidateAPList() const { std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) + for (auto child = children.begin(); child != children.end(); ++child) { - if(child->getUri().find(OC_RSRVD_ES_URI_WIFICONF) != std::string::npos) + if (child->getUri().find(OC_RSRVD_ES_URI_WIFICONF) != std::string::npos) { OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) + if (child->hasAttribute(OC_RSRVD_REPRESENTATION)) { rep = child->getValue(OC_RSRVD_REPRESENTATION); } - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATEAPS)) + if (rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATEAPS)) { - std::vector candidateInfo=rep.getValue>(SC_RSRVD_ES_VENDOR_CANDIDATEAPS); - std::vector postcandidateInfo; - for(std::vector::iterator it = candidateInfo.begin(); - it != candidateInfo.end(); ++it) - { - OC::OCRepresentation rep = *it; - SCCandidateAPInfo candidateInfotemp; - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_SSID)) - { - candidateInfotemp.ssid=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_SSID); - } - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_PASSPHRASE)) - { - candidateInfotemp.passphrase=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_PASSPHRASE); - } - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_CHANNEL)) - { - candidateInfotemp.channel=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_CHANNEL); - - } - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_CANDIDATE_BSSID)) - { - candidateInfotemp.bssid=rep.getValue(SC_RSRVD_ES_VENDOR_CANDIDATE_BSSID); - } - postcandidateInfo.push_back(candidateInfotemp); - } - return postcandidateInfo; + return getCandidateAPListHelper(rep); } } } @@ -982,28 +924,7 @@ namespace OIC */ std::string getNetworkProvisioningInfo() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_NETWORK_PROV_INFO)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_NETWORK_PROV_INFO); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_NETWORK_PROV_INFO); } /** @@ -1016,28 +937,7 @@ namespace OIC */ std::string getSSOList() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_SSO_LIST)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_SSO_LIST); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_SSO_LIST); } /** @@ -1077,28 +977,7 @@ namespace OIC */ std::string getTCHeader() { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_TC_HEADER)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_TC_HEADER); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_TC_HEADER); } /** @@ -1108,28 +987,7 @@ namespace OIC */ std::string getTCVersion() { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_TC_VERSION)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_TC_VERSION); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_TC_VERSION); } /** @@ -1139,28 +997,7 @@ namespace OIC */ std::string getPnpPin() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_PNP_PIN)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_PNP_PIN); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_PNP_PIN); } /** @@ -1227,28 +1064,7 @@ namespace OIC */ std::string getUTCDatetime() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_UTC_DATE_TIME)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_UTC_DATE_TIME); - } - } - } - return {}; + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_UTC_DATE_TIME); } /** @@ -1258,28 +1074,7 @@ namespace OIC */ std::string getRegionalDatetime() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_REGIONAL_DATE_TIME)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_REGIONAL_DATE_TIME); - } - } - } - return {}; + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_REGIONAL_DATE_TIME); } /** @@ -1294,28 +1089,7 @@ namespace OIC */ std::string getESProtocolVersion() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_ES_PROTOCOL_VERSION)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_ES_PROTOCOL_VERSION); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_ES_PROTOCOL_VERSION); } /** @@ -1326,28 +1100,7 @@ namespace OIC */ std::string getCountry() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_COUNTRY)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_COUNTRY); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_COUNTRY); } /** @@ -1357,30 +1110,8 @@ namespace OIC */ std::string getHubId() const { - std::vector children = m_EasySetupRep.getChildren(); - for(auto child = children.begin(); child != children.end(); ++child) - { - if(child->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OCRepresentation rep; - if(child->hasAttribute(OC_RSRVD_REPRESENTATION)) - { - rep = child->getValue(OC_RSRVD_REPRESENTATION); - } - else - { - return std::string(""); - } - - if(rep.hasAttribute(SC_RSRVD_ES_VENDOR_HUB_ID)) - { - return rep.getValue(SC_RSRVD_ES_VENDOR_HUB_ID); - } - } - } - return std::string(""); + return getEasySetupPropHelper(m_EasySetupRep, SC_RSRVD_ES_VENDOR_HUB_ID); } - }; } } diff --git a/service/notification/android/build.gradle b/service/notification/android/build.gradle index cbfddfd..7e38f76 100644 --- a/service/notification/android/build.gradle +++ b/service/notification/android/build.gradle @@ -21,6 +21,7 @@ buildscript { repositories { + mavenCentral() jcenter() } dependencies { @@ -33,6 +34,7 @@ buildscript { allprojects { repositories { + mavenCentral() jcenter { url "http://jcenter.bintray.com/" } diff --git a/service/resource-container/android/build.gradle b/service/resource-container/android/build.gradle index cbfddfd..5cf7264 100644 --- a/service/resource-container/android/build.gradle +++ b/service/resource-container/android/build.gradle @@ -21,6 +21,7 @@ buildscript { repositories { + mavenCentral() jcenter() } dependencies { @@ -33,6 +34,7 @@ buildscript { allprojects { repositories { + mavenCentral() jcenter { url "http://jcenter.bintray.com/" } diff --git a/service/resource-encapsulation/android/build.gradle b/service/resource-encapsulation/android/build.gradle index cbfddfd..92321fe 100644 --- a/service/resource-encapsulation/android/build.gradle +++ b/service/resource-encapsulation/android/build.gradle @@ -21,6 +21,7 @@ buildscript { repositories { + mavenCentral() jcenter() } dependencies { @@ -33,6 +34,7 @@ buildscript { allprojects { repositories { + mavenCentral() jcenter { url "http://jcenter.bintray.com/" } diff --git a/service/resource-encapsulation/include/RCSRemoteResourceObject.h b/service/resource-encapsulation/include/RCSRemoteResourceObject.h index 05b189c..19d3e98 100644 --- a/service/resource-encapsulation/include/RCSRemoteResourceObject.h +++ b/service/resource-encapsulation/include/RCSRemoteResourceObject.h @@ -581,6 +581,7 @@ namespace OIC std::vector< std::string > getInterfaces() const; private: + std::weak_ptr< RCSRemoteResourceObject > weakFromThis(); std::shared_ptr< PrimitiveResource > m_primitiveResource; CacheID m_cacheId; BrokerID m_brokerId; diff --git a/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp b/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp index 26d83f9..80c25fe 100644 --- a/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp +++ b/service/resource-encapsulation/src/resourceClient/RCSRemoteResourceObject.cpp @@ -98,27 +98,26 @@ namespace { SCOPE_LOG_F(DEBUG, TAG); + std::shared_ptr resource = resourcePtr.lock(); + if(!resource) + { + OIC_LOG(ERROR, TAG, "Resource object is null"); + return OC_STACK_OK; + } + //If error code is failure then RE Cache module should //do clean up for caching flags, maps etc. if(eCode > 4) { - OIC_LOG_V(ERROR, TAG, "Error code: %d",eCode); + OIC_LOG_V(ERROR, TAG, "Error code: %d", eCode); try { - std::shared_ptr resource = resourcePtr.lock(); - if(resource) - { - resource->stopCaching(); - } - else - { - OIC_LOG(ERROR, TAG, "Resource object is null"); - } + resource->stopCaching(); } catch(...) { //Exception will be thrown: stack will return OC_STACK_ERROR - // if it already stopped observe. This call is reqired for clearing + //if it already stopped observe. This call is reqired for clearing //Cache manager. OIC_LOG(DEBUG, TAG, "Cleared Cache"); } @@ -217,6 +216,11 @@ namespace OIC } } + std::weak_ptr< RCSRemoteResourceObject > RCSRemoteResourceObject::weakFromThis() + { + return shared_from_this(); + } + RCSRemoteResourceObject::Ptr RCSRemoteResourceObject::fromOCResource( std::shared_ptr< OC::OCResource > ocResource) { @@ -340,17 +344,16 @@ namespace OIC m_primitiveResource, std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, - std::move(cb), shared_from_this()), CACHE_METHOD::OBSERVE_ONLY, + std::move(cb), weakFromThis()), CACHE_METHOD::OBSERVE_ONLY, freq, 0); } - else if (cb) { m_cacheId = ResourceCacheManager::getInstance()->requestResourceCache( m_primitiveResource, std::bind(cachingCallback, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, - std::move(cb), shared_from_this()), CACHE_METHOD::ITERATED_GET, + std::move(cb), weakFromThis()), CACHE_METHOD::ITERATED_GET, freq, 0); } else