typedef struct PROVRESOURCE
{
OCResourceHandle handle;
- int ps; // provisiong status, 1 : need to provisioning, 2 : Connected to Enroller
- int tnt; // target network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee, ...
- char tnn[MAXSSIDLEN]; // target network name, i.e. SSID for WLAN, MAC address for BT
- char cd[MAXNETCREDLEN]; // credential information
+ int64_t ps; // provisiong status, 1 : need to provisioning, 2 : Connected to Enroller.
+ int64_t tr; // Trigger network connection, 0 : Init value, 1 : Connected to the target network.
+ int64_t tnt; // target network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee.
+ char tnn[MAXSSIDLEN]; // target network name, i.e. SSID for WLAN, MAC address for BT.
+ char cd[MAXNETCREDLEN]; // credential information.
} ProvResource;
/* Structure to represent a Light resource */
typedef struct NETRESOURCE
{
OCResourceHandle handle;
- int cnt; // current network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee, ...
- int ant[MAXNUMTYPE]; // available network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee, ...
- char ipaddr[MAXADDRLEN]; // ip address
- char cnn[MAXSSIDLEN]; // current network name
+ int64_t cnt; // current network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee.
+ int64_t ant[MAXNUMTYPE]; // available network type, 1: WLAN, 2: BT, 3: BLE, 4: Zigbee.
+ char ipaddr[MAXADDRLEN]; // ip address.
+ char cnn[MAXSSIDLEN]; // current network name.
} NetResource;
OCStackResult CreateProvisioningResource(bool isSecured);
/**
* @var gProvResource
- * @brief Structure for holding the Provisioning status and target information required to connect to the target network
+ * @brief Structure for holding the Provisioning status and target information required to
+ * connect to the target network
*/
static ProvResource gProvResource;
//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
-OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
- OCEntityHandlerRequest *ehRequest, void *callback);
+OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
+ void *callback);
const char *getResult(OCStackResult result);
-OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
- OCRepPayload** payload);
-OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
- OCRepPayload** payload);
-OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
- OCRepPayload** payload);
+OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
+OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
+OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
-static int g_flag = 0;
-
ResourceEventCallback gNetworkInfoProvEventCb = NULL;
void RegisterResourceEventCallBack(ResourceEventCallback cb)
OCStackResult CreateProvisioningResource(bool isSecured)
{
gProvResource.ps = ES_PS_NEED_PROVISIONING;
+ gProvResource.tr = ES_PS_TRIGGER_INIT_VALUE;
gProvResource.tnt = CT_ADAPTER_IP;
- OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn),"Unknown");
+ OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), "Unknown");
OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), "Unknown");
OCStackResult res = OC_STACK_ERROR;
if (isSecured)
{
res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
- OC_RSRVD_INTERFACE_DEFAULT,
- OC_RSRVD_ES_URI_PROV,
- OCEntityHandlerCb,
- NULL,
- OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
+ OC_RSRVD_INTERFACE_DEFAULT,
+ OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
+ NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
}
else
{
res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_PROV_RES_TYPE,
- OC_RSRVD_INTERFACE_DEFAULT,
- OC_RSRVD_ES_URI_PROV,
- OCEntityHandlerCb,
- NULL,
- OC_DISCOVERABLE | OC_OBSERVABLE);
+ OC_RSRVD_INTERFACE_DEFAULT,
+ OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
+ NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
}
OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
return res;
}
-OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
- OCRepPayload **payload)
+OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
{
OCEntityHandlerResult ehResult = OC_EH_ERROR;
if (!ehRequest)
return ehResult;
}
-OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
- OCRepPayload** payload)
+OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
{
- OIC_LOG(INFO, ES_RH_TAG, "ProcessPutRequest enter");
+ OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
OCEntityHandlerResult ehResult = OC_EH_ERROR;
if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
{
return ehResult;
}
- //TODO : ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
+ int64_t tr;
+ if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_TR, &tr))
+ {
+ // Triggering
+ gProvResource.tr = tr;
+ }
+
+ //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 (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED)
+ if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED && tr == ES_PS_TRIGGER_CONNECTION)
+ {
+ OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
+ "Tiggering the network connection");
+
+ if (gNetworkInfoProvEventCb)
+ {
+ gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
+ ehResult = OC_EH_OK;
+ }
+ else
+ {
+ OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
+ "Network handler not registered. Failed to connect to the network");
+ ehResult = OC_EH_ERROR;
+ }
+
+ return ehResult;
+ }
+ else if (gProvResource.ps == ES_PS_PROVISIONING_COMPLETED)
{
OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed. "
"This a request to override the existing the network provisioning information");
}
+ else
+ {
+ OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
+ }
- // PUT request is appropriate for provisioning information to the enrollee.
- // When an enrollee receives the put request, the entire resource information should
- // be overwritten.
OICStrcpy(gProvResource.tnn, sizeof(gProvResource.tnn), "");
OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), "");
}
OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.tnn %s", gProvResource.tnn);
+
char* cd;
if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd))
{
OICStrcpy(gProvResource.cd, sizeof(gProvResource.cd), cd);
OIC_LOG(INFO, ES_RH_TAG, "got password");
- }
- OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd);
- gProvResource.ps = 2;
- OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %d", gProvResource.ps);
- g_flag = 1;
+ }OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.cd %s", gProvResource.cd);
+
+ gProvResource.ps = ES_PS_PROVISIONING_COMPLETED;
+
+ OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.ps %lld", gProvResource.ps);
OCRepPayload *getResp = constructResponse(ehRequest);
if (!getResp)
return ehResult;
}
-OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
- OCRepPayload** /*payload*/)
+OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * /*ehRequest*/,
+ OCRepPayload** /*payload*/)
{
OCEntityHandlerResult ehResult = OC_EH_ERROR;
- if (!ehRequest)
- {
- OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
- return ehResult;
- }
- if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
- {
- OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
- return ehResult;
- }
-
- OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
- if (!input)
- {
- OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
- return ehResult;
- }
- char* tr;
- if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TR, &tr))
- {
- // Triggering
- ehResult = OC_EH_OK;
- }
- g_flag = 1;
return ehResult;
}
OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PS, gProvResource.ps);
OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TNT, gProvResource.tnt);
- OCRepPayloadSetPropString(payload, OC_RSRVD_ES_TNN, gProvResource.tnn);
- OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CD, gProvResource.cd);
}
else if (ehRequest->requestHandle == gNetResource.handle)
{
{
(void) callback;
OCEntityHandlerResult ehRet = OC_EH_OK;
- OCEntityHandlerResponse response = { 0, 0, OC_EH_ERROR, 0, 0, { },{ 0 }, false };
+ OCEntityHandlerResponse response =
+ { 0, 0, OC_EH_ERROR, 0, 0,
+ { },
+ { 0 }, false };
OCRepPayload* payload = NULL;
+
if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
{
if (OC_REST_GET == entityHandlerRequest->method)
{
OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
- if (gProvResource.handle != NULL && entityHandlerRequest->resource == gProvResource.handle)
+ //PUT request will be handled in the internal implementation
+ if (gProvResource.handle != NULL
+ && entityHandlerRequest->resource == gProvResource.handle)
{
ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
}
}
else if (OC_REST_POST == entityHandlerRequest->method)
{
- // TODO: As of now, POST request will be not received.
OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
- //ehRet = ProcessPostRequest (entityHandlerRequest, payload, sizeof(payload) - 1);
+ if (gProvResource.handle != NULL
+ && entityHandlerRequest->resource == gProvResource.handle)
+ {
+ ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
+ }
+ else
+ {
+ OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
+ ehRet = OC_EH_ERROR;
+ }
}
if (ehRet == OC_EH_OK)
response.payload = (OCPayload*) (payload);
response.numSendVendorSpecificHeaderOptions = 0;
memset(response.sendVendorSpecificHeaderOptions, 0,
- sizeof response.sendVendorSpecificHeaderOptions);
- memset(response.resourceUri, 0, sizeof response.resourceUri);
+ sizeof(response.sendVendorSpecificHeaderOptions));
+ memset(response.resourceUri, 0, sizeof(response.resourceUri));
// Indicate that response is NOT in a persistent buffer
response.persistentBufferFlag = 0;
}
}
- if (g_flag == 1)
- {
- gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
- g_flag = 0;
- }
-
return ehRet;
}
*/
#define ES_PS_NEED_PROVISIONING 1
#define ES_PS_PROVISIONING_COMPLETED 2
+#define ES_PS_TRIGGER_INIT_VALUE 0
+#define ES_PS_TRIGGER_CONNECTION 1
+
/**
* Device Roles defined for each device type used in easy setup
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
-
-#ifndef __PROVISIONING_HANDLER_H_
-#define __PROVISIONING_HANDLER_H_
+#ifndef __ES_PROVISIONING_HANDLER_H_
+#define __ES_PROVISIONING_HANDLER_H_
#include "octypes.h"
//-----------------------------------------------------------------------------
#define EASY_SETUP_DEFAULT_CONTEXT_VALUE 0x99
-
-//-----------------------------------------------------------------------------
-// Typedefs
-//-----------------------------------------------------------------------------
-
/**
* List of methods that can be inititated from the client
*/
OCStackResult TerminateProvisioningHandler();
-void *listeningFunc(void *);
+OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
+ const OCDevAddr *destination);
+
+OCStackResult StartProvisioningProcess(const ProvConfig *netInfo,
+ WiFiOnboadingConnection *onboardConn, OCProvisioningStatusCB provisioningStatusCallback,
+ char *findResQuery);
+
+void StopProvisioningProcess();
+
+/**
+ * Internal worker functions.
+ */
+OCStackResult TriggerNetworkConnection(OCQualityOfService qos, const char *query,
+ const char *resUri, OCDevAddr *destination, int /*pauseBeforeStart*/);
+
+OCStackApplicationResult TriggerNetworkConnectionResponse(void *ctx, OCDoHandle handle,
+ OCClientResponse *clientResponse);
OCStackApplicationResult ProvisionEnrolleeResponse(void *ctx, OCDoHandle handle,
- OCClientResponse *clientResponse);
+ OCClientResponse *clientResponse);
OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri,
- OCDevAddr *destination, int pauseBeforeStart);
+ OCDevAddr *destination, int pauseBeforeStart);
OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
- OCClientResponse *clientResponse);
+ OCClientResponse *clientResponse);
OCStackResult InvokeOCDoResource(const char *query, OCMethod method, const OCDevAddr *dest,
- OCQualityOfService qos, OCClientResponseHandler cb,
- OCRepPayload *payload,
- OCHeaderOption *options, uint8_t numOptions);
-
-OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
- const OCDevAddr *destination);
-
-OCStackResult StartProvisioningProcess(const ProvConfig *netInfo,WiFiOnboadingConnection *onboardConn,
- OCProvisioningStatusCB provisioningStatusCallback,
- char *findResQuery);
-
-void StopProvisioningProcess();
+ OCQualityOfService qos, OCClientResponseHandler cb, OCRepPayload *payload,
+ OCHeaderOption *options, uint8_t numOptions);
OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
- OCClientResponse *clientResponse);
+ OCClientResponse *clientResponse);
void *FindProvisioningResource(void *data);
-//Invoke Provisioning Status Callback
ProvisioningInfo *PrepareProvisioingStatusCB(OCClientResponse *clientResponse,
- ProvStatus provStatus);
-
+ ProvStatus provStatus);
+/**
+ * Internal Util functions.
+ */
void LogProvisioningResponse(OCRepPayloadValue * val);
bool ConfigEnrolleeObject(const ProvConfig *netInfo, WiFiOnboadingConnection *onboardConn);
void ErrorCallback(ProvStatus status);
-bool ValidateEnrolleResponse(OCClientResponse * clientResponse);
+bool ValidateEnrolleeResponse(OCClientResponse * clientResponse);
-bool ValidateFinddResourceResponse(OCClientResponse * clientResponse);
+bool ValidateFindResourceResponse(OCClientResponse * clientResponse);
+
+bool ValidateEnrolleeBasicResponse(OCClientResponse * clientResponse);
ProvisioningInfo *GetCallbackObjectOnSuccess(OCClientResponse *clientResponse,
- ProvStatus provStatus);
+ ProvStatus provStatus);
ProvisioningInfo *GetCallbackObjectOnError(ProvStatus status);
bool InProgress();
-bool ValidateEasySetupParams(const ProvConfig *netInfo,WiFiOnboadingConnection *onboardConn,
- OCProvisioningStatusCB provisioningStatusCallback);
+bool ValidateEasySetupParams(const ProvConfig *netInfo, WiFiOnboadingConnection *onboardConn,
+ OCProvisioningStatusCB provisioningStatusCallback);
bool IsSetupStopped();
-
-#endif
+#endif //__ES_PROVISIONING_HANDLER_H_
#include "provisioning.h"
-
//-----------------------------------------------------------------------------
// Defines
//-----------------------------------------------------------------------------
#ifdef __cplusplus
-extern "C" {
+extern "C"
+{
#endif
+ OCStackResult InitProvProcess();
-OCStackResult InitProvProcess();
-
-OCStackResult ResetProvProcess();
-
-OCStackResult RegisterCallback(
- OCProvisioningStatusCB provisioningStatusCallback);
+ OCStackResult ResetProvProcess();
-void UnRegisterCallback();
+ OCStackResult RegisterCallback(OCProvisioningStatusCB provisioningStatusCallback);
-OCStackResult StartProvisioning(const ProvConfig *netInfo, WiFiOnboadingConnection *onboardConn);
+ void UnRegisterCallback();
-OCStackResult StopProvisioning(OCConnectivityType connectivityType);
+ OCStackResult StartProvisioning(const ProvConfig *netInfo,
+ WiFiOnboadingConnection *onboardConn);
+ OCStackResult StopProvisioning(OCConnectivityType connectivityType);
#ifdef __cplusplus
}
#endif
#include "ocpayload.h"
#include "escommon.h"
-
// External includes
#include "logger.h"
#include "oic_malloc.h"
static ProvConfig *gProvConfig;
static WiFiOnboadingConnection *gOnboardConn;
-static char gSzFindResourceQueryUri[64] = {0};
+static char gSzFindResourceQueryUri[64] =
+{ 0 };
/**
* @var cbData
*/
static OCProvisioningStatusCB cbData = NULL;
-
-void ErrorCallback(ProvStatus status) {
+/**
+ * Utility function for error callback.
+ */
+void ErrorCallback(ProvStatus status)
+{
ProvisioningInfo *provInfo = GetCallbackObjectOnError(status);
- cbData(provInfo);
- ResetProgress();
+ if(cbData != NULL)
+ {
+ cbData(provInfo);
+ }
}
-
-OCStackResult InitProvisioningHandler() {
+/**
+ * Functions implementing the exposed APIs.
+ */
+OCStackResult InitProvisioningHandler()
+{
OCStackResult ret = OC_STACK_ERROR;
/* Initialize OCStack*/
- if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK) {
+ if (OCInit(NULL, 0, OC_CLIENT) != OC_STACK_OK)
+ {
OIC_LOG(ERROR, ES_PROV_TAG, "OCStack init error");
return ret;
}
-
- pthread_t thread_handle;
-
- if (pthread_create(&thread_handle, NULL, listeningFunc, NULL)) {
- OIC_LOG(DEBUG, ES_PROV_TAG, "Thread creation failed");
- return OC_STACK_ERROR;
- }
-
return OC_STACK_OK;
}
-OCStackResult TerminateProvisioningHandler() {
- OCStackResult ret = OC_STACK_ERROR;
- if (OCStop() != OC_STACK_OK) {
- OIC_LOG(ERROR, ES_PROV_TAG, "OCStack stop error");
- }
-
- gProvisioningCondFlag = true;
-
- ret = OC_STACK_OK;
- return ret;
-}
-
-void *listeningFunc(void* /*data*/) {
- while (!gProvisioningCondFlag) {
- OCStackResult result;
-
- result = OCProcess();
-
- if (result != OC_STACK_OK) {
- OIC_LOG(ERROR, ES_PROV_TAG, "OCStack stop error");
- }
-
- // To minimize CPU utilization we may wish to do this with sleep
- sleep(1);
- }
- return NULL;
-}
-
-
-OCStackApplicationResult ProvisionEnrolleeResponse(void* /*ctx*/, OCDoHandle /*handle*/,
- OCClientResponse *clientResponse) {
- OIC_LOG_V(DEBUG, ES_PROV_TAG, "INSIDE ProvisionEnrolleeResponse");
-
- // If user stopped the process then return from this function;
- if (IsSetupStopped()) {
- ErrorCallback(DEVICE_NOT_PROVISIONED);
- ClearMemory();
- return OC_STACK_DELETE_TRANSACTION;
- }
+OCStackResult StartProvisioningProcess(const ProvConfig *netInfo,
+ WiFiOnboadingConnection *onboardConn, OCProvisioningStatusCB provisioningStatusCallback,
+ char *findResQuery)
+{
- if (!ValidateEnrolleResponse(clientResponse)) {
- ErrorCallback(DEVICE_NOT_PROVISIONED);
- return OC_STACK_DELETE_TRANSACTION;
- }
-
- char *tnn = NULL;
- char *cd = NULL;
-
- OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
-
- while (input) {
-
- int64_t ps;
- if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps)) {
-
- if (ps == 2) {
- input = input->next;
- continue;
- }
- else {
- OIC_LOG_V(DEBUG, ES_PROV_TAG, "PS is NOT proper");
- goto Error;
-
- }
- }
-
- if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_TNN, &tnn)) {
- if (!strcmp(tnn, gProvConfig->provData.WIFI.ssid)) {
- OIC_LOG_V(DEBUG, ES_PROV_TAG, "SSID is proper");
- input = input->next;
- OICFree(tnn);
- continue;
- }
- else {
- OIC_LOG_V(DEBUG, ES_PROV_TAG, "SSID is NOT proper");
- OICFree(tnn);
- goto Error;
- }
- }
-
- if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CD, &cd)) {
- if (!strcmp(cd, gProvConfig->provData.WIFI.pwd)) {
- OIC_LOG_V(DEBUG, ES_PROV_TAG, "Password is proper");
- input = input->next;
- OICFree(cd);
- continue;
- }
- else {
- OIC_LOG_V(DEBUG, ES_PROV_TAG, "Password is NOT proper");
- OICFree(cd);
- goto Error;
- }
- }
-
- LogProvisioningResponse(input->values);
-
- input = input->next;
- }
-
- SuccessCallback(clientResponse);
- return OC_STACK_KEEP_TRANSACTION;
-
- Error:
- {
- ErrorCallback(DEVICE_NOT_PROVISIONED);
- return OC_STACK_DELETE_TRANSACTION;
- }
-
-}
-
-OCStackResult StartProvisioningProcess(const ProvConfig *netInfo,WiFiOnboadingConnection *onboardConn,
- OCProvisioningStatusCB provisioningStatusCallback,
- char *findResQuery) {
-
- if(findResQuery != NULL)
+ if (findResQuery != NULL)
{
OICStrcpy(gSzFindResourceQueryUri, sizeof(gSzFindResourceQueryUri) - 1, findResQuery);
}
pthread_t thread_handle;
- if (!ValidateEasySetupParams(netInfo, onboardConn, provisioningStatusCallback)) {
+ if (!ValidateEasySetupParams(netInfo, onboardConn, provisioningStatusCallback))
+ {
goto Error;
}
- if (!SetProgress(provisioningStatusCallback)) {
+ if (!SetProgress(provisioningStatusCallback))
+ {
// Device provisioning session is running already.
OIC_LOG(INFO, ES_PROV_TAG, PCF("Device provisioning session is running already"));
goto Error;
}
- if (!ConfigEnrolleeObject(netInfo, onboardConn)) {
+ if (!ConfigEnrolleeObject(netInfo, onboardConn))
+ {
goto Error;
}
- if (pthread_create(&thread_handle, NULL, FindProvisioningResource, NULL)) {
+ if (pthread_create(&thread_handle, NULL, FindProvisioningResource, NULL))
+ {
goto Error;
}
pthread_join(thread_handle, NULL);
-
return OC_STACK_OK;
Error:
}
-void StopProvisioningProcess() {
- //Only basis test is done for below API
+void StopProvisioningProcess()
+{
+ ResetProgress();
+}
+
+OCStackResult TerminateProvisioningHandler()
+{
+ OCStackResult ret = OC_STACK_ERROR;
+ if (OCStop() != OC_STACK_OK)
+ {
+ OIC_LOG(ERROR, ES_PROV_TAG, "OCStack stop error");
+ }
+
+ gProvisioningCondFlag = true;
ResetProgress();
+
+ ret = OC_STACK_OK;
+ return ret;
}
-bool ClearMemory() {
+OCStackApplicationResult TriggerNetworkConnectionResponse(void* /*ctx*/, OCDoHandle /*handle*/,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG_V(DEBUG, ES_PROV_TAG, "INSIDE TriggerNetworkConnectionResponse");
+
+ // If user stopped the process then return from this function;
+ if (IsSetupStopped())
+ {
+ ErrorCallback(DEVICE_NOT_PROVISIONED);
+ ClearMemory();
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ if (!ValidateEnrolleeBasicResponse(clientResponse))
+ {
+ ErrorCallback(DEVICE_NOT_PROVISIONED);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ SuccessCallback(clientResponse);
+ return OC_STACK_KEEP_TRANSACTION;
+}
+
+OCStackResult TriggerNetworkConnection(OCQualityOfService qos, const char *query,
+ const char *resUri, OCDevAddr *destination, int /*pauseBeforeStart*/)
+{
+ OIC_LOG_V(INFO, ES_PROV_TAG, "Inside TriggerNetworkConnection");
+
+ OCRepPayload *payload = OCRepPayloadCreate();
+
+ OCRepPayloadSetUri(payload, resUri);
+ OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_TR, ES_PS_TRIGGER_CONNECTION);
+
+ OIC_LOG_V(DEBUG, ES_PROV_TAG, "Triggering network connection from Mediator");
+
+ OCStackResult ret = InvokeOCDoResource(query, OC_REST_POST, destination, qos,
+ TriggerNetworkConnectionResponse, payload, NULL, 0);
+
+ return ret;
+}
+
+
+
+bool ClearMemory()
+{
OIC_LOG(DEBUG, ES_PROV_TAG, "thread_pool_add_task of FindProvisioningResource failed");
OICFree(gProvConfig);
}
-bool ConfigEnrolleeObject(const ProvConfig *netInfo, WiFiOnboadingConnection *connection) {
+bool ConfigEnrolleeObject(const ProvConfig *netInfo, WiFiOnboadingConnection *connection)
+{
//Copy Network Provisioning Information
gProvConfig = (ProvConfig *) OICCalloc(1, sizeof(ProvConfig));
gOnboardConn = (WiFiOnboadingConnection *) OICCalloc(1, sizeof(WiFiOnboadingConnection));
- if (gProvConfig == NULL) {
+ if (gProvConfig == NULL)
+ {
OIC_LOG(ERROR, ES_PROV_TAG, "Invalid input..");
return false;
}
- if (gOnboardConn == NULL) {
+ if (gOnboardConn == NULL)
+ {
OIC_LOG(ERROR, ES_PROV_TAG, "Invalid input..");
return false;
}
memcpy(gOnboardConn, connection, sizeof(WiFiOnboadingConnection));
OIC_LOG_V(DEBUG, ES_PROV_TAG, "Network Provisioning Info. SSID = %s",
- gProvConfig->provData.WIFI.ssid);
+ gProvConfig->provData.WIFI.ssid);
OIC_LOG_V(DEBUG, ES_PROV_TAG, "Network Provisioning Info. PWD = %s",
- gProvConfig->provData.WIFI.pwd);
+ gProvConfig->provData.WIFI.pwd);
return true;
}
-void LogProvisioningResponse(OCRepPayloadValue * val) {
+void LogProvisioningResponse(OCRepPayloadValue * val)
+{
- switch (val->type) {
+ switch (val->type)
+ {
case OCREP_PROP_NULL:
OIC_LOG_V(DEBUG, ES_PROV_TAG, "\t\t%s: NULL", val->name);
break;
OIC_LOG_V(DEBUG, ES_PROV_TAG, "\t\t%s(OCRep):%s", val->name, val->obj->uri);
break;
case OCREP_PROP_ARRAY:
- switch (val->arr.type) {
+ switch (val->arr.type)
+ {
case OCREP_PROP_INT:
OIC_LOG_V(DEBUG, ES_PROV_TAG, "\t\t%s(int array):%zu x %zu x %zu",
- val->name,
- val->arr.dimensions[0], val->arr.dimensions[1],
- val->arr.dimensions[2]);
+ val->name,
+ val->arr.dimensions[0], val->arr.dimensions[1],
+ val->arr.dimensions[2]);
break;
case OCREP_PROP_DOUBLE:
OIC_LOG_V(DEBUG, ES_PROV_TAG, "\t\t%s(double array):%zu x %zu x %zu",
- val->name,
- val->arr.dimensions[0], val->arr.dimensions[1],
- val->arr.dimensions[2]);
+ val->name,
+ val->arr.dimensions[0], val->arr.dimensions[1],
+ val->arr.dimensions[2]);
break;
case OCREP_PROP_BOOL:
OIC_LOG_V(DEBUG, ES_PROV_TAG, "\t\t%s(bool array):%zu x %zu x %zu",
- val->name,
- val->arr.dimensions[0], val->arr.dimensions[1],
- val->arr.dimensions[2]);
+ val->name,
+ val->arr.dimensions[0], val->arr.dimensions[1],
+ val->arr.dimensions[2]);
break;
case OCREP_PROP_STRING:
OIC_LOG_V(DEBUG, ES_PROV_TAG, "\t\t%s(string array):%zu x %zu x %zu",
- val->name,
- val->arr.dimensions[0], val->arr.dimensions[1],
- val->arr.dimensions[2]);
+ val->name,
+ val->arr.dimensions[0], val->arr.dimensions[1],
+ val->arr.dimensions[2]);
break;
case OCREP_PROP_OBJECT:
OIC_LOG_V(DEBUG, ES_PROV_TAG, "\t\t%s(OCRep array):%zu x %zu x %zu",
- val->name,
- val->arr.dimensions[0], val->arr.dimensions[1],
- val->arr.dimensions[2]);
+ val->name,
+ val->arr.dimensions[0], val->arr.dimensions[1],
+ val->arr.dimensions[2]);
break;
default:
break;
}
}
-OCStackResult FindNetworkResource() {
+OCStackResult FindNetworkResource()
+{
OCStackResult ret = OC_STACK_ERROR;
- if (OCStop() != OC_STACK_OK) {
+ if (OCStop() != OC_STACK_OK)
+ {
OIC_LOG(ERROR, ES_PROV_TAG, "OCStack stop error");
}
}
ProvisioningInfo *PrepareProvisioingStatusCB(OCClientResponse *clientResponse,
- ProvStatus provStatus) {
+ ProvStatus provStatus)
+{
ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
- if (provInfo == NULL) {
+ if (provInfo == NULL)
+ {
OIC_LOG_V(ERROR, ES_PROV_TAG, "Failed to allocate memory");
return NULL;
}
OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
- if (devAddr == NULL) {
+ if (devAddr == NULL)
+ {
OIC_LOG_V(ERROR, ES_PROV_TAG, "Failed to allocate memory");
OICFree(provInfo);
return NULL;
return provInfo;
}
-
-bool InProgress() {
+bool InProgress()
+{
// It means already Easy Setup provisioning session is going on.
- if (NULL != cbData) {
+ if (NULL != cbData)
+ {
OIC_LOG(ERROR, ES_PROV_TAG, "Easy setup session is already in progress");
return true;
}
return false;
}
-bool SetProgress(OCProvisioningStatusCB provisioningStatusCallback) {
+bool SetProgress(OCProvisioningStatusCB provisioningStatusCallback)
+{
if (InProgress())
return false;
cbData = provisioningStatusCallback;
-
return true;
}
-bool ResetProgress() {
+bool ResetProgress()
+{
cbData = NULL;
return true;
}
-ProvisioningInfo *CreateCallBackObject() {
+ProvisioningInfo *CreateCallBackObject()
+{
ProvisioningInfo *provInfo = (ProvisioningInfo *) OICCalloc(1, sizeof(ProvisioningInfo));
- if (provInfo == NULL) {
+ if (provInfo == NULL)
+ {
OIC_LOG_V(ERROR, ES_PROV_TAG, "Failed to allocate memory");
return NULL;
}
OCDevAddr *devAddr = (OCDevAddr *) OICCalloc(1, sizeof(OCDevAddr));
- if (devAddr == NULL) {
+ if (devAddr == NULL)
+ {
OIC_LOG_V(ERROR, ES_PROV_TAG, "Failed to allocate memory");
OICFree(provInfo);
return NULL;
}
-ProvisioningInfo *GetCallbackObjectOnError(ProvStatus status) {
+ProvisioningInfo *GetCallbackObjectOnError(ProvStatus status)
+{
ProvisioningInfo *provInfo = CreateCallBackObject();
OICStrcpy(provInfo->provDeviceInfo.addr->addr, sizeof(provInfo->provDeviceInfo.addr->addr),
- gOnboardConn->ipAddress);
+ gOnboardConn->ipAddress);
provInfo->provDeviceInfo.addr->port = IP_PORT;
provInfo->provStatus = status;
}
ProvisioningInfo *GetCallbackObjectOnSuccess(OCClientResponse *clientResponse,
- ProvStatus provStatus) {
+ ProvStatus provStatus)
+{
ProvisioningInfo *provInfo = CreateCallBackObject();
OICStrcpy(provInfo->provDeviceInfo.addr->addr, sizeof(provInfo->provDeviceInfo.addr->addr),
- clientResponse->addr->addr);
+ clientResponse->addr->addr);
provInfo->provDeviceInfo.addr->port = clientResponse->addr->port;
provInfo->provStatus = provStatus;
return provInfo;
}
-bool ValidateFinddResourceResponse(OCClientResponse * clientResponse) {
-
- if (!(clientResponse) || !(clientResponse->payload)) {
-
- OIC_LOG_V(INFO, ES_PROV_TAG, "ProvisionEnrolleeResponse received Null clientResponse");
-
+bool ValidateFindResourceResponse(OCClientResponse * clientResponse)
+{
+ if (!(clientResponse) || !(clientResponse->payload))
+ {
+ OIC_LOG_V(INFO, ES_PROV_TAG, "Received Null clientResponse");
return false;
+ }
+ if (clientResponse->payload->type != PAYLOAD_TYPE_DISCOVERY)
+ {
+ OIC_LOG_V(DEBUG, ES_PROV_TAG, "Payload is not discovery type");
+ return false;
}
+
return true;
}
-bool ValidateEnrolleResponse(OCClientResponse * clientResponse) {
-
- if (!(clientResponse) || !(clientResponse->payload)) {
-
- OIC_LOG_V(INFO, ES_PROV_TAG, "ProvisionEnrolleeResponse received Null clientResponse");
-
+bool ValidateEnrolleeResponse(OCClientResponse * clientResponse)
+{
+ if (!(clientResponse) || !(clientResponse->payload))
+ {
+ OIC_LOG_V(INFO, ES_PROV_TAG, "Received Null clientResponse");
return false;
+ }
+ if (clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION)
+ {
+ OIC_LOG_V(DEBUG, ES_PROV_TAG, "Incoming payload is not a representation");
+ return false;
}
- if (clientResponse->payload->type != PAYLOAD_TYPE_REPRESENTATION) {
+ // If flow reachese here means no error condition hit.
+ return true;
+}
- OIC_LOG_V(DEBUG, ES_PROV_TAG, "Incoming payload not a representation");
+bool ValidateEnrolleeBasicResponse(OCClientResponse * clientResponse)
+{
+ if (!clientResponse)
+ {
+ OIC_LOG_V(INFO, ES_PROV_TAG, "Received Null clientResponse");
return false;
+ }
+ if(clientResponse->result != OC_STACK_OK)
+ {
+ OIC_LOG_V(INFO, ES_PROV_TAG, "Received error response");
+ return false;
}
- // If flow reachese here means no error condition hit.
+ // If flow reaches, then there no error condition hit.
return true;
-
}
-void SuccessCallback(OCClientResponse * clientResponse) {
+
+void SuccessCallback(OCClientResponse * clientResponse)
+{
ProvisioningInfo *provInfo = GetCallbackObjectOnSuccess(clientResponse, DEVICE_PROVISIONED);
- cbData(provInfo);
- ResetProgress();
+ if(cbData != NULL)
+ {
+ cbData(provInfo);
+ }
}
-void* FindProvisioningResource(void* /*data*/) {
+void* FindProvisioningResource(void* /*data*/)
+{
// If user stopped the process before thread get scheduled then check and return from this function;
- if (IsSetupStopped()) {
+ if (IsSetupStopped())
+ {
ErrorCallback(DEVICE_NOT_PROVISIONED);
ClearMemory();
return NULL;
ocCBData.context = (void *) EASY_SETUP_DEFAULT_CONTEXT_VALUE;
ocCBData.cd = NULL;
-
ret = OCDoResource(NULL, OC_REST_DISCOVER, gSzFindResourceQueryUri, NULL, NULL,
- gProvConfig->connType, OC_LOW_QOS,
- &ocCBData, NULL, 0);
+ gProvConfig->connType, OC_LOW_QOS, &ocCBData, NULL, 0);
- if (ret != OC_STACK_OK) {
+ if (ret != OC_STACK_OK)
+ {
ErrorCallback(DEVICE_NOT_PROVISIONED);
ClearMemory();
}
}
OCStackResult InvokeOCDoResource(const char *query, OCMethod method, const OCDevAddr *dest,
- OCQualityOfService qos, OCClientResponseHandler cb,
- OCRepPayload *payload,
- OCHeaderOption *options, uint8_t numOptions) {
+ OCQualityOfService qos, OCClientResponseHandler cb, OCRepPayload *payload,
+ OCHeaderOption *options, uint8_t numOptions)
+{
OCStackResult ret;
OCCallbackData cbData;
cbData.cd = NULL;
ret = OCDoResource(NULL, method, query, dest, (OCPayload *) payload, gProvConfig->connType, qos,
- &cbData, options, numOptions);
+ &cbData, options, numOptions);
- if (ret != OC_STACK_OK) {
+ if (ret != OC_STACK_OK)
+ {
OIC_LOG_V(ERROR, ES_PROV_TAG, "OCDoResource returns error %d with method %d", ret, method);
}
}
OCStackResult ProvisionEnrollee(OCQualityOfService qos, const char *query, const char *resUri,
- OCDevAddr *destination, int pauseBeforeStart) {
-
+ OCDevAddr *destination, int pauseBeforeStart)
+{
// This sleep is required in case of BLE provisioning due to packet drop issue.
OIC_LOG_V(INFO, ES_PROV_TAG, "Sleeping for %d seconds", pauseBeforeStart);
OIC_LOG_V(DEBUG, ES_PROV_TAG, "OCPayload ready for ProvisionEnrollee");
- OCStackResult ret = InvokeOCDoResource(query, OC_REST_PUT, destination, qos,
- ProvisionEnrolleeResponse, payload, NULL, 0);
+ OCStackResult ret = InvokeOCDoResource(query, OC_REST_POST, destination, qos,
+ ProvisionEnrolleeResponse, payload, NULL, 0);
return ret;
}
-bool IsSetupStopped() {
+bool IsSetupStopped()
+{
return (cbData == NULL) ? true : false;
}
#include "provisioningapi.h"
#include "oic_string.h"
-
#define ES_PROV_ADAP_TAG "ES_PROVISIONING_ADAPTER"
//Use ipv4addr for both InitDiscovery and InitDeviceDiscovery
-char ipv4addr[IPV4_ADDR_SIZE] = {0};
+char ipv4addr[IPV4_ADDR_SIZE] =
+{ 0 };
#ifdef REMOTE_ARDUINO_ENROLEE
- //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
- // to Arduino WiFi enrollee
- static const char * UNICAST_PROVISIONING_QUERY = "coap://%s:%d/oic/res?rt=oic.r.prov";
+//Arduino Enrollee needs mediator application provide IP and port55555 which is specific
+// to Arduino WiFi enrollee
+static const char * UNICAST_PROVISIONING_QUERY = "coap://%s:%d/oic/res?rt=oic.r.prov";
#else
- static const char * UNICAST_PROVISIONING_QUERY = "/oic/res?rt=oic.r.prov";
+static const char * UNICAST_PROVISIONING_QUERY = "/oic/res?rt=oic.r.prov";
#endif
volatile static OCProvisioningStatusCB cbData = NULL;
-OCStackResult InitProvProcess() {
-
+OCStackResult InitProvProcess()
+{
OCStackResult result = OC_STACK_ERROR;
- if (InitProvisioningHandler() == OC_STACK_OK) {
+ if (InitProvisioningHandler() == OC_STACK_OK)
+ {
result = OC_STACK_OK;
OIC_LOG(DEBUG, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned Success");
- } else {
+ }
+ else
+ {
result = OC_STACK_ERROR;
OIC_LOG_V(ERROR, ES_PROV_ADAP_TAG, "InitProvisioningHandler returned error = %d",
- result);
+ result);
}
return result;
}
-OCStackResult ResetProvProcess() {
+OCStackResult ResetProvProcess()
+{
return TerminateProvisioningHandler();
}
-OCStackResult RegisterCallback(OCProvisioningStatusCB provisioningStatusCallback) {
+OCStackResult RegisterCallback(OCProvisioningStatusCB provisioningStatusCallback)
+{
OCStackResult result = OC_STACK_OK;
- if (provisioningStatusCallback != NULL) {
+ if (provisioningStatusCallback != NULL)
+ {
cbData = provisioningStatusCallback;
}
- else {
+ else
+ {
result = OC_STACK_ERROR;
OIC_LOG(ERROR, ES_PROV_ADAP_TAG, "provisioningStatusCallback is NULL");
}
return result;
}
-void UnRegisterCallback() {
- if (cbData) {
+void UnRegisterCallback()
+{
+ if (cbData)
+ {
cbData = NULL;
}
}
-OCStackResult StartProvisioning(const ProvConfig *provConfig, WiFiOnboadingConnection *onboardConn) {
+OCStackResult StartProvisioning(const ProvConfig *provConfig, WiFiOnboadingConnection *onboardConn)
+{
- char findQuery[64] = {0};
+ char findQuery[64] =
+ { 0 };
- if(provConfig == NULL || onboardConn == NULL){
+ if (provConfig == NULL || onboardConn == NULL)
+ {
return OC_STACK_ERROR;
}
//Arduino Enrollee needs mediator application provide IP and port55555 which is specific
// to Arduino WiFi enrollee
snprintf(findQuery, sizeof(findQuery) - 1, UNICAST_PROVISIONING_QUERY,
- onboardConn->ipAddress, IP_PORT);
+ onboardConn->ipAddress, IP_PORT);
#else
- OICStrcpy(findQuery, sizeof(findQuery)-1, UNICAST_PROVISIONING_QUERY);
+ OICStrcpy(findQuery, sizeof(findQuery) - 1, UNICAST_PROVISIONING_QUERY);
#endif
return StartProvisioningProcess(provConfig, onboardConn, cbData, findQuery);
}
-OCStackResult StopProvisioning(OCConnectivityType /*connectivityType*/) {
+OCStackResult StopProvisioning(OCConnectivityType /*connectivityType*/)
+{
OCStackResult result = OC_STACK_OK;
StopProvisioningProcess();
// External includes
#include "logger.h"
+#include "oic_string.h"
#define ES_WIFI_PROV_TAG "ES_WIFI_PROVISIONING"
static const char * UNICAST_PROV_STATUS_QUERY = "coap://%s:%d%s";
-OCStackApplicationResult GetProvisioningStatusResponse(void* /*ctx*/,
- OCDoHandle /*handle*/,
- OCClientResponse *clientResponse) {
+OCStackApplicationResult ProvisionEnrolleeResponse(void* /*ctx*/, OCDoHandle /*handle*/,
+ OCClientResponse *clientResponse)
+{
+ OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "Inside ProvisionEnrolleeResponse");
+ // If user stopped the process then return from this function;
+ if (IsSetupStopped())
+ {
+ ErrorCallback(DEVICE_NOT_PROVISIONED);
+ ClearMemory();
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ if (!ValidateEnrolleeResponse(clientResponse))
+ {
+ ErrorCallback(DEVICE_NOT_PROVISIONED);
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+
+ char query[OIC_STRING_MAX_VALUE] =
+ { '\0' };
+ char resUri[MAX_URI_LENGTH] =
+ { '\0' };
+ OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "Resource URI = %s", clientResponse->resourceUri);
+
+ OICStrcpy(resUri, sizeof(resUri), clientResponse->resourceUri);
+
+#ifdef REMOTE_ARDUINO_ENROLEE
+ //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
+ // to Arduino WiFi enrollee
+ // REMOTE_ARDUINO_ENROLEE has to be defined if Mediator is being tested with Arduino
+ snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
+ resUri);
+#else
+ snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr,
+ clientResponse->addr->port, resUri);
+#endif
+
+ if (TriggerNetworkConnection(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
+ != OC_STACK_OK)
+ {
+ OIC_LOG(INFO, ES_WIFI_PROV_TAG, "GetProvisioningStatusResponse received NULL clientResponse");
+
+ ErrorCallback(DEVICE_NOT_PROVISIONED);
+ ClearMemory();
+ }
+
+ return OC_STACK_DELETE_TRANSACTION;
+}
+
+OCStackApplicationResult GetProvisioningStatusResponse(void* /*ctx*/, OCDoHandle /*handle*/,
+ OCClientResponse *clientResponse)
+{
// If user stopped the process then return from this function;
- if (IsSetupStopped()) {
+ if (IsSetupStopped())
+ {
ErrorCallback(DEVICE_NOT_PROVISIONED);
ClearMemory();
return OC_STACK_DELETE_TRANSACTION;
}
- if (!ValidateEnrolleResponse(clientResponse)) {
+ if (!ValidateEnrolleeResponse(clientResponse))
+ {
ErrorCallback(DEVICE_NOT_PROVISIONED);
ClearMemory();
return OC_STACK_DELETE_TRANSACTION;
}
- OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
+ OCRepPayload *input = (OCRepPayload *) (clientResponse->payload);
- char query[OIC_STRING_MAX_VALUE] =
- {'\0'};
- char resURI[MAX_URI_LENGTH] =
- {'\0'};
+ char resUri[MAX_URI_LENGTH] = { '\0' };
- OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", input->uri);
+ OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", clientResponse->resourceUri);
- strncpy(resURI, input->uri, sizeof(resURI) - 1);
+ OICStrcpy(resUri, sizeof(resUri), clientResponse->resourceUri);
+ while (input)
+ {
+ int64_t ps;
+ if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps))
+ {
+
+ if (ps == ES_PS_NEED_PROVISIONING)
+ {
+ input = input->next;
+ continue;
+ }
+ else
+ {
+ ErrorCallback(DEVICE_NOT_PROVISIONED);
+ ClearMemory();
+ return OC_STACK_DELETE_TRANSACTION;
+ }
+ }
+
+ LogProvisioningResponse(input->values);
+ input = input->next;
+ }
+
+ char query[OIC_STRING_MAX_VALUE] =
+ { '\0' };
#ifdef REMOTE_ARDUINO_ENROLEE
//Arduino Enrollee needs mediator application provide IP and port55555 which is specific
// to Arduino WiFi enrollee
// REMOTE_ARDUINO_ENROLEE has to be defined if Mediator is being tested with Arduino
snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
- resURI);
+ resUri);
#else
snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr,
- clientResponse->addr->port,
- resURI);
+ clientResponse->addr->port, resUri);
#endif
-
if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
- != OC_STACK_OK) {
+ != OC_STACK_OK)
+ {
OIC_LOG(INFO, ES_WIFI_PROV_TAG, "GetProvisioningStatusResponse received NULL clientResponse");
ErrorCallback(DEVICE_NOT_PROVISIONED);
}
OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
- const OCDevAddr *destination) {
+ const OCDevAddr *destination)
+{
OCStackResult ret = OC_STACK_ERROR;
OCHeaderOption options[MAX_HEADER_OPTIONS];
- OIC_LOG_V(INFO, ES_WIFI_PROV_TAG, "\n\nExecuting %s", __func__);
+ OIC_LOG(DEBUG, ES_WIFI_PROV_TAG, "Inside GetProvisioningStatus");
uint8_t option0[] =
- {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
uint8_t option1[] =
- {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
+ { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
options[0].protocolID = OC_COAP_ID;
options[0].optionID = 2048;
memcpy(options[1].optionData, option1, sizeof(option1));
options[1].optionLength = 10;
- ret = InvokeOCDoResource(query, OC_REST_GET, destination, qos,
- GetProvisioningStatusResponse, NULL, options, 2);
+ ret = InvokeOCDoResource(query, OC_REST_GET, destination, qos, GetProvisioningStatusResponse,
+ NULL, options, 2);
return ret;
}
-
// This is a function called back when a device is discovered
-OCStackApplicationResult FindProvisioningResourceResponse(void* /*ctx*/,
- OCDoHandle /*handle*/,
- OCClientResponse *clientResponse) {
+OCStackApplicationResult FindProvisioningResourceResponse(void* /*ctx*/, OCDoHandle /*handle*/,
+ OCClientResponse *clientResponse)
+{
OIC_LOG_V(INFO, ES_WIFI_PROV_TAG, "Entering FindProvisioningResourceResponse %s",
clientResponse->devAddr.addr);
// If user stopped the process then return from this function;
- if (IsSetupStopped()) {
+ if (IsSetupStopped())
+ {
ErrorCallback(DEVICE_NOT_PROVISIONED);
ClearMemory();
return OC_STACK_DELETE_TRANSACTION;
}
- if (!ValidateFinddResourceResponse(clientResponse)) {
+ if (!ValidateFindResourceResponse(clientResponse))
+ {
ErrorCallback(DEVICE_NOT_PROVISIONED);
return OC_STACK_DELETE_TRANSACTION;
}
- char szQueryUri[64] = {0};
+ char szQueryUri[64] =
+ { 0 };
- OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload * )(clientResponse->payload);
+ OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload *) (clientResponse->payload);
OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", discoveryPayload->resources->uri);
// to Arduino WiFi enrollee
// REMOTE_ARDUINO_ENROLEE has to be defined if Mediator is being tested with Arduino
snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
- clientResponse->addr->addr,
- IP_PORT,
- discoveryPayload->resources->uri);
+ clientResponse->addr->addr,
+ IP_PORT,
+ discoveryPayload->resources->uri);
#else
snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
- clientResponse->devAddr.addr,
- clientResponse->devAddr.port,
- discoveryPayload->resources->uri);
+ clientResponse->devAddr.addr, clientResponse->devAddr.port,
+ discoveryPayload->resources->uri);
#endif
OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
- if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK) {
+ if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK)
+ {
ErrorCallback(DEVICE_NOT_PROVISIONED);
return OC_STACK_DELETE_TRANSACTION;
}
}
-bool ValidateEasySetupParams(const ProvConfig *netInfo,WiFiOnboadingConnection *onboardConn,
- OCProvisioningStatusCB provisioningStatusCallback) {
-
+bool ValidateEasySetupParams(const ProvConfig */*netInfo*/, WiFiOnboadingConnection *onboardConn,
+ OCProvisioningStatusCB provisioningStatusCallback)
+{
- if (onboardConn == NULL || strlen(onboardConn->ipAddress) == 0) {
+ if (onboardConn == NULL || strlen(onboardConn->ipAddress) == 0)
+ {
OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "Request URI is NULL");
return false;
}
- if (provisioningStatusCallback == NULL) {
+ if (provisioningStatusCallback == NULL)
+ {
OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "ProvisioningStatusCallback is NULL");
return false;
}
}
-
-
*/
#define ES_PS_NEED_PROVISIONING 1
#define ES_PS_PROVISIONING_COMPLETED 2
+#define ES_PS_TRIGGER_INIT_VALUE 0
+#define ES_PS_TRIGGER_CONNECTION 1
#ifndef WITH_ARDUINO
namespace OIC
ESResult ESDiscoveryTimeout(unsigned short waittime);
void onDeviceDiscovered(std::shared_ptr<OC::OCResource> resource);
+
+ void triggerNetworkConnection();
+
+ void triggerNetworkConnectionCb(
+ const HeaderOptions& headerOptions, const OCRepresentation& rep,
+ const int eCode);
};
}
}
m_discoveryResponse = false;
}
+ void RemoteEnrolleeResource::triggerNetworkConnectionCb(
+ const HeaderOptions& /*headerOptions*/, const OCRepresentation& rep,
+ const int eCode)
+ {
+ OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : %s, eCode = %d",
+ rep.getUri().c_str(),
+ eCode);
+
+ if (eCode != 0)
+ {
+ OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
+ "triggerNetworkConnectionCb : Trigger action failed ");
+ std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
+ ProvisioningStatus >(ESResult::ES_ERROR, ESState::ES_PROVISIONING_ERROR);
+ m_provStatusCb(provStatus);
+ return;
+ }
+ else
+ {
+ OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
+ "triggerNetworkConnectionCb : Provisioning is success ");
+ std::shared_ptr< ProvisioningStatus > provStatus = std::make_shared<
+ ProvisioningStatus >(ESResult::ES_OK, ESState::ES_PROVISIONING_SUCCESS);
+ m_provStatusCb(provStatus);
+ return;
+ }
+ }
+
+ 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 RemoteEnrolleeResource::checkProvInformationCb(const HeaderOptions& /*headerOptions*/,
const OCRepresentation& rep, const int eCode)
{
}
int ps = -1;
- std::string tnn = "";
- std::string cd = "";
rep.getValue(OC_RSRVD_ES_PS, ps);
- rep.getValue(OC_RSRVD_ES_TNN, tnn);
- rep.getValue(OC_RSRVD_ES_CD, cd);
OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "checkProvInformationCb : ps - %d", ps);
- OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG,
- "checkProvInformationCb : tnn - %s", tnn.c_str());
- OIC_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_ProvConfig.provData.WIFI.ssid))
- {
- OIC_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);
- return;
- }
-
- if (cd != std::string(m_ProvConfig.provData.WIFI.pwd))
- {
- OIC_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<
- ProvisioningStatus >(ESResult::ES_ERROR,
- ESState::ES_PROVISIONING_ERROR);
- m_provStatusCb(provStatus);
- return;
- }
-
OIC_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);
- m_provStatusCb(provStatus);
+ "checkProvInformationCb : Provisioning is success. "
+ "Now trigger network connection ");
+
+ triggerNetworkConnection();
return;
}
else
}
int ps = -1;
- std::string tnn = "";
- std::string cd = "";
rep.getValue(OC_RSRVD_ES_PS, ps);
- rep.getValue(OC_RSRVD_ES_TNN, tnn);
- rep.getValue(OC_RSRVD_ES_CD, cd);
OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : ps - %d",
ps);
- OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : tnn - %s",
- tnn.c_str());
- OIC_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
{
OIC_LOG_V (DEBUG, ES_REMOTE_ENROLLEE_RES_TAG, "getProvStatusResponse : pwd - %s",
m_ProvConfig.provData.WIFI.pwd);
- m_ocResource->put(provisioningRepresentation, QueryParamsMap(),
+ m_ocResource->post(provisioningRepresentation, QueryParamsMap(),
std::function<
void(const HeaderOptions& headerOptions,
const OCRepresentation& rep, const int eCode) >(
InitProvProcess();
- RegisterCallback(ProvisioningStatusCallback);
+ RegisterCallback(&ProvisioningStatusCallback);
while ((opt = getopt(argc, argv, "d:s:p:")) != -1) {
switch (opt) {
signal(SIGINT, handleSigInt);
while (!quitFlag) {
+ OCStackResult result;
+
+ result = OCProcess();
+
+ if (result != OC_STACK_OK)
+ {
+ OIC_LOG(ERROR, "Mediator_CSDK", "OCStack stop error");
+ }
+
+ // To minimize CPU utilization we may wish to do this with sleep
sleep(1);
}
easySetupIntance = EasySetup::getInstance();
ipaddress = "192.168.1.104";
- //std::cout << "Enter the target enrollee ipv4 address ";
-
-
ssid = "hub2.4G";
- //std::cout << "Enter the ssid of the target Enrolleer ";
- //std:: cin >> ssid;
-
pwd = "22221111";
- //std::cout << "Enter the pwd of the target Enrolleer ";
- //std::cin >> pwd;
netInfo.connType = CT_ADAPTER_IP;
- //netInfo.isSecured = false;
- //netInfo.needSecuredEasysetup = false;
OICStrcpy(netInfo.provData.WIFI.ssid, NET_WIFI_SSID_SIZE - 1, ssid.c_str());
OICStrcpy(netInfo.provData.WIFI.pwd, NET_WIFI_PWD_SIZE - 1, pwd.c_str());
onboardingConn.isSecured = false;
OICStrcpy(onboardingConn.ipAddress, IPV4_ADDR_SIZE - 1, ipaddress.c_str());
-
try
{
remoteEnrollee = easySetupIntance->createEnrolleeDevice(netInfo,onboardingConn);