Update a response representation according to requested interface in easy setup
[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(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
56 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
57 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, 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 bool CompareResourceInterface(char *from, char *iface)
68 {
69     char *str = OICStrdup(from);
70     char *ptr = strtok(str, ";");
71
72     if(ptr == NULL)
73     {
74         return false;
75     }
76
77     do
78     {
79         if(strstr(ptr, ".if."))
80         {
81             char *if_ptr = NULL;
82             if_ptr = strtok(ptr, "=");
83             if_ptr = strtok(NULL, "=");
84
85             if(!strcmp(if_ptr, iface))
86             {
87                 return true;
88             }
89         }
90
91     } while ((ptr = strtok(NULL, ";")));
92
93     return false;
94 }
95
96 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
97 {
98     if(!readCb && !writeCb)
99     {
100         OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
101         return ES_ERROR;
102     }
103     gReadUserdataCb = readCb;
104     gWriteUserdataCb = writeCb;
105     return ES_OK;
106 }
107
108 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
109 {
110     gWifiRsrcEvtCb = cb;
111 }
112
113 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
114 {
115     gCloudRsrcEvtCb = cb;
116 }
117
118 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
119 {
120     gDevConfRsrcEvtCb = cb;
121 }
122
123 void UnRegisterResourceEventCallBack()
124 {
125     if (gWifiRsrcEvtCb)
126     {
127         gWifiRsrcEvtCb = NULL;
128     }
129     if (gCloudRsrcEvtCb)
130     {
131         gCloudRsrcEvtCb = NULL;
132     }
133     if (gDevConfRsrcEvtCb)
134     {
135         gDevConfRsrcEvtCb = NULL;
136     }
137 }
138
139 OCStackResult initProvResource(bool isSecured)
140 {
141     gProvResource.status = ES_STATE_INIT;
142     gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
143     OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
144
145     OCStackResult res = OC_STACK_ERROR;
146     if (isSecured)
147     {
148         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
149         OC_RSRVD_INTERFACE_DEFAULT,
150         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
151         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
152     }else
153     {
154         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
155         OC_RSRVD_INTERFACE_DEFAULT,
156         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
157         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
158     }
159     if(res)
160     {
161         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
162         return res;
163     }
164
165     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
166     if(res)
167     {
168         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
169         return res;
170     }
171     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
172     if(res)
173     {
174         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
175         return res;
176     }
177
178     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
179     return res;
180 }
181
182 OCStackResult initWiFiResource(bool isSecured)
183 {
184     OCStackResult res = OC_STACK_ERROR;
185
186     gWiFiResource.supportedFreq = WIFI_BOTH;
187     gWiFiResource.supportedMode[0] = WIFI_11A;
188     gWiFiResource.supportedMode[1] = WIFI_11B;
189     gWiFiResource.supportedMode[2] = WIFI_11G;
190     gWiFiResource.supportedMode[3] = WIFI_11N;
191     gWiFiResource.numMode = 4;
192     gWiFiResource.authType = NONE_AUTH;
193     gWiFiResource.encType = NONE_ENC;
194     OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
195     OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
196
197     if (isSecured)
198     {
199         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
200         OC_RSRVD_INTERFACE_DEFAULT,
201         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
202         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
203     }else
204     {
205         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
206         OC_RSRVD_INTERFACE_DEFAULT,
207         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
208         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
209     }
210
211     OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
212     return res;
213
214 }
215
216 OCStackResult initCloudServerResource(bool isSecured)
217 {
218     OCStackResult res = OC_STACK_ERROR;
219
220     OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
221     OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
222     OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
223
224     if (isSecured)
225     {
226         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
227         OC_RSRVD_INTERFACE_DEFAULT,
228         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
229         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
230     }else
231     {
232         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
233         OC_RSRVD_INTERFACE_DEFAULT,
234         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
235         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
236     }
237
238     OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
239     return res;
240
241 }
242
243 OCStackResult initDevConfResource(bool isSecured)
244 {
245     OCStackResult res = OC_STACK_ERROR;
246
247     OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
248     OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
249     OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
250     OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
251     OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
252
253     if (isSecured)
254     {
255         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
256         OC_RSRVD_INTERFACE_DEFAULT,
257         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
258         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
259     }else
260     {
261         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
262         OC_RSRVD_INTERFACE_DEFAULT,
263         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
264         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
265     }
266
267     OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
268     return res;
269
270 }
271
272 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
273 {
274     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
275
276     if(ehRequest->query)
277     {
278         if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
279         {
280             // When Provisioning resource has a POST with BatchInterface
281             updateCloudResource(NULL, input);
282             updateWiFiResource(NULL, input);
283             updateDevConfResource(NULL, input);
284         }
285     }
286 }
287
288 void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
289 {
290     if(ehRequest &&
291         strcmp(ehRequest->query, "") &&
292         !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
293     {
294         // In case of link list, batch interface
295         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
296         return ;
297     }
298
299     ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
300
301     if(wiFiData == NULL)
302     {
303         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
304         return ;
305     }
306
307     memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
308     memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
309     wiFiData->authtype = NONE_AUTH;
310     wiFiData->enctype = NONE_AUTH;
311     wiFiData->userdata = NULL;
312
313     char* ssid = NULL;
314     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
315     {
316         OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
317         OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
318         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
319     }
320
321     char* cred = NULL;
322     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
323     {
324         OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
325         OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
326         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
327     }
328
329     int64_t authType = -1;
330     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
331     {
332         gWiFiResource.authType = authType;
333         wiFiData->authtype = gWiFiResource.authType;
334         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
335     }
336
337     int64_t encType = -1;
338     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
339     {
340         gWiFiResource.encType = encType;
341         wiFiData->enctype = gWiFiResource.encType;
342         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
343     }
344
345     if(gReadUserdataCb)
346     {
347         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
348     }
349
350     if(ssid || cred || authType!= -1 || encType != -1)
351     {
352         OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
353
354         // TODO : Need to check appropriateness of gWiFiData
355         if(gWifiRsrcEvtCb != NULL)
356         {
357             gWifiRsrcEvtCb(ES_OK, wiFiData);
358         }
359         else
360         {
361             OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
362         }
363     }
364
365     OICFree(wiFiData);
366 }
367
368 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
369 {
370     if(ehRequest &&
371         strcmp(ehRequest->query, "") &&
372         !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
373     {
374         // In case of link list, batch interface
375         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
376         return ;
377     }
378
379     ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
380
381     if(cloudData == NULL)
382     {
383         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
384         return;
385     }
386
387     memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
388     memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
389     memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
390     cloudData->userdata = NULL;
391
392     char *authCode = NULL;
393     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
394     {
395         OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
396         OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
397         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
398     }
399
400     char *authProvider = NULL;
401     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
402     {
403         OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
404         OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
405         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
406     }
407
408     char *ciServer = NULL;
409     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
410     {
411         OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
412         OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
413         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
414     }
415
416     if(gReadUserdataCb)
417     {
418         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
419     }
420
421     if(authCode || authProvider || ciServer)
422     {
423         OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
424
425         // TODO : Need to check appropriateness of gCloudData
426         if(gCloudRsrcEvtCb != NULL)
427         {
428             gCloudRsrcEvtCb(ES_OK, cloudData);
429         }
430         else
431         {
432             OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
433         }
434     }
435
436     OICFree(cloudData);
437 }
438
439 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
440 {
441     if(ehRequest &&
442         strcmp(ehRequest->query, "") &&
443         !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
444     {
445         // In case of link list, batch interface
446         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
447         return ;
448     }
449
450     ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
451
452     if(devConfData == NULL)
453     {
454         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
455         return;
456     }
457     memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
458     memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
459     devConfData->userdata = NULL;
460
461     char *location = NULL;
462     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
463     {
464         OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
465         OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
466         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
467     }
468
469     char *country = NULL;
470     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
471     {
472         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
473         OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
474         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
475     }
476
477     char *language = NULL;
478     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
479     {
480         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
481         OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
482         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
483     }
484
485     if(gReadUserdataCb)
486     {
487         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
488     }
489
490     if(country || language)
491     {
492         OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
493
494         // TODO : Need to check appropriateness of gDevConfData
495         if(gDevConfRsrcEvtCb != NULL)
496         {
497             gDevConfRsrcEvtCb(ES_OK, devConfData);
498         }
499         else
500         {
501             OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
502         }
503     }
504
505     OICFree(devConfData);
506 }
507
508 OCRepPayload* constructResponseOfWiFi(OCEntityHandlerRequest *ehRequest)
509 {
510     if(ehRequest &&
511         strcmp(ehRequest->query, "") &&
512         !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
513     {
514         // In case of link list, batch interface
515         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
516         return NULL;
517     }
518
519     OCRepPayload* payload = OCRepPayloadCreate();
520     if (!payload)
521     {
522         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
523         return NULL;
524     }
525
526     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
527     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
528     OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
529     OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
530
531     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
532     int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
533     for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
534     {
535         modes_64[i] = gWiFiResource.supportedMode[i];
536     }
537     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
538
539     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
540     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
541     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
542     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
543     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
544
545     if(gWriteUserdataCb)
546     {
547         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
548     }
549
550     return payload;
551 }
552
553 OCRepPayload* constructResponseOfCloud(OCEntityHandlerRequest *ehRequest)
554 {
555     if(ehRequest &&
556         strcmp(ehRequest->query, "") &&
557         !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
558     {
559         // In case of link list, batch interface
560         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
561         return NULL;
562     }
563
564     OCRepPayload* payload = OCRepPayloadCreate();
565     if (!payload)
566     {
567         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
568         return NULL;
569     }
570
571     OIC_LOG(INFO, ES_RH_TAG, "constructResponse cloudserver res");
572     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
573     OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
574     OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
575
576     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
577     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
578     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
579
580     if(gWriteUserdataCb)
581     {
582         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
583     }
584
585     return payload;
586 }
587
588 OCRepPayload* constructResponseOfDevConf(OCEntityHandlerRequest *ehRequest)
589 {
590     if(ehRequest &&
591         strcmp(ehRequest->query, "") &&
592         !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
593     {
594         // In case of link list, batch interface
595         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
596         return NULL;
597     }
598
599     OCRepPayload* payload = OCRepPayloadCreate();
600     if (!payload)
601     {
602         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
603         return NULL;
604     }
605
606     OIC_LOG(INFO, ES_RH_TAG, "constructResponse devconf res");
607     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
608     OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
609     OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
610
611     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
612     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
613     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
614     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
615     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
616
617     if(gWriteUserdataCb)
618     {
619         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
620     }
621
622     return payload;
623 }
624
625 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
626 {
627     OCRepPayload* payload = OCRepPayloadCreate();
628     if (!payload)
629     {
630         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
631         return NULL;
632     }
633
634     // Requested interface is Link list interface
635     //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
636     if(!ehRequest->query ||
637         (ehRequest->query && !strcmp(ehRequest->query, "")) ||
638         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
639         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
640     {
641         const OCRepPayload *arrayPayload[3] = {NULL};
642
643         int childResCnt = 0;
644
645         if(gWiFiResource.handle != NULL)
646         {
647             OCRepPayload *add = OCRepPayloadCreate();
648             if(!add)
649             {
650                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
651                 return NULL;
652             }
653
654             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
655             char **resourceType = NULL;
656             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
657             char **resourceInterface = NULL;
658             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
659
660             if(!resourceType || !resourceInterface)
661             {
662                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
663                 return NULL;
664             }
665
666             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFI);
667             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
668
669             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
670             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFI);
671             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
672                                             (const char **)resourceType, dimensions);
673             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
674                                             (const char **)resourceInterface, dimensions);
675
676             arrayPayload[childResCnt++] = add;
677         }
678
679         if(gDevConfResource.handle != NULL)
680         {
681             OCRepPayload *add = OCRepPayloadCreate();
682             if(!add)
683             {
684                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
685                 return NULL;
686             }
687
688             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
689             char **resourceType = NULL;
690             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
691             char **resourceInterface = NULL;
692             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
693
694             if(!resourceType || !resourceInterface)
695             {
696                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
697                 return NULL;
698             }
699
700             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
701             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
702
703             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
704             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
705             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
706                                             (const char **)resourceType, dimensions);
707             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
708                                             (const char **)resourceInterface, dimensions);
709
710             arrayPayload[childResCnt++] = add;
711         }
712
713         if(gCloudResource.handle != NULL)
714         {
715             OCRepPayload *add = OCRepPayloadCreate();
716             if(!add)
717             {
718                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
719                 return NULL;
720             }
721
722             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
723             char **resourceType = NULL;
724             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
725             char **resourceInterface = NULL;
726             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
727
728             if(!resourceType || !resourceInterface)
729             {
730                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
731                 return NULL;
732             }
733
734             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
735             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
736
737             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
738             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
739             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
740                                             (const char **)resourceType, dimensions);
741             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
742                                             (const char **)resourceInterface, dimensions);
743
744             arrayPayload[childResCnt++] = add;
745         }
746
747         size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
748
749         if(!ehRequest->query ||
750             (ehRequest->query && !strcmp(ehRequest->query, "")) ||
751             (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
752         {
753             OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
754             OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
755             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
756             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
757             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
758             OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_PROV);
759
760             OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
761             OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
762
763             OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
764         }
765         else    // link list interface
766         {
767             OCRepPayload* head = payload;
768             OCRepPayload* nextPayload = NULL;
769
770             childResCnt = 0;
771
772             for(int i = 0 ; i < childResCnt ; ++i)
773             {
774                 nextPayload = arrayPayload[i];
775                 if(nextPayload != NULL)
776                 {
777                     payload->next = nextPayload;
778                     payload = payload->next;
779                 }
780             }
781             if(head->next != NULL)
782             {
783                 payload = head->next;
784             }
785             else
786             {
787                 payload = head;
788             }
789         }
790     } else if (
791         ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
792
793     {
794         OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
795         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
796         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
797         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
798         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
799         OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_PROV);
800
801         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
802         OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
803     }
804
805     if(gWriteUserdataCb)
806     {
807         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
808     }
809
810     if(ehRequest->query)
811     {
812         if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
813         {// When Provisioning resource has a GET with BatchInterface
814             OCRepPayload* head = payload;
815             OCRepPayload* nextPayload = NULL;
816
817             nextPayload = constructResponseOfWiFi(NULL);
818             if(nextPayload != NULL)
819             {
820                 payload->next = nextPayload;
821                 payload = payload->next;
822             }
823
824             nextPayload = constructResponseOfCloud(NULL);
825             if(nextPayload != NULL)
826             {
827                 payload->next = nextPayload;
828                 payload = payload->next;
829             }
830
831             nextPayload = constructResponseOfDevConf(NULL);
832             if(nextPayload != NULL)
833             {
834                 payload->next = nextPayload;
835                 payload = payload->next;
836             }
837
838             payload = head;
839         }
840     }
841
842     return payload;
843 }
844
845
846 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
847 {
848     OCStackResult res = OC_STACK_ERROR;
849     bool maskFlag = false;
850
851     res = initProvResource(isSecured);
852     if(res != OC_STACK_OK)
853     {
854         // TODO: destroy logic will be added
855         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
856
857         return res;
858     }
859
860     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
861     {
862         maskFlag = true;
863         res = initWiFiResource(isSecured);
864         if(res != OC_STACK_OK)
865         {
866             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
867             return res;
868         }
869
870         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
871         if(res != OC_STACK_OK)
872         {
873             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
874             return res;
875         }
876
877     }
878
879     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
880     {
881         maskFlag = true;
882         res = initCloudServerResource(isSecured);
883         if(res != OC_STACK_OK)
884         {
885             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
886             return res;
887         }
888
889         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
890         if(res != OC_STACK_OK)
891         {
892             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
893             return res;
894         }
895     }
896
897     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
898     {
899         maskFlag = true;
900         res = initDevConfResource(isSecured);
901         if(res != OC_STACK_OK)
902         {
903             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
904             return res;
905         }
906
907         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
908         if(res != OC_STACK_OK)
909         {
910             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
911             return res;
912         }
913     }
914
915
916     if(maskFlag == false)
917     {
918         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
919         return OC_STACK_ERROR;
920
921     }
922
923     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
924
925     return res;
926 }
927
928 OCStackResult DeleteProvisioningResource()
929 {
930     OCStackResult res = OCDeleteResource(gProvResource.handle);
931     if (res != OC_STACK_OK)
932     {
933         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
934     }
935
936     return res;
937 }
938
939 OCStackResult DeleteEasySetupResources()
940 {
941     OCStackResult res = OC_STACK_ERROR;
942     if (gWiFiResource.handle != NULL)
943     {
944         res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
945         if(res != OC_STACK_OK)
946         {
947             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
948         }
949     }
950     if (gCloudResource.handle != NULL)
951     {
952         res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
953         if(res != OC_STACK_OK)
954         {
955             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
956         }
957     }
958     if (gDevConfResource.handle != NULL)
959     {
960         res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
961         if(res != OC_STACK_OK)
962         {
963             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
964         }
965     }
966
967     if (gWiFiResource.handle != NULL)
968     {
969         res = OCDeleteResource(gWiFiResource.handle);
970         if (res != OC_STACK_OK)
971         {
972             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
973         }
974     }
975
976     if(gCloudResource.handle != NULL)
977     {
978         res = OCDeleteResource(gCloudResource.handle);
979         if (res != OC_STACK_OK)
980         {
981             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
982         }
983     }
984
985     if(gDevConfResource.handle != NULL)
986     {
987         res = OCDeleteResource(gDevConfResource.handle);
988         if (res != OC_STACK_OK)
989         {
990             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
991         }
992     }
993
994     if(gProvResource.handle != NULL)
995     {
996         res = OCDeleteResource(gProvResource.handle);
997         if (res != OC_STACK_OK)
998         {
999             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1000         }
1001     }
1002
1003     return res;
1004 }
1005
1006 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1007 {
1008     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1009     if (!ehRequest)
1010     {
1011         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1012         return ehResult;
1013     }
1014     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1015     {
1016         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1017         return ehResult;
1018     }
1019
1020     OCRepPayload *getResp = NULL;
1021     *payload = NULL;
1022
1023     if(ehRequest->resource == gProvResource.handle)
1024     {
1025         if(ehRequest->query &&
1026             strcmp(ehRequest->query, "") &&
1027             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1028             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1029             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1030         {
1031             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1032             return OC_EH_BAD_REQ;
1033         }
1034         else
1035         {
1036             getResp = constructResponseOfProv(ehRequest);
1037         }
1038     }
1039     else if(ehRequest->resource == gWiFiResource.handle)
1040     {
1041         if(ehRequest->query &&
1042             strcmp(ehRequest->query, "") &&
1043             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1044         {
1045             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1046             return OC_EH_BAD_REQ;
1047         }
1048         else
1049         {
1050             getResp = constructResponseOfWiFi(ehRequest);
1051         }
1052     }
1053     else if(ehRequest->resource == gCloudResource.handle)
1054     {
1055         if(ehRequest->query &&
1056             strcmp(ehRequest->query, "") &&
1057             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1058         {
1059             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1060             return OC_EH_BAD_REQ;
1061         }
1062         else
1063         {
1064             getResp = constructResponseOfCloud(ehRequest);
1065         }
1066     }
1067     else if(ehRequest->resource == gDevConfResource.handle)
1068     {
1069         if(ehRequest->query &&
1070             strcmp(ehRequest->query, "") &&
1071             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1072         {
1073             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1074             return OC_EH_BAD_REQ;
1075         }
1076         else
1077         {
1078             getResp = constructResponseOfDevConf(ehRequest);
1079         }
1080     }
1081
1082     if (!getResp)
1083     {
1084         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1085         return OC_EH_ERROR;
1086     }
1087
1088     *payload = getResp;
1089     ehResult = OC_EH_OK;
1090
1091     return ehResult;
1092 }
1093
1094 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1095 {
1096     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
1097     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1098     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1099     {
1100         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1101         return ehResult;
1102     }
1103
1104     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1105     if (!input)
1106     {
1107         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1108         return ehResult;
1109     }
1110
1111     if(ehRequest->resource == gProvResource.handle)
1112     {
1113         if(ehRequest->query &&
1114             strcmp(ehRequest->query, "") &&
1115             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1116             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1117         {
1118             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1119             return OC_EH_BAD_REQ;
1120         }
1121         else
1122         {
1123             updateProvResource(ehRequest, input);
1124         }
1125     }
1126     else if(ehRequest->resource == gWiFiResource.handle)
1127     {
1128         if(ehRequest->query &&
1129             strcmp(ehRequest->query, "") &&
1130             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1131         {
1132             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1133             return OC_EH_BAD_REQ;
1134         }
1135         else
1136         {
1137             updateWiFiResource(ehRequest, input);
1138         }
1139     }
1140     else if(ehRequest->resource == gCloudResource.handle)
1141     {
1142         if(ehRequest->query &&
1143             strcmp(ehRequest->query, "") &&
1144             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1145         {
1146             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1147             return OC_EH_BAD_REQ;
1148         }
1149         else
1150         {
1151             updateCloudResource(ehRequest, input);
1152         }
1153     }
1154     else if(ehRequest->resource == gDevConfResource.handle)
1155     {
1156         if(ehRequest->query &&
1157             strcmp(ehRequest->query, "") &&
1158             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1159         {
1160             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1161             return OC_EH_BAD_REQ;
1162         }
1163         else
1164         {
1165             updateDevConfResource(ehRequest, input);
1166         }
1167     }
1168
1169     OCRepPayload *getResp = NULL;
1170     if(ehRequest->resource == gProvResource.handle)
1171     {
1172         getResp = constructResponseOfProv(ehRequest);
1173     }
1174     else if(ehRequest->resource == gWiFiResource.handle)
1175     {
1176         getResp = constructResponseOfWiFi(NULL);
1177     }
1178     else if(ehRequest->resource == gCloudResource.handle)
1179     {
1180         getResp = constructResponseOfCloud(NULL);
1181     }
1182     else if(ehRequest->resource == gDevConfResource.handle)
1183     {
1184         getResp = constructResponseOfDevConf(NULL);
1185     }
1186
1187     if (!getResp)
1188     {
1189         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1190         return OC_EH_ERROR;
1191     }
1192
1193     *payload = getResp;
1194     ehResult = OC_EH_OK;
1195
1196     return ehResult;
1197 }
1198
1199 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1200         OCRepPayload** payload)
1201 {
1202     (void) ehRequest;
1203     (void) payload;
1204     OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1205
1206     return ehResult;
1207 }
1208 /**
1209  * This is the entity handler for the registered resource.
1210  * This is invoked by OCStack whenever it recevies a request for this resource.
1211  */
1212 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1213         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1214 {
1215     (void) callback;
1216     OCEntityHandlerResult ehRet = OC_EH_OK;
1217     OCEntityHandlerResponse response =
1218     { 0, 0, OC_EH_ERROR, 0, 0,
1219     { },
1220     { 0 }, false };
1221     OCRepPayload* payload = NULL;
1222
1223     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1224     {
1225         if (OC_REST_GET == entityHandlerRequest->method)
1226         {
1227             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1228             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1229         }
1230         else if (OC_REST_PUT == entityHandlerRequest->method)
1231         {
1232             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1233
1234             //PUT request will be handled in the internal implementation
1235             if (gProvResource.handle != NULL)
1236             {
1237                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1238             }
1239             else
1240             {
1241                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1242                 ehRet = OC_EH_ERROR;
1243             }
1244         }
1245         else if (OC_REST_POST == entityHandlerRequest->method)
1246         {
1247             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1248             if (gProvResource.handle != NULL)
1249             {
1250                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1251             }
1252             else
1253             {
1254                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1255                 ehRet = OC_EH_ERROR;
1256             }
1257         }
1258
1259         // Format the response.  Note this requires some info about the request
1260         response.requestHandle = entityHandlerRequest->requestHandle;
1261         response.resourceHandle = entityHandlerRequest->resource;
1262         response.ehResult = ehRet;
1263         //response uses OCPaylod while all get,put methodes use OCRepPayload
1264         response.payload = (OCPayload*) (payload);
1265         response.numSendVendorSpecificHeaderOptions = 0;
1266         memset(response.sendVendorSpecificHeaderOptions, 0,
1267                 sizeof(response.sendVendorSpecificHeaderOptions));
1268         memset(response.resourceUri, 0, sizeof(response.resourceUri));
1269         // Indicate that response is NOT in a persistent buffer
1270         response.persistentBufferFlag = 0;
1271
1272         // Send the response
1273         if (OCDoResponse(&response) != OC_STACK_OK)
1274         {
1275             OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1276             ehRet = OC_EH_ERROR;
1277         }
1278     }
1279
1280     return ehRet;
1281 }
1282
1283 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1284 {
1285     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
1286
1287     gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
1288     OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
1289
1290     int modeIdx = 0;
1291     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1292     {
1293         gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1294         OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
1295         modeIdx ++;
1296     }
1297     gWiFiResource.numMode = modeIdx;
1298
1299     OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1300     OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
1301
1302     OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
1303                                                             (deviceProperty->DevConf).modelNumber);
1304     OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
1305
1306     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
1307     return OC_STACK_OK;
1308 }
1309
1310 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1311 {
1312     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
1313
1314     gProvResource.status = esState;
1315     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
1316
1317     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
1318     return OC_STACK_OK;
1319 }
1320
1321 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1322 {
1323     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
1324
1325     gProvResource.lastErrCode = esErrCode;
1326     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
1327
1328     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1329     return OC_STACK_OK;
1330 }
1331
1332 const char *getResult(OCStackResult result)
1333 {
1334     switch (result)
1335     {
1336         case OC_STACK_OK:
1337             return "OC_STACK_OK";
1338         case OC_STACK_INVALID_URI:
1339             return "OC_STACK_INVALID_URI";
1340         case OC_STACK_INVALID_QUERY:
1341             return "OC_STACK_INVALID_QUERY";
1342         case OC_STACK_INVALID_IP:
1343             return "OC_STACK_INVALID_IP";
1344         case OC_STACK_INVALID_PORT:
1345             return "OC_STACK_INVALID_PORT";
1346         case OC_STACK_INVALID_CALLBACK:
1347             return "OC_STACK_INVALID_CALLBACK";
1348         case OC_STACK_INVALID_METHOD:
1349             return "OC_STACK_INVALID_METHOD";
1350         case OC_STACK_NO_MEMORY:
1351             return "OC_STACK_NO_MEMORY";
1352         case OC_STACK_COMM_ERROR:
1353             return "OC_STACK_COMM_ERROR";
1354         case OC_STACK_INVALID_PARAM:
1355             return "OC_STACK_INVALID_PARAM";
1356         case OC_STACK_NOTIMPL:
1357             return "OC_STACK_NOTIMPL";
1358         case OC_STACK_NO_RESOURCE:
1359             return "OC_STACK_NO_RESOURCE";
1360         case OC_STACK_RESOURCE_ERROR:
1361             return "OC_STACK_RESOURCE_ERROR";
1362         case OC_STACK_SLOW_RESOURCE:
1363             return "OC_STACK_SLOW_RESOURCE";
1364         case OC_STACK_NO_OBSERVERS:
1365             return "OC_STACK_NO_OBSERVERS";
1366         case OC_STACK_ERROR:
1367             return "OC_STACK_ERROR";
1368         default:
1369             return "UNKNOWN";
1370     }
1371 }