From d5e14d63efaae9d16198b9ea0035e74ba9d0cbde Mon Sep 17 00:00:00 2001 From: Jay Sharma Date: Mon, 7 Sep 2015 21:13:42 +0530 Subject: [PATCH] [Resource-Encapsulation] Java SDK for resource-encapsulation layer - SDK for client side [updated as per review comments] - SDK for server side [updated as per review comments] Change-Id: Ie13656f263d39377269dd2479c43ef2a3efaeac0 Signed-off-by: Jay Sharma Reviewed-on: https://gerrit.iotivity.org/gerrit/2372 Tested-by: jenkins-iotivity Reviewed-by: Madan Lanka --- .../src/com/re/sampleclient/ResourceClient.java | 22 +- .../sdk/java/AndroidManifest.xml | 10 + .../resource-encapsulation/sdk/java/jni/Android.mk | 85 +++ .../sdk/java/jni/Application.mk | 2 + .../sdk/java/jni/jniutil/inc/jni_re_jvm.h | 134 +++++ .../sdk/java/jni/jniutil/inc/jni_re_utils.h | 158 ++++++ .../sdk/java/jni/jniutil/src/jni_re_jvm.cpp | 222 ++++++++ .../sdk/java/jni/jniutil/src/jni_re_utils.cpp | 253 +++++++++ .../re/include/jni_re_attribute_updated_listener.h | 69 +++ .../jni/re/include/jni_re_cache_update_listener.h | 64 +++ .../re/include/jni_re_discover_resource_listener.h | 69 +++ .../jni/re/include/jni_re_get_request_listener.h | 69 +++ .../jni/re/include/jni_re_getattributes_listener.h | 67 +++ .../java/jni/re/include/jni_re_listener_manager.h | 145 +++++ .../re/include/jni_re_listener_manager_server.h | 145 +++++ .../jni/re/include/jni_re_rcs_discovery_manager.h | 82 +++ .../re/include/jni_re_rcs_remoteresourceobject.h | 319 +++++++++++ .../jni/re/include/jni_re_resource_attributes.h | 228 ++++++++ .../java/jni/re/include/jni_re_resource_builder.h | 65 +++ .../java/jni/re/include/jni_re_resource_object.h | 333 ++++++++++++ .../include/jni_re_resource_statechange_listener.h | 67 +++ .../jni/re/include/jni_re_set_request_listener.h | 69 +++ .../jni/re/include/jni_re_setattributes_listener.h | 67 +++ .../re/src/jni_re_attribute_updated_listener.cpp | 152 ++++++ .../jni/re/src/jni_re_cache_update_listener.cpp | 117 ++++ .../re/src/jni_re_discover_resource_listener.cpp | 125 +++++ .../jni/re/src/jni_re_get_request_listener.cpp | 184 +++++++ .../jni/re/src/jni_re_getattributes_listener.cpp | 118 ++++ .../jni/re/src/jni_re_rcs_discovery_manager.cpp | 137 +++++ .../jni/re/src/jni_re_rcs_remoteresourceobject.cpp | 502 +++++++++++++++++ .../java/jni/re/src/jni_re_resource_attributes.cpp | 289 ++++++++++ .../java/jni/re/src/jni_re_resource_builder.cpp | 81 +++ .../sdk/java/jni/re/src/jni_re_resource_object.cpp | 599 +++++++++++++++++++++ .../src/jni_re_resource_statechange_listener.cpp | 103 ++++ .../jni/re/src/jni_re_set_request_listener.cpp | 214 ++++++++ .../jni/re/src/jni_re_setattributes_listener.cpp | 118 ++++ .../sdk/java/project.properties | 15 + .../ResourceEncapsulation/client/RCSAddress.java | 114 ++++ .../client/RCSDiscoveryManager.java | 178 ++++++ .../client/RCSRemoteResourceObject.java | 559 +++++++++++++++++++ .../ResourceEncapsulation/common/RCSException.java | 37 ++ .../common/RCSResourceAttributes.java | 295 ++++++++++ .../ResourceEncapsulation/server/Builder.java | 140 +++++ .../server/RCSGetResponse.java | 158 ++++++ .../ResourceEncapsulation/server/RCSRequest.java | 48 ++ .../server/RCSResourceObject.java | 543 +++++++++++++++++++ .../server/RCSSetResponse.java | 247 +++++++++ .../ResourceEncapsulation/utils/DiscoveryType.java | 57 ++ .../utils/EntityHandlerResult.java | 38 ++ 49 files changed, 7901 insertions(+), 11 deletions(-) create mode 100644 service/resource-encapsulation/sdk/java/AndroidManifest.xml create mode 100644 service/resource-encapsulation/sdk/java/jni/Android.mk create mode 100644 service/resource-encapsulation/sdk/java/jni/Application.mk create mode 100644 service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_jvm.h create mode 100644 service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_utils.h create mode 100644 service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_jvm.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_utils.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_attribute_updated_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_cache_update_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_discover_resource_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_get_request_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_getattributes_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager_server.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_discovery_manager.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_remoteresourceobject.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_attributes.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_builder.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_object.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_statechange_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_set_request_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/include/jni_re_setattributes_listener.h create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_attribute_updated_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_cache_update_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_discover_resource_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_get_request_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_getattributes_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_discovery_manager.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_remoteresourceobject.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_attributes.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_builder.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_object.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_statechange_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_set_request_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/jni/re/src/jni_re_setattributes_listener.cpp create mode 100644 service/resource-encapsulation/sdk/java/project.properties create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSAddress.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSDiscoveryManager.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSException.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSResourceAttributes.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/Builder.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSGetResponse.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSRequest.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSResourceObject.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSSetResponse.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/DiscoveryType.java create mode 100644 service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/EntityHandlerResult.java diff --git a/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClient.java b/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClient.java index a60ef91..0e018a7 100644 --- a/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClient.java +++ b/service/resource-encapsulation/examples/android/SampleResourceClientApp/src/com/re/sampleclient/ResourceClient.java @@ -21,18 +21,13 @@ package com.re.sampleclient; import java.util.Vector; -import org.iotivity.ResourceEncapsulation.client.ICacheUpdateListener; -import org.iotivity.ResourceEncapsulation.client.IDiscoverResourceListener; -import org.iotivity.ResourceEncapsulation.client.IRemoteAttributeListener; -import org.iotivity.ResourceEncapsulation.client.IStateChangedCallbackListener; import org.iotivity.ResourceEncapsulation.client.RCSAddress; import org.iotivity.ResourceEncapsulation.client.RCSDiscoveryManager; +import org.iotivity.ResourceEncapsulation.client.RCSDiscoveryManager.*; import org.iotivity.ResourceEncapsulation.client.RCSRemoteResourceObject; - -import org.iotivity.ResourceEncapsulation.utils.CacheState; +import org.iotivity.ResourceEncapsulation.client.RCSRemoteResourceObject.*; import org.iotivity.ResourceEncapsulation.common.RCSException; import org.iotivity.ResourceEncapsulation.common.RCSResourceAttributes; -import org.iotivity.ResourceEncapsulation.utils.ResourceState; import android.os.Message; import android.util.Log; @@ -62,7 +57,7 @@ public class ResourceClient { // constructor public ResourceClient() { - discoveyManagerObj = new RCSDiscoveryManager(); + discoveyManagerObj = RCSDiscoveryManager.getInstance(); discoverResourceListener = new DiscoverResourceListener(); stateChangedListener = new StateChangedListener(); cacheUpdateListener = new CacheUpdateListener(); @@ -78,7 +73,7 @@ public class ResourceClient { /** * Listener for receiving Resource discovered in network. */ - private class DiscoverResourceListener implements IDiscoverResourceListener { + private class DiscoverResourceListener implements IDiscoverResourceListener{ @Override public void onResourceDiscovered(RCSRemoteResourceObject foundResource) { @@ -124,7 +119,7 @@ public class ResourceClient { private class StateChangedListener implements IStateChangedCallbackListener { @Override - public void onStateChangedCallback(ResourceState resourceState) { + public void onStateChanged(ResourceState resourceState) { Log.i(LOG_TAG, "onStateChangedCallback invoked"); switch (resourceState) { @@ -195,7 +190,12 @@ public class ResourceClient { public void disocverResources() { Log.d(LOG_TAG, "discover resources entry"); RCSAddress address = RCSAddress.multicast(); - discoveyManagerObj.discoverResource(address); + try { + discoveyManagerObj.discoverResource(address); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } Log.d(LOG_TAG, "discover resources exit"); } diff --git a/service/resource-encapsulation/sdk/java/AndroidManifest.xml b/service/resource-encapsulation/sdk/java/AndroidManifest.xml new file mode 100644 index 0000000..0496cd0 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/AndroidManifest.xml @@ -0,0 +1,10 @@ + + + + + diff --git a/service/resource-encapsulation/sdk/java/jni/Android.mk b/service/resource-encapsulation/sdk/java/jni/Android.mk new file mode 100644 index 0000000..62b4335 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/Android.mk @@ -0,0 +1,85 @@ +LOCAL_PATH := $(call my-dir) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../out/android/$(TARGET_ARCH_ABI)/release +LOCAL_MODULE := android-oc_logger +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/liboc_logger.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../out/android/$(TARGET_ARCH_ABI)/release +LOCAL_MODULE := android-octbstack +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/liboctbstack.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../out/android/$(TARGET_ARCH_ABI)/release +LOCAL_MODULE := android-oc +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/liboc.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../out/android/$(TARGET_ARCH_ABI)/release +LOCAL_MODULE := android-connectivity_abstraction +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/libconnectivity_abstraction.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../android/android_api/base/libs/$(TARGET_ARCH_ABI) +LOCAL_MODULE := android-ocstack-jni +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/libocstack-jni.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../android/android_api/base/libs/$(TARGET_ARCH_ABI) +LOCAL_MODULE := android-ca-interface +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/libca-interface.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../out/android/$(TARGET_ARCH_ABI)/release +LOCAL_MODULE := android-resourcecommon +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/librcs_common.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../out/android/$(TARGET_ARCH_ABI)/release +LOCAL_MODULE := android-resourceclient +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/librcs_client.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_LIB_PATH := ../../../../../out/android/$(TARGET_ARCH_ABI)/release +LOCAL_MODULE := android-resourceserver +LOCAL_SRC_FILES := $(OIC_LIB_PATH)/librcs_server.so +include $(PREBUILT_SHARED_LIBRARY) + +include $(CLEAR_VARS) +OIC_SRC_DIR := ../../../../.. +LOCAL_MODULE := re-jni + +LOCAL_C_INCLUDES := $(LOCAL_PATH)/jniutil/inc \ + $(OIC_SRC_DIR)/android/android_api/base/jni \ + $(OIC_SRC_DIR)/resource/include \ + $(OIC_SRC_DIR)/resource/c_common \ + $(OIC_SRC_DIR)/resource/csdk/stack/include \ + $(OIC_SRC_DIR)/resource/oc_logger/include \ + $(OIC_SRC_DIR)/extlibs/boost/boost_1_58_0 \ + $(OIC_SRC_DIR)/extlibs/timer \ + $(OIC_SRC_DIR)/service/resource-encapsulation/include \ + $(LOCAL_PATH)/re/include + +LOCAL_SRC_FILES += $(patsubst $(LOCAL_PATH)/%, %, $(wildcard $(LOCAL_PATH)/jniutil/src/*.cpp)) +LOCAL_SRC_FILES += $(patsubst $(LOCAL_PATH)/%, %, $(wildcard $(LOCAL_PATH)/re/src/*.cpp)) + +LOCAL_CPPFLAGS := -std=c++0x -frtti -fexceptions + +LOCAL_LDLIBS := -llog +LOCAL_SHARED_LIBRARIES := android-ocstack-jni +LOCAL_SHARED_LIBRARIES += android-resourcecommon +LOCAL_SHARED_LIBRARIES += android-resourceclient +LOCAL_SHARED_LIBRARIES += android-resourceserver +LOCAL_SHARED_LIBRARIES += android-oc +LOCAL_SHARED_LIBRARIES += android-octbstack + +include $(BUILD_SHARED_LIBRARY) diff --git a/service/resource-encapsulation/sdk/java/jni/Application.mk b/service/resource-encapsulation/sdk/java/jni/Application.mk new file mode 100644 index 0000000..c177a13 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/Application.mk @@ -0,0 +1,2 @@ +NDK_TOOLCHAIN_VERSION := 4.9 +APP_STL := gnustl_shared diff --git a/service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_jvm.h b/service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_jvm.h new file mode 100644 index 0000000..ce6e70b --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_jvm.h @@ -0,0 +1,134 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** + * @file jni_re_jvm.h + * + * @brief This file contains the essential declarations and functions required + * for JNI implementation + */ + +#ifndef __JNI_RE_JVM_H +#define __JNI_RE_JVM_H + +#include +#include +#include + +#define RETAG "RE-JNI" +#define JNI_CURRENT_VERSION JNI_VERSION_1_6 + +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, RETAG, __VA_ARGS__) +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, RETAG, __VA_ARGS__) +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, RETAG, __VA_ARGS__) + +extern JavaVM *g_jvm; + +extern jclass g_cls_Integer; +extern jclass g_cls_Double; +extern jclass g_cls_Boolean; +extern jclass g_cls_String; +extern jclass g_cls_LinkedList; +extern jclass g_cls_Iterator; +extern jclass g_cls_Vector; +extern jclass g_cls_RCSRemoteResourceObject; +extern jclass g_cls_RCSRemoteResourceAttributesObject; +extern jclass g_cls_RCSException; + +extern jclass g_cls_RCSResourceObject; +extern jclass g_cls_RCSRequest; + +extern jmethodID g_mid_Integer_ctor; +extern jmethodID g_mid_Double_ctor; +extern jmethodID g_mid_Boolean_ctor; +extern jmethodID g_mid_LinkedList_ctor; +extern jmethodID g_mid_LinkedList_add_object; +extern jmethodID g_mid_Iterator_hasNext; +extern jmethodID g_mid_Iterator_next; +extern jmethodID g_mid_RCSRemoteResourceObject_ctor; +extern jmethodID g_mid_RCSRemoteResourceAttributesObject_ctor; +extern jmethodID g_mid_RCSException_ctor; + +extern jmethodID g_mid_RCSResourceObject_ctor; +extern jmethodID g_mid_RCSRequest_ctor; + +typedef void(*RemoveListenerCallback)(JNIEnv *env, jobject jListener); + +/** + * @brief Get the native handle field + */ +static jfieldID REGetHandleField(JNIEnv *env, jobject jobj) +{ + jclass cls = env->GetObjectClass(jobj); + return env->GetFieldID(cls, "m_nativeHandle", "J"); +} + +/** + * @brief Get the native handle + */ +template +static T *REGetHandle(JNIEnv *env, jobject jobj) +{ + jlong handle = env->GetLongField(jobj, REGetHandleField(env, jobj)); + return reinterpret_cast(handle); +} + +/** + * @brief Set the native handle + */ +template +static void RESetHandle(JNIEnv *env, jobject jobj, T *type) +{ + jlong handle = reinterpret_cast(type); + + env->SetLongField(jobj, REGetHandleField(env, jobj), handle); +} + +/** + * @brief Get the JNI Environment + */ +static JNIEnv *GetREJNIEnv(jint &ret) +{ + JNIEnv *env = NULL; + + ret = g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION); + switch (ret) + { + case JNI_OK: + return env; + case JNI_EDETACHED: + if (g_jvm->AttachCurrentThread(&env, NULL) < 0) + { + LOGE("Failed to get the environment"); + return nullptr; + } + else + { + return env; + } + + case JNI_EVERSION: + LOGE("JNI version not supported"); + default: + LOGE("Failed to get the environment"); + return nullptr; + } +} +#endif // __JNI_RE_JVM_H diff --git a/service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_utils.h b/service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_utils.h new file mode 100644 index 0000000..548f88b --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/jniutil/inc/jni_re_utils.h @@ -0,0 +1,158 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** + * @file jni_re_utils.h + * + * @brief This file contains the utility functions for JNI + */ + +#ifndef __JNI_RE_UTILS_H_ +#define __JNI_RE_UTILS_H_ + +#include "jni_re_jvm.h" +#include "RCSRemoteResourceObject.h" +#include "RCSResourceObject.h" + +#include + +using namespace OIC::Service; + +/** + * @brief Convert Native ResourceState to Integer + */ +int covertResourceStateToInt(ResourceState state); + +/** + * @brief Convert Native CacheState to Integer + */ +int convertCacheStateToInt(CacheState state); + +/** + * @brief Native Vector to Java list + */ +jobject convertStrVectorToJavaStrList(JNIEnv *env, std::vector vector); + +/** + * @brief Convert Native ResourceState to Java ResourceState + */ +jobject convertNativeResourceStateToJavaResourceState(ResourceState nativeResourceState, + JNIEnv *env); + + +/** + * @brief Convert Java AutoNotifyPolicy to Native AutoNotifyPolicy + */ +RCSResourceObject::AutoNotifyPolicy convertJavaNotifyPolicyToNativePolicy( int policy); + +/** + * @brief Convert AutoNotifyPolicy to integer + */ +int convertAutoNotifyPolicyToInt(RCSResourceObject::AutoNotifyPolicy policy); + +/** + *@brief Convert Java setRequestHandlerPolicy to Native setRequestHandlerPolicy + */ +RCSResourceObject::SetRequestHandlerPolicy convertJavasetRequestHandlerPolicyToNativePolicy( + int policy); + +/** + *@brief Convert setRequestHandlerPolicy to int + */ +int convertSetRequestHandlerPolicyToInt( RCSResourceObject::SetRequestHandlerPolicy policy); + + +OCEntityHandlerResult convertIntToOCEntityHandlerResult(int result); + +/** + * @brief Throw the RCS exception to the upper layer + */ +void throwRCSException(JNIEnv *env, std::string reason); + +/** + * @class ResourceStateWrapper + * @brief This class is JNI wrapper for Java ResourceState. + * + */ +class ResourceStateWrapper +{ + ResourceStateWrapper() {} + public : + + //Field IDs + static jfieldID ALIVE_ID; + static jfieldID REQUESTED_ID; + static jfieldID LOST_SIGNAL_ID; + static jfieldID DESTROYED_ID; + static jfieldID NONE_ID; + + //Method IDs + static jmethodID ordinal_ID; + static jmethodID toString_ID; + static jmethodID valueOf_ID; + + static jclass clazz; + + /** + * @brief Get jobject for ALIVE ResourceState + */ + static jobject getALIVE(JNIEnv *env); + + /** + * @brief Get jobject for REQUESTED ResourceState + */ + static jobject getREQUESTED(JNIEnv *env); + + /** + * @brief Get jobject for SIGNAL ResourceState + */ + static jobject getLOST_SIGNAL(JNIEnv *env); + + /** + * @brief Get jobject for DESTROYED ResourceState + */ + static jobject getDESTROYED(JNIEnv *env); + + /** + * @brief Get jobject for NONE ResourceState + */ + static jobject getNONE(JNIEnv *env); + + /** + * @brief Returns the ordinal value of Enum Object + */ + static jint ordinal(JNIEnv *env, jobject enumObj); + + /** + * @brief Returns the name of Enum Object + */ + static jstring toString(JNIEnv *env, jobject enumObj); + + /** + * @brief Returns Enum Object by name + */ + static jobject valueOf(JNIEnv *env, jstring enumName); + + /** + * @brief virtual destructor + */ + virtual ~ResourceStateWrapper() {}; +}; +#endif //__JNI_RE_UTILS_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_jvm.cpp b/service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_jvm.cpp new file mode 100644 index 0000000..9fe316e --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_jvm.cpp @@ -0,0 +1,222 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +#include "jni_re_jvm.h" +#include "jni_re_utils.h" + +JavaVM *g_jvm = NULL; + +jclass g_cls_Integer = NULL; +jclass g_cls_Double = NULL; +jclass g_cls_Boolean = NULL; +jclass g_cls_String = NULL; +jclass g_cls_LinkedList = NULL; +jclass g_cls_Iterator = NULL; +jclass g_cls_Vector = NULL; +jclass g_cls_RCSRemoteResourceObject = NULL; +jclass g_cls_RCSRemoteResourceAttributesObject = NULL; +jclass g_cls_RCSException = NULL; + +jclass g_cls_RCSResourceObject = NULL; +jclass g_cls_RCSRequest = NULL; + +jmethodID g_mid_Integer_ctor = NULL; +jmethodID g_mid_Double_ctor = NULL; +jmethodID g_mid_Boolean_ctor = NULL; +jmethodID g_mid_LinkedList_ctor = NULL; +jmethodID g_mid_LinkedList_add_object = NULL; +jmethodID g_mid_Iterator_hasNext = NULL; +jmethodID g_mid_Iterator_next = NULL; +jmethodID g_mid_RCSRemoteResourceObject_ctor = NULL; +jmethodID g_mid_RCSRemoteResourceAttributesObject_ctor = NULL; +jmethodID g_mid_RCSException_ctor = NULL; + +jmethodID g_mid_RCSResourceObject_ctor = NULL; +jmethodID g_mid_RCSRequest_ctor = NULL; + + +// JNI OnLoad +JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) +{ + LOGI("JNI_OnLoad"); + JNIEnv *env; + g_jvm = vm; + + if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK) + { + LOGE("Failed to get the environment using GetEnv()"); + return JNI_ERR; + } + + jclass clazz = nullptr; + + //Integer + clazz = env->FindClass("java/lang/Integer"); + if (!clazz) return JNI_ERR; + g_cls_Integer = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_Integer_ctor = env->GetMethodID(g_cls_Integer, "", "(I)V"); + if (!g_mid_Integer_ctor) return JNI_ERR; + + //Double + clazz = env->FindClass("java/lang/Double"); + if (!clazz) return JNI_ERR; + g_cls_Double = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_Double_ctor = env->GetMethodID(g_cls_Double, "", "(D)V"); + if (!g_mid_Double_ctor) return JNI_ERR; + + //Boolean + clazz = env->FindClass("java/lang/Boolean"); + if (!clazz) return JNI_ERR; + g_cls_Boolean = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_Boolean_ctor = env->GetMethodID(g_cls_Boolean, "", "(Z)V"); + if (!g_mid_Boolean_ctor) return JNI_ERR; + + //String + clazz = env->FindClass("java/lang/String"); + if (!clazz) return JNI_ERR; + g_cls_String = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + //LinkedList + clazz = env->FindClass("java/util/LinkedList"); + if (!clazz) return JNI_ERR; + g_cls_LinkedList = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_LinkedList_ctor = env->GetMethodID(g_cls_LinkedList, "", "()V"); + if (!g_mid_LinkedList_ctor) return JNI_ERR; + + g_mid_LinkedList_add_object = env->GetMethodID(g_cls_LinkedList, "add", "(Ljava/lang/Object;)Z"); + if (!g_mid_LinkedList_add_object) return JNI_ERR; + + //Iterator + clazz = env->FindClass("java/util/Iterator"); + if (!clazz) return JNI_ERR; + g_cls_Iterator = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_Iterator_hasNext = env->GetMethodID(g_cls_Iterator, "hasNext", "()Z"); + if (!g_mid_Iterator_hasNext) return JNI_ERR; + + g_mid_Iterator_next = env->GetMethodID(g_cls_Iterator, "next", "()Ljava/lang/Object;"); + if (!g_mid_Iterator_next) return JNI_ERR; + + //Vector + clazz = env->FindClass("java/util/Vector"); + if (!clazz) return JNI_ERR; + g_cls_Vector = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + //RCSRemoteResourceObject + clazz = env->FindClass("org/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject"); + if (!clazz) return JNI_ERR; + g_cls_RCSRemoteResourceObject = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_RCSRemoteResourceObject_ctor = env->GetMethodID(g_cls_RCSRemoteResourceObject, "", + "(J)V"); + if (!g_mid_RCSRemoteResourceObject_ctor) return JNI_ERR; + + //ResourceState enum + jclass tmpclazz = reinterpret_cast + (env->FindClass("org/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState")); + if (!tmpclazz) { + LOGE("Failed to get the class ResourceState"); + return JNI_ERR; + } + ResourceStateWrapper::clazz = reinterpret_cast (env->NewGlobalRef(tmpclazz)); + env->DeleteLocalRef(tmpclazz); + + //RCS Remote Resource Attributes + clazz = env->FindClass("org/iotivity/ResourceEncapsulation/common/RCSResourceAttributes"); + if (!clazz) return JNI_ERR; + g_cls_RCSRemoteResourceAttributesObject = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_RCSRemoteResourceAttributesObject_ctor = env->GetMethodID( + g_cls_RCSRemoteResourceAttributesObject, "", "(J)V"); + if (!g_mid_RCSRemoteResourceAttributesObject_ctor) return JNI_ERR; + + //RCSException + clazz = env->FindClass("org/iotivity/ResourceEncapsulation/common/RCSException"); + if (!clazz) return JNI_ERR; + g_cls_RCSException = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_RCSException_ctor = env->GetMethodID(g_cls_RCSException, "", "(Ljava/lang/String;)V"); + if (!g_mid_RCSException_ctor) return JNI_ERR; + + //RCSResourceObject + clazz = env->FindClass("org/iotivity/ResourceEncapsulation/server/RCSResourceObject"); + if (!clazz) return JNI_ERR; + g_cls_RCSResourceObject = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_RCSResourceObject_ctor = env->GetMethodID(g_cls_RCSResourceObject, "", + "(J)V"); + if (!g_mid_RCSResourceObject_ctor) return JNI_ERR; + + //RCSRequest + clazz = env->FindClass("org/iotivity/ResourceEncapsulation/server/RCSRequest"); + if (!clazz) return JNI_ERR; + g_cls_RCSRequest = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + /* g_mid_RCSRequest_ctor = env->GetMethodID(g_cls_RCSRequest, "", + "(J)V"); + if (!g_mid_RCSRequest_ctor) return JNI_ERR;*/ + + return JNI_CURRENT_VERSION; +} + +JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) +{ + LOGI("JNI_OnUnload"); + JNIEnv *env; + + if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK) + { + LOGE("Failed to get the environment using GetEnv()"); + return; + } + + env->DeleteGlobalRef(g_cls_Integer); + env->DeleteGlobalRef(g_cls_Double); + env->DeleteGlobalRef(g_cls_Boolean); + env->DeleteGlobalRef(g_cls_String); + env->DeleteGlobalRef(g_cls_LinkedList); + env->DeleteGlobalRef(g_cls_Iterator); + env->DeleteGlobalRef(g_cls_RCSRemoteResourceObject); + env->DeleteGlobalRef(g_cls_RCSRemoteResourceAttributesObject); + env->DeleteGlobalRef(g_cls_RCSException); + env->DeleteGlobalRef(g_cls_RCSResourceObject); + env->DeleteGlobalRef(g_cls_RCSRequest); + + if (env) + { + if (ResourceStateWrapper::clazz != 0) + env->DeleteGlobalRef(ResourceStateWrapper::clazz); + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_utils.cpp b/service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_utils.cpp new file mode 100644 index 0000000..5466f72 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/jniutil/src/jni_re_utils.cpp @@ -0,0 +1,253 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +#include "jni_re_utils.h" + +int covertResourceStateToInt(ResourceState state) +{ + switch (state) + { + case ResourceState::NONE: + return 0; + + case ResourceState::REQUESTED: + return 1; + + case ResourceState::ALIVE: + return 2; + + case ResourceState::LOST_SIGNAL: + return 3; + + case ResourceState::DESTROYED: + return 4; + } + return 0; +} + +int convertCacheStateToInt(CacheState state) +{ + switch (state) + { + case CacheState::NONE: + return 0; + + case CacheState::UNREADY: + return 1; + + case CacheState::READY: + return 2; + + case CacheState::LOST_SIGNAL: + return 3; + } + return 0; +} + +jobject convertStrVectorToJavaStrList(JNIEnv *env, std::vector vector) +{ + jobject jList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor); + if (!jList) return nullptr; + for (size_t i = 0; i < vector.size(); ++i) + { + jstring jStr = env->NewStringUTF(vector[i].c_str()); + if (!jStr) return nullptr; + env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr); + if (env->ExceptionCheck()) return nullptr; + env->DeleteLocalRef(jStr); + } + return jList; +} + +OCEntityHandlerResult convertIntToOCEntityHandlerResult(int result) +{ + switch (result) + { + case 1: + return OCEntityHandlerResult::OC_EH_OK; + case 2: + return OCEntityHandlerResult::OC_EH_ERROR; + case 3: + return OCEntityHandlerResult::OC_EH_RESOURCE_CREATED; + case 4: + return OCEntityHandlerResult::OC_EH_RESOURCE_DELETED; + case 5: + return OCEntityHandlerResult::OC_EH_SLOW; + case 6: + return OCEntityHandlerResult::OC_EH_FORBIDDEN; + case 7: + return OCEntityHandlerResult::OC_EH_RESOURCE_NOT_FOUND; + } + return OCEntityHandlerResult::OC_EH_ERROR; +} + +void throwRCSException(JNIEnv *env, std::string reason) +{ + jobject ex = env->NewObject(g_cls_RCSException, g_mid_RCSException_ctor, + env->NewStringUTF(reason.c_str())); + if (!ex) + { + LOGI("throwRCSException jobject == NULL"); + } + env->Throw((jthrowable)ex); +} + +jfieldID ResourceStateWrapper::ALIVE_ID = 0; +jfieldID ResourceStateWrapper::REQUESTED_ID = 0; +jfieldID ResourceStateWrapper::LOST_SIGNAL_ID = 0; +jfieldID ResourceStateWrapper::DESTROYED_ID = 0; +jfieldID ResourceStateWrapper::NONE_ID = 0; + +jmethodID ResourceStateWrapper::ordinal_ID = 0; +jmethodID ResourceStateWrapper::toString_ID = 0; +jmethodID ResourceStateWrapper::valueOf_ID = 0; +jclass ResourceStateWrapper::clazz = 0; + +jobject ResourceStateWrapper::getALIVE(JNIEnv *env) +{ + return env->GetStaticObjectField(clazz, ALIVE_ID); +} +jobject ResourceStateWrapper::getREQUESTED(JNIEnv *env) +{ + return env->GetStaticObjectField(clazz, REQUESTED_ID); +} +jobject ResourceStateWrapper::getLOST_SIGNAL(JNIEnv *env) +{ + return env->GetStaticObjectField(clazz, LOST_SIGNAL_ID); +} +jobject ResourceStateWrapper::getDESTROYED(JNIEnv *env) +{ + return env->GetStaticObjectField(clazz, DESTROYED_ID); +} +jobject ResourceStateWrapper::getNONE(JNIEnv *env) +{ + return env->GetStaticObjectField(clazz, NONE_ID); +} +jint ResourceStateWrapper::ordinal(JNIEnv *env, jobject enumObj) +{ + return env->CallIntMethod(enumObj, ordinal_ID); +} +jstring ResourceStateWrapper::toString(JNIEnv *env, jobject enumObj) +{ + return reinterpret_cast(env->CallObjectMethod(enumObj, toString_ID)); +} +jobject ResourceStateWrapper::valueOf(JNIEnv *env, jstring enumName) +{ + return env->CallStaticObjectMethod(clazz, valueOf_ID, (jobject)enumName); +} + +jobject convertNativeResourceStateToJavaResourceState(ResourceState nativeResourceState, + JNIEnv *env) +{ + LOGI("convertNativeResourceStateToJavaResourceState enter"); + + ResourceStateWrapper::ALIVE_ID = env->GetStaticFieldID(ResourceStateWrapper::clazz, "ALIVE", + "Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState;"); + ResourceStateWrapper::REQUESTED_ID = env->GetStaticFieldID(ResourceStateWrapper::clazz, "REQUESTED", + "Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState;"); + ResourceStateWrapper::LOST_SIGNAL_ID = env->GetStaticFieldID(ResourceStateWrapper::clazz, + "LOST_SIGNAL", "Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState;"); + ResourceStateWrapper::DESTROYED_ID = env->GetStaticFieldID(ResourceStateWrapper::clazz, "DESTROYED", + "Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState;"); + ResourceStateWrapper::NONE_ID = env->GetStaticFieldID(ResourceStateWrapper::clazz, "NONE", + "Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState;"); + + ResourceStateWrapper::ordinal_ID = env->GetMethodID(ResourceStateWrapper::clazz, "ordinal", "()I"); + ResourceStateWrapper::toString_ID = env->GetMethodID(ResourceStateWrapper::clazz, "toString", + "()Ljava/lang/String;"); + ResourceStateWrapper::valueOf_ID = env->GetStaticMethodID(ResourceStateWrapper::clazz, "valueOf", + "(Ljava/lang/String;)Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState;"); + + LOGI("convertNativeResourceStateToJavaResourceState ResourceStateWrapper::clazz == NULL first time"); + + switch (nativeResourceState) + { + case ResourceState::NONE: + return ResourceStateWrapper::getNONE(env); + + case ResourceState::REQUESTED: + return ResourceStateWrapper::getREQUESTED(env); + + case ResourceState::ALIVE: + return ResourceStateWrapper::getALIVE(env); + + case ResourceState::LOST_SIGNAL: + return ResourceStateWrapper::getLOST_SIGNAL(env); + + case ResourceState::DESTROYED: + return ResourceStateWrapper::getDESTROYED(env); + } +} + +RCSResourceObject::AutoNotifyPolicy convertJavaNotifyPolicyToNativePolicy( int policy) +{ + switch (policy) + { + case 1: + return RCSResourceObject::AutoNotifyPolicy::NEVER; + + case 2: + return RCSResourceObject::AutoNotifyPolicy::ALWAYS; + + case 3: + return RCSResourceObject::AutoNotifyPolicy::UPDATED; + } +} + +RCSResourceObject::SetRequestHandlerPolicy convertJavasetRequestHandlerPolicyToNativePolicy( + int policy) +{ + switch (policy) + { + case 1: + return RCSResourceObject::SetRequestHandlerPolicy::NEVER; + + case 2: + return RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE; + } +} + +int convertAutoNotifyPolicyToInt(RCSResourceObject::AutoNotifyPolicy policy) +{ + switch (policy) + { + case RCSResourceObject::AutoNotifyPolicy::NEVER: + return 0; + + case RCSResourceObject::AutoNotifyPolicy::ALWAYS: + return 1; + + case RCSResourceObject::AutoNotifyPolicy::UPDATED: + return 2; + } + return 0; +} + +int convertSetRequestHandlerPolicyToInt(RCSResourceObject::SetRequestHandlerPolicy policy) +{ + switch (policy) + { + case RCSResourceObject::SetRequestHandlerPolicy::NEVER: + return 0; + + case RCSResourceObject::SetRequestHandlerPolicy::ACCEPTANCE: + return 1; + } + return 0; +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_attribute_updated_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_attribute_updated_listener.h new file mode 100644 index 0000000..174b2ff --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_attribute_updated_listener.h @@ -0,0 +1,69 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_attribute_updated_listener.h + * + * @brief This file contains JNI attribute update Listener class + */ + +#ifndef __JNI_RE_ATTRIBUTE_UPDATED_LISTENER_H_ +#define __JNI_RE_ATTRIBUTE_UPDATED_LISTENER_H_ + +#include + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" +#include "jni_re_resource_attributes.h" + + +class JniRCSResourceObject; + +/** + * @class JniAttributeUpdatedListener + * @brief This class provides functions for handling the attribute updated callback between the Java and Native layer + * + */ +class JniAttributeUpdatedListener +{ + public: + /** + * @brief constructor + */ + JniAttributeUpdatedListener(JNIEnv *env, jobject jListener, + JniRCSResourceObject *resource); + + /** + * @brief destructor + */ + ~JniAttributeUpdatedListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + void onAttributeUpdatedCallback(OIC::Service::RCSResourceAttributes::Value oldValue, + OIC::Service::RCSResourceAttributes::Value newValue); + + private: + jweak m_jwListener; + JniRCSResourceObject *m_ownerResource; + void checkExAndRemoveListener(JNIEnv *env); +}; + +#endif //__JNI_RE_ATTRIBUTE_UPDATED_LISTENER_H_ \ No newline at end of file diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_cache_update_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_cache_update_listener.h new file mode 100644 index 0000000..215ca32 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_cache_update_listener.h @@ -0,0 +1,64 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_cache_update_listener.h + * + * @brief This file contains JNI CacheUpdate Listener class + */ + +#ifndef __JNI_RE_CACHE_UPDATE_LISTENER_H_ +#define __JNI_RE_CACHE_UPDATE_LISTENER_H_ + +#include + +#include "RCSRemoteResourceObject.h" + +#include "jni_re_jvm.h" +#include "jni_re_rcs_remoteresourceobject.h" + +/** + * @class JniCacheUpdateListener + * @brief This class provides functions for handling the CacheUpdate Callback between the Java and Native layer + * + */ +class JniCacheUpdateListener +{ + public: + /** + * @brief constructor + */ + JniCacheUpdateListener(JNIEnv *env, jobject jListener, JniRCSRemoteResourceObject *resource); + + /** + * @brief destructor + */ + ~JniCacheUpdateListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + void cacheUpdatedCallback(OIC::Service::RCSResourceAttributes attributes); + + private: + jweak m_jwListener; + JniRCSRemoteResourceObject *m_ownerResource; + void checkExAndRemoveListener(JNIEnv *env); +}; +#endif //__JNI_RE_CACHE_UPDATE_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_discover_resource_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_discover_resource_listener.h new file mode 100644 index 0000000..bcc2a05 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_discover_resource_listener.h @@ -0,0 +1,69 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_discover_resource_listener.h + * + * @brief This file contains JNI Discover Resource Listener class + */ + +#ifndef __JNI_RE_DISCOVER_RESOURCE_LISTENER_H_ +#define __JNI_RE_DISCOVER_RESOURCE_LISTENER_H_ + +#include + +#include "RCSRemoteResourceObject.h" + +#include "jni_re_jvm.h" +#include "jni_re_rcs_remoteresourceobject.h" + +typedef void(*RemoveListenerCallback)(JNIEnv *env, jobject jListener); + +class RCSRemoteResourceObject; + +/** + * @class JniDiscoverResourceListener + * @brief This class provides functions for handling the Discover Resource Callback between the Java and Native layer + * + */ +class JniDiscoverResourceListener +{ + public: + /** + * @brief constructor + */ + JniDiscoverResourceListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback); + + /** + * @brief destructor + */ + ~JniDiscoverResourceListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + void discoverResourceCallback(std::shared_ptr resource); + + private: + RemoveListenerCallback m_removeListenerCallback; + jweak m_jwListener; + void checkExAndRemoveListener(JNIEnv *env); +}; +#endif //__JNI_RE_DISCOVER_RESOURCE_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_get_request_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_get_request_listener.h new file mode 100644 index 0000000..867d9ac --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_get_request_listener.h @@ -0,0 +1,69 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_get_request_listener.h + * + * @brief This file contains JNI get request Listener class + */ + +#ifndef __JNI_RE_GET_REQUEST_LISTENER_H_ +#define __JNI_RE_GET_REQUEST_LISTENER_H_ + +#include + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" +#include "jni_re_resource_attributes.h" + + +class JniRCSResourceObject; + +/** + * @class JniGetRequestListener + * @brief This class provides functions for handling the get request callback between the Java and Native layer + * + */ +class JniGetRequestListener +{ + public: + /** + * @brief constructor + */ + JniGetRequestListener(JNIEnv *env, jobject jListener, + JniRCSResourceObject *resource); + + /** + * @brief destructor + */ + ~JniGetRequestListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + OIC::Service::RCSGetResponse onGetRequestCallback(OIC::Service::RCSRequest request, + OIC::Service::RCSResourceAttributes attributes); + + private: + jweak m_jwListener; + JniRCSResourceObject *m_ownerResource; + void checkExAndRemoveListener(JNIEnv *env); +}; + +#endif //__JNI_RE_GET_REQUEST_LISTENER_H_ \ No newline at end of file diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_getattributes_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_getattributes_listener.h new file mode 100644 index 0000000..ece0d0a --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_getattributes_listener.h @@ -0,0 +1,67 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_getattributes_listener.h + * + * @brief This file contains JNI get attributes Listener class + */ + +#ifndef __JNI_RE_GET_ATTRIBUTES_LISTENER_H_ +#define __JNI_RE_GET_ATTRIBUTES_LISTENER_H_ + +#include + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" +#include "jni_re_resource_attributes.h" + +class JniRCSRemoteResourceObject; + +/** + * @class JniGetAttributesListener + * @brief This class provides functions for handling the get attribute callback between the Java and Native layer + * + */ +class JniGetAttributesListener +{ + public: + /** + * @brief constructor + */ + JniGetAttributesListener(JNIEnv *env, jobject jListener, + JniRCSRemoteResourceObject *resource); + + /** + * @brief destructor + */ + ~JniGetAttributesListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + void onRemoteAttributesGetCallback(OIC::Service::RCSResourceAttributes attributes); + + private: + jweak m_jwListener; + JniRCSRemoteResourceObject *m_ownerResource; + void checkExAndRemoveListener(JNIEnv *env); +}; + +#endif //__JNI_RE_GET_ATTRIBUTES_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager.h new file mode 100644 index 0000000..c775305 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager.h @@ -0,0 +1,145 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_listener_manager.h + * + * @brief This file contains JNI Listener Manager class for client (jni_re_rcs_remoteresourceobject) + */ + +#ifndef __JNI_RE_LISTENER_MANAGER_LISTENER_H_ +#define __JNI_RE_LISTENER_MANAGER_LISTENER_H_ + +#include +#include + +class JniRCSRemoteResourceObject; + +/** + * @class JniReListenerManager + * @brief This class provides functions for managing listeners + * + */ +template +class JniReListenerManager +{ + public: + /** + * API for Adding the Listener to listener Map. + * + * @param owner - JniRCSRemoteResourceObject + */ + T *addListener(JNIEnv *env, jobject jListener, JniRCSRemoteResourceObject *owner) + { + T *onEventListener = NULL; + + m_mapMutex.lock(); + + for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onEventListener = refPair.first; + refPair.second++; + it->second = refPair; + m_listenerMap.insert(*it); + LOGD("OnEventListener: ref. count is incremented"); + break; + } + } + if (!onEventListener) + { + onEventListener = new T(env, jListener, owner); + jobject jgListener = env->NewGlobalRef(jListener); + + if (jgListener) + { + m_listenerMap.insert( + std::pair < jobject, + std::pair> (jgListener, std::pair(onEventListener, 1))); + } + else + { + LOGD("OnEventListener: Failed to create global listener ref."); + delete onEventListener; + } + LOGD("OnEventListener: new listener"); + } + m_mapMutex.unlock(); + return onEventListener; + } + + /** + * @brief API for removing the Listener from listener Map. + */ + void removeListener(JNIEnv *env, jobject jListener) + { + m_mapMutex.lock(); + for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + m_listenerMap.insert(*it); + LOGI("OnEventListener: ref. count is decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + T *listener = refPair.first; + delete listener; + m_listenerMap.erase(it); + + LOGI("OnEventListener is removed"); + } + break; + } + } + m_mapMutex.unlock(); + } + + /** + * @brief API for removing all the Listener from listener Map. + */ + void removeAllListeners(JNIEnv *env) + { + m_mapMutex.lock(); + + for (auto & pair : m_listenerMap) + { + env->DeleteGlobalRef(pair.first); + auto refPair = pair.second; + delete refPair.first; + } + m_listenerMap.clear(); + + m_mapMutex.unlock(); + } + + private: + std::map> m_listenerMap; + std::mutex m_mapMutex; +}; + +#endif //__JNI_RE_LISTENER_MANAGER_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager_server.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager_server.h new file mode 100644 index 0000000..19735e9 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_listener_manager_server.h @@ -0,0 +1,145 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_listener_manager_server.h + * + * @brief This file contains JNI Listener Manager class for server (jni_re_rcs_resource_object) + */ + +#ifndef __JNI_RE_LISTENER_MANAGER_LISTENER_H_ +#define __JNI_RE_LISTENER_MANAGER_LISTENER_H_ + +#include +#include + +class JniRemoteResourceObject; + +/** + * @class JniReListenerManagerServer + * @brief This class provides functions for managing listeners + * + */ +template +class JniReListenerManagerServer +{ + public: + /** + * API for Adding the Listener to listener Map. + * + * @param owner - JniRCSResourceObject + */ + T *addListener(JNIEnv *env, jobject jListener, JniRCSResourceObject *owner) + { + T *onEventListener = NULL; + + m_mapMutex.lock(); + + for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onEventListener = refPair.first; + refPair.second++; + it->second = refPair; + m_listenerMap.insert(*it); + LOGD("OnEventListener: ref. count is incremented"); + break; + } + } + if (!onEventListener) + { + onEventListener = new T(env, jListener, owner); + jobject jgListener = env->NewGlobalRef(jListener); + + if (jgListener) + { + m_listenerMap.insert( + std::pair < jobject, + std::pair> (jgListener, std::pair(onEventListener, 1))); + } + else + { + LOGD("OnEventListener: Failed to create global listener ref."); + delete onEventListener; + } + LOGD("OnEventListener: new listener"); + } + m_mapMutex.unlock(); + return onEventListener; + } + + /** + * @brief API for removing the Listener from listener Map. + */ + void removeListener(JNIEnv *env, jobject jListener) + { + m_mapMutex.lock(); + for (auto it = m_listenerMap.begin(); it != m_listenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + m_listenerMap.insert(*it); + LOGI("OnEventListener: ref. count is decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + T *listener = refPair.first; + delete listener; + m_listenerMap.erase(it); + + LOGI("OnEventListener is removed"); + } + break; + } + } + m_mapMutex.unlock(); + } + + /** + * @brief API for removing all the Listener from listener Map. + */ + void removeAllListeners(JNIEnv *env) + { + m_mapMutex.lock(); + + for (auto & pair : m_listenerMap) + { + env->DeleteGlobalRef(pair.first); + auto refPair = pair.second; + delete refPair.first; + } + m_listenerMap.clear(); + + m_mapMutex.unlock(); + } + + private: + std::map> m_listenerMap; + std::mutex m_mapMutex; +}; + +#endif //__JNI_RE_LISTENER_MANAGER_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_discovery_manager.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_discovery_manager.h new file mode 100644 index 0000000..c58fa05 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_discovery_manager.h @@ -0,0 +1,82 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_rcs_discovery_manager.h + * + * @brief This file contains the declaration of Discover Resource API + * for JNI implementation + */ + +#ifndef __JNI_RE_RCS_DISCOVERY_MANAGER_H_ +#define __JNI_RE_RCS_DISCOVERY_MANAGER_H_ + +#include +#include +#include +#include +#include +#include "jni_re_discover_resource_listener.h" + +#define LOG_TAG "JNIDM" + +/** +* @brief API for adding listener to discover resource listener Map. +*/ +JniDiscoverResourceListener *AddDiscoverResourceListener(JNIEnv *env, jobject jListener); + +/** +* @brief API for removing listener from discover resource listener Map. +*/ +void RemoveDiscoverResourceListener(JNIEnv *env, jobject jListener); + +/** +* @brief discover resource listener Map. +*/ +std::map> discoverResourceListenerMap; + +/** + * Mutex for thread synchronization + */ +std::mutex discoverResourceMapLock; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * API for discovering the resource of interest in the network + * + * @param address - address + * @param typeofDiscovery - multicast or unicast + * @param relativeURI - relative URI for the resource of interest + * @param resourceType - resource type + * @param discoveryListener - Listener to obtain discovered resource + * + * NOTE: 0 - Unicast 1 - Multicast + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSDiscoveryManager_discoverResource +(JNIEnv *env, jclass interfaceClass, jstring address, jint typeofDiscovery, jstring relativeURI, + jstring resourceType, jobject discoveryListener); + +#ifdef __cplusplus +} +#endif +#endif //__JNI_RE_RCS_DISCOVERY_MANAGER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_remoteresourceobject.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_remoteresourceobject.h new file mode 100644 index 0000000..debd855 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_rcs_remoteresourceobject.h @@ -0,0 +1,319 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_rcs_remoteresourceobject.h + * + * @brief This file contains the JniRCSRemoteResourceObject class + * & declaration of RCSRemoteResourceObject APIs for JNI implementation + */ + +#ifndef _JNI_RE_RCS_REMOTE_RESOURCE_OBJECT_H_ +#define _JNI_RE_RCS_REMOTE_RESOURCE_OBJECT_H_ + +#include "RCSRemoteResourceObject.h" + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" +#include "jni_re_listener_manager.h" +#include "jni_re_resource_statechange_listener.h" +#include "jni_re_cache_update_listener.h" +#include "jni_re_getattributes_listener.h" +#include "jni_re_setattributes_listener.h" + +using namespace OIC::Service; + +/** + * @class JniRCSRemoteResourceObject + * @brief This class contains all the APIs for RCSRemoteResourceObject + * + * NOTE: JNI APIs internally call the APIs of this class. + */ +class JniRCSRemoteResourceObject +{ + public: + + /** + * @brief constructor + */ + JniRCSRemoteResourceObject(std::shared_ptr< RCSRemoteResourceObject> resource); + + /** + * @brief destructor + */ + ~JniRCSRemoteResourceObject(); + + // ***** JNI APIs internally call the APIs of this class ***** // + + bool isMonitoring(); + bool isCaching(); + bool isObservable(); + void startMonitoring(JNIEnv *env, jobject jListener) ; + void stopMonitoring(); + int getState(); + void startCaching(JNIEnv *env, jobject jListener); + void startCaching(JNIEnv *env); + void stopCaching(); + int getCacheState(); + bool isCachedAvailable(); + std::string getUri(); + std::string getAddress(); + jobject getInterfaces(JNIEnv *env); + jobject getTypes(JNIEnv *env); + void getRemoteAttributes(JNIEnv *env, jobject jListener); + void setRemoteAttributes(JNIEnv *env, jobject attribute, jobject jListener); + jobject getCachedAttributes(JNIEnv *env); + jstring getCachedAttribute(JNIEnv *env, jstring key, jobject object); + + JniResourceStateChangeListener *addOnStateChangeListener(JNIEnv *env, jobject jListener); + JniCacheUpdateListener *addOnCacheUpdateListener(JNIEnv *env, jobject jListener); + JniGetAttributesListener *addOngetAttributesListener(JNIEnv *env, jobject jListener); + JniSetAttributesListener *addOnsetAttributesListener(JNIEnv *env, jobject jListener); + + void removeOnStateChangeListener(JNIEnv *env, jobject jListener); + void removeCacheUpdateListener(JNIEnv *env, jobject jListener); + void removeOngetAttributesListener(JNIEnv *env, jobject jListener); + void removeOnsetAttributesListener(JNIEnv *env, jobject jListener); + + std::shared_ptr< RCSRemoteResourceObject> getRCSResource(); + + static JniRCSRemoteResourceObject *getJniRCSRemoteResourceObject(JNIEnv *env, jobject thiz); + + private: + JniReListenerManager m_onStateChange; + JniReListenerManager m_onCacheUpdateChange; + JniReListenerManager m_ongetAttributes; + JniReListenerManager m_onsetAttributes; + + std::shared_ptr< RCSRemoteResourceObject> m_sharedResource; + +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * API for checking whether monitoring is enabled or not. + * + * @return jboolean - boolean value (true/false) + * + * NOTE: If monitoring is started returns true otherwise false + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsMonitoring +(JNIEnv *env, jobject interfaceClass); + +/** + * API for checking whether Caching is enabled or not. + * + * @return jboolean - boolean value (true/false) + * + * NOTE: If Caching is started returns true otherwise false + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsCaching +(JNIEnv *env, jobject interfaceClass); + +/** + * API for checking whether resource is observable or not. + * + * @return jboolean - boolean value (true/false) + * + * NOTE: If resource is observable returns true otherwise false + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsObservable +(JNIEnv *env, jobject interfaceClass); + +/** + * Starts monitoring the resource. + * + * Monitoring provides a feature to check the presence of a resource, + * even when the server is not announcing Presence using startPresnece. + * + * @param stateChangedListener - Listener to obtain the changed resource state + * + * @throws RCSException If monitoring is already started. + * + * @note The callback will be invoked in an internal thread. + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStartMonitoring +(JNIEnv *env, jobject interfaceClass, jobject stateChangedListener); + +/** + * Stops monitoring the resource. + * + * It does nothing if monitoring is not started. + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStopMonitoring +(JNIEnv *env, jobject interfaceClass); + +/** + * Returns the current state of the resource. + * + */ +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetState +(JNIEnv *env, jobject interfaceClass); + +/** + * Starts caching attributes of the resource. + * + * This will start data caching for the resource. + * Once caching started it will look for the data updation on the resource. + * If this method is called with null listener it will not notify the updated value to caller. + * It it is called with a valid listener updated cached values will be notified to caller. + * + * @param cacheUpdateListener - Listener to obtain the updated cached data + * + * @throws RCSException If Caching is already started + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStartCaching +(JNIEnv *env, jobject interfaceClass, jobject cacheUpdateListener); + +/** + * Stops caching. + * + * It does nothing if caching is not started. + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStopCaching +(JNIEnv *env, jobject interfaceClass); + +/** + * Returns the current cache state. + * + */ +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetCacheState +(JNIEnv *env, jobject interfaceClass); + +/** + * Returns whether cached data is available. + * + * Cache will be available always after CacheState::READY even if current state is + * LOST_SIGNAL. + * + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsCachedAvailable +(JNIEnv *env, jobject interfaceClass); + +/** + * Gets the cached RCSResourceAttributes data. + * + * @pre Cache should be available. + * + * @return The cached attributes. + * + * @throws RCSException If the precondition is not fulfilled. + * + */ +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetCachedAttributes +(JNIEnv *env, jobject interfaceClass); + +/** + * Gets a particular cached a ResourceAttribute Value. + * + * @pre Cache should be available. + * + * @param key - Attribute Key + * + * @return Requested attribute value. + * + * @throws RCSException If the precondition is not fulfilled or key doesn't match the key of any value. + * + */ +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetCachedAttribute +(JNIEnv *env, jobject interfaceClass, jstring key); + +/** + * Gets resource attributes directly from the server. + * + * This API send a get request to the resource of interest and provides + * the attributes to the caller in the RemoteAttributesReceivedCallback. + * + * @param resourceAttributesListener - Listener to get the resource attributes + * + * @note The callback will be invoked in an internal thread. + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetRemoteAttributes +(JNIEnv *env, jobject interfaceClass, jobject resourceAttributesListener); + +/** + * Sends a set request with resource attributes to the server. + * + * The SetRequest behavior depends on the server, whether updating its attributes or not. + * + * @param resourceAttributes - Attributes to set + * @param resourceAttributesListener - Listener to obtain the updated cached data + * + * @note The callback will be invoked in an internal thread. + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeSetRemoteAttributes +(JNIEnv *env, jobject interfaceClass, jobject resourceAttributes, + jobject resourceAttributesListener); + +/** + * Returns the uri of the resource. + * + */ +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetUri +(JNIEnv *env, jobject interfaceClass); + +/** + * Returns the address of the resource . + * + */ +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetAddress +(JNIEnv *env, jobject interfaceClass); + +/** + * Returns the resource types of the resource. + * + */ +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetTypes +(JNIEnv *env, jobject interfaceClass); + +/** + * Returns the resource interfaces of the resource. + * + */ +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetInterFaces +(JNIEnv *env, jobject interfaceClass); + +#ifdef __cplusplus +} +#endif +#endif // _JNI_RE_RCS_REMOTE_RESOURCE_OBJECT_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_attributes.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_attributes.h new file mode 100644 index 0000000..7e32759 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_attributes.h @@ -0,0 +1,228 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_resource_attributes.h + * + * @brief This file contains the declaration of Resource Attribute APIs + * for JNI implementation + */ + +#ifndef __JNI_RE_RCS_RESOURCE_ATTRIBUTES_H_ +#define __JNI_RE_RCS_RESOURCE_ATTRIBUTES_H_ + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" + +#include + +#define JNI_EXCEPTION 1000 +#define JNI_NO_NATIVE_POINTER 1001 +#define JNI_INVALID_VALUE 1002 +#define JNI_NO_SUCH_KEY 1003 + +struct NullType {}; + +/** + * @class JniResourceAttributes + * @brief This class provides the API for getting the RCSResourceAttribute pointer + * + */ +class JniResourceAttributes +{ + public: + /** + * @brief Get the ResourceAttibute pointer + * internally get the native handle by calling REGetHandle + */ + static RCSResourceAttributes *getResourceAttributePtr(JNIEnv *env, jobject thiz); +}; + +/** + * @brief Utility structure for jobject conversion + */ +struct JObjectConverter : boost::static_visitor < jobject > +{ + JObjectConverter(JNIEnv *env) : env(env) {} + + jobject operator()(const NullType &) const { return nullptr; } + jobject operator()(const int &val) const + { + jobject jobj = env->NewObject( + g_cls_Integer, + g_mid_Integer_ctor, + static_cast(val)); + return jobj; + } + jobject operator()(const double &val) const + { + jobject jobj = env->NewObject( + g_cls_Double, + g_mid_Double_ctor, + static_cast(val)); + return jobj; + } + jobject operator()(const bool &val) const + { + jobject jobj = env->NewObject( + g_cls_Boolean, + g_mid_Boolean_ctor, + static_cast(val)); + return jobj; + } + jobject operator()(const std::string &val) const + { + jstring jstr = env->NewStringUTF(val.c_str()); + return static_cast(jstr); + } + private: + JNIEnv *env; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * API for getting attribute value corresponding the requested Key + * + * @param key - Attribute key + * + */ +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeGetValueN +(JNIEnv *env, jobject interfaceObject, jstring key); + +/** + * API for setting integer value for attribute + * + * @param key - Attribute key + * @param value - Attribute value + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueInteger +(JNIEnv *env, jobject interfaceObject, jstring key, jint value); + +/** + * API for setting Double value for attribute + * + * @param key - Attribute key + * @param value - Attribute value + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueDouble +(JNIEnv *env, jobject interfaceObject, jstring key, jdouble value); + +/** + * API for setting Boolean value for attribute + * + * @param key - Attribute key + * @param value - Attribute value + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueBoolean +(JNIEnv *env, jobject interfaceObject, jstring key, jboolean value); + +/** + * API for setting String value for attribute + * + * @param Key - Attribute key + * @param value - Attribute value + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueString +(JNIEnv *env, jobject interfaceObject, jstring key, jstring value); + +/** + * API for setting IntegerArray for attribute + * + * @param key - Attribute key + * @param value - Attribute value + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueIntegerArray +(JNIEnv *env, jobject interfaceObject, jstring key, jintArray value); + +/** + * API for checking whether attribute is empty or not + * + * @return jBoolean - boolean value (true/false) + * + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeIsEmpty +(JNIEnv *env, jobject interfaceObject); + +/** + * API for getting the size of the attributes + * + * @return jint - size of attributes + * + */ +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSize +(JNIEnv *env, jobject interfaceObject); + +/** + * API for removing a particular attribute + * + * @param key - Attribute key + * + * @return jBoolean - boolean value (true/false) + * + * NOTE: If requested attribute is successfully removed it will return true otherwise false + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeRemove +(JNIEnv *env, jobject interfaceObject, jstring key); + +/** + * API for checking a particular attribute + * + * @param key - Attribute key + * + * @return jBoolean - boolean value (true/false) + * + * NOTE: If requested attribute is present it will return true otherwise false + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeHasAttribute +(JNIEnv *env, jobject interfaceObject, jstring key); + +/** + * @brief API for creating a new RCSResourceAttributes object and setting of Native handle + */ +JNIEXPORT void JNICALL Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_create +(JNIEnv *env, jobject interfaceObject); + +/** + * @brief API for deleting the RCSResourceAttributes object + */ +JNIEXPORT void JNICALL Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_dispose +(JNIEnv *env, jobject interfaceObject, jboolean jNeedsDelete); + +#ifdef __cplusplus +} +#endif +#endif //__JNI_RE_RCS_RESOURCE_ATTRIBUTES_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_builder.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_builder.h new file mode 100644 index 0000000..7699fd7 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_builder.h @@ -0,0 +1,65 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** + * @file jni_re_resource_builder.h + * + * @brief This file contains the resource builder function + * for JNI implementation + */ + +#ifndef __JNI_RE_RESOURCEBUILDER_H +#define __JNI_RE_RESOURCEBUILDER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * API for constructing a new RCSResourceObject. + * + * + * @param resourceURI Resource URI value to be set + * @param resourceType Resource type value to be set + * @param resourceInterface Interface value to be set + * @param observableFlag Whether resource should be observable or not. + * @param discovervableFlag Whether resource should be discovervable or not. + * @param attributes Attributes to be set + * + * @return RCSResourceObject object (It is the resource that is created) + * + * @throw RCSException + * It catches exception from registerResource API of OCPlatform and + * throws it to developer. + */ +JNIEXPORT jobject JNICALL Java_org_iotivity_ResourceEncapsulation_server_Builder_nativeBuild +(JNIEnv *env, jobject interfaceObject, jstring resourceURI, jstring resourceType, + jstring resourceInterface, + jboolean observableFlag, jboolean discovervableFlag, jobject attributes); + +#ifdef __cplusplus +} +#endif +#endif //__JNI_RE_RESOURCEBUILDER_H + + diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_object.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_object.h new file mode 100644 index 0000000..6eb73a3 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_object.h @@ -0,0 +1,333 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_resource_object.h + * + * @brief This file contains the JniResourceObject class + * & declaration of RCSRemoteResourceObject APIs for JNI implementation + */ + +#ifndef __JNI_RE_RCS_RESOURCE_OBJECT +#define __JNI_RE_RCS_RESOURCE_OBJECT + +#include + +#include "jni_re_jvm.h" +#include "RCSResourceObject.h" +#include "jni_re_utils.h" +#include "jni_re_get_request_listener.h" +#include "jni_re_set_request_listener.h" +#include "jni_re_attribute_updated_listener.h" +#include "jni_re_listener_manager_server.h" + +using namespace OIC::Service; + +/** + * @class JniRCSResourceObject + * @brief This class contains all the APIs for RCSResourceObject + * + * NOTE: JNI APIs internally call the APIs of this class. + */ +class JniRCSResourceObject +{ + public: + + /** + * @brief constructor + */ + JniRCSResourceObject(std::shared_ptr resource); + + /** + * @brief destructor + */ + ~JniRCSResourceObject(); + + // ***** JNI APIs internally call the APIs of this class ***** // + + void setAttributeInteger(JNIEnv *env, jobject jObject, jstring key, jint value); + void setAttributeDouble(JNIEnv *env, jobject jObject, jstring key, jdouble value); + void setAttributeBool(JNIEnv *env, jobject jObject, jstring key, jboolean value); + void setAttributeString(JNIEnv *env, jobject jObject, jstring key, jstring value); + jstring getAttributeValue(JNIEnv *env, jstring jkey); + bool removeAttribute(JNIEnv *env, jstring key); + bool containsAttribute(JNIEnv *env, jstring key); + jobject getAttributes(JNIEnv *env, jobject jObject); + bool isObservable(); + bool isDiscoverable(); + void setGetRequestHandler(JNIEnv *env, jobject getRequestListener); + void setSetRequestHandler(JNIEnv *env, jobject jSetRequestListener); + void addAttributeUpdatedListener(JNIEnv *env, jstring key, jobject jAttributeUpdatedListener); + bool removeAttributeUpdatedListener(JNIEnv *env, jobject jObject, jstring key); + void notify(); + void setAutoNotifyPolicy(int policy); + int getAutoNotifyPolicy(); + void setSetRequestHandlerPolicy(int policy); + int getSetRequestHandlerPolicy(); + + JniGetRequestListener *addOnGetRequestListener(JNIEnv *env, jobject jListener); + JniSetRequestListener *addOnSetRequestListener(JNIEnv *env, jobject jListener); + JniAttributeUpdatedListener *addOnAttributeUpdatedListener(JNIEnv *env, jobject jListener); + + void removeOnGetRequestListener(JNIEnv *env, jobject jListener); + void removeOnSetRequestListener(JNIEnv *env, jobject jListener); + void removeOnAttributeUpdatedListener(JNIEnv *env, jobject jListener); + + static JniRCSResourceObject *getJniRCSResourceObject(JNIEnv *env, jobject thiz); + + private: + + JniReListenerManagerServer m_onGetRequest; + JniReListenerManagerServer m_onSetRequest; + JniReListenerManagerServer m_onAttributeUpdated; + + std::shared_ptr m_sharedResource; + +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief API for setting a particular attribute value as a integer + * + * @param key name of attribute(used to map the attribute value). + * @param value integer value to be mapped against the key. + * + * It is guaranteed thread-safety about attributes. + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeInteger +(JNIEnv *env, jobject jObject, jstring key, jint value); + +/** + * @brief API for setting a particular attribute value as a double + * + * @param key name of attribute(used to map the attribute value). + * @param value Double value to be mapped against the key. + * + * It is guaranteed thread-safety about attributes. + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeDouble +(JNIEnv *env, jobject jObject, jstring key, jdouble value); + +/** + * @brief API for setting a particular attribute value as a boolean + * + * @param key name of attribute(used to map the attribute value). + * @param value boolean value to be mapped against the key. + * + * It is guaranteed thread-safety about attributes. + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeBool +(JNIEnv *env, jobject jObject, jstring key, jboolean value); + +/** + * @brief API for setting a particular attribute value as a String + * + * @param key name of attribute(used to map the attribute value). + * @param value String value to be mapped against the key. + * + * It is guaranteed thread-safety about attributes. + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeString +(JNIEnv *env, jobject jObject, jstring key, jstring value); + +/** + * @brief API for retrieving the attribute value associated with the supplied key. + * + * @param key Name of the attribute + * + * @return resource attributes value as a string + * + * It is guaranteed thread-safety about attributes. + */ +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetAttributeValue +(JNIEnv *env, jobject jObject, jstring key); + +/** + * @brief API for removing a particular attribute of the resource. + * + * @param key Name of the attribute. + * + * @return If the key exist and matched attribute is deleted, return true. + * + * It is guaranteed thread-safety about attributes. + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeRemoveAttribute +(JNIEnv *env, jobject jObject, jstring key); + +/** + * @brief API for checking whether a particular attribute is there for a resource or not. + * + * @param key Name of the attribute. + * + * @return If the key exist, return true. + * + * It is guaranteed thread-safety about attributes. + */ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeContainsAttribute +(JNIEnv *env, jobject jObject, jstring key); + + +/** + * @brief API for getting all the attributes of the RCSResourceObject. + * + * @return RCSResourceAttributes + * + * @see RCSResourceAttributes + */ +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetAttributes +(JNIEnv *env, jobject jObject); + +/** +* @brief API for checking whether the particular resource is observable or not +*/ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeIsObservable +(JNIEnv *env, jobject jObject); + +/** +* @brief API for checking whether the particular resource is discoverable or not +*/ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeIsDiscoverable +(JNIEnv *env, jobject jObject); + +/** + * API for setting the resource's get request handler by the developer/application. + * If developer set this handler then all get request will come to the application & + * developer can send the response to the client using APIs of RCSGetResponse class. + * + * @param jGetRequestListener Request handler for get requests + * + * @see RCSGetResponse + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetGetRequestHandler +(JNIEnv *env, jobject jObject, jobject jGetRequestListener); + +/** + * API for setting the resource's set request handler by the developer/application. + * If developer set this handler then all set request for the resource + * will come to the application & developer can send the response to the client + * using APIs of RCSSetResponse class. + * + * @param jSetRequestListener Request handler for set requests + * + * @see RCSSetResponse + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetSetRequestHandler +(JNIEnv *env, jobject jObject, jobject jSetRequestListener); + +/** + * API for setting the Listener for a particular attribute update. + * + * @param key The interested attribute's key + * @param jAttributeUpdatedListener Listener for updation of the interested attribute + * + */ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeAddAttributeUpdatedListener +(JNIEnv *env, jobject jObject, jstring key, jobject jAttributeUpdatedListener); + +/** +* API for removing the handler for a particular attribute update. +* +* @param key The interested attribute's key +* +*/ +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeRemoveAttributeUpdatedListener +(JNIEnv *env, jobject jObject, jstring key); + +/** + * API for notifying all observers of the RCSResourceObject + * with the updated attributes value + */ +JNIEXPORT void JNICALL Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeNotify +(JNIEnv *env, jobject jObject); + +/** +* API for setting Auto notify policy +* +* @param policy policy to be set (Its datatype is jint we will convert it to AutoNotifyPolicy enum value using jni_re_utils) +* +* @see AutoNotifyPolicy +* @see jni_re_utils +* +*/ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAutoNotifyPolicy +(JNIEnv *env, jobject jObject, jint policy); + +/** +* API for getting auto notify policy +* +* @returns jint AntoNotify policy as a integer in java it will be converted to AutoNotifyPolicy enum. +* +* @see AutoNotifyPolicy +* +*/ +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetAutoNotifyPolicy +(JNIEnv *env, jobject jObject); + +/** +* API for setting the policy for a setRequestHandler. +* +* @param policy policy to be set (Its datatype is jint we will convert it to SetRequestHandlerPolicy enum value using jni_re_utils) +* +* @see SetRequestHandlerPolicy +* @see jni_re_utils +* +*/ +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetSetRequestHandlerPolicy +(JNIEnv *env, jobject jObject, jint policy); + +/** +* API for getting the SetRequestHandler Policy. +* +* @returns jint SetRequestHandlerPolicy policy as a integer in java it will be converted to SetRequestHandlerPolicy enum. +* +* @see SetRequestHandlerPolicy +* +*/ +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetSetRequestHandlerPolicy +(JNIEnv *env, jobject jObject); + +#ifdef __cplusplus +} +#endif +#endif + + diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_statechange_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_statechange_listener.h new file mode 100644 index 0000000..43a911e --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_resource_statechange_listener.h @@ -0,0 +1,67 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** @file jni_re_resource_statechange_listener.h + * + * @brief This file contains JNI resource state change Listener class + */ + +#ifndef __JNI_RE_RESOURCE_STATE_CHANGE_LISTENER_H_ +#define __JNI_RE_RESOURCE_STATE_CHANGE_LISTENER_H_ + +#include + +#include "RCSRemoteResourceObject.h" + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" + +class JniRCSRemoteResourceObject; + +/** + * @class JniResourceStateChangeListener + * @brief This class provides functions for handling the Resource state change callback between the Java and native layer + * + */ +class JniResourceStateChangeListener +{ + public: + /** + * @brief constructor + */ + JniResourceStateChangeListener(JNIEnv *env, jobject jListener, + JniRCSRemoteResourceObject *resource); + + /** + * @brief destructor + */ + ~JniResourceStateChangeListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + void stateChangeCallback(OIC::Service::ResourceState state); + + private: + jweak m_jwListener; + JniRCSRemoteResourceObject *m_ownerResource; + void checkExAndRemoveListener(JNIEnv *env); +}; + +#endif //__JNI_RE_RESOURCE_STATE_CHANGE_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_set_request_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_set_request_listener.h new file mode 100644 index 0000000..4f6b723 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_set_request_listener.h @@ -0,0 +1,69 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_set_request_listener.h + * + * @brief This file contains JNI set request Listener class + */ + +#ifndef __JNI_RE_SET_REQUEST_LISTENER_H_ +#define __JNI_RE_SET_REQUEST_LISTENER_H_ + +#include + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" +#include "jni_re_resource_attributes.h" + + +class JniRCSResourceObject; + +/** + * @class JniSetRequestListener + * @brief This class provides functions for handling the set request callback between the Java and Native layer + * + */ +class JniSetRequestListener +{ + public: + /** + * @brief constructor + */ + JniSetRequestListener(JNIEnv *env, jobject jListener, + JniRCSResourceObject *resource); + + /** + * @brief destructor + */ + ~JniSetRequestListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + OIC::Service::RCSSetResponse onSetRequestCallback(OIC::Service::RCSRequest request, + OIC::Service::RCSResourceAttributes attributes); + + private: + jweak m_jwListener; + JniRCSResourceObject *m_ownerResource; + void checkExAndRemoveListener(JNIEnv *env); +}; + +#endif //__JNI_RE_SET_REQUEST_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_setattributes_listener.h b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_setattributes_listener.h new file mode 100644 index 0000000..c589f88 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/include/jni_re_setattributes_listener.h @@ -0,0 +1,67 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +/** @file jni_re_setattributes_listener.h + * + * @brief This file contains JNI set attributes Listener class + */ + +#ifndef __JNI_RE_SET_ATTRIBUTES_LISTENER_H_ +#define __JNI_RE_SET_ATTRIBUTES_LISTENER_H_ + +#include + +#include "jni_re_jvm.h" +#include "jni_re_utils.h" +#include "jni_re_resource_attributes.h" + +class JniRCSRemoteResourceObject; + +/** + * @class JniSetAttributesListener + * @brief This class provides functions for handling the set attribute callback between the Java and native layer + * + */ +class JniSetAttributesListener +{ + public: + /** + * @brief constructor + */ + JniSetAttributesListener(JNIEnv *env, jobject jListener, + JniRCSRemoteResourceObject *resource); + + /** + * @brief destructor + */ + ~JniSetAttributesListener(); + + /** + * @brief callback function that will be passed to Native layer + */ + void onRemoteAttributesSetCallback(OIC::Service::RCSResourceAttributes attributes); + + private: + jweak m_jwListener; + JniRCSRemoteResourceObject *m_ownerResource; + void checkExAndRemoveListener(JNIEnv *env); +}; + +#endif //__JNI_RE_SET_ATTRIBUTES_LISTENER_H_ diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_attribute_updated_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_attribute_updated_listener.cpp new file mode 100644 index 0000000..d9f73c8 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_attribute_updated_listener.cpp @@ -0,0 +1,152 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_attribute_updated_listener.h" +#include "jni_re_resource_object.h" + +JniAttributeUpdatedListener::JniAttributeUpdatedListener(JNIEnv *env, jobject jListener, + JniRCSResourceObject *owner) + : m_ownerResource(owner) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); +} + +JniAttributeUpdatedListener::~JniAttributeUpdatedListener() +{ + LOGI("JniAttributeUpdatedListener::~JniAttributeUpdatedListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniAttributeUpdatedListener::onAttributeUpdatedCallback( + OIC::Service::RCSResourceAttributes::Value oldValue, + OIC::Service::RCSResourceAttributes::Value newValue) +{ + LOGI("JniAttributeUpdatedListener::onAttributeUpdatedCallback got a callback in JNI"); + + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + RCSResourceAttributes::TypeId oldValueTypeId = oldValue.getType().getId(); + RCSResourceAttributes::TypeId newValueTypeId = newValue.getType().getId(); + jstring javaOldValue; + jstring javaNewValue; + char buf[64]; // assumed large enough to cope with result + char bufValue[64]; + + if (oldValueTypeId == RCSResourceAttributes::TypeId::INT) + { + int oldVal = oldValue.get(); + sprintf(buf, "%d", oldVal); + javaOldValue = (env)->NewStringUTF(buf); + + int newVal = newValue.get(); + sprintf(bufValue, "%d", newVal); + javaNewValue = (env)->NewStringUTF(bufValue); + + } + else if (oldValueTypeId == RCSResourceAttributes::TypeId::DOUBLE) + { + double oldVal = oldValue.get(); + sprintf(buf, "%f", oldVal); + javaOldValue = (env)->NewStringUTF(buf); + + double newVal = newValue.get(); + sprintf(bufValue, "%f", newVal); + javaNewValue = (env)->NewStringUTF(bufValue); + + } + else if (oldValueTypeId == RCSResourceAttributes::TypeId::BOOL) + { + bool oldVal = oldValue.get(); + sprintf(buf, "%d", oldVal); + javaOldValue = (env)->NewStringUTF(buf); + + bool newVal = newValue.get(); + sprintf(bufValue, "%d", newVal); + javaNewValue = (env)->NewStringUTF(bufValue); + } + else if (oldValueTypeId == RCSResourceAttributes::TypeId::STRING) + { + std::string oldVal = oldValue.get(); + javaOldValue = env->NewStringUTF(oldVal.c_str()); + + std::string newVal = newValue.get(); + javaNewValue = env->NewStringUTF(newVal.c_str()); + } + + //sending callback to application + jclass clsL = env->GetObjectClass(jListener); + + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jmethodID midL = env->GetMethodID(clsL, "onAttributeUpdate", + "(Ljava/lang/String;Ljava/lang/String;)V"); + if (!midL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + env->CallVoidMethod(jListener, midL, javaOldValue, javaNewValue); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + } + + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("JniDiscoverResourceListener::cacheUpdatedCallback exit"); + +} + +void JniAttributeUpdatedListener::checkExAndRemoveListener(JNIEnv *env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_ownerResource->removeOnSetRequestListener(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_ownerResource->removeOnSetRequestListener(env, m_jwListener); + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_cache_update_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_cache_update_listener.cpp new file mode 100644 index 0000000..abb42e6 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_cache_update_listener.cpp @@ -0,0 +1,117 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_discover_resource_listener.h" +#include "RCSRemoteResourceObject.h" +#include "jni_re_cache_update_listener.h" + +JniCacheUpdateListener::JniCacheUpdateListener(JNIEnv *env, jobject jListener, + JniRCSRemoteResourceObject *owner) + : m_ownerResource(owner) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); +} + +JniCacheUpdateListener::~JniCacheUpdateListener() +{ + LOGI("JniCacheUpdateListener::~JniCacheUpdateListener"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniCacheUpdateListener::cacheUpdatedCallback(OIC::Service::RCSResourceAttributes attributes) +{ + LOGI("JniDiscoverResourceListener::cacheUpdatedCallback enter"); + + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jclass clsL = env->GetObjectClass(jListener); + + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); + jlong handle = reinterpret_cast(attributesObj); + jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, + g_mid_RCSRemoteResourceAttributesObject_ctor, + handle, true); + if (!jAttributes) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jmethodID midL = env->GetMethodID(clsL, "onCacheUpdatedCallback", + "(Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;)V"); + + if (!midL) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + env->CallVoidMethod(jListener, midL, jAttributes); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete attributesObj; + } + + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("JniDiscoverResourceListener::cacheUpdatedCallback exit"); +} + +void JniCacheUpdateListener::checkExAndRemoveListener(JNIEnv *env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_ownerResource->removeCacheUpdateListener(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_ownerResource->removeCacheUpdateListener(env, m_jwListener); + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_discover_resource_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_discover_resource_listener.cpp new file mode 100644 index 0000000..78bc742 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_discover_resource_listener.cpp @@ -0,0 +1,125 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "RCSRemoteResourceObject.h" + +#include "jni_re_discover_resource_listener.h" + +JniDiscoverResourceListener::JniDiscoverResourceListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); + m_removeListenerCallback = removeListenerCallback; +} + +JniDiscoverResourceListener::~JniDiscoverResourceListener() +{ + LOGI("JniDiscoverResourceListener::~jni_DiscoverResourceListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniDiscoverResourceListener::discoverResourceCallback( + std::shared_ptr resource) +{ + LOGI("JniDiscoverResourceListener::discoverResourceCallback enter"); + + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jobject jResource = env->NewObject(g_cls_RCSRemoteResourceObject, + g_mid_RCSRemoteResourceObject_ctor); + if (!jResource) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + JniRCSRemoteResourceObject *jniRCSRemoteResourceObject = new JniRCSRemoteResourceObject(resource); + RESetHandle(env, jResource, jniRCSRemoteResourceObject); + if (env->ExceptionCheck()) + { + delete jniRCSRemoteResourceObject; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jclass clsL = env->GetObjectClass(jListener); + if (!clsL) + { + delete jniRCSRemoteResourceObject; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + jmethodID midL = env->GetMethodID(clsL, "onResourceDiscovered", + "(Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject;)V"); + if (!midL) + { + delete jniRCSRemoteResourceObject; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + env->CallVoidMethod(jListener, midL, jResource); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete jniRCSRemoteResourceObject; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("JniDiscoverResourceListener::discoverResourceCallback exit"); +} + +void JniDiscoverResourceListener::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/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_get_request_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_get_request_listener.cpp new file mode 100644 index 0000000..ac4fe58 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_get_request_listener.cpp @@ -0,0 +1,184 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_get_request_listener.h" +#include "jni_re_resource_object.h" + +JniGetRequestListener::JniGetRequestListener(JNIEnv *env, jobject jListener, + JniRCSResourceObject *owner) + : m_ownerResource(owner) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); +} + +JniGetRequestListener::~JniGetRequestListener() +{ + LOGI("JniGetRequestListener::~JniGetRequestListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +OIC::Service::RCSGetResponse JniGetRequestListener::onGetRequestCallback( + OIC::Service::RCSRequest request, OIC::Service::RCSResourceAttributes attributes) +{ + LOGI("JniGetRequestListener::onGetRequestCallback enter"); + + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) + LOGE("env is NULL"); + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGE("jListener is NULL"); + } + + jclass clsL = env->GetObjectClass(jListener); + + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGE("clsL is NULL"); + } + + //constructing java attribute object + RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); + jlong handle = reinterpret_cast(attributesObj); + jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, + g_mid_RCSRemoteResourceAttributesObject_ctor, + handle, true); + if (!jAttributes) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("onGetRequestCallback: jAttributes == null"); + } + + //constructing java RCSRequest object + std::string resourceUri = request.getResourceUri(); + jstring javaUri = env->NewStringUTF(resourceUri.c_str()); + + g_mid_RCSRequest_ctor = env->GetMethodID(g_cls_RCSRequest, "", "(Ljava/lang/String;)V"); + jobject javaRCSRequestObject = env->NewObject( g_cls_RCSRequest, g_mid_RCSRequest_ctor, javaUri); + + //getting the method ID + + jmethodID midL = env->GetMethodID(clsL, "onGetRequest", + "(Lorg/iotivity/ResourceEncapsulation/server/RCSRequest;Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;)Lorg/iotivity/ResourceEncapsulation/server/RCSGetResponse;"); + + if (!midL) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGE(" midL is null"); + } + + //calling the Java method + jobject javaGetResponse = env->CallObjectMethod(jListener, midL, javaRCSRequestObject, jAttributes); + + //getting the information related to Response + jclass GetResponseClass = env->GetObjectClass(javaGetResponse); + + jmethodID midGetTypeOfGetResponse = env->GetMethodID(GetResponseClass, "getTypeOfGetResponse", + "()I"); + jint type = env->CallIntMethod(javaGetResponse, midGetTypeOfGetResponse); + + if (0 == type) + { + return OIC::Service::RCSGetResponse::defaultAction(); + } + else if (1 == type) + { + LOGI("Type == 1"); + jmethodID midGetResult = env->GetMethodID(GetResponseClass, "getResult", "()I"); + jmethodID midGetErrorCode = env->GetMethodID(GetResponseClass, "getErrorCode", "()I"); + jint handlerResult = env->CallIntMethod(javaGetResponse, midGetResult); + jint errorCode = env->CallIntMethod(javaGetResponse, midGetErrorCode); + OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); + return OIC::Service::RCSGetResponse::create(nativeHandlerResult, errorCode); + } + else if (2 == type) + { + LOGI("Type == 2"); + jmethodID midGetAttributes = env->GetMethodID(GetResponseClass, "getAttributes", + "()Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;"); + jobject attributes = env->CallObjectMethod(javaGetResponse, midGetAttributes); + RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, + attributes); + return OIC::Service::RCSGetResponse::create(*attributesObj); + } + else if (3 == type) + { + LOGI("Type == 3"); + jmethodID midGetResult = env->GetMethodID(GetResponseClass, "getResult", "()I"); + jmethodID midGetErrorCode = env->GetMethodID(GetResponseClass, "getErrorCode", "()I"); + jint handlerResult = env->CallIntMethod(javaGetResponse, midGetResult); + jint errorCode = env->CallIntMethod(javaGetResponse, midGetErrorCode); + OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); + jmethodID midGetAttributes = env->GetMethodID(GetResponseClass, "getAttributes", + "()Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;"); + jobject attributes = env->CallObjectMethod(javaGetResponse, midGetAttributes); + RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, + attributes); + return OIC::Service::RCSGetResponse::create(*attributesObj, nativeHandlerResult, errorCode); + } + else + { + LOGI("defaultAction"); + return OIC::Service::RCSGetResponse::defaultAction(); + } + + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete attributesObj; + } + + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("JniGetRequestListener::onGetRequestCallback exit"); +} + +void JniGetRequestListener::checkExAndRemoveListener(JNIEnv *env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_ownerResource->removeOnGetRequestListener(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_ownerResource->removeOnGetRequestListener(env, m_jwListener); + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_getattributes_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_getattributes_listener.cpp new file mode 100644 index 0000000..119e831 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_getattributes_listener.cpp @@ -0,0 +1,118 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_getattributes_listener.h" +#include "jni_re_rcs_remoteresourceobject.h" + +JniGetAttributesListener::JniGetAttributesListener(JNIEnv *env, jobject jListener, + JniRCSRemoteResourceObject *owner) + : m_ownerResource(owner) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); +} + +JniGetAttributesListener::~JniGetAttributesListener() +{ + LOGI("JniGetAttributesListener::~JniGetAttributesListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniGetAttributesListener::onRemoteAttributesGetCallback(OIC::Service::RCSResourceAttributes + attributes) +{ + LOGI("JniGetAttributesListener::onRemoteAttributesGetCallback enter"); + + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jclass clsL = env->GetObjectClass(jListener); + + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); + jlong handle = reinterpret_cast(attributesObj); + jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, + g_mid_RCSRemoteResourceAttributesObject_ctor, + handle, true); + if (!jAttributes) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jmethodID midL = env->GetMethodID(clsL, "onRemoteAttributesGetCallback", + "(Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;)V"); + + if (!midL) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + env->CallVoidMethod(jListener, midL, jAttributes); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete attributesObj; + } + + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("JniGetAttributesListener::onRemoteAttributesGetCallback exit"); +} + +void JniGetAttributesListener::checkExAndRemoveListener(JNIEnv *env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_ownerResource->removeOngetAttributesListener(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_ownerResource->removeOngetAttributesListener(env, m_jwListener); + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_discovery_manager.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_discovery_manager.cpp new file mode 100644 index 0000000..ea8ece5 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_discovery_manager.cpp @@ -0,0 +1,137 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_rcs_discovery_manager.h" + +#include "RCSDiscoveryManager.h" +#include "RCSAddress.h" + +using namespace OIC::Service; + +JniDiscoverResourceListener *AddDiscoverResourceListener(JNIEnv *env, jobject jListener) +{ + JniDiscoverResourceListener *onResourceDiscovered = NULL; + + discoverResourceMapLock.lock(); + + for (auto it = discoverResourceListenerMap.begin(); it != discoverResourceListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onResourceDiscovered = refPair.first; + refPair.second++; + it->second = refPair; + discoverResourceListenerMap.insert(*it); + LOGD("onResourceDiscovered: ref. count incremented"); + break; + } + } + + if (!onResourceDiscovered) + { + onResourceDiscovered = new JniDiscoverResourceListener(env, jListener, + RemoveDiscoverResourceListener); + jobject jgListener = env->NewGlobalRef(jListener); + + discoverResourceListenerMap.insert(std::pair < jobject, std::pair < JniDiscoverResourceListener *, + int >> (jgListener, std::pair(onResourceDiscovered, 1))); + LOGD("onResourceDiscovered: new listener"); + } + discoverResourceMapLock.unlock(); + return onResourceDiscovered; +} + +void RemoveDiscoverResourceListener(JNIEnv *env, jobject jListener) +{ + discoverResourceMapLock.lock(); + + for (auto it = discoverResourceListenerMap.begin(); it != discoverResourceListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + discoverResourceListenerMap.insert(*it); + LOGI("OnResourceDiscoveredListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniDiscoverResourceListener *listener = refPair.first; + delete listener; + discoverResourceListenerMap.erase(it); + LOGI("OnResourceDiscoveredListener removed"); + } + break; + } + } + discoverResourceMapLock.unlock(); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSDiscoveryManager_discoverResource +(JNIEnv *env, jclass interfaceClass, jstring jAddress, jint jTypeofDiscovery, jstring jRelativeURI, + jstring jresourceType, jobject jDiscoveryListener) +{ + LOGI("NativeInterface_discoverResource entry"); + std::string address; + if (jAddress) + { + address = env->GetStringUTFChars(jAddress, NULL); + } + std::string relativeUri; + if (jRelativeURI) + { + relativeUri = env->GetStringUTFChars(jRelativeURI, NULL); + } + std::string resourceType; + if (jresourceType) + { + resourceType = env->GetStringUTFChars(jresourceType, NULL); + } + + JniDiscoverResourceListener *onResFoundListener = AddDiscoverResourceListener(env, + jDiscoveryListener); + LOGI("NativeInterface_discoverResource onResFoundListener added"); + RCSDiscoveryManager::ResourceDiscoveredCallback discoverCallback = [onResFoundListener]( + std::shared_ptr< OIC::Service::RCSRemoteResourceObject> resource) + { + onResFoundListener->discoverResourceCallback(resource); + }; + if ( 0 == jTypeofDiscovery ) + { + RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::multicast(), relativeUri, + resourceType, discoverCallback); + LOGI("NativeInterface_discoverResource multicast"); + } + else if ( 1 == jTypeofDiscovery) + { + RCSDiscoveryManager::getInstance()->discoverResourceByType(RCSAddress::unicast(address), + relativeUri, resourceType, discoverCallback ); + LOGI("NativeInterface_discoverResource unicast"); + } + + LOGI("NativeInterface_discoverResource exit"); + +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_remoteresourceobject.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_remoteresourceobject.cpp new file mode 100644 index 0000000..56cf292 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_rcs_remoteresourceobject.cpp @@ -0,0 +1,502 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_rcs_remoteresourceobject.h" + +using namespace OIC::Service; +JniRCSRemoteResourceObject::JniRCSRemoteResourceObject(std::shared_ptr< RCSRemoteResourceObject> + resource) + : m_sharedResource(resource) +{} + +JniRCSRemoteResourceObject::~JniRCSRemoteResourceObject() +{ + LOGD("JniRCSRemoteResourceObject::~JniOcResource()"); + m_sharedResource = NULL; + + jint envRet; + JNIEnv *env = GetREJNIEnv(envRet); + if (NULL == env) return; + + if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); +} + +JniRCSRemoteResourceObject *JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject(JNIEnv *env, + jobject thiz) +{ + JniRCSRemoteResourceObject *resource = REGetHandle(env, thiz); + if (env->ExceptionCheck()) + { + LOGE("Failed to get native handle from RCSRemoteResourceObject"); + } + if (!resource) + { + LOGE("getJniRCSRemoteResourceObject :: no resource"); + } + return resource; +} + +std::string JniRCSRemoteResourceObject::getUri() +{ + return m_sharedResource->getUri(); +} + +std::string JniRCSRemoteResourceObject::getAddress() +{ + return m_sharedResource->getAddress(); +} + +jobject JniRCSRemoteResourceObject::getTypes(JNIEnv *env) +{ + return convertStrVectorToJavaStrList( env, m_sharedResource->getTypes()); +} + +jobject JniRCSRemoteResourceObject::getInterfaces(JNIEnv *env) +{ + return convertStrVectorToJavaStrList(env, m_sharedResource->getInterfaces()); +} + +bool JniRCSRemoteResourceObject::isMonitoring() +{ + return m_sharedResource->isMonitoring(); +} + +bool JniRCSRemoteResourceObject::isCaching() +{ + return m_sharedResource->isCaching(); +} + +bool JniRCSRemoteResourceObject::isObservable() +{ + return m_sharedResource->isObservable(); +} + +bool JniRCSRemoteResourceObject::isCachedAvailable() +{ + return m_sharedResource->isCachedAvailable(); +} + +int JniRCSRemoteResourceObject::getState() +{ + ResourceState state = m_sharedResource->getState(); + return covertResourceStateToInt(state); +} + +int JniRCSRemoteResourceObject::getCacheState() +{ + CacheState state = m_sharedResource->getCacheState(); + return convertCacheStateToInt(state); +} + +void JniRCSRemoteResourceObject::startMonitoring(JNIEnv *env, jobject jListener) +{ + JniResourceStateChangeListener *onResourceStateChanged = addOnStateChangeListener(env, jListener); + RCSRemoteResourceObject::StateChangedCallback stateChangecb = [onResourceStateChanged]( + OIC::Service::ResourceState state) + { + onResourceStateChanged->stateChangeCallback(state); + }; + try + { + m_sharedResource->startMonitoring(stateChangecb); + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } +} + +void JniRCSRemoteResourceObject::stopMonitoring() +{ + m_sharedResource->stopMonitoring(); +} + +void JniRCSRemoteResourceObject::startCaching(JNIEnv *env, jobject jListener) +{ + JniCacheUpdateListener *onCacheUpdated = addOnCacheUpdateListener(env, jListener); + RCSRemoteResourceObject::CacheUpdatedCallback cacheUpdatedCallback = [onCacheUpdated]( + OIC::Service::RCSResourceAttributes attributes) + { + onCacheUpdated->cacheUpdatedCallback(attributes); + }; + try + { + m_sharedResource->startCaching(cacheUpdatedCallback); + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } + +} + +void JniRCSRemoteResourceObject::startCaching(JNIEnv *env) +{ + try + { + m_sharedResource->startCaching(); + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } +} + +void JniRCSRemoteResourceObject::stopCaching() +{ + m_sharedResource->stopCaching(); +} + +void JniRCSRemoteResourceObject::getRemoteAttributes(JNIEnv *env, jobject jListener) +{ + JniGetAttributesListener *onGetAttributes = addOngetAttributesListener(env, jListener); + RCSRemoteResourceObject::RemoteAttributesGetCallback getArributescb = [onGetAttributes]( + OIC::Service::RCSResourceAttributes attributes) + { + onGetAttributes->onRemoteAttributesGetCallback(attributes); + }; + m_sharedResource->getRemoteAttributes(getArributescb); +} + +void JniRCSRemoteResourceObject::setRemoteAttributes(JNIEnv *env, jobject attributes, + jobject jListener) +{ + JniSetAttributesListener *onSetAttributes = addOnsetAttributesListener(env, jListener); + RCSRemoteResourceObject::RemoteAttributesSetCallback setArributescb = [onSetAttributes]( + OIC::Service::RCSResourceAttributes attribute) + { + onSetAttributes->onRemoteAttributesSetCallback(attribute); + }; + RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, + attributes); + m_sharedResource->setRemoteAttributes(*attributesObj, setArributescb); +} + + +jobject JniRCSRemoteResourceObject::getCachedAttributes(JNIEnv *env) +{ + try + { + RCSResourceAttributes attributes = m_sharedResource->getCachedAttributes(); + RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); + jlong handle = reinterpret_cast(attributesObj); + jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, + g_mid_RCSRemoteResourceAttributesObject_ctor, + handle, true); + return jAttributes; + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } +} + +jstring JniRCSRemoteResourceObject::getCachedAttribute(JNIEnv *env, jstring jkey, + jobject interfaceObject ) +{ + const char *str = env->GetStringUTFChars(jkey , NULL); + std::string s(str); + char buf[64]; // assumed large enough to cope with result + env->ReleaseStringUTFChars(jkey, str); + RCSResourceAttributes::Value value; + RCSResourceAttributes::TypeId typeId; + try + { + value = m_sharedResource->getCachedAttribute(s); + typeId = value.getType().getId(); + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } + + if (typeId == RCSResourceAttributes::TypeId::INT) + { + int val = value.get(); + sprintf(buf, "%d", val); + return (env)->NewStringUTF(buf); + } + else if (typeId == RCSResourceAttributes::TypeId::DOUBLE) + { + double val = value.get(); + sprintf(buf, "%f", val); + return (env)->NewStringUTF(buf); + } + else if (typeId == RCSResourceAttributes::TypeId::BOOL) + { + bool val = value.get(); + sprintf(buf, "%d", val); + return (env)->NewStringUTF(buf); + } + else if (typeId == RCSResourceAttributes::TypeId::STRING) + { + std::string val = value.get(); + jstring jstr = env->NewStringUTF(val.c_str()); + return jstr; + } +} +JniResourceStateChangeListener *JniRCSRemoteResourceObject::addOnStateChangeListener(JNIEnv *env, + jobject jListener) +{ + return this->m_onStateChange.addListener(env, jListener, this); +} + +JniCacheUpdateListener *JniRCSRemoteResourceObject::addOnCacheUpdateListener(JNIEnv *env, + jobject jListener) +{ + return this->m_onCacheUpdateChange.addListener(env, jListener, this); +} + +JniGetAttributesListener *JniRCSRemoteResourceObject::addOngetAttributesListener(JNIEnv *env, + jobject jListener) +{ + return this->m_ongetAttributes.addListener(env, jListener, this); +} + +JniSetAttributesListener *JniRCSRemoteResourceObject::addOnsetAttributesListener(JNIEnv *env, + jobject jListener) +{ + return this->m_onsetAttributes.addListener(env, jListener, this); +} + +void JniRCSRemoteResourceObject::removeOnStateChangeListener(JNIEnv *env, jobject jListener) +{ + this->m_onStateChange.removeListener(env, jListener); +} + +void JniRCSRemoteResourceObject::removeCacheUpdateListener(JNIEnv *env, jobject jListener) +{ + this->m_onCacheUpdateChange.removeListener(env, jListener); +} + +void JniRCSRemoteResourceObject::removeOngetAttributesListener(JNIEnv *env, jobject jListener) +{ + this->m_ongetAttributes.removeListener(env, jListener); +} + +void JniRCSRemoteResourceObject::removeOnsetAttributesListener(JNIEnv *env, jobject jListener) +{ + this->m_onsetAttributes.removeListener(env, jListener); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsMonitoring +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeIsMonitoring"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + return (jboolean)resource->isMonitoring(); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsCaching +(JNIEnv *env, jobject interfaceClass) +{ + + LOGD("RCSRemoteResourceObject_nativeIsCaching"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + return (jboolean)resource->isCaching(); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsObservable +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeIsObservable"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + return (jboolean)resource->isObservable(); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStartMonitoring +(JNIEnv *env, jobject interfaceClass, jobject jStateChangedListener) +{ + LOGD("RCSRemoteResourceObject_nativeStartMonitoring"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return; + + resource->startMonitoring(env, jStateChangedListener); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStopMonitoring +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeStopMonitoring"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + resource->stopMonitoring(); +} + +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetState +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeGetState"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + return (jint)resource->getState(); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStartCaching +(JNIEnv *env, jobject interfaceClass, jobject jCacheUpdateListener) +{ + LOGD("RCSRemoteResourceObject_nativeStartCaching"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return; + if (NULL == jCacheUpdateListener) + { + resource->startCaching(env); + } + else + { + resource->startCaching(env, jCacheUpdateListener); + } +} +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeStopCaching +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeStopCaching"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + resource->stopCaching(); +} + +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetCacheState +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeGetCacheState"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + return (jint)resource->getCacheState(); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeIsCachedAvailable +(JNIEnv *env, jobject interfaceClass) +{ + + LOGD("RCSRemoteResourceObject_nativeIsMonitoring"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + return (jboolean)resource->isMonitoring(); +} + +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetCachedAttributes +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeGetCachedAttributes"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + return resource->getCachedAttributes(env); +} + +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetCachedAttribute +(JNIEnv *env, jobject interfaceClass, jstring jKey) +{ + LOGD("RCSRemoteResourceObject_nativeGetRemoteAttributes"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + resource->getCachedAttribute(env, jKey, interfaceClass); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetRemoteAttributes +(JNIEnv *env, jobject interfaceClass, jobject jResourceAttributesListener) +{ + LOGD("RCSRemoteResourceObject_nativeGetRemoteAttributes"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return; + + resource->getRemoteAttributes(env, jResourceAttributesListener); +} + + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeSetRemoteAttributes +(JNIEnv *env, jobject interfaceClass, jobject jresourceAttribute, + jobject jResourceAttributesListener) +{ + LOGD("RCSRemoteResourceObject_nativeSetRemoteAttributes"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return; + + resource->setRemoteAttributes(env, jresourceAttribute, jResourceAttributesListener); +} + +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetUri +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("JNI RCSRemoteResourceObjectNativeInterface_getUri"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return nullptr; + + return env->NewStringUTF(resource->getUri().c_str()); +} + +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetAddress +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeGetAddress"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return nullptr; + + return env->NewStringUTF(resource->getAddress().c_str()); +} + +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetTypes +(JNIEnv *env, jobject interfaceClass) +{ + LOGD("RCSRemoteResourceObject_nativeGetTypes"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return nullptr; + return resource->getTypes(env); +} + +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_client_RCSRemoteResourceObject_nativeGetInterFaces +(JNIEnv *env, jobject interfaceClass) +{ + + LOGD("RCSRemoteResourceObject_nativeGetInterFaces"); + JniRCSRemoteResourceObject *resource = JniRCSRemoteResourceObject::getJniRCSRemoteResourceObject( + env, interfaceClass); + if (!resource) return nullptr; + return resource->getInterfaces(env); +} + diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_attributes.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_attributes.cpp new file mode 100644 index 0000000..db9149e --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_attributes.cpp @@ -0,0 +1,289 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_resource_attributes.h" +#include "jni_re_jvm.h" + +RCSResourceAttributes *JniResourceAttributes::getResourceAttributePtr(JNIEnv *env, + jobject interfaceObject) +{ + RCSResourceAttributes *rep = REGetHandle(env, interfaceObject); + if (env->ExceptionCheck()) + { + LOGE("Failed to get native handle from RCSResourceAttributes"); + } + if (!rep) + { + throwRCSException(env, "JNI no native pointer"); + } + return rep; +} + +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeGetValueN +(JNIEnv *env, jobject interfaceObject, jstring jKey) +{ + LOGD("RCSResourceAttributes_getValueN"); + if (!jKey) + { + throwRCSException(env, "attributeKey cannot be null"); + return nullptr; + } + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return nullptr; + + std::string key = env->GetStringUTFChars(jKey, NULL); + + if (! rep->contains(key)) return nullptr; + + RCSResourceAttributes::Value attrValue = rep->at(key); + RCSResourceAttributes::TypeId typeId = attrValue.getType().getId(); + + if (typeId == RCSResourceAttributes::TypeId::INT) + { + int val = attrValue.get(); + jobject jobj = env->NewObject( + g_cls_Integer, + g_mid_Integer_ctor, + static_cast(val)); + return jobj; + } + else if (typeId == RCSResourceAttributes::TypeId::DOUBLE) + { + double val = attrValue.get(); + jobject jobj = env->NewObject( + g_cls_Double, + g_mid_Double_ctor, + static_cast(val)); + return jobj; + } + else if (typeId == RCSResourceAttributes::TypeId::BOOL) + { + bool val = attrValue.get(); + jobject jobj = env->NewObject( + g_cls_Boolean, + g_mid_Boolean_ctor, + static_cast(val)); + return jobj; + } + else if (typeId == RCSResourceAttributes::TypeId::STRING) + { + std::string val = attrValue.get(); + jstring jstr = env->NewStringUTF(val.c_str()); + return static_cast(jstr); + } +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueInteger +(JNIEnv *env, jobject interfaceObject, jstring jKey, jint jValue) +{ + LOGD("RCSResourceAttributes_setValueInteger"); + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return; + + std::string key = env->GetStringUTFChars(jKey, NULL); + + RCSResourceAttributes setAttribute; + setAttribute[key] = static_cast(jValue); + *rep = setAttribute; +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueDouble +(JNIEnv *env, jobject interfaceObject, jstring jKey, jdouble jValue) +{ + LOGD("RCSResourceAttributes_setValueDouble"); + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return; + + std::string key = env->GetStringUTFChars(jKey, NULL); + + RCSResourceAttributes setAttribute; + setAttribute[key] = static_cast(jValue); + *rep = setAttribute; +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueBoolean +(JNIEnv *env, jobject interfaceObject, jstring jKey, jboolean jValue) +{ + LOGD("RCSResourceAttributes_setValueBoolean"); + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return; + + std::string key = env->GetStringUTFChars(jKey, NULL); + + RCSResourceAttributes setAttribute; + setAttribute[key] = static_cast(jValue); + *rep = setAttribute; +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueStringN +(JNIEnv *env, jobject interfaceObject, jstring jKey, jstring jValue) +{ + LOGD("RCSResourceAttributes_setValueStringN"); + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return; + + std::string key = env->GetStringUTFChars(jKey, NULL); + std::string value = env->GetStringUTFChars(jValue, NULL); + + RCSResourceAttributes setAttribute; + setAttribute[key] = value; + *rep = setAttribute; +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSetValueIntegerArray +(JNIEnv *env, jobject interfaceObject, jstring jKey, jintArray jValue) +{ + LOGD("RCSResourceAttributes_setValueIntegerArray"); + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + + const jsize len = env->GetArrayLength(jValue); + jint *ints = env->GetIntArrayElements(jValue, NULL); + + std::vector value; + for (jsize i = 0; i < len; ++i) + { + value.push_back(static_cast(ints[i])); + } + env->ReleaseIntArrayElements(jValue, ints, JNI_ABORT); + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return; + + std::string key = env->GetStringUTFChars(jKey, NULL); + RCSResourceAttributes setAttribute; +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeIsEmpty +(JNIEnv *env, jobject interfaceObject) +{ + LOGD("RCSResourceAttributes_isEmpty"); + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return false; + + return static_cast(rep->empty()); +} + +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeSize +(JNIEnv *env, jobject interfaceObject) +{ + LOGD("RCSResourceAttributes_size"); + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return -1; + + return static_cast(rep->size()); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeRemove +(JNIEnv *env, jobject interfaceObject, jstring jAttributeKey) +{ + LOGD("RCSResourceAttributes_remove"); + if (!jAttributeKey) + { + throwRCSException(env, "attributeKey cannot be null"); + return false; + } + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return false; + + std::string attributeKey = env->GetStringUTFChars(jAttributeKey, NULL); + return static_cast(rep->erase(attributeKey)); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_nativeHasAttribute +(JNIEnv *env, jobject interfaceObject, jstring jstr) +{ + LOGD("RCSResourceAttributes_hasAttribute"); + if (!jstr) + { + throwRCSException(env, "attributeKey cannot be null"); + return false; + } + + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + if (!rep) return false; + + std::string attributeKey = env->GetStringUTFChars(jstr, NULL); + return static_cast(rep->contains(attributeKey)); +} + +JNIEXPORT void JNICALL Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_create +(JNIEnv *env, jobject interfaceObject) +{ + LOGD("RCSResourceAttributes_create"); + RCSResourceAttributes *rep = new RCSResourceAttributes(); + RESetHandle(env, interfaceObject, rep); + if (env->ExceptionCheck()) + { + LOGE("Failed to set native handle for RCSResourceAttributes"); + delete rep; + } +} + +JNIEXPORT void JNICALL Java_org_iotivity_ResourceEncapsulation_common_RCSResourceAttributes_dispose +(JNIEnv *env, jobject interfaceObject, jboolean jNeedsDelete) +{ + LOGD("RCSResourceAttributes_dispose"); + RCSResourceAttributes *rep = JniResourceAttributes::getResourceAttributePtr(env, interfaceObject); + + if (jNeedsDelete) + { + delete rep; + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_builder.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_builder.cpp new file mode 100644 index 0000000..84243d7 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_builder.cpp @@ -0,0 +1,81 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +#include "jni_re_jvm.h" +#include "jni_re_resource_builder.h" +#include "jni_re_resource_object.h" +#include "RCSResourceObject.h" +#include "jni_re_resource_attributes.h" + +using namespace OIC::Service; + +JNIEXPORT jobject JNICALL Java_org_iotivity_ResourceEncapsulation_server_Builder_nativeBuild +(JNIEnv *env, jobject interfaceObject, jstring jResourceURI, jstring jResourceType, + jstring jResourceInterface, + jboolean jObservableFlag, jboolean jDiscovervableFlag, jobject jAttributes) +{ + LOGI("nativeBuild called"); + + std::string resourceURI; + if (jResourceURI) + { + resourceURI = env->GetStringUTFChars(jResourceURI, NULL); + } + std::string resourceType; + if (jResourceType) + { + resourceType = env->GetStringUTFChars(jResourceType, NULL); + } + std::string resourceInterface; + if (jResourceInterface) + { + resourceInterface = env->GetStringUTFChars(jResourceInterface, NULL); + } + + std::shared_ptr< RCSResourceObject > NativeRCSResourceObject; + try + { + if (jAttributes) + { + RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, + jAttributes); + NativeRCSResourceObject = RCSResourceObject::Builder(resourceURI, resourceType, + resourceInterface).setDiscoverable((bool)jObservableFlag).setObservable(( + bool)jObservableFlag).setAttributes(*attributesObj).build(); + } + else + { + NativeRCSResourceObject = RCSResourceObject::Builder(resourceURI, resourceType, + resourceInterface).setDiscoverable((bool)jObservableFlag).setObservable(( + bool)jObservableFlag).build(); + } + } + catch (OIC::Service::PlatformException exception) + { + LOGE("nativeBuild : platform Exception"); + throwRCSException(env, exception.what()); + } + + jobject jResource = env->NewObject(g_cls_RCSResourceObject, g_mid_RCSResourceObject_ctor); + JniRCSResourceObject *jniRCSResourceObject = new JniRCSResourceObject(NativeRCSResourceObject); + RESetHandle(env, jResource, jniRCSResourceObject); + + return jResource; + +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_object.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_object.cpp new file mode 100644 index 0000000..0a8e416 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_object.cpp @@ -0,0 +1,599 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_resource_object.h" +#include "jni_re_get_request_listener.h" +#include "RCSResourceObject.h" + +JniRCSResourceObject::JniRCSResourceObject(std::shared_ptr< RCSResourceObject> + resource) + : m_sharedResource(resource) +{} + +JniRCSResourceObject::~JniRCSResourceObject() +{ + LOGD("JniResourceObject::~JniResourceObject()"); + m_sharedResource = NULL; + + jint envRet; + JNIEnv *env = GetREJNIEnv(envRet); + if (NULL == env) return; + + if (JNI_EDETACHED == envRet) g_jvm->DetachCurrentThread(); +} + +JniRCSResourceObject *JniRCSResourceObject::getJniRCSResourceObject(JNIEnv *env, + jobject thiz) +{ + JniRCSResourceObject *resource = REGetHandle(env, thiz); + if (env->ExceptionCheck()) + { + LOGE("Failed to get native handle from RCSResourceObject"); + } + if (!resource) + { + LOGE("getJniRCSResourceObject :: no resource"); + } + return resource; +} + +void JniRCSResourceObject::setAttributeInteger(JNIEnv *env, jobject jObject, jstring jKey, + jint jValue) +{ + LOGD("JniRCSResourceObject::setAttributeInteger"); + + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + std::string key = env->GetStringUTFChars(jKey, NULL); + RCSResourceAttributes::Value value ; + RCSResourceAttributes::TypeId typeId; + + try + { + value = m_sharedResource->getAttributeValue(key); + if (value != NULL) + { + typeId = value.getType().getId(); + if (typeId == RCSResourceAttributes::TypeId::INT) + { + RCSResourceObject::LockGuard lock(*m_sharedResource); + m_sharedResource->getAttributes()[key] = (int)jValue; + } + else + { + LOGD("JniRCSResourceObject::setAttributeInteger value is not interger type"); + } + } + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } +} + +void JniRCSResourceObject::setAttributeDouble(JNIEnv *env, jobject jObject, jstring jKey, + jdouble jValue) +{ + LOGD("JniRCSResourceObject::setAttributeDouble"); + + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + std::string key = env->GetStringUTFChars(jKey, NULL); + RCSResourceAttributes::Value value ; + RCSResourceAttributes::TypeId typeId; + + try + { + value = m_sharedResource->getAttributeValue(key); + if (value != NULL) + { + typeId = value.getType().getId(); + if (typeId == RCSResourceAttributes::TypeId::DOUBLE) + { + RCSResourceObject::LockGuard lock(*m_sharedResource); + m_sharedResource->getAttributes()[key] = (double)jValue; + } + else + { + LOGD("JniRCSResourceObject::setAttributeInteger value is not double type"); + } + } + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } + +} + + +void JniRCSResourceObject::setAttributeBool(JNIEnv *env, jobject jObject, jstring jKey, + jboolean jValue) +{ + LOGD("JniRCSResourceObject::setAttributeBool"); + + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + std::string key = env->GetStringUTFChars(jKey, NULL); + RCSResourceAttributes::Value value ; + RCSResourceAttributes::TypeId typeId; + + try + { + value = m_sharedResource->getAttributeValue(key); + if (value != NULL) + { + typeId = value.getType().getId(); + if (typeId == RCSResourceAttributes::TypeId::BOOL) + { + RCSResourceObject::LockGuard lock(*m_sharedResource); + m_sharedResource->getAttributes()[key] = (bool)jValue; + } + else + { + LOGD("JniRCSResourceObject::setAttributeInteger value is not bool type"); + } + } + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } +} + +void JniRCSResourceObject::setAttributeString(JNIEnv *env, jobject jObject, jstring jKey, + jstring jValue) +{ + LOGD("JniRCSResourceObject::setAttributeString"); + + if (!jKey) + { + throwRCSException(env, "key cannot be null"); + return; + } + std::string key = env->GetStringUTFChars(jKey, NULL); + RCSResourceAttributes::Value value ; + RCSResourceAttributes::TypeId typeId; + + try + { + value = m_sharedResource->getAttributeValue(key); + if (value != NULL) + { + typeId = value.getType().getId(); + if (typeId == RCSResourceAttributes::TypeId::STRING) + { + RCSResourceObject::LockGuard lock(*m_sharedResource); + std::string nativeValue = env->GetStringUTFChars(jValue, NULL); + m_sharedResource->getAttributes()[key] = nativeValue; + } + else + { + LOGD("JniRCSResourceObject::setAttributeInteger value is not String type"); + } + } + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } +} + +jstring JniRCSResourceObject::getAttributeValue(JNIEnv *env, jstring jKey) +{ + char buf[64]; + std::string key; + if (jKey) + { + key = env->GetStringUTFChars(jKey, NULL); + } + else + { + LOGE("getAttributeValue Key is NULL"); + return NULL; + } + + RCSResourceAttributes::Value value; + RCSResourceAttributes::TypeId typeId; + try + { + value = m_sharedResource->getAttributeValue(key); + typeId = value.getType().getId(); + } + catch (BadRequestException exception) + { + throwRCSException(env, exception.what()); + } + + if (typeId == RCSResourceAttributes::TypeId::INT) + { + int val = value.get(); + sprintf(buf, "%d", val); + return (env)->NewStringUTF(buf); + } + else if (typeId == RCSResourceAttributes::TypeId::DOUBLE) + { + double val = value.get(); + sprintf(buf, "%f", val); + return (env)->NewStringUTF(buf); + } + else if (typeId == RCSResourceAttributes::TypeId::BOOL) + { + bool val = value.get(); + sprintf(buf, "%d", val); + return (env)->NewStringUTF(buf); + } + else if (typeId == RCSResourceAttributes::TypeId::STRING) + { + std::string val = value.get(); + jstring jstr = env->NewStringUTF(val.c_str()); + return jstr; + } +} + +bool JniRCSResourceObject::removeAttribute(JNIEnv *env, jstring jKey) +{ + std::string key; + if (jKey) + { + key = env->GetStringUTFChars(jKey, NULL); + } + return m_sharedResource->removeAttribute(key); +} +bool JniRCSResourceObject::containsAttribute(JNIEnv *env, jstring jKey) +{ + std::string key; + if (jKey) + { + key = env->GetStringUTFChars(jKey, NULL); + } + return m_sharedResource->containsAttribute(key); +} + +jobject JniRCSResourceObject::getAttributes(JNIEnv *env, jobject jObject) +{ + RCSResourceObject::LockGuard lock(*m_sharedResource); + RCSResourceAttributes nativeAttributes = m_sharedResource->getAttributes(); + + RCSResourceAttributes *attributesObj = new RCSResourceAttributes(nativeAttributes); + + jlong handle = reinterpret_cast(attributesObj); + jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, + g_mid_RCSRemoteResourceAttributesObject_ctor, + handle, true); + return jAttributes; +} + +bool JniRCSResourceObject::isObservable() +{ + return m_sharedResource->isObservable(); +} + +bool JniRCSResourceObject::isDiscoverable() +{ + return m_sharedResource->isDiscoverable(); +} + +void JniRCSResourceObject::setGetRequestHandler(JNIEnv *env, jobject jGetRequestListener) +{ + + JniGetRequestListener *onGetRequestListener = addOnGetRequestListener(env, jGetRequestListener); + + OIC::Service::RCSResourceObject::GetRequestHandler getRequestCallback = [onGetRequestListener]( + OIC::Service::RCSRequest request, + OIC::Service::RCSResourceAttributes attributes) + { + return onGetRequestListener->onGetRequestCallback(request, attributes); + }; + m_sharedResource->setGetRequestHandler(getRequestCallback); +} + +void JniRCSResourceObject::setSetRequestHandler(JNIEnv *env, jobject jSetRequestListener) +{ + + JniSetRequestListener *onSetRequestListener = addOnSetRequestListener(env, jSetRequestListener); + + OIC::Service::RCSResourceObject::SetRequestHandler setRequestCallback = [onSetRequestListener]( + OIC::Service::RCSRequest request, + OIC::Service::RCSResourceAttributes attributes) + { + return onSetRequestListener->onSetRequestCallback(request, attributes); + }; + m_sharedResource->setSetRequestHandler(setRequestCallback); +} + +void JniRCSResourceObject::addAttributeUpdatedListener(JNIEnv *env, jstring jKey, + jobject jAttributeUpdatedListener) +{ + std::string nativeKey; + if (jKey) + { + nativeKey = env->GetStringUTFChars(jKey, NULL); + } + else + { + LOGE("addAttributeUpdatedListener is NULL"); + return; + } + JniAttributeUpdatedListener *onAttributeUpdatedListener = addOnAttributeUpdatedListener(env, + jAttributeUpdatedListener); + + OIC::Service::RCSResourceObject::AttributeUpdatedListener attributeUpdatedCallback + = [onAttributeUpdatedListener]( OIC::Service::RCSResourceAttributes::Value oldValue, + OIC::Service::RCSResourceAttributes::Value newValue) + { + onAttributeUpdatedListener->onAttributeUpdatedCallback(oldValue, newValue); + }; + m_sharedResource->addAttributeUpdatedListener(nativeKey, attributeUpdatedCallback); +} + +bool JniRCSResourceObject::removeAttributeUpdatedListener(JNIEnv *env, jobject jObject, + jstring jKey) +{ + std::string nativeKey; + if (jKey) + { + nativeKey = env->GetStringUTFChars(jKey, NULL); + } + return m_sharedResource->removeAttributeUpdatedListener(nativeKey); +} + +void JniRCSResourceObject::notify() +{ + m_sharedResource->notify(); +} + +void JniRCSResourceObject::setAutoNotifyPolicy(int policy) +{ + RCSResourceObject::AutoNotifyPolicy nativePolicy = convertJavaNotifyPolicyToNativePolicy(policy); + m_sharedResource->setAutoNotifyPolicy(nativePolicy); +} + +int JniRCSResourceObject::getAutoNotifyPolicy() +{ + return convertAutoNotifyPolicyToInt(m_sharedResource->getAutoNotifyPolicy()); +} + +void JniRCSResourceObject::setSetRequestHandlerPolicy(int policy) +{ + RCSResourceObject::SetRequestHandlerPolicy nativePolicy = + convertJavasetRequestHandlerPolicyToNativePolicy(policy); + m_sharedResource->setSetRequestHandlerPolicy(nativePolicy); +} + +int JniRCSResourceObject::getSetRequestHandlerPolicy() +{ + return convertSetRequestHandlerPolicyToInt(m_sharedResource->getSetRequestHandlerPolicy()); +} + +JniGetRequestListener *JniRCSResourceObject::addOnGetRequestListener(JNIEnv *env, + jobject jListener) +{ + return this->m_onGetRequest.addListener(env, jListener, this); +} + +JniSetRequestListener *JniRCSResourceObject::addOnSetRequestListener(JNIEnv *env, + jobject jListener) +{ + return this->m_onSetRequest.addListener(env, jListener, this); +} + +JniAttributeUpdatedListener *JniRCSResourceObject::addOnAttributeUpdatedListener(JNIEnv *env, + jobject jListener) +{ + return this->m_onAttributeUpdated.addListener(env, jListener, this); +} + +void JniRCSResourceObject::removeOnGetRequestListener(JNIEnv *env, jobject jListener) +{ + this->m_onGetRequest.removeListener(env, jListener); +} + +void JniRCSResourceObject::removeOnSetRequestListener(JNIEnv *env, jobject jListener) +{ + this->m_onSetRequest.removeListener(env, jListener); +} + +void JniRCSResourceObject::removeOnAttributeUpdatedListener(JNIEnv *env, jobject jListener) +{ + this->m_onAttributeUpdated.removeListener(env, jListener); +} + + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeInteger +(JNIEnv *env, jobject jObject, jstring key, jint value) +{ + LOGD("RCSRemoteResourceObject_nativeSetAttributeInteger"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setAttributeInteger(env, jObject, key, value); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeDouble +(JNIEnv *env, jobject jObject, jstring key, jdouble value) +{ + LOGD("RCSRemoteResourceObject_nativeSetAttributeFloat"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setAttributeDouble(env, jObject, key, value); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeBool +(JNIEnv *env, jobject jObject, jstring key, jboolean value) +{ + LOGD("RCSRemoteResourceObject_nativeSetAttributeDouble"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setAttributeBool(env, jObject, key, value); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAttributeString +(JNIEnv *env, jobject jObject, jstring key, jstring value) +{ + LOGD("RCSRemoteResourceObject_nativeSetAttributeFloat"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setAttributeString(env, jObject, key, value); +} + +JNIEXPORT jstring JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetAttributeValue +(JNIEnv *env, jobject jObject, jstring key) +{ + LOGD("RCSRemoteResourceObject_nativeGetAttributeValue"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return resource->getAttributeValue(env, key); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeRemoveAttribute +(JNIEnv *env, jobject jObject, jstring key) +{ + LOGD("RCSRemoteResourceObject_nativeRemoveAttribute"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return (jboolean)resource->removeAttribute(env, key); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeContainsAttribute +(JNIEnv *env, jobject jObject, jstring key) +{ + LOGD("RCSRemoteResourceObject_nativeContainsAttribute"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return (jboolean)resource->containsAttribute(env, key); +} + +JNIEXPORT jobject JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetAttributes +(JNIEnv *env, jobject jObject) +{ + LOGD("RCSRemoteResourceObject_nativeGetAttributes"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return resource->getAttributes(env, jObject); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeIsObservable +(JNIEnv *env, jobject jObject) +{ + LOGD("RCSRemoteResourceObject_nativeIsObservable"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return (jboolean)resource->isObservable(); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeIsDiscoverable +(JNIEnv *env, jobject jObject) +{ + LOGD("RCSRemoteResourceObject_nativeIsDiscoverable"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return (jboolean)resource->isDiscoverable(); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetGetRequestHandler +(JNIEnv *env, jobject jObject , jobject getRequestListener) +{ + LOGD("RCSRemoteResourceObject_nativeSetGetRequestHandler"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setGetRequestHandler(env, getRequestListener); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetSetRequestHandler +(JNIEnv *env, jobject jObject , jobject setRequestListener) +{ + LOGD("RCSRemoteResourceObject_nativeSetSetRequestHandler"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setSetRequestHandler(env, setRequestListener); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeAddAttributeUpdatedListener +(JNIEnv *env, jobject jObject, jstring key, jobject attributeUpdatedListener) +{ + LOGD("RCSRemoteResourceObject_nativeAddAttributeUpdatedListener"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->addAttributeUpdatedListener(env, key, attributeUpdatedListener); +} + +JNIEXPORT jboolean JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeRemoveAttributeUpdatedListener +(JNIEnv *env, jobject jObject, jstring key) +{ + LOGD("RCSRemoteResourceObject_nativeAddAttributeUpdatedListener"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return (jboolean)resource->removeAttributeUpdatedListener(env, jObject, key); +} + +JNIEXPORT void JNICALL Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeNotify +(JNIEnv *env, jobject jObject) +{ + LOGD("RCSRemoteResourceObject_nativeNotify"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->notify(); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetAutoNotifyPolicy +(JNIEnv *env, jobject jObject, jint jPolicy) +{ + LOGD("RCSRemoteResourceObject_nativeSetAutoNotifyPolicy"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setAutoNotifyPolicy(jPolicy); +} + +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetAutoNotifyPolicy +(JNIEnv *env, jobject jObject) +{ + LOGD("RCSRemoteResourceObject_nativeGetAutoNotifyPolicy"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return (jint)resource->getAutoNotifyPolicy(); +} + +JNIEXPORT void JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeSetSetRequestHandlerPolicy +(JNIEnv *env, jobject jObject, jint jPolicy) +{ + LOGD("RCSRemoteResourceObject_nativeSetSetRequestHandlerPolicy"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + resource->setSetRequestHandlerPolicy(jPolicy); +} + +JNIEXPORT jint JNICALL +Java_org_iotivity_ResourceEncapsulation_server_RCSResourceObject_nativeGetSetRequestHandlerPolicy +(JNIEnv *env, jobject jObject) +{ + LOGD("RCSRemoteResourceObject_nativeGetSetRequestHandlerPolicy"); + JniRCSResourceObject *resource = JniRCSResourceObject::getJniRCSResourceObject(env, jObject); + return (jint)resource->getSetRequestHandlerPolicy(); +} + + diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_statechange_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_statechange_listener.cpp new file mode 100644 index 0000000..716cda6 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_resource_statechange_listener.cpp @@ -0,0 +1,103 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_resource_statechange_listener.h" +#include "jni_re_rcs_remoteresourceobject.h" + +JniResourceStateChangeListener::JniResourceStateChangeListener(JNIEnv *env, jobject jListener, + JniRCSRemoteResourceObject *owner) + : m_ownerResource(owner) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); +} + +JniResourceStateChangeListener::~JniResourceStateChangeListener() +{ + LOGI("~JniResourceStateChangeListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniResourceStateChangeListener::stateChangeCallback(OIC::Service::ResourceState state) +{ + + LOGI("JniResourceStateChangeListener::stateChangeCallback enter"); + + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jclass clsL = env->GetObjectClass(jListener); + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + jmethodID midL = env->GetMethodID(clsL, "onStateChanged", + "(Lorg/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject$ResourceState;)V"); + if (!midL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + jobject resourcestate = convertNativeResourceStateToJavaResourceState(state, env); + env->CallVoidMethod(jListener, midL, resourcestate); + + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); +} + +void JniResourceStateChangeListener::checkExAndRemoveListener(JNIEnv *env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_ownerResource->removeOnStateChangeListener(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_ownerResource->removeOnStateChangeListener(env, m_jwListener); + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_set_request_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_set_request_listener.cpp new file mode 100644 index 0000000..f2818dd --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_set_request_listener.cpp @@ -0,0 +1,214 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_set_request_listener.h" +#include "jni_re_resource_object.h" + +JniSetRequestListener::JniSetRequestListener(JNIEnv *env, jobject jListener, + JniRCSResourceObject *owner) + : m_ownerResource(owner) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); +} + +JniSetRequestListener::~JniSetRequestListener() +{ + LOGI("JniSetRequestListener::~JniSetRequestListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +OIC::Service::RCSSetResponse JniSetRequestListener::onSetRequestCallback( + OIC::Service::RCSRequest request, OIC::Service::RCSResourceAttributes attributes) +{ + LOGI("JniGetRequestListener::onSetRequestCallback enter"); + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) + LOGE("onSetRequestCallback: env is NULL"); + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGE("onSetRequestCallback: jListener is NULL"); + } + + jclass clsL = env->GetObjectClass(jListener); + + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGE("onSetRequestCallback: clsL is NULL"); + } + + //constructing java attribute object.. + RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); + jlong handle = reinterpret_cast(attributesObj); + jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, + g_mid_RCSRemoteResourceAttributesObject_ctor, + handle, true); + if (!jAttributes) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGE("onGetRequestCallback: jAttributes == null"); + } + + //constructing java RCSRequest object + std::string resourceUri = request.getResourceUri(); + jstring javaUri = env->NewStringUTF(resourceUri.c_str()); + + g_mid_RCSRequest_ctor = env->GetMethodID(g_cls_RCSRequest, "", "(Ljava/lang/String;)V"); + jobject javaRCSRequestObject = env->NewObject( g_cls_RCSRequest, g_mid_RCSRequest_ctor, javaUri); + + //getting the method ID + jmethodID midL = env->GetMethodID(clsL, "onSetRequest", + "(Lorg/iotivity/ResourceEncapsulation/server/RCSRequest;Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;)Lorg/iotivity/ResourceEncapsulation/server/RCSSetResponse;"); + + if (!midL) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("onGetRequestCallback: midL == null"); + } + + //calling the Java method + jobject javaSetResponse = env->CallObjectMethod(jListener, midL, javaRCSRequestObject, + jAttributes); //todo + + //getting the information related to Response + jclass SetResponseClass = env->GetObjectClass(javaSetResponse); + + jmethodID midSetTypeOfGetResponse = env->GetMethodID(SetResponseClass, "getTypeOfGetResponse", + "()I"); + jint type = env->CallIntMethod(javaSetResponse, midSetTypeOfGetResponse); + + if (0 == type) + { + LOGI("Type == Default"); + return OIC::Service::RCSSetResponse::defaultAction(); + } + else if (1 == type) + { + LOGI("Type == 1"); + jmethodID midGetResult = env->GetMethodID(SetResponseClass, "getResult", "()I"); + jmethodID midGetErrorCode = env->GetMethodID(SetResponseClass, "getErrorCode", "()I"); + jint handlerResult = env->CallIntMethod(javaSetResponse, midGetResult); + jint errorCode = env->CallIntMethod(javaSetResponse, midGetErrorCode); + OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); + return OIC::Service::RCSSetResponse::create(nativeHandlerResult, errorCode); + } + else if (2 == type) + { + LOGI("Type == 2"); + jmethodID midGetAttributes = env->GetMethodID(SetResponseClass, "getAttributes", + "()Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;"); + jobject attributes = env->CallObjectMethod(javaSetResponse, midGetAttributes); + RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, + attributes); + return OIC::Service::RCSSetResponse::create(*attributesObj); + } + else if (3 == type) + { + LOGI("Type == 3"); + jmethodID midGetResult = env->GetMethodID(SetResponseClass, "getResult", "()I"); + jmethodID midGetErrorCode = env->GetMethodID(SetResponseClass, "getErrorCode", "()I"); + jint handlerResult = env->CallIntMethod(javaSetResponse, midGetResult); + jint errorCode = env->CallIntMethod(javaSetResponse, midGetErrorCode); + OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); + jmethodID midGetAttributes = env->GetMethodID(SetResponseClass, "getAttributes", + "()Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;"); + jobject attributes = env->CallObjectMethod(javaSetResponse, midGetAttributes); + RCSResourceAttributes *attributesObj = JniResourceAttributes::getResourceAttributePtr(env, + attributes); + return OIC::Service::RCSSetResponse::create(*attributesObj, nativeHandlerResult, errorCode); + } + else if (4 == type) + { + LOGI("Type == 4"); + return OIC::Service::RCSSetResponse::accept(); + } + else if (5 == type) + { + LOGI("Type == 5"); + jmethodID midGetResult = env->GetMethodID(SetResponseClass, "getResult", "()I"); + jmethodID midGetErrorCode = env->GetMethodID(SetResponseClass, "getErrorCode", "()I"); + jint handlerResult = env->CallIntMethod(javaSetResponse, midGetResult); + jint errorCode = env->CallIntMethod(javaSetResponse, midGetErrorCode); + OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); + return OIC::Service::RCSSetResponse::accept(nativeHandlerResult, errorCode); + } + else if (6 == type) + { + LOGI("Type == 6"); + return OIC::Service::RCSSetResponse::ignore(); + } + else if (7 == type) + { + LOGI("Type == 7"); + jmethodID midGetResult = env->GetMethodID(SetResponseClass, "getResult", "()I"); + jmethodID midGetErrorCode = env->GetMethodID(SetResponseClass, "getErrorCode", "()I"); + jint handlerResult = env->CallIntMethod(javaSetResponse, midGetResult); + jint errorCode = env->CallIntMethod(javaSetResponse, midGetErrorCode); + OCEntityHandlerResult nativeHandlerResult = convertIntToOCEntityHandlerResult(handlerResult); + return OIC::Service::RCSSetResponse::ignore(nativeHandlerResult, errorCode); + } + else + { + LOGI("Default action"); + return OIC::Service::RCSSetResponse::defaultAction(); + } + + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete attributesObj; + } + + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + LOGI("JniGetRequestListener::onSetRequestCallback exit"); +} + +void JniSetRequestListener::checkExAndRemoveListener(JNIEnv *env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_ownerResource->removeOnSetRequestListener(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_ownerResource->removeOnSetRequestListener(env, m_jwListener); + } +} diff --git a/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_setattributes_listener.cpp b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_setattributes_listener.cpp new file mode 100644 index 0000000..e48910e --- /dev/null +++ b/service/resource-encapsulation/sdk/java/jni/re/src/jni_re_setattributes_listener.cpp @@ -0,0 +1,118 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +#include "jni_re_setattributes_listener.h" +#include "jni_re_rcs_remoteresourceobject.h" + +JniSetAttributesListener::JniSetAttributesListener(JNIEnv *env, jobject jListener, + JniRCSRemoteResourceObject *owner) + : m_ownerResource(owner) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); +} + +JniSetAttributesListener::~JniSetAttributesListener() +{ + LOGI("~JniGetAttributesListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniSetAttributesListener::onRemoteAttributesSetCallback(OIC::Service::RCSResourceAttributes + attributes) +{ + + LOGI("JniSetAttributesListener::onRemoteAttributesSetCallback : got attributes"); + + jint ret; + JNIEnv *env = GetREJNIEnv(ret); + if (NULL == env) return; + + jobject jListener = env->NewLocalRef(m_jwListener); + if (!jListener) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jclass clsL = env->GetObjectClass(jListener); + + if (!clsL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + RCSResourceAttributes *attributesObj = new RCSResourceAttributes(attributes); + jlong handle = reinterpret_cast(attributesObj); + jobject jAttributes = env->NewObject(g_cls_RCSRemoteResourceAttributesObject, + g_mid_RCSRemoteResourceAttributesObject_ctor, + handle, true); + if (!jAttributes) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + jmethodID midL = env->GetMethodID(clsL, "onRemoteAttributesGetCallback", + "(Lorg/iotivity/ResourceEncapsulation/common/RCSResourceAttributes;)V"); + + if (!midL) + { + delete attributesObj; + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + env->CallVoidMethod(jListener, midL, jAttributes); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + delete attributesObj; + } + + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); +} + +void JniSetAttributesListener::checkExAndRemoveListener(JNIEnv *env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_ownerResource->removeOnsetAttributesListener(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_ownerResource->removeOnsetAttributesListener(env, m_jwListener); + } +} diff --git a/service/resource-encapsulation/sdk/java/project.properties b/service/resource-encapsulation/sdk/java/project.properties new file mode 100644 index 0000000..93c8c3c --- /dev/null +++ b/service/resource-encapsulation/sdk/java/project.properties @@ -0,0 +1,15 @@ +# This file is automatically generated by Android Tools. +# Do not modify this file -- YOUR CHANGES WILL BE ERASED! +# +# This file must be checked in Version Control Systems. +# +# To customize properties used by the Ant build system edit +# "ant.properties", and override values to adapt the script to your +# project structure. +# +# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): +#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt + +# Project target. +target=android-21 +android.library=true diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSAddress.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSAddress.java new file mode 100644 index 0000000..6707810 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSAddress.java @@ -0,0 +1,114 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSAddress.java + * + * @brief This file provides a class for a set of APIs relating to resource address and the + * the type of discovery request for a resource of interest. + * + */ + +package org.iotivity.ResourceEncapsulation.client; + +import org.iotivity.ResourceEncapsulation.utils.DiscoveryType; + +/** + * This class provides a set of APIs for constructing RCSAddress object. + * + *

+ * RCSAddress object is the first parameter for Discover Resource APIs of + * RCSDiscoveryManager Class. + *

+ * {@link RCSDiscoveryManager} + */ +public class RCSAddress { + + static { + System.loadLibrary("gnustl_shared"); + System.loadLibrary("oc_logger"); + System.loadLibrary("connectivity_abstraction"); + System.loadLibrary("ca-interface"); + System.loadLibrary("octbstack"); + System.loadLibrary("oc"); + System.loadLibrary("ocstack-jni"); + System.loadLibrary("rcs_common"); + System.loadLibrary("rcs_client"); + System.loadLibrary("rcs_server"); + System.loadLibrary("re-jni"); + } + + private String m_address; + private DiscoveryType m_typeOfDiscovery; + private static RCSAddress m_rcsAddressInstance; + + private RCSAddress(String addr, DiscoveryType discoveryType) { + m_address = addr; + m_typeOfDiscovery = discoveryType; + } + + /** + * Constructs a RCSAddress object for multicast discovery. + * + * @return RCSAddress - Instance of RCSAddress. + * + * {@link RCSAddress} + */ + public static RCSAddress multicast() { + m_rcsAddressInstance = new RCSAddress("", DiscoveryType.MULTICAST); + return m_rcsAddressInstance; + } + + /** + * Constructs a RCSAddress object for setting unicast discovery. + * + * @param address + * required discovery address for the unicast resource discovery. + * + * @return RCSAddress -Instance of RCSAddress. + * + * {@link RCSAddress} + */ + public static RCSAddress unicast(String address) { + m_rcsAddressInstance = new RCSAddress(address, DiscoveryType.UNICAST); + return m_rcsAddressInstance; + } + + // APIs used by DiscoveryManager class for getting the address information. + + /** + * Get the discovery resource address in string format. + * + * @return String - Discovery address in string format. + */ + public String getAddress() { + return m_address; + } + + /** + * Get the resource discovery type(unicast or multicast). + * + * @return DiscoveryType + * + * {@link DiscoveryType} + */ + public DiscoveryType getTypeOfDiscovery() { + return m_typeOfDiscovery; + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSDiscoveryManager.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSDiscoveryManager.java new file mode 100644 index 0000000..07cf4a1 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSDiscoveryManager.java @@ -0,0 +1,178 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSDiscoveryManager.java + * + * @brief This file provides a class for a set of APIs relating to discovery of a particular + * resource in the network + * + */ + +package org.iotivity.ResourceEncapsulation.client; + +import org.iotivity.ResourceEncapsulation.common.RCSException; +import org.iotivity.ResourceEncapsulation.utils.DiscoveryType; + +import android.util.Log; + +/** + * This class contains the resource discovery method. + * + * {@link RCSRemoteResourceObject} + */ +public class RCSDiscoveryManager { + + private IDiscoverResourceListener m_discoverListener; + private static RCSDiscoveryManager m_rcsDiscoveryManagerInstacne; + private final String OC_RSRVD_WELL_KNOWN_URI = "/oic/res"; + private final String LOG_TAG = this.getClass() + .getSimpleName(); + + private native void discoverResource(String address, int typeOfDiscovery, + String relativeUri, String resourceType, + IDiscoverResourceListener listener); + + /** + * Constructor for RCSDiscoveryManager. Set listener to null on constructing + * new RCSDiscoveryManager. + */ + private RCSDiscoveryManager() { + } + + /** + * Returns RCSDiscoveryManager instance if already present else creates a + * new instance of RCSDiscoveryManager. + * + * @return RCSDiscoveryManager - Instance of RCSDiscoveryManager. + */ + public static RCSDiscoveryManager getInstance() { + if (null == m_rcsDiscoveryManagerInstacne) { + m_rcsDiscoveryManagerInstacne = new RCSDiscoveryManager(); + } + return m_rcsDiscoveryManagerInstacne; + } + + /** + * Provides interface for receiving the callback for discovering a resource + * of interest. + */ + public interface IDiscoverResourceListener { + + /** + * This callback method is called when a asynchronous response for the + * discoverResource request is received. + * + * @param rcsRemoteResourceObject + * It is the resource for which discovery is requested.. + * + * {@link RCSDiscoveryManager} + * {@link RCSRemoteResourceObject} + */ + public void onResourceDiscovered( + RCSRemoteResourceObject rcsRemoteResourceObject); + + } + + /** + * Set listener for receiving asynchronous response for resource discovery + * request. + * + * @param listener + * setDiscoverResourceListener to receive the discovered + * resources. + */ + public void setDiscoverResourceListener(IDiscoverResourceListener listener) { + m_discoverListener = listener; + } + + /** + * API for discovering the resource of Interest, regardless of URI and + * resource type + * + * @param address + * A RCSAddress object + * + * {@link RCSAddress} + */ + public void discoverResource(RCSAddress address) throws RCSException { + + Log.i(LOG_TAG, "discoverResource called"); + discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, ""); + } + + /** + * API for discovering the resource of Interest, regardless of resource + * type. + * + * @param address + * A RCSAddress object + * @param relativeURI + * The relative uri of resource to be searched + * + * {@link RCSAddress} + */ + public void discoverResource(RCSAddress address, String relativeURI) + throws RCSException { + + Log.i(LOG_TAG, "discoverResource called"); + discoverResourceByType(address, relativeURI, ""); + } + + /** + * API for discovering the resource of Interest by resource type. + * + * @param address + * A RCSAddress object + * @param resourceType + * Ressource Type + * + * {@link RCSAddress} + */ + public void discoverResourceByType(RCSAddress address, String resourceType) + throws RCSException { + + Log.i(LOG_TAG, "discoverResource called"); + discoverResourceByType(address, OC_RSRVD_WELL_KNOWN_URI, resourceType); + } + + /** + * API for discovering the resource of Interest by Resource type with + * provided relativeURI + * + * @param address + * A RCSAddress object + * @param relativeURI + * The relative uri of resource to be searched + * @param resourceType + * Ressource Type + * + * {@link RCSAddress} + */ + public void discoverResourceByType(RCSAddress address, String relativeURI, + String resourceType) throws RCSException { + + Log.i(LOG_TAG, "discoverResource called"); + String addr = address.getAddress(); + DiscoveryType typeOfDiscovery = address.getTypeOfDiscovery(); + this.discoverResource(addr, typeOfDiscovery.ordinal(), relativeURI, + resourceType, m_discoverListener); + } + +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject.java new file mode 100644 index 0000000..8e77d21 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/client/RCSRemoteResourceObject.java @@ -0,0 +1,559 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSRemoteResourceObject.java + * + * @brief This file provides a class which provides a set of native functions and API's + * of resource client, provided to the developers. + */ + +package org.iotivity.ResourceEncapsulation.client; + +import java.util.List; +import java.util.Vector; + +import org.iotivity.ResourceEncapsulation.common.RCSException; +import org.iotivity.ResourceEncapsulation.common.RCSResourceAttributes; + +import android.util.Log; + +/** + * + * The resource can be discovered with discoverResource. On successful resource + * discovery an instance of this class is returned. This class is an interaction + * point between Resource and the developers. Developer will get the + * RCSRemoteResourceObject by calling RCSDiscoveryManager's discoverResource + * API. + * + * {@link RCSDiscoveryManager} + * + */ +public class RCSRemoteResourceObject { + + private IStateChangedCallbackListener m_stateChangedListener; + private ICacheUpdateListener m_cacheUpdateListener; + private IRemoteAttributeListener m_remoteAttributeListener; + private long m_nativeHandle; + private String LOG_TAG = this.getClass() + .getSimpleName(); + + // native methods + private native boolean nativeIsMonitoring(); + + private native boolean nativeIsCaching(); + + private native boolean nativeIsObservable(); + + private native void nativeStartMonitoring( + IStateChangedCallbackListener listener); + + private native void nativeStopMonitoring(); + + private native int nativeGetState(); + + private native void nativeStartCaching(ICacheUpdateListener listener); + + private native void nativeStopCaching(); + + private native int nativeGetCacheState(); + + private native boolean nativeIsCachedAvailable(); + + private native RCSResourceAttributes nativeGetCachedAttributes(); + + private native String nativeGetCachedAttribute(String key); + + private native void nativeGetRemoteAttributes( + IRemoteAttributeListener listener); + + private native void nativeSetRemoteAttributes( + RCSResourceAttributes attributes, IRemoteAttributeListener listener); + + private native String nativeGetUri(); + + private native String nativeGetAddress(); + + private native List nativeGetTypes(); + + private native List nativeGetInterFaces(); + + private RCSRemoteResourceObject(long nativeHandle) { + this.m_nativeHandle = nativeHandle; + } + + /** + * This Enum contains Status codes for resource state. + * + *

+ * Developer will get the ResoureState as a result of getState API of + * RCSRemoteResourceObject class and as a callback result of startMonitoring + * API + * + * {@link RCSRemoteResourceObject} + */ + public enum ResourceState { + + NONE, /** < Monitoring is not started. */ + REQUESTED, /** + * < Monitoring is started and checking state is in progress. + * This is the default state after startMonitoring. + */ + ALIVE, /** < The resource is alive. */ + LOST_SIGNAL, /** < Failed to reach the resource. */ + DESTROYED; + /** < The resource is deleted. */ + + /** + * Utility API for integer to enum conversion. + */ + public static ResourceState getResourceState(int ordinal) { + + if (ordinal == 0) + return ResourceState.values()[0]; + else if (ordinal == 1) + return ResourceState.values()[1]; + else if (ordinal == 2) + return ResourceState.values()[2]; + else if (ordinal == 3) + return ResourceState.values()[3]; + + return null; + } + } + + /** + * This Enum contains Status codes for cache state. + * + *

+ * Application will get this CacheState as a result of getCacheState API of + * RCSRemoteResourceObject class + * + * {@link RCSRemoteResourceObject} + */ + public enum CacheState { + + NONE, /** < Caching is not started. */ + UNREADY, /** + * < Caching is started, but the data is not ready yet. This is + * the default state after startCaching. + */ + READY, /** < The data is ready. */ + LOST_SIGNAL; + /** < Failed to reach the resource. */ + + /** + * Utility API for int to enum conversion. + */ + public static CacheState getCacheState(int ordinal) { + + CacheState result = CacheState.values()[0]; + + if (ordinal == 1) + result = CacheState.values()[1]; + else if (ordinal == 2) + result = CacheState.values()[2]; + else if (ordinal == 3) + result = CacheState.values()[3]; + + return result; + } + } + + /** + * Provides interface for receiving the callback on cache update. + */ + public interface ICacheUpdateListener { + + /** + * This callback method is called when a asynchronous response for + * startCaching request is received. Listener should be set to receive + * this callback. + * + * @param attributes + * It is the updated resource attributes. + * + * {@link RCSResourceAttributes} + */ + public void onCacheUpdatedCallback(RCSResourceAttributes attributes); + + } + + /** + * Provides interface for receiving the callback for the getRemoteAttributes + * and setRemoteAttributes requested actions. + */ + public interface IRemoteAttributeListener { + + /** + * This callback method is called when a asynchronous response for the + * getRemoteAttributes request or setRemoteAttributes request is + * received. + * + * @param attributes + * It is the updated resource attributes value received from + * the remote resource. + * + * {@link RCSResourceAttributes} + */ + public void onRemoteAttributesGetCallback( + RCSResourceAttributes attributes); + + } + + /** + * Provides interface for receiving the callback for the startMonitoring + * This callback is received on remote resource state change. + */ + public interface IStateChangedCallbackListener { + + /** + * This callback method is called when a asynchronous response for the + * startMonitoring request is received. + * + * @param resourceState + * It gives the current remote resource state. + * + * {@link ResourceState} + */ + public void onStateChanged(ResourceState resourceState); + } + + /** + * Set listener for receiving asynchronous response for cache updates. + * + * @param listener + * ICacheUpdateListener to receive asynchronous response for + * cache updates. + */ + public void setCacheUpdateListener(ICacheUpdateListener listener) { + m_cacheUpdateListener = listener; + } + + /** + * Set listener for receiving asynchronous response for remote resource + * state changes. + * + * @param listener + * IStateChangedCallbackListener to receive asynchronous response + * for remote resource state changes. + */ + public void setStateChangedListener(IStateChangedCallbackListener listener) { + m_stateChangedListener = listener; + } + + /** + * Set listener for receiving asynchronous response for resource attributes + * SET and GET request. + * + * @param listener + * IRemoteAttributeListener to receive asynchronous response for + * resource attributes SET and GET request. + */ + public void setRemoteAttributesListener(IRemoteAttributeListener listener) { + m_remoteAttributeListener = listener; + } + + /** + * Returns whether monitoring is enabled. + * + * @return boolean - true if monitoring the resource. + * + * {@link #startMonitoring()} + */ + public boolean isMonitoring() { + + Log.i(LOG_TAG, "isMonitoring called"); + return this.nativeIsMonitoring(); + } + + /** + * Returns whether caching is enabled. + * + * @return boolean - true if caching the resource. + * + * {@link #startCaching()} + */ + public boolean isCaching() { + + Log.i(LOG_TAG, "isCaching called"); + return this.nativeIsCaching(); + } + + /** + * Returns whether resource is observable. + * + * @return boolean - true if resource is observable. + */ + public boolean isObservable() { + + Log.i(LOG_TAG, "isObservable called"); + return this.nativeIsObservable(); + } + + /** + * Starts monitoring the resource. + *

+ * Monitoring provides a feature to check the presence of a resource, even + * when the server is not announcing Presence using startPresnece. + *

+ * StateChangedListener should be set before calling this API + * + * @throws RCSException + * If monitoring is already started. + * + * {@link IStateChangedCallbackListener} {@link ResourceState} + * {@link #isMonitoring()} {@link #stopMonitoring()} + */ + public void startMonitoring() throws RCSException { + + Log.i(LOG_TAG, "startMonitoring called"); + this.nativeStartMonitoring(m_stateChangedListener); + } + + /** + * Stop monitoring the resource. + *

+ * It does nothing if monitoring is not started. + *

+ * {@link #startMonitoring()} + */ + public void stopMonitoring() { + + Log.i(LOG_TAG, "stopMonitoring called"); + this.nativeStopMonitoring(); + } + + /** + * Returns the current state of the resource. + * + * @return ResourceState - current resource state + * + * {@link #startMonitoring()} {@link ResourceState} + */ + public ResourceState getState() { + + Log.i(LOG_TAG, "getState called"); + int state = this.nativeGetState(); + + return ResourceState.getResourceState(state); + } + + /** + * Starts caching attributes of the resource. + * + * This will start data caching for the resource. Once caching started it + * will look for the data updation on the resource and updates the cache + * data accordingly. + * + * @throws RCSException + * + *

+ * If this method is called with null listener it will not + * notify the updated value to caller. It it is called with a + * valid listener updated cached values will be notified to + * caller. + * + * {@link #getCacheState()} {@link #getCachedAttributes()} + */ + public void startCaching() throws RCSException { + + if (null == m_cacheUpdateListener) { + Log.i(LOG_TAG, "startCaching WITHOUT callback"); + } else { + Log.i(LOG_TAG, "startCaching WITH callback"); + } + + this.nativeStartCaching(m_cacheUpdateListener); + } + + /** + * Stops caching. + * + * It does nothing if caching is not started. + * + * {@link #startCaching()} + */ + public void stopCaching() { + + Log.i(LOG_TAG, "stopCaching called"); + this.nativeStopCaching(); + } + + /** + * Returns the current cache state. + * + * @return CacheState - current cache state. + * + * {@link CacheState} + */ + public CacheState getCacheState() { + + Log.i(LOG_TAG, "getCacheState called"); + return CacheState.getCacheState(this.nativeGetCacheState()); + } + + /** + * Returns whether cached data is available. + * + * Cache will be available always after cache state READY even if current + * state is LOST_SIGNAL. + * + * @return boolean - true if cache data is available. + * + * {@link #startCaching()} {@link CacheState} + */ + public boolean isCachedAvailable() { + + Log.i(LOG_TAG, "isCachedAvailable called"); + return this.nativeIsCachedAvailable(); + } + + /** + * Gets the cached RCSResourceAttributes data. + * + *

+ * Cache should be available. + * + * @return RCSResourceAttributes + * + * @throws RCSException + * If the Cache is not started. + * + * {@link RCSResourceAttributes} {@link #isCachedAvailable()} + * {@link #startCaching()} + */ + public RCSResourceAttributes getCachedAttributes() throws RCSException { + + Log.i(LOG_TAG, "getCachedAttributes called"); + return this.nativeGetCachedAttributes(); + } + + /** + * Gets a particular cached ResourceAttribute Value. + * + *

+ * Cache should be available. + * + * @param key + * particular attribute name as a string to get the value for. + * + * @return String - Requested attribute value in string. + * + * @throws RCSException + * If the Cache is not started or InvalidKeyException If @a key + * doesn't match the key of any value. + * + * {@link RCSResourceAttributes} {@link #isCachedAvailable()} + * {@link #startCaching()} + */ + public String getCachedAttribute(String key) throws RCSException { + + Log.i(LOG_TAG, "getCachedAttribute called"); + return this.nativeGetCachedAttribute(key); + } + + /** + * Gets resource attributes directly from the server. + * + * This API send a get request to the resource of interest and provides the + * attributes to the caller in the RemoteAttributesReceivedCallback. + * + *

+ * IRemoteAttributeListener should be set to get the attributes + * + * {@link IRemoteAttributeListener} + */ + public void getRemoteAttributes() throws RCSException { + + Log.i(LOG_TAG, "getRemoteAttributes called"); + this.nativeGetRemoteAttributes(m_remoteAttributeListener); + } + + /** + * Sends a set request with resource attributes to the server. + * + * The SetRequest behavior depends on the server, whether updating its + * attributes or not. + * + * @param attributes + * Attribute value to set for the remote resource. + * + *

+ * IRemoteAttributeListener should be set to call this API + * + * {@link IRemoteAttributeListener} + */ + public void setRemoteAttributes(RCSResourceAttributes attributes) + throws RCSException { + + Log.i(LOG_TAG, "getRemoteAttributes called"); + this.nativeSetRemoteAttributes(attributes, m_remoteAttributeListener); + } + + /** + * Returns the uri of the resource. + * + * @return Uri of the resource + */ + public String getUri() { + + Log.i(LOG_TAG, "getUri called"); + return this.nativeGetUri(); + } + + /** + * Returns the address of the resource . + * + * @return Address of the resource + */ + public String getAddress() { + + Log.i(LOG_TAG, "getAddress called"); + return this.nativeGetAddress(); + } + + /** + * Returns the resource types of the resource. + * + * @return vector containing resource types + */ + public Vector getTypes() { + + Log.i(LOG_TAG, "getTypes called"); + List types = this.nativeGetTypes(); + Vector resourceType = new Vector(); + resourceType.addAll(types); + return resourceType; + } + + /** + * Returns the resource interfaces of the resource. + * + * @return vector containing resource Interfaces + */ + public Vector getInterFaces() { + + Log.i(LOG_TAG, "getInterFaces called"); + List interfaces = this.nativeGetInterFaces(); + Vector resourceInterfaces = new Vector(); + resourceInterfaces.addAll(interfaces); + return resourceInterfaces; + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSException.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSException.java new file mode 100644 index 0000000..5c39190 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSException.java @@ -0,0 +1,37 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSException.java + * + * @brief This file provides RCSException class for RE layer + * + */ + +package org.iotivity.ResourceEncapsulation.common; + +/** + * This is the Exception class for the Resource Encapsulation APIs + */ +public class RCSException extends Exception { + + public RCSException(String exception) { + super(exception); + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSResourceAttributes.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSResourceAttributes.java new file mode 100644 index 0000000..79114c3 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/common/RCSResourceAttributes.java @@ -0,0 +1,295 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSResourceAttributes.java + * + * @brief This file provides a class which represents the attributes for a resource. + */ + +package org.iotivity.ResourceEncapsulation.common; + +import org.iotivity.ResourceEncapsulation.client.RCSDiscoveryManager; +import org.iotivity.ResourceEncapsulation.client.RCSRemoteResourceObject; +import org.iotivity.ResourceEncapsulation.server.RCSResourceObject; + +/** + * RCSResourceAttributes represents the attributes for a resource. + * + * An attribute value can be one of various types : integer, double, boolean, + * String
+ * + *

+ * If developer on client side wants to get the RCSResourceAttributes for the + * resource of interest following are the steps: - first call the discover API + * of DiscoveryManager class. - After getting the RemoteResourceObject, call + * getRemoteAttributes() API of RemoteResourceObject class + * + * If the developer on server side wants to get the attributes following are the + * steps: Call the getAttributes() API of RCSResourceObject class. + * + * {@link RCSDiscoveryManager} {@link RCSRemoteResourceObject} + * {@link RCSResourceObject} + */ +public class RCSResourceAttributes { + + private long m_nativeHandle; + private boolean m_nativeNeedsDelete; + + // native methods + private native Object nativeGetValueN(String key); + + private native void nativeSetValueInteger(String key, int value); + + private native void nativeSetValueDouble(String key, double value); + + private native void nativeSetValueBoolean(String key, boolean value); + + private native void nativeSetValueString(String key, String value); + + private native boolean nativeIsEmpty(); + + private native int nativeSize(); + + private native boolean nativeRemove(String key); + + private native boolean nativeHasAttribute(String key); + + private native void create(); + + private native void dispose(boolean needsDelete); + + /** + * Constructor for RCSResourceAttributes. Invokes create() which internally + * constructs a new RCSResourceAttributes and sets its handle. + * + *

+ * Native RCSResourceAttributes object was created using "new" and needs to + * be deleted + */ + public RCSResourceAttributes() { + create(); + this.m_nativeNeedsDelete = true; + } + + private RCSResourceAttributes(long nativeHandle) { + this.m_nativeHandle = nativeHandle; + this.m_nativeNeedsDelete = false; + } + + private RCSResourceAttributes(long nativeHandle, boolean nativeNeedsDelete) { + this.m_nativeHandle = nativeHandle; + this.m_nativeNeedsDelete = nativeNeedsDelete; + } + + private T getValue(String key) throws RCSException { + Object obj = this.nativeGetValueN(key); + T t = (T) obj; + return t; + } + + /** + * get attribute value as an integer + * + * @param key + * - Key of the element. + * + * @return int - integer value of key provided. + */ + public int getValueInt(String key) { + Integer value = 0; + try { + value = this.getValue(key); + } catch (RCSException e) { + e.printStackTrace(); + } + return value; + } + + /** + * get attribute value as a double + * + * @param key + * - Key of the element. + * + * @return boolean - boolean value of key provided. + */ + public double getValueDouble(String key) { + double value = 0; + try { + value = this.getValue(key); + } catch (RCSException e) { + e.printStackTrace(); + } + return value; + } + + /** + * get attribute value as a boolean + * + * @param key + * - Key of the element. + * + * @return boolean - boolean value of key provided. + */ + public boolean getValueBool(String key) { + Boolean value = false; + try { + value = this.getValue(key); + } catch (RCSException e) { + e.printStackTrace(); + } + return value; + } + + /** + * get attribute value as a string + * + * @param key + * - Key of the element. + * + * @return String - String value of key provided. + */ + public String getValueString(String key) { + String value = ""; + try { + value = this.getValue(key); + } catch (RCSException e) { + e.printStackTrace(); + } + return value; + } + + /** + * set an integer type attribute value. + * + * @param key + * - Key of the element to be added. + * + * @param value + * - integer value of key to be set. + */ + public void setValueInt(String key, int value) throws RCSException { + try { + this.nativeSetValueInteger(key, value); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * set a double attribute value. + * + * @param key + * - Key of the element to be added. + * + * @param value + * - double value of key to be set. + */ + public void setValueDouble(String key, double value) throws RCSException { + try { + this.nativeSetValueDouble(key, value); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * set a boolean attribute value. + * + * @param key + * - Key of the element to be added. + * + * @param value + * - integer value of key to be set. + */ + public void setValueBool(String key, boolean value) throws RCSException { + try { + this.nativeSetValueBoolean(key, value); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * set a string attribute value. + * + * @param key + * - Key of the element to be added. + * + * @param value + * - integer value of key to be set. + */ + public void setValueString(String key, String value) throws RCSException { + try { + this.nativeSetValueString(key, value); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * Returns whether attribute is empty. + * + * @return boolean + */ + public boolean isEmpty() { + return this.nativeIsEmpty(); + } + + /** + * Returns the number of elements. + * + * @return boolean + */ + public int size() { + return this.nativeSize(); + } + + /** + * Removes a single attribute + * + * @param key + * Key to be removed. + * + * @return true if an attribute is removed, false otherwise. + */ + public boolean remove(String key) { + return this.nativeRemove(key); + } + + /** + * Checks the container has an attribute with a Key equivalent to the + * provided key. + * + * @param key + * Key to check. + * + * @return true if an attribute with requests key exists, false otherwise. + */ + public boolean hasAttribute(String key) { + return this.nativeHasAttribute(key); + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + dispose(this.m_nativeNeedsDelete); + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/Builder.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/Builder.java new file mode 100644 index 0000000..39ec7d1 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/Builder.java @@ -0,0 +1,140 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file Builder.java + * + * This file contains Builder class which provides the easy creation of Resource. + * + */ + +package org.iotivity.ResourceEncapsulation.server; + +import org.iotivity.ResourceEncapsulation.common.RCSException; +import org.iotivity.ResourceEncapsulation.common.RCSResourceAttributes; + +/** + * This class provides APIs for resource creation, setting properties and + * attributes for the constructed resource. It provides the build() API which + * builds a resource and returns RCSResourceObject. + * + * {@link RCSResourceObject} + */ +public class Builder { + static { + System.loadLibrary("gnustl_shared"); + System.loadLibrary("oc_logger"); + System.loadLibrary("connectivity_abstraction"); + System.loadLibrary("ca-interface"); + System.loadLibrary("octbstack"); + System.loadLibrary("oc"); + System.loadLibrary("ocstack-jni"); + System.loadLibrary("rcs_common"); + System.loadLibrary("rcs_client"); + System.loadLibrary("rcs_server"); + System.loadLibrary("re-jni"); + } + + private String m_resourceUri; + private String m_resourceType; + private String m_resourceInterface; + private boolean m_observableFlag; + private boolean m_discovervableFlag; + private RCSResourceAttributes m_attributes; + + /** + * Native method for creation of Resource + */ + private native RCSResourceObject nativeBuild(String resourceURI, + String resourceType, String resourceInterface, + boolean observableFlag, boolean discovervableFlag, + RCSResourceAttributes attributes); + + /** + * Constructor + * + * @param resourceURI + * Resource URI value to be set + * @param resourceType + * Resource type value to be set + * @param resourceInterface + * Interface value to be set + */ + public Builder(String resourceURI, String resourceType, + String resourceInterface) { + m_resourceUri = resourceURI; + m_resourceType = resourceType; + m_resourceInterface = resourceInterface; + } + + /** + * Sets the discoverable(OC_DISCOVERABLE) property for the resource. + * + * @param isDiscoverable + * Whether to be discovered or not + */ + public void setDiscoverable(boolean isDiscoverable) { + m_discovervableFlag = isDiscoverable; + } + + /** + * Sets the observable(OC_OBSERVABLE) property of the resource. + * + * @param isObservable + * Whether to be observed or not + */ + public void setObservable(boolean isObservable) { + m_observableFlag = isObservable; + } + + /** + * API for setting attributes of the resource. + * + * @param attributes + * Resource Attributes to set + * + * {@link RCSResourceAttributes} + */ + public void setAttributes(RCSResourceAttributes attributes) { + m_attributes = attributes; + } + + /** + * API for constructing a new RCSResourceObject. + * + * @return RCSResourceObject object (It is the resource that is created) + * + * @throws RCSException + * It catches exception from registerResource API of OCPlatform + * and throws it to developer. + * + *

+ * Steps for creation of Resource : - Get the object of this + * class by calling Constructor with resourceURI, resourceType, + * resourceInterface - Using the object set the Discoverable and + * Observable as needed. [optional] - Set the attributes for the + * resource. [optional] - At last call the build() method to + * create the resource. + */ + public RCSResourceObject build() throws RCSException { + return this.nativeBuild(m_resourceUri, m_resourceType, + m_resourceInterface, m_observableFlag, m_discovervableFlag, + m_attributes); + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSGetResponse.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSGetResponse.java new file mode 100644 index 0000000..db44896 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSGetResponse.java @@ -0,0 +1,158 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSGetResponse.java + * + * This file contains the class for creating a Get response + * for the received Get request. + * + */ + +package org.iotivity.ResourceEncapsulation.server; + +import org.iotivity.ResourceEncapsulation.common.RCSResourceAttributes; +import org.iotivity.ResourceEncapsulation.utils.EntityHandlerResult; + +/** + * This class provides methods to create the response for a received get + * request. + * + * {@link RCSResourceObject} + */ +public class RCSGetResponse { + + private int m_typeOfGetResponse; + private int m_handlerResult; + private int m_errorCode; + private RCSResourceAttributes m_attributes; + + private final static int DEFAULT_RESPONSE = 0; + private final static int CREATE_WITH_RESULT = 1; + private final static int CREATE_WITH_ATTRIBUTE = 2; + private final static int CREATE_WITH_ATTRIBUTE_AND_RESULT = 3; + + /** + * Creates a default RCSGetResponse. The response will have OC_EH_OK for the + * result and 200 for the errorCode. The attributes of RCSResourceObject + * will be set as the result attributes. + * + * @return RCSGetResponse + */ + public static RCSGetResponse defaultAction() { + return new RCSGetResponse(); + } + + /** + * Creates a RCSGetResponse with a result and error code passed. The + * attributes of the RCSResourceObject will be set as the result attributes. + * + * @param result + * The response result. + * @param errorCode + * The error code to set in response. + * + * @return RCSGetResponse + * + * {@link EntityHandlerResult} + */ + public static RCSGetResponse create(EntityHandlerResult result, + int errorCode) { + return new RCSGetResponse(result.ordinal(), errorCode); + + } + + /** + * Creates a RCSGetResponse with custom attributes. This sends the passed + * attributes as the result attributes instead of the one the + * RCSResourceObject holds. + * + * @param attributes + * The attributes to send in response. + * + * @return RCSGetResponse + * + * {@link RCSResourceAttributes} + */ + public static RCSGetResponse create(RCSResourceAttributes attributes) { + return new RCSGetResponse(attributes); + } + + /** + * Creates a RCSGetResponse with a result and error code passed. This sends + * the passed attributes as the result attributes instead of one the + * RCSResourceObject holds. + * + * @param attributes + * The attributes to send in response. + * @param result + * The response result. + * @param errorCode + * The error code for response. + * + * {@link RCSResourceAttributes} + * + * @return RCSGetResponse + */ + public static RCSGetResponse create(RCSResourceAttributes attributes, + EntityHandlerResult result, int errorCode) { + return new RCSGetResponse(attributes, result.ordinal(), errorCode); + } + + // ******* JNI UTILTY FUNCTIONS ******* // + + private RCSGetResponse() { + m_typeOfGetResponse = DEFAULT_RESPONSE; + } + + private RCSGetResponse(int handlerResult, int errorCode) { + m_typeOfGetResponse = CREATE_WITH_RESULT; + this.m_handlerResult = handlerResult; + this.m_errorCode = errorCode; + } + + private RCSGetResponse(RCSResourceAttributes attributes) { + m_typeOfGetResponse = CREATE_WITH_ATTRIBUTE; + this.m_attributes = attributes; + } + + private RCSGetResponse(RCSResourceAttributes attributes, int handlerResult, + int errorCode) { + m_typeOfGetResponse = CREATE_WITH_ATTRIBUTE_AND_RESULT; + this.m_attributes = attributes; + this.m_handlerResult = handlerResult; + this.m_errorCode = errorCode; + } + + private int getTypeOfGetResponse() { + return m_typeOfGetResponse; + } + + private int getResult() { + return m_handlerResult; + } + + private int getErrorCode() { + return m_errorCode; + } + + private RCSResourceAttributes getAttributes() { + return m_attributes; + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSRequest.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSRequest.java new file mode 100644 index 0000000..eacf56a --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSRequest.java @@ -0,0 +1,48 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSRequest.java + * + * This file provides RCSRequest class which will describes the request. + * + */ + +package org.iotivity.ResourceEncapsulation.server; + +/** + * This class describes the request. + */ +public class RCSRequest { + + private String m_resourceUri; + + private RCSRequest(String resourceUri) { + m_resourceUri = resourceUri; + } + + /** + * Returns the URI of the request. + * + * @return Uri of the request in string form + */ + public String getResourceUri() { + return m_resourceUri; + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSResourceObject.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSResourceObject.java new file mode 100644 index 0000000..2eb5361 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSResourceObject.java @@ -0,0 +1,543 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSResourceObject.java + * + * This file contains the resource object APIs provided to the developers. + * RCSResourceObject is a part of the server builder module. + * + */ + +package org.iotivity.ResourceEncapsulation.server; + +import org.iotivity.ResourceEncapsulation.common.RCSResourceAttributes; + +import android.util.Log; + +/** + * RCSResourceObject represents a resource. It handles any requests from clients + * automatically with attributes. + *

+ * It also provides an auto notification mechanism that notifies to the + * observers. Requests are handled automatically by defaultAction of + * RCSGetResponse and RCSSetResponse. Developer can override them and send your + * own response. + *

+ * For simple resources, developer may want to know whenever attributes are + * changed by a set request. In this case, add an AttributeUpdatedListener with + * a key interested in instead of overriding SetRequestHandler. + * + *

+ * Developer will get the object of this when they call the build() API of + * Builder class + * + * {@link Builder} + */ +public class RCSResourceObject { + + private long m_nativeHandle; + private IGetRequestHandler m_getRequestListener; + private ISetRequestHandler m_setRequestListener; + private IAttributeUpdatedListener m_attributeUpdatedListener; + private final String LOG_TAG = this.getClass().getSimpleName(); + + // native methods + private native void nativeSetAttributeInteger(String key, int value); + + private native void nativeSetAttributeDouble(String key, double value); + + private native void nativeSetAttributeBool(String key, boolean value); + + private native void nativeSetAttributeString(String key, String value); + + private native String nativeGetAttributeValue(String key); + + private native boolean nativeRemoveAttribute(String key); + + private native boolean nativeContainsAttribute(String key); + + private native RCSResourceAttributes nativeGetAttributes(); + + private native boolean nativeIsObservable(); + + private native boolean nativeIsDiscoverable(); + + private native void nativeNotify(); + + private native void nativeSetAutoNotifyPolicy(int policy); + + private native int nativeGetAutoNotifyPolicy(); + + private native void nativeSetSetRequestHandlerPolicy(int policy); + + private native int nativeGetSetRequestHandlerPolicy(); + + private native void nativeSetGetRequestHandler(IGetRequestHandler handler); + + private native void nativeSetSetRequestHandler(ISetRequestHandler handler); + + private native void nativeAddAttributeUpdatedListener(String key, + IAttributeUpdatedListener listener); + + private native boolean nativeRemoveAttributeUpdatedListener(String key); + + private RCSResourceObject(long nativeHandle) { + m_nativeHandle = nativeHandle; + } + + /** + * Represents the policy of AutoNotify function of RCSResourceObject class + * In accord with this policy, observers are notified of attributes that are + * changed or updated. + * + *

+ * Attributes are changed or updated according to execution of some + * functions or receipt of 'set-request'. + * + * {@link RCSResourceObject} + */ + public enum AutoNotifyPolicy { + NEVER, /** < Never notify. */ + ALWAYS, /** < Always notify. */ + UPDATED; + /** < When attributes are changed, notify. */ + + /** + * Utility API for integer to enum conversion. + */ + public static AutoNotifyPolicy getAutoNotifyPolicy(int ordinal) { + if (ordinal == 0) + return AutoNotifyPolicy.values()[0]; + else if (ordinal == 1) + return AutoNotifyPolicy.values()[1]; + else if (ordinal == 2) + return AutoNotifyPolicy.values()[2]; + return null; + } + } + + public enum SetRequestHandlerPolicy { + + NEVER, /** + * < Server ignore when server is received set-request of + * attributes of the new key. + */ + ACCEPTANCE; + /** + * < Server creates attributes of the new key When server is received + * set-request of attributes of the new key. + */ + + /** + * Utility API for integer to enum conversion. + */ + public static SetRequestHandlerPolicy getSetRequestHandlerPolicy( + int ordinal) { + + if (ordinal == 0) + return SetRequestHandlerPolicy.values()[0]; + else if (ordinal == 1) + return SetRequestHandlerPolicy.values()[1]; + return null; + } + } + + /** + * Provides interface for receiving the callback on receiving a Get request + * from the client + */ + public interface IGetRequestHandler { + + /** + * This callback method is called when received a get request from the + * client. Listener should be set to receive this callback. + * + * @param request + * Request for the resource + * @param attributes + * Resource Attributes + * + * @return RCSGetResponse Response to be sent. + * + * {@link RCSResourceObject} {@link RCSGetResponse} + * {@link RCSResourceAttributes} + */ + RCSGetResponse onGetRequest(RCSRequest request, + RCSResourceAttributes attributes); + + } + + /** + * Provides interface for receiving the callback on receiving a Set request + * from the client + */ + public interface ISetRequestHandler { + + /** + * This callback method is called when received a set request from the + * client. Listener should be set to receive this callback. + * + * @param request + * Request for the resource + * @param attributes + * Resource Attributes to set + * + * @return RCSSetResponse Response to be sent. + * + * {@link RCSResourceObject} {{@link RCSSetResponse} + * {@link RCSResourceAttributes} + */ + RCSSetResponse onSetRequest(RCSRequest request, + RCSResourceAttributes attributes); + + } + + /** + * Provides interface for receiving the callback on update of requested + * attribute. + */ + public interface IAttributeUpdatedListener { + + /** + * This callback method is called when a requested attribute value is + * updated. Listener should be set to receive this callback. + * + * @param oldValue + * It is the attribute value before updation. + * @param newValue + * It is the current resource attribute value. + * + * {@link RCSResourceAttributes} + */ + void onAttributeUpdate(String oldValue, String newValue); + } + + /** + * Set listener for receiving get request from the client asynchronously. + * + * @param listener + * IGetRequestHandler to receive asynchronous get request from + * client. + * + * {@link IGetRequestHandler} + */ + public void setGetRequestListener(IGetRequestHandler listener) { + m_getRequestListener = listener; + } + + /** + * Set listener for receiving set request from the client asynchronously. + * + * @param listener + * ISetRequestHandler to receive asynchronous set request from + * client. + * + * {@link ISetRequestHandler} + */ + public void setSetRequestListener(ISetRequestHandler listener) { + m_setRequestListener = listener; + } + + /** + * Set listener for receiving the updated attribute value asynchronously. + * + * @param listener + * IAttributeUpdatedListener to receive updated attribute value + * asynchronously. + * + * {@link IAttributeUpdatedListener} + */ + public void setAttributeUpdatedListener(IAttributeUpdatedListener listener) { + m_attributeUpdatedListener = listener; + } + + /** + * API for setting a particular attribute value as a integer + * + * @param key + * name of attribute(used to map the attribute value). + * @param value + * integer value to be mapped against the key. + * + * The thread-safety for attributes is taken care internally. + */ + public void setAttribute(String key, int value) { + Log.i(LOG_TAG, "setAttribute (integer) called"); + this.nativeSetAttributeInteger(key, value); + } + + /** + * API for setting a particular attribute value as a double + * + * @param key + * name of attribute(used to map the attribute value). + * @param value + * Double value to be mapped against the key. + * + * The thread-safety for attributes is taken care internally. + */ + public void setAttribute(String key, double value) { + Log.i(LOG_TAG, "setAttribute (float) called"); + this.nativeSetAttributeDouble(key, value); + } + + /** + * API for setting a particular attribute value as a boolean + * + * @param key + * name of attribute(used to map the attribute value). + * @param value + * boolean value to be mapped against the key. + * + * The thread-safety for attributes is taken care internally. + */ + public void setAttribute(String key, boolean value) { + Log.i(LOG_TAG, "setAttribute (double) called "); + this.nativeSetAttributeBool(key, value); + } + + /** + * API for setting a particular attribute value as a String + * + * @param key + * name of attribute(used to map the attribute value). + * @param value + * String value to be mapped against the key. + * + * The thread-safety for attributes is taken care internally. + */ + public void setAttribute(String key, String value) { + Log.i(LOG_TAG, "setAttribute (String) called "); + this.nativeSetAttributeString(key, value); + } + + /** + * API for retrieving the attribute value associated with the supplied key. + * + * @param key + * Name of the attribute + * + * @return resource attributes value as a string + * + * The thread-safety for attributes is taken care internally. + */ + public String getAttributeValue(String key) { + Log.i(LOG_TAG, "getAttributeValue called"); + return this.nativeGetAttributeValue(key); + } + + /** + * API for removing a particular attribute of the resource. + * + * @param key + * Name of the attribute. + * + * @return If the key exist and matched attribute is deleted, return true. + * + * The thread-safety for attributes is taken care internally. + */ + public boolean removeAttribute(String key) { + Log.i(LOG_TAG, "removeAttribute called"); + return this.nativeRemoveAttribute(key); + } + + /** + * API for checking whether a particular attribute is there for a resource + * or not. + * + * @param key + * Name of the attribute. + * + * @return If the key exist, return true. + * + * The thread-safety for attributes is taken care internally. + */ + public boolean containsAttribute(String key) { + Log.i(LOG_TAG, "containsAttribute called"); + return this.nativeContainsAttribute(key); + } + + /** + * API for getting all the attributes of the RCSResourceObject. + * + * @return RCSResourceAttributes + * + * {@link RCSResourceAttributes} + */ + public RCSResourceAttributes getAttributes() { + Log.i(LOG_TAG, "getAttributes called"); + return this.nativeGetAttributes(); + } + + /** + * API for checking whether the particular resource is observable or not + * + * @return true if observable otherwise false + */ + public boolean isObservable() { + Log.i(LOG_TAG, "isObservable called"); + return this.nativeIsObservable(); + } + + /** + * API for checking whether the particular resource is discoverable or not + * + * @return true if discoverable else false + */ + public boolean isDiscoverable() { + Log.i(LOG_TAG, "isDiscoverable called"); + return this.nativeIsDiscoverable(); + } + + /** + * API for setting the resource's get request handler by the + * developer/application. If developer set this handler then all get request + * will come to the application and developer can send the response to the + * client using APIs of RCSGetResponse class. + * + *

+ * IGetRequestHandler should be set to call this API. For setting the + * listener call setGetRequestListener() API + * + * {@link RCSGetResponse} {@link #setGetRequestListener(IGetRequestHandler)} + */ + public void setGetRequestHandler() { + if (null == m_getRequestListener) { + Log.i(LOG_TAG, "getRequestListener is not set"); + return; + } else { + this.nativeSetGetRequestHandler(m_getRequestListener); + } + } + + /** + * API for setting the resource's set request handler by the + * developer/application. If developer set this handler then all set request + * will come to the application and developer can send the response to the + * client using APIs of RCSSetResponse class. + * + *

+ * ISetRequestHandler should be set to call this API. For setting the + * listener call setSetRequestListener() API + * + * {@link RCSSetResponse} {@link #setSetRequestListener(ISetRequestHandler)} + */ + public void setSetRequestHandler() { + if (null == m_setRequestListener) { + Log.i(LOG_TAG, "setRequestListener is not set"); + return; + } else { + this.nativeSetSetRequestHandler(m_setRequestListener); + } + } + + /** + * API for setting the Listener for a particular attribute update. + * + * @param key + * The interested attribute's key + * + *

+ * IAttributeUpdatedListener should be set to call this API. For + * setting the listener call setAttributeUpdatedListener() API + */ + public void addAttributeUpdatedListener(String key) { + if (null == m_attributeUpdatedListener) { + Log.i(LOG_TAG, "addAttributeUpdatedListener is not set"); + return; + } else { + this.nativeAddAttributeUpdatedListener(key, + m_attributeUpdatedListener); + } + } + + /** + * API for removing the handler for a particular attribute update. + * + * @param key + * The interested attribute's key + * + * @return true if requested attribute is removed successfully else false + */ + public boolean removeAttributeUpdatedListener(String key) { + return this.nativeRemoveAttributeUpdatedListener(key); + } + + /** + * API for notifying all observers of the RCSResourceObject with the updated + * attributes value + */ + public void notifyObservers() { + Log.i(LOG_TAG, "notifyObservers called"); + this.nativeNotify(); + } + + /** + * API for setting Auto notify policy + * + * @param policy + * policy to be set + * + * {@link AutoNotifyPolicy} + */ + public void setAutoNotifyPolicy(AutoNotifyPolicy policy) { + Log.i(LOG_TAG, "setAutoNotifyPolicy called"); + this.nativeSetAutoNotifyPolicy(policy.ordinal()); + } + + /** + * API for getting auto notify policy + * + * @return AutoNotifyPolicy + * + * {@link AutoNotifyPolicy} + */ + public AutoNotifyPolicy getAutoNotifyPolicy() { + Log.i(LOG_TAG, "getAutoNotifyPolicy called"); + return AutoNotifyPolicy.getAutoNotifyPolicy(this + .nativeGetAutoNotifyPolicy()); + } + + /** + * API for setting the policy for a setRequestHandler. + * + * @param policy + * policy to be set + * + * {@link SetRequestHandlerPolicy} + */ + public void setSetRequestHandlerPolicy(SetRequestHandlerPolicy policy) { + Log.i(LOG_TAG, "setSetRequestHandlerPolicy called"); + this.nativeSetSetRequestHandlerPolicy(policy.ordinal()); + } + + /** + * API for getting the SetRequestHandler Policy. + * + * @return SetRequestHandlerPolicy + * + * {@link SetRequestHandlerPolicy} + */ + public SetRequestHandlerPolicy getSetRequestHandlerPolicy() { + Log.i(LOG_TAG, "getSetRequestHandlerPolicy called"); + return SetRequestHandlerPolicy.getSetRequestHandlerPolicy(this + .nativeGetSetRequestHandlerPolicy()); + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSSetResponse.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSSetResponse.java new file mode 100644 index 0000000..2ad35e5 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/server/RCSSetResponse.java @@ -0,0 +1,247 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file RCSSetResponse.java + * + * This file contains the class for creating a Set response + * for the received Set request. + * + */ + +package org.iotivity.ResourceEncapsulation.server; + +import org.iotivity.ResourceEncapsulation.common.RCSResourceAttributes; +import org.iotivity.ResourceEncapsulation.utils.EntityHandlerResult; + +/** + * This class provides methods to create the response for a received set + * request. + * + * {@link RCSResourceObject} + */ +public class RCSSetResponse { + + private int m_typeOfSetResponse; + private int m_handlerResult; + private int m_errorCode; + private RCSResourceAttributes m_attributes; + + private final static int DEFAULT_RESPONSE = 0; + private final static int CREATE_WITH_RESULT = 1; + private final static int CREATE_WITH_ATTRIBUTE = 2; + private final static int CREATE_WITH_ATTRIBUTE_AND_RESULT = 3; + private final static int ACCEPT = 4; + private final static int ACCEPT_WITH_RESULT = 5; + private final static int IGNORE = 6; + private final static int IGNORE_WIH_RESULT = 7; + + /** + * Creates a default RCSSetResponse with DEFAULT acceptance method. The + * response will have OC_EH_OK for the result and 200 for the errorCode. The + * attributes of RCSResourceObject will be set as the result attributes. + * + *

+ * DEFAULT : Follow SetRequestHandlerPolicy of the RCSResourceObject. + * + * @return RCSSetResponse + * + * {@link SetRequestHandlerPolicy} + */ + public static RCSSetResponse defaultAction() { + return new RCSSetResponse(DEFAULT_RESPONSE); + } + + /** + * Creates a default RCSSetResponse with ACCEPT acceptance method. The + * response will have OC_EH_OK for the result and 200 for the errorCode. The + * attributes of RCSResourceObject will be set as the result attributes. + * + *

+ * ACCEPT : Accept the request attributes even if there is an unknown key or + * mismatched type. + * + * @return RCSSetResponse + */ + public static RCSSetResponse accept() { + return new RCSSetResponse(ACCEPT); + } + + /** + * Creates a RCSSetResponse with ACCEPT acceptance method. with a result and + * error code passed. The attributes of the RCSResourceObject will be set as + * the result attributes. + * + * @param result + * The response result. + * @param errorCode + * The error code to set in response. + * + *

+ * ACCEPT : Accept the request attributes even if there is an + * unknown key or mismatched type. + * + * @return RCSSetResponse + */ + public static RCSSetResponse accept(EntityHandlerResult result, + int errorCode) { + return new RCSSetResponse(result.ordinal(), errorCode, + ACCEPT_WITH_RESULT); + } + + /** + * Creates a default RCSSetResponse with IGNORE acceptance method. The + * response will have OC_EH_OK for the result and 200 for the errorCode. The + * attributes of RCSResourceObject will be set as the result attributes. + * + *

+ * IGNORE : Ignore the request attributes. + * + * @return RCSSetResponse + */ + public static RCSSetResponse ignore() { + return new RCSSetResponse(IGNORE); + } + + /** + * Creates a RCSSetResponse with IGNORE acceptance method. with a result and + * error code passed. The attributes of the RCSResourceObject will be set as + * the result attributes. + * + * @param result + * The response result. + * @param errorCode + * The error code to set in response. + * + *

+ * IGNORE : Ignore the request attributes. + * + * @return RCSSetResponse + */ + public static RCSSetResponse ignore(EntityHandlerResult result, + int errorCode) { + return new RCSSetResponse(result.ordinal(), errorCode, + IGNORE_WIH_RESULT); + } + + /** + * Creates a RCSSetResponse with a result and error code passed and has + * DEFAULT acceptance method. The attributes of the RCSResourceObject will + * be set as the result attributes. + * + *

+ * DEFAULT : Follow SetRequestHandlerPolicy of the RCSResourceObject. + * + * @param result + * The response result. + * @param errorCode + * The error code to set in response. + * + * {@link EntityHandlerResult} + * + * @return RCSSetResponse + */ + public static RCSSetResponse create(EntityHandlerResult result, + int errorCode) { + return new RCSSetResponse(result.ordinal(), errorCode, + CREATE_WITH_RESULT); + } + + /** + * Creates a RCSSetResponse with custom attributes and has DEFAULT + * acceptance method. This sends the passed attributes as the result + * attributes instead of one the RCSResourceObject holds. + * + *

+ * DEFAULT : Follow SetRequestHandlerPolicy of the RCSResourceObject. + * + * @param attributes + * The attributes to send in response. + * + * {@link RCSResourceAttributes} + * + * @return RCSSetResponse + */ + public static RCSSetResponse create(RCSResourceAttributes attributes) { + return new RCSSetResponse(attributes); + } + + /** + * Creates a RCSSetResponse with a result and error code passed. This sends + * the passed attributes as the result attributes instead of one the + * RCSResourceObject holds. + * + * @param attributes + * The attributes to send in response. + * @param result + * The response result. + * @param errorCode + * The error code for response. + * + *

+ * RCSResourceAttributes + * + * @return RCSSetResponse + */ + public static RCSSetResponse create(RCSResourceAttributes attributes, + EntityHandlerResult result, int errorCode) { + return new RCSSetResponse(attributes, result.ordinal(), errorCode); + } + + // ******* JNI UTILTY FUNCTIONS ******* // + + private RCSSetResponse(int typeOfSetResponse) { + this.m_typeOfSetResponse = typeOfSetResponse; + } + + private RCSSetResponse(int handlerResult, int errorCode, int typeOfRequest) { + m_typeOfSetResponse = typeOfRequest; + m_handlerResult = handlerResult; + m_errorCode = errorCode; + } + + private RCSSetResponse(RCSResourceAttributes attributes) { + m_typeOfSetResponse = CREATE_WITH_ATTRIBUTE; + this.m_attributes = attributes; + } + + private RCSSetResponse(RCSResourceAttributes attributes, int handlerResult, + int errorCode) { + m_typeOfSetResponse = CREATE_WITH_ATTRIBUTE_AND_RESULT; + m_attributes = attributes; + m_handlerResult = handlerResult; + m_errorCode = errorCode; + } + + private int getTypeOfGetResponse() { + return m_typeOfSetResponse; + } + + private int getResult() { + return m_handlerResult; + } + + private int getErrorCode() { + return m_errorCode; + } + + private RCSResourceAttributes getAttributes() { + return m_attributes; + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/DiscoveryType.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/DiscoveryType.java new file mode 100644 index 0000000..0f6dd57 --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/DiscoveryType.java @@ -0,0 +1,57 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file DiscoveryType.java + * + * @brief This file provides a Enum to handle unicast or multicast discovery. + * + *@link RCSAddress + */ + +package org.iotivity.ResourceEncapsulation.utils; + +/** + * This Enum defines discovery codes for different discovery types. + * + *

+ * Application will get this DiscoveryType as a result of getTypeOfDiscovery API + * of RCSAddress class + * + * {@link RCSAddress} + */ +public enum DiscoveryType { + + UNICAST, /** < Unicast type discovery. */ + MULTICAST; + /** < Multicast type discovery. */ + + /** + * Utility API for int to enum conversion. + */ + public static DiscoveryType getDiscoveryType(int ordinal) { + + if (ordinal == 0) + return DiscoveryType.values()[0]; + if (ordinal == 1) + return DiscoveryType.values()[1]; + + return null; + } +} diff --git a/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/EntityHandlerResult.java b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/EntityHandlerResult.java new file mode 100644 index 0000000..42f48ea --- /dev/null +++ b/service/resource-encapsulation/sdk/java/src/org/iotivity/ResourceEncapsulation/utils/EntityHandlerResult.java @@ -0,0 +1,38 @@ +/****************************************************************** + * + * Copyright 2015 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ +/** + * @file EntityHandlerResult.java + * + * @brief This file provides EntityHandlerResult Enum & utility function. + * + */ + +package org.iotivity.ResourceEncapsulation.utils; + +/** + * Represents the EntityHandlerResult, that application can send for a Get or a + * Set request. + * + * {@link RCSResourceObject} {@link RCSGetResponse} {@link RCSSetResponse} + */ +public enum EntityHandlerResult { + + OC_EH_OK, OC_EH_ERROR, OC_EH_RESOURCE_CREATED, OC_EH_RESOURCE_DELETED, OC_EH_SLOW, OC_EH_FORBIDDEN, OC_EH_RESOURCE_NOT_FOUND; +} -- 2.7.4