Added Device dashboard OC APIs
authorRandeep Singh <randeep.s@samsung.com>
Wed, 9 Sep 2015 09:10:16 +0000 (18:10 +0900)
committerSachin Agrawal <sachin.agrawal@intel.com>
Mon, 14 Sep 2015 16:52:39 +0000 (16:52 +0000)
The apis in this change will be used to get complete state of
all the devices like owned/unowned status,device link status
and power on/off status.

[Patch 2] Updated code according to review comments.
[Patch 7] Rebase

Change-Id: If29a73646a4f77e4c6305de9ab4f3088a69a6eb7
Signed-off-by: Randeep Singh <randeep.s@samsung.com>
Signed-off-by: Woochul Shim <woochul.shim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2333
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
resource/csdk/security/provisioning/include/internal/provisioningdatabasemanager.h
resource/csdk/security/provisioning/include/ocprovisioningmanager.h
resource/csdk/security/provisioning/include/pmtypes.h
resource/csdk/security/provisioning/include/pmutility.h
resource/csdk/security/provisioning/src/ocprovisioningmanager.c
resource/csdk/security/provisioning/src/pmutility.c
resource/csdk/security/provisioning/src/provisioningdatabasemanager.c
resource/csdk/security/provisioning/unittest/ocprovisioningmanager.cpp
resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp

index 72d64cc..45588b3 100644 (file)
@@ -141,17 +141,16 @@ OCStackResult PDMClose();
  * This method is used by provisioning manager free memory allocated to OCUuidList_t lists.
  *
  * @param[in] ptr start pointer of link list.
- * @return OC_STACK_OK in case of success and other value otherwise.
  */
-OCStackResult PDMDestoryOicUuidLinkList(OCUuidList_t* ptr);
+void PDMDestoryOicUuidLinkList(OCUuidList_t* ptr);
 
 /**
  * This method is used by provisioning manager free memory allocated to Stalelist.
  *
  * @param[in] ptr start pointer of link list.
- * @return OC_STACK_OK in case of success and other value otherwise.
+ *
  */
-OCStackResult PDMDestoryStaleLinkList(OCPairList_t* ptr);
+void PDMDestoryStaleLinkList(OCPairList_t* ptr);
 
 #ifdef __cplusplus
 }
index 2acc7ac..2a0c69a 100644 (file)
@@ -165,6 +165,36 @@ OCStackResult OCRemoveDevice(void* ctx,
                              unsigned short waitTimeForOwnedDeviceDiscovery,\r
                              const OCProvisionDev_t* pTargetDev,\r
                              OCProvisionResultCB resultCallback);\r
+/**
+ * API to get status of all the devices in current subnet. The status include endpoint information\r
+ * and doxm information which can be extracted duing owned and unowned discovery. Along with this\r
+ * information. The API will provide information about devices' status\r
+ * Device can have following states\r
+ *  - ON/OFF: Device is switched on or off.\r
+ *\r
+ * NOTE: Caller need to call OCDeleteDiscoveredDevices to delete memory allocated by this API for out\r
+ * variables pOwnedDevList and pUnownedDevList.\r
+ *\r
+ * @param[in] waitime Wait time for the API. The wait time will be divided by 2, and half of wait time\r
+ * will be used for unowned discovery and remaining half for owned discovery.\r
+ * @param[out] pOwnedDevList  list of owned devices.\r
+ * @param[out] pUnownedDevList  list of unowned devices.\r
+ * @return OC_STACK_OK in case of success and other value otherwise.\r
+ */\r
+OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,\r
+                                       OCProvisionDev_t** pOwnedDevList,\r
+                                       OCProvisionDev_t** pUnownedDevList);\r
+/**\r
+ * This method is used to get linked devices' IDs.\r
+ *\r
+ * @param[in] uuidOfDevice a target device's uuid.\r
+ * @param[out] uuidList information about the list of linked devices' uuids.\r
+ * @param[out] numOfDevices total number of linked devices.\r
+ * @return OC_STACK_OK in case of success and other value otherwise.\r
+ */\r
+OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice,\r
+                                  OCUuidList_t** uuidList,\r
+                                  size_t* numOfDevices);\r
 \r
 /**\r
  * API to delete memory allocated to linked list created by OCDiscover_XXX_Devices API.\r
@@ -173,6 +203,13 @@ OCStackResult OCRemoveDevice(void* ctx,
  */\r
 void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList);\r
 \r
+/**\r
+ * API to delete memory allocated to OicUuid_t list.\r
+ *\r
+ * @param[in] pList Pointer to OicUuid_t list which should be deleted.\r
+ */\r
+void OCDeleteUuidList(OCUuidList_t* pList);\r
+\r
 #ifdef __cplusplus\r
 }\r
 #endif // __cplusplus\r
index a1ce5af..4313e36 100644 (file)
@@ -49,6 +49,13 @@ struct OCUuidList
     OCUuidList_t *next;
 };
 
+/*
+ * Device's power on/off state.
+ */
+typedef enum {
+    DEV_STATUS_ON = (1 << 0),
+    DEV_STATUS_OFF = (1 << 1)
+}DeviceStatus;
 
 /**
  * Device Information of discoverd unowned/owned device(s) for provisioning.
@@ -60,7 +67,8 @@ typedef struct OCProvisionDev
     OicSecDoxm_t    *doxm;           /**< Pointer to target's doxm resource. **/
     OCConnectivityType connType;     /**< Connectivity type of endpoint */
     uint16_t        securePort;      /**< secure port **/
-    struct OCProvisionDev  *next;           /**< Next pointer. **/
+    DeviceStatus    devStatus;       /**< status of device **/
+    struct OCProvisionDev  *next;    /**< Next pointer. **/
 }OCProvisionDev_t;
 
 /**
index 7bf7571..15a5f8e 100644 (file)
@@ -101,6 +101,16 @@ bool PMGenerateQuery(bool isSecure,
  */
 void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev);
 
+/** Function to delete matched UUID from the UUIDlist.
+ *
+ * @param[in] pUuidList a pointer to UUID list.
+ * @param[in] targetId  a pointer to UUID to be deleted in the list.
+ *
+ * @return true when deletion is happened, false when no deletion is occured. In case either of
+ * two arguments is null it will return false.
+ */
+bool PMDeleteFromUUIDList(OCUuidList_t *pUuidList, OicUuid_t *targetId);
+
 #ifdef __cplusplus
 }
 #endif
index 4a463ca..4ea4042 100644 (file)
@@ -28,6 +28,8 @@
 #include "secureresourceprovider.h"
 #include "provisioningdatabasemanager.h"
 #include "credresource.h"
+#include "utlist.h"
+
 
 #define TAG "OCPMAPI"
 
@@ -554,3 +556,110 @@ OCStackResult OCProvisionPairwiseDevices(void* ctx, OicSecCredType_t type, size_
     return res;
 
 }
+
+OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime,
+                                       OCProvisionDev_t** pOwnedDevList,
+                                       OCProvisionDev_t** pUnownedDevList)
+{
+    //TODO will be replaced by more efficient logic
+    if (pOwnedDevList == NULL || *pOwnedDevList != NULL || pUnownedDevList == NULL
+         || *pUnownedDevList != NULL)
+    {
+        return OC_STACK_INVALID_PARAM;
+    }
+
+    // Code for unowned discovery
+    OCProvisionDev_t *unownedDevice = NULL;
+    OCStackResult res =  OCDiscoverUnownedDevices(waittime/2, &unownedDevice);
+    if (OC_STACK_OK != res)
+    {
+        OC_LOG(ERROR,TAG, "Error in unowned discovery");
+        return res;
+    }
+
+    // Code for owned discovery
+    OCProvisionDev_t *ownedDevice = NULL;
+    res =  OCDiscoverOwnedDevices(waittime/2, &ownedDevice);
+    if (OC_STACK_OK != res)
+    {
+        OC_LOG(ERROR,TAG, "Error in owned discovery");
+        PMDeleteDeviceList(unownedDevice);
+        return res;
+    }
+
+    // Code to get list of all the owned devices.
+    OCUuidList_t *uuidList = NULL;
+    size_t numOfDevices = 0;
+    res =  PDMGetOwnedDevices(&uuidList, &numOfDevices);
+    if (OC_STACK_OK != res)
+    {
+        OC_LOG(ERROR, TAG, "Error while getting info from DB");
+        PMDeleteDeviceList(unownedDevice);
+        PMDeleteDeviceList(ownedDevice);
+        return res;
+    }
+
+    // Code to compare devices in owned list and deviceid from DB.
+    OCProvisionDev_t* pCurDev = ownedDevice;
+    size_t deleteCnt = 0;
+    while (pCurDev)
+    {
+        if(true == PMDeleteFromUUIDList(uuidList, &pCurDev->doxm->deviceID))
+        {
+            deleteCnt++;
+        }
+        pCurDev = pCurDev->next;
+    }
+    // If there is no remaind device in uuidList, we have to assign NULL to prevent free.
+    if (deleteCnt == numOfDevices)
+    {
+        uuidList = NULL;
+    }
+    // Code to add information of the devices which are currently off in owned list.
+    OCUuidList_t *powerOffDeviceList = uuidList;
+    while (powerOffDeviceList)
+    {
+        OCProvisionDev_t *ptr = (OCProvisionDev_t *)OICCalloc(1, sizeof (OCProvisionDev_t));
+        if (NULL == ptr)
+        {
+            OC_LOG(ERROR,TAG,"Fail to allocate memory");
+            PMDeleteDeviceList(unownedDevice);
+            PMDeleteDeviceList(ownedDevice);
+            OCDeleteUuidList(uuidList);
+            return OC_STACK_NO_MEMORY;
+        }
+
+        ptr->doxm = (OicSecDoxm_t*)OICCalloc(1, sizeof(OicSecDoxm_t));
+        if (NULL == ptr->doxm)
+        {
+            OC_LOG(ERROR,TAG,"Fail to allocate memory");
+            PMDeleteDeviceList(unownedDevice);
+            PMDeleteDeviceList(ownedDevice);
+            OCDeleteUuidList(uuidList);
+            OICFree(ptr);
+            return OC_STACK_NO_MEMORY;
+        }
+
+        memcpy(ptr->doxm->deviceID.id, powerOffDeviceList->dev.id, sizeof(ptr->doxm->deviceID.id));
+
+        ptr->devStatus = DEV_STATUS_OFF;
+        LL_PREPEND(ownedDevice, ptr);
+        powerOffDeviceList = powerOffDeviceList->next;
+
+    }
+    OCDeleteUuidList(uuidList);
+    *pOwnedDevList = ownedDevice;
+    *pUnownedDevList = unownedDevice;
+    return OC_STACK_OK;
+}
+
+OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, OCUuidList_t** uuidList,
+                                 size_t* numOfDevices)
+{
+    return PDMGetLinkedDevices(uuidOfDevice, uuidList, numOfDevices);
+}
+
+void OCDeleteUuidList(OCUuidList_t* pList)
+{
+    PDMDestoryOicUuidLinkList(pList);
+}
index 111c180..3708430 100644 (file)
@@ -656,3 +656,22 @@ void PMPrintOCProvisionDev(const OCProvisionDev_t* pDev)
         OC_LOG(DEBUG, TAG, "+++++ OCProvisionDev_t is NULL +++++");
     }
 }
+
+bool PMDeleteFromUUIDList(OCUuidList_t *pUuidList, OicUuid_t *targetId)
+{
+    if(pUuidList == NULL || targetId == NULL)
+    {
+        return false;
+    }
+    OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
+    LL_FOREACH_SAFE(pUuidList, tmp1, tmp2)
+    {
+        if(0 == memcmp(tmp1->dev.id, targetId->id, sizeof(targetId->id)))
+        {
+            LL_DELETE(pUuidList, tmp1);
+            OICFree(tmp1);
+            return true;
+        }
+    }
+    return false;
+}
index 5cfd94a..1f5c461 100644 (file)
@@ -655,24 +655,28 @@ OCStackResult PDMClose()
     return OC_STACK_OK;
 }
 
-OCStackResult PDMDestoryOicUuidLinkList(OCUuidList_t* ptr)
+void PDMDestoryOicUuidLinkList(OCUuidList_t* ptr)
 {
-    OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
-    LL_FOREACH_SAFE(ptr, tmp1, tmp2)
+    if(ptr)
     {
-        LL_DELETE(ptr, tmp1);
-        OICFree(tmp1);
+        OCUuidList_t *tmp1 = NULL,*tmp2=NULL;
+        LL_FOREACH_SAFE(ptr, tmp1, tmp2)
+        {
+            LL_DELETE(ptr, tmp1);
+            OICFree(tmp1);
+        }
     }
-    return OC_STACK_OK;
 }
 
-OCStackResult PDMDestoryStaleLinkList(OCPairList_t* ptr)
+void PDMDestoryStaleLinkList(OCPairList_t* ptr)
 {
-    OCPairList_t *tmp1 = NULL,*tmp2=NULL;
-    LL_FOREACH_SAFE(ptr, tmp1, tmp2)
+    if(ptr)
     {
-        LL_DELETE(ptr, tmp1);
-        OICFree(tmp1);
+        OCPairList_t *tmp1 = NULL,*tmp2=NULL;
+        LL_FOREACH_SAFE(ptr, tmp1, tmp2)
+        {
+            LL_DELETE(ptr, tmp1);
+            OICFree(tmp1);
+        }
     }
-    return OC_STACK_OK;
 }
index 31477c3..2e38f6b 100644 (file)
@@ -66,4 +66,23 @@ TEST(OCRemoveDeviceTest, ZeroWaitTime)
     unsigned short waitTime = 0;
     OCProvisionDev_t dev1;
     EXPECT_EQ(OC_STACK_INVALID_PARAM, OCRemoveDevice(NULL, waitTime, &dev1, NULL));
-}
\ No newline at end of file
+}
+
+TEST(OCGetDevInfoFromNetworkTest, NullUnOwnedDeviceInfo)
+{
+    OCProvisionDev_t *ownedList = NULL;
+    EXPECT_EQ(OC_STACK_INVALID_PARAM, OCGetDevInfoFromNetwork(0, &ownedList, NULL));
+}
+
+TEST(OCGetDevInfoFromNetworkTest, NullOwnedDeviceInfo)
+{
+    OCProvisionDev_t *unownedList = NULL;
+    EXPECT_EQ(OC_STACK_INVALID_PARAM, OCGetDevInfoFromNetwork(0, NULL, &unownedList));
+}
+
+TEST(OCGetLinkedStatusTest, NULLDeviceID)
+{
+    OCUuidList_t *list = NULL;
+    size_t noOfDevices = 0;
+    EXPECT_EQ(OC_STACK_INVALID_PARAM, OCGetLinkedStatus(NULL, &list, &noOfDevices));
+}
index d8cf55a..c77656c 100644 (file)
@@ -235,10 +235,10 @@ TEST(PDMClose, ValidCase)
 
 TEST(PDMDestoryOicUuidLinkList, NULLParam)
 {
-   EXPECT_EQ(OC_STACK_OK, PDMDestoryOicUuidLinkList(NULL));
+    PDMDestoryOicUuidLinkList(NULL);
 }
 
 TEST(PDMDestoryStaleLinkList, NULLParam)
 {
-   EXPECT_EQ(OC_STACK_OK, PDMDestoryOicUuidLinkList(NULL));
+    PDMDestoryStaleLinkList(NULL);
 }