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