Add DeviceID Parameter on SingleDeviceDiscovery function 65/12065/25
authorJihun Ha <jihun.ha@samsung.com>
Fri, 7 Oct 2016 15:57:54 +0000 (00:57 +0900)
committerRandeep Singh <randeep.s@samsung.com>
Mon, 10 Oct 2016 03:56:09 +0000 (03:56 +0000)
this Parameter for discover a specified deviceID
If specify the deviceID, It can discover a specified device
when there is one or more device on the network

Change-Id: I99bff0620ee339a42e22e434099772e4175d73d9
Signed-off-by: Sijae Kim <sijae.kim@samsung.com>
Signed-off-by: Parkhi <h_w.park@samsung.com>
Signed-off-by: Jihun Ha <jihun.ha@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/12065
Reviewed-by: Kevin Kane <kkane@microsoft.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
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
resource/csdk/security/provisioning/src/ocprovisioningmanager.c [changed mode: 0644->0755]
resource/csdk/security/provisioning/src/pmutility.c
resource/csdk/security/provisioning/unittest/otmunittest.cpp
resource/include/OCProvisioningManager.h [changed mode: 0644->0755]
resource/provisioning/src/OCProvisioningManager.cpp [changed mode: 0644->0755]
resource/provisioning/unittests/OCProvisioningTest.cpp
resource/provisioning/unittests/SConscript

old mode 100644 (file)
new mode 100755 (executable)
index e0f1422..920dd0c
@@ -42,18 +42,17 @@ extern "C" {
 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
+ * The function is responsible for discovery of owned/unowned device is specified endpoint/deviceID.\r
+ * It will return the found 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
+ *                    server before returning the device.\r
+ * @param[in] deviceID         deviceID of target device.\r
+ * @param[out] ppFoundDevice     OCProvisionDev_t of found 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
+OCStackResult OCDiscoverSingleDevice(unsigned short timeout, const OicUuid_t* deviceID,\r
+                             OCProvisionDev_t **ppFoundDevice);\r
 \r
 /**\r
  * The function is responsible for discovery of device is current subnet. It will list\r
index 5d8c8a7..a791a50 100755 (executable)
@@ -43,18 +43,18 @@ extern "C"
 #define COAPS_TCP_QUERY "coaps+tcp://%s:%d%s"
 
 /**
- * Discover owned/unowned devices in the specified endpoint.
- * It will return when found one or more device even though timeout is not exceeded
+ * Discover owned/unowned device in the specified endpoint/deviceID.
+ * It will return the found 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
+ * @param[in] deviceID           deviceID of target device.
+ * @param[out] ppFoundDevice     OCProvisionDev_t of found device
  *
- * @return OC_STACK_OK on success otherwise error.
+ * @return OC_STACK_OK on success otherwise error.\n
+ *         OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
  */
-OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
-                                 OCConnectivityType connType, OCProvisionDev_t **ppDevicesList);
+OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
+                                 OCProvisionDev_t **ppFoundDevice);
 
 /**
  * Discover owned/unowned devices in the same IP subnet. .
old mode 100644 (file)
new mode 100755 (executable)
index b64e84d..8364a20
@@ -66,25 +66,24 @@ OCStackResult OCInitPM(const char* 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
+ * The function is responsible for discovery of owned/unowned device is specified endpoint/deviceID.
+ * And this function will only return the specified device's response.
  *
  * @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.
+ *                    server before returning the device.
+ * @param[in] deviceID         deviceID of target device.
+ * @param[out] ppFoundDevice     OCProvisionDev_t of found 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)
+OCStackResult OCDiscoverSingleDevice(unsigned short timeout, const OicUuid_t* deviceID,
+                             OCProvisionDev_t **ppFoundDevice)
 {
-    if( ppList == NULL || *ppList != NULL || 0 == timeout || host == NULL)
+    if( NULL == ppFoundDevice || NULL != *ppFoundDevice || 0 == timeout || NULL == deviceID)
     {
         return OC_STACK_INVALID_PARAM;
     }
 
-    return PMSingleDeviceDiscovery(timeout, host, connType, ppList);
+    return PMSingleDeviceDiscovery(timeout, deviceID, ppFoundDevice);
 }
 
 /**
index ee4c607..1e6f610 100755 (executable)
@@ -56,6 +56,7 @@ typedef struct _DiscoveryInfo{
     bool                isOwnedDiscovery;
     bool                isSingleDiscovery;
     bool                isFound;
+    const OicUuid_t     *targetId;
 } DiscoveryInfo;
 
 /*
@@ -723,8 +724,16 @@ static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNU
                     DeleteDoxmBinData(ptrDoxm);
                     return OC_STACK_KEEP_TRANSACTION;
                 }
+                //if targetId and discovered deviceID are different, discard it
+                if ((pDInfo->isSingleDiscovery) &&
+                    (0 != memcmp(&ptrDoxm->deviceID.id, &pDInfo->targetId->id, sizeof(pDInfo->targetId->id))) )
+                {
+                    OIC_LOG(DEBUG, TAG, "Discovered device is not target device");
+                    DeleteDoxmBinData(ptrDoxm);
+                    return OC_STACK_KEEP_TRANSACTION;
+                }
                 //if this is owned discovery and this is PT's reply, discard it
-                if(((pDInfo->isSingleDiscovery) || (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");
@@ -770,27 +779,34 @@ static OCStackApplicationResult DeviceDiscoveryHandler(void *ctx, OCDoHandle UNU
 
 
 /**
- * Discover owned/unowned devices in the specified endpoint.
- * It will return when found one or more device even though timeout is not exceeded
+ * Discover owned/unowned device in the specified endpoint/deviceID.
+ * It will return the found 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
+ * @param[in] deviceID           deviceID of target device.
+ * @param[out] ppFoundDevice     OCProvisionDev_t of found device
  *
- * @return OC_STACK_OK on success otherwise error.
+ * @return OC_STACK_OK on success otherwise error.\n
+ *         OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not initailized.
  */
-OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
-                                 OCConnectivityType connType, OCProvisionDev_t **ppDevicesList)
+OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const OicUuid_t* deviceID,
+                                 OCProvisionDev_t **ppFoundDevice)
 {
     OIC_LOG(DEBUG, TAG, "IN PMSingleDeviceDiscovery");
 
-    if (NULL != *ppDevicesList)
+    if (NULL != *ppFoundDevice)
     {
         OIC_LOG(ERROR, TAG, "List is not null can cause memory leak");
         return OC_STACK_INVALID_PARAM;
     }
 
+    if (NULL == deviceID)
+    {
+        OIC_LOG(ERROR, TAG, "Invalid device ID");
+        return OC_STACK_INVALID_PARAM;
+    }
+
+
     DiscoveryInfo *pDInfo = OICCalloc(1, sizeof(DiscoveryInfo));
     if(NULL == pDInfo)
     {
@@ -798,10 +814,11 @@ OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
         return OC_STACK_NO_MEMORY;
     }
 
-    pDInfo->ppDevicesList = ppDevicesList;
+    pDInfo->ppDevicesList = ppFoundDevice;
     pDInfo->isOwnedDiscovery = false;
     pDInfo->isSingleDiscovery = true;
     pDInfo->isFound = false;
+    pDInfo->targetId = deviceID;
 
     OCCallbackData cbData;
     cbData.cb = &DeviceDiscoveryHandler;
@@ -810,15 +827,11 @@ OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
     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);
+    snprintf(query, MAX_URI_LENGTH + MAX_QUERY_LENGTH + 1, "/oic/sec/doxm");
 
     OCDoHandle handle = NULL;
     res = OCDoResource(&handle, OC_REST_DISCOVER, query, 0, 0,
-                                     connType, OC_HIGH_QOS, &cbData, NULL, 0);
+                                     CT_DEFAULT, OC_HIGH_QOS, &cbData, NULL, 0);
     if (res != OC_STACK_OK)
     {
         OIC_LOG(ERROR, TAG, "OCStack resource error");
@@ -829,7 +842,7 @@ OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
     //Waiting for each response.
     res = OC_STACK_OK;
     uint64_t startTime = OICGetCurrentTime(TIME_IN_MS);
-    while (OC_STACK_OK == res && pDInfo->isFound == false)
+    while (OC_STACK_OK == res && !pDInfo->isFound)
     {
         uint64_t currTime = OICGetCurrentTime(TIME_IN_MS);
 
@@ -853,7 +866,7 @@ OCStackResult PMSingleDeviceDiscovery(unsigned short waittime, const char* host,
         return res;
     }
 
-    if(*ppDevicesList == NULL)
+    if(NULL == *ppFoundDevice)
     {
         res = OCCancel(handle,OC_HIGH_QOS,NULL,0);
     }
@@ -902,6 +915,7 @@ OCStackResult PMDeviceDiscovery(unsigned short waittime, bool isOwned, OCProvisi
     pDInfo->ppDevicesList = ppDevicesList;
     pDInfo->isOwnedDiscovery = isOwned;
     pDInfo->isSingleDiscovery = false;
+    pDInfo->targetId = NULL;
 
     OCCallbackData cbData;
     cbData.cb = &DeviceDiscoveryHandler;
index 26fff98..8040646 100755 (executable)
@@ -418,8 +418,11 @@ TEST(PerformSecureResourceDiscovery, NullParam)
     OCStackResult result = OC_STACK_ERROR;
     OCProvisionDev_t* foundDevice = NULL;
 
+    OicUuid_t uuid;
+    ConvertStrToUuid("11111111-1111-1111-1111-111111111111", &uuid);
+
     OIC_LOG(INFO, TAG, "Discovering Owned/Unowned Device using multicast\n");
-    result = OCDiscoverSecureResource(DISCOVERY_TIMEOUT, "", CT_DEFAULT, &foundDevice);
+    result = OCDiscoverSingleDevice(DISCOVERY_TIMEOUT, &uuid, &foundDevice);
     EXPECT_EQ(OC_STACK_OK, result);
 
     int NumOfFoundDevice = 0;
old mode 100644 (file)
new mode 100755 (executable)
index f42d662..3f1a004
@@ -147,20 +147,20 @@ namespace OC
                     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
+             * API is responsible for discovery of devices in specified endpoint/deviceID.
+             * And this function will only return the specified device's response.
              *
              * @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.
+             *                    responses from server before returning the specified device.
+             * @param deviceID  deviceID of target device
+             * @param foundDevice OCSecureResource object of found device.
+             * @return ::OC_STACK_OK in case of success and other value otherwise.\n
+             *         ::OC_STACK_INVALID_PARAM when deviceID is NULL or ppFoundDevice is not
+             *                                  initailized.
              */
-            static OCStackResult discoverSecureResource(unsigned short timeout,
-                    const std::string& host,
-                    OCConnectivityType connType,
-                    DeviceList_t &list);
+            static OCStackResult discoverSingleDevice(unsigned short timeout,
+                    const OicUuid_t* deviceID,
+                    std::shared_ptr<OCSecureResource> &foundDevice);
 
             /**
              * API for registering Ownership transfer methods for a particular transfer Type.
old mode 100644 (file)
new mode 100755 (executable)
index e422b9e..31700cb
@@ -121,30 +121,29 @@ namespace OC
         return result;
     }
 
-    OCStackResult OCSecure::discoverSecureResource(unsigned short timeout,
-            const std::string& host,
-            OCConnectivityType connType,
-            DeviceList_t &list)
+    OCStackResult OCSecure::discoverSingleDevice(unsigned short timeout,
+            const OicUuid_t* deviceID,
+            std::shared_ptr<OCSecureResource> &foundDevice)
     {
         OCStackResult result;
-        OCProvisionDev_t *pDevList = nullptr, *pCurDev = nullptr, *tmp = nullptr;
+        OCProvisionDev_t *pDev = 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);
+            result = OCDiscoverSingleDevice(timeout, deviceID, &pDev);
             if (result == OC_STACK_OK)
             {
-                pCurDev = pDevList;
-                while (pCurDev)
+                if (pDev)
                 {
-                    tmp = pCurDev;
-                    list.push_back(std::shared_ptr<OCSecureResource>(
-                                new OCSecureResource(csdkLock, pCurDev)));
-                    pCurDev = pCurDev->next;
-                    tmp->next = nullptr;
+                    foundDevice.reset(new OCSecureResource(csdkLock, pDev));
+                }
+                else
+                {
+                    oclog() <<"Not found Secure resource!";
+                    foundDevice.reset();
                 }
             }
             else
index 1a9dac6..41567f0 100755 (executable)
@@ -24,6 +24,7 @@
 #include <OCPlatform_impl.h>
 #include <oxmjustworks.h>
 #include <oxmrandompin.h>
+#include <srmutility.h>
 #include <OCProvisioningManager.h>
 #include <gtest/gtest.h>
 
@@ -53,14 +54,20 @@ namespace OCProvisioningTest
 
     TEST(DiscoveryTest, SecureResource)
     {
-        DeviceList_t list;
-        EXPECT_EQ(OC_STACK_OK, OCSecure::discoverSecureResource(TIMEOUT, "", CT_DEFAULT, list));
+        std::shared_ptr< OC::OCSecureResource > secureResource;
+        OicUuid_t uuid;
+        ConvertStrToUuid("11111111-1111-1111-1111-111111111111", &uuid);
+
+        EXPECT_EQ(OC_STACK_OK, OCSecure::discoverSingleDevice(TIMEOUT, &uuid, secureResource));
     }
 
     TEST(DiscoveryTest, SecureResourceZeroTimeout)
     {
-        DeviceList_t list;
-        EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverSecureResource(0, "", CT_DEFAULT, list));
+        std::shared_ptr< OC::OCSecureResource > secureResource;
+        OicUuid_t uuid;
+        ConvertStrToUuid("11111111-1111-1111-1111-111111111111", &uuid);
+
+        EXPECT_EQ(OC_STACK_INVALID_PARAM, OCSecure::discoverSingleDevice(0, &uuid, secureResource));
     }
 
     TEST(DiscoveryTest, UnownedDevices)
index ea41a36..b3abc6c 100755 (executable)
@@ -48,6 +48,8 @@ provisiontests_env.PrependUnique(CPPPATH = [
                '../../csdk/ocsocket/include',
                '../../csdk/ocrandom/include',
                '../../csdk/logger/include',
+               '../../csdk/connectivity/lib/libcoap-4.1.1/include',
+               '../../../extlibs/cjson/',
                '../../../extlibs/hippomocks-master/HippoMocks',
                '../../../extlibs/hippomocks-master/HippoMocksTest'
                ])