Add PMSingleDiscovery function for discover owned/unowned devices in specified endpoint 61/10461/5
authorSijae Kim <sijae.kim@samsung.com>
Mon, 15 Aug 2016 11:04:59 +0000 (20:04 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Fri, 19 Aug 2016 05:55:37 +0000 (05:55 +0000)
this function will return when found one or more device even though timeout is not exceeded

Change-Id: Ib03a775bb4142466274bb38517433803971a4213
Signed-off-by: Sijae Kim <sijae.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/10461
Reviewed-by: Jongmin Choi <jminl.choi@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jongho Park <jh8397.park@samsung.com>
Reviewed-by: dongik Lee <dongik.lee@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/provisioning/include/ocprovisioningmanager.h [changed mode: 0644->0755]
resource/csdk/security/provisioning/include/pmutility.h [changed mode: 0644->0755]
resource/csdk/security/provisioning/src/ocprovisioningmanager.c [changed mode: 0644->0755]
resource/csdk/security/provisioning/src/pmutility.c [changed mode: 0644->0755]
resource/csdk/security/provisioning/unittest/otmunittest.cpp [changed mode: 0644->0755]
resource/include/OCProvisioningManager.h [changed mode: 0644->0755]
resource/provisioning/src/OCProvisioningManager.cpp [changed mode: 0644->0755]
resource/provisioning/unittests/OCProvisioningTest.cpp [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index dd6ac1d..1ab0f10
@@ -41,6 +41,20 @@ extern "C" {
  */\r
 OCStackResult OCInitPM(const char* dbPath);\r
 \r
+/**\r
+ * The function is responsible for discovery of owned/unowned device is specified endpoint.\r
+ * It will return when found one or more device even though timeout is not exceeded\r
+ *\r
+ * @param[in] timeout Timeout in seconds, value till which function will listen to responses from\r
+ *                    server before returning the list of devices.\r
+ * @param[in] host               address of target endpoint\r
+ * @param[in] connType           connectivity type of endpoint\r
+ * @param[out] ppList            List of device.\r
+ * @return OTM_SUCCESS in case of success and other value otherwise.\r
+ */\r
+OCStackResult OCDiscoverSecureResource(unsigned short timeout, const char* host,\r
+                             OCConnectivityType connType, OCProvisionDev_t **ppList);\r
+\r
 /**\r
  * The function is responsible for discovery of device is current subnet. It will list\r
  * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as\r
old mode 100644 (file)
new mode 100755 (executable)
index ddb2968..d3947b1
@@ -39,7 +39,22 @@ extern "C"
 
 #define COAP_TCP_PREFIX "coap+tcp://"
 #define COAPS_TCP_PREFIX "coaps+tcp://"
-    /**
+
+/**
+ * Discover owned/unowned devices in the specified endpoint.
+ * It will return when found one or more device even though timeout is not exceeded
+ *
+ * @param[in] waittime           Timeout in seconds
+ * @param[in] host               address of target endpoint
+ * @param[in] connType           connectivity type of endpoint
+ * @param[out] ppDevicesList      List of OCProvisionDev_t
+ *
+ * @return OC_STACK_OK on success otherwise error.
+ */
+OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
+                                 OCConnectivityType connType, OCProvisionDev_t **ppDevicesList);
+
+/**
  * Discover owned/unowned devices in the same IP subnet. .
  *
  * @param[in] waittime      Timeout in seconds.
old mode 100644 (file)
new mode 100755 (executable)
index ae2f054..67f859a
@@ -65,6 +65,28 @@ OCStackResult OCInitPM(const char* dbPath)
     return PDMInit(dbPath);
 }
 
+/**
+ * The function is responsible for discovery of owned/unowned device is specified endpoint.
+ * It will return when found one or more device even though timeout is not exceeded
+ *
+ * @param[in] timeout Timeout in seconds, value till which function will listen to responses from
+ *                    server before returning the list of devices.
+ * @param[in] host               address of target endpoint
+ * @param[in] connType           connectivity type of endpoint
+ * @param[out] ppList            List of device.
+ * @return OTM_SUCCESS in case of success and other value otherwise.
+ */
+OCStackResult OCDiscoverSecureResource(unsigned short timeout, const char* host,
+                             OCConnectivityType connType, OCProvisionDev_t **ppList)
+{
+    if( ppList == NULL || *ppList != NULL || 0 == timeout || host == NULL)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    return PMSingleDeviceDiscovery(timeout, host, connType, ppList);
+}
+
 /**
  * The function is responsible for discovery of device is current subnet. It will list
  * all the device in subnet which are not yet owned. Please call OCInit with OC_CLIENT_SERVER as
old mode 100644 (file)
new mode 100755 (executable)
index bda2270..f679388
@@ -53,6 +53,8 @@
 typedef struct _DiscoveryInfo{
     OCProvisionDev_t    **ppDevicesList;
     bool                isOwnedDiscovery;
+    bool                isSingleDiscovery;
+    bool                isFound;
 } DiscoveryInfo;
 
 /*
@@ -538,6 +540,10 @@ static OCStackApplicationResult SecurityVersionDiscoveryHandler(void *ctx, OCDoH
 
                 OIC_LOG(INFO, TAG, "Exiting SecVersionDiscoveryHandler.");
                 DeleteVerBinData(ptrVer);
+                if(pDInfo->isSingleDiscovery)
+                {
+                    pDInfo->isFound = true;
+                }
             }
         }
     }
@@ -717,7 +723,7 @@ static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNU
                     return OC_STACK_KEEP_TRANSACTION;
                 }
                 //if this is owned discovery and this is PT's reply, discard it
-                if((pDInfo->isOwnedDiscovery) &&
+                if(((pDInfo->isSingleDiscovery) || (pDInfo->isOwnedDiscovery)) &&
                         (0 == memcmp(&ptrDoxm->deviceID.id, &myId.id, sizeof(myId.id))) )
                 {
                     OIC_LOG(DEBUG, TAG, "discarding provision tool's reply");
@@ -743,6 +749,10 @@ static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNU
                 }
 
                 OIC_LOG(INFO, TAG, "Exiting ProvisionDiscoveryHandler.");
+                if(pDInfo->isSingleDiscovery)
+                {
+                    return OC_STACK_DELETE_TRANSACTION;
+                }
             }
 
             return  OC_STACK_KEEP_TRANSACTION;
@@ -757,6 +767,108 @@ static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNU
     return  OC_STACK_DELETE_TRANSACTION;
 }
 
+
+/**
+ * Discover owned/unowned devices in the specified endpoint.
+ * It will return when found one or more device even though timeout is not exceeded
+ *
+ * @param[in] waittime           Timeout in seconds
+ * @param[in] host               address of target endpoint
+ * @param[in] connType           connectivity type of endpoint
+ * @param[out] ppDevicesList      List of OCProvisionDev_t
+ *
+ * @return OC_STACK_OK on success otherwise error.
+ */
+OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
+                                 OCConnectivityType connType, OCProvisionDev_t **ppDevicesList)
+{
+    OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
+
+    if (NULL != *ppDevicesList)
+    {
+        OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
+    if(NULL == pDInfo)
+    {
+        OIC_LOG(ERROR, TAG, "PMSingleDeviceDiscovery : Memory allocation failed.");
+        return OC_STACK_NO_MEMORY;
+    }
+
+    pDInfo->ppDevicesList = ppDevicesList;
+    pDInfo->isOwnedDiscovery = false;
+    pDInfo->isSingleDiscovery = true;
+    pDInfo->isFound = false;
+
+    OCCallbackData cbData;
+    cbData.cb = &DeviceDiscoveryHandler;
+    cbData.context = (void *)pDInfo;
+    cbData.cd = NULL;
+    OCStackResult res = OC_STACK_ERROR;
+
+    char query[MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1] = { '\0' };
+    if(host == NULL)
+    {
+        host = "";
+    }
+    snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "%s/oic/sec/doxm", host);
+
+    OCDoHandle handle = NULL;
+    res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
+                                     connType, OC_HIGH_QOS, &cbData, NULL, 0);
+    if (res != OC_STACK_OK)
+    {
+        OIC_LOG(ERROR, TAG, "OCStack resource error");
+        OICFree(pDInfo);
+        return res;
+    }
+
+    //Waiting for each response.
+    res = OC_STACK_OK;
+    uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
+    while (OC_STACK_OK == res && pDInfo->isFound == false)
+    {
+        uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
+
+        long elapsed = (long)((currTime - startTime) / MS_PER_SEC);
+        if (elapsed > waittime)
+        {
+            break;
+        }
+        res = OCProcess();
+    }
+
+    if(OC_STACK_OK != res)
+    {
+        OIC_LOG(ERROR, TAG, "Failed to wait response for secure discovery.");
+        OICFree(pDInfo);
+        OCStackResult resCancel = OCCancel(handle, OC_HIGH_QOS, NULL, 0);
+        if(OC_STACK_OK !=  resCancel)
+        {
+            OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
+        }
+        return res;
+    }
+
+    if(*ppDevicesList == NULL)
+    {
+        res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
+    }
+
+    if (OC_STACK_OK != res)
+    {
+        OIC_LOG(ERROR, TAG, "Failed to remove registered callback");
+        OICFree(pDInfo);
+        return res;
+    }
+    OIC_LOG(DEBUG, TAG, "OUT PMSingleDeviceDiscovery");
+    OICFree(pDInfo);
+    return res;
+}
+
+
 /**
  * Discover owned/unowned devices in the same IP subnet. .
  *
@@ -788,6 +900,7 @@ OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisi
 
     pDInfo->ppDevicesList = ppDevicesList;
     pDInfo->isOwnedDiscovery = isOwned;
+    pDInfo->isSingleDiscovery = false;
 
     OCCallbackData cbData;
     cbData.cb = &DeviceDiscoveryHandler;
old mode 100644 (file)
new mode 100755 (executable)
index 40eac29..4d97057
@@ -413,6 +413,26 @@ TEST(InitForOTM, NullParam)
     g_callbackResult = false;
 }
 
+TEST(PerformSecureResourceDiscovery, NullParam)
+{
+    OCStackResult result = OC_STACK_ERROR;
+    OCProvisionDev_t* foundDevice = NULL;
+
+    OIC_LOG(INFO, TAG, "Discovering Owned/Unowned Device using multicast\n");
+    result = OCDiscoverSecureResource(DISCOVERY_TIMEOUT, "", CT_DEFAULT, &foundDevice);
+    EXPECT_EQ(OC_STACK_OK, result);
+
+    int NumOfFoundDevice = 0;
+    OCProvisionDev_t* tempDev = foundDevice;
+    while(tempDev)
+    {
+        NumOfFoundDevice++;
+        tempDev = tempDev->next;
+    }
+    PMDeleteDeviceList(foundDevice);
+
+    EXPECT_EQ(true, NumOfFoundDevice > 0);
+}
 
 TEST(PerformUnownedDeviceDiscovery, NullParam)
 {
old mode 100644 (file)
new mode 100755 (executable)
index 3602cd8..add8105
@@ -143,6 +143,22 @@ namespace OC
             static OCStackResult discoverOwnedDevices(unsigned short timeout,
                     DeviceList_t &list);
 
+            /**
+             * API is responsible for discovery of devices in specified endpoint.
+             * It will return when found one or more device even though timeout is not exceeded
+             *
+             * @param timeout Timeout in seconds, time until which function will listen to
+             *                    responses from server before returning the list of devices.
+             * @param host        address of target endpoint
+             * @param connType    connectivity type of endpoint
+             * @param list List of devices.
+             * @return ::OC_STACK_OK in case of success and other value otherwise.
+             */
+            static OCStackResult discoverSecureResource(unsigned short timeout,
+                    const std::string& host,
+                    OCConnectivityType connType,
+                    DeviceList_t &list);
+
             /**
              * API for registering Ownership transfer methods for a particular transfer Type.
              *
old mode 100644 (file)
new mode 100755 (executable)
index cb13e67..822297a
@@ -121,6 +121,47 @@ namespace OC
         return result;
     }
 
+    OCStackResult OCSecure::discoverSecureResource(unsigned short timeout,
+            const std::string& host,
+            OCConnectivityType connType,
+            DeviceList_t &list)
+    {
+        OCStackResult result;
+        OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
+        auto csdkLock = OCPlatform_impl::Instance().csdkLock();
+        auto cLock = csdkLock.lock();
+
+        if (cLock)
+        {
+            std::lock_guard<std::recursive_mutex> lock(*cLock);
+            result = OCDiscoverSecureResource(timeout, host.c_str(), connType, &pDevList);
+            if (result == OC_STACK_OK)
+            {
+                pCurDev = pDevList;
+                while (pCurDev)
+                {
+                    tmp = pCurDev;
+                    list.push_back(std::shared_ptr<OCSecureResource>(
+                                new OCSecureResource(csdkLock, pCurDev)));
+                    pCurDev = pCurDev->next;
+                    tmp->next = nullptr;
+                }
+            }
+            else
+            {
+                oclog() <<"Secure resource discovery failed!";
+                result = OC_STACK_ERROR;
+            }
+        }
+        else
+        {
+            oclog() <<"Mutex not found";
+            result = OC_STACK_ERROR;
+        }
+
+        return result;
+    }
+
     OCStackResult OCSecure::setOwnerTransferCallbackData(OicSecOxm_t oxm,
             OTMCallbackData_t* callbackData, InputPinCallback inputPin)
     {
old mode 100644 (file)
new mode 100755 (executable)
index 9e44e06..1a9dac6
@@ -51,6 +51,18 @@ namespace OCProvisioningTest
         EXPECT_EQ(OC_STACK_OK, OCSecure::provisionInit(dbPath));
     }
 
+    TEST(DiscoveryTest, SecureResource)
+    {
+        DeviceList_t list;
+        EXPECT_EQ(OC_STACK_OK, OCSecure::discoverSecureResource(TIMEOUT, "", CT_DEFAULT, list));
+    }
+
+    TEST(DiscoveryTest, SecureResourceZeroTimeout)
+    {
+        DeviceList_t list;
+        EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverSecureResource(0, "", CT_DEFAULT, list));
+    }
+
     TEST(DiscoveryTest, UnownedDevices)
     {
         DeviceList_t list;