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.");
432 void updateCoapCloudConfResource(OCRepPayload* input)
434 ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
436 if(cloudData == NULL)
438 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
442 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
443 memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
444 g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
445 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
446 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
447 cloudData->userdata = NULL;
449 char *authCode = NULL;
450 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
452 OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
453 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
454 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
457 char *accessToken = NULL;
458 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
460 OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
461 OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
462 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
465 int64_t accessTokenType = -1;
466 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
468 g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
469 cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
470 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
473 char *authProvider = NULL;
474 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
476 OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
477 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
478 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
481 char *ciServer = NULL;
482 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
484 OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
485 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
486 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
491 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
494 if(authCode || accessToken || authProvider || ciServer)
496 OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
498 // TODO : Need to check appropriateness of gCloudData
499 if(gCoapCloudConfRsrcEvtCb != NULL)
501 gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
505 OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
509 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
511 OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
517 void updateDevConfResource(OCRepPayload* input)
519 ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
521 if(devConfData == NULL)
523 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
526 devConfData->userdata = NULL;
530 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
533 if( devConfData->userdata != NULL )
535 OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
537 // TODO : Need to check appropriateness of gDevConfData
538 if(gDevConfRsrcEvtCb != NULL)
540 gDevConfRsrcEvtCb(ES_OK, devConfData);
544 OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
548 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
550 OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
553 OICFree(devConfData);
556 OCRepPayload* constructResponseOfWiFiConf(char *interface)
558 OCRepPayload* payload = OCRepPayloadCreate();
561 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
565 if(g_ESWiFiConfResource.handle == NULL)
567 OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
571 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
572 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
574 OCRepPayload* repPayload = NULL;
575 OCRepPayload* tempPayload = NULL;
576 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
578 repPayload = OCRepPayloadCreate();
581 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
585 tempPayload = payload;
586 payload = repPayload;
588 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
589 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
592 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
596 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
598 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
600 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
601 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
604 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
608 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
610 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
614 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
615 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
618 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESWiFiConfResource.numMode, 0, 0};
619 int64_t *modes_64 = (int64_t *)OICMalloc(g_ESWiFiConfResource.numMode * sizeof(int64_t));
622 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
626 for(int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
628 modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
630 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
632 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, g_ESWiFiConfResource.supportedFreq);
633 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
634 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
635 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) g_ESWiFiConfResource.authType);
636 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) g_ESWiFiConfResource.encType);
640 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
643 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
645 payload = tempPayload;
646 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
652 OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
654 OCRepPayload* payload = OCRepPayloadCreate();
657 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
661 if(g_ESCoapCloudConfResource.handle == NULL)
663 OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
667 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
668 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
670 OCRepPayload* repPayload = NULL;
671 OCRepPayload* tempPayload = NULL;
672 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
674 repPayload = OCRepPayloadCreate();
677 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
681 tempPayload = payload;
682 payload = repPayload;
684 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
685 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
688 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
692 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
694 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
696 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
697 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
700 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
704 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
706 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
710 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
711 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
714 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, g_ESCoapCloudConfResource.authCode);
715 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ACCESSTOKEN, g_ESCoapCloudConfResource.accessToken);
716 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ACCESSTOKEN_TYPE, (int)g_ESCoapCloudConfResource.accessTokenType);
717 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, g_ESCoapCloudConfResource.authProvider);
718 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, g_ESCoapCloudConfResource.ciServer);
722 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
725 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
727 payload = tempPayload;
728 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
734 OCRepPayload* constructResponseOfDevConf(char *interface)
736 OCRepPayload* payload = OCRepPayloadCreate();
739 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
743 if(g_ESDevConfResource.handle == NULL)
745 OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
749 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
750 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
752 OCRepPayload* repPayload = NULL;
753 OCRepPayload* tempPayload = NULL;
754 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
756 repPayload = OCRepPayloadCreate();
759 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
763 tempPayload = payload;
764 payload = repPayload;
766 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
767 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
770 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
774 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
776 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
778 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
779 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
782 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
786 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
788 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
792 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
793 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
796 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, g_ESDevConfResource.devName);
800 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
803 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
805 payload = tempPayload;
806 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
812 OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
814 OCRepPayload* payload = OCRepPayloadCreate();
817 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
821 // Requested interface is Link list interface
822 //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
823 if(!ehRequest->query ||
824 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
825 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
826 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
828 const OCRepPayload *arrayPayload[3] = {NULL};
832 if(g_ESWiFiConfResource.handle != NULL)
834 OCRepPayload *add = OCRepPayloadCreate();
837 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
841 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
842 char **resourceType = NULL;
843 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
844 char **resourceInterface = NULL;
845 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
847 if(!resourceType || !resourceInterface)
849 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
853 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
854 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
856 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
857 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
858 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
859 (const char **)resourceType, dimensions);
860 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
861 (const char **)resourceInterface, dimensions);
863 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
864 OCRepPayload *policy = OCRepPayloadCreate();
867 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
871 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
872 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
875 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
876 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
877 ehRequest->devAddr.flags);
878 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
881 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
883 arrayPayload[childResCnt++] = add;
886 if(g_ESDevConfResource.handle != NULL)
888 OCRepPayload *add = OCRepPayloadCreate();
891 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
895 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
896 char **resourceType = NULL;
897 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
898 char **resourceInterface = NULL;
899 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
901 if(!resourceType || !resourceInterface)
903 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
907 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
908 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
910 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
911 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
912 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
913 (const char **)resourceType, dimensions);
914 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
915 (const char **)resourceInterface, dimensions);
917 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle);
918 OCRepPayload *policy = OCRepPayloadCreate();
921 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
925 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
926 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
929 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
930 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
931 ehRequest->devAddr.flags);
932 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
935 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
937 arrayPayload[childResCnt++] = add;
940 if(g_ESCoapCloudConfResource.handle != NULL)
942 OCRepPayload *add = OCRepPayloadCreate();
945 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
949 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
950 char **resourceType = NULL;
951 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
952 char **resourceInterface = NULL;
953 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
955 if(!resourceType || !resourceInterface)
957 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
961 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
962 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
964 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
965 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_COAPCLOUDCONF);
966 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
967 (const char **)resourceType, dimensions);
968 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
969 (const char **)resourceInterface, dimensions);
971 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESCoapCloudConfResource.handle);
972 OCRepPayload *policy = OCRepPayloadCreate();
975 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
979 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
980 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
983 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
984 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
985 ehRequest->devAddr.flags);
986 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
989 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
991 arrayPayload[childResCnt++] = add;
994 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
996 if(!ehRequest->query ||
997 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
998 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
1000 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1001 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1002 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
1003 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
1004 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
1005 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1006 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
1008 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1009 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1011 if(g_ESEasySetupResource.numRequest > 0)
1013 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1014 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t));
1017 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1021 for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1023 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1025 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1029 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1030 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1031 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1034 if(gWriteUserdataCb)
1036 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1039 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1041 else // link list interface
1043 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1046 ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1049 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1050 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1052 OCRepPayload* repPayload = NULL;
1054 repPayload = OCRepPayloadCreate();
1057 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1061 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {3, 0, 0};
1062 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
1065 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1069 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
1070 interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_LL);
1071 interfaces[2] = OICStrdup(OC_RSRVD_INTERFACE_BATCH);
1073 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
1075 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
1076 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
1079 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1083 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1084 resourceTypes[1] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COL);
1086 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
1088 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1089 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1090 if(g_ESEasySetupResource.numRequest > 0)
1092 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1093 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t));
1096 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1100 for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1102 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1104 OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1108 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1109 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1110 OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1113 if(gWriteUserdataCb)
1115 gWriteUserdataCb(repPayload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1118 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
1121 if(ehRequest->query)
1123 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1124 {// When Provisioning resource has a GET with BatchInterface
1125 OCRepPayload* head = payload;
1126 OCRepPayload* nextPayload = NULL;
1128 nextPayload = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_BATCH);
1129 if(nextPayload != NULL)
1131 payload->next = nextPayload;
1132 payload = payload->next;
1135 nextPayload = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_BATCH);
1136 if(nextPayload != NULL)
1138 payload->next = nextPayload;
1139 payload = payload->next;
1142 nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
1143 if(nextPayload != NULL)
1145 payload->next = nextPayload;
1156 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
1158 OCStackResult res = OC_STACK_ERROR;
1159 bool maskFlag = false;
1161 res = initEasySetupResource(isSecured);
1162 if(res != OC_STACK_OK)
1164 // TODO: destroy logic will be added
1165 OIC_LOG_V(ERROR, ES_RH_TAG, "initEasySetupResource result: %s", getResult(res));
1170 if((resourceMask & ES_WIFICONF_RESOURCE) == ES_WIFICONF_RESOURCE)
1173 res = initWiFiConfResource(isSecured);
1174 if(res != OC_STACK_OK)
1176 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiConfResource result: %s", getResult(res));
1180 res = OCBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1181 if(res != OC_STACK_OK)
1183 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiConfResource result: %s", getResult(res));
1189 if((resourceMask & ES_COAPCLOUDCONF_RESOURCE) == ES_COAPCLOUDCONF_RESOURCE)
1192 res = initCoapCloudConfResource(isSecured);
1193 if(res != OC_STACK_OK)
1195 OIC_LOG_V(ERROR, ES_RH_TAG, "initCoapCloudConfResource result: %s", getResult(res));
1199 res = OCBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1200 if(res != OC_STACK_OK)
1202 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CoapCloudConfResource result: %s", getResult(res));
1207 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
1210 res = initDevConfResource(isSecured);
1211 if(res != OC_STACK_OK)
1213 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
1217 res = OCBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1218 if(res != OC_STACK_OK)
1220 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
1225 if(maskFlag == false)
1227 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
1228 return OC_STACK_ERROR;
1232 OIC_LOG_V(DEBUG, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
1237 OCStackResult DeleteEasySetupResources()
1239 OCStackResult res = OC_STACK_ERROR;
1240 if (g_ESWiFiConfResource.handle != NULL)
1242 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1243 if(res != OC_STACK_OK)
1245 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
1248 if (g_ESCoapCloudConfResource.handle != NULL)
1250 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1251 if(res != OC_STACK_OK)
1253 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
1256 if (g_ESDevConfResource.handle != NULL)
1258 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1259 if(res != OC_STACK_OK)
1261 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
1265 if (g_ESWiFiConfResource.handle != NULL)
1267 res = OCDeleteResource(g_ESWiFiConfResource.handle);
1268 if (res != OC_STACK_OK)
1270 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
1274 if(g_ESCoapCloudConfResource.handle != NULL)
1276 res = OCDeleteResource(g_ESCoapCloudConfResource.handle);
1277 if (res != OC_STACK_OK)
1279 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
1283 if(g_ESDevConfResource.handle != NULL)
1285 res = OCDeleteResource(g_ESDevConfResource.handle);
1286 if (res != OC_STACK_OK)
1288 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
1292 if(g_ESEasySetupResource.handle != NULL)
1294 res = OCDeleteResource(g_ESEasySetupResource.handle);
1295 if (res != OC_STACK_OK)
1297 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1304 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1306 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1309 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1312 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1314 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1318 OCRepPayload *getResp = NULL;
1321 if(ehRequest->resource == g_ESEasySetupResource.handle)
1323 if(ehRequest->query &&
1324 strcmp(ehRequest->query, "") &&
1325 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1326 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1327 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1329 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1330 return OC_EH_BAD_REQ;
1334 getResp = constructResponseOfEasySetup(ehRequest);
1337 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1339 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1341 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1342 return OC_EH_BAD_REQ;
1346 getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1349 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1351 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1353 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1354 return OC_EH_BAD_REQ;
1358 getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1361 else if(ehRequest->resource == g_ESDevConfResource.handle)
1363 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1365 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1366 return OC_EH_BAD_REQ;
1370 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1376 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1381 ehResult = OC_EH_OK;
1386 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1388 OIC_LOG(DEBUG, ES_RH_TAG, "ProcessPostRequest enter");
1389 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1390 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1392 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1396 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1399 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1403 if(ehRequest->resource == g_ESEasySetupResource.handle)
1405 if(ehRequest->query &&
1406 strcmp(ehRequest->query, "") &&
1407 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1408 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1410 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1411 return OC_EH_BAD_REQ;
1415 updateEasySetupResource(ehRequest, input);
1418 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1420 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1422 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1423 return OC_EH_BAD_REQ;
1427 updateWiFiConfResource(input);
1430 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1432 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1434 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1435 return OC_EH_BAD_REQ;
1439 updateCoapCloudConfResource(input);
1442 else if(ehRequest->resource == g_ESDevConfResource.handle)
1444 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1446 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1447 return OC_EH_BAD_REQ;
1451 updateDevConfResource(input);
1455 OCRepPayload *getResp = NULL;
1456 if(ehRequest->resource == g_ESEasySetupResource.handle)
1458 getResp = constructResponseOfEasySetup(ehRequest);
1460 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1462 getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1464 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1466 getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1468 else if(ehRequest->resource == g_ESDevConfResource.handle)
1470 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1475 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1480 ehResult = OC_EH_OK;
1485 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1486 OCRepPayload** payload)
1490 OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1495 * This is the entity handler for the registered resource.
1496 * This is invoked by OCStack whenever it recevies a request for this resource.
1498 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1499 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1502 OCEntityHandlerResult ehRet = OC_EH_OK;
1503 OCEntityHandlerResponse response =
1504 { 0, 0, OC_EH_ERROR, 0, 0,
1507 OCRepPayload* payload = NULL;
1509 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1511 if (OC_REST_GET == entityHandlerRequest->method)
1513 OIC_LOG(DEBUG, ES_RH_TAG, "Received GET request");
1514 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1516 else if (OC_REST_PUT == entityHandlerRequest->method)
1518 OIC_LOG(DEBUG, ES_RH_TAG, "Received PUT request");
1520 //PUT request will be handled in the internal implementation
1521 if (g_ESEasySetupResource.handle != NULL)
1523 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1527 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1528 ehRet = OC_EH_ERROR;
1531 else if (OC_REST_POST == entityHandlerRequest->method)
1533 OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_REST_POST from client");
1534 if (g_ESEasySetupResource.handle != NULL)
1536 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1540 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1541 ehRet = OC_EH_ERROR;
1545 // Format the response. Note this requires some info about the request
1546 response.requestHandle = entityHandlerRequest->requestHandle;
1547 response.resourceHandle = entityHandlerRequest->resource;
1548 response.ehResult = ehRet;
1549 //response uses OCPaylod while all get,put methodes use OCRepPayload
1550 response.payload = (OCPayload*) (payload);
1551 response.numSendVendorSpecificHeaderOptions = 0;
1552 memset(response.sendVendorSpecificHeaderOptions, 0,
1553 sizeof(response.sendVendorSpecificHeaderOptions));
1554 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1555 // Indicate that response is NOT in a persistent buffer
1556 response.persistentBufferFlag = 0;
1558 // Send the response
1559 if (OCDoResponse(&response) != OC_STACK_OK)
1561 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1562 ehRet = OC_EH_ERROR;
1565 if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
1567 OIC_LOG(DEBUG, ES_RH_TAG, "Flag includes OC_OBSERVE_FLAG");
1569 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
1571 OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_REGISTER from Mediator");
1573 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
1575 OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_DEREGISTER from Mediator");
1581 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1583 OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty IN");
1585 g_ESWiFiConfResource.supportedFreq = (deviceProperty->WiFi).freq;
1586 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Freq : %d", g_ESWiFiConfResource.supportedFreq);
1589 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1591 g_ESWiFiConfResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1592 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Mode : %d", g_ESWiFiConfResource.supportedMode[modeIdx]);
1595 g_ESWiFiConfResource.numMode = modeIdx;
1597 OICStrcpy(g_ESDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1598 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Device Name : %s", g_ESDevConfResource.devName);
1600 if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
1602 OIC_LOG(DEBUG, ES_RH_TAG, "wifiResource doesn't have any observers.");
1605 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
1607 OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observers.");
1610 OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty OUT");
1614 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1616 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState IN");
1618 g_ESEasySetupResource.status = esState;
1619 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee Status : %d", g_ESEasySetupResource.status);
1621 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1623 OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1626 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState OUT");
1630 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1632 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode IN");
1634 g_ESEasySetupResource.lastErrCode = esErrCode;
1635 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee ErrorCode : %d", g_ESEasySetupResource.lastErrCode);
1637 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1639 OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1642 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1646 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1648 if( !(ehRequest->query) ||
1649 (ehRequest->query &&
1650 (strcmp(ehRequest->query, "") && !CompareResourceInterface(ehRequest->query,
1651 OC_RSRVD_INTERFACE_DEFAULT))))
1653 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1654 return OC_EH_BAD_REQ;
1659 const char *getResult(OCStackResult result)
1664 return "OC_STACK_OK";
1665 case OC_STACK_INVALID_URI:
1666 return "OC_STACK_INVALID_URI";
1667 case OC_STACK_INVALID_QUERY:
1668 return "OC_STACK_INVALID_QUERY";
1669 case OC_STACK_INVALID_IP:
1670 return "OC_STACK_INVALID_IP";
1671 case OC_STACK_INVALID_PORT:
1672 return "OC_STACK_INVALID_PORT";
1673 case OC_STACK_INVALID_CALLBACK:
1674 return "OC_STACK_INVALID_CALLBACK";
1675 case OC_STACK_INVALID_METHOD:
1676 return "OC_STACK_INVALID_METHOD";
1677 case OC_STACK_NO_MEMORY:
1678 return "OC_STACK_NO_MEMORY";
1679 case OC_STACK_COMM_ERROR:
1680 return "OC_STACK_COMM_ERROR";
1681 case OC_STACK_INVALID_PARAM:
1682 return "OC_STACK_INVALID_PARAM";
1683 case OC_STACK_NOTIMPL:
1684 return "OC_STACK_NOTIMPL";
1685 case OC_STACK_NO_RESOURCE:
1686 return "OC_STACK_NO_RESOURCE";
1687 case OC_STACK_RESOURCE_ERROR:
1688 return "OC_STACK_RESOURCE_ERROR";
1689 case OC_STACK_SLOW_RESOURCE:
1690 return "OC_STACK_SLOW_RESOURCE";
1691 case OC_STACK_NO_OBSERVERS:
1692 return "OC_STACK_NO_OBSERVERS";
1693 case OC_STACK_ERROR:
1694 return "OC_STACK_ERROR";