[IOT-2906][ES] Remove "href" from child response.
[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 static bool gIsSecured = false;
43
44 static ESProvisioningCallbacks gESProvisioningCb;
45
46 void ESConnectRequestCallback(ESResult esResult, ESConnectRequest *eventData)
47 {
48     OIC_LOG_V(DEBUG, ES_ENROLLEE_TAG, "ESConnectRequestCallback IN");
49
50     if(esResult != ES_OK)
51     {
52         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ESConnectRequestCallback Error Occured");
53         return;
54     }
55
56     if(gESProvisioningCb.ConnectRequestCb != NULL)
57     {
58         gESProvisioningCb.ConnectRequestCb(eventData);
59     }
60     else
61     {
62         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "ConnectRequestCb is NULL");
63         return;
64     }
65 }
66
67 void ESWiFiConfRsrcCallback(ESResult esResult, ESWiFiConfData *eventData)
68 {
69     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESWiFiConfRsrcCallback IN");
70
71     if(esResult != ES_OK)
72     {
73         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESWiFiConfRsrcCallback Error Occured");
74         return;
75     }
76
77     // deliver data to ESProvisioningCallbacks
78     if(gESProvisioningCb.WiFiConfProvCb != NULL)
79     {
80         gESProvisioningCb.WiFiConfProvCb(eventData);
81     }
82     else
83     {
84         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiConfProvCb is NULL");
85         return;
86     }
87 }
88
89 void ESCoapCloudConfRsrcCallback(ESResult esResult, ESCoapCloudConfData *eventData)
90 {
91     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESCoapCloudConfRsrcCallback IN");
92
93     if(esResult != ES_OK)
94     {
95         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESCoapCloudConfRsrcCallback Error Occured");
96         return;
97     }
98
99     if(gESProvisioningCb.CoapCloudConfProvCb != NULL)
100     {
101         gESProvisioningCb.CoapCloudConfProvCb(eventData);
102     }
103     else
104     {
105         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CoapCloudConfProvCb is NULL");
106         return;
107     }
108 }
109
110 void ESDevConfRsrcallback(ESResult esResult, ESDevConfData *eventData)
111 {
112     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESDevConfRsrcallback IN");
113
114     if(esResult != ES_OK)
115     {
116         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESDevConfRsrcallback Error Occured");
117         return;
118     }
119
120     if(gESProvisioningCb.DevConfProvCb != NULL)
121     {
122         gESProvisioningCb.DevConfProvCb(eventData);
123     }
124     else
125     {
126         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb is NULL");
127         return;
128     }
129 }
130
131 ESResult ESInitEnrollee(bool isSecured, ESResourceMask resourceMask, ESProvisioningCallbacks callbacks)
132 {
133     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee IN");
134
135     gIsSecured = isSecured;
136
137     if((resourceMask & ES_WIFICONF_RESOURCE) == ES_WIFICONF_RESOURCE)
138     {
139         if(callbacks.WiFiConfProvCb != NULL)
140         {
141             gESProvisioningCb.WiFiConfProvCb = callbacks.WiFiConfProvCb;
142             RegisterWifiRsrcEventCallBack(ESWiFiConfRsrcCallback);
143         }
144         else
145         {
146             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "WiFiConfProvCb NULL");
147             return ES_ERROR;
148         }
149     }
150     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
151     {
152         if(callbacks.DevConfProvCb != NULL)
153         {
154             gESProvisioningCb.DevConfProvCb = callbacks.DevConfProvCb;
155             RegisterDevConfRsrcEventCallBack(ESDevConfRsrcallback);
156         }
157         else
158         {
159             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "DevConfProvCb NULL");
160             return ES_ERROR;
161         }
162     }
163     if((resourceMask & ES_COAPCLOUDCONF_RESOURCE) == ES_COAPCLOUDCONF_RESOURCE)
164     {
165         if(callbacks.CoapCloudConfProvCb != NULL)
166         {
167             gESProvisioningCb.CoapCloudConfProvCb = callbacks.CoapCloudConfProvCb;
168             RegisterCloudRsrcEventCallBack(ESCoapCloudConfRsrcCallback);
169         }
170         else
171         {
172             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "CoapCloudConfProvCb NULL");
173             return ES_ERROR;
174         }
175     }
176
177     // TODO: if EasySetupProvCb is NULL, we should return an error at this moment.
178     if(callbacks.ConnectRequestCb != NULL)
179     {
180         gESProvisioningCb.ConnectRequestCb = callbacks.ConnectRequestCb;
181         RegisterConnectRequestEventCallBack(ESConnectRequestCallback);
182     }
183
184     if(CreateEasySetupResources(gIsSecured, resourceMask) != OC_STACK_OK)
185     {
186         UnRegisterResourceEventCallBack();
187
188         if (DeleteEasySetupResources() != OC_STACK_OK)
189         {
190             OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
191         }
192
193         return ES_ERROR;
194     }
195
196     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESInitEnrollee OUT");
197     return ES_OK;
198 }
199
200 ESResult ESSetDeviceProperty(ESDeviceProperty *deviceProperty)
201 {
202     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty IN");
203
204     if(SetDeviceProperty(deviceProperty) != OC_STACK_OK)
205     {
206         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetDeviceProperty Error");
207         return ES_ERROR;
208     }
209
210     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetDeviceProperty OUT");
211     return ES_OK;
212 }
213
214 #if defined(__GNUC__)
215 #pragma GCC diagnostic push
216 /*
217  * Prevent reporting esState < ES_STATE_INT and esErrCode < ES_ERRCODE_NO_ERROR as type-limits
218  * The type used for an enum is decided by the compiler the only way to know that the value
219  * passed in by the user is within the limits is to check.
220  * Note this warning is seems to be limited to older compilers.
221  */
222 #pragma GCC diagnostic ignored "-Wtype-limits"
223 #endif
224
225 ESResult ESSetState(ESEnrolleeState esState)
226 {
227     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState IN");
228
229     if(esState < ES_STATE_INIT || esState >= ES_STATE_EOF)
230     {
231         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESEnrolleeState : %d", esState);
232         return ES_ERROR;
233     }
234
235     if(SetEnrolleeState(esState) != OC_STACK_OK)
236     {
237         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetState ES_ERROR");
238         return ES_ERROR;
239     }
240
241     OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESState succesfully : %d", esState);
242     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetState OUT");
243     return ES_OK;
244 }
245
246 ESResult ESSetErrorCode(ESErrorCode esErrCode)
247 {
248     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode IN");
249
250     if(esErrCode < ES_ERRCODE_NO_ERROR || esErrCode > ES_ERRCODE_UNKNOWN)
251     {
252         OIC_LOG_V(ERROR, ES_ENROLLEE_TAG, "Invalid ESSetErrorCode : %d", esErrCode);
253             return ES_ERROR;
254     }
255
256     if(SetEnrolleeErrCode(esErrCode) != OC_STACK_OK)
257     {
258         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "ESSetErrorCode ES_ERROR");
259         return ES_ERROR;
260     }
261
262     OIC_LOG_V(INFO, ES_ENROLLEE_TAG, "Set ESErrorCode succesfully : %d", esErrCode);
263     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetErrorCode OUT");
264     return ES_OK;
265 }
266
267 #if defined(__GNUC__)
268 #pragma GCC diagnostic pop
269 #endif
270
271 ESResult ESTerminateEnrollee()
272 {
273     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESTerminateEnrollee IN");
274
275     UnRegisterResourceEventCallBack();
276
277     //Delete Prov resource
278     if (DeleteEasySetupResources() != OC_STACK_OK)
279     {
280         OIC_LOG(ERROR, ES_ENROLLEE_TAG, "Deleting prov resource error!!");
281         return ES_ERROR;
282     }
283
284     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESTerminateEnrollee success");
285     return ES_OK;
286 }
287
288 ESResult ESSetCallbackForUserdata(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
289 {
290     OIC_LOG(INFO, ES_ENROLLEE_TAG, "ESSetCallbackForUserdata IN");
291
292     if(!readCb && !writeCb)
293     {
294         OIC_LOG(INFO, ES_ENROLLEE_TAG, "Both of callbacks for user data are null");
295         return ES_ERROR;
296     }
297
298     SetCallbackForUserData(readCb, writeCb);
299     return ES_OK;
300 }