Merge remote-tracking branch 'origin/master' into notification-service
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / resourcehandler.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 #include "resourcehandler.h"
22
23 #include "ocpayload.h"
24 #include "oic_string.h"
25 #include "oic_malloc.h"
26
27 /**
28  * @var ES_RH_TAG
29  * @brief Logging tag for module name.
30  */
31 #define ES_RH_TAG "ES_RH"
32 //-----------------------------------------------------------------------------
33 // Private variables
34 //-----------------------------------------------------------------------------
35
36 /**
37  * @var gProvResource
38  * @brief Structure for holding the Provisioning status and target information required to
39  * connect to the target network
40  */
41 static ProvResource gProvResource;
42 static WiFiResource gWiFiResource;
43 static CloudResource gCloudResource;
44 static DevConfResource gDevConfResource;
45
46 //-----------------------------------------------------------------------------
47 // Private internal function prototypes
48 //-----------------------------------------------------------------------------
49 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
50         void *callback);
51 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
52 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
53 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
54 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
55 void updateWiFiResource(OCRepPayload* input);
56 void updateCloudResource(OCRepPayload* input);
57 void updateDevConfResource(OCRepPayload* input);
58 const char *getResult(OCStackResult result);
59
60 ESWiFiCB gWifiRsrcEvtCb = NULL;
61 ESCloudCB gCloudRsrcEvtCb = NULL;
62 ESDevConfCB gDevConfRsrcEvtCb = NULL;
63
64 ESReadUserdataCb gReadUserdataCb = NULL;
65 ESWriteUserdataCb gWriteUserdataCb = NULL;
66
67 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
68 {
69     if(!readCb && !writeCb)
70     {
71         OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
72         return ES_ERROR;
73     }
74     gReadUserdataCb = readCb;
75     gWriteUserdataCb = writeCb;
76     return ES_OK;
77 }
78
79 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
80 {
81     gWifiRsrcEvtCb = cb;
82 }
83
84 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
85 {
86     gCloudRsrcEvtCb = cb;
87 }
88
89 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
90 {
91     gDevConfRsrcEvtCb = cb;
92 }
93
94 void UnRegisterResourceEventCallBack()
95 {
96     if (gWifiRsrcEvtCb)
97     {
98         gWifiRsrcEvtCb = NULL;
99     }
100     if (gCloudRsrcEvtCb)
101     {
102         gCloudRsrcEvtCb = NULL;
103     }
104     if (gDevConfRsrcEvtCb)
105     {
106         gDevConfRsrcEvtCb = NULL;
107     }
108 }
109
110 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
111 {
112     if (name != NULL && pass != NULL)
113     {
114         OICStrcpy(name, MAX_WEBLINKLEN, gWiFiResource.ssid);
115         OICStrcpy(pass, MAX_WEBLINKLEN, gWiFiResource.cred);
116     }
117 }
118
119 OCStackResult initProvResource(bool isSecured)
120 {
121     gProvResource.status = ES_STATE_INIT;
122     gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
123     OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
124
125     OCStackResult res = OC_STACK_ERROR;
126     if (isSecured)
127     {
128         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
129         OC_RSRVD_INTERFACE_DEFAULT,
130         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
131         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
132     }else
133     {
134         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
135         OC_RSRVD_INTERFACE_DEFAULT,
136         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
137         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
138     }
139     if(res)
140     {
141         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
142         return res;
143     }
144
145     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
146     if(res)
147     {
148         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
149         return res;
150     }
151     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
152     if(res)
153     {
154         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
155         return res;
156     }
157
158     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
159     return res;
160 }
161
162 OCStackResult initWiFiResource(bool isSecured)
163 {
164     OCStackResult res = OC_STACK_ERROR;
165
166     gWiFiResource.supportedFreq = WIFI_BOTH;
167     gWiFiResource.supportedMode[0] = WIFI_11A;
168     gWiFiResource.supportedMode[1] = WIFI_11B;
169     gWiFiResource.supportedMode[2] = WIFI_11G;
170     gWiFiResource.supportedMode[3] = WIFI_11N;
171     gWiFiResource.numMode = 4;
172     gWiFiResource.authType = NONE_AUTH;
173     gWiFiResource.encType = NONE_ENC;
174     OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
175     OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
176
177     if (isSecured)
178     {
179         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
180         OC_RSRVD_INTERFACE_DEFAULT,
181         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
182         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
183     }else
184     {
185         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
186         OC_RSRVD_INTERFACE_DEFAULT,
187         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
188         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
189     }
190
191     OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
192     return res;
193
194 }
195
196 OCStackResult initCloudServerResource(bool isSecured)
197 {
198     OCStackResult res = OC_STACK_ERROR;
199
200     OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
201     OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
202     OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
203
204     if (isSecured)
205     {
206         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
207         OC_RSRVD_INTERFACE_DEFAULT,
208         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
209         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
210     }else
211     {
212         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
213         OC_RSRVD_INTERFACE_DEFAULT,
214         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
215         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
216     }
217
218     OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
219     return res;
220
221 }
222
223 OCStackResult initDevConfResource(bool isSecured)
224 {
225     OCStackResult res = OC_STACK_ERROR;
226
227     OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
228     OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
229     OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
230     OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
231     OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
232
233     if (isSecured)
234     {
235         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
236         OC_RSRVD_INTERFACE_DEFAULT,
237         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
238         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
239     }else
240     {
241         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
242         OC_RSRVD_INTERFACE_DEFAULT,
243         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
244         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
245     }
246
247     OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
248     return res;
249
250 }
251
252 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
253 {
254     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
255
256     if(ehRequest->query)
257     {
258         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
259         {
260         // When Provisioning resource has a POST with BatchInterface
261             updateCloudResource(input);
262             updateWiFiResource(input);
263             updateDevConfResource(input);
264         }
265     }
266 }
267
268 void updateWiFiResource(OCRepPayload* input)
269 {
270     ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
271
272     if(wiFiData == NULL)
273     {
274         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
275         return ;
276     }
277
278     memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
279     memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
280     wiFiData->authtype = NONE_AUTH;
281     wiFiData->enctype = NONE_AUTH;
282     wiFiData->userdata = NULL;
283
284     char* ssid = NULL;
285     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
286     {
287         OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
288         OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
289         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
290     }
291
292     char* cred = NULL;
293     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
294     {
295         OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
296         OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
297         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
298     }
299
300     int64_t authType = -1;
301     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
302     {
303         gWiFiResource.authType = authType;
304         wiFiData->authtype = gWiFiResource.authType;
305         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
306     }
307
308     int64_t encType = -1;
309     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
310     {
311         gWiFiResource.encType = encType;
312         wiFiData->enctype = gWiFiResource.encType;
313         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
314     }
315
316     if(gReadUserdataCb)
317     {
318         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
319     }
320
321     if(ssid || cred || authType!= -1 || encType != -1)
322     {
323         OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
324
325         // TODO : Need to check appropriateness of gWiFiData
326         if(gWifiRsrcEvtCb != NULL)
327         {
328             gWifiRsrcEvtCb(ES_OK, wiFiData);
329         }
330         else
331         {
332             OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
333         }
334     }
335
336     OICFree(wiFiData);
337 }
338
339 void updateCloudResource(OCRepPayload* input)
340 {
341     ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
342
343     if(cloudData == NULL)
344     {
345         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
346         return;
347     }
348
349     memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
350     memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
351     memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
352     cloudData->userdata = NULL;
353
354     char *authCode = NULL;
355     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
356     {
357         OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
358         OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
359         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
360     }
361
362     char *authProvider = NULL;
363     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
364     {
365         OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
366         OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
367         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
368     }
369
370     char *ciServer = NULL;
371     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
372     {
373         OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
374         OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
375         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
376     }
377
378     if(gReadUserdataCb)
379     {
380         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
381     }
382
383     if(authCode || authProvider || ciServer)
384     {
385         OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
386
387         // TODO : Need to check appropriateness of gCloudData
388         if(gCloudRsrcEvtCb != NULL)
389         {
390             gCloudRsrcEvtCb(ES_OK, cloudData);
391         }
392         else
393         {
394             OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
395         }
396     }
397
398     OICFree(cloudData);
399 }
400
401 void updateDevConfResource(OCRepPayload* input)
402 {
403     ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
404
405     if(devConfData == NULL)
406     {
407         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
408         return;
409     }
410     memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
411     memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
412     devConfData->userdata = NULL;
413
414     char *location = NULL;
415     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
416     {
417         OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
418         OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
419         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
420     }
421
422     char *country = NULL;
423     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
424     {
425         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
426         OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
427         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
428     }
429
430     char *language = NULL;
431     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
432     {
433         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
434         OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
435         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
436     }
437
438     if(gReadUserdataCb)
439     {
440         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
441     }
442
443     if(country || language)
444     {
445         OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
446
447         // TODO : Need to check appropriateness of gDevConfData
448         if(gDevConfRsrcEvtCb != NULL)
449         {
450             gDevConfRsrcEvtCb(ES_OK, devConfData);
451         }
452         else
453         {
454             OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
455         }
456     }
457
458     OICFree(devConfData);
459 }
460
461 OCRepPayload* constructResponseOfWiFi()
462 {
463     OCRepPayload* payload = OCRepPayloadCreate();
464     if (!payload)
465     {
466         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
467         return NULL;
468     }
469
470     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
471     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
472
473     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
474     int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
475     for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
476     {
477         modes_64[i] = gWiFiResource.supportedMode[i];
478     }
479     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
480
481     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
482     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
483     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
484     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
485     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
486
487     if(gWriteUserdataCb)
488     {
489         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
490     }
491
492     return payload;
493 }
494
495 OCRepPayload* constructResponseOfCloud()
496 {
497     OCRepPayload* payload = OCRepPayloadCreate();
498     if (!payload)
499     {
500         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
501         return NULL;
502     }
503
504     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
505     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
506     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
507     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
508     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
509
510     if(gWriteUserdataCb)
511     {
512         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
513     }
514
515     return payload;
516 }
517
518 OCRepPayload* constructResponseOfDevConf()
519 {
520     OCRepPayload* payload = OCRepPayloadCreate();
521     if (!payload)
522     {
523         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
524         return NULL;
525     }
526
527     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
528     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
529     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
530     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
531     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
532     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
533     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
534
535     if(gWriteUserdataCb)
536     {
537         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
538     }
539
540     return payload;
541 }
542
543 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
544 {
545     OCRepPayload* payload = OCRepPayloadCreate();
546     if (!payload)
547     {
548         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
549         return NULL;
550     }
551
552     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
553     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
554     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
555     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
556     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
557
558     if(gWriteUserdataCb)
559     {
560         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
561     }
562
563     if(ehRequest->query)
564     {
565         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
566         {// When Provisioning resource has a GET with BatchInterface
567             OCRepPayload* head = payload;
568             OCRepPayload* nextPayload = NULL;
569
570             nextPayload = constructResponseOfWiFi();
571             if(nextPayload != NULL)
572             {
573                 payload->next = nextPayload;
574                 payload = payload->next;
575             }
576
577             nextPayload = constructResponseOfCloud();
578             if(nextPayload != NULL)
579             {
580                 payload->next = nextPayload;
581                 payload = payload->next;
582             }
583
584             nextPayload = constructResponseOfDevConf();
585             if(nextPayload != NULL)
586             {
587                 payload->next = nextPayload;
588                 payload = payload->next;
589             }
590
591             payload = head;
592         }
593     }
594
595     return payload;
596 }
597
598
599 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
600 {
601     OCStackResult res = OC_STACK_ERROR;
602     bool maskFlag = false;
603
604     res = initProvResource(isSecured);
605     if(res != OC_STACK_OK)
606     {
607         // TODO: destroy logic will be added
608         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
609
610         return res;
611     }
612
613     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
614     {
615         maskFlag = true;
616         res = initWiFiResource(isSecured);
617         if(res != OC_STACK_OK)
618         {
619             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
620             return res;
621         }
622
623         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
624         if(res != OC_STACK_OK)
625         {
626             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
627             return res;
628         }
629
630     }
631
632     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
633     {
634         maskFlag = true;
635         res = initCloudServerResource(isSecured);
636         if(res != OC_STACK_OK)
637         {
638             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
639             return res;
640         }
641
642         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
643         if(res != OC_STACK_OK)
644         {
645             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
646             return res;
647         }
648     }
649
650     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
651     {
652         maskFlag = true;
653         res = initDevConfResource(isSecured);
654         if(res != OC_STACK_OK)
655         {
656             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
657             return res;
658         }
659
660         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
661         if(res != OC_STACK_OK)
662         {
663             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
664             return res;
665         }
666     }
667
668
669     if(maskFlag == false)
670     {
671         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
672         return OC_STACK_ERROR;
673
674     }
675
676     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
677
678     return res;
679 }
680
681 OCStackResult DeleteProvisioningResource()
682 {
683     OCStackResult res = OCDeleteResource(gProvResource.handle);
684     if (res != OC_STACK_OK)
685     {
686         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
687     }
688
689     return res;
690 }
691
692 OCStackResult DeleteEasySetupResources()
693 {
694     OCStackResult res = OC_STACK_ERROR;
695     if (gWiFiResource.handle != NULL)
696     {
697         res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
698         if(res != OC_STACK_OK)
699         {
700             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
701         }
702     }
703     if (gCloudResource.handle != NULL)
704     {
705         res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
706         if(res != OC_STACK_OK)
707         {
708             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
709         }
710     }
711     if (gDevConfResource.handle != NULL)
712     {
713         res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
714         if(res != OC_STACK_OK)
715         {
716             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
717         }
718     }
719
720     if (gWiFiResource.handle != NULL)
721     {
722         res = OCDeleteResource(gWiFiResource.handle);
723         if (res != OC_STACK_OK)
724         {
725             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
726         }
727     }
728
729     if(gCloudResource.handle != NULL)
730     {
731         res = OCDeleteResource(gCloudResource.handle);
732         if (res != OC_STACK_OK)
733         {
734             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
735         }
736     }
737
738     if(gDevConfResource.handle != NULL)
739     {
740         res = OCDeleteResource(gDevConfResource.handle);
741         if (res != OC_STACK_OK)
742         {
743             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
744         }
745     }
746
747     if(gProvResource.handle != NULL)
748     {
749         res = OCDeleteResource(gProvResource.handle);
750         if (res != OC_STACK_OK)
751         {
752             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
753         }
754     }
755
756     return res;
757 }
758
759 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
760 {
761     OCEntityHandlerResult ehResult = OC_EH_ERROR;
762     if (!ehRequest)
763     {
764         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
765         return ehResult;
766     }
767     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
768     {
769         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
770         return ehResult;
771     }
772
773     OCRepPayload *getResp = NULL;
774
775     if(ehRequest->resource == gProvResource.handle)
776     {
777         getResp = constructResponseOfProv(ehRequest);
778     }
779     else if(ehRequest->resource == gWiFiResource.handle)
780     {
781         getResp = constructResponseOfWiFi();
782     }
783     else if(ehRequest->resource == gCloudResource.handle)
784     {
785         getResp = constructResponseOfCloud();
786     }
787     else if(ehRequest->resource == gDevConfResource.handle)
788     {
789         getResp = constructResponseOfDevConf();
790     }
791
792     if (!getResp)
793     {
794         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
795         return OC_EH_ERROR;
796     }
797
798     *payload = getResp;
799     ehResult = OC_EH_OK;
800
801     return ehResult;
802 }
803
804 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
805 {
806     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
807     OCEntityHandlerResult ehResult = OC_EH_ERROR;
808     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
809     {
810         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
811         return ehResult;
812     }
813
814     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
815     if (!input)
816     {
817         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
818         return ehResult;
819     }
820
821     if(ehRequest->resource == gProvResource.handle)
822     {
823         updateProvResource(ehRequest, input);
824     }
825     else if(ehRequest->resource == gWiFiResource.handle)
826     {
827         updateWiFiResource(input);
828     }
829     else if(ehRequest->resource == gCloudResource.handle)
830     {
831         updateCloudResource(input);
832     }
833     else if(ehRequest->resource == gDevConfResource.handle)
834     {
835         updateDevConfResource(input);
836     }
837
838     OCRepPayload *getResp = NULL;
839     if(ehRequest->resource == gProvResource.handle)
840     {
841         getResp = constructResponseOfProv(ehRequest);
842     }
843     else if(ehRequest->resource == gWiFiResource.handle)
844     {
845         getResp = constructResponseOfWiFi();
846     }
847     else if(ehRequest->resource == gCloudResource.handle)
848     {
849         getResp = constructResponseOfCloud();
850     }
851     else if(ehRequest->resource == gDevConfResource.handle)
852     {
853         getResp = constructResponseOfDevConf();
854     }
855
856     if (!getResp)
857     {
858         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
859         return OC_EH_ERROR;
860     }
861
862     *payload = getResp;
863     ehResult = OC_EH_OK;
864
865     return ehResult;
866 }
867
868 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
869         OCRepPayload** payload)
870 {
871     (void) ehRequest;
872     (void) payload;
873     OCEntityHandlerResult ehResult = OC_EH_ERROR;
874
875     return ehResult;
876 }
877 /**
878  * This is the entity handler for the registered resource.
879  * This is invoked by OCStack whenever it recevies a request for this resource.
880  */
881 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
882         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
883 {
884     (void) callback;
885     OCEntityHandlerResult ehRet = OC_EH_OK;
886     OCEntityHandlerResponse response =
887     { 0, 0, OC_EH_ERROR, 0, 0,
888     { },
889     { 0 }, false };
890     OCRepPayload* payload = NULL;
891
892     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
893     {
894         if (OC_REST_GET == entityHandlerRequest->method)
895         {
896             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
897             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
898         }
899         else if (OC_REST_PUT == entityHandlerRequest->method)
900         {
901             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
902
903             //PUT request will be handled in the internal implementation
904             if (gProvResource.handle != NULL)
905             {
906                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
907             }
908             else
909             {
910                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
911                 ehRet = OC_EH_ERROR;
912             }
913         }
914         else if (OC_REST_POST == entityHandlerRequest->method)
915         {
916             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
917             if (gProvResource.handle != NULL)
918             {
919                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
920             }
921             else
922             {
923                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
924                 ehRet = OC_EH_ERROR;
925             }
926         }
927
928         // Format the response.  Note this requires some info about the request
929         response.requestHandle = entityHandlerRequest->requestHandle;
930         response.resourceHandle = entityHandlerRequest->resource;
931         response.ehResult = ehRet;
932         //response uses OCPaylod while all get,put methodes use OCRepPayload
933         response.payload = (OCPayload*) (payload);
934         response.numSendVendorSpecificHeaderOptions = 0;
935         memset(response.sendVendorSpecificHeaderOptions, 0,
936                 sizeof(response.sendVendorSpecificHeaderOptions));
937         memset(response.resourceUri, 0, sizeof(response.resourceUri));
938         // Indicate that response is NOT in a persistent buffer
939         response.persistentBufferFlag = 0;
940
941         // Send the response
942         if (OCDoResponse(&response) != OC_STACK_OK)
943         {
944             OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
945             ehRet = OC_EH_ERROR;
946         }
947     }
948
949     return ehRet;
950 }
951
952 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
953 {
954     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
955
956     gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
957     OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
958
959     int modeIdx = 0;
960     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
961     {
962         gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
963         OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
964         modeIdx ++;
965     }
966     gWiFiResource.numMode = modeIdx;
967
968     OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
969     OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
970
971     OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
972                                                             (deviceProperty->DevConf).modelNumber);
973     OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
974
975     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
976     return OC_STACK_OK;
977 }
978
979 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
980 {
981     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
982
983     gProvResource.status = esState;
984     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
985
986     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
987     return OC_STACK_OK;
988 }
989
990 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
991 {
992     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
993
994     gProvResource.lastErrCode = esErrCode;
995     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
996
997     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
998     return OC_STACK_OK;
999 }
1000
1001 const char *getResult(OCStackResult result)
1002 {
1003     switch (result)
1004     {
1005         case OC_STACK_OK:
1006             return "OC_STACK_OK";
1007         case OC_STACK_INVALID_URI:
1008             return "OC_STACK_INVALID_URI";
1009         case OC_STACK_INVALID_QUERY:
1010             return "OC_STACK_INVALID_QUERY";
1011         case OC_STACK_INVALID_IP:
1012             return "OC_STACK_INVALID_IP";
1013         case OC_STACK_INVALID_PORT:
1014             return "OC_STACK_INVALID_PORT";
1015         case OC_STACK_INVALID_CALLBACK:
1016             return "OC_STACK_INVALID_CALLBACK";
1017         case OC_STACK_INVALID_METHOD:
1018             return "OC_STACK_INVALID_METHOD";
1019         case OC_STACK_NO_MEMORY:
1020             return "OC_STACK_NO_MEMORY";
1021         case OC_STACK_COMM_ERROR:
1022             return "OC_STACK_COMM_ERROR";
1023         case OC_STACK_INVALID_PARAM:
1024             return "OC_STACK_INVALID_PARAM";
1025         case OC_STACK_NOTIMPL:
1026             return "OC_STACK_NOTIMPL";
1027         case OC_STACK_NO_RESOURCE:
1028             return "OC_STACK_NO_RESOURCE";
1029         case OC_STACK_RESOURCE_ERROR:
1030             return "OC_STACK_RESOURCE_ERROR";
1031         case OC_STACK_SLOW_RESOURCE:
1032             return "OC_STACK_SLOW_RESOURCE";
1033         case OC_STACK_NO_OBSERVERS:
1034             return "OC_STACK_NO_OBSERVERS";
1035         case OC_STACK_ERROR:
1036             return "OC_STACK_ERROR";
1037         default:
1038             return "UNKNOWN";
1039     }
1040 }