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