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