From: Rahul Rahul Date: Mon, 29 Jun 2015 20:27:18 +0000 (-0700) Subject: Added platform information to the android code, as per the spec compliance changes X-Git-Tag: 0.9.2-beta~93 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=af8e9f1075b0372fd35f6192ac0e4002d2a590ea;p=contrib%2Fiotivity.git Added platform information to the android code, as per the spec compliance changes Change-Id: I78fba71991d8e9da92f706f26166be86a6704c7c Signed-off-by: Rahul Rahul Reviewed-on: https://gerrit.iotivity.org/gerrit/1443 Tested-by: jenkins-iotivity Reviewed-by: Mandeep Shetty Reviewed-by: Erich Keane --- diff --git a/android/android_api/base/jni/Android.mk b/android/android_api/base/jni/Android.mk index aa2cbae..6a7e613 100644 --- a/android/android_api/base/jni/Android.mk +++ b/android/android_api/base/jni/Android.mk @@ -43,6 +43,7 @@ LOCAL_SRC_FILES := JniOcStack.cpp \ JniEntityHandler.cpp \ JniOnResourceFoundListener.cpp \ JniOnDeviceInfoListener.cpp \ + JniOnPlatformInfoListener.cpp \ JniOnPresenceListener.cpp \ JniOnGetListener.cpp \ JniOnPutListener.cpp \ diff --git a/android/android_api/base/jni/JniOcPlatform.cpp b/android/android_api/base/jni/JniOcPlatform.cpp index 40b5898..5dfe78b 100644 --- a/android/android_api/base/jni/JniOcPlatform.cpp +++ b/android/android_api/base/jni/JniOcPlatform.cpp @@ -1,1503 +1,1814 @@ -/* -* //****************************************************************** -* // -* // Copyright 2015 Intel Corporation. -* // -* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -* // -* // 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 "JniOcPlatform.h" -#include "OCPlatform.h" -#include "JniOcResource.h" -#include "JniOcResourceHandle.h" -#include "JniOcPresenceHandle.h" -#include "JniOcResourceResponse.h" -#include "JniUtils.h" - -using namespace OC; - -JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener) -{ - JniOnResourceFoundListener *onResourceFoundListener = NULL; - - resourceFoundMapLock.lock(); - - for (auto it = onResourceFoundListenerMap.begin(); it != onResourceFoundListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - onResourceFoundListener = refPair.first; - refPair.second++; - it->second = refPair; - onResourceFoundListenerMap.insert(*it); - LOGD("OnResourceFoundListener: ref. count incremented"); - break; - } - } - - if (!onResourceFoundListener) - { - onResourceFoundListener = new JniOnResourceFoundListener(env, jListener, RemoveOnResourceFoundListener); - jobject jgListener = env->NewGlobalRef(jListener); - - onResourceFoundListenerMap.insert(std::pair < jobject, std::pair < JniOnResourceFoundListener*, - int >> (jgListener, std::pair(onResourceFoundListener, 1))); - LOGD("OnResourceFoundListener: new listener"); - } - resourceFoundMapLock.unlock(); - return onResourceFoundListener; -} - -void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener) -{ - resourceFoundMapLock.lock(); - - for (auto it = onResourceFoundListenerMap.begin(); it != onResourceFoundListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - if (refPair.second > 1) - { - refPair.second--; - it->second = refPair; - onResourceFoundListenerMap.insert(*it); - LOGI("OnResourceFoundListener: ref. count decremented"); - } - else - { - env->DeleteGlobalRef(it->first); - JniOnResourceFoundListener* listener = refPair.first; - delete listener; - onResourceFoundListenerMap.erase(it); - LOGI("OnResourceFoundListener removed"); - } - break; - } - } - resourceFoundMapLock.unlock(); -} - -JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener) -{ - JniOnDeviceInfoListener *onDeviceInfoListener = NULL; - - deviceInfoMapLock.lock(); - - for (auto it = onDeviceInfoListenerMap.begin(); it != onDeviceInfoListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - onDeviceInfoListener = refPair.first; - refPair.second++; - it->second = refPair; - onDeviceInfoListenerMap.insert(*it); - LOGD("OnDeviceInfoListener: ref. count incremented"); - break; - } - } - - if (!onDeviceInfoListener) - { - onDeviceInfoListener = new JniOnDeviceInfoListener(env, jListener, RemoveOnDeviceInfoListener); - jobject jgListener = env->NewGlobalRef(jListener); - - onDeviceInfoListenerMap.insert(std::pair < jobject, std::pair < JniOnDeviceInfoListener*, - int >> (jgListener, std::pair(onDeviceInfoListener, 1))); - LOGI("OnDeviceInfoListener: new listener"); - } - - deviceInfoMapLock.unlock(); - return onDeviceInfoListener; -} - -void RemoveOnDeviceInfoListener(JNIEnv* env, jobject jListener) -{ - deviceInfoMapLock.lock(); - bool isFound = false; - for (auto it = onDeviceInfoListenerMap.begin(); it != onDeviceInfoListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - if (refPair.second > 1) - { - refPair.second--; - it->second = refPair; - onDeviceInfoListenerMap.insert(*it); - LOGI("OnDeviceInfoListener: ref. count decremented"); - } - else - { - env->DeleteGlobalRef(it->first); - JniOnDeviceInfoListener* listener = refPair.first; - delete listener; - onDeviceInfoListenerMap.erase(it); - - LOGI("OnDeviceInfoListener removed"); - } - - isFound = true; - break; - } - } - - if (!isFound) - { - ThrowOcException(JNI_EXCEPTION, "OnDeviceInfoListenet not found"); - } - deviceInfoMapLock.unlock(); -} - -JniOnPresenceListener* AddOnPresenceListener(JNIEnv* env, jobject jListener) -{ - JniOnPresenceListener *onPresenceListener = NULL; - - presenceMapLock.lock(); - - for (auto it = onPresenceListenerMap.begin(); it != onPresenceListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - onPresenceListener = refPair.first; - refPair.second++; - it->second = refPair; - onPresenceListenerMap.insert(*it); - LOGD("OnPresenceListener: ref. count incremented"); - break; - } - } - if (!onPresenceListener) - { - onPresenceListener = new JniOnPresenceListener(env, jListener, RemoveOnPresenceListener); - jobject jgListener = env->NewGlobalRef(jListener); - onPresenceListenerMap.insert(std::pair < jobject, std::pair < JniOnPresenceListener*, - int >> (jgListener, std::pair(onPresenceListener, 1))); - LOGI("OnPresenceListener: new listener"); - } - presenceMapLock.unlock(); - return onPresenceListener; -} - -void RemoveOnPresenceListener(JNIEnv* env, jobject jListener) -{ - presenceMapLock.lock(); - bool isFound = false; - for (auto it = onPresenceListenerMap.begin(); it != onPresenceListenerMap.end(); ++it) - { - if (env->IsSameObject(jListener, it->first)) - { - auto refPair = it->second; - if (refPair.second > 1) - { - refPair.second--; - it->second = refPair; - onPresenceListenerMap.insert(*it); - LOGI("OnPresenceListener: ref. count decremented"); - } - else - { - env->DeleteGlobalRef(it->first); - JniOnPresenceListener* listener = refPair.first; - delete listener; - onPresenceListenerMap.erase(it); - LOGI("OnPresenceListener is removed"); - } - isFound = true; - break; - } - } - if (!isFound) - { - ThrowOcException(JNI_EXCEPTION, "OnPresenceListener not found"); - } - presenceMapLock.unlock(); -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: configure -* Signature: (IILjava/lang/String;II)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure -(JNIEnv *env, jclass clazz, jint jServiceType, jint jModeType, jstring jIpAddress, jint jPort, jint jQOS) -{ - LOGI("OcPlatform_configure"); - - std::string ipAddress; - if (jIpAddress) - { - ipAddress = env->GetStringUTFChars(jIpAddress, NULL); - } - uint16_t port; - if (jPort > 0) - { - port = static_cast(jPort); - } - PlatformConfig cfg{ - JniUtils::getServiceType(env, jServiceType), - JniUtils::getModeType(env, jModeType), - ipAddress, - port, - JniUtils::getQOS(env, static_cast(jQOS)) - }; - - OCPlatform::Configure(cfg); -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: notifyAllObservers0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0 -(JNIEnv *env, jclass clazz, jobject jResourceHandle) -{ - LOGI("OcPlatform_notifyAllObservers"); - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - try - { - OCStackResult result = OCPlatform::notifyAllObservers(jniOcResourceHandle->getOCResourceHandle()); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to notify all observers"); - return; - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: notifyAllObservers1 -* Signature: (Lorg/iotivity/base/OcResourceHandle;I)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1 -(JNIEnv *env, jclass clazz, jobject jResourceHandle, jint jQoS) -{ - LOGI("OcPlatform_notifyAllObservers1"); - - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - try{ - OCStackResult result = OCPlatform::notifyAllObservers( - jniOcResourceHandle->getOCResourceHandle(), - JniUtils::getQOS(env, static_cast(jQoS))); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to notify all observers"); - return; - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: notifyListOfObservers2 -* Signature: (Lorg/iotivity/base/OcResourceHandle;[Ljava/lang/Byte;Lorg/iotivity/base/OcResourceResponse;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers2 -(JNIEnv *env, jclass clazz, jobject jResourceHandle, jbyteArray jObservationIdArr, jobject jResourceResponse) -{ - LOGD("OcPlatform_notifyListOfObservers2"); - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - if (!jObservationIdArr) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "observationIdList cannot be null"); - return; - } - if (!jResourceResponse) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - JniOcResourceResponse* jniOcResourceResponse = JniOcResourceResponse::getJniOcResourceResponsePtr( - env, jResourceResponse); - if (!jniOcResourceResponse) return; - - int len = env->GetArrayLength(jObservationIdArr); - uint8_t* bArr = (uint8_t*)env->GetByteArrayElements(jObservationIdArr, 0); - - ObservationIds observationIds; - for (int i = 0; i < len; ++i) - { - observationIds.push_back(bArr[i]); - } - - env->ReleaseByteArrayElements(jObservationIdArr, (jbyte*)bArr, 0); - - try{ - OCStackResult result = OCPlatform::notifyListOfObservers( - jniOcResourceHandle->getOCResourceHandle(), - observationIds, - jniOcResourceResponse->getOCResourceResponse()); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to notify all observers"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: notifyListOfObservers3 -* Signature: (Lorg/iotivity/base/OcResourceHandle;[Ljava/lang/Byte;Lorg/iotivity/base/OcResourceResponse;I)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers3 -(JNIEnv *env, jclass clazz, jobject jResourceHandle, jbyteArray jObservationIdArr, jobject jResourceResponse, jint jQoS) -{ - LOGD("OcPlatform_notifyListOfObservers3"); - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - if (!jObservationIdArr) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "observationIdList cannot be null"); - return; - } - if (!jResourceResponse) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - JniOcResourceResponse* jniOcResourceResponse = JniOcResourceResponse::getJniOcResourceResponsePtr( - env, jResourceResponse); - if (!jniOcResourceResponse) return; - - int len = env->GetArrayLength(jObservationIdArr); - uint8_t* bArr = (uint8_t*)env->GetByteArrayElements(jObservationIdArr, 0); - - ObservationIds observationIds; - for (int i = 0; i < len; ++i) - { - observationIds.push_back(bArr[i]); - } - - env->ReleaseByteArrayElements(jObservationIdArr, (jbyte*)bArr, 0); - - try{ - OCStackResult result = OCPlatform::notifyListOfObservers( - jniOcResourceHandle->getOCResourceHandle(), - observationIds, - jniOcResourceResponse->getOCResourceResponse(), - JniUtils::getQOS(env, static_cast(jQoS))); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to notify all observers"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: findResource0 -* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0 -(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener) -{ - LOGD("OcPlatform_findResource"); - std::string host; - if (jHost) - { - host = env->GetStringUTFChars(jHost, NULL); - } - std::string resourceUri; - if (jResourceUri) - { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "onResourceFoundListener cannot be null"); - return; - } - - JniOnResourceFoundListener *onResFoundListener = AddOnResourceFoundListener(env, jListener); - - FindCallback findCallback = [onResFoundListener](std::shared_ptr resource) - { - onResFoundListener->foundResourceCallback(resource); - }; - - try - { - OCStackResult result = OCPlatform::findResource( - host, - resourceUri, - JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), - findCallback); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Find resource has failed"); - return; - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: findResource1 -* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;I)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1 -(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener, jint jQoS) -{ - LOGD("OcPlatform_findResource"); - std::string host; - if (jHost) - { - host = env->GetStringUTFChars(jHost, NULL); - } - std::string resourceUri; - if (jResourceUri) - { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "onResourceFoundListener cannot be null"); - return; - } - JniOnResourceFoundListener *onResFoundListener = AddOnResourceFoundListener(env, jListener); - - FindCallback findCallback = [onResFoundListener](std::shared_ptr resource) - { - onResFoundListener->foundResourceCallback(resource); - }; - - try - { - OCStackResult result = OCPlatform::findResource( - host, - resourceUri, - JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), - findCallback, - JniUtils::getQOS(env, static_cast(jQoS))); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Find resource has failed"); - return; - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: getDeviceInfo0 -* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0 -(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener) -{ - LOGD("OcPlatform_getDeviceInfo0"); - std::string host; - if (jHost) - { - host = env->GetStringUTFChars(jHost, NULL); - } - std::string resourceUri; - if (jResourceUri) - { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "onDeviceFoundListener cannot be null"); - return; - } - JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener); - - FindDeviceCallback findDeviceCallback = [onDeviceInfoListener](const OCRepresentation& ocRepresentation) - { - onDeviceInfoListener->foundDeviceCallback(ocRepresentation); - }; - - try - { - OCStackResult result = OCPlatform::getDeviceInfo( - host, - resourceUri, - JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), - findDeviceCallback); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Find device has failed"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: getDeviceInfo1 -* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;I)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1 -(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener, jint jQoS) -{ - LOGD("OcPlatform_getDeviceInfo1"); - std::string host; - if (jHost) - { - host = env->GetStringUTFChars(jHost, NULL); - } - std::string resourceUri; - if (jResourceUri) - { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "onDeviceFoundListener cannot be null"); - return; - } - JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener); - - FindDeviceCallback findDeviceCallback = [onDeviceInfoListener](const OCRepresentation& ocRepresentation) - { - onDeviceInfoListener->foundDeviceCallback(ocRepresentation); - }; - - try - { - OCStackResult result = OCPlatform::getDeviceInfo( - host, - resourceUri, - JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), - findDeviceCallback, - JniUtils::getQOS(env, static_cast(jQoS))); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Find device has failed"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: registerResource0 -* Signature: (Lorg/iotivity/base/OcResource;)Lorg/iotivity/base/OcResourceHandle; -*/ -JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0 -(JNIEnv *env, jclass clazz, jobject jResource) -{ - LOGD("OcPlatform_registerResource"); - if (!jResource) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "Resource cannot be null"); - return nullptr; - } - JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, jResource); - if (!resource) return nullptr; - - OCResourceHandle resourceHandle; - try - { - OCStackResult result = OCPlatform::registerResource( - resourceHandle, - resource->getOCResource()); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "register resource"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - return nullptr; - } - - JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle); - jlong handle = reinterpret_cast(jniHandle); - jobject jResourceHandle = env->NewObject(g_cls_OcResourceHandle, g_mid_OcResourceHandle_N_ctor, handle); - if (!jResourceHandle) - { - LOGE("Failed to create OcResourceHandle"); - delete jniHandle; - } - return jResourceHandle; -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: registerResource1 -* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcPlatform/EntityHandler;I)Lorg/iotivity/base/OcResourceHandle; -*/ -JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource1 -(JNIEnv *env, jclass clazz, jstring jResourceUri, jstring jResourceTypeName, jstring jResourceInterface, -jobject jListener, jint jResourceProperty) -{ - LOGI("OcPlatform_registerResource1"); - std::string resourceUri; - if (jResourceUri) - { - resourceUri = env->GetStringUTFChars(jResourceUri, NULL); - } - std::string resourceTypeName; - if (jResourceTypeName) - { - resourceTypeName = env->GetStringUTFChars(jResourceTypeName, NULL); - } - std::string resourceInterface; - if (jResourceInterface) - { - resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "entityHandler cannot be null"); - return nullptr; - } - JniEntityHandler* entityHandler = new JniEntityHandler(env, jListener); - EntityHandler handleEntityCallback = [entityHandler](const std::shared_ptr request) -> - OCEntityHandlerResult{ - return entityHandler->handleEntity(request); - }; - - OCResourceHandle resourceHandle; - try - { - OCStackResult result = OCPlatform::registerResource( - resourceHandle, - resourceUri, - resourceTypeName, - resourceInterface, - handleEntityCallback, - static_cast(jResourceProperty)); - - if (OC_STACK_OK != result) - { - delete entityHandler; - ThrowOcException(result, "register resource"); - return nullptr; - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - delete entityHandler; - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - return nullptr; - } - - JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle); - jlong handle = reinterpret_cast(jniHandle); - jobject jResourceHandle = env->NewObject(g_cls_OcResourceHandle, g_mid_OcResourceHandle_N_ctor, handle); - if (!jResourceHandle) - { - LOGE("Failed to create OcResourceHandle"); - delete jniHandle; - } - - return jResourceHandle; -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: registerDeviceInfo0 -* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0 -(JNIEnv *env, -jclass clazz, -jstring jDeviceName) -{ - LOGI("OcPlatform_registerDeviceInfo"); - - std::string deviceName; - if (jDeviceName) - { - deviceName = env->GetStringUTFChars(jDeviceName, NULL); - } - - OCDeviceInfo deviceInfo; - try - { - DuplicateString(&deviceInfo.deviceName, deviceName); - } - catch (std::exception &e) - { - ThrowOcException(JNI_EXCEPTION, "Failed to construct device info"); - return; - } - - try - { - OCStackResult result = OCPlatform::registerDeviceInfo(deviceInfo); - - delete deviceInfo.deviceName; - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to register device info"); - return; - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: unregisterResource0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unregisterResource0 -(JNIEnv *env, jclass clazz, jobject jResourceHandle) -{ - LOGI("OcPlatform_unregisterResource"); - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - try - { - OCResourceHandle resHandle = jniOcResourceHandle->getOCResourceHandle(); - OCStackResult result = OCPlatform::unregisterResource(resHandle); - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to unregister resource"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: bindResource0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0 -(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobject jResourceHandle) -{ - LOGI("OcPlatform_bindResource"); - if (!jResourceCollectionHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); - return; - } - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceCollectionHandle); - if (!jniOcResourceCollectionHandle) return; - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - try - { - OCStackResult result = OCPlatform::bindResource( - jniOcResourceCollectionHandle->getOCResourceHandle(), - jniOcResourceHandle->getOCResourceHandle() - ); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to bind resource"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: bindResources0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResources0 -(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobjectArray jResourceHandleArray) -{ - LOGI("OcPlatform_bindResources"); - - if (!jResourceCollectionHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); - return; - } - if (!jResourceHandleArray) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceCollectionHandle); - if (!jniOcResourceCollectionHandle) return; - - std::vector resourceHandleList; - int len = env->GetArrayLength(jResourceHandleArray); - for (int 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) return; - - resourceHandleList.push_back( - jniOcResourceHandle->getOCResourceHandle()); - } - - try - { - OCStackResult result = OCPlatform::bindResources( - jniOcResourceCollectionHandle->getOCResourceHandle(), - resourceHandleList - ); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to bind resources"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: unbindResource0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResource0 -(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobject jResourceHandle) -{ - LOGI("OcPlatform_unbindResource"); - if (!jResourceCollectionHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); - return; - } - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceCollectionHandle); - if (!jniOcResourceCollectionHandle) return; - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - try - { - OCStackResult result = OCPlatform::unbindResource( - jniOcResourceCollectionHandle->getOCResourceHandle(), - jniOcResourceHandle->getOCResourceHandle() - ); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to unbind resource"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: unbindResources0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResources0 -(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobjectArray jResourceHandleArray) -{ - LOGI("OcPlatform_unbindResources"); - if (!jResourceCollectionHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); - return; - } - if (!jResourceHandleArray) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null"); - return; - } - - JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceCollectionHandle); - if (!jniOcResourceCollectionHandle) return; - - std::vector resourceHandleList; - int len = env->GetArrayLength(jResourceHandleArray); - for (int 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) return; - - resourceHandleList.push_back( - jniOcResourceHandle->getOCResourceHandle()); - } - - try - { - OCStackResult result = OCPlatform::unbindResources( - jniOcResourceCollectionHandle->getOCResourceHandle(), - resourceHandleList - ); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to unbind resources"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: bindTypeToResource0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0 -(JNIEnv *env, jclass clazz, jobject jResourceHandle, jstring jResourceTypeName) -{ - LOGI("OcPlatform_bindTypeToResource"); - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - std::string typeName; - if (jResourceTypeName) - { - typeName = env->GetStringUTFChars(jResourceTypeName, NULL); - } - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - try - { - OCStackResult result = OCPlatform::bindTypeToResource( - jniOcResourceHandle->getOCResourceHandle(), - typeName - ); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to bind type to resource"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: bindInterfaceToResource0 -* Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource0 -(JNIEnv *env, jclass clazz, jobject jResourceHandle, jstring jResourceInterfaceName) -{ - LOGI("OcPlatform_bindInterfaceToResource"); - if (!jResourceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); - return; - } - std::string interfaceName; - if (jResourceInterfaceName) - { - interfaceName = env->GetStringUTFChars(jResourceInterfaceName, NULL); - } - - JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( - env, jResourceHandle); - if (!jniOcResourceHandle) return; - - try - { - OCStackResult result = OCPlatform::bindInterfaceToResource( - jniOcResourceHandle->getOCResourceHandle(), - interfaceName - ); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to bind interface to resource"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: startPresence0 -* Signature: (I)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_startPresence0 -(JNIEnv *env, jclass clazz, jint ttl) -{ - LOGI("OcPlatform_startPresence"); - - try - { - OCStackResult result = OCPlatform::startPresence((unsigned int)ttl); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to start presence"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: stopPresence0 -* Signature: ()V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stopPresence0 -(JNIEnv *env, jclass clazz) -{ - LOGI("OcPlatform_stopPresence"); - - try - { - OCStackResult result = OCPlatform::stopPresence(); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "Failed to stop presence"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: subscribePresence0 -* Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; -*/ -JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0 -(JNIEnv *env, jclass clazz, jstring jHost, jint jConnectivityType, jobject jListener) -{ - LOGD("OcPlatform_subscribePresence"); - std::string host; - if (jHost) - { - host = env->GetStringUTFChars(jHost, NULL); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "onPresenceListener cannot be null"); - return nullptr; - } - - JniOnPresenceListener *onPresenceListener = AddOnPresenceListener(env, jListener); - - SubscribeCallback subscribeCallback = [onPresenceListener](OCStackResult result, const unsigned int nonce, - const std::string& hostAddress) - { - onPresenceListener->onPresenceCallback(result, nonce, hostAddress); - }; - - OCPlatform::OCPresenceHandle presenceHandle; - try - { - OCStackResult result = OCPlatform::subscribePresence( - presenceHandle, - host, - JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), - subscribeCallback); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "subscribe presence has failed"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - return nullptr; - } - - JniOcPresenceHandle* jniPresenceHandle = new JniOcPresenceHandle(onPresenceListener, 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; -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: subscribePresence1 -* Signature: (Ljava/lang/String;Ljava/lang/String;I -Lorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; -*/ -JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1 -(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceType, jint jConnectivityType, jobject jListener) -{ - LOGD("OcPlatform_subscribePresence1"); - std::string host; - if (jHost) - { - host = env->GetStringUTFChars(jHost, NULL); - } - std::string resourceType; - if (jResourceType) - { - resourceType = env->GetStringUTFChars(jResourceType, NULL); - } - if (!jListener) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "onPresenceListener cannot be null"); - return nullptr; - } - - JniOnPresenceListener *onPresenceListener = AddOnPresenceListener(env, jListener); - - SubscribeCallback subscribeCallback = [onPresenceListener](OCStackResult result, - const unsigned int nonce, const std::string& hostAddress) - { - onPresenceListener->onPresenceCallback(result, nonce, hostAddress); - }; - - OCPlatform::OCPresenceHandle presenceHandle; - try - { - OCStackResult result = OCPlatform::subscribePresence( - presenceHandle, - host, - resourceType, - JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), - subscribeCallback); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "subscribe presence has failed"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - return nullptr; - } - - JniOcPresenceHandle* jniPresenceHandle = new JniOcPresenceHandle(onPresenceListener, 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; -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: unsubscribePresence0 -* Signature: (Lorg/iotivity/base/OcPresenceHandle;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0 -(JNIEnv *env, jclass clazz, jobject jPresenceHandle) -{ - LOGD("OcPlatform_unsubscribePresence"); - if (!jPresenceHandle) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "presenceHandle cannot be null"); - return; - } - JniOcPresenceHandle* jniPresenceHandle = JniOcPresenceHandle::getJniOcPresenceHandlePtr(env, jPresenceHandle); - if (!jniPresenceHandle) return; - - OCPresenceHandle presenceHandle = jniPresenceHandle->getOCPresenceHandle(); - - try - { - OCStackResult result = OCPlatform::unsubscribePresence(presenceHandle); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "unsubscribe presence has failed"); - return; - } - jweak jwOnPresenceListener = jniPresenceHandle->getJniOnPresenceListener()->getJWListener(); - if (jwOnPresenceListener) - { - RemoveOnPresenceListener(env, jwOnPresenceListener); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} - -/* -* 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, NULL); - } - std::string uri; - if (jUri) - { - uri = env->GetStringUTFChars(jUri, NULL); - } - if (!jResourceTypeArray) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceTypeList cannot be null"); - return nullptr; - } - if (!jInterfaceArray) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "interfaceList cannot be null"); - return nullptr; - } - - std::vector resourceTypes; - JniUtils::convertJavaStrArrToStrVector(env, jResourceTypeArray, resourceTypes); - - std::vector interfaces; - JniUtils::convertJavaStrArrToStrVector(env, jInterfaceArray, interfaces); - - std::shared_ptr resource = OCPlatform::constructResourceObject( - host, - uri, - JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), - static_cast(jIsObservable), - resourceTypes, - interfaces); - - if (!resource) - { - ThrowOcException(OC_STACK_ERROR, "Failed to create OCResource"); - return nullptr; - } - - JniOcResource *jniOcResource = new JniOcResource(resource); - jlong handle = reinterpret_cast(jniOcResource); - - jobject jResource = env->NewObject(g_cls_OcResource, g_mid_OcResource_ctor); - if (!jResource) - { - delete jniOcResource; - return nullptr; - } - SetHandle(env, jResource, jniOcResource); - if (env->ExceptionCheck()) - { - delete jniOcResource; - return nullptr; - } - return jResource; -} - -/* -* Class: org_iotivity_base_OcPlatform -* Method: sendResponse0 -* Signature: (Lorg/iotivity/base/OcResourceResponse;)V -*/ -JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0 -(JNIEnv *env, jclass clazz, jobject jResourceResponse) -{ - LOGD("OcPlatform_sendResponse"); - if (!jResourceResponse) - { - ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null"); - return; - } - - JniOcResourceResponse *jniResponse = JniOcResourceResponse::getJniOcResourceResponsePtr( - env, jResourceResponse); - if (!jniResponse) return; - - try - { - OCStackResult result = OCPlatform::sendResponse(jniResponse->getOCResourceResponse()); - - if (OC_STACK_OK != result) - { - ThrowOcException(result, "failed to send response"); - } - } - catch (OCException& e) - { - LOGE("%s", e.reason().c_str()); - ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); - } -} \ No newline at end of file +/* +* //****************************************************************** +* // +* // Copyright 2015 Intel Corporation. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // 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 "JniOcPlatform.h" +#include "OCPlatform.h" +#include "JniOcResource.h" +#include "JniOcResourceHandle.h" +#include "JniOcPresenceHandle.h" +#include "JniOcResourceResponse.h" +#include "JniUtils.h" + +using namespace OC; + +JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener) +{ + JniOnResourceFoundListener *onResourceFoundListener = NULL; + + resourceFoundMapLock.lock(); + + for (auto it = onResourceFoundListenerMap.begin(); it != onResourceFoundListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onResourceFoundListener = refPair.first; + refPair.second++; + it->second = refPair; + onResourceFoundListenerMap.insert(*it); + LOGD("OnResourceFoundListener: ref. count incremented"); + break; + } + } + + if (!onResourceFoundListener) + { + onResourceFoundListener = new JniOnResourceFoundListener(env, jListener, RemoveOnResourceFoundListener); + jobject jgListener = env->NewGlobalRef(jListener); + + onResourceFoundListenerMap.insert(std::pair < jobject, std::pair < JniOnResourceFoundListener*, + int >> (jgListener, std::pair(onResourceFoundListener, 1))); + LOGD("OnResourceFoundListener: new listener"); + } + resourceFoundMapLock.unlock(); + return onResourceFoundListener; +} + +void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener) +{ + resourceFoundMapLock.lock(); + + for (auto it = onResourceFoundListenerMap.begin(); it != onResourceFoundListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onResourceFoundListenerMap.insert(*it); + LOGI("OnResourceFoundListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnResourceFoundListener* listener = refPair.first; + delete listener; + onResourceFoundListenerMap.erase(it); + LOGI("OnResourceFoundListener removed"); + } + break; + } + } + resourceFoundMapLock.unlock(); +} + +JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener) +{ + JniOnDeviceInfoListener *onDeviceInfoListener = NULL; + + deviceInfoMapLock.lock(); + + for (auto it = onDeviceInfoListenerMap.begin(); it != onDeviceInfoListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onDeviceInfoListener = refPair.first; + refPair.second++; + it->second = refPair; + onDeviceInfoListenerMap.insert(*it); + LOGD("OnDeviceInfoListener: ref. count incremented"); + break; + } + } + + if (!onDeviceInfoListener) + { + onDeviceInfoListener = new JniOnDeviceInfoListener(env, jListener, RemoveOnDeviceInfoListener); + jobject jgListener = env->NewGlobalRef(jListener); + + onDeviceInfoListenerMap.insert(std::pair < jobject, std::pair < JniOnDeviceInfoListener*, + int >> (jgListener, std::pair(onDeviceInfoListener, 1))); + LOGI("OnDeviceInfoListener: new listener"); + } + + deviceInfoMapLock.unlock(); + return onDeviceInfoListener; +} + +void RemoveOnDeviceInfoListener(JNIEnv* env, jobject jListener) +{ + deviceInfoMapLock.lock(); + bool isFound = false; + for (auto it = onDeviceInfoListenerMap.begin(); it != onDeviceInfoListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onDeviceInfoListenerMap.insert(*it); + LOGI("OnDeviceInfoListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnDeviceInfoListener* listener = refPair.first; + delete listener; + onDeviceInfoListenerMap.erase(it); + + LOGI("OnDeviceInfoListener removed"); + } + + isFound = true; + break; + } + } + + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "OnDeviceInfoListenet not found"); + } + deviceInfoMapLock.unlock(); +} + +JniOnPlatformInfoListener* AddOnPlatformInfoListener(JNIEnv* env, jobject jListener) +{ + JniOnPlatformInfoListener *onPlatformInfoListener = NULL; + + platformInfoMapLock.lock(); + + for (auto it = onPlatformInfoListenerMap.begin(); it != onPlatformInfoListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onPlatformInfoListener = refPair.first; + refPair.second++; + it->second = refPair; + onPlatformInfoListenerMap.insert(*it); + LOGD("OnPlatformInfoListener: ref. count incremented"); + break; + } + } + + if (!onPlatformInfoListener) + { + onPlatformInfoListener = new JniOnPlatformInfoListener(env, jListener, RemoveOnPlatformInfoListener); + jobject jgListener = env->NewGlobalRef(jListener); + + onPlatformInfoListenerMap.insert(std::pair < jobject, std::pair < JniOnPlatformInfoListener*, + int >> (jgListener, std::pair(onPlatformInfoListener, 1))); + LOGI("OnPlatformInfoListener: new listener"); + } + + platformInfoMapLock.unlock(); + return onPlatformInfoListener; +} + +void RemoveOnPlatformInfoListener(JNIEnv* env, jobject jListener) +{ + platformInfoMapLock.lock(); + bool isFound = false; + for (auto it = onPlatformInfoListenerMap.begin(); it != onPlatformInfoListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onPlatformInfoListenerMap.insert(*it); + LOGI("OnPlatformInfoListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnPlatformInfoListener* listener = refPair.first; + delete listener; + onPlatformInfoListenerMap.erase(it); + + LOGI("OnPlatformInfoListener removed"); + } + + isFound = true; + break; + } + } + + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "OnPlatformInfoListenet not found"); + } + platformInfoMapLock.unlock(); +} + +JniOnPresenceListener* AddOnPresenceListener(JNIEnv* env, jobject jListener) +{ + JniOnPresenceListener *onPresenceListener = NULL; + + presenceMapLock.lock(); + + for (auto it = onPresenceListenerMap.begin(); it != onPresenceListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onPresenceListener = refPair.first; + refPair.second++; + it->second = refPair; + onPresenceListenerMap.insert(*it); + LOGD("OnPresenceListener: ref. count incremented"); + break; + } + } + if (!onPresenceListener) + { + onPresenceListener = new JniOnPresenceListener(env, jListener, RemoveOnPresenceListener); + jobject jgListener = env->NewGlobalRef(jListener); + onPresenceListenerMap.insert(std::pair < jobject, std::pair < JniOnPresenceListener*, + int >> (jgListener, std::pair(onPresenceListener, 1))); + LOGI("OnPresenceListener: new listener"); + } + presenceMapLock.unlock(); + return onPresenceListener; +} + +void RemoveOnPresenceListener(JNIEnv* env, jobject jListener) +{ + presenceMapLock.lock(); + bool isFound = false; + for (auto it = onPresenceListenerMap.begin(); it != onPresenceListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onPresenceListenerMap.insert(*it); + LOGI("OnPresenceListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnPresenceListener* listener = refPair.first; + delete listener; + onPresenceListenerMap.erase(it); + LOGI("OnPresenceListener is removed"); + } + isFound = true; + break; + } + } + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "OnPresenceListener not found"); + } + presenceMapLock.unlock(); +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: configure +* Signature: (IILjava/lang/String;II)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure +(JNIEnv *env, jclass clazz, jint jServiceType, jint jModeType, jstring jIpAddress, jint jPort, jint jQOS) +{ + LOGI("OcPlatform_configure"); + + std::string ipAddress; + if (jIpAddress) + { + ipAddress = env->GetStringUTFChars(jIpAddress, NULL); + } + uint16_t port; + if (jPort > 0) + { + port = static_cast(jPort); + } + PlatformConfig cfg{ + JniUtils::getServiceType(env, jServiceType), + JniUtils::getModeType(env, jModeType), + ipAddress, + port, + JniUtils::getQOS(env, static_cast(jQOS)) + }; + + OCPlatform::Configure(cfg); +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: notifyAllObservers0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0 +(JNIEnv *env, jclass clazz, jobject jResourceHandle) +{ + LOGI("OcPlatform_notifyAllObservers"); + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + try + { + OCStackResult result = OCPlatform::notifyAllObservers(jniOcResourceHandle->getOCResourceHandle()); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to notify all observers"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: notifyAllObservers1 +* Signature: (Lorg/iotivity/base/OcResourceHandle;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1 +(JNIEnv *env, jclass clazz, jobject jResourceHandle, jint jQoS) +{ + LOGI("OcPlatform_notifyAllObservers1"); + + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + try{ + OCStackResult result = OCPlatform::notifyAllObservers( + jniOcResourceHandle->getOCResourceHandle(), + JniUtils::getQOS(env, static_cast(jQoS))); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to notify all observers"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: notifyListOfObservers2 +* Signature: (Lorg/iotivity/base/OcResourceHandle;[Ljava/lang/Byte;Lorg/iotivity/base/OcResourceResponse;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers2 +(JNIEnv *env, jclass clazz, jobject jResourceHandle, jbyteArray jObservationIdArr, jobject jResourceResponse) +{ + LOGD("OcPlatform_notifyListOfObservers2"); + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + if (!jObservationIdArr) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "observationIdList cannot be null"); + return; + } + if (!jResourceResponse) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null"); + return; + } + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + JniOcResourceResponse* jniOcResourceResponse = JniOcResourceResponse::getJniOcResourceResponsePtr( + env, jResourceResponse); + if (!jniOcResourceResponse) return; + + int len = env->GetArrayLength(jObservationIdArr); + uint8_t* bArr = (uint8_t*)env->GetByteArrayElements(jObservationIdArr, 0); + + ObservationIds observationIds; + for (int i = 0; i < len; ++i) + { + observationIds.push_back(bArr[i]); + } + + env->ReleaseByteArrayElements(jObservationIdArr, (jbyte*)bArr, 0); + + try{ + OCStackResult result = OCPlatform::notifyListOfObservers( + jniOcResourceHandle->getOCResourceHandle(), + observationIds, + jniOcResourceResponse->getOCResourceResponse()); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to notify all observers"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: notifyListOfObservers3 +* Signature: (Lorg/iotivity/base/OcResourceHandle;[Ljava/lang/Byte;Lorg/iotivity/base/OcResourceResponse;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers3 +(JNIEnv *env, jclass clazz, jobject jResourceHandle, jbyteArray jObservationIdArr, jobject jResourceResponse, jint jQoS) +{ + LOGD("OcPlatform_notifyListOfObservers3"); + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + if (!jObservationIdArr) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "observationIdList cannot be null"); + return; + } + if (!jResourceResponse) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null"); + return; + } + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + JniOcResourceResponse* jniOcResourceResponse = JniOcResourceResponse::getJniOcResourceResponsePtr( + env, jResourceResponse); + if (!jniOcResourceResponse) return; + + int len = env->GetArrayLength(jObservationIdArr); + uint8_t* bArr = (uint8_t*)env->GetByteArrayElements(jObservationIdArr, 0); + + ObservationIds observationIds; + for (int i = 0; i < len; ++i) + { + observationIds.push_back(bArr[i]); + } + + env->ReleaseByteArrayElements(jObservationIdArr, (jbyte*)bArr, 0); + + try{ + OCStackResult result = OCPlatform::notifyListOfObservers( + jniOcResourceHandle->getOCResourceHandle(), + observationIds, + jniOcResourceResponse->getOCResourceResponse(), + JniUtils::getQOS(env, static_cast(jQoS))); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to notify all observers"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: findResource0 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0 +(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener) +{ + LOGD("OcPlatform_findResource"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + std::string resourceUri; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onResourceFoundListener cannot be null"); + return; + } + + JniOnResourceFoundListener *onResFoundListener = AddOnResourceFoundListener(env, jListener); + + FindCallback findCallback = [onResFoundListener](std::shared_ptr resource) + { + onResFoundListener->foundResourceCallback(resource); + }; + + try + { + OCStackResult result = OCPlatform::findResource( + host, + resourceUri, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + findCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find resource has failed"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: findResource1 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1 +(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener, jint jQoS) +{ + LOGD("OcPlatform_findResource"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + std::string resourceUri; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onResourceFoundListener cannot be null"); + return; + } + JniOnResourceFoundListener *onResFoundListener = AddOnResourceFoundListener(env, jListener); + + FindCallback findCallback = [onResFoundListener](std::shared_ptr resource) + { + onResFoundListener->foundResourceCallback(resource); + }; + + try + { + OCStackResult result = OCPlatform::findResource( + host, + resourceUri, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + findCallback, + JniUtils::getQOS(env, static_cast(jQoS))); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find resource has failed"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: getDeviceInfo0 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0 +(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener) +{ + LOGD("OcPlatform_getDeviceInfo0"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + std::string resourceUri; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onDeviceFoundListener cannot be null"); + return; + } + JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener); + + FindDeviceCallback findDeviceCallback = [onDeviceInfoListener](const OCRepresentation& ocRepresentation) + { + onDeviceInfoListener->foundDeviceCallback(ocRepresentation); + }; + + try + { + OCStackResult result = OCPlatform::getDeviceInfo( + host, + resourceUri, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + findDeviceCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find device has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: getDeviceInfo1 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1 +(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener, jint jQoS) +{ + LOGD("OcPlatform_getDeviceInfo1"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + std::string resourceUri; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onDeviceFoundListener cannot be null"); + return; + } + JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener); + + FindDeviceCallback findDeviceCallback = [onDeviceInfoListener](const OCRepresentation& ocRepresentation) + { + onDeviceInfoListener->foundDeviceCallback(ocRepresentation); + }; + + try + { + OCStackResult result = OCPlatform::getDeviceInfo( + host, + resourceUri, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + findDeviceCallback, + JniUtils::getQOS(env, static_cast(jQoS))); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find device has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: getPlatformInfo0 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPlatformFoundListener;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo0 +(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener) +{ + LOGD("OcPlatform_getPlatformInfo0"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + std::string resourceUri; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onPlatformFoundListener cannot be null"); + return; + } + JniOnPlatformInfoListener *onPlatformInfoListener = AddOnPlatformInfoListener(env, jListener); + + FindPlatformCallback findPlatformCallback = [onPlatformInfoListener](const OCRepresentation& ocRepresentation) + { + onPlatformInfoListener->foundPlatformCallback(ocRepresentation); + }; + + try + { + OCStackResult result = OCPlatform::getPlatformInfo( + host, + resourceUri, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + findPlatformCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find platform has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: getPlatformInfo1 +* Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPlatformFoundListener;I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1 +(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceUri, jint jConnectivityType, jobject jListener, jint jQoS) +{ + LOGD("OcPlatform_getPlatformInfo1"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + std::string resourceUri; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onPlatformFoundListener cannot be null"); + return; + } + JniOnDeviceInfoListener *onDeviceInfoListener = AddOnDeviceInfoListener(env, jListener); + + FindDeviceCallback findDeviceCallback = [onDeviceInfoListener](const OCRepresentation& ocRepresentation) + { + onDeviceInfoListener->foundDeviceCallback(ocRepresentation); + }; + + try + { + OCStackResult result = OCPlatform::getPlatformInfo( + host, + resourceUri, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + findDeviceCallback, + JniUtils::getQOS(env, static_cast(jQoS))); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Find platform has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: registerResource0 +* Signature: (Lorg/iotivity/base/OcResource;)Lorg/iotivity/base/OcResourceHandle; +*/ +JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0 +(JNIEnv *env, jclass clazz, jobject jResource) +{ + LOGD("OcPlatform_registerResource"); + if (!jResource) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "Resource cannot be null"); + return nullptr; + } + JniOcResource *resource = JniOcResource::getJniOcResourcePtr(env, jResource); + if (!resource) return nullptr; + LOGD("OcPlatform_registerResource1"); + OCResourceHandle resourceHandle; + try + { + LOGD("OcPlatform_registerResource2"); + OCStackResult result = OCPlatform::registerResource( + resourceHandle, + resource->getOCResource()); + LOGD("OcPlatform_registerResource3"); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "register resource"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + return nullptr; + } + LOGD("OcPlatform_registerResource4"); + JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle); + jlong handle = reinterpret_cast(jniHandle); + jobject jResourceHandle = env->NewObject(g_cls_OcResourceHandle, g_mid_OcResourceHandle_N_ctor, handle); + LOGD("OcPlatform_registerResource5"); + if (!jResourceHandle) + { + LOGE("Failed to create OcResourceHandle"); + delete jniHandle; + } + return jResourceHandle; +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: registerResource1 +* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcPlatform/EntityHandler;I)Lorg/iotivity/base/OcResourceHandle; +*/ +JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource1 +(JNIEnv *env, jclass clazz, jstring jResourceUri, jstring jResourceTypeName, jstring jResourceInterface, +jobject jListener, jint jResourceProperty) +{ + LOGI("OcPlatform_registerResource1"); + std::string resourceUri; + if (jResourceUri) + { + resourceUri = env->GetStringUTFChars(jResourceUri, NULL); + } + std::string resourceTypeName; + if (jResourceTypeName) + { + resourceTypeName = env->GetStringUTFChars(jResourceTypeName, NULL); + } + std::string resourceInterface; + if (jResourceInterface) + { + resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "entityHandler cannot be null"); + return nullptr; + } + JniEntityHandler* entityHandler = new JniEntityHandler(env, jListener); + EntityHandler handleEntityCallback = [entityHandler](const std::shared_ptr request) -> + OCEntityHandlerResult{ + return entityHandler->handleEntity(request); + }; + + OCResourceHandle resourceHandle; + try + { + OCStackResult result = OCPlatform::registerResource( + resourceHandle, + resourceUri, + resourceTypeName, + resourceInterface, + handleEntityCallback, + static_cast(jResourceProperty)); + + if (OC_STACK_OK != result) + { + delete entityHandler; + ThrowOcException(result, "register resource"); + return nullptr; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + delete entityHandler; + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + return nullptr; + } + + JniOcResourceHandle* jniHandle = new JniOcResourceHandle(resourceHandle); + jlong handle = reinterpret_cast(jniHandle); + jobject jResourceHandle = env->NewObject(g_cls_OcResourceHandle, g_mid_OcResourceHandle_N_ctor, handle); + if (!jResourceHandle) + { + LOGE("Failed to create OcResourceHandle"); + delete jniHandle; + } + + return jResourceHandle; +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: registerDeviceInfo0 +* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0 +(JNIEnv *env, +jclass clazz, +jstring jDeviceName) +{ + LOGI("OcPlatform_registerDeviceInfo"); + + std::string deviceName; + if (jDeviceName) + { + deviceName = env->GetStringUTFChars(jDeviceName, NULL); + } + + OCDeviceInfo deviceInfo; + try + { + DuplicateString(&deviceInfo.deviceName, deviceName); + } + catch (std::exception &e) + { + ThrowOcException(JNI_EXCEPTION, "Failed to construct device info"); + return; + } + + try + { + OCStackResult result = OCPlatform::registerDeviceInfo(deviceInfo); + + delete deviceInfo.deviceName; + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to register device info"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: registerPlatformInfo0 +* Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0 +(JNIEnv *env, +jclass clazz, +jstring jPlatformID, +jstring jManufacturerName, +jstring jManufacturerUrl, +jstring jModelNumber, +jstring jDateOfManufacture, +jstring jPlatformVersion, +jstring jOperatingSystemVersion, +jstring jHardwareVersion, +jstring jFirmwareVersion, +jstring jSupportUrl, +jstring jSystemTime) +{ + LOGI("OcPlatform_registerPlatformInfo"); + + + std::string platformID; + std::string manufacturerName; + std::string manufacturerUrl; + std::string modelNumber; + std::string dateOfManufacture; + std::string platformVersion; + std::string operatingSystemVersion; + std::string hardwareVersion; + std::string firmwareVersion; + std::string supportUrl; + std::string systemTime; + + if (jPlatformID) + { + platformID = env->GetStringUTFChars(jPlatformID, NULL); + } + if (jManufacturerName) + { + manufacturerName = env->GetStringUTFChars(jManufacturerName, NULL); + } + if (jManufacturerUrl) + { + manufacturerUrl = env->GetStringUTFChars(jManufacturerUrl, NULL); + } + if (jModelNumber) + { + modelNumber = env->GetStringUTFChars(jModelNumber, NULL); + } + if (jDateOfManufacture) + { + dateOfManufacture = env->GetStringUTFChars(jDateOfManufacture, NULL); + } + if (jPlatformVersion) + { + platformVersion = env->GetStringUTFChars(jPlatformVersion, NULL); + } + if (jOperatingSystemVersion) + { + operatingSystemVersion = env->GetStringUTFChars(jOperatingSystemVersion, NULL); + } + if (jHardwareVersion) + { + hardwareVersion = env->GetStringUTFChars(jHardwareVersion, NULL); + } + if (jFirmwareVersion) + { + firmwareVersion = env->GetStringUTFChars(jFirmwareVersion, NULL); + } + if (jSupportUrl) + { + supportUrl = env->GetStringUTFChars(jSupportUrl, NULL); + } + if (jSystemTime) + { + systemTime = env->GetStringUTFChars(jSystemTime, NULL); + } + + OCPlatformInfo platformInfo; + try + { + DuplicateString(&platformInfo.platformID, platformID); + DuplicateString(&platformInfo.manufacturerName, manufacturerName); + DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl); + DuplicateString(&platformInfo.modelNumber, modelNumber); + DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture); + DuplicateString(&platformInfo.platformVersion, platformVersion); + DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion); + DuplicateString(&platformInfo.hardwareVersion, hardwareVersion); + DuplicateString(&platformInfo.firmwareVersion, firmwareVersion); + DuplicateString(&platformInfo.supportUrl, supportUrl); + DuplicateString(&platformInfo.systemTime, systemTime); + } + 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(OC_STACK_ERROR, e.reason().c_str()); + } + + + +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: unregisterResource0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unregisterResource0 +(JNIEnv *env, jclass clazz, jobject jResourceHandle) +{ + LOGI("OcPlatform_unregisterResource"); + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + try + { + OCResourceHandle resHandle = jniOcResourceHandle->getOCResourceHandle(); + OCStackResult result = OCPlatform::unregisterResource(resHandle); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to unregister resource"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: bindResource0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0 +(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobject jResourceHandle) +{ + LOGI("OcPlatform_bindResource"); + if (!jResourceCollectionHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); + return; + } + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceCollectionHandle); + if (!jniOcResourceCollectionHandle) return; + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + try + { + OCStackResult result = OCPlatform::bindResource( + jniOcResourceCollectionHandle->getOCResourceHandle(), + jniOcResourceHandle->getOCResourceHandle() + ); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to bind resource"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: bindResources0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResources0 +(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobjectArray jResourceHandleArray) +{ + LOGI("OcPlatform_bindResources"); + + if (!jResourceCollectionHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); + return; + } + if (!jResourceHandleArray) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null"); + return; + } + + JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceCollectionHandle); + if (!jniOcResourceCollectionHandle) return; + + std::vector resourceHandleList; + int len = env->GetArrayLength(jResourceHandleArray); + for (int 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) return; + + resourceHandleList.push_back( + jniOcResourceHandle->getOCResourceHandle()); + } + + try + { + OCStackResult result = OCPlatform::bindResources( + jniOcResourceCollectionHandle->getOCResourceHandle(), + resourceHandleList + ); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to bind resources"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: unbindResource0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResource0 +(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobject jResourceHandle) +{ + LOGI("OcPlatform_unbindResource"); + if (!jResourceCollectionHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); + return; + } + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + + JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceCollectionHandle); + if (!jniOcResourceCollectionHandle) return; + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + try + { + OCStackResult result = OCPlatform::unbindResource( + jniOcResourceCollectionHandle->getOCResourceHandle(), + jniOcResourceHandle->getOCResourceHandle() + ); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to unbind resource"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: unbindResources0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResources0 +(JNIEnv *env, jclass clazz, jobject jResourceCollectionHandle, jobjectArray jResourceHandleArray) +{ + LOGI("OcPlatform_unbindResources"); + if (!jResourceCollectionHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceCollectionHandle cannot be null"); + return; + } + if (!jResourceHandleArray) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandleList cannot be null"); + return; + } + + JniOcResourceHandle* jniOcResourceCollectionHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceCollectionHandle); + if (!jniOcResourceCollectionHandle) return; + + std::vector resourceHandleList; + int len = env->GetArrayLength(jResourceHandleArray); + for (int 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) return; + + resourceHandleList.push_back( + jniOcResourceHandle->getOCResourceHandle()); + } + + try + { + OCStackResult result = OCPlatform::unbindResources( + jniOcResourceCollectionHandle->getOCResourceHandle(), + resourceHandleList + ); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to unbind resources"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: bindTypeToResource0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0 +(JNIEnv *env, jclass clazz, jobject jResourceHandle, jstring jResourceTypeName) +{ + LOGI("OcPlatform_bindTypeToResource"); + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + std::string typeName; + if (jResourceTypeName) + { + typeName = env->GetStringUTFChars(jResourceTypeName, NULL); + } + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + try + { + OCStackResult result = OCPlatform::bindTypeToResource( + jniOcResourceHandle->getOCResourceHandle(), + typeName + ); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to bind type to resource"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: bindInterfaceToResource0 +* Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource0 +(JNIEnv *env, jclass clazz, jobject jResourceHandle, jstring jResourceInterfaceName) +{ + LOGI("OcPlatform_bindInterfaceToResource"); + if (!jResourceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceHandle cannot be null"); + return; + } + std::string interfaceName; + if (jResourceInterfaceName) + { + interfaceName = env->GetStringUTFChars(jResourceInterfaceName, NULL); + } + + JniOcResourceHandle* jniOcResourceHandle = JniOcResourceHandle::getJniOcResourceHandlePtr( + env, jResourceHandle); + if (!jniOcResourceHandle) return; + + try + { + OCStackResult result = OCPlatform::bindInterfaceToResource( + jniOcResourceHandle->getOCResourceHandle(), + interfaceName + ); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to bind interface to resource"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: startPresence0 +* Signature: (I)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_startPresence0 +(JNIEnv *env, jclass clazz, jint ttl) +{ + LOGI("OcPlatform_startPresence"); + + try + { + OCStackResult result = OCPlatform::startPresence((unsigned int)ttl); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to start presence"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: stopPresence0 +* Signature: ()V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stopPresence0 +(JNIEnv *env, jclass clazz) +{ + LOGI("OcPlatform_stopPresence"); + + try + { + OCStackResult result = OCPlatform::stopPresence(); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to stop presence"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: subscribePresence0 +* Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; +*/ +JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0 +(JNIEnv *env, jclass clazz, jstring jHost, jint jConnectivityType, jobject jListener) +{ + LOGD("OcPlatform_subscribePresence"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onPresenceListener cannot be null"); + return nullptr; + } + + JniOnPresenceListener *onPresenceListener = AddOnPresenceListener(env, jListener); + + SubscribeCallback subscribeCallback = [onPresenceListener](OCStackResult result, const unsigned int nonce, + const std::string& hostAddress) + { + onPresenceListener->onPresenceCallback(result, nonce, hostAddress); + }; + + OCPlatform::OCPresenceHandle presenceHandle; + try + { + OCStackResult result = OCPlatform::subscribePresence( + presenceHandle, + host, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + subscribeCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "subscribe presence has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + return nullptr; + } + + JniOcPresenceHandle* jniPresenceHandle = new JniOcPresenceHandle(onPresenceListener, 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; +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: subscribePresence1 +* Signature: (Ljava/lang/String;Ljava/lang/String;I +Lorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; +*/ +JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1 +(JNIEnv *env, jclass clazz, jstring jHost, jstring jResourceType, jint jConnectivityType, jobject jListener) +{ + LOGD("OcPlatform_subscribePresence1"); + std::string host; + if (jHost) + { + host = env->GetStringUTFChars(jHost, NULL); + } + std::string resourceType; + if (jResourceType) + { + resourceType = env->GetStringUTFChars(jResourceType, NULL); + } + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onPresenceListener cannot be null"); + return nullptr; + } + + JniOnPresenceListener *onPresenceListener = AddOnPresenceListener(env, jListener); + + SubscribeCallback subscribeCallback = [onPresenceListener](OCStackResult result, + const unsigned int nonce, const std::string& hostAddress) + { + onPresenceListener->onPresenceCallback(result, nonce, hostAddress); + }; + + OCPlatform::OCPresenceHandle presenceHandle; + try + { + OCStackResult result = OCPlatform::subscribePresence( + presenceHandle, + host, + resourceType, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + subscribeCallback); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "subscribe presence has failed"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + return nullptr; + } + + JniOcPresenceHandle* jniPresenceHandle = new JniOcPresenceHandle(onPresenceListener, 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; +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: unsubscribePresence0 +* Signature: (Lorg/iotivity/base/OcPresenceHandle;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0 +(JNIEnv *env, jclass clazz, jobject jPresenceHandle) +{ + LOGD("OcPlatform_unsubscribePresence"); + if (!jPresenceHandle) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "presenceHandle cannot be null"); + return; + } + JniOcPresenceHandle* jniPresenceHandle = JniOcPresenceHandle::getJniOcPresenceHandlePtr(env, jPresenceHandle); + if (!jniPresenceHandle) return; + + OCPresenceHandle presenceHandle = jniPresenceHandle->getOCPresenceHandle(); + + try + { + OCStackResult result = OCPlatform::unsubscribePresence(presenceHandle); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "unsubscribe presence has failed"); + return; + } + jweak jwOnPresenceListener = jniPresenceHandle->getJniOnPresenceListener()->getJWListener(); + if (jwOnPresenceListener) + { + RemoveOnPresenceListener(env, jwOnPresenceListener); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} + +/* +* 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, NULL); + } + std::string uri; + if (jUri) + { + uri = env->GetStringUTFChars(jUri, NULL); + } + if (!jResourceTypeArray) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceTypeList cannot be null"); + return nullptr; + } + if (!jInterfaceArray) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "interfaceList cannot be null"); + return nullptr; + } + + std::vector resourceTypes; + JniUtils::convertJavaStrArrToStrVector(env, jResourceTypeArray, resourceTypes); + + std::vector interfaces; + JniUtils::convertJavaStrArrToStrVector(env, jInterfaceArray, interfaces); + + std::shared_ptr resource = OCPlatform::constructResourceObject( + host, + uri, + JniUtils::getConnectivityType(env, static_cast(jConnectivityType)), + static_cast(jIsObservable), + resourceTypes, + interfaces); + + if (!resource) + { + ThrowOcException(OC_STACK_ERROR, "Failed to create OCResource"); + return nullptr; + } + + JniOcResource *jniOcResource = new JniOcResource(resource); + jlong handle = reinterpret_cast(jniOcResource); + + jobject jResource = env->NewObject(g_cls_OcResource, g_mid_OcResource_ctor); + if (!jResource) + { + delete jniOcResource; + return nullptr; + } + SetHandle(env, jResource, jniOcResource); + if (env->ExceptionCheck()) + { + delete jniOcResource; + return nullptr; + } + return jResource; +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: sendResponse0 +* Signature: (Lorg/iotivity/base/OcResourceResponse;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0 +(JNIEnv *env, jclass clazz, jobject jResourceResponse) +{ + LOGD("OcPlatform_sendResponse"); + if (!jResourceResponse) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "resourceResponse cannot be null"); + return; + } + + JniOcResourceResponse *jniResponse = JniOcResourceResponse::getJniOcResourceResponsePtr( + env, jResourceResponse); + if (!jniResponse) return; + + try + { + OCStackResult result = OCPlatform::sendResponse(jniResponse->getOCResourceResponse()); + + if (OC_STACK_OK != result) + { + ThrowOcException(result, "failed to send response"); + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(OC_STACK_ERROR, e.reason().c_str()); + } +} diff --git a/android/android_api/base/jni/JniOcPlatform.h b/android/android_api/base/jni/JniOcPlatform.h index 1019147..62c9938 100644 --- a/android/android_api/base/jni/JniOcPlatform.h +++ b/android/android_api/base/jni/JniOcPlatform.h @@ -1,264 +1,295 @@ -/* -* //****************************************************************** -* // -* // Copyright 2015 Intel Corporation. -* // -* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -* // -* // 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 "JniOcStack.h" -#include "JniOnResourceFoundListener.h" -#include "JniOnDeviceInfoListener.h" -#include "JniOnPresenceListener.h" -#include - -#ifndef _Included_org_iotivity_base_OcPlatform -#define _Included_org_iotivity_base_OcPlatform - -using namespace OC; - -JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener); -void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener); - -JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener); -void RemoveOnDeviceInfoListener(JNIEnv* env, jobject jListener); - -JniOnPresenceListener* AddOnPresenceListener(JNIEnv* env, jobject jListener); -void RemoveOnPresenceListener(JNIEnv* env, jobject jListener); - -std::map> onResourceFoundListenerMap; -std::map> onDeviceInfoListenerMap; -std::map> onPresenceListenerMap; - -std::mutex resourceFoundMapLock; -std::mutex deviceInfoMapLock; -std::mutex presenceMapLock; - -#ifdef __cplusplus -extern "C" { -#endif - /* - * Class: org_iotivity_base_OcPlatform - * Method: configure - * Signature: (IILjava/lang/String;II)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure - (JNIEnv *, jclass, jint, jint, jstring, jint, jint); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: notifyAllObservers0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0 - (JNIEnv *, jclass, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: notifyAllObservers1 - * Signature: (Lorg/iotivity/base/OcResourceHandle;I)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1 - (JNIEnv *, jclass, jobject, jint); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: notifyListOfObservers2 - * Signature: (Lorg/iotivity/base/OcResourceHandle;[BLorg/iotivity/base/OcResourceResponse;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers2 - (JNIEnv *, jclass, jobject, jbyteArray, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: notifyListOfObservers3 - * Signature: (Lorg/iotivity/base/OcResourceHandle;[BLorg/iotivity/base/OcResourceResponse;I)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers3 - (JNIEnv *, jclass, jobject, jbyteArray, jobject, jint); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: findResource0 - * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0 - (JNIEnv *, jclass, jstring, jstring, jint, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: findResource1 - * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;I)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1 - (JNIEnv *, jclass, jstring, jstring, jint, jobject, jint); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: getDeviceInfo0 - * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0 - (JNIEnv *, jclass, jstring, jstring, jint, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: getDeviceInfo1 - * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;I)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1 - (JNIEnv *, jclass, jstring, jstring, jint, jobject, jint); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: registerResource0 - * Signature: (Lorg/iotivity/base/OcResource;)Lorg/iotivity/base/OcResourceHandle; - */ - JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0 - (JNIEnv *, jclass, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: registerResource1 - * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcPlatform/EntityHandler;I)Lorg/iotivity/base/OcResourceHandle; - */ - JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource1 - (JNIEnv *, jclass, jstring, jstring, jstring, jobject, jint); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: registerDeviceInfo0 - * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0 - (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: unregisterResource0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unregisterResource0 - (JNIEnv *, jclass, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: bindResource0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0 - (JNIEnv *, jclass, jobject, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: bindResources0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResources0 - (JNIEnv *, jclass, jobject, jobjectArray); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: unbindResource0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResource0 - (JNIEnv *, jclass, jobject, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: unbindResources0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResources0 - (JNIEnv *, jclass, jobject, jobjectArray); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: bindTypeToResource0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0 - (JNIEnv *, jclass, jobject, jstring); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: bindInterfaceToResource0 - * Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource0 - (JNIEnv *, jclass, jobject, jstring); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: startPresence0 - * Signature: (I)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_startPresence0 - (JNIEnv *, jclass, jint); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: stopPresence0 - * Signature: ()V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stopPresence0 - (JNIEnv *, jclass); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: subscribePresence0 - * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; - */ - JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0 - (JNIEnv *, jclass, jstring, jint, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: subscribePresence1 - * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; - */ - JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1 - (JNIEnv *, jclass, jstring, jstring, jint, jobject); - - /* - * Class: org_iotivity_base_OcPlatform - * Method: unsubscribePresence0 - * Signature: (Lorg/iotivity/base/OcPresenceHandle;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0 - (JNIEnv *, jclass, jobject); - - /* - * 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 *, jclass, jstring, jstring, jint, jboolean, jobjectArray, jobjectArray); - - /* - * Class: org_iotivity_base_OcPlatform0 - * Method: sendResponse0 - * Signature: (Lorg/iotivity/base/OcResourceResponse;)V - */ - JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0 - (JNIEnv *, jclass, jobject); - -#ifdef __cplusplus -} -#endif -#endif +/* +* //****************************************************************** +* // +* // Copyright 2015 Intel Corporation. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // 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 "JniOcStack.h" +#include "JniOnResourceFoundListener.h" +#include "JniOnDeviceInfoListener.h" +#include "JniOnPlatformInfoListener.h" +#include "JniOnPresenceListener.h" +#include + +#ifndef _Included_org_iotivity_base_OcPlatform +#define _Included_org_iotivity_base_OcPlatform + +using namespace OC; + +JniOnResourceFoundListener* AddOnResourceFoundListener(JNIEnv* env, jobject jListener); +void RemoveOnResourceFoundListener(JNIEnv* env, jobject jListener); + +JniOnDeviceInfoListener* AddOnDeviceInfoListener(JNIEnv* env, jobject jListener); +void RemoveOnDeviceInfoListener(JNIEnv* env, jobject jListener); + +JniOnPlatformInfoListener* AddOnPlatformInfoListener(JNIEnv* env, jobject jListener); +void RemoveOnPlatformInfoListener(JNIEnv* env, jobject jListener); + +JniOnPresenceListener* AddOnPresenceListener(JNIEnv* env, jobject jListener); +void RemoveOnPresenceListener(JNIEnv* env, jobject jListener); + +std::map> onResourceFoundListenerMap; +std::map> onDeviceInfoListenerMap; +std::map> onPlatformInfoListenerMap; +std::map> onPresenceListenerMap; + +std::mutex resourceFoundMapLock; +std::mutex deviceInfoMapLock; +std::mutex platformInfoMapLock; +std::mutex presenceMapLock; + +#ifdef __cplusplus +extern "C" { +#endif + /* + * Class: org_iotivity_base_OcPlatform + * Method: configure + * Signature: (IILjava/lang/String;II)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_configure + (JNIEnv *, jclass, jint, jint, jstring, jint, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: notifyAllObservers0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers0 + (JNIEnv *, jclass, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: notifyAllObservers1 + * Signature: (Lorg/iotivity/base/OcResourceHandle;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyAllObservers1 + (JNIEnv *, jclass, jobject, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: notifyListOfObservers2 + * Signature: (Lorg/iotivity/base/OcResourceHandle;[BLorg/iotivity/base/OcResourceResponse;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers2 + (JNIEnv *, jclass, jobject, jbyteArray, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: notifyListOfObservers3 + * Signature: (Lorg/iotivity/base/OcResourceHandle;[BLorg/iotivity/base/OcResourceResponse;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_notifyListOfObservers3 + (JNIEnv *, jclass, jobject, jbyteArray, jobject, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: findResource0 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource0 + (JNIEnv *, jclass, jstring, jstring, jint, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: findResource1 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnResourceFoundListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1 + (JNIEnv *, jclass, jstring, jstring, jint, jobject, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: getDeviceInfo0 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo0 + (JNIEnv *, jclass, jstring, jstring, jint, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: getDeviceInfo1 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDeviceInfo1 + (JNIEnv *, jclass, jstring, jstring, jint, jobject, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: getPlatformInfo0 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPlatformFoundListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo0 + (JNIEnv *, jclass, jstring, jstring, jint, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: getPlatformInfo1 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPlatformFoundListener;I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getPlatformInfo1 + (JNIEnv *, jclass, jstring, jstring, jint, jobject, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: registerResource0 + * Signature: (Lorg/iotivity/base/OcResource;)Lorg/iotivity/base/OcResourceHandle; + */ + JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource0 + (JNIEnv *, jclass, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: registerResource1 + * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Lorg/iotivity/base/OcPlatform/EntityHandler;I)Lorg/iotivity/base/OcResourceHandle; + */ + JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_registerResource1 + (JNIEnv *, jclass, jstring, jstring, jstring, jobject, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: registerDeviceInfo0 + * Signature: (Ljava/lang/String;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0 + (JNIEnv *, jclass, jstring); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: registerPlatformInfo0 + * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerPlatformInfo0 + (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring, jstring); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: unregisterResource0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unregisterResource0 + (JNIEnv *, jclass, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: bindResource0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResource0 + (JNIEnv *, jclass, jobject, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: bindResources0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindResources0 + (JNIEnv *, jclass, jobject, jobjectArray); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: unbindResource0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;Lorg/iotivity/base/OcResourceHandle;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResource0 + (JNIEnv *, jclass, jobject, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: unbindResources0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;[Lorg/iotivity/base/OcResourceHandle;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unbindResources0 + (JNIEnv *, jclass, jobject, jobjectArray); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: bindTypeToResource0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindTypeToResource0 + (JNIEnv *, jclass, jobject, jstring); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: bindInterfaceToResource0 + * Signature: (Lorg/iotivity/base/OcResourceHandle;Ljava/lang/String;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_bindInterfaceToResource0 + (JNIEnv *, jclass, jobject, jstring); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: startPresence0 + * Signature: (I)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_startPresence0 + (JNIEnv *, jclass, jint); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: stopPresence0 + * Signature: ()V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_stopPresence0 + (JNIEnv *, jclass); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: subscribePresence0 + * Signature: (Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; + */ + JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence0 + (JNIEnv *, jclass, jstring, jint, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: subscribePresence1 + * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnPresenceListener;)Lorg/iotivity/base/OcPresenceHandle; + */ + JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_subscribePresence1 + (JNIEnv *, jclass, jstring, jstring, jint, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: unsubscribePresence0 + * Signature: (Lorg/iotivity/base/OcPresenceHandle;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_unsubscribePresence0 + (JNIEnv *, jclass, jobject); + + /* + * 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 *, jclass, jstring, jstring, jint, jboolean, jobjectArray, jobjectArray); + + /* + * Class: org_iotivity_base_OcPlatform0 + * Method: sendResponse0 + * Signature: (Lorg/iotivity/base/OcResourceResponse;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0 + (JNIEnv *, jclass, jobject); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/android/android_api/base/jni/JniOnPlatformInfoListener.cpp b/android/android_api/base/jni/JniOnPlatformInfoListener.cpp new file mode 100644 index 0000000..27ecf74 --- /dev/null +++ b/android/android_api/base/jni/JniOnPlatformInfoListener.cpp @@ -0,0 +1,114 @@ +/* +* //****************************************************************** +* // +* // Copyright 2015 Intel Corporation. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // 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 "JniOnPlatformInfoListener.h" +#include "JniOcRepresentation.h" + +JniOnPlatformInfoListener::JniOnPlatformInfoListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); + m_removeListenerCallback = removeListenerCallback; +} + +JniOnPlatformInfoListener::~JniOnPlatformInfoListener() +{ + LOGI("~JniOnPlatformInfoListener"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniOnPlatformInfoListener::foundPlatformCallback(const OC::OCRepresentation& ocRepresentation) +{ + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (NULL == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + LOGI("Java onPlatformInfoListener object is already destroyed, quiting"); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + OCRepresentation* rep = new OCRepresentation(ocRepresentation); + jlong handle = reinterpret_cast(rep); + jobject jRepresentation = env->NewObject(g_cls_OcRepresentation, g_mid_OcRepresentation_N_ctor_bool, + handle, true); + if (!jRepresentation) + { + delete rep; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jclass clsL = env->GetObjectClass(jListener); + if (!clsL) + { + delete rep; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + jmethodID midL = env->GetMethodID(clsL, "onPlatformFound", "(Lorg/iotivity/base/OcRepresentation;)V"); + if (!midL) + { + delete rep; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + env->CallVoidMethod(jListener, midL, jRepresentation); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete rep; + checkExAndRemoveListener(env); + } + + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); +} + +void JniOnPlatformInfoListener::checkExAndRemoveListener(JNIEnv* env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_removeListenerCallback(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_removeListenerCallback(env, m_jwListener); + } +} + diff --git a/android/android_api/base/jni/JniOnPlatformInfoListener.h b/android/android_api/base/jni/JniOnPlatformInfoListener.h new file mode 100644 index 0000000..ebc3c09 --- /dev/null +++ b/android/android_api/base/jni/JniOnPlatformInfoListener.h @@ -0,0 +1,42 @@ +/* +* //****************************************************************** +* // +* // Copyright 2015 Intel Corporation. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // 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 "JniOcStack.h" + +#ifndef _Included_org_iotivity_base_OcPlatform_OnPlatformFoundListener +#define _Included_org_iotivity_base_OcPlatform_OnPlatformFoundListener + +class JniOnPlatformInfoListener +{ +public: + JniOnPlatformInfoListener(JNIEnv *env, jobject jListener, RemoveListenerCallback removeListener); + ~JniOnPlatformInfoListener(); + + void foundPlatformCallback(const OC::OCRepresentation& ocRepresentation); + +private: + jweak m_jwListener; + RemoveListenerCallback m_removeListenerCallback; + void checkExAndRemoveListener(JNIEnv* env); +}; + +#endif diff --git a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java index 31a1a1d..596d2ca 100644 --- a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java +++ b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java @@ -984,6 +984,56 @@ public class SmokeTest extends InstrumentationTestCase { } } + public void testPlatformInfo() throws InterruptedException { + final String resourceType = "unit.test.resource" + new Date().getTime(); + final CountDownLatch signal = new CountDownLatch(1); + + OcPlatform.OnPlatformFoundListener platformFoundListener = new OcPlatform.OnPlatformFoundListener() { + @Override + public void onPlatformFound(OcRepresentation ocRepresentation) { + Log.i(TAG, "Platform Info Received: "); + Log.i(TAG, "URI: " + ocRepresentation.getUri()); + signal.countDown(); + } + }; + + OcPlatformInfo platformInfo = null; + try { + platformInfo = new OcPlatformInfo("myPlatformID", "myManuName", "myManuUrl"); + } catch (OcException e) { + Log.e(TAG, "Could not construct platformInfo. " + e.getMessage()); + assertTrue(false); + } + + platformInfo.setModelNumber("myModelNumber"); + platformInfo.setDateOfManufacture("myDateOfManufacture"); + platformInfo.setPlatformVersion("myPlatformVersion"); + platformInfo.setOperatingSystemVersion("myOperatingSystemVersion"); + platformInfo.setHardwareVersion("myHardwareVersion"); + platformInfo.setFirmwareVersion("myFirmwareVersion"); + platformInfo.setSupportUrl("mySupportUrl"); + platformInfo.setSystemTime("mySystemTime"); + + try { + //server + + OcPlatform.registerPlatformInfo(platformInfo); + + //client + OcPlatform.getPlatformInfo( + "", + OcPlatform.MULTICAST_PREFIX + "/oic/p", + OcConnectivityType.IPV4, + platformFoundListener); + + //wait for onPlatformFound event + assertTrue(signal.await(60, TimeUnit.SECONDS)); + } catch (OcException e) { + Log.e(TAG, e.getMessage() + platformInfo.toString()); + assertTrue(false); + } + } + // public void testRegisterDeviceInfoGetDeviceInfo() throws InterruptedException { // final String resourceType = "unit.test.resource" + new Date().getTime(); // final CountDownLatch signal = new CountDownLatch(1); diff --git a/android/android_api/base/src/main/java/org/iotivity/base/ErrorCode.java b/android/android_api/base/src/main/java/org/iotivity/base/ErrorCode.java index d1862e0..b83e4b3 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/ErrorCode.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/ErrorCode.java @@ -1,95 +1,103 @@ -/* - * //****************************************************************** - * // - * // Copyright 2015 Intel Corporation. - * // - * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - * // - * // 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. - * // - * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - */ - -package org.iotivity.base; - -public enum ErrorCode { - /* Success status code - START HERE */ - OK("OK", ""), - RESOURCE_CREATED("RESOURCE_CREATED", ""), - RESOURCE_DELETED("RESOURCE_DELETED", ""), - CONTINUE("CONTINUE", ""), - /* Success status code - END HERE */ - /* Error status code - START HERE */ - INVALID_URI("INVALID_URI", ""), - INVALID_QUERY("INVALID_QUERY", ""), - INVALID_IP("INVALID_IP", ""), - INVALID_PORT("INVALID_PORT", ""), - INVALID_CALLBACK("INVALID_CALLBACK", ""), - INVALID_METHOD("INVALID_METHOD", ""), - INVALID_PARAM("INVALID_PARAM", ""), - INVALID_OBSERVE_PARAM("INVALID_OBSERVE_PARAM", ""), - NO_MEMORY("NO_MEMORY", ""), - COMM_ERROR("COMM_ERROR", ""), - NOT_IMPL("NOTIMPL", ""), - NO_RESOURCE("NO_RESOURCE", "Resource not found"), - RESOURCE_ERROR("RESOURCE_ERROR", "Not supported method or interface"), - SLOW_RESOURCE("SLOW_RESOURCE", ""), - NO_OBSERVERS("NO_OBSERVERS", "Resource has no registered observers"), - OBSERVER_NOT_FOUND("OBSERVER_NOT_FOUND", ""), - PRESENCE_STOPPED("PRESENCE_STOPPED", ""), - PRESENCE_TIMEOUT("PRESENCE_TIMEOUT", ""), - PRESENCE_DO_NOT_HANDLE("PRESENCE_DO_NOT_HANDLE", ""), - VIRTUAL_DO_NOT_HANDLE("VIRTUAL_DO_NOT_HANDLE", ""), - INVALID_OPTION("INVALID_OPTION", ""), - MALFORMED_RESPONSE("MALFORMED_RESPONSE", "Remote reply contained malformed data"), - PERSISTENT_BUFFER_REQUIRED("PERSISTENT_BUFFER_REQUIRED", ""), - INVALID_REQUEST_HANDLE("INVALID_REQUEST_HANDLE", ""), - INVALID_DEVICE_INFO("INVALID_DEVICE_INFO", ""), - ERROR("ERROR", "Generic error"), - - JNI_EXCEPTION("JNI_EXCEPTION", "Generic Java binder error"), - JNI_NO_NATIVE_OBJECT("JNI_NO_NATIVE_OBJECT", ""), - JNI_INVALID_VALUE("JNI_INVALID_VALUE", ""), - - INVALID_CLASS_CAST("INVALID_CLASS_CAST", ""),; - - private String error; - private String description; - - private ErrorCode(String error, String description) { - this.error = error; - this.description = description; - } - - public String getError() { - return error; - } - - public String getDescription() { - return description; - } - - public static ErrorCode get(String errorCode) { - for (ErrorCode eCode : ErrorCode.values()) { - if (eCode.getError().equals(errorCode)) { - return eCode; - } - } - throw new IllegalArgumentException("Unexpected ErrorCode value"); - } - - @Override - public String toString() { - return error + (description.isEmpty() ? "" : " : " + description); - } -} +/* + * //****************************************************************** + * // + * // Copyright 2015 Intel Corporation. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +public enum ErrorCode { + /* Success status code - START HERE */ + OK("OK", ""), + RESOURCE_CREATED("RESOURCE_CREATED", ""), + RESOURCE_DELETED("RESOURCE_DELETED", ""), + CONTINUE("CONTINUE", ""), + /* Success status code - END HERE */ + /* Error status code - START HERE */ + INVALID_URI("INVALID_URI", ""), + INVALID_QUERY("INVALID_QUERY", ""), + INVALID_IP("INVALID_IP", ""), + INVALID_PORT("INVALID_PORT", ""), + INVALID_CALLBACK("INVALID_CALLBACK", ""), + INVALID_METHOD("INVALID_METHOD", ""), + INVALID_PARAM("INVALID_PARAM", ""), + INVALID_OBSERVE_PARAM("INVALID_OBSERVE_PARAM", ""), + NO_MEMORY("NO_MEMORY", ""), + COMM_ERROR("COMM_ERROR", ""), + NOT_IMPL("NOTIMPL", ""), + NO_RESOURCE("NO_RESOURCE", "Resource not found"), + RESOURCE_ERROR("RESOURCE_ERROR", "Not supported method or interface"), + SLOW_RESOURCE("SLOW_RESOURCE", ""), + NO_OBSERVERS("NO_OBSERVERS", "Resource has no registered observers"), + OBSERVER_NOT_FOUND("OBSERVER_NOT_FOUND", ""), + PRESENCE_STOPPED("PRESENCE_STOPPED", ""), + PRESENCE_TIMEOUT("PRESENCE_TIMEOUT", ""), + PRESENCE_DO_NOT_HANDLE("PRESENCE_DO_NOT_HANDLE", ""), + VIRTUAL_DO_NOT_HANDLE("VIRTUAL_DO_NOT_HANDLE", ""), + INVALID_OPTION("INVALID_OPTION", ""), + MALFORMED_RESPONSE("MALFORMED_RESPONSE", "Remote reply contained malformed data"), + PERSISTENT_BUFFER_REQUIRED("PERSISTENT_BUFFER_REQUIRED", ""), + INVALID_REQUEST_HANDLE("INVALID_REQUEST_HANDLE", ""), + INVALID_DEVICE_INFO("INVALID_DEVICE_INFO", ""), + INVALID_PLATFORM_INFO_PLATFORMID("INVALID_PLATFORM_INFO_PLATFORMID", + "PlatformID cannot be null or empty"), + INVALID_PLATFORM_INFO_MANUFACTURER_NAME("INVALID_PLATFORM_INFO_MANUFACTURER_NAME", + "ManufacturerName cannot be null, empty or greater than " + + OcStackConfig.MAX_MANUFACTURER_NAME_LENGTH + " characters long"), + INVALID_PLATFORM_INFO_PLATFORMID_MANUFACTURER_URL("INVALID_PLATFORM_INFO_MANUFACTURER_URL", + "MANUFACTURER_URL cannot be null, empty or greater than " + + OcStackConfig.MAX_MANUFACTURER_URL_LENGTH + " characters long"), + ERROR("ERROR", "Generic error"), + + JNI_EXCEPTION("JNI_EXCEPTION", "Generic Java binder error"), + JNI_NO_NATIVE_OBJECT("JNI_NO_NATIVE_OBJECT", ""), + JNI_INVALID_VALUE("JNI_INVALID_VALUE", ""), + + INVALID_CLASS_CAST("INVALID_CLASS_CAST", ""),; + + private String error; + private String description; + + private ErrorCode(String error, String description) { + this.error = error; + this.description = description; + } + + public String getError() { + return error; + } + + public String getDescription() { + return description; + } + + public static ErrorCode get(String errorCode) { + for (ErrorCode eCode : ErrorCode.values()) { + if (eCode.getError().equals(errorCode)) { + return eCode; + } + } + throw new IllegalArgumentException("Unexpected ErrorCode value"); + } + + @Override + public String toString() { + return error + (description.isEmpty() ? "" : " : " + description); + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java index ac17a5b..891d487 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java @@ -1,757 +1,862 @@ -/* - * //****************************************************************** - * // - * // Copyright 2015 Intel Corporation. - * // - * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - * // - * // 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. - * // - * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - */ - -package org.iotivity.base; - -import org.iotivity.ca.CaInterface; - -import java.util.EnumSet; -import java.util.Iterator; -import java.util.List; - -/** - * Contains the main entrance/functionality of the product. To set a custom configuration, the - * implementer must make a call to OcPlatform.Configure before the first usage of a function in this - * class. - */ -public final class OcPlatform { - - static { - System.loadLibrary("oc_logger"); - System.loadLibrary("octbstack"); - System.loadLibrary("connectivity_abstraction"); - System.loadLibrary("oc"); - System.loadLibrary("ocstack-jni"); - } - - /** - * Default interface - */ - public static final String DEFAULT_INTERFACE = "oic.if.baseline"; - - /** - * Used in discovering (GET) links to other resources of a collection - */ - public static final String LINK_INTERFACE = "oic.if.ll"; - - /** - * Used in GET, PUT, POST, DELETE methods on links to other resources of a collection - */ - public static final String BATCH_INTERFACE = "oic.if.b"; - - /** - * Used in GET, PUT, POST methods on links to other remote resources of a group - */ - public static final String GROUP_INTERFACE = "oic.mi.grp"; - - public static final String WELL_KNOWN_QUERY = "224.0.1.187:5683/oic/res"; - public static final String MULTICAST_PREFIX = "224.0.1.187:5683"; - public static final String MULTICAST_IP = "224.0.1.187"; - public static final int MULTICAST_PORT = 5683; - public static final int DEFAULT_PRESENCE_TTL = 60; - public static final String DEVICE_URI = "/oic/d"; - public static final String PRESENCE_URI = "/oic/ad"; - - private static volatile boolean sIsPlatformInitialized = false; - - private OcPlatform() { - } - - /** - * API for setting the configuration of the OcPlatform. - * Note: Any calls made to this AFTER the first call to OcPlatform.Configure will have no affect - * - * @param platformConfig platform configuration - */ - public synchronized static void Configure(PlatformConfig platformConfig) { - if (!sIsPlatformInitialized) { - CaInterface.initialize(platformConfig.getContext()); - - OcPlatform.configure( - platformConfig.getServiceType().getValue(), - platformConfig.getModeType().getValue(), - platformConfig.getIpAddress(), - platformConfig.getPort(), - platformConfig.getQualityOfService().getValue() - ); - - sIsPlatformInitialized = true; - } - } - - private static native void configure(int serviceType, - int modeType, - String ipAddress, - int port, - int qualityOfService); - - /** - * API for notifying base that resource's attributes have changed. - * - * @param ocResourceHandle resource handle of the resource - * @throws OcException - */ - public static void notifyAllObservers( - OcResourceHandle ocResourceHandle) throws OcException { - OcPlatform.initCheck(); - OcPlatform.notifyAllObservers0(ocResourceHandle); - } - - private static native void notifyAllObservers0( - OcResourceHandle ocResourceHandle) throws OcException; - - /** - * API for notifying base that resource's attributes have changed. - * - * @param ocResourceHandle resource handle of the resource - * @param qualityOfService the quality of communication - * @throws OcException - */ - public static void notifyAllObservers( - OcResourceHandle ocResourceHandle, - QualityOfService qualityOfService) throws OcException { - OcPlatform.initCheck(); - OcPlatform.notifyAllObservers1(ocResourceHandle, qualityOfService.getValue()); - } - - private static native void notifyAllObservers1( - OcResourceHandle ocResourceHandle, - int qualityOfService) throws OcException; - - /** - * API for notifying only specific clients that resource's attributes have changed. - * - * @param ocResourceHandle resource handle of the resource - * @param ocObservationIdList These set of ids are ones which which will be notified upon - * resource change. - * @param ocResourceResponse OcResourceResponse object used by app to fill the response for - * this resource change - * @throws OcException - */ - public static void notifyListOfObservers( - OcResourceHandle ocResourceHandle, - List ocObservationIdList, - OcResourceResponse ocResourceResponse) throws OcException { - OcPlatform.initCheck(); - - byte[] idArr = new byte[ocObservationIdList.size()]; - Iterator it = ocObservationIdList.iterator(); - int i = 0; - while (it.hasNext()) { - idArr[i++] = (byte) it.next(); - } - - OcPlatform.notifyListOfObservers2( - ocResourceHandle, - idArr, - ocResourceResponse); - } - - private static native void notifyListOfObservers2( - OcResourceHandle ocResourceHandle, - byte[] ocObservationIdArray, - OcResourceResponse ocResourceResponse) throws OcException; - - /** - * API for notifying only specific clients that resource's attributes have changed. - * - * @param ocResourceHandle resource handle of the resource - * @param ocObservationIdList These set of ids are ones which which will be notified upon - * resource change. - * @param ocResourceResponse OcResourceResponse object used by app to fill the response for - * this resource change - * @param qualityOfService the quality of communication - * @throws OcException - */ - public static void notifyListOfObservers( - OcResourceHandle ocResourceHandle, - List ocObservationIdList, - OcResourceResponse ocResourceResponse, - QualityOfService qualityOfService) throws OcException { - OcPlatform.initCheck(); - - byte[] idArr = new byte[ocObservationIdList.size()]; - Iterator it = ocObservationIdList.iterator(); - int i = 0; - while (it.hasNext()) { - idArr[i++] = (byte) it.next(); - } - - OcPlatform.notifyListOfObservers3( - ocResourceHandle, - idArr, - ocResourceResponse, - qualityOfService.getValue() - ); - } - - private static native void notifyListOfObservers3( - OcResourceHandle ocResourceHandle, - byte[] ocObservationIdArray, - OcResourceResponse ocResourceResponse, - int qualityOfService) throws OcException; - - /** - * API for Service and Resource Discovery. NOTE: This API applies to client side only - * - * @param host Host IP Address of a service to direct resource discovery query. - * If empty, performs multicast resource discovery query - * @param resourceUri name of the resource. If null or empty, performs search for all - * resource names - * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, - * IPV6, ALL - * @param onResourceFoundListener Handles events, success states and failure states. - * @throws OcException - */ - public static void findResource( - String host, - String resourceUri, - OcConnectivityType connectivityType, - OnResourceFoundListener onResourceFoundListener) throws OcException { - OcPlatform.initCheck(); - OcPlatform.findResource0( - host, - resourceUri, - connectivityType.getValue(), - onResourceFoundListener - ); - } - - private static native void findResource0( - String host, - String resourceUri, - int connectivityType, - OnResourceFoundListener onResourceFoundListener) throws OcException; - - /** - * API for Service and Resource Discovery. NOTE: This API applies to client side only - * - * @param host Host IP Address of a service to direct resource discovery query. - * If empty, performs multicast resource discovery query - * @param resourceUri name of the resource. If null or empty, performs search for all - * resource names - * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, - * IPV6, ALL - * @param onResourceFoundListener Handles events, success states and failure states. - * @param qualityOfService the quality of communication - * @throws OcException - */ - public static void findResource( - String host, - String resourceUri, - OcConnectivityType connectivityType, - OnResourceFoundListener onResourceFoundListener, - QualityOfService qualityOfService) throws OcException { - OcPlatform.initCheck(); - OcPlatform.findResource1(host, - resourceUri, - connectivityType.getValue(), - onResourceFoundListener, - qualityOfService.getValue() - ); - } - - private static native void findResource1( - String host, - String resourceUri, - int connectivityType, - OnResourceFoundListener onResourceFoundListener, - int qualityOfService) throws OcException; - - /** - * API for Device Discovery - * - * @param host Host IP Address. If null or empty, Multicast is performed. - * @param deviceUri Uri containing address to the virtual device - * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, - * IPV6, ALL - * @param onDeviceFoundListener Handles events, success states and failure states. - * @throws OcException - */ - public static void getDeviceInfo( - String host, - String deviceUri, - OcConnectivityType connectivityType, - OnDeviceFoundListener onDeviceFoundListener) throws OcException { - OcPlatform.initCheck(); - OcPlatform.getDeviceInfo0( - host, - deviceUri, - connectivityType.getValue(), - onDeviceFoundListener - ); - } - - private static native void getDeviceInfo0( - String host, - String deviceUri, - int connectivityType, - OnDeviceFoundListener onDeviceFoundListener) throws OcException; - - /** - * API for Device Discovery - * - * @param host Host IP Address. If null or empty, Multicast is performed. - * @param deviceUri Uri containing address to the virtual device - * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, - * IPV6, ALL - * @param onDeviceFoundListener Handles events, success states and failure states. - * @param qualityOfService the quality of communication - * @throws OcException - */ - public static void getDeviceInfo( - String host, - String deviceUri, - OcConnectivityType connectivityType, - OnDeviceFoundListener onDeviceFoundListener, - QualityOfService qualityOfService) throws OcException { - OcPlatform.initCheck(); - OcPlatform.getDeviceInfo1( - host, - deviceUri, - connectivityType.getValue(), - onDeviceFoundListener, - qualityOfService.getValue() - ); - } - - private static native void getDeviceInfo1( - String host, - String deviceUri, - int connectivityType, - OnDeviceFoundListener onDeviceFoundListener, - int qualityOfService) throws OcException; - - /** - * This API registers a resource with the server NOTE: This API applies to server side only. - * - * @param ocResource The instance of OcResource with all data filled - * @return resource handle - * @throws OcException - */ - public static OcResourceHandle registerResource( - OcResource ocResource) throws OcException { - OcPlatform.initCheck(); - return OcPlatform.registerResource0(ocResource); - } - - private static native OcResourceHandle registerResource0( - OcResource ocResource) throws OcException; - - /** - * This API registers a resource with the server NOTE: This API applies to server side only. - * - * @param resourceUri The URI of the resource. Example: "a/light" - * @param resourceTypeName The resource type. Example: "light" - * @param resourceInterface The resource interface (whether it is collection etc). - * @param entityHandler entity handler. - * @param resourcePropertySet indicates the property of the resource - * @return resource handle - * @throws OcException - */ - public static OcResourceHandle registerResource( - String resourceUri, - String resourceTypeName, - String resourceInterface, - EntityHandler entityHandler, - EnumSet resourcePropertySet) throws OcException { - OcPlatform.initCheck(); - - int resProperty = 0; - - for (ResourceProperty prop : ResourceProperty.values()) { - if (resourcePropertySet.contains(prop)) - resProperty |= prop.getValue(); - } - - return OcPlatform.registerResource1(resourceUri, - resourceTypeName, - resourceInterface, - entityHandler, - resProperty); - } - - private static native OcResourceHandle registerResource1( - String resourceUri, - String resourceTypeName, - String resourceInterface, - EntityHandler entityHandler, - int resourceProperty) throws OcException; - - /** - * Register Device Info - * - * @param ocDeviceInfo object containing all the device specific information - * @throws OcException - */ - public static void registerDeviceInfo( - OcDeviceInfo ocDeviceInfo) throws OcException { - OcPlatform.initCheck(); - OcPlatform.registerDeviceInfo0( - ocDeviceInfo.getDeviceName() - ); - } - - private static native void registerDeviceInfo0( - String deviceName - ) throws OcException; - - /** - * This API unregisters a resource with the server NOTE: This API applies to server side only. - * - * @param ocResourceHandle This is the resource handle which we which to unregister from the - * server - * @throws OcException - */ - public static void unregisterResource( - OcResourceHandle ocResourceHandle) throws OcException { - OcPlatform.initCheck(); - OcPlatform.unregisterResource0(ocResourceHandle); - } - - private static native void unregisterResource0( - OcResourceHandle ocResourceHandle) throws OcException; - - - /** - * Add a resource to a collection resource - * - * @param ocResourceCollectionHandle handle to the collection resource - * @param ocResourceHandle handle to resource to be added to the collection resource - * @throws OcException - */ - public static void bindResource( - OcResourceHandle ocResourceCollectionHandle, - OcResourceHandle ocResourceHandle) throws OcException { - OcPlatform.initCheck(); - OcPlatform.bindResource0(ocResourceCollectionHandle, ocResourceHandle); - } - - private static native void bindResource0( - OcResourceHandle ocResourceCollectionHandle, - OcResourceHandle ocResourceHandle) throws OcException; - - /** - * Add multiple resources to a collection resource. - * - * @param ocResourceCollectionHandle handle to the collection resource - * @param ocResourceHandleList reference to list of resource handles to be added to the - * collection resource - * @throws OcException - */ - public static void bindResources( - OcResourceHandle ocResourceCollectionHandle, - List ocResourceHandleList) throws OcException { - OcPlatform.initCheck(); - OcPlatform.bindResources0( - ocResourceCollectionHandle, - ocResourceHandleList.toArray( - new OcResourceHandle[ocResourceHandleList.size()]) - ); - } - - private static native void bindResources0( - OcResourceHandle ocResourceCollectionHandle, - OcResourceHandle[] ocResourceHandleArray) throws OcException; - - /** - * Unbind a resource from a collection resource. - * - * @param ocResourceCollectionHandle handle to the collection resource - * @param ocResourceHandle resource handle to be unbound from the collection resource - * @throws OcException - */ - public static void unbindResource( - OcResourceHandle ocResourceCollectionHandle, - OcResourceHandle ocResourceHandle) throws OcException { - OcPlatform.initCheck(); - OcPlatform.unbindResource0(ocResourceCollectionHandle, ocResourceHandle); - } - - private static native void unbindResource0( - OcResourceHandle ocResourceCollectionHandle, - OcResourceHandle ocResourceHandle) throws OcException; - - /** - * Unbind resources from a collection resource. - * - * @param ocResourceCollectionHandle Handle to the collection resource - * @param ocResourceHandleList List of resource handles to be unbound from the collection - * resource - * @throws OcException - */ - public static void unbindResources( - OcResourceHandle ocResourceCollectionHandle, - List ocResourceHandleList) throws OcException { - OcPlatform.initCheck(); - OcPlatform.unbindResources0( - ocResourceCollectionHandle, - ocResourceHandleList.toArray( - new OcResourceHandle[ocResourceHandleList.size()]) - ); - } - - private static native void unbindResources0( - OcResourceHandle ocResourceCollectionHandle, - OcResourceHandle[] ocResourceHandleArray) throws OcException; - - /** - * Binds a type to a particular resource - * - * @param ocResourceHandle handle to the resource - * @param resourceTypeName new typename to bind to the resource - * @throws OcException - */ - public static void bindTypeToResource( - OcResourceHandle ocResourceHandle, - String resourceTypeName) throws OcException { - OcPlatform.initCheck(); - OcPlatform.bindTypeToResource0(ocResourceHandle, resourceTypeName); - } - - private static native void bindTypeToResource0( - OcResourceHandle ocResourceHandle, - String resourceTypeName) throws OcException; - - /** - * Binds an interface to a particular resource - * - * @param ocResourceHandle handle to the resource - * @param resourceInterfaceName new interface to bind to the resource - * @throws OcException - */ - public static void bindInterfaceToResource( - OcResourceHandle ocResourceHandle, - String resourceInterfaceName) throws OcException { - OcPlatform.initCheck(); - OcPlatform.bindInterfaceToResource0(ocResourceHandle, resourceInterfaceName); - } - - private static native void bindInterfaceToResource0( - OcResourceHandle ocResourceHandle, - String resourceInterfaceName) throws OcException; - - /** - * Start Presence announcements. - * - * @param ttl time to live in seconds - * @throws OcException - */ - public static void startPresence(int ttl) throws OcException { - OcPlatform.initCheck(); - OcPlatform.startPresence0(ttl); - } - - private static native void startPresence0(int ttl) throws OcException; - - /** - * Stop Presence announcements. - * - * @throws OcException - */ - public static void stopPresence() throws OcException { - OcPlatform.initCheck(); - OcPlatform.stopPresence0(); - } - - private static native void stopPresence0() throws OcException; - - /** - * Subscribes to a server's presence change events. By making this subscription, every time a - * server adds/removes/alters a resource, starts or is intentionally stopped - * - * @param host The IP address/addressable name of the server to subscribe to - * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, - * IPV6, ALL - * @param onPresenceListener listener that will receive notifications/subscription events - * @return a handle object that can be used to identify this subscription request. It can be - * used to unsubscribe from these events in the future - * @throws OcException - */ - public static OcPresenceHandle subscribePresence( - String host, - OcConnectivityType connectivityType, - OnPresenceListener onPresenceListener) throws OcException { - OcPlatform.initCheck(); - return OcPlatform.subscribePresence0( - host, - connectivityType.getValue(), - onPresenceListener - ); - } - - private static native OcPresenceHandle subscribePresence0( - String host, - int connectivityType, - OnPresenceListener onPresenceListener) throws OcException; - - /** - * Subscribes to a server's presence change events. By making this subscription, every time a - * server adds/removes/alters a resource, starts or is intentionally stopped - * - * @param host The IP address/addressable name of the server to subscribe to - * @param resourceType a resource type specified as a filter for subscription events. - * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, - * IPV6, ALL - * @param onPresenceListener listener that will receive notifications/subscription events - * @return a handle object that can be used to identify this subscription request. It can be - * used to unsubscribe from these events in the future - * @throws OcException - */ - public static OcPresenceHandle subscribePresence( - String host, - String resourceType, - OcConnectivityType connectivityType, - OnPresenceListener onPresenceListener) throws OcException { - OcPlatform.initCheck(); - return OcPlatform.subscribePresence1( - host, - resourceType, - connectivityType.getValue(), - onPresenceListener); - } - - private static native OcPresenceHandle subscribePresence1( - String host, - String resourceType, - int connectivityType, - OnPresenceListener onPresenceListener) throws OcException; - - /** - * Unsubscribes from a previously subscribed server's presence events. Note that you may for - * a short time still receive events from the server since it may take time for the - * unsubscribe to take effect. - * - * @param ocPresenceHandle the handle object provided by the subscribePresence call that - * identifies this subscription - * @throws OcException - */ - public static void unsubscribePresence( - OcPresenceHandle ocPresenceHandle) throws OcException { - OcPlatform.initCheck(); - OcPlatform.unsubscribePresence0(ocPresenceHandle); - } - - private static native void unsubscribePresence0( - OcPresenceHandle ocPresenceHandle) throws OcException; - - /** - * Creates a resource proxy object so that get/put/observe functionality can be used without - * discovering the object in advance. Note that the consumer of this method needs to provide - * all of the details required to correctly contact and observe the object. If the consumer - * lacks any of this information, they should discover the resource object normally. - * Additionally, you can only create this object if OcPlatform was initialized to be a Client - * or Client/Server. - * - * @param host a string containing a resolvable host address of the server holding - * the resource - * @param uri the rest of the resource's URI that will permit messages to be - * properly routed. - * Example: /a/light - * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, - * IPV6, ALL - * @param isObservable a boolean containing whether the resource supports observation - * @param resourceTypeList a collection of resource types implemented by the resource - * @param interfaceList a collection of interfaces that the resource supports/implements - * @return new resource object - * @throws OcException - */ - public static OcResource constructResourceObject( - String host, - String uri, - OcConnectivityType connectivityType, - boolean isObservable, - List resourceTypeList, - List interfaceList) throws OcException { - OcPlatform.initCheck(); - return OcPlatform.constructResourceObject0( - host, - uri, - connectivityType.getValue(), - isObservable, - resourceTypeList.toArray(new String[resourceTypeList.size()]), - interfaceList.toArray(new String[interfaceList.size()]) - ); - } - - private static native OcResource constructResourceObject0( - String host, - String uri, - int connectivityType, - boolean isObservable, - String[] resourceTypes, - String[] interfaces) throws OcException; - - /** - * Allows application entity handler to send response to an incoming request. - * - * @param ocResourceResponse resource response - * @throws OcException - */ - public static void sendResponse(OcResourceResponse ocResourceResponse) - throws OcException { - OcPlatform.initCheck(); - OcPlatform.sendResponse0(ocResourceResponse); - } - - private static native void sendResponse0(OcResourceResponse ocResourceResponse) - throws OcException; - - /** - * An OnResourceFoundListener can be registered via the OcPlatform.findResource call. - * Event listeners are notified asynchronously - */ - public interface OnResourceFoundListener { - public void onResourceFound(OcResource resource); - } - - /** - * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call. - * Event listeners are notified asynchronously - */ - public interface OnDeviceFoundListener { - public void onDeviceFound(OcRepresentation ocRepresentation); - } - - /** - * An OnPresenceListener can be registered via the OcPlatform.subscribePresence call. - * Event listeners are notified asynchronously - */ - public interface OnPresenceListener { - public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress); - } - - /** - * An EntityHandler can be registered via the OcPlatform.registerResource call. - * Event listeners are notified asynchronously - */ - public interface EntityHandler { - public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest); - } - - private static void initCheck() { - if (!sIsPlatformInitialized) { - throw new IllegalStateException("OcPlatform must be configured by making a call to " + - "OcPlatform.Configure before any other API calls are permitted"); - } - } -} +/* + * //****************************************************************** + * // + * // Copyright 2015 Intel Corporation. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import org.iotivity.ca.CaInterface; + +import java.util.EnumSet; +import java.util.Iterator; +import java.util.List; + +/** + * Contains the main entrance/functionality of the product. To set a custom configuration, the + * implementer must make a call to OcPlatform.Configure before the first usage of a function in this + * class. + */ +public final class OcPlatform { + + static { + System.loadLibrary("oc_logger"); + System.loadLibrary("octbstack"); + System.loadLibrary("connectivity_abstraction"); + System.loadLibrary("oc"); + System.loadLibrary("ocstack-jni"); + } + + /** + * Default interface + */ + public static final String DEFAULT_INTERFACE = "oic.if.baseline"; + + /** + * Used in discovering (GET) links to other resources of a collection + */ + public static final String LINK_INTERFACE = "oic.if.ll"; + + /** + * Used in GET, PUT, POST, DELETE methods on links to other resources of a collection + */ + public static final String BATCH_INTERFACE = "oic.if.b"; + + /** + * Used in GET, PUT, POST methods on links to other remote resources of a group + */ + public static final String GROUP_INTERFACE = "oic.mi.grp"; + + public static final String WELL_KNOWN_QUERY = "224.0.1.187:5683/oic/res"; + public static final String MULTICAST_PREFIX = "224.0.1.187:5683"; + public static final String MULTICAST_IP = "224.0.1.187"; + public static final int MULTICAST_PORT = 5683; + public static final int DEFAULT_PRESENCE_TTL = 60; + public static final String DEVICE_URI = "/oic/d"; + public static final String PRESENCE_URI = "/oic/ad"; + + private static volatile boolean sIsPlatformInitialized = false; + + private OcPlatform() { + } + + /** + * API for setting the configuration of the OcPlatform. + * Note: Any calls made to this AFTER the first call to OcPlatform.Configure will have no affect + * + * @param platformConfig platform configuration + */ + public synchronized static void Configure(PlatformConfig platformConfig) { + if (!sIsPlatformInitialized) { + CaInterface.initialize(platformConfig.getContext()); + + OcPlatform.configure( + platformConfig.getServiceType().getValue(), + platformConfig.getModeType().getValue(), + platformConfig.getIpAddress(), + platformConfig.getPort(), + platformConfig.getQualityOfService().getValue() + ); + + sIsPlatformInitialized = true; + } + } + + private static native void configure(int serviceType, + int modeType, + String ipAddress, + int port, + int qualityOfService); + + /** + * API for notifying base that resource's attributes have changed. + * + * @param ocResourceHandle resource handle of the resource + * @throws OcException + */ + public static void notifyAllObservers( + OcResourceHandle ocResourceHandle) throws OcException { + OcPlatform.initCheck(); + OcPlatform.notifyAllObservers0(ocResourceHandle); + } + + private static native void notifyAllObservers0( + OcResourceHandle ocResourceHandle) throws OcException; + + /** + * API for notifying base that resource's attributes have changed. + * + * @param ocResourceHandle resource handle of the resource + * @param qualityOfService the quality of communication + * @throws OcException + */ + public static void notifyAllObservers( + OcResourceHandle ocResourceHandle, + QualityOfService qualityOfService) throws OcException { + OcPlatform.initCheck(); + OcPlatform.notifyAllObservers1(ocResourceHandle, qualityOfService.getValue()); + } + + private static native void notifyAllObservers1( + OcResourceHandle ocResourceHandle, + int qualityOfService) throws OcException; + + /** + * API for notifying only specific clients that resource's attributes have changed. + * + * @param ocResourceHandle resource handle of the resource + * @param ocObservationIdList These set of ids are ones which which will be notified upon + * resource change. + * @param ocResourceResponse OcResourceResponse object used by app to fill the response for + * this resource change + * @throws OcException + */ + public static void notifyListOfObservers( + OcResourceHandle ocResourceHandle, + List ocObservationIdList, + OcResourceResponse ocResourceResponse) throws OcException { + OcPlatform.initCheck(); + + byte[] idArr = new byte[ocObservationIdList.size()]; + Iterator it = ocObservationIdList.iterator(); + int i = 0; + while (it.hasNext()) { + idArr[i++] = (byte) it.next(); + } + + OcPlatform.notifyListOfObservers2( + ocResourceHandle, + idArr, + ocResourceResponse); + } + + private static native void notifyListOfObservers2( + OcResourceHandle ocResourceHandle, + byte[] ocObservationIdArray, + OcResourceResponse ocResourceResponse) throws OcException; + + /** + * API for notifying only specific clients that resource's attributes have changed. + * + * @param ocResourceHandle resource handle of the resource + * @param ocObservationIdList These set of ids are ones which which will be notified upon + * resource change. + * @param ocResourceResponse OcResourceResponse object used by app to fill the response for + * this resource change + * @param qualityOfService the quality of communication + * @throws OcException + */ + public static void notifyListOfObservers( + OcResourceHandle ocResourceHandle, + List ocObservationIdList, + OcResourceResponse ocResourceResponse, + QualityOfService qualityOfService) throws OcException { + OcPlatform.initCheck(); + + byte[] idArr = new byte[ocObservationIdList.size()]; + Iterator it = ocObservationIdList.iterator(); + int i = 0; + while (it.hasNext()) { + idArr[i++] = (byte) it.next(); + } + + OcPlatform.notifyListOfObservers3( + ocResourceHandle, + idArr, + ocResourceResponse, + qualityOfService.getValue() + ); + } + + private static native void notifyListOfObservers3( + OcResourceHandle ocResourceHandle, + byte[] ocObservationIdArray, + OcResourceResponse ocResourceResponse, + int qualityOfService) throws OcException; + + /** + * API for Service and Resource Discovery. NOTE: This API applies to client side only + * + * @param host Host IP Address of a service to direct resource discovery query. + * If empty, performs multicast resource discovery query + * @param resourceUri name of the resource. If null or empty, performs search for all + * resource names + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onResourceFoundListener Handles events, success states and failure states. + * @throws OcException + */ + public static void findResource( + String host, + String resourceUri, + OcConnectivityType connectivityType, + OnResourceFoundListener onResourceFoundListener) throws OcException { + OcPlatform.initCheck(); + OcPlatform.findResource0( + host, + resourceUri, + connectivityType.getValue(), + onResourceFoundListener + ); + } + + private static native void findResource0( + String host, + String resourceUri, + int connectivityType, + OnResourceFoundListener onResourceFoundListener) throws OcException; + + /** + * API for Service and Resource Discovery. NOTE: This API applies to client side only + * + * @param host Host IP Address of a service to direct resource discovery query. + * If empty, performs multicast resource discovery query + * @param resourceUri name of the resource. If null or empty, performs search for all + * resource names + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onResourceFoundListener Handles events, success states and failure states. + * @param qualityOfService the quality of communication + * @throws OcException + */ + public static void findResource( + String host, + String resourceUri, + OcConnectivityType connectivityType, + OnResourceFoundListener onResourceFoundListener, + QualityOfService qualityOfService) throws OcException { + OcPlatform.initCheck(); + OcPlatform.findResource1(host, + resourceUri, + connectivityType.getValue(), + onResourceFoundListener, + qualityOfService.getValue() + ); + } + + private static native void findResource1( + String host, + String resourceUri, + int connectivityType, + OnResourceFoundListener onResourceFoundListener, + int qualityOfService) throws OcException; + + /** + * API for Device Discovery + * + * @param host Host IP Address. If null or empty, Multicast is performed. + * @param deviceUri Uri containing address to the virtual device + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onDeviceFoundListener Handles events, success states and failure states. + * @throws OcException + */ + public static void getDeviceInfo( + String host, + String deviceUri, + OcConnectivityType connectivityType, + OnDeviceFoundListener onDeviceFoundListener) throws OcException { + OcPlatform.initCheck(); + OcPlatform.getDeviceInfo0( + host, + deviceUri, + connectivityType.getValue(), + onDeviceFoundListener + ); + } + + private static native void getDeviceInfo0( + String host, + String deviceUri, + int connectivityType, + OnDeviceFoundListener onDeviceFoundListener) throws OcException; + + /** + * API for Device Discovery + * + * @param host Host IP Address. If null or empty, Multicast is performed. + * @param deviceUri Uri containing address to the virtual device + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onDeviceFoundListener Handles events, success states and failure states. + * @param qualityOfService the quality of communication + * @throws OcException + */ + public static void getDeviceInfo( + String host, + String deviceUri, + OcConnectivityType connectivityType, + OnDeviceFoundListener onDeviceFoundListener, + QualityOfService qualityOfService) throws OcException { + OcPlatform.initCheck(); + OcPlatform.getDeviceInfo1( + host, + deviceUri, + connectivityType.getValue(), + onDeviceFoundListener, + qualityOfService.getValue() + ); + } + + private static native void getDeviceInfo1( + String host, + String deviceUri, + int connectivityType, + OnDeviceFoundListener onDeviceFoundListener, + int qualityOfService) throws OcException; + + /** + * API for Platform Discovery + * + * @param host Host IP Address. If null or empty, Multicast is performed. + * @param platformUri Uri containing address to the platform + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onPlatformFoundListener Handles events, success states and failure states. + * @throws OcException + */ + + public static void getPlatformInfo( + String host, + String platformUri, + OcConnectivityType connectivityType, + OnPlatformFoundListener onPlatformFoundListener) throws OcException { + OcPlatform.initCheck(); + OcPlatform.getPlatformInfo0( + host, + platformUri, + connectivityType.getValue(), + onPlatformFoundListener + ); + } + + private static native void getPlatformInfo0( + String host, + String platformUri, + int connectivityType, + OnPlatformFoundListener onPlatformInfoFoundListener) throws OcException; + + /** + * API for Platform Discovery + * + * @param host Host IP Address. If null or empty, Multicast is performed. + * @param platformUri Uri containing address to the platform + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onPlatformFoundListener Handles events, success states and failure states. + * @param qualityOfService the quality of communication + * @throws OcException + */ + + public static void getPlatformInfo( + String host, + String platformUri, + OcConnectivityType connectivityType, + OnPlatformFoundListener onPlatformFoundListener, + QualityOfService qualityOfService) throws OcException { + OcPlatform.initCheck(); + OcPlatform.getPlatformInfo1( + host, + platformUri, + connectivityType.getValue(), + onPlatformFoundListener, + qualityOfService.getValue() + ); + } + + private static native void getPlatformInfo1( + String host, + String platformUri, + int connectivityType, + OnPlatformFoundListener onPlatformFoundListener, + int qualityOfService) throws OcException; + + /** + * This API registers a resource with the server NOTE: This API applies to server side only. + * + * @param ocResource The instance of OcResource with all data filled + * @return resource handle + * @throws OcException + */ + public static OcResourceHandle registerResource( + OcResource ocResource) throws OcException { + OcPlatform.initCheck(); + return OcPlatform.registerResource0(ocResource); + } + + private static native OcResourceHandle registerResource0( + OcResource ocResource) throws OcException; + + /** + * This API registers a resource with the server NOTE: This API applies to server side only. + * + * @param resourceUri The URI of the resource. Example: "a/light" + * @param resourceTypeName The resource type. Example: "light" + * @param resourceInterface The resource interface (whether it is collection etc). + * @param entityHandler entity handler. + * @param resourcePropertySet indicates the property of the resource + * @return resource handle + * @throws OcException + */ + public static OcResourceHandle registerResource( + String resourceUri, + String resourceTypeName, + String resourceInterface, + EntityHandler entityHandler, + EnumSet resourcePropertySet) throws OcException { + OcPlatform.initCheck(); + + int resProperty = 0; + + for (ResourceProperty prop : ResourceProperty.values()) { + if (resourcePropertySet.contains(prop)) + resProperty |= prop.getValue(); + } + + return OcPlatform.registerResource1(resourceUri, + resourceTypeName, + resourceInterface, + entityHandler, + resProperty); + } + + private static native OcResourceHandle registerResource1( + String resourceUri, + String resourceTypeName, + String resourceInterface, + EntityHandler entityHandler, + int resourceProperty) throws OcException; + + /** + * Register Device Info + * + * @param ocDeviceInfo object containing all the device specific information + * @throws OcException + */ + public static void registerDeviceInfo( + OcDeviceInfo ocDeviceInfo) throws OcException { + OcPlatform.initCheck(); + OcPlatform.registerDeviceInfo0( + ocDeviceInfo.getDeviceName() + ); + } + + private static native void registerDeviceInfo0( + String deviceName + ) throws OcException; + + /** + * Register Platform Info + * + * @param ocPlatformInfo object containing all the platform specific information + * @throws OcException + */ + public static void registerPlatformInfo( + OcPlatformInfo ocPlatformInfo) throws OcException { + OcPlatform.initCheck(); + OcPlatform.registerPlatformInfo0( + ocPlatformInfo.getPlatformID(), + ocPlatformInfo.getManufacturerName(), + ocPlatformInfo.getManufacturerUrl(), + ocPlatformInfo.getModelNumber(), + ocPlatformInfo.getDateOfManufacture(), + ocPlatformInfo.getPlatformVersion(), + ocPlatformInfo.getOperatingSystemVersion(), + ocPlatformInfo.getHardwareVersion(), + ocPlatformInfo.getFirmwareVersion(), + ocPlatformInfo.getSupportUrl(), + ocPlatformInfo.getSystemTime() + ); + } + + private static native void registerPlatformInfo0( + String platformId, String manufacturerName, String manufacturerUrl, + String modelNumber, String dateOfManufacture, String platformVersion, + String operatingSystemVersion, String hardwareVersion, String firmwareVersion, + String supportUrl, String systemTime + ) throws OcException; + + /** + * This API unregisters a resource with the server NOTE: This API applies to server side only. + * + * @param ocResourceHandle This is the resource handle which we which to unregister from the + * server + * @throws OcException + */ + public static void unregisterResource( + OcResourceHandle ocResourceHandle) throws OcException { + OcPlatform.initCheck(); + OcPlatform.unregisterResource0(ocResourceHandle); + } + + private static native void unregisterResource0( + OcResourceHandle ocResourceHandle) throws OcException; + + + /** + * Add a resource to a collection resource + * + * @param ocResourceCollectionHandle handle to the collection resource + * @param ocResourceHandle handle to resource to be added to the collection resource + * @throws OcException + */ + public static void bindResource( + OcResourceHandle ocResourceCollectionHandle, + OcResourceHandle ocResourceHandle) throws OcException { + OcPlatform.initCheck(); + OcPlatform.bindResource0(ocResourceCollectionHandle, ocResourceHandle); + } + + private static native void bindResource0( + OcResourceHandle ocResourceCollectionHandle, + OcResourceHandle ocResourceHandle) throws OcException; + + /** + * Add multiple resources to a collection resource. + * + * @param ocResourceCollectionHandle handle to the collection resource + * @param ocResourceHandleList reference to list of resource handles to be added to the + * collection resource + * @throws OcException + */ + public static void bindResources( + OcResourceHandle ocResourceCollectionHandle, + List ocResourceHandleList) throws OcException { + OcPlatform.initCheck(); + OcPlatform.bindResources0( + ocResourceCollectionHandle, + ocResourceHandleList.toArray( + new OcResourceHandle[ocResourceHandleList.size()]) + ); + } + + private static native void bindResources0( + OcResourceHandle ocResourceCollectionHandle, + OcResourceHandle[] ocResourceHandleArray) throws OcException; + + /** + * Unbind a resource from a collection resource. + * + * @param ocResourceCollectionHandle handle to the collection resource + * @param ocResourceHandle resource handle to be unbound from the collection resource + * @throws OcException + */ + public static void unbindResource( + OcResourceHandle ocResourceCollectionHandle, + OcResourceHandle ocResourceHandle) throws OcException { + OcPlatform.initCheck(); + OcPlatform.unbindResource0(ocResourceCollectionHandle, ocResourceHandle); + } + + private static native void unbindResource0( + OcResourceHandle ocResourceCollectionHandle, + OcResourceHandle ocResourceHandle) throws OcException; + + /** + * Unbind resources from a collection resource. + * + * @param ocResourceCollectionHandle Handle to the collection resource + * @param ocResourceHandleList List of resource handles to be unbound from the collection + * resource + * @throws OcException + */ + public static void unbindResources( + OcResourceHandle ocResourceCollectionHandle, + List ocResourceHandleList) throws OcException { + OcPlatform.initCheck(); + OcPlatform.unbindResources0( + ocResourceCollectionHandle, + ocResourceHandleList.toArray( + new OcResourceHandle[ocResourceHandleList.size()]) + ); + } + + private static native void unbindResources0( + OcResourceHandle ocResourceCollectionHandle, + OcResourceHandle[] ocResourceHandleArray) throws OcException; + + /** + * Binds a type to a particular resource + * + * @param ocResourceHandle handle to the resource + * @param resourceTypeName new typename to bind to the resource + * @throws OcException + */ + public static void bindTypeToResource( + OcResourceHandle ocResourceHandle, + String resourceTypeName) throws OcException { + OcPlatform.initCheck(); + OcPlatform.bindTypeToResource0(ocResourceHandle, resourceTypeName); + } + + private static native void bindTypeToResource0( + OcResourceHandle ocResourceHandle, + String resourceTypeName) throws OcException; + + /** + * Binds an interface to a particular resource + * + * @param ocResourceHandle handle to the resource + * @param resourceInterfaceName new interface to bind to the resource + * @throws OcException + */ + public static void bindInterfaceToResource( + OcResourceHandle ocResourceHandle, + String resourceInterfaceName) throws OcException { + OcPlatform.initCheck(); + OcPlatform.bindInterfaceToResource0(ocResourceHandle, resourceInterfaceName); + } + + private static native void bindInterfaceToResource0( + OcResourceHandle ocResourceHandle, + String resourceInterfaceName) throws OcException; + + /** + * Start Presence announcements. + * + * @param ttl time to live in seconds + * @throws OcException + */ + public static void startPresence(int ttl) throws OcException { + OcPlatform.initCheck(); + OcPlatform.startPresence0(ttl); + } + + private static native void startPresence0(int ttl) throws OcException; + + /** + * Stop Presence announcements. + * + * @throws OcException + */ + public static void stopPresence() throws OcException { + OcPlatform.initCheck(); + OcPlatform.stopPresence0(); + } + + private static native void stopPresence0() throws OcException; + + /** + * Subscribes to a server's presence change events. By making this subscription, every time a + * server adds/removes/alters a resource, starts or is intentionally stopped + * + * @param host The IP address/addressable name of the server to subscribe to + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onPresenceListener listener that will receive notifications/subscription events + * @return a handle object that can be used to identify this subscription request. It can be + * used to unsubscribe from these events in the future + * @throws OcException + */ + public static OcPresenceHandle subscribePresence( + String host, + OcConnectivityType connectivityType, + OnPresenceListener onPresenceListener) throws OcException { + OcPlatform.initCheck(); + return OcPlatform.subscribePresence0( + host, + connectivityType.getValue(), + onPresenceListener + ); + } + + private static native OcPresenceHandle subscribePresence0( + String host, + int connectivityType, + OnPresenceListener onPresenceListener) throws OcException; + + /** + * Subscribes to a server's presence change events. By making this subscription, every time a + * server adds/removes/alters a resource, starts or is intentionally stopped + * + * @param host The IP address/addressable name of the server to subscribe to + * @param resourceType a resource type specified as a filter for subscription events. + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param onPresenceListener listener that will receive notifications/subscription events + * @return a handle object that can be used to identify this subscription request. It can be + * used to unsubscribe from these events in the future + * @throws OcException + */ + public static OcPresenceHandle subscribePresence( + String host, + String resourceType, + OcConnectivityType connectivityType, + OnPresenceListener onPresenceListener) throws OcException { + OcPlatform.initCheck(); + return OcPlatform.subscribePresence1( + host, + resourceType, + connectivityType.getValue(), + onPresenceListener); + } + + private static native OcPresenceHandle subscribePresence1( + String host, + String resourceType, + int connectivityType, + OnPresenceListener onPresenceListener) throws OcException; + + /** + * Unsubscribes from a previously subscribed server's presence events. Note that you may for + * a short time still receive events from the server since it may take time for the + * unsubscribe to take effect. + * + * @param ocPresenceHandle the handle object provided by the subscribePresence call that + * identifies this subscription + * @throws OcException + */ + public static void unsubscribePresence( + OcPresenceHandle ocPresenceHandle) throws OcException { + OcPlatform.initCheck(); + OcPlatform.unsubscribePresence0(ocPresenceHandle); + } + + private static native void unsubscribePresence0( + OcPresenceHandle ocPresenceHandle) throws OcException; + + /** + * Creates a resource proxy object so that get/put/observe functionality can be used without + * discovering the object in advance. Note that the consumer of this method needs to provide + * all of the details required to correctly contact and observe the object. If the consumer + * lacks any of this information, they should discover the resource object normally. + * Additionally, you can only create this object if OcPlatform was initialized to be a Client + * or Client/Server. + * + * @param host a string containing a resolvable host address of the server holding + * the resource + * @param uri the rest of the resource's URI that will permit messages to be + * properly routed. + * Example: /a/light + * @param connectivityType a type of connectivity indicating the interface. Example: IPV4, + * IPV6, ALL + * @param isObservable a boolean containing whether the resource supports observation + * @param resourceTypeList a collection of resource types implemented by the resource + * @param interfaceList a collection of interfaces that the resource supports/implements + * @return new resource object + * @throws OcException + */ + public static OcResource constructResourceObject( + String host, + String uri, + OcConnectivityType connectivityType, + boolean isObservable, + List resourceTypeList, + List interfaceList) throws OcException { + OcPlatform.initCheck(); + return OcPlatform.constructResourceObject0( + host, + uri, + connectivityType.getValue(), + isObservable, + resourceTypeList.toArray(new String[resourceTypeList.size()]), + interfaceList.toArray(new String[interfaceList.size()]) + ); + } + + private static native OcResource constructResourceObject0( + String host, + String uri, + int connectivityType, + boolean isObservable, + String[] resourceTypes, + String[] interfaces) throws OcException; + + /** + * Allows application entity handler to send response to an incoming request. + * + * @param ocResourceResponse resource response + * @throws OcException + */ + public static void sendResponse(OcResourceResponse ocResourceResponse) + throws OcException { + OcPlatform.initCheck(); + OcPlatform.sendResponse0(ocResourceResponse); + } + + private static native void sendResponse0(OcResourceResponse ocResourceResponse) + throws OcException; + + /** + * An OnResourceFoundListener can be registered via the OcPlatform.findResource call. + * Event listeners are notified asynchronously + */ + public interface OnResourceFoundListener { + public void onResourceFound(OcResource resource); + } + + /** + * An OnDeviceFoundListener can be registered via the OcPlatform.getDeviceInfo call. + * Event listeners are notified asynchronously + */ + public interface OnDeviceFoundListener { + public void onDeviceFound(OcRepresentation ocRepresentation); + } + + /** + * An OnPlatformFoundListener can be registered via the OcPlatform.getPlatformInfo call. + * Event listeners are notified asynchronously + */ + public interface OnPlatformFoundListener { + public void onPlatformFound(OcRepresentation ocRepresentation); + } + + /** + * An OnPresenceListener can be registered via the OcPlatform.subscribePresence call. + * Event listeners are notified asynchronously + */ + public interface OnPresenceListener { + public void onPresence(OcPresenceStatus ocPresenceStatus, int nonce, String hostAddress); + } + + /** + * An EntityHandler can be registered via the OcPlatform.registerResource call. + * Event listeners are notified asynchronously + */ + public interface EntityHandler { + public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest); + } + + private static void initCheck() { + if (!sIsPlatformInitialized) { + throw new IllegalStateException("OcPlatform must be configured by making a call to " + + "OcPlatform.Configure before any other API calls are permitted"); + } + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcPlatformInfo.java b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatformInfo.java new file mode 100644 index 0000000..53d366d --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatformInfo.java @@ -0,0 +1,156 @@ +/* + * //****************************************************************** + * // + * // Copyright 2015 Intel Corporation. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import java.lang.Error; + +public class OcPlatformInfo { + + private String platformID; + private String manufacturerName; + private String manufacturerUrl; + private String modelNumber; + private String dateOfManufacture; + private String platformVersion; + private String operatingSystemVersion; + private String hardwareVersion; + private String firmwareVersion; + private String supportUrl; + private String systemTime; + + // construct OcPlatformInfo with mandatory fields which cannot be null + // manufacturerName cannot be > 16 chars + // manufacturerUrl cannot be > 32 chars + protected OcPlatformInfo(String platformID, String manufacturerName, + String manufacturerUrl) throws OcException { + ErrorCode result = validatePlatformInfo(platformID, manufacturerName, manufacturerUrl); + if (ErrorCode.OK == result) { + this.platformID = platformID; + this.manufacturerName = manufacturerName; + this.manufacturerUrl = manufacturerUrl; + } else { + throw new OcException(result, result.getDescription()); + } + } + + public ErrorCode validatePlatformInfo(String platformID, String manufacturerName, + String manufacturerUrl) { + // checks to see if the mandatory fields have non-null values or not + if (platformID == null || platformID.isEmpty()) return ErrorCode.INVALID_PLATFORM_INFO_PLATFORMID; + if (manufacturerName == null || manufacturerName.isEmpty() || + manufacturerName.length() > OcStackConfig.MAX_MANUFACTURER_NAME_LENGTH) + return ErrorCode.INVALID_PLATFORM_INFO_MANUFACTURER_NAME; + if (manufacturerUrl == null || manufacturerUrl.isEmpty() || + manufacturerUrl.length() > OcStackConfig.MAX_MANUFACTURER_URL_LENGTH) + return ErrorCode.INVALID_PLATFORM_INFO_PLATFORMID_MANUFACTURER_URL; + return ErrorCode.OK; + } + + public String getPlatformID() { + return platformID; + } + + public void setPlatformID(String platformID) { + this.platformID = platformID; + } + + public String getManufacturerName() { + return manufacturerName; + } + + public void setManufacturerName(String manufacturerName) { + this.manufacturerName = manufacturerName; + } + + public String getManufacturerUrl() { + return manufacturerUrl; + } + + public void setManufacturerUrl(String manufacturerUrl) { + this.manufacturerUrl = manufacturerUrl; + } + + public String getModelNumber() { + return modelNumber; + } + + public void setModelNumber(String modelNumber) { + this.modelNumber = modelNumber; + } + + public String getDateOfManufacture() { + return dateOfManufacture; + } + + public void setDateOfManufacture(String dateOfManufacture) { + this.dateOfManufacture = dateOfManufacture; + } + + public String getPlatformVersion() { + return platformVersion; + } + + public void setPlatformVersion(String platformVersion) { + this.platformVersion = platformVersion; + } + + public String getOperatingSystemVersion() { + return operatingSystemVersion; + } + + public void setOperatingSystemVersion(String operatingSystemVersion) { + this.operatingSystemVersion = operatingSystemVersion; + } + + public String getHardwareVersion() { + return hardwareVersion; + } + + public void setHardwareVersion(String hardwareVersion) { + this.hardwareVersion = hardwareVersion; + } + + public String getFirmwareVersion() { + return firmwareVersion; + } + + public void setFirmwareVersion(String firmwareVersion) { + this.firmwareVersion = firmwareVersion; + } + + public String getSupportUrl() { + return supportUrl; + } + + public void setSupportUrl(String supportUrl) { + this.supportUrl = supportUrl; + } + + public String getSystemTime() { + return systemTime; + } + + public void setSystemTime(String systemTime) { + this.systemTime = systemTime; + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcStackConfig.java b/android/android_api/base/src/main/java/org/iotivity/base/OcStackConfig.java new file mode 100644 index 0000000..ff48d22 --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcStackConfig.java @@ -0,0 +1,33 @@ +/* + * //****************************************************************** + * // + * // Copyright 2015 Intel Corporation. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + * // + * // 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +// This file contains all the variables which can be configured/modified as +// per platform or specific product usage scenarios. + +package org.iotivity.base; + +public interface OcStackConfig { + // max manufacturer name length for OcPlatformInfo supported by server + public static final int MAX_MANUFACTURER_NAME_LENGTH = 16; + // max manufacturer url length for OcPlatformInfo supported by server + public static final int MAX_MANUFACTURER_URL_LENGTH = 32; +} \ No newline at end of file