From: Ashwini Kumar Date: Mon, 14 Sep 2015 04:40:16 +0000 (+0530) Subject: Add C++ API for OCUnlink / OCRemove CAPI to Provisioning Manager X-Git-Tag: 1.0.0-RC1~2^2~5 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=90e35d8cbbff20742f9bf4d67061c64bce41f63f;p=contrib%2Fiotivity.git Add C++ API for OCUnlink / OCRemove CAPI to Provisioning Manager - 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 Reviewed-on: https://gerrit.iotivity.org/gerrit/2497 Reviewed-by: Sachin Agrawal Tested-by: jenkins-iotivity --- diff --git a/resource/include/OCProvisioningManager.h b/resource/include/OCProvisioningManager.h index ec8987b..5c63ace 100644 --- a/resource/include/OCProvisioningManager.h +++ b/resource/include/OCProvisioningManager.h @@ -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. diff --git a/resource/provisioning/examples/provisioningclient.cpp b/resource/provisioning/examples/provisioningclient.cpp index 3928083..5ed14fa 100644 --- a/resource/provisioning/examples/provisioningclient.cpp +++ b/resource/provisioning/examples/provisioningclient.cpp @@ -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 <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; diff --git a/resource/provisioning/src/OCProvisioningManager.cpp b/resource/provisioning/src/OCProvisioningManager.cpp index 31509e8..25c5fd0 100644 --- a/resource/provisioning/src/OCProvisioningManager.cpp +++ b/resource/provisioning/src/OCProvisioningManager.cpp @@ -169,10 +169,8 @@ namespace OC if(cLock) { std::lock_guard 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 lock(*cLock); -//TODO: this change is dependent on changesets #2344. Once these are merged, this code will be -// updated -// result = OCUnlinkDevices(static_cast(context), -// devPtr, device2.getDevPtr(), &OCSecureResource::callbackWrapper); + result = OCUnlinkDevices(static_cast(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 lock(*cLock); -//TODO: this change is dependent on changesets #2344. Once these are merged, this code will be -// updated -// result = OCRemoveDevice(static_cast(context), -// devPtr, &OCSecureResource::callbackWrapper); + result = OCRemoveDevice(static_cast(context), waitTimeForOwnedDeviceDiscovery, + devPtr, &OCSecureResource::callbackWrapper); } else { @@ -433,11 +428,8 @@ namespace OC if(cLock) { std::lock_guard 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() diff --git a/resource/provisioning/unittests/OCProvisioningTest.cpp b/resource/provisioning/unittests/OCProvisioningTest.cpp index a3f4032..6d3c5da 100644 --- a/resource/provisioning/unittests/OCProvisioningTest.cpp +++ b/resource/provisioning/unittests/OCProvisioningTest.cpp @@ -18,7 +18,8 @@ * * *****************************************************************/ -//#include +#include +#include #include #include #include @@ -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); } }