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