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"
26 #include "cautilinterface.h"
30 * @brief Logging tag for module name.
32 #define ES_RH_TAG "ES_RH"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
38 * @var g_ESEasySetupResource
39 * @brief Structure for holding the Provisioning status
41 EasySetupResource g_ESEasySetupResource;
42 WiFiConfResource g_ESWiFiConfResource;
43 CoapCloudConfResource g_ESCoapCloudConfResource;
44 DevConfResource g_ESDevConfResource;
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 updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
55 void updateWiFiConfResource(OCRepPayload* input);
56 void updateCoapCloudConfResource(OCRepPayload* input);
57 void updateDevConfResource(OCRepPayload* input);
58 const char *getResult(OCStackResult result);
60 ESConnectRequestCB gConnectRequestEvtCb = NULL;
61 ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
62 ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
63 ESDevConfCB gDevConfRsrcEvtCb = NULL;
65 ESReadUserdataCb gReadUserdataCb = NULL;
66 ESWriteUserdataCb gWriteUserdataCb = NULL;
68 bool CompareResourceInterface(char *from, char *iface)
70 char *str = OICStrdup(from);
71 char *ptr = strtok(str, ";");
81 if(strstr(ptr, ".if."))
84 if_ptr = strtok(ptr, "=");
85 if_ptr = strtok(NULL, "=");
87 if(!strcmp(if_ptr, iface))
94 } while ((ptr = strtok(NULL, ";")));
100 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
102 if(!readCb && !writeCb)
104 OIC_LOG(DEBUG, ES_RH_TAG, "Both of callbacks for user data are null");
107 gReadUserdataCb = readCb;
108 gWriteUserdataCb = writeCb;
112 void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
114 gWifiConfRsrcEvtCb = cb;
117 void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
119 gCoapCloudConfRsrcEvtCb = cb;
122 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
124 gDevConfRsrcEvtCb = cb;
127 void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
129 gConnectRequestEvtCb = cb;
132 void UnRegisterResourceEventCallBack()
134 if (gWifiConfRsrcEvtCb)
136 gWifiConfRsrcEvtCb = NULL;
138 if (gCoapCloudConfRsrcEvtCb)
140 gCoapCloudConfRsrcEvtCb = NULL;
142 if (gDevConfRsrcEvtCb)
144 gDevConfRsrcEvtCb = NULL;
146 if (gConnectRequestEvtCb)
148 gConnectRequestEvtCb = NULL;
152 OCStackResult initEasySetupResource(bool isSecured)
154 g_ESEasySetupResource.status = ES_STATE_INIT;
155 g_ESEasySetupResource.lastErrCode = ES_ERRCODE_NO_ERROR;
156 for( int i = 0 ; i < NUM_CONNECT_TYPE ; ++i )
158 g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
160 g_ESEasySetupResource.numRequest = 0;
162 OCStackResult res = OC_STACK_ERROR;
165 res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
166 OC_RSRVD_INTERFACE_DEFAULT,
167 OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
168 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
171 res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
172 OC_RSRVD_INTERFACE_DEFAULT,
173 OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
174 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
176 if(res != OC_STACK_OK)
178 OIC_LOG_V(ERROR, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
182 res = OCBindResourceTypeToResource(g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_COL);
183 if(res != OC_STACK_OK)
185 OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource type with result: %s", getResult(res));
189 res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_LL);
190 if(res != OC_STACK_OK)
192 OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
195 res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_BATCH);
196 if(res != OC_STACK_OK)
198 OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
202 OIC_LOG_V(DEBUG, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
206 OCStackResult initWiFiConfResource(bool isSecured)
208 OCStackResult res = OC_STACK_ERROR;
210 g_ESWiFiConfResource.supportedFreq = WIFI_BOTH;
211 g_ESWiFiConfResource.supportedMode[0] = WIFI_11A;
212 g_ESWiFiConfResource.supportedMode[1] = WIFI_11B;
213 g_ESWiFiConfResource.supportedMode[2] = WIFI_11G;
214 g_ESWiFiConfResource.supportedMode[3] = WIFI_11N;
215 g_ESWiFiConfResource.numMode = 4;
216 g_ESWiFiConfResource.authType = NONE_AUTH;
217 g_ESWiFiConfResource.encType = NONE_ENC;
218 OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), "");
219 OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), "");
223 res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
224 OC_RSRVD_INTERFACE_DEFAULT,
225 OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
226 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
229 res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
230 OC_RSRVD_INTERFACE_DEFAULT,
231 OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
232 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
235 OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
240 OCStackResult initCoapCloudConfResource(bool isSecured)
242 OCStackResult res = OC_STACK_ERROR;
244 OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
245 OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), "");
246 g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
247 OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), "");
248 OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
252 res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
253 OC_RSRVD_INTERFACE_DEFAULT,
254 OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
255 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
258 res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
259 OC_RSRVD_INTERFACE_DEFAULT,
260 OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
261 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
264 OIC_LOG_V(DEBUG, ES_RH_TAG, "Created CoapCloudConf resource with result: %s", getResult(res));
269 OCStackResult initDevConfResource(bool isSecured)
271 OCStackResult res = OC_STACK_ERROR;
273 OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
277 res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
278 OC_RSRVD_INTERFACE_DEFAULT,
279 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
280 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
283 res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
284 OC_RSRVD_INTERFACE_DEFAULT,
285 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
286 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
289 OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
294 void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
296 OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
298 int64_t *connect_req = NULL;
299 size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
300 if (OCRepPayloadGetIntArray(input, OC_RSRVD_ES_CONNECT, &connect_req, dimensions))
302 ESConnectRequest* connectRequest = (ESConnectRequest*)OICMalloc(sizeof(ESConnectRequest));
303 if( !connectRequest )
305 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
310 for (size_t i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
312 g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
313 connectRequest->connect[i] = ES_CONNECT_NONE;
315 if(i < dimensions[0] &&
316 (connect_req[i] == ES_CONNECT_WIFI || connect_req[i] == ES_CONNECT_COAPCLOUD))
318 g_ESEasySetupResource.connectRequest[cntRequest] = connect_req[i];
319 connectRequest->connect[cntRequest] = connect_req[i];
320 OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.connectType[%d] : %d",
321 cntRequest, g_ESEasySetupResource.connectRequest[cntRequest]);
325 connectRequest->numRequest = cntRequest;
326 g_ESEasySetupResource.numRequest = cntRequest;
328 if(g_ESEasySetupResource.connectRequest[0] != ES_CONNECT_NONE)
330 OIC_LOG(DEBUG, ES_RH_TAG, "Send ConnectRequest Callback To ES");
332 // TODO : Need to check appropriateness of gWiFiData
333 if(gConnectRequestEvtCb != NULL)
335 gConnectRequestEvtCb(ES_OK, connectRequest);
339 OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
346 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
348 // When Provisioning resource has a POST with BatchInterface
349 updateCoapCloudConfResource(input);
350 updateWiFiConfResource(input);
351 updateDevConfResource(input);
356 void updateWiFiConfResource(OCRepPayload* input)
358 ESWiFiConfData* wiFiData = (ESWiFiConfData*)OICMalloc(sizeof(ESWiFiConfData));
362 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
366 memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
367 memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
368 wiFiData->authtype = NONE_AUTH;
369 wiFiData->enctype = NONE_AUTH;
370 wiFiData->userdata = NULL;
373 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
375 OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
376 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
377 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s", g_ESWiFiConfResource.ssid);
381 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
383 OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
384 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
385 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s", g_ESWiFiConfResource.cred);
388 int64_t authType = -1;
389 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
391 g_ESWiFiConfResource.authType = authType;
392 wiFiData->authtype = g_ESWiFiConfResource.authType;
393 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u", g_ESWiFiConfResource.authType);
396 int64_t encType = -1;
397 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
399 g_ESWiFiConfResource.encType = encType;
400 wiFiData->enctype = g_ESWiFiConfResource.encType;
401 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u", g_ESWiFiConfResource.encType);
406 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
409 if(ssid || cred || authType!= -1 || encType != -1)
411 OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
413 // TODO : Need to check appropriateness of gWiFiData
414 if(gWifiConfRsrcEvtCb != NULL)
416 gWifiConfRsrcEvtCb(ES_OK, wiFiData);
420 OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
424 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
426 OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
434 void updateCoapCloudConfResource(OCRepPayload* input)
436 ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
438 if(cloudData == NULL)
440 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
444 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
445 memset(cloudData->accessToken, 0, OIC_STRING_ACCESS_TOKEN_MAX_VALUE);
446 g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
447 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
448 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
449 cloudData->userdata = NULL;
451 char *authCode = NULL;
452 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
454 OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
455 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
456 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
459 char *accessToken = NULL;
460 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
462 OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
463 OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
464 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
467 int64_t accessTokenType = -1;
468 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
470 g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
471 cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
472 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
475 char *authProvider = NULL;
476 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
478 OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
479 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
480 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
483 char *ciServer = NULL;
484 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
486 OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
487 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
488 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
493 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
496 if(authCode || accessToken || authProvider || ciServer)
498 OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
500 // TODO : Need to check appropriateness of gCloudData
501 if(gCoapCloudConfRsrcEvtCb != NULL)
503 gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
507 OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
511 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
513 OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
517 OICFree(accessToken);
518 OICFree(authProvider);
523 void updateDevConfResource(OCRepPayload* input)
525 ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
527 if(devConfData == NULL)
529 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
532 devConfData->userdata = NULL;
536 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
539 if( devConfData->userdata != NULL )
541 OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
543 // TODO : Need to check appropriateness of gDevConfData
544 if(gDevConfRsrcEvtCb != NULL)
546 gDevConfRsrcEvtCb(ES_OK, devConfData);
550 OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
554 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
556 OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
559 OICFree(devConfData);
562 OCRepPayload* constructResponseOfWiFiConf(char *interface)
564 OCRepPayload* payload = OCRepPayloadCreate();
567 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
571 if(g_ESWiFiConfResource.handle == NULL)
573 OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
577 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
578 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
580 OCRepPayload* repPayload = NULL;
581 OCRepPayload* tempPayload = NULL;
582 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
584 repPayload = OCRepPayloadCreate();
587 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
591 tempPayload = payload;
592 payload = repPayload;
594 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
595 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
598 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
602 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
604 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
606 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
607 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
610 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
614 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
616 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
620 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
621 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
624 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESWiFiConfResource.numMode, 0, 0};
625 int64_t *modes_64 = (int64_t *)OICMalloc(g_ESWiFiConfResource.numMode * sizeof(int64_t));
628 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
632 for(int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
634 modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
636 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
638 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, g_ESWiFiConfResource.supportedFreq);
639 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
640 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
641 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) g_ESWiFiConfResource.authType);
642 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) g_ESWiFiConfResource.encType);
646 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
649 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
651 payload = tempPayload;
652 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
658 OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
660 OCRepPayload* payload = OCRepPayloadCreate();
663 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
667 if(g_ESCoapCloudConfResource.handle == NULL)
669 OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
673 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
674 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
676 OCRepPayload* repPayload = NULL;
677 OCRepPayload* tempPayload = NULL;
678 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
680 repPayload = OCRepPayloadCreate();
683 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
687 tempPayload = payload;
688 payload = repPayload;
690 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
691 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
694 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
698 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
700 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
702 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
703 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
706 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
710 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
712 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
716 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
717 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
720 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, g_ESCoapCloudConfResource.authCode);
721 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ACCESSTOKEN, g_ESCoapCloudConfResource.accessToken);
722 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ACCESSTOKEN_TYPE, (int)g_ESCoapCloudConfResource.accessTokenType);
723 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, g_ESCoapCloudConfResource.authProvider);
724 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, g_ESCoapCloudConfResource.ciServer);
728 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
731 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
733 payload = tempPayload;
734 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
740 OCRepPayload* constructResponseOfDevConf(char *interface)
742 OCRepPayload* payload = OCRepPayloadCreate();
745 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
749 if(g_ESDevConfResource.handle == NULL)
751 OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
755 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
756 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
758 OCRepPayload* repPayload = NULL;
759 OCRepPayload* tempPayload = NULL;
760 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
762 repPayload = OCRepPayloadCreate();
765 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
769 tempPayload = payload;
770 payload = repPayload;
772 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
773 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
776 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
780 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
782 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
784 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
785 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
788 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
792 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
794 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
798 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
799 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
802 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, g_ESDevConfResource.devName);
806 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
809 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
811 payload = tempPayload;
812 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
818 OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
820 OCRepPayload* payload = OCRepPayloadCreate();
823 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
827 // Requested interface is Link list interface
828 //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
829 if (!ehRequest->query || !strcmp(ehRequest->query, "") ||
830 CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) ||
831 CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
833 const OCRepPayload *arrayPayload[3] = {NULL};
837 if(g_ESWiFiConfResource.handle != NULL)
839 OCRepPayload *add = OCRepPayloadCreate();
842 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
846 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
847 char **resourceType = NULL;
848 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
849 char **resourceInterface = NULL;
850 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
852 if(!resourceType || !resourceInterface)
854 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
858 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
859 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
861 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
862 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
863 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
864 (const char **)resourceType, dimensions);
865 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
866 (const char **)resourceInterface, dimensions);
868 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
869 OCRepPayload *policy = OCRepPayloadCreate();
872 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
876 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
877 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
880 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
881 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
882 ehRequest->devAddr.flags);
883 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
886 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
888 arrayPayload[childResCnt++] = add;
891 if(g_ESDevConfResource.handle != NULL)
893 OCRepPayload *add = OCRepPayloadCreate();
896 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
900 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
901 char **resourceType = NULL;
902 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
903 char **resourceInterface = NULL;
904 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
906 if(!resourceType || !resourceInterface)
908 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
912 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
913 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
915 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
916 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
917 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
918 (const char **)resourceType, dimensions);
919 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
920 (const char **)resourceInterface, dimensions);
922 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle);
923 OCRepPayload *policy = OCRepPayloadCreate();
926 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
930 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
931 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
934 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
935 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
936 ehRequest->devAddr.flags);
937 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
940 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
942 arrayPayload[childResCnt++] = add;
945 if(g_ESCoapCloudConfResource.handle != NULL)
947 OCRepPayload *add = OCRepPayloadCreate();
950 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
954 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
955 char **resourceType = NULL;
956 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
957 char **resourceInterface = NULL;
958 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
960 if(!resourceType || !resourceInterface)
962 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
966 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
967 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
969 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
970 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_COAPCLOUDCONF);
971 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
972 (const char **)resourceType, dimensions);
973 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
974 (const char **)resourceInterface, dimensions);
976 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESCoapCloudConfResource.handle);
977 OCRepPayload *policy = OCRepPayloadCreate();
980 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
984 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
985 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
988 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
989 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
990 ehRequest->devAddr.flags);
991 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
994 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
996 arrayPayload[childResCnt++] = add;
999 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
1001 if(!ehRequest->query || !strcmp(ehRequest->query, "") ||
1002 CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1004 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1005 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1006 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
1007 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
1008 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
1009 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1010 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
1012 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1013 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1015 if(g_ESEasySetupResource.numRequest > 0)
1017 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1018 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t));
1021 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1025 for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1027 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1029 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1033 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1034 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1035 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1038 if(gWriteUserdataCb)
1040 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1043 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1045 else // link list interface
1047 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1050 ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1053 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1054 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1056 OCRepPayload* repPayload = NULL;
1058 repPayload = OCRepPayloadCreate();
1061 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1065 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {3, 0, 0};
1066 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
1069 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1073 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
1074 interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_LL);
1075 interfaces[2] = OICStrdup(OC_RSRVD_INTERFACE_BATCH);
1077 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
1079 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
1080 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
1083 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1087 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1088 resourceTypes[1] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COL);
1090 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
1092 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1093 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1094 if(g_ESEasySetupResource.numRequest > 0)
1096 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1097 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t));
1100 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1104 for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1106 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1108 OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1112 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1113 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1114 OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1117 if(gWriteUserdataCb)
1119 gWriteUserdataCb(repPayload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1122 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
1125 if(ehRequest->query)
1127 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1128 {// When Provisioning resource has a GET with BatchInterface
1129 OCRepPayload* head = payload;
1130 OCRepPayload* nextPayload = NULL;
1132 nextPayload = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_BATCH);
1133 if(nextPayload != NULL)
1135 payload->next = nextPayload;
1136 payload = payload->next;
1139 nextPayload = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_BATCH);
1140 if(nextPayload != NULL)
1142 payload->next = nextPayload;
1143 payload = payload->next;
1146 nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
1147 if(nextPayload != NULL)
1149 payload->next = nextPayload;
1160 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
1162 OCStackResult res = OC_STACK_ERROR;
1163 bool maskFlag = false;
1165 res = initEasySetupResource(isSecured);
1166 if(res != OC_STACK_OK)
1168 // TODO: destroy logic will be added
1169 OIC_LOG_V(ERROR, ES_RH_TAG, "initEasySetupResource result: %s", getResult(res));
1174 if((resourceMask & ES_WIFICONF_RESOURCE) == ES_WIFICONF_RESOURCE)
1177 res = initWiFiConfResource(isSecured);
1178 if(res != OC_STACK_OK)
1180 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiConfResource result: %s", getResult(res));
1184 res = OCBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1185 if(res != OC_STACK_OK)
1187 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiConfResource result: %s", getResult(res));
1193 if((resourceMask & ES_COAPCLOUDCONF_RESOURCE) == ES_COAPCLOUDCONF_RESOURCE)
1196 res = initCoapCloudConfResource(isSecured);
1197 if(res != OC_STACK_OK)
1199 OIC_LOG_V(ERROR, ES_RH_TAG, "initCoapCloudConfResource result: %s", getResult(res));
1203 res = OCBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1204 if(res != OC_STACK_OK)
1206 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CoapCloudConfResource result: %s", getResult(res));
1211 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
1214 res = initDevConfResource(isSecured);
1215 if(res != OC_STACK_OK)
1217 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
1221 res = OCBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1222 if(res != OC_STACK_OK)
1224 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
1229 if(maskFlag == false)
1231 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
1232 return OC_STACK_ERROR;
1236 OIC_LOG_V(DEBUG, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
1241 OCStackResult DeleteEasySetupResources()
1243 OCStackResult res = OC_STACK_ERROR;
1244 if (g_ESWiFiConfResource.handle != NULL)
1246 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1247 if(res != OC_STACK_OK)
1249 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
1252 if (g_ESCoapCloudConfResource.handle != NULL)
1254 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1255 if(res != OC_STACK_OK)
1257 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
1260 if (g_ESDevConfResource.handle != NULL)
1262 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1263 if(res != OC_STACK_OK)
1265 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
1269 if (g_ESWiFiConfResource.handle != NULL)
1271 res = OCDeleteResource(g_ESWiFiConfResource.handle);
1272 if (res != OC_STACK_OK)
1274 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
1278 if(g_ESCoapCloudConfResource.handle != NULL)
1280 res = OCDeleteResource(g_ESCoapCloudConfResource.handle);
1281 if (res != OC_STACK_OK)
1283 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
1287 if(g_ESDevConfResource.handle != NULL)
1289 res = OCDeleteResource(g_ESDevConfResource.handle);
1290 if (res != OC_STACK_OK)
1292 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
1296 if(g_ESEasySetupResource.handle != NULL)
1298 res = OCDeleteResource(g_ESEasySetupResource.handle);
1299 if (res != OC_STACK_OK)
1301 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1308 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1310 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1313 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1316 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1318 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1322 OCRepPayload *getResp = NULL;
1325 if(ehRequest->resource == g_ESEasySetupResource.handle)
1327 if(ehRequest->query &&
1328 strcmp(ehRequest->query, "") &&
1329 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1330 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1331 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1333 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1334 return OC_EH_BAD_REQ;
1338 getResp = constructResponseOfEasySetup(ehRequest);
1341 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1343 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1345 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1346 return OC_EH_BAD_REQ;
1350 getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1353 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1355 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1357 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1358 return OC_EH_BAD_REQ;
1362 getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1365 else if(ehRequest->resource == g_ESDevConfResource.handle)
1367 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1369 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1370 return OC_EH_BAD_REQ;
1374 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1380 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1385 ehResult = OC_EH_OK;
1390 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1392 OIC_LOG(DEBUG, ES_RH_TAG, "ProcessPostRequest enter");
1393 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1394 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1396 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1400 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1403 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1407 if(ehRequest->resource == g_ESEasySetupResource.handle)
1409 if(ehRequest->query &&
1410 strcmp(ehRequest->query, "") &&
1411 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1412 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1414 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1415 return OC_EH_BAD_REQ;
1419 updateEasySetupResource(ehRequest, input);
1422 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1424 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1426 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1427 return OC_EH_BAD_REQ;
1431 updateWiFiConfResource(input);
1434 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1436 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1438 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1439 return OC_EH_BAD_REQ;
1443 updateCoapCloudConfResource(input);
1446 else if(ehRequest->resource == g_ESDevConfResource.handle)
1448 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1450 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1451 return OC_EH_BAD_REQ;
1455 updateDevConfResource(input);
1459 OCRepPayload *getResp = NULL;
1460 if(ehRequest->resource == g_ESEasySetupResource.handle)
1462 getResp = constructResponseOfEasySetup(ehRequest);
1464 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1466 getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1468 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1470 getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1472 else if(ehRequest->resource == g_ESDevConfResource.handle)
1474 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1479 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1484 ehResult = OC_EH_OK;
1489 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1490 OCRepPayload** payload)
1494 OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1499 * This is the entity handler for the registered resource.
1500 * This is invoked by OCStack whenever it recevies a request for this resource.
1502 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1503 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1506 OCEntityHandlerResult ehRet = OC_EH_OK;
1507 OCEntityHandlerResponse response =
1508 { 0, 0, OC_EH_ERROR, 0, 0,
1511 OCRepPayload* payload = NULL;
1513 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1515 if (OC_REST_GET == entityHandlerRequest->method)
1517 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1518 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1520 else if (OC_REST_PUT == entityHandlerRequest->method)
1522 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1524 //PUT request will be handled in the internal implementation
1525 if (g_ESEasySetupResource.handle != NULL)
1527 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1531 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1532 ehRet = OC_EH_ERROR;
1535 else if (OC_REST_POST == entityHandlerRequest->method)
1537 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1538 if (g_ESEasySetupResource.handle != NULL)
1540 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1544 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1545 ehRet = OC_EH_ERROR;
1549 // Format the response. Note this requires some info about the request
1550 response.requestHandle = entityHandlerRequest->requestHandle;
1551 response.resourceHandle = entityHandlerRequest->resource;
1552 response.ehResult = ehRet;
1553 //response uses OCPaylod while all get,put methodes use OCRepPayload
1554 response.payload = (OCPayload*) (payload);
1555 response.numSendVendorSpecificHeaderOptions = 0;
1556 memset(response.sendVendorSpecificHeaderOptions, 0,
1557 sizeof(response.sendVendorSpecificHeaderOptions));
1558 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1559 // Indicate that response is NOT in a persistent buffer
1560 response.persistentBufferFlag = 0;
1562 // Send the response
1563 OIC_LOG(INFO, ES_RH_TAG, "Invoking OCDoResponse");
1564 if (OCDoResponse(&response) != OC_STACK_OK)
1566 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1567 ehRet = OC_EH_ERROR;
1570 if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
1572 OIC_LOG(DEBUG, ES_RH_TAG, "Flag includes OC_OBSERVE_FLAG");
1574 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
1576 OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_REGISTER from Mediator");
1578 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
1580 OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_DEREGISTER from Mediator");
1584 OIC_LOG(INFO, ES_RH_TAG, "ES OCEntityHandlerCb OUT");
1588 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1590 OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty IN");
1592 g_ESWiFiConfResource.supportedFreq = (deviceProperty->WiFi).freq;
1593 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Freq : %d", g_ESWiFiConfResource.supportedFreq);
1596 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1598 g_ESWiFiConfResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1599 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Mode : %d", g_ESWiFiConfResource.supportedMode[modeIdx]);
1602 g_ESWiFiConfResource.numMode = modeIdx;
1604 OICStrcpy(g_ESDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1605 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Device Name : %s", g_ESDevConfResource.devName);
1607 if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
1609 OIC_LOG(DEBUG, ES_RH_TAG, "wifiResource doesn't have any observers.");
1612 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
1614 OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observers.");
1617 OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty OUT");
1621 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1623 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState IN");
1625 g_ESEasySetupResource.status = esState;
1626 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee Status : %d", g_ESEasySetupResource.status);
1628 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1630 OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1633 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState OUT");
1637 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1639 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode IN");
1641 g_ESEasySetupResource.lastErrCode = esErrCode;
1642 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee ErrorCode : %d", g_ESEasySetupResource.lastErrCode);
1644 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1646 OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1649 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1653 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1655 if( !ehRequest->query || (strcmp(ehRequest->query, "") &&
1656 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
1658 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1659 return OC_EH_BAD_REQ;
1664 const char *getResult(OCStackResult result)
1669 return "OC_STACK_OK";
1670 case OC_STACK_INVALID_URI:
1671 return "OC_STACK_INVALID_URI";
1672 case OC_STACK_INVALID_QUERY:
1673 return "OC_STACK_INVALID_QUERY";
1674 case OC_STACK_INVALID_IP:
1675 return "OC_STACK_INVALID_IP";
1676 case OC_STACK_INVALID_PORT:
1677 return "OC_STACK_INVALID_PORT";
1678 case OC_STACK_INVALID_CALLBACK:
1679 return "OC_STACK_INVALID_CALLBACK";
1680 case OC_STACK_INVALID_METHOD:
1681 return "OC_STACK_INVALID_METHOD";
1682 case OC_STACK_NO_MEMORY:
1683 return "OC_STACK_NO_MEMORY";
1684 case OC_STACK_COMM_ERROR:
1685 return "OC_STACK_COMM_ERROR";
1686 case OC_STACK_INVALID_PARAM:
1687 return "OC_STACK_INVALID_PARAM";
1688 case OC_STACK_NOTIMPL:
1689 return "OC_STACK_NOTIMPL";
1690 case OC_STACK_NO_RESOURCE:
1691 return "OC_STACK_NO_RESOURCE";
1692 case OC_STACK_RESOURCE_ERROR:
1693 return "OC_STACK_RESOURCE_ERROR";
1694 case OC_STACK_SLOW_RESOURCE:
1695 return "OC_STACK_SLOW_RESOURCE";
1696 case OC_STACK_NO_OBSERVERS:
1697 return "OC_STACK_NO_OBSERVERS";
1698 case OC_STACK_ERROR:
1699 return "OC_STACK_ERROR";