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, MAXSSIDLEN, gWiFiResource.ssid);
123 OICStrcpy(pass, MAXNETCREDLEN, gWiFiResource.cred);
127 OCStackResult initProvResource(bool isSecured)
129 gProvResource.status = NO_PROVISION;
130 gProvResource.trigger = false;
132 OCStackResult res = OC_STACK_ERROR;
135 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
136 OC_RSRVD_INTERFACE_DEFAULT,
137 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
138 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
141 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
142 OC_RSRVD_INTERFACE_DEFAULT,
143 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
144 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
148 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
152 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
155 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
158 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
161 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
165 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
169 OCStackResult initWiFiResource(bool isSecured)
171 OCStackResult res = OC_STACK_ERROR;
173 gWiFiResource.supportedFreq = WiFi_BOTH;
174 gWiFiResource.supportedMode[0] = WiFi_11A;
175 gWiFiResource.supportedMode[1] = WiFi_11B;
176 gWiFiResource.supportedMode[2] = WiFi_11G;
177 gWiFiResource.supportedMode[3] = WiFi_11N;
178 gWiFiResource.numMode = 4;
179 gWiFiResource.authType = NONE_AUTH;
180 gWiFiResource.encType = NONE_ENC;
181 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
182 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
186 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
187 OC_RSRVD_INTERFACE_DEFAULT,
188 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
189 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
192 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
193 OC_RSRVD_INTERFACE_DEFAULT,
194 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
195 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
198 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
203 OCStackResult initCloudServerResource(bool isSecured)
205 OCStackResult res = OC_STACK_ERROR;
207 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
208 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
209 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
213 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
214 OC_RSRVD_INTERFACE_DEFAULT,
215 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
216 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
219 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
220 OC_RSRVD_INTERFACE_DEFAULT,
221 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
222 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
225 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
230 OCStackResult initDevConfResource(bool isSecured)
232 OCStackResult res = OC_STACK_ERROR;
234 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
235 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
236 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
240 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
241 OC_RSRVD_INTERFACE_DEFAULT,
242 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
243 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
246 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
247 OC_RSRVD_INTERFACE_DEFAULT,
248 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
249 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
252 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
257 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
259 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %lld", gProvResource.status);
263 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
265 // When Provisioning resource has a POST with BatchInterface
266 updateCloudResource(input);
267 updateWiFiResource(input);
268 updateDevConfResource(input);
273 void updateWiFiResource(OCRepPayload* input)
276 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
278 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
279 OICStrcpy(gWiFiData.ssid, sizeof(gWiFiData.ssid), ssid);
280 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
284 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
286 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
287 OICStrcpy(gWiFiData.pwd, sizeof(gWiFiData.pwd), cred);
288 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
291 int64_t authType = -1;
292 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
294 gWiFiResource.authType = authType;
295 gWiFiData.authtype = gWiFiResource.authType;
296 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
299 int64_t encType = -1;
300 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
302 gWiFiResource.encType = encType;
303 gWiFiData.enctype = gWiFiResource.encType;
304 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
307 if(ssid || cred || authType!= -1 || encType != -1)
309 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
311 // TODO : Need to check appropriateness of gWiFiData
312 if(gWifiRsrcEvtCb != NULL)
314 gWifiRsrcEvtCb(ES_OK, &gWiFiData);
318 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
324 void updateCloudResource(OCRepPayload* input)
326 char *authCode = NULL;
327 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
329 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
330 OICStrcpy(gCloudData.authCode, sizeof(gCloudData.authCode), authCode);
331 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
334 char *authProvider = NULL;
335 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
337 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
338 OICStrcpy(gCloudData.authProvider, sizeof(gCloudData.authProvider), authProvider);
339 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
342 char *ciServer = NULL;
343 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
345 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
346 OICStrcpy(gCloudData.ciServer, sizeof(gCloudData.ciServer), ciServer);
347 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
350 if(authCode || authProvider || ciServer)
352 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
354 // TODO : Need to check appropriateness of gCloudData
355 if(gCloudRsrcEvtCb != NULL)
357 gCloudRsrcEvtCb(ES_OK, &gCloudData);
361 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
366 void updateDevConfResource(OCRepPayload* input)
368 char *country = NULL;
369 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &country))
371 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
372 OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
373 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
376 char *language = NULL;
377 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &language))
379 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
380 OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
381 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
384 if(country || language)
386 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
388 // TODO : Need to check appropriateness of gDevConfData
389 if(gDevConfRsrcEvtCb != NULL)
391 gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
395 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
400 OCRepPayload* constructResponseOfWiFi()
402 OCRepPayload* payload = OCRepPayloadCreate();
405 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
409 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
410 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
412 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
413 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)gWiFiResource.supportedMode, dimensions);
415 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
416 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
417 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
418 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
419 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
421 printf("%s\n", gWiFiResource.ssid);
426 OCRepPayload* constructResponseOfCloud()
428 OCRepPayload* payload = OCRepPayloadCreate();
431 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
435 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
436 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
437 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
438 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
439 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
444 OCRepPayload* constructResponseOfDevConf()
446 OCRepPayload* payload = OCRepPayloadCreate();
449 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
453 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
454 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
455 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
456 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
457 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
462 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
464 OCRepPayload* payload = OCRepPayloadCreate();
467 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
471 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
472 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
473 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
474 OCRepPayloadSetPropBool(payload, OC_RSRVD_ES_TRIGGER, gProvResource.trigger);
478 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
479 {// When Provisioning resource has a GET with BatchInterface
480 payload->next = constructResponseOfWiFi();
483 payload->next->next = constructResponseOfCloud();
487 if(payload->next->next)
488 payload->next->next->next = constructResponseOfDevConf();
498 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
500 OCStackResult res = OC_STACK_ERROR;
501 bool maskFlag = false;
503 res = initProvResource(isSecured);
504 if(res != OC_STACK_OK)
506 // TODO: destroy logic will be added
507 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
512 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
515 res = initWiFiResource(isSecured);
516 if(res != OC_STACK_OK)
518 // TODO: destroy logic will be added
519 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
524 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
525 if(res != OC_STACK_OK)
527 // TODO : Error Handling
528 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 // TODO: destroy logic will be added
542 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
547 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
548 if(res != OC_STACK_OK)
550 // TODO : Error Handling
551 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
557 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
560 res = initDevConfResource(isSecured);
561 if(res != OC_STACK_OK)
563 // TODO: destroy logic will be added
564 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
569 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
570 if(res != OC_STACK_OK)
572 // TODO : Error Handling
573 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
580 if(maskFlag == false)
582 // TODO: destroy logic will be added
583 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
584 return OC_STACK_ERROR;
587 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
591 OCStackResult DeleteProvisioningResource()
593 OCStackResult res = OCDeleteResource(gProvResource.handle);
594 if (res != OC_STACK_OK)
596 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
602 OCStackResult DeleteEasySetupResources()
604 OCStackResult res = OCDeleteResource(gProvResource.handle);
605 if (res != OC_STACK_OK)
607 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
609 res = OCDeleteResource(gWiFiResource.handle);
610 if (res != OC_STACK_OK)
612 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
614 res = OCDeleteResource(gCloudResource.handle);
615 if (res != OC_STACK_OK)
617 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
619 res = OCDeleteResource(gDevConfResource.handle);
620 if (res != OC_STACK_OK)
622 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
628 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
630 OCEntityHandlerResult ehResult = OC_EH_ERROR;
633 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
636 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
638 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
642 OCRepPayload *getResp = NULL;
644 if(ehRequest->resource == gProvResource.handle)
645 getResp = constructResponseOfProv(ehRequest);
646 else if(ehRequest->resource == gWiFiResource.handle)
647 getResp = constructResponseOfWiFi();
648 else if(ehRequest->resource == gCloudResource.handle)
649 getResp = constructResponseOfCloud();
650 else if(ehRequest->resource == gDevConfResource.handle)
651 getResp = constructResponseOfDevConf();
655 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
665 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
667 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
668 OCEntityHandlerResult ehResult = OC_EH_ERROR;
669 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
671 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
675 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
678 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
682 if(ehRequest->resource == gProvResource.handle)
683 updateProvResource(ehRequest, input);
684 else if(ehRequest->resource == gWiFiResource.handle)
685 updateWiFiResource(input);
686 else if(ehRequest->resource == gCloudResource.handle)
687 updateCloudResource(input);
688 else if(ehRequest->resource == gDevConfResource.handle)
689 updateDevConfResource(input);
691 // TBD : Discuss about triggering flag (to be existed or not)
692 // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
693 // A new request for provisioning means overriding existing network provisioning information.
694 // if (gProvResource.trigger)
696 // OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
697 // "Tiggering the network connection");
699 // if (gNetworkInfoProvEventCb)
701 // gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
702 // ehResult = OC_EH_OK;
706 // OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
707 // "Network handler not registered. Failed to connect to the network");
708 // ehResult = OC_EH_ERROR;
714 // OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
717 OCRepPayload *getResp = NULL;
718 if(ehRequest->resource == gProvResource.handle)
719 getResp = constructResponseOfProv(ehRequest);
720 else if(ehRequest->resource == gWiFiResource.handle)
721 getResp = constructResponseOfWiFi();
722 else if(ehRequest->resource == gCloudResource.handle)
723 getResp = constructResponseOfCloud();
724 else if(ehRequest->resource == gDevConfResource.handle)
725 getResp = constructResponseOfDevConf();
727 if (gProvResource.trigger)// Trigger false should be restored after executed
728 gProvResource.trigger = false;
732 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
742 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
743 OCRepPayload** payload)
747 OCEntityHandlerResult ehResult = OC_EH_ERROR;
752 * This is the entity handler for the registered resource.
753 * This is invoked by OCStack whenever it recevies a request for this resource.
755 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
756 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
759 OCEntityHandlerResult ehRet = OC_EH_OK;
760 OCEntityHandlerResponse response =
761 { 0, 0, OC_EH_ERROR, 0, 0,
764 OCRepPayload* payload = NULL;
766 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
768 if (OC_REST_GET == entityHandlerRequest->method)
770 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
771 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
773 else if (OC_REST_PUT == entityHandlerRequest->method)
775 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
777 //PUT request will be handled in the internal implementation
778 if (gProvResource.handle != NULL)
780 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
784 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
788 else if (OC_REST_POST == entityHandlerRequest->method)
790 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
791 if (gProvResource.handle != NULL)
793 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
797 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
802 if (ehRet == OC_EH_OK)
804 // Format the response. Note this requires some info about the request
805 response.requestHandle = entityHandlerRequest->requestHandle;
806 response.resourceHandle = entityHandlerRequest->resource;
807 response.ehResult = ehRet;
808 //response uses OCPaylod while all get,put methodes use OCRepPayload
809 response.payload = (OCPayload*) (payload);
810 response.numSendVendorSpecificHeaderOptions = 0;
811 memset(response.sendVendorSpecificHeaderOptions, 0,
812 sizeof(response.sendVendorSpecificHeaderOptions));
813 memset(response.resourceUri, 0, sizeof(response.resourceUri));
814 // Indicate that response is NOT in a persistent buffer
815 response.persistentBufferFlag = 0;
818 if (OCDoResponse(&response) != OC_STACK_OK)
820 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
829 const char *getResult(OCStackResult result)
834 return "OC_STACK_OK";
835 case OC_STACK_INVALID_URI:
836 return "OC_STACK_INVALID_URI";
837 case OC_STACK_INVALID_QUERY:
838 return "OC_STACK_INVALID_QUERY";
839 case OC_STACK_INVALID_IP:
840 return "OC_STACK_INVALID_IP";
841 case OC_STACK_INVALID_PORT:
842 return "OC_STACK_INVALID_PORT";
843 case OC_STACK_INVALID_CALLBACK:
844 return "OC_STACK_INVALID_CALLBACK";
845 case OC_STACK_INVALID_METHOD:
846 return "OC_STACK_INVALID_METHOD";
847 case OC_STACK_NO_MEMORY:
848 return "OC_STACK_NO_MEMORY";
849 case OC_STACK_COMM_ERROR:
850 return "OC_STACK_COMM_ERROR";
851 case OC_STACK_INVALID_PARAM:
852 return "OC_STACK_INVALID_PARAM";
853 case OC_STACK_NOTIMPL:
854 return "OC_STACK_NOTIMPL";
855 case OC_STACK_NO_RESOURCE:
856 return "OC_STACK_NO_RESOURCE";
857 case OC_STACK_RESOURCE_ERROR:
858 return "OC_STACK_RESOURCE_ERROR";
859 case OC_STACK_SLOW_RESOURCE:
860 return "OC_STACK_SLOW_RESOURCE";
861 case OC_STACK_NO_OBSERVERS:
862 return "OC_STACK_NO_OBSERVERS";
864 return "OC_STACK_ERROR";