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