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, ";");
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(NULL, input);
282 updateWiFiResource(NULL, input);
283 updateDevConfResource(NULL, input);
288 void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
291 strcmp(ehRequest->query, "") &&
292 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
294 // In case of link list, batch interface
295 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
299 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
303 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
307 memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
308 memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
309 wiFiData->authtype = NONE_AUTH;
310 wiFiData->enctype = NONE_AUTH;
311 wiFiData->userdata = NULL;
314 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
316 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
317 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
318 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
322 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
324 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
325 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
326 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
329 int64_t authType = -1;
330 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
332 gWiFiResource.authType = authType;
333 wiFiData->authtype = gWiFiResource.authType;
334 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
337 int64_t encType = -1;
338 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
340 gWiFiResource.encType = encType;
341 wiFiData->enctype = gWiFiResource.encType;
342 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
347 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
350 if(ssid || cred || authType!= -1 || encType != -1)
352 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
354 // TODO : Need to check appropriateness of gWiFiData
355 if(gWifiRsrcEvtCb != NULL)
357 gWifiRsrcEvtCb(ES_OK, wiFiData);
361 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
368 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
371 strcmp(ehRequest->query, "") &&
372 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
374 // In case of link list, batch interface
375 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
379 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
381 if(cloudData == NULL)
383 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
387 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
388 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
389 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
390 cloudData->userdata = NULL;
392 char *authCode = NULL;
393 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
395 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
396 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
397 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
400 char *authProvider = NULL;
401 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
403 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
404 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
405 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
408 char *ciServer = NULL;
409 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
411 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
412 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
413 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
418 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
421 if(authCode || authProvider || ciServer)
423 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
425 // TODO : Need to check appropriateness of gCloudData
426 if(gCloudRsrcEvtCb != NULL)
428 gCloudRsrcEvtCb(ES_OK, cloudData);
432 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
439 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
442 strcmp(ehRequest->query, "") &&
443 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
445 // In case of link list, batch interface
446 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
450 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
452 if(devConfData == NULL)
454 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
457 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
458 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
459 devConfData->userdata = NULL;
461 char *location = NULL;
462 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
464 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
465 OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
466 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
469 char *country = NULL;
470 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
472 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
473 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
474 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
477 char *language = NULL;
478 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
480 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
481 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
482 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
487 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
490 if(country || language)
492 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
494 // TODO : Need to check appropriateness of gDevConfData
495 if(gDevConfRsrcEvtCb != NULL)
497 gDevConfRsrcEvtCb(ES_OK, devConfData);
501 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
505 OICFree(devConfData);
508 OCRepPayload* constructResponseOfWiFi(OCEntityHandlerRequest *ehRequest)
511 strcmp(ehRequest->query, "") &&
512 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
514 // In case of link list, batch interface
515 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
519 OCRepPayload* payload = OCRepPayloadCreate();
522 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
526 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
527 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_WIFI);
529 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
530 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
531 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
533 modes_64[i] = gWiFiResource.supportedMode[i];
535 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
537 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
538 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
539 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
540 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
541 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
545 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
551 OCRepPayload* constructResponseOfCloud(OCEntityHandlerRequest *ehRequest)
554 strcmp(ehRequest->query, "") &&
555 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
557 // In case of link list, batch interface
558 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
562 OCRepPayload* payload = OCRepPayloadCreate();
565 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
569 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
570 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
571 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
572 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
573 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
577 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
583 OCRepPayload* constructResponseOfDevConf(OCEntityHandlerRequest *ehRequest)
586 strcmp(ehRequest->query, "") &&
587 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
589 // In case of link list, batch interface
590 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
594 OCRepPayload* payload = OCRepPayloadCreate();
597 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
601 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
602 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_DEVCONF);
603 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
604 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
605 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
606 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
607 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
611 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
617 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
619 OCRepPayload* payload = OCRepPayloadCreate();
622 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
626 // Requested interface is Link list interface
627 if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
629 const OCRepPayload *arrayPayload[3] = {NULL};
633 if(gWiFiResource.handle != NULL)
635 OCRepPayload *add = OCRepPayloadCreate();
638 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
642 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
643 char **resourceType = NULL;
644 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
645 char **resourceInterface = NULL;
646 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
648 if(!resourceType || !resourceInterface)
650 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
654 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFI);
655 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
657 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
658 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFI);
659 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
660 (const char **)resourceType, dimensions);
661 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
662 (const char **)resourceInterface, dimensions);
664 arrayPayload[childResCnt++] = add;
667 if(gDevConfResource.handle != NULL)
669 OCRepPayload *add = OCRepPayloadCreate();
672 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
676 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
677 char **resourceType = NULL;
678 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
679 char **resourceInterface = NULL;
680 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
682 if(!resourceType || !resourceInterface)
684 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
688 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
689 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
691 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
692 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
693 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
694 (const char **)resourceType, dimensions);
695 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
696 (const char **)resourceInterface, dimensions);
698 arrayPayload[childResCnt++] = add;
701 if(gCloudResource.handle != NULL)
703 OCRepPayload *add = OCRepPayloadCreate();
706 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
710 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
711 char **resourceType = NULL;
712 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
713 char **resourceInterface = NULL;
714 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
716 if(!resourceType || !resourceInterface)
718 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
722 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
723 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
725 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
726 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
727 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
728 (const char **)resourceType, dimensions);
729 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
730 (const char **)resourceInterface, dimensions);
732 arrayPayload[childResCnt++] = add;
735 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
736 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
739 } else if (!ehRequest->query ||
740 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH)) ||
741 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
743 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
744 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_PROV);
745 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
746 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
747 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
752 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
757 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
758 {// When Provisioning resource has a GET with BatchInterface
759 OCRepPayload* head = payload;
760 OCRepPayload* nextPayload = NULL;
762 nextPayload = constructResponseOfWiFi(NULL);
763 if(nextPayload != NULL)
765 payload->next = nextPayload;
766 payload = payload->next;
769 nextPayload = constructResponseOfCloud(NULL);
770 if(nextPayload != NULL)
772 payload->next = nextPayload;
773 payload = payload->next;
776 nextPayload = constructResponseOfDevConf(NULL);
777 if(nextPayload != NULL)
779 payload->next = nextPayload;
780 payload = payload->next;
791 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
793 OCStackResult res = OC_STACK_ERROR;
794 bool maskFlag = false;
796 res = initProvResource(isSecured);
797 if(res != OC_STACK_OK)
799 // TODO: destroy logic will be added
800 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
805 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
808 res = initWiFiResource(isSecured);
809 if(res != OC_STACK_OK)
811 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
815 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
816 if(res != OC_STACK_OK)
818 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
824 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
827 res = initCloudServerResource(isSecured);
828 if(res != OC_STACK_OK)
830 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
834 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
835 if(res != OC_STACK_OK)
837 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
842 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
845 res = initDevConfResource(isSecured);
846 if(res != OC_STACK_OK)
848 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
852 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
853 if(res != OC_STACK_OK)
855 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
861 if(maskFlag == false)
863 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
864 return OC_STACK_ERROR;
868 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
873 OCStackResult DeleteProvisioningResource()
875 OCStackResult res = OCDeleteResource(gProvResource.handle);
876 if (res != OC_STACK_OK)
878 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
884 OCStackResult DeleteEasySetupResources()
886 OCStackResult res = OC_STACK_ERROR;
887 if (gWiFiResource.handle != NULL)
889 res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
890 if(res != OC_STACK_OK)
892 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
895 if (gCloudResource.handle != NULL)
897 res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
898 if(res != OC_STACK_OK)
900 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
903 if (gDevConfResource.handle != NULL)
905 res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
906 if(res != OC_STACK_OK)
908 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
912 if (gWiFiResource.handle != NULL)
914 res = OCDeleteResource(gWiFiResource.handle);
915 if (res != OC_STACK_OK)
917 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
921 if(gCloudResource.handle != NULL)
923 res = OCDeleteResource(gCloudResource.handle);
924 if (res != OC_STACK_OK)
926 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
930 if(gDevConfResource.handle != NULL)
932 res = OCDeleteResource(gDevConfResource.handle);
933 if (res != OC_STACK_OK)
935 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
939 if(gProvResource.handle != NULL)
941 res = OCDeleteResource(gProvResource.handle);
942 if (res != OC_STACK_OK)
944 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
951 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
953 OCEntityHandlerResult ehResult = OC_EH_ERROR;
956 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
959 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
961 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
965 OCRepPayload *getResp = NULL;
967 if(ehRequest->resource == gProvResource.handle)
969 getResp = constructResponseOfProv(ehRequest);
971 else if(ehRequest->resource == gWiFiResource.handle)
973 getResp = constructResponseOfWiFi(ehRequest);
975 else if(ehRequest->resource == gCloudResource.handle)
977 getResp = constructResponseOfCloud(ehRequest);
979 else if(ehRequest->resource == gDevConfResource.handle)
981 getResp = constructResponseOfDevConf(ehRequest);
986 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
996 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
998 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
999 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1000 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1002 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1006 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1009 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1013 if(ehRequest->resource == gProvResource.handle)
1015 updateProvResource(ehRequest, input);
1017 else if(ehRequest->resource == gWiFiResource.handle)
1019 updateWiFiResource(ehRequest, input);
1021 else if(ehRequest->resource == gCloudResource.handle)
1023 updateCloudResource(ehRequest, input);
1025 else if(ehRequest->resource == gDevConfResource.handle)
1027 updateDevConfResource(ehRequest, input);
1030 OCRepPayload *getResp = NULL;
1031 if(ehRequest->resource == gProvResource.handle)
1033 getResp = constructResponseOfProv(ehRequest);
1035 else if(ehRequest->resource == gWiFiResource.handle)
1037 getResp = constructResponseOfWiFi(NULL);
1039 else if(ehRequest->resource == gCloudResource.handle)
1041 getResp = constructResponseOfCloud(NULL);
1043 else if(ehRequest->resource == gDevConfResource.handle)
1045 getResp = constructResponseOfDevConf(NULL);
1050 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1055 ehResult = OC_EH_OK;
1060 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1061 OCRepPayload** payload)
1065 OCEntityHandlerResult ehResult = OC_EH_METHOD_NOT_ALLOWED;
1070 * This is the entity handler for the registered resource.
1071 * This is invoked by OCStack whenever it recevies a request for this resource.
1073 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1074 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1077 OCEntityHandlerResult ehRet = OC_EH_OK;
1078 OCEntityHandlerResponse response =
1079 { 0, 0, OC_EH_ERROR, 0, 0,
1082 OCRepPayload* payload = NULL;
1084 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1086 if (OC_REST_GET == entityHandlerRequest->method)
1088 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1089 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1091 else if (OC_REST_PUT == entityHandlerRequest->method)
1093 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1095 //PUT request will be handled in the internal implementation
1096 if (gProvResource.handle != NULL)
1098 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1102 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1103 ehRet = OC_EH_ERROR;
1106 else if (OC_REST_POST == entityHandlerRequest->method)
1108 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1109 if (gProvResource.handle != NULL)
1111 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1115 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1116 ehRet = OC_EH_ERROR;
1120 // Format the response. Note this requires some info about the request
1121 response.requestHandle = entityHandlerRequest->requestHandle;
1122 response.resourceHandle = entityHandlerRequest->resource;
1123 response.ehResult = ehRet;
1124 //response uses OCPaylod while all get,put methodes use OCRepPayload
1125 response.payload = (OCPayload*) (payload);
1126 response.numSendVendorSpecificHeaderOptions = 0;
1127 memset(response.sendVendorSpecificHeaderOptions, 0,
1128 sizeof(response.sendVendorSpecificHeaderOptions));
1129 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1130 // Indicate that response is NOT in a persistent buffer
1131 response.persistentBufferFlag = 0;
1133 // Send the response
1134 if (OCDoResponse(&response) != OC_STACK_OK)
1136 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1137 ehRet = OC_EH_ERROR;
1144 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1146 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
1148 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
1149 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
1152 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1154 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1155 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
1158 gWiFiResource.numMode = modeIdx;
1160 OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1161 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
1163 OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
1164 (deviceProperty->DevConf).modelNumber);
1165 OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
1167 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
1171 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1173 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
1175 gProvResource.status = esState;
1176 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
1178 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
1182 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1184 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
1186 gProvResource.lastErrCode = esErrCode;
1187 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
1189 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1193 const char *getResult(OCStackResult result)
1198 return "OC_STACK_OK";
1199 case OC_STACK_INVALID_URI:
1200 return "OC_STACK_INVALID_URI";
1201 case OC_STACK_INVALID_QUERY:
1202 return "OC_STACK_INVALID_QUERY";
1203 case OC_STACK_INVALID_IP:
1204 return "OC_STACK_INVALID_IP";
1205 case OC_STACK_INVALID_PORT:
1206 return "OC_STACK_INVALID_PORT";
1207 case OC_STACK_INVALID_CALLBACK:
1208 return "OC_STACK_INVALID_CALLBACK";
1209 case OC_STACK_INVALID_METHOD:
1210 return "OC_STACK_INVALID_METHOD";
1211 case OC_STACK_NO_MEMORY:
1212 return "OC_STACK_NO_MEMORY";
1213 case OC_STACK_COMM_ERROR:
1214 return "OC_STACK_COMM_ERROR";
1215 case OC_STACK_INVALID_PARAM:
1216 return "OC_STACK_INVALID_PARAM";
1217 case OC_STACK_NOTIMPL:
1218 return "OC_STACK_NOTIMPL";
1219 case OC_STACK_NO_RESOURCE:
1220 return "OC_STACK_NO_RESOURCE";
1221 case OC_STACK_RESOURCE_ERROR:
1222 return "OC_STACK_RESOURCE_ERROR";
1223 case OC_STACK_SLOW_RESOURCE:
1224 return "OC_STACK_SLOW_RESOURCE";
1225 case OC_STACK_NO_OBSERVERS:
1226 return "OC_STACK_NO_OBSERVERS";
1227 case OC_STACK_ERROR:
1228 return "OC_STACK_ERROR";