Add C++ API for OCUnlink / OCRemove CAPI to Provisioning Manager
authorAshwini Kumar <k.ashwini@samsung.com>
Mon, 14 Sep 2015 04:40:16 +0000 (10:10 +0530)
committerSachin Agrawal <sachin.agrawal@intel.com>
Wed, 16 Sep 2015 04:57:13 +0000 (04:57 +0000)
  - Based on changeset #2407
[Patch #2]: Addressed review comments, removed warnings,
    updated to changeset #2333
[Patch #3]: Solve build issues

Change-Id: I1e0088c214154c643fd790fdc8fe61741657cf4c
Signed-off-by: Ashwini Kumar <k.ashwini@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/2497
Reviewed-by: Sachin Agrawal <sachin.agrawal@intel.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
resource/include/OCProvisioningManager.h
resource/provisioning/examples/provisioningclient.cpp
resource/provisioning/src/OCProvisioningManager.cpp
resource/provisioning/unittests/OCProvisioningTest.cpp

index ec8987b..5c63ace 100644 (file)
@@ -227,13 +227,16 @@ namespace OC
                     ResultCallBack resultCallback);
 
             /*
-            * API to remove device credential from all devices in subnet.
-            *
-            * @param[in] resultCallback callback provided by API user, callback will be called when
-            *            credential revocation is finished.
+             * API to remove device credential from all devices in subnet.
+             *
+             * @param[in] resultCallback callback provided by API user, callback will be called when
+             *            credential revocation is finished.
+             * @param[in] waitTimeForOwnedDeviceDiscovery Maximum wait time for owned device
+             *            discovery.(seconds)
              * @return  OC_STACK_OK in case of success and other value otherwise.
-            */
-            OCStackResult removeDevice(ResultCallBack resultCallback);
+             */
+            OCStackResult removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
+                    ResultCallBack resultCallback);
 
             /**
              * This method is used to get linked devices' IDs.
index 3928083..5ed14fa 100644 (file)
@@ -60,22 +60,9 @@ using namespace OC;
 DeviceList_t pUnownedDevList, pOwnedDevList;
 static int transferDevIdx, ask = 1;
 
-OicSecAcl_t defaultAcl =
-{
-    {},
-    1,
-    NULL,
-    0x001F,
-    0,
-    NULL,
-    NULL,
-    1,
-    NULL,
-    NULL,
-};
-
-static FILE* client_open(const char *path, const char *mode)
+static FILE* client_open(const char *UNUSED_PARAM, const char *mode)
 {
+    (void)UNUSED_PARAM;
     return fopen(JSON_DB_PATH, mode);
 }
 
@@ -117,7 +104,7 @@ void printUuid(OicUuid_t uuid)
 {
     for (int i = 0; i < UUID_LENGTH; i++)
     {
-        std::cout << uuid.id[i];
+        std::cout <<std::hex << uuid.id[i] << " ";
     }
     std::cout<<std::endl;
 }
@@ -173,8 +160,8 @@ void provisionCB(PMResultList_t *result, int hasError)
        std::cout<< "\nReceived provisioning results: ";
        for (unsigned int i = 0; i < result->size(); i++)
        {
-           for (int j = 0; j < UUID_LENGTH; j++) std::cout << result->at(i).deviceId.id[j];
-           std::cout << ", result = " << result->at(i).res << std::endl;
+           std::cout << "Result is = " << result->at(i).res <<" for device ";
+           printUuid(result->at(i).deviceId);
        }
 
        delete result;
@@ -289,7 +276,7 @@ static void deleteACL(OicSecAcl_t *acl)
 
         /* Clean ACL node itself */
         /* Required only if acl was created in heap */
-        //OICFree((acl));
+        OICFree((acl));
     }
 }
 
@@ -542,10 +529,11 @@ int main(void)
     try
     {
         int choice;
+        OicSecAcl_t *acl1 = nullptr, *acl2 = nullptr;
         if (OCSecure::provisionInit("") != OC_STACK_OK)
         {
             std::cout <<"PM Init failed"<< std::endl;
-            //return 1;
+            return 1;
         }
 
         for (int out = 0; !out;)
@@ -554,6 +542,19 @@ int main(void)
             {
                 sleep(1);
             }
+
+            if (acl1)
+            {
+                deleteACL(acl1);
+                acl1 = nullptr;
+            }
+
+            if (acl2)
+            {
+                deleteACL(acl2);
+                acl2 = nullptr;
+            }
+
             printMenu();
             std::cin >> choice;
             switch(choice) {
@@ -677,23 +678,26 @@ int main(void)
                         std::cout << "Provision ACL for : "<<
                             pOwnedDevList[index]->getDeviceID()<< std::endl;
 
-                        OicSecAcl_t acl = defaultAcl;
+                        acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
+                        if (NULL == acl1)
+                        {
+                            OC_LOG(ERROR, TAG, "Error while memory allocation");
+                            break;
+                        }
 
                         std::cout << "Please input ACL for selected device: " << std::endl;
-                        if (0 != InputACL(&acl))
+                        if (0 != InputACL(acl1))
                         {
-                            deleteACL(&acl);
                             break;
                         }
 
                         ask = 0;
 
-                        if (pOwnedDevList[index]->provisionACL(&acl, provisionCB) != OC_STACK_OK)
+                        if (pOwnedDevList[index]->provisionACL(acl1, provisionCB) != OC_STACK_OK)
                         {
                             ask = 1;
                             std::cout <<"provisionACL is failed"<< std::endl;
                         }
-                        deleteACL(&acl);
                     }
                     break;
                 case 5: //Provision Credentials
@@ -742,75 +746,82 @@ int main(void)
                         if (0 != InputCredentials(cred))
                             break;
 
-                        OicSecAcl_t acl1 = defaultAcl;
+                        acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
+                        if (NULL == acl1)
+                        {
+                            OC_LOG(ERROR, TAG, "Error while memory allocation");
+                            break;
+                        }
 
                         std::cout << "Please input ACL for selected device: " << std::endl;
-                        if (0 != InputACL(&acl1))
+                        if (0 != InputACL(acl1))
                         {
-                            deleteACL(&acl1);
                             break;
                         }
 
-                        OicSecAcl_t acl2 = defaultAcl;
+                        acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
+                        if (NULL == acl2)
+                        {
+                            OC_LOG(ERROR, TAG, "Error while memory allocation");
+                            break;
+                        }
 
                         std::cout << "Please input ACL for selected device: " << std::endl;
-                        if (0 != InputACL(&acl2))
+                        if (0 != InputACL(acl2))
                         {
-                            deleteACL(&acl2);
                             break;
                         }
 
                         ask = 0;
 
-                        if (pOwnedDevList[first]->provisionPairwiseDevices(cred, &acl1,
-                               *pOwnedDevList[second].get(), &acl2, provisionCB) != OC_STACK_OK)
+                        if (pOwnedDevList[first]->provisionPairwiseDevices(cred, acl1,
+                                    *pOwnedDevList[second].get(), acl2, provisionCB) != OC_STACK_OK)
                         {
                             ask = 1;
                             std::cout <<"provisionPairwiseDevices is failed"<< std::endl;
                         }
-                        deleteACL(&acl1);
-                        deleteACL(&acl2);
                     }
                     break;
                 case 7: //Unlink Devices
-                {
-                   int devices[2];
+                    {
+                        int devices[2];
 
-                   if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
+                        if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) break;
 
-                   int first  = devices[0];
-                   int second = devices[1];
+                        int first  = devices[0];
+                        int second = devices[1];
 
-                   std::cout << "Unlink devices: "<< pOwnedDevList[first]->getDeviceID();
-                   std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
+                        std::cout << "Unlink devices: "<< pOwnedDevList[first]->getDeviceID();
+                        std::cout << " and "<< pOwnedDevList[second]->getDeviceID() << std::endl;
 
-                   ask = 0;
+                        ask = 0;
 
-                   if (pOwnedDevList[first]->unlinkDevices(*pOwnedDevList[second].get(),
-                         provisionCB) != OC_STACK_OK)
-                   {
-                       ask = 1;
-                       std::cout <<"removeDevice is failed"<< std::endl;
-                   }
-                   break;
-                }
+                        if (pOwnedDevList[first]->unlinkDevices(*pOwnedDevList[second].get(),
+                                    provisionCB) != OC_STACK_OK)
+                        {
+                            ask = 1;
+                            std::cout <<"unlinkDevice is failed"<< std::endl;
+                        }
+                        break;
+                    }
                 case 8: //Remove Device
-                {
-                   int index;
+                    {
+                        int index;
 
-                   if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
+                        if (0 != readDeviceNumber(pOwnedDevList, 1, &index)) break;
 
-                   std::cout << "Remove Device: "<< pOwnedDevList[index]->getDeviceID()<< std::endl;
+                        std::cout << "Remove Device: "<< pOwnedDevList[index]->getDeviceID()<< std::endl;
 
-                   ask = 0;
+                        ask = 0;
 
-                   if (pOwnedDevList[index]->removeDevice(provisionCB) != OC_STACK_OK)
-                   {
-                       ask = 1;
-                       std::cout <<"removeDevice is failed"<< std::endl;
-                   }
-                   break;
-                }
+                        if (pOwnedDevList[index]->removeDevice(DISCOVERY_TIMEOUT, provisionCB)
+                                != OC_STACK_OK)
+                        {
+                            ask = 1;
+                            std::cout <<"removeDevice is failed"<< std::endl;
+                        }
+                        break;
+                    }
                 case 9: //Get Linked devices
                     {
                         UuidList_t linkedUuid;
index 31509e8..25c5fd0 100644 (file)
@@ -169,10 +169,8 @@ namespace OC
         if(cLock)
         {
             std::lock_guard<std::recursive_mutex> lock(*cLock);
-//TODO: this change is dependent on changesets #2333, 2316. Once these are merged, this code will be
-//      updated
 
-//            result = OCGetDevInfoFromNetwork(nullptr, timeout, &owned, &unowned);
+            result = OCGetDevInfoFromNetwork(timeout, &owned, &unowned);
 
             if (result == OC_STACK_OK)
             {
@@ -379,11 +377,9 @@ namespace OC
             ProvisionContext* context = new ProvisionContext(resultCallback);
 
             std::lock_guard<std::recursive_mutex> lock(*cLock);
-//TODO: this change is dependent on changesets #2344. Once these are merged, this code will be
-//      updated
 
-//           result = OCUnlinkDevices(static_cast<void*>(context),
-//                   devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
+            result = OCUnlinkDevices(static_cast<void*>(context),
+                    devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper);
         }
         else
         {
@@ -393,7 +389,8 @@ namespace OC
         return result;
     }
 
-    OCStackResult OCSecureResource::removeDevice(ResultCallBack resultCallback)
+    OCStackResult OCSecureResource::removeDevice(unsigned short waitTimeForOwnedDeviceDiscovery,
+            ResultCallBack resultCallback)
     {
         if(!resultCallback)
         {
@@ -409,11 +406,9 @@ namespace OC
             ProvisionContext* context = new ProvisionContext(resultCallback);
 
             std::lock_guard<std::recursive_mutex> lock(*cLock);
-//TODO: this change is dependent on changesets #2344. Once these are merged, this code will be
-//      updated
 
-//           result = OCRemoveDevice(static_cast<void*>(context),
-//                   devPtr, &OCSecureResource::callbackWrapper);
+            result = OCRemoveDevice(static_cast<void*>(context), waitTimeForOwnedDeviceDiscovery,
+                    devPtr, &OCSecureResource::callbackWrapper);
         }
         else
         {
@@ -433,11 +428,8 @@ namespace OC
         if(cLock)
         {
             std::lock_guard<std::recursive_mutex> lock(*cLock);
-//TODO: this change is dependent on changesets #2333, 2316. Once these are merged, this code will be 
-//      updated
 
-#if CHANGESET_2316
-            OicUuidList* linkedDevs = nullptr, *tmp = nullptr;
+            OCUuidList_t* linkedDevs = nullptr, *tmp = nullptr;
             result = OCGetLinkedStatus(&devUuid, &linkedDevs, &numOfDevices);
             if (result == OC_STACK_OK)
             {
@@ -445,9 +437,8 @@ namespace OC
                 {
                     uuidList.push_back(tmp->dev);
                 }
-                OCDeleteUuidList(&linkedDevs);
+                OCDeleteUuidList(linkedDevs);
             }
-#endif
         }
         else
         {
@@ -468,7 +459,10 @@ namespace OC
         b64Ret = b64Encode(devPtr->doxm->deviceID.id, sizeof(devPtr->doxm->deviceID.id), base64Buff,
                 sizeof(base64Buff), &outLen);
 
-        deviceId << base64Buff;
+        if (B64_OK == b64Ret)
+        {
+            deviceId << base64Buff;
+        }
         return deviceId.str();
     }
 
@@ -487,10 +481,7 @@ namespace OC
     int OCSecureResource::getDeviceStatus()
     {
         validateSecureResource();
-//TODO: this change is dependent on changesets #2333, 2316. Once these are merged, this code will be 
-//      updated
-//      return (int)devPtr->devStatus;
-        return 0;
+        return (int)devPtr->devStatus;
     }
 
     bool OCSecureResource::getOwnedStatus()
index a3f4032..6d3c5da 100644 (file)
@@ -18,7 +18,8 @@
  *
  * *****************************************************************/
 
-//#include <ocprovisioningmanager.h>
+#include <ocstack.h>
+#include <oic_malloc.h>
 #include <OCApi.h>
 #include <OCPlatform_impl.h>
 #include <oxmjustworks.h>
@@ -32,21 +33,10 @@ namespace OCProvisioningTest
 {
     using namespace OC;
 
-    static OicSecAcl_t defaultAcl =
-    { {},
-        1,
-        NULL,
-        0x001F,
-        0,
-        NULL,
-        NULL,
-        1,
-        NULL,
-        NULL,
-    };
-
     void resultCallback(PMResultList_t *result, int hasError)
     {
+        (void)result;
+        (void)hasError;
     }
 
     TEST(ProvisionInitTest, TestWithEmptyPath)
@@ -148,8 +138,9 @@ namespace OCProvisioningTest
     TEST(ProvisionAclTest, ProvisionAclTestNullCallback)
     {
         OCSecureResource device;
-        OicSecAcl_t acl = defaultAcl;
-        EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionACL(&acl, nullptr));
+        OicSecAcl_t *acl = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
+        EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionACL(acl, nullptr));
+        OICFree(acl);
     }
 
     TEST(ProvisionAclTest, ProvisionAclTestNullCallbackNUllAcl)
@@ -169,9 +160,12 @@ namespace OCProvisioningTest
     {
         OCSecureResource device, dev2;
         Credential cred;
-        OicSecAcl_t acl1 = defaultAcl, acl2 = defaultAcl;
-        EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionPairwiseDevices(cred, &acl1,
-                    dev2, &acl2, nullptr));
+        OicSecAcl_t *acl1 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
+        OicSecAcl_t *acl2 = (OicSecAcl_t *)OICCalloc(1,sizeof(OicSecAcl_t));
+        EXPECT_EQ(OC_STACK_INVALID_PARAM, device.provisionPairwiseDevices(cred, acl1,
+                    dev2, acl2, nullptr));
+        OICFree(acl1);
+        OICFree(acl2);
     }
 
 }