void RemoteEnrollee::provisionDeviceProperties(const DeviceProp& deviceProp, const DevicePropProvStatusCb callback) { OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter provisionDeviceProperties"); if(!callback) { throw ESInvalidParameterException("Callback is empty"); } m_devicePropProvStatusCb = callback; if (m_enrolleeResource == nullptr) { throw ESBadRequestException ("Device not created"); } if(deviceProp.getSsid().empty()) { throw ESBadRequestException ("Invalid Provisiong Data."); } DevicePropProvStatusCb devicePropProvStatusCb = std::bind( &RemoteEnrollee::devicePropProvisioningStatusHandler, this, std::placeholders::_1); m_enrolleeResource->registerDevicePropProvStatusCallback(devicePropProvStatusCb); m_enrolleeResource->provisionEnrollee(deviceProp); }
void RemoteEnrolleeResource::provisionEnrollee() { if (m_ocResource == nullptr) { throw ESBadRequestException("Resource is not initialized"); } OC::QueryParamsMap query; OC::OCRepresentation rep; std::function< OCStackResult(void) > 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) { std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared< ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR); m_provStatusCb(provStatus); return; } }
void RemoteEnrollee::initCloudResource() { ESResult result = ES_ERROR; if (m_cloudResource != nullptr) { throw ESBadRequestException ("Already created"); } result = discoverResource(); if (result == ES_ERROR) { OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_TAG, "Failed to create resource object using discoverResource"); throw ESBadRequestException ("Resource object not created"); } else { if(m_ocResource != nullptr) { m_cloudResource = std::make_shared<CloudResource>(m_ocResource); std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared< CloudPropProvisioningStatus >(ESResult::ES_OK, ESCloudProvState::ES_CLOUD_ENROLLEE_FOUND); m_cloudPropProvStatusCb(provStatus); } else { throw ESBadGetException ("Resource handle is invalid"); } } }
void RemoteEnrollee::getConfiguration(const GetConfigurationStatusCb callback) { if(!callback) { throw ESInvalidParameterException("Callback is empty"); } if (m_enrolleeResource == nullptr) { throw ESBadRequestException ("Device not created"); } m_getConfigurationStatusCb = callback; GetConfigurationStatusCb getConfigurationStatusCb = std::bind( &RemoteEnrollee::getConfigurationStatusHandler, this, std::placeholders::_1); m_enrolleeResource->registerGetConfigurationStatusCallback(getConfigurationStatusCb); m_enrolleeResource->getConfiguration(); }
void RemoteEnrollee::registerEasySetupStatusHandler(EasySetupStatusCB callback) { OIC_LOG ( DEBUG, ES_REMOTE_ENROLLEE_TAG, "Entered registerStatusHandler"); if(!callback) { throw ESInvalidParameterException("Callback is empty"); } if (m_easySetupStatusCb) { throw ESBadRequestException("Callback handler already registered"); } else { m_easySetupStatusCb = callback; m_remoteResource = std::make_shared< RemoteEnrolleeResource >(m_ProvConfig, m_wifiOnboardingconn); } }
void RemoteEnrolleeResource::triggerNetworkConnection() { if (m_ocResource == nullptr) { throw ESBadRequestException("Resource is not initialized"); } OCRepresentation provisioningRepresentation; provisioningRepresentation.setValue(OC_RSRVD_ES_TR, 1); m_ocResource->post(provisioningRepresentation, QueryParamsMap(), std::function< void(const HeaderOptions& headerOptions, const OCRepresentation& rep, const int eCode) >( std::bind(&RemoteEnrolleeResource::triggerNetworkConnectionCb, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))); }
void RemoteEnrollee::provisionCloudProperties(const CloudProp& cloudProp, const CloudPropProvStatusCb callback) { OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Enter provisionCloudProperties"); if(!callback) { throw ESInvalidParameterException("Callback is empty"); } m_cloudPropProvStatusCb = callback; if(cloudProp.getAuthCode().empty() || cloudProp.getAuthProvider().empty() || cloudProp.getCiServer().empty()) { throw ESBadRequestException ("Invalid Cloud Provisiong Info."); } try { initCloudResource(); } catch (const std::exception& e) { OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG, "Exception caught in provisionCloudProperties = %s", e.what()); std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared< CloudPropProvisioningStatus >(ESResult::ES_ERROR, ESCloudProvState::ES_CLOUD_ENROLLEE_NOT_FOUND); m_cloudPropProvStatusCb(provStatus); return; } #ifdef __WITH_DTLS__ try { ESResult res = ES_OK; m_enrolleeSecurity = std::make_shared <EnrolleeSecurity> (m_ocResource, ""); if(cloudProp.getCloudID().empty()) { throw ESBadRequestException("Invalid Cloud Server UUID."); } res = m_enrolleeSecurity->performACLProvisioningForCloudServer(cloudProp.getCloudID()); if(res == ESResult::ES_ERROR) { throw ESBadRequestException("Error in provisioning operation!"); } } catch (const std::exception& e) { OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG, "Exception caught in provisionCloudProperties = %s", e.what()); m_cloudResource = nullptr; std::shared_ptr< CloudPropProvisioningStatus > provStatus = std::make_shared< CloudPropProvisioningStatus >(ESResult::ES_ERROR, ESCloudProvState::ES_CLOUD_PROVISIONING_ERROR); m_cloudPropProvStatusCb(provStatus); return; } #endif if (m_cloudResource == nullptr) { throw ESBadRequestException ("Cloud Resource not created"); } CloudPropProvStatusCb cloudPropProvStatusCb = std::bind( &RemoteEnrollee::cloudPropProvisioningStatusHandler, this, std::placeholders::_1); m_cloudResource->registerCloudPropProvisioningStatusCallback(cloudPropProvStatusCb); m_cloudResource->provisionEnrollee(cloudProp); }
void RemoteEnrollee::startProvisioning() { OIC_LOG(DEBUG,ES_REMOTE_ENROLLEE_TAG,"Entering startProvisioning"); if (m_remoteResource == nullptr) { throw ESBadRequestException ("Device not created"); } ESResult result = ES_ERROR; result = m_remoteResource->constructResourceObject(); if (result == ES_ERROR) { OIC_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_isSecured && 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 <EnrolleeSecurity> (m_remoteResource, ""); m_enrolleeSecurity->registerCallbackHandler(securityProvStatusCb, m_securityPinCb, m_secProvisioningDbPathCb); try { EasySetupState easySetupState = m_enrolleeSecurity->performOwnershipTransfer(); if (easySetupState == DEVICE_NOT_OWNED) { OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "performOwnershipTransfer returned : %d", easySetupState); return; } else if (easySetupState == DEVICE_OWNED) { OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_TAG, "performOwnershipTransfer returned : %d", easySetupState); OIC_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(); } } catch (OCException & e) { OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_TAG, "Exception for performOwnershipTransfer : %s", e.reason().c_str()); return ; } } #else OIC_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 }
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 > interface = { DEFAULT_INTERFACE}; std::vector< std::string > resTypes = { ES_PROV_RES_TYPE}; OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Before OCPlatform::constructResourceObject"); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_host = %s", m_wifiOnboardingconn.ipAddress); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "ES_PROV_RES_URI = %s", ES_PROV_RES_URI); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_connectivityType = %d", m_ProvConfig.connType); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "resTypes = %s", resTypes.at(0).c_str()); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "interface = %s", interface.at(0).c_str()); std::string host; if(m_wifiOnboardingconn.isSecured) { host.append("coaps://"); } else { host.append("coap://"); } if(m_ProvConfig.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_wifiOnboardingconn.ipAddress); //TODO : If the target Enrollee is not a Arduino Wi-Fi device, // then the port number will be found during resource discovery instead of // using 55555 host.append(":55555"); } OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "HOST = %s", host.c_str()); m_ocResource = OC::OCPlatform::constructResourceObject(host, ES_PROV_RES_URI, m_ProvConfig.connType, true, resTypes, interface); OIC_LOG_V(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "created OCResource : %s", m_ocResource->uri().c_str()); return ES_OK; } catch (OCException & e) { OIC_LOG_V(ERROR, ES_REMOTE_ENROLLEE_RES_TAG, "Exception for constructResourceObject : %s", e.reason().c_str()); return ES_ERROR; } #else std::string host(""); std::string query(""); if (m_wifiOnboardingconn.isSecured) { host.append("coaps://"); } else { host.append("coap://"); } if (m_ProvConfig.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_wifiOnboardingconn.ipAddress); } query.append(ES_BASE_RES_URI); query.append("?rt="); query.append(ES_PROV_RES_TYPE); OIC_LOG(DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "Before OCPlatform::constructResourceObject"); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "host = %s", host.c_str()); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "query = %s", query.c_str()); OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "m_connectivityType = %d", m_ProvConfig.connType); m_discoveryResponse = false; std::function< void (std::shared_ptr<OC::OCResource>) > onDeviceDiscoveredCb = std::bind(&RemoteEnrolleeResource::onDeviceDiscovered, this, std::placeholders::_1); OCStackResult result = OC::OCPlatform::findResource("", query, CT_DEFAULT, onDeviceDiscoveredCb); if (result != OCStackResult::OC_STACK_OK) { OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_RES_TAG, "Failed to create device using constructResourceObject"); return ES_ERROR; } ESResult foundResponse = ESDiscoveryTimeout (DISCOVERY_TIMEOUT); if (foundResponse ==ES_ERROR || !m_discoveryResponse) { OIC_LOG(ERROR,ES_REMOTE_ENROLLEE_RES_TAG, "Failed to create device using constructResourceObject"); return ES_ERROR; } return ES_OK; #endif }