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<EnrolleeResource> 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<EnrolleeResource> 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);
}
}
- 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<ESEnrolleeResourceCb>(
+ 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");
OC::OCRepresentation rep;
std::function< OCStackResult(void) > getConfigurationStatus = [&]
- { return m_ocResource->get(m_ocResource->getResourceTypes().at(0),
- BATCH_INTERFACE, query, std::function<void(const HeaderOptions& headerOptions,
- const OCRepresentation& rep, const int eCode) >(
+ {
+ ESEnrolleeResourceCb cb = std::bind(&EnrolleeResource::onEnrolleeResourceSafetyCB,
+ std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
+ static_cast<ESEnrolleeResourceCb>(
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<ESEnrolleeResourceCb>(
+ 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<OCRepresentation> 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<ES_CONNECT_TYPE> &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<int> types = prop->getValue<std::vector<int>>(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<WIFI_MODE>(*type));
- }
+ throw ESBadRequestException("Resource is not initialized");
+ }
- wifiConf.freq = static_cast<WIFI_FREQ>(prop->getValue<int>(OC_RSRVD_ES_SUPPORTEDWIFIFREQ));
+ OC::QueryParamsMap query;
+ OC::OCRepresentation requestRepresentation;
+ std::vector<int> connectTypes_int;
+ connectTypes_int.clear();
+ for(auto it : connectTypes)
+ {
+ connectTypes_int.push_back(static_cast<int>(it));
+ }
- OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "OC_RSRVD_ES_SUPPORTEDWIFIFREQ = %d",
- wifiConf.freq);
- }
- }
+ requestRepresentation.setValue<std::vector<int>>(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<std::string>(OC_RSRVD_ES_DEVNAME);
- devConf.language = prop->getValue<std::string>(OC_RSRVD_ES_LANGUAGE);
- devConf.country = prop->getValue<std::string>(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<ESEnrolleeResourceCb>(
+ 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");
}
-
}
}