Fix defects detected by static analysis
[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             }
1000
1001             payload = head;
1002         }
1003     }
1004
1005     return payload;
1006 }
1007
1008
1009 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
1010 {
1011     OCStackResult res = OC_STACK_ERROR;
1012     bool maskFlag = false;
1013
1014     res = initProvResource(isSecured);
1015     if(res != OC_STACK_OK)
1016     {
1017         // TODO: destroy logic will be added
1018         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
1019
1020         return res;
1021     }
1022
1023     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
1024     {
1025         maskFlag = true;
1026         res = initWiFiResource(isSecured);
1027         if(res != OC_STACK_OK)
1028         {
1029             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
1030             return res;
1031         }
1032
1033         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
1034         if(res != OC_STACK_OK)
1035         {
1036             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
1037             return res;
1038         }
1039
1040     }
1041
1042     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
1043     {
1044         maskFlag = true;
1045         res = initCloudServerResource(isSecured);
1046         if(res != OC_STACK_OK)
1047         {
1048             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
1049             return res;
1050         }
1051
1052         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
1053         if(res != OC_STACK_OK)
1054         {
1055             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
1056             return res;
1057         }
1058     }
1059
1060     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
1061     {
1062         maskFlag = true;
1063         res = initDevConfResource(isSecured);
1064         if(res != OC_STACK_OK)
1065         {
1066             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
1067             return res;
1068         }
1069
1070         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
1071         if(res != OC_STACK_OK)
1072         {
1073             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
1074             return res;
1075         }
1076     }
1077
1078
1079     if(maskFlag == false)
1080     {
1081         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
1082         return OC_STACK_ERROR;
1083
1084     }
1085
1086     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
1087
1088     return res;
1089 }
1090
1091 OCStackResult DeleteProvisioningResource()
1092 {
1093     OCStackResult res = OCDeleteResource(gProvResource.handle);
1094     if (res != OC_STACK_OK)
1095     {
1096         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1097     }
1098
1099     return res;
1100 }
1101
1102 OCStackResult DeleteEasySetupResources()
1103 {
1104     OCStackResult res = OC_STACK_ERROR;
1105     if (gWiFiResource.handle != NULL)
1106     {
1107         res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
1108         if(res != OC_STACK_OK)
1109         {
1110             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
1111         }
1112     }
1113     if (gCloudResource.handle != NULL)
1114     {
1115         res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
1116         if(res != OC_STACK_OK)
1117         {
1118             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
1119         }
1120     }
1121     if (gDevConfResource.handle != NULL)
1122     {
1123         res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
1124         if(res != OC_STACK_OK)
1125         {
1126             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
1127         }
1128     }
1129
1130     if (gWiFiResource.handle != NULL)
1131     {
1132         res = OCDeleteResource(gWiFiResource.handle);
1133         if (res != OC_STACK_OK)
1134         {
1135             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
1136         }
1137     }
1138
1139     if(gCloudResource.handle != NULL)
1140     {
1141         res = OCDeleteResource(gCloudResource.handle);
1142         if (res != OC_STACK_OK)
1143         {
1144             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
1145         }
1146     }
1147
1148     if(gDevConfResource.handle != NULL)
1149     {
1150         res = OCDeleteResource(gDevConfResource.handle);
1151         if (res != OC_STACK_OK)
1152         {
1153             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
1154         }
1155     }
1156
1157     if(gProvResource.handle != NULL)
1158     {
1159         res = OCDeleteResource(gProvResource.handle);
1160         if (res != OC_STACK_OK)
1161         {
1162             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1163         }
1164     }
1165
1166     return res;
1167 }
1168
1169 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1170 {
1171     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1172     if (!ehRequest)
1173     {
1174         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1175         return ehResult;
1176     }
1177     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1178     {
1179         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1180         return ehResult;
1181     }
1182
1183     OCRepPayload *getResp = NULL;
1184     *payload = NULL;
1185
1186     if(ehRequest->resource == gProvResource.handle)
1187     {
1188         if(ehRequest->query &&
1189             strcmp(ehRequest->query, "") &&
1190             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1191             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1192             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1193         {
1194             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1195             return OC_EH_BAD_REQ;
1196         }
1197         else
1198         {
1199             getResp = constructResponseOfProv(ehRequest);
1200         }
1201     }
1202     else if(ehRequest->resource == gWiFiResource.handle)
1203     {
1204         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1205         {
1206             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1207             return OC_EH_BAD_REQ;
1208         }
1209         else
1210         {
1211             getResp = constructResponseOfWiFi(OC_RSRVD_INTERFACE_DEFAULT);
1212         }
1213     }
1214     else if(ehRequest->resource == gCloudResource.handle)
1215     {
1216         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1217         {
1218             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1219             return OC_EH_BAD_REQ;
1220         }
1221         else
1222         {
1223             getResp = constructResponseOfCloud(OC_RSRVD_INTERFACE_DEFAULT);
1224         }
1225     }
1226     else if(ehRequest->resource == gDevConfResource.handle)
1227     {
1228         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1229         {
1230             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1231             return OC_EH_BAD_REQ;
1232         }
1233         else
1234         {
1235             getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1236         }
1237     }
1238
1239     if (!getResp)
1240     {
1241         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1242         return OC_EH_ERROR;
1243     }
1244
1245     *payload = getResp;
1246     ehResult = OC_EH_OK;
1247
1248     return ehResult;
1249 }
1250
1251 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1252 {
1253     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
1254     OCEntityHandlerResult ehResult = OC_EH_ERROR;
1255     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1256     {
1257         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1258         return ehResult;
1259     }
1260
1261     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1262     if (!input)
1263     {
1264         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1265         return ehResult;
1266     }
1267
1268     if(ehRequest->resource == gProvResource.handle)
1269     {
1270         if(ehRequest->query &&
1271             strcmp(ehRequest->query, "") &&
1272             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1273             !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1274         {
1275             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1276             return OC_EH_BAD_REQ;
1277         }
1278         else
1279         {
1280             updateProvResource(ehRequest, input);
1281         }
1282     }
1283     else if(ehRequest->resource == gWiFiResource.handle)
1284     {
1285         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1286         {
1287             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1288             return OC_EH_BAD_REQ;
1289         }
1290         else
1291         {
1292             updateWiFiResource(input);
1293         }
1294     }
1295     else if(ehRequest->resource == gCloudResource.handle)
1296     {
1297         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1298         {
1299             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1300             return OC_EH_BAD_REQ;
1301         }
1302         else
1303         {
1304             updateCloudResource(input);
1305         }
1306     }
1307     else if(ehRequest->resource == gDevConfResource.handle)
1308     {
1309         if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1310         {
1311             OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1312             return OC_EH_BAD_REQ;
1313         }
1314         else
1315         {
1316             updateDevConfResource(input);
1317         }
1318     }
1319
1320     OCRepPayload *getResp = NULL;
1321     if(ehRequest->resource == gProvResource.handle)
1322     {
1323         getResp = constructResponseOfProv(ehRequest);
1324     }
1325     else if(ehRequest->resource == gWiFiResource.handle)
1326     {
1327         getResp = constructResponseOfWiFi(OC_RSRVD_INTERFACE_DEFAULT);
1328     }
1329     else if(ehRequest->resource == gCloudResource.handle)
1330     {
1331         getResp = constructResponseOfCloud(OC_RSRVD_INTERFACE_DEFAULT);
1332     }
1333     else if(ehRequest->resource == gDevConfResource.handle)
1334     {
1335         getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1336     }
1337
1338     if (!getResp)
1339     {
1340         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1341         return OC_EH_ERROR;
1342     }
1343
1344     *payload = getResp;
1345     ehResult = OC_EH_OK;
1346
1347     return ehResult;
1348 }
1349
1350 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1351         OCRepPayload** payload)
1352 {
1353     (void) ehRequest;
1354     (void) payload;
1355     OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1356
1357     return ehResult;
1358 }
1359 /**
1360  * This is the entity handler for the registered resource.
1361  * This is invoked by OCStack whenever it recevies a request for this resource.
1362  */
1363 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1364         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1365 {
1366     (void) callback;
1367     OCEntityHandlerResult ehRet = OC_EH_OK;
1368     OCEntityHandlerResponse response =
1369     { 0, 0, OC_EH_ERROR, 0, 0,
1370     { },
1371     { 0 }, false };
1372     OCRepPayload* payload = NULL;
1373
1374     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1375     {
1376         if (OC_REST_GET == entityHandlerRequest->method)
1377         {
1378             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1379             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1380         }
1381         else if (OC_REST_PUT == entityHandlerRequest->method)
1382         {
1383             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1384
1385             //PUT request will be handled in the internal implementation
1386             if (gProvResource.handle != NULL)
1387             {
1388                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1389             }
1390             else
1391             {
1392                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1393                 ehRet = OC_EH_ERROR;
1394             }
1395         }
1396         else if (OC_REST_POST == entityHandlerRequest->method)
1397         {
1398             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1399             if (gProvResource.handle != NULL)
1400             {
1401                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1402             }
1403             else
1404             {
1405                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1406                 ehRet = OC_EH_ERROR;
1407             }
1408         }
1409
1410         // Format the response.  Note this requires some info about the request
1411         response.requestHandle = entityHandlerRequest->requestHandle;
1412         response.resourceHandle = entityHandlerRequest->resource;
1413         response.ehResult = ehRet;
1414         //response uses OCPaylod while all get,put methodes use OCRepPayload
1415         response.payload = (OCPayload*) (payload);
1416         response.numSendVendorSpecificHeaderOptions = 0;
1417         memset(response.sendVendorSpecificHeaderOptions, 0,
1418                 sizeof(response.sendVendorSpecificHeaderOptions));
1419         memset(response.resourceUri, 0, sizeof(response.resourceUri));
1420         // Indicate that response is NOT in a persistent buffer
1421         response.persistentBufferFlag = 0;
1422
1423         // Send the response
1424         if (OCDoResponse(&response) != OC_STACK_OK)
1425         {
1426             OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1427             ehRet = OC_EH_ERROR;
1428         }
1429     }
1430     if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
1431     {
1432         OIC_LOG(INFO, ES_RH_TAG, "Flag includes OC_OBSERVE_FLAG");
1433
1434         if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
1435         {
1436             OIC_LOG (INFO, ES_RH_TAG, "Received OC_OBSERVE_REGISTER from Mediator");
1437         }
1438         else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
1439         {
1440             OIC_LOG (INFO, ES_RH_TAG, "Received OC_OBSERVE_DEREGISTER from Mediator");
1441         }
1442     }
1443     return ehRet;
1444 }
1445
1446 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1447 {
1448     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
1449
1450     gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
1451     OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
1452
1453     int modeIdx = 0;
1454     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1455     {
1456         gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1457         OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
1458         modeIdx ++;
1459     }
1460     gWiFiResource.numMode = modeIdx;
1461
1462     OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1463     OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
1464
1465     OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
1466                                                             (deviceProperty->DevConf).modelNumber);
1467     OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
1468
1469     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gWiFiResource.handle, OC_HIGH_QOS))
1470     {
1471         OIC_LOG(INFO, ES_RH_TAG, "wifiResource doesn't have any observers.");
1472     }
1473
1474     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gDevConfResource.handle, OC_HIGH_QOS))
1475     {
1476         OIC_LOG(INFO, ES_RH_TAG, "devConfResource doesn't have any observers.");
1477     }
1478
1479     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
1480     return OC_STACK_OK;
1481 }
1482
1483 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1484 {
1485     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
1486
1487     gProvResource.status = esState;
1488     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
1489
1490     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gProvResource.handle, OC_HIGH_QOS))
1491     {
1492         OIC_LOG(INFO, ES_RH_TAG, "provResource doesn't have any observers.");
1493     }
1494
1495     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
1496     return OC_STACK_OK;
1497 }
1498
1499 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1500 {
1501     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
1502
1503     gProvResource.lastErrCode = esErrCode;
1504     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
1505
1506     if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gProvResource.handle, OC_HIGH_QOS))
1507     {
1508         OIC_LOG(INFO, ES_RH_TAG, "provResource doesn't have any observers.");
1509     }
1510
1511     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1512     return OC_STACK_OK;
1513 }
1514
1515 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1516 {
1517     if( !(ehRequest->query) ||
1518                 (ehRequest->query &&
1519                 (strcmp(ehRequest->query, "") && !CompareResourceInterface(ehRequest->query,
1520                                                                         OC_RSRVD_INTERFACE_DEFAULT))))
1521     {
1522         OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1523         return OC_EH_BAD_REQ;
1524     }
1525     return OC_EH_OK;
1526 }
1527
1528 const char *getResult(OCStackResult result)
1529 {
1530     switch (result)
1531     {
1532         case OC_STACK_OK:
1533             return "OC_STACK_OK";
1534         case OC_STACK_INVALID_URI:
1535             return "OC_STACK_INVALID_URI";
1536         case OC_STACK_INVALID_QUERY:
1537             return "OC_STACK_INVALID_QUERY";
1538         case OC_STACK_INVALID_IP:
1539             return "OC_STACK_INVALID_IP";
1540         case OC_STACK_INVALID_PORT:
1541             return "OC_STACK_INVALID_PORT";
1542         case OC_STACK_INVALID_CALLBACK:
1543             return "OC_STACK_INVALID_CALLBACK";
1544         case OC_STACK_INVALID_METHOD:
1545             return "OC_STACK_INVALID_METHOD";
1546         case OC_STACK_NO_MEMORY:
1547             return "OC_STACK_NO_MEMORY";
1548         case OC_STACK_COMM_ERROR:
1549             return "OC_STACK_COMM_ERROR";
1550         case OC_STACK_INVALID_PARAM:
1551             return "OC_STACK_INVALID_PARAM";
1552         case OC_STACK_NOTIMPL:
1553             return "OC_STACK_NOTIMPL";
1554         case OC_STACK_NO_RESOURCE:
1555             return "OC_STACK_NO_RESOURCE";
1556         case OC_STACK_RESOURCE_ERROR:
1557             return "OC_STACK_RESOURCE_ERROR";
1558         case OC_STACK_SLOW_RESOURCE:
1559             return "OC_STACK_SLOW_RESOURCE";
1560         case OC_STACK_NO_OBSERVERS:
1561             return "OC_STACK_NO_OBSERVERS";
1562         case OC_STACK_ERROR:
1563             return "OC_STACK_ERROR";
1564         default:
1565             return "UNKNOWN";
1566     }
1567 }