1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "resourcehandler.h"
23 #include "ocpayload.h"
24 #include "oic_string.h"
25 #include "oic_malloc.h"
29 * @brief Logging tag for module name.
31 #define ES_RH_TAG "ES_RH"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
38 * @brief Structure for holding the Provisioning status and target information required to
39 * connect to the target network
41 static ProvResource gProvResource;
42 static WiFiResource gWiFiResource;
43 static CloudResource gCloudResource;
44 static DevConfResource gDevConfResource;
46 //-----------------------------------------------------------------------------
47 // Private internal function prototypes
48 //-----------------------------------------------------------------------------
49 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
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);
60 ESWiFiCB gWifiRsrcEvtCb = NULL;
61 ESCloudCB gCloudRsrcEvtCb = NULL;
62 ESDevConfCB gDevConfRsrcEvtCb = NULL;
64 ESReadUserdataCb gReadUserdataCb = NULL;
65 ESWriteUserdataCb gWriteUserdataCb = NULL;
67 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
69 if(!readCb && !writeCb)
71 OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
74 gReadUserdataCb = readCb;
75 gWriteUserdataCb = writeCb;
79 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
84 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
89 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
91 gDevConfRsrcEvtCb = cb;
94 void UnRegisterResourceEventCallBack()
98 gWifiRsrcEvtCb = NULL;
102 gCloudRsrcEvtCb = NULL;
104 if (gDevConfRsrcEvtCb)
106 gDevConfRsrcEvtCb = NULL;
110 OCStackResult initProvResource(bool isSecured)
112 gProvResource.status = ES_STATE_INIT;
113 gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
114 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
116 OCStackResult res = OC_STACK_ERROR;
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);
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);
132 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
136 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
139 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
142 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
145 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
149 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
153 OCStackResult initWiFiResource(bool isSecured)
155 OCStackResult res = OC_STACK_ERROR;
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), "");
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);
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);
182 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
187 OCStackResult initCloudServerResource(bool isSecured)
189 OCStackResult res = OC_STACK_ERROR;
191 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
192 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
193 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
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);
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);
209 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
214 OCStackResult initDevConfResource(bool isSecured)
216 OCStackResult res = OC_STACK_ERROR;
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), "");
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);
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);
238 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
243 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
245 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
249 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
251 // When Provisioning resource has a POST with BatchInterface
252 updateCloudResource(input);
253 updateWiFiResource(input);
254 updateDevConfResource(input);
259 void updateWiFiResource(OCRepPayload* input)
261 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
265 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
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;
276 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
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);
284 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
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);
291 int64_t authType = -1;
292 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
294 gWiFiResource.authType = authType;
295 wiFiData->authtype = gWiFiResource.authType;
296 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
299 int64_t encType = -1;
300 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
302 gWiFiResource.encType = encType;
303 wiFiData->enctype = gWiFiResource.encType;
304 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
309 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
312 if(ssid || cred || authType!= -1 || encType != -1)
314 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
316 // TODO : Need to check appropriateness of gWiFiData
317 if(gWifiRsrcEvtCb != NULL)
319 gWifiRsrcEvtCb(ES_OK, wiFiData);
323 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
330 void updateCloudResource(OCRepPayload* input)
332 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
334 if(cloudData == NULL)
336 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
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;
345 char *authCode = NULL;
346 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
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);
353 char *authProvider = NULL;
354 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
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);
361 char *ciServer = NULL;
362 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
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);
371 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
374 if(authCode || authProvider || ciServer)
376 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
378 // TODO : Need to check appropriateness of gCloudData
379 if(gCloudRsrcEvtCb != NULL)
381 gCloudRsrcEvtCb(ES_OK, cloudData);
385 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
392 void updateDevConfResource(OCRepPayload* input)
394 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
396 if(devConfData == NULL)
398 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
401 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
402 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
403 devConfData->userdata = NULL;
405 char *location = NULL;
406 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
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);
413 char *country = NULL;
414 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
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);
421 char *language = NULL;
422 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
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);
431 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
434 if(country || language)
436 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
438 // TODO : Need to check appropriateness of gDevConfData
439 if(gDevConfRsrcEvtCb != NULL)
441 gDevConfRsrcEvtCb(ES_OK, devConfData);
445 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
449 OICFree(devConfData);
452 OCRepPayload* constructResponseOfWiFi()
454 OCRepPayload* payload = OCRepPayloadCreate();
457 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
461 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
462 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
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)
468 modes_64[i] = gWiFiResource.supportedMode[i];
470 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
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);
480 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
486 OCRepPayload* constructResponseOfCloud()
488 OCRepPayload* payload = OCRepPayloadCreate();
491 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
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);
503 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
509 OCRepPayload* constructResponseOfDevConf()
511 OCRepPayload* payload = OCRepPayloadCreate();
514 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
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);
528 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
534 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
536 OCRepPayload* payload = OCRepPayloadCreate();
539 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
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);
551 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
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;
561 nextPayload = constructResponseOfWiFi();
562 if(nextPayload != NULL)
564 payload->next = nextPayload;
565 payload = payload->next;
568 nextPayload = constructResponseOfCloud();
569 if(nextPayload != NULL)
571 payload->next = nextPayload;
572 payload = payload->next;
575 nextPayload = constructResponseOfDevConf();
576 if(nextPayload != NULL)
578 payload->next = nextPayload;
579 payload = payload->next;
590 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
592 OCStackResult res = OC_STACK_ERROR;
593 bool maskFlag = false;
595 res = initProvResource(isSecured);
596 if(res != OC_STACK_OK)
598 // TODO: destroy logic will be added
599 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
604 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
607 res = initWiFiResource(isSecured);
608 if(res != OC_STACK_OK)
610 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
614 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
615 if(res != OC_STACK_OK)
617 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
623 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
626 res = initCloudServerResource(isSecured);
627 if(res != OC_STACK_OK)
629 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
633 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
634 if(res != OC_STACK_OK)
636 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
641 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
644 res = initDevConfResource(isSecured);
645 if(res != OC_STACK_OK)
647 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
651 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
652 if(res != OC_STACK_OK)
654 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
660 if(maskFlag == false)
662 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
663 return OC_STACK_ERROR;
667 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
672 OCStackResult DeleteProvisioningResource()
674 OCStackResult res = OCDeleteResource(gProvResource.handle);
675 if (res != OC_STACK_OK)
677 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
683 OCStackResult DeleteEasySetupResources()
685 OCStackResult res = OC_STACK_ERROR;
686 if (gWiFiResource.handle != NULL)
688 res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
689 if(res != OC_STACK_OK)
691 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
694 if (gCloudResource.handle != NULL)
696 res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
697 if(res != OC_STACK_OK)
699 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
702 if (gDevConfResource.handle != NULL)
704 res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
705 if(res != OC_STACK_OK)
707 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
711 if (gWiFiResource.handle != NULL)
713 res = OCDeleteResource(gWiFiResource.handle);
714 if (res != OC_STACK_OK)
716 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
720 if(gCloudResource.handle != NULL)
722 res = OCDeleteResource(gCloudResource.handle);
723 if (res != OC_STACK_OK)
725 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
729 if(gDevConfResource.handle != NULL)
731 res = OCDeleteResource(gDevConfResource.handle);
732 if (res != OC_STACK_OK)
734 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
738 if(gProvResource.handle != NULL)
740 res = OCDeleteResource(gProvResource.handle);
741 if (res != OC_STACK_OK)
743 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
750 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
752 OCEntityHandlerResult ehResult = OC_EH_ERROR;
755 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
758 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
760 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
764 OCRepPayload *getResp = NULL;
766 if(ehRequest->resource == gProvResource.handle)
768 getResp = constructResponseOfProv(ehRequest);
770 else if(ehRequest->resource == gWiFiResource.handle)
772 getResp = constructResponseOfWiFi();
774 else if(ehRequest->resource == gCloudResource.handle)
776 getResp = constructResponseOfCloud();
778 else if(ehRequest->resource == gDevConfResource.handle)
780 getResp = constructResponseOfDevConf();
785 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
795 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
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)
801 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
805 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
808 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
812 if(ehRequest->resource == gProvResource.handle)
814 updateProvResource(ehRequest, input);
816 else if(ehRequest->resource == gWiFiResource.handle)
818 updateWiFiResource(input);
820 else if(ehRequest->resource == gCloudResource.handle)
822 updateCloudResource(input);
824 else if(ehRequest->resource == gDevConfResource.handle)
826 updateDevConfResource(input);
829 OCRepPayload *getResp = NULL;
830 if(ehRequest->resource == gProvResource.handle)
832 getResp = constructResponseOfProv(ehRequest);
834 else if(ehRequest->resource == gWiFiResource.handle)
836 getResp = constructResponseOfWiFi();
838 else if(ehRequest->resource == gCloudResource.handle)
840 getResp = constructResponseOfCloud();
842 else if(ehRequest->resource == gDevConfResource.handle)
844 getResp = constructResponseOfDevConf();
849 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
859 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
860 OCRepPayload** payload)
864 OCEntityHandlerResult ehResult = OC_EH_ERROR;
869 * This is the entity handler for the registered resource.
870 * This is invoked by OCStack whenever it recevies a request for this resource.
872 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
873 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
876 OCEntityHandlerResult ehRet = OC_EH_OK;
877 OCEntityHandlerResponse response =
878 { 0, 0, OC_EH_ERROR, 0, 0,
881 OCRepPayload* payload = NULL;
883 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
885 if (OC_REST_GET == entityHandlerRequest->method)
887 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
888 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
890 else if (OC_REST_PUT == entityHandlerRequest->method)
892 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
894 //PUT request will be handled in the internal implementation
895 if (gProvResource.handle != NULL)
897 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
901 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
905 else if (OC_REST_POST == entityHandlerRequest->method)
907 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
908 if (gProvResource.handle != NULL)
910 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
914 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
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;
933 if (OCDoResponse(&response) != OC_STACK_OK)
935 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
943 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
945 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
947 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
948 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
951 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
953 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
954 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
957 gWiFiResource.numMode = modeIdx;
959 OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
960 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
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);
966 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
970 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
972 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
974 gProvResource.status = esState;
975 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
977 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
981 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
983 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
985 gProvResource.lastErrCode = esErrCode;
986 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
988 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
992 const char *getResult(OCStackResult result)
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";