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.errorMessage, MAX_ERRMSGLEN, "");
132 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
134 OCStackResult res = OC_STACK_ERROR;
137 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
138 OC_RSRVD_INTERFACE_DEFAULT,
139 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
140 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
143 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
144 OC_RSRVD_INTERFACE_DEFAULT,
145 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
146 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
150 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
154 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
157 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
160 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
163 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
167 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
171 OCStackResult initWiFiResource(bool isSecured)
173 OCStackResult res = OC_STACK_ERROR;
175 gWiFiResource.supportedFreq = WiFi_BOTH;
176 gWiFiResource.supportedMode[0] = WiFi_11A;
177 gWiFiResource.supportedMode[1] = WiFi_11B;
178 gWiFiResource.supportedMode[2] = WiFi_11G;
179 gWiFiResource.supportedMode[3] = WiFi_11N;
180 gWiFiResource.numMode = 4;
181 gWiFiResource.authType = NONE_AUTH;
182 gWiFiResource.encType = NONE_ENC;
183 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
184 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
188 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
189 OC_RSRVD_INTERFACE_DEFAULT,
190 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
191 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
194 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
195 OC_RSRVD_INTERFACE_DEFAULT,
196 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
197 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
200 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
205 OCStackResult initCloudServerResource(bool isSecured)
207 OCStackResult res = OC_STACK_ERROR;
209 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
210 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
211 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
215 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
216 OC_RSRVD_INTERFACE_DEFAULT,
217 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
218 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
221 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
222 OC_RSRVD_INTERFACE_DEFAULT,
223 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
224 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
227 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
232 OCStackResult initDevConfResource(bool isSecured)
234 OCStackResult res = OC_STACK_ERROR;
236 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
237 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
238 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
242 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
243 OC_RSRVD_INTERFACE_DEFAULT,
244 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
245 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
248 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
249 OC_RSRVD_INTERFACE_DEFAULT,
250 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
251 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
254 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
259 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
261 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
265 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
267 // When Provisioning resource has a POST with BatchInterface
268 updateCloudResource(input);
269 updateWiFiResource(input);
270 updateDevConfResource(input);
275 void updateWiFiResource(OCRepPayload* input)
278 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
280 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
281 OICStrcpy(gWiFiData.ssid, sizeof(gWiFiData.ssid), ssid);
282 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
286 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
288 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
289 OICStrcpy(gWiFiData.pwd, sizeof(gWiFiData.pwd), cred);
290 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
293 int64_t authType = -1;
294 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
296 gWiFiResource.authType = authType;
297 gWiFiData.authtype = gWiFiResource.authType;
298 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
301 int64_t encType = -1;
302 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
304 gWiFiResource.encType = encType;
305 gWiFiData.enctype = gWiFiResource.encType;
306 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
309 if(ssid || cred || authType!= -1 || encType != -1)
311 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
313 // TODO : Need to check appropriateness of gWiFiData
314 if(gWifiRsrcEvtCb != NULL)
316 gWifiRsrcEvtCb(ES_OK, &gWiFiData);
320 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
326 void updateCloudResource(OCRepPayload* input)
328 char *authCode = NULL;
329 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
331 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
332 OICStrcpy(gCloudData.authCode, sizeof(gCloudData.authCode), authCode);
333 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
336 char *authProvider = NULL;
337 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
339 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
340 OICStrcpy(gCloudData.authProvider, sizeof(gCloudData.authProvider), authProvider);
341 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
344 char *ciServer = NULL;
345 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
347 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
348 OICStrcpy(gCloudData.ciServer, sizeof(gCloudData.ciServer), ciServer);
349 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
352 if(authCode || authProvider || ciServer)
354 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
356 // TODO : Need to check appropriateness of gCloudData
357 if(gCloudRsrcEvtCb != NULL)
359 gCloudRsrcEvtCb(ES_OK, &gCloudData);
363 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
368 void updateDevConfResource(OCRepPayload* input)
370 char *country = NULL;
371 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
373 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
374 OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
375 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
378 char *language = NULL;
379 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
381 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
382 OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
383 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
386 if(country || language)
388 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
390 // TODO : Need to check appropriateness of gDevConfData
391 if(gDevConfRsrcEvtCb != NULL)
393 gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
397 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
402 OCRepPayload* constructResponseOfWiFi()
404 OCRepPayload* payload = OCRepPayloadCreate();
407 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
411 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
412 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
414 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
415 int64_t *modes_64 = (int64_t *)malloc(gWiFiResource.numMode * sizeof(int64_t));
416 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
417 modes_64[i] = gWiFiResource.supportedMode[i];
418 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
420 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
421 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
422 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
423 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
424 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
429 OCRepPayload* constructResponseOfCloud()
431 OCRepPayload* payload = OCRepPayloadCreate();
434 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
438 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
439 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
440 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
441 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
442 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
447 OCRepPayload* constructResponseOfDevConf()
449 OCRepPayload* payload = OCRepPayloadCreate();
452 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
456 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
457 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
458 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
459 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
460 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
465 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
467 OCRepPayload* payload = OCRepPayloadCreate();
470 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
474 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
475 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
476 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
477 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
478 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
482 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
483 {// When Provisioning resource has a GET with BatchInterface
484 payload->next = constructResponseOfWiFi();
487 payload->next->next = constructResponseOfCloud();
491 if(payload->next->next)
492 payload->next->next->next = constructResponseOfDevConf();
502 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
504 OCStackResult res = OC_STACK_ERROR;
505 bool maskFlag = false;
507 res = initProvResource(isSecured);
508 if(res != OC_STACK_OK)
510 // TODO: destroy logic will be added
511 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
516 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
519 res = initWiFiResource(isSecured);
520 if(res != OC_STACK_OK)
522 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
526 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
527 if(res != OC_STACK_OK)
529 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
535 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
538 res = initCloudServerResource(isSecured);
539 if(res != OC_STACK_OK)
541 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
545 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
546 if(res != OC_STACK_OK)
548 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
553 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
556 res = initDevConfResource(isSecured);
557 if(res != OC_STACK_OK)
559 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
563 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
564 if(res != OC_STACK_OK)
566 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
572 if(maskFlag == false)
574 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
575 return OC_STACK_ERROR;
579 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
584 OCStackResult DeleteProvisioningResource()
586 OCStackResult res = OCDeleteResource(gProvResource.handle);
587 if (res != OC_STACK_OK)
589 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
595 OCStackResult DeleteEasySetupResources()
597 OCStackResult res = OCDeleteResource(gWiFiResource.handle);
598 if (res != OC_STACK_OK)
600 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
602 res = OCDeleteResource(gCloudResource.handle);
603 if (res != OC_STACK_OK)
605 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
607 res = OCDeleteResource(gDevConfResource.handle);
608 if (res != OC_STACK_OK)
610 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
613 res = OCDeleteResource(gProvResource.handle);
614 if (res != OC_STACK_OK)
616 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
622 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
624 OCEntityHandlerResult ehResult = OC_EH_ERROR;
627 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
630 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
632 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
636 OCRepPayload *getResp = NULL;
638 if(ehRequest->resource == gProvResource.handle)
639 getResp = constructResponseOfProv(ehRequest);
640 else if(ehRequest->resource == gWiFiResource.handle)
641 getResp = constructResponseOfWiFi();
642 else if(ehRequest->resource == gCloudResource.handle)
643 getResp = constructResponseOfCloud();
644 else if(ehRequest->resource == gDevConfResource.handle)
645 getResp = constructResponseOfDevConf();
649 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
659 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
661 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
662 OCEntityHandlerResult ehResult = OC_EH_ERROR;
663 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
665 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
669 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
672 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
676 // TBD : Discuss about triggering flag (to be existed or not)
677 // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
678 // A new request for provisioning means overriding existing network provisioning information.
680 if(ehRequest->resource == gProvResource.handle)
681 updateProvResource(ehRequest, input);
682 else if(ehRequest->resource == gWiFiResource.handle)
683 updateWiFiResource(input);
684 else if(ehRequest->resource == gCloudResource.handle)
685 updateCloudResource(input);
686 else if(ehRequest->resource == gDevConfResource.handle)
687 updateDevConfResource(input);
689 OCRepPayload *getResp = NULL;
690 if(ehRequest->resource == gProvResource.handle)
691 getResp = constructResponseOfProv(ehRequest);
692 else if(ehRequest->resource == gWiFiResource.handle)
693 getResp = constructResponseOfWiFi();
694 else if(ehRequest->resource == gCloudResource.handle)
695 getResp = constructResponseOfCloud();
696 else if(ehRequest->resource == gDevConfResource.handle)
697 getResp = constructResponseOfDevConf();
701 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
711 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
712 OCRepPayload** payload)
716 OCEntityHandlerResult ehResult = OC_EH_ERROR;
721 * This is the entity handler for the registered resource.
722 * This is invoked by OCStack whenever it recevies a request for this resource.
724 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
725 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
728 OCEntityHandlerResult ehRet = OC_EH_OK;
729 OCEntityHandlerResponse response =
730 { 0, 0, OC_EH_ERROR, 0, 0,
733 OCRepPayload* payload = NULL;
735 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
737 if (OC_REST_GET == entityHandlerRequest->method)
739 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
740 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
742 else if (OC_REST_PUT == entityHandlerRequest->method)
744 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
746 //PUT request will be handled in the internal implementation
747 if (gProvResource.handle != NULL)
749 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
753 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
757 else if (OC_REST_POST == entityHandlerRequest->method)
759 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
760 if (gProvResource.handle != NULL)
762 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
766 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
771 if (ehRet == OC_EH_OK)
773 // Format the response. Note this requires some info about the request
774 response.requestHandle = entityHandlerRequest->requestHandle;
775 response.resourceHandle = entityHandlerRequest->resource;
776 response.ehResult = ehRet;
777 //response uses OCPaylod while all get,put methodes use OCRepPayload
778 response.payload = (OCPayload*) (payload);
779 response.numSendVendorSpecificHeaderOptions = 0;
780 memset(response.sendVendorSpecificHeaderOptions, 0,
781 sizeof(response.sendVendorSpecificHeaderOptions));
782 memset(response.resourceUri, 0, sizeof(response.resourceUri));
783 // Indicate that response is NOT in a persistent buffer
784 response.persistentBufferFlag = 0;
787 if (OCDoResponse(&response) != OC_STACK_OK)
789 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
798 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
800 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
802 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
803 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
806 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
808 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
809 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
812 gWiFiResource.numMode = modeIdx;
814 OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
815 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
817 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
821 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
823 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
825 gProvResource.status = esState;
826 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
828 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
832 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
834 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
836 gProvResource.lastErrCode = esErrCode;
837 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
839 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
842 const char *getResult(OCStackResult result)
847 return "OC_STACK_OK";
848 case OC_STACK_INVALID_URI:
849 return "OC_STACK_INVALID_URI";
850 case OC_STACK_INVALID_QUERY:
851 return "OC_STACK_INVALID_QUERY";
852 case OC_STACK_INVALID_IP:
853 return "OC_STACK_INVALID_IP";
854 case OC_STACK_INVALID_PORT:
855 return "OC_STACK_INVALID_PORT";
856 case OC_STACK_INVALID_CALLBACK:
857 return "OC_STACK_INVALID_CALLBACK";
858 case OC_STACK_INVALID_METHOD:
859 return "OC_STACK_INVALID_METHOD";
860 case OC_STACK_NO_MEMORY:
861 return "OC_STACK_NO_MEMORY";
862 case OC_STACK_COMM_ERROR:
863 return "OC_STACK_COMM_ERROR";
864 case OC_STACK_INVALID_PARAM:
865 return "OC_STACK_INVALID_PARAM";
866 case OC_STACK_NOTIMPL:
867 return "OC_STACK_NOTIMPL";
868 case OC_STACK_NO_RESOURCE:
869 return "OC_STACK_NO_RESOURCE";
870 case OC_STACK_RESOURCE_ERROR:
871 return "OC_STACK_RESOURCE_ERROR";
872 case OC_STACK_SLOW_RESOURCE:
873 return "OC_STACK_SLOW_RESOURCE";
874 case OC_STACK_NO_OBSERVERS:
875 return "OC_STACK_NO_OBSERVERS";
877 return "OC_STACK_ERROR";