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     char *serverID = NULL;
352     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SERVERID, &serverID))
353     {
354         OICStrcpy(gCloudResource.serverID, sizeof(gCloudResource.serverID), serverID);
355         OICStrcpy(gCloudData.serverID, sizeof(gCloudData.serverID), serverID);
356         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.serverID %s", gCloudResource.serverID);
357     }
358
359     if(authCode || authProvider || ciServer)
360     {
361         OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
362
363         // TODO : Need to check appropriateness of gCloudData
364         if(gCloudRsrcEvtCb != NULL)
365         {
366             gCloudRsrcEvtCb(ES_OK, &gCloudData);
367         }
368         else
369         {
370             OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
371         }
372     }
373 }
374
375 void updateDevConfResource(OCRepPayload* input)
376 {
377     char *country = NULL;
378     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
379     {
380         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
381         OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
382         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
383     }
384
385     char *language = NULL;
386     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
387     {
388         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
389         OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
390         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
391     }
392
393     if(country || language)
394     {
395         OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
396
397         // TODO : Need to check appropriateness of gDevConfData
398         if(gDevConfRsrcEvtCb != NULL)
399         {
400             gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
401         }
402         else
403         {
404             OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
405         }
406     }
407 }
408
409 OCRepPayload* constructResponseOfWiFi()
410 {
411     OCRepPayload* payload = OCRepPayloadCreate();
412     if (!payload)
413     {
414         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
415         return NULL;
416     }
417
418     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
419     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
420
421     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
422     int64_t *modes_64 = (int64_t *)malloc(gWiFiResource.numMode * sizeof(int64_t));
423     for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
424         modes_64[i] = gWiFiResource.supportedMode[i];
425     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
426
427     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
428     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
429     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
430     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
431     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
432
433     return payload;
434 }
435
436 OCRepPayload* constructResponseOfCloud()
437 {
438     OCRepPayload* payload = OCRepPayloadCreate();
439     if (!payload)
440     {
441         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
442         return NULL;
443     }
444
445     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
446     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
447     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
448     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
449     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
450     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SERVERID, gCloudResource.serverID);
451
452     return payload;
453 }
454
455 OCRepPayload* constructResponseOfDevConf()
456 {
457     OCRepPayload* payload = OCRepPayloadCreate();
458     if (!payload)
459     {
460         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
461         return NULL;
462     }
463
464     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
465     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
466     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
467     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
468     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
469
470     return payload;
471 }
472
473 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
474 {
475     OCRepPayload* payload = OCRepPayloadCreate();
476     if (!payload)
477     {
478         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
479         return NULL;
480     }
481
482     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
483     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
484     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
485     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
486     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
487
488     if(ehRequest->query)
489     {
490         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
491         {// When Provisioning resource has a GET with BatchInterface
492             payload->next = constructResponseOfWiFi();
493
494             if(payload->next)
495                 payload->next->next = constructResponseOfCloud();
496             else
497                 return payload;
498
499             if(payload->next->next)
500                 payload->next->next->next = constructResponseOfDevConf();
501             else
502                 return payload;
503         }
504     }
505
506     return payload;
507 }
508
509
510 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
511 {
512     OCStackResult res = OC_STACK_ERROR;
513     bool maskFlag = false;
514
515     res = initProvResource(isSecured);
516     if(res != OC_STACK_OK)
517     {
518         // TODO: destroy logic will be added
519         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
520
521         return res;
522     }
523
524     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
525     {
526         maskFlag = true;
527         res = initWiFiResource(isSecured);
528         if(res != OC_STACK_OK)
529         {
530             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
531             return res;
532         }
533
534         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
535         if(res != OC_STACK_OK)
536         {
537             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
538             return res;
539         }
540
541     }
542
543     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
544     {
545         maskFlag = true;
546         res = initCloudServerResource(isSecured);
547         if(res != OC_STACK_OK)
548         {
549             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
550             return res;
551         }
552
553         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
554         if(res != OC_STACK_OK)
555         {
556             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
557             return res;
558         }
559     }
560
561     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
562     {
563         maskFlag = true;
564         res = initDevConfResource(isSecured);
565         if(res != OC_STACK_OK)
566         {
567             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
568             return res;
569         }
570
571         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
572         if(res != OC_STACK_OK)
573         {
574             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
575             return res;
576         }
577     }
578
579
580     if(maskFlag == false)
581     {
582         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
583         return OC_STACK_ERROR;
584
585     }
586
587     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
588
589     return res;
590 }
591
592 OCStackResult DeleteProvisioningResource()
593 {
594     OCStackResult res = OCDeleteResource(gProvResource.handle);
595     if (res != OC_STACK_OK)
596     {
597         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
598     }
599
600     return res;
601 }
602
603 OCStackResult DeleteEasySetupResources()
604 {
605     OCStackResult res = OCDeleteResource(gWiFiResource.handle);
606     if (res != OC_STACK_OK)
607     {
608         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
609     }
610     res = OCDeleteResource(gCloudResource.handle);
611     if (res != OC_STACK_OK)
612     {
613         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
614     }
615     res = OCDeleteResource(gDevConfResource.handle);
616     if (res != OC_STACK_OK)
617     {
618         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
619     }
620
621     res = OCDeleteResource(gProvResource.handle);
622     if (res != OC_STACK_OK)
623     {
624         OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
625     }
626
627     return res;
628 }
629
630 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
631 {
632     OCEntityHandlerResult ehResult = OC_EH_ERROR;
633     if (!ehRequest)
634     {
635         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
636         return ehResult;
637     }
638     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
639     {
640         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
641         return ehResult;
642     }
643
644     OCRepPayload *getResp = NULL;
645
646     if(ehRequest->resource == gProvResource.handle)
647         getResp = constructResponseOfProv(ehRequest);
648     else if(ehRequest->resource == gWiFiResource.handle)
649         getResp = constructResponseOfWiFi();
650     else if(ehRequest->resource == gCloudResource.handle)
651         getResp = constructResponseOfCloud();
652     else if(ehRequest->resource == gDevConfResource.handle)
653         getResp = constructResponseOfDevConf();
654
655     if (!getResp)
656     {
657         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
658         return OC_EH_ERROR;
659     }
660
661     *payload = getResp;
662     ehResult = OC_EH_OK;
663
664     return ehResult;
665 }
666
667 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
668 {
669     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
670     OCEntityHandlerResult ehResult = OC_EH_ERROR;
671     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
672     {
673         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
674         return ehResult;
675     }
676
677     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
678     if (!input)
679     {
680         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
681         return ehResult;
682     }
683
684     // TBD : Discuss about triggering flag (to be existed or not)
685     // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
686     // A new request for provisioning means overriding existing network provisioning information.  
687
688     if(ehRequest->resource == gProvResource.handle)
689         updateProvResource(ehRequest, input);
690     else if(ehRequest->resource == gWiFiResource.handle)
691         updateWiFiResource(input);
692     else if(ehRequest->resource == gCloudResource.handle)
693         updateCloudResource(input);
694     else if(ehRequest->resource == gDevConfResource.handle)
695         updateDevConfResource(input);      
696
697     OCRepPayload *getResp = NULL;
698     if(ehRequest->resource == gProvResource.handle)
699         getResp = constructResponseOfProv(ehRequest);
700     else if(ehRequest->resource == gWiFiResource.handle)
701         getResp = constructResponseOfWiFi();
702     else if(ehRequest->resource == gCloudResource.handle)
703         getResp = constructResponseOfCloud();
704     else if(ehRequest->resource == gDevConfResource.handle)
705         getResp = constructResponseOfDevConf();
706
707     if (!getResp)
708     {
709         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
710         return OC_EH_ERROR;
711     }
712
713     *payload = getResp;
714     ehResult = OC_EH_OK;
715
716     return ehResult;
717 }
718
719 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
720         OCRepPayload** payload)
721 {
722     (void) ehRequest;
723     (void) payload;
724     OCEntityHandlerResult ehResult = OC_EH_ERROR;
725
726     return ehResult;
727 }
728 /**
729  * This is the entity handler for the registered resource.
730  * This is invoked by OCStack whenever it recevies a request for this resource.
731  */
732 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
733         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
734 {
735     (void) callback;
736     OCEntityHandlerResult ehRet = OC_EH_OK;
737     OCEntityHandlerResponse response =
738     { 0, 0, OC_EH_ERROR, 0, 0,
739     { },
740     { 0 }, false };
741     OCRepPayload* payload = NULL;
742
743     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
744     {
745         if (OC_REST_GET == entityHandlerRequest->method)
746         {
747             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
748             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
749         }
750         else if (OC_REST_PUT == entityHandlerRequest->method)
751         {
752             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
753
754             //PUT request will be handled in the internal implementation
755             if (gProvResource.handle != NULL)
756             {
757                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
758             }
759             else
760             {
761                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
762                 ehRet = OC_EH_ERROR;
763             }
764         }
765         else if (OC_REST_POST == entityHandlerRequest->method)
766         {
767             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
768             if (gProvResource.handle != NULL)
769             {
770                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
771             }
772             else
773             {
774                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
775                 ehRet = OC_EH_ERROR;
776             }
777         }
778
779         if (ehRet == OC_EH_OK)
780         {
781             // Format the response.  Note this requires some info about the request
782             response.requestHandle = entityHandlerRequest->requestHandle;
783             response.resourceHandle = entityHandlerRequest->resource;
784             response.ehResult = ehRet;
785             //response uses OCPaylod while all get,put methodes use OCRepPayload
786             response.payload = (OCPayload*) (payload);
787             response.numSendVendorSpecificHeaderOptions = 0;
788             memset(response.sendVendorSpecificHeaderOptions, 0,
789                     sizeof(response.sendVendorSpecificHeaderOptions));
790             memset(response.resourceUri, 0, sizeof(response.resourceUri));
791             // Indicate that response is NOT in a persistent buffer
792             response.persistentBufferFlag = 0;
793
794             // Send the response
795             if (OCDoResponse(&response) != OC_STACK_OK)
796             {
797                 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
798                 ehRet = OC_EH_ERROR;
799             }
800         }
801     }
802
803     return ehRet;
804 }
805
806 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
807 {
808     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
809
810     gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
811     OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
812
813     int modeIdx = 0;
814     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
815     {
816         gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
817         OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
818         modeIdx ++;
819     }
820     gWiFiResource.numMode = modeIdx;
821
822     OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
823     OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
824
825     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
826     return OC_STACK_OK;
827 }
828
829 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
830 {
831     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
832
833     gProvResource.status = esState;
834     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
835
836     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
837     return OC_STACK_OK;
838 }
839
840 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
841 {
842     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
843
844     gProvResource.lastErrCode = esErrCode;
845     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
846
847     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
848     return OC_STACK_OK;
849 }
850 const char *getResult(OCStackResult result)
851 {
852     switch (result)
853     {
854         case OC_STACK_OK:
855             return "OC_STACK_OK";
856         case OC_STACK_INVALID_URI:
857             return "OC_STACK_INVALID_URI";
858         case OC_STACK_INVALID_QUERY:
859             return "OC_STACK_INVALID_QUERY";
860         case OC_STACK_INVALID_IP:
861             return "OC_STACK_INVALID_IP";
862         case OC_STACK_INVALID_PORT:
863             return "OC_STACK_INVALID_PORT";
864         case OC_STACK_INVALID_CALLBACK:
865             return "OC_STACK_INVALID_CALLBACK";
866         case OC_STACK_INVALID_METHOD:
867             return "OC_STACK_INVALID_METHOD";
868         case OC_STACK_NO_MEMORY:
869             return "OC_STACK_NO_MEMORY";
870         case OC_STACK_COMM_ERROR:
871             return "OC_STACK_COMM_ERROR";
872         case OC_STACK_INVALID_PARAM:
873             return "OC_STACK_INVALID_PARAM";
874         case OC_STACK_NOTIMPL:
875             return "OC_STACK_NOTIMPL";
876         case OC_STACK_NO_RESOURCE:
877             return "OC_STACK_NO_RESOURCE";
878         case OC_STACK_RESOURCE_ERROR:
879             return "OC_STACK_RESOURCE_ERROR";
880         case OC_STACK_SLOW_RESOURCE:
881             return "OC_STACK_SLOW_RESOURCE";
882         case OC_STACK_NO_OBSERVERS:
883             return "OC_STACK_NO_OBSERVERS";
884         case OC_STACK_ERROR:
885             return "OC_STACK_ERROR";
886         default:
887             return "UNKNOWN";
888     }
889 }
890