X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=android%2Fandroid_api%2Fbase%2Fjni%2FJniOcPlatform.cpp;h=87d08a23e22a7171745554d0e14d69e9a7ca09f0;hb=c315c87e07c4080ecd0ef488e7a1047bc3c509b2;hp=231f1c9faeed9d0e7680f508ff70926a92f4006d;hpb=a942f3c9ab2adb06d953dc99fb375399b7b82d61;p=platform%2Fupstream%2Fiotivity.git diff --git a/android/android_api/base/jni/JniOcPlatform.cpp b/android/android_api/base/jni/JniOcPlatform.cpp index 231f1c9..87d08a2 100644 --- a/android/android_api/base/jni/JniOcPlatform.cpp +++ b/android/android_api/base/jni/JniOcPlatform.cpp @@ -24,15 +24,19 @@ #include "JniOcResourceHandle.h" #include "JniOcPresenceHandle.h" #include "JniOcResourceResponse.h" +#include "JniOcRepresentation.h" #include "JniOcSecurity.h" #include "JniOcDirectPairDevice.h" #include "JniUtils.h" #include "ocpayload.h" +#include "RDClient.h" #ifdef WITH_CLOUD #include "JniOcAccountManager.h" #endif +#define AES_KEY_SIZE 32 + using namespace OC; JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener) @@ -326,6 +330,74 @@ void RemoveOnPresenceListener(JNIEnv* env, jobject jListener) presenceMapLock.unlock(); } +JniOnObserveListener* AddOnObserveListener(JNIEnv* env, jobject jListener) +{ + JniOnObserveListener *onObserveListener = nullptr; + + observeMapLock.lock(); + + for (auto it = onObserveListenerMap.begin(); it != onObserveListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onObserveListener = refPair.first; + refPair.second++; + it->second = refPair; + onObserveListenerMap.insert(*it); + LOGD("OnObserveListener: ref. count incremented"); + break; + } + } + if (!onObserveListener) + { + onObserveListener = new JniOnObserveListener(env, jListener, (JniOcResource*)nullptr); + jobject jgListener = env->NewGlobalRef(jListener); + onObserveListenerMap.insert( + std::pair>( + jgListener, + std::pair(onObserveListener, 1))); + LOGI("OnObserveListener: new listener"); + } + observeMapLock.unlock(); + return onObserveListener; +} + +void RemoveOnObserveListener(JNIEnv* env, jobject jListener) +{ + observeMapLock.lock(); + bool isFound = false; + for (auto it = onObserveListenerMap.begin(); it != onObserveListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onObserveListenerMap.insert(*it); + LOGI("OnObserveListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnObserveListener* listener = refPair.first; + delete listener; + onObserveListenerMap.erase(it); + LOGI("OnObserveListener is removed"); + } + isFound = true; + break; + } + } + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "OnObserveListener not found"); + } + observeMapLock.unlock(); +} + JniOnDPDevicesFoundListener* AddOnDPDevicesFoundListener(JNIEnv* env, jobject jListener) { JniOnDPDevicesFoundListener *onDPDeviceListener = nullptr; @@ -468,142 +540,47 @@ void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener) directPairingListenerMapLock.unlock(); } -JniOnPublishResourceListener* AddOnPublishResourceListener(JNIEnv* env, jobject jListener) -{ - if (!env) - { - LOGD("env is null"); - return nullptr; - } - - JniOnPublishResourceListener *onPublishResourceListener = nullptr; - - publishResourceListenerMapLock.lock(); - - for (auto it = onPublishResourceListenerMap.begin(); it != - onPublishResourceListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - onPublishResourceListener = refPair.first; - refPair.second++; - it->second = refPair; - onPublishResourceListenerMap.insert(*it); - LOGD("onPublishResourceListener: ref. count incremented"); - break; - } - } - if (!onPublishResourceListener) - { - onPublishResourceListener = new JniOnPublishResourceListener(env, jListener, - RemoveOnPublishResourceListener); - jobject jgListener = env->NewGlobalRef(jListener); - onPublishResourceListenerMap.insert( - std::pair>( - jgListener, - std::pair(onPublishResourceListener, 1))); - LOGI("onPublishResourceListener: new listener"); - } - publishResourceListenerMapLock.unlock(); - return onPublishResourceListener; -} - -void RemoveOnPublishResourceListener(JNIEnv* env, jobject jListener) -{ - if (!env) - { - ThrowOcException(JNI_EXCEPTION, "env is null"); - return; - } - - publishResourceListenerMapLock.lock(); - bool isFound = false; - for (auto it = onPublishResourceListenerMap.begin(); it != - onPublishResourceListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - if (refPair.second > 1) - { - refPair.second--; - it->second = refPair; - onPublishResourceListenerMap.insert(*it); - LOGI("onPublishResourceListener: ref. count decremented"); - } - else - { - env->DeleteGlobalRef(it->first); - JniOnPublishResourceListener* listener = refPair.first; - delete listener; - onPublishResourceListenerMap.erase(it); - LOGI("onPublishResourceListener is removed"); - } - isFound = true; - break; - } - } - if (!isFound) - { - ThrowOcException(JNI_EXCEPTION, "onPublishResourceListener not found"); - } - publishResourceListenerMapLock.unlock(); -} - -JniOnDeleteResourceListener* AddOnDeleteResourceListener(JNIEnv* env, jobject jListener) +#ifdef TCP_ADAPTER +JniKeepAliveListener* AddKeepAliveListener(JNIEnv* env, jobject jListener) { - if (!env) - { - LOGD("env is null"); - return nullptr; - } + JniKeepAliveListener *KeepAliveListener = nullptr; - JniOnDeleteResourceListener *onDeleteResourceListener = nullptr; + KeepAliveListenerMapLock.lock(); - deleteResourceListenerMapLock.lock(); - - for (auto it = onDeleteResourceListenerMap.begin(); it != - onDeleteResourceListenerMap.end(); ++it) + for (auto it = KeepAliveListenerMap.begin(); it != + KeepAliveListenerMap.end(); ++it) { if (env->IsSameObject(jListener, it->first)) { auto refPair = it->second; - onDeleteResourceListener = refPair.first; + KeepAliveListener = refPair.first; refPair.second++; it->second = refPair; - onDeleteResourceListenerMap.insert(*it); - LOGD("onDeleteResourceListener: ref. count incremented"); + KeepAliveListenerMap.insert(*it); + LOGD("KeepAliveListener: ref. count incremented"); break; } } - if (!onDeleteResourceListener) + if (!KeepAliveListener) { - onDeleteResourceListener = new JniOnDeleteResourceListener(env, jListener, - RemoveOnDeleteResourceListener); - jobject jgListener = env->NewGlobalRef(jListener); - onDeleteResourceListenerMap.insert( - std::pair>( - jgListener, - std::pair(onDeleteResourceListener, 1))); - LOGI("onDeleteResourceListener: new listener"); + KeepAliveListener = new JniKeepAliveListener(env, jListener, RemoveKeepAliveListener); + jobject jgListener = env->NewGlobalRef(jListener); + KeepAliveListenerMap.insert(std::pair>( + jgListener, + std::pair(KeepAliveListener, 1))); + LOGI("KeepAliveListener: new listener"); } - deleteResourceListenerMapLock.unlock(); - return onDeleteResourceListener; + KeepAliveListenerMapLock.unlock(); + return KeepAliveListener; } - -void RemoveOnDeleteResourceListener(JNIEnv* env, jobject jListener) +#endif +#ifdef TCP_ADAPTER +void RemoveKeepAliveListener(JNIEnv* env, jobject jListener) { - if (!env) - { - ThrowOcException(JNI_EXCEPTION, "env is null"); - return; - } - - deleteResourceListenerMapLock.lock(); + KeepAliveListenerMapLock.lock(); bool isFound = false; - for (auto it = onDeleteResourceListenerMap.begin(); it != - onDeleteResourceListenerMap.end(); ++it) + for (auto it = KeepAliveListenerMap.begin(); it != + KeepAliveListenerMap.end(); ++it) { if (env->IsSameObject(jListener, it->first)) { @@ -612,16 +589,16 @@ void RemoveOnDeleteResourceListener(JNIEnv* env, jobject jListener) { refPair.second--; it->second = refPair; - onDeleteResourceListenerMap.insert(*it); - LOGI("onDeleteResourceListener: ref. count decremented"); + KeepAliveListenerMap.insert(*it); + LOGI("KeepAliveListener: ref.count decremented"); } else { env->DeleteGlobalRef(it->first); - JniOnDeleteResourceListener* listener = refPair.first; + JniKeepAliveListener* listener = refPair.first; delete listener; - onDeleteResourceListenerMap.erase(it); - LOGI("onDeleteResourceListener is removed"); + KeepAliveListenerMap.erase(it); + LOGI("KeepAliveListener is removed"); } isFound = true; break; @@ -629,24 +606,26 @@ void RemoveOnDeleteResourceListener(JNIEnv* env, jobject jListener) } if (!isFound) { - ThrowOcException(JNI_EXCEPTION, "onDeleteResourceListener not found"); + ThrowOcException(JNI_EXCEPTION, "KeepAliveListener not found"); } - deleteResourceListenerMapLock.unlock(); + KeepAliveListenerMapLock.unlock(); } - +#endif /* * Class: org_iotivity_base_OcPlatform * Method: configure -* Signature: (IILjava/lang/String;II)V +* Signature: (IILjava/lang/String;IILjava/lang/String;I)V */ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure (JNIEnv *env, jclass clazz, jint jServiceType, jint jModeType, jstring jIpAddress, jint jPort, - jint jQOS, jstring jDbPath) + jint jQOS, jstring jDbPath, jstring jDbPathDefault , jstring jRescueDBPath, jint jkeySize, +jbyteArray data, jint jTransport) { LOGI("OcPlatform_configure"); std::string ipAddress; - std::string dbfile; + std::string dbfile, defaultPath, rescuePath; + static unsigned char *aesKey = NULL; if (jIpAddress) { ipAddress = env->GetStringUTFChars(jIpAddress, nullptr); @@ -656,6 +635,39 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure dbfile = env->GetStringUTFChars(jDbPath, nullptr); JniOcSecurity::StoreDbPath(dbfile); } + //Either we have both paths or neither of them. + if ((jDbPathDefault && !jRescueDBPath) || (!jDbPathDefault && jRescueDBPath)) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "path cannot be null"); + return; + } + + if (!jDbPathDefault && !jRescueDBPath) + { + LOGI("Secure SVR DB feature not enable!!"); + } + else + { + aesKey = (unsigned char*)calloc(1, sizeof(unsigned char) * AES_KEY_SIZE); + defaultPath = env->GetStringUTFChars(jDbPathDefault, nullptr); + rescuePath = env->GetStringUTFChars(jRescueDBPath, nullptr); + OC::JniOcSecurity::StoreDefault_DbPath(defaultPath, rescuePath, (int)jkeySize); + + jbyte* key = env->GetByteArrayElements(data, 0); + jsize arrayLength = env->GetArrayLength(data); + if(arrayLength != AES_KEY_SIZE) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "key size mismatch"); + } + else + { + for(int i=0;i < AES_KEY_SIZE; i++) + { + aesKey[i]=(jbyte)key[i]; + } + } + + } uint16_t port = 0; if (jPort > 0) { @@ -667,10 +679,66 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure JniUtils::getModeType(env, jModeType), ipAddress, port, + JniUtils::getOCTransportAdapter(jTransport), JniUtils::getQOS(env, static_cast(jQOS)), - JniOcSecurity::getOCPersistentStorage() + aesKey, + JniOcSecurity::getOCPersistentStorage(), + JniOcSecurity::getOCPersistentStorageEnc(), + JniOcSecurity::getOCPersistentStorageRescue() }; + OCPlatform::Configure(cfg); + +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: stop +* Signature: ()V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stop +(JNIEnv *env, jclass clazz) +{ + LOGI("OcPlatform.stop"); + + try { + OCStackResult result = OCPlatform::stop(); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to stop ocplatform"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: start +* Signature: ()V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_start +(JNIEnv *env, jclass clazz) +{ + LOGI("OcPlatform.start"); + + try { + OCStackResult result = OCPlatform::start(); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to start ocplatform"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } } /* @@ -1373,12 +1441,12 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1( ThrowOcException(OC_STACK_INVALID_PARAM, "onPlatformFoundListener cannot be null"); return; } - JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener); + JniOnPlatformInfoListener *onPlatformInfoListener = AddOnPlatformInfoListener(env, jListener); - FindDeviceCallback findDeviceCallback = - [onDeviceInfoListener](const OCRepresentation& ocRepresentation) + FindPlatformCallback findPlatformCallback = + [onPlatformInfoListener](const OCRepresentation& ocRepresentation) { - onDeviceInfoListener->foundDeviceCallback(ocRepresentation); + onPlatformInfoListener->foundPlatformCallback(ocRepresentation); }; try @@ -1387,7 +1455,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1( host, resourceUri, static_cast(jConnectivityType), - findDeviceCallback, + findPlatformCallback, JniUtils::getQOS(env, static_cast(jQoS))); if (OC_STACK_OK != result) @@ -1559,10 +1627,10 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( } OCDeviceInfo deviceInfo; + memset(&deviceInfo, 0, sizeof(deviceInfo)); try { DuplicateString(&deviceInfo.deviceName, env->GetStringUTFChars(jDeviceName, nullptr)); - deviceInfo.types = NULL; jsize len = env->GetArrayLength(jDeviceTypes); for (jsize i = 0; i < len; ++i) @@ -1570,7 +1638,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( jstring jStr = (jstring)env->GetObjectArrayElement(jDeviceTypes, i); if (!jStr) { - delete deviceInfo.deviceName; + delete[] deviceInfo.deviceName; ThrowOcException(OC_STACK_INVALID_PARAM, "device type cannot be null"); return; } @@ -1578,7 +1646,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( OCResourcePayloadAddStringLL(&deviceInfo.types, env->GetStringUTFChars(jStr, nullptr)); if (env->ExceptionCheck()) { - delete deviceInfo.deviceName; + delete[] deviceInfo.deviceName; return; } @@ -1595,7 +1663,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( { OCStackResult result = OCPlatform::registerDeviceInfo(deviceInfo); - delete deviceInfo.deviceName; + delete[] deviceInfo.deviceName; if (OC_STACK_OK != result) { @@ -1716,17 +1784,17 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0( { OCStackResult result = OCPlatform::registerPlatformInfo(platformInfo); - delete platformInfo.platformID; - delete platformInfo.manufacturerName; - delete platformInfo.manufacturerUrl; - delete platformInfo.modelNumber; - delete platformInfo.dateOfManufacture; - delete platformInfo.platformVersion; - delete platformInfo.operatingSystemVersion; - delete platformInfo.hardwareVersion; - delete platformInfo.firmwareVersion; - delete platformInfo.supportUrl; - delete platformInfo.systemTime; + delete[] platformInfo.platformID; + delete[] platformInfo.manufacturerName; + delete[] platformInfo.manufacturerUrl; + delete[] platformInfo.modelNumber; + delete[] platformInfo.dateOfManufacture; + delete[] platformInfo.platformVersion; + delete[] platformInfo.operatingSystemVersion; + delete[] platformInfo.hardwareVersion; + delete[] platformInfo.firmwareVersion; + delete[] platformInfo.supportUrl; + delete[] platformInfo.systemTime; if (OC_STACK_OK != result) { @@ -1741,6 +1809,104 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0( } } +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue0 + (JNIEnv *env, jclass clazz, jint jType, jstring jPropName, jobjectArray jPropValue) +{ + try + { + OCPayloadType type = (OCPayloadType)jType; + std::string propName; + std::vector propValue; + if (jPropName) + { + propName = env->GetStringUTFChars(jPropName, nullptr); + } + if (jPropValue) + { + JniUtils::convertJavaStrArrToStrVector(env, jPropValue, propValue); + } + OCStackResult result = OCPlatform::setPropertyValue(type, propName, propValue); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to set property"); + return; + } + } + catch (OCException& e) + { + LOGE("Error is due to %s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } + +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: setPropertyValue0 +* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setPropertyValue1 + (JNIEnv *env, jclass clazz, jint jType, jstring jPropName, jstring jPropValue) +{ + try + { + OCPayloadType type = (OCPayloadType)jType; + std::string propName; + std::string propValue; + if (jPropName) + { + propName = env->GetStringUTFChars(jPropName, nullptr); + } + if (jPropValue) + { + propValue = env->GetStringUTFChars(jPropValue, nullptr); + } + OCStackResult result = OCPlatform::setPropertyValue(type, propName, propValue); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to set property"); + return; + } + } + catch (OCException& e) + { + LOGE("Error is due to %s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: getPropertyValue +* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPropertyValue0 + (JNIEnv *env, jclass clazz, jint jType, jstring jPropName, jstring jPropValue) +{ + try + { + OCPayloadType type = (OCPayloadType) jType; + std::string propName; + std::string propValue; + + if (jPropName) + { + propName = env->GetStringUTFChars(jPropName, nullptr); + } + OCStackResult result = OCPlatform::getPropertyValue(type, propName, propValue); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to get property value."); + return; + } + } + catch (OCException& e) + { + LOGE("Error is due to %s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + /* * Class: org_iotivity_base_OcPlatform * Method: unregisterResource0 @@ -2349,11 +2515,15 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0( ThrowOcException(result, "unsubscribe presence has failed"); return; } - jweak jwOnPresenceListener = - jniPresenceHandle->getJniOnPresenceListener()->getJWListener(); - if (jwOnPresenceListener) + + JniOnPresenceListener* jniPresenceListener = jniPresenceHandle->getJniOnPresenceListener(); + if (jniPresenceListener) { - RemoveOnPresenceListener(env, jwOnPresenceListener); + jweak jwOnPresenceListener = jniPresenceListener->getJWListener(); + if (jwOnPresenceListener) + { + RemoveOnPresenceListener(env, jwOnPresenceListener); + } } } catch (OCException& e) @@ -2365,34 +2535,118 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0( /* * Class: org_iotivity_base_OcPlatform -* Method: constructResourceObject0 -* Signature: (Ljava/lang/String;Ljava/lang/String;IZ[Ljava/lang/String;[Ljava/lang/String;) -Lorg/iotivity/base/OcResource; +* Method: subscribeDevicePresence0 +* Signature: (Ljava/lang/String;[Ljava/lang/String;I +Lorg/iotivity/base/OcResource/OnObserveListener;)Lorg/iotivity/base/OcPresenceHandle; */ -JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructResourceObject0( +JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribeDevicePresence0( JNIEnv *env, jclass clazz, jstring jHost, - jstring jUri, + jobjectArray jDiArray, jint jConnectivityType, - jboolean jIsObservable, - jobjectArray jResourceTypeArray, - jobjectArray jInterfaceArray) + jobject jListener) { - LOGD("OcPlatform_constructResourceObject"); + LOGD("OcPlatform_subscribeDevicePresence0"); +#ifdef WITH_CLOUD std::string host; if (jHost) { host = env->GetStringUTFChars(jHost, nullptr); } - std::string uri; - if (jUri) - { - uri = env->GetStringUTFChars(jUri, nullptr); - } - if (!jResourceTypeArray) + + if (!jDiArray) { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceTypeList cannot be null"); + ThrowOcException(OC_STACK_INVALID_PARAM, "device id List cannot be null"); + return nullptr; + } + + std::vector di; + JniUtils::convertJavaStrArrToStrVector(env, jDiArray, di); + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onObserveListener cannot be null"); + return nullptr; + } + + JniOnObserveListener *onObserveListener = AddOnObserveListener(env, jListener); + + ObserveCallback observeCallback = [onObserveListener](const HeaderOptions& opts, + const OCRepresentation& rep, const int& eCode, const int& sequenceNumber) + { + onObserveListener->onObserveCallback(opts, rep, eCode, sequenceNumber); + }; + + OCPlatform::OCPresenceHandle presenceHandle; + try + { + OCStackResult result = OCPlatform::subscribeDevicePresence( + presenceHandle, + host, + di, + static_cast(jConnectivityType), + observeCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "subscribe device presence has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + return nullptr; + } + + JniOcPresenceHandle* jniPresenceHandle = + new JniOcPresenceHandle(onObserveListener, presenceHandle); + jlong jhandle = reinterpret_cast(jniPresenceHandle); + jobject jPresenceHandle = + env->NewObject(g_cls_OcPresenceHandle, g_mid_OcPresenceHandle_N_ctor, jhandle); + if (!jPresenceHandle) + { + LOGE("Failed to create OcPresenceHandle"); + delete jniPresenceHandle; + } + return jPresenceHandle; +#else + ThrowOcException(JNI_NO_SUPPORT, "Not supported"); + return nullptr; +#endif +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: constructResourceObject0 +* Signature: (Ljava/lang/String;Ljava/lang/String;IZ[Ljava/lang/String;[Ljava/lang/String;) +Lorg/iotivity/base/OcResource; +*/ +JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructResourceObject0( + JNIEnv *env, + jclass clazz, + jstring jHost, + jstring jUri, + jint jConnectivityType, + jboolean jIsObservable, + jobjectArray jResourceTypeArray, + jobjectArray jInterfaceArray) +{ + LOGD("OcPlatform_constructResourceObject"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, nullptr); + } + std::string uri; + if (jUri) + { + uri = env->GetStringUTFChars(jUri, nullptr); + } + if (!jResourceTypeArray) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceTypeList cannot be null"); return nullptr; } if (!jInterfaceArray) @@ -2478,144 +2732,137 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0( } /* - * Class: org_iotivity_base_OcPlatform - * Method: publishResourceToRD0 - * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPublishResourceListener;I)V - */ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_publishResourceToRD0( - JNIEnv *env, - jclass clazz, - jstring jHost, - jint jConnectivityType, - jobject jListener, - jint jQoS) +* Class: org_iotivity_base_OcPlatform +* Method: constructAccountManagerObject0 +* Signature: (Ljava/lang/String;I)Lorg/iotivity/base/OcAccountManager; +*/ +JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructAccountManagerObject0( + JNIEnv *env, + jclass clazz, + jstring jHost, + jint jConnectivityType) { - LOGD("OcPlatform_publishResourceToRD"); -#ifdef RD_CLIENT - std::string host; - if (jHost) +#ifndef WITH_CLOUD + ThrowOcException(OC_STACK_ERROR, + "OCAccountManager is not supported. (Please build with WITH_CLOUD=1 option)"); + return nullptr; +#else + LOGD("OcPlatform_constructAccountManagerObject"); + if (!jHost) { - host = env->GetStringUTFChars(jHost, nullptr); + ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null"); + return nullptr; } - if (!jListener) + + const char* charHost = env->GetStringUTFChars(jHost, nullptr); + if (!charHost) { - ThrowOcException(OC_STACK_INVALID_PARAM, "onPublishResourceListener cannot be null"); - return; + ThrowOcException(JNI_EXCEPTION, "charHost is null"); + return nullptr; } - JniOnPublishResourceListener *onPubResListener = AddOnPublishResourceListener(env, jListener); + std::string host(charHost); + env->ReleaseStringUTFChars(jHost, charHost); - PublishResourceCallback pubResCallback = [onPubResListener]( - const OCRepresentation& ocRepresentation, - const int eCode) - { - onPubResListener->onPublishResourceCallback(ocRepresentation, eCode); - }; + std::shared_ptr accountManager = OCPlatform::constructAccountManagerObject( + host, + static_cast(jConnectivityType)); - try + if (!accountManager) { - OCStackResult result = OCPlatform::publishResourceToRD( - host, - static_cast(jConnectivityType), - pubResCallback, - JniUtils::getQOS(env, static_cast(jQoS))); + ThrowOcException(OC_STACK_ERROR, "Failed to create OCAccountManager"); + return nullptr; + } - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Publish resource has failed"); - return; - } + JniOcAccountManager *jniOcAccountManager = new JniOcAccountManager(accountManager); + + jobject jAccountManager = env->NewObject(g_cls_OcAccountManager, g_mid_OcAccountManager_ctor); + if (!jAccountManager) + { + delete jniOcAccountManager; + return nullptr; } - catch (OCException& e) + SetHandle(env, jAccountManager, jniOcAccountManager); + if (env->ExceptionCheck()) { - LOGE("%s", e.reason().c_str()); - ThrowOcException(e.code(), e.reason().c_str()); + delete jniOcAccountManager; + return nullptr; } -#else - ThrowOcException(OC_STACK_ERROR, "Publish resource has failed"); - return; + return jAccountManager; #endif } /* - * Class: org_iotivity_base_OcPlatform - * Method: publishResourceToRD1 - * Signature: (Ljava/lang/String;I[Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcPlatform/OnPublishResourceListener;I)V - */ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_publishResourceToRD1( - JNIEnv *env, - jclass clazz, - jstring jHost, - jint jConnectivityType, - jobjectArray jResourceHandleArray, - jint jQoS, - jobject jListener) +* Class: org_iotivity_base_OcPlatform +* Method: getDeviceId +* Signature: (I)V +*/ +JNIEXPORT jbyteArray JNICALL Java_org_iotivity_base_OcPlatform_getDeviceId +(JNIEnv *env, jobject thiz) { - LOGD("OcPlatform_publishResourceToRD"); -#ifdef RD_CLIENT - if (!env) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "env is null"); - return; - } - std::string host; - if (jHost) - { - host = env->GetStringUTFChars(jHost, nullptr); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "onPublishResourceListener cannot be null"); - return; - } - if (!jResourceHandleArray) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null"); - return; - } - JniOnPublishResourceListener *onPubResListener = AddOnPublishResourceListener(env, jListener); + LOGD("OcPlatform_getDeviceId"); + OCUUIdentity deviceId; - PublishResourceCallback pubResCallback = [onPubResListener]( - const OCRepresentation& ocRepresentation, - const int eCode) + jbyteArray ret = env->NewByteArray(UUID_IDENTITY_SIZE); + jbyte uuid[UUID_IDENTITY_SIZE]; + try { - onPubResListener->onPublishResourceCallback(ocRepresentation, eCode); - }; - std::vector resourceHandleList; - size_t len = env->GetArrayLength(jResourceHandleArray); - for (size_t i = 0; i < len; ++i) - { - jobject jResourceHandle = env->GetObjectArrayElement(jResourceHandleArray, i); - if (!jResourceHandle) + OCStackResult result = OCPlatform::getDeviceId(&deviceId); + LOGD("OcPlatform_getDeviceId return from CPP"); + if (OC_STACK_OK != result) { - ThrowOcException(JNI_EXCEPTION, "resource handle cannot be null"); - return; + ThrowOcException(result, "Error while getting my device Id"); } - - JniOcResourceHandle* jniOcResourceHandle = - JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle); - if (!jniOcResourceHandle) + else { - ThrowOcException(OC_STACK_INVALID_PARAM, "resource handle is invalid"); - return; + for(int i=0;i < UUID_IDENTITY_SIZE; i++) + { + uuid[i] =(jbyte) deviceId.id[i]; + } } - resourceHandleList.push_back(jniOcResourceHandle->getOCResourceHandle()); + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } + env->SetByteArrayRegion(ret, 0, UUID_IDENTITY_SIZE, uuid); + + return ret; +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: setDeviceId +* Signature: (Ljava/lang/byte;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setDeviceId( + JNIEnv *env, jobject thiz, jbyteArray data) +{ + LOGI("OcPlatform_setDeviceId"); + OCUUIdentity deviceId; try { - OCStackResult result = OCPlatform::publishResourceToRD( - host, - static_cast(jConnectivityType), - resourceHandleList, - pubResCallback, - JniUtils::getQOS(env, static_cast(jQoS))); - - if (OC_STACK_OK != result) + OCStackResult result; + jbyte* uuid = env->GetByteArrayElements(data, 0); + jsize arrayLength = env->GetArrayLength(data); + if(arrayLength!=UUID_IDENTITY_SIZE) { - ThrowOcException(result, "Publish resource has failed"); - return; + ThrowOcException(OC_STACK_INVALID_PARAM, "Byte length not equal to UUID_IDENTITY_SIZE"); + } + else + { + for(int i=0;i < UUID_IDENTITY_SIZE; i++) + { + deviceId.id[i]=(jchar)uuid[i]; + } + result = OCPlatform::setDeviceId(&deviceId); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to set DeviceId"); + } } } catch (OCException& e) @@ -2623,216 +2870,119 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_publishResourceToRD1( LOGE("%s", e.reason().c_str()); ThrowOcException(e.code(), e.reason().c_str()); } -#else - ThrowOcException(OC_STACK_ERROR, "Publish resource has failed"); - return; -#endif } /* * Class: org_iotivity_base_OcPlatform - * Method: deleteResourceFromRD0 - * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeleteResourceListener;I)V + * Method: findKeepAliveResourceImpl + * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcPlatform/KeepAliveListener;)V */ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_deleteResourceFromRD0( - JNIEnv *env, - jclass clazz, - jstring jHost, - jint jConnectivityType, - jobject jListener, - jint jQoS) +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findKeepAliveResourceImpl( + JNIEnv *env, jclass clazz, jstring jHost, jobject jListener) { - LOGD("OcPlatform_deleteResourceFromRD"); -#ifdef RD_CLIENT +#ifndef TCP_ADAPTER + ThrowOcException(OC_STACK_ERROR, + "findKeepAliveResource is not supported. (Please build with WITH_TCP=1 option)"); + return; +#else + LOGI("OcPlatform_findKeepAliveResource"); std::string host; - if (jHost) + if (!jHost) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null"); + return; + } + else { host = env->GetStringUTFChars(jHost, nullptr); } + if (!jListener) { - ThrowOcException(OC_STACK_INVALID_PARAM, "onDeleteResourceListener cannot be null"); + ThrowOcException(OC_STACK_INVALID_PARAM, "onKeepAliveFoundListener cannot be null"); return; } - JniOnDeleteResourceListener *onDelResListener = AddOnDeleteResourceListener(env, jListener); - DeleteResourceCallback delResCallback = [onDelResListener](const int eCode) + JniKeepAliveListener *onKeepAliveFoundListener = AddKeepAliveListener(env, jListener); + + KeepAliveCallback KeepAliveCallback = [onKeepAliveFoundListener](const int ret, + const OCRepresentation& rep) { - onDelResListener->onDeleteResourceCallback(eCode); + onKeepAliveFoundListener->onKeepAliveListener(ret, rep); }; try { - OCStackResult result = OCPlatform::deleteResourceFromRD( - host, - static_cast(jConnectivityType), - delResCallback, - JniUtils::getQOS(env, static_cast(jQoS))); + OCStackResult result = OCPlatform::findKeepAliveResource(host, KeepAliveCallback); if (OC_STACK_OK != result) { - ThrowOcException(result, "Delete resource has failed"); - return; + ThrowOcException(result, "findKeepAliveResource has failed"); } } catch (OCException& e) { LOGE("%s", e.reason().c_str()); ThrowOcException(e.code(), e.reason().c_str()); + return; } -#else - ThrowOcException(OC_STACK_ERROR, "Delete resource has failed"); - return; #endif } /* * Class: org_iotivity_base_OcPlatform - * Method: deleteResourceFromRD1 - * Signature: (Ljava/lang/String;I[Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcPlatform/OnDeleteResourceListener;I)V + * Method: sendKeepAliveRequestImpl + * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/KeepAliveListener;)V */ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_deleteResourceFromRD1( - JNIEnv *env, - jclass clazz, - jstring jHost, - jint jConnectivityType, - jobjectArray jResourceHandleArray, - jint jQoS, - jobject jListener) +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendKeepAliveRequestImpl( + JNIEnv *env, jclass clazz, jstring jHost, jobject jRep, jobject jListener) { - LOGD("OcPlatform_deleteResourceFromRD"); -#ifdef RD_CLIENT - if (!env) +#ifndef TCP_ADAPTER + ThrowOcException(OC_STACK_ERROR, + "sendKeepAlive is not supported. (Please build with WITH_TCP=1 option)"); + return; +#else + LOGI("OcPlatform_sendKeepAliveRequest"); + std::string host; + if (!jHost) { - ThrowOcException(OC_STACK_INVALID_PARAM, "env is null"); + ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null"); return; } - std::string host; - if (jHost) + else { host = env->GetStringUTFChars(jHost, nullptr); } + if (!jListener) { - ThrowOcException(OC_STACK_INVALID_PARAM, "onDeleteResourceListener cannot be null"); - return; - } - if (!jResourceHandleArray) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null"); + ThrowOcException(OC_STACK_INVALID_PARAM, "keepAliveResponseListener cannot be null"); return; - } - JniOnDeleteResourceListener *onDelResListener = AddOnDeleteResourceListener(env, jListener); - - DeleteResourceCallback delResCallback = [onDelResListener](const int eCode) - { - onDelResListener->onDeleteResourceCallback(eCode); }; - std::vector resourceHandleList; - size_t len = env->GetArrayLength(jResourceHandleArray); - for (size_t i = 0; i < len; ++i) - { - jobject jResourceHandle = env->GetObjectArrayElement(jResourceHandleArray, i); - if (!jResourceHandle) - { - ThrowOcException(JNI_EXCEPTION, "resource handle cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceHandle = - JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle); - if (!jniOcResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resource handle is invalid"); - return; - } + JniKeepAliveListener *KeepAliveResponseListener = AddKeepAliveListener(env, jListener); - resourceHandleList.push_back(jniOcResourceHandle->getOCResourceHandle()); - } + KeepAliveCallback KeepAliveCallback = [KeepAliveResponseListener](const int ret, + const OCRepresentation& rep) + { + KeepAliveResponseListener->onKeepAliveListener(ret, rep); + }; try { - OCStackResult result = OCPlatform::deleteResourceFromRD( - host, - static_cast(jConnectivityType), - resourceHandleList, - delResCallback, - JniUtils::getQOS(env, static_cast(jQoS))); + OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep); + OCStackResult result = OCPlatform::sendKeepAliveRequest(host, *rep, KeepAliveCallback); if (OC_STACK_OK != result) { - ThrowOcException(result, "Delete resource has failed"); - return; + ThrowOcException(result, "sendKeepAliveRequest has failed"); } } catch (OCException& e) { LOGE("%s", e.reason().c_str()); ThrowOcException(e.code(), e.reason().c_str()); + return; } -#else - ThrowOcException(OC_STACK_ERROR, "Delete resource has failed"); - return; -#endif -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: constructAccountManagerObject0 -* Signature: (Ljava/lang/String;I)Lorg/iotivity/base/OcAccountManager; -*/ -JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructAccountManagerObject0( - JNIEnv *env, - jclass clazz, - jstring jHost, - jint jConnectivityType) -{ -#ifndef WITH_CLOUD - ThrowOcException(OC_STACK_ERROR, - "OCAccountManager is not supported. (Please build with WITH_CLOUD=1 option)"); - return nullptr; -#else - LOGD("OcPlatform_constructAccountManagerObject"); - if (!jHost) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null"); - return nullptr; - } - - const char* charHost = env->GetStringUTFChars(jHost, nullptr); - if (!charHost) - { - ThrowOcException(JNI_EXCEPTION, "charHost is null"); - return nullptr; - } - std::string host(charHost); - env->ReleaseStringUTFChars(jHost, charHost); - - std::shared_ptr accountManager = OCPlatform::constructAccountManagerObject( - host, - static_cast(jConnectivityType)); - - if (!accountManager) - { - ThrowOcException(OC_STACK_ERROR, "Failed to create OCAccountManager"); - return nullptr; - } - - JniOcAccountManager *jniOcAccountManager = new JniOcAccountManager(accountManager); - - jobject jAccountManager = env->NewObject(g_cls_OcAccountManager, g_mid_OcAccountManager_ctor); - if (!jAccountManager) - { - delete jniOcAccountManager; - return nullptr; - } - SetHandle(env, jAccountManager, jniOcAccountManager); - if (env->ExceptionCheck()) - { - delete jniOcAccountManager; - return nullptr; - } - return jAccountManager; #endif }