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