From daa465f6f080860bab1c71ccfc963f3cc957eca9 Mon Sep 17 00:00:00 2001 From: Randeep Singh Date: Wed, 9 Sep 2015 18:10:16 +0900 Subject: [PATCH] Added Device dashboard OC APIs 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 Signed-off-by: Woochul Shim Reviewed-on: https://gerrit.iotivity.org/gerrit/2333 Tested-by: jenkins-iotivity Reviewed-by: Sachin Agrawal --- .../include/internal/provisioningdatabasemanager.h | 7 +- .../provisioning/include/ocprovisioningmanager.h | 37 +++++++ .../csdk/security/provisioning/include/pmtypes.h | 10 +- .../csdk/security/provisioning/include/pmutility.h | 10 ++ .../provisioning/src/ocprovisioningmanager.c | 109 +++++++++++++++++++++ .../csdk/security/provisioning/src/pmutility.c | 19 ++++ .../provisioning/src/provisioningdatabasemanager.c | 28 +++--- .../unittest/ocprovisioningmanager.cpp | 21 +++- .../unittest/provisioningdatabasemanager.cpp | 4 +- 9 files changed, 225 insertions(+), 20 deletions(-) diff --git a/resource/csdk/security/provisioning/include/internal/provisioningdatabasemanager.h b/resource/csdk/security/provisioning/include/internal/provisioningdatabasemanager.h index 72d64cc..45588b3 100644 --- a/resource/csdk/security/provisioning/include/internal/provisioningdatabasemanager.h +++ b/resource/csdk/security/provisioning/include/internal/provisioningdatabasemanager.h @@ -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 } diff --git a/resource/csdk/security/provisioning/include/ocprovisioningmanager.h b/resource/csdk/security/provisioning/include/ocprovisioningmanager.h index 2acc7ac..2a0c69a 100644 --- a/resource/csdk/security/provisioning/include/ocprovisioningmanager.h +++ b/resource/csdk/security/provisioning/include/ocprovisioningmanager.h @@ -165,6 +165,36 @@ OCStackResult OCRemoveDevice(void* ctx, unsigned short waitTimeForOwnedDeviceDiscovery, const OCProvisionDev_t* pTargetDev, OCProvisionResultCB resultCallback); +/** + * API to get status of all the devices in current subnet. The status include endpoint information + * and doxm information which can be extracted duing owned and unowned discovery. Along with this + * information. The API will provide information about devices' status + * Device can have following states + * - ON/OFF: Device is switched on or off. + * + * NOTE: Caller need to call OCDeleteDiscoveredDevices to delete memory allocated by this API for out + * variables pOwnedDevList and pUnownedDevList. + * + * @param[in] waitime Wait time for the API. The wait time will be divided by 2, and half of wait time + * will be used for unowned discovery and remaining half for owned discovery. + * @param[out] pOwnedDevList list of owned devices. + * @param[out] pUnownedDevList list of unowned devices. + * @return OC_STACK_OK in case of success and other value otherwise. + */ +OCStackResult OCGetDevInfoFromNetwork(unsigned short waittime, + OCProvisionDev_t** pOwnedDevList, + OCProvisionDev_t** pUnownedDevList); +/** + * This method is used to get linked devices' IDs. + * + * @param[in] uuidOfDevice a target device's uuid. + * @param[out] uuidList information about the list of linked devices' uuids. + * @param[out] numOfDevices total number of linked devices. + * @return OC_STACK_OK in case of success and other value otherwise. + */ +OCStackResult OCGetLinkedStatus(const OicUuid_t* uuidOfDevice, + OCUuidList_t** uuidList, + size_t* numOfDevices); /** * API to delete memory allocated to linked list created by OCDiscover_XXX_Devices API. @@ -173,6 +203,13 @@ OCStackResult OCRemoveDevice(void* ctx, */ void OCDeleteDiscoveredDevices(OCProvisionDev_t *pList); +/** + * API to delete memory allocated to OicUuid_t list. + * + * @param[in] pList Pointer to OicUuid_t list which should be deleted. + */ +void OCDeleteUuidList(OCUuidList_t* pList); + #ifdef __cplusplus } #endif // __cplusplus diff --git a/resource/csdk/security/provisioning/include/pmtypes.h b/resource/csdk/security/provisioning/include/pmtypes.h index a1ce5af..4313e36 100644 --- a/resource/csdk/security/provisioning/include/pmtypes.h +++ b/resource/csdk/security/provisioning/include/pmtypes.h @@ -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; /** diff --git a/resource/csdk/security/provisioning/include/pmutility.h b/resource/csdk/security/provisioning/include/pmutility.h index 7bf7571..15a5f8e 100644 --- a/resource/csdk/security/provisioning/include/pmutility.h +++ b/resource/csdk/security/provisioning/include/pmutility.h @@ -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 diff --git a/resource/csdk/security/provisioning/src/ocprovisioningmanager.c b/resource/csdk/security/provisioning/src/ocprovisioningmanager.c index 4a463ca..4ea4042 100644 --- a/resource/csdk/security/provisioning/src/ocprovisioningmanager.c +++ b/resource/csdk/security/provisioning/src/ocprovisioningmanager.c @@ -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); +} diff --git a/resource/csdk/security/provisioning/src/pmutility.c b/resource/csdk/security/provisioning/src/pmutility.c index 111c180..3708430 100644 --- a/resource/csdk/security/provisioning/src/pmutility.c +++ b/resource/csdk/security/provisioning/src/pmutility.c @@ -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; +} diff --git a/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c b/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c index 5cfd94a..1f5c461 100644 --- a/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c +++ b/resource/csdk/security/provisioning/src/provisioningdatabasemanager.c @@ -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; } diff --git a/resource/csdk/security/provisioning/unittest/ocprovisioningmanager.cpp b/resource/csdk/security/provisioning/unittest/ocprovisioningmanager.cpp index 31477c3..2e38f6b 100644 --- a/resource/csdk/security/provisioning/unittest/ocprovisioningmanager.cpp +++ b/resource/csdk/security/provisioning/unittest/ocprovisioningmanager.cpp @@ -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)); +} diff --git a/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp b/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp index d8cf55a..c77656c 100644 --- a/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp +++ b/resource/csdk/security/provisioning/unittest/provisioningdatabasemanager.cpp @@ -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); } -- 2.7.4