From 7ea088fe39d55a06d6390c3d01c8a1ce0f35f8c7 Mon Sep 17 00:00:00 2001 From: lankamadan Date: Fri, 11 Dec 2015 19:39:52 +0900 Subject: [PATCH] [easysetup] Bug fix, code cleanup and function name changes for better readability - Provisioning status 0 was considered for checking success. Now changed to 2. - PUT is retained based on the spec. PUT request is appropriate for provisioning information to the enrollee. When an enrollee receives the put request, the entire resource information should be overwritten. Change-Id: Ib001c19c8a8ed74a967251c1be4219fd9cc70cdf Signed-off-by: lankamadan Reviewed-on: https://gerrit.iotivity.org/gerrit/4527 Tested-by: jenkins-iotivity --- service/easy-setup/sdk/common/escommon.h | 6 +- .../sdk/enrollee/src/resourceHandler.cpp | 21 ++++- .../sdk/mediator/src/RemoteEnrolleeResource.cpp | 97 ++++++++++++---------- .../sdk/mediator/src/RemoteEnrolleeResource.h | 4 +- 4 files changed, 82 insertions(+), 46 deletions(-) diff --git a/service/easy-setup/sdk/common/escommon.h b/service/easy-setup/sdk/common/escommon.h index 1e94983..67d628f 100755 --- a/service/easy-setup/sdk/common/escommon.h +++ b/service/easy-setup/sdk/common/escommon.h @@ -53,7 +53,11 @@ #define OC_RSRVD_ES_URI_PROV "/oic/prov" #define OC_RSRVD_ES_URI_NET "/oic/net" - +/** + * @brief Defines for Provisioning status accepted values + */ +#define ES_PS_NEED_PROVISIONING 1 +#define ES_PS_PROVISIONING_COMPLETED 2 typedef enum { diff --git a/service/easy-setup/sdk/enrollee/src/resourceHandler.cpp b/service/easy-setup/sdk/enrollee/src/resourceHandler.cpp index 74efd8b..f066367 100755 --- a/service/easy-setup/sdk/enrollee/src/resourceHandler.cpp +++ b/service/easy-setup/sdk/enrollee/src/resourceHandler.cpp @@ -169,6 +169,23 @@ OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, return ehResult; } + //TODO : ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed. + // A new request for provisioning means overriding existing network provisioning information. + // Metadata to indicate that it is override is needed. The metadata can be a new attribute + // should be added to the /oic/prov resource indicating to override the existing network + // information. + if (g_prov.ps == ES_PS_PROVISIONING_COMPLETED) + { + OC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. " + "This a request to override the existing the network provisioning information"); + } + + // PUT request is appropriate for provisioning information to the enrollee. + // When an enrollee receives the put request, the entire resource information should + // be overwritten. + sprintf(g_prov.tnn, "%s", ""); + sprintf(g_prov.cd, "%s", ""); + char* tnn; if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn)) { @@ -193,6 +210,9 @@ OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OC_LOG_V(INFO, ES_RH_TAG, "g_prov.cd %s", g_prov.cd); + g_prov.ps = 2; + OC_LOG_V(INFO, ES_RH_TAG, "g_prov.ps %d", g_prov.ps); + g_flag = 1; OCRepPayload *getResp = constructResponse(ehRequest); @@ -232,7 +252,6 @@ OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, char* tr; if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr)) { - // Triggering ehResult = OC_EH_OK; } diff --git a/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp b/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp index 9284ea8..7fc4180 100755 --- a/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp +++ b/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.cpp @@ -40,17 +40,17 @@ namespace OIC m_enrolleeNWProvInfo = enrolleeNWProvInfo; } - void RemoteEnrolleeResource::onProvPostResource(const HeaderOptions& /*headerOptions*/, + void RemoteEnrolleeResource::checkProvInformationCb(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode) { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvPostResource : %s, eCode = %d", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "checkProvInformationCb : %s, eCode = %d", rep.getUri().c_str(), eCode); if(eCode != 0) { OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, - "onProvPostResource : Provisioning is failed "); + "checkProvInformationCb : Provisioning is failed "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); m_provStatusCb(provStatus); @@ -65,39 +65,48 @@ namespace OIC rep.getValue(OC_RSRVD_ES_TNN, tnn); rep.getValue(OC_RSRVD_ES_CD, cd); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvPostResource : ps - %d", - ps);OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : tnn - %s", - tnn.c_str());OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : cd - %s", - cd.c_str()); + 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()); //Provisioning status check - if (ps == 0) + if (ps == ES_PS_PROVISIONING_COMPLETED) { - OC::QueryParamsMap query; - OC::OCRepresentation rep; - - OCStackResult result = m_ocResource->get(m_ocResource->getResourceTypes().at(0), - m_ocResource->getResourceInterfaces().at(0), query, - std::function< - void(const HeaderOptions& headerOptions, - const OCRepresentation& rep, const int eCode) >( - std::bind(&RemoteEnrolleeResource::onProvResource, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3))); + if(tnn != std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.ssid)) + { + OC_LOG_V (ERROR, ES_REMOTE_ENROLLEE_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); + return; + } - if(result != OCStackResult::OC_STACK_OK) + if(cd != std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.pwd)) { + OC_LOG_V (ERROR, ES_REMOTE_ENROLLEE_TAG, + "checkProvInformationCb : Network PWD is not the same as the " + "PWD provisioned"); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); m_provStatusCb(provStatus); return; } + + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, + "checkProvInformationCb : Provisioning is success "); + std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< + ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONING_SUCCESS); + m_provStatusCb(provStatus); + return; } else { OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, - "onProvResource : Provisioning is failed "); + "checkProvInformationCb : Provisioning is failed "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); m_provStatusCb(provStatus); @@ -105,18 +114,18 @@ namespace OIC } } - void RemoteEnrolleeResource::onProvResource(const HeaderOptions& /*headerOptions*/, + void RemoteEnrolleeResource::getProvStatusResponse(const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep, const int eCode) { - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : %s, eCode = %d", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : %s, eCode = %d", rep.getUri().c_str(), eCode); if (eCode != 0) { OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, - "onProvResource : Provisioning is failed "); + "getProvStatusResponse : Provisioning is failed "); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); m_provStatusCb(provStatus); @@ -131,14 +140,14 @@ namespace OIC rep.getValue(OC_RSRVD_ES_TNN, tnn); rep.getValue(OC_RSRVD_ES_CD, cd); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : ps - %d", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : ps - %d", ps); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : tnn - %s", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : tnn - %s", tnn.c_str()); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : cd - %s", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : cd - %s", cd.c_str()); - if (ps == 1) //ps == 1 indicates the need for provisioning + if (ps == ES_PS_NEED_PROVISIONING) //Indicates the need for provisioning { OCRepresentation provisioningRepresentation; @@ -147,23 +156,23 @@ namespace OIC provisioningRepresentation.setValue(OC_RSRVD_ES_CD, std::string(m_enrolleeNWProvInfo.netAddressInfo.WIFI.pwd)); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : ssid - %s", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "getProvStatusResponse : ssid - %s", m_enrolleeNWProvInfo.netAddressInfo.WIFI.ssid); - OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_TAG, "onProvResource : pwd - %s", + OC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_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::onProvPostResource, this, + std::bind(&RemoteEnrolleeResource::checkProvInformationCb, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))); } - else if(ps == 0) //ps == 1 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, - "onProvResource : Provisioning is successful"); + "getProvStatusResponse : Provisioning is successful"); std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONED_ALREADY); m_provStatusCb(provStatus); @@ -252,14 +261,18 @@ namespace OIC OC::QueryParamsMap query; OC::OCRepresentation rep; - OCStackResult result = m_ocResource->get(m_ocResource->getResourceTypes().at(0), - m_ocResource->getResourceInterfaces().at(0), query, - std::function< - void(const HeaderOptions& headerOptions, const OCRepresentation& rep, - const int eCode) >( - std::bind(&RemoteEnrolleeResource::onProvResource, this, - std::placeholders::_1, std::placeholders::_2, - std::placeholders::_3))); + std::function getProvisioingStatus = [&] + { return m_ocResource->get(m_ocResource->getResourceTypes().at(0), + m_ocResource->getResourceInterfaces().at(0), query, + std::function< + void(const HeaderOptions& headerOptions, const OCRepresentation& rep, + const int eCode) >( + std::bind(&RemoteEnrolleeResource::getProvStatusResponse, this, + std::placeholders::_1, std::placeholders::_2, + std::placeholders::_3))); + }; + + OCStackResult result = getProvisioingStatus(); if (result != OCStackResult::OC_STACK_OK) { @@ -286,7 +299,7 @@ namespace OIC std::function< void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) >( - std::bind(&RemoteEnrolleeResource::onProvPostResource, this, + 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 cc520ec..8e41c28 100755 --- a/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.h +++ b/service/easy-setup/sdk/mediator/src/RemoteEnrolleeResource.h @@ -107,10 +107,10 @@ namespace OIC ProvStatusCb m_provStatusCb; EnrolleeNWProvInfo m_enrolleeNWProvInfo; - void onProvResource(const HeaderOptions& headerOptions, const OCRepresentation& rep, + void getProvStatusResponse(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode); - void onProvPostResource(const HeaderOptions& headerOptions, const OCRepresentation& rep, + void checkProvInformationCb(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode); }; } -- 2.7.4