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