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, ";");
74 if(strstr(ptr, ".if."))
77 if_ptr = strtok(ptr, "=");
78 if_ptr = strtok(NULL, "=");
80 if(!strcmp(if_ptr, iface))
86 } while (ptr = strtok(NULL, ";"));
91 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
93 if(!readCb && !writeCb)
95 OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
98 gReadUserdataCb = readCb;
99 gWriteUserdataCb = writeCb;
103 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
108 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
110 gCloudRsrcEvtCb = cb;
113 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
115 gDevConfRsrcEvtCb = cb;
118 void UnRegisterResourceEventCallBack()
122 gWifiRsrcEvtCb = NULL;
126 gCloudRsrcEvtCb = NULL;
128 if (gDevConfRsrcEvtCb)
130 gDevConfRsrcEvtCb = NULL;
134 OCStackResult initProvResource(bool isSecured)
136 gProvResource.status = ES_STATE_INIT;
137 gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
138 OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
140 OCStackResult res = OC_STACK_ERROR;
143 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
144 OC_RSRVD_INTERFACE_DEFAULT,
145 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
146 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
149 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
150 OC_RSRVD_INTERFACE_DEFAULT,
151 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
152 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
156 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
160 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
163 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
166 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
169 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
173 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
177 OCStackResult initWiFiResource(bool isSecured)
179 OCStackResult res = OC_STACK_ERROR;
181 gWiFiResource.supportedFreq = WIFI_BOTH;
182 gWiFiResource.supportedMode[0] = WIFI_11A;
183 gWiFiResource.supportedMode[1] = WIFI_11B;
184 gWiFiResource.supportedMode[2] = WIFI_11G;
185 gWiFiResource.supportedMode[3] = WIFI_11N;
186 gWiFiResource.numMode = 4;
187 gWiFiResource.authType = NONE_AUTH;
188 gWiFiResource.encType = NONE_ENC;
189 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
190 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
194 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
195 OC_RSRVD_INTERFACE_DEFAULT,
196 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
197 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
200 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
201 OC_RSRVD_INTERFACE_DEFAULT,
202 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
203 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
206 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
211 OCStackResult initCloudServerResource(bool isSecured)
213 OCStackResult res = OC_STACK_ERROR;
215 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
216 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
217 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
221 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
222 OC_RSRVD_INTERFACE_DEFAULT,
223 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
224 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
227 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
228 OC_RSRVD_INTERFACE_DEFAULT,
229 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
230 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
233 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
238 OCStackResult initDevConfResource(bool isSecured)
240 OCStackResult res = OC_STACK_ERROR;
242 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
243 OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
244 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
245 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
246 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
250 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
251 OC_RSRVD_INTERFACE_DEFAULT,
252 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
253 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
256 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
257 OC_RSRVD_INTERFACE_DEFAULT,
258 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
259 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
262 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
267 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
269 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
273 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
275 // When Provisioning resource has a POST with BatchInterface
276 updateCloudResource(NULL, input);
277 updateWiFiResource(NULL, input);
278 updateDevConfResource(NULL, input);
283 void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
285 if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
287 // In case of link list, batch interface
288 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
292 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
296 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
300 memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
301 memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
302 wiFiData->authtype = NONE_AUTH;
303 wiFiData->enctype = NONE_AUTH;
304 wiFiData->userdata = NULL;
307 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
309 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
310 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
311 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
315 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
317 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
318 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
319 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
322 int64_t authType = -1;
323 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
325 gWiFiResource.authType = authType;
326 wiFiData->authtype = gWiFiResource.authType;
327 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
330 int64_t encType = -1;
331 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
333 gWiFiResource.encType = encType;
334 wiFiData->enctype = gWiFiResource.encType;
335 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
340 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
343 if(ssid || cred || authType!= -1 || encType != -1)
345 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
347 // TODO : Need to check appropriateness of gWiFiData
348 if(gWifiRsrcEvtCb != NULL)
350 gWifiRsrcEvtCb(ES_OK, wiFiData);
354 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
361 void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
363 if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
365 // In case of link list, batch interface
366 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
370 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
372 if(cloudData == NULL)
374 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
378 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
379 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
380 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
381 cloudData->userdata = NULL;
383 char *authCode = NULL;
384 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
386 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
387 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
388 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
391 char *authProvider = NULL;
392 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
394 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
395 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
396 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
399 char *ciServer = NULL;
400 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
402 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
403 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
404 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
409 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
412 if(authCode || authProvider || ciServer)
414 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
416 // TODO : Need to check appropriateness of gCloudData
417 if(gCloudRsrcEvtCb != NULL)
419 gCloudRsrcEvtCb(ES_OK, cloudData);
423 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
430 void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
432 if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
434 // In case of link list, batch interface
435 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
439 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
441 if(devConfData == NULL)
443 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
446 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
447 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
448 devConfData->userdata = NULL;
450 char *location = NULL;
451 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
453 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
454 OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
455 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
458 char *country = NULL;
459 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
461 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
462 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
463 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
466 char *language = NULL;
467 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
469 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
470 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
471 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
476 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
479 if(country || language)
481 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
483 // TODO : Need to check appropriateness of gDevConfData
484 if(gDevConfRsrcEvtCb != NULL)
486 gDevConfRsrcEvtCb(ES_OK, devConfData);
490 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
494 OICFree(devConfData);
497 OCRepPayload* constructResponseOfWiFi(OCEntityHandlerRequest *ehRequest)
499 if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
501 // In case of link list, batch interface
502 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
506 OCRepPayload* payload = OCRepPayloadCreate();
509 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
513 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
514 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
516 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
517 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
518 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
520 modes_64[i] = gWiFiResource.supportedMode[i];
522 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
524 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
525 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
526 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
527 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
528 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
532 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
538 OCRepPayload* constructResponseOfCloud(OCEntityHandlerRequest *ehRequest)
540 if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
542 // In case of link list, batch interface
543 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
547 OCRepPayload* payload = OCRepPayloadCreate();
550 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
554 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
555 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
556 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
557 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
558 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
562 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
568 OCRepPayload* constructResponseOfDevConf(OCEntityHandlerRequest *ehRequest)
570 if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
572 // In case of link list, batch interface
573 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
577 OCRepPayload* payload = OCRepPayloadCreate();
580 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
584 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
585 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
586 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
587 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
588 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
589 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
590 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
594 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
600 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
602 OCRepPayload* payload = OCRepPayloadCreate();
605 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
609 // Requested interface is Link list interface
610 if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
612 const OCRepPayload *arrayPayload[3] = {NULL};
616 if(gWiFiResource.handle != NULL)
618 OCRepPayload *add = OCRepPayloadCreate();
621 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
625 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
626 char **resourceType = NULL;
627 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
628 char **resourceInterface = NULL;
629 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
631 if(!resourceType || !resourceInterface)
633 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
637 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFI);
638 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
640 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
641 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFI);
642 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
643 (const char **)resourceType, dimensions);
644 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
645 (const char **)resourceInterface, dimensions);
647 arrayPayload[childResCnt++] = add;
650 if(gDevConfResource.handle != NULL)
652 OCRepPayload *add = OCRepPayloadCreate();
655 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
659 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
660 char **resourceType = NULL;
661 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
662 char **resourceInterface = NULL;
663 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
665 if(!resourceType || !resourceInterface)
667 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
671 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
672 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
674 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
675 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
676 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
677 (const char **)resourceType, dimensions);
678 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
679 (const char **)resourceInterface, dimensions);
681 arrayPayload[childResCnt++] = add;
684 if(gCloudResource.handle != NULL)
686 OCRepPayload *add = OCRepPayloadCreate();
689 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
693 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
694 char **resourceType = NULL;
695 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
696 char **resourceInterface = NULL;
697 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
699 if(!resourceType || !resourceInterface)
701 OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
705 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
706 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
708 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
709 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
710 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
711 (const char **)resourceType, dimensions);
712 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
713 (const char **)resourceInterface, dimensions);
715 arrayPayload[childResCnt++] = add;
718 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
719 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
722 } else if (!ehRequest->query ||
723 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH)) ||
724 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
726 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
727 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
728 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
729 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
730 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
735 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
740 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
741 {// When Provisioning resource has a GET with BatchInterface
742 OCRepPayload* head = payload;
743 OCRepPayload* nextPayload = NULL;
745 nextPayload = constructResponseOfWiFi(NULL);
746 if(nextPayload != NULL)
748 payload->next = nextPayload;
749 payload = payload->next;
752 nextPayload = constructResponseOfCloud(NULL);
753 if(nextPayload != NULL)
755 payload->next = nextPayload;
756 payload = payload->next;
759 nextPayload = constructResponseOfDevConf(NULL);
760 if(nextPayload != NULL)
762 payload->next = nextPayload;
763 payload = payload->next;
774 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
776 OCStackResult res = OC_STACK_ERROR;
777 bool maskFlag = false;
779 res = initProvResource(isSecured);
780 if(res != OC_STACK_OK)
782 // TODO: destroy logic will be added
783 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
788 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
791 res = initWiFiResource(isSecured);
792 if(res != OC_STACK_OK)
794 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
798 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
799 if(res != OC_STACK_OK)
801 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
807 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
810 res = initCloudServerResource(isSecured);
811 if(res != OC_STACK_OK)
813 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
817 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
818 if(res != OC_STACK_OK)
820 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
825 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
828 res = initDevConfResource(isSecured);
829 if(res != OC_STACK_OK)
831 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
835 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
836 if(res != OC_STACK_OK)
838 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
844 if(maskFlag == false)
846 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
847 return OC_STACK_ERROR;
851 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
856 OCStackResult DeleteProvisioningResource()
858 OCStackResult res = OCDeleteResource(gProvResource.handle);
859 if (res != OC_STACK_OK)
861 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
867 OCStackResult DeleteEasySetupResources()
869 OCStackResult res = OC_STACK_ERROR;
870 if (gWiFiResource.handle != NULL)
872 res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
873 if(res != OC_STACK_OK)
875 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
878 if (gCloudResource.handle != NULL)
880 res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
881 if(res != OC_STACK_OK)
883 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
886 if (gDevConfResource.handle != NULL)
888 res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
889 if(res != OC_STACK_OK)
891 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
895 if (gWiFiResource.handle != NULL)
897 res = OCDeleteResource(gWiFiResource.handle);
898 if (res != OC_STACK_OK)
900 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
904 if(gCloudResource.handle != NULL)
906 res = OCDeleteResource(gCloudResource.handle);
907 if (res != OC_STACK_OK)
909 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
913 if(gDevConfResource.handle != NULL)
915 res = OCDeleteResource(gDevConfResource.handle);
916 if (res != OC_STACK_OK)
918 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
922 if(gProvResource.handle != NULL)
924 res = OCDeleteResource(gProvResource.handle);
925 if (res != OC_STACK_OK)
927 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
934 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
936 OCEntityHandlerResult ehResult = OC_EH_ERROR;
939 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
942 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
944 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
948 OCRepPayload *getResp = NULL;
950 if(ehRequest->resource == gProvResource.handle)
952 getResp = constructResponseOfProv(ehRequest);
954 else if(ehRequest->resource == gWiFiResource.handle)
956 getResp = constructResponseOfWiFi(ehRequest);
958 else if(ehRequest->resource == gCloudResource.handle)
960 getResp = constructResponseOfCloud(ehRequest);
962 else if(ehRequest->resource == gDevConfResource.handle)
964 getResp = constructResponseOfDevConf(ehRequest);
969 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
979 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
981 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
982 OCEntityHandlerResult ehResult = OC_EH_ERROR;
983 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
985 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
989 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
992 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
996 if(ehRequest->resource == gProvResource.handle)
998 updateProvResource(ehRequest, input);
1000 else if(ehRequest->resource == gWiFiResource.handle)
1002 updateWiFiResource(ehRequest, input);
1004 else if(ehRequest->resource == gCloudResource.handle)
1006 updateCloudResource(ehRequest, input);
1008 else if(ehRequest->resource == gDevConfResource.handle)
1010 updateDevConfResource(ehRequest, input);
1013 OCRepPayload *getResp = NULL;
1014 if(ehRequest->resource == gProvResource.handle)
1016 getResp = constructResponseOfProv(ehRequest);
1018 else if(ehRequest->resource == gWiFiResource.handle)
1020 getResp = constructResponseOfWiFi(NULL);
1022 else if(ehRequest->resource == gCloudResource.handle)
1024 getResp = constructResponseOfCloud(NULL);
1026 else if(ehRequest->resource == gDevConfResource.handle)
1028 getResp = constructResponseOfDevConf(NULL);
1033 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1038 ehResult = OC_EH_OK;
1043 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1044 OCRepPayload** payload)
1048 OCEntityHandlerResult ehResult = OC_EH_METHOD_NOT_ALLOWED;
1053 * This is the entity handler for the registered resource.
1054 * This is invoked by OCStack whenever it recevies a request for this resource.
1056 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1057 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1060 OCEntityHandlerResult ehRet = OC_EH_OK;
1061 OCEntityHandlerResponse response =
1062 { 0, 0, OC_EH_ERROR, 0, 0,
1065 OCRepPayload* payload = NULL;
1067 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1069 if (OC_REST_GET == entityHandlerRequest->method)
1071 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1072 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1074 else if (OC_REST_PUT == entityHandlerRequest->method)
1076 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1078 //PUT request will be handled in the internal implementation
1079 if (gProvResource.handle != NULL)
1081 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1085 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1086 ehRet = OC_EH_ERROR;
1089 else if (OC_REST_POST == entityHandlerRequest->method)
1091 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1092 if (gProvResource.handle != NULL)
1094 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1098 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1099 ehRet = OC_EH_ERROR;
1103 // Format the response. Note this requires some info about the request
1104 response.requestHandle = entityHandlerRequest->requestHandle;
1105 response.resourceHandle = entityHandlerRequest->resource;
1106 response.ehResult = ehRet;
1107 //response uses OCPaylod while all get,put methodes use OCRepPayload
1108 response.payload = (OCPayload*) (payload);
1109 response.numSendVendorSpecificHeaderOptions = 0;
1110 memset(response.sendVendorSpecificHeaderOptions, 0,
1111 sizeof(response.sendVendorSpecificHeaderOptions));
1112 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1113 // Indicate that response is NOT in a persistent buffer
1114 response.persistentBufferFlag = 0;
1116 // Send the response
1117 if (OCDoResponse(&response) != OC_STACK_OK)
1119 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1120 ehRet = OC_EH_ERROR;
1127 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1129 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
1131 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
1132 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
1135 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1137 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1138 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
1141 gWiFiResource.numMode = modeIdx;
1143 OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1144 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
1146 OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
1147 (deviceProperty->DevConf).modelNumber);
1148 OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
1150 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
1154 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1156 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
1158 gProvResource.status = esState;
1159 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
1161 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
1165 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1167 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
1169 gProvResource.lastErrCode = esErrCode;
1170 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
1172 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1176 const char *getResult(OCStackResult result)
1181 return "OC_STACK_OK";
1182 case OC_STACK_INVALID_URI:
1183 return "OC_STACK_INVALID_URI";
1184 case OC_STACK_INVALID_QUERY:
1185 return "OC_STACK_INVALID_QUERY";
1186 case OC_STACK_INVALID_IP:
1187 return "OC_STACK_INVALID_IP";
1188 case OC_STACK_INVALID_PORT:
1189 return "OC_STACK_INVALID_PORT";
1190 case OC_STACK_INVALID_CALLBACK:
1191 return "OC_STACK_INVALID_CALLBACK";
1192 case OC_STACK_INVALID_METHOD:
1193 return "OC_STACK_INVALID_METHOD";
1194 case OC_STACK_NO_MEMORY:
1195 return "OC_STACK_NO_MEMORY";
1196 case OC_STACK_COMM_ERROR:
1197 return "OC_STACK_COMM_ERROR";
1198 case OC_STACK_INVALID_PARAM:
1199 return "OC_STACK_INVALID_PARAM";
1200 case OC_STACK_NOTIMPL:
1201 return "OC_STACK_NOTIMPL";
1202 case OC_STACK_NO_RESOURCE:
1203 return "OC_STACK_NO_RESOURCE";
1204 case OC_STACK_RESOURCE_ERROR:
1205 return "OC_STACK_RESOURCE_ERROR";
1206 case OC_STACK_SLOW_RESOURCE:
1207 return "OC_STACK_SLOW_RESOURCE";
1208 case OC_STACK_NO_OBSERVERS:
1209 return "OC_STACK_NO_OBSERVERS";
1210 case OC_STACK_ERROR:
1211 return "OC_STACK_ERROR";