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"
39 #include "oic_string.h"
41 #define ES_WIFI_PROV_TAG "ES_WIFI_PROVISIONING"
43 static const char * UNICAST_PROV_STATUS_QUERY = "coap://%s:%d%s";
45 OCStackApplicationResult ProvisionEnrolleeResponse(void* /*ctx*/, OCDoHandle /*handle*/,
46 OCClientResponse *clientResponse)
48 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "Inside ProvisionEnrolleeResponse");
50 // If user stopped the process then return from this function;
53 ErrorCallback(DEVICE_NOT_PROVISIONED);
55 return OC_STACK_DELETE_TRANSACTION;
58 if (!ValidateEnrolleeResponse(clientResponse))
60 ErrorCallback(DEVICE_NOT_PROVISIONED);
61 return OC_STACK_DELETE_TRANSACTION;
64 char query[OIC_STRING_MAX_VALUE] =
66 char resUri[MAX_URI_LENGTH] =
69 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "Resource URI = %s", clientResponse->resourceUri);
71 OICStrcpy(resUri, sizeof(resUri), clientResponse->resourceUri);
73 #ifdef REMOTE_ARDUINO_ENROLEE
74 //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
75 // to Arduino WiFi enrollee
76 // REMOTE_ARDUINO_ENROLEE has to be defined if Mediator is being tested with Arduino
77 snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
80 snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr,
81 clientResponse->addr->port, resUri);
84 if (TriggerNetworkConnection(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
87 OIC_LOG(INFO, ES_WIFI_PROV_TAG, "GetProvisioningStatusResponse received NULL clientResponse");
89 ErrorCallback(DEVICE_NOT_PROVISIONED);
93 return OC_STACK_DELETE_TRANSACTION;
96 OCStackApplicationResult GetProvisioningStatusResponse(void* /*ctx*/, OCDoHandle /*handle*/,
97 OCClientResponse *clientResponse)
99 // If user stopped the process then return from this function;
100 if (IsSetupStopped())
102 ErrorCallback(DEVICE_NOT_PROVISIONED);
104 return OC_STACK_DELETE_TRANSACTION;
107 if (!ValidateEnrolleeResponse(clientResponse))
109 ErrorCallback(DEVICE_NOT_PROVISIONED);
111 return OC_STACK_DELETE_TRANSACTION;
114 OCRepPayload *input = (OCRepPayload *) (clientResponse->payload);
116 char resUri[MAX_URI_LENGTH] = { '\0' };
118 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", clientResponse->resourceUri);
120 OICStrcpy(resUri, sizeof(resUri), clientResponse->resourceUri);
125 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_PS, &ps))
128 if (ps == ES_PS_NEED_PROVISIONING)
135 ErrorCallback(DEVICE_NOT_PROVISIONED);
137 return OC_STACK_DELETE_TRANSACTION;
141 LogProvisioningResponse(input->values);
145 char query[OIC_STRING_MAX_VALUE] =
148 #ifdef REMOTE_ARDUINO_ENROLEE
149 //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
150 // to Arduino WiFi enrollee
151 // REMOTE_ARDUINO_ENROLEE has to be defined if Mediator is being tested with Arduino
152 snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
155 snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr,
156 clientResponse->addr->port, resUri);
159 if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
162 OIC_LOG(INFO, ES_WIFI_PROV_TAG, "GetProvisioningStatusResponse received NULL clientResponse");
164 ErrorCallback(DEVICE_NOT_PROVISIONED);
166 return OC_STACK_DELETE_TRANSACTION;
169 return OC_STACK_KEEP_TRANSACTION;
173 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
174 const OCDevAddr *destination)
176 OCStackResult ret = OC_STACK_ERROR;
177 OCHeaderOption options[MAX_HEADER_OPTIONS];
179 OIC_LOG(DEBUG, ES_WIFI_PROV_TAG, "Inside GetProvisioningStatus");
182 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
184 { 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
185 memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
186 options[0].protocolID = OC_COAP_ID;
187 options[0].optionID = 2048;
188 memcpy(options[0].optionData, option0, sizeof(option0));
189 options[0].optionLength = 10;
190 options[1].protocolID = OC_COAP_ID;
191 options[1].optionID = 3000;
192 memcpy(options[1].optionData, option1, sizeof(option1));
193 options[1].optionLength = 10;
195 ret = InvokeOCDoResource(query, OC_REST_GET, destination, qos, GetProvisioningStatusResponse,
200 // This is a function called back when a device is discovered
201 OCStackApplicationResult FindProvisioningResourceResponse(void* /*ctx*/, OCDoHandle /*handle*/,
202 OCClientResponse *clientResponse)
205 OIC_LOG_V(INFO, ES_WIFI_PROV_TAG, "Entering FindProvisioningResourceResponse %s",
206 clientResponse->devAddr.addr);
208 // If user stopped the process then return from this function;
209 if (IsSetupStopped())
211 ErrorCallback(DEVICE_NOT_PROVISIONED);
213 return OC_STACK_DELETE_TRANSACTION;
216 if (!ValidateFindResourceResponse(clientResponse))
218 ErrorCallback(DEVICE_NOT_PROVISIONED);
219 return OC_STACK_DELETE_TRANSACTION;
222 char szQueryUri[64] =
225 OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload *) (clientResponse->payload);
227 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", discoveryPayload->resources->uri);
229 #ifdef REMOTE_ARDUINO_ENROLEE
230 //Arduino Enrollee needs mediator application provide IP and port55555 which is specific
231 // to Arduino WiFi enrollee
232 // REMOTE_ARDUINO_ENROLEE has to be defined if Mediator is being tested with Arduino
233 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
234 clientResponse->addr->addr,
236 discoveryPayload->resources->uri);
238 snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
239 clientResponse->devAddr.addr, clientResponse->devAddr.port,
240 discoveryPayload->resources->uri);
243 OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
245 if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK)
247 ErrorCallback(DEVICE_NOT_PROVISIONED);
248 return OC_STACK_DELETE_TRANSACTION;
251 return OC_STACK_KEEP_TRANSACTION;
255 bool ValidateEasySetupParams(const ProvConfig */*netInfo*/, WiFiOnboadingConnection *onboardConn,
256 OCProvisioningStatusCB provisioningStatusCallback)
259 if (onboardConn == NULL || strlen(onboardConn->ipAddress) == 0)
261 OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "Request URI is NULL");
265 if (provisioningStatusCallback == NULL)
267 OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "ProvisioningStatusCallback is NULL");