Changing the length of access token
[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 #include "cautilinterface.h"
27
28 /**
29  * @var ES_RH_TAG
30  * @brief Logging tag for module name.
31  */
32 #define ES_RH_TAG "ES_RH"
33 //-----------------------------------------------------------------------------
34 // Private variables
35 //-----------------------------------------------------------------------------
36
37 /**
38  * @var g_ESEasySetupResource
39  * @brief Structure for holding the Provisioning status
40  */
41 EasySetupResource g_ESEasySetupResource;
42 WiFiConfResource g_ESWiFiConfResource;
43 CoapCloudConfResource g_ESCoapCloudConfResource;
44 DevConfResource g_ESDevConfResource;
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 updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
55 void updateWiFiConfResource(OCRepPayload* input);
56 void updateCoapCloudConfResource(OCRepPayload* input);
57 void updateDevConfResource(OCRepPayload* input);
58 const char *getResult(OCStackResult result);
59
60 ESConnectRequestCB gConnectRequestEvtCb = NULL;
61 ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
62 ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
63 ESDevConfCB gDevConfRsrcEvtCb = NULL;
64
65 ESReadUserdataCb gReadUserdataCb = NULL;
66 ESWriteUserdataCb gWriteUserdataCb = NULL;
67
68 bool CompareResourceInterface(char *from, char *iface)
69 {
70     char *str = OICStrdup(from);
71     char *ptr = strtok(str, ";");
72
73     if(ptr == NULL)
74     {
75         OICFree(str);
76         return false;
77     }
78
79     do
80     {
81         if(strstr(ptr, ".if."))
82         {
83             char *if_ptr = NULL;
84             if_ptr = strtok(ptr, "=");
85             if_ptr = strtok(NULL, "=");
86
87             if(!strcmp(if_ptr, iface))
88             {
89                 OICFree(str);
90                 return true;
91             }
92         }
93
94     } while ((ptr = strtok(NULL, ";")));
95
96     OICFree(str);
97     return false;
98 }
99
100 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
101 {
102     if(!readCb && !writeCb)
103     {
104         OIC_LOG(DEBUG, ES_RH_TAG, "Both of callbacks for user data are null");
105         return ES_ERROR;
106     }
107     gReadUserdataCb = readCb;
108     gWriteUserdataCb = writeCb;
109     return ES_OK;
110 }
111
112 void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
113 {
114     gWifiConfRsrcEvtCb = cb;
115 }
116
117 void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
118 {
119     gCoapCloudConfRsrcEvtCb = cb;
120 }
121
122 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
123 {
124     gDevConfRsrcEvtCb = cb;
125 }
126
127 void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
128 {
129     gConnectRequestEvtCb = cb;
130 }
131
132 void UnRegisterResourceEventCallBack()
133 {
134     if (gWifiConfRsrcEvtCb)
135     {
136         gWifiConfRsrcEvtCb = NULL;
137     }
138     if (gCoapCloudConfRsrcEvtCb)
139     {
140         gCoapCloudConfRsrcEvtCb = NULL;
141     }
142     if (gDevConfRsrcEvtCb)
143     {
144         gDevConfRsrcEvtCb = NULL;
145     }
146     if (gConnectRequestEvtCb)
147     {
148         gConnectRequestEvtCb = NULL;
149     }
150 }
151
152 OCStackResult initEasySetupResource(bool isSecured)
153 {
154     g_ESEasySetupResource.status = ES_STATE_INIT;
155     g_ESEasySetupResource.lastErrCode = ES_ERRCODE_NO_ERROR;
156     for( int i = 0 ; i < NUM_CONNECT_TYPE ; ++i )
157     {
158         g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
159     }
160     g_ESEasySetupResource.numRequest = 0;
161
162     OCStackResult res = OC_STACK_ERROR;
163     if (isSecured)
164     {
165         res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
166         OC_RSRVD_INTERFACE_DEFAULT,
167         OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
168         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
169     }else
170     {
171         res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
172         OC_RSRVD_INTERFACE_DEFAULT,
173         OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
174         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
175     }
176     if(res != OC_STACK_OK)
177     {
178         OIC_LOG_V(ERROR, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
179         return res;
180     }
181
182     res = OCBindResourceTypeToResource(g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_COL);
183     if(res != OC_STACK_OK)
184     {
185         OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource type with result: %s", getResult(res));
186         return res;
187     }
188
189     res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_LL);
190     if(res != OC_STACK_OK)
191     {
192         OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
193         return res;
194     }
195     res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_BATCH);
196     if(res != OC_STACK_OK)
197     {
198         OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
199         return res;
200     }
201
202     OIC_LOG_V(DEBUG, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
203     return res;
204 }
205
206 OCStackResult initWiFiConfResource(bool isSecured)
207 {
208     OCStackResult res = OC_STACK_ERROR;
209
210     g_ESWiFiConfResource.supportedFreq = WIFI_BOTH;
211     g_ESWiFiConfResource.supportedMode[0] = WIFI_11A;
212     g_ESWiFiConfResource.supportedMode[1] = WIFI_11B;
213     g_ESWiFiConfResource.supportedMode[2] = WIFI_11G;
214     g_ESWiFiConfResource.supportedMode[3] = WIFI_11N;
215     g_ESWiFiConfResource.numMode = 4;
216     g_ESWiFiConfResource.authType = NONE_AUTH;
217     g_ESWiFiConfResource.encType = NONE_ENC;
218     OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), "");
219     OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), "");
220
221     if (isSecured)
222     {
223         res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
224         OC_RSRVD_INTERFACE_DEFAULT,
225         OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
226         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
227     }else
228     {
229         res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
230         OC_RSRVD_INTERFACE_DEFAULT,
231         OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
232         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
233     }
234
235     OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
236     return res;
237
238 }
239
240 OCStackResult initCoapCloudConfResource(bool isSecured)
241 {
242     OCStackResult res = OC_STACK_ERROR;
243
244     OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
245     OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), "");
246     g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
247     OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), "");
248     OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
249
250     if (isSecured)
251     {
252         res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
253         OC_RSRVD_INTERFACE_DEFAULT,
254         OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
255         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
256     }else
257     {
258         res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
259         OC_RSRVD_INTERFACE_DEFAULT,
260         OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
261         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
262     }
263
264     OIC_LOG_V(DEBUG, ES_RH_TAG, "Created CoapCloudConf resource with result: %s", getResult(res));
265     return res;
266
267 }
268
269 OCStackResult initDevConfResource(bool isSecured)
270 {
271     OCStackResult res = OC_STACK_ERROR;
272
273     OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
274
275     if (isSecured)
276     {
277         res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
278         OC_RSRVD_INTERFACE_DEFAULT,
279         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
280         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
281     }else
282     {
283         res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
284         OC_RSRVD_INTERFACE_DEFAULT,
285         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
286         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
287     }
288
289     OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
290     return res;
291
292 }
293
294 void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
295 {
296     OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
297
298     int64_t *connect_req = NULL;
299     size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
300     if (OCRepPayloadGetIntArray(input, OC_RSRVD_ES_CONNECT, &connect_req, dimensions))
301     {
302         ESConnectRequest* connectRequest = (ESConnectRequest*)OICMalloc(sizeof(ESConnectRequest));
303         if( !connectRequest )
304         {
305             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
306             return;
307         }
308
309         int cntRequest = 0;
310         for (size_t i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
311         {
312             g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
313             connectRequest->connect[i] = ES_CONNECT_NONE;
314
315             if(i < dimensions[0] &&
316                 (connect_req[i] == ES_CONNECT_WIFI || connect_req[i] == ES_CONNECT_COAPCLOUD))
317             {
318                 g_ESEasySetupResource.connectRequest[cntRequest] = connect_req[i];
319                 connectRequest->connect[cntRequest] = connect_req[i];
320                 OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.connectType[%d] : %d",
321                                                     cntRequest, g_ESEasySetupResource.connectRequest[cntRequest]);
322                 cntRequest++;
323             }
324         }
325         connectRequest->numRequest = cntRequest;
326         g_ESEasySetupResource.numRequest = cntRequest;
327
328         if(g_ESEasySetupResource.connectRequest[0] != ES_CONNECT_NONE)
329         {
330             OIC_LOG(DEBUG, ES_RH_TAG, "Send ConnectRequest Callback To ES");
331
332             // TODO : Need to check appropriateness of gWiFiData
333             if(gConnectRequestEvtCb != NULL)
334             {
335                 gConnectRequestEvtCb(ES_OK, connectRequest);
336             }
337             else
338             {
339                 OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
340             }
341         }
342     }
343
344     if(ehRequest->query)
345     {
346         if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
347         {
348             // When Provisioning resource has a POST with BatchInterface
349             updateCoapCloudConfResource(input);
350             updateWiFiConfResource(input);
351             updateDevConfResource(input);
352         }
353     }
354 }
355
356 void updateWiFiConfResource(OCRepPayload* input)
357 {
358     ESWiFiConfData* wiFiData = (ESWiFiConfData*)OICMalloc(sizeof(ESWiFiConfData));
359
360     if(wiFiData == NULL)
361     {
362         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
363         return ;
364     }
365
366     memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
367     memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
368     wiFiData->authtype = NONE_AUTH;
369     wiFiData->enctype = NONE_AUTH;
370     wiFiData->userdata = NULL;
371
372     char* ssid = NULL;
373     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
374     {
375         OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
376         OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
377         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s", g_ESWiFiConfResource.ssid);
378     }
379
380     char* cred = NULL;
381     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
382     {
383         OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
384         OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
385         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s", g_ESWiFiConfResource.cred);
386     }
387
388     int64_t authType = -1;
389     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
390     {
391         g_ESWiFiConfResource.authType = authType;
392         wiFiData->authtype = g_ESWiFiConfResource.authType;
393         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u", g_ESWiFiConfResource.authType);
394     }
395
396     int64_t encType = -1;
397     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
398     {
399         g_ESWiFiConfResource.encType = encType;
400         wiFiData->enctype = g_ESWiFiConfResource.encType;
401         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u", g_ESWiFiConfResource.encType);
402     }
403
404     if(gReadUserdataCb)
405     {
406         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
407     }
408
409     if(ssid || cred || authType!= -1 || encType != -1)
410     {
411         OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
412
413         // TODO : Need to check appropriateness of gWiFiData
414         if(gWifiConfRsrcEvtCb != NULL)
415         {
416             gWifiConfRsrcEvtCb(ES_OK, wiFiData);
417         }
418         else
419         {
420             OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
421         }
422     }
423
424     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
425     {
426         OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
427     }
428
429     OICFree(ssid);
430     OICFree(cred);
431     OICFree(wiFiData);
432 }
433
434 void updateCoapCloudConfResource(OCRepPayload* input)
435 {
436     ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
437
438     if(cloudData == NULL)
439     {
440         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
441         return;
442     }
443
444     memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
445     memset(cloudData->accessToken, 0, OIC_STRING_ACCESS_TOKEN_MAX_VALUE);
446     g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
447     memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
448     memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
449     cloudData->userdata = NULL;
450
451     char *authCode = NULL;
452     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
453     {
454         OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
455         OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
456         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
457     }
458
459     char *accessToken = NULL;
460     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
461     {
462         OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
463         OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
464         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
465     }
466
467     int64_t accessTokenType = -1;
468     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
469     {
470         g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
471         cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
472         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
473     }
474
475     char *authProvider = NULL;
476     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
477     {
478         OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
479         OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
480         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
481     }
482
483     char *ciServer = NULL;
484     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
485     {
486         OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
487         OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
488         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
489     }
490
491     if(gReadUserdataCb)
492     {
493         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
494     }
495
496     if(authCode || accessToken || authProvider || ciServer)
497     {
498         OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
499
500         // TODO : Need to check appropriateness of gCloudData
501         if(gCoapCloudConfRsrcEvtCb != NULL)
502         {
503             gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
504         }
505         else
506         {
507             OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
508         }
509     }
510
511     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
512     {
513         OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
514     }
515
516     OICFree(authCode);
517     OICFree(accessToken);
518     OICFree(authProvider);
519     OICFree(ciServer);
520     OICFree(cloudData);
521 }
522
523 void updateDevConfResource(OCRepPayload* input)
524 {
525     ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
526
527     if(devConfData == NULL)
528     {
529         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
530         return;
531     }
532     devConfData->userdata = NULL;
533
534     if (gReadUserdataCb)
535     {
536         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
537     }
538
539     if( devConfData->userdata != NULL )
540     {
541         OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
542
543         // TODO : Need to check appropriateness of gDevConfData
544         if(gDevConfRsrcEvtCb != NULL)
545         {
546             gDevConfRsrcEvtCb(ES_OK, devConfData);
547         }
548         else
549         {
550             OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
551         }
552     }
553
554     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
555     {
556         OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
557     }
558
559     OICFree(devConfData);
560 }
561
562 OCRepPayload* constructResponseOfWiFiConf(char *interface)
563 {
564     OCRepPayload* payload = OCRepPayloadCreate();
565     if (!payload)
566     {
567         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
568         return NULL;
569     }
570
571     if(g_ESWiFiConfResource.handle == NULL)
572     {
573         OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
574         return NULL;
575     }
576
577     OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
578     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
579
580     OCRepPayload* repPayload = NULL;
581     OCRepPayload* tempPayload = NULL;
582     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
583     {
584         repPayload = OCRepPayloadCreate();
585         if (!repPayload)
586         {
587             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
588             return NULL;
589         }
590
591         tempPayload = payload;
592         payload = repPayload;
593
594         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
595         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
596         if( !interfaces )
597         {
598             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
599             return NULL;
600         }
601
602         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
603
604         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
605
606         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
607         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
608         if( !resourceTypes )
609         {
610             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
611             return NULL;
612         }
613
614         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
615
616         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
617     }
618     else
619     {
620         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
621         OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
622     }
623
624     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESWiFiConfResource.numMode, 0, 0};
625     int64_t *modes_64 = (int64_t *)OICMalloc(g_ESWiFiConfResource.numMode * sizeof(int64_t));
626     if( !modes_64 )
627     {
628         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
629         return NULL;
630     }
631
632     for(int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
633     {
634         modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
635     }
636     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
637
638     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, g_ESWiFiConfResource.supportedFreq);
639     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
640     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
641     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) g_ESWiFiConfResource.authType);
642     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) g_ESWiFiConfResource.encType);
643
644     if(gWriteUserdataCb)
645     {
646         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
647     }
648
649     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
650     {
651         payload = tempPayload;
652         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
653     }
654
655     return payload;
656 }
657
658 OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
659 {
660     OCRepPayload* payload = OCRepPayloadCreate();
661     if (!payload)
662     {
663         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
664         return NULL;
665     }
666
667     if(g_ESCoapCloudConfResource.handle == NULL)
668     {
669         OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
670         return NULL;
671     }
672
673     OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
674     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
675
676     OCRepPayload* repPayload = NULL;
677     OCRepPayload* tempPayload = NULL;
678     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
679     {
680         repPayload = OCRepPayloadCreate();
681         if (!repPayload)
682         {
683             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
684             return NULL;
685         }
686
687         tempPayload = payload;
688         payload = repPayload;
689
690         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
691         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
692         if(!interfaces)
693         {
694             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
695             return NULL;
696         }
697
698         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
699
700         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
701
702         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
703         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
704         if(!resourceTypes)
705         {
706             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
707             return NULL;
708         }
709
710         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
711
712         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
713     }
714     else
715     {
716         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
717         OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
718     }
719
720     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, g_ESCoapCloudConfResource.authCode);
721     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ACCESSTOKEN, g_ESCoapCloudConfResource.accessToken);
722     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ACCESSTOKEN_TYPE, (int)g_ESCoapCloudConfResource.accessTokenType);
723     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, g_ESCoapCloudConfResource.authProvider);
724     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, g_ESCoapCloudConfResource.ciServer);
725
726     if(gWriteUserdataCb)
727     {
728         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
729     }
730
731     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
732     {
733         payload = tempPayload;
734         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
735     }
736
737     return payload;
738 }
739
740 OCRepPayload* constructResponseOfDevConf(char *interface)
741 {
742     OCRepPayload* payload = OCRepPayloadCreate();
743     if (!payload)
744     {
745         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
746         return NULL;
747     }
748
749     if(g_ESDevConfResource.handle == NULL)
750     {
751         OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
752         return NULL;
753     }
754
755     OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
756     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
757
758     OCRepPayload* repPayload = NULL;
759     OCRepPayload* tempPayload = NULL;
760     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
761     {
762         repPayload = OCRepPayloadCreate();
763         if (!repPayload)
764         {
765             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
766             return NULL;
767         }
768
769         tempPayload = payload;
770         payload = repPayload;
771
772         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
773         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
774         if( !interfaces )
775         {
776             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
777             return NULL;
778         }
779
780         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
781
782         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
783
784         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
785         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
786         if( !resourceTypes )
787         {
788             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
789             return NULL;
790         }
791
792         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
793
794         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
795     }
796     else
797     {
798         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
799         OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
800     }
801
802     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, g_ESDevConfResource.devName);
803
804     if(gWriteUserdataCb)
805     {
806         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
807     }
808
809     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
810     {
811         payload = tempPayload;
812         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
813     }
814
815     return payload;
816 }
817
818 OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
819 {
820     OCRepPayload* payload = OCRepPayloadCreate();
821     if (!payload)
822     {
823         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
824         return NULL;
825     }
826
827     // Requested interface is Link list interface
828     //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
829     if(!ehRequest->query ||
830         (ehRequest->query && !strcmp(ehRequest->query, "")) ||
831         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
832         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
833     {
834         const OCRepPayload *arrayPayload[3] = {NULL};
835
836         int childResCnt = 0;
837
838         if(g_ESWiFiConfResource.handle != NULL)
839         {
840             OCRepPayload *add = OCRepPayloadCreate();
841             if(!add)
842             {
843                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
844                 return NULL;
845             }
846
847             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
848             char **resourceType = NULL;
849             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
850             char **resourceInterface = NULL;
851             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
852
853             if(!resourceType || !resourceInterface)
854             {
855                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
856                 return NULL;
857             }
858
859             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
860             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
861
862             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
863             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
864             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
865                                             (const char **)resourceType, dimensions);
866             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
867                                             (const char **)resourceInterface, dimensions);
868
869             OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
870             OCRepPayload *policy = OCRepPayloadCreate();
871             if (!policy)
872             {
873                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
874                 return NULL;
875             }
876
877             OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
878                                     ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
879             if (p & OC_SECURE)
880             {
881                 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
882                 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
883                                                                     ehRequest->devAddr.flags);
884                 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
885             }
886
887             OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
888
889             arrayPayload[childResCnt++] = add;
890         }
891
892         if(g_ESDevConfResource.handle != NULL)
893         {
894             OCRepPayload *add = OCRepPayloadCreate();
895             if(!add)
896             {
897                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
898                 return NULL;
899             }
900
901             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
902             char **resourceType = NULL;
903             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
904             char **resourceInterface = NULL;
905             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
906
907             if(!resourceType || !resourceInterface)
908             {
909                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
910                 return NULL;
911             }
912
913             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
914             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
915
916             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
917             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
918             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
919                                             (const char **)resourceType, dimensions);
920             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
921                                             (const char **)resourceInterface, dimensions);
922
923             OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle);
924             OCRepPayload *policy = OCRepPayloadCreate();
925             if (!policy)
926             {
927                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
928                 return NULL;
929             }
930
931             OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
932                                     ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
933             if (p & OC_SECURE)
934             {
935                 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
936                 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
937                                                                     ehRequest->devAddr.flags);
938                 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
939             }
940
941             OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
942
943             arrayPayload[childResCnt++] = add;
944         }
945
946         if(g_ESCoapCloudConfResource.handle != NULL)
947         {
948             OCRepPayload *add = OCRepPayloadCreate();
949             if(!add)
950             {
951                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
952                 return NULL;
953             }
954
955             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
956             char **resourceType = NULL;
957             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
958             char **resourceInterface = NULL;
959             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
960
961             if(!resourceType || !resourceInterface)
962             {
963                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
964                 return NULL;
965             }
966
967             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
968             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
969
970             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
971             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_COAPCLOUDCONF);
972             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
973                                             (const char **)resourceType, dimensions);
974             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
975                                             (const char **)resourceInterface, dimensions);
976
977             OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESCoapCloudConfResource.handle);
978             OCRepPayload *policy = OCRepPayloadCreate();
979             if (!policy)
980             {
981                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
982                 return NULL;
983             }
984
985             OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
986                                     ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
987             if (p & OC_SECURE)
988             {
989                 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
990                 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
991                                                                     ehRequest->devAddr.flags);
992                 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
993             }
994
995             OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
996
997             arrayPayload[childResCnt++] = add;
998         }
999
1000         size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
1001
1002         if(!ehRequest->query ||
1003             (ehRequest->query && !strcmp(ehRequest->query, "")) ||
1004             (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
1005         {
1006             OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1007             OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1008             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
1009             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
1010             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
1011             OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1012             OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
1013
1014             OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1015             OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1016
1017             if(g_ESEasySetupResource.numRequest > 0)
1018             {
1019                 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1020                 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest  * sizeof(int64_t));
1021                 if(!connectRequest)
1022                 {
1023                     OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1024                     return NULL;
1025                 }
1026
1027                 for(int i = 0 ; i < g_ESEasySetupResource.numRequest  ; ++i)
1028                 {
1029                     connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1030                 }
1031                 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1032             }
1033             else
1034             {
1035                 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1036                 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1037                 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1038             }
1039
1040             if(gWriteUserdataCb)
1041             {
1042                 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1043             }
1044
1045             OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1046         }
1047         else    // link list interface
1048         {
1049             OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1050         }
1051     } else if (
1052         ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1053
1054     {
1055         OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1056         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1057
1058         OCRepPayload* repPayload = NULL;
1059
1060         repPayload = OCRepPayloadCreate();
1061         if (!repPayload)
1062         {
1063             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1064             return NULL;
1065         }
1066
1067         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {3, 0, 0};
1068         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
1069         if(!interfaces)
1070         {
1071             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1072             return NULL;
1073         }
1074
1075         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
1076         interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_LL);
1077         interfaces[2] = OICStrdup(OC_RSRVD_INTERFACE_BATCH);
1078
1079         OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
1080
1081         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
1082         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
1083         if(!resourceTypes)
1084         {
1085             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1086             return NULL;
1087         }
1088
1089         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1090         resourceTypes[1] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COL);
1091
1092         OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
1093
1094         OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1095         OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1096         if(g_ESEasySetupResource.numRequest > 0)
1097         {
1098             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1099             int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest  * sizeof(int64_t));
1100             if(!connectRequest)
1101             {
1102                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1103                 return NULL;
1104             }
1105
1106             for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1107             {
1108                 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1109             }
1110             OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1111         }
1112         else
1113         {
1114             OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1115             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1116             OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1117         }
1118
1119         if(gWriteUserdataCb)
1120         {
1121             gWriteUserdataCb(repPayload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1122         }
1123
1124         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
1125     }
1126
1127     if(ehRequest->query)
1128     {
1129         if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1130         {// When Provisioning resource has a GET with BatchInterface
1131             OCRepPayload* head = payload;
1132             OCRepPayload* nextPayload = NULL;
1133
1134             nextPayload = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_BATCH);
1135             if(nextPayload != NULL)
1136             {
1137                 payload->next = nextPayload;
1138                 payload = payload->next;
1139             }
1140
1141             nextPayload = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_BATCH);
1142             if(nextPayload != NULL)
1143             {
1144                 payload->next = nextPayload;
1145                 payload = payload->next;
1146             }
1147
1148             nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
1149             if(nextPayload != NULL)
1150             {
1151                 payload->next = nextPayload;
1152             }
1153
1154             payload = head;
1155         }
1156     }
1157
1158     return payload;
1159 }
1160
1161
1162 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
1163 {
1164     OCStackResult res = OC_STACK_ERROR;
1165     bool maskFlag = false;
1166
1167     res = initEasySetupResource(isSecured);
1168     if(res != OC_STACK_OK)
1169     {
1170         // TODO: destroy logic will be added
1171         OIC_LOG_V(ERROR, ES_RH_TAG, "initEasySetupResource result: %s", getResult(res));
1172
1173         return res;
1174     }
1175
1176     if((resourceMask & ES_WIFICONF_RESOURCE) == ES_WIFICONF_RESOURCE)
1177     {
1178         maskFlag = true;
1179         res = initWiFiConfResource(isSecured);
1180         if(res != OC_STACK_OK)
1181         {
1182             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiConfResource result: %s", getResult(res));
1183             return res;
1184         }
1185
1186         res = OCBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1187         if(res != OC_STACK_OK)
1188         {
1189             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiConfResource result: %s", getResult(res));
1190             return res;
1191         }
1192
1193     }
1194
1195     if((resourceMask & ES_COAPCLOUDCONF_RESOURCE) == ES_COAPCLOUDCONF_RESOURCE)
1196     {
1197         maskFlag = true;
1198         res = initCoapCloudConfResource(isSecured);
1199         if(res != OC_STACK_OK)
1200         {
1201             OIC_LOG_V(ERROR, ES_RH_TAG, "initCoapCloudConfResource result: %s", getResult(res));
1202             return res;
1203         }
1204
1205         res = OCBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1206         if(res != OC_STACK_OK)
1207         {
1208             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CoapCloudConfResource result: %s", getResult(res));
1209             return res;
1210         }
1211     }
1212
1213     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
1214     {
1215         maskFlag = true;
1216         res = initDevConfResource(isSecured);
1217         if(res != OC_STACK_OK)
1218         {
1219             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
1220             return res;
1221         }
1222
1223         res = OCBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1224         if(res != OC_STACK_OK)
1225         {
1226             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
1227             return res;
1228         }
1229     }
1230
1231     if(maskFlag == false)
1232     {
1233         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
1234         return OC_STACK_ERROR;
1235
1236     }
1237
1238     OIC_LOG_V(DEBUG, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
1239
1240     return res;
1241 }
1242
1243 OCStackResult DeleteEasySetupResources()
1244 {
1245     OCStackResult res = OC_STACK_ERROR;
1246     if (g_ESWiFiConfResource.handle != NULL)
1247     {
1248         res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1249         if(res != OC_STACK_OK)
1250         {
1251             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
1252         }
1253     }
1254     if (g_ESCoapCloudConfResource.handle != NULL)
1255     {
1256         res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1257         if(res != OC_STACK_OK)
1258         {
1259             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
1260         }
1261     }
1262     if (g_ESDevConfResource.handle != NULL)
1263     {
1264         res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1265         if(res != OC_STACK_OK)
1266         {
1267             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
1268         }
1269     }
1270
1271     if (g_ESWiFiConfResource.handle != NULL)
1272     {
1273         res = OCDeleteResource(g_ESWiFiConfResource.handle);
1274         if (res != OC_STACK_OK)
1275         {
1276             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
1277         }
1278     }
1279
1280     if(g_ESCoapCloudConfResource.handle != NULL)
1281     {
1282         res = OCDeleteResource(g_ESCoapCloudConfResource.handle);
1283         if (res != OC_STACK_OK)
1284         {
1285             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
1286         }
1287     }
1288
1289     if(g_ESDevConfResource.handle != NULL)
1290     {
1291         res = OCDeleteResource(g_ESDevConfResource.handle);
1292         if (res != OC_STACK_OK)
1293         {
1294             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
1295         }
1296     }
1297
1298     if(g_ESEasySetupResource.handle != NULL)
1299     {
1300         res = OCDeleteResource(g_ESEasySetupResource.handle);
1301         if (res != OC_STACK_OK)
1302         {
1303             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1304         }
1305     }
1306
1307     return res;
1308 }
1309
1310 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1311 {
1312     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1313     if (!ehRequest)
1314     {
1315         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1316         return ehResult;
1317     }
1318     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1319     {
1320         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1321         return ehResult;
1322     }
1323
1324     OCRepPayload *getResp = NULL;
1325     *payload = NULL;
1326
1327     if(ehRequest->resource == g_ESEasySetupResource.handle)
1328     {
1329         if(ehRequest->query &&
1330             strcmp(ehRequest->query, "") &&
1331             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1332             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1333             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1334         {
1335             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1336             return OC_EH_BAD_REQ;
1337         }
1338         else
1339         {
1340             getResp = constructResponseOfEasySetup(ehRequest);
1341         }
1342     }
1343     else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1344     {
1345         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1346         {
1347             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1348             return OC_EH_BAD_REQ;
1349         }
1350         else
1351         {
1352             getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1353         }
1354     }
1355     else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1356     {
1357         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1358         {
1359             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1360             return OC_EH_BAD_REQ;
1361         }
1362         else
1363         {
1364             getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1365         }
1366     }
1367     else if(ehRequest->resource == g_ESDevConfResource.handle)
1368     {
1369         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1370         {
1371             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1372             return OC_EH_BAD_REQ;
1373         }
1374         else
1375         {
1376             getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1377         }
1378     }
1379
1380     if (!getResp)
1381     {
1382         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1383         return OC_EH_ERROR;
1384     }
1385
1386     *payload = getResp;
1387     ehResult = OC_EH_OK;
1388
1389     return ehResult;
1390 }
1391
1392 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1393 {
1394     OIC_LOG(DEBUG, ES_RH_TAG, "ProcessPostRequest enter");
1395     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1396     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1397     {
1398         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1399         return ehResult;
1400     }
1401
1402     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1403     if (!input)
1404     {
1405         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1406         return ehResult;
1407     }
1408
1409     if(ehRequest->resource == g_ESEasySetupResource.handle)
1410     {
1411         if(ehRequest->query &&
1412             strcmp(ehRequest->query, "") &&
1413             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1414             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1415         {
1416             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1417             return OC_EH_BAD_REQ;
1418         }
1419         else
1420         {
1421             updateEasySetupResource(ehRequest, input);
1422         }
1423     }
1424     else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1425     {
1426         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1427         {
1428             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1429             return OC_EH_BAD_REQ;
1430         }
1431         else
1432         {
1433             updateWiFiConfResource(input);
1434         }
1435     }
1436     else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1437     {
1438         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1439         {
1440             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1441             return OC_EH_BAD_REQ;
1442         }
1443         else
1444         {
1445             updateCoapCloudConfResource(input);
1446         }
1447     }
1448     else if(ehRequest->resource == g_ESDevConfResource.handle)
1449     {
1450         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1451         {
1452             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1453             return OC_EH_BAD_REQ;
1454         }
1455         else
1456         {
1457             updateDevConfResource(input);
1458         }
1459     }
1460
1461     OCRepPayload *getResp = NULL;
1462     if(ehRequest->resource == g_ESEasySetupResource.handle)
1463     {
1464         getResp = constructResponseOfEasySetup(ehRequest);
1465     }
1466     else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1467     {
1468         getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1469     }
1470     else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1471     {
1472         getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1473     }
1474     else if(ehRequest->resource == g_ESDevConfResource.handle)
1475     {
1476         getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1477     }
1478
1479     if (!getResp)
1480     {
1481         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1482         return OC_EH_ERROR;
1483     }
1484
1485     *payload = getResp;
1486     ehResult = OC_EH_OK;
1487
1488     return ehResult;
1489 }
1490
1491 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1492         OCRepPayload** payload)
1493 {
1494     (void) ehRequest;
1495     (void) payload;
1496     OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1497
1498     return ehResult;
1499 }
1500 /**
1501  * This is the entity handler for the registered resource.
1502  * This is invoked by OCStack whenever it recevies a request for this resource.
1503  */
1504 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1505         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1506 {
1507     (void) callback;
1508     OCEntityHandlerResult ehRet = OC_EH_OK;
1509     OCEntityHandlerResponse response =
1510     { 0, 0, OC_EH_ERROR, 0, 0,
1511     { },
1512     { 0 }, false };
1513     OCRepPayload* payload = NULL;
1514
1515     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1516     {
1517         if (OC_REST_GET == entityHandlerRequest->method)
1518         {
1519             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1520             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1521         }
1522         else if (OC_REST_PUT == entityHandlerRequest->method)
1523         {
1524             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1525
1526             //PUT request will be handled in the internal implementation
1527             if (g_ESEasySetupResource.handle != NULL)
1528             {
1529                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1530             }
1531             else
1532             {
1533                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1534                 ehRet = OC_EH_ERROR;
1535             }
1536         }
1537         else if (OC_REST_POST == entityHandlerRequest->method)
1538         {
1539             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1540             if (g_ESEasySetupResource.handle != NULL)
1541             {
1542                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1543             }
1544             else
1545             {
1546                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1547                 ehRet = OC_EH_ERROR;
1548             }
1549         }
1550
1551         // Format the response.  Note this requires some info about the request
1552         response.requestHandle = entityHandlerRequest->requestHandle;
1553         response.resourceHandle = entityHandlerRequest->resource;
1554         response.ehResult = ehRet;
1555         //response uses OCPaylod while all get,put methodes use OCRepPayload
1556         response.payload = (OCPayload*) (payload);
1557         response.numSendVendorSpecificHeaderOptions = 0;
1558         memset(response.sendVendorSpecificHeaderOptions, 0,
1559                 sizeof(response.sendVendorSpecificHeaderOptions));
1560         memset(response.resourceUri, 0, sizeof(response.resourceUri));
1561         // Indicate that response is NOT in a persistent buffer
1562         response.persistentBufferFlag = 0;
1563
1564         // Send the response
1565         OIC_LOG(INFO, ES_RH_TAG, "Invoking OCDoResponse");
1566         if (OCDoResponse(&response) != OC_STACK_OK)
1567         {
1568             OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1569             ehRet = OC_EH_ERROR;
1570         }
1571     }
1572     if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
1573     {
1574         OIC_LOG(DEBUG, ES_RH_TAG, "Flag includes OC_OBSERVE_FLAG");
1575
1576         if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
1577         {
1578             OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_REGISTER from Mediator");
1579         }
1580         else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
1581         {
1582             OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_DEREGISTER from Mediator");
1583         }
1584     }
1585
1586     OIC_LOG(INFO, ES_RH_TAG, "ES OCEntityHandlerCb OUT");
1587     return ehRet;
1588 }
1589
1590 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1591 {
1592     OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty IN");
1593
1594     g_ESWiFiConfResource.supportedFreq = (deviceProperty->WiFi).freq;
1595     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Freq : %d", g_ESWiFiConfResource.supportedFreq);
1596
1597     int modeIdx = 0;
1598     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1599     {
1600         g_ESWiFiConfResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1601         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Mode : %d", g_ESWiFiConfResource.supportedMode[modeIdx]);
1602         modeIdx ++;
1603     }
1604     g_ESWiFiConfResource.numMode = modeIdx;
1605
1606     OICStrcpy(g_ESDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1607     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Device Name : %s", g_ESDevConfResource.devName);
1608
1609     if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
1610     {
1611         OIC_LOG(DEBUG, ES_RH_TAG, "wifiResource doesn't have any observers.");
1612     }
1613
1614     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
1615     {
1616         OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observers.");
1617     }
1618
1619     OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty OUT");
1620     return OC_STACK_OK;
1621 }
1622
1623 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1624 {
1625     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState IN");
1626
1627     g_ESEasySetupResource.status = esState;
1628     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee Status : %d", g_ESEasySetupResource.status);
1629
1630     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1631     {
1632         OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1633     }
1634
1635     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState OUT");
1636     return OC_STACK_OK;
1637 }
1638
1639 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1640 {
1641     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode IN");
1642
1643     g_ESEasySetupResource.lastErrCode = esErrCode;
1644     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee ErrorCode : %d", g_ESEasySetupResource.lastErrCode);
1645
1646     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1647     {
1648         OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1649     }
1650
1651     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1652     return OC_STACK_OK;
1653 }
1654
1655 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1656 {
1657     if( !(ehRequest->query) ||
1658                 (ehRequest->query &&
1659                 (strcmp(ehRequest->query, "") && !CompareResourceInterface(ehRequest->query,
1660                                                                         OC_RSRVD_INTERFACE_DEFAULT))))
1661     {
1662         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1663         return OC_EH_BAD_REQ;
1664     }
1665     return OC_EH_OK;
1666 }
1667
1668 const char *getResult(OCStackResult result)
1669 {
1670     switch (result)
1671     {
1672         case OC_STACK_OK:
1673             return "OC_STACK_OK";
1674         case OC_STACK_INVALID_URI:
1675             return "OC_STACK_INVALID_URI";
1676         case OC_STACK_INVALID_QUERY:
1677             return "OC_STACK_INVALID_QUERY";
1678         case OC_STACK_INVALID_IP:
1679             return "OC_STACK_INVALID_IP";
1680         case OC_STACK_INVALID_PORT:
1681             return "OC_STACK_INVALID_PORT";
1682         case OC_STACK_INVALID_CALLBACK:
1683             return "OC_STACK_INVALID_CALLBACK";
1684         case OC_STACK_INVALID_METHOD:
1685             return "OC_STACK_INVALID_METHOD";
1686         case OC_STACK_NO_MEMORY:
1687             return "OC_STACK_NO_MEMORY";
1688         case OC_STACK_COMM_ERROR:
1689             return "OC_STACK_COMM_ERROR";
1690         case OC_STACK_INVALID_PARAM:
1691             return "OC_STACK_INVALID_PARAM";
1692         case OC_STACK_NOTIMPL:
1693             return "OC_STACK_NOTIMPL";
1694         case OC_STACK_NO_RESOURCE:
1695             return "OC_STACK_NO_RESOURCE";
1696         case OC_STACK_RESOURCE_ERROR:
1697             return "OC_STACK_RESOURCE_ERROR";
1698         case OC_STACK_SLOW_RESOURCE:
1699             return "OC_STACK_SLOW_RESOURCE";
1700         case OC_STACK_NO_OBSERVERS:
1701             return "OC_STACK_NO_OBSERVERS";
1702         case OC_STACK_ERROR:
1703             return "OC_STACK_ERROR";
1704         default:
1705             return "UNKNOWN";
1706     }
1707 }