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"
25 #include "ocpayload.h"
26 #include "oic_string.h"
30 * @brief Logging tag for module name.
32 #define ES_RH_TAG "ES_RH"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
39 * @brief Structure for holding the Provisioning status and target information required to
40 * connect to the target network
42 static ProvResource gProvResource;
43 static WiFiResource gWiFiResource;
44 static CloudResource gCloudResource;
45 static DevConfResource gDevConfResource;
49 * @brief Structure for holding the target information required to
50 * connect to the target network
52 static ESWiFiProvData gWiFiData;
56 * @brief Structure for holding the device information
58 static ESDevConfProvData gDevConfData;
62 * @brief Structure for holding the cloud information required to
63 * connect to CI Server
65 static ESCloudProvData gCloudData;
69 //-----------------------------------------------------------------------------
70 // Private internal function prototypes
71 //-----------------------------------------------------------------------------
72 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
74 const char *getResult(OCStackResult result);
75 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
76 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
77 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
78 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
79 void updateWiFiResource(OCRepPayload* input);
80 void updateCloudResource(OCRepPayload* input);
81 void updateDevConfResource(OCRepPayload* input);
83 ESWiFiCB gWifiRsrcEvtCb = NULL;
84 ESCloudCB gCloudRsrcEvtCb = NULL;
85 ESDevConfCB gDevConfRsrcEvtCb = NULL;
87 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
92 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
97 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
99 gDevConfRsrcEvtCb = cb;
102 void UnRegisterResourceEventCallBack()
106 gWifiRsrcEvtCb = NULL;
110 gCloudRsrcEvtCb = NULL;
112 if (gDevConfRsrcEvtCb)
114 gDevConfRsrcEvtCb = NULL;
118 void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
120 if (name != NULL && pass != NULL)
122 OICStrcpy(name, MAX_SSIDLEN, gWiFiResource.ssid);
123 OICStrcpy(pass, MAX_CREDLEN, gWiFiResource.cred);
127 OCStackResult initProvResource(bool isSecured)
129 gProvResource.status = ES_STATE_INIT;
130 gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
131 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
133 OCStackResult res = OC_STACK_ERROR;
136 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
137 OC_RSRVD_INTERFACE_DEFAULT,
138 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
139 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
142 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
143 OC_RSRVD_INTERFACE_DEFAULT,
144 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
145 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
149 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
153 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
156 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
159 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
162 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
166 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
170 OCStackResult initWiFiResource(bool isSecured)
172 OCStackResult res = OC_STACK_ERROR;
174 gWiFiResource.supportedFreq = WiFi_BOTH;
175 gWiFiResource.supportedMode[0] = WiFi_11A;
176 gWiFiResource.supportedMode[1] = WiFi_11B;
177 gWiFiResource.supportedMode[2] = WiFi_11G;
178 gWiFiResource.supportedMode[3] = WiFi_11N;
179 gWiFiResource.numMode = 4;
180 gWiFiResource.authType = NONE_AUTH;
181 gWiFiResource.encType = NONE_ENC;
182 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
183 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
187 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
188 OC_RSRVD_INTERFACE_DEFAULT,
189 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
190 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
193 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
194 OC_RSRVD_INTERFACE_DEFAULT,
195 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
196 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
199 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
204 OCStackResult initCloudServerResource(bool isSecured)
206 OCStackResult res = OC_STACK_ERROR;
208 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
209 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
210 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
214 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
215 OC_RSRVD_INTERFACE_DEFAULT,
216 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
217 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
220 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
221 OC_RSRVD_INTERFACE_DEFAULT,
222 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
223 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
226 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
231 OCStackResult initDevConfResource(bool isSecured)
233 OCStackResult res = OC_STACK_ERROR;
235 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
236 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
237 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
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 | OC_SECURE);
247 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
248 OC_RSRVD_INTERFACE_DEFAULT,
249 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
250 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
253 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
258 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
260 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
264 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
266 // When Provisioning resource has a POST with BatchInterface
267 updateCloudResource(input);
268 updateWiFiResource(input);
269 updateDevConfResource(input);
274 void updateWiFiResource(OCRepPayload* input)
277 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
279 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
280 OICStrcpy(gWiFiData.ssid, sizeof(gWiFiData.ssid), ssid);
281 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
285 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
287 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
288 OICStrcpy(gWiFiData.pwd, sizeof(gWiFiData.pwd), cred);
289 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
292 int64_t authType = -1;
293 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
295 gWiFiResource.authType = authType;
296 gWiFiData.authtype = gWiFiResource.authType;
297 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
300 int64_t encType = -1;
301 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
303 gWiFiResource.encType = encType;
304 gWiFiData.enctype = gWiFiResource.encType;
305 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
308 if(ssid || cred || authType!= -1 || encType != -1)
310 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
312 // TODO : Need to check appropriateness of gWiFiData
313 if(gWifiRsrcEvtCb != NULL)
315 gWifiRsrcEvtCb(ES_OK, &gWiFiData);
319 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
325 void updateCloudResource(OCRepPayload* input)
327 char *authCode = NULL;
328 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
330 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
331 OICStrcpy(gCloudData.authCode, sizeof(gCloudData.authCode), authCode);
332 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
335 char *authProvider = NULL;
336 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
338 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
339 OICStrcpy(gCloudData.authProvider, sizeof(gCloudData.authProvider), authProvider);
340 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
343 char *ciServer = NULL;
344 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
346 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
347 OICStrcpy(gCloudData.ciServer, sizeof(gCloudData.ciServer), ciServer);
348 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
351 char *serverID = NULL;
352 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SERVERID, &serverID))
354 OICStrcpy(gCloudResource.serverID, sizeof(gCloudResource.serverID), serverID);
355 OICStrcpy(gCloudData.serverID, sizeof(gCloudData.serverID), serverID);
356 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.serverID %s", gCloudResource.serverID);
359 if(authCode || authProvider || ciServer)
361 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
363 // TODO : Need to check appropriateness of gCloudData
364 if(gCloudRsrcEvtCb != NULL)
366 gCloudRsrcEvtCb(ES_OK, &gCloudData);
370 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
375 void updateDevConfResource(OCRepPayload* input)
377 char *country = NULL;
378 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
380 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
381 OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
382 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
385 char *language = NULL;
386 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
388 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
389 OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
390 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
393 if(country || language)
395 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
397 // TODO : Need to check appropriateness of gDevConfData
398 if(gDevConfRsrcEvtCb != NULL)
400 gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
404 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
409 OCRepPayload* constructResponseOfWiFi()
411 OCRepPayload* payload = OCRepPayloadCreate();
414 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
418 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
419 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
421 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
422 int64_t *modes_64 = (int64_t *)malloc(gWiFiResource.numMode * sizeof(int64_t));
423 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
424 modes_64[i] = gWiFiResource.supportedMode[i];
425 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
427 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
428 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
429 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
430 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
431 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
436 OCRepPayload* constructResponseOfCloud()
438 OCRepPayload* payload = OCRepPayloadCreate();
441 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
445 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
446 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
447 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
448 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
449 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
450 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SERVERID, gCloudResource.serverID);
455 OCRepPayload* constructResponseOfDevConf()
457 OCRepPayload* payload = OCRepPayloadCreate();
460 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
464 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
465 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
466 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
467 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
468 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
473 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
475 OCRepPayload* payload = OCRepPayloadCreate();
478 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
482 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
483 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
484 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
485 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
486 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
490 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
491 {// When Provisioning resource has a GET with BatchInterface
492 payload->next = constructResponseOfWiFi();
495 payload->next->next = constructResponseOfCloud();
499 if(payload->next->next)
500 payload->next->next->next = constructResponseOfDevConf();
510 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
512 OCStackResult res = OC_STACK_ERROR;
513 bool maskFlag = false;
515 res = initProvResource(isSecured);
516 if(res != OC_STACK_OK)
518 // TODO: destroy logic will be added
519 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
524 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
527 res = initWiFiResource(isSecured);
528 if(res != OC_STACK_OK)
530 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
534 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
535 if(res != OC_STACK_OK)
537 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
543 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
546 res = initCloudServerResource(isSecured);
547 if(res != OC_STACK_OK)
549 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
553 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
554 if(res != OC_STACK_OK)
556 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
561 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
564 res = initDevConfResource(isSecured);
565 if(res != OC_STACK_OK)
567 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
571 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
572 if(res != OC_STACK_OK)
574 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
580 if(maskFlag == false)
582 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
583 return OC_STACK_ERROR;
587 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
592 OCStackResult DeleteProvisioningResource()
594 OCStackResult res = OCDeleteResource(gProvResource.handle);
595 if (res != OC_STACK_OK)
597 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
603 OCStackResult DeleteEasySetupResources()
605 OCStackResult res = OCDeleteResource(gWiFiResource.handle);
606 if (res != OC_STACK_OK)
608 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
610 res = OCDeleteResource(gCloudResource.handle);
611 if (res != OC_STACK_OK)
613 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
615 res = OCDeleteResource(gDevConfResource.handle);
616 if (res != OC_STACK_OK)
618 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
621 res = OCDeleteResource(gProvResource.handle);
622 if (res != OC_STACK_OK)
624 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
630 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
632 OCEntityHandlerResult ehResult = OC_EH_ERROR;
635 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
638 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
640 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
644 OCRepPayload *getResp = NULL;
646 if(ehRequest->resource == gProvResource.handle)
647 getResp = constructResponseOfProv(ehRequest);
648 else if(ehRequest->resource == gWiFiResource.handle)
649 getResp = constructResponseOfWiFi();
650 else if(ehRequest->resource == gCloudResource.handle)
651 getResp = constructResponseOfCloud();
652 else if(ehRequest->resource == gDevConfResource.handle)
653 getResp = constructResponseOfDevConf();
657 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
667 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
669 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
670 OCEntityHandlerResult ehResult = OC_EH_ERROR;
671 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
673 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
677 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
680 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
684 // TBD : Discuss about triggering flag (to be existed or not)
685 // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
686 // A new request for provisioning means overriding existing network provisioning information.
688 if(ehRequest->resource == gProvResource.handle)
689 updateProvResource(ehRequest, input);
690 else if(ehRequest->resource == gWiFiResource.handle)
691 updateWiFiResource(input);
692 else if(ehRequest->resource == gCloudResource.handle)
693 updateCloudResource(input);
694 else if(ehRequest->resource == gDevConfResource.handle)
695 updateDevConfResource(input);
697 OCRepPayload *getResp = NULL;
698 if(ehRequest->resource == gProvResource.handle)
699 getResp = constructResponseOfProv(ehRequest);
700 else if(ehRequest->resource == gWiFiResource.handle)
701 getResp = constructResponseOfWiFi();
702 else if(ehRequest->resource == gCloudResource.handle)
703 getResp = constructResponseOfCloud();
704 else if(ehRequest->resource == gDevConfResource.handle)
705 getResp = constructResponseOfDevConf();
709 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
719 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
720 OCRepPayload** payload)
724 OCEntityHandlerResult ehResult = OC_EH_ERROR;
729 * This is the entity handler for the registered resource.
730 * This is invoked by OCStack whenever it recevies a request for this resource.
732 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
733 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
736 OCEntityHandlerResult ehRet = OC_EH_OK;
737 OCEntityHandlerResponse response =
738 { 0, 0, OC_EH_ERROR, 0, 0,
741 OCRepPayload* payload = NULL;
743 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
745 if (OC_REST_GET == entityHandlerRequest->method)
747 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
748 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
750 else if (OC_REST_PUT == entityHandlerRequest->method)
752 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
754 //PUT request will be handled in the internal implementation
755 if (gProvResource.handle != NULL)
757 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
761 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
765 else if (OC_REST_POST == entityHandlerRequest->method)
767 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
768 if (gProvResource.handle != NULL)
770 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
774 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
779 if (ehRet == OC_EH_OK)
781 // Format the response. Note this requires some info about the request
782 response.requestHandle = entityHandlerRequest->requestHandle;
783 response.resourceHandle = entityHandlerRequest->resource;
784 response.ehResult = ehRet;
785 //response uses OCPaylod while all get,put methodes use OCRepPayload
786 response.payload = (OCPayload*) (payload);
787 response.numSendVendorSpecificHeaderOptions = 0;
788 memset(response.sendVendorSpecificHeaderOptions, 0,
789 sizeof(response.sendVendorSpecificHeaderOptions));
790 memset(response.resourceUri, 0, sizeof(response.resourceUri));
791 // Indicate that response is NOT in a persistent buffer
792 response.persistentBufferFlag = 0;
795 if (OCDoResponse(&response) != OC_STACK_OK)
797 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
806 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
808 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
810 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
811 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
814 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
816 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
817 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
820 gWiFiResource.numMode = modeIdx;
822 OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
823 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
825 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
829 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
831 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
833 gProvResource.status = esState;
834 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
836 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
840 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
842 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
844 gProvResource.lastErrCode = esErrCode;
845 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
847 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
850 const char *getResult(OCStackResult result)
855 return "OC_STACK_OK";
856 case OC_STACK_INVALID_URI:
857 return "OC_STACK_INVALID_URI";
858 case OC_STACK_INVALID_QUERY:
859 return "OC_STACK_INVALID_QUERY";
860 case OC_STACK_INVALID_IP:
861 return "OC_STACK_INVALID_IP";
862 case OC_STACK_INVALID_PORT:
863 return "OC_STACK_INVALID_PORT";
864 case OC_STACK_INVALID_CALLBACK:
865 return "OC_STACK_INVALID_CALLBACK";
866 case OC_STACK_INVALID_METHOD:
867 return "OC_STACK_INVALID_METHOD";
868 case OC_STACK_NO_MEMORY:
869 return "OC_STACK_NO_MEMORY";
870 case OC_STACK_COMM_ERROR:
871 return "OC_STACK_COMM_ERROR";
872 case OC_STACK_INVALID_PARAM:
873 return "OC_STACK_INVALID_PARAM";
874 case OC_STACK_NOTIMPL:
875 return "OC_STACK_NOTIMPL";
876 case OC_STACK_NO_RESOURCE:
877 return "OC_STACK_NO_RESOURCE";
878 case OC_STACK_RESOURCE_ERROR:
879 return "OC_STACK_RESOURCE_ERROR";
880 case OC_STACK_SLOW_RESOURCE:
881 return "OC_STACK_SLOW_RESOURCE";
882 case OC_STACK_NO_OBSERVERS:
883 return "OC_STACK_NO_OBSERVERS";
885 return "OC_STACK_ERROR";