X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=service%2Feasy-setup%2Fmediator%2Frichsdk%2Fsrc%2FEnrolleeResource.cpp;h=a1e35e9df2645785c93a71c7ebd295b0a3f220b1;hb=7f00f942c39b7bc27c7eeecf213a239c3fe4173c;hp=21c5471cfce769a44fe6bfecc55b5cd21331dff4;hpb=5ca344b5d30aa6322c89a460f9c551d5a194be83;p=platform%2Fupstream%2Fiotivity.git diff --git a/service/easy-setup/mediator/richsdk/src/EnrolleeResource.cpp b/service/easy-setup/mediator/richsdk/src/EnrolleeResource.cpp index 21c5471..a1e35e9 100755 --- a/service/easy-setup/mediator/richsdk/src/EnrolleeResource.cpp +++ b/service/easy-setup/mediator/richsdk/src/EnrolleeResource.cpp @@ -36,60 +36,124 @@ namespace OIC EnrolleeResource::EnrolleeResource(std::shared_ptr< OC::OCResource > resource) { m_ocResource = resource; + m_getStatusCb = nullptr; + m_getConfigurationStatusCb = nullptr; + m_devicePropProvStatusCb = nullptr; + m_connectRequestStatusCb = nullptr; } - void EnrolleeResource::checkProvInformationCb(const HeaderOptions& /*headerOptions*/, - const OCRepresentation& rep, const int eCode) + void EnrolleeResource::onEnrolleeResourceSafetyCB(const HeaderOptions& headerOptions, + const OCRepresentation& rep, + const int eCode, + ESEnrolleeResourceCb cb, + std::weak_ptr this_ptr) { - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : %s, eCode = %d", - rep.getUri().c_str(), - eCode); + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onEnrolleeResourceSafetyCB"); + std::shared_ptr Ptr = this_ptr.lock(); + if(Ptr) + { + cb(headerOptions, rep, eCode); + } + } - if (eCode != 0) + void EnrolleeResource::onProvisioningResponse(const HeaderOptions& /*headerOptions*/, + const OCRepresentation& /*rep*/, const int eCode) + { + OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onProvisioningResponse : eCode = %d", + eCode); + + if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED) { - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, - "checkProvInformationCb : Provisioning is failed "); + ESResult result = ESResult::ES_ERROR; + + OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, + "onProvisioningResponse : Provisioning is failed "); + + if(eCode == OCStackResult::OC_STACK_COMM_ERROR) + { + OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, + "can't receive any response from Enrollee by a timeout threshold."); + result = ESResult::ES_COMMUNICATION_ERROR; + } + std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared< - DevicePropProvisioningStatus >(ESResult::ES_ERROR); + DevicePropProvisioningStatus >(result); m_devicePropProvStatusCb(provStatus); return; } - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, - "checkProvInformationCb : Provisioning is success. "); + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "onProvisioningResponse : Provisioning is success. "); std::shared_ptr< DevicePropProvisioningStatus > provStatus = std::make_shared< DevicePropProvisioningStatus >(ESResult::ES_OK); m_devicePropProvStatusCb(provStatus); } + void EnrolleeResource::onGetStatusResponse(const HeaderOptions& /*headerOptions*/, + const OCRepresentation& rep, const int eCode) + { + OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetStatusResponse : eCode = %d", + eCode); + + if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED) + { + ESResult result = ESResult::ES_ERROR; + + OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, + "onGetStatusResponse : onGetStatusResponse is failed "); + + if(eCode == OCStackResult::OC_STACK_COMM_ERROR) + { + OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, + "can't receive any response from Enrollee by a timeout threshold."); + result = ESResult::ES_COMMUNICATION_ERROR; + } + + EnrolleeStatus enrolleeStatus(rep); + std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared< + GetEnrolleeStatus >(result, enrolleeStatus); + + m_getStatusCb(getEnrolleeStatus); + } + else + { + EnrolleeStatus enrolleeStatus(rep); + std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared< + GetEnrolleeStatus >(ESResult::ES_OK, enrolleeStatus); + + m_getStatusCb(getEnrolleeStatus); + } + } + void EnrolleeResource::onGetConfigurationResponse(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode) { - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : %s, eCode = %d", - rep.getUri().c_str(), eCode); + OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onGetConfigurationResponse : eCode = %d", + eCode); - if (eCode != 0) + if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED) { - ESResult result = ESResult::ES_ERROR; + ESResult result = ESResult::ES_ERROR; - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,"onGetConfigurationResponse : onGetConfigurationResponse is failed "); + OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, + "onGetConfigurationResponse : onGetConfigurationResponse is failed "); - if (eCode == OCStackResult::OC_STACK_UNAUTHORIZED_REQ) + if(eCode == OCStackResult::OC_STACK_COMM_ERROR) { - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, - "Mediator is unauthorized from Enrollee."); - result = ESResult::ES_UNAUTHORIZED; + OIC_LOG(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, + "can't receive any response from Enrollee by a timeout threshold."); + result = ESResult::ES_COMMUNICATION_ERROR; } - EnrolleeConf enrolleeConf; + EnrolleeConf enrolleeConf(rep); std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared< GetConfigurationStatus >(result, enrolleeConf); m_getConfigurationStatusCb(getConfigurationStatus); } else { - EnrolleeConf enrolleeConf = parseEnrolleeConfFromRepresentation(rep); + EnrolleeConf enrolleeConf(rep); std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared< GetConfigurationStatus >(ESResult::ES_OK, enrolleeConf); @@ -97,18 +161,109 @@ namespace OIC } } - void EnrolleeResource::registerGetConfigurationStatusCallback(GetConfigurationStatusCb callback) + void EnrolleeResource::onConnectRequestResponse(const HeaderOptions& /*headerOptions*/, + const OCRepresentation& /*rep*/, const int eCode) + { + OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "onConnectRequestResponse : eCode = %d", + eCode); + + if (eCode > OCStackResult::OC_STACK_RESOURCE_CHANGED) + { + ESResult result = ESResult::ES_ERROR; + + OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "onConnectRequestResponse : onConnectRequestResponse is failed "); + + if(eCode == OCStackResult::OC_STACK_COMM_ERROR) + { + OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "can't receive any response from Enrollee by a timeout threshold."); + result = ESResult::ES_COMMUNICATION_ERROR; + } + + std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared< + ConnectRequestStatus >(result); + m_connectRequestStatusCb(connectRequestStatus); + return; + } + + OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, + "onConnectRequestResponse : Provisioning is success. "); + + std::shared_ptr< ConnectRequestStatus > connectRequestStatus = std::make_shared< + ConnectRequestStatus >(ESResult::ES_OK); + m_connectRequestStatusCb(connectRequestStatus); + } + + + void EnrolleeResource::registerGetStatusCallback( + const GetStatusCb callback) + { + m_getStatusCb = callback; + } + + void EnrolleeResource::registerGetConfigurationStatusCallback( + const GetConfigurationStatusCb callback) { m_getConfigurationStatusCb = callback; } - void EnrolleeResource::registerDevicePropProvStatusCallback(DevicePropProvStatusCb callback) + void EnrolleeResource::registerDevicePropProvStatusCallback( + const DevicePropProvStatusCb callback) { m_devicePropProvStatusCb = callback; } + void EnrolleeResource::registerConnectRequestStatusCallback( + const ConnectRequestStatusCb callback) + { + m_connectRequestStatusCb = callback; + } + + void EnrolleeResource::getStatus() + { + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus IN"); + + if (m_ocResource == nullptr) + { + throw ESBadRequestException("Resource is not initialized"); + } + + OC::QueryParamsMap query; + OC::OCRepresentation rep; + + std::function< OCStackResult(void) > getStatus = [&] + { + ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + static_cast( + std::bind(&EnrolleeResource::onGetStatusResponse, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), + shared_from_this()); + + return m_ocResource->get(m_ocResource->getResourceTypes().at(0), + DEFAULT_INTERFACE, query, cb, OC::QualityOfService::HighQos); + }; + + OCStackResult result = getStatus(); + + if (result != OCStackResult::OC_STACK_OK) + { + EnrolleeStatus enrolleeStatus(rep);// = {ES_STATE_INIT, ES_ERRCODE_NO_ERROR}; + std::shared_ptr< GetEnrolleeStatus > getEnrolleeStatus = std::make_shared< + GetEnrolleeStatus >(ESResult::ES_ERROR, enrolleeStatus); + + m_getStatusCb(getEnrolleeStatus); + + return; + } + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getStatus OUT"); + } + void EnrolleeResource::getConfiguration() { + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration IN"); + if (m_ocResource == nullptr) { throw ESBadRequestException("Resource is not initialized"); @@ -118,135 +273,87 @@ namespace OIC OC::OCRepresentation rep; std::function< OCStackResult(void) > getConfigurationStatus = [&] - { return m_ocResource->get(m_ocResource->getResourceTypes().at(0), - BATCH_INTERFACE, query, std::function( + { + ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + static_cast( std::bind(&EnrolleeResource::onGetConfigurationResponse, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3))); + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), + shared_from_this()); + + return m_ocResource->get(m_ocResource->getResourceTypes().at(0), + BATCH_INTERFACE, query, cb, OC::QualityOfService::HighQos); }; OCStackResult result = getConfigurationStatus(); if (result != OCStackResult::OC_STACK_OK) { - EnrolleeConf enrolleeConf; + EnrolleeConf enrolleeConf(rep); std::shared_ptr< GetConfigurationStatus > getConfigurationStatus = std::make_shared< GetConfigurationStatus >(ESResult::ES_ERROR, enrolleeConf); m_getConfigurationStatusCb(getConfigurationStatus); return; } - } - void EnrolleeResource::provisionEnrollee(const DeviceProp& deviceProp) + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getConfiguration OUT"); + } + void EnrolleeResource::provisionProperties(const DeviceProp& deviceProp) { + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties IN"); if (m_ocResource == nullptr) { throw ESBadRequestException("Resource is not initialized"); } OC::QueryParamsMap query; - OC::OCRepresentation provisioningRepresentation; - - provisioningRepresentation.setValue(OC_RSRVD_ES_SSID, deviceProp.WIFI.ssid); - provisioningRepresentation.setValue(OC_RSRVD_ES_CRED, deviceProp.WIFI.pwd); - provisioningRepresentation.setValue(OC_RSRVD_ES_AUTHTYPE, deviceProp.WIFI.authtype); - provisioningRepresentation.setValue(OC_RSRVD_ES_ENCTYPE, deviceProp.WIFI.enctype); - provisioningRepresentation.setValue(OC_RSRVD_ES_LANGUAGE, deviceProp.Device.language); - provisioningRepresentation.setValue(OC_RSRVD_ES_COUNTRY, deviceProp.Device.country); - - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ssid - %s", - (deviceProp.WIFI.ssid).c_str()); - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s", - (deviceProp.WIFI.pwd).c_str()); - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : authtype - %d", - deviceProp.WIFI.authtype); - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : enctype - %d", - deviceProp.WIFI.enctype); - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : language - %s", - (deviceProp.Device.language).c_str()); - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : country - %s", - (deviceProp.Device.country).c_str()); - - m_ocResource->post(OC_RSRVD_ES_RES_TYPE_PROV, BATCH_INTERFACE, - provisioningRepresentation, QueryParamsMap(), - std::function< - void(const HeaderOptions& headerOptions, - const OCRepresentation& rep, const int eCode) >( - std::bind(&EnrolleeResource::checkProvInformationCb, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3))); - } + OC::OCRepresentation provisioningRepresentation = deviceProp.toOCRepresentation(); - EnrolleeConf EnrolleeResource::parseEnrolleeConfFromRepresentation(const OCRepresentation& rep) - { - OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Enter parseEnrolleeConfFromRepresentation"); + ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + static_cast( + std::bind(&EnrolleeResource::onProvisioningResponse, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), + shared_from_this()); - DeviceConfig devConf; - WiFiConfig wifiConf; - bool cloudable = false; + m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE, + provisioningRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos); - std::vector children = rep.getChildren(); + OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "provisionProperties OUT"); + } - for(auto prop = children.begin(); prop != children.end(); ++prop) + void EnrolleeResource::requestToConnect(const std::vector &connectTypes) + { + OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect IN"); + if (m_ocResource == nullptr) { - if(prop->getUri().find(OC_RSRVD_ES_URI_WIFI) != std::string::npos) - { - OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find wifi resource"); - if(prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIMODE) - && prop->hasAttribute(OC_RSRVD_ES_SUPPORTEDWIFIFREQ)) - { - std::vector types = prop->getValue>(OC_RSRVD_ES_SUPPORTEDWIFIMODE); - - for(auto type = types.begin(); type != types.end(); ++type) - { - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIMODE = %d", - *type); - wifiConf.types.push_back(static_cast(*type)); - } + throw ESBadRequestException("Resource is not initialized"); + } - wifiConf.freq = static_cast(prop->getValue(OC_RSRVD_ES_SUPPORTEDWIFIFREQ)); + OC::QueryParamsMap query; + OC::OCRepresentation requestRepresentation; + std::vector connectTypes_int; + connectTypes_int.clear(); + for(auto it : connectTypes) + { + connectTypes_int.push_back(static_cast(it)); + } - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d", - wifiConf.freq); - } - } + requestRepresentation.setValue>(OC_RSRVD_ES_CONNECT, connectTypes_int); - else if(prop->getUri().find(OC_RSRVD_ES_URI_DEVCONF) != std::string::npos) - { - OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find devconf"); - if(prop->hasAttribute(OC_RSRVD_ES_DEVNAME) - && prop->hasAttribute(OC_RSRVD_ES_LANGUAGE) - && prop->hasAttribute(OC_RSRVD_ES_COUNTRY)) - { - //TODO:: setting DeviceID. - //devInfo.id = devId; - devConf.name = prop->getValue(OC_RSRVD_ES_DEVNAME); - devConf.language = prop->getValue(OC_RSRVD_ES_LANGUAGE); - devConf.country = prop->getValue(OC_RSRVD_ES_COUNTRY); - - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_DEVNAME = %s", - devConf.name.c_str()); - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_LANGUAGE = %s", - devConf.language.c_str()); - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_COUNTRY = %s", - devConf.country.c_str()); - } - } + ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, + static_cast( + std::bind(&EnrolleeResource::onConnectRequestResponse, this, + std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)), + shared_from_this()); - else if(prop->getUri().find(OC_RSRVD_ES_URI_CLOUDSERVER) != std::string::npos) - { - OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_RES_TAG, "Find cloudserver"); - cloudable = true; - OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "cloudable = %d", - cloudable); - } - } + m_ocResource->post(OC_RSRVD_ES_RES_TYPE_EASYSETUP, OC_RSRVD_INTERFACE_DEFAULT, + requestRepresentation, QueryParamsMap(), cb, OC::QualityOfService::HighQos); - return EnrolleeConf(devConf, wifiConf, cloudable); + OIC_LOG (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "requestToConnect OUT"); } - } }