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));
272 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
276 memset(wiFiData->ssid, 0, MAX_SSIDLEN);
277 memset(wiFiData->pwd, 0, MAX_CREDLEN);
278 wiFiData->authtype = NONE_AUTH;
279 wiFiData->enctype = NONE_AUTH;
280 wiFiData->userdata = NULL;
283 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
285 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
286 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
287 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
291 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
293 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
294 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
295 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
298 int64_t authType = -1;
299 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
301 gWiFiResource.authType = authType;
302 wiFiData->authtype = gWiFiResource.authType;
303 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
306 int64_t encType = -1;
307 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
309 gWiFiResource.encType = encType;
310 wiFiData->enctype = gWiFiResource.encType;
311 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
316 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
319 if(ssid || cred || authType!= -1 || encType != -1)
321 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
323 // TODO : Need to check appropriateness of gWiFiData
324 if(gWifiRsrcEvtCb != NULL)
326 gWifiRsrcEvtCb(ES_OK, wiFiData);
330 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
337 void updateCloudResource(OCRepPayload* input)
339 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
341 if(cloudData == NULL)
343 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
347 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
348 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
349 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
350 cloudData->userdata = NULL;
352 char *authCode = NULL;
353 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
355 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
356 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
357 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
360 char *authProvider = NULL;
361 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
363 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
364 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
365 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
368 char *ciServer = NULL;
369 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
371 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
372 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
373 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
378 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
381 if(authCode || authProvider || ciServer)
383 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
385 // TODO : Need to check appropriateness of gCloudData
386 if(gCloudRsrcEvtCb != NULL)
388 gCloudRsrcEvtCb(ES_OK, cloudData);
392 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
399 void updateDevConfResource(OCRepPayload* input)
401 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
403 if(devConfData == NULL)
405 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
408 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
409 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
410 devConfData->userdata = NULL;
412 char *country = NULL;
413 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
415 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
416 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
417 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
420 char *language = NULL;
421 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
423 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
424 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
425 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
430 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
433 if(country || language)
435 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
437 // TODO : Need to check appropriateness of gDevConfData
438 if(gDevConfRsrcEvtCb != NULL)
440 gDevConfRsrcEvtCb(ES_OK, devConfData);
444 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
448 OICFree(devConfData);
451 OCRepPayload* constructResponseOfWiFi()
453 OCRepPayload* payload = OCRepPayloadCreate();
456 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
460 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
461 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
463 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
464 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
465 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
467 modes_64[i] = gWiFiResource.supportedMode[i];
469 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
471 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
472 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
473 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
474 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
475 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
479 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
485 OCRepPayload* constructResponseOfCloud()
487 OCRepPayload* payload = OCRepPayloadCreate();
490 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
494 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
495 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
496 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
497 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
498 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
502 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
508 OCRepPayload* constructResponseOfDevConf()
510 OCRepPayload* payload = OCRepPayloadCreate();
513 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
517 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
518 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
519 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
520 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
521 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
525 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
531 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
533 OCRepPayload* payload = OCRepPayloadCreate();
536 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
540 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
541 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
542 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
543 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
544 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
548 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
553 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
554 {// When Provisioning resource has a GET with BatchInterface
555 payload->next = constructResponseOfWiFi();
559 payload->next->next = constructResponseOfCloud();
566 if(payload->next->next)
568 payload->next->next->next = constructResponseOfDevConf();
581 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
583 OCStackResult res = OC_STACK_ERROR;
584 bool maskFlag = false;
586 res = initProvResource(isSecured);
587 if(res != OC_STACK_OK)
589 // TODO: destroy logic will be added
590 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
595 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
598 res = initWiFiResource(isSecured);
599 if(res != OC_STACK_OK)
601 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
605 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
606 if(res != OC_STACK_OK)
608 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
614 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
617 res = initCloudServerResource(isSecured);
618 if(res != OC_STACK_OK)
620 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
624 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
625 if(res != OC_STACK_OK)
627 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
632 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
635 res = initDevConfResource(isSecured);
636 if(res != OC_STACK_OK)
638 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
642 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
643 if(res != OC_STACK_OK)
645 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
651 if(maskFlag == false)
653 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
654 return OC_STACK_ERROR;
658 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
663 OCStackResult DeleteProvisioningResource()
665 OCStackResult res = OCDeleteResource(gProvResource.handle);
666 if (res != OC_STACK_OK)
668 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
674 OCStackResult DeleteEasySetupResources()
676 OCStackResult res = OCDeleteResource(gWiFiResource.handle);
677 if (res != OC_STACK_OK)
679 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
681 res = OCDeleteResource(gCloudResource.handle);
682 if (res != OC_STACK_OK)
684 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
686 res = OCDeleteResource(gDevConfResource.handle);
687 if (res != OC_STACK_OK)
689 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
692 res = OCDeleteResource(gProvResource.handle);
693 if (res != OC_STACK_OK)
695 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
701 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
703 OCEntityHandlerResult ehResult = OC_EH_ERROR;
706 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
709 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
711 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
715 OCRepPayload *getResp = NULL;
717 if(ehRequest->resource == gProvResource.handle)
719 getResp = constructResponseOfProv(ehRequest);
721 else if(ehRequest->resource == gWiFiResource.handle)
723 getResp = constructResponseOfWiFi();
725 else if(ehRequest->resource == gCloudResource.handle)
727 getResp = constructResponseOfCloud();
729 else if(ehRequest->resource == gDevConfResource.handle)
731 getResp = constructResponseOfDevConf();
736 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
746 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
748 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
749 OCEntityHandlerResult ehResult = OC_EH_ERROR;
750 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
752 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
756 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
759 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
763 if(ehRequest->resource == gProvResource.handle)
765 updateProvResource(ehRequest, input);
767 else if(ehRequest->resource == gWiFiResource.handle)
769 updateWiFiResource(input);
771 else if(ehRequest->resource == gCloudResource.handle)
773 updateCloudResource(input);
775 else if(ehRequest->resource == gDevConfResource.handle)
777 updateDevConfResource(input);
780 OCRepPayload *getResp = NULL;
781 if(ehRequest->resource == gProvResource.handle)
783 getResp = constructResponseOfProv(ehRequest);
785 else if(ehRequest->resource == gWiFiResource.handle)
787 getResp = constructResponseOfWiFi();
789 else if(ehRequest->resource == gCloudResource.handle)
791 getResp = constructResponseOfCloud();
793 else if(ehRequest->resource == gDevConfResource.handle)
795 getResp = constructResponseOfDevConf();
800 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
810 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
811 OCRepPayload** payload)
815 OCEntityHandlerResult ehResult = OC_EH_ERROR;
820 * This is the entity handler for the registered resource.
821 * This is invoked by OCStack whenever it recevies a request for this resource.
823 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
824 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
827 OCEntityHandlerResult ehRet = OC_EH_OK;
828 OCEntityHandlerResponse response =
829 { 0, 0, OC_EH_ERROR, 0, 0,
832 OCRepPayload* payload = NULL;
834 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
836 if (OC_REST_GET == entityHandlerRequest->method)
838 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
839 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
841 else if (OC_REST_PUT == entityHandlerRequest->method)
843 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
845 //PUT request will be handled in the internal implementation
846 if (gProvResource.handle != NULL)
848 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
852 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
856 else if (OC_REST_POST == entityHandlerRequest->method)
858 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
859 if (gProvResource.handle != NULL)
861 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
865 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
870 if (ehRet == OC_EH_OK)
872 // Format the response. Note this requires some info about the request
873 response.requestHandle = entityHandlerRequest->requestHandle;
874 response.resourceHandle = entityHandlerRequest->resource;
875 response.ehResult = ehRet;
876 //response uses OCPaylod while all get,put methodes use OCRepPayload
877 response.payload = (OCPayload*) (payload);
878 response.numSendVendorSpecificHeaderOptions = 0;
879 memset(response.sendVendorSpecificHeaderOptions, 0,
880 sizeof(response.sendVendorSpecificHeaderOptions));
881 memset(response.resourceUri, 0, sizeof(response.resourceUri));
882 // Indicate that response is NOT in a persistent buffer
883 response.persistentBufferFlag = 0;
886 if (OCDoResponse(&response) != OC_STACK_OK)
888 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
897 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
899 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
901 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
902 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
905 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
907 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
908 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
911 gWiFiResource.numMode = modeIdx;
913 OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
914 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
916 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
920 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
922 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
924 gProvResource.status = esState;
925 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
927 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
931 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
933 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
935 gProvResource.lastErrCode = esErrCode;
936 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
938 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
942 const char *getResult(OCStackResult result)
947 return "OC_STACK_OK";
948 case OC_STACK_INVALID_URI:
949 return "OC_STACK_INVALID_URI";
950 case OC_STACK_INVALID_QUERY:
951 return "OC_STACK_INVALID_QUERY";
952 case OC_STACK_INVALID_IP:
953 return "OC_STACK_INVALID_IP";
954 case OC_STACK_INVALID_PORT:
955 return "OC_STACK_INVALID_PORT";
956 case OC_STACK_INVALID_CALLBACK:
957 return "OC_STACK_INVALID_CALLBACK";
958 case OC_STACK_INVALID_METHOD:
959 return "OC_STACK_INVALID_METHOD";
960 case OC_STACK_NO_MEMORY:
961 return "OC_STACK_NO_MEMORY";
962 case OC_STACK_COMM_ERROR:
963 return "OC_STACK_COMM_ERROR";
964 case OC_STACK_INVALID_PARAM:
965 return "OC_STACK_INVALID_PARAM";
966 case OC_STACK_NOTIMPL:
967 return "OC_STACK_NOTIMPL";
968 case OC_STACK_NO_RESOURCE:
969 return "OC_STACK_NO_RESOURCE";
970 case OC_STACK_RESOURCE_ERROR:
971 return "OC_STACK_RESOURCE_ERROR";
972 case OC_STACK_SLOW_RESOURCE:
973 return "OC_STACK_SLOW_RESOURCE";
974 case OC_STACK_NO_OBSERVERS:
975 return "OC_STACK_NO_OBSERVERS";
977 return "OC_STACK_ERROR";