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"
23 #include "ocpayload.h"
24 #include "oic_string.h"
25 #include "oic_malloc.h"
29 * @brief Logging tag for module name.
31 #define ES_RH_TAG "ES_RH"
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
38 * @brief Structure for holding the Provisioning status and target information required to
39 * connect to the target network
41 static ProvResource gProvResource;
42 static WiFiResource gWiFiResource;
43 static CloudResource gCloudResource;
44 static DevConfResource gDevConfResource;
46 //-----------------------------------------------------------------------------
47 // Private internal function prototypes
48 //-----------------------------------------------------------------------------
49 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
51 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
52 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
53 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
54 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
55 void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
56 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
57 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
58 const char *getResult(OCStackResult result);
60 ESWiFiCB gWifiRsrcEvtCb = NULL;
61 ESCloudCB gCloudRsrcEvtCb = NULL;
62 ESDevConfCB gDevConfRsrcEvtCb = NULL;
64 ESReadUserdataCb gReadUserdataCb = NULL;
65 ESWriteUserdataCb gWriteUserdataCb = NULL;
67 bool CompareResourceInterface(char *from, char *iface)
69 char *str = OICStrdup(from);
70 char *ptr = strtok(str, ";");
77 if(strstr(ptr, ".if."))
80 if_ptr = strtok(ptr, "=");
81 if_ptr = strtok(NULL, "=");
83 if(!strcmp(if_ptr, iface))
89 } while (ptr = strtok(NULL, ";"));
94 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
96 if(!readCb && !writeCb)
98 OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
101 gReadUserdataCb = readCb;
102 gWriteUserdataCb = writeCb;
106 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
111 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
113 gCloudRsrcEvtCb = cb;
116 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
118 gDevConfRsrcEvtCb = cb;
121 void UnRegisterResourceEventCallBack()
125 gWifiRsrcEvtCb = NULL;
129 gCloudRsrcEvtCb = NULL;
131 if (gDevConfRsrcEvtCb)
133 gDevConfRsrcEvtCb = NULL;
137 OCStackResult initProvResource(bool isSecured)
139 gProvResource.status = ES_STATE_INIT;
140 gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
141 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
143 OCStackResult res = OC_STACK_ERROR;
146 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
147 OC_RSRVD_INTERFACE_DEFAULT,
148 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
149 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
152 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
153 OC_RSRVD_INTERFACE_DEFAULT,
154 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
155 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
159 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
163 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
166 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
169 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
172 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
176 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
180 OCStackResult initWiFiResource(bool isSecured)
182 OCStackResult res = OC_STACK_ERROR;
184 gWiFiResource.supportedFreq = WIFI_BOTH;
185 gWiFiResource.supportedMode[0] = WIFI_11A;
186 gWiFiResource.supportedMode[1] = WIFI_11B;
187 gWiFiResource.supportedMode[2] = WIFI_11G;
188 gWiFiResource.supportedMode[3] = WIFI_11N;
189 gWiFiResource.numMode = 4;
190 gWiFiResource.authType = NONE_AUTH;
191 gWiFiResource.encType = NONE_ENC;
192 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
193 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
197 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
198 OC_RSRVD_INTERFACE_DEFAULT,
199 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
200 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
203 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
204 OC_RSRVD_INTERFACE_DEFAULT,
205 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
206 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
209 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
214 OCStackResult initCloudServerResource(bool isSecured)
216 OCStackResult res = OC_STACK_ERROR;
218 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
219 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
220 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
224 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
225 OC_RSRVD_INTERFACE_DEFAULT,
226 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
227 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
230 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
231 OC_RSRVD_INTERFACE_DEFAULT,
232 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
233 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
236 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
241 OCStackResult initDevConfResource(bool isSecured)
243 OCStackResult res = OC_STACK_ERROR;
245 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
246 OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
247 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
248 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
249 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
253 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
254 OC_RSRVD_INTERFACE_DEFAULT,
255 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
256 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
259 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
260 OC_RSRVD_INTERFACE_DEFAULT,
261 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
262 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
265 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
270 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
272 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
276 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
278 // When Provisioning resource has a POST with BatchInterface
279 updateCloudResource(NULL, input);
280 updateWiFiResource(NULL, input);
281 updateDevConfResource(NULL, input);
286 void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
289 strcmp(ehRequest->query, "") &&
290 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
292 // In case of link list, batch interface
293 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
297 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
301 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
305 memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
306 memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
307 wiFiData->authtype = NONE_AUTH;
308 wiFiData->enctype = NONE_AUTH;
309 wiFiData->userdata = NULL;
312 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
314 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
315 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
316 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
320 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
322 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
323 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
324 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
327 int64_t authType = -1;
328 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
330 gWiFiResource.authType = authType;
331 wiFiData->authtype = gWiFiResource.authType;
332 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
335 int64_t encType = -1;
336 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
338 gWiFiResource.encType = encType;
339 wiFiData->enctype = gWiFiResource.encType;
340 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
345 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
348 if(ssid || cred || authType!= -1 || encType != -1)
350 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
352 // TODO : Need to check appropriateness of gWiFiData
353 if(gWifiRsrcEvtCb != NULL)
355 gWifiRsrcEvtCb(ES_OK, wiFiData);
359 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
366 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
369 strcmp(ehRequest->query, "") &&
370 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
372 // In case of link list, batch interface
373 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
377 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
379 if(cloudData == NULL)
381 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
385 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
386 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
387 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
388 cloudData->userdata = NULL;
390 char *authCode = NULL;
391 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
393 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
394 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
395 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
398 char *authProvider = NULL;
399 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
401 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
402 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
403 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
406 char *ciServer = NULL;
407 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
409 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
410 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
411 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
416 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
419 if(authCode || authProvider || ciServer)
421 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
423 // TODO : Need to check appropriateness of gCloudData
424 if(gCloudRsrcEvtCb != NULL)
426 gCloudRsrcEvtCb(ES_OK, cloudData);
430 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
437 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
440 strcmp(ehRequest->query, "") &&
441 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
443 // In case of link list, batch interface
444 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
448 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
450 if(devConfData == NULL)
452 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
455 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
456 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
457 devConfData->userdata = NULL;
459 char *location = NULL;
460 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
462 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
463 OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
464 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
467 char *country = NULL;
468 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
470 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
471 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
472 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
475 char *language = NULL;
476 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
478 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
479 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
480 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
485 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
488 if(country || language)
490 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
492 // TODO : Need to check appropriateness of gDevConfData
493 if(gDevConfRsrcEvtCb != NULL)
495 gDevConfRsrcEvtCb(ES_OK, devConfData);
499 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
503 OICFree(devConfData);
506 OCRepPayload* constructResponseOfWiFi(OCEntityHandlerRequest *ehRequest)
509 strcmp(ehRequest->query, "") &&
510 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
512 // In case of link list, batch interface
513 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
517 OCRepPayload* payload = OCRepPayloadCreate();
520 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
524 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
525 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_WIFI);
527 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
528 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
529 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
531 modes_64[i] = gWiFiResource.supportedMode[i];
533 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
535 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
536 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
537 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
538 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
539 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
543 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
549 OCRepPayload* constructResponseOfCloud(OCEntityHandlerRequest *ehRequest)
552 strcmp(ehRequest->query, "") &&
553 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
555 // In case of link list, batch interface
556 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
560 OCRepPayload* payload = OCRepPayloadCreate();
563 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
567 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
568 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
569 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
570 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
571 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
575 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
581 OCRepPayload* constructResponseOfDevConf(OCEntityHandlerRequest *ehRequest)
584 strcmp(ehRequest->query, "") &&
585 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
587 // In case of link list, batch interface
588 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
592 OCRepPayload* payload = OCRepPayloadCreate();
595 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
599 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
600 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_DEVCONF);
601 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
602 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
603 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
604 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
605 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
609 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
615 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
617 OCRepPayload* payload = OCRepPayloadCreate();
620 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
624 // Requested interface is Link list interface
625 if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
627 const OCRepPayload *arrayPayload[3] = {NULL};
631 if(gWiFiResource.handle != NULL)
633 OCRepPayload *add = OCRepPayloadCreate();
636 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
640 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
641 char **resourceType = NULL;
642 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
643 char **resourceInterface = NULL;
644 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
646 if(!resourceType || !resourceInterface)
648 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
652 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFI);
653 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
655 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
656 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFI);
657 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
658 (const char **)resourceType, dimensions);
659 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
660 (const char **)resourceInterface, dimensions);
662 arrayPayload[childResCnt++] = add;
665 if(gDevConfResource.handle != NULL)
667 OCRepPayload *add = OCRepPayloadCreate();
670 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
674 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
675 char **resourceType = NULL;
676 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
677 char **resourceInterface = NULL;
678 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
680 if(!resourceType || !resourceInterface)
682 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
686 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
687 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
689 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
690 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
691 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
692 (const char **)resourceType, dimensions);
693 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
694 (const char **)resourceInterface, dimensions);
696 arrayPayload[childResCnt++] = add;
699 if(gCloudResource.handle != NULL)
701 OCRepPayload *add = OCRepPayloadCreate();
704 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
708 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
709 char **resourceType = NULL;
710 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
711 char **resourceInterface = NULL;
712 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
714 if(!resourceType || !resourceInterface)
716 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
720 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
721 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
723 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
724 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
725 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
726 (const char **)resourceType, dimensions);
727 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
728 (const char **)resourceInterface, dimensions);
730 arrayPayload[childResCnt++] = add;
733 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
734 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
737 } else if (!ehRequest->query ||
738 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH)) ||
739 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
741 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
742 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_PROV);
743 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
744 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
745 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
750 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
755 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
756 {// When Provisioning resource has a GET with BatchInterface
757 OCRepPayload* head = payload;
758 OCRepPayload* nextPayload = NULL;
760 nextPayload = constructResponseOfWiFi(NULL);
761 if(nextPayload != NULL)
763 payload->next = nextPayload;
764 payload = payload->next;
767 nextPayload = constructResponseOfCloud(NULL);
768 if(nextPayload != NULL)
770 payload->next = nextPayload;
771 payload = payload->next;
774 nextPayload = constructResponseOfDevConf(NULL);
775 if(nextPayload != NULL)
777 payload->next = nextPayload;
778 payload = payload->next;
789 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
791 OCStackResult res = OC_STACK_ERROR;
792 bool maskFlag = false;
794 res = initProvResource(isSecured);
795 if(res != OC_STACK_OK)
797 // TODO: destroy logic will be added
798 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
803 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
806 res = initWiFiResource(isSecured);
807 if(res != OC_STACK_OK)
809 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
813 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
814 if(res != OC_STACK_OK)
816 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
822 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
825 res = initCloudServerResource(isSecured);
826 if(res != OC_STACK_OK)
828 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
832 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
833 if(res != OC_STACK_OK)
835 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
840 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
843 res = initDevConfResource(isSecured);
844 if(res != OC_STACK_OK)
846 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
850 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
851 if(res != OC_STACK_OK)
853 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
859 if(maskFlag == false)
861 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
862 return OC_STACK_ERROR;
866 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
871 OCStackResult DeleteProvisioningResource()
873 OCStackResult res = OCDeleteResource(gProvResource.handle);
874 if (res != OC_STACK_OK)
876 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
882 OCStackResult DeleteEasySetupResources()
884 OCStackResult res = OC_STACK_ERROR;
885 if (gWiFiResource.handle != NULL)
887 res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
888 if(res != OC_STACK_OK)
890 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
893 if (gCloudResource.handle != NULL)
895 res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
896 if(res != OC_STACK_OK)
898 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
901 if (gDevConfResource.handle != NULL)
903 res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
904 if(res != OC_STACK_OK)
906 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
910 if (gWiFiResource.handle != NULL)
912 res = OCDeleteResource(gWiFiResource.handle);
913 if (res != OC_STACK_OK)
915 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
919 if(gCloudResource.handle != NULL)
921 res = OCDeleteResource(gCloudResource.handle);
922 if (res != OC_STACK_OK)
924 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
928 if(gDevConfResource.handle != NULL)
930 res = OCDeleteResource(gDevConfResource.handle);
931 if (res != OC_STACK_OK)
933 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
937 if(gProvResource.handle != NULL)
939 res = OCDeleteResource(gProvResource.handle);
940 if (res != OC_STACK_OK)
942 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
949 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
951 OCEntityHandlerResult ehResult = OC_EH_ERROR;
954 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
957 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
959 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
963 OCRepPayload *getResp = NULL;
965 if(ehRequest->resource == gProvResource.handle)
967 getResp = constructResponseOfProv(ehRequest);
969 else if(ehRequest->resource == gWiFiResource.handle)
971 getResp = constructResponseOfWiFi(ehRequest);
973 else if(ehRequest->resource == gCloudResource.handle)
975 getResp = constructResponseOfCloud(ehRequest);
977 else if(ehRequest->resource == gDevConfResource.handle)
979 getResp = constructResponseOfDevConf(ehRequest);
984 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
994 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
996 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
997 OCEntityHandlerResult ehResult = OC_EH_ERROR;
998 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1000 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1004 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1007 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1011 if(ehRequest->resource == gProvResource.handle)
1013 updateProvResource(ehRequest, input);
1015 else if(ehRequest->resource == gWiFiResource.handle)
1017 updateWiFiResource(ehRequest, input);
1019 else if(ehRequest->resource == gCloudResource.handle)
1021 updateCloudResource(ehRequest, input);
1023 else if(ehRequest->resource == gDevConfResource.handle)
1025 updateDevConfResource(ehRequest, input);
1028 OCRepPayload *getResp = NULL;
1029 if(ehRequest->resource == gProvResource.handle)
1031 getResp = constructResponseOfProv(ehRequest);
1033 else if(ehRequest->resource == gWiFiResource.handle)
1035 getResp = constructResponseOfWiFi(NULL);
1037 else if(ehRequest->resource == gCloudResource.handle)
1039 getResp = constructResponseOfCloud(NULL);
1041 else if(ehRequest->resource == gDevConfResource.handle)
1043 getResp = constructResponseOfDevConf(NULL);
1048 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1053 ehResult = OC_EH_OK;
1058 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1059 OCRepPayload** payload)
1063 OCEntityHandlerResult ehResult = OC_EH_METHOD_NOT_ALLOWED;
1068 * This is the entity handler for the registered resource.
1069 * This is invoked by OCStack whenever it recevies a request for this resource.
1071 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1072 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1075 OCEntityHandlerResult ehRet = OC_EH_OK;
1076 OCEntityHandlerResponse response =
1077 { 0, 0, OC_EH_ERROR, 0, 0,
1080 OCRepPayload* payload = NULL;
1082 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1084 if (OC_REST_GET == entityHandlerRequest->method)
1086 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1087 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1089 else if (OC_REST_PUT == entityHandlerRequest->method)
1091 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1093 //PUT request will be handled in the internal implementation
1094 if (gProvResource.handle != NULL)
1096 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1100 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1101 ehRet = OC_EH_ERROR;
1104 else if (OC_REST_POST == entityHandlerRequest->method)
1106 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1107 if (gProvResource.handle != NULL)
1109 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1113 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1114 ehRet = OC_EH_ERROR;
1118 // Format the response. Note this requires some info about the request
1119 response.requestHandle = entityHandlerRequest->requestHandle;
1120 response.resourceHandle = entityHandlerRequest->resource;
1121 response.ehResult = ehRet;
1122 //response uses OCPaylod while all get,put methodes use OCRepPayload
1123 response.payload = (OCPayload*) (payload);
1124 response.numSendVendorSpecificHeaderOptions = 0;
1125 memset(response.sendVendorSpecificHeaderOptions, 0,
1126 sizeof(response.sendVendorSpecificHeaderOptions));
1127 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1128 // Indicate that response is NOT in a persistent buffer
1129 response.persistentBufferFlag = 0;
1131 // Send the response
1132 if (OCDoResponse(&response) != OC_STACK_OK)
1134 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1135 ehRet = OC_EH_ERROR;
1142 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1144 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
1146 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
1147 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
1150 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1152 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1153 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
1156 gWiFiResource.numMode = modeIdx;
1158 OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1159 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
1161 OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
1162 (deviceProperty->DevConf).modelNumber);
1163 OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
1165 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
1169 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1171 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
1173 gProvResource.status = esState;
1174 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
1176 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
1180 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1182 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
1184 gProvResource.lastErrCode = esErrCode;
1185 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
1187 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1191 const char *getResult(OCStackResult result)
1196 return "OC_STACK_OK";
1197 case OC_STACK_INVALID_URI:
1198 return "OC_STACK_INVALID_URI";
1199 case OC_STACK_INVALID_QUERY:
1200 return "OC_STACK_INVALID_QUERY";
1201 case OC_STACK_INVALID_IP:
1202 return "OC_STACK_INVALID_IP";
1203 case OC_STACK_INVALID_PORT:
1204 return "OC_STACK_INVALID_PORT";
1205 case OC_STACK_INVALID_CALLBACK:
1206 return "OC_STACK_INVALID_CALLBACK";
1207 case OC_STACK_INVALID_METHOD:
1208 return "OC_STACK_INVALID_METHOD";
1209 case OC_STACK_NO_MEMORY:
1210 return "OC_STACK_NO_MEMORY";
1211 case OC_STACK_COMM_ERROR:
1212 return "OC_STACK_COMM_ERROR";
1213 case OC_STACK_INVALID_PARAM:
1214 return "OC_STACK_INVALID_PARAM";
1215 case OC_STACK_NOTIMPL:
1216 return "OC_STACK_NOTIMPL";
1217 case OC_STACK_NO_RESOURCE:
1218 return "OC_STACK_NO_RESOURCE";
1219 case OC_STACK_RESOURCE_ERROR:
1220 return "OC_STACK_RESOURCE_ERROR";
1221 case OC_STACK_SLOW_RESOURCE:
1222 return "OC_STACK_SLOW_RESOURCE";
1223 case OC_STACK_NO_OBSERVERS:
1224 return "OC_STACK_NO_OBSERVERS";
1225 case OC_STACK_ERROR:
1226 return "OC_STACK_ERROR";