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 // TODO: destroy logic will be added
525 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
530 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
531 if(res != OC_STACK_OK)
533 // TODO : Error Handling
534 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
541 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
544 res = initCloudServerResource(isSecured);
545 if(res != OC_STACK_OK)
547 // TODO: destroy logic will be added
548 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
553 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
554 if(res != OC_STACK_OK)
556 // TODO : Error Handling
557 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
563 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
566 res = initDevConfResource(isSecured);
567 if(res != OC_STACK_OK)
569 // TODO: destroy logic will be added
570 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
575 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
576 if(res != OC_STACK_OK)
578 // TODO : Error Handling
579 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
586 if(maskFlag == false)
588 // TODO: destroy logic will be added
589 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
590 return OC_STACK_ERROR;
593 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
597 OCStackResult DeleteProvisioningResource()
599 OCStackResult res = OCDeleteResource(gProvResource.handle);
600 if (res != OC_STACK_OK)
602 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
608 OCStackResult DeleteEasySetupResources()
610 OCStackResult res = OCDeleteResource(gProvResource.handle);
611 if (res != OC_STACK_OK)
613 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
615 res = OCDeleteResource(gWiFiResource.handle);
616 if (res != OC_STACK_OK)
618 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
620 res = OCDeleteResource(gCloudResource.handle);
621 if (res != OC_STACK_OK)
623 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
625 res = OCDeleteResource(gDevConfResource.handle);
626 if (res != OC_STACK_OK)
628 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
634 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
636 OCEntityHandlerResult ehResult = OC_EH_ERROR;
639 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
642 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
644 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
648 OCRepPayload *getResp = NULL;
650 if(ehRequest->resource == gProvResource.handle)
651 getResp = constructResponseOfProv(ehRequest);
652 else if(ehRequest->resource == gWiFiResource.handle)
653 getResp = constructResponseOfWiFi();
654 else if(ehRequest->resource == gCloudResource.handle)
655 getResp = constructResponseOfCloud();
656 else if(ehRequest->resource == gDevConfResource.handle)
657 getResp = constructResponseOfDevConf();
661 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
671 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
673 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
674 OCEntityHandlerResult ehResult = OC_EH_ERROR;
675 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
677 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
681 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
684 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
688 if(ehRequest->resource == gProvResource.handle)
689 updateProvResource(ehRequest, input);
690 else if(ehRequest->resource == gWiFiResource.handle)
691 updateWiFiResource(input);
692 else if(ehRequest->resource == gCloudResource.handle)
693 updateCloudResource(input);
694 else if(ehRequest->resource == gDevConfResource.handle)
695 updateDevConfResource(input);
697 // TBD : Discuss about triggering flag (to be existed or not)
698 // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
699 // A new request for provisioning means overriding existing network provisioning information.
700 // if (gProvResource.trigger)
702 // OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning already completed."
703 // "Tiggering the network connection");
705 // if (gNetworkInfoProvEventCb)
707 // gNetworkInfoProvEventCb(ES_RECVTRIGGEROFPROVRES);
708 // ehResult = OC_EH_OK;
712 // OIC_LOG(ERROR, ES_RH_TAG, "gNetworkInfoProvEventCb is NULL."
713 // "Network handler not registered. Failed to connect to the network");
714 // ehResult = OC_EH_ERROR;
720 // OIC_LOG(DEBUG, ES_RH_TAG, "Provisioning the network information to the Enrollee.");
723 OCRepPayload *getResp = NULL;
724 if(ehRequest->resource == gProvResource.handle)
725 getResp = constructResponseOfProv(ehRequest);
726 else if(ehRequest->resource == gWiFiResource.handle)
727 getResp = constructResponseOfWiFi();
728 else if(ehRequest->resource == gCloudResource.handle)
729 getResp = constructResponseOfCloud();
730 else if(ehRequest->resource == gDevConfResource.handle)
731 getResp = constructResponseOfDevConf();
733 if (gProvResource.trigger)// Trigger false should be restored after executed
734 gProvResource.trigger = false;
738 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
748 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
749 OCRepPayload** payload)
753 OCEntityHandlerResult ehResult = OC_EH_ERROR;
758 * This is the entity handler for the registered resource.
759 * This is invoked by OCStack whenever it recevies a request for this resource.
761 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
762 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
765 OCEntityHandlerResult ehRet = OC_EH_OK;
766 OCEntityHandlerResponse response =
767 { 0, 0, OC_EH_ERROR, 0, 0,
770 OCRepPayload* payload = NULL;
772 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
774 if (OC_REST_GET == entityHandlerRequest->method)
776 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
777 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
779 else if (OC_REST_PUT == entityHandlerRequest->method)
781 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
783 //PUT request will be handled in the internal implementation
784 if (gProvResource.handle != NULL)
786 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
790 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
794 else if (OC_REST_POST == entityHandlerRequest->method)
796 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
797 if (gProvResource.handle != NULL)
799 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
803 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
808 if (ehRet == OC_EH_OK)
810 // Format the response. Note this requires some info about the request
811 response.requestHandle = entityHandlerRequest->requestHandle;
812 response.resourceHandle = entityHandlerRequest->resource;
813 response.ehResult = ehRet;
814 //response uses OCPaylod while all get,put methodes use OCRepPayload
815 response.payload = (OCPayload*) (payload);
816 response.numSendVendorSpecificHeaderOptions = 0;
817 memset(response.sendVendorSpecificHeaderOptions, 0,
818 sizeof(response.sendVendorSpecificHeaderOptions));
819 memset(response.resourceUri, 0, sizeof(response.resourceUri));
820 // Indicate that response is NOT in a persistent buffer
821 response.persistentBufferFlag = 0;
824 if (OCDoResponse(&response) != OC_STACK_OK)
826 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
835 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
837 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
839 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
840 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
843 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
845 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
846 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
849 gWiFiResource.numMode = modeIdx;
851 OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
852 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
854 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
858 const char *getResult(OCStackResult result)
863 return "OC_STACK_OK";
864 case OC_STACK_INVALID_URI:
865 return "OC_STACK_INVALID_URI";
866 case OC_STACK_INVALID_QUERY:
867 return "OC_STACK_INVALID_QUERY";
868 case OC_STACK_INVALID_IP:
869 return "OC_STACK_INVALID_IP";
870 case OC_STACK_INVALID_PORT:
871 return "OC_STACK_INVALID_PORT";
872 case OC_STACK_INVALID_CALLBACK:
873 return "OC_STACK_INVALID_CALLBACK";
874 case OC_STACK_INVALID_METHOD:
875 return "OC_STACK_INVALID_METHOD";
876 case OC_STACK_NO_MEMORY:
877 return "OC_STACK_NO_MEMORY";
878 case OC_STACK_COMM_ERROR:
879 return "OC_STACK_COMM_ERROR";
880 case OC_STACK_INVALID_PARAM:
881 return "OC_STACK_INVALID_PARAM";
882 case OC_STACK_NOTIMPL:
883 return "OC_STACK_NOTIMPL";
884 case OC_STACK_NO_RESOURCE:
885 return "OC_STACK_NO_RESOURCE";
886 case OC_STACK_RESOURCE_ERROR:
887 return "OC_STACK_RESOURCE_ERROR";
888 case OC_STACK_SLOW_RESOURCE:
889 return "OC_STACK_SLOW_RESOURCE";
890 case OC_STACK_NO_OBSERVERS:
891 return "OC_STACK_NO_OBSERVERS";
893 return "OC_STACK_ERROR";