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(OCRepPayload* input);
56 void updateCloudResource(OCRepPayload* input);
57 void updateDevConfResource(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, ";");
79 if(strstr(ptr, ".if."))
82 if_ptr = strtok(ptr, "=");
83 if_ptr = strtok(NULL, "=");
85 if(!strcmp(if_ptr, iface))
91 } while ((ptr = strtok(NULL, ";")));
96 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
98 if(!readCb && !writeCb)
100 OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
103 gReadUserdataCb = readCb;
104 gWriteUserdataCb = writeCb;
108 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
113 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
115 gCloudRsrcEvtCb = cb;
118 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
120 gDevConfRsrcEvtCb = cb;
123 void UnRegisterResourceEventCallBack()
127 gWifiRsrcEvtCb = NULL;
131 gCloudRsrcEvtCb = NULL;
133 if (gDevConfRsrcEvtCb)
135 gDevConfRsrcEvtCb = NULL;
139 OCStackResult initProvResource(bool isSecured)
141 gProvResource.status = ES_STATE_INIT;
142 gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
143 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
145 OCStackResult res = OC_STACK_ERROR;
148 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
149 OC_RSRVD_INTERFACE_DEFAULT,
150 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
151 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
154 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
155 OC_RSRVD_INTERFACE_DEFAULT,
156 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
157 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
161 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
165 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
168 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
171 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
174 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
178 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
182 OCStackResult initWiFiResource(bool isSecured)
184 OCStackResult res = OC_STACK_ERROR;
186 gWiFiResource.supportedFreq = WIFI_BOTH;
187 gWiFiResource.supportedMode[0] = WIFI_11A;
188 gWiFiResource.supportedMode[1] = WIFI_11B;
189 gWiFiResource.supportedMode[2] = WIFI_11G;
190 gWiFiResource.supportedMode[3] = WIFI_11N;
191 gWiFiResource.numMode = 4;
192 gWiFiResource.authType = NONE_AUTH;
193 gWiFiResource.encType = NONE_ENC;
194 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
195 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
199 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
200 OC_RSRVD_INTERFACE_DEFAULT,
201 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
202 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
205 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
206 OC_RSRVD_INTERFACE_DEFAULT,
207 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
208 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
211 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
216 OCStackResult initCloudServerResource(bool isSecured)
218 OCStackResult res = OC_STACK_ERROR;
220 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
221 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
222 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
226 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
227 OC_RSRVD_INTERFACE_DEFAULT,
228 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
229 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
232 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
233 OC_RSRVD_INTERFACE_DEFAULT,
234 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
235 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
238 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
243 OCStackResult initDevConfResource(bool isSecured)
245 OCStackResult res = OC_STACK_ERROR;
247 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
248 OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
249 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
250 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
251 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
255 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
256 OC_RSRVD_INTERFACE_DEFAULT,
257 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
258 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
261 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
262 OC_RSRVD_INTERFACE_DEFAULT,
263 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
264 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
267 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
272 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
274 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
278 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
280 // When Provisioning resource has a POST with BatchInterface
281 updateCloudResource(input);
282 updateWiFiResource(input);
283 updateDevConfResource(input);
288 void updateWiFiResource(OCRepPayload* input)
290 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
294 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
298 memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
299 memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
300 wiFiData->authtype = NONE_AUTH;
301 wiFiData->enctype = NONE_AUTH;
302 wiFiData->userdata = NULL;
305 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
307 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
308 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
309 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
313 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
315 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
316 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
317 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
320 int64_t authType = -1;
321 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
323 gWiFiResource.authType = authType;
324 wiFiData->authtype = gWiFiResource.authType;
325 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
328 int64_t encType = -1;
329 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
331 gWiFiResource.encType = encType;
332 wiFiData->enctype = gWiFiResource.encType;
333 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
338 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
341 if(ssid || cred || authType!= -1 || encType != -1)
343 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
345 // TODO : Need to check appropriateness of gWiFiData
346 if(gWifiRsrcEvtCb != NULL)
348 gWifiRsrcEvtCb(ES_OK, wiFiData);
352 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
359 void updateCloudResource(OCRepPayload* input)
361 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
363 if(cloudData == NULL)
365 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
369 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
370 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
371 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
372 cloudData->userdata = NULL;
374 char *authCode = NULL;
375 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
377 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
378 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
379 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
382 char *authProvider = NULL;
383 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
385 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
386 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
387 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
390 char *ciServer = NULL;
391 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
393 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
394 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
395 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
400 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
403 if(authCode || authProvider || ciServer)
405 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
407 // TODO : Need to check appropriateness of gCloudData
408 if(gCloudRsrcEvtCb != NULL)
410 gCloudRsrcEvtCb(ES_OK, cloudData);
414 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
421 void updateDevConfResource(OCRepPayload* input)
423 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
425 if(devConfData == NULL)
427 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
430 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
431 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
432 devConfData->userdata = NULL;
434 char *location = NULL;
435 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
437 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
438 OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
439 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
442 char *country = NULL;
443 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
445 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
446 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
447 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
450 char *language = NULL;
451 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
453 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
454 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
455 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
460 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
463 if(country || language)
465 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
467 // TODO : Need to check appropriateness of gDevConfData
468 if(gDevConfRsrcEvtCb != NULL)
470 gDevConfRsrcEvtCb(ES_OK, devConfData);
474 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
478 OICFree(devConfData);
481 OCRepPayload* constructResponseOfWiFi()
483 OCRepPayload* payload = OCRepPayloadCreate();
486 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
490 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
491 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
492 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
493 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
495 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
496 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
497 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
499 modes_64[i] = gWiFiResource.supportedMode[i];
501 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
503 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
504 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
505 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
506 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
507 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
511 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
517 OCRepPayload* constructResponseOfCloud()
519 OCRepPayload* payload = OCRepPayloadCreate();
522 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
526 OIC_LOG(INFO, ES_RH_TAG, "constructResponse cloudserver res");
527 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
528 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
529 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
531 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
532 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
533 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
537 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
543 OCRepPayload* constructResponseOfDevConf()
545 OCRepPayload* payload = OCRepPayloadCreate();
548 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
552 OIC_LOG(INFO, ES_RH_TAG, "constructResponse devconf res");
553 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
554 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
555 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
557 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
558 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
559 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
560 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
561 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
565 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
571 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
573 OCRepPayload* payload = OCRepPayloadCreate();
576 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
580 // Requested interface is Link list interface
581 //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
582 if(!ehRequest->query ||
583 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
584 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
585 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
587 const OCRepPayload *arrayPayload[3] = {NULL};
591 if(gWiFiResource.handle != NULL)
593 OCRepPayload *add = OCRepPayloadCreate();
596 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
600 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
601 char **resourceType = NULL;
602 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
603 char **resourceInterface = NULL;
604 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
606 if(!resourceType || !resourceInterface)
608 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
612 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFI);
613 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
615 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
616 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFI);
617 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
618 (const char **)resourceType, dimensions);
619 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
620 (const char **)resourceInterface, dimensions);
622 arrayPayload[childResCnt++] = add;
625 if(gDevConfResource.handle != NULL)
627 OCRepPayload *add = OCRepPayloadCreate();
630 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
634 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
635 char **resourceType = NULL;
636 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
637 char **resourceInterface = NULL;
638 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
640 if(!resourceType || !resourceInterface)
642 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
646 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
647 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
649 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
650 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
651 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
652 (const char **)resourceType, dimensions);
653 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
654 (const char **)resourceInterface, dimensions);
656 arrayPayload[childResCnt++] = add;
659 if(gCloudResource.handle != NULL)
661 OCRepPayload *add = OCRepPayloadCreate();
664 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
668 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
669 char **resourceType = NULL;
670 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
671 char **resourceInterface = NULL;
672 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
674 if(!resourceType || !resourceInterface)
676 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
680 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
681 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
683 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
684 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
685 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
686 (const char **)resourceType, dimensions);
687 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
688 (const char **)resourceInterface, dimensions);
690 arrayPayload[childResCnt++] = add;
693 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
695 if(!ehRequest->query ||
696 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
697 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
699 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
700 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
701 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
702 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
703 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
704 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_PROV);
706 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
707 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
709 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
711 else // link list interface
713 OCRepPayload* head = payload;
714 OCRepPayload* nextPayload = NULL;
716 for(int i = 0 ; i < childResCnt ; ++i)
718 nextPayload = arrayPayload[i];
719 if(nextPayload != NULL)
721 payload->next = nextPayload;
722 payload = payload->next;
725 if(head->next != NULL)
727 payload = head->next;
735 ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
738 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
739 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
740 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
741 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
742 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
743 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_PROV);
745 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
746 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
751 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
756 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
757 {// When Provisioning resource has a GET with BatchInterface
758 OCRepPayload* head = payload;
759 OCRepPayload* nextPayload = NULL;
761 nextPayload = constructResponseOfWiFi();
762 if(nextPayload != NULL)
764 payload->next = nextPayload;
765 payload = payload->next;
768 nextPayload = constructResponseOfCloud();
769 if(nextPayload != NULL)
771 payload->next = nextPayload;
772 payload = payload->next;
775 nextPayload = constructResponseOfDevConf();
776 if(nextPayload != NULL)
778 payload->next = nextPayload;
779 payload = payload->next;
790 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
792 OCStackResult res = OC_STACK_ERROR;
793 bool maskFlag = false;
795 res = initProvResource(isSecured);
796 if(res != OC_STACK_OK)
798 // TODO: destroy logic will be added
799 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
804 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
807 res = initWiFiResource(isSecured);
808 if(res != OC_STACK_OK)
810 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
814 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
815 if(res != OC_STACK_OK)
817 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
823 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
826 res = initCloudServerResource(isSecured);
827 if(res != OC_STACK_OK)
829 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
833 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
834 if(res != OC_STACK_OK)
836 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
841 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
844 res = initDevConfResource(isSecured);
845 if(res != OC_STACK_OK)
847 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
851 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
852 if(res != OC_STACK_OK)
854 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
860 if(maskFlag == false)
862 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
863 return OC_STACK_ERROR;
867 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
872 OCStackResult DeleteProvisioningResource()
874 OCStackResult res = OCDeleteResource(gProvResource.handle);
875 if (res != OC_STACK_OK)
877 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
883 OCStackResult DeleteEasySetupResources()
885 OCStackResult res = OC_STACK_ERROR;
886 if (gWiFiResource.handle != NULL)
888 res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
889 if(res != OC_STACK_OK)
891 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
894 if (gCloudResource.handle != NULL)
896 res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
897 if(res != OC_STACK_OK)
899 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
902 if (gDevConfResource.handle != NULL)
904 res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
905 if(res != OC_STACK_OK)
907 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
911 if (gWiFiResource.handle != NULL)
913 res = OCDeleteResource(gWiFiResource.handle);
914 if (res != OC_STACK_OK)
916 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
920 if(gCloudResource.handle != NULL)
922 res = OCDeleteResource(gCloudResource.handle);
923 if (res != OC_STACK_OK)
925 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
929 if(gDevConfResource.handle != NULL)
931 res = OCDeleteResource(gDevConfResource.handle);
932 if (res != OC_STACK_OK)
934 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
938 if(gProvResource.handle != NULL)
940 res = OCDeleteResource(gProvResource.handle);
941 if (res != OC_STACK_OK)
943 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
950 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
952 OCEntityHandlerResult ehResult = OC_EH_ERROR;
955 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
958 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
960 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
964 OCRepPayload *getResp = NULL;
967 if(ehRequest->resource == gProvResource.handle)
969 if(ehRequest->query &&
970 strcmp(ehRequest->query, "") &&
971 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
972 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
973 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
975 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
976 return OC_EH_BAD_REQ;
980 getResp = constructResponseOfProv(ehRequest);
983 else if(ehRequest->resource == gWiFiResource.handle)
985 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
987 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
988 return OC_EH_BAD_REQ;
992 getResp = constructResponseOfWiFi();
995 else if(ehRequest->resource == gCloudResource.handle)
997 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
999 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1000 return OC_EH_BAD_REQ;
1004 getResp = constructResponseOfCloud();
1007 else if(ehRequest->resource == gDevConfResource.handle)
1009 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1011 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1012 return OC_EH_BAD_REQ;
1016 getResp = constructResponseOfDevConf();
1022 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1027 ehResult = OC_EH_OK;
1032 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1034 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
1035 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1036 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1038 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1042 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1045 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1049 if(ehRequest->resource == gProvResource.handle)
1051 if(ehRequest->query &&
1052 strcmp(ehRequest->query, "") &&
1053 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1054 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1056 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1057 return OC_EH_BAD_REQ;
1061 updateProvResource(ehRequest, input);
1064 else if(ehRequest->resource == gWiFiResource.handle)
1066 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1068 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1069 return OC_EH_BAD_REQ;
1073 updateWiFiResource(input);
1076 else if(ehRequest->resource == gCloudResource.handle)
1078 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1080 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1081 return OC_EH_BAD_REQ;
1085 updateCloudResource(input);
1088 else if(ehRequest->resource == gDevConfResource.handle)
1090 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1092 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1093 return OC_EH_BAD_REQ;
1097 updateDevConfResource(input);
1101 OCRepPayload *getResp = NULL;
1102 if(ehRequest->resource == gProvResource.handle)
1104 getResp = constructResponseOfProv(ehRequest);
1106 else if(ehRequest->resource == gWiFiResource.handle)
1108 getResp = constructResponseOfWiFi();
1110 else if(ehRequest->resource == gCloudResource.handle)
1112 getResp = constructResponseOfCloud();
1114 else if(ehRequest->resource == gDevConfResource.handle)
1116 getResp = constructResponseOfDevConf();
1121 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1126 ehResult = OC_EH_OK;
1131 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1132 OCRepPayload** payload)
1136 OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1141 * This is the entity handler for the registered resource.
1142 * This is invoked by OCStack whenever it recevies a request for this resource.
1144 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1145 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1148 OCEntityHandlerResult ehRet = OC_EH_OK;
1149 OCEntityHandlerResponse response =
1150 { 0, 0, OC_EH_ERROR, 0, 0,
1153 OCRepPayload* payload = NULL;
1155 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1157 if (OC_REST_GET == entityHandlerRequest->method)
1159 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1160 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1162 else if (OC_REST_PUT == entityHandlerRequest->method)
1164 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1166 //PUT request will be handled in the internal implementation
1167 if (gProvResource.handle != NULL)
1169 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1173 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1174 ehRet = OC_EH_ERROR;
1177 else if (OC_REST_POST == entityHandlerRequest->method)
1179 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1180 if (gProvResource.handle != NULL)
1182 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1186 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1187 ehRet = OC_EH_ERROR;
1191 // Format the response. Note this requires some info about the request
1192 response.requestHandle = entityHandlerRequest->requestHandle;
1193 response.resourceHandle = entityHandlerRequest->resource;
1194 response.ehResult = ehRet;
1195 //response uses OCPaylod while all get,put methodes use OCRepPayload
1196 response.payload = (OCPayload*) (payload);
1197 response.numSendVendorSpecificHeaderOptions = 0;
1198 memset(response.sendVendorSpecificHeaderOptions, 0,
1199 sizeof(response.sendVendorSpecificHeaderOptions));
1200 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1201 // Indicate that response is NOT in a persistent buffer
1202 response.persistentBufferFlag = 0;
1204 // Send the response
1205 if (OCDoResponse(&response) != OC_STACK_OK)
1207 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1208 ehRet = OC_EH_ERROR;
1215 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1217 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
1219 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
1220 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
1223 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1225 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1226 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
1229 gWiFiResource.numMode = modeIdx;
1231 OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1232 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
1234 OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
1235 (deviceProperty->DevConf).modelNumber);
1236 OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
1238 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
1242 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1244 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
1246 gProvResource.status = esState;
1247 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
1249 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
1253 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1255 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
1257 gProvResource.lastErrCode = esErrCode;
1258 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
1260 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1264 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1266 if( !(ehRequest->query) ||
1267 (ehRequest->query &&
1268 (strcmp(ehRequest->query, "") && !CompareResourceInterface(ehRequest->query,
1269 OC_RSRVD_INTERFACE_DEFAULT))))
1271 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1272 return OC_EH_BAD_REQ;
1277 const char *getResult(OCStackResult result)
1282 return "OC_STACK_OK";
1283 case OC_STACK_INVALID_URI:
1284 return "OC_STACK_INVALID_URI";
1285 case OC_STACK_INVALID_QUERY:
1286 return "OC_STACK_INVALID_QUERY";
1287 case OC_STACK_INVALID_IP:
1288 return "OC_STACK_INVALID_IP";
1289 case OC_STACK_INVALID_PORT:
1290 return "OC_STACK_INVALID_PORT";
1291 case OC_STACK_INVALID_CALLBACK:
1292 return "OC_STACK_INVALID_CALLBACK";
1293 case OC_STACK_INVALID_METHOD:
1294 return "OC_STACK_INVALID_METHOD";
1295 case OC_STACK_NO_MEMORY:
1296 return "OC_STACK_NO_MEMORY";
1297 case OC_STACK_COMM_ERROR:
1298 return "OC_STACK_COMM_ERROR";
1299 case OC_STACK_INVALID_PARAM:
1300 return "OC_STACK_INVALID_PARAM";
1301 case OC_STACK_NOTIMPL:
1302 return "OC_STACK_NOTIMPL";
1303 case OC_STACK_NO_RESOURCE:
1304 return "OC_STACK_NO_RESOURCE";
1305 case OC_STACK_RESOURCE_ERROR:
1306 return "OC_STACK_RESOURCE_ERROR";
1307 case OC_STACK_SLOW_RESOURCE:
1308 return "OC_STACK_SLOW_RESOURCE";
1309 case OC_STACK_NO_OBSERVERS:
1310 return "OC_STACK_NO_OBSERVERS";
1311 case OC_STACK_ERROR:
1312 return "OC_STACK_ERROR";