Fix a logic to copy userdata in ESReadUserdataCb
[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 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
68 {
69     if(!readCb && !writeCb)
70     {
71         OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
72         return ES_ERROR;
73     }
74     gReadUserdataCb = readCb;
75     gWriteUserdataCb = writeCb;
76     return ES_OK;
77 }
78
79 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
80 {
81     gWifiRsrcEvtCb = cb;
82 }
83
84 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
85 {
86     gCloudRsrcEvtCb = cb;
87 }
88
89 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
90 {
91     gDevConfRsrcEvtCb = cb;
92 }
93
94 void UnRegisterResourceEventCallBack()
95 {
96     if (gWifiRsrcEvtCb)
97     {
98         gWifiRsrcEvtCb = NULL;
99     }
100     if (gCloudRsrcEvtCb)
101     {
102         gCloudRsrcEvtCb = NULL;
103     }
104     if (gDevConfRsrcEvtCb)
105     {
106         gDevConfRsrcEvtCb = NULL;
107     }
108 }
109
110 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
111 {
112     if (name != NULL && pass != NULL)
113     {
114         OICStrcpy(name, MAX_SSIDLEN, gWiFiResource.ssid);
115         OICStrcpy(pass, MAX_CREDLEN, gWiFiResource.cred);
116     }
117 }
118
119 OCStackResult initProvResource(bool isSecured)
120 {
121     gProvResource.status = ES_STATE_INIT;
122     gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
123     OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
124
125     OCStackResult res = OC_STACK_ERROR;
126     if (isSecured)
127     {
128         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
129         OC_RSRVD_INTERFACE_DEFAULT,
130         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
131         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
132     }else
133     {
134         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
135         OC_RSRVD_INTERFACE_DEFAULT,
136         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
137         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
138     }
139     if(res)
140     {
141         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
142         return res;
143     }
144
145     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
146     if(res)
147     {
148         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
149         return res;
150     }
151     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
152     if(res)
153     {
154         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
155         return res;
156     }
157
158     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
159     return res;
160 }
161
162 OCStackResult initWiFiResource(bool isSecured)
163 {
164     OCStackResult res = OC_STACK_ERROR;
165
166     gWiFiResource.supportedFreq = WIFI_BOTH;
167     gWiFiResource.supportedMode[0] = WIFI_11A;
168     gWiFiResource.supportedMode[1] = WIFI_11B;
169     gWiFiResource.supportedMode[2] = WIFI_11G;
170     gWiFiResource.supportedMode[3] = WIFI_11N;
171     gWiFiResource.numMode = 4;
172     gWiFiResource.authType = NONE_AUTH;
173     gWiFiResource.encType = NONE_ENC;
174     OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
175     OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
176
177     if (isSecured)
178     {
179         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
180         OC_RSRVD_INTERFACE_DEFAULT,
181         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
182         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
183     }else
184     {
185         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
186         OC_RSRVD_INTERFACE_DEFAULT,
187         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
188         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
189     }
190
191     OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
192     return res;
193
194 }
195
196 OCStackResult initCloudServerResource(bool isSecured)
197 {
198     OCStackResult res = OC_STACK_ERROR;
199
200     OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
201     OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
202     OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
203
204     if (isSecured)
205     {
206         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
207         OC_RSRVD_INTERFACE_DEFAULT,
208         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
209         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
210     }else
211     {
212         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
213         OC_RSRVD_INTERFACE_DEFAULT,
214         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
215         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
216     }
217
218     OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
219     return res;
220
221 }
222
223 OCStackResult initDevConfResource(bool isSecured)
224 {
225     OCStackResult res = OC_STACK_ERROR;
226
227     OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
228     OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
229     OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
230
231     if (isSecured)
232     {
233         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
234         OC_RSRVD_INTERFACE_DEFAULT,
235         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
236         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
237     }else
238     {
239         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
240         OC_RSRVD_INTERFACE_DEFAULT,
241         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
242         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
243     }
244
245     OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
246     return res;
247
248 }
249
250 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
251 {
252     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
253
254     if(ehRequest->query)
255     {
256         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
257         {
258         // When Provisioning resource has a POST with BatchInterface
259             updateCloudResource(input);
260             updateWiFiResource(input);
261             updateDevConfResource(input);
262         }
263     }
264 }
265
266 void updateWiFiResource(OCRepPayload* input)
267 {
268     ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
269     if(wiFiData == NULL)
270     {
271         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
272     }
273     wiFiData->userdata = NULL;
274
275     char* ssid = NULL;
276     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
277     {
278         OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
279         OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
280         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
281     }
282
283     char* cred = NULL;
284     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
285     {
286         OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
287         OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
288         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
289     }
290
291     int64_t authType = -1;
292     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
293     {
294         gWiFiResource.authType = authType;
295         wiFiData->authtype = gWiFiResource.authType;
296         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
297     }
298
299     int64_t encType = -1;
300     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
301     {
302         gWiFiResource.encType = encType;
303         wiFiData->enctype = gWiFiResource.encType;
304         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
305     }
306
307     if(gReadUserdataCb)
308     {
309         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
310     }
311
312     if(ssid || cred || authType!= -1 || encType != -1)
313     {
314         OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
315
316         // TODO : Need to check appropriateness of gWiFiData
317         if(gWifiRsrcEvtCb != NULL)
318         {
319             gWifiRsrcEvtCb(ES_OK, wiFiData);
320         }
321         else
322         {
323             OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
324         }
325     }
326
327     OICFree(wiFiData);
328 }
329
330 void updateCloudResource(OCRepPayload* input)
331 {
332     ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
333     if(cloudData == NULL)
334     {
335         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
336     }
337     cloudData->userdata = NULL;
338
339     char *authCode = NULL;
340     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
341     {
342         OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
343         OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
344         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
345     }
346
347     char *authProvider = NULL;
348     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
349     {
350         OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
351         OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
352         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
353     }
354
355     char *ciServer = NULL;
356     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
357     {
358         OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
359         OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
360         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
361     }
362
363     if(gReadUserdataCb)
364     {
365         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
366     }
367
368     if(authCode || authProvider || ciServer)
369     {
370         OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
371
372         // TODO : Need to check appropriateness of gCloudData
373         if(gCloudRsrcEvtCb != NULL)
374         {
375             gCloudRsrcEvtCb(ES_OK, cloudData);
376         }
377         else
378         {
379             OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
380         }
381     }
382
383     OICFree(cloudData);
384 }
385
386 void updateDevConfResource(OCRepPayload* input)
387 {
388     ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
389     if(devConfData == NULL)
390     {
391         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
392     }
393     devConfData->userdata = NULL;
394
395     char *country = NULL;
396     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
397     {
398         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
399         OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
400         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
401     }
402
403     char *language = NULL;
404     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
405     {
406         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
407         OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
408         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
409     }
410
411     if(gReadUserdataCb)
412     {
413         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
414     }
415
416     if(country || language)
417     {
418         OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
419
420         // TODO : Need to check appropriateness of gDevConfData
421         if(gDevConfRsrcEvtCb != NULL)
422         {
423             gDevConfRsrcEvtCb(ES_OK, devConfData);
424         }
425         else
426         {
427             OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
428         }
429     }
430
431     OICFree(devConfData);
432 }
433
434 OCRepPayload* constructResponseOfWiFi()
435 {
436     OCRepPayload* payload = OCRepPayloadCreate();
437     if (!payload)
438     {
439         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
440         return NULL;
441     }
442
443     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
444     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
445
446     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
447     int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
448     for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
449         modes_64[i] = gWiFiResource.supportedMode[i];
450     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
451
452     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
453     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
454     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
455     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
456     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
457
458     if(gWriteUserdataCb)
459     {
460         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
461     }
462
463     return payload;
464 }
465
466 OCRepPayload* constructResponseOfCloud()
467 {
468     OCRepPayload* payload = OCRepPayloadCreate();
469     if (!payload)
470     {
471         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
472         return NULL;
473     }
474
475     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
476     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
477     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
478     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
479     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
480
481     if(gWriteUserdataCb)
482     {
483         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
484     }
485
486     return payload;
487 }
488
489 OCRepPayload* constructResponseOfDevConf()
490 {
491     OCRepPayload* payload = OCRepPayloadCreate();
492     if (!payload)
493     {
494         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
495         return NULL;
496     }
497
498     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
499     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
500     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
501     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
502     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
503
504     if(gWriteUserdataCb)
505     {
506         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
507     }
508
509     return payload;
510 }
511
512 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
513 {
514     OCRepPayload* payload = OCRepPayloadCreate();
515     if (!payload)
516     {
517         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
518         return NULL;
519     }
520
521     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
522     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
523     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
524     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
525     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
526
527     if(gWriteUserdataCb)
528     {
529         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
530     }
531
532     if(ehRequest->query)
533     {
534         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
535         {// When Provisioning resource has a GET with BatchInterface
536             payload->next = constructResponseOfWiFi();
537
538             if(payload->next)
539                 payload->next->next = constructResponseOfCloud();
540             else
541                 return payload;
542
543             if(payload->next->next)
544                 payload->next->next->next = constructResponseOfDevConf();
545             else
546                 return payload;
547         }
548     }
549
550     return payload;
551 }
552
553
554 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
555 {
556     OCStackResult res = OC_STACK_ERROR;
557     bool maskFlag = false;
558
559     res = initProvResource(isSecured);
560     if(res != OC_STACK_OK)
561     {
562         // TODO: destroy logic will be added
563         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
564
565         return res;
566     }
567
568     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
569     {
570         maskFlag = true;
571         res = initWiFiResource(isSecured);
572         if(res != OC_STACK_OK)
573         {
574             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
575             return res;
576         }
577
578         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
579         if(res != OC_STACK_OK)
580         {
581             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
582             return res;
583         }
584
585     }
586
587     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
588     {
589         maskFlag = true;
590         res = initCloudServerResource(isSecured);
591         if(res != OC_STACK_OK)
592         {
593             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
594             return res;
595         }
596
597         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
598         if(res != OC_STACK_OK)
599         {
600             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
601             return res;
602         }
603     }
604
605     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
606     {
607         maskFlag = true;
608         res = initDevConfResource(isSecured);
609         if(res != OC_STACK_OK)
610         {
611             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
612             return res;
613         }
614
615         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
616         if(res != OC_STACK_OK)
617         {
618             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
619             return res;
620         }
621     }
622
623
624     if(maskFlag == false)
625     {
626         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
627         return OC_STACK_ERROR;
628
629     }
630
631     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
632
633     return res;
634 }
635
636 OCStackResult DeleteProvisioningResource()
637 {
638     OCStackResult res = OCDeleteResource(gProvResource.handle);
639     if (res != OC_STACK_OK)
640     {
641         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
642     }
643
644     return res;
645 }
646
647 OCStackResult DeleteEasySetupResources()
648 {
649     OCStackResult res = OCDeleteResource(gWiFiResource.handle);
650     if (res != OC_STACK_OK)
651     {
652         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
653     }
654     res = OCDeleteResource(gCloudResource.handle);
655     if (res != OC_STACK_OK)
656     {
657         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
658     }
659     res = OCDeleteResource(gDevConfResource.handle);
660     if (res != OC_STACK_OK)
661     {
662         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
663     }
664
665     res = OCDeleteResource(gProvResource.handle);
666     if (res != OC_STACK_OK)
667     {
668         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
669     }
670
671     return res;
672 }
673
674 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
675 {
676     OCEntityHandlerResult ehResult = OC_EH_ERROR;
677     if (!ehRequest)
678     {
679         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
680         return ehResult;
681     }
682     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
683     {
684         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
685         return ehResult;
686     }
687
688     OCRepPayload *getResp = NULL;
689
690     if(ehRequest->resource == gProvResource.handle)
691     {
692         getResp = constructResponseOfProv(ehRequest);
693     }
694     else if(ehRequest->resource == gWiFiResource.handle)
695     {
696         getResp = constructResponseOfWiFi();
697     }
698     else if(ehRequest->resource == gCloudResource.handle)
699     {
700         getResp = constructResponseOfCloud();
701     }
702     else if(ehRequest->resource == gDevConfResource.handle)
703     {
704         getResp = constructResponseOfDevConf();
705     }
706
707     if (!getResp)
708     {
709         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
710         return OC_EH_ERROR;
711     }
712
713     *payload = getResp;
714     ehResult = OC_EH_OK;
715
716     return ehResult;
717 }
718
719 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
720 {
721     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
722     OCEntityHandlerResult ehResult = OC_EH_ERROR;
723     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
724     {
725         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
726         return ehResult;
727     }
728
729     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
730     if (!input)
731     {
732         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
733         return ehResult;
734     }
735
736     if(ehRequest->resource == gProvResource.handle)
737     {
738         updateProvResource(ehRequest, input);
739     }
740     else if(ehRequest->resource == gWiFiResource.handle)
741     {
742         updateWiFiResource(input);
743     }
744     else if(ehRequest->resource == gCloudResource.handle)
745     {
746         updateCloudResource(input);
747     }
748     else if(ehRequest->resource == gDevConfResource.handle)
749     {
750         updateDevConfResource(input);
751     }
752
753     OCRepPayload *getResp = NULL;
754     if(ehRequest->resource == gProvResource.handle)
755     {
756         getResp = constructResponseOfProv(ehRequest);
757     }
758     else if(ehRequest->resource == gWiFiResource.handle)
759     {
760         getResp = constructResponseOfWiFi();
761     }
762     else if(ehRequest->resource == gCloudResource.handle)
763     {
764         getResp = constructResponseOfCloud();
765     }
766     else if(ehRequest->resource == gDevConfResource.handle)
767     {
768         getResp = constructResponseOfDevConf();
769     }
770
771     if (!getResp)
772     {
773         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
774         return OC_EH_ERROR;
775     }
776
777     *payload = getResp;
778     ehResult = OC_EH_OK;
779
780     return ehResult;
781 }
782
783 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
784         OCRepPayload** payload)
785 {
786     (void) ehRequest;
787     (void) payload;
788     OCEntityHandlerResult ehResult = OC_EH_ERROR;
789
790     return ehResult;
791 }
792 /**
793  * This is the entity handler for the registered resource.
794  * This is invoked by OCStack whenever it recevies a request for this resource.
795  */
796 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
797         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
798 {
799     (void) callback;
800     OCEntityHandlerResult ehRet = OC_EH_OK;
801     OCEntityHandlerResponse response =
802     { 0, 0, OC_EH_ERROR, 0, 0,
803     { },
804     { 0 }, false };
805     OCRepPayload* payload = NULL;
806
807     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
808     {
809         if (OC_REST_GET == entityHandlerRequest->method)
810         {
811             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
812             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
813         }
814         else if (OC_REST_PUT == entityHandlerRequest->method)
815         {
816             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
817
818             //PUT request will be handled in the internal implementation
819             if (gProvResource.handle != NULL)
820             {
821                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
822             }
823             else
824             {
825                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
826                 ehRet = OC_EH_ERROR;
827             }
828         }
829         else if (OC_REST_POST == entityHandlerRequest->method)
830         {
831             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
832             if (gProvResource.handle != NULL)
833             {
834                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
835             }
836             else
837             {
838                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
839                 ehRet = OC_EH_ERROR;
840             }
841         }
842
843         if (ehRet == OC_EH_OK)
844         {
845             // Format the response.  Note this requires some info about the request
846             response.requestHandle = entityHandlerRequest->requestHandle;
847             response.resourceHandle = entityHandlerRequest->resource;
848             response.ehResult = ehRet;
849             //response uses OCPaylod while all get,put methodes use OCRepPayload
850             response.payload = (OCPayload*) (payload);
851             response.numSendVendorSpecificHeaderOptions = 0;
852             memset(response.sendVendorSpecificHeaderOptions, 0,
853                     sizeof(response.sendVendorSpecificHeaderOptions));
854             memset(response.resourceUri, 0, sizeof(response.resourceUri));
855             // Indicate that response is NOT in a persistent buffer
856             response.persistentBufferFlag = 0;
857
858             // Send the response
859             if (OCDoResponse(&response) != OC_STACK_OK)
860             {
861                 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
862                 ehRet = OC_EH_ERROR;
863             }
864         }
865     }
866
867     return ehRet;
868 }
869
870 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
871 {
872     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
873
874     gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
875     OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
876
877     int modeIdx = 0;
878     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
879     {
880         gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
881         OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
882         modeIdx ++;
883     }
884     gWiFiResource.numMode = modeIdx;
885
886     OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
887     OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
888
889     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
890     return OC_STACK_OK;
891 }
892
893 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
894 {
895     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
896
897     gProvResource.status = esState;
898     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
899
900     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
901     return OC_STACK_OK;
902 }
903
904 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
905 {
906     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
907
908     gProvResource.lastErrCode = esErrCode;
909     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
910
911     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
912     return OC_STACK_OK;
913 }
914
915 const char *getResult(OCStackResult result)
916 {
917     switch (result)
918     {
919         case OC_STACK_OK:
920             return "OC_STACK_OK";
921         case OC_STACK_INVALID_URI:
922             return "OC_STACK_INVALID_URI";
923         case OC_STACK_INVALID_QUERY:
924             return "OC_STACK_INVALID_QUERY";
925         case OC_STACK_INVALID_IP:
926             return "OC_STACK_INVALID_IP";
927         case OC_STACK_INVALID_PORT:
928             return "OC_STACK_INVALID_PORT";
929         case OC_STACK_INVALID_CALLBACK:
930             return "OC_STACK_INVALID_CALLBACK";
931         case OC_STACK_INVALID_METHOD:
932             return "OC_STACK_INVALID_METHOD";
933         case OC_STACK_NO_MEMORY:
934             return "OC_STACK_NO_MEMORY";
935         case OC_STACK_COMM_ERROR:
936             return "OC_STACK_COMM_ERROR";
937         case OC_STACK_INVALID_PARAM:
938             return "OC_STACK_INVALID_PARAM";
939         case OC_STACK_NOTIMPL:
940             return "OC_STACK_NOTIMPL";
941         case OC_STACK_NO_RESOURCE:
942             return "OC_STACK_NO_RESOURCE";
943         case OC_STACK_RESOURCE_ERROR:
944             return "OC_STACK_RESOURCE_ERROR";
945         case OC_STACK_SLOW_RESOURCE:
946             return "OC_STACK_SLOW_RESOURCE";
947         case OC_STACK_NO_OBSERVERS:
948             return "OC_STACK_NO_OBSERVERS";
949         case OC_STACK_ERROR:
950             return "OC_STACK_ERROR";
951         default:
952             return "UNKNOWN";
953     }
954 }