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