From 213fc20e4d1a342423b39b326e3fdf21e3339758 Mon Sep 17 00:00:00 2001 From: Randeep Singh Date: Thu, 6 Oct 2016 12:22:04 +0530 Subject: [PATCH] Added implementation for getDeviceId and setDeviceId code formatted Signed-off-by: Sunil Kumar K R Change-Id: Ic0d640638bd0c1a258db6c21bbecbd73e2c6dd3f Signed-off-by: Ashwini Kumar Signed-off-by: Randeep Singh Reviewed-on: https://gerrit.iotivity.org/gerrit/10621 Tested-by: jenkins-iotivity Reviewed-by: dongik Lee --- android/android_api/base/jni/JniOcPlatform.cpp | 81 ++++++++++++++++++++++ android/android_api/base/jni/JniOcPlatform.h | 16 +++++ .../main/java/org/iotivity/base/OcPlatform.java | 10 +++ .../provisioningclient/ProvisioningClient.java | 10 +++ resource/csdk/octbstack_product.def | 2 + resource/csdk/stack/include/ocstack.h | 15 ++++ resource/csdk/stack/src/ocstack.c | 31 +++++++++ resource/include/OCPlatform.h | 16 +++++ resource/include/OCPlatform_impl.h | 5 ++ resource/src/OCPlatform.cpp | 9 +++ resource/src/OCPlatform_impl.cpp | 10 +++ 11 files changed, 205 insertions(+) diff --git a/android/android_api/base/jni/JniOcPlatform.cpp b/android/android_api/base/jni/JniOcPlatform.cpp index 6b03638..a746cfe 100644 --- a/android/android_api/base/jni/JniOcPlatform.cpp +++ b/android/android_api/base/jni/JniOcPlatform.cpp @@ -2992,3 +2992,84 @@ JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructAccountMana return jAccountManager; #endif } + +/* +* Class: org_iotivity_base_OcPlatform +* Method: getDeviceId +* Signature: (I)V +*/ +JNIEXPORT jbyteArray JNICALL Java_org_iotivity_base_OcPlatform_getDeviceId +(JNIEnv *env, jobject thiz) +{ + LOGD("OcPlatform_getDeviceId"); + OCUUIdentity deviceId; + + jbyteArray ret = env->NewByteArray(UUID_IDENTITY_SIZE); + jbyte uuid[UUID_IDENTITY_SIZE]; + try + { + + OCStackResult result = OCPlatform::getDeviceId(&deviceId); + LOGD("OcPlatform_getDeviceId return from CPP"); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Error while getting my device Id"); + } + else + { + for(int i=0;i < UUID_IDENTITY_SIZE; i++) + { + uuid[i] =(jbyte) deviceId.id[i]; + } + } + + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } + + env->SetByteArrayRegion(ret, 0, UUID_IDENTITY_SIZE, uuid); + + return ret; +} + +/* +* Class: org_iotivity_base_OcPlatform +* Method: setDeviceId +* Signature: (Ljava/lang/byte;)V +*/ +JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setDeviceId( + JNIEnv *env, jobject thiz, jbyteArray data) +{ + LOGI("OcPlatform_setDeviceId"); + OCUUIdentity deviceId; + try + { + OCStackResult result; + jbyte* uuid = env->GetByteArrayElements(data, 0); + jsize arrayLength = env->GetArrayLength(data); + if(arrayLength!=UUID_IDENTITY_SIZE) + { + ThrowOcException(OC_STACK_INVALID_PARAM, "Byte length not equal to UUID_IDENTITY_SIZE"); + } + else + { + for(int i=0;i < UUID_IDENTITY_SIZE; i++) + { + deviceId.id[i]=(jchar)uuid[i]; + } + result = OCPlatform::setDeviceId(&deviceId); + if (OC_STACK_OK != result) + { + ThrowOcException(result, "Failed to set DeviceId"); + } + } + } + catch (OCException& e) + { + LOGE("%s", e.reason().c_str()); + ThrowOcException(e.code(), e.reason().c_str()); + } +} diff --git a/android/android_api/base/jni/JniOcPlatform.h b/android/android_api/base/jni/JniOcPlatform.h index b6c59c2..cf6e20f 100644 --- a/android/android_api/base/jni/JniOcPlatform.h +++ b/android/android_api/base/jni/JniOcPlatform.h @@ -393,6 +393,22 @@ extern "C" { JNIEXPORT jobject JNICALL Java_org_iotivity_base_OcPlatform_constructAccountManagerObject0 (JNIEnv *, jclass, jstring, jint); + /* + * Class: org_iotivity_base_OcPlatform + * Method: getDeviceId + * Signature: (I)V + */ + JNIEXPORT jbyteArray JNICALL Java_org_iotivity_base_OcPlatform_getDeviceId + (JNIEnv *, jobject); + + /* + * Class: org_iotivity_base_OcPlatform + * Method: setDeviceId + * Signature: (Ljava/lang/byte;)V + */ + JNIEXPORT void JNICALL Java_org_iotivity_base_OcPlatform_setDeviceId( + JNIEnv *, jobject, jbyteArray); + #ifdef __cplusplus } #endif 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 bdbf79e..110577c 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 @@ -1438,4 +1438,14 @@ public final class OcPlatform { private static native OcAccountManager constructAccountManagerObject0( String host, int connectivityType) throws OcException; + /** + * Method to get device Id in byte array. + * @return My DeviceId. + */ + public static native byte[] getDeviceId(); + + /** + * Method to set DeviceId. + */ + public static native void setDeviceId(byte[] deviceId); } diff --git a/android/examples/provisioningclient/src/main/java/org/iotivity/base/examples/provisioningclient/ProvisioningClient.java b/android/examples/provisioningclient/src/main/java/org/iotivity/base/examples/provisioningclient/ProvisioningClient.java index 07ae29b..b2e857c 100644 --- a/android/examples/provisioningclient/src/main/java/org/iotivity/base/examples/provisioningclient/ProvisioningClient.java +++ b/android/examples/provisioningclient/src/main/java/org/iotivity/base/examples/provisioningclient/ProvisioningClient.java @@ -205,6 +205,16 @@ OcSecureResource.DoOwnershipTransferListener, OcSecureResource.ProvisionPairwise 0, QualityOfService.LOW, filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE); OcPlatform.Configure(cfg); + + //Get deviceId + byte [] deviceIdBytes= OcPlatform.getDeviceId(); + String devId = new String(deviceIdBytes); + Log.d(TAG, "Get Device Id "+devId); + //Set deviceId + String setId = "adminDeviceUuid1"; + OcPlatform.setDeviceId(setId.getBytes()); + Log.d(TAG, "Set Device Id done"); + try { /* * Initialize DataBase diff --git a/resource/csdk/octbstack_product.def b/resource/csdk/octbstack_product.def index 3747389..1f457bc 100644 --- a/resource/csdk/octbstack_product.def +++ b/resource/csdk/octbstack_product.def @@ -104,3 +104,5 @@ OCStopPresence OCUnBindResource OCSetHeaderOption OCGetHeaderOption +OCGetDeviceId +OCSetDeviceId diff --git a/resource/csdk/stack/include/ocstack.h b/resource/csdk/stack/include/ocstack.h index e7d257e..9bdcccc 100644 --- a/resource/csdk/stack/include/ocstack.h +++ b/resource/csdk/stack/include/ocstack.h @@ -635,6 +635,21 @@ OCGetHeaderOption(OCHeaderOption* ocHdrOpt, size_t optionDataLength, uint16_t* receivedDatalLength); +/** + * gets the deviceId of the client + * + * @param deviceId pointer. + * @return Returns ::OC_STACK_OK if success. + */ +OCStackResult OCGetDeviceId(OCUUIdentity *deviceId); + +/** + * sets the deviceId of the client + * + * @param deviceId pointer. + * @return Returns ::OC_STACK_OK if success. + */ +OCStackResult OCSetDeviceId(const OCUUIdentity *deviceId); #ifdef __cplusplus } #endif // __cplusplus diff --git a/resource/csdk/stack/src/ocstack.c b/resource/csdk/stack/src/ocstack.c index bab1f21..a5ac56e 100644 --- a/resource/csdk/stack/src/ocstack.c +++ b/resource/csdk/stack/src/ocstack.c @@ -5030,3 +5030,34 @@ void OCSetNetworkMonitorHandler(CAAdapterStateChangedCB adapterHandler, g_connectionHandler = connectionHandler; } +OCStackResult OCGetDeviceId(OCUUIdentity *deviceId) +{ + OicUuid_t oicUuid; + OCStackResult ret; + + ret = GetDoxmDeviceID(&oicUuid); + if (OC_STACK_OK == ret) + { + memcpy(deviceId, &oicUuid, UUID_IDENTITY_SIZE); + } + else + { + OIC_LOG(ERROR, TAG, "Device ID Get error"); + } + return ret; +} + +OCStackResult OCSetDeviceId(const OCUUIdentity *deviceId) +{ + OicUuid_t oicUuid; + OCStackResult ret; + OIC_LOG(ERROR, TAG, "Set deviceId DOXM"); + + memcpy(&oicUuid, deviceId, UUID_LENGTH); + for(int i=0;i < UUID_LENGTH; i++) + { + OIC_LOG_V(INFO, TAG, "Set Device Id %x", oicUuid.id[i]); + } + ret = SetDoxmDeviceID(&oicUuid); + return ret; +} diff --git a/resource/include/OCPlatform.h b/resource/include/OCPlatform.h index 335d08a..257a5a7 100644 --- a/resource/include/OCPlatform.h +++ b/resource/include/OCPlatform.h @@ -799,6 +799,22 @@ namespace OC ResourceHandles& resourceHandles, DeleteResourceCallback callback, QualityOfService QoS); #endif + + /** + * gets the deviceId of the client + * + * @param deviceId pointer. + * @return Returns ::OC_STACK_OK if success. + */ + OCStackResult getDeviceId(OCUUIdentity *deviceId); + + /** + * sets the deviceId of the client + * + * @param deviceId pointer. + * @return Returns ::OC_STACK_OK if success. + */ + OCStackResult setDeviceId(const OCUUIdentity *deviceId); } } diff --git a/resource/include/OCPlatform_impl.h b/resource/include/OCPlatform_impl.h index 49d59d9..0a6c063 100644 --- a/resource/include/OCPlatform_impl.h +++ b/resource/include/OCPlatform_impl.h @@ -277,6 +277,11 @@ namespace OC OCAccountManager::Ptr constructAccountManagerObject(const std::string& host, OCConnectivityType connectivityType); #endif // WITH_CLOUD + + OCStackResult getDeviceId(OCUUIdentity *myUuid); + + OCStackResult setDeviceId(const OCUUIdentity *myUuid); + private: PlatformConfig m_cfg; diff --git a/resource/src/OCPlatform.cpp b/resource/src/OCPlatform.cpp index 4ab35a8..21f8204 100644 --- a/resource/src/OCPlatform.cpp +++ b/resource/src/OCPlatform.cpp @@ -393,6 +393,15 @@ namespace OC QoS); } #endif + OCStackResult getDeviceId(OCUUIdentity *deviceId) + { + return OCPlatform_impl::Instance().getDeviceId(deviceId); + } + + OCStackResult setDeviceId(const OCUUIdentity *deviceId) + { + return OCPlatform_impl::Instance().setDeviceId(deviceId); + } } // namespace OCPlatform } //namespace OC diff --git a/resource/src/OCPlatform_impl.cpp b/resource/src/OCPlatform_impl.cpp index d816e30..547b4d9 100644 --- a/resource/src/OCPlatform_impl.cpp +++ b/resource/src/OCPlatform_impl.cpp @@ -483,5 +483,15 @@ namespace OC connectivityType)); } #endif // WITH_CLOUD + + OCStackResult OCPlatform_impl::getDeviceId(OCUUIdentity *myUuid) + { + return OCGetDeviceId(myUuid); + } + + OCStackResult OCPlatform_impl::setDeviceId(const OCUUIdentity *myUuid) + { + return OCSetDeviceId(myUuid); + } } //namespace OC -- 2.7.4