1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 // NOTE : Keeping Wifi provisioning in this file to have adaptability while doing OOPs refactoring
23 #include "provisioning.h"
33 //EasySetup include files
34 #include "ocpayload.h"
40 #define ES_WIFI_PROV_TAG "ES_WIFI_PROVISIONING"
42 static const char * UNICAST_PROV_STATUS_QUERY = "coap://%s:%d%s";
44 OCStackApplicationResult GetProvisioningStatusResponse(void* /*ctx*/,
45 OCDoHandle /*handle*/,
46 OCClientResponse *clientResponse) {
49 // If user stopped the process then return from this function;
50 if (IsSetupStopped()) {
51 ErrorCallback(DEVICE_NOT_PROVISIONED);
53 return OC_STACK_DELETE_TRANSACTION;
56 if (!ValidateEnrolleResponse(clientResponse)) {
57 ErrorCallback(DEVICE_NOT_PROVISIONED);
59 return OC_STACK_DELETE_TRANSACTION;
62 OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
64 char query[OIC_STRING_MAX_VALUE] =
66 char resURI[MAX_URI_LENGTH] =
69 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", input->uri);
71 strncpy(resURI, input->uri, sizeof(resURI) - 1);
73 snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
76 if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
78 OIC_LOG(INFO, ES_WIFI_PROV_TAG, "GetProvisioningStatusResponse received NULL clientResponse");
80 ErrorCallback(DEVICE_NOT_PROVISIONED);
82 return OC_STACK_DELETE_TRANSACTION;
85 return OC_STACK_KEEP_TRANSACTION;
89 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
90 const OCDevAddr *destination) {
91 OCStackResult ret = OC_STACK_ERROR;
92 OCHeaderOption options[MAX_HEADER_OPTIONS];
94 OIC_LOG_V(INFO, ES_WIFI_PROV_TAG, "\n\nExecuting %s", __func__);
97 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
99 {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
100 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
101 options[0].protocolID = OC_COAP_ID;
102 options[0].optionID = 2048;
103 memcpy(options[0].optionData, option0, sizeof(option0));
104 options[0].optionLength = 10;
105 options[1].protocolID = OC_COAP_ID;
106 options[1].optionID = 3000;
107 memcpy(options[1].optionData, option1, sizeof(option1));
108 options[1].optionLength = 10;
110 ret = InvokeOCDoResource(query, OC_REST_GET, destination, qos,
111 GetProvisioningStatusResponse, NULL, options, 2);
116 // This is a function called back when a device is discovered
117 OCStackApplicationResult FindProvisioningResourceResponse(void* /*ctx*/,
118 OCDoHandle /*handle*/,
119 OCClientResponse *clientResponse) {
121 OIC_LOG(INFO, ES_WIFI_PROV_TAG, PCF("Entering FindProvisioningResourceResponse"));
123 // If user stopped the process then return from this function;
124 if (IsSetupStopped()) {
125 ErrorCallback(DEVICE_NOT_PROVISIONED);
127 return OC_STACK_DELETE_TRANSACTION;
131 if (!ValidateFinddResourceResponse(clientResponse)) {
132 ErrorCallback(DEVICE_NOT_PROVISIONED);
133 return OC_STACK_DELETE_TRANSACTION;
136 char szQueryUri[64] = {0};
138 OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload * )(clientResponse->payload);
140 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", discoveryPayload->resources->uri);
142 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
143 clientResponse->devAddr.addr, IP_PORT, discoveryPayload->resources->uri);
145 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
147 if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK) {
148 ErrorCallback(DEVICE_NOT_PROVISIONED);
149 return OC_STACK_DELETE_TRANSACTION;
152 return OC_STACK_KEEP_TRANSACTION;
156 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
157 OCProvisioningStatusCB provisioningStatusCallback) {
159 if (netInfo == NULL || strlen(netInfo->netAddressInfo.WIFI.ipAddress) == 0) {
160 OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "Request URI is NULL");
164 if (provisioningStatusCallback == NULL) {
165 OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "ProvisioningStatusCallback is NULL");