X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=android%2Fandroid_api%2Fbase%2Fjni%2FJniOcPlatform.cpp;h=87d08a23e22a7171745554d0e14d69e9a7ca09f0;hb=c315c87e07c4080ecd0ef488e7a1047bc3c509b2;hp=8314e9e168e94a396172d34941381b12903c5094;hpb=f156a5121851f45a6f803be854197d5d3c6184e2;p=platform%2Fupstream%2Fiotivity.git diff --git a/android/android_api/base/jni/JniOcPlatform.cpp b/android/android_api/base/jni/JniOcPlatform.cpp index 8314e9e..87d08a2 100644 --- a/android/android_api/base/jni/JniOcPlatform.cpp +++ b/android/android_api/base/jni/JniOcPlatform.cpp @@ -24,14 +24,24 @@ #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) { - JniOnResourceFoundListener *onResourceFoundListener = NULL; + JniOnResourceFoundListener *onResourceFoundListener = nullptr; resourceFoundMapLock.lock(); @@ -103,7 +113,7 @@ void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener) JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener) { - JniOnDeviceInfoListener *onDeviceInfoListener = NULL; + JniOnDeviceInfoListener *onDeviceInfoListener = nullptr; deviceInfoMapLock.lock(); @@ -179,7 +189,7 @@ void RemoveOnDeviceInfoListener(JNIEnv* env, jobject jListener) JniOnPlatformInfoListener* AddOnPlatformInfoListener(JNIEnv* env, jobject jListener) { - JniOnPlatformInfoListener *onPlatformInfoListener = NULL; + JniOnPlatformInfoListener *onPlatformInfoListener = nullptr; platformInfoMapLock.lock(); @@ -254,7 +264,7 @@ void RemoveOnPlatformInfoListener(JNIEnv* env, jobject jListener) JniOnPresenceListener* AddOnPresenceListener(JNIEnv* env, jobject jListener) { - JniOnPresenceListener *onPresenceListener = NULL; + JniOnPresenceListener *onPresenceListener = nullptr; presenceMapLock.lock(); @@ -320,43 +330,415 @@ 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; + + dpDevicesFoundListenerMapLock.lock(); + + for (auto it = onDPDevicesFoundListenerMap.begin(); it != + onDPDevicesFoundListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onDPDeviceListener = refPair.first; + refPair.second++; + it->second = refPair; + onDPDevicesFoundListenerMap.insert(*it); + LOGD("onDPDeviceListener: ref. count incremented"); + break; + } + } + if (!onDPDeviceListener) + { + onDPDeviceListener = new JniOnDPDevicesFoundListener(env, jListener, + RemoveOnDPDevicesFoundListener); + jobject jgListener = env->NewGlobalRef(jListener); + onDPDevicesFoundListenerMap.insert( + std::pair>( + jgListener, + std::pair(onDPDeviceListener, 1))); + LOGI("onDPDeviceListener: new listener"); + } + dpDevicesFoundListenerMapLock.unlock(); + return onDPDeviceListener; +} + +void RemoveOnDPDevicesFoundListener(JNIEnv* env, jobject jListener) +{ + dpDevicesFoundListenerMapLock.lock(); + bool isFound = false; + for (auto it = onDPDevicesFoundListenerMap.begin(); it != + onDPDevicesFoundListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onDPDevicesFoundListenerMap.insert(*it); + LOGI("onDPDeviceListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnDPDevicesFoundListener* listener = refPair.first; + delete listener; + onDPDevicesFoundListenerMap.erase(it); + LOGI("onDPDeviceListener is removed"); + } + isFound = true; + break; + } + } + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "onDPDeviceListener not found"); + } + dpDevicesFoundListenerMapLock.unlock(); +} + +JniOnDirectPairingListener* AddOnDirectPairingListener(JNIEnv* env, jobject jListener) +{ + JniOnDirectPairingListener *onDirectPairingListener = nullptr; + + directPairingListenerMapLock.lock(); + + for (auto it = directPairingListenerMap.begin(); it != + directPairingListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onDirectPairingListener = refPair.first; + refPair.second++; + it->second = refPair; + directPairingListenerMap.insert(*it); + LOGD("onDirectPairingListener: ref. count incremented"); + break; + } + } + if (!onDirectPairingListener) + { + onDirectPairingListener = new JniOnDirectPairingListener(env, jListener, + RemoveOnDirectPairingListener); + jobject jgListener = env->NewGlobalRef(jListener); + directPairingListenerMap.insert( + std::pair>( + jgListener, + std::pair(onDirectPairingListener, 1))); + LOGI("onDirectPairingListener: new listener"); + } + directPairingListenerMapLock.unlock(); + return onDirectPairingListener; +} + +void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener) +{ + directPairingListenerMapLock.lock(); + bool isFound = false; + for (auto it = directPairingListenerMap.begin(); it != + directPairingListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + directPairingListenerMap.insert(*it); + LOGI("onDirectPairingListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnDirectPairingListener* listener = refPair.first; + delete listener; + directPairingListenerMap.erase(it); + LOGI("onDirectPairingListener is removed"); + } + isFound = true; + break; + } + } + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "onDirectPairingListener not found"); + } + directPairingListenerMapLock.unlock(); +} + +#ifdef TCP_ADAPTER +JniKeepAliveListener* AddKeepAliveListener(JNIEnv* env, jobject jListener) +{ + JniKeepAliveListener *KeepAliveListener = nullptr; + + KeepAliveListenerMapLock.lock(); + + for (auto it = KeepAliveListenerMap.begin(); it != + KeepAliveListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + KeepAliveListener = refPair.first; + refPair.second++; + it->second = refPair; + KeepAliveListenerMap.insert(*it); + LOGD("KeepAliveListener: ref. count incremented"); + break; + } + } + if (!KeepAliveListener) + { + KeepAliveListener = new JniKeepAliveListener(env, jListener, RemoveKeepAliveListener); + jobject jgListener = env->NewGlobalRef(jListener); + KeepAliveListenerMap.insert(std::pair>( + jgListener, + std::pair(KeepAliveListener, 1))); + LOGI("KeepAliveListener: new listener"); + } + KeepAliveListenerMapLock.unlock(); + return KeepAliveListener; +} +#endif +#ifdef TCP_ADAPTER +void RemoveKeepAliveListener(JNIEnv* env, jobject jListener) +{ + KeepAliveListenerMapLock.lock(); + bool isFound = false; + for (auto it = KeepAliveListenerMap.begin(); it != + KeepAliveListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + KeepAliveListenerMap.insert(*it); + LOGI("KeepAliveListener: ref.count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniKeepAliveListener* listener = refPair.first; + delete listener; + KeepAliveListenerMap.erase(it); + LOGI("KeepAliveListener is removed"); + } + isFound = true; + break; + } + } + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "KeepAliveListener not found"); + } + 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, NULL); + ipAddress = env->GetStringUTFChars(jIpAddress, nullptr); } if (jDbPath) { 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) { port = static_cast(jPort); } + PlatformConfig cfg{ JniUtils::getServiceType(env, jServiceType), 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()); + } } /* @@ -376,7 +758,10 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0 JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( env, jResourceHandle); - if (!jniOcResourceHandle) return; + if (!jniOcResourceHandle) + { + return; + } try { @@ -392,7 +777,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0 catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -419,7 +804,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1 return; } - try{ + try { OCStackResult result = OCPlatform::notifyAllObservers( jniOcResourceHandle->getOCResourceHandle(), JniUtils::getQOS(env, static_cast(jQoS))); @@ -433,7 +818,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1 catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -506,7 +891,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers2( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -581,7 +966,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers3( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -602,12 +987,12 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string resourceUri; if (jResourceUri) { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); } if (!jListener) { @@ -622,13 +1007,19 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0( onResFoundListener->foundResourceCallback(resource); }; + FindErrorCallback findErrorCallback = [onResFoundListener](const std::string& uri, const int eCode) + { + onResFoundListener->findResourceErrorCallback(uri, eCode); + }; + try { OCStackResult result = OCPlatform::findResource( host, resourceUri, static_cast(jConnectivityType), - findCallback); + findCallback, + findErrorCallback); if (OC_STACK_OK != result) { @@ -639,7 +1030,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -661,12 +1052,12 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string resourceUri; if (jResourceUri) { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); } if (!jListener) { @@ -680,6 +1071,11 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1( onResFoundListener->foundResourceCallback(resource); }; + FindErrorCallback findErrorCallback = [onResFoundListener](const std::string& uri, const int eCode) + { + onResFoundListener->findResourceErrorCallback(uri, eCode); + }; + try { OCStackResult result = OCPlatform::findResource( @@ -687,6 +1083,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1( resourceUri, static_cast(jConnectivityType), findCallback, + findErrorCallback, JniUtils::getQOS(env, static_cast(jQoS))); if (OC_STACK_OK != result) @@ -698,7 +1095,146 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* + * Class: org_iotivity_base_OcPlatform + * Method: findDirectPairingDevices + * Signature: (ILorg/iotivity/base/OcPlatform/FindDirectPairingListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findDirectPairingDevices + (JNIEnv * env, jclass clazz, jint jTimeout, jobject jListener) +{ + LOGD("OcPlatform_findDirectPairingDevices"); + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onDPDevicesFoundListener cannot be null"); + return; + } + JniOnDPDevicesFoundListener *onDPDevsFoundListener = AddOnDPDevicesFoundListener(env, + jListener); + + GetDirectPairedCallback getDirectPairedCallback = + [onDPDevsFoundListener](PairedDevices pairingDevList) + { + onDPDevsFoundListener->directPairingDevicesCallback(pairingDevList, + DPFunc::FIND_DIRECT_PAIRED_DEV_LIST); + }; + + try + { + OCStackResult result = OCPlatform::findDirectPairingDevices(jTimeout, + getDirectPairedCallback); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "OCPlatform::findDirectPairingDevices has failed"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* + * Class: org_iotivity_base_OcPlatform + * Method: getDirectPairedDevices + * Signature: (Lorg/iotivity/base/OcDirectPairDevice/GetDirectPairedListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDirectPairedDevices +(JNIEnv *env, jclass jclazz, jobject jListener) +{ + LOGD("OcPlatform_getDirectPairedDevices"); + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "getPairedDevList Callback cannot be null"); + return; + } + JniOnDPDevicesFoundListener *onGetPairedDevicesListener = AddOnDPDevicesFoundListener(env, + jListener); + + GetDirectPairedCallback getDirectPairedCallback = + [onGetPairedDevicesListener](PairedDevices pairedDevList) + { + onGetPairedDevicesListener->directPairingDevicesCallback(pairedDevList, + DPFunc::GET_PAIRED_DEV_LIST); + }; + + try + { + OCStackResult result = OCPlatform::getDirectPairedDevices(getDirectPairedCallback); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "OcDirectPairDevice_getDirectPairedDevices"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* + * Class: org_iotivity_base_OcPlatform + * Method: doDirectPairing + * Signature: (Lorg/iotivity/base/OcDirectPairDevice;Lorg/iotivity/base/OcPrmType; + * Ljava/lang/String;Lorg/iotivity/base/OcDirectPairDevice/DirectPairingListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_doDirectPairing0 +(JNIEnv *env, jclass clazz, jobject jpeer, jint jprmType, jstring jpin, jobject jListener) +{ + LOGD("OcPlatform_doDirectPairing"); + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "doDirectPairing Callback cannot be null"); + return; + } + if (!jpeer) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "Peer cannot be null"); + return; + } + + JniOnDirectPairingListener *onDirectPairingListener = AddOnDirectPairingListener(env, + jListener); + + DirectPairingCallback DirectPairingCB = + [onDirectPairingListener](std::shared_ptr dpDev, OCStackResult result) + { + onDirectPairingListener->doDirectPairingCB(dpDev, result); + }; + + JniOcDirectPairDevice *dev = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env, jpeer); + + if (!dev) + { + return ; + } + std::string pin = env->GetStringUTFChars(jpin, 0); + + try + { + OCStackResult result = OCPlatform::doDirectPairing(dev->getPtr(), (OCPrm_t)jprmType, + pin, DirectPairingCB); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "OcPlatform_oDirectPairing"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -719,12 +1255,12 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string resourceUri; if (jResourceUri) { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); } if (!jListener) { @@ -755,7 +1291,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -777,12 +1313,12 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string resourceUri; if (jResourceUri) { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); } if (!jListener) { @@ -814,7 +1350,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -835,12 +1371,12 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo0( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string resourceUri; if (jResourceUri) { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); } if (!jListener) { @@ -871,7 +1407,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -893,24 +1429,24 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string resourceUri; if (jResourceUri) { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); } if (!jListener) { 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 @@ -919,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) @@ -930,7 +1466,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -949,7 +1485,10 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0( return nullptr; } JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, jResource); - if (!resource) return nullptr; + if (!resource) + { + return nullptr; + } OCResourceHandle resourceHandle; try @@ -966,7 +1505,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); return nullptr; } JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle); @@ -999,29 +1538,29 @@ jobject jListener, jint jResourceProperty) std::string resourceUri; if (jResourceUri) { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + resourceUri = env->GetStringUTFChars(jResourceUri, nullptr); } std::string resourceTypeName; if (jResourceTypeName) { - resourceTypeName = env->GetStringUTFChars(jResourceTypeName, NULL); + resourceTypeName = env->GetStringUTFChars(jResourceTypeName, nullptr); } std::string resourceInterface; if (jResourceInterface) { - resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL); + resourceInterface = env->GetStringUTFChars(jResourceInterface, nullptr); } - if (!jListener) + JniEntityHandler* entityHandler = NULL; + EntityHandler handleEntityCallback = NULL; + if (jListener) { - ThrowOcException(OC_STACK_INVALID_PARAM, "entityHandler cannot be null"); - return nullptr; + entityHandler = new JniEntityHandler(env, jListener); + handleEntityCallback = + [entityHandler](const std::shared_ptr request) -> OCEntityHandlerResult + { + return entityHandler->handleEntity(request); + }; } - JniEntityHandler* entityHandler = new JniEntityHandler(env, jListener); - EntityHandler handleEntityCallback = - [entityHandler](const std::shared_ptr request) -> OCEntityHandlerResult - { - return entityHandler->handleEntity(request); - }; OCResourceHandle resourceHandle; try @@ -1045,7 +1584,7 @@ jobject jListener, jint jResourceProperty) { LOGE("%s", e.reason().c_str()); delete entityHandler; - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); return nullptr; } @@ -1070,20 +1609,49 @@ jobject jListener, jint jResourceProperty) JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( JNIEnv *env, jclass clazz, - jstring jDeviceName) + jstring jDeviceName, + jobjectArray jDeviceTypes) { LOGI("OcPlatform_registerDeviceInfo"); - std::string deviceName; - if (jDeviceName) + if (!jDeviceName) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "deviceName cannot be null"); + return; + } + + if (!jDeviceTypes) { - deviceName = env->GetStringUTFChars(jDeviceName, NULL); + ThrowOcException(OC_STACK_INVALID_PARAM, "deviceTypes cannot be null"); + return; } OCDeviceInfo deviceInfo; + memset(&deviceInfo, 0, sizeof(deviceInfo)); try { - DuplicateString(&deviceInfo.deviceName, deviceName); + DuplicateString(&deviceInfo.deviceName, env->GetStringUTFChars(jDeviceName, nullptr)); + + jsize len = env->GetArrayLength(jDeviceTypes); + for (jsize i = 0; i < len; ++i) + { + jstring jStr = (jstring)env->GetObjectArrayElement(jDeviceTypes, i); + if (!jStr) + { + delete[] deviceInfo.deviceName; + ThrowOcException(OC_STACK_INVALID_PARAM, "device type cannot be null"); + return; + } + + OCResourcePayloadAddStringLL(&deviceInfo.types, env->GetStringUTFChars(jStr, nullptr)); + if (env->ExceptionCheck()) + { + delete[] deviceInfo.deviceName; + return; + } + + env->DeleteLocalRef(jStr); + } } catch (std::exception &e) { @@ -1095,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) { @@ -1106,7 +1674,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1147,47 +1715,47 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0( if (jPlatformID) { - platformID = env->GetStringUTFChars(jPlatformID, NULL); + platformID = env->GetStringUTFChars(jPlatformID, nullptr); } if (jManufacturerName) { - manufacturerName = env->GetStringUTFChars(jManufacturerName, NULL); + manufacturerName = env->GetStringUTFChars(jManufacturerName, nullptr); } if (jManufacturerUrl) { - manufacturerUrl = env->GetStringUTFChars(jManufacturerUrl, NULL); + manufacturerUrl = env->GetStringUTFChars(jManufacturerUrl, nullptr); } if (jModelNumber) { - modelNumber = env->GetStringUTFChars(jModelNumber, NULL); + modelNumber = env->GetStringUTFChars(jModelNumber, nullptr); } if (jDateOfManufacture) { - dateOfManufacture = env->GetStringUTFChars(jDateOfManufacture, NULL); + dateOfManufacture = env->GetStringUTFChars(jDateOfManufacture, nullptr); } if (jPlatformVersion) { - platformVersion = env->GetStringUTFChars(jPlatformVersion, NULL); + platformVersion = env->GetStringUTFChars(jPlatformVersion, nullptr); } if (jOperatingSystemVersion) { - operatingSystemVersion = env->GetStringUTFChars(jOperatingSystemVersion, NULL); + operatingSystemVersion = env->GetStringUTFChars(jOperatingSystemVersion, nullptr); } if (jHardwareVersion) { - hardwareVersion = env->GetStringUTFChars(jHardwareVersion, NULL); + hardwareVersion = env->GetStringUTFChars(jHardwareVersion, nullptr); } if (jFirmwareVersion) { - firmwareVersion = env->GetStringUTFChars(jFirmwareVersion, NULL); + firmwareVersion = env->GetStringUTFChars(jFirmwareVersion, nullptr); } if (jSupportUrl) { - supportUrl = env->GetStringUTFChars(jSupportUrl, NULL); + supportUrl = env->GetStringUTFChars(jSupportUrl, nullptr); } if (jSystemTime) { - systemTime = env->GetStringUTFChars(jSystemTime, NULL); + systemTime = env->GetStringUTFChars(jSystemTime, nullptr); } OCPlatformInfo platformInfo; @@ -1205,40 +1773,138 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0( DuplicateString(&platformInfo.supportUrl, supportUrl); DuplicateString(&platformInfo.systemTime, systemTime); } - catch (std::exception &e) + catch (std::exception &e) + { + ThrowOcException(JNI_EXCEPTION, "Failed to construct platform info"); + return; + } + + // __android_log_print(ANDROID_LOG_INFO, "Rahul", "platformID = %s", platformID); + try + { + 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; + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to register platform info"); + return; + } + } + catch (OCException& e) + { + LOGE("Error is due to %s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +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) { - ThrowOcException(JNI_EXCEPTION, "Failed to construct platform info"); - return; + propValue = env->GetStringUTFChars(jPropValue, nullptr); } - - // __android_log_print(ANDROID_LOG_INFO, "Rahul", "platformID = %s", platformID); - try + OCStackResult result = OCPlatform::setPropertyValue(type, propName, propValue); + if (OC_STACK_OK != result) { - OCStackResult result = OCPlatform::registerPlatformInfo(platformInfo); + 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()); + } +} - 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; +/* +* 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 (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to register platform info"); - return; - } + if (jPropName) + { + propName = env->GetStringUTFChars(jPropName, nullptr); } - catch (OCException& e) + OCStackResult result = OCPlatform::getPropertyValue(type, propName, propValue); + if (OC_STACK_OK != result) { - LOGE("Error is due to %s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + 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()); + } } /* @@ -1274,7 +1940,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unregisterResource0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1299,7 +1965,10 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0 } JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceCollectionHandle); - if (!jniOcResourceCollectionHandle) return; + if (!jniOcResourceCollectionHandle) + { + return; + } JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( env, jResourceHandle); @@ -1323,7 +1992,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0 catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1395,7 +2064,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResources0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1450,7 +2119,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResource0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1521,7 +2190,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResources0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1545,12 +2214,15 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0( std::string typeName; if (jResourceTypeName) { - typeName = env->GetStringUTFChars(jResourceTypeName, NULL); + typeName = env->GetStringUTFChars(jResourceTypeName, nullptr); } JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr(env, jResourceHandle); - if (!jniOcResourceHandle) return; + if (!jniOcResourceHandle) + { + return; + } try { @@ -1567,7 +2239,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1588,7 +2260,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource std::string interfaceName; if (jResourceInterfaceName) { - interfaceName = env->GetStringUTFChars(jResourceInterfaceName, NULL); + interfaceName = env->GetStringUTFChars(jResourceInterfaceName, nullptr); } JniOcResourceHandle* jniOcResourceHandle = @@ -1613,7 +2285,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1639,7 +2311,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_startPresence0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1665,7 +2337,7 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stopPresence0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); } } @@ -1685,7 +2357,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } if (!jListener) { @@ -1720,7 +2392,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); return nullptr; } @@ -1755,12 +2427,12 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1( std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string resourceType; if (jResourceType) { - resourceType = env->GetStringUTFChars(jResourceType, NULL); + resourceType = env->GetStringUTFChars(jResourceType, nullptr); } if (!jListener) { @@ -1794,7 +2466,7 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); return nullptr; } @@ -1843,18 +2515,106 @@ 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) + { + jweak jwOnPresenceListener = jniPresenceListener->getJWListener(); + if (jwOnPresenceListener) + { + RemoveOnPresenceListener(env, jwOnPresenceListener); + } + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* 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_subscribeDevicePresence0( + JNIEnv *env, + jclass clazz, + jstring jHost, + jobjectArray jDiArray, + jint jConnectivityType, + jobject jListener) +{ + LOGD("OcPlatform_subscribeDevicePresence0"); +#ifdef WITH_CLOUD + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, nullptr); + } + + if (!jDiArray) + { + 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) { - RemoveOnPresenceListener(env, jwOnPresenceListener); + ThrowOcException(result, "subscribe device presence has failed"); } } catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, 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 } /* @@ -1877,12 +2637,12 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructResourceObj std::string host; if (jHost) { - host = env->GetStringUTFChars(jHost, NULL); + host = env->GetStringUTFChars(jHost, nullptr); } std::string uri; if (jUri) { - uri = env->GetStringUTFChars(jUri, NULL); + uri = env->GetStringUTFChars(jUri, nullptr); } if (!jResourceTypeArray) { @@ -1916,7 +2676,6 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructResourceObj } JniOcResource *jniOcResource = new JniOcResource(resource); - jlong handle = reinterpret_cast(jniOcResource); jobject jResource = env->NewObject(g_cls_OcResource, g_mid_OcResource_ctor); if (!jResource) @@ -1950,7 +2709,10 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0( JniOcResourceResponse *jniResponse = JniOcResourceResponse::getJniOcResourceResponsePtr(env, jResourceResponse); - if (!jniResponse) return; + if (!jniResponse) + { + return; + } try { @@ -1965,6 +2727,262 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0( catch (OCException& e) { LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* +* 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 +} + +/* +* 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_getDeviceId"); + OCUUIdentity deviceId; + + jbyteArray ret = env->NewByteArray(UUID_IDENTITY_SIZE); + jbyte uuid[UUID_IDENTITY_SIZE]; + try + { + + OCStackResult result = OCPlatform::getDeviceId(&deviceId); + LOGD("OcPlatform_getDeviceId return from CPP"); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Error while getting my device Id"); + } + else + { + for(int i=0;i < UUID_IDENTITY_SIZE; i++) + { + uuid[i] =(jbyte) deviceId.id[i]; + } + } + + } + 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; + jbyte* uuid = env->GetByteArrayElements(data, 0); + jsize arrayLength = env->GetArrayLength(data); + if(arrayLength!=UUID_IDENTITY_SIZE) + { + 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) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* + * Class: org_iotivity_base_OcPlatform + * Method: findKeepAliveResourceImpl + * Signature: (Ljava/lang/String;Lorg/iotivity/base/OcPlatform/KeepAliveListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findKeepAliveResourceImpl( + JNIEnv *env, jclass clazz, jstring jHost, jobject jListener) +{ +#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) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "host cannot be null"); + return; + } + else + { + host = env->GetStringUTFChars(jHost, nullptr); + } + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onKeepAliveFoundListener cannot be null"); + return; + } + + JniKeepAliveListener *onKeepAliveFoundListener = AddKeepAliveListener(env, jListener); + + KeepAliveCallback KeepAliveCallback = [onKeepAliveFoundListener](const int ret, + const OCRepresentation& rep) + { + onKeepAliveFoundListener->onKeepAliveListener(ret, rep); + }; + + try + { + OCStackResult result = OCPlatform::findKeepAliveResource(host, KeepAliveCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "findKeepAliveResource has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + return; + } +#endif +} + +/* + * Class: org_iotivity_base_OcPlatform + * Method: sendKeepAliveRequestImpl + * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/KeepAliveListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendKeepAliveRequestImpl( + JNIEnv *env, jclass clazz, jstring jHost, jobject jRep, jobject jListener) +{ +#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, "host cannot be null"); + return; + } + else + { + host = env->GetStringUTFChars(jHost, nullptr); + } + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "keepAliveResponseListener cannot be null"); + return; + }; + + JniKeepAliveListener *KeepAliveResponseListener = AddKeepAliveListener(env, jListener); + + KeepAliveCallback KeepAliveCallback = [KeepAliveResponseListener](const int ret, + const OCRepresentation& rep) + { + KeepAliveResponseListener->onKeepAliveListener(ret, rep); + }; + + try + { + OCRepresentation *rep = JniOcRepresentation::getOCRepresentationPtr(env, jRep); + OCStackResult result = OCPlatform::sendKeepAliveRequest(host, *rep, KeepAliveCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "sendKeepAliveRequest has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + return; } +#endif }