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 if(authCode || authProvider || ciServer)
353 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
355 // TODO : Need to check appropriateness of gCloudData
356 if(gCloudRsrcEvtCb != NULL)
358 gCloudRsrcEvtCb(ES_OK, &gCloudData);
362 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
367 void updateDevConfResource(OCRepPayload* input)
369 char *country = NULL;
370 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
372 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
373 OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
374 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
377 char *language = NULL;
378 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
380 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
381 OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
382 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
385 if(country || language)
387 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
389 // TODO : Need to check appropriateness of gDevConfData
390 if(gDevConfRsrcEvtCb != NULL)
392 gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
396 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
401 OCRepPayload* constructResponseOfWiFi()
403 OCRepPayload* payload = OCRepPayloadCreate();
406 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
410 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
411 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
413 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
414 int64_t *modes_64 = (int64_t *)malloc(gWiFiResource.numMode * sizeof(int64_t));
415 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
416 modes_64[i] = gWiFiResource.supportedMode[i];
417 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
419 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
420 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
421 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
422 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
423 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
428 OCRepPayload* constructResponseOfCloud()
430 OCRepPayload* payload = OCRepPayloadCreate();
433 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
437 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
438 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
439 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
440 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
441 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
446 OCRepPayload* constructResponseOfDevConf()
448 OCRepPayload* payload = OCRepPayloadCreate();
451 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
455 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
456 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
457 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
458 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
459 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
464 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
466 OCRepPayload* payload = OCRepPayloadCreate();
469 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
473 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
474 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
475 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
476 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
477 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
481 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
482 {// When Provisioning resource has a GET with BatchInterface
483 payload->next = constructResponseOfWiFi();
486 payload->next->next = constructResponseOfCloud();
490 if(payload->next->next)
491 payload->next->next->next = constructResponseOfDevConf();
501 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
503 OCStackResult res = OC_STACK_ERROR;
504 bool maskFlag = false;
506 res = initProvResource(isSecured);
507 if(res != OC_STACK_OK)
509 // TODO: destroy logic will be added
510 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
515 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
518 res = initWiFiResource(isSecured);
519 if(res != OC_STACK_OK)
521 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
525 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
526 if(res != OC_STACK_OK)
528 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
534 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
537 res = initCloudServerResource(isSecured);
538 if(res != OC_STACK_OK)
540 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
544 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
545 if(res != OC_STACK_OK)
547 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
552 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
555 res = initDevConfResource(isSecured);
556 if(res != OC_STACK_OK)
558 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
562 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
563 if(res != OC_STACK_OK)
565 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
571 if(maskFlag == false)
573 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
574 return OC_STACK_ERROR;
578 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
583 OCStackResult DeleteProvisioningResource()
585 OCStackResult res = OCDeleteResource(gProvResource.handle);
586 if (res != OC_STACK_OK)
588 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
594 OCStackResult DeleteEasySetupResources()
596 OCStackResult res = OCDeleteResource(gWiFiResource.handle);
597 if (res != OC_STACK_OK)
599 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
601 res = OCDeleteResource(gCloudResource.handle);
602 if (res != OC_STACK_OK)
604 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
606 res = OCDeleteResource(gDevConfResource.handle);
607 if (res != OC_STACK_OK)
609 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
612 res = OCDeleteResource(gProvResource.handle);
613 if (res != OC_STACK_OK)
615 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
621 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
623 OCEntityHandlerResult ehResult = OC_EH_ERROR;
626 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
629 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
631 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
635 OCRepPayload *getResp = NULL;
637 if(ehRequest->resource == gProvResource.handle)
638 getResp = constructResponseOfProv(ehRequest);
639 else if(ehRequest->resource == gWiFiResource.handle)
640 getResp = constructResponseOfWiFi();
641 else if(ehRequest->resource == gCloudResource.handle)
642 getResp = constructResponseOfCloud();
643 else if(ehRequest->resource == gDevConfResource.handle)
644 getResp = constructResponseOfDevConf();
648 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
658 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
660 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
661 OCEntityHandlerResult ehResult = OC_EH_ERROR;
662 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
664 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
668 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
671 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
675 // TBD : Discuss about triggering flag (to be existed or not)
676 // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
677 // A new request for provisioning means overriding existing network provisioning information.
679 if(ehRequest->resource == gProvResource.handle)
680 updateProvResource(ehRequest, input);
681 else if(ehRequest->resource == gWiFiResource.handle)
682 updateWiFiResource(input);
683 else if(ehRequest->resource == gCloudResource.handle)
684 updateCloudResource(input);
685 else if(ehRequest->resource == gDevConfResource.handle)
686 updateDevConfResource(input);
688 OCRepPayload *getResp = NULL;
689 if(ehRequest->resource == gProvResource.handle)
690 getResp = constructResponseOfProv(ehRequest);
691 else if(ehRequest->resource == gWiFiResource.handle)
692 getResp = constructResponseOfWiFi();
693 else if(ehRequest->resource == gCloudResource.handle)
694 getResp = constructResponseOfCloud();
695 else if(ehRequest->resource == gDevConfResource.handle)
696 getResp = constructResponseOfDevConf();
700 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
710 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
711 OCRepPayload** payload)
715 OCEntityHandlerResult ehResult = OC_EH_ERROR;
720 * This is the entity handler for the registered resource.
721 * This is invoked by OCStack whenever it recevies a request for this resource.
723 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
724 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
727 OCEntityHandlerResult ehRet = OC_EH_OK;
728 OCEntityHandlerResponse response =
729 { 0, 0, OC_EH_ERROR, 0, 0,
732 OCRepPayload* payload = NULL;
734 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
736 if (OC_REST_GET == entityHandlerRequest->method)
738 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
739 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
741 else if (OC_REST_PUT == entityHandlerRequest->method)
743 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
745 //PUT request will be handled in the internal implementation
746 if (gProvResource.handle != NULL)
748 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
752 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
756 else if (OC_REST_POST == entityHandlerRequest->method)
758 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
759 if (gProvResource.handle != NULL)
761 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
765 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
770 if (ehRet == OC_EH_OK)
772 // Format the response. Note this requires some info about the request
773 response.requestHandle = entityHandlerRequest->requestHandle;
774 response.resourceHandle = entityHandlerRequest->resource;
775 response.ehResult = ehRet;
776 //response uses OCPaylod while all get,put methodes use OCRepPayload
777 response.payload = (OCPayload*) (payload);
778 response.numSendVendorSpecificHeaderOptions = 0;
779 memset(response.sendVendorSpecificHeaderOptions, 0,
780 sizeof(response.sendVendorSpecificHeaderOptions));
781 memset(response.resourceUri, 0, sizeof(response.resourceUri));
782 // Indicate that response is NOT in a persistent buffer
783 response.persistentBufferFlag = 0;
786 if (OCDoResponse(&response) != OC_STACK_OK)
788 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
797 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
799 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
801 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
802 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
805 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
807 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
808 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
811 gWiFiResource.numMode = modeIdx;
813 OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
814 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
816 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
820 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
822 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
824 gProvResource.status = esState;
825 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
827 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
831 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
833 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
835 gProvResource.lastErrCode = esErrCode;
836 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
838 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
841 const char *getResult(OCStackResult result)
846 return "OC_STACK_OK";
847 case OC_STACK_INVALID_URI:
848 return "OC_STACK_INVALID_URI";
849 case OC_STACK_INVALID_QUERY:
850 return "OC_STACK_INVALID_QUERY";
851 case OC_STACK_INVALID_IP:
852 return "OC_STACK_INVALID_IP";
853 case OC_STACK_INVALID_PORT:
854 return "OC_STACK_INVALID_PORT";
855 case OC_STACK_INVALID_CALLBACK:
856 return "OC_STACK_INVALID_CALLBACK";
857 case OC_STACK_INVALID_METHOD:
858 return "OC_STACK_INVALID_METHOD";
859 case OC_STACK_NO_MEMORY:
860 return "OC_STACK_NO_MEMORY";
861 case OC_STACK_COMM_ERROR:
862 return "OC_STACK_COMM_ERROR";
863 case OC_STACK_INVALID_PARAM:
864 return "OC_STACK_INVALID_PARAM";
865 case OC_STACK_NOTIMPL:
866 return "OC_STACK_NOTIMPL";
867 case OC_STACK_NO_RESOURCE:
868 return "OC_STACK_NO_RESOURCE";
869 case OC_STACK_RESOURCE_ERROR:
870 return "OC_STACK_RESOURCE_ERROR";
871 case OC_STACK_SLOW_RESOURCE:
872 return "OC_STACK_SLOW_RESOURCE";
873 case OC_STACK_NO_OBSERVERS:
874 return "OC_STACK_NO_OBSERVERS";
876 return "OC_STACK_ERROR";