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 = NO_PROVISION;
130 gProvResource.trigger = false;
131 gProvResource.lastErrCode = ES_ERRCODE_NONE;
132 OICStrcpy(gProvResource.errorMessage, MAX_ERRMSGLEN, "");
133 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
135 OCStackResult res = OC_STACK_ERROR;
138 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
139 OC_RSRVD_INTERFACE_DEFAULT,
140 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
141 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
144 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
145 OC_RSRVD_INTERFACE_DEFAULT,
146 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
147 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
151 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
155 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
158 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
161 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
164 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
168 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
172 OCStackResult initWiFiResource(bool isSecured)
174 OCStackResult res = OC_STACK_ERROR;
176 gWiFiResource.supportedFreq = WiFi_BOTH;
177 gWiFiResource.supportedMode[0] = WiFi_11A;
178 gWiFiResource.supportedMode[1] = WiFi_11B;
179 gWiFiResource.supportedMode[2] = WiFi_11G;
180 gWiFiResource.supportedMode[3] = WiFi_11N;
181 gWiFiResource.numMode = 4;
182 gWiFiResource.authType = NONE_AUTH;
183 gWiFiResource.encType = NONE_ENC;
184 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
185 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
189 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
190 OC_RSRVD_INTERFACE_DEFAULT,
191 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
192 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
195 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
196 OC_RSRVD_INTERFACE_DEFAULT,
197 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
198 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
201 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
206 OCStackResult initCloudServerResource(bool isSecured)
208 OCStackResult res = OC_STACK_ERROR;
210 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
211 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
212 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
216 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
217 OC_RSRVD_INTERFACE_DEFAULT,
218 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
219 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
222 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
223 OC_RSRVD_INTERFACE_DEFAULT,
224 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
225 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
228 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
233 OCStackResult initDevConfResource(bool isSecured)
235 OCStackResult res = OC_STACK_ERROR;
237 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
238 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
239 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
243 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
244 OC_RSRVD_INTERFACE_DEFAULT,
245 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
246 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
249 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
250 OC_RSRVD_INTERFACE_DEFAULT,
251 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
252 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
255 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
260 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
262 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
266 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
268 // When Provisioning resource has a POST with BatchInterface
269 updateCloudResource(input);
270 updateWiFiResource(input);
271 updateDevConfResource(input);
276 void updateWiFiResource(OCRepPayload* input)
279 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
281 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
282 OICStrcpy(gWiFiData.ssid, sizeof(gWiFiData.ssid), ssid);
283 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
287 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
289 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
290 OICStrcpy(gWiFiData.pwd, sizeof(gWiFiData.pwd), cred);
291 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
294 int64_t authType = -1;
295 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
297 gWiFiResource.authType = authType;
298 gWiFiData.authtype = gWiFiResource.authType;
299 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
302 int64_t encType = -1;
303 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
305 gWiFiResource.encType = encType;
306 gWiFiData.enctype = gWiFiResource.encType;
307 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
310 if(ssid || cred || authType!= -1 || encType != -1)
312 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
314 // TODO : Need to check appropriateness of gWiFiData
315 if(gWifiRsrcEvtCb != NULL)
317 gWifiRsrcEvtCb(ES_OK, &gWiFiData);
321 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
327 void updateCloudResource(OCRepPayload* input)
329 char *authCode = NULL;
330 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
332 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
333 OICStrcpy(gCloudData.authCode, sizeof(gCloudData.authCode), authCode);
334 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
337 char *authProvider = NULL;
338 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
340 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
341 OICStrcpy(gCloudData.authProvider, sizeof(gCloudData.authProvider), authProvider);
342 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
345 char *ciServer = NULL;
346 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
348 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
349 OICStrcpy(gCloudData.ciServer, sizeof(gCloudData.ciServer), ciServer);
350 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
353 if(authCode || authProvider || ciServer)
355 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
357 // TODO : Need to check appropriateness of gCloudData
358 if(gCloudRsrcEvtCb != NULL)
360 gCloudRsrcEvtCb(ES_OK, &gCloudData);
364 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
369 void updateDevConfResource(OCRepPayload* input)
371 char *country = NULL;
372 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &country))
374 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
375 OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
376 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
379 char *language = NULL;
380 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &language))
382 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
383 OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
384 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
387 if(country || language)
389 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
391 // TODO : Need to check appropriateness of gDevConfData
392 if(gDevConfRsrcEvtCb != NULL)
394 gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
398 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
403 OCRepPayload* constructResponseOfWiFi()
405 OCRepPayload* payload = OCRepPayloadCreate();
408 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
412 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
413 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
415 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
416 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)gWiFiResource.supportedMode, dimensions);
418 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
419 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
420 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
421 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
422 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
424 printf("%s\n", gWiFiResource.ssid);
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 OCRepPayloadSetPropBool(payload, OC_RSRVD_ES_TRIGGER, gProvResource.trigger);
478 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
479 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ERRORMESSAGE, gProvResource.errorMessage);
480 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
484 if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
485 {// When Provisioning resource has a GET with BatchInterface
486 payload->next = constructResponseOfWiFi();
489 payload->next->next = constructResponseOfCloud();
493 if(payload->next->next)
494 payload->next->next->next = constructResponseOfDevConf();
504 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
506 OCStackResult res = OC_STACK_ERROR;
507 bool maskFlag = false;
509 res = initProvResource(isSecured);
510 if(res != OC_STACK_OK)
512 // TODO: destroy logic will be added
513 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
518 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
521 res = initWiFiResource(isSecured);
522 if(res != OC_STACK_OK)
524 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
528 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
529 if(res != OC_STACK_OK)
531 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
537 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
540 res = initCloudServerResource(isSecured);
541 if(res != OC_STACK_OK)
543 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 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
555 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
558 res = initDevConfResource(isSecured);
559 if(res != OC_STACK_OK)
561 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
565 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
566 if(res != OC_STACK_OK)
568 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
574 if(maskFlag == false)
576 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
577 return OC_STACK_ERROR;
581 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
586 OCStackResult DeleteProvisioningResource()
588 OCStackResult res = OCDeleteResource(gProvResource.handle);
589 if (res != OC_STACK_OK)
591 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
597 OCStackResult DeleteEasySetupResources()
599 OCStackResult res = OCDeleteResource(gWiFiResource.handle);
600 if (res != OC_STACK_OK)
602 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
604 res = OCDeleteResource(gCloudResource.handle);
605 if (res != OC_STACK_OK)
607 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
609 res = OCDeleteResource(gDevConfResource.handle);
610 if (res != OC_STACK_OK)
612 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
615 res = OCDeleteResource(gProvResource.handle);
616 if (res != OC_STACK_OK)
618 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
624 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
626 OCEntityHandlerResult ehResult = OC_EH_ERROR;
629 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
632 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
634 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
638 OCRepPayload *getResp = NULL;
640 if(ehRequest->resource == gProvResource.handle)
641 getResp = constructResponseOfProv(ehRequest);
642 else if(ehRequest->resource == gWiFiResource.handle)
643 getResp = constructResponseOfWiFi();
644 else if(ehRequest->resource == gCloudResource.handle)
645 getResp = constructResponseOfCloud();
646 else if(ehRequest->resource == gDevConfResource.handle)
647 getResp = constructResponseOfDevConf();
651 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
661 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
663 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
664 OCEntityHandlerResult ehResult = OC_EH_ERROR;
665 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
667 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
671 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
674 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
678 if(ehRequest->resource == gProvResource.handle)
679 updateProvResource(ehRequest, input);
680 else if(ehRequest->resource == gWiFiResource.handle)
681 updateWiFiResource(input);
682 else if(ehRequest->resource == gCloudResource.handle)
683 updateCloudResource(input);
684 else if(ehRequest->resource == gDevConfResource.handle)
685 updateDevConfResource(input);
687 // TBD : Discuss about triggering flag (to be existed or not)
688 // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
689 // A new request for provisioning means overriding existing network provisioning information.
690 // if (gProvResource.trigger)
692 // OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
693 // "Tiggering the network connection");
695 // if (gNetworkInfoProvEventCb)
697 // gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
698 // ehResult = OC_EH_OK;
702 // OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
703 // "Network handler not registered. Failed to connect to the network");
704 // ehResult = OC_EH_ERROR;
710 // OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
713 OCRepPayload *getResp = NULL;
714 if(ehRequest->resource == gProvResource.handle)
715 getResp = constructResponseOfProv(ehRequest);
716 else if(ehRequest->resource == gWiFiResource.handle)
717 getResp = constructResponseOfWiFi();
718 else if(ehRequest->resource == gCloudResource.handle)
719 getResp = constructResponseOfCloud();
720 else if(ehRequest->resource == gDevConfResource.handle)
721 getResp = constructResponseOfDevConf();
723 if (gProvResource.trigger)// Trigger false should be restored after executed
724 gProvResource.trigger = false;
728 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
738 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
739 OCRepPayload** payload)
743 OCEntityHandlerResult ehResult = OC_EH_ERROR;
748 * This is the entity handler for the registered resource.
749 * This is invoked by OCStack whenever it recevies a request for this resource.
751 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
752 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
755 OCEntityHandlerResult ehRet = OC_EH_OK;
756 OCEntityHandlerResponse response =
757 { 0, 0, OC_EH_ERROR, 0, 0,
760 OCRepPayload* payload = NULL;
762 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
764 if (OC_REST_GET == entityHandlerRequest->method)
766 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
767 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
769 else if (OC_REST_PUT == entityHandlerRequest->method)
771 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
773 //PUT request will be handled in the internal implementation
774 if (gProvResource.handle != NULL)
776 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
780 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
784 else if (OC_REST_POST == entityHandlerRequest->method)
786 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
787 if (gProvResource.handle != NULL)
789 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
793 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
798 if (ehRet == OC_EH_OK)
800 // Format the response. Note this requires some info about the request
801 response.requestHandle = entityHandlerRequest->requestHandle;
802 response.resourceHandle = entityHandlerRequest->resource;
803 response.ehResult = ehRet;
804 //response uses OCPaylod while all get,put methodes use OCRepPayload
805 response.payload = (OCPayload*) (payload);
806 response.numSendVendorSpecificHeaderOptions = 0;
807 memset(response.sendVendorSpecificHeaderOptions, 0,
808 sizeof(response.sendVendorSpecificHeaderOptions));
809 memset(response.resourceUri, 0, sizeof(response.resourceUri));
810 // Indicate that response is NOT in a persistent buffer
811 response.persistentBufferFlag = 0;
814 if (OCDoResponse(&response) != OC_STACK_OK)
816 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
825 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
827 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
829 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
830 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
833 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
835 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
836 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
839 gWiFiResource.numMode = modeIdx;
841 OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
842 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
844 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
848 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
850 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
852 gProvResource.status = esState;
853 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
855 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
859 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
861 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
863 gProvResource.lastErrCode = esErrCode;
864 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
866 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
869 const char *getResult(OCStackResult result)
874 return "OC_STACK_OK";
875 case OC_STACK_INVALID_URI:
876 return "OC_STACK_INVALID_URI";
877 case OC_STACK_INVALID_QUERY:
878 return "OC_STACK_INVALID_QUERY";
879 case OC_STACK_INVALID_IP:
880 return "OC_STACK_INVALID_IP";
881 case OC_STACK_INVALID_PORT:
882 return "OC_STACK_INVALID_PORT";
883 case OC_STACK_INVALID_CALLBACK:
884 return "OC_STACK_INVALID_CALLBACK";
885 case OC_STACK_INVALID_METHOD:
886 return "OC_STACK_INVALID_METHOD";
887 case OC_STACK_NO_MEMORY:
888 return "OC_STACK_NO_MEMORY";
889 case OC_STACK_COMM_ERROR:
890 return "OC_STACK_COMM_ERROR";
891 case OC_STACK_INVALID_PARAM:
892 return "OC_STACK_INVALID_PARAM";
893 case OC_STACK_NOTIMPL:
894 return "OC_STACK_NOTIMPL";
895 case OC_STACK_NO_RESOURCE:
896 return "OC_STACK_NO_RESOURCE";
897 case OC_STACK_RESOURCE_ERROR:
898 return "OC_STACK_RESOURCE_ERROR";
899 case OC_STACK_SLOW_RESOURCE:
900 return "OC_STACK_SLOW_RESOURCE";
901 case OC_STACK_NO_OBSERVERS:
902 return "OC_STACK_NO_OBSERVERS";
904 return "OC_STACK_ERROR";