From: Youngjae Shin Date: Tue, 2 Aug 2016 04:14:01 +0000 (+0900) Subject: Imported Upstream version 1.1.1 X-Git-Tag: upstream/1.1.1^0 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1cee1631595cac6a6394ac2e0b365c6dd5a42c68;hp=390866079e285d2c74918432c0d597d5da52f8a0;p=platform%2Fupstream%2Fiotivity.git Imported Upstream version 1.1.1 --- diff --git a/android/android_api/base/build.gradle b/android/android_api/base/build.gradle index 7deb01c..a0b52ae 100755 --- a/android/android_api/base/build.gradle +++ b/android/android_api/base/build.gradle @@ -41,7 +41,7 @@ android { minSdkVersion 21 targetSdkVersion 21 versionCode 1 - versionName "1.1" + versionName "1.1.1" buildConfigField 'int', 'SECURED', SECURED } buildTypes { diff --git a/android/android_api/base/jni/Android.mk b/android/android_api/base/jni/Android.mk index cc43275..51d9dcd 100644 --- a/android/android_api/base/jni/Android.mk +++ b/android/android_api/base/jni/Android.mk @@ -58,7 +58,7 @@ LOCAL_SRC_FILES := JniOcStack.cpp \ JniEntityHandler.cpp \ JniOnResourceFoundListener.cpp \ JniOnDeviceInfoListener.cpp \ - JniOnPlatformInfoListener.cpp \ + JniOnPlatformInfoListener.cpp \ JniOnPresenceListener.cpp \ JniOnGetListener.cpp \ JniOnPutListener.cpp \ @@ -74,15 +74,18 @@ LOCAL_SRC_FILES := JniOcStack.cpp \ JniOcPlatform.cpp \ JniOcResource.cpp \ JniOcResourceIdentifier.cpp \ - JniOcSecurity.cpp + JniOcSecurity.cpp \ + JniOnDPDevicesFoundListener.cpp \ + JniOnDirectPairingListener.cpp \ + JniOcDirectPairDevice.cpp ifeq ($(SECURED), 1) -LOCAL_SRC_FILES += JniOcSecureResource.cpp \ - JniOcProvisioning.cpp \ - JniSecureUtils.cpp \ - JniProvisionResultListner.cpp \ - JniPinCheckListener.cpp \ - JniDisplayPinListener.cpp -endif + LOCAL_SRC_FILES += JniOcSecureResource.cpp \ + JniOcProvisioning.cpp \ + JniSecureUtils.cpp \ + JniProvisionResultListner.cpp \ + JniPinCheckListener.cpp \ + JniDisplayPinListener.cpp + endif LOCAL_LDLIBS := -llog LOCAL_STATIC_LIBRARIES := android-oc @@ -112,6 +115,7 @@ LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/security/provisioning/include LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/security/provisioning/include/oxm/ LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/security/provisioning/include/internal LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/security/include +LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/security/include/internal LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/connectivity/api LOCAL_C_INCLUDES += $(OIC_SRC_PATH)/csdk/connectivity/lib/libcoap-4.1.1 include $(BUILD_SHARED_LIBRARY) diff --git a/android/android_api/base/jni/JniOcDirectPairDevice.cpp b/android/android_api/base/jni/JniOcDirectPairDevice.cpp new file mode 100644 index 0000000..535bebc --- /dev/null +++ b/android/android_api/base/jni/JniOcDirectPairDevice.cpp @@ -0,0 +1,132 @@ +/* +* //****************************************************************** +* // +* // Copyright 2016 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 "JniOcDirectPairDevice.h" +#include "OCDirectPairing.h" +#include "OCDirectPairing.h" +using namespace OC; +namespace PH = std::placeholders; + +JniOcDirectPairDevice::JniOcDirectPairDevice(std::shared_ptr directPairingDevice) + : m_sharedDirectPairDevice(directPairingDevice) +{} + +JniOcDirectPairDevice::~JniOcDirectPairDevice() +{ + LOGD("~JniOcDirectPairDevice()"); + m_sharedDirectPairDevice = nullptr; +} + +std::string JniOcDirectPairDevice::getHost() +{ + return m_sharedDirectPairDevice->getHost(); +} + +std::vector JniOcDirectPairDevice::getPairingMethods() +{ + + return m_sharedDirectPairDevice->getPairingMethods(); +} + +OCConnectivityType JniOcDirectPairDevice::connectivityType() +{ + return m_sharedDirectPairDevice->getConnType(); +} + +JniOcDirectPairDevice* JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(JNIEnv *env, jobject thiz) +{ + JniOcDirectPairDevice *dpDev = GetHandle(env, thiz); + if (env->ExceptionCheck()) + { + LOGE("Failed to get native handle from OcDirectPairingDevice"); + } + if (!dpDev) + { + ThrowOcException(JNI_NO_NATIVE_POINTER, ""); + } + return dpDev; +} +std::shared_ptr JniOcDirectPairDevice::getPtr() +{ + return m_sharedDirectPairDevice; +} + +JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcDirectPairDevice_getHost +(JNIEnv *env, jobject thiz) +{ + + LOGD("OcDirectPairing_getHost"); + + JniOcDirectPairDevice* device = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env,thiz); + if (!device) return nullptr; + + std::string dev = device->getHost(); + return env->NewStringUTF(dev.c_str()); +} + +JNIEXPORT jintArray JNICALL Java_org_iotivity_base_OcDirectPairDevice_getPairingMethods + (JNIEnv *env, jobject thiz) +{ + + LOGD("OcDirectPairing_getPairingMethods"); + + std::vector pairingMethodList; + JniOcDirectPairDevice* device = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env,thiz); + if (!device) return nullptr; + + std::vector pairingMethods = device->getPairingMethods(); + return JniOcDirectPairDevice::JconvertIntVectorToJavaList(env,pairingMethods); +} + + +jintArray JniOcDirectPairDevice::JconvertIntVectorToJavaList(JNIEnv *env, std::vector &vector) +{ + + jsize len = static_cast(vector.size()); + + jintArray intArray = env->NewIntArray(len); + if (!intArray) return nullptr; + + env->SetIntArrayRegion(intArray, (jsize)0, len, (const jint*)&vector[0]); + + if (env->ExceptionCheck()) + { + LOGE("ArrayIndexOutOfBoundsException in JconvertIntVectorToJavaList"); + } + return intArray; +} + +/* +* Class: org_iotivity_base_OcDirectPairDevice +* Method: getConnectivityTypeN +* Signature: ()I +*/ +JNIEXPORT jint JNICALL Java_org_iotivity_base_OcDirectPairDevice_getConnectivityTypeN +(JNIEnv *env, jobject thiz) +{ + LOGD("OcDirectPairDevice_getConnectivityType"); + JniOcDirectPairDevice* device = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env,thiz); + if (!device) return -1; + + OCConnectivityType connectivityType = device->connectivityType(); + return static_cast(connectivityType); +} diff --git a/android/android_api/base/jni/JniOcDirectPairDevice.h b/android/android_api/base/jni/JniOcDirectPairDevice.h new file mode 100644 index 0000000..505bac2 --- /dev/null +++ b/android/android_api/base/jni/JniOcDirectPairDevice.h @@ -0,0 +1,101 @@ +/* +* //****************************************************************** +* // +* // Copyright 2016 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 "JniOcStack.h" +#include "JniOnDPDevicesFoundListener.h" +#include "OCDirectPairing.h" +#include + +#ifndef _Included_org_iotivity_base_OcDirectPairDevice +#define _Included_org_iotivity_base_OcDirectPairDevice + +using namespace OC; + +class JniOcDirectPairDevice +{ + public: + JniOcDirectPairDevice(std::shared_ptr directPairingDevice); + ~JniOcDirectPairDevice(); + + static JniOcDirectPairDevice* getJniOcDirectPairDevicePtr(JNIEnv *env, jobject thiz); + static jintArray JconvertIntVectorToJavaList(JNIEnv *env, std::vector &vector); + + std::shared_ptr getPtr(); + std::string getHost(); + std::vector getPairingMethods(); + OCConnectivityType connectivityType(); + + private: + std::shared_ptr m_sharedDirectPairDevice; +}; + +/* DO NOT EDIT THIS FILE BEYOND THIS LINE - it is machine generated */ + +#ifdef __cplusplus +extern "C" { +#endif + /* + * Class: org_iotivity_base_OcDirectPairDevice + * Method: getDirectPairedDevices + * Signature: (Lorg/iotivity/base/OcDirectPairDevice/GetDirectPairedListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcDirectPairDevice_getDirectPairedDevices + (JNIEnv *, jobject, jobject); + + /* + * Class: org_iotivity_base_OcDirectPairDevice + * Method: getHost + * Signature: ()Ljava/lang/String; + */ + JNIEXPORT jstring JNICALL Java_org_iotivity_base_OcDirectPairDevice_getHost + (JNIEnv *, jobject); + + /* + * Class: org_iotivity_base_OcDirectPairDevice + * Method: getPairingMethods + * Signature: ()Ljava/util/List; + */ + JNIEXPORT jintArray JNICALL Java_org_iotivity_base_OcDirectPairDevice_getPairingMethods + (JNIEnv *, jobject); + + /* + * Class: org_iotivity_base_OcDirectPairDevice + * Method: doDirectPairing + * Signature: (Lorg/iotivity/base/OcDirectPairDevice;Lorg/iotivity/base/OcPrmType; + * Ljava/lang/String;Lorg/iotivity/base/OcDirectPairDevice/DirectPairingListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcDirectPairDevice_doDirectPairing + (JNIEnv *, jobject, jobject, jint, jstring, jobject); + + /* + * Class: org_iotivity_base_OcDirectPairDevice + * Method: getConnectivityTypeN + * Signature: ()I + */ + JNIEXPORT jint JNICALL Java_org_iotivity_base_OcDirectPairDevice_getConnectivityTypeN + (JNIEnv *env, jobject thiz); + +#ifdef __cplusplus +} +#endif +#endif + diff --git a/android/android_api/base/jni/JniOcPlatform.cpp b/android/android_api/base/jni/JniOcPlatform.cpp index 93029c9..82f693f 100644 --- a/android/android_api/base/jni/JniOcPlatform.cpp +++ b/android/android_api/base/jni/JniOcPlatform.cpp @@ -25,7 +25,9 @@ #include "JniOcPresenceHandle.h" #include "JniOcResourceResponse.h" #include "JniOcSecurity.h" +#include "JniOcDirectPairDevice.h" #include "JniUtils.h" +#include "ocpayload.h" using namespace OC; @@ -320,6 +322,148 @@ void RemoveOnPresenceListener(JNIEnv* env, jobject jListener) presenceMapLock.unlock(); } +JniOnDPDevicesFoundListener* AddOnDPDevicesFoundListener(JNIEnv* env, jobject jListener) +{ + JniOnDPDevicesFoundListener *onDPDeviceListener = nullptr; + + dpDevicesFoundListenerMapLock.lock(); + + for (auto it = onDPDevicesFoundListenerMap.begin(); it != + onDPDevicesFoundListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onDPDeviceListener = refPair.first; + refPair.second++; + it->second = refPair; + onDPDevicesFoundListenerMap.insert(*it); + LOGD("onDPDeviceListener: ref. count incremented"); + break; + } + } + if (!onDPDeviceListener) + { + onDPDeviceListener = new JniOnDPDevicesFoundListener(env, jListener, + RemoveOnDPDevicesFoundListener); + jobject jgListener = env->NewGlobalRef(jListener); + onDPDevicesFoundListenerMap.insert( + std::pair>( + jgListener, + std::pair(onDPDeviceListener, 1))); + LOGI("onDPDeviceListener: new listener"); + } + dpDevicesFoundListenerMapLock.unlock(); + return onDPDeviceListener; +} + +void RemoveOnDPDevicesFoundListener(JNIEnv* env, jobject jListener) +{ + dpDevicesFoundListenerMapLock.lock(); + bool isFound = false; + for (auto it = onDPDevicesFoundListenerMap.begin(); it != + onDPDevicesFoundListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + onDPDevicesFoundListenerMap.insert(*it); + LOGI("onDPDeviceListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnDPDevicesFoundListener* listener = refPair.first; + delete listener; + onDPDevicesFoundListenerMap.erase(it); + LOGI("onDPDeviceListener is removed"); + } + isFound = true; + break; + } + } + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "onDPDeviceListener not found"); + } + dpDevicesFoundListenerMapLock.unlock(); +} + +JniOnDirectPairingListener* AddOnDirectPairingListener(JNIEnv* env, jobject jListener) +{ + JniOnDirectPairingListener *onDirectPairingListener = nullptr; + + directPairingListenerMapLock.lock(); + + for (auto it = directPairingListenerMap.begin(); it != + directPairingListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + onDirectPairingListener = refPair.first; + refPair.second++; + it->second = refPair; + directPairingListenerMap.insert(*it); + LOGD("onDirectPairingListener: ref. count incremented"); + break; + } + } + if (!onDirectPairingListener) + { + onDirectPairingListener = new JniOnDirectPairingListener(env, jListener, + RemoveOnDirectPairingListener); + jobject jgListener = env->NewGlobalRef(jListener); + directPairingListenerMap.insert( + std::pair>( + jgListener, + std::pair(onDirectPairingListener, 1))); + LOGI("onDirectPairingListener: new listener"); + } + directPairingListenerMapLock.unlock(); + return onDirectPairingListener; +} + +void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener) +{ + directPairingListenerMapLock.lock(); + bool isFound = false; + for (auto it = directPairingListenerMap.begin(); it != + directPairingListenerMap.end(); ++it) + { + if (env->IsSameObject(jListener, it->first)) + { + auto refPair = it->second; + if (refPair.second > 1) + { + refPair.second--; + it->second = refPair; + directPairingListenerMap.insert(*it); + LOGI("onDirectPairingListener: ref. count decremented"); + } + else + { + env->DeleteGlobalRef(it->first); + JniOnDirectPairingListener* listener = refPair.first; + delete listener; + directPairingListenerMap.erase(it); + LOGI("onDirectPairingListener is removed"); + } + isFound = true; + break; + } + } + if (!isFound) + { + ThrowOcException(JNI_EXCEPTION, "onDirectPairingListener not found"); + } + directPairingListenerMapLock.unlock(); +} + /* * Class: org_iotivity_base_OcPlatform * Method: configure @@ -703,6 +847,145 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findResource1( } /* + * Class: org_iotivity_base_OcPlatform + * Method: findDirectPairingDevices + * Signature: (ILorg/iotivity/base/OcPlatform/FindDirectPairingListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findDirectPairingDevices + (JNIEnv * env, jclass clazz, jint jTimeout, jobject jListener) +{ + LOGD("OcPlatform_findDirectPairingDevices"); + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "onDPDevicesFoundListener cannot be null"); + return; + } + JniOnDPDevicesFoundListener *onDPDevsFoundListener = AddOnDPDevicesFoundListener(env, + jListener); + + GetDirectPairedCallback getDirectPairedCallback = + [onDPDevsFoundListener](PairedDevices pairingDevList) + { + onDPDevsFoundListener->directPairingDevicesCallback(pairingDevList, + DPFunc::FIND_DIRECT_PAIRED_DEV_LIST); + }; + + try + { + OCStackResult result = OCPlatform::findDirectPairingDevices(jTimeout, + getDirectPairedCallback); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "OCPlatform::findDirectPairingDevices has failed"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* + * Class: org_iotivity_base_OcPlatform + * Method: getDirectPairedDevices + * Signature: (Lorg/iotivity/base/OcDirectPairDevice/GetDirectPairedListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDirectPairedDevices +(JNIEnv *env, jclass jclazz, jobject jListener) +{ + LOGD("OcPlatform_getDirectPairedDevices"); + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "getPairedDevList Callback cannot be null"); + return; + } + JniOnDPDevicesFoundListener *onGetPairedDevicesListener = AddOnDPDevicesFoundListener(env, + jListener); + + GetDirectPairedCallback getDirectPairedCallback = + [onGetPairedDevicesListener](PairedDevices pairedDevList) + { + onGetPairedDevicesListener->directPairingDevicesCallback(pairedDevList, + DPFunc::GET_PAIRED_DEV_LIST); + }; + + try + { + OCStackResult result = OCPlatform::getDirectPairedDevices(getDirectPairedCallback); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "OcDirectPairDevice_getDirectPairedDevices"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* + * Class: org_iotivity_base_OcPlatform + * Method: doDirectPairing + * Signature: (Lorg/iotivity/base/OcDirectPairDevice;Lorg/iotivity/base/OcPrmType; + * Ljava/lang/String;Lorg/iotivity/base/OcDirectPairDevice/DirectPairingListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_doDirectPairing0 +(JNIEnv *env, jclass clazz, jobject jpeer, jint jprmType, jstring jpin, jobject jListener) +{ + LOGD("OcPlatform_doDirectPairing"); + + if (!jListener) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "doDirectPairing Callback cannot be null"); + return; + } + if (!jpeer) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "Peer cannot be null"); + return; + } + + JniOnDirectPairingListener *onDirectPairingListener = AddOnDirectPairingListener(env, + jListener); + + DirectPairingCallback DirectPairingCB = + [onDirectPairingListener](std::shared_ptr dpDev, OCStackResult result) + { + onDirectPairingListener->doDirectPairingCB(dpDev, result); + }; + + JniOcDirectPairDevice *dev = JniOcDirectPairDevice::getJniOcDirectPairDevicePtr(env, jpeer); + + if (!dev) + { + return ; + } + std::string pin = env->GetStringUTFChars(jpin, 0); + + try + { + OCStackResult result = OCPlatform::doDirectPairing(dev->getPtr(), (OCPrm_t)jprmType, + pin, DirectPairingCB); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "OcPlatform_oDirectPairing"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* * Class: org_iotivity_base_OcPlatform * Method: getDeviceInfo0 * Signature: (Ljava/lang/String;Ljava/lang/String;ILorg/iotivity/base/OcPlatform/OnDeviceFoundListener;)V @@ -1070,20 +1353,44 @@ jobject jListener, jint jResourceProperty) JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0( JNIEnv *env, jclass clazz, - jstring jDeviceName) + jstring jDeviceName, + jobjectArray jDeviceTypes) { LOGI("OcPlatform_registerDeviceInfo"); - std::string deviceName; - if (jDeviceName) + if (!jDeviceName) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "deviceName cannot be null"); + return; + } + + if (!jDeviceTypes) { - deviceName = env->GetStringUTFChars(jDeviceName, nullptr); + ThrowOcException(OC_STACK_INVALID_PARAM, "deviceTypes cannot be null"); + return; } OCDeviceInfo deviceInfo; try { - DuplicateString(&deviceInfo.deviceName, deviceName); + DuplicateString(&deviceInfo.deviceName, env->GetStringUTFChars(jDeviceName, nullptr)); + deviceInfo.types = NULL; + + jsize len = env->GetArrayLength(jDeviceTypes); + for (jsize i = 0; i < len; ++i) + { + jstring jStr = (jstring)env->GetObjectArrayElement(jDeviceTypes, i); + if (!jStr) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "device type cannot be null"); + return; + } + + OCResourcePayloadAddStringLL(&deviceInfo.types, env->GetStringUTFChars(jStr, nullptr)); + if (env->ExceptionCheck()) return; + + env->DeleteLocalRef(jStr); + } } catch (std::exception &e) { diff --git a/android/android_api/base/jni/JniOcPlatform.h b/android/android_api/base/jni/JniOcPlatform.h index cfd6853..a45f059 100644 --- a/android/android_api/base/jni/JniOcPlatform.h +++ b/android/android_api/base/jni/JniOcPlatform.h @@ -23,6 +23,8 @@ #include "JniOnResourceFoundListener.h" #include "JniOnDeviceInfoListener.h" #include "JniOnPlatformInfoListener.h" +#include "JniOnDPDevicesFoundListener.h" +#include "JniOnDirectPairingListener.h" #include "JniOnPresenceListener.h" #include @@ -43,15 +45,26 @@ void RemoveOnPlatformInfoListener(JNIEnv* env, jobject jListener); JniOnPresenceListener* AddOnPresenceListener(JNIEnv* env, jobject jListener); void RemoveOnPresenceListener(JNIEnv* env, jobject jListener); +JniOnDPDevicesFoundListener* AddOnDPDevicesFoundListener(JNIEnv* env, jobject jListener); +void RemoveOnDPDevicesFoundListener(JNIEnv* env, jobject jListener); + +JniOnDirectPairingListener* AddOnDirectPairingListener(JNIEnv* env, jobject jListener); +void RemoveOnDirectPairingListener(JNIEnv* env, jobject jListener); + + std::map> onResourceFoundListenerMap; std::map> onDeviceInfoListenerMap; std::map> onPlatformInfoListenerMap; std::map> onPresenceListenerMap; +std::map> onDPDevicesFoundListenerMap; +std::map> directPairingListenerMap; std::mutex resourceFoundMapLock; std::mutex deviceInfoMapLock; std::mutex platformInfoMapLock; std::mutex presenceMapLock; +std::mutex dpDevicesFoundListenerMapLock; +std::mutex directPairingListenerMapLock; #ifdef __cplusplus extern "C" { @@ -163,10 +176,10 @@ extern "C" { /* * Class: org_iotivity_base_OcPlatform * Method: registerDeviceInfo0 - * Signature: (Ljava/lang/String;)V + * Signature: (Ljava/lang/String;[Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_registerDeviceInfo0 - (JNIEnv *, jclass, jstring); + (JNIEnv *, jclass, jstring, jobjectArray); /* * Class: org_iotivity_base_OcPlatform @@ -288,6 +301,30 @@ extern "C" { JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_sendResponse0 (JNIEnv *, jclass, jobject); + /* + * Class: org_iotivity_base_OcPlatform + * Method: findDirectPairingDevices + * Signature: (ILorg/iotivity/base/OcPlatform/FindDirectPairingListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_findDirectPairingDevices + (JNIEnv *, jclass, jint, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: getDirectPairedDevices + * Signature: (Lorg/iotivity/base/OcDirectPairDevice/GetDirectPairedListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_getDirectPairedDevices + (JNIEnv *, jclass, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: doDirectPairing + * Signature: (Lorg/iotivity/base/OcDirectPairDevice;Lorg/iotivity/base/OcPrmType; + * Ljava/lang/String;Lorg/iotivity/base/OcDirectPairDevice/DirectPairingListener;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_doDirectPairing0 + (JNIEnv *, jclass, jobject, jint, jstring, jobject); #ifdef __cplusplus } #endif diff --git a/android/android_api/base/jni/JniOcSecureResource.cpp b/android/android_api/base/jni/JniOcSecureResource.cpp index 77c8dc6..4b12e52 100644 --- a/android/android_api/base/jni/JniOcSecureResource.cpp +++ b/android/android_api/base/jni/JniOcSecureResource.cpp @@ -22,6 +22,9 @@ #include "JniOcSecureResource.h" #include "JniSecureUtils.h" +#include "aclresource.h" +#include "oic_malloc.h" +#include "oic_string.h" namespace PH = std::placeholders; JniOcSecureResource::JniOcSecureResource(std::shared_ptr device) @@ -210,32 +213,112 @@ OCStackResult JniOcSecureResource::provisionACL(JNIEnv* env, jobject _acl, jobje { OCStackResult ret; JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener); - OicSecAcl_t *acl = new OicSecAcl_t; + OicSecAcl_t *acl = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t)); if (!acl) { return OC_STACK_NO_MEMORY; } - acl->next = nullptr; - if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, _acl, acl)) { - delete acl; + JniSecureUtils::FreeACLList(acl); return OC_STACK_ERROR; } ResultCallBack resultCallback = [acl, resultListener](PMResultList_t *result, int hasError) { - delete acl; + JniSecureUtils::FreeACLList(acl); resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::PROVISIONACL); }; ret = m_sharedSecureResource->provisionACL(acl, resultCallback); if (ret != OC_STACK_OK) { - delete acl; + JniSecureUtils::FreeACLList(acl); + } + return ret; +} + +OCStackResult JniOcSecureResource::provisionDirectPairing(JNIEnv* env, jobjectArray jpdacls, + jobject jListener, std::string pin, std::vector prms, int edp) +{ + OCStackResult ret; + JniProvisionResultListner *resultListener = AddProvisionResultListener(env, jListener); + + jsize len = env->GetArrayLength(jpdacls); + + OicSecPconf_t *pconf = nullptr; + OicSecPdAcl_t *head = nullptr; + + for (jsize i = 0; i < len; ++i) + { + OicSecPdAcl_t *pdacl = new OicSecPdAcl_t; + if (!pdacl) + { + return OC_STACK_NO_MEMORY; + } + + memset(pdacl, 0, sizeof(OicSecPdAcl_t)); + pdacl->next = nullptr; + + jobject jpdacl = env->GetObjectArrayElement(jpdacls, i); + + if (OC_STACK_OK != JniSecureUtils::convertJavaPdACLToOCAcl(env, jpdacl, pdacl)) + { + delete pdacl; + return OC_STACK_ERROR; + } + + pdacl->next = head; + head = pdacl; + } + + pconf = new OicSecPconf_t; + memset(pconf, 0, sizeof(OicSecPconf_t)); + pconf->edp = edp; + pconf->prmLen = prms.size(); + pconf->prm = new OicSecPrm_t[pconf->prmLen]; + pconf->pddevLen = 0; + + for (int i = 0 ; i < prms.size(); i++) + pconf->prm[i] = (OicSecPrm_t)prms[i]; + + memcpy(pconf->pin.val, pin.c_str(), DP_PIN_LENGTH); + pconf->pdacls = head; + + ResultCallBack resultCallback = [head, resultListener, pconf, prms] + (PMResultList_t *result, int hasError) + { + OicSecPdAcl_t *tmp1, *tmp2; + tmp1 = head; + while (tmp1) + { + tmp2 = tmp1->next; + delete tmp1; + tmp1 = tmp2; + } + + delete pconf->prm; + delete pconf; + resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::PROVISIONDIRECTPAIRING); + }; + + ret = m_sharedSecureResource->provisionDirectPairing(pconf, resultCallback); + + if (ret != OC_STACK_OK) + { + OicSecPdAcl_t *tmp1, *tmp2; + tmp1 = head; + while (tmp1) + { + tmp2 = tmp1->next; + delete tmp1; + tmp1 = tmp2; + } + delete pconf->prm; + delete pconf; } return ret; } @@ -259,7 +342,7 @@ OCStackResult JniOcSecureResource::provisionPairwiseDevices(JNIEnv* env, jint ty if (_acl1) { - acl1 = new OicSecAcl_t; + acl1 = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t)); if (!acl1) { return OC_STACK_NO_MEMORY; @@ -267,45 +350,43 @@ OCStackResult JniOcSecureResource::provisionPairwiseDevices(JNIEnv* env, jint ty if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, _acl1, acl1)) { - delete acl1; + JniSecureUtils::FreeACLList(acl1); return OC_STACK_ERROR; } - acl1->next = nullptr; } if (_acl2) { - acl2 = new OicSecAcl_t; + acl2 = (OicSecAcl_t*)OICCalloc(1, sizeof(OicSecAcl_t)); if (!acl2) { - delete acl1; + JniSecureUtils::FreeACLList(acl1); return OC_STACK_NO_MEMORY; } if (OC_STACK_OK != JniSecureUtils::convertJavaACLToOCAcl(env, _acl2, acl2)) { - delete acl2; + JniSecureUtils::FreeACLList(acl1); + JniSecureUtils::FreeACLList(acl2); return OC_STACK_ERROR; } - acl2->next = nullptr; } ResultCallBack resultCallback = [acl1, acl2, resultListener](PMResultList_t *result, int hasError) { - delete acl1; - delete acl2; + JniSecureUtils::FreeACLList(acl1); + JniSecureUtils::FreeACLList(acl2); resultListener->ProvisionResultCallback(result, hasError, ListenerFunc::PROVISIONPAIRWISEDEVICES); }; - ret = m_sharedSecureResource->provisionPairwiseDevices(cred, acl1, *device2->getDevicePtr(), acl2, resultCallback); if (ret != OC_STACK_OK) { - delete acl1; - delete acl2; + JniSecureUtils::FreeACLList(acl1); + JniSecureUtils::FreeACLList(acl2); } return ret; } @@ -543,6 +624,55 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionPairwise /* * Class: org_iotivity_base_OcSecureResource + * Method: provisionDirectPairing + * Signature: (Ljava/lang/String;[Lorg/iotivity/base/OicSecPdAcl;ILorg/iotivity/base/OcSecureResource/ProvisionDirectPairingListener;I)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionDirectPairing +(JNIEnv *env, jobject thiz, jstring jpin, jobjectArray pdacls, jintArray jprmType, + jint jedp, jobject jListener) +{ + LOGD("OcSecureResource_provisionDirectPairing"); + if (!jListener || !pdacls || !jpin || ! jprmType) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "Invalid Parameters"); + return; + } + std::string pin = env->GetStringUTFChars(jpin, nullptr); + + JniOcSecureResource *secureResource = JniOcSecureResource::getJniOcSecureResourcePtr(env, thiz); + if (!secureResource) + { + return; + } + + const jsize len = env->GetArrayLength(jprmType); + jint* ints = env->GetIntArrayElements(jprmType, nullptr); + std::vector value; + for (jsize i = 0; i < len; ++i) + { + value.push_back(static_cast(ints[i])); + } + env->ReleaseIntArrayElements(jprmType, ints, JNI_ABORT); + + try + { + OCStackResult result = secureResource->provisionDirectPairing(env, pdacls, jListener, + pin, value, static_cast(jedp)); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "OcSecureResource_provisionDirectPairing"); + return; + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} + +/* + * Class: org_iotivity_base_OcSecureResource * Method: getLinkedDevices * Signature: ()Ljava/util/List; */ diff --git a/android/android_api/base/jni/JniOcSecureResource.h b/android/android_api/base/jni/JniOcSecureResource.h index 0b98f75..d071e96 100644 --- a/android/android_api/base/jni/JniOcSecureResource.h +++ b/android/android_api/base/jni/JniOcSecureResource.h @@ -55,7 +55,8 @@ class JniOcSecureResource jobject device2, jobject jListener); OCStackResult unlinkDevices(JNIEnv* env, jobject device2, jobject jListener); OCStackResult removeDevice(JNIEnv* env, jint timeout, jobject jListener); - + OCStackResult provisionDirectPairing(JNIEnv* env, jobjectArray jpdacls,jobject jListener, + std::string pin, std::vector prms, int edp); private: std::map> resultMap; @@ -118,6 +119,14 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionPairwise /* * Class: org_iotivity_base_OcSecureResource + * Method: provisionDirectPairing + * Signature: (Ljava/lang/Object;Lorg/iotivity/base/OcSecureResource/ProvisionDirectPairingListener;)V + */ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_provisionDirectPairing + (JNIEnv *, jobject, jstring, jobjectArray, jintArray, jint, jobject); + +/* + * Class: org_iotivity_base_OcSecureResource * Method: getLinkedDevices * Signature: ()Ljava/util/List; */ diff --git a/android/android_api/base/jni/JniOcStack.cpp b/android/android_api/base/jni/JniOcStack.cpp index c5c3d7d..18d0160 100644 --- a/android/android_api/base/jni/JniOcStack.cpp +++ b/android/android_api/base/jni/JniOcStack.cpp @@ -25,6 +25,18 @@ #include "OCRepresentation.h" #include "JniUtils.h" +/** + * Macro to verify the validity of input argument. + * + * @param arg log level + */ +#define VERIFY_VARIABLE_NULL(arg) \ + if (nullptr == (arg)) \ + { \ + LOGE("invalid input"); \ + return JNI_ERR; \ + } \ + JavaVM* g_jvm = nullptr; jclass g_cls_Integer = nullptr; @@ -62,6 +74,11 @@ jclass g_cls_OcResourceIdentifier = nullptr; jclass g_cls_OcProvisionResult = nullptr; jclass g_cls_OcSecureResource = nullptr; jclass g_cls_OcOicSecAcl = nullptr; +jclass g_cls_OcOicSecAcl_ace = nullptr; +jclass g_cls_OcOicSecAcl_resr = nullptr; +jclass g_cls_OcOicSecAcl_validity = nullptr; +jclass g_cls_OcOicSecPdAcl = nullptr; +jclass g_cls_OcDirectPairDevice = nullptr; jmethodID g_mid_Integer_ctor = nullptr; jmethodID g_mid_Double_ctor = nullptr; @@ -94,14 +111,30 @@ jmethodID g_mid_OcPresenceStatus_get = nullptr; jmethodID g_mid_OcResourceIdentifier_N_ctor = nullptr; jmethodID g_mid_OcProvisionResult_ctor = nullptr; jmethodID g_mid_OcSecureResource_ctor = nullptr; - -jmethodID g_mid_OcOicSecAcl_get_subject = nullptr; -jmethodID g_mid_OcOicSecAcl_get_resources_cnt = nullptr; -jmethodID g_mid_OcOicSecAcl_get_resources = nullptr; -jmethodID g_mid_OcOicSecAcl_get_permission = nullptr; -jmethodID g_mid_OcOicSecAcl_get_periods_cnt = nullptr; -jmethodID g_mid_OcOicSecAcl_get_periods = nullptr; -jmethodID g_mid_OcOicSecAcl_get_recurrences = nullptr; +jmethodID g_mid_OcDirectPairDevice_ctor = nullptr; +jmethodID g_mid_OcDirectPairDevice_dev_ctor = nullptr; + +jmethodID g_mid_OcOicSecPdAcl_get_resources_cnt = nullptr; +jmethodID g_mid_OcOicSecPdAcl_get_resources = nullptr; +jmethodID g_mid_OcOicSecPdAcl_get_permission = nullptr; +jmethodID g_mid_OcOicSecPdAcl_get_periods_cnt = nullptr; +jmethodID g_mid_OcOicSecPdAcl_get_periods = nullptr; +jmethodID g_mid_OcOicSecPdAcl_get_recurrences = nullptr; + +jmethodID g_mid_OcOicSecAcl_get_aces = nullptr; +jmethodID g_mid_OcOicSecAcl_ace_get_subjectID = nullptr; +jmethodID g_mid_OcOicSecAcl_ace_get_permissions = nullptr; +jmethodID g_mid_OcOicSecAcl_ace_get_resources = nullptr; +jmethodID g_mid_OcOicSecAcl_ace_get_validities = nullptr; +jmethodID g_mid_OcOicSecAcl_resr_get_href = nullptr; +jmethodID g_mid_OcOicSecAcl_resr_get_rel = nullptr; +jmethodID g_mid_OcOicSecAcl_resr_get_types = nullptr; +jmethodID g_mid_OcOicSecAcl_resr_get_typeLen = nullptr; +jmethodID g_mid_OcOicSecAcl_resr_get_interfaces = nullptr; +jmethodID g_mid_OcOicSecAcl_validity_get_getPeriod = nullptr; +jmethodID g_mid_OcOicSecAcl_validity_get_recurrences = nullptr; +jmethodID g_mid_OcOicSecAcl_validity_get_recurrenceLen = nullptr; +jmethodID g_mid_OcOicSecAcl_resr_get_interfaceLen = nullptr; jmethodID g_mid_OcOicSecAcl_get_rownerID = nullptr; jobject getOcException(JNIEnv* env, const char* file, const char* functionName, @@ -141,7 +174,7 @@ void throwOcException(JNIEnv* env, jobject ex) JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) { LOGI("JNI_OnLoad"); - JNIEnv* env; + JNIEnv* env = nullptr; g_jvm = vm; if (g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION) != JNI_OK) @@ -149,316 +182,392 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) LOGE("Failed to get the environment using GetEnv()"); return JNI_ERR; } + VERIFY_VARIABLE_NULL(env); jclass clazz = nullptr; //Integer clazz = env->FindClass("java/lang/Integer"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); + 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; + VERIFY_VARIABLE_NULL(g_mid_Integer_ctor); clazz = env->FindClass("[I"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_int1DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); clazz = env->FindClass("[[I"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_int2DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); //Double clazz = env->FindClass("java/lang/Double"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); 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; + VERIFY_VARIABLE_NULL(g_mid_Double_ctor); clazz = env->FindClass("[D"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_double1DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); clazz = env->FindClass("[[D"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_double2DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); //Boolean clazz = env->FindClass("java/lang/Boolean"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); 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; + VERIFY_VARIABLE_NULL(g_mid_Boolean_ctor); clazz = env->FindClass("[Z"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_boolean1DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); clazz = env->FindClass("[[Z"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_boolean2DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); //String clazz = env->FindClass("java/lang/String"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_String = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); clazz = env->FindClass("[Ljava/lang/String;"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_String1DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); clazz = env->FindClass("[[Ljava/lang/String;"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_String2DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); //LinkedList clazz = env->FindClass("java/util/LinkedList"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); 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; + VERIFY_VARIABLE_NULL(g_mid_LinkedList_ctor); g_mid_LinkedList_add_object = env->GetMethodID(g_cls_LinkedList, "add", "(Ljava/lang/Object;)Z"); - if (!g_mid_LinkedList_add_object) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_LinkedList_add_object); //Map clazz = env->FindClass("java/util/Map"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_Map = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_Map_entrySet = env->GetMethodID(g_cls_Map, "entrySet", "()Ljava/util/Set;"); - if (!g_mid_Map_entrySet) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_Map_entrySet); //MapEntry clazz = env->FindClass("java/util/Map$Entry"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_MapEntry = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_MapEntry_getKey = env->GetMethodID(g_cls_MapEntry, "getKey", "()Ljava/lang/Object;"); - if (!g_mid_MapEntry_getKey) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_MapEntry_getKey); g_mid_MapEntry_getValue = env->GetMethodID(g_cls_MapEntry, "getValue", "()Ljava/lang/Object;"); - if (!g_mid_MapEntry_getValue) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_MapEntry_getValue); //Set clazz = env->FindClass("java/util/Set"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_Set = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_Set_iterator = env->GetMethodID(g_cls_Set, "iterator", "()Ljava/util/Iterator;"); - if (!g_mid_Set_iterator) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_Set_iterator); //Iterator clazz = env->FindClass("java/util/Iterator"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); 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; + VERIFY_VARIABLE_NULL(g_mid_Iterator_hasNext); g_mid_Iterator_next = env->GetMethodID(g_cls_Iterator, "next", "()Ljava/lang/Object;"); - if (!g_mid_Iterator_next) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_Iterator_next); //HashMap clazz = env->FindClass("java/util/HashMap"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_HashMap = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_HashMap_ctor = env->GetMethodID(g_cls_HashMap, "", "()V"); - if (!g_mid_HashMap_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_HashMap_ctor); g_mid_HashMap_put = env->GetMethodID(g_cls_HashMap, "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); - if (!g_mid_HashMap_put) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_HashMap_put); //OcException clazz = env->FindClass("org/iotivity/base/OcException"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcException = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcException_ctor = env->GetMethodID(g_cls_OcException, "", "(Ljava/lang/String;Ljava/lang/String;)V"); - if (!g_mid_OcException_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcException_ctor); g_mid_OcException_setNativeExceptionLocation = env->GetMethodID(g_cls_OcException, "setNativeExceptionLocation", "(Ljava/lang/String;""Ljava/lang/String;""I)V"); - if (!g_mid_OcException_setNativeExceptionLocation) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcException_setNativeExceptionLocation); //OcResource clazz = env->FindClass("org/iotivity/base/OcResource"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcResource = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcResource_ctor = env->GetMethodID(g_cls_OcResource, "", "(J)V"); - if (!g_mid_OcResource_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcResource_ctor); //OcRepresentation clazz = env->FindClass("org/iotivity/base/OcRepresentation"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcRepresentation = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcRepresentation_N_ctor = env->GetMethodID(g_cls_OcRepresentation, "", "(J)V"); - if (!g_mid_OcRepresentation_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcRepresentation_N_ctor); g_mid_OcRepresentation_N_ctor_bool = env->GetMethodID(g_cls_OcRepresentation, "", "(JZ)V"); - if (!g_mid_OcRepresentation_N_ctor_bool) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcRepresentation_N_ctor_bool); clazz = env->FindClass("[Lorg/iotivity/base/OcRepresentation;"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcRepresentation1DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); clazz = env->FindClass("[[Lorg/iotivity/base/OcRepresentation;"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcRepresentation2DArray = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); //HeaderOptions clazz = env->FindClass("org/iotivity/base/OcHeaderOption"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcHeaderOption = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcHeaderOption_ctor = env->GetMethodID(g_cls_OcHeaderOption, "", "(ILjava/lang/String;)V"); - if (!g_mid_OcHeaderOption_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcHeaderOption_ctor); g_mid_OcHeaderOption_get_id = env->GetMethodID(g_cls_OcHeaderOption, "getOptionId", "()I"); - if (!g_mid_OcHeaderOption_get_id) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcHeaderOption_get_id); g_mid_OcHeaderOption_get_data = env->GetMethodID(g_cls_OcHeaderOption, "getOptionData", "()Ljava/lang/String;"); - if (!g_mid_OcHeaderOption_get_data) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcHeaderOption_get_data); //OcResourceRequest clazz = env->FindClass("org/iotivity/base/OcResourceRequest"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcResourceRequest = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcResourceRequest_N_ctor = env->GetMethodID(g_cls_OcResourceRequest, "", "(J)V"); - if (!g_mid_OcResourceRequest_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcResourceRequest_N_ctor); //OcResourceResponse clazz = env->FindClass("org/iotivity/base/OcResourceResponse"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcResourceResponse = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcResourceResponse_N_ctor = env->GetMethodID(g_cls_OcResourceResponse, "", "(J)V"); - if (!g_mid_OcResourceResponse_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcResourceResponse_N_ctor); //OcResourceHandle clazz = env->FindClass("org/iotivity/base/OcResourceHandle"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcResourceHandle = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcResourceHandle_N_ctor = env->GetMethodID(g_cls_OcResourceHandle, "", "(J)V"); - if (!g_mid_OcResourceHandle_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcResourceHandle_N_ctor); //OcPresenceHandle clazz = env->FindClass("org/iotivity/base/OcPresenceHandle"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcPresenceHandle = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcPresenceHandle_N_ctor = env->GetMethodID(g_cls_OcPresenceHandle, "", "(J)V"); - if (!g_mid_OcPresenceHandle_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcPresenceHandle_N_ctor); //OcRequestHandle clazz = env->FindClass("org/iotivity/base/OcRequestHandle"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcRequestHandle = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcRequestHandle_N_ctor = env->GetMethodID(g_cls_OcRequestHandle, "", "(J)V"); - if (!g_mid_OcRequestHandle_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcRequestHandle_N_ctor); //OcPresenceStatus clazz = env->FindClass("org/iotivity/base/OcPresenceStatus"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcPresenceStatus = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcPresenceStatus_get = env->GetStaticMethodID(g_cls_OcPresenceStatus, "get", "(Ljava/lang/String;)Lorg/iotivity/base/OcPresenceStatus;"); - if (!g_mid_OcPresenceStatus_get) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcPresenceStatus_get); //ObservationInfo clazz = env->FindClass("org/iotivity/base/ObservationInfo"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_ObservationInfo = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_ObservationInfo_N_ctor = env->GetMethodID(g_cls_ObservationInfo, "", "(IB)V"); - if (!g_mid_ObservationInfo_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_ObservationInfo_N_ctor); clazz = env->FindClass("org/iotivity/base/OcResourceIdentifier"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcResourceIdentifier = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcResourceIdentifier_N_ctor = env->GetMethodID(g_cls_OcResourceIdentifier, "", "(J)V"); - if (!g_mid_OcResourceIdentifier_N_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcResourceIdentifier_N_ctor); //OcSecureResource clazz = env->FindClass("org/iotivity/base/OcSecureResource"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcSecureResource = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcSecureResource_ctor = env->GetMethodID(g_cls_OcSecureResource, "", "(J)V"); - if (!g_mid_OcSecureResource_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcSecureResource_ctor); //ProvisionResult clazz = env->FindClass("org/iotivity/base/ProvisionResult"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcProvisionResult = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); g_mid_OcProvisionResult_ctor = env->GetMethodID(g_cls_OcProvisionResult, "", "(Ljava/lang/String;I)V"); - if (!g_mid_OcProvisionResult_ctor) return JNI_ERR; + VERIFY_VARIABLE_NULL(g_mid_OcProvisionResult_ctor); + + //OcDirectPairDevice + clazz = env->FindClass("org/iotivity/base/OcDirectPairDevice"); + VERIFY_VARIABLE_NULL(clazz); + g_cls_OcDirectPairDevice = (jclass)env->NewGlobalRef(clazz); + g_mid_OcDirectPairDevice_ctor = env->GetMethodID(g_cls_OcDirectPairDevice, "", "(J)V"); + VERIFY_VARIABLE_NULL(g_mid_OcDirectPairDevice_ctor); + + g_mid_OcDirectPairDevice_dev_ctor = env->GetMethodID(g_cls_OcDirectPairDevice, "", "(Ljava/lang/String;)V"); + VERIFY_VARIABLE_NULL(g_mid_OcDirectPairDevice_dev_ctor); + env->DeleteLocalRef(clazz); //OicSecAcl clazz = env->FindClass("org/iotivity/base/OicSecAcl"); - if (!clazz) return JNI_ERR; + VERIFY_VARIABLE_NULL(clazz); g_cls_OcOicSecAcl = (jclass)env->NewGlobalRef(clazz); env->DeleteLocalRef(clazz); - g_mid_OcOicSecAcl_get_subject = env->GetMethodID(g_cls_OcOicSecAcl, "getSubject", "()Ljava/lang/String;"); - if (!g_mid_OcOicSecAcl_get_subject) return JNI_ERR; + g_mid_OcOicSecAcl_get_rownerID = env->GetMethodID(g_cls_OcOicSecAcl, "getRownerID", "()Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_get_rownerID); + + g_mid_OcOicSecAcl_get_aces = env->GetMethodID(g_cls_OcOicSecAcl, "getOicSecAces", "()[Lorg/iotivity/base/OicSecAce;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_get_aces); + + //OicSecAce + clazz = env->FindClass("org/iotivity/base/OicSecAce"); + VERIFY_VARIABLE_NULL(clazz); + g_cls_OcOicSecAcl_ace = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_OcOicSecAcl_ace_get_subjectID = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getSubjectID","()Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_subjectID); - g_mid_OcOicSecAcl_get_resources_cnt = env->GetMethodID(g_cls_OcOicSecAcl, "getResourcesCount", "()I"); - if (!g_mid_OcOicSecAcl_get_resources_cnt) return JNI_ERR; + g_mid_OcOicSecAcl_ace_get_permissions = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getPermission","()I"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_permissions); - g_mid_OcOicSecAcl_get_resources = env->GetMethodID(g_cls_OcOicSecAcl, "getResources", "(I)Ljava/lang/String;"); - if (!g_mid_OcOicSecAcl_get_resources) return JNI_ERR; + g_mid_OcOicSecAcl_ace_get_resources = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getResources","()[Lorg/iotivity/base/OicSecResr;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_resources); - g_mid_OcOicSecAcl_get_permission = env->GetMethodID(g_cls_OcOicSecAcl, "getPermission", "()I"); - if (!g_mid_OcOicSecAcl_get_permission) return JNI_ERR; + g_mid_OcOicSecAcl_ace_get_validities = env->GetMethodID(g_cls_OcOicSecAcl_ace, "getValidities","()[Lorg/iotivity/base/OicSecValidity;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_ace_get_validities); - g_mid_OcOicSecAcl_get_periods_cnt = env->GetMethodID(g_cls_OcOicSecAcl, "getPeriodsCount", "()I"); - if (!g_mid_OcOicSecAcl_get_periods_cnt) return JNI_ERR; + //OicSecResr + clazz = env->FindClass("org/iotivity/base/OicSecResr"); + VERIFY_VARIABLE_NULL(clazz); + g_cls_OcOicSecAcl_resr = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); - g_mid_OcOicSecAcl_get_periods = env->GetMethodID(g_cls_OcOicSecAcl, "getPeriods", "(I)Ljava/lang/String;"); - if (!g_mid_OcOicSecAcl_get_periods) return JNI_ERR; + g_mid_OcOicSecAcl_resr_get_href = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getHref","()Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_href); - g_mid_OcOicSecAcl_get_recurrences = env->GetMethodID(g_cls_OcOicSecAcl, "getRecurrences", "(I)Ljava/lang/String;"); - if (!g_mid_OcOicSecAcl_get_recurrences) return JNI_ERR; + g_mid_OcOicSecAcl_resr_get_rel = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getRel","()Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_rel); - g_mid_OcOicSecAcl_get_rownerID = env->GetMethodID(g_cls_OcOicSecAcl, "getRownerID", "()Ljava/lang/String;"); - if (!g_mid_OcOicSecAcl_get_rownerID) return JNI_ERR; + g_mid_OcOicSecAcl_resr_get_types = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getTypes","(I)Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_types); + + g_mid_OcOicSecAcl_resr_get_typeLen = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getTypeLen","()I"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_typeLen); + + g_mid_OcOicSecAcl_resr_get_interfaces = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getInterfaces","(I)Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_interfaces); + + g_mid_OcOicSecAcl_resr_get_interfaceLen = env->GetMethodID(g_cls_OcOicSecAcl_resr, "getInterfaceLen","()I"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_resr_get_interfaceLen); + + //OicSecAcl$OicSecValidity + clazz = env->FindClass("org/iotivity/base/OicSecValidity"); + VERIFY_VARIABLE_NULL(clazz); + g_cls_OcOicSecAcl_validity = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_OcOicSecAcl_validity_get_getPeriod = env->GetMethodID(g_cls_OcOicSecAcl_validity, "getPeriod","()Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_validity_get_getPeriod); + + g_mid_OcOicSecAcl_validity_get_recurrences = env->GetMethodID(g_cls_OcOicSecAcl_validity, "getRecurrences","(I)Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_validity_get_recurrences); + + g_mid_OcOicSecAcl_validity_get_recurrenceLen = env->GetMethodID(g_cls_OcOicSecAcl_validity, "getRecurrenceLen","()I"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecAcl_validity_get_recurrenceLen); + + //OicSecPdAcl + clazz = env->FindClass("org/iotivity/base/OicSecPdAcl"); + VERIFY_VARIABLE_NULL(clazz); + g_cls_OcOicSecPdAcl = (jclass)env->NewGlobalRef(clazz); + env->DeleteLocalRef(clazz); + + g_mid_OcOicSecPdAcl_get_resources_cnt = env->GetMethodID(g_cls_OcOicSecPdAcl, "getResourcesCount", "()I"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_resources_cnt); + + g_mid_OcOicSecPdAcl_get_resources = env->GetMethodID(g_cls_OcOicSecPdAcl, "getResources", "(I)Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_resources); + + g_mid_OcOicSecPdAcl_get_permission = env->GetMethodID(g_cls_OcOicSecPdAcl, "getPermission", "()I"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_permission); + + g_mid_OcOicSecPdAcl_get_periods_cnt = env->GetMethodID(g_cls_OcOicSecPdAcl, "getPeriodsCount", "()I"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_periods_cnt); + + g_mid_OcOicSecPdAcl_get_periods = env->GetMethodID(g_cls_OcOicSecPdAcl, "getPeriods", "(I)Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_periods); + + g_mid_OcOicSecPdAcl_get_recurrences = env->GetMethodID(g_cls_OcOicSecPdAcl, "getRecurrences", "(I)Ljava/lang/String;"); + VERIFY_VARIABLE_NULL(g_mid_OcOicSecPdAcl_get_recurrences); return JNI_CURRENT_VERSION; } @@ -509,4 +618,7 @@ JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) env->DeleteGlobalRef(g_cls_OcSecureResource); env->DeleteGlobalRef(g_cls_OcProvisionResult); env->DeleteGlobalRef(g_cls_OcOicSecAcl); -} \ No newline at end of file + env->DeleteGlobalRef(g_cls_OcOicSecAcl_ace); + env->DeleteGlobalRef(g_cls_OcOicSecAcl_resr); + env->DeleteGlobalRef(g_cls_OcOicSecAcl_validity); +} diff --git a/android/android_api/base/jni/JniOcStack.h b/android/android_api/base/jni/JniOcStack.h index b9f2ccd..243ce4c 100644 --- a/android/android_api/base/jni/JniOcStack.h +++ b/android/android_api/base/jni/JniOcStack.h @@ -82,6 +82,12 @@ extern jclass g_cls_OcResourceIdentifier; extern jclass g_cls_OcProvisionResult; extern jclass g_cls_OcSecureResource; extern jclass g_cls_OcOicSecAcl; +extern jclass g_cls_OcOicSecPdAcl; +extern jclass g_cls_OcDirectPairDevice; + +extern jclass g_cls_OcOicSecAcl_ace; +extern jclass g_cls_OcOicSecAcl_resr; +extern jclass g_cls_OcOicSecAcl_validity; extern jmethodID g_mid_Integer_ctor; extern jmethodID g_mid_Double_ctor; @@ -114,14 +120,30 @@ extern jmethodID g_mid_OcPresenceStatus_get; extern jmethodID g_mid_OcResourceIdentifier_N_ctor; extern jmethodID g_mid_OcProvisionResult_ctor; extern jmethodID g_mid_OcSecureResource_ctor; -extern jmethodID g_mid_OcOicSecAcl_get_subject; -extern jmethodID g_mid_OcOicSecAcl_get_resources_cnt; -extern jmethodID g_mid_OcOicSecAcl_get_resources; -extern jmethodID g_mid_OcOicSecAcl_get_permission; -extern jmethodID g_mid_OcOicSecAcl_get_periods_cnt; -extern jmethodID g_mid_OcOicSecAcl_get_periods; -extern jmethodID g_mid_OcOicSecAcl_get_recurrences; +extern jmethodID g_mid_OcDirectPairDevice_ctor; +extern jmethodID g_mid_OcDirectPairDevice_dev_ctor; + extern jmethodID g_mid_OcOicSecAcl_get_rownerID; +extern jmethodID g_mid_OcOicSecAcl_get_aces; +extern jmethodID g_mid_OcOicSecAcl_ace_get_subjectID; +extern jmethodID g_mid_OcOicSecAcl_ace_get_permissions; +extern jmethodID g_mid_OcOicSecAcl_ace_get_resources; +extern jmethodID g_mid_OcOicSecAcl_ace_get_validities; +extern jmethodID g_mid_OcOicSecAcl_resr_get_href; +extern jmethodID g_mid_OcOicSecAcl_resr_get_rel; +extern jmethodID g_mid_OcOicSecAcl_resr_get_types; +extern jmethodID g_mid_OcOicSecAcl_resr_get_typeLen; +extern jmethodID g_mid_OcOicSecAcl_resr_get_interfaces; +extern jmethodID g_mid_OcOicSecAcl_resr_get_interfaceLen; +extern jmethodID g_mid_OcOicSecAcl_validity_get_getPeriod; +extern jmethodID g_mid_OcOicSecAcl_validity_get_recurrences; +extern jmethodID g_mid_OcOicSecAcl_validity_get_recurrenceLen; +extern jmethodID g_mid_OcOicSecPdAcl_get_resources_cnt; +extern jmethodID g_mid_OcOicSecPdAcl_get_resources; +extern jmethodID g_mid_OcOicSecPdAcl_get_permission; +extern jmethodID g_mid_OcOicSecPdAcl_get_periods_cnt; +extern jmethodID g_mid_OcOicSecPdAcl_get_periods; +extern jmethodID g_mid_OcOicSecPdAcl_get_recurrences; typedef void(*RemoveListenerCallback)(JNIEnv* env, jobject jListener); @@ -151,26 +173,29 @@ static JNIEnv* GetJNIEnv(jint& ret) JNIEnv *env = nullptr; ret = g_jvm->GetEnv((void **)&env, JNI_CURRENT_VERSION); - switch (ret) { - case JNI_OK: - return env; - case JNI_EDETACHED: - if (g_jvm->AttachCurrentThread(&env, nullptr) < 0) - { + switch (ret) + { + case JNI_OK: + return env; + case JNI_EDETACHED: + if (g_jvm->AttachCurrentThread(&env, nullptr) < 0) + { + LOGE("Failed to get the environment"); + return nullptr; + } + else + { + return env; + } + + case JNI_EVERSION: + LOGE("JNI version not supported"); + break; + default: 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; } + return nullptr; } static void DuplicateString(char ** targetString, std::string sourceString) @@ -179,4 +204,4 @@ static void DuplicateString(char ** targetString, std::string sourceString) strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1)); } -#endif // _Included_org_iotivity_base_ocstack \ No newline at end of file +#endif // _Included_org_iotivity_base_ocstack diff --git a/android/android_api/base/jni/JniOnDPDevicesFoundListener.cpp b/android/android_api/base/jni/JniOnDPDevicesFoundListener.cpp new file mode 100644 index 0000000..5c5d900 --- /dev/null +++ b/android/android_api/base/jni/JniOnDPDevicesFoundListener.cpp @@ -0,0 +1,165 @@ +/* +* //****************************************************************** +* // +* // Copyright 2016 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 "JniOnDPDevicesFoundListener.h" +#include "JniOcDirectPairDevice.h" +#include "OCApi.h" +using namespace OC; + +JniOnDPDevicesFoundListener::JniOnDPDevicesFoundListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); + m_removeListenerCallback = removeListenerCallback; +} + +jobject JniOnDPDevicesFoundListener::convertdpDevVectorToJavaList(JNIEnv *env, PairedDevices DPdevList) +{ + jobject jResultList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor); + if (!jResultList) + { + return nullptr; + } + + for (size_t i = 0; i < DPdevList.size(); ++i) + { + JniOcDirectPairDevice *device = new JniOcDirectPairDevice(DPdevList.at(i)); + if (!device) + { + return nullptr; + } + + jstring jStr = env->NewStringUTF(((DPdevList.at(i))->getDeviceID()).c_str()); + if (!jStr) + { + return nullptr; + } + jobject jresult = env->NewObject(g_cls_OcDirectPairDevice, + g_mid_OcDirectPairDevice_dev_ctor,jStr); + if (!jresult) + { + return nullptr; + } + + SetHandle(env, jresult, device); + + env->CallBooleanMethod(jResultList, g_mid_LinkedList_add_object, jresult); + if (env->ExceptionCheck()) + { + return nullptr; + } + env->DeleteLocalRef(jresult); + env->DeleteLocalRef(jStr); + } + return jResultList; +} + +JniOnDPDevicesFoundListener::~JniOnDPDevicesFoundListener() +{ + LOGI("~JniOnDPDevicesFoundListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + m_jwListener = nullptr; + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniOnDPDevicesFoundListener::directPairingDevicesCallback(PairedDevices paringDevicesList, DPFunc func) +{ + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == 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; + } + + jobject jpairingDevicesList = convertdpDevVectorToJavaList(env, paringDevicesList); + std::string calledFunc; + switch (func) + { + case DPFunc::FIND_DIRECT_PAIRED_DEV_LIST: + { + calledFunc = "onFindDirectPairingListener"; + } + break; + case DPFunc::GET_PAIRED_DEV_LIST: + { + calledFunc = "onGetDirectPairedListener"; + } + default: + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } + return; + } + + jmethodID midL = env->GetMethodID(clsL, calledFunc.c_str(), "(Ljava/util/List;)V"); + + if (!midL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + env->CallVoidMethod(jListener, midL, jpairingDevicesList); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + } + + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); +} + +void JniOnDPDevicesFoundListener::checkExAndRemoveListener(JNIEnv* env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_removeListenerCallback(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_removeListenerCallback(env, m_jwListener); + } +} diff --git a/android/android_api/base/jni/JniOnDPDevicesFoundListener.h b/android/android_api/base/jni/JniOnDPDevicesFoundListener.h new file mode 100644 index 0000000..88acb61 --- /dev/null +++ b/android/android_api/base/jni/JniOnDPDevicesFoundListener.h @@ -0,0 +1,51 @@ +/* +* //****************************************************************** +* // +* // Copyright 2016 Samsung Electronics All Rights Reserved. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // Licensed under the Apache License, Version 2.0 (the "License"); +* // you may not use this file except in compliance with the License. +* // You may obtain a copy of the License at +* // +* // http://www.apache.org/licenses/LICENSE-2.0 +* // +* // Unless required by applicable law or agreed to in writing, software +* // distributed under the License is distributed on an "AS IS" BASIS, +* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* // See the License for the specific language governing permissions and +* // limitations under the License. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +*/ +#include +#include "OCDirectPairing.h" +#include "OCApi.h" +#include "JniOcStack.h" + +#ifndef _Included_org_iotivity_base_OcPlatform_JniOnDPDevicesFoundListener +#define _Included_org_iotivity_base_OcPlatform_JniOnDPDevicesFoundListener + +enum class DPFunc +{ + FIND_DIRECT_PAIRED_DEV_LIST = 1, + GET_PAIRED_DEV_LIST, +}; + +class JniOnDPDevicesFoundListener +{ +public: + JniOnDPDevicesFoundListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback); + ~JniOnDPDevicesFoundListener(); + + jobject convertdpDevVectorToJavaList(JNIEnv *env, OC::PairedDevices DPdevList); + void directPairingDevicesCallback(OC::PairedDevices paringDevicesList, DPFunc); + +private: + RemoveListenerCallback m_removeListenerCallback; + jweak m_jwListener; + void checkExAndRemoveListener(JNIEnv* env); +}; +#endif diff --git a/android/android_api/base/jni/JniOnDirectPairingListener.cpp b/android/android_api/base/jni/JniOnDirectPairingListener.cpp new file mode 100644 index 0000000..f580079 --- /dev/null +++ b/android/android_api/base/jni/JniOnDirectPairingListener.cpp @@ -0,0 +1,129 @@ +/* +* //****************************************************************** +* // +* // Copyright 2016 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 "JniOnDirectPairingListener.h" +#include "JniOcDirectPairDevice.h" +#include "OCDirectPairing.h" +#include "OCApi.h" +using namespace OC; + +JniOnDirectPairingListener::JniOnDirectPairingListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback) +{ + m_jwListener = env->NewWeakGlobalRef(jListener); + m_removeListenerCallback = removeListenerCallback; +} + + +JniOnDirectPairingListener::~JniOnDirectPairingListener() +{ + LOGI("~JniOnDirectPairingListener()"); + if (m_jwListener) + { + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == env) return; + env->DeleteWeakGlobalRef(m_jwListener); + m_jwListener = nullptr; + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + } +} + +void JniOnDirectPairingListener::doDirectPairingCB(std::shared_ptr dpDev, + OCStackResult result) +{ + jint ret; + JNIEnv *env = GetJNIEnv(ret); + if (nullptr == 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; + } + + JniOcDirectPairDevice *device = new JniOcDirectPairDevice(dpDev); + if (!device) return; + + jstring jStr = env->NewStringUTF((dpDev->getDeviceID()).c_str()); + if (!jStr) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return ; + } + + jobject jresult = env->NewObject(g_cls_OcDirectPairDevice, g_mid_OcDirectPairDevice_ctor); + if (!jresult) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return ; + } + + SetHandle(env, jresult, device); + + jint jres = static_cast(result); + + jmethodID midL = env->GetMethodID(clsL, "onDirectPairingListener", "(Ljava/lang/String;I)V"); + + if (!midL) + { + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); + return; + } + + env->CallVoidMethod(jListener, midL, jStr, jres); + if (env->ExceptionCheck()) + { + LOGE("Java exception is thrown"); + } + + checkExAndRemoveListener(env); + if (JNI_EDETACHED == ret) g_jvm->DetachCurrentThread(); +} + +void JniOnDirectPairingListener::checkExAndRemoveListener(JNIEnv* env) +{ + if (env->ExceptionCheck()) + { + jthrowable ex = env->ExceptionOccurred(); + env->ExceptionClear(); + m_removeListenerCallback(env, m_jwListener); + env->Throw((jthrowable)ex); + } + else + { + m_removeListenerCallback(env, m_jwListener); + } +} diff --git a/android/android_api/base/jni/JniOnDirectPairingListener.h b/android/android_api/base/jni/JniOnDirectPairingListener.h new file mode 100644 index 0000000..8ec2b7e --- /dev/null +++ b/android/android_api/base/jni/JniOnDirectPairingListener.h @@ -0,0 +1,43 @@ +/* +* //****************************************************************** +* // +* // Copyright 2016 Samsung Electronics All Rights Reserved. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +* // +* // Licensed under the Apache License, Version 2.0 (the "License"); +* // you may not use this file except in compliance with the License. +* // You may obtain a copy of the License at +* // +* // http://www.apache.org/licenses/LICENSE-2.0 +* // +* // Unless required by applicable law or agreed to in writing, software +* // distributed under the License is distributed on an "AS IS" BASIS, +* // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* // See the License for the specific language governing permissions and +* // limitations under the License. +* // +* //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +*/ +#include +#include "OCDirectPairing.h" +#include "JniOcStack.h" + +#ifndef _Included_org_iotivity_base_OcPlatform_JniOnDirectPairingListener +#define _Included_org_iotivity_base_OcPlatform_JniOnDirectPairingListener + +class JniOnDirectPairingListener +{ +public: + JniOnDirectPairingListener(JNIEnv *env, jobject jListener, + RemoveListenerCallback removeListenerCallback); + ~JniOnDirectPairingListener(); + + void doDirectPairingCB(std::shared_ptr, OCStackResult); + +private: + RemoveListenerCallback m_removeListenerCallback; + jweak m_jwListener; + void checkExAndRemoveListener(JNIEnv* env); +}; +#endif diff --git a/android/android_api/base/jni/JniProvisionResultListner.cpp b/android/android_api/base/jni/JniProvisionResultListner.cpp index 6b9fd04..a600e64 100644 --- a/android/android_api/base/jni/JniProvisionResultListner.cpp +++ b/android/android_api/base/jni/JniProvisionResultListner.cpp @@ -109,6 +109,11 @@ void JniProvisionResultListner::ProvisionResultCallback(PMResultList_t *result, calledFunc = "provisionPairwiseDevicesListener"; } break; + case ListenerFunc::PROVISIONDIRECTPAIRING: + { + calledFunc = "provisionDirectPairingListener"; + } + break; default: { checkExAndRemoveListener(env); diff --git a/android/android_api/base/jni/JniProvisionResultListner.h b/android/android_api/base/jni/JniProvisionResultListner.h index 6df56d7..8bf5242 100644 --- a/android/android_api/base/jni/JniProvisionResultListner.h +++ b/android/android_api/base/jni/JniProvisionResultListner.h @@ -35,7 +35,8 @@ enum class ListenerFunc PROVISIONCREDENTIALS, UNLINKDEVICES, REMOVEDEVICE, - PROVISIONPAIRWISEDEVICES + PROVISIONPAIRWISEDEVICES, + PROVISIONDIRECTPAIRING }; class JniProvisionResultListner diff --git a/android/android_api/base/jni/JniSecureUtils.cpp b/android/android_api/base/jni/JniSecureUtils.cpp index 24e4f57..718bb4a 100644 --- a/android/android_api/base/jni/JniSecureUtils.cpp +++ b/android/android_api/base/jni/JniSecureUtils.cpp @@ -21,7 +21,9 @@ */ #include "JniSecureUtils.h" +#include "JniOcStack.h" #include "JniOcSecureResource.h" +#include "oic_malloc.h" #include "srmutility.h" #include "base64.h" @@ -35,7 +37,15 @@ jobject JniSecureUtils::convertProvisionresultVectorToJavaList(JNIEnv *env, cons for (size_t i = 0; i < result->size(); ++i) { - jstring jStr = env->NewStringUTF((convertUUIDtoStr(result->at(i).deviceId).c_str())); + char *Str = NULL; + + if (OC_STACK_OK != ConvertUuidToStr(&(result->at(i).deviceId), &Str)) + { + return nullptr; + } + jstring jStr = env->NewStringUTF(Str); + OICFree(Str); + if (!jStr) { return nullptr; @@ -117,7 +127,15 @@ jobject JniSecureUtils::convertUUIDVectorToJavaStrList(JNIEnv *env, UuidList_t & } for (size_t i = 0; i < vector.size(); ++i) { - jstring jStr = env->NewStringUTF((convertUUIDtoStr(vector[i])).c_str()); + char *Str = NULL; + + if (OC_STACK_OK != ConvertUuidToStr(&(vector[i]), &Str)) + { + return nullptr; + } + jstring jStr = env->NewStringUTF(Str); + OICFree(Str); + if (!jStr) { return nullptr; @@ -132,19 +150,270 @@ jobject JniSecureUtils::convertUUIDVectorToJavaStrList(JNIEnv *env, UuidList_t & return jList; } +static void freeValidities(OicSecValidity_t *head) +{ + OicSecValidity_t *tmp = head; + while(tmp != NULL) + { + head = head->next; + free(tmp->recurrences); + free(tmp); + tmp = head; + } +} + +static void freeResources(OicSecRsrc_t *head) +{ + OicSecRsrc_t *tmp = head; + while(tmp != NULL) + { + head = head->next; + free(tmp->types); + free(tmp->interfaces); + free(tmp); + tmp = head; + } +} + +void JniSecureUtils::FreeACLList(OicSecAcl_t *acl) +{ + if (acl) + { + if (acl->aces) + { + freeResources(acl->aces->resources); + freeValidities(acl->aces->validities); + OICFree(acl->aces); + } + OICFree(acl); + } +} + +static OicSecValidity_t* getValiditiesList(JNIEnv *env, jobject validityObject) +{ + jstring jData; + jobjectArray valList = (jobjectArray)env->CallObjectMethod(validityObject, g_mid_OcOicSecAcl_ace_get_validities); + if (!valList || env->ExceptionCheck()) + { + return nullptr; + } + int nr_validities = env->GetArrayLength(valList); + + OicSecValidity_t *valHead = NULL; + + for (int i = 0 ; i < nr_validities; i++) + { + OicSecValidity_t *tmp = (OicSecValidity_t*)OICCalloc(1, sizeof(OicSecValidity_t)); + if (NULL == tmp) + { + LOGE("Failed to allocate memory"); + freeValidities(valHead); + return nullptr; + } + + jobject element = env->GetObjectArrayElement(valList, i); + if (!element || env->ExceptionCheck()) + { + OICFree(tmp); + freeValidities(valHead); + return nullptr; + } + + jData = (jstring)env->CallObjectMethod(element, g_mid_OcOicSecAcl_validity_get_getPeriod); + if (!jData || env->ExceptionCheck()) + { + OICFree(tmp); + freeValidities(valHead); + return nullptr; + } + tmp->period = (char*)env->GetStringUTFChars(jData, 0); + + jint jrecurrenceLen = (jint) env->CallIntMethod(element, + g_mid_OcOicSecAcl_validity_get_recurrenceLen); + if (env->ExceptionCheck()) + { + OICFree(tmp); + freeValidities(valHead); + return nullptr; + } + tmp->recurrenceLen = (int)jrecurrenceLen; + + if (jrecurrenceLen > 0) + { + jvalue argv[1]; + tmp->recurrences = (char**)OICCalloc(jrecurrenceLen, sizeof(char*)); + + for (int i = 0 ; i < jrecurrenceLen; i++) + { + argv[0].i = i; + jData = (jstring)env->CallObjectMethodA(element, g_mid_OcOicSecAcl_validity_get_recurrences, argv); + if (!jData || env->ExceptionCheck()) + { + OICFree(tmp->recurrences); + OICFree(tmp); + freeValidities(valHead); + return nullptr; + } + tmp->recurrences[i] = (char*)env->GetStringUTFChars(jData, 0); + } + } + if (NULL == valHead) + { + valHead = tmp; + } + else + { + OicSecValidity_t *ptr = valHead; + while(ptr->next != NULL) ptr = ptr->next; + ptr->next = tmp; + tmp->next = NULL; + } + env->DeleteLocalRef(element); + } + return valHead; +} + +static OicSecRsrc_t * getResourcesList(JNIEnv *env, jobject resourceObject) +{ + jstring jData; + + jobjectArray rescList = (jobjectArray)env->CallObjectMethod(resourceObject, g_mid_OcOicSecAcl_ace_get_resources); + if (!rescList || env->ExceptionCheck()) + { + return nullptr; + } + + int nr_resc = env->GetArrayLength(rescList); + OicSecRsrc_t *rescHead = NULL; + + for (int i = 0 ; i < nr_resc; i++) + { + OicSecRsrc_t *tmp = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t)); + if (NULL == tmp) + { + LOGE("Failed to allocate memory"); + freeResources(rescHead); + return nullptr; + } + jobject element = env->GetObjectArrayElement(rescList, i); + if (!element || env->ExceptionCheck()) + { + OICFree(tmp); + freeResources(rescHead); + return nullptr; + } + jData = (jstring)env->CallObjectMethod(element, g_mid_OcOicSecAcl_resr_get_href); + if (!jData || env->ExceptionCheck()) + { + OICFree(tmp); + freeResources(rescHead); + return nullptr; + } + tmp->href = (char*)env->GetStringUTFChars(jData, 0); + + jData = (jstring)env->CallObjectMethod(element, g_mid_OcOicSecAcl_resr_get_rel); + if (!jData || env->ExceptionCheck()) + { + OICFree(tmp); + freeResources(rescHead); + return nullptr; + } + tmp->rel = (char*)env->GetStringUTFChars(jData, 0); + + jint len = (jint) env->CallIntMethod(element, g_mid_OcOicSecAcl_resr_get_typeLen); + if (env->ExceptionCheck()) + { + OICFree(tmp); + freeResources(rescHead); + return nullptr; + } + tmp->typeLen = (int)len; + if (len > 0) + { + jvalue argv[1]; + tmp->types = (char**)OICCalloc(len, sizeof(char*)); + if (NULL == tmp->types) + { + LOGE("Failed to allocate memory"); + return nullptr; + } + + for (int i = 0 ; i < len; i++) + { + argv[0].i = i; + jData = (jstring)env->CallObjectMethodA(element, g_mid_OcOicSecAcl_resr_get_types, argv); + if (!jData || env->ExceptionCheck()) + { + OICFree(tmp->types); + OICFree(tmp); + freeResources(rescHead); + return nullptr; + } + tmp->types[i] = (char*)env->GetStringUTFChars(jData, 0); + } + } + + len = (jint) env->CallIntMethod(element, g_mid_OcOicSecAcl_resr_get_interfaceLen); + if (env->ExceptionCheck()) + { + OICFree(tmp->types); + OICFree(tmp); + freeResources(rescHead); + return nullptr; + } + tmp->interfaceLen = len; + if (len > 0) + { + jvalue argv[1]; + tmp->interfaces = (char**)OICCalloc(len, sizeof(char*)); + + for (int i = 0 ; i < len; i++) + { + argv[0].i = i; + jData = (jstring)env->CallObjectMethodA(element, g_mid_OcOicSecAcl_resr_get_interfaces, argv); + if (!jData || env->ExceptionCheck()) + { + OICFree(tmp->types); + OICFree(tmp); + freeResources(rescHead); + return nullptr; + } + tmp->interfaces[i] = (char*)env->GetStringUTFChars(jData, 0); + } + } + + if (NULL == rescHead) + { + rescHead = tmp; + } + else + { + OicSecRsrc_t *ptr = rescHead; + while(ptr->next != NULL) ptr = ptr->next; + ptr->next = tmp; + tmp->next = NULL; + } + env->DeleteLocalRef(element); + } + return rescHead; +} + OCStackResult JniSecureUtils::convertJavaACLToOCAcl(JNIEnv *env, jobject in, OicSecAcl_t *acl) { jstring jData; - jvalue args[1]; - jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_subject); + jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_rownerID); if (!jData || env->ExceptionCheck()) { return OC_STACK_ERROR; } char *str = (char*) env->GetStringUTFChars(jData, 0); - if (OC_STACK_OK == ConvertStrToUuid(str, &acl->subject)) + if (!str) + { + return OC_STACK_ERROR; + } + if (OC_STACK_OK == ConvertStrToUuid(str, &acl->rownerID)) { env->ReleaseStringUTFChars(jData, str); } @@ -153,82 +422,160 @@ OCStackResult JniSecureUtils::convertJavaACLToOCAcl(JNIEnv *env, jobject in, Oic return OC_STACK_ERROR; } - jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_resources_cnt); + jobjectArray acesList = (jobjectArray)env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_aces); + + if (!acesList || env->ExceptionCheck()) + { + return OC_STACK_ERROR; + } + + int nr_aces = env->GetArrayLength(acesList); + + OicSecAce_t *acesHead = NULL; + + for (int i = 0 ; i < nr_aces; i++) + { + OicSecAce_t *tmp = (OicSecAce_t*)OICCalloc(1, sizeof(OicSecAce_t)); + + jobject element = env->GetObjectArrayElement(acesList, i); + if (!element || env->ExceptionCheck()) + { + return OC_STACK_ERROR; + } + + jData = (jstring) env->CallObjectMethod(element, g_mid_OcOicSecAcl_ace_get_subjectID); + if (!jData || env->ExceptionCheck()) + { + return OC_STACK_ERROR; + } + + str = (char*) env->GetStringUTFChars(jData, 0); + + if (!str) + { + return OC_STACK_ERROR; + } + if (OC_STACK_OK == ConvertStrToUuid(str, &tmp->subjectuuid)) + { + env->ReleaseStringUTFChars(jData, str); + } + else + { + return OC_STACK_ERROR; + } + + jint perm = (jint)env->CallIntMethod(element, g_mid_OcOicSecAcl_ace_get_permissions); + tmp->permission = (uint16_t)perm; + if (nullptr == (tmp->resources = getResourcesList(env, element))) + { + return OC_STACK_ERROR; + } + + tmp->validities = NULL; //TODO Seems Validities CBOR conversion is broken in C stack +#if 0 + if (nullptr == (tmp->validities = getValiditiesList(env, element))) + { + return OC_STACK_ERROR; + } +#endif + if (NULL == acesHead) + { + acesHead = tmp; + } + else + { + OicSecAce_t *ptr = acesHead; + while(ptr->next != NULL) ptr = ptr->next; + ptr->next = tmp; + tmp->next = NULL; + } + } + acl->aces = acesHead; + return OC_STACK_OK; +} + +OCStackResult JniSecureUtils::convertJavaPdACLToOCAcl(JNIEnv *env, jobject in, OicSecPdAcl_t *pdacl) +{ + jstring jData; + jvalue args[1]; + + jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecPdAcl_get_resources_cnt); if (!jCount || env->ExceptionCheck()) { return OC_STACK_ERROR; } - acl->resourcesLen = jCount; - acl->resources = new char*[jCount]; + pdacl->resourcesLen = jCount; + pdacl->resources = new char*[jCount]; + + if (!pdacl->resources) + { + return OC_STACK_ERROR; + } for (jint i = 0; i < jCount; ++i) { args[0].i = i; - jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_resources, args); + jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecPdAcl_get_resources, args); if (!jData || env->ExceptionCheck()) { return OC_STACK_ERROR; } - acl->resources[i] = (char*) env->GetStringUTFChars(jData, 0); + pdacl->resources[i] = (char*) env->GetStringUTFChars(jData, 0); } - jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_permission); + jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecPdAcl_get_permission); if (env->ExceptionCheck()) { return OC_STACK_ERROR; } - acl->permission = jCount; - jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_periods_cnt); + pdacl->permission = jCount; + jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecPdAcl_get_periods_cnt); if (env->ExceptionCheck()) { return OC_STACK_ERROR; } - acl->prdRecrLen = jCount; - acl->periods = new char*[jCount]; + pdacl->prdRecrLen = jCount; + if (jCount) + { + pdacl->periods = new char*[jCount]; + if (!pdacl->periods) + { + return OC_STACK_ERROR; + } + } for (jint i = 0; i < jCount; ++i) { args[0].i = i; - jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_periods, args); + jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecPdAcl_get_periods, args); if (!jData || env->ExceptionCheck()) { return OC_STACK_ERROR; } - acl->periods[i] = (char*) env->GetStringUTFChars(jData, 0); + pdacl->periods[i] = (char*) env->GetStringUTFChars(jData, 0); } - acl->recurrences = new char*[jCount]; //TODO:Period Len and Reccurence len is same + if (jCount) + { + pdacl->recurrences = new char*[jCount]; + if (!pdacl->recurrences) + { + return OC_STACK_ERROR; + } + } for (jint i = 0; i < jCount; ++i) { args[0].i = i; - jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_recurrences, args); + jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecPdAcl_get_recurrences, args); if (!jData || env->ExceptionCheck()) { return OC_STACK_ERROR; } - acl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0); - } - - jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_rownerID); - if (!jData || env->ExceptionCheck()) - { - return OC_STACK_ERROR; - } - - str = (char*) env->GetStringUTFChars(jData, 0); - - if (OC_STACK_OK == ConvertStrToUuid(str, &acl->rownerID)) - { - env->ReleaseStringUTFChars(jData, str); - } - else - { - return OC_STACK_ERROR; + pdacl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0); } - return OC_STACK_OK; } diff --git a/android/android_api/base/jni/JniSecureUtils.h b/android/android_api/base/jni/JniSecureUtils.h index c239c44..c16457d 100644 --- a/android/android_api/base/jni/JniSecureUtils.h +++ b/android/android_api/base/jni/JniSecureUtils.h @@ -29,10 +29,12 @@ class JniSecureUtils static std::string convertUUIDtoStr(OicUuid_t uuid); static void convertStrToUUID(char *str, OicUuid_t &uuid); public: + static void FreeACLList(OicSecAcl_t *acl); static jobject convertProvisionresultVectorToJavaList(JNIEnv *, const OC::PMResultList_t *); static jobjectArray convertDeviceVectorToJavaArray(JNIEnv *env, std::vector>& deviceListVector); static jobject convertUUIDVectorToJavaStrList(JNIEnv *env, OC::UuidList_t &vector); static OCStackResult convertJavaACLToOCAcl(JNIEnv *env, jobject in, OicSecAcl_t *out); + static OCStackResult convertJavaPdACLToOCAcl(JNIEnv *env, jobject in, OicSecPdAcl_t *pdacl); }; diff --git a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java index 9645343..4842eca 100644 --- a/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java +++ b/android/android_api/base/src/androidTest/java/org/iotivity/base/SmokeTest.java @@ -25,6 +25,7 @@ package org.iotivity.base; import android.test.InstrumentationTestCase; import android.util.Log; +import java.util.Arrays; import java.util.Date; import java.util.EnumSet; import java.util.HashMap; @@ -1048,7 +1049,10 @@ public class SmokeTest extends InstrumentationTestCase { } }; - OcDeviceInfo devInfo = new OcDeviceInfo("myDeviceName"); + OcDeviceInfo devInfo = new OcDeviceInfo( + "myDeviceName", + Arrays.asList(new String[]{"oic.d.test"}) + ); try { //server diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java b/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java index 4a89856..062405f 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcDeviceInfo.java @@ -22,20 +22,27 @@ package org.iotivity.base; +import java.util.List; + /** - * This class is expected as input for device properties. Device name is mandatory and expected - * from the application. Device id of type UUID will be generated by the stack. + * This class is expected as input for device properties. Device name and types are mandatory + * and expected from the application. Device id of type UUID will be generated by the stack. */ public class OcDeviceInfo { private String mDeviceName; + private List mDeviceTypes; - public OcDeviceInfo(String deviceName) { - + public OcDeviceInfo(String deviceName, List deviceTypes) { this.mDeviceName = deviceName; + this.mDeviceTypes = deviceTypes; } public String getDeviceName() { return mDeviceName; } + + public List getDeviceTypes() { + return mDeviceTypes; + } } diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcDirectPairDevice.java b/android/android_api/base/src/main/java/org/iotivity/base/OcDirectPairDevice.java new file mode 100644 index 0000000..aff3238 --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcDirectPairDevice.java @@ -0,0 +1,113 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import java.util.List; +import java.util.EnumSet; +import java.util.Map; +import java.util.Arrays; +import java.util.ArrayList; + +public class OcDirectPairDevice { + + + private long mNativeHandle; + private String mdeviceID; + private String mHost; + + private OcDirectPairDevice(long nativeHandle) { + this.mNativeHandle = nativeHandle; + } + + public OcDirectPairDevice(String deviceID) + { + this.mdeviceID = deviceID; + } + + public String getDevId() + { + return this.mdeviceID; + } + + public native String getHost(); + + public List getPairingMethodList() { + int [] intList = this.getPairingMethods(); + List prmList = new ArrayList(); + for (int i = 0; i < intList.length; i++) + { + prmList.add(intList[i]); + } + return prmList; + } + + private native int[] getPairingMethods(); + + /** + * Method to get the connectivity type of this resource + * + * @return EnumSet connectivity type set + */ + public EnumSet getConnectivityTypeSet() { + return OcConnectivityType.convertToEnumSet( + this.getConnectivityTypeN() + ); + } + + private native int getConnectivityTypeN(); + + + /** + * Method to get list of all paired devices for a given device. + * + * @param GetDirectPairedListener Callback function, which will receive the list of direct paired devices. + * @throws OcException + */ + + public native void getDirectPairedDevices(GetDirectPairedListener getDirectPairedListener) throws OcException; + + public interface GetDirectPairedListener { + public void getDirectPairedListener(List ocPairedDeviceList); + } + + /** + * Method to perform direct pairing between two devices. + * + * @param DirectPairingListener Callback function, which will be called after + * completion of direct pairing. + * @throws OcException + */ + public native void doDirectPairing(OcDirectPairDevice peer, + OcPrmType pmSel, String pinNumber, + DirectPairingListener directPairingListener) throws OcException; + + + public interface DirectPairingListener { + public void directPairingListener(String devId, int result); + } + + @Override + public String toString() { + return this.mdeviceID; + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcHeaderOption.java b/android/android_api/base/src/main/java/org/iotivity/base/OcHeaderOption.java index d672bf0..6bf442f 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcHeaderOption.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcHeaderOption.java @@ -30,19 +30,30 @@ import java.security.InvalidParameterException; * After creating instances of OcHeaderOptions, use setHeaderOptions API * (in OcResource) to set header Options. * NOTE: optionId is an integer value which MUST be within - * range of 2048 to 3000 inclusive of lower and upper bound. + * range of 2048 to 3000 inclusive of lower and upper bound + * except for a few options including If-Match with empty(num : 1), + * If-None-Match(num : 5), Location-Path(num : 8), + * Location-Query(num : 20) CoAP option. * HeaderOption instance creation fails if above condition is not satisfied. */ public class OcHeaderOption { public static final int MIN_HEADER_OPTION_ID = 2048; public static final int MAX_HEADER_OPTION_ID = 3000; + public static final int IF_MATCH_OPTION_ID = 1; + public static final int IF_NONE_MATCH_OPTION_ID = 5; + public static final int LOCATION_PATH_OPTION_ID = 8; + public static final int LOCATION_QUERY_OPTION_ID = 20; private int mOptionId; private String mOptionData; public OcHeaderOption(int optionId, String optionData) { - if (!(optionId >= MIN_HEADER_OPTION_ID && optionId <= MAX_HEADER_OPTION_ID)) { + if (!(optionId >= MIN_HEADER_OPTION_ID && optionId <= MAX_HEADER_OPTION_ID) + && optionId != IF_MATCH_OPTION_ID + && optionId != IF_NONE_MATCH_OPTION_ID + && optionId != LOCATION_PATH_OPTION_ID + && optionId != LOCATION_QUERY_OPTION_ID) { throw new InvalidParameterException("Option ID range is invalid"); } diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java index c8678ff..76cd649 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcPlatform.java @@ -541,12 +541,16 @@ public final class OcPlatform { OcDeviceInfo ocDeviceInfo) throws OcException { OcPlatform.initCheck(); OcPlatform.registerDeviceInfo0( - ocDeviceInfo.getDeviceName() + ocDeviceInfo.getDeviceName(), + ocDeviceInfo.getDeviceTypes().toArray( + new String[ocDeviceInfo.getDeviceTypes().size()] + ) ); } private static native void registerDeviceInfo0( - String deviceName + String deviceName, + String[] deviceTypes ) throws OcException; /** @@ -897,6 +901,81 @@ public final class OcPlatform { throws OcException; /** + * Method to find all devices which are eligible for direct pairing and return the list. + * + * @param timeout timeout for discovering direct pair devices. + * @param FindDirectPairingListener Callback function, which will receive the list of direct + * pairable devices. + * @throws OcException + */ + public static native void findDirectPairingDevices(int timeout, + FindDirectPairingListener onFindDirectPairingListener) throws OcException; + + /** + * Method to get list of all paired devices for a given device. + * + * @param GetDirectPairedListener Callback function, which will receive the list of direct + * paired devices. + * @throws OcException + */ + public native void getDirectPairedDevices(GetDirectPairedListener onGetDirectPairedListener) + throws OcException; + + /** + * Method to perform direct pairing between two devices. + * + * @param peer Target peer + * @param prmType Pairing Method to be used for Pairing + * @param pin pin + * @param DirectPairingListener Callback function, which will be called after + * completion of direct pairing. + * @throws OcException + */ + public static void doDirectPairing( + OcDirectPairDevice peer, + OcPrmType prmType, + String pin, + DirectPairingListener onDirectPairingListener) throws OcException { + + OcPlatform.doDirectPairing0( + peer, + prmType.getValue(), + pin, + onDirectPairingListener + ); + } + + private static native void doDirectPairing0(OcDirectPairDevice peer, + int pmSel, String pinNumber, DirectPairingListener onDirectPairingListener) + throws OcException; + + /** + * An FindDirectPairingListener can be registered via the OcPlatform.findDirectPairingDevices call. + * Event listeners are notified asynchronously + */ + public interface FindDirectPairingListener { + public void onFindDirectPairingListener(List ocPairedDeviceList); + } + + /** + * Listerner to Get List of already Direct Paired devices. + * An GetDirectPairedListener can be registered via the OcPlatform.getDirectPairedDevices call. + * Event listeners are notified asynchronously + */ + public interface GetDirectPairedListener { + public void onGetDirectPairedListener(List ocPairedDeviceList); + } + + /** + * Listner to get result of doDirectPairing. + * An DirectPairingListener can be registered via the OcPlatform.doDirectPairing call. + * Event listeners are notified asynchronously + */ + public interface DirectPairingListener { + public void onDirectPairingListener(String devId, int result); + } + + /** * An OnResourceFoundListener can be registered via the OcPlatform.findResource call. * Event listeners are notified asynchronously */ diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcPrmType.java b/android/android_api/base/src/main/java/org/iotivity/base/OcPrmType.java new file mode 100755 index 0000000..2c2b37f --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcPrmType.java @@ -0,0 +1,58 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ +/** + * @brief direct pairing Method Type. + * 0: not allowed + * 1: pre-configured pin + * 2: random pin + */ +package org.iotivity.base; + +public enum OcPrmType { + DP_INVALID (-1), + DP_NOT_ALLOWED (0), + DP_PRE_CONFIGURED (1 << 0), + DP_RANDOM_PIN (1 << 1); + + private int value; + + private OcPrmType(int value) { + this.value = value; + } + + public int getValue() { + return this.value; + } + + public static OcPrmType convertOcPrmType(int value) { + switch (value) + { + case 0: + return OcPrmType.DP_NOT_ALLOWED; + case 1: + return OcPrmType.DP_PRE_CONFIGURED; + case 2: + return OcPrmType.DP_RANDOM_PIN; + } + return OcPrmType.DP_INVALID; + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OcSecureResource.java b/android/android_api/base/src/main/java/org/iotivity/base/OcSecureResource.java index 218b9e2..b1daf6c 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OcSecureResource.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OcSecureResource.java @@ -131,6 +131,46 @@ public class OcSecureResource { ProvisionPairwiseDevicesListener provisionPairwiseDevicesListener) throws OcException; /** + * Method to configure resource for direct pairing + * + * @param pin pin number + * @param pdacls Array of Device Pairing Access Control List + * @param type List of supported OcPrmType + * @param edp enable (1) / disable (0) + * @param ProvisionDirectPairing Callback function, which will be called after completion + * of Direct Pairing. + * @throws OcException + */ + + public void doProvisionDirectPairing(String pin, OicSecPdAcl[] pdacls, List type, + boolean edp , ProvisionDirectPairingListener provisionDirectPairingListener) + throws OcException { + + int[] typeArray = new int[type.size()]; + int i = 0; + for (OcPrmType ocPrmType:type) { + typeArray[i++] = ocPrmType.getValue(); + } + + this.provisionDirectPairing(pin, pdacls, typeArray, (edp?1:0), + provisionDirectPairingListener); + } + + private native void provisionDirectPairing(String pin, OicSecPdAcl[] pdacls, int[] type, + int edp , ProvisionDirectPairingListener provisionDirectPairingListener) + throws OcException; + + /** + * provisionDirectPairingListener can be registered with doOwnershipTransfer + * call. + * Listener notified asynchronously. + */ + public interface ProvisionDirectPairingListener { + public void provisionDirectPairingListener(List provisionResultList, + int hasError); + } + + /** * doOwnershipTransferListener can be registered with doOwnershipTransfer * call. * Listener notified asynchronously. diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OicSecAce.java b/android/android_api/base/src/main/java/org/iotivity/base/OicSecAce.java new file mode 100644 index 0000000..a5bd53f --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OicSecAce.java @@ -0,0 +1,80 @@ +/* + ******************************************************************* + * + * Copyright 2016 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. + * + *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import java.util.List; +public class OicSecAce { + private String subjectID; + private int permission; + private List resources; + private List validities; + + public OicSecAce(String subjectID, int permission, + List resources, List validities) { + super(); + this.subjectID = subjectID; + this.permission = permission; + this.resources = resources; + this.validities = validities; + } + + public String getSubjectID() { + return subjectID; + } + + public void setSubjectID(String subjectID) { + this.subjectID = subjectID; + } + + public int getPermission() { + return permission; + } + + public void setPermission(int permission) { + this.permission = permission; + } + + public List getResourcesList() { + return resources; + } + + public OicSecResr[] getResources() { + return resources.toArray(new OicSecResr[resources.size()]); + } + public void setResources(List resources) { + this.resources = resources; + } + + public List getValiditiesList() { + return validities; + } + + public OicSecValidity[] getValidities() { + return validities.toArray(new OicSecValidity[validities.size()]); + } + + + public void setValidities(List validities) { + this.validities = validities; + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OicSecAcl.java b/android/android_api/base/src/main/java/org/iotivity/base/OicSecAcl.java index a47ffaa..f0fdc72 100644 --- a/android/android_api/base/src/main/java/org/iotivity/base/OicSecAcl.java +++ b/android/android_api/base/src/main/java/org/iotivity/base/OicSecAcl.java @@ -1,116 +1,55 @@ -/* - * //****************************************************************** - * // - * // 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. - * // - * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= - */ - -package org.iotivity.base; - -import java.io.Serializable; -import java.util.List; -import java.util.Arrays; - -public class OicSecAcl implements Serializable { - - private String subject; - private int permission; - private List resources; - private List periods; - private List recurrences; - private String rownerID; - - public OicSecAcl(String subject, List recurrences, List periods, int permission, - List resources, String rownerID) { - this.subject = subject; - this.recurrences = recurrences; - this.periods = periods; - this.permission = permission; - this.resources = resources; - this.rownerID = rownerID; - } - - public String getSubject() { - return this.subject; - } - - public void setSubject(String subject) { - this.subject = subject; - } - - - public void setRownerID(String rownerID) { - this.rownerID = rownerID; - } - - public List getRecurrences() { - return recurrences; - } - - public void setRecurrences(List recurrences) { - this.recurrences = recurrences; - } - - public List getPeriods() { - return periods; - } - - public void setPeriods(List periods) { - this.periods = periods; - } - - public int getPermission() { - return this.permission; - } - - public void setPermission(int permission) { - this.permission = permission; - } - - public List getResources() { - return resources; - } - - public void setResources(List resources) { - this.resources = resources; - } - - public int getResourcesCount() { - return this.resources.size(); - } - - public String getResources(int i) { - return this.resources.get(i); - } - - public int getPeriodsCount() { - return this.periods.size(); - } - - public String getPeriods(int i) { - return this.periods.get(i); - } - - public String getRecurrences(int i) { - return this.recurrences.get(i); - } - - public String getRownerID() { - return this.rownerID; - } -} +/* + ******************************************************************* + * + * Copyright 2016 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. + * + *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import java.util.List; + +public class OicSecAcl { + private String rownerID; + private List oicSecAces; + + public OicSecAcl (String rownerID, List oicSecAces) { + super(); + this.rownerID = rownerID; + this.oicSecAces = oicSecAces; + } + public String getRownerID() { + return rownerID; + } + + public void setRownerID(String rownerID) { + this.rownerID = rownerID; + } + + public List getOicSecAcesList() { + return oicSecAces; + } + + public OicSecAce[] getOicSecAces() { + return oicSecAces.toArray(new OicSecAce[oicSecAces.size()]); + } + + public void setOicSecAces(List oicSecAces) { + this.oicSecAces = oicSecAces; + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OicSecPdAcl.java b/android/android_api/base/src/main/java/org/iotivity/base/OicSecPdAcl.java new file mode 100644 index 0000000..5768123 --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OicSecPdAcl.java @@ -0,0 +1,95 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import java.io.Serializable; +import java.util.List; +import java.util.Arrays; + +public class OicSecPdAcl implements Serializable { + + private int mPermission; + private List mResources; + private List mPeriods; + private List mRecurrences; + + public OicSecPdAcl(List recurrences, List periods, int permission, + List resources) { + this.mRecurrences = recurrences; + this.mPeriods = periods; + this.mPermission = permission; + this.mResources = resources; + } + + public List getRecurrences() { + return mRecurrences; + } + + public void setRecurrences(List recurrences) { + this.mRecurrences = recurrences; + } + + public List getPeriods() { + return mPeriods; + } + + public void setPeriods(List periods) { + this.mPeriods = periods; + } + + public int getPermission() { + return this.mPermission; + } + + public void setPermission(int permission) { + this.mPermission = permission; + } + + public List getResources() { + return mResources; + } + + public void setResources(List resources) { + this.mResources = resources; + } + + public int getResourcesCount() { + return this.mResources.size(); + } + + public String getResources(int i) { + return this.mResources.get(i); + } + + public int getPeriodsCount() { + return this.mPeriods.size(); + } + + public String getPeriods(int i) { + return this.mPeriods.get(i); + } + + public String getRecurrences(int i) { + return this.mRecurrences.get(i); + } +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OicSecResr.java b/android/android_api/base/src/main/java/org/iotivity/base/OicSecResr.java new file mode 100644 index 0000000..490462e --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OicSecResr.java @@ -0,0 +1,102 @@ +/* + ******************************************************************* + * + * Copyright 2016 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. + * + *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import java.util.List; +public class OicSecResr { + + private String href; + private String rel; + private List types; + private int typeLen; + private List interfaces; + private int interfaceLen; + + public OicSecResr(String href, String rel, List types, int typeLen, + List interfaces, int interfaceLen) { + super(); + this.href = href; + this.rel = rel; + this.types = types; + this.typeLen = typeLen; + this.interfaces = interfaces; + this.interfaceLen = interfaceLen; + } + + public String getHref() { + return href; + } + + public void setHref(String href) { + this.href = href; + } + + public String getRel() { + return rel; + } + + public void setRel(String rel) { + this.rel = rel; + } + + public List getTypesList() { + return types; + } + + public void setTypes(List types) { + this.types = types; + } + + public int getTypeLen() { + return typeLen; + } + + public void setTypeLen(int typeLen) { + this.typeLen = typeLen; + } + + public List getInterfacesList() { + return interfaces; + } + + public String getInterfaces(int i) { + return this.interfaces.get(i); + } + + public String getTypes(int i) { + return this.types.get(i); + } + + public void setInterfaces(List interfaces) { + this.interfaces = interfaces; + } + + public int getInterfaceLen() { + return interfaceLen; + } + + public void setInterfaceLen(int interfaceLen) { + this.interfaceLen = interfaceLen; + } + +} diff --git a/android/android_api/base/src/main/java/org/iotivity/base/OicSecValidity.java b/android/android_api/base/src/main/java/org/iotivity/base/OicSecValidity.java new file mode 100644 index 0000000..0846658 --- /dev/null +++ b/android/android_api/base/src/main/java/org/iotivity/base/OicSecValidity.java @@ -0,0 +1,67 @@ +/* + ******************************************************************* + * + * Copyright 2016 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. + * + *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base; + +import java.util.List; +public class OicSecValidity { + private String period; + private List recurrences; + private int recurrenceLen; + + public OicSecValidity(String period, List recurrences, + int recurrenceLen) { + super(); + this.period = period; + this.recurrences = recurrences; + this.recurrenceLen = recurrenceLen; + } + + public String getPeriod() { + return period; + } + + public void setPeriod(String period) { + this.period = period; + } + + public List getRecurrencesList() { + return recurrences; + } + + public String getRecurrences(int i) { + return this.recurrences.get(i); + } + + public void setRecurrences(List recurrences) { + this.recurrences = recurrences; + } + + public int getRecurrenceLen() { + return recurrenceLen; + } + + public void setRecurrenceLen(int recurrenceLen) { + this.recurrenceLen = recurrenceLen; + } + +} diff --git a/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java b/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java index 81ab7d5..2314df9 100644 --- a/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java +++ b/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java @@ -73,16 +73,16 @@ public class CaIpInterface { private static BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, - WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_DISABLED) { - caIpStateDisabled(); - } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { + if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager manager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo nwInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); + NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); - if(nwInfo.isConnected()) { + if (mobileInfo != null && mobileInfo.isConnected() || wifiInfo.isConnected()) { caIpStateEnabled(); + } else { + caIpStateDisabled(); } } diff --git a/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java b/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java index 0801bdb..8b383a7 100644 --- a/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java +++ b/android/android_api/base/src/main/java/org/iotivity/ca/CaLeClientInterface.java @@ -41,7 +41,7 @@ import android.util.Log; public class CaLeClientInterface { private static String SERVICE_UUID = "ADE3D529-C784-4F63-A987-EB69F70EE816"; - private static String TAG = "Sample_Service : CaLeClientInterface"; + private static String TAG = "OIC_LE_CB_INTERFACE"; private static Context mContext; private CaLeClientInterface(Context context) { diff --git a/android/examples/DirectPairing/.classpath b/android/examples/DirectPairing/.classpath new file mode 100644 index 0000000..26bdfa6 --- /dev/null +++ b/android/examples/DirectPairing/.classpath @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/android/examples/DirectPairing/.project b/android/examples/DirectPairing/.project new file mode 100644 index 0000000..2dadd42 --- /dev/null +++ b/android/examples/DirectPairing/.project @@ -0,0 +1,33 @@ + + + Iot + + + + + + com.android.ide.eclipse.adt.ResourceManagerBuilder + + + + + com.android.ide.eclipse.adt.PreCompilerBuilder + + + + + org.eclipse.jdt.core.javabuilder + + + + + com.android.ide.eclipse.adt.ApkBuilder + + + + + + com.android.ide.eclipse.adt.AndroidNature + org.eclipse.jdt.core.javanature + + diff --git a/android/examples/DirectPairing/DirectPairing.iml b/android/examples/DirectPairing/DirectPairing.iml new file mode 100644 index 0000000..5de258a --- /dev/null +++ b/android/examples/DirectPairing/DirectPairing.iml @@ -0,0 +1,96 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/examples/DirectPairing/build.gradle b/android/examples/DirectPairing/build.gradle new file mode 100644 index 0000000..8499e31 --- /dev/null +++ b/android/examples/DirectPairing/build.gradle @@ -0,0 +1,38 @@ +apply plugin: 'com.android.application' + +android { + compileSdkVersion 21 + buildToolsVersion "20.0.0" + + defaultConfig { + applicationId "org.iotivity.base.examples.DirectPairing" + minSdkVersion 21 + targetSdkVersion 21 + versionCode 1 + versionName "1.0" + } + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + } + } + lintOptions { + abortOnError false + } +} +repositories { + flatDir { + dirs "../../android_api/base/build/outputs/aar/" + } +} + +try { + dependencies { + compile ":iotivity-base-${TARGET_ARCH}-${RELEASE}@aar" + } +} catch (all) { + print "${ERROR_MSG}" + assert all +} + diff --git a/android/examples/DirectPairing/project.properties b/android/examples/DirectPairing/project.properties new file mode 100644 index 0000000..8e02666 --- /dev/null +++ b/android/examples/DirectPairing/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-23 +android.library.reference.1=../appcompat_v7 diff --git a/android/examples/DirectPairing/src/main/AndroidManifest.xml b/android/examples/DirectPairing/src/main/AndroidManifest.xml new file mode 100644 index 0000000..54ec6b9 --- /dev/null +++ b/android/examples/DirectPairing/src/main/AndroidManifest.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + diff --git a/android/examples/DirectPairing/src/main/assets/oic_svr_db_client_directpairing.dat b/android/examples/DirectPairing/src/main/assets/oic_svr_db_client_directpairing.dat new file mode 100644 index 0000000..43b7a1c Binary files /dev/null and b/android/examples/DirectPairing/src/main/assets/oic_svr_db_client_directpairing.dat differ diff --git a/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/ExpandableListAdapter.java b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/ExpandableListAdapter.java new file mode 100644 index 0000000..377917c --- /dev/null +++ b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/ExpandableListAdapter.java @@ -0,0 +1,137 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ + +package org.iotivity.base.examples.DirectPairing; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseExpandableListAdapter; +import android.widget.ImageView; +import android.widget.TextView; + +import org.iotivity.base.examples.DirectPairing.Led; + +import java.util.List; + +public class ExpandableListAdapter extends BaseExpandableListAdapter { + private List mGroupList; + private List mChilditemsList; + private Context mContext; + + public ExpandableListAdapter(List groupList, List childitemsList, + Context context) { + super(); + this.mContext = context; + this.mGroupList = groupList; + this.mChilditemsList = childitemsList; + } + + @Override + public int getGroupCount() { + return mGroupList.size(); + } + + @Override + public int getChildrenCount(int groupPosition) { + return 1; + } + + @Override + public Object getGroup(int groupPosition) { + return mGroupList.get(groupPosition); + } + + @Override + public Object getChild(int groupPosition, int childPosition) { + return this.mChilditemsList.get(childPosition); + } + + @Override + public long getGroupId(int groupPosition) { + return groupPosition; + } + + @Override + public long getChildId(int groupPosition, int childPosition) { + return childPosition; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public View getGroupView(int groupPosition, boolean isExpanded, + View convertView, ViewGroup parent) { + + String data = (String) getGroup(groupPosition); + if (convertView == null) { + LayoutInflater infalInflater = (LayoutInflater) this.mContext + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = infalInflater.inflate(R.layout.list_group, null); + } + + ImageView img = (ImageView) convertView.findViewById(R.id.image); + if (isExpanded) { + img.setImageResource(R.drawable.arrow_down); + + } else { + img.setImageResource(R.drawable.arrow_right); + } + + TextView groupText = (TextView) convertView.findViewById(R.id.title); + groupText.setText(mGroupList.get(groupPosition)); + + return convertView; + } + + @Override + public View getChildView(int groupPosition, int childPosition, boolean isLastChild, + View convertView, ViewGroup parent) { + LayoutInflater inflater = (LayoutInflater) mContext + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.list_item, parent, false); + + Led ledResource = (Led) getChild(groupPosition, childPosition); + TextView textItem = (TextView) convertView + .findViewById(R.id.textViewItem); + TextView textSource = (TextView) convertView + .findViewById(R.id.textViewSource); + TextView textStatus = (TextView) convertView + .findViewById(R.id.textViewStatus); + textStatus.setText("URI:" + "/a/led"); + textItem.setText("State: " + ledResource.getState()); + textSource.setText("Power: " + ledResource.getPower()); + + return convertView; + + } + + @Override + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; + } + +} diff --git a/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/Led.java b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/Led.java new file mode 100644 index 0000000..f44f97c --- /dev/null +++ b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/Led.java @@ -0,0 +1,66 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ +package org.iotivity.base.examples.DirectPairing; + +/** + * Led + *

+ * This class is used by simpleclient to create an object and update the values depending + * on the server response + */ +public class Led { + private boolean mState; + private int mPower; + private String mUri; + + // constructor + public Led() { + mState = false; + mPower = 0; + mUri = ""; + } + + // accessor methods + public boolean getState() { + return mState; + } + + public void setState(boolean state) { + mState = state; + } + + public int getPower() { + return mPower; + } + + public void setPower(int power) { + mPower = power; + } + + public String getUri() { + return mUri; + } + + public void setUri(String uri) { + mUri = uri; + } +} diff --git a/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/MainActivity.java b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/MainActivity.java new file mode 100644 index 0000000..90c4301 --- /dev/null +++ b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/MainActivity.java @@ -0,0 +1,534 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ +package org.iotivity.base.examples.DirectPairing; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.content.SharedPreferences; +import android.graphics.Color; +import android.os.AsyncTask; +import android.os.Bundle; +import android.os.Environment; +import android.preference.PreferenceManager; +import android.text.InputFilter; +import android.util.Log; +import android.view.View; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.CompoundButton; +import android.widget.EditText; +import android.widget.ExpandableListView; +import android.widget.ExpandableListView.OnGroupExpandListener; +import android.widget.LinearLayout; +import android.widget.ListView; +import android.widget.Switch; +import android.widget.Toast; + +import org.iotivity.base.ModeType; +import org.iotivity.base.OcDirectPairDevice; +import org.iotivity.base.OcException; +import org.iotivity.base.OcHeaderOption; +import org.iotivity.base.OcPlatform; +import org.iotivity.base.OcPrmType; +import org.iotivity.base.OcProvisioning; +import org.iotivity.base.OcRepresentation; +import org.iotivity.base.OcResource; +import org.iotivity.base.PlatformConfig; +import org.iotivity.base.QualityOfService; +import org.iotivity.base.ServiceType; +import org.iotivity.base.examples.DirectPairing.Led; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +public class MainActivity extends Activity { + private static final int BUFFER_SIZE = 1024; + private static final String TAG = "DirectPairing: "; + private static int selected = -1; + private int[] pList; + private Button save; + private int prmType; + private EditText pinText; + private Led ledResource; + private ListView mListView; + private String filePath = ""; + private OcResource led = null; + private Switch discoverButton; + private List resourceInterfaces; + private ArrayAdapter arrayAdapter; + private List onGetResponseList; + private ExpandableListAdapter exlistAdapter; + OcResource.OnGetListener onGetListener = new OcResource.OnGetListener() { + @Override + public synchronized void onGetCompleted(List headerOptionList, + OcRepresentation ocRepresentation) { + onGetResponseList = new ArrayList(); + ledResource.setState(ocRepresentation.getValueBool("state")); + ledResource.setPower(ocRepresentation.getValueInt("power")); + ledResource.setUri(ocRepresentation.getUri()); + runOnUiThread(new Runnable() { + public void run() { + onGetResponseList.add(ledResource); + exlistAdapter.notifyDataSetChanged(); + } + }); + + Log.d(TAG, "Got a response from " + ocRepresentation.getUri() + + " " + ocRepresentation.getValueBool("state") + " " + + ocRepresentation.getValueInt("power") + " " + + ocRepresentation.getValueString("name")); + } + + @Override + public synchronized void onGetFailed(Throwable throwable) { + Log.d(TAG, "GET request has failed"); + Log.e(TAG, throwable.toString()); + } + }; + private ExpandableListView mPairedListDisplay; + private ArrayAdapter adapter; + private List pairedList; + private List discoveredListObj; + OcPlatform.DirectPairingListener doDPListener = new OcPlatform.DirectPairingListener() { + @Override + public void onDirectPairingListener(String devId, int result) { + Log.d(TAG, "Inside getDPListener"); + if (result == StringConstants.SUCCES_CODE) { + pairedList.clear(); + pairedList.add(devId); + onGetResponseList.add(new Led()); + runOnUiThread(new Runnable() { + public void run() { + mPairedListDisplay.setAdapter(exlistAdapter); + int pos = mListView.getCheckedItemPosition(); + if (pos != ListView.INVALID_POSITION && discoveredListObj.size() != 0) { + discoveredListObj.remove(pos); + adapter.notifyDataSetChanged(); + } + } + }); + Log.d(TAG, "direct pair successfull for " + devId); + } else { + Log.d(TAG, "direct pairing failed"); + runOnUiThread(new Runnable() { + public void run() { + Toast.makeText(getApplicationContext(), + "Direct Pairing Failed", Toast.LENGTH_SHORT).show(); + } + }); + } + exportCBOR(); + } + }; + private List discoveredPairableDevices; + OcPlatform.FindDirectPairingListener finddirectPairing = + new OcPlatform.FindDirectPairingListener() { + @Override + public void onFindDirectPairingListener(List direcPairingDevice) { + Log.d(TAG, "find direct pairable successfull "); + discoveredListObj.clear(); + discoveredPairableDevices = + new ArrayList(direcPairingDevice); + for (int i = 0; i < direcPairingDevice.size(); i++) { + OcDirectPairDevice dpPairDevice = direcPairingDevice.get(i); + Log.d(TAG, "getHost from Find Direct Pairing " + dpPairDevice.getHost()); + discoveredListObj.add(dpPairDevice); + } + + runOnUiThread(new Runnable() { + public void run() { + mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); + mListView.setAdapter(adapter); + discoverButton.setChecked(false); + } + }); + } + }; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + onGetResponseList = new ArrayList(); + discoveredListObj = new ArrayList(); + pairedList = new ArrayList(); + + ledResource = new Led(); + + mListView = (ListView) findViewById(R.id.list_view); + discoverButton = (Switch) findViewById(R.id.discover); + mPairedListDisplay = (ExpandableListView) findViewById(R.id.list_view_paired_list); + + + filePath = getFilesDir().getPath() + "/"; // data/data//files/ + //copy CBOR file when application runs first time + SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this); + boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true); + if (isFirstRun) { + copyCborFromAsset(); + SharedPreferences.Editor editor = wmbPreference.edit(); + editor.putBoolean("FIRSTRUN", false); + editor.commit(); + } + + initOICStack(); + + adapter = new ArrayAdapter(this, + android.R.layout.simple_list_item_single_choice, + discoveredListObj); + + arrayAdapter = new ArrayAdapter( + MainActivity.this, + android.R.layout.select_dialog_singlechoice); + + save = (Button) findViewById(R.id.saveDiscovered); + + save.setEnabled(false); + save.setBackgroundColor(Color.GRAY); + + resourceInterfaces = new LinkedList<>(); + resourceInterfaces.add(OcPlatform.DEFAULT_INTERFACE); + + exlistAdapter = new ExpandableListAdapter(pairedList, onGetResponseList, + MainActivity.this); + mPairedListDisplay.setAdapter(exlistAdapter); + + // to expand only a single group item. + mPairedListDisplay.setOnGroupExpandListener(new OnGroupExpandListener() { + int previousGroup = -1; + + @Override + public void onGroupExpand(int groupPosition) { + Log.d(TAG, "Calling GET api on mResource"); + try { + if (null != led) led.get(new HashMap(), onGetListener); + } catch (OcException e) { + Log.d(TAG, "Error in GET calls"); + Log.e(TAG, e.getMessage()); + } + + if (groupPosition != previousGroup) + mPairedListDisplay.collapseGroup(previousGroup); + + previousGroup = groupPosition; + } + }); + + save.setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View v) { + + AlertDialog.Builder alertPinDialog = + new AlertDialog.Builder(MainActivity.this); + alertPinDialog.setTitle("PIN"); + alertPinDialog.setMessage("Enter Pin"); + + pinText = new EditText(MainActivity.this); + int maxLength = 8; + InputFilter[] fArray = new InputFilter[1]; + fArray[0] = new InputFilter.LengthFilter(maxLength); + pinText.setFilters(fArray); + pinText.setMaxLines(1); + LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( + LinearLayout.LayoutParams.MATCH_PARENT, + LinearLayout.LayoutParams.MATCH_PARENT); + pinText.setLayoutParams(lp); + alertPinDialog.setView(pinText); + alertPinDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + selected = -1; + final AlertDialog alertPrmTypeDialog = new AlertDialog.Builder + (MainActivity.this) + .setSingleChoiceItems(arrayAdapter, -1, + new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + selected = which; + prmType = pList[which]; + Log.d(TAG, " prmType " + prmType); + } + }) + .setTitle("Select Direct Pair Method") + .setPositiveButton("OK", null) + .setNegativeButton("CANCEL", null) + .create(); + + alertPrmTypeDialog.setOnShowListener( + new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + Button ok = + alertPrmTypeDialog + .getButton(AlertDialog.BUTTON_POSITIVE); + + Button cancel = + alertPrmTypeDialog + .getButton(AlertDialog.BUTTON_NEGATIVE); + + ok.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + int pos = mListView.getCheckedItemPosition(); + if (selected == -1) { + Toast.makeText(getApplicationContext(), + "Please Select A Value", + Toast.LENGTH_SHORT).show(); + } else if (pos != ListView.INVALID_POSITION) { + if (!discoveredListObj.isEmpty()) { + try { + OcDirectPairDevice peer = + (OcDirectPairDevice) + discoveredListObj. + get(pos); + OcPlatform.doDirectPairing(peer, + OcPrmType.convertOcPrmType(prmType), + pinText.getText().toString(), + doDPListener); + } catch (OcException e) { + Log.d(TAG, "do Direct Pairing error: " + + e.getMessage()); + Log.e(TAG, e.getMessage()); + } + + alertPrmTypeDialog.dismiss(); + } else { + alertPrmTypeDialog.dismiss(); + } + } + } + }); + + cancel.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + alertPrmTypeDialog.dismiss(); + } + }); + } + }); + + alertPrmTypeDialog.show(); + } + }); + alertPinDialog.show(); + } + }); + + mListView.setOnItemClickListener(new OnItemClickListener() { + @Override + public void onItemClick(AdapterView adapt, View view, int position, long itemId) { + save.setEnabled(true); + save.setBackgroundColor(Color.parseColor("#13AEF4")); + OcDirectPairDevice dpPairDevice = + (OcDirectPairDevice) adapt.getItemAtPosition(position); + Log.d(TAG, "DevId On List Item Clicked " + dpPairDevice.getDevId()); + List pTypeList = dpPairDevice.getPairingMethodList(); + pList = new int[pTypeList.size()]; + Iterator iterator = pTypeList.iterator(); + for(int k = 0; k < pList.length; k++){ + pList[k]=iterator.next().intValue(); + } + arrayAdapter.clear(); + for (int j = 0; j < pList.length; j++) { + Log.d(TAG, " List Item Value " + pList[j]); + OcPrmType prmTy = OcPrmType.convertOcPrmType(pList[j]); + arrayAdapter.add(prmTy.name()); + } + } + }); + + discoverButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + save.setEnabled(false); + save.setBackgroundColor(Color.GRAY); + new GetDiscoveredItems().execute(); + } + } + }); + + } + + /** + * Copy svr db CBOR dat file from assets folder to app data files dir + */ + private void copyCborFromAsset() { + InputStream inputStream = null; + OutputStream outputStream = null; + int length; + byte[] buffer = new byte[BUFFER_SIZE]; + try { + inputStream = getAssets().open(StringConstants.OIC_CLIENT_CBOR_DB_FILE); + File file = new File(filePath); + //check files directory exists + if (!(file.exists() && file.isDirectory())) { + file.mkdirs(); + } + outputStream = new FileOutputStream(filePath + + StringConstants.OIC_CLIENT_CBOR_DB_FILE); + while ((length = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, length); + } + } catch (NullPointerException e) { + Log.d(TAG, "Null pointer exception " + e.getMessage()); + Log.e(TAG, e.getMessage()); + } catch (FileNotFoundException e) { + Log.d(TAG, "CBOR svr db file not found " + e.getMessage()); + Log.e(TAG, e.getMessage()); + } catch (IOException e) { + Log.d(TAG, StringConstants.OIC_CLIENT_CBOR_DB_FILE + " file copy failed"); + Log.e(TAG, e.getMessage()); + } finally { + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + Log.e(TAG, e.getMessage()); + } + } + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + Log.e(TAG, e.getMessage()); + } + } + } + } + + void exportCBOR() { + try { + File sd = Environment.getExternalStorageDirectory(); + File data = Environment.getDataDirectory(); + if (sd.canWrite()) { + String currentDBPath = "/data/data/" + getPackageName() + + "/files/" + StringConstants.OIC_CLIENT_CBOR_DB_FILE; + File currentDB = new File(currentDBPath); + File backupDB = new File(sd, StringConstants.OIC_CLIENT_CBOR_DB_FILE); + if (currentDB.exists()) { + FileChannel src = new FileInputStream(currentDB).getChannel(); + FileChannel dst = new FileOutputStream(backupDB).getChannel(); + dst.transferFrom(src, 0, src.size()); + src.close(); + dst.close(); + } + } + } catch (Exception e) { + Log.e(TAG, e.getMessage()); + } + } + + /** + * configure OIC platform and call findResource + */ + private void initOICStack() { + //create platform config + PlatformConfig cfg = new PlatformConfig(this, ServiceType.IN_PROC, ModeType.CLIENT_SERVER, + "0.0.0.0", // bind to all available interfaces + 0, + QualityOfService.LOW, filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE); + OcPlatform.Configure(cfg); + try { + /* + * Initialize DataBase + */ + String sqlDbPath = getFilesDir().getAbsolutePath().replace("files", "databases") + + File.separator; + File file = new File(sqlDbPath); + //check files directory exists + if (!(file.isDirectory())) { + file.mkdirs(); + Log.d(TAG, "Sql db directory created at " + sqlDbPath); + } + Log.d(TAG, "Sql db directory exists at " + sqlDbPath); + OcProvisioning.provisionInit(sqlDbPath + StringConstants.OIC_SQL_DB_FILE); + } catch (OcException e) { + Log.d(TAG, "provisionInit error: " + e.getMessage()); + Log.e(TAG, e.getMessage()); + } + } + + class GetDiscoveredItems extends AsyncTask> { + + ProgressDialog pDialog; + + @Override + protected void onPreExecute() { + pDialog = new ProgressDialog(MainActivity.this); + pDialog.setMessage("Getting Discovered Items"); + pDialog.setCancelable(false); + pDialog.setCanceledOnTouchOutside(false); + pDialog.show(); + } + + ; + + @Override + protected List doInBackground(Void... params) { + try { + OcPlatform.findDirectPairingDevices(5, finddirectPairing); + } catch (OcException e) { + Log.e(TAG, e.getMessage()); + } + return null; + } + + @Override + protected void onPostExecute(List discoveredItems) { + pDialog.dismiss(); + for (Iterator iterator = discoveredListObj.iterator(); iterator.hasNext(); ) { + OcDirectPairDevice ledResource = (OcDirectPairDevice) iterator.next(); + List lightTypes = new LinkedList<>(); + lightTypes.add("core.led"); + try { + Log.d(TAG, "Constructing Led Resource"); + led = OcPlatform.constructResourceObject(ledResource.getHost(), + "/a/led", ledResource.getConnectivityTypeSet(), + false, //isObservable + lightTypes, + resourceInterfaces); + Log.d(TAG, "Constructed Led Resource"); + } catch (OcException e) { + Log.e(TAG, e.getMessage()); + } + } + } + + } +} diff --git a/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/StringConstants.java b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/StringConstants.java new file mode 100644 index 0000000..c793422 --- /dev/null +++ b/android/examples/DirectPairing/src/main/java/org/iotivity/base/examples/DirectPairing/StringConstants.java @@ -0,0 +1,40 @@ +/* + * //****************************************************************** + * // + * // Copyright 2016 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. + * // + * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= + */ +package org.iotivity.base.examples.DirectPairing; + +public interface StringConstants { + + public static final int NUMBER_ZERO = 0; + public static final int DISCOVERY_TIMEOUT_10 = 10; + public static final int DISCOVERY_TIMEOUT_20 = 20; + public static final int SUCCES_CODE = 0; + public static final String DEFAULT_ROWNER_ID = "61646d69-6e44-6576-6963-655555494430"; + public static final String DEFAULT_RESOURCES = "*"; + public static final String DEFAULT_RECURRENCES = "Daily"; + public static final String DEFAULT_PERIOD = "12-12-16"; + public static final int DEFAULT_PERMISSION = 31; + public static final String OIC_CLIENT_CBOR_DB_FILE = "oic_svr_db_client_directpairing.dat"; + public static final String MESSAGE = "message"; + public static final String OIC_SQL_DB_FILE = "Pdm.db"; + public static final int CREDENTIAL_TYPE = 1; + public static final int OWNER_PSK_LENGTH_128 = 128 / 8; +} diff --git a/android/examples/DirectPairing/src/main/res/drawable-hdpi/arrow_down.png b/android/examples/DirectPairing/src/main/res/drawable-hdpi/arrow_down.png new file mode 100644 index 0000000..2e69816 Binary files /dev/null and b/android/examples/DirectPairing/src/main/res/drawable-hdpi/arrow_down.png differ diff --git a/android/examples/DirectPairing/src/main/res/drawable-hdpi/arrow_right.png b/android/examples/DirectPairing/src/main/res/drawable-hdpi/arrow_right.png new file mode 100644 index 0000000..91667d5 Binary files /dev/null and b/android/examples/DirectPairing/src/main/res/drawable-hdpi/arrow_right.png differ diff --git a/android/examples/DirectPairing/src/main/res/drawable-hdpi/ic_launcher.png b/android/examples/DirectPairing/src/main/res/drawable-hdpi/ic_launcher.png new file mode 100644 index 0000000..288b665 Binary files /dev/null and b/android/examples/DirectPairing/src/main/res/drawable-hdpi/ic_launcher.png differ diff --git a/android/examples/DirectPairing/src/main/res/drawable-mdpi/ic_launcher.png b/android/examples/DirectPairing/src/main/res/drawable-mdpi/ic_launcher.png new file mode 100644 index 0000000..6ae570b Binary files /dev/null and b/android/examples/DirectPairing/src/main/res/drawable-mdpi/ic_launcher.png differ diff --git a/android/examples/DirectPairing/src/main/res/drawable-xhdpi/ic_launcher.png b/android/examples/DirectPairing/src/main/res/drawable-xhdpi/ic_launcher.png new file mode 100644 index 0000000..d4fb7cd Binary files /dev/null and b/android/examples/DirectPairing/src/main/res/drawable-xhdpi/ic_launcher.png differ diff --git a/android/examples/DirectPairing/src/main/res/drawable-xxhdpi/ic_launcher.png b/android/examples/DirectPairing/src/main/res/drawable-xxhdpi/ic_launcher.png new file mode 100644 index 0000000..85a6081 Binary files /dev/null and b/android/examples/DirectPairing/src/main/res/drawable-xxhdpi/ic_launcher.png differ diff --git a/android/examples/DirectPairing/src/main/res/layout/activity_main.xml b/android/examples/DirectPairing/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..1ee1a2b --- /dev/null +++ b/android/examples/DirectPairing/src/main/res/layout/activity_main.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + +