Implement cloud acl-provisioning feature & update discovery logic.
authorJihun Ha <jihun.ha@samsung.com>
Mon, 1 Aug 2016 06:47:59 +0000 (15:47 +0900)
committerMadan Lanka <lanka.madan@samsung.com>
Mon, 1 Aug 2016 09:35:55 +0000 (09:35 +0000)
Change-Id: Id26b1a68106d8f8b8a0f3880fd02699f66ead81a
Signed-off-by: Heewon Park <h_w.park@samsung.com>
Signed-off-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/9843
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Madan Lanka <lanka.madan@samsung.com>
13 files changed:
service/easy-setup/mediator/richsdk/android/EasySetupCore/src/main/java/org/iotivity/service/easysetup/mediator/CloudProp.java
service/easy-setup/mediator/richsdk/android/EasySetupCore/src/main/java/org/iotivity/service/easysetup/mediator/RemoteEnrollee.java
service/easy-setup/mediator/richsdk/android/jni/JniRemoteEnrollee.cpp
service/easy-setup/mediator/richsdk/android/jni/JniRemoteEnrollee.h
service/easy-setup/mediator/richsdk/inc/ESRichCommon.h
service/easy-setup/mediator/richsdk/inc/EnrolleeSecurity.h
service/easy-setup/mediator/richsdk/inc/RemoteEnrollee.h
service/easy-setup/mediator/richsdk/src/EnrolleeResource.cpp
service/easy-setup/mediator/richsdk/src/EnrolleeSecurity.cpp
service/easy-setup/mediator/richsdk/src/RemoteEnrollee.cpp
service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/java/org/iotivity/service/easysetup/EasysetupActivity.java [changed mode: 0755->0644]
service/easy-setup/sampleapp/mediator/android/EasySetup/app/src/main/res/layout/easysetup_main.xml
service/easy-setup/sampleapp/mediator/linux/richsdk_sample/mediator_cpp.cpp

index 90f0477..6f4d5be 100644 (file)
@@ -31,12 +31,14 @@ import org.iotivity.base.OcRepresentation;
 public class CloudProp {
     private static final String TAG = CloudProp.class.getName();
     protected OcRepresentation mRep;
+    protected String mCloudID;
 
     /**
      * Constructor
      */
     public CloudProp() {
         mRep = new OcRepresentation();
+        mCloudID = "";
     }
 
     public void setCloudProp(String authCode, String authProvider, String ciServer)
@@ -50,6 +52,11 @@ public class CloudProp {
         }
     }
 
+    public void setCloudID(String cloudID)
+    {
+        mCloudID = cloudID;
+    }
+
     /**
      * This method returns the authCode used for the first registration to IoTivity cloud
      * @return AuthCode for sign-up to IoTivity cloud
@@ -95,6 +102,15 @@ public class CloudProp {
         return new String("");
     }
 
+    /**
+     * This method returns the Cloud Interface server's UUID
+     * @return CI server's UUID
+     */
+    public String getCloudID()
+    {
+        return mCloudID;
+    }
+
     public OcRepresentation toOCRepresentation()
     {
         return mRep;
index 726bc90..0ff4652 100755 (executable)
@@ -44,7 +44,7 @@ public class RemoteEnrollee{
     private native void nativeProvisionSecurity(SecurityProvisioningCallback callback);
     private native void nativeProvisionDeviceProperties(OcRepresentation deviceProp,
                                                         DevicePropProvisioningCallback callback);
-    private native void nativeProvisionCloudProperties(OcRepresentation cloudProp,
+    private native void nativeProvisionCloudProperties(OcRepresentation cloudProp, String cloudID,
                                                        CloudPropProvisioningCallback callback);
 
     /* constructor will be invoked from the native layer */
@@ -61,7 +61,8 @@ public class RemoteEnrollee{
      *
      * @see GetConfigurationCallback
      */
-    public void getConfiguration(GetConfigurationCallback callback) throws ESException
+    public void getConfiguration(GetConfigurationCallback callback)
+                                                        throws ESException
     {
         if(callback != null)
         {
@@ -80,7 +81,8 @@ public class RemoteEnrollee{
      *
      * @see SecurityProvisioningCallback
      */
-    public void provisionSecurity(SecurityProvisioningCallback callback) throws ESException
+    public void provisionSecurity(SecurityProvisioningCallback callback)
+                                                        throws ESException
     {
         if(callback != null)
         {
@@ -103,11 +105,12 @@ public class RemoteEnrollee{
      * @see DeviceProp
      * @see DevicePropProvisioningCallback
      */
-    public void provisionDeviceProperties(DeviceProp deviceProp, DevicePropProvisioningCallback callback) throws ESException
-    {
+    public void provisionDeviceProperties(DeviceProp deviceProp,
+                    DevicePropProvisioningCallback callback) throws ESException{
         if(callback != null)
         {
-            nativeProvisionDeviceProperties(deviceProp.toOCRepresentation(), callback);
+            nativeProvisionDeviceProperties(deviceProp.toOCRepresentation(),
+                                                callback);
             return;
         }
         Log.d(TAG, "DevicePropProvisioningCallback is null ");
@@ -129,10 +132,13 @@ public class RemoteEnrollee{
      * @see CloudProp
      * @see CloudPropProvisioningCallback
      */
-    public void provisionCloudProperties(CloudProp cloudProp, CloudPropProvisioningCallback callback) throws ESException{
+    public void provisionCloudProperties(CloudProp cloudProp,
+                    CloudPropProvisioningCallback callback) throws ESException{
         if(callback != null)
         {
-            nativeProvisionCloudProperties(cloudProp.toOCRepresentation(), callback);
+            nativeProvisionCloudProperties(cloudProp.toOCRepresentation(),
+                                            cloudProp.getCloudID(),
+                                            callback);
             return;
         }
         Log.d(TAG, "CloudPropProvisioningCallback is null ");
index 8191650..0faef04 100755 (executable)
@@ -99,7 +99,9 @@ void JniRemoteEnrollee::provisionSecurity(JNIEnv *env, jobject jListener)
     }
 }
 
-void JniRemoteEnrollee::provisionDeviceProperties(JNIEnv *env, jobject jRepresentation, jobject jListener)
+void JniRemoteEnrollee::provisionDeviceProperties(JNIEnv *env,
+                                                    jobject jRepresentation,
+                                                    jobject jListener)
 {
     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
     if (!representation)
@@ -129,7 +131,10 @@ void JniRemoteEnrollee::provisionDeviceProperties(JNIEnv *env, jobject jRepresen
     }
 }
 
-void JniRemoteEnrollee::provisionCloudProperties(JNIEnv *env, jobject jRepresentation, jobject jListener)
+void JniRemoteEnrollee::provisionCloudProperties(JNIEnv *env,
+                                                jobject jRepresentation,
+                                                jstring jCloudID,
+                                                jobject jListener)
 {
     OCRepresentation *representation = JniOcRepresentation::getOCRepresentationPtr(env, jRepresentation);
     if (!representation)
@@ -138,6 +143,8 @@ void JniRemoteEnrollee::provisionCloudProperties(JNIEnv *env, jobject jRepresent
     }
 
     CloudProp cloudProp(*representation);
+    cloudProp.setCloudID(env->GetStringUTFChars(jCloudID, NULL));
+
     JniCloudPropProvisioningStatusListener *onCloudPropProvisioningStatusReceived =
                     addStatusListener<JniCloudPropProvisioningStatusListener>(env, jListener);
 
@@ -199,12 +206,12 @@ Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionDevic
 
 JNIEXPORT void JNICALL
 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionCloudProperties
-(JNIEnv *env, jobject jClass, jobject jRepresentation, jobject jListener)
+(JNIEnv *env, jobject jClass, jobject jRepresentation, jstring jCloudID, jobject jListener)
 {
     ES_LOGD("nativeprovisionCloudProperties Enter");
 
     JniRemoteEnrollee *remoteEnrollee = JniRemoteEnrollee::getJniRemoteEnrollee(env, jClass);
-    remoteEnrollee->provisionCloudProperties(env, jRepresentation, jListener);
+    remoteEnrollee->provisionCloudProperties(env, jRepresentation, jCloudID, jListener);
 
     ES_LOGD("nativeprovisionCloudProperties Exit");
-}
\ No newline at end of file
+}
index b3f013b..b279051 100755 (executable)
@@ -64,9 +64,8 @@ class JniRemoteEnrollee
         // ***** JNI APIs internally call the APIs of this class ***** //
         void getConfiguration(JNIEnv *env, jobject jListener);
         void provisionSecurity(JNIEnv *env, jobject jListener);
-
         void provisionDeviceProperties(JNIEnv *env, jobject jRepresentation, jobject jListener);
-        void provisionCloudProperties(JNIEnv *env, jobject jRepresentation, jobject jListener);
+        void provisionCloudProperties(JNIEnv *env, jobject jRepresentation, jstring jCloudID, jobject jListener);
 
         static JniRemoteEnrollee *getJniRemoteEnrollee(JNIEnv *env, jobject thiz);
 
@@ -117,7 +116,7 @@ Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionDevic
  */
 JNIEXPORT void JNICALL
 Java_org_iotivity_service_easysetup_mediator_RemoteEnrollee_nativeProvisionCloudProperties
-(JNIEnv *env, jobject jClass, jobject jRepresentation, jobject jListener);
+(JNIEnv *env, jobject jClass, jobject jRepresentation, jstring jCloudID, jobject jListener);
 
 #ifdef __cplusplus
 }
index 2025774..4acb8d2 100755 (executable)
@@ -272,7 +272,11 @@ namespace OIC
             /**
              * Constructor with OCRepresentation object. This is used for JNI communication.
              */
-            CloudProp(const OCRepresentation &rep) { m_rep = rep; }
+            CloudProp(const OCRepresentation &rep)
+            {
+                m_rep = rep;
+                m_cloudID = "";
+            }
 
             /**
              * Set CloudServer resource properties to be delivered to Enrollee
@@ -289,6 +293,16 @@ namespace OIC
             }
 
             /**
+             * Set CloudServer's UUID
+             *
+             * @param cloudID Cloud Interface server's UUID
+             */
+            void setCloudID(string cloudID)
+            {
+                m_cloudID = cloudID;
+            }
+
+            /**
              * Get an auth code to be delivered.
              *
              * @return an auth code to be delivered.
@@ -325,6 +339,16 @@ namespace OIC
             }
 
             /**
+             * Get a CI server's Uuid to be delivered
+             *
+             * @return a CI server's Uuid to be delivered
+             */
+            std::string getCloudID() const
+            {
+                return m_cloudID;
+            }
+
+            /**
              * Get OCRepresentation object
              *
              * @return OCRepresentation object
@@ -335,6 +359,7 @@ namespace OIC
             }
         protected:
             OCRepresentation m_rep;
+            std::string m_cloudID;
         };
 
         /**
index 33c576f..1559fa3 100755 (executable)
@@ -22,6 +22,7 @@
 #define ENROLLEE_SECURITY_H_
 
 #include <functional>
+#include <condition_variable>
 
 #include "ESRichCommon.h"
 #include "OCProvisioningManager.h"
@@ -50,7 +51,7 @@ namespace OIC
             void registerCallbackHandler(SecurityProvStatusCb securityProvStatusCb,
                     SecurityPinCb securityPinCb, SecProvisioningDbPathCb secProvisioningDbPathCb);
             void performOwnershipTransfer();
-            void performACLProvisioningForCloudServer(OicUuid_t serverUuid);
+            ESResult performACLProvisioningForCloudServer(std::string cloudUuid);
 
         private:
             std::shared_ptr< OC::OCResource > m_ocResource;
@@ -59,13 +60,17 @@ namespace OIC
             SecProvisioningDbPathCb m_secProvisioningDbPathCb;
             std::shared_ptr< OC::OCSecureResource > m_unownedDevice;
 
+            std::mutex m_mtx;
+            std::condition_variable m_cond;
+            bool aclResult;
 
             std::shared_ptr< OC::OCSecureResource > m_securedResource;
 
             std::shared_ptr< OC::OCSecureResource > getEnrollee(OC::DeviceList_t &list);
             void ownershipTransferCb(OC::PMResultList_t *result, int hasError);
             void convertUUIDToString(OicUuid_t uuid, std::string& uuidString);
-            OicSecAcl_t* createAcl(OicUuid_t serverUuid);
+            void convertStringToUUID(OicUuid_t& uuid, std::string uuidString);
+            OicSecAcl_t* createAcl(OicUuid_t cloudUuid);
 
             void ACLProvisioningCb(PMResultList_t *result, int hasError);
         };
index a4274e6..6f5c6e4 100755 (executable)
@@ -22,6 +22,8 @@
 #define REMOTE_ENROLLEE_H_
 
 #include <memory>
+#include <iostream>
+#include <condition_variable>
 
 #include "ESRichCommon.h"
 #include "OCApi.h"
@@ -113,7 +115,8 @@ namespace OIC
              * @see DeviceProp
              * @see DevicePropProvStatusCb
              */
-            void provisionDeviceProperties(const DeviceProp& devProp, DevicePropProvStatusCb callback);
+            void provisionDeviceProperties(const DeviceProp& devProp,
+                                               DevicePropProvStatusCb callback);
 
             /**
              * Provision Cloud information to Enrollee, which includes Auth code, auth provider,
@@ -131,13 +134,13 @@ namespace OIC
              * @see CloudProp
              * @see CloudPropProvStatusCb
              */
-            void provisionCloudProperties(const CloudProp& cloudProp, CloudPropProvStatusCb callback);
+            void provisionCloudProperties(const CloudProp& cloudProp,
+                                              CloudPropProvStatusCb callback);
 
         private:
             RemoteEnrollee(std::shared_ptr< OC::OCResource > resource);
 
             ESResult discoverResource();
-            ESResult ESDiscoveryTimeout(unsigned short waittime);
             void onDeviceDiscovered(std::shared_ptr<OC::OCResource> resource);
             void initCloudResource();
 
@@ -156,6 +159,9 @@ namespace OIC
             std::string  m_deviceId;
             bool m_discoveryResponse;
 
+            std::mutex m_discoverymtx;
+            std::condition_variable m_cond;
+
             SecurityProvStatusCb m_securityProvStatusCb;
             GetStatusCb m_getStatusCb;
             GetConfigurationStatusCb m_getConfigurationStatusCb;
index c1f2467..13d1c9d 100755 (executable)
@@ -85,7 +85,7 @@ namespace OIC
 
                 EnrolleeStatus enrolleeStatus(rep);
                 std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared<
-                        GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus);
+                        GetEnrolleeStatus >(result, enrolleeStatus);
 
                 m_getStatusCb(getEnrolleeStatus);
             }
index 332fe34..97fd68c 100755 (executable)
@@ -45,7 +45,7 @@ namespace OIC
         //TODO : Currently discovery timeout for owned and unowned devices is fixed as 5
         // The value should be accepted from the application as a parameter during ocplatform
         // config call
-#define ES_SEC_DISCOVERY_TIMEOUT 5
+        #define ES_SEC_DISCOVERY_TIMEOUT 5
 
         EnrolleeSecurity::EnrolleeSecurity(
         std::shared_ptr< OC::OCResource > resource,
@@ -74,12 +74,6 @@ namespace OIC
                     OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "From IP :%s", list[i]->getDevAddr().c_str());
                     return list[i];
                 }
-                //Always return the first element of the unOwned devices. This is considering that Mediator is
-                // always connected with only one Enrollee for which ownership transfer is being performed.
-                // Incase of multiple Enrollee devices connected to the Mediator via any OnBoarding method (SoftAp
-                // for example), the Enrollee devices will be provisioned in the first come first serve basis in the order
-                // returned by the security layer.
-
             }
             OIC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! DeviceList_t is NULL");
             return NULL;
@@ -103,6 +97,34 @@ namespace OIC
             uuidString =  deviceId.str();
         }
 
+        void EnrolleeSecurity::convertStringToUUID(OicUuid_t& uuid, std::string uuidString)
+        {
+            size_t outBufSize = B64DECODE_OUT_SAFESIZE((uuidString.length() + 1));
+            uint8_t* outKey = (uint8_t*)OICCalloc(1, outBufSize);
+            uint32_t outKeySize;
+            if(NULL == outKey)
+            {
+                OIC_LOG (ERROR, ENROLEE_SECURITY_TAG, "Failed to memoray allocation.");
+                throw ESBadRequestException ("Failed to memoray allocation.");
+            }
+
+            if(B64_OK == b64Decode((char*)uuidString.c_str(),
+                                    uuidString.length(),
+                                    outKey,
+                                    outBufSize,
+                                    &outKeySize))
+            {
+                memcpy(uuid.id, outKey, outKeySize);
+            }
+            else
+            {
+                OIC_LOG (ERROR, ENROLEE_SECURITY_TAG, "Failed to base64 decoding.");
+                throw ESBadRequestException ("Failed to base64 decoding.");
+            }
+
+            OICFree(outKey);
+        }
+
         void EnrolleeSecurity::ownershipTransferCb(OC::PMResultList_t *result, int hasError)
         {
             if (hasError)
@@ -144,7 +166,7 @@ namespace OIC
             pUnownedDevList.clear();
 
             OCStackResult result;
-            /*
+
             result = OCSecure::discoverOwnedDevices(ES_SEC_DISCOVERY_TIMEOUT,
                     pOwnedDevList);
             if (result != OC_STACK_OK)
@@ -167,7 +189,7 @@ namespace OIC
                     return;
                 }
             }
-            */
+
             result = OCSecure::discoverUnownedDevices(ES_SEC_DISCOVERY_TIMEOUT, pUnownedDevList);
             if (result != OC_STACK_OK)
             {
@@ -205,16 +227,26 @@ namespace OIC
                         throw ESPlatformException(result);
                     }
                 }
+                else
+                {
+                    OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No matched unowned devices found.");
+                    throw ESException("No matched unowned devices found.");
+                }
             }
             else
             {
-                OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No unOwned devices found.");
-                throw ESException("No unOwned devices found.");
+                OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No unowned devices found.");
+                throw ESException("No unowned devices found.");
             }
         }
 
-        void EnrolleeSecurity::performACLProvisioningForCloudServer(OicUuid_t serverUuid)
+        ESResult EnrolleeSecurity::performACLProvisioningForCloudServer(std::string cloudUuid)
         {
+            ESResult res = ESResult::ES_ERROR;
+
+            OicUuid_t uuid;
+            convertStringToUUID(uuid, cloudUuid);
+
             // Need to discover Owned device in a given network, again
             OC::DeviceList_t pOwnedDevList;
             std::shared_ptr< OC::OCSecureResource > ownedDevice = NULL;
@@ -240,21 +272,21 @@ namespace OIC
                 if (!ownedDevice)
                 {
                     OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Not found owned devices.");
-                    return;
+                    return res;
                 }
             }
             else
             {
                 OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Not found owned devices.");
-                return;
+                return res;
             }
 
             // Create Acl for Cloud Server to be provisioned to Enrollee
-            OicSecAcl_t* acl = createAcl(serverUuid);
+            OicSecAcl_t* acl = createAcl(uuid);
             if(!acl)
             {
                 OIC_LOG(ERROR, ENROLEE_SECURITY_TAG, "createAcl error return");
-                return;
+                return res;
             }
 
             OC::ResultCallBack aclProvisioningCb = std::bind(
@@ -265,11 +297,21 @@ namespace OIC
             if(OC_STACK_OK != rst)
             {
                 OIC_LOG_V(ERROR, ENROLEE_SECURITY_TAG, "OCProvisionACL API error: %d", rst);
-                return;
+                return res;
             }
+
+            std::unique_lock<std::mutex> lck(m_mtx);
+            m_cond.wait_for(lck, std::chrono::seconds(ES_SEC_DISCOVERY_TIMEOUT));
+
+            if(aclResult)
+            {
+                res = ESResult::ES_OK;
+            }
+
+            return res;
         }
 
-        OicSecAcl_t* EnrolleeSecurity::createAcl(OicUuid_t serverUuid)
+        OicSecAcl_t* EnrolleeSecurity::createAcl(OicUuid_t cloudUuid)
         {
             // allocate memory for |acl| struct
             OicSecAcl_t* acl = (OicSecAcl_t*) OICCalloc(1, sizeof(OicSecAcl_t));
@@ -286,7 +328,7 @@ namespace OIC
             }
             LL_APPEND(acl->aces, ace);
 
-            memcpy(&ace->subjectuuid, &serverUuid, UUID_LENGTH);
+            memcpy(&ace->subjectuuid, &cloudUuid, UUID_LENGTH);
 
             OicSecRsrc_t* rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t));
             if(!rsrc)
@@ -323,8 +365,26 @@ namespace OIC
 
         void EnrolleeSecurity::ACLProvisioningCb(PMResultList_t *result, int hasError)
         {
-            (void) result;
-            (void) hasError;
+            if (hasError)
+            {
+               OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Error in provisioning operation!");
+               aclResult = false;
+            }
+            else
+            {
+               OIC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received provisioning results: ");
+
+               std::string devUuid;
+               for (unsigned int i = 0; i < result->size(); i++)
+               {
+                   convertUUIDToString(result->at(i).deviceId, devUuid);
+                   OIC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d  for device %s",
+                                                           result->at(i).res, devUuid.c_str());
+               }
+               delete result;
+               aclResult = true;
+            }
+            m_cond.notify_all();
         }
     }
 }
index bbabc3c..73cef4d 100755 (executable)
@@ -27,6 +27,8 @@
 #include "OCResource.h"
 #ifdef __WITH_DTLS__
 #include "EnrolleeSecurity.h"
+#include "base64.h"
+#include "oic_malloc.h"
 #endif //__WITH_DTLS
 
 namespace OIC
@@ -69,7 +71,7 @@ namespace OIC
         void RemoteEnrollee::securityStatusHandler(
                         std::shared_ptr< SecProvisioningStatus > status)
         {
-            OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, "
+            OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s,"
                     "Status = %d", status->getDeviceUUID().c_str(),
                     status->getESResult());
 
@@ -104,7 +106,8 @@ namespace OIC
         {
             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering getConfigurationStatusHandler");
 
-            OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"GetConfigurationStatus = %d", status->getESResult());
+            OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"GetConfigurationStatus = %d",
+                                                    status->getESResult());
 
             m_getConfigurationStatusCb(status);
         }
@@ -126,58 +129,13 @@ namespace OIC
         {
             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering cloudPropProvisioningStatusHandler");
 
-            OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"CloudProvStatus = %d", status->getESCloudState());
+            OIC_LOG_V(DEBUG,ES_REMOTE_ENROLLEE_TAG,"CloudProvStatus = %d",
+                                                    status->getESCloudState());
 
             m_cloudPropProvStatusCb(status);
             return;
         }
 
-        ESResult RemoteEnrollee::ESDiscoveryTimeout(unsigned short waittime)
-        {
-            struct timespec startTime;
-            startTime.tv_sec=0;
-            startTime.tv_sec=0;
-            struct timespec currTime;
-            currTime.tv_sec=0;
-            currTime.tv_nsec=0;
-
-            ESResult res = ES_OK;
-            #ifdef _POSIX_MONOTONIC_CLOCK
-                int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime);
-            #else
-                int clock_res = clock_gettime(CLOCK_REALTIME, &startTime);
-            #endif
-
-            if (0 != clock_res)
-            {
-                return ES_ERROR;
-            }
-
-            while (ES_OK == res || m_discoveryResponse == false)
-            {
-                #ifdef _POSIX_MONOTONIC_CLOCK
-                        clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime);
-                #else
-                        clock_res = clock_gettime(CLOCK_REALTIME, &currTime);
-                #endif
-
-                if (0 != clock_res)
-                {
-                    return ES_ERROR;
-                }
-                long elapsed = (currTime.tv_sec - startTime.tv_sec);
-                if (elapsed > waittime)
-                {
-                    return ES_OK;
-                }
-                if (m_discoveryResponse)
-                {
-                    res = ES_OK;
-                }
-             }
-             return res;
-        }
-
         void RemoteEnrollee::onDeviceDiscovered(std::shared_ptr<OC::OCResource> resource)
         {
             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onDeviceDiscovered");
@@ -211,6 +169,7 @@ namespace OIC
                             OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_TAG, "Find matched CloudResource");
                             m_ocResource = resource;
                             m_discoveryResponse = true;
+                            m_cond.notify_all();
                         }
                     }
                 }
@@ -248,9 +207,10 @@ namespace OIC
                 return ES_ERROR;
             }
 
-            ESResult foundResponse = ESDiscoveryTimeout (DISCOVERY_TIMEOUT);
+            std::unique_lock<std::mutex> lck(m_discoverymtx);
+            m_cond.wait_for(lck, std::chrono::seconds(DISCOVERY_TIMEOUT));
 
-            if (foundResponse == ES_ERROR || !m_discoveryResponse)
+            if (!m_discoveryResponse)
             {
                 OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG,
                         "Failed discoverResource because timeout");
@@ -271,16 +231,17 @@ namespace OIC
             //TODO : DBPath is passed empty as of now. Need to take dbpath from application.
             m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource, "");
 
-            m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb, m_securityPinCb, m_secProvisioningDbPathCb);
+            m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb, m_securityPinCb,
+                                                        m_secProvisioningDbPathCb);
 
             try
             {
                 m_enrolleeSecurity->performOwnershipTransfer();
             }
-            catch (OCException & e)
+            catch (const std::exception& e)
             {
                 OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
-                        "Exception for performOwnershipTransfer : %s", e.reason().c_str());
+                        "Exception for performOwnershipTransfer : %s", e.what());
 
                 OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Fail performOwnershipTransfer");
                     std::shared_ptr< SecProvisioningStatus > securityProvisioningStatus =
@@ -338,7 +299,8 @@ namespace OIC
             m_enrolleeResource->getConfiguration();
         }
 
-        void RemoteEnrollee::provisionDeviceProperties(const DeviceProp& deviceProp, DevicePropProvStatusCb callback)
+        void RemoteEnrollee::provisionDeviceProperties(const DeviceProp& deviceProp,
+                                                            DevicePropProvStatusCb callback)
         {
             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter provisionDeviceProperties");
 
@@ -360,7 +322,8 @@ namespace OIC
             }
 
             DevicePropProvStatusCb devicePropProvStatusCb = std::bind(
-                    &RemoteEnrollee::devicePropProvisioningStatusHandler, this, std::placeholders::_1);
+                    &RemoteEnrollee::devicePropProvisioningStatusHandler,
+                    this, std::placeholders::_1);
 
             m_enrolleeResource->registerDevicePropProvStatusCallback(devicePropProvStatusCb);
             m_enrolleeResource->provisionEnrollee(deviceProp);
@@ -388,10 +351,11 @@ namespace OIC
             {
                 if(m_ocResource != nullptr)
                 {
-                    m_cloudResource = std::make_shared<CloudResource>(std::move(m_ocResource));
+                    m_cloudResource = std::make_shared<CloudResource>(m_ocResource);
 
                     std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
-                        CloudPropProvisioningStatus >(ESResult::ES_OK, ESCloudProvState::ES_CLOUD_ENROLLEE_FOUND);
+                        CloudPropProvisioningStatus >(ESResult::ES_OK,
+                                                        ESCloudProvState::ES_CLOUD_ENROLLEE_FOUND);
 
                     m_cloudPropProvStatusCb(provStatus);
                 }
@@ -402,12 +366,11 @@ namespace OIC
             }
         }
 
-
-        void RemoteEnrollee::provisionCloudProperties(const CloudProp& cloudProp, CloudPropProvStatusCb callback)
+        void RemoteEnrollee::provisionCloudProperties(const CloudProp& cloudProp,
+                                                            CloudPropProvStatusCb callback)
         {
             OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter provisionCloudProperties");
-
-            ESResult result = ES_ERROR;
+            ESResult res = ES_OK;
 
             if(!callback)
             {
@@ -434,17 +397,54 @@ namespace OIC
                     "Exception caught in provisionCloudProperties = %s", e.what());
 
                 std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
-                        CloudPropProvisioningStatus >(ESResult::ES_ERROR, ESCloudProvState::ES_CLOUD_ENROLLEE_NOT_FOUND);
+                        CloudPropProvisioningStatus >(ESResult::ES_ERROR,
+                                                    ESCloudProvState::ES_CLOUD_ENROLLEE_NOT_FOUND);
                 m_cloudPropProvStatusCb(provStatus);
+                return;
             }
 
+#ifdef __WITH_DTLS__
+            try
+            {
+                m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource, "");
+
+                if(cloudProp.getCloudID().empty())
+                {
+                    throw ESBadRequestException("Invalid Cloud Server UUID.");
+                }
+
+                res = m_enrolleeSecurity->performACLProvisioningForCloudServer(cloudProp.getCloudID());
+
+                if(res == ESResult::ES_ERROR)
+                {
+                    throw ESBadRequestException("Error in provisioning operation!");
+                }
+
+            }
+
+            catch (const std::exception& e)
+            {
+                OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG,
+                    "Exception caught in provisionCloudProperties = %s", e.what());
+
+                m_cloudResource = nullptr;
+
+                std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared<
+                        CloudPropProvisioningStatus >(ESResult::ES_ERROR,
+                                                    ESCloudProvState::ES_CLOUD_PROVISIONING_ERROR);
+                m_cloudPropProvStatusCb(provStatus);
+                return;
+            }
+#endif
+
             if (m_cloudResource == nullptr)
             {
                 throw ESBadRequestException ("Cloud Resource not created");
             }
 
             CloudPropProvStatusCb cloudPropProvStatusCb = std::bind(
-                    &RemoteEnrollee::cloudPropProvisioningStatusHandler, this, std::placeholders::_1);
+                    &RemoteEnrollee::cloudPropProvisioningStatusHandler,
+                                    this, std::placeholders::_1);
 
             m_cloudResource->registerCloudPropProvisioningStatusCallback(cloudPropProvStatusCb);
             m_cloudResource->provisionEnrollee(cloudProp);
old mode 100755 (executable)
new mode 100644 (file)
index 8ed6ebd..1135e5b
@@ -100,14 +100,14 @@ public class EasysetupActivity extends Activity {
     RadioGroup mEasysetupProcess;
     RadioButton mConfigureSecProcess;
     RadioButton mGetConfigurationProcess;
-    RadioButton mProvisionDevConfProcess;
-    RadioButton mProvisionCloudConfProcess;
+    RadioButton mProvisionDevPropProcess;
+    RadioButton mProvisionCloudPropProcess;
 
     Button mDiscoverResource;
     Button mStartGetConfiguration;
     Button mStartConfigureSec;
-    Button mStartProvisionDevConf;
-    Button mStartProvisionCloudConf;
+    Button mStartProvisionDevProp;
+    Button mStartProvisionCloudProp;
 
     TextView mGetconfigurationStateText;
     TextView mDevNameText;
@@ -118,8 +118,8 @@ public class EasysetupActivity extends Activity {
     TextView mCloudAccessableText;
     TextView mSecStateText;
     TextView mSecDevIDText;
-    TextView mProvisionDevConfState;
-    TextView mProvisionCloudConfState;
+    TextView mProvisionDevPropState;
+    TextView mProvisionCloudPropState;
 
     EditText mEnrollerSsidText;
     EditText mEnrollerPWText;
@@ -131,8 +131,8 @@ public class EasysetupActivity extends Activity {
 
     LinearLayout mGetConfigurationInfo;
     LinearLayout mConfigureSecInfo;
-    LinearLayout mProvisionDevConfInfo;
-    LinearLayout mProvisionCloudConfInfo;
+    LinearLayout mProvisionDevPropInfo;
+    LinearLayout mProvisionCloudPropInfo;
 
     Spinner mAuthType;
     Spinner mEncType;
@@ -157,16 +157,16 @@ public class EasysetupActivity extends Activity {
 
         mConfigureSecProcess = (RadioButton) findViewById(R.id.btn_configurSec);
         mGetConfigurationProcess = (RadioButton) findViewById(R.id.btn_getConfiguration);
-        mProvisionDevConfProcess = (RadioButton) findViewById(R.id.btn_provisionDevConf);
-        mProvisionCloudConfProcess =
+        mProvisionDevPropProcess = (RadioButton) findViewById(R.id.btn_provisionDevConf);
+        mProvisionCloudPropProcess =
                 (RadioButton) findViewById(R.id.btn_provisionCloudConf);
 
         mDiscoverResource = (Button) findViewById(R.id.btn_discoverResource);
         mStartGetConfiguration =
                 (Button) findViewById(R.id.btn_startGetConfiguration);
         mStartConfigureSec = (Button) findViewById(R.id.btn_startConfigureSec);
-        mStartProvisionDevConf = (Button) findViewById(R.id.btn_startProvisionDevConf);
-        mStartProvisionCloudConf = (Button) findViewById(R.id.btn_startProvisionCloudConf);
+        mStartProvisionDevProp = (Button) findViewById(R.id.btn_startProvisionDevConf);
+        mStartProvisionCloudProp = (Button) findViewById(R.id.btn_startProvisionCloudConf);
 
         mGetconfigurationStateText =
                 (TextView) findViewById(R.id.txt_getConfigurationState);
@@ -178,8 +178,8 @@ public class EasysetupActivity extends Activity {
         mCloudAccessableText = (TextView) findViewById(R.id.txt_cloudAccessable);
         mSecStateText = (TextView) findViewById(R.id.txt_secState);
         mSecDevIDText = (TextView) findViewById(R.id.txt_secDevID);
-        mProvisionDevConfState = (TextView) findViewById(R.id.txt_provisionDevConfState);
-        mProvisionCloudConfState =
+        mProvisionDevPropState = (TextView) findViewById(R.id.txt_provisionDevConfState);
+        mProvisionCloudPropState =
                 (TextView) findViewById(R.id.txt_provisionCloudConfState);
 
         mEnrollerSsidText = (EditText) findViewById(R.id.editText_EnrollerSSID);
@@ -193,8 +193,8 @@ public class EasysetupActivity extends Activity {
         mGetConfigurationInfo =
                 (LinearLayout) findViewById(R.id.layout_GetConfiguration);
         mConfigureSecInfo = (LinearLayout) findViewById(R.id.layout_ConfigurSec);
-        mProvisionDevConfInfo = (LinearLayout) findViewById(R.id.layout_ProvisionDevConf);
-        mProvisionCloudConfInfo = (LinearLayout) findViewById(R.id.layout_ProvisionCloudConf);
+        mProvisionDevPropInfo = (LinearLayout) findViewById(R.id.layout_ProvisionDevConf);
+        mProvisionCloudPropInfo = (LinearLayout) findViewById(R.id.layout_ProvisionCloudConf);
 
         mAuthType = (Spinner) findViewById(R.id.spinner_authType);
         mEncType = (Spinner) findViewById(R.id.spinner_encType);
@@ -204,8 +204,8 @@ public class EasysetupActivity extends Activity {
             public void onCheckedChanged(RadioGroup group, int checkedId) {
                 mGetConfigurationInfo.setVisibility(View.GONE);
                 mConfigureSecInfo.setVisibility(View.GONE);
-                mProvisionDevConfInfo.setVisibility(View.GONE);
-                mProvisionCloudConfInfo.setVisibility(View.GONE);
+                mProvisionDevPropInfo.setVisibility(View.GONE);
+                mProvisionCloudPropInfo.setVisibility(View.GONE);
 
                 switch (checkedId) {
                     case R.id.btn_configurSec:
@@ -217,11 +217,11 @@ public class EasysetupActivity extends Activity {
                         break;
 
                     case R.id.btn_provisionDevConf:
-                        mProvisionDevConfInfo.setVisibility(View.VISIBLE);
+                        mProvisionDevPropInfo.setVisibility(View.VISIBLE);
                         break;
 
                     case R.id.btn_provisionCloudConf:
-                        mProvisionCloudConfInfo.setVisibility(View.VISIBLE);
+                        mProvisionCloudPropInfo.setVisibility(View.VISIBLE);
                         break;
                 }
             }
@@ -252,8 +252,8 @@ public class EasysetupActivity extends Activity {
         mSecurityMode.setClickable(false);
         mConfigureSecProcess.setEnabled(false);
         mGetConfigurationProcess.setEnabled(false);
-        mProvisionDevConfProcess.setEnabled(false);
-        mProvisionCloudConfProcess.setEnabled(false);
+        mProvisionDevPropProcess.setEnabled(false);
+        mProvisionCloudPropProcess.setEnabled(false);
 
         mEasySetup = EasySetup.getInstance(getApplicationContext());
 
@@ -362,11 +362,11 @@ public class EasysetupActivity extends Activity {
                             runOnUiThread(new Runnable() {
                                 @Override
                                 public void run() {
-                                    mDiscoverResource.setText("Founded");
+                                    mDiscoverResource.setText("Found");
                                     mConfigureSecProcess.setEnabled(true);
                                     mGetConfigurationProcess.setEnabled(true);
-                                    mProvisionDevConfProcess.setEnabled(true);
-                                    mProvisionCloudConfProcess.setEnabled(true);
+                                    mProvisionDevPropProcess.setEnabled(true);
+                                    mProvisionCloudPropProcess.setEnabled(true);
                                 }
                             });
                             isFirstTime = false;
@@ -542,7 +542,7 @@ public class EasysetupActivity extends Activity {
     }
 
     private void addListenerForStartProvisionDevProp() {
-        mStartProvisionDevConf.setOnClickListener(new View.OnClickListener() {
+        mStartProvisionDevProp.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Thread thread = new Thread() {
@@ -552,8 +552,8 @@ public class EasysetupActivity extends Activity {
                             runOnUiThread(new Runnable() {
                                 @Override
                                 public void run() {
-                                    mProvisionDevConfState.setText("Progress");
-                                    mStartProvisionDevConf.setEnabled(false);
+                                    mProvisionDevPropState.setText("Progress");
+                                    mStartProvisionDevProp.setEnabled(false);
                                 }
                             });
 
@@ -578,15 +578,15 @@ public class EasysetupActivity extends Activity {
                                         @Override
                                         public void run() {
                                             if(result.equals(ESResult.ES_OK)) {
-                                                mProvisionDevConfState.setText("Success");
+                                                mProvisionDevPropState.setText("Success");
                                             }
                                             else if(result.equals(ESResult.ES_ERROR)) {
-                                                mProvisionDevConfState.setText("Failed");
+                                                mProvisionDevPropState.setText("Failed");
                                             }
                                             else if(result.equals(ESResult.ES_UNAUTHORIZED)) {
-                                                mProvisionDevConfState.setText("Failed. Need SecProv");
+                                                mProvisionDevPropState.setText("Failed. Need SecProv");
                                             }
-                                            mStartProvisionDevConf.setEnabled(true);
+                                            mStartProvisionDevProp.setEnabled(true);
                                         }
                                     });
                                 }
@@ -596,8 +596,8 @@ public class EasysetupActivity extends Activity {
                             runOnUiThread(new Runnable() {
                                 @Override
                                 public void run() {
-                                    mProvisionDevConfState.setText("Failed");
-                                    mStartProvisionDevConf.setEnabled(true);
+                                    mProvisionDevPropState.setText("Failed");
+                                    mStartProvisionDevProp.setEnabled(true);
                                 }
                             });
                         }
@@ -610,7 +610,7 @@ public class EasysetupActivity extends Activity {
     }
 
     private void addListenerForStartProvisionCloudProp() {
-        mStartProvisionCloudConf.setOnClickListener(new View.OnClickListener() {
+        mStartProvisionCloudProp.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 Thread thread = new Thread() {
@@ -619,8 +619,8 @@ public class EasysetupActivity extends Activity {
                         runOnUiThread(new Runnable() {
                             @Override
                             public void run() {
-                                mProvisionCloudConfState.setText("Progress");
-                                mStartProvisionCloudConf.setEnabled(false);
+                                mProvisionCloudPropState.setText("Progress");
+                                mStartProvisionCloudProp.setEnabled(false);
                             }
                         });
 
@@ -631,6 +631,7 @@ public class EasysetupActivity extends Activity {
 
                             CloudProp cloudProp = new CloudProp();
                             cloudProp.setCloudProp(authCode, authProvider, ciserver);
+                            cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
 
                             mRemoteEnrollee.provisionCloudProperties(cloudProp, new CloudPropProvisioningCallback() {
                                 @Override
@@ -642,20 +643,20 @@ public class EasysetupActivity extends Activity {
                                         public void run() {
                                             if(result.equals(ESResult.ES_OK)) {
                                                 if(state.equals(ESCloudProvState.ES_CLOUD_ENROLLEE_FOUND)) {
-                                                    mProvisionCloudConfState.setText("Found Resource");
+                                                    mProvisionCloudPropState.setText("Found Resource");
                                                 }
                                                 else if(state.equals(ESCloudProvState.ES_CLOUD_PROVISIONING_SUCCESS)) {
-                                                    mProvisionCloudConfState.setText("Success");
+                                                    mProvisionCloudPropState.setText("Success");
                                                 }
                                             }
                                             else {
                                                 if(state.equals(ESCloudProvState.ES_CLOUD_ENROLLEE_NOT_FOUND)) {
-                                                    mProvisionCloudConfState.setText("Not Found Resource");
+                                                    mProvisionCloudPropState.setText("Not Found Resource");
                                                 }
                                                 else if(state.equals(ESCloudProvState.ES_CLOUD_PROVISIONING_ERROR)) {
-                                                    mProvisionCloudConfState.setText("Failed");
+                                                    mProvisionCloudPropState.setText("Failed");
                                                 }
-                                                mStartProvisionCloudConf.setEnabled(true);
+                                                mStartProvisionCloudProp.setEnabled(true);
                                             }
                                         }
                                     });
@@ -666,8 +667,8 @@ public class EasysetupActivity extends Activity {
                             runOnUiThread(new Runnable() {
                                 @Override
                                 public void run() {
-                                    mProvisionCloudConfState.setText("Failed");
-                                    mStartProvisionCloudConf.setEnabled(true);
+                                    mProvisionCloudPropState.setText("Failed");
+                                    mStartProvisionCloudProp.setEnabled(true);
                                 }
                             });
                         }
index 5d028c0..d9c83a9 100644 (file)
                         android:layout_height="wrap_content"\r
                         android:id="@+id/editText_authcode"\r
                         android:layout_marginLeft="20dp"\r
-                        android:layout_gravity="center_vertical" />\r
+                        android:layout_gravity="center_vertical"\r
+                        android:text="authcode" />\r
                 </LinearLayout>\r
 \r
                 <LinearLayout\r
                         android:layout_height="wrap_content"\r
                         android:id="@+id/editText_authprovider"\r
                         android:layout_marginLeft="20dp"\r
-                        android:layout_gravity="center_vertical" />\r
+                        android:layout_gravity="center_vertical"\r
+                        android:text="authprovider" />\r
                 </LinearLayout>\r
 \r
                 <LinearLayout\r
                         android:layout_height="wrap_content"\r
                         android:id="@+id/editText_ciserver"\r
                         android:layout_marginLeft="20dp"\r
-                        android:layout_gravity="center_vertical" />\r
+                        android:layout_gravity="center_vertical"\r
+                        android:text="ciserver" />\r
                 </LinearLayout>\r
 \r
                 <Button\r
index ddf87a6..c94bc05 100755 (executable)
@@ -89,9 +89,23 @@ void printStatus(EnrolleeStatus status)
     cout << "===========================================" << endl;
 }
 
+void provisionSecurityStatusCallback(std::shared_ptr<SecProvisioningStatus> secProvisioningStatus)
+{
+    if(secProvisioningStatus->getESResult() != ES_OK)
+    {
+      cout << "provisionSecurity is failed." << endl;
+      return;
+    }
+    else
+    {
+      cout << "provisionSecurity is success." << endl;
+      cout << "uuid : " << secProvisioningStatus->getDeviceUUID()<< endl;
+    }
+}
+
 void provisionSecurity()
 {
-    // TODO
+    remoteEnrollee->provisionSecurity(provisionSecurityStatusCallback);
 }
 
 void getStatusCallback(std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus)
@@ -215,6 +229,7 @@ void provisionCloudProperty()
 
     CloudProp cloudProp;
     cloudProp.setCloudProp("authCode", "authProvider", "ciServer");
+    cloudProp.setCloudID("f002ae8b-c42c-40d3-8b8d-1927c17bd1b3");
 
     try
     {