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_SSIDLEN, gWiFiResource.ssid);
115 OICStrcpy(pass, MAX_CREDLEN, 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.country, sizeof(gDevConfResource.country), "");
229 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
233 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
234 OC_RSRVD_INTERFACE_DEFAULT,
235 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
236 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
239 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
240 OC_RSRVD_INTERFACE_DEFAULT,
241 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
242 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
245 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
250 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
252 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
256 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
258 // When Provisioning resource has a POST with BatchInterface
259 updateCloudResource(input);
260 updateWiFiResource(input);
261 updateDevConfResource(input);
266 void updateWiFiResource(OCRepPayload* input)
268 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
271 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
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));
333 if(cloudData == NULL)
335 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
337 cloudData->userdata = NULL;
339 char *authCode = NULL;
340 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
342 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
343 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
344 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
347 char *authProvider = NULL;
348 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
350 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
351 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
352 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
355 char *ciServer = NULL;
356 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
358 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
359 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
360 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
365 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, cloudData->userdata);
368 if(authCode || authProvider || ciServer)
370 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
372 // TODO : Need to check appropriateness of gCloudData
373 if(gCloudRsrcEvtCb != NULL)
375 gCloudRsrcEvtCb(ES_OK, cloudData);
379 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
386 void updateDevConfResource(OCRepPayload* input)
388 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
389 if(devConfData == NULL)
391 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
393 devConfData->userdata = NULL;
395 char *country = NULL;
396 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
398 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
399 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
400 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
403 char *language = NULL;
404 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
406 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
407 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
408 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
413 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, devConfData->userdata);
416 if(country || language)
418 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
420 // TODO : Need to check appropriateness of gDevConfData
421 if(gDevConfRsrcEvtCb != NULL)
423 gDevConfRsrcEvtCb(ES_OK, devConfData);
427 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
431 OICFree(devConfData);
434 OCRepPayload* constructResponseOfWiFi()
436 OCRepPayload* payload = OCRepPayloadCreate();
439 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
443 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
444 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
446 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
447 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
448 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
449 modes_64[i] = gWiFiResource.supportedMode[i];
450 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
452 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
453 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
454 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
455 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
456 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
460 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
466 OCRepPayload* constructResponseOfCloud()
468 OCRepPayload* payload = OCRepPayloadCreate();
471 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
475 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
476 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
477 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
478 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
479 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
482 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
487 OCRepPayload* constructResponseOfDevConf()
489 OCRepPayload* payload = OCRepPayloadCreate();
492 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
496 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
497 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
498 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
499 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
500 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
504 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
510 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
512 OCRepPayload* payload = OCRepPayloadCreate();
515 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
519 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
520 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
521 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
522 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
523 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
527 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
532 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
533 {// When Provisioning resource has a GET with BatchInterface
534 payload->next = constructResponseOfWiFi();
537 payload->next->next = constructResponseOfCloud();
541 if(payload->next->next)
542 payload->next->next->next = constructResponseOfDevConf();
552 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
554 OCStackResult res = OC_STACK_ERROR;
555 bool maskFlag = false;
557 res = initProvResource(isSecured);
558 if(res != OC_STACK_OK)
560 // TODO: destroy logic will be added
561 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
566 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
569 res = initWiFiResource(isSecured);
570 if(res != OC_STACK_OK)
572 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
576 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
577 if(res != OC_STACK_OK)
579 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
585 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
588 res = initCloudServerResource(isSecured);
589 if(res != OC_STACK_OK)
591 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
595 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
596 if(res != OC_STACK_OK)
598 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
603 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
606 res = initDevConfResource(isSecured);
607 if(res != OC_STACK_OK)
609 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
613 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
614 if(res != OC_STACK_OK)
616 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
622 if(maskFlag == false)
624 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
625 return OC_STACK_ERROR;
629 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
634 OCStackResult DeleteProvisioningResource()
636 OCStackResult res = OCDeleteResource(gProvResource.handle);
637 if (res != OC_STACK_OK)
639 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
645 OCStackResult DeleteEasySetupResources()
647 OCStackResult res = OCDeleteResource(gWiFiResource.handle);
648 if (res != OC_STACK_OK)
650 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
652 res = OCDeleteResource(gCloudResource.handle);
653 if (res != OC_STACK_OK)
655 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
657 res = OCDeleteResource(gDevConfResource.handle);
658 if (res != OC_STACK_OK)
660 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
663 res = OCDeleteResource(gProvResource.handle);
664 if (res != OC_STACK_OK)
666 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
672 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
674 OCEntityHandlerResult ehResult = OC_EH_ERROR;
677 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
680 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
682 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
686 OCRepPayload *getResp = NULL;
688 if(ehRequest->resource == gProvResource.handle)
690 getResp = constructResponseOfProv(ehRequest);
692 else if(ehRequest->resource == gWiFiResource.handle)
694 getResp = constructResponseOfWiFi();
696 else if(ehRequest->resource == gCloudResource.handle)
698 getResp = constructResponseOfCloud();
700 else if(ehRequest->resource == gDevConfResource.handle)
702 getResp = constructResponseOfDevConf();
707 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
717 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
719 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
720 OCEntityHandlerResult ehResult = OC_EH_ERROR;
721 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
723 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
727 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
730 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
734 if(ehRequest->resource == gProvResource.handle)
736 updateProvResource(ehRequest, input);
738 else if(ehRequest->resource == gWiFiResource.handle)
740 updateWiFiResource(input);
742 else if(ehRequest->resource == gCloudResource.handle)
744 updateCloudResource(input);
746 else if(ehRequest->resource == gDevConfResource.handle)
748 updateDevConfResource(input);
751 OCRepPayload *getResp = NULL;
752 if(ehRequest->resource == gProvResource.handle)
754 getResp = constructResponseOfProv(ehRequest);
756 else if(ehRequest->resource == gWiFiResource.handle)
758 getResp = constructResponseOfWiFi();
760 else if(ehRequest->resource == gCloudResource.handle)
762 getResp = constructResponseOfCloud();
764 else if(ehRequest->resource == gDevConfResource.handle)
766 getResp = constructResponseOfDevConf();
771 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
781 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
782 OCRepPayload** payload)
786 OCEntityHandlerResult ehResult = OC_EH_ERROR;
791 * This is the entity handler for the registered resource.
792 * This is invoked by OCStack whenever it recevies a request for this resource.
794 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
795 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
798 OCEntityHandlerResult ehRet = OC_EH_OK;
799 OCEntityHandlerResponse response =
800 { 0, 0, OC_EH_ERROR, 0, 0,
803 OCRepPayload* payload = NULL;
805 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
807 if (OC_REST_GET == entityHandlerRequest->method)
809 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
810 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
812 else if (OC_REST_PUT == entityHandlerRequest->method)
814 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
816 //PUT request will be handled in the internal implementation
817 if (gProvResource.handle != NULL)
819 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
823 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
827 else if (OC_REST_POST == entityHandlerRequest->method)
829 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
830 if (gProvResource.handle != NULL)
832 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
836 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
841 if (ehRet == OC_EH_OK)
843 // Format the response. Note this requires some info about the request
844 response.requestHandle = entityHandlerRequest->requestHandle;
845 response.resourceHandle = entityHandlerRequest->resource;
846 response.ehResult = ehRet;
847 //response uses OCPaylod while all get,put methodes use OCRepPayload
848 response.payload = (OCPayload*) (payload);
849 response.numSendVendorSpecificHeaderOptions = 0;
850 memset(response.sendVendorSpecificHeaderOptions, 0,
851 sizeof(response.sendVendorSpecificHeaderOptions));
852 memset(response.resourceUri, 0, sizeof(response.resourceUri));
853 // Indicate that response is NOT in a persistent buffer
854 response.persistentBufferFlag = 0;
857 if (OCDoResponse(&response) != OC_STACK_OK)
859 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
868 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
870 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
872 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
873 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
876 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
878 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
879 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
882 gWiFiResource.numMode = modeIdx;
884 OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
885 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
887 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
891 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
893 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
895 gProvResource.status = esState;
896 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
898 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
902 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
904 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
906 gProvResource.lastErrCode = esErrCode;
907 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
909 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
913 const char *getResult(OCStackResult result)
918 return "OC_STACK_OK";
919 case OC_STACK_INVALID_URI:
920 return "OC_STACK_INVALID_URI";
921 case OC_STACK_INVALID_QUERY:
922 return "OC_STACK_INVALID_QUERY";
923 case OC_STACK_INVALID_IP:
924 return "OC_STACK_INVALID_IP";
925 case OC_STACK_INVALID_PORT:
926 return "OC_STACK_INVALID_PORT";
927 case OC_STACK_INVALID_CALLBACK:
928 return "OC_STACK_INVALID_CALLBACK";
929 case OC_STACK_INVALID_METHOD:
930 return "OC_STACK_INVALID_METHOD";
931 case OC_STACK_NO_MEMORY:
932 return "OC_STACK_NO_MEMORY";
933 case OC_STACK_COMM_ERROR:
934 return "OC_STACK_COMM_ERROR";
935 case OC_STACK_INVALID_PARAM:
936 return "OC_STACK_INVALID_PARAM";
937 case OC_STACK_NOTIMPL:
938 return "OC_STACK_NOTIMPL";
939 case OC_STACK_NO_RESOURCE:
940 return "OC_STACK_NO_RESOURCE";
941 case OC_STACK_RESOURCE_ERROR:
942 return "OC_STACK_RESOURCE_ERROR";
943 case OC_STACK_SLOW_RESOURCE:
944 return "OC_STACK_SLOW_RESOURCE";
945 case OC_STACK_NO_OBSERVERS:
946 return "OC_STACK_NO_OBSERVERS";
948 return "OC_STACK_ERROR";