replace : iotivity -> iotivity-sec
[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(wiFiData);
430 }
431
432 void updateCoapCloudConfResource(OCRepPayload* input)
433 {
434     ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
435
436     if(cloudData == NULL)
437     {
438         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
439         return;
440     }
441
442     memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
443     memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
444     g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
445     memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
446     memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
447     cloudData->userdata = NULL;
448
449     char *authCode = NULL;
450     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
451     {
452         OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
453         OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
454         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
455     }
456
457     char *accessToken = NULL;
458     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
459     {
460         OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
461         OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
462         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
463     }
464
465     int64_t accessTokenType = -1;
466     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
467     {
468         g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
469         cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
470         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
471     }
472
473     char *authProvider = NULL;
474     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
475     {
476         OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
477         OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
478         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
479     }
480
481     char *ciServer = NULL;
482     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
483     {
484         OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
485         OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
486         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
487     }
488
489     if(gReadUserdataCb)
490     {
491         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
492     }
493
494     if(authCode || accessToken || authProvider || ciServer)
495     {
496         OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
497
498         // TODO : Need to check appropriateness of gCloudData
499         if(gCoapCloudConfRsrcEvtCb != NULL)
500         {
501             gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
502         }
503         else
504         {
505             OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
506         }
507     }
508
509     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
510     {
511         OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
512     }
513
514     OICFree(cloudData);
515 }
516
517 void updateDevConfResource(OCRepPayload* input)
518 {
519     ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
520
521     if(devConfData == NULL)
522     {
523         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
524         return;
525     }
526     devConfData->userdata = NULL;
527
528     if (gReadUserdataCb)
529     {
530         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
531     }
532
533     if( devConfData->userdata != NULL )
534     {
535         OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
536
537         // TODO : Need to check appropriateness of gDevConfData
538         if(gDevConfRsrcEvtCb != NULL)
539         {
540             gDevConfRsrcEvtCb(ES_OK, devConfData);
541         }
542         else
543         {
544             OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
545         }
546     }
547
548     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
549     {
550         OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
551     }
552
553     OICFree(devConfData);
554 }
555
556 OCRepPayload* constructResponseOfWiFiConf(char *interface)
557 {
558     OCRepPayload* payload = OCRepPayloadCreate();
559     if (!payload)
560     {
561         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
562         return NULL;
563     }
564
565     if(g_ESWiFiConfResource.handle == NULL)
566     {
567         OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
568         return NULL;
569     }
570
571     OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
572     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
573
574     OCRepPayload* repPayload = NULL;
575     OCRepPayload* tempPayload = NULL;
576     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
577     {
578         repPayload = OCRepPayloadCreate();
579         if (!repPayload)
580         {
581             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
582             return NULL;
583         }
584
585         tempPayload = payload;
586         payload = repPayload;
587
588         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
589         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
590         if( !interfaces )
591         {
592             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
593             return NULL;
594         }
595
596         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
597
598         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
599
600         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
601         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
602         if( !resourceTypes )
603         {
604             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
605             return NULL;
606         }
607
608         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
609
610         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
611     }
612     else
613     {
614         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
615         OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
616     }
617
618     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESWiFiConfResource.numMode, 0, 0};
619     int64_t *modes_64 = (int64_t *)OICMalloc(g_ESWiFiConfResource.numMode * sizeof(int64_t));
620     if( !modes_64 )
621     {
622         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
623         return NULL;
624     }
625
626     for(int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
627     {
628         modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
629     }
630     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
631
632     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, g_ESWiFiConfResource.supportedFreq);
633     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
634     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
635     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) g_ESWiFiConfResource.authType);
636     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) g_ESWiFiConfResource.encType);
637
638     if(gWriteUserdataCb)
639     {
640         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
641     }
642
643     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
644     {
645         payload = tempPayload;
646         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
647     }
648
649     return payload;
650 }
651
652 OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
653 {
654     OCRepPayload* payload = OCRepPayloadCreate();
655     if (!payload)
656     {
657         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
658         return NULL;
659     }
660
661     if(g_ESCoapCloudConfResource.handle == NULL)
662     {
663         OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
664         return NULL;
665     }
666
667     OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
668     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
669
670     OCRepPayload* repPayload = NULL;
671     OCRepPayload* tempPayload = NULL;
672     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
673     {
674         repPayload = OCRepPayloadCreate();
675         if (!repPayload)
676         {
677             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
678             return NULL;
679         }
680
681         tempPayload = payload;
682         payload = repPayload;
683
684         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
685         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
686         if(!interfaces)
687         {
688             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
689             return NULL;
690         }
691
692         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
693
694         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
695
696         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
697         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
698         if(!resourceTypes)
699         {
700             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
701             return NULL;
702         }
703
704         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
705
706         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
707     }
708     else
709     {
710         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
711         OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
712     }
713
714     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, g_ESCoapCloudConfResource.authCode);
715     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ACCESSTOKEN, g_ESCoapCloudConfResource.accessToken);
716     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ACCESSTOKEN_TYPE, (int)g_ESCoapCloudConfResource.accessTokenType);
717     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, g_ESCoapCloudConfResource.authProvider);
718     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, g_ESCoapCloudConfResource.ciServer);
719
720     if(gWriteUserdataCb)
721     {
722         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
723     }
724
725     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
726     {
727         payload = tempPayload;
728         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
729     }
730
731     return payload;
732 }
733
734 OCRepPayload* constructResponseOfDevConf(char *interface)
735 {
736     OCRepPayload* payload = OCRepPayloadCreate();
737     if (!payload)
738     {
739         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
740         return NULL;
741     }
742
743     if(g_ESDevConfResource.handle == NULL)
744     {
745         OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
746         return NULL;
747     }
748
749     OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
750     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
751
752     OCRepPayload* repPayload = NULL;
753     OCRepPayload* tempPayload = NULL;
754     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
755     {
756         repPayload = OCRepPayloadCreate();
757         if (!repPayload)
758         {
759             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
760             return NULL;
761         }
762
763         tempPayload = payload;
764         payload = repPayload;
765
766         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
767         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
768         if( !interfaces )
769         {
770             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
771             return NULL;
772         }
773
774         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
775
776         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
777
778         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
779         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
780         if( !resourceTypes )
781         {
782             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
783             return NULL;
784         }
785
786         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
787
788         OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
789     }
790     else
791     {
792         OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
793         OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
794     }
795
796     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, g_ESDevConfResource.devName);
797
798     if(gWriteUserdataCb)
799     {
800         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
801     }
802
803     if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
804     {
805         payload = tempPayload;
806         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
807     }
808
809     return payload;
810 }
811
812 OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
813 {
814     OCRepPayload* payload = OCRepPayloadCreate();
815     if (!payload)
816     {
817         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
818         return NULL;
819     }
820
821     // Requested interface is Link list interface
822     //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
823     if(!ehRequest->query ||
824         (ehRequest->query && !strcmp(ehRequest->query, "")) ||
825         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
826         (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
827     {
828         const OCRepPayload *arrayPayload[3] = {NULL};
829
830         int childResCnt = 0;
831
832         if(g_ESWiFiConfResource.handle != NULL)
833         {
834             OCRepPayload *add = OCRepPayloadCreate();
835             if(!add)
836             {
837                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
838                 return NULL;
839             }
840
841             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
842             char **resourceType = NULL;
843             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
844             char **resourceInterface = NULL;
845             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
846
847             if(!resourceType || !resourceInterface)
848             {
849                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
850                 return NULL;
851             }
852
853             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
854             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
855
856             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
857             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
858             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
859                                             (const char **)resourceType, dimensions);
860             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
861                                             (const char **)resourceInterface, dimensions);
862
863             OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
864             OCRepPayload *policy = OCRepPayloadCreate();
865             if (!policy)
866             {
867                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
868                 return NULL;
869             }
870
871             OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
872                                     ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
873             if (p & OC_SECURE)
874             {
875                 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
876                 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
877                                                                     ehRequest->devAddr.flags);
878                 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
879             }
880
881             OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
882
883             arrayPayload[childResCnt++] = add;
884         }
885
886         if(g_ESDevConfResource.handle != NULL)
887         {
888             OCRepPayload *add = OCRepPayloadCreate();
889             if(!add)
890             {
891                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
892                 return NULL;
893             }
894
895             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
896             char **resourceType = NULL;
897             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
898             char **resourceInterface = NULL;
899             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
900
901             if(!resourceType || !resourceInterface)
902             {
903                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
904                 return NULL;
905             }
906
907             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
908             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
909
910             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
911             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
912             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
913                                             (const char **)resourceType, dimensions);
914             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
915                                             (const char **)resourceInterface, dimensions);
916
917             OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle);
918             OCRepPayload *policy = OCRepPayloadCreate();
919             if (!policy)
920             {
921                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
922                 return NULL;
923             }
924
925             OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
926                                     ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
927             if (p & OC_SECURE)
928             {
929                 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
930                 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
931                                                                     ehRequest->devAddr.flags);
932                 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
933             }
934
935             OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
936
937             arrayPayload[childResCnt++] = add;
938         }
939
940         if(g_ESCoapCloudConfResource.handle != NULL)
941         {
942             OCRepPayload *add = OCRepPayloadCreate();
943             if(!add)
944             {
945                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
946                 return NULL;
947             }
948
949             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
950             char **resourceType = NULL;
951             resourceType = (char **)OICMalloc(sizeof(char *) * 1);
952             char **resourceInterface = NULL;
953             resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
954
955             if(!resourceType || !resourceInterface)
956             {
957                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
958                 return NULL;
959             }
960
961             resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
962             resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
963
964             add->base.type = PAYLOAD_TYPE_REPRESENTATION;
965             OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_COAPCLOUDCONF);
966             OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
967                                             (const char **)resourceType, dimensions);
968             OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
969                                             (const char **)resourceInterface, dimensions);
970
971             OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESCoapCloudConfResource.handle);
972             OCRepPayload *policy = OCRepPayloadCreate();
973             if (!policy)
974             {
975                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
976                 return NULL;
977             }
978
979             OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
980                                     ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
981             if (p & OC_SECURE)
982             {
983                 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
984                 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
985                                                                     ehRequest->devAddr.flags);
986                 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
987             }
988
989             OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
990
991             arrayPayload[childResCnt++] = add;
992         }
993
994         size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
995
996         if(!ehRequest->query ||
997             (ehRequest->query && !strcmp(ehRequest->query, "")) ||
998             (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
999         {
1000             OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1001             OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1002             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
1003             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
1004             OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
1005             OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1006             OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
1007
1008             OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1009             OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1010
1011             if(g_ESEasySetupResource.numRequest > 0)
1012             {
1013                 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1014                 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest  * sizeof(int64_t));
1015                 if(!connectRequest)
1016                 {
1017                     OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1018                     return NULL;
1019                 }
1020
1021                 for(int i = 0 ; i < g_ESEasySetupResource.numRequest  ; ++i)
1022                 {
1023                     connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1024                 }
1025                 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1026             }
1027             else
1028             {
1029                 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1030                 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1031                 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1032             }
1033
1034             if(gWriteUserdataCb)
1035             {
1036                 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1037             }
1038
1039             OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1040         }
1041         else    // link list interface
1042         {
1043             OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1044         }
1045     } else if (
1046         ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1047
1048     {
1049         OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1050         OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1051
1052         OCRepPayload* repPayload = NULL;
1053
1054         repPayload = OCRepPayloadCreate();
1055         if (!repPayload)
1056         {
1057             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1058             return NULL;
1059         }
1060
1061         size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {3, 0, 0};
1062         char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
1063         if(!interfaces)
1064         {
1065             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1066             return NULL;
1067         }
1068
1069         interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
1070         interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_LL);
1071         interfaces[2] = OICStrdup(OC_RSRVD_INTERFACE_BATCH);
1072
1073         OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
1074
1075         size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
1076         char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
1077         if(!resourceTypes)
1078         {
1079             OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1080             return NULL;
1081         }
1082
1083         resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1084         resourceTypes[1] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COL);
1085
1086         OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
1087
1088         OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1089         OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1090         if(g_ESEasySetupResource.numRequest > 0)
1091         {
1092             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1093             int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest  * sizeof(int64_t));
1094             if(!connectRequest)
1095             {
1096                 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1097                 return NULL;
1098             }
1099
1100             for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1101             {
1102                 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1103             }
1104             OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1105         }
1106         else
1107         {
1108             OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1109             size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1110             OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1111         }
1112
1113         if(gWriteUserdataCb)
1114         {
1115             gWriteUserdataCb(repPayload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1116         }
1117
1118         OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
1119     }
1120
1121     if(ehRequest->query)
1122     {
1123         if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1124         {// When Provisioning resource has a GET with BatchInterface
1125             OCRepPayload* head = payload;
1126             OCRepPayload* nextPayload = NULL;
1127
1128             nextPayload = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_BATCH);
1129             if(nextPayload != NULL)
1130             {
1131                 payload->next = nextPayload;
1132                 payload = payload->next;
1133             }
1134
1135             nextPayload = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_BATCH);
1136             if(nextPayload != NULL)
1137             {
1138                 payload->next = nextPayload;
1139                 payload = payload->next;
1140             }
1141
1142             nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
1143             if(nextPayload != NULL)
1144             {
1145                 payload->next = nextPayload;
1146             }
1147
1148             payload = head;
1149         }
1150     }
1151
1152     return payload;
1153 }
1154
1155
1156 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
1157 {
1158     OCStackResult res = OC_STACK_ERROR;
1159     bool maskFlag = false;
1160
1161     res = initEasySetupResource(isSecured);
1162     if(res != OC_STACK_OK)
1163     {
1164         // TODO: destroy logic will be added
1165         OIC_LOG_V(ERROR, ES_RH_TAG, "initEasySetupResource result: %s", getResult(res));
1166
1167         return res;
1168     }
1169
1170     if((resourceMask & ES_WIFICONF_RESOURCE) == ES_WIFICONF_RESOURCE)
1171     {
1172         maskFlag = true;
1173         res = initWiFiConfResource(isSecured);
1174         if(res != OC_STACK_OK)
1175         {
1176             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiConfResource result: %s", getResult(res));
1177             return res;
1178         }
1179
1180         res = OCBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1181         if(res != OC_STACK_OK)
1182         {
1183             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiConfResource result: %s", getResult(res));
1184             return res;
1185         }
1186
1187     }
1188
1189     if((resourceMask & ES_COAPCLOUDCONF_RESOURCE) == ES_COAPCLOUDCONF_RESOURCE)
1190     {
1191         maskFlag = true;
1192         res = initCoapCloudConfResource(isSecured);
1193         if(res != OC_STACK_OK)
1194         {
1195             OIC_LOG_V(ERROR, ES_RH_TAG, "initCoapCloudConfResource result: %s", getResult(res));
1196             return res;
1197         }
1198
1199         res = OCBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1200         if(res != OC_STACK_OK)
1201         {
1202             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CoapCloudConfResource result: %s", getResult(res));
1203             return res;
1204         }
1205     }
1206
1207     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
1208     {
1209         maskFlag = true;
1210         res = initDevConfResource(isSecured);
1211         if(res != OC_STACK_OK)
1212         {
1213             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
1214             return res;
1215         }
1216
1217         res = OCBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1218         if(res != OC_STACK_OK)
1219         {
1220             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
1221             return res;
1222         }
1223     }
1224
1225     if(maskFlag == false)
1226     {
1227         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
1228         return OC_STACK_ERROR;
1229
1230     }
1231
1232     OIC_LOG_V(DEBUG, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
1233
1234     return res;
1235 }
1236
1237 OCStackResult DeleteEasySetupResources()
1238 {
1239     OCStackResult res = OC_STACK_ERROR;
1240     if (g_ESWiFiConfResource.handle != NULL)
1241     {
1242         res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1243         if(res != OC_STACK_OK)
1244         {
1245             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
1246         }
1247     }
1248     if (g_ESCoapCloudConfResource.handle != NULL)
1249     {
1250         res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1251         if(res != OC_STACK_OK)
1252         {
1253             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
1254         }
1255     }
1256     if (g_ESDevConfResource.handle != NULL)
1257     {
1258         res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1259         if(res != OC_STACK_OK)
1260         {
1261             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
1262         }
1263     }
1264
1265     if (g_ESWiFiConfResource.handle != NULL)
1266     {
1267         res = OCDeleteResource(g_ESWiFiConfResource.handle);
1268         if (res != OC_STACK_OK)
1269         {
1270             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
1271         }
1272     }
1273
1274     if(g_ESCoapCloudConfResource.handle != NULL)
1275     {
1276         res = OCDeleteResource(g_ESCoapCloudConfResource.handle);
1277         if (res != OC_STACK_OK)
1278         {
1279             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
1280         }
1281     }
1282
1283     if(g_ESDevConfResource.handle != NULL)
1284     {
1285         res = OCDeleteResource(g_ESDevConfResource.handle);
1286         if (res != OC_STACK_OK)
1287         {
1288             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
1289         }
1290     }
1291
1292     if(g_ESEasySetupResource.handle != NULL)
1293     {
1294         res = OCDeleteResource(g_ESEasySetupResource.handle);
1295         if (res != OC_STACK_OK)
1296         {
1297             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1298         }
1299     }
1300
1301     return res;
1302 }
1303
1304 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1305 {
1306     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1307     if (!ehRequest)
1308     {
1309         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1310         return ehResult;
1311     }
1312     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1313     {
1314         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1315         return ehResult;
1316     }
1317
1318     OCRepPayload *getResp = NULL;
1319     *payload = NULL;
1320
1321     if(ehRequest->resource == g_ESEasySetupResource.handle)
1322     {
1323         if(ehRequest->query &&
1324             strcmp(ehRequest->query, "") &&
1325             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1326             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1327             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1328         {
1329             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1330             return OC_EH_BAD_REQ;
1331         }
1332         else
1333         {
1334             getResp = constructResponseOfEasySetup(ehRequest);
1335         }
1336     }
1337     else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1338     {
1339         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1340         {
1341             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1342             return OC_EH_BAD_REQ;
1343         }
1344         else
1345         {
1346             getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1347         }
1348     }
1349     else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1350     {
1351         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1352         {
1353             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1354             return OC_EH_BAD_REQ;
1355         }
1356         else
1357         {
1358             getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1359         }
1360     }
1361     else if(ehRequest->resource == g_ESDevConfResource.handle)
1362     {
1363         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1364         {
1365             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1366             return OC_EH_BAD_REQ;
1367         }
1368         else
1369         {
1370             getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1371         }
1372     }
1373
1374     if (!getResp)
1375     {
1376         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1377         return OC_EH_ERROR;
1378     }
1379
1380     *payload = getResp;
1381     ehResult = OC_EH_OK;
1382
1383     return ehResult;
1384 }
1385
1386 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1387 {
1388     OIC_LOG(DEBUG, ES_RH_TAG, "ProcessPostRequest enter");
1389     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1390     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1391     {
1392         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1393         return ehResult;
1394     }
1395
1396     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1397     if (!input)
1398     {
1399         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1400         return ehResult;
1401     }
1402
1403     if(ehRequest->resource == g_ESEasySetupResource.handle)
1404     {
1405         if(ehRequest->query &&
1406             strcmp(ehRequest->query, "") &&
1407             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1408             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1409         {
1410             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1411             return OC_EH_BAD_REQ;
1412         }
1413         else
1414         {
1415             updateEasySetupResource(ehRequest, input);
1416         }
1417     }
1418     else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1419     {
1420         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1421         {
1422             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1423             return OC_EH_BAD_REQ;
1424         }
1425         else
1426         {
1427             updateWiFiConfResource(input);
1428         }
1429     }
1430     else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1431     {
1432         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1433         {
1434             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1435             return OC_EH_BAD_REQ;
1436         }
1437         else
1438         {
1439             updateCoapCloudConfResource(input);
1440         }
1441     }
1442     else if(ehRequest->resource == g_ESDevConfResource.handle)
1443     {
1444         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1445         {
1446             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1447             return OC_EH_BAD_REQ;
1448         }
1449         else
1450         {
1451             updateDevConfResource(input);
1452         }
1453     }
1454
1455     OCRepPayload *getResp = NULL;
1456     if(ehRequest->resource == g_ESEasySetupResource.handle)
1457     {
1458         getResp = constructResponseOfEasySetup(ehRequest);
1459     }
1460     else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1461     {
1462         getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1463     }
1464     else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1465     {
1466         getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1467     }
1468     else if(ehRequest->resource == g_ESDevConfResource.handle)
1469     {
1470         getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1471     }
1472
1473     if (!getResp)
1474     {
1475         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1476         return OC_EH_ERROR;
1477     }
1478
1479     *payload = getResp;
1480     ehResult = OC_EH_OK;
1481
1482     return ehResult;
1483 }
1484
1485 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1486         OCRepPayload** payload)
1487 {
1488     (void) ehRequest;
1489     (void) payload;
1490     OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1491
1492     return ehResult;
1493 }
1494 /**
1495  * This is the entity handler for the registered resource.
1496  * This is invoked by OCStack whenever it recevies a request for this resource.
1497  */
1498 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1499         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1500 {
1501     (void) callback;
1502     OCEntityHandlerResult ehRet = OC_EH_OK;
1503     OCEntityHandlerResponse response =
1504     { 0, 0, OC_EH_ERROR, 0, 0,
1505     { },
1506     { 0 }, false };
1507     OCRepPayload* payload = NULL;
1508
1509     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1510     {
1511         if (OC_REST_GET == entityHandlerRequest->method)
1512         {
1513             OIC_LOG(DEBUG, ES_RH_TAG, "Received GET request");
1514             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1515         }
1516         else if (OC_REST_PUT == entityHandlerRequest->method)
1517         {
1518             OIC_LOG(DEBUG, ES_RH_TAG, "Received PUT request");
1519
1520             //PUT request will be handled in the internal implementation
1521             if (g_ESEasySetupResource.handle != NULL)
1522             {
1523                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1524             }
1525             else
1526             {
1527                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1528                 ehRet = OC_EH_ERROR;
1529             }
1530         }
1531         else if (OC_REST_POST == entityHandlerRequest->method)
1532         {
1533             OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_REST_POST from client");
1534             if (g_ESEasySetupResource.handle != NULL)
1535             {
1536                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1537             }
1538             else
1539             {
1540                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1541                 ehRet = OC_EH_ERROR;
1542             }
1543         }
1544
1545         // Format the response.  Note this requires some info about the request
1546         response.requestHandle = entityHandlerRequest->requestHandle;
1547         response.resourceHandle = entityHandlerRequest->resource;
1548         response.ehResult = ehRet;
1549         //response uses OCPaylod while all get,put methodes use OCRepPayload
1550         response.payload = (OCPayload*) (payload);
1551         response.numSendVendorSpecificHeaderOptions = 0;
1552         memset(response.sendVendorSpecificHeaderOptions, 0,
1553                 sizeof(response.sendVendorSpecificHeaderOptions));
1554         memset(response.resourceUri, 0, sizeof(response.resourceUri));
1555         // Indicate that response is NOT in a persistent buffer
1556         response.persistentBufferFlag = 0;
1557
1558         // Send the response
1559         if (OCDoResponse(&response) != OC_STACK_OK)
1560         {
1561             OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1562             ehRet = OC_EH_ERROR;
1563         }
1564     }
1565     if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
1566     {
1567         OIC_LOG(DEBUG, ES_RH_TAG, "Flag includes OC_OBSERVE_FLAG");
1568
1569         if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
1570         {
1571             OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_REGISTER from Mediator");
1572         }
1573         else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
1574         {
1575             OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_DEREGISTER from Mediator");
1576         }
1577     }
1578     return ehRet;
1579 }
1580
1581 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1582 {
1583     OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty IN");
1584
1585     g_ESWiFiConfResource.supportedFreq = (deviceProperty->WiFi).freq;
1586     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Freq : %d", g_ESWiFiConfResource.supportedFreq);
1587
1588     int modeIdx = 0;
1589     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1590     {
1591         g_ESWiFiConfResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1592         OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Mode : %d", g_ESWiFiConfResource.supportedMode[modeIdx]);
1593         modeIdx ++;
1594     }
1595     g_ESWiFiConfResource.numMode = modeIdx;
1596
1597     OICStrcpy(g_ESDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1598     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Device Name : %s", g_ESDevConfResource.devName);
1599
1600     if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
1601     {
1602         OIC_LOG(DEBUG, ES_RH_TAG, "wifiResource doesn't have any observers.");
1603     }
1604
1605     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
1606     {
1607         OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observers.");
1608     }
1609
1610     OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty OUT");
1611     return OC_STACK_OK;
1612 }
1613
1614 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1615 {
1616     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState IN");
1617
1618     g_ESEasySetupResource.status = esState;
1619     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee Status : %d", g_ESEasySetupResource.status);
1620
1621     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1622     {
1623         OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1624     }
1625
1626     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState OUT");
1627     return OC_STACK_OK;
1628 }
1629
1630 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1631 {
1632     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode IN");
1633
1634     g_ESEasySetupResource.lastErrCode = esErrCode;
1635     OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee ErrorCode : %d", g_ESEasySetupResource.lastErrCode);
1636
1637     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1638     {
1639         OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1640     }
1641
1642     OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1643     return OC_STACK_OK;
1644 }
1645
1646 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1647 {
1648     if( !(ehRequest->query) ||
1649                 (ehRequest->query &&
1650                 (strcmp(ehRequest->query, "") && !CompareResourceInterface(ehRequest->query,
1651                                                                         OC_RSRVD_INTERFACE_DEFAULT))))
1652     {
1653         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1654         return OC_EH_BAD_REQ;
1655     }
1656     return OC_EH_OK;
1657 }
1658
1659 const char *getResult(OCStackResult result)
1660 {
1661     switch (result)
1662     {
1663         case OC_STACK_OK:
1664             return "OC_STACK_OK";
1665         case OC_STACK_INVALID_URI:
1666             return "OC_STACK_INVALID_URI";
1667         case OC_STACK_INVALID_QUERY:
1668             return "OC_STACK_INVALID_QUERY";
1669         case OC_STACK_INVALID_IP:
1670             return "OC_STACK_INVALID_IP";
1671         case OC_STACK_INVALID_PORT:
1672             return "OC_STACK_INVALID_PORT";
1673         case OC_STACK_INVALID_CALLBACK:
1674             return "OC_STACK_INVALID_CALLBACK";
1675         case OC_STACK_INVALID_METHOD:
1676             return "OC_STACK_INVALID_METHOD";
1677         case OC_STACK_NO_MEMORY:
1678             return "OC_STACK_NO_MEMORY";
1679         case OC_STACK_COMM_ERROR:
1680             return "OC_STACK_COMM_ERROR";
1681         case OC_STACK_INVALID_PARAM:
1682             return "OC_STACK_INVALID_PARAM";
1683         case OC_STACK_NOTIMPL:
1684             return "OC_STACK_NOTIMPL";
1685         case OC_STACK_NO_RESOURCE:
1686             return "OC_STACK_NO_RESOURCE";
1687         case OC_STACK_RESOURCE_ERROR:
1688             return "OC_STACK_RESOURCE_ERROR";
1689         case OC_STACK_SLOW_RESOURCE:
1690             return "OC_STACK_SLOW_RESOURCE";
1691         case OC_STACK_NO_OBSERVERS:
1692             return "OC_STACK_NO_OBSERVERS";
1693         case OC_STACK_ERROR:
1694             return "OC_STACK_ERROR";
1695         default:
1696             return "UNKNOWN";
1697     }
1698 }