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_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 ||
830 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
831 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
832 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
834 const OCRepPayload *arrayPayload[3] = {NULL};
838 if(g_ESWiFiConfResource.handle != NULL)
840 OCRepPayload *add = OCRepPayloadCreate();
843 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
847 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
848 char **resourceType = NULL;
849 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
850 char **resourceInterface = NULL;
851 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
853 if(!resourceType || !resourceInterface)
855 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
859 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
860 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
862 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
863 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
864 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
865 (const char **)resourceType, dimensions);
866 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
867 (const char **)resourceInterface, dimensions);
869 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
870 OCRepPayload *policy = OCRepPayloadCreate();
873 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
877 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
878 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
881 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
882 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
883 ehRequest->devAddr.flags);
884 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
887 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
889 arrayPayload[childResCnt++] = add;
892 if(g_ESDevConfResource.handle != NULL)
894 OCRepPayload *add = OCRepPayloadCreate();
897 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
901 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
902 char **resourceType = NULL;
903 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
904 char **resourceInterface = NULL;
905 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
907 if(!resourceType || !resourceInterface)
909 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
913 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
914 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
916 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
917 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
918 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
919 (const char **)resourceType, dimensions);
920 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
921 (const char **)resourceInterface, dimensions);
923 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle);
924 OCRepPayload *policy = OCRepPayloadCreate();
927 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
931 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
932 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
935 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
936 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
937 ehRequest->devAddr.flags);
938 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
941 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
943 arrayPayload[childResCnt++] = add;
946 if(g_ESCoapCloudConfResource.handle != NULL)
948 OCRepPayload *add = OCRepPayloadCreate();
951 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
955 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
956 char **resourceType = NULL;
957 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
958 char **resourceInterface = NULL;
959 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
961 if(!resourceType || !resourceInterface)
963 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
967 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
968 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
970 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
971 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_COAPCLOUDCONF);
972 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
973 (const char **)resourceType, dimensions);
974 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
975 (const char **)resourceInterface, dimensions);
977 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESCoapCloudConfResource.handle);
978 OCRepPayload *policy = OCRepPayloadCreate();
981 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
985 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
986 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
989 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
990 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
991 ehRequest->devAddr.flags);
992 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
995 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
997 arrayPayload[childResCnt++] = add;
1000 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
1002 if(!ehRequest->query ||
1003 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
1004 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
1006 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1007 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1008 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
1009 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
1010 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
1011 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1012 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
1014 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1015 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1017 if(g_ESEasySetupResource.numRequest > 0)
1019 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1020 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t));
1023 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1027 for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1029 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1031 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1035 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1036 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1037 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1040 if(gWriteUserdataCb)
1042 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1045 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1047 else // link list interface
1049 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
1052 ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1055 OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse EasySetup res");
1056 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_EASYSETUP);
1058 OCRepPayload* repPayload = NULL;
1060 repPayload = OCRepPayloadCreate();
1063 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1067 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {3, 0, 0};
1068 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
1071 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1075 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
1076 interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_LL);
1077 interfaces[2] = OICStrdup(OC_RSRVD_INTERFACE_BATCH);
1079 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
1081 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
1082 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
1085 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1089 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1090 resourceTypes[1] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COL);
1092 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
1094 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, g_ESEasySetupResource.status);
1095 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, g_ESEasySetupResource.lastErrCode);
1096 if(g_ESEasySetupResource.numRequest > 0)
1098 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESEasySetupResource.numRequest, 0, 0};
1099 int64_t *connectRequest = (int64_t *)OICMalloc(g_ESEasySetupResource.numRequest * sizeof(int64_t));
1102 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
1106 for(int i = 0 ; i < g_ESEasySetupResource.numRequest ; ++i)
1108 connectRequest[i] = g_ESEasySetupResource.connectRequest[i];
1110 OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, (int64_t *)connectRequest, dimensions);
1114 OIC_LOG(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.numRequest is 0");
1115 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {0, 0, 0};
1116 OCRepPayloadSetIntArray(repPayload, OC_RSRVD_ES_CONNECT, NULL, dimensions);
1119 if(gWriteUserdataCb)
1121 gWriteUserdataCb(repPayload, OC_RSRVD_ES_RES_TYPE_EASYSETUP);
1124 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
1127 if(ehRequest->query)
1129 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
1130 {// When Provisioning resource has a GET with BatchInterface
1131 OCRepPayload* head = payload;
1132 OCRepPayload* nextPayload = NULL;
1134 nextPayload = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_BATCH);
1135 if(nextPayload != NULL)
1137 payload->next = nextPayload;
1138 payload = payload->next;
1141 nextPayload = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_BATCH);
1142 if(nextPayload != NULL)
1144 payload->next = nextPayload;
1145 payload = payload->next;
1148 nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
1149 if(nextPayload != NULL)
1151 payload->next = nextPayload;
1162 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
1164 OCStackResult res = OC_STACK_ERROR;
1165 bool maskFlag = false;
1167 res = initEasySetupResource(isSecured);
1168 if(res != OC_STACK_OK)
1170 // TODO: destroy logic will be added
1171 OIC_LOG_V(ERROR, ES_RH_TAG, "initEasySetupResource result: %s", getResult(res));
1176 if((resourceMask & ES_WIFICONF_RESOURCE) == ES_WIFICONF_RESOURCE)
1179 res = initWiFiConfResource(isSecured);
1180 if(res != OC_STACK_OK)
1182 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiConfResource result: %s", getResult(res));
1186 res = OCBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1187 if(res != OC_STACK_OK)
1189 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiConfResource result: %s", getResult(res));
1195 if((resourceMask & ES_COAPCLOUDCONF_RESOURCE) == ES_COAPCLOUDCONF_RESOURCE)
1198 res = initCoapCloudConfResource(isSecured);
1199 if(res != OC_STACK_OK)
1201 OIC_LOG_V(ERROR, ES_RH_TAG, "initCoapCloudConfResource result: %s", getResult(res));
1205 res = OCBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1206 if(res != OC_STACK_OK)
1208 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CoapCloudConfResource result: %s", getResult(res));
1213 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
1216 res = initDevConfResource(isSecured);
1217 if(res != OC_STACK_OK)
1219 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
1223 res = OCBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1224 if(res != OC_STACK_OK)
1226 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
1231 if(maskFlag == false)
1233 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
1234 return OC_STACK_ERROR;
1238 OIC_LOG_V(DEBUG, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
1243 OCStackResult DeleteEasySetupResources()
1245 OCStackResult res = OC_STACK_ERROR;
1246 if (g_ESWiFiConfResource.handle != NULL)
1248 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESWiFiConfResource.handle);
1249 if(res != OC_STACK_OK)
1251 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
1254 if (g_ESCoapCloudConfResource.handle != NULL)
1256 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESCoapCloudConfResource.handle);
1257 if(res != OC_STACK_OK)
1259 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
1262 if (g_ESDevConfResource.handle != NULL)
1264 res = OCUnBindResource(g_ESEasySetupResource.handle, g_ESDevConfResource.handle);
1265 if(res != OC_STACK_OK)
1267 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
1271 if (g_ESWiFiConfResource.handle != NULL)
1273 res = OCDeleteResource(g_ESWiFiConfResource.handle);
1274 if (res != OC_STACK_OK)
1276 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
1280 if(g_ESCoapCloudConfResource.handle != NULL)
1282 res = OCDeleteResource(g_ESCoapCloudConfResource.handle);
1283 if (res != OC_STACK_OK)
1285 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
1289 if(g_ESDevConfResource.handle != NULL)
1291 res = OCDeleteResource(g_ESDevConfResource.handle);
1292 if (res != OC_STACK_OK)
1294 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
1298 if(g_ESEasySetupResource.handle != NULL)
1300 res = OCDeleteResource(g_ESEasySetupResource.handle);
1301 if (res != OC_STACK_OK)
1303 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1310 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1312 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1315 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1318 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1320 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1324 OCRepPayload *getResp = NULL;
1327 if(ehRequest->resource == g_ESEasySetupResource.handle)
1329 if(ehRequest->query &&
1330 strcmp(ehRequest->query, "") &&
1331 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1332 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1333 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1335 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1336 return OC_EH_BAD_REQ;
1340 getResp = constructResponseOfEasySetup(ehRequest);
1343 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1345 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1347 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1348 return OC_EH_BAD_REQ;
1352 getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1355 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1357 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1359 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1360 return OC_EH_BAD_REQ;
1364 getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1367 else if(ehRequest->resource == g_ESDevConfResource.handle)
1369 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1371 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1372 return OC_EH_BAD_REQ;
1376 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1382 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1387 ehResult = OC_EH_OK;
1392 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1394 OIC_LOG(DEBUG, ES_RH_TAG, "ProcessPostRequest enter");
1395 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1396 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1398 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1402 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1405 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1409 if(ehRequest->resource == g_ESEasySetupResource.handle)
1411 if(ehRequest->query &&
1412 strcmp(ehRequest->query, "") &&
1413 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1414 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1416 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1417 return OC_EH_BAD_REQ;
1421 updateEasySetupResource(ehRequest, input);
1424 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1426 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1428 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1429 return OC_EH_BAD_REQ;
1433 updateWiFiConfResource(input);
1436 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1438 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1440 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1441 return OC_EH_BAD_REQ;
1445 updateCoapCloudConfResource(input);
1448 else if(ehRequest->resource == g_ESDevConfResource.handle)
1450 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1452 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1453 return OC_EH_BAD_REQ;
1457 updateDevConfResource(input);
1461 OCRepPayload *getResp = NULL;
1462 if(ehRequest->resource == g_ESEasySetupResource.handle)
1464 getResp = constructResponseOfEasySetup(ehRequest);
1466 else if(ehRequest->resource == g_ESWiFiConfResource.handle)
1468 getResp = constructResponseOfWiFiConf(OC_RSRVD_INTERFACE_DEFAULT);
1470 else if(ehRequest->resource == g_ESCoapCloudConfResource.handle)
1472 getResp = constructResponseOfCoapCloudConf(OC_RSRVD_INTERFACE_DEFAULT);
1474 else if(ehRequest->resource == g_ESDevConfResource.handle)
1476 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1481 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1486 ehResult = OC_EH_OK;
1491 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1492 OCRepPayload** payload)
1496 OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1501 * This is the entity handler for the registered resource.
1502 * This is invoked by OCStack whenever it recevies a request for this resource.
1504 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1505 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1508 OCEntityHandlerResult ehRet = OC_EH_OK;
1509 OCEntityHandlerResponse response =
1510 { 0, 0, OC_EH_ERROR, 0, 0,
1513 OCRepPayload* payload = NULL;
1515 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1517 if (OC_REST_GET == entityHandlerRequest->method)
1519 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1520 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1522 else if (OC_REST_PUT == entityHandlerRequest->method)
1524 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1526 //PUT request will be handled in the internal implementation
1527 if (g_ESEasySetupResource.handle != NULL)
1529 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1533 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1534 ehRet = OC_EH_ERROR;
1537 else if (OC_REST_POST == entityHandlerRequest->method)
1539 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1540 if (g_ESEasySetupResource.handle != NULL)
1542 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1546 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1547 ehRet = OC_EH_ERROR;
1551 // Format the response. Note this requires some info about the request
1552 response.requestHandle = entityHandlerRequest->requestHandle;
1553 response.resourceHandle = entityHandlerRequest->resource;
1554 response.ehResult = ehRet;
1555 //response uses OCPaylod while all get,put methodes use OCRepPayload
1556 response.payload = (OCPayload*) (payload);
1557 response.numSendVendorSpecificHeaderOptions = 0;
1558 memset(response.sendVendorSpecificHeaderOptions, 0,
1559 sizeof(response.sendVendorSpecificHeaderOptions));
1560 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1561 // Indicate that response is NOT in a persistent buffer
1562 response.persistentBufferFlag = 0;
1564 // Send the response
1565 OIC_LOG(INFO, ES_RH_TAG, "Invoking OCDoResponse");
1566 if (OCDoResponse(&response) != OC_STACK_OK)
1568 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1569 ehRet = OC_EH_ERROR;
1572 if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
1574 OIC_LOG(DEBUG, ES_RH_TAG, "Flag includes OC_OBSERVE_FLAG");
1576 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
1578 OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_REGISTER from Mediator");
1580 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
1582 OIC_LOG(DEBUG, ES_RH_TAG, "Received OC_OBSERVE_DEREGISTER from Mediator");
1586 OIC_LOG(INFO, ES_RH_TAG, "ES OCEntityHandlerCb OUT");
1590 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1592 OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty IN");
1594 g_ESWiFiConfResource.supportedFreq = (deviceProperty->WiFi).freq;
1595 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Freq : %d", g_ESWiFiConfResource.supportedFreq);
1598 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1600 g_ESWiFiConfResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1601 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "WiFi Mode : %d", g_ESWiFiConfResource.supportedMode[modeIdx]);
1604 g_ESWiFiConfResource.numMode = modeIdx;
1606 OICStrcpy(g_ESDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1607 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Device Name : %s", g_ESDevConfResource.devName);
1609 if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
1611 OIC_LOG(DEBUG, ES_RH_TAG, "wifiResource doesn't have any observers.");
1614 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
1616 OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observers.");
1619 OIC_LOG(DEBUG, ES_RH_TAG, "SetDeviceProperty OUT");
1623 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1625 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState IN");
1627 g_ESEasySetupResource.status = esState;
1628 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee Status : %d", g_ESEasySetupResource.status);
1630 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1632 OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1635 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeState OUT");
1639 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1641 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode IN");
1643 g_ESEasySetupResource.lastErrCode = esErrCode;
1644 OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "Enrollee ErrorCode : %d", g_ESEasySetupResource.lastErrCode);
1646 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
1648 OIC_LOG(DEBUG, ES_RH_TAG, "provResource doesn't have any observers.");
1651 OIC_LOG(DEBUG, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1655 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1657 if( !(ehRequest->query) ||
1658 (ehRequest->query &&
1659 (strcmp(ehRequest->query, "") && !CompareResourceInterface(ehRequest->query,
1660 OC_RSRVD_INTERFACE_DEFAULT))))
1662 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1663 return OC_EH_BAD_REQ;
1668 const char *getResult(OCStackResult result)
1673 return "OC_STACK_OK";
1674 case OC_STACK_INVALID_URI:
1675 return "OC_STACK_INVALID_URI";
1676 case OC_STACK_INVALID_QUERY:
1677 return "OC_STACK_INVALID_QUERY";
1678 case OC_STACK_INVALID_IP:
1679 return "OC_STACK_INVALID_IP";
1680 case OC_STACK_INVALID_PORT:
1681 return "OC_STACK_INVALID_PORT";
1682 case OC_STACK_INVALID_CALLBACK:
1683 return "OC_STACK_INVALID_CALLBACK";
1684 case OC_STACK_INVALID_METHOD:
1685 return "OC_STACK_INVALID_METHOD";
1686 case OC_STACK_NO_MEMORY:
1687 return "OC_STACK_NO_MEMORY";
1688 case OC_STACK_COMM_ERROR:
1689 return "OC_STACK_COMM_ERROR";
1690 case OC_STACK_INVALID_PARAM:
1691 return "OC_STACK_INVALID_PARAM";
1692 case OC_STACK_NOTIMPL:
1693 return "OC_STACK_NOTIMPL";
1694 case OC_STACK_NO_RESOURCE:
1695 return "OC_STACK_NO_RESOURCE";
1696 case OC_STACK_RESOURCE_ERROR:
1697 return "OC_STACK_RESOURCE_ERROR";
1698 case OC_STACK_SLOW_RESOURCE:
1699 return "OC_STACK_SLOW_RESOURCE";
1700 case OC_STACK_NO_OBSERVERS:
1701 return "OC_STACK_NO_OBSERVERS";
1702 case OC_STACK_ERROR:
1703 return "OC_STACK_ERROR";