Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / easysetup.c
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 /**
22  * @file
23  *
24  * This file contains the implementation for EasySetup Enrollee device
25  */
26
27 #include "easysetup.h"
28 #include "logger.h"
29 #include "resourcehandler.h"
30 #include "oic_string.h"
31
32 /**
33  * @var ES_ENROLLEE_TAG
34  * @brief Logging tag for module name.
35  */
36 #define ES_ENROLLEE_TAG "ES"
37
38 //-----------------------------------------------------------------------------
39 // Private variables
40 //-----------------------------------------------------------------------------
41
42 /**
43  * @var gTargetSsid
44  * @brief Target SSID of the Soft Access point to which the device has to connect
45  */
46 // static char gTargetSsid[MAXSSIDLEN];
47
48 /**
49  * @var gTargetPass
50  * @brief Password of the target access point to which the device has to connect
51  */
52 // static char gTargetPass[MAXNETCREDLEN];
53
54 /**
55  * @var gEnrolleeStatusCb
56  * @brief Fucntion pointer holding the callback for intimation of EasySetup Enrollee status callback
57  */
58 // static ESEnrolleeEventCallback gEnrolleeStatusCb = NULL;
59
60 /**
61  * @var gIsSecured
62  * @brief Variable to check if secure mode is enabled or not.
63  */
64 static bool gIsSecured = false;
65
66 static ESProvisioningCallbacks gESProvisioningCb;
67 static ESDeviceProperty gESDeviceProperty;
68
69 void ESWiFiRsrcCallback(ESResult esResult, ESWiFiProvData *eventData)
70 {
71     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback IN");
72
73     if(esResult != ES_OK)
74     {
75         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESWiFiRsrcCallback Error Occured");
76         return;
77     }
78
79     // deliver data to ESProvisioningCallbacks
80     if(gESProvisioningCb.WiFiProvCb != NULL)
81     {
82         gESProvisioningCb.WiFiProvCb(eventData);
83     }
84     else
85     {
86         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb is NULL");
87         return;
88     }
89 }
90
91 void ESCloudRsrcCallback(ESResult esResult, ESCloudProvData *eventData)
92 {
93     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESCloudRsrcCallback IN");
94
95     if(esResult != ES_OK)
96     {
97         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESCloudRsrcCallback Error Occured");
98         return;
99     }
100
101     if(gESProvisioningCb.CloudDataProvCb != NULL)
102     {
103         gESProvisioningCb.CloudDataProvCb(eventData);
104     }
105     else
106     {
107         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb is NULL");
108         return;
109     }
110 }
111
112 void ESDevconfRsrcallback(ESResult esResult, ESDevConfProvData *eventData)
113 {
114     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESDevconfRsrcallback IN");
115
116     if(esResult != ES_OK)
117     {
118         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESDevconfRsrcallback Error Occured");
119         return;
120     }
121
122     if(gESProvisioningCb.DevConfProvCb != NULL)
123     {
124         gESProvisioningCb.DevConfProvCb(eventData);
125     }
126     else
127     {
128         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb is NULL");
129         return;
130     }
131 }
132
133 ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvisioningCallbacks callbacks)
134 {
135     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
136
137     gIsSecured = isSecured;
138
139     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
140     {
141         if(callbacks.WiFiProvCb != NULL)
142         {
143             gESProvisioningCb.WiFiProvCb = callbacks.WiFiProvCb;
144             RegisterWifiRsrcEventCallBack(ESWiFiRsrcCallback);
145         }
146         else
147         {
148             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiProvCb NULL");
149             return ES_ERROR;
150         }
151     }
152     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
153     {
154         if(callbacks.DevConfProvCb != NULL)
155         {
156             gESProvisioningCb.DevConfProvCb = callbacks.DevConfProvCb;
157             RegisterDevConfRsrcEventCallBack(ESDevconfRsrcallback);
158         }
159         else
160         {
161             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb NULL");
162             return ES_ERROR;
163         }
164     }
165     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
166     {
167         if(callbacks.DevConfProvCb != NULL)
168         {
169             gESProvisioningCb.CloudDataProvCb = callbacks.CloudDataProvCb;
170             RegisterCloudRsrcEventCallBack(ESCloudRsrcCallback);
171         }
172         else
173         {
174             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CloudDataProvCb NULL");
175             return ES_ERROR;
176         }
177     }
178
179     if(CreateEasySetupResources(gIsSecured, resourceMask) != OC_STACK_OK)
180     {
181         UnRegisterResourceEventCallBack();
182
183         if (DeleteEasySetupResources() != OC_STACK_OK)
184         {
185             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
186         }
187
188         return ES_ERROR;
189     }
190
191
192     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
193     return ES_OK;
194 }
195
196 ESResult ESSetDeviceProperty(ESDeviceProperty *deviceProperty)
197 {
198     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty IN");
199
200     if(SetDeviceProperty(deviceProperty) != OC_STACK_OK)
201     {
202         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetDeviceProperty Error");
203         return ES_ERROR;
204     }
205
206     int modeIdx = 0;
207     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
208     {
209         (gESDeviceProperty.WiFi).mode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
210         OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "WiFi Mode : %d", (gESDeviceProperty.WiFi).mode[modeIdx]);
211         modeIdx ++;
212     }
213     (gESDeviceProperty.WiFi).freq = (deviceProperty->WiFi).freq;
214     OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "WiFi Freq : %d", (gESDeviceProperty.WiFi).freq);
215
216     OICStrcpy((gESDeviceProperty.DevConf).deviceName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
217     OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Device Name : %s", (gESDeviceProperty.DevConf).deviceName);
218
219     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty OUT");
220     return ES_OK;
221 }
222
223 ESResult ESSetState(ESEnrolleeState esState)
224 {
225     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState IN");
226
227     if(esState < ES_STATE_INIT || esState > ES_STATE_REGISTRRED_FAIL_TO_CLOUD)
228     {
229         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESEnrolleeState : %d", esState);
230         return ES_ERROR;
231     }
232
233     if(SetEnrolleeState(esState) != OC_STACK_OK)
234     {
235         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetState ES_ERROR");
236         return ES_ERROR;
237     }
238
239     OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESState succesfully : %d", esState);
240     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState OUT");
241     return ES_OK;
242 }
243
244 ESResult ESSetErrorCode(ESErrorCode esErrCode)
245 {
246     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode IN");
247
248     if(esErrCode < ES_ERRCODE_NO_ERROR || esErrCode > ES_ERRCODE_UNKNOWN)
249     {
250         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESSetErrorCode : %d", esErrCode);
251             return ES_ERROR;
252     }
253
254     if(SetEnrolleeErrCode(esErrCode) != OC_STACK_OK)
255     {
256         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetErrorCode ES_ERROR");
257         return ES_ERROR;
258     }
259
260     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "Set ESErrorCode succesfully : %d", esErrCode);
261     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode OUT");
262     return ES_OK;
263 }
264
265 ESResult ESTerminateEnrollee()
266 {
267     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESTerminateEnrollee IN");
268
269     UnRegisterResourceEventCallBack();
270
271     //Delete Prov resource
272     if (DeleteEasySetupResources() != OC_STACK_OK)
273     {
274         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
275         return ES_ERROR;
276     }
277
278     OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
279     return ES_OK;
280 }