Add & fix unittest of easy setup.
[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 "ocpayload.h"
24 #include "oic_string.h"
25 #include "oic_malloc.h"
26
27 /**
28  * @var ES_RH_TAG
29  * @brief Logging tag for module name.
30  */
31 #define ES_RH_TAG "ES_RH"
32 //-----------------------------------------------------------------------------
33 // Private variables
34 //-----------------------------------------------------------------------------
35
36 /**
37  * @var gProvResource
38  * @brief Structure for holding the Provisioning status and target information required to
39  * connect to the target network
40  */
41 static ProvResource gProvResource;
42 static WiFiResource gWiFiResource;
43 static CloudResource gCloudResource;
44 static DevConfResource gDevConfResource;
45
46 //-----------------------------------------------------------------------------
47 // Private internal function prototypes
48 //-----------------------------------------------------------------------------
49 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
50         void *callback);
51 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
52 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
53 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
54 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
55 void updateWiFiResource(OCRepPayload* input);
56 void updateCloudResource(OCRepPayload* input);
57 void updateDevConfResource(OCRepPayload* input);
58 const char *getResult(OCStackResult result);
59
60 ESWiFiCB gWifiRsrcEvtCb = NULL;
61 ESCloudCB gCloudRsrcEvtCb = NULL;
62 ESDevConfCB gDevConfRsrcEvtCb = NULL;
63
64 ESReadUserdataCb gReadUserdataCb = NULL;
65 ESWriteUserdataCb gWriteUserdataCb = NULL;
66
67 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
68 {
69     if(!readCb && !writeCb)
70     {
71         OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
72         return ES_ERROR;
73     }
74     gReadUserdataCb = readCb;
75     gWriteUserdataCb = writeCb;
76     return ES_OK;
77 }
78
79 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
80 {
81     gWifiRsrcEvtCb = cb;
82 }
83
84 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
85 {
86     gCloudRsrcEvtCb = cb;
87 }
88
89 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
90 {
91     gDevConfRsrcEvtCb = cb;
92 }
93
94 void UnRegisterResourceEventCallBack()
95 {
96     if (gWifiRsrcEvtCb)
97     {
98         gWifiRsrcEvtCb = NULL;
99     }
100     if (gCloudRsrcEvtCb)
101     {
102         gCloudRsrcEvtCb = NULL;
103     }
104     if (gDevConfRsrcEvtCb)
105     {
106         gDevConfRsrcEvtCb = NULL;
107     }
108 }
109
110 OCStackResult initProvResource(bool isSecured)
111 {
112     gProvResource.status = ES_STATE_INIT;
113     gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
114     OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
115
116     OCStackResult res = OC_STACK_ERROR;
117     if (isSecured)
118     {
119         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
120         OC_RSRVD_INTERFACE_DEFAULT,
121         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
122         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
123     }else
124     {
125         res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
126         OC_RSRVD_INTERFACE_DEFAULT,
127         OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
128         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
129     }
130     if(res)
131     {
132         OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
133         return res;
134     }
135
136     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
137     if(res)
138     {
139         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
140         return res;
141     }
142     res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
143     if(res)
144     {
145         OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
146         return res;
147     }
148
149     OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
150     return res;
151 }
152
153 OCStackResult initWiFiResource(bool isSecured)
154 {
155     OCStackResult res = OC_STACK_ERROR;
156
157     gWiFiResource.supportedFreq = WIFI_BOTH;
158     gWiFiResource.supportedMode[0] = WIFI_11A;
159     gWiFiResource.supportedMode[1] = WIFI_11B;
160     gWiFiResource.supportedMode[2] = WIFI_11G;
161     gWiFiResource.supportedMode[3] = WIFI_11N;
162     gWiFiResource.numMode = 4;
163     gWiFiResource.authType = NONE_AUTH;
164     gWiFiResource.encType = NONE_ENC;
165     OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
166     OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
167
168     if (isSecured)
169     {
170         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
171         OC_RSRVD_INTERFACE_DEFAULT,
172         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
173         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
174     }else
175     {
176         res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
177         OC_RSRVD_INTERFACE_DEFAULT,
178         OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
179         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
180     }
181
182     OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
183     return res;
184
185 }
186
187 OCStackResult initCloudServerResource(bool isSecured)
188 {
189     OCStackResult res = OC_STACK_ERROR;
190
191     OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
192     OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
193     OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
194
195     if (isSecured)
196     {
197         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
198         OC_RSRVD_INTERFACE_DEFAULT,
199         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
200         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
201     }else
202     {
203         res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
204         OC_RSRVD_INTERFACE_DEFAULT,
205         OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
206         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
207     }
208
209     OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
210     return res;
211
212 }
213
214 OCStackResult initDevConfResource(bool isSecured)
215 {
216     OCStackResult res = OC_STACK_ERROR;
217
218     OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
219     OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
220     OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
221     OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
222     OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
223
224     if (isSecured)
225     {
226         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
227         OC_RSRVD_INTERFACE_DEFAULT,
228         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
229         NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
230     }else
231     {
232         res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
233         OC_RSRVD_INTERFACE_DEFAULT,
234         OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
235         NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
236     }
237
238     OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
239     return res;
240
241 }
242
243 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
244 {
245     OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
246
247     if(ehRequest->query)
248     {
249         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
250         {
251         // When Provisioning resource has a POST with BatchInterface
252             updateCloudResource(input);
253             updateWiFiResource(input);
254             updateDevConfResource(input);
255         }
256     }
257 }
258
259 void updateWiFiResource(OCRepPayload* input)
260 {
261     ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
262
263     if(wiFiData == NULL)
264     {
265         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
266         return ;
267     }
268
269     memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
270     memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
271     wiFiData->authtype = NONE_AUTH;
272     wiFiData->enctype = NONE_AUTH;
273     wiFiData->userdata = NULL;
274
275     char* ssid = NULL;
276     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
277     {
278         OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
279         OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
280         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
281     }
282
283     char* cred = NULL;
284     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
285     {
286         OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
287         OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
288         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
289     }
290
291     int64_t authType = -1;
292     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
293     {
294         gWiFiResource.authType = authType;
295         wiFiData->authtype = gWiFiResource.authType;
296         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
297     }
298
299     int64_t encType = -1;
300     if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
301     {
302         gWiFiResource.encType = encType;
303         wiFiData->enctype = gWiFiResource.encType;
304         OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
305     }
306
307     if(gReadUserdataCb)
308     {
309         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
310     }
311
312     if(ssid || cred || authType!= -1 || encType != -1)
313     {
314         OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
315
316         // TODO : Need to check appropriateness of gWiFiData
317         if(gWifiRsrcEvtCb != NULL)
318         {
319             gWifiRsrcEvtCb(ES_OK, wiFiData);
320         }
321         else
322         {
323             OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
324         }
325     }
326
327     OICFree(wiFiData);
328 }
329
330 void updateCloudResource(OCRepPayload* input)
331 {
332     ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
333
334     if(cloudData == NULL)
335     {
336         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
337         return;
338     }
339
340     memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
341     memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
342     memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
343     cloudData->userdata = NULL;
344
345     char *authCode = NULL;
346     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
347     {
348         OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
349         OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
350         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
351     }
352
353     char *authProvider = NULL;
354     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
355     {
356         OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
357         OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
358         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
359     }
360
361     char *ciServer = NULL;
362     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
363     {
364         OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
365         OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
366         OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
367     }
368
369     if(gReadUserdataCb)
370     {
371         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
372     }
373
374     if(authCode || authProvider || ciServer)
375     {
376         OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
377
378         // TODO : Need to check appropriateness of gCloudData
379         if(gCloudRsrcEvtCb != NULL)
380         {
381             gCloudRsrcEvtCb(ES_OK, cloudData);
382         }
383         else
384         {
385             OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
386         }
387     }
388
389     OICFree(cloudData);
390 }
391
392 void updateDevConfResource(OCRepPayload* input)
393 {
394     ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
395
396     if(devConfData == NULL)
397     {
398         OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
399         return;
400     }
401     memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
402     memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
403     devConfData->userdata = NULL;
404
405     char *location = NULL;
406     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
407     {
408         OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
409         OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
410         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
411     }
412
413     char *country = NULL;
414     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
415     {
416         OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
417         OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
418         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
419     }
420
421     char *language = NULL;
422     if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
423     {
424         OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
425         OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
426         OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
427     }
428
429     if(gReadUserdataCb)
430     {
431         gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
432     }
433
434     if(country || language)
435     {
436         OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
437
438         // TODO : Need to check appropriateness of gDevConfData
439         if(gDevConfRsrcEvtCb != NULL)
440         {
441             gDevConfRsrcEvtCb(ES_OK, devConfData);
442         }
443         else
444         {
445             OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
446         }
447     }
448
449     OICFree(devConfData);
450 }
451
452 OCRepPayload* constructResponseOfWiFi()
453 {
454     OCRepPayload* payload = OCRepPayloadCreate();
455     if (!payload)
456     {
457         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
458         return NULL;
459     }
460
461     OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
462     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
463
464     size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
465     int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
466     for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
467     {
468         modes_64[i] = gWiFiResource.supportedMode[i];
469     }
470     OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
471
472     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
473     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
474     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
475     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
476     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
477
478     if(gWriteUserdataCb)
479     {
480         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
481     }
482
483     return payload;
484 }
485
486 OCRepPayload* constructResponseOfCloud()
487 {
488     OCRepPayload* payload = OCRepPayloadCreate();
489     if (!payload)
490     {
491         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
492         return NULL;
493     }
494
495     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
496     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
497     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
498     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
499     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
500
501     if(gWriteUserdataCb)
502     {
503         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
504     }
505
506     return payload;
507 }
508
509 OCRepPayload* constructResponseOfDevConf()
510 {
511     OCRepPayload* payload = OCRepPayloadCreate();
512     if (!payload)
513     {
514         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
515         return NULL;
516     }
517
518     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
519     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
520     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
521     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
522     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
523     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
524     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
525
526     if(gWriteUserdataCb)
527     {
528         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
529     }
530
531     return payload;
532 }
533
534 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
535 {
536     OCRepPayload* payload = OCRepPayloadCreate();
537     if (!payload)
538     {
539         OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
540         return NULL;
541     }
542
543     OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
544     OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
545     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
546     OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
547     OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
548
549     if(gWriteUserdataCb)
550     {
551         gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
552     }
553
554     if(ehRequest->query)
555     {
556         if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
557         {// When Provisioning resource has a GET with BatchInterface
558             OCRepPayload* head = payload;
559             OCRepPayload* nextPayload = NULL;
560
561             nextPayload = constructResponseOfWiFi();
562             if(nextPayload != NULL)
563             {
564                 payload->next = nextPayload;
565                 payload = payload->next;
566             }
567
568             nextPayload = constructResponseOfCloud();
569             if(nextPayload != NULL)
570             {
571                 payload->next = nextPayload;
572                 payload = payload->next;
573             }
574
575             nextPayload = constructResponseOfDevConf();
576             if(nextPayload != NULL)
577             {
578                 payload->next = nextPayload;
579                 payload = payload->next;
580             }
581
582             payload = head;
583         }
584     }
585
586     return payload;
587 }
588
589
590 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
591 {
592     OCStackResult res = OC_STACK_ERROR;
593     bool maskFlag = false;
594
595     res = initProvResource(isSecured);
596     if(res != OC_STACK_OK)
597     {
598         // TODO: destroy logic will be added
599         OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
600
601         return res;
602     }
603
604     if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
605     {
606         maskFlag = true;
607         res = initWiFiResource(isSecured);
608         if(res != OC_STACK_OK)
609         {
610             OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
611             return res;
612         }
613
614         res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
615         if(res != OC_STACK_OK)
616         {
617             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
618             return res;
619         }
620
621     }
622
623     if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
624     {
625         maskFlag = true;
626         res = initCloudServerResource(isSecured);
627         if(res != OC_STACK_OK)
628         {
629             OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
630             return res;
631         }
632
633         res = OCBindResource(gProvResource.handle, gCloudResource.handle);
634         if(res != OC_STACK_OK)
635         {
636             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
637             return res;
638         }
639     }
640
641     if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
642     {
643         maskFlag = true;
644         res = initDevConfResource(isSecured);
645         if(res != OC_STACK_OK)
646         {
647             OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
648             return res;
649         }
650
651         res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
652         if(res != OC_STACK_OK)
653         {
654             OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
655             return res;
656         }
657     }
658
659
660     if(maskFlag == false)
661     {
662         OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
663         return OC_STACK_ERROR;
664
665     }
666
667     OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
668
669     return res;
670 }
671
672 OCStackResult DeleteProvisioningResource()
673 {
674     OCStackResult res = OCDeleteResource(gProvResource.handle);
675     if (res != OC_STACK_OK)
676     {
677         OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
678     }
679
680     return res;
681 }
682
683 OCStackResult DeleteEasySetupResources()
684 {
685     OCStackResult res = OC_STACK_ERROR;
686     if (gWiFiResource.handle != NULL)
687     {
688         res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
689         if(res != OC_STACK_OK)
690         {
691             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
692         }
693     }
694     if (gCloudResource.handle != NULL)
695     {
696         res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
697         if(res != OC_STACK_OK)
698         {
699             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
700         }
701     }
702     if (gDevConfResource.handle != NULL)
703     {
704         res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
705         if(res != OC_STACK_OK)
706         {
707             OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
708         }
709     }
710
711     if (gWiFiResource.handle != NULL)
712     {
713         res = OCDeleteResource(gWiFiResource.handle);
714         if (res != OC_STACK_OK)
715         {
716             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
717         }
718     }
719
720     if(gCloudResource.handle != NULL)
721     {
722         res = OCDeleteResource(gCloudResource.handle);
723         if (res != OC_STACK_OK)
724         {
725             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
726         }
727     }
728
729     if(gDevConfResource.handle != NULL)
730     {
731         res = OCDeleteResource(gDevConfResource.handle);
732         if (res != OC_STACK_OK)
733         {
734             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
735         }
736     }
737
738     if(gProvResource.handle != NULL)
739     {
740         res = OCDeleteResource(gProvResource.handle);
741         if (res != OC_STACK_OK)
742         {
743             OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
744         }
745     }
746
747     return res;
748 }
749
750 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
751 {
752     OCEntityHandlerResult ehResult = OC_EH_ERROR;
753     if (!ehRequest)
754     {
755         OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
756         return ehResult;
757     }
758     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
759     {
760         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
761         return ehResult;
762     }
763
764     OCRepPayload *getResp = NULL;
765
766     if(ehRequest->resource == gProvResource.handle)
767     {
768         getResp = constructResponseOfProv(ehRequest);
769     }
770     else if(ehRequest->resource == gWiFiResource.handle)
771     {
772         getResp = constructResponseOfWiFi();
773     }
774     else if(ehRequest->resource == gCloudResource.handle)
775     {
776         getResp = constructResponseOfCloud();
777     }
778     else if(ehRequest->resource == gDevConfResource.handle)
779     {
780         getResp = constructResponseOfDevConf();
781     }
782
783     if (!getResp)
784     {
785         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
786         return OC_EH_ERROR;
787     }
788
789     *payload = getResp;
790     ehResult = OC_EH_OK;
791
792     return ehResult;
793 }
794
795 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
796 {
797     OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
798     OCEntityHandlerResult ehResult = OC_EH_ERROR;
799     if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
800     {
801         OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
802         return ehResult;
803     }
804
805     OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
806     if (!input)
807     {
808         OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
809         return ehResult;
810     }
811
812     if(ehRequest->resource == gProvResource.handle)
813     {
814         updateProvResource(ehRequest, input);
815     }
816     else if(ehRequest->resource == gWiFiResource.handle)
817     {
818         updateWiFiResource(input);
819     }
820     else if(ehRequest->resource == gCloudResource.handle)
821     {
822         updateCloudResource(input);
823     }
824     else if(ehRequest->resource == gDevConfResource.handle)
825     {
826         updateDevConfResource(input);
827     }
828
829     OCRepPayload *getResp = NULL;
830     if(ehRequest->resource == gProvResource.handle)
831     {
832         getResp = constructResponseOfProv(ehRequest);
833     }
834     else if(ehRequest->resource == gWiFiResource.handle)
835     {
836         getResp = constructResponseOfWiFi();
837     }
838     else if(ehRequest->resource == gCloudResource.handle)
839     {
840         getResp = constructResponseOfCloud();
841     }
842     else if(ehRequest->resource == gDevConfResource.handle)
843     {
844         getResp = constructResponseOfDevConf();
845     }
846
847     if (!getResp)
848     {
849         OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
850         return OC_EH_ERROR;
851     }
852
853     *payload = getResp;
854     ehResult = OC_EH_OK;
855
856     return ehResult;
857 }
858
859 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
860         OCRepPayload** payload)
861 {
862     (void) ehRequest;
863     (void) payload;
864     OCEntityHandlerResult ehResult = OC_EH_ERROR;
865
866     return ehResult;
867 }
868 /**
869  * This is the entity handler for the registered resource.
870  * This is invoked by OCStack whenever it recevies a request for this resource.
871  */
872 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
873         OCEntityHandlerRequest* entityHandlerRequest, void *callback)
874 {
875     (void) callback;
876     OCEntityHandlerResult ehRet = OC_EH_OK;
877     OCEntityHandlerResponse response =
878     { 0, 0, OC_EH_ERROR, 0, 0,
879     { },
880     { 0 }, false };
881     OCRepPayload* payload = NULL;
882
883     if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
884     {
885         if (OC_REST_GET == entityHandlerRequest->method)
886         {
887             OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
888             ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
889         }
890         else if (OC_REST_PUT == entityHandlerRequest->method)
891         {
892             OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
893
894             //PUT request will be handled in the internal implementation
895             if (gProvResource.handle != NULL)
896             {
897                 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
898             }
899             else
900             {
901                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
902                 ehRet = OC_EH_ERROR;
903             }
904         }
905         else if (OC_REST_POST == entityHandlerRequest->method)
906         {
907             OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
908             if (gProvResource.handle != NULL)
909             {
910                 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
911             }
912             else
913             {
914                 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
915                 ehRet = OC_EH_ERROR;
916             }
917         }
918
919         // Format the response.  Note this requires some info about the request
920         response.requestHandle = entityHandlerRequest->requestHandle;
921         response.resourceHandle = entityHandlerRequest->resource;
922         response.ehResult = ehRet;
923         //response uses OCPaylod while all get,put methodes use OCRepPayload
924         response.payload = (OCPayload*) (payload);
925         response.numSendVendorSpecificHeaderOptions = 0;
926         memset(response.sendVendorSpecificHeaderOptions, 0,
927                 sizeof(response.sendVendorSpecificHeaderOptions));
928         memset(response.resourceUri, 0, sizeof(response.resourceUri));
929         // Indicate that response is NOT in a persistent buffer
930         response.persistentBufferFlag = 0;
931
932         // Send the response
933         if (OCDoResponse(&response) != OC_STACK_OK)
934         {
935             OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
936             ehRet = OC_EH_ERROR;
937         }
938     }
939
940     return ehRet;
941 }
942
943 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
944 {
945     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
946
947     gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
948     OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
949
950     int modeIdx = 0;
951     while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
952     {
953         gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
954         OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
955         modeIdx ++;
956     }
957     gWiFiResource.numMode = modeIdx;
958
959     OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
960     OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
961
962     OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
963                                                             (deviceProperty->DevConf).modelNumber);
964     OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
965
966     OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
967     return OC_STACK_OK;
968 }
969
970 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
971 {
972     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
973
974     gProvResource.status = esState;
975     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
976
977     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
978     return OC_STACK_OK;
979 }
980
981 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
982 {
983     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
984
985     gProvResource.lastErrCode = esErrCode;
986     OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
987
988     OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
989     return OC_STACK_OK;
990 }
991
992 const char *getResult(OCStackResult result)
993 {
994     switch (result)
995     {
996         case OC_STACK_OK:
997             return "OC_STACK_OK";
998         case OC_STACK_INVALID_URI:
999             return "OC_STACK_INVALID_URI";
1000         case OC_STACK_INVALID_QUERY:
1001             return "OC_STACK_INVALID_QUERY";
1002         case OC_STACK_INVALID_IP:
1003             return "OC_STACK_INVALID_IP";
1004         case OC_STACK_INVALID_PORT:
1005             return "OC_STACK_INVALID_PORT";
1006         case OC_STACK_INVALID_CALLBACK:
1007             return "OC_STACK_INVALID_CALLBACK";
1008         case OC_STACK_INVALID_METHOD:
1009             return "OC_STACK_INVALID_METHOD";
1010         case OC_STACK_NO_MEMORY:
1011             return "OC_STACK_NO_MEMORY";
1012         case OC_STACK_COMM_ERROR:
1013             return "OC_STACK_COMM_ERROR";
1014         case OC_STACK_INVALID_PARAM:
1015             return "OC_STACK_INVALID_PARAM";
1016         case OC_STACK_NOTIMPL:
1017             return "OC_STACK_NOTIMPL";
1018         case OC_STACK_NO_RESOURCE:
1019             return "OC_STACK_NO_RESOURCE";
1020         case OC_STACK_RESOURCE_ERROR:
1021             return "OC_STACK_RESOURCE_ERROR";
1022         case OC_STACK_SLOW_RESOURCE:
1023             return "OC_STACK_SLOW_RESOURCE";
1024         case OC_STACK_NO_OBSERVERS:
1025             return "OC_STACK_NO_OBSERVERS";
1026         case OC_STACK_ERROR:
1027             return "OC_STACK_ERROR";
1028         default:
1029             return "UNKNOWN";
1030     }
1031 }