From 04324a1776fa6b4af9084890105b971ceef469bb Mon Sep 17 00:00:00 2001 From: lankamadan Date: Thu, 24 Dec 2015 20:06:48 +0900 Subject: [PATCH] [easysetup] Easysetup Justworks bug fix and sample application bug fix when security is enabled - Bug when security is enabled is fixed Change-Id: Ifb96ee5a57622fab7216a46946d904e237317641 Signed-off-by: lankamadan Reviewed-on: https://gerrit.iotivity.org/gerrit/4723 Tested-by: jenkins-iotivity --- .../sampleapp/mediator/linux/mediator_cpp.cpp | 2 +- service/easy-setup/sdk/common/escommon.h | 30 +- .../sdk/mediator/include/RemoteEnrollee.h | 7 +- .../sdk/mediator/src/EnrolleeSecurity.cpp | 356 ++++++++++++++------- .../easy-setup/sdk/mediator/src/EnrolleeSecurity.h | 16 +- .../easy-setup/sdk/mediator/src/RemoteEnrollee.cpp | 124 +++++-- .../sdk/mediator/src/RemoteEnrolleeResource.cpp | 298 +++++++++++++---- .../sdk/mediator/src/RemoteEnrolleeResource.h | 7 +- 8 files changed, 625 insertions(+), 215 deletions(-) diff --git a/service/easy-setup/sampleapp/mediator/linux/mediator_cpp.cpp b/service/easy-setup/sampleapp/mediator/linux/mediator_cpp.cpp index 91d0aed..9e5125d 100755 --- a/service/easy-setup/sampleapp/mediator/linux/mediator_cpp.cpp +++ b/service/easy-setup/sampleapp/mediator/linux/mediator_cpp.cpp @@ -93,7 +93,7 @@ void initEasySetup() easySetupIntance = EasySetup::getInstance(); - ipaddress = "192.168.0.150"; + ipaddress = "10.113.64.106"; //std::cout << "Enter the target enrollee ipv4 address "; diff --git a/service/easy-setup/sdk/common/escommon.h b/service/easy-setup/sdk/common/escommon.h index 67d628f..520990a 100755 --- a/service/easy-setup/sdk/common/escommon.h +++ b/service/easy-setup/sdk/common/escommon.h @@ -240,18 +240,34 @@ namespace OIC /** * Security Provisioning Status */ - typedef struct { - /// UUID of the target device - std::string devUUID; - /// EasySetup result - ESResult res; - } SecProvisioningResult; + class SecProvisioningResult + { + public: + std::shared_ptr< SecProvisioningResult > shared_ptr; + SecProvisioningResult(std::string deviceUUID, ESResult result) : + m_devUUID(deviceUUID), m_result(result) + { + + } + + std::string getDeviceUUID() + { + return m_devUUID; + } + ESResult getResult() + { + return m_result; + } + private: + std::string m_devUUID; + ESResult m_result; + }; /** * Callback function definition for providing Enrollee security status . */ - typedef std::function< void(SecProvisioningResult) > EnrolleeSecStatusCb; + typedef std::function< void(std::shared_ptr) > EnrolleeSecStatusCb; /** * Callback definition to be invoked when the security stack expects a pin from application. diff --git a/service/easy-setup/sdk/mediator/include/RemoteEnrollee.h b/service/easy-setup/sdk/mediator/include/RemoteEnrollee.h index 485626a..fbd273d 100755 --- a/service/easy-setup/sdk/mediator/include/RemoteEnrollee.h +++ b/service/easy-setup/sdk/mediator/include/RemoteEnrollee.h @@ -66,7 +66,6 @@ namespace OIC /** * Register Security status and other information callback handlers. * - * @param enrolleeSecStatusCb Callback to get security status callbacks. * @param secProvisioningDbCb Callback to be invoked when the stack expects a * path for the provisioning db. * @param securityPinCb Callback to get security pin during pin based ownership transfer. @@ -76,8 +75,8 @@ namespace OIC * * @see SecProvisioningResult */ - ESResult registerSecurityCallbackHandler(EnrolleeSecStatusCb enrolleeSecStatusCb, - SecurityPinCb securityPinCb, SecProvisioningDbPathCb secProvisioningDbPathCb); + ESResult registerSecurityCallbackHandler(SecurityPinCb securityPinCb, + SecProvisioningDbPathCb secProvisioningDbPathCb); #endif //__WITH_DTLS__ /** @@ -134,6 +133,8 @@ namespace OIC bool m_needSecuredEasysetup; void provisioningStatusHandler (std::shared_ptr< ProvisioningStatus > provStatus); + void easySetupSecurityStatusCallback( + std::shared_ptr< SecProvisioningResult > secProvisioningResult); }; } } diff --git a/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.cpp b/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.cpp index 219d429..2446a3d 100755 --- a/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.cpp +++ b/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.cpp @@ -18,6 +18,8 @@ // //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= +#include "base64.h" + #include "EnrolleeSecurity.h" #include "oxmjustworks.h" #include "oxmrandompin.h" @@ -25,21 +27,31 @@ #include "logger.h" #include "ESException.h" #include "oic_malloc.h" +#include "oic_string.h" namespace OIC { namespace Service { + #define MAX_PERMISSION_LENGTH (5) + #define CREATE (1) + #define READ (2) + #define UPDATE (4) + #define DELETE (8) + #define NOTIFY (16) + #define DASH '-' + //TODO : Currently discovery timeout for owned and unowned devices is fixed as 5 // The value should be accepted from the application as a parameter during ocplatform // config call - #define ES_SEC_DISCOVERY_TIMEOUT 5 +#define ES_SEC_DISCOVERY_TIMEOUT 5 EnrolleeSecurity::EnrolleeSecurity( - std::shared_ptr< RemoteEnrolleeResource > remoteEnrolleeResource, - std::string secDbPath) + std::shared_ptr< RemoteEnrolleeResource > remoteEnrolleeResource, + std::string secDbPath) { m_enrolleeSecState = EnrolleeSecState::ES_SEC_UNKNOWN; + m_remoteEnrolleeResource = remoteEnrolleeResource; //Initializing the provisioning client stack using the db path provided by the // application. @@ -52,9 +64,8 @@ namespace OIC } } - ESResult EnrolleeSecurity::registerCallbackHandler( - EnrolleeSecStatusCb enrolleeSecStatusCb, SecurityPinCb securityPinCb, - SecProvisioningDbPathCb secProvisioningDbPathCb) + ESResult EnrolleeSecurity::registerCallbackHandler(EnrolleeSecStatusCb enrolleeSecStatusCb, + SecurityPinCb securityPinCb, SecProvisioningDbPathCb secProvisioningDbPathCb) { m_enrolleeSecStatusCb = enrolleeSecStatusCb; @@ -70,10 +81,9 @@ namespace OIC for (unsigned int i = 0; i < list.size(); i++) { OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Device %d ID %s ", i + 1, - list[i]->getDeviceID().c_str()); - OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "From IP :%s", list[i]->getDevAddr().c_str()); + list[i]->getDeviceID().c_str());OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "From IP :%s", list[i]->getDevAddr().c_str()); - if(list[i]->getDevAddr() == host) + if (list[i]->getDevAddr() == host) { return list[i]; } @@ -82,22 +92,64 @@ namespace OIC return nullptr; } + void EnrolleeSecurity::convertUUIDToString(OicUuid_t uuid, std::string& uuidString) + { + char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*) 0)->id)) + 1] = + { 0, }; + uint32_t outLen = 0; + B64Result b64Ret = B64_OK; + std::ostringstream deviceId(""); + + b64Ret = b64Encode(uuid.id, sizeof(uuid.id), + base64Buff, sizeof(base64Buff), &outLen); + + if (B64_OK == b64Ret) + { + deviceId << base64Buff; + } + uuidString = deviceId.str(); + } + void EnrolleeSecurity::ownershipTransferCb(OC::PMResultList_t *result, int hasError) { if (hasError) { OC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! in OwnershipTransfer"); + + std::shared_ptr< SecProvisioningResult > securityProvisioningStatus = nullptr; + std::string uuid; + convertUUIDToString(result->at(0).deviceId, uuid); + securityProvisioningStatus = std::make_shared< SecProvisioningResult >(uuid, + ES_ERROR); + + m_enrolleeSecStatusCb(securityProvisioningStatus); + return; } else { OC_LOG(DEBUG, ENROLEE_SECURITY_TAG,"Transferred Ownership successfuly for device : "); - OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "UUID : %s",result->at(0).deviceId.id); - - delete result; + std::string uuid; + convertUUIDToString(result->at(0).deviceId, uuid); + OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "UUID : %s",uuid.c_str()); //TODO : Decide if we have to manage the owned/unowned devices. //pOwnedDevList.push_back(pUnownedDevList[transferDevIdx]); //pUnownedDevList.erase(pUnownedDevList.begin() + transferDevIdx); + + OC_LOG(DEBUG, ENROLEE_SECURITY_TAG, + "Ownership transfer success success. Continuing with provisioning ACL"); + + if (provisionAcl() == ES_ERROR) + { + OC_LOG(ERROR, ENROLEE_SECURITY_TAG,"Error!!! in provisionAcl"); + std::shared_ptr< SecProvisioningResult > securityProvisioningStatus = nullptr; + std::string uuid; + convertUUIDToString(result->at(0).deviceId, uuid); + securityProvisioningStatus = std::make_shared< SecProvisioningResult >(uuid, + ES_ERROR); + } + + delete result; } } @@ -111,37 +163,114 @@ namespace OIC { if (hasError) { - OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Error in provisioning operation!"); + OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "provisionCb : Error in provisioning operation!"); } else { - OC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "Received provisioning results: "); + OC_LOG(DEBUG, ENROLEE_SECURITY_TAG, "provisionCb : Received provisioning results: "); for (unsigned int i = 0; i < result->size(); i++) { - OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device",result->at(i).res); OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "UUID : %s",result->at(0).deviceId.id); + OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Result is = %d for device",result->at(i).res); + std::string uuid; + convertUUIDToString(result->at(0).deviceId, uuid); + + OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "UUID : %s",uuid.c_str()); + std::shared_ptr< SecProvisioningResult > securityProvisioningStatus = nullptr; + securityProvisioningStatus = std::make_shared< SecProvisioningResult >(uuid, + ES_OK); + + m_enrolleeSecStatusCb(securityProvisioningStatus); + return; } delete result; } } - ESResult EnrolleeSecurity::performOwnershipTransfer() + /** + * Calculate ACL permission from string to bit + * + * @param[in] temp_psm Input data of ACL permission string + * @param[in,out] pms The pointer of ACL permission value + * @return 0 on success otherwise -1. + */ + int EnrolleeSecurity::CalculateAclPermission(const char *temp_pms, uint16_t *pms) { - ESResult res = ES_ERROR; + int i = 0; + + if (NULL == temp_pms || NULL == pms) + { + return -1; + } + *pms = 0; + while (temp_pms[i] != '\0') + { + switch (temp_pms[i]) + { + case 'C': + { + *pms += CREATE; + i++; + break; + } + case 'R': + { + *pms += READ; + i++; + break; + } + case 'U': + { + *pms += UPDATE; + i++; + break; + } + case 'D': + { + *pms += DELETE; + i++; + break; + } + case 'N': + { + *pms += NOTIFY; + i++; + break; + } + case '_': + { + i++; + break; + } + default: + { + return -1; + } + } + } + return 0; + } + + EasySetupState EnrolleeSecurity::performOwnershipTransfer() + { + EasySetupState ownershipStatus = DEVICE_NOT_OWNED; OC::DeviceList_t pUnownedDevList, pOwnedDevList; pOwnedDevList.clear(); pUnownedDevList.clear(); - OCStackResult result = OCSecure::discoverOwnedDevices(ES_SEC_DISCOVERY_TIMEOUT, + OCStackResult result; + + result = OCSecure::discoverOwnedDevices(ES_SEC_DISCOVERY_TIMEOUT, pOwnedDevList); if (result != OC_STACK_OK) { OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Owned Discovery failed."); - res = ES_ERROR; - - return res; + ownershipStatus = DEVICE_NOT_OWNED; + //Throw exception + throw ESPlatformException(result); + return ownershipStatus; } else if (pOwnedDevList.size()) { @@ -149,40 +278,40 @@ namespace OIC pOwnedDevList.size()); std::shared_ptr< OC::OCSecureResource > ownedDevice = findEnrollee( - m_remoteEnrolleeResource->m_enrolleeNWProvInfo.netAddressInfo.WIFI.ipAddress, + std::string( + m_remoteEnrolleeResource->m_enrolleeNWProvInfo.netAddressInfo.WIFI.ipAddress), pOwnedDevList); - if(ownedDevice) + if (ownedDevice) { - res = ES_OK; + ownershipStatus = DEVICE_OWNED; + return ownershipStatus; } } else { - OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No Secure devices found."); - res = ES_ERROR; - - return res; + OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No owned devices found."); + ownershipStatus = DEVICE_NOT_OWNED; } - result = OCSecure::discoverUnownedDevices(ES_SEC_DISCOVERY_TIMEOUT, - pUnownedDevList); + result = OCSecure::discoverUnownedDevices(ES_SEC_DISCOVERY_TIMEOUT, pUnownedDevList); if (result != OC_STACK_OK) { OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "UnOwned Discovery failed."); - res = ES_ERROR; - - return res; + ownershipStatus = DEVICE_NOT_OWNED; + //Throw exception + throw ESPlatformException(result); + return ownershipStatus; } else if (pUnownedDevList.size()) { OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Found Unowned devices. Count =%d", pUnownedDevList.size()); - std::shared_ptr< OC::OCSecureResource > unownedDevice = + m_unownedDevice = findEnrollee( m_remoteEnrolleeResource->m_enrolleeNWProvInfo.netAddressInfo.WIFI.ipAddress, pUnownedDevList); - if (unownedDevice) + if (m_unownedDevice) { OTMCallbackData_t justWorksCBData; justWorksCBData.loadSecretCB = LoadSecretJustWorksCallback; @@ -193,60 +322,100 @@ namespace OIC OCSecure::setOwnerTransferCallbackData(OIC_JUST_WORKS, &justWorksCBData, NULL); OC_LOG_V(DEBUG, ENROLEE_SECURITY_TAG, "Transfering ownership for : %s ", - unownedDevice->getDeviceID().c_str()); + m_unownedDevice->getDeviceID().c_str()); OC::ResultCallBack ownershipTransferCb = std::bind( &EnrolleeSecurity::ownershipTransferCb, this, std::placeholders::_1, std::placeholders::_2); - if (unownedDevice->doOwnershipTransfer(ownershipTransferCb) != OC_STACK_OK) + if (m_unownedDevice->doOwnershipTransfer(ownershipTransferCb) != OC_STACK_OK) { OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "OwnershipTransferCallback is failed"); - res = ES_ERROR; - } - else - { - if(provisionCreds() == ES_OK) - { - OC_LOG(DEBUG, ENROLEE_SECURITY_TAG, - "provisionCreds success. Continuing with provisioning ACL"); - - if (provisionAcl() == ES_OK) - { - OC_LOG(DEBUG, ENROLEE_SECURITY_TAG, - "provisionAcl success."); - - //returning success; - res = ES_OK; - } - else - { - OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "provisionAcl failed."); - res = ES_ERROR; - } - } - else - { - OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "provisionCreds failed."); - res = ES_ERROR; - } + ownershipStatus = DEVICE_NOT_OWNED; + //Throw exception + throw ESPlatformException(result); } + ownershipStatus = DEVICE_NOT_OWNED; } } else { - OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No Secure devices found."); - res = ES_ERROR; + OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "No unSecure devices found."); + ownershipStatus = DEVICE_NOT_OWNED; + + return ownershipStatus; + } + + return ownershipStatus; + } + + ESResult EnrolleeSecurity::createProvisiongResourceACL(OicSecAcl_t *acl) + { + //TODO : Have to accept subject id of the mediator from application during easysetup + // initialization. + char temp_id[UUID_LENGTH+1] = {"admindeviceUUID0"}; + for (int i = 0, j = 0; temp_id[i] != '\0'; i++) + { + acl->subject.id[j++] = temp_id[i]; + } + + //Always resource length is 1. /oic/prov is the only resource for which needs ACL + // permission has to be set + acl->resourcesLen = 1; + acl->resources = (char **) OICCalloc(acl->resourcesLen, sizeof(char *)); + if (NULL == acl->resources) + { + OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Error while memory allocation"); + return ES_ERROR; + } + + char temp_rsc[MAX_URI_LENGTH] = {OC_RSRVD_ES_URI_PROV}; + + for (size_t i = 0; i < acl->resourcesLen; i++) + { + acl->resources[i] = OICStrdup(temp_rsc); + if (NULL == acl->resources[i]) + { + OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Error while memory allocation"); + return ES_ERROR; + } + } + // Set Permission + char temp_pms[MAX_PERMISSION_LENGTH+1]={"CRUDN"}; + int ret; + do + { + ret = CalculateAclPermission(temp_pms, &(acl->permission)); + } while (0 != ret); - return res; + //TODO : Have to accept subject id of the mediator from application during easysetup + // initialization. + for (int i = 0, j = 0; temp_id[i] != '\0'; i++) + { + acl->subject.id[j++] = temp_id[i]; } - return res; + // Set Rowner + acl->ownersLen = 1;//Always resource owner is only one, which is the mediator + acl->owners = (OicUuid_t *) OICCalloc(acl->ownersLen, sizeof(OicUuid_t)); + if (NULL == acl->owners) + { + OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "Error while memory allocation"); + return ES_ERROR; + } + for (size_t i = 0; i < acl->ownersLen; i++) + { + for (int k = 0, j = 0; temp_id[k] != '\0'; k++) + { + acl->owners[i].id[j++] = temp_id[k]; + } + } + return ES_OK; } ESResult EnrolleeSecurity::provisionAcl() { - // TODO : Currently device id is hardcoded, but this id has to be obtained from the + // TODO : Currently device uuid is hardcoded, but this id has to be obtained from the // application OicSecAcl_t *acl = nullptr; @@ -257,50 +426,21 @@ namespace OIC return ES_ERROR; } -// std::cout << "Please input ACL for selected device: " << std::endl; -// if (0 != InputACL(acl1)) -// { -// break; -// } + if ( createProvisiongResourceACL (acl) == ES_ERROR) + { + return ES_ERROR; + } + OC::ResultCallBack provisioningCb = std::bind(&EnrolleeSecurity::provisionCb, this, - std::placeholders::_1, std::placeholders::_2); + std::placeholders::_1, std::placeholders::_2); - if (m_securedResource->provisionACL(acl, provisioningCb) != OC_STACK_OK) + if (m_unownedDevice->provisionACL(acl, provisioningCb) != OC_STACK_OK) { OC_LOG(ERROR, ENROLEE_SECURITY_TAG, "provisionACL is failed"); + return ES_ERROR; } - return ES_ERROR; - } - - ESResult EnrolleeSecurity::provisionCreds() - { -// int devices[2]; -// -// if (0 != readDeviceNumber(pOwnedDevList, 2, devices)) -// break; -// -// int first = devices[0]; -// int second = devices[1]; -// -// std::cout << "Provision Credentials to devices: " -// << pOwnedDevList[first]->getDeviceID(); -// std::cout << " and " << pOwnedDevList[second]->getDeviceID() << std::endl; -// -// Credential cred(NO_SECURITY_MODE, 0); -// std::cout << "Please input credentials for selected devices: " << std::endl; -// if (0 != InputCredentials(cred)) -// break; -// -// ask = 0; -// -// if (pOwnedDevList[first]->provisionCredentials(cred, *pOwnedDevList[second].get(), -// provisionCB) != OC_STACK_OK) -// { -// ask = 1; -// std::cout << "provisionCredentials is failed" << std::endl; -// } - return ES_ERROR; + return ES_OK; } } } diff --git a/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.h b/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.h index 8adb9d5..2fba49e 100755 --- a/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.h +++ b/service/easy-setup/sdk/mediator/src/EnrolleeSecurity.h @@ -30,7 +30,7 @@ namespace OIC { namespace Service { - #define ENROLEE_SECURITY_TAG "ENROLEE_SECURITY" +#define ENROLEE_SECURITY_TAG "ENROLEE_SECURITY" class RemoteEnrolleeResource; class OCSecureResource; @@ -44,31 +44,33 @@ namespace OIC { public: EnrolleeSecurity(std::shared_ptr< RemoteEnrolleeResource > remoteEnrolleeResource, - std::string secDbPath); + std::string secDbPath); ESResult registerCallbackHandler(EnrolleeSecStatusCb enrolleeSecStatusCb, SecurityPinCb securityPinCb, SecProvisioningDbPathCb secProvisioningDbPathCb); - - ESResult performOwnershipTransfer(); + EasySetupState performOwnershipTransfer(); ESResult provisionCreds(); ESResult provisionAcl(); private: - std::shared_ptr < RemoteEnrolleeResource > m_remoteEnrolleeResource; + std::shared_ptr< RemoteEnrolleeResource > m_remoteEnrolleeResource; EnrolleeSecStatusCb m_enrolleeSecStatusCb; SecurityPinCb m_securityPinCb; SecProvisioningDbPathCb m_secProvisioningDbPathCb; + std::shared_ptr< OC::OCSecureResource > m_unownedDevice; - EnrolleeSecState m_enrolleeSecState; - std::shared_ptr < OC::OCSecureResource > m_securedResource; + EnrolleeSecState m_enrolleeSecState;std::shared_ptr< OC::OCSecureResource > m_securedResource; std::shared_ptr< OC::OCSecureResource > findEnrollee(std::string host, OC::DeviceList_t &list); void provisionCb(OC::PMResultList_t *result, int hasError); void ownershipTransferCb(OC::PMResultList_t *result, int hasError); + void convertUUIDToString(OicUuid_t uuid, std::string& uuidString); + ESResult createProvisiongResourceACL(OicSecAcl_t *acl); + int CalculateAclPermission(const char *temp_pms, uint16_t *pms); }; } } diff --git a/service/easy-setup/sdk/mediator/src/RemoteEnrollee.cpp b/service/easy-setup/sdk/mediator/src/RemoteEnrollee.cpp index 4365a93..3ef1f94 100755 --- a/service/easy-setup/sdk/mediator/src/RemoteEnrollee.cpp +++ b/service/easy-setup/sdk/mediator/src/RemoteEnrollee.cpp @@ -42,8 +42,7 @@ namespace OIC } #ifdef __WITH_DTLS__ - ESResult RemoteEnrollee::registerSecurityCallbackHandler( - EnrolleeSecStatusCb enrolleeSecStatusCb, SecurityPinCb securityPinCb, + ESResult RemoteEnrollee::registerSecurityCallbackHandler(SecurityPinCb securityPinCb, SecProvisioningDbPathCb secProvisioningDbPathCb) { // No need to check NULL for m_secProvisioningDbPathCB as this is not a mandatory @@ -52,7 +51,6 @@ namespace OIC // If PDM.db is found, the provisioning manager operations will succeed. // Otherwise all the provisioning manager operations will fail. m_secProvisioningDbPathCb = secProvisioningDbPathCb; - m_enrolleeSecStatusCb = enrolleeSecStatusCb; m_securityPinCb = securityPinCb; return ES_OK; } @@ -75,8 +73,48 @@ namespace OIC m_easySetupStatusCb = callback; m_remoteResource = std::make_shared< RemoteEnrolleeResource >(m_enrolleeNWProvInfo); + } + } + + void RemoteEnrollee::easySetupSecurityStatusCallback( + std::shared_ptr< SecProvisioningResult > secProvisioningResult) + { + OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "easySetupStatusCallback status is, UUID = %s, " + "Status = %d", secProvisioningResult->getDeviceUUID().c_str(), + secProvisioningResult->getResult()); + + if(secProvisioningResult->getResult() == ES_OK) + { + OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful. " + "Continue with Network information provisioning"); + + m_currentESState = CurrentESState::ES_OWNED; - m_remoteResource->constructResourceObject(); + OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee"); + + RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind( + &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1); + + m_remoteResource->registerProvStatusCallback(provStatusCb); + m_remoteResource->provisionEnrollee(); + } + else + { + OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Ownership and ACL are successful"); + std::shared_ptr< EasySetupStatus > easySetupStatus = nullptr; + easySetupStatus = std::make_shared< EasySetupStatus >(DEVICE_NOT_PROVISIONED, + m_enrolleeNWProvInfo); + if (m_easySetupStatusCb) + { + if (easySetupStatus) + { + m_easySetupStatusCb(easySetupStatus); + } + else + { + m_easySetupStatusCb(nullptr); + } + } } } @@ -163,38 +201,74 @@ namespace OIC { throw ESBadRequestException ("Device not created"); } - else + + ESResult result = ES_ERROR; + + result = m_remoteResource->constructResourceObject(); + + if (result == ES_ERROR) { - m_currentESState = CurrentESState::ES_ONBOARDED; + OC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG, + "Failed to create device using constructResourceObject"); + throw ESBadRequestException ("Device not created"); + } + + m_currentESState = CurrentESState::ES_ONBOARDED; #ifdef __WITH_DTLS__ - if (m_needSecuredEasysetup && m_currentESState < CurrentESState::ES_OWNED) - { - //TODO : DBPath is passed empty as of now. Need to take dbpath from application. - m_enrolleeSecurity = std::make_shared (m_remoteResource, ""); + if (m_needSecuredEasysetup && m_currentESState < CurrentESState::ES_OWNED) + { + EnrolleeSecStatusCb securityProvStatusCb = std::bind( + &RemoteEnrollee::easySetupSecurityStatusCallback, + this, + std::placeholders::_1); + //TODO : DBPath is passed empty as of now. Need to take dbpath from application. + m_enrolleeSecurity = std::make_shared (m_remoteResource, ""); - m_enrolleeSecurity->registerCallbackHandler(m_enrolleeSecStatusCb, - m_securityPinCb, m_secProvisioningDbPathCb); + m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb, + m_securityPinCb, m_secProvisioningDbPathCb); - if (m_enrolleeSecurity->performOwnershipTransfer() == ES_ERROR) + try + { + EasySetupState easySetupState = m_enrolleeSecurity->performOwnershipTransfer(); + if (easySetupState == DEVICE_NOT_OWNED) { - std::shared_ptr< EasySetupStatus > easySetupStatus = nullptr; - - easySetupStatus = std::make_shared < EasySetupStatus - > (DEVICE_NOT_OWNED, m_enrolleeNWProvInfo); + OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, + "performOwnershipTransfer returned : %d", + easySetupState); return; } - } -#endif - - OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee"); + else if (easySetupState == DEVICE_OWNED) + { + OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, + "performOwnershipTransfer returned : %d", + easySetupState); + OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee"); - RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind( - &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1); + RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind( + &RemoteEnrollee::provisioningStatusHandler, + this, std::placeholders::_1); - m_remoteResource->registerProvStatusCallback(provStatusCb); - m_remoteResource->provisionEnrollee(); + m_remoteResource->registerProvStatusCallback(provStatusCb); + m_remoteResource->provisionEnrollee(); + } + } + catch (OCException & e) + { + OC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG, + "Exception for performOwnershipTransfer : %s", e.reason().c_str()); + return ; + } } +#else + OC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Before ProvisionEnrollee"); + + RemoteEnrolleeResource::ProvStatusCb provStatusCb = std::bind( + &RemoteEnrollee::provisioningStatusHandler, this, std::placeholders::_1); + + m_remoteResource->registerProvStatusCallback(provStatusCb); + m_remoteResource->provisionEnrollee(); +#endif } void RemoteEnrollee::stopProvisioning() diff --git a/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp b/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp index 7fc4180..a4118aa 100755 --- a/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp +++ b/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp @@ -19,6 +19,7 @@ //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #include +#include #include "RemoteEnrolleeResource.h" @@ -31,25 +32,29 @@ namespace OIC { namespace Service { - #define ES_REMOTE_ENROLLEE_TAG "ES_REMOTE_ENROLLEE" - static const char ES_PROV_RES_URI[] = "/oic/prov"; + #define ES_REMOTE_ENROLLEE_RES_TAG "ES_REMOTE_ENROLLEE_RES" + #define DISCOVERY_TIMEOUT 5 + + static const char ES_BASE_RES_URI[] = "/oic/res"; + static const char ES_PROV_RES_URI[] = "/oic/prov"; static const char ES_PROV_RES_TYPE[] = "oic.r.prov"; RemoteEnrolleeResource::RemoteEnrolleeResource(EnrolleeNWProvInfo enrolleeNWProvInfo) { m_enrolleeNWProvInfo = enrolleeNWProvInfo; + m_discoveryResponse = false; } void RemoteEnrolleeResource::checkProvInformationCb(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode) { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "checkProvInformationCb : %s, eCode = %d", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : %s, eCode = %d", rep.getUri().c_str(), eCode); - if(eCode != 0) + if (eCode != 0) { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : Provisioning is failed "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); @@ -65,28 +70,30 @@ namespace OIC rep.getValue(OC_RSRVD_ES_TNN, tnn); rep.getValue(OC_RSRVD_ES_CD, cd); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "checkProvInformationCb : ps - %d", ps); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "checkProvInformationCb : tnn - %s", tnn.c_str()); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "checkProvInformationCb : cd - %s", cd.c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : ps - %d", ps); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "checkProvInformationCb : tnn - %s", tnn.c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "checkProvInformationCb : cd - %s", cd.c_str()); //Provisioning status check if (ps == ES_PS_PROVISIONING_COMPLETED) { - if(tnn != std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.ssid)) + if (tnn != std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.ssid)) { - OC_LOG_V (ERROR, ES_REMOTE_ENROLLEE_TAG, + OC_LOG_V (ERROR, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : Network SSID is not the same as the " "SSID provisioned"); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); - m_provStatusCb(provStatus); + m_provStatusCb(provStatus); return; } - if(cd != std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.pwd)) + if (cd != std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.pwd)) { - OC_LOG_V (ERROR, ES_REMOTE_ENROLLEE_TAG, + OC_LOG_V (ERROR, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : Network PWD is not the same as the " "PWD provisioned"); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< @@ -96,7 +103,7 @@ namespace OIC return; } - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : Provisioning is success "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONING_SUCCESS); @@ -105,7 +112,7 @@ namespace OIC } else { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : Provisioning is failed "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); @@ -115,17 +122,16 @@ namespace OIC } void RemoteEnrolleeResource::getProvStatusResponse(const HeaderOptions& /*headerOptions*/, - const OCRepresentation& rep, - const int eCode) + const OCRepresentation& rep, const int eCode) { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : %s, eCode = %d", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : %s, eCode = %d", rep.getUri().c_str(), eCode); if (eCode != 0) { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, - "getProvStatusResponse : Provisioning is failed "); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "getProvStatusResponse : Provisioning is failed "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); m_provStatusCb(provStatus); @@ -140,38 +146,38 @@ namespace OIC rep.getValue(OC_RSRVD_ES_TNN, tnn); rep.getValue(OC_RSRVD_ES_CD, cd); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : ps - %d", - ps); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : tnn - %s", - tnn.c_str()); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : cd - %s", - cd.c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ps - %d", + ps); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : tnn - %s", + tnn.c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : cd - %s", + cd.c_str()); if (ps == ES_PS_NEED_PROVISIONING) //Indicates the need for provisioning { OCRepresentation provisioningRepresentation; provisioningRepresentation.setValue(OC_RSRVD_ES_TNN, - std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.ssid)); + std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.ssid)); provisioningRepresentation.setValue(OC_RSRVD_ES_CD, - std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.pwd)); + std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.pwd)); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : ssid - %s", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s", m_enrolleeNWProvInfo.netAddressInfo.WIFI.ssid); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : pwd - %s", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s", m_enrolleeNWProvInfo.netAddressInfo.WIFI.pwd); m_ocResource->put(provisioningRepresentation, QueryParamsMap(), std::function< void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) >( - std::bind(&RemoteEnrolleeResource::checkProvInformationCb, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3))); + std::bind(&RemoteEnrolleeResource::checkProvInformationCb, this, + std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3))); } - else if(ps == ES_PS_PROVISIONING_COMPLETED) //Indicates that provisioning is completed + else if (ps == ES_PS_PROVISIONING_COMPLETED) //Indicates that provisioning is completed { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : Provisioning is successful"); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONED_ALREADY); @@ -179,35 +185,138 @@ namespace OIC } } - void RemoteEnrolleeResource::registerProvStatusCallback (ProvStatusCb provStatusCb) + void RemoteEnrolleeResource::registerProvStatusCallback(ProvStatusCb provStatusCb) { m_provStatusCb = provStatusCb; } - void RemoteEnrolleeResource::constructResourceObject() + ESResult RemoteEnrolleeResource::ESDiscoveryTimeout(unsigned short waittime) + { + struct timespec startTime; + startTime.tv_sec=0; + startTime.tv_sec=0; + struct timespec currTime; + currTime.tv_sec=0; + currTime.tv_nsec=0; + + ESResult res = ES_OK; + #ifdef _POSIX_MONOTONIC_CLOCK + int clock_res = clock_gettime(CLOCK_MONOTONIC, &startTime); + #else + int clock_res = clock_gettime(CLOCK_REALTIME, &startTime); + #endif + + if (0 != clock_res) + { + return ES_ERROR; + } + + while (ES_OK == res || m_discoveryResponse == false) + { + #ifdef _POSIX_MONOTONIC_CLOCK + clock_res = clock_gettime(CLOCK_MONOTONIC, &currTime); + #else + clock_res = clock_gettime(CLOCK_REALTIME, &currTime); + #endif + + if (0 != clock_res) + { + return ES_ERROR; + } + long elapsed = (currTime.tv_sec - startTime.tv_sec); + if (elapsed > waittime) + { + return ES_OK; + } + if (m_discoveryResponse) + { + res = ES_OK; + } + } + return res; + } + + void RemoteEnrolleeResource::onDeviceDiscovered(std::shared_ptr resource) + { + OC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onDeviceDiscovered"); + + std::string resourceURI; + std::string hostAddress; + try + { + if(resource) + { + // Get the resource URI + resourceURI = resource->uri(); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "URI of the resource: %s", resourceURI.c_str()); + + // Get the resource host address + hostAddress = resource->host(); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "Host address of the resource: %s", hostAddress.c_str()); + + std::size_t foundIP = + hostAddress.find( + std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.ipAddress)); + + if(resourceURI == ES_PROV_RES_URI && foundIP!=std::string::npos) + { + m_ocResource = resource; + m_discoveryResponse = true; + + OC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "Found the device with the resource"); + + return; + } + else + { + OC_LOG (ERROR, ES_REMOTE_ENROLLEE_RES_TAG, "NOT the intended resource."); + } + } + else + { + OC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Resource is invalid"); + } + + } + catch(std::exception& e) + { + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "Exception in foundResource: %s", e.what()); + } + } + + + ESResult RemoteEnrolleeResource::constructResourceObject() { if (m_ocResource != nullptr) { throw ESBadRequestException("Remote resource is already created"); } +#ifdef REMOTE_ARDUINO_ENROLEE + //This process will create OCResource with port 55555 which is specific + // to Arduino WiFi enrollee try { - - std::vector< std::string > m_if = { DEFAULT_INTERFACE }; - std::vector< std::string > m_resTypes = {ES_PROV_RES_TYPE}; + std::vector< std::string > interface = + { DEFAULT_INTERFACE}; + std::vector< std::string > resTypes = + { ES_PROV_RES_TYPE}; - OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_TAG, "Before OCPlatform::constructResourceObject"); + OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Before OCPlatform::constructResourceObject"); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "m_host = %s", - m_enrolleeNWProvInfo.netAddressInfo.WIFI.ipAddress); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "ES_PROV_RES_URI = %s", ES_PROV_RES_URI); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "m_connectivityType = %d", - m_enrolleeNWProvInfo.connType); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "m_resTypes = %s", - m_resTypes.at(0).c_str()); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "m_if = %s", m_if.at(0).c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_host = %s", + m_enrolleeNWProvInfo.netAddressInfo.WIFI.ipAddress); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "ES_PROV_RES_URI = %s", ES_PROV_RES_URI); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_connectivityType = %d", + m_enrolleeNWProvInfo.connType); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "resTypes = %s", + resTypes.at(0).c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "interface = %s", interface.at(0).c_str()); std::string host; if(m_enrolleeNWProvInfo.needSecuredEasysetup) @@ -232,36 +341,99 @@ namespace OIC host.append(":55555"); } - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "HOST = %s", host.c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "HOST = %s", host.c_str()); m_ocResource = OC::OCPlatform::constructResourceObject(host, - ES_PROV_RES_URI, - m_enrolleeNWProvInfo.connType, - true, - m_resTypes, - m_if); - OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, + ES_PROV_RES_URI, + m_enrolleeNWProvInfo.connType, + true, + resTypes, + interface); + OC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "created OCResource : %s", m_ocResource->uri().c_str()); + + return ES_OK; } catch (OCException & e) { - OC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG, + OC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, "Exception for constructResourceObject : %s", e.reason().c_str()); } + +#else + std::string host(""); + std::string query(""); + + if (m_enrolleeNWProvInfo.needSecuredEasysetup) + { + host.append("coaps://"); + } + else + { + host.append("coap://"); + } + + if (m_enrolleeNWProvInfo.connType == CT_ADAPTER_IP) + { + // TODO : RemoteEnrollee is current handling easysetup on IP transport. + // WiFiRemoteEnrollee need to extend RemoteEnrollee for providing IP specific + // Enrollee easysetup. + + host.append(m_enrolleeNWProvInfo.netAddressInfo.WIFI.ipAddress); + } + + query.append(ES_BASE_RES_URI); + query.append("?rt="); + query.append(ES_PROV_RES_TYPE); + + OC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Before OCPlatform::constructResourceObject"); + + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "host = %s", + host.c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "query = %s", query.c_str()); + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_connectivityType = %d", + m_enrolleeNWProvInfo.connType); + + m_discoveryResponse = false; + std::function< void (std::shared_ptr) > onDeviceDiscoveredCb = + std::bind(&RemoteEnrolleeResource::onDeviceDiscovered, this, + std::placeholders::_1); + OCStackResult result = OC::OCPlatform::findResource("", query, CT_DEFAULT, + onDeviceDiscoveredCb); + + if (result != OCStackResult::OC_STACK_OK) + { + OC_LOG(ERROR,ES_REMOTE_ENROLLEE_RES_TAG, + "Failed to create device using constructResourceObject"); + return ES_ERROR; + } + + + ESResult foundResponse = ESDiscoveryTimeout (DISCOVERY_TIMEOUT); + + if (!m_discoveryResponse) + { + OC_LOG(ERROR,ES_REMOTE_ENROLLEE_RES_TAG, + "Failed to create device using constructResourceObject"); + return ES_ERROR; + } + + return ES_OK; +#endif } void RemoteEnrolleeResource::provisionEnrollee() { - if(m_ocResource == nullptr) + if (m_ocResource == nullptr) { - throw ESBadRequestException ("Resource is not initialized"); + throw ESBadRequestException("Resource is not initialized"); } OC::QueryParamsMap query; OC::OCRepresentation rep; - std::function getProvisioingStatus = [&] + std::function< OCStackResult(void) > getProvisioingStatus = [&] { return m_ocResource->get(m_ocResource->getResourceTypes().at(0), m_ocResource->getResourceInterfaces().at(0), query, std::function< @@ -299,9 +471,9 @@ namespace OIC std::function< void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) >( - std::bind(&RemoteEnrolleeResource::checkProvInformationCb, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3))); + std::bind(&RemoteEnrolleeResource::checkProvInformationCb, this, + std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3))); } } } diff --git a/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.h b/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.h index 8e41c28..69a4f65 100755 --- a/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.h +++ b/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.h @@ -83,7 +83,7 @@ namespace OIC * * @see ProvisioningStatus */ - void constructResourceObject(); + ESResult constructResourceObject(); /** * Function for provisioning of Remote Enrollee resource using the information provided. @@ -106,12 +106,17 @@ namespace OIC std::mutex m_mutex; ProvStatusCb m_provStatusCb; EnrolleeNWProvInfo m_enrolleeNWProvInfo; + bool m_discoveryResponse; void getProvStatusResponse(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode); void checkProvInformationCb(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode); + + ESResult ESDiscoveryTimeout(unsigned short waittime); + + void onDeviceDiscovered(std::shared_ptr resource); }; } } -- 2.7.4