Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / easy-setup / sdk / mediator / src / wifi_provisioning.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 // NOTE : Keeping Wifi provisioning in this file to have adaptability while doing OOPs refactoring
22
23 #include "provisioning.h"
24
25 //Standard includes
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <signal.h>
30 #include <unistd.h>
31 #include <pthread.h>
32
33 //EasySetup include files
34 #include "ocpayload.h"
35 #include "escommon.h"
36
37 // External includes
38 #include "logger.h"
39
40 #define ES_WIFI_PROV_TAG "ES_WIFI_PROVISIONING"
41
42 static const char * UNICAST_PROV_STATUS_QUERY = "coap://%s:%d%s";
43
44 OCStackApplicationResult GetProvisioningStatusResponse(void* /*ctx*/,
45                                                         OCDoHandle /*handle*/,
46                                                         OCClientResponse *clientResponse) {
47
48
49     // If user stopped the process then return from this function;
50     if (IsSetupStopped()) {
51         ErrorCallback(DEVICE_NOT_PROVISIONED);
52         ClearMemory();
53         return OC_STACK_DELETE_TRANSACTION;
54     }
55
56     if (!ValidateEnrolleResponse(clientResponse)) {
57         ErrorCallback(DEVICE_NOT_PROVISIONED);
58         ClearMemory();
59         return OC_STACK_DELETE_TRANSACTION;
60     }
61
62     OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
63
64     char query[OIC_STRING_MAX_VALUE] =
65             {'\0'};
66     char resURI[MAX_URI_LENGTH] =
67             {'\0'};
68
69     OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", input->uri);
70
71     strncpy(resURI, input->uri, sizeof(resURI) - 1);
72
73     snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
74              resURI);
75
76     if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
77         != OC_STACK_OK) {
78         OIC_LOG(INFO, ES_WIFI_PROV_TAG, "GetProvisioningStatusResponse received NULL clientResponse");
79
80         ErrorCallback(DEVICE_NOT_PROVISIONED);
81         ClearMemory();
82         return OC_STACK_DELETE_TRANSACTION;
83     }
84
85     return OC_STACK_KEEP_TRANSACTION;
86
87 }
88
89 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
90                                     const OCDevAddr *destination) {
91     OCStackResult ret = OC_STACK_ERROR;
92     OCHeaderOption options[MAX_HEADER_OPTIONS];
93
94     OIC_LOG_V(INFO, ES_WIFI_PROV_TAG, "\n\nExecuting %s", __func__);
95
96     uint8_t option0[] =
97             {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
98     uint8_t option1[] =
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;
109
110     ret = InvokeOCDoResource(query, OC_REST_GET, destination, qos,
111                              GetProvisioningStatusResponse, NULL, options, 2);
112     return ret;
113 }
114
115
116 // This is a function called back when a device is discovered
117 OCStackApplicationResult FindProvisioningResourceResponse(void* /*ctx*/,
118                                                             OCDoHandle /*handle*/,
119                                                             OCClientResponse *clientResponse) {
120
121     OIC_LOG(INFO, ES_WIFI_PROV_TAG, PCF("Entering FindProvisioningResourceResponse"));
122
123     // If user stopped the process then return from this function;
124     if (IsSetupStopped()) {
125         ErrorCallback(DEVICE_NOT_PROVISIONED);
126         ClearMemory();
127         return OC_STACK_DELETE_TRANSACTION;
128     }
129
130
131     if (!ValidateFinddResourceResponse(clientResponse)) {
132         ErrorCallback(DEVICE_NOT_PROVISIONED);
133         return OC_STACK_DELETE_TRANSACTION;
134     }
135
136     char szQueryUri[64] = {0};
137
138     OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload * )(clientResponse->payload);
139
140     OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "resUri = %s", discoveryPayload->resources->uri);
141
142     snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
143              clientResponse->devAddr.addr, IP_PORT, discoveryPayload->resources->uri);
144
145     OIC_LOG_V(DEBUG, ES_WIFI_PROV_TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
146
147     if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK) {
148         ErrorCallback(DEVICE_NOT_PROVISIONED);
149         return OC_STACK_DELETE_TRANSACTION;
150     }
151
152     return OC_STACK_KEEP_TRANSACTION;
153
154 }
155
156 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
157                              OCProvisioningStatusCB provisioningStatusCallback) {
158
159     if (netInfo == NULL || strlen(netInfo->netAddressInfo.WIFI.ipAddress) == 0) {
160         OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "Request URI is NULL");
161         return false;
162     }
163
164     if (provisioningStatusCallback == NULL) {
165         OIC_LOG(ERROR, ES_WIFI_PROV_TAG, "ProvisioningStatusCallback is NULL");
166         return false;
167     }
168
169     return true;
170
171 }
172
173
174