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