Merge branch 'master' into extended-easysetup
[platform/upstream/iotivity.git] / service / easy-setup / enrollee / src / resourcehandler.c
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "resourcehandler.h"
22
23 #include <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, MAX_SSIDLEN, gWiFiResource.ssid);
123         OICStrcpy(pass, MAX_CREDLEN, gWiFiResource.cred);
124     }
125 }
126
127 OCStackResult initProvResource(bool isSecured)
128 {
129     gProvResource.status = NO_PROVISION;
130     gProvResource.trigger = false;
131     gProvResource.lastErrCode = ES_ERRCODE_NONE;
132     OICStrcpy(gProvResource.errorMessage, MAX_ERRMSGLEN, "");
133     OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
134
135     OCStackResult res = OC_STACK_ERROR;
136     if (isSecured)
137     {
138         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
139         OC_RSRVD_INTERFACE_DEFAULT,
140         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
141         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
142     }else
143     {
144         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
145         OC_RSRVD_INTERFACE_DEFAULT,
146         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
147         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
148     }
149     if(res)
150     {
151         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
152         return res;
153     }
154
155     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
156     if(res)
157     {
158         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
159         return res;
160     }
161     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
162     if(res)
163     {
164         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
165         return res;
166     }
167
168     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
169     return res;
170 }
171
172 OCStackResult initWiFiResource(bool isSecured)
173 {
174     OCStackResult res = OC_STACK_ERROR;
175
176     gWiFiResource.supportedFreq = WiFi_BOTH;
177     gWiFiResource.supportedMode[0] = WiFi_11A;
178     gWiFiResource.supportedMode[1] = WiFi_11B;
179     gWiFiResource.supportedMode[2] = WiFi_11G;
180     gWiFiResource.supportedMode[3] = WiFi_11N;
181     gWiFiResource.numMode = 4;
182     gWiFiResource.authType = NONE_AUTH;
183     gWiFiResource.encType = NONE_ENC;
184     OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
185     OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
186
187     if (isSecured)
188     {
189         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
190         OC_RSRVD_INTERFACE_DEFAULT,
191         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
192         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
193     }else
194     {
195         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
196         OC_RSRVD_INTERFACE_DEFAULT,
197         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
198         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
199     }
200
201     OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
202     return res;
203
204 }
205
206 OCStackResult initCloudServerResource(bool isSecured)
207 {
208     OCStackResult res = OC_STACK_ERROR;
209
210     OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
211     OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
212     OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
213
214     if (isSecured)
215     {
216         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
217         OC_RSRVD_INTERFACE_DEFAULT,
218         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
219         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
220     }else
221     {
222         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
223         OC_RSRVD_INTERFACE_DEFAULT,
224         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
225         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
226     }
227
228     OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
229     return res;
230
231 }
232
233 OCStackResult initDevConfResource(bool isSecured)
234 {
235     OCStackResult res = OC_STACK_ERROR;
236
237     OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
238     OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
239     OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
240
241     if (isSecured)
242     {
243         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
244         OC_RSRVD_INTERFACE_DEFAULT,
245         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
246         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
247     }else
248     {
249         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
250         OC_RSRVD_INTERFACE_DEFAULT,
251         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
252         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
253     }
254
255     OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
256     return res;
257
258 }
259
260 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
261 {
262     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
263
264     if(ehRequest->query)
265     {
266         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
267         {
268         // When Provisioning resource has a POST with BatchInterface
269             updateCloudResource(input);
270             updateWiFiResource(input);
271             updateDevConfResource(input);
272         }
273     }
274 }
275
276 void updateWiFiResource(OCRepPayload* input)
277 {
278     char* ssid = NULL;
279     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
280     {
281         OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
282         OICStrcpy(gWiFiData.ssid, sizeof(gWiFiData.ssid), ssid);
283         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
284     }
285
286     char* cred = NULL;
287     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
288     {
289         OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
290         OICStrcpy(gWiFiData.pwd, sizeof(gWiFiData.pwd), cred);
291         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
292     }
293
294     int64_t authType = -1;
295     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
296     {
297         gWiFiResource.authType = authType;
298         gWiFiData.authtype = gWiFiResource.authType;
299         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
300     }
301
302     int64_t encType = -1;
303     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
304     {
305         gWiFiResource.encType = encType;
306         gWiFiData.enctype = gWiFiResource.encType;
307         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
308     }
309
310     if(ssid || cred || authType!= -1 || encType != -1)
311     {
312         OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
313
314         // TODO : Need to check appropriateness of gWiFiData
315         if(gWifiRsrcEvtCb != NULL)
316         {
317             gWifiRsrcEvtCb(ES_OK, &gWiFiData);
318         }
319         else
320         {
321             OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
322         }
323     }
324
325 }
326
327 void updateCloudResource(OCRepPayload* input)
328 {
329     char *authCode = NULL;
330     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
331     {
332         OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
333         OICStrcpy(gCloudData.authCode, sizeof(gCloudData.authCode), authCode);
334         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
335     }
336
337     char *authProvider = NULL;
338     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
339     {
340         OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
341         OICStrcpy(gCloudData.authProvider, sizeof(gCloudData.authProvider), authProvider);
342         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
343     }
344
345     char *ciServer = NULL;
346     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
347     {
348         OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
349         OICStrcpy(gCloudData.ciServer, sizeof(gCloudData.ciServer), ciServer);
350         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
351     }
352
353     if(authCode || authProvider || ciServer)
354     {
355         OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
356
357         // TODO : Need to check appropriateness of gCloudData
358         if(gCloudRsrcEvtCb != NULL)
359         {
360             gCloudRsrcEvtCb(ES_OK, &gCloudData);
361         }
362         else
363         {
364             OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
365         }
366     }
367 }
368
369 void updateDevConfResource(OCRepPayload* input)
370 {
371     char *country = NULL;
372     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &country))
373     {
374         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
375         OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
376         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
377     }
378
379     char *language = NULL;
380     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &language))
381     {
382         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
383         OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
384         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
385     }
386
387     if(country || language)
388     {
389         OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
390
391         // TODO : Need to check appropriateness of gDevConfData
392         if(gDevConfRsrcEvtCb != NULL)
393         {
394             gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
395         }
396         else
397         {
398             OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
399         }
400     }
401 }
402
403 OCRepPayload* constructResponseOfWiFi()
404 {
405     OCRepPayload* payload = OCRepPayloadCreate();
406     if (!payload)
407     {
408         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
409         return NULL;
410     }
411
412     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
413     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
414
415     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
416     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)gWiFiResource.supportedMode, dimensions);
417
418     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
419     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
420     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
421     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
422     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
423
424     printf("%s\n", gWiFiResource.ssid);
425
426     return payload;
427 }
428
429 OCRepPayload* constructResponseOfCloud()
430 {
431     OCRepPayload* payload = OCRepPayloadCreate();
432     if (!payload)
433     {
434         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
435         return NULL;
436     }
437
438     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
439     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
440     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
441     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
442     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
443
444     return payload;
445 }
446
447 OCRepPayload* constructResponseOfDevConf()
448 {
449     OCRepPayload* payload = OCRepPayloadCreate();
450     if (!payload)
451     {
452         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
453         return NULL;
454     }
455
456     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
457     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
458     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
459     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
460     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
461
462     return payload;
463 }
464
465 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
466 {
467     OCRepPayload* payload = OCRepPayloadCreate();
468     if (!payload)
469     {
470         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
471         return NULL;
472     }
473
474     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
475     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
476     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
477     OCRepPayloadSetPropBool(payload, OC_RSRVD_ES_TRIGGER, gProvResource.trigger);
478     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
479     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ERRORMESSAGE, gProvResource.errorMessage);
480     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
481
482     if(ehRequest->query)
483     {
484         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
485         {// When Provisioning resource has a GET with BatchInterface
486             payload->next = constructResponseOfWiFi();
487
488             if(payload->next)
489                 payload->next->next = constructResponseOfCloud();
490             else
491                 return payload;
492
493             if(payload->next->next)
494                 payload->next->next->next = constructResponseOfDevConf();
495             else
496                 return payload;
497         }
498     }
499
500     return payload;
501 }
502
503
504 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
505 {
506     OCStackResult res = OC_STACK_ERROR;
507     bool maskFlag = false;
508
509     res = initProvResource(isSecured);
510     if(res != OC_STACK_OK)
511     {
512         // TODO: destroy logic will be added
513         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
514
515         return res;
516     }
517
518     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
519     {
520         maskFlag = true;
521         res = initWiFiResource(isSecured);
522         if(res != OC_STACK_OK)
523         {
524             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
525             return res;
526         }
527
528         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
529         if(res != OC_STACK_OK)
530         {
531             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
532             return res;
533         }
534
535     }
536
537     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
538     {
539         maskFlag = true;
540         res = initCloudServerResource(isSecured);
541         if(res != OC_STACK_OK)
542         {
543             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
544             return res;
545         }
546
547         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
548         if(res != OC_STACK_OK)
549         {
550             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
551             return res;
552         }
553     }
554
555     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
556     {
557         maskFlag = true;
558         res = initDevConfResource(isSecured);
559         if(res != OC_STACK_OK)
560         {
561             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
562             return res;
563         }
564
565         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
566         if(res != OC_STACK_OK)
567         {
568             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
569             return res;
570         }
571     }
572
573
574     if(maskFlag == false)
575     {
576         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
577         return OC_STACK_ERROR;
578
579     }
580
581     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
582
583     return res;
584 }
585
586 OCStackResult DeleteProvisioningResource()
587 {
588     OCStackResult res = OCDeleteResource(gProvResource.handle);
589     if (res != OC_STACK_OK)
590     {
591         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
592     }
593
594     return res;
595 }
596
597 OCStackResult DeleteEasySetupResources()
598 {
599     OCStackResult res = OCDeleteResource(gWiFiResource.handle);
600     if (res != OC_STACK_OK)
601     {
602         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
603     }
604     res = OCDeleteResource(gCloudResource.handle);
605     if (res != OC_STACK_OK)
606     {
607         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
608     }
609     res = OCDeleteResource(gDevConfResource.handle);
610     if (res != OC_STACK_OK)
611     {
612         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
613     }
614
615     res = OCDeleteResource(gProvResource.handle);
616     if (res != OC_STACK_OK)
617     {
618         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
619     }
620
621     return res;
622 }
623
624 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
625 {
626     OCEntityHandlerResult ehResult = OC_EH_ERROR;
627     if (!ehRequest)
628     {
629         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
630         return ehResult;
631     }
632     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
633     {
634         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
635         return ehResult;
636     }
637
638     OCRepPayload *getResp = NULL;
639
640     if(ehRequest->resource == gProvResource.handle)
641         getResp = constructResponseOfProv(ehRequest);
642     else if(ehRequest->resource == gWiFiResource.handle)
643         getResp = constructResponseOfWiFi();
644     else if(ehRequest->resource == gCloudResource.handle)
645         getResp = constructResponseOfCloud();
646     else if(ehRequest->resource == gDevConfResource.handle)
647         getResp = constructResponseOfDevConf();
648
649     if (!getResp)
650     {
651         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
652         return OC_EH_ERROR;
653     }
654
655     *payload = getResp;
656     ehResult = OC_EH_OK;
657
658     return ehResult;
659 }
660
661 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
662 {
663     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
664     OCEntityHandlerResult ehResult = OC_EH_ERROR;
665     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
666     {
667         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
668         return ehResult;
669     }
670
671     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
672     if (!input)
673     {
674         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
675         return ehResult;
676     }
677
678     if(ehRequest->resource == gProvResource.handle)
679         updateProvResource(ehRequest, input);
680     else if(ehRequest->resource == gWiFiResource.handle)
681         updateWiFiResource(input);
682     else if(ehRequest->resource == gCloudResource.handle)
683         updateCloudResource(input);
684     else if(ehRequest->resource == gDevConfResource.handle)
685         updateDevConfResource(input);
686
687     // TBD : Discuss about triggering flag (to be existed or not)
688     // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
689     // A new request for provisioning means overriding existing network provisioning information.
690     // if (gProvResource.trigger)
691     // {
692     //     OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
693     //             "Tiggering the network connection");
694
695     //     if (gNetworkInfoProvEventCb)
696     //     {
697     //         gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
698     //         ehResult = OC_EH_OK;
699     //     }
700     //     else
701     //     {
702     //         OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
703     //                 "Network handler not registered. Failed to connect to the network");
704     //         ehResult = OC_EH_ERROR;
705     //         return ehResult;
706     //     }
707     // }
708     // else
709     // {
710     //     OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
711     // }
712
713     OCRepPayload *getResp = NULL;
714     if(ehRequest->resource == gProvResource.handle)
715         getResp = constructResponseOfProv(ehRequest);
716     else if(ehRequest->resource == gWiFiResource.handle)
717         getResp = constructResponseOfWiFi();
718     else if(ehRequest->resource == gCloudResource.handle)
719         getResp = constructResponseOfCloud();
720     else if(ehRequest->resource == gDevConfResource.handle)
721         getResp = constructResponseOfDevConf();
722
723     if (gProvResource.trigger)// Trigger false should be restored after executed
724         gProvResource.trigger = false;
725
726     if (!getResp)
727     {
728         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
729         return OC_EH_ERROR;
730     }
731
732     *payload = getResp;
733     ehResult = OC_EH_OK;
734
735     return ehResult;
736 }
737
738 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
739         OCRepPayload** payload)
740 {
741     (void) ehRequest;
742     (void) payload;
743     OCEntityHandlerResult ehResult = OC_EH_ERROR;
744
745     return ehResult;
746 }
747 /**
748  * This is the entity handler for the registered resource.
749  * This is invoked by OCStack whenever it recevies a request for this resource.
750  */
751 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
752         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
753 {
754     (void) callback;
755     OCEntityHandlerResult ehRet = OC_EH_OK;
756     OCEntityHandlerResponse response =
757     { 0, 0, OC_EH_ERROR, 0, 0,
758     { },
759     { 0 }, false };
760     OCRepPayload* payload = NULL;
761
762     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
763     {
764         if (OC_REST_GET == entityHandlerRequest->method)
765         {
766             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
767             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
768         }
769         else if (OC_REST_PUT == entityHandlerRequest->method)
770         {
771             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
772
773             //PUT request will be handled in the internal implementation
774             if (gProvResource.handle != NULL)
775             {
776                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
777             }
778             else
779             {
780                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
781                 ehRet = OC_EH_ERROR;
782             }
783         }
784         else if (OC_REST_POST == entityHandlerRequest->method)
785         {
786             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
787             if (gProvResource.handle != NULL)
788             {
789                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
790             }
791             else
792             {
793                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
794                 ehRet = OC_EH_ERROR;
795             }
796         }
797
798         if (ehRet == OC_EH_OK)
799         {
800             // Format the response.  Note this requires some info about the request
801             response.requestHandle = entityHandlerRequest->requestHandle;
802             response.resourceHandle = entityHandlerRequest->resource;
803             response.ehResult = ehRet;
804             //response uses OCPaylod while all get,put methodes use OCRepPayload
805             response.payload = (OCPayload*) (payload);
806             response.numSendVendorSpecificHeaderOptions = 0;
807             memset(response.sendVendorSpecificHeaderOptions, 0,
808                     sizeof(response.sendVendorSpecificHeaderOptions));
809             memset(response.resourceUri, 0, sizeof(response.resourceUri));
810             // Indicate that response is NOT in a persistent buffer
811             response.persistentBufferFlag = 0;
812
813             // Send the response
814             if (OCDoResponse(&response) != OC_STACK_OK)
815             {
816                 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
817                 ehRet = OC_EH_ERROR;
818             }
819         }
820     }
821
822     return ehRet;
823 }
824
825 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
826 {
827     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
828
829     gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
830     OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
831
832     int modeIdx = 0;
833     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
834     {
835         gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
836         OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
837         modeIdx ++;
838     }
839     gWiFiResource.numMode = modeIdx;
840
841     OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
842     OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
843
844     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
845     return OC_STACK_OK;
846 }
847
848 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
849 {
850     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
851
852     gProvResource.status = esState;
853     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
854
855     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
856     return OC_STACK_OK;
857 }
858
859 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
860 {
861     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
862
863     gProvResource.lastErrCode = esErrCode;
864     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
865
866     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
867     return OC_STACK_OK;
868 }
869 const char *getResult(OCStackResult result)
870 {
871     switch (result)
872     {
873         case OC_STACK_OK:
874             return "OC_STACK_OK";
875         case OC_STACK_INVALID_URI:
876             return "OC_STACK_INVALID_URI";
877         case OC_STACK_INVALID_QUERY:
878             return "OC_STACK_INVALID_QUERY";
879         case OC_STACK_INVALID_IP:
880             return "OC_STACK_INVALID_IP";
881         case OC_STACK_INVALID_PORT:
882             return "OC_STACK_INVALID_PORT";
883         case OC_STACK_INVALID_CALLBACK:
884             return "OC_STACK_INVALID_CALLBACK";
885         case OC_STACK_INVALID_METHOD:
886             return "OC_STACK_INVALID_METHOD";
887         case OC_STACK_NO_MEMORY:
888             return "OC_STACK_NO_MEMORY";
889         case OC_STACK_COMM_ERROR:
890             return "OC_STACK_COMM_ERROR";
891         case OC_STACK_INVALID_PARAM:
892             return "OC_STACK_INVALID_PARAM";
893         case OC_STACK_NOTIMPL:
894             return "OC_STACK_NOTIMPL";
895         case OC_STACK_NO_RESOURCE:
896             return "OC_STACK_NO_RESOURCE";
897         case OC_STACK_RESOURCE_ERROR:
898             return "OC_STACK_RESOURCE_ERROR";
899         case OC_STACK_SLOW_RESOURCE:
900             return "OC_STACK_SLOW_RESOURCE";
901         case OC_STACK_NO_OBSERVERS:
902             return "OC_STACK_NO_OBSERVERS";
903         case OC_STACK_ERROR:
904             return "OC_STACK_ERROR";
905         default:
906             return "UNKNOWN";
907     }
908 }
909