[Enrollee] Implement ESInitEnrollee() API
[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 <stdio.h>
24
25 #include "ocpayload.h"
26 #include "oic_string.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  * @var gWiFiData
49  * @brief Structure for holding the target information required to
50  * connect to the target network
51  */
52  static ESWiFiProvData gWiFiData;
53
54 /**
55  * @var gDevConfData
56  * @brief Structure for holding the device information
57  */
58  static ESDevConfProvData gDevConfData;
59
60 /**
61  * @var gCloudData
62  * @brief Structure for holding the cloud information required to
63  * connect to CI Server
64  */
65  static ESCloudProvData gCloudData;
66
67
68
69 //-----------------------------------------------------------------------------
70 // Private internal function prototypes
71 //-----------------------------------------------------------------------------
72 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
73         void *callback);
74 const char *getResult(OCStackResult result);
75 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
76 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
77 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
78 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
79 void updateWiFiResource(OCRepPayload* input);
80 void updateCloudResource(OCRepPayload* input);
81 void updateDevConfResource(OCRepPayload* input);
82
83 ESWiFiCB gWifiRsrcEvtCb = NULL;
84 ESCloudCB gCloudRsrcEvtCb = NULL;
85 ESDevConfCB gDevConfRsrcEvtCb = NULL;
86
87 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
88 {
89     gWifiRsrcEvtCb = cb;
90 }
91
92 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
93 {
94     gCloudRsrcEvtCb = cb;
95 }
96
97 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
98 {
99     gDevConfRsrcEvtCb = cb;
100 }
101
102 void UnRegisterResourceEventCallBack()
103 {
104     if (gWifiRsrcEvtCb)
105     {
106         gWifiRsrcEvtCb = NULL;
107     }
108     if (gCloudRsrcEvtCb)
109     {
110         gCloudRsrcEvtCb = NULL;
111     }
112     if (gDevConfRsrcEvtCb)
113     {
114         gDevConfRsrcEvtCb = NULL;
115     }
116 }
117
118 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
119 {
120     if (name != NULL && pass != NULL)
121     {
122         OICStrcpy(name, MAXSSIDLEN, gWiFiResource.ssid);
123         OICStrcpy(pass, MAXNETCREDLEN, gWiFiResource.cred);
124     }
125 }
126
127 OCStackResult initProvResource(bool isSecured)
128 {
129     gProvResource.status = NO_PROVISION;
130     gProvResource.trigger = false;
131
132     OCStackResult res = OC_STACK_ERROR;
133     if (isSecured)
134     {
135         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
136         OC_RSRVD_INTERFACE_DEFAULT,
137         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
138         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
139     }else
140     {
141         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
142         OC_RSRVD_INTERFACE_DEFAULT,
143         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
144         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
145     }
146     if(res)
147     {
148         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
149         return res;
150     }
151
152     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
153     if(res)
154     {
155         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
156         return res;
157     }
158     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
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     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
166     return res;
167 }
168
169 OCStackResult initWiFiResource(bool isSecured)
170 {
171     OCStackResult res = OC_STACK_ERROR;
172
173     gWiFiResource.supportedFreq = WiFi_BOTH;
174     gWiFiResource.supportedMode[0] = WiFi_11A;
175     gWiFiResource.supportedMode[1] = WiFi_11B;
176     gWiFiResource.supportedMode[2] = WiFi_11G;
177     gWiFiResource.supportedMode[3] = WiFi_11N;
178     gWiFiResource.numMode = 4;
179     gWiFiResource.authType = NONE_AUTH;
180     gWiFiResource.encType = NONE_ENC;
181     OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
182     OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
183
184     if (isSecured)
185     {
186         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
187         OC_RSRVD_INTERFACE_DEFAULT,
188         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
189         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
190     }else
191     {
192         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
193         OC_RSRVD_INTERFACE_DEFAULT,
194         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
195         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
196     }
197
198     OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
199     return res;
200
201 }
202
203 OCStackResult initCloudServerResource(bool isSecured)
204 {
205     OCStackResult res = OC_STACK_ERROR;
206
207     OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
208     OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
209     OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
210
211     if (isSecured)
212     {
213         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
214         OC_RSRVD_INTERFACE_DEFAULT,
215         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
216         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
217     }else
218     {
219         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
220         OC_RSRVD_INTERFACE_DEFAULT,
221         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
222         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
223     }
224
225     OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
226     return res;
227
228 }
229
230 OCStackResult initDevConfResource(bool isSecured)
231 {
232     OCStackResult res = OC_STACK_ERROR;
233
234     OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
235     OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
236     OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
237
238     if (isSecured)
239     {
240         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
241         OC_RSRVD_INTERFACE_DEFAULT,
242         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
243         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
244     }else
245     {
246         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
247         OC_RSRVD_INTERFACE_DEFAULT,
248         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
249         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
250     }
251
252     OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
253     return res;
254
255 }
256
257 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
258 {
259     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %lld", gProvResource.status);
260
261     if(ehRequest->query)
262     {
263         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
264         {
265         // When Provisioning resource has a POST with BatchInterface
266             updateCloudResource(input);
267             updateWiFiResource(input);
268             updateDevConfResource(input);
269         }
270     }
271 }
272
273 void updateWiFiResource(OCRepPayload* input)
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(gWiFiData.ssid, sizeof(gWiFiData.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(gWiFiData.pwd, sizeof(gWiFiData.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         gWiFiData.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         gWiFiData.enctype = gWiFiResource.encType;
304         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
305     }
306
307     if(ssid || cred || authType!= -1 || encType != -1)
308     {
309         OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
310
311         // TODO : Need to check appropriateness of gWiFiData
312         if(gWifiRsrcEvtCb != NULL)
313         {
314             gWifiRsrcEvtCb(ES_OK, &gWiFiData);
315         }
316         else
317         {
318             OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
319         }
320     }
321
322 }
323
324 void updateCloudResource(OCRepPayload* input)
325 {
326     char *authCode = NULL;
327     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
328     {
329         OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
330         OICStrcpy(gCloudData.authCode, sizeof(gCloudData.authCode), authCode);
331         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
332     }
333
334     char *authProvider = NULL;
335     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
336     {
337         OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
338         OICStrcpy(gCloudData.authProvider, sizeof(gCloudData.authProvider), authProvider);
339         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
340     }
341
342     char *ciServer = NULL;
343     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
344     {
345         OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
346         OICStrcpy(gCloudData.ciServer, sizeof(gCloudData.ciServer), ciServer);
347         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
348     }
349
350     if(authCode || authProvider || ciServer)
351     {
352         OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
353
354         // TODO : Need to check appropriateness of gCloudData
355         if(gCloudRsrcEvtCb != NULL)
356         {
357             gCloudRsrcEvtCb(ES_OK, &gCloudData);
358         }
359         else
360         {
361             OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
362         }
363     }
364 }
365
366 void updateDevConfResource(OCRepPayload* input)
367 {
368     char *country = NULL;
369     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &country))
370     {
371         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
372         OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
373         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
374     }
375
376     char *language = NULL;
377     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &language))
378     {
379         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
380         OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
381         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
382     }
383
384     if(country || language)
385     {
386         OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
387
388         // TODO : Need to check appropriateness of gDevConfData
389         if(gDevConfRsrcEvtCb != NULL)
390         {
391             gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
392         }
393         else
394         {
395             OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
396         }
397     }
398 }
399
400 OCRepPayload* constructResponseOfWiFi()
401 {
402     OCRepPayload* payload = OCRepPayloadCreate();
403     if (!payload)
404     {
405         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
406         return NULL;
407     }
408
409     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
410     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
411
412     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
413     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)gWiFiResource.supportedMode, dimensions);
414
415     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
416     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
417     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
418     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
419     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
420
421     printf("%s\n", gWiFiResource.ssid);
422
423     return payload;
424 }
425
426 OCRepPayload* constructResponseOfCloud()
427 {
428     OCRepPayload* payload = OCRepPayloadCreate();
429     if (!payload)
430     {
431         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
432         return NULL;
433     }
434
435     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
436     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
437     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
438     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
439     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
440
441     return payload;
442 }
443
444 OCRepPayload* constructResponseOfDevConf()
445 {
446     OCRepPayload* payload = OCRepPayloadCreate();
447     if (!payload)
448     {
449         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
450         return NULL;
451     }
452
453     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
454     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
455     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
456     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
457     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
458
459     return payload;
460 }
461
462 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
463 {
464     OCRepPayload* payload = OCRepPayloadCreate();
465     if (!payload)
466     {
467         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
468         return NULL;
469     }
470
471     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
472     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
473     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
474     OCRepPayloadSetPropBool(payload, OC_RSRVD_ES_TRIGGER, gProvResource.trigger);
475
476     if(ehRequest->query)
477     {
478         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
479         {// When Provisioning resource has a GET with BatchInterface
480             payload->next = constructResponseOfWiFi();
481
482             if(payload->next)
483                 payload->next->next = constructResponseOfCloud();
484             else
485                 return payload;
486
487             if(payload->next->next)
488                 payload->next->next->next = constructResponseOfDevConf();
489             else
490                 return payload;
491         }
492     }
493
494     return payload;
495 }
496
497
498 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
499 {
500     OCStackResult res = OC_STACK_ERROR;
501     bool maskFlag = false;
502
503     res = initProvResource(isSecured);
504     if(res != OC_STACK_OK)
505     {
506         // TODO: destroy logic will be added
507         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
508
509         return res;
510     }
511
512     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
513     {
514         maskFlag = true;
515         res = initWiFiResource(isSecured);
516         if(res != OC_STACK_OK)
517         {
518             // TODO: destroy logic will be added
519             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
520
521             return res;
522         }
523
524         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
525         if(res != OC_STACK_OK)
526         {
527             // TODO : Error Handling
528             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
529
530             return res;
531         }
532
533     }
534
535     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
536     {
537         maskFlag = true;
538         res = initCloudServerResource(isSecured);
539         if(res != OC_STACK_OK)
540         {
541             // TODO: destroy logic will be added
542             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
543
544             return res;
545         }
546
547         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
548         if(res != OC_STACK_OK)
549         {
550             // TODO : Error Handling
551             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
552
553             return res;
554         }
555     }
556
557     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
558     {
559         maskFlag = true;
560         res = initDevConfResource(isSecured);
561         if(res != OC_STACK_OK)
562         {
563             // TODO: destroy logic will be added
564             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
565
566             return res;
567         }
568
569         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
570         if(res != OC_STACK_OK)
571         {
572             // TODO : Error Handling
573             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
574
575             return res;
576         }
577     }
578
579
580     if(maskFlag == false)
581     {
582         // TODO: destroy logic will be added
583         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
584         return OC_STACK_ERROR;
585
586     }
587     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
588     return res;
589 }
590
591 OCStackResult DeleteProvisioningResource()
592 {
593     OCStackResult res = OCDeleteResource(gProvResource.handle);
594     if (res != OC_STACK_OK)
595     {
596         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
597     }
598
599     return res;
600 }
601
602 OCStackResult DeleteEasySetupResources()
603 {
604     OCStackResult res = OCDeleteResource(gProvResource.handle);
605     if (res != OC_STACK_OK)
606     {
607         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
608     }
609     res = OCDeleteResource(gWiFiResource.handle);
610     if (res != OC_STACK_OK)
611     {
612         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
613     }
614     res = OCDeleteResource(gCloudResource.handle);
615     if (res != OC_STACK_OK)
616     {
617         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
618     }
619     res = OCDeleteResource(gDevConfResource.handle);
620     if (res != OC_STACK_OK)
621     {
622         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
623     }
624
625     return res;
626 }
627
628 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
629 {
630     OCEntityHandlerResult ehResult = OC_EH_ERROR;
631     if (!ehRequest)
632     {
633         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
634         return ehResult;
635     }
636     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
637     {
638         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
639         return ehResult;
640     }
641
642     OCRepPayload *getResp = NULL;
643
644     if(ehRequest->resource == gProvResource.handle)
645         getResp = constructResponseOfProv(ehRequest);
646     else if(ehRequest->resource == gWiFiResource.handle)
647         getResp = constructResponseOfWiFi();
648     else if(ehRequest->resource == gCloudResource.handle)
649         getResp = constructResponseOfCloud();
650     else if(ehRequest->resource == gDevConfResource.handle)
651         getResp = constructResponseOfDevConf();
652
653     if (!getResp)
654     {
655         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
656         return OC_EH_ERROR;
657     }
658
659     *payload = getResp;
660     ehResult = OC_EH_OK;
661
662     return ehResult;
663 }
664
665 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
666 {
667     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
668     OCEntityHandlerResult ehResult = OC_EH_ERROR;
669     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
670     {
671         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
672         return ehResult;
673     }
674
675     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
676     if (!input)
677     {
678         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
679         return ehResult;
680     }
681
682     if(ehRequest->resource == gProvResource.handle)
683         updateProvResource(ehRequest, input);
684     else if(ehRequest->resource == gWiFiResource.handle)
685         updateWiFiResource(input);
686     else if(ehRequest->resource == gCloudResource.handle)
687         updateCloudResource(input);
688     else if(ehRequest->resource == gDevConfResource.handle)
689         updateDevConfResource(input);
690
691     // TBD : Discuss about triggering flag (to be existed or not)
692     // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
693     // A new request for provisioning means overriding existing network provisioning information.
694     // if (gProvResource.trigger)
695     // {
696     //     OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
697     //             "Tiggering the network connection");
698
699     //     if (gNetworkInfoProvEventCb)
700     //     {
701     //         gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
702     //         ehResult = OC_EH_OK;
703     //     }
704     //     else
705     //     {
706     //         OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
707     //                 "Network handler not registered. Failed to connect to the network");
708     //         ehResult = OC_EH_ERROR;
709     //         return ehResult;
710     //     }
711     // }
712     // else
713     // {
714     //     OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
715     // }
716
717     OCRepPayload *getResp = NULL;
718     if(ehRequest->resource == gProvResource.handle)
719         getResp = constructResponseOfProv(ehRequest);
720     else if(ehRequest->resource == gWiFiResource.handle)
721         getResp = constructResponseOfWiFi();
722     else if(ehRequest->resource == gCloudResource.handle)
723         getResp = constructResponseOfCloud();
724     else if(ehRequest->resource == gDevConfResource.handle)
725         getResp = constructResponseOfDevConf();
726
727     if (gProvResource.trigger)// Trigger false should be restored after executed
728         gProvResource.trigger = false;
729
730     if (!getResp)
731     {
732         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
733         return OC_EH_ERROR;
734     }
735
736     *payload = getResp;
737     ehResult = OC_EH_OK;
738
739     return ehResult;
740 }
741
742 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
743         OCRepPayload** payload)
744 {
745     (void) ehRequest;
746     (void) payload;
747     OCEntityHandlerResult ehResult = OC_EH_ERROR;
748
749     return ehResult;
750 }
751 /**
752  * This is the entity handler for the registered resource.
753  * This is invoked by OCStack whenever it recevies a request for this resource.
754  */
755 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
756         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
757 {
758     (void) callback;
759     OCEntityHandlerResult ehRet = OC_EH_OK;
760     OCEntityHandlerResponse response =
761     { 0, 0, OC_EH_ERROR, 0, 0,
762     { },
763     { 0 }, false };
764     OCRepPayload* payload = NULL;
765
766     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
767     {
768         if (OC_REST_GET == entityHandlerRequest->method)
769         {
770             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
771             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
772         }
773         else if (OC_REST_PUT == entityHandlerRequest->method)
774         {
775             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
776
777             //PUT request will be handled in the internal implementation
778             if (gProvResource.handle != NULL)
779             {
780                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
781             }
782             else
783             {
784                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
785                 ehRet = OC_EH_ERROR;
786             }
787         }
788         else if (OC_REST_POST == entityHandlerRequest->method)
789         {
790             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
791             if (gProvResource.handle != NULL)
792             {
793                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
794             }
795             else
796             {
797                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
798                 ehRet = OC_EH_ERROR;
799             }
800         }
801
802         if (ehRet == OC_EH_OK)
803         {
804             // Format the response.  Note this requires some info about the request
805             response.requestHandle = entityHandlerRequest->requestHandle;
806             response.resourceHandle = entityHandlerRequest->resource;
807             response.ehResult = ehRet;
808             //response uses OCPaylod while all get,put methodes use OCRepPayload
809             response.payload = (OCPayload*) (payload);
810             response.numSendVendorSpecificHeaderOptions = 0;
811             memset(response.sendVendorSpecificHeaderOptions, 0,
812                     sizeof(response.sendVendorSpecificHeaderOptions));
813             memset(response.resourceUri, 0, sizeof(response.resourceUri));
814             // Indicate that response is NOT in a persistent buffer
815             response.persistentBufferFlag = 0;
816
817             // Send the response
818             if (OCDoResponse(&response) != OC_STACK_OK)
819             {
820                 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
821                 ehRet = OC_EH_ERROR;
822             }
823         }
824     }
825
826     return ehRet;
827 }
828
829 const char *getResult(OCStackResult result)
830 {
831     switch (result)
832     {
833         case OC_STACK_OK:
834             return "OC_STACK_OK";
835         case OC_STACK_INVALID_URI:
836             return "OC_STACK_INVALID_URI";
837         case OC_STACK_INVALID_QUERY:
838             return "OC_STACK_INVALID_QUERY";
839         case OC_STACK_INVALID_IP:
840             return "OC_STACK_INVALID_IP";
841         case OC_STACK_INVALID_PORT:
842             return "OC_STACK_INVALID_PORT";
843         case OC_STACK_INVALID_CALLBACK:
844             return "OC_STACK_INVALID_CALLBACK";
845         case OC_STACK_INVALID_METHOD:
846             return "OC_STACK_INVALID_METHOD";
847         case OC_STACK_NO_MEMORY:
848             return "OC_STACK_NO_MEMORY";
849         case OC_STACK_COMM_ERROR:
850             return "OC_STACK_COMM_ERROR";
851         case OC_STACK_INVALID_PARAM:
852             return "OC_STACK_INVALID_PARAM";
853         case OC_STACK_NOTIMPL:
854             return "OC_STACK_NOTIMPL";
855         case OC_STACK_NO_RESOURCE:
856             return "OC_STACK_NO_RESOURCE";
857         case OC_STACK_RESOURCE_ERROR:
858             return "OC_STACK_RESOURCE_ERROR";
859         case OC_STACK_SLOW_RESOURCE:
860             return "OC_STACK_SLOW_RESOURCE";
861         case OC_STACK_NO_OBSERVERS:
862             return "OC_STACK_NO_OBSERVERS";
863         case OC_STACK_ERROR:
864             return "OC_STACK_ERROR";
865         default:
866             return "UNKNOWN";
867     }
868 }
869