Added Multi PHY EasySetup Service
[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 "common.h"
36
37 // External includes
38 #include "logger.h"
39 #include "oic_malloc.h"
40
41
42 static EnrolleeNWProvInfo_t *netProvInfo;
43
44 /**
45  * @var cbData
46  * @brief Callback for providing provisioning status callback to application
47  */
48 static OCProvisioningStatusCB cbData = NULL;
49
50
51 OCStackApplicationResult GetProvisioningStatusResponse(void *ctx, OCDoHandle handle,
52                                                        OCClientResponse *clientResponse) {
53
54
55     // If user stopped the process then return from this function;
56     if (IsSetupStopped()) {
57         ErrorCallback(DEVICE_NOT_PROVISIONED);
58         ClearMemory();
59         return OC_STACK_DELETE_TRANSACTION;
60     }
61
62
63     ProvisioningInfo *provInfo;
64
65     if (!ValidateEnrolleResponse(clientResponse)) {
66         ErrorCallback(DEVICE_NOT_PROVISIONED);
67         ClearMemory();
68         return OC_STACK_DELETE_TRANSACTION;
69     }
70
71     OCRepPayload *input = (OCRepPayload * )(clientResponse->payload);
72
73     char query[OIC_STRING_MAX_VALUE] =
74             {'\0'};
75     char resURI[MAX_URI_LENGTH] =
76             {'\0'};
77
78     OIC_LOG_V(DEBUG, TAG, "resUri = %s", input->uri);
79
80     strncpy(resURI, input->uri, sizeof(resURI) - 1);
81
82     snprintf(query, sizeof(query), UNICAST_PROV_STATUS_QUERY, clientResponse->addr->addr, IP_PORT,
83              resURI);
84
85     //OCPayloadLogRep(DEBUG,TAG,input);
86
87     if (ProvisionEnrollee(OC_HIGH_QOS, query, OC_RSRVD_ES_URI_PROV, clientResponse->addr, 0)
88         != OC_STACK_OK) {
89         OIC_LOG(INFO, TAG,
90                 "GetProvisioningStatusResponse received NULL clientResponse.Invoking Provisioing Status Callback");
91
92         ErrorCallback(DEVICE_NOT_PROVISIONED);
93         ClearMemory();
94         return OC_STACK_DELETE_TRANSACTION;
95     }
96
97     return OC_STACK_KEEP_TRANSACTION;
98
99 }
100
101 OCStackResult GetProvisioningStatus(OCQualityOfService qos, const char *query,
102                                     const OCDevAddr *destination) {
103     OCStackResult ret = OC_STACK_ERROR;
104     OCHeaderOption options[MAX_HEADER_OPTIONS];
105
106     OIC_LOG_V(INFO, TAG, "\n\nExecuting %s", __func__);
107
108     uint8_t option0[] =
109             {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
110     uint8_t option1[] =
111             {11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
112     memset(options, 0, sizeof(OCHeaderOption) * MAX_HEADER_OPTIONS);
113     options[0].protocolID = OC_COAP_ID;
114     options[0].optionID = 2048;
115     memcpy(options[0].optionData, option0, sizeof(option0));
116     options[0].optionLength = 10;
117     options[1].protocolID = OC_COAP_ID;
118     options[1].optionID = 3000;
119     memcpy(options[1].optionData, option1, sizeof(option1));
120     options[1].optionLength = 10;
121
122     ret = InvokeOCDoResource(query, OC_REST_GET, destination, OC_HIGH_QOS,
123                              GetProvisioningStatusResponse, NULL, options, 2);
124     return ret;
125 }
126
127
128 // This is a function called back when a device is discovered
129 OCStackApplicationResult FindProvisioningResourceResponse(void *ctx, OCDoHandle handle,
130                                                           OCClientResponse *clientResponse) {
131
132     OIC_LOG(INFO, TAG, PCF("Entering FindProvisioningResourceResponse"));
133
134     // If user stopped the process then return from this function;
135     if (IsSetupStopped()) {
136         ErrorCallback(DEVICE_NOT_PROVISIONED);
137         ClearMemory();
138         return OC_STACK_DELETE_TRANSACTION;
139     }
140
141
142     if (!ValidateFinddResourceResponse(clientResponse)) {
143         ErrorCallback(DEVICE_NOT_PROVISIONED);
144         return OC_STACK_DELETE_TRANSACTION;
145     }
146
147     OCStackApplicationResult response = OC_STACK_DELETE_TRANSACTION;
148
149     ProvisioningInfo *provInfo;
150     char szQueryUri[64] =
151             {0};
152
153     OCDiscoveryPayload *discoveryPayload = (OCDiscoveryPayload * )(clientResponse->payload);
154
155     // Need to conform if below check is required or not. As Null check of clientResponse->payload is already performed above
156     if (!discoveryPayload) {
157         OIC_LOG_V(DEBUG, TAG, "Failed To parse");
158         ErrorCallback(DEVICE_NOT_PROVISIONED);
159         return OC_STACK_DELETE_TRANSACTION;
160     }
161
162     OIC_LOG_V(DEBUG, TAG, "resUri = %s", discoveryPayload->resources->uri);
163
164     snprintf(szQueryUri, sizeof(szQueryUri), UNICAST_PROV_STATUS_QUERY,
165              clientResponse->devAddr.addr, IP_PORT, discoveryPayload->resources->uri);
166
167     OIC_LOG_V(DEBUG, TAG, "query before GetProvisioningStatus call = %s", szQueryUri);
168
169     if (GetProvisioningStatus(OC_HIGH_QOS, szQueryUri, &clientResponse->devAddr) != OC_STACK_OK) {
170         ErrorCallback(DEVICE_NOT_PROVISIONED);
171         return OC_STACK_DELETE_TRANSACTION;
172     }
173
174     return OC_STACK_KEEP_TRANSACTION;
175
176 }
177
178 bool ValidateEasySetupParams(const EnrolleeNWProvInfo_t *netInfo,
179                              OCProvisioningStatusCB provisioningStatusCallback) {
180
181     if (netInfo == NULL || strlen(netInfo->netAddressInfo.WIFI.ipAddress) == 0) {
182         OIC_LOG(ERROR, TAG, "Request URI is NULL");
183         return false;
184     }
185
186     if (provisioningStatusCallback == NULL) {
187         OIC_LOG(ERROR, TAG, "ProvisioningStatusCallback is NULL");
188         return false;
189     }
190
191     return true;
192
193 }
194
195
196