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 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
112 if (name != NULL && pass != NULL)
114 OICStrcpy(name, MAX_WEBLINKLEN, gWiFiResource.ssid);
115 OICStrcpy(pass, MAX_WEBLINKLEN, gWiFiResource.cred);
119 OCStackResult initProvResource(bool isSecured)
121 gProvResource.status = ES_STATE_INIT;
122 gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
123 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
125 OCStackResult res = OC_STACK_ERROR;
128 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
129 OC_RSRVD_INTERFACE_DEFAULT,
130 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
131 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
134 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
135 OC_RSRVD_INTERFACE_DEFAULT,
136 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
137 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
141 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
145 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
148 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
151 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
154 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
158 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
162 OCStackResult initWiFiResource(bool isSecured)
164 OCStackResult res = OC_STACK_ERROR;
166 gWiFiResource.supportedFreq = WIFI_BOTH;
167 gWiFiResource.supportedMode[0] = WIFI_11A;
168 gWiFiResource.supportedMode[1] = WIFI_11B;
169 gWiFiResource.supportedMode[2] = WIFI_11G;
170 gWiFiResource.supportedMode[3] = WIFI_11N;
171 gWiFiResource.numMode = 4;
172 gWiFiResource.authType = NONE_AUTH;
173 gWiFiResource.encType = NONE_ENC;
174 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
175 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
179 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
180 OC_RSRVD_INTERFACE_DEFAULT,
181 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
182 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
185 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
186 OC_RSRVD_INTERFACE_DEFAULT,
187 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
188 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
191 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
196 OCStackResult initCloudServerResource(bool isSecured)
198 OCStackResult res = OC_STACK_ERROR;
200 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
201 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
202 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
206 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
207 OC_RSRVD_INTERFACE_DEFAULT,
208 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
209 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
212 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
213 OC_RSRVD_INTERFACE_DEFAULT,
214 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
215 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
218 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
223 OCStackResult initDevConfResource(bool isSecured)
225 OCStackResult res = OC_STACK_ERROR;
227 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
228 OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
229 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
230 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
231 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
235 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
236 OC_RSRVD_INTERFACE_DEFAULT,
237 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
238 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
241 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
242 OC_RSRVD_INTERFACE_DEFAULT,
243 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
244 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
247 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
252 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
254 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
258 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
260 // When Provisioning resource has a POST with BatchInterface
261 updateCloudResource(input);
262 updateWiFiResource(input);
263 updateDevConfResource(input);
268 void updateWiFiResource(OCRepPayload* input)
270 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
274 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
278 memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
279 memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
280 wiFiData->authtype = NONE_AUTH;
281 wiFiData->enctype = NONE_AUTH;
282 wiFiData->userdata = NULL;
285 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
287 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
288 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
289 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
293 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
295 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
296 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
297 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
300 int64_t authType = -1;
301 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
303 gWiFiResource.authType = authType;
304 wiFiData->authtype = gWiFiResource.authType;
305 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
308 int64_t encType = -1;
309 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
311 gWiFiResource.encType = encType;
312 wiFiData->enctype = gWiFiResource.encType;
313 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
318 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
321 if(ssid || cred || authType!= -1 || encType != -1)
323 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
325 // TODO : Need to check appropriateness of gWiFiData
326 if(gWifiRsrcEvtCb != NULL)
328 gWifiRsrcEvtCb(ES_OK, wiFiData);
332 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
339 void updateCloudResource(OCRepPayload* input)
341 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
343 if(cloudData == NULL)
345 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
349 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
350 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
351 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
352 cloudData->userdata = NULL;
354 char *authCode = NULL;
355 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
357 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
358 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
359 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
362 char *authProvider = NULL;
363 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
365 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
366 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
367 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
370 char *ciServer = NULL;
371 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
373 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
374 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
375 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
380 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
383 if(authCode || authProvider || ciServer)
385 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
387 // TODO : Need to check appropriateness of gCloudData
388 if(gCloudRsrcEvtCb != NULL)
390 gCloudRsrcEvtCb(ES_OK, cloudData);
394 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
401 void updateDevConfResource(OCRepPayload* input)
403 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
405 if(devConfData == NULL)
407 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
410 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
411 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
412 devConfData->userdata = NULL;
414 char *location = NULL;
415 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
417 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
418 OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
419 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
422 char *country = NULL;
423 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
425 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
426 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
427 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
430 char *language = NULL;
431 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
433 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
434 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
435 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
440 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
443 if(country || language)
445 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
447 // TODO : Need to check appropriateness of gDevConfData
448 if(gDevConfRsrcEvtCb != NULL)
450 gDevConfRsrcEvtCb(ES_OK, devConfData);
454 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
458 OICFree(devConfData);
461 OCRepPayload* constructResponseOfWiFi()
463 OCRepPayload* payload = OCRepPayloadCreate();
466 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
470 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
471 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
473 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
474 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
475 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
477 modes_64[i] = gWiFiResource.supportedMode[i];
479 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
481 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
482 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
483 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
484 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
485 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
489 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
495 OCRepPayload* constructResponseOfCloud()
497 OCRepPayload* payload = OCRepPayloadCreate();
500 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
504 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
505 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
506 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
507 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
508 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
512 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
518 OCRepPayload* constructResponseOfDevConf()
520 OCRepPayload* payload = OCRepPayloadCreate();
523 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
527 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
528 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
529 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
530 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
531 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
532 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
533 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
537 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
543 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
545 OCRepPayload* payload = OCRepPayloadCreate();
548 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
552 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
553 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
554 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
555 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
556 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
560 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
565 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
566 {// When Provisioning resource has a GET with BatchInterface
567 OCRepPayload* head = payload;
568 OCRepPayload* nextPayload = NULL;
570 nextPayload = constructResponseOfWiFi();
571 if(nextPayload != NULL)
573 payload->next = nextPayload;
574 payload = payload->next;
577 nextPayload = constructResponseOfCloud();
578 if(nextPayload != NULL)
580 payload->next = nextPayload;
581 payload = payload->next;
584 nextPayload = constructResponseOfDevConf();
585 if(nextPayload != NULL)
587 payload->next = nextPayload;
588 payload = payload->next;
599 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
601 OCStackResult res = OC_STACK_ERROR;
602 bool maskFlag = false;
604 res = initProvResource(isSecured);
605 if(res != OC_STACK_OK)
607 // TODO: destroy logic will be added
608 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
613 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
616 res = initWiFiResource(isSecured);
617 if(res != OC_STACK_OK)
619 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
623 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
624 if(res != OC_STACK_OK)
626 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
632 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
635 res = initCloudServerResource(isSecured);
636 if(res != OC_STACK_OK)
638 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
642 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
643 if(res != OC_STACK_OK)
645 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
650 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
653 res = initDevConfResource(isSecured);
654 if(res != OC_STACK_OK)
656 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
660 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
661 if(res != OC_STACK_OK)
663 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
669 if(maskFlag == false)
671 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
672 return OC_STACK_ERROR;
676 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
681 OCStackResult DeleteProvisioningResource()
683 OCStackResult res = OCDeleteResource(gProvResource.handle);
684 if (res != OC_STACK_OK)
686 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
692 OCStackResult DeleteEasySetupResources()
694 OCStackResult res = OC_STACK_ERROR;
695 if (gWiFiResource.handle != NULL)
697 res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
698 if(res != OC_STACK_OK)
700 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
703 if (gCloudResource.handle != NULL)
705 res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
706 if(res != OC_STACK_OK)
708 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
711 if (gDevConfResource.handle != NULL)
713 res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
714 if(res != OC_STACK_OK)
716 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
720 if (gWiFiResource.handle != NULL)
722 res = OCDeleteResource(gWiFiResource.handle);
723 if (res != OC_STACK_OK)
725 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
729 if(gCloudResource.handle != NULL)
731 res = OCDeleteResource(gCloudResource.handle);
732 if (res != OC_STACK_OK)
734 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
738 if(gDevConfResource.handle != NULL)
740 res = OCDeleteResource(gDevConfResource.handle);
741 if (res != OC_STACK_OK)
743 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
747 if(gProvResource.handle != NULL)
749 res = OCDeleteResource(gProvResource.handle);
750 if (res != OC_STACK_OK)
752 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
759 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
761 OCEntityHandlerResult ehResult = OC_EH_ERROR;
764 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
767 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
769 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
773 OCRepPayload *getResp = NULL;
775 if(ehRequest->resource == gProvResource.handle)
777 getResp = constructResponseOfProv(ehRequest);
779 else if(ehRequest->resource == gWiFiResource.handle)
781 getResp = constructResponseOfWiFi();
783 else if(ehRequest->resource == gCloudResource.handle)
785 getResp = constructResponseOfCloud();
787 else if(ehRequest->resource == gDevConfResource.handle)
789 getResp = constructResponseOfDevConf();
794 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
804 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
806 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
807 OCEntityHandlerResult ehResult = OC_EH_ERROR;
808 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
810 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
814 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
817 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
821 if(ehRequest->resource == gProvResource.handle)
823 updateProvResource(ehRequest, input);
825 else if(ehRequest->resource == gWiFiResource.handle)
827 updateWiFiResource(input);
829 else if(ehRequest->resource == gCloudResource.handle)
831 updateCloudResource(input);
833 else if(ehRequest->resource == gDevConfResource.handle)
835 updateDevConfResource(input);
838 OCRepPayload *getResp = NULL;
839 if(ehRequest->resource == gProvResource.handle)
841 getResp = constructResponseOfProv(ehRequest);
843 else if(ehRequest->resource == gWiFiResource.handle)
845 getResp = constructResponseOfWiFi();
847 else if(ehRequest->resource == gCloudResource.handle)
849 getResp = constructResponseOfCloud();
851 else if(ehRequest->resource == gDevConfResource.handle)
853 getResp = constructResponseOfDevConf();
858 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
868 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
869 OCRepPayload** payload)
873 OCEntityHandlerResult ehResult = OC_EH_ERROR;
878 * This is the entity handler for the registered resource.
879 * This is invoked by OCStack whenever it recevies a request for this resource.
881 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
882 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
885 OCEntityHandlerResult ehRet = OC_EH_OK;
886 OCEntityHandlerResponse response =
887 { 0, 0, OC_EH_ERROR, 0, 0,
890 OCRepPayload* payload = NULL;
892 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
894 if (OC_REST_GET == entityHandlerRequest->method)
896 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
897 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
899 else if (OC_REST_PUT == entityHandlerRequest->method)
901 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
903 //PUT request will be handled in the internal implementation
904 if (gProvResource.handle != NULL)
906 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
910 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
914 else if (OC_REST_POST == entityHandlerRequest->method)
916 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
917 if (gProvResource.handle != NULL)
919 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
923 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
928 // Format the response. Note this requires some info about the request
929 response.requestHandle = entityHandlerRequest->requestHandle;
930 response.resourceHandle = entityHandlerRequest->resource;
931 response.ehResult = ehRet;
932 //response uses OCPaylod while all get,put methodes use OCRepPayload
933 response.payload = (OCPayload*) (payload);
934 response.numSendVendorSpecificHeaderOptions = 0;
935 memset(response.sendVendorSpecificHeaderOptions, 0,
936 sizeof(response.sendVendorSpecificHeaderOptions));
937 memset(response.resourceUri, 0, sizeof(response.resourceUri));
938 // Indicate that response is NOT in a persistent buffer
939 response.persistentBufferFlag = 0;
942 if (OCDoResponse(&response) != OC_STACK_OK)
944 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
952 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
954 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
956 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
957 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
960 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
962 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
963 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
966 gWiFiResource.numMode = modeIdx;
968 OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
969 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
971 OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
972 (deviceProperty->DevConf).modelNumber);
973 OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
975 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
979 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
981 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
983 gProvResource.status = esState;
984 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
986 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
990 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
992 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
994 gProvResource.lastErrCode = esErrCode;
995 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
997 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1001 const char *getResult(OCStackResult result)
1006 return "OC_STACK_OK";
1007 case OC_STACK_INVALID_URI:
1008 return "OC_STACK_INVALID_URI";
1009 case OC_STACK_INVALID_QUERY:
1010 return "OC_STACK_INVALID_QUERY";
1011 case OC_STACK_INVALID_IP:
1012 return "OC_STACK_INVALID_IP";
1013 case OC_STACK_INVALID_PORT:
1014 return "OC_STACK_INVALID_PORT";
1015 case OC_STACK_INVALID_CALLBACK:
1016 return "OC_STACK_INVALID_CALLBACK";
1017 case OC_STACK_INVALID_METHOD:
1018 return "OC_STACK_INVALID_METHOD";
1019 case OC_STACK_NO_MEMORY:
1020 return "OC_STACK_NO_MEMORY";
1021 case OC_STACK_COMM_ERROR:
1022 return "OC_STACK_COMM_ERROR";
1023 case OC_STACK_INVALID_PARAM:
1024 return "OC_STACK_INVALID_PARAM";
1025 case OC_STACK_NOTIMPL:
1026 return "OC_STACK_NOTIMPL";
1027 case OC_STACK_NO_RESOURCE:
1028 return "OC_STACK_NO_RESOURCE";
1029 case OC_STACK_RESOURCE_ERROR:
1030 return "OC_STACK_RESOURCE_ERROR";
1031 case OC_STACK_SLOW_RESOURCE:
1032 return "OC_STACK_SLOW_RESOURCE";
1033 case OC_STACK_NO_OBSERVERS:
1034 return "OC_STACK_NO_OBSERVERS";
1035 case OC_STACK_ERROR:
1036 return "OC_STACK_ERROR";