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 //-----------------------------------------------------------------------------
39 * @brief Structure for holding the Provisioning status and target information required to
40 * connect to the target network
42 static ProvResource gProvResource;
43 static WiFiResource gWiFiResource;
44 static CloudResource gCloudResource;
45 static DevConfResource gDevConfResource;
47 //-----------------------------------------------------------------------------
48 // Private internal function prototypes
49 //-----------------------------------------------------------------------------
50 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
52 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
53 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
54 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
55 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
56 void updateWiFiResource(OCRepPayload* input);
57 void updateCloudResource(OCRepPayload* input);
58 void updateDevConfResource(OCRepPayload* input);
59 const char *getResult(OCStackResult result);
61 ESWiFiCB gWifiRsrcEvtCb = NULL;
62 ESCloudCB gCloudRsrcEvtCb = 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, ";");
80 if(strstr(ptr, ".if."))
83 if_ptr = strtok(ptr, "=");
84 if_ptr = strtok(NULL, "=");
86 if(!strcmp(if_ptr, iface))
92 } while ((ptr = strtok(NULL, ";")));
97 ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
99 if(!readCb && !writeCb)
101 OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
104 gReadUserdataCb = readCb;
105 gWriteUserdataCb = writeCb;
109 void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
114 void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
116 gCloudRsrcEvtCb = cb;
119 void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
121 gDevConfRsrcEvtCb = cb;
124 void UnRegisterResourceEventCallBack()
128 gWifiRsrcEvtCb = NULL;
132 gCloudRsrcEvtCb = NULL;
134 if (gDevConfRsrcEvtCb)
136 gDevConfRsrcEvtCb = NULL;
140 OCStackResult initProvResource(bool isSecured)
142 gProvResource.status = ES_STATE_INIT;
143 gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
145 OCStackResult res = OC_STACK_ERROR;
148 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
149 OC_RSRVD_INTERFACE_DEFAULT,
150 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
151 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
154 res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
155 OC_RSRVD_INTERFACE_DEFAULT,
156 OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
157 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
161 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
165 res = OCBindResourceTypeToResource(gProvResource.handle, OC_RSRVD_ES_RES_TYPE_COL);
168 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource type with result: %s", getResult(res));
172 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
175 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
178 res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
181 OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
185 OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
189 OCStackResult initWiFiResource(bool isSecured)
191 OCStackResult res = OC_STACK_ERROR;
193 gWiFiResource.supportedFreq = WIFI_BOTH;
194 gWiFiResource.supportedMode[0] = WIFI_11A;
195 gWiFiResource.supportedMode[1] = WIFI_11B;
196 gWiFiResource.supportedMode[2] = WIFI_11G;
197 gWiFiResource.supportedMode[3] = WIFI_11N;
198 gWiFiResource.numMode = 4;
199 gWiFiResource.authType = NONE_AUTH;
200 gWiFiResource.encType = NONE_ENC;
201 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
202 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");
206 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
207 OC_RSRVD_INTERFACE_DEFAULT,
208 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
209 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
212 res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
213 OC_RSRVD_INTERFACE_DEFAULT,
214 OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
215 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
218 OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFi resource with result: %s", getResult(res));
223 OCStackResult initCloudServerResource(bool isSecured)
225 OCStackResult res = OC_STACK_ERROR;
227 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
228 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
229 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");
233 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
234 OC_RSRVD_INTERFACE_DEFAULT,
235 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
236 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
239 res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
240 OC_RSRVD_INTERFACE_DEFAULT,
241 OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
242 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
245 OIC_LOG_V(INFO, ES_RH_TAG, "Created CloudServer resource with result: %s", getResult(res));
250 OCStackResult initDevConfResource(bool isSecured)
252 OCStackResult res = OC_STACK_ERROR;
254 OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
255 OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
256 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
257 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
258 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");
262 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
263 OC_RSRVD_INTERFACE_DEFAULT,
264 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
265 NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
268 res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
269 OC_RSRVD_INTERFACE_DEFAULT,
270 OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
271 NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
274 OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
279 void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
281 OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
285 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
287 // When Provisioning resource has a POST with BatchInterface
288 updateCloudResource(input);
289 updateWiFiResource(input);
290 updateDevConfResource(input);
295 void updateWiFiResource(OCRepPayload* input)
297 ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
301 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
305 memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
306 memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
307 wiFiData->authtype = NONE_AUTH;
308 wiFiData->enctype = NONE_AUTH;
309 wiFiData->userdata = NULL;
312 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
314 OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
315 OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
316 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
320 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
322 OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
323 OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
324 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
327 int64_t authType = -1;
328 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
330 gWiFiResource.authType = authType;
331 wiFiData->authtype = gWiFiResource.authType;
332 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
335 int64_t encType = -1;
336 if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
338 gWiFiResource.encType = encType;
339 wiFiData->enctype = gWiFiResource.encType;
340 OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
345 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
348 if(ssid || cred || authType!= -1 || encType != -1)
350 OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");
352 // TODO : Need to check appropriateness of gWiFiData
353 if(gWifiRsrcEvtCb != NULL)
355 gWifiRsrcEvtCb(ES_OK, wiFiData);
359 OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
363 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gWiFiResource.handle, OC_HIGH_QOS))
365 OIC_LOG(INFO, ES_RH_TAG, "Enrollee doesn't have any observers.");
371 void updateCloudResource(OCRepPayload* input)
373 ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
375 if(cloudData == NULL)
377 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
381 memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
382 memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
383 memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
384 cloudData->userdata = NULL;
386 char *authCode = NULL;
387 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
389 OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
390 OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
391 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
394 char *authProvider = NULL;
395 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
397 OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
398 OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
399 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
402 char *ciServer = NULL;
403 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
405 OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
406 OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
407 OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
412 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
415 if(authCode || authProvider || ciServer)
417 OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");
419 // TODO : Need to check appropriateness of gCloudData
420 if(gCloudRsrcEvtCb != NULL)
422 gCloudRsrcEvtCb(ES_OK, cloudData);
426 OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
430 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gCloudResource.handle, OC_HIGH_QOS))
432 OIC_LOG(INFO, ES_RH_TAG, "cloudResource doesn't have any observers.");
438 void updateDevConfResource(OCRepPayload* input)
440 ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
442 if(devConfData == NULL)
444 OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
447 memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
448 memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
449 devConfData->userdata = NULL;
451 char *location = NULL;
452 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
454 OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
455 OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
456 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
459 char *country = NULL;
460 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
462 OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
463 OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
464 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
467 char *language = NULL;
468 if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
470 OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
471 OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
472 OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
477 gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
480 if(country || language)
482 OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
484 // TODO : Need to check appropriateness of gDevConfData
485 if(gDevConfRsrcEvtCb != NULL)
487 gDevConfRsrcEvtCb(ES_OK, devConfData);
491 OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
495 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gDevConfResource.handle, OC_HIGH_QOS))
497 OIC_LOG(INFO, ES_RH_TAG, "devConfResource doesn't have any observers.");
500 OICFree(devConfData);
503 OCRepPayload* constructResponseOfWiFi(char *interface)
505 OCRepPayload* payload = OCRepPayloadCreate();
508 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
512 if(gWiFiResource.handle == NULL)
514 OIC_LOG(ERROR, ES_RH_TAG, "WiFi resource is not created");
518 OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
519 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);
521 OCRepPayload* repPayload = NULL;
522 OCRepPayload* tempPayload = NULL;
523 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
525 repPayload = OCRepPayloadCreate();
528 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
532 tempPayload = payload;
533 payload = repPayload;
535 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
536 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
538 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
540 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);
542 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
543 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
545 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFI);
547 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
551 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
552 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
\r
555 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
556 int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
557 for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
559 modes_64[i] = gWiFiResource.supportedMode[i];
561 OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
563 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
564 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
565 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
566 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
567 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
571 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
574 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
576 payload = tempPayload;
577 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
583 OCRepPayload* constructResponseOfCloud(char *interface)
585 OCRepPayload* payload = OCRepPayloadCreate();
588 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
592 if(gCloudResource.handle == NULL)
594 OIC_LOG(ERROR, ES_RH_TAG, "CloudServer resource is not created");
598 OIC_LOG(INFO, ES_RH_TAG, "constructResponse cloudserver res");
599 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
601 OCRepPayload* repPayload = NULL;
602 OCRepPayload* tempPayload = NULL;
603 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
605 repPayload = OCRepPayloadCreate();
608 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
612 tempPayload = payload;
613 payload = repPayload;
615 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
616 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
618 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
620 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);
622 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
623 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
625 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
627 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
631 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
632 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
635 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
636 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
637 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
641 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
644 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
646 payload = tempPayload;
647 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
653 OCRepPayload* constructResponseOfDevConf(char *interface)
655 OCRepPayload* payload = OCRepPayloadCreate();
658 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
662 if(gDevConfResource.handle == NULL)
664 OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
668 OIC_LOG(INFO, ES_RH_TAG, "constructResponse devconf res");
669 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
671 OCRepPayload* repPayload = NULL;
672 OCRepPayload* tempPayload = NULL;
673 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
675 repPayload = OCRepPayloadCreate();
678 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
682 tempPayload = payload;
683 payload = repPayload;
685 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
686 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
688 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
690 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);
692 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
693 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
695 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
697 OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
701 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
702 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
705 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
706 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
707 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
708 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
709 OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);
713 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
716 if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
718 payload = tempPayload;
719 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
725 OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
727 OCRepPayload* payload = OCRepPayloadCreate();
730 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
734 // Requested interface is Link list interface
735 //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
736 if(!ehRequest->query ||
737 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
738 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
739 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
741 const OCRepPayload *arrayPayload[3] = {NULL};
745 if(gWiFiResource.handle != NULL)
747 OCRepPayload *add = OCRepPayloadCreate();
750 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
754 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
755 char **resourceType = NULL;
756 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
757 char **resourceInterface = NULL;
758 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
760 if(!resourceType || !resourceInterface)
762 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
766 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFI);
767 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
769 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
770 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFI);
771 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
772 (const char **)resourceType, dimensions);
773 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
774 (const char **)resourceInterface, dimensions);
776 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)gWiFiResource.handle);
777 OCRepPayload *policy = OCRepPayloadCreate();
780 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
784 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
785 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
788 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
789 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
790 ehRequest->devAddr.flags);
791 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
794 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
796 arrayPayload[childResCnt++] = add;
799 if(gDevConfResource.handle != NULL)
801 OCRepPayload *add = OCRepPayloadCreate();
804 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
808 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
809 char **resourceType = NULL;
810 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
811 char **resourceInterface = NULL;
812 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
814 if(!resourceType || !resourceInterface)
816 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
820 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
821 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
823 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
824 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
825 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
826 (const char **)resourceType, dimensions);
827 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
828 (const char **)resourceInterface, dimensions);
830 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)gDevConfResource.handle);
831 OCRepPayload *policy = OCRepPayloadCreate();
834 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
838 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
839 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
842 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
843 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
844 ehRequest->devAddr.flags);
845 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
848 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
850 arrayPayload[childResCnt++] = add;
853 if(gCloudResource.handle != NULL)
855 OCRepPayload *add = OCRepPayloadCreate();
858 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
862 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
863 char **resourceType = NULL;
864 resourceType = (char **)OICMalloc(sizeof(char *) * 1);
865 char **resourceInterface = NULL;
866 resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);
868 if(!resourceType || !resourceInterface)
870 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
874 resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
875 resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
877 add->base.type = PAYLOAD_TYPE_REPRESENTATION;
878 OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
879 OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
880 (const char **)resourceType, dimensions);
881 OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
882 (const char **)resourceInterface, dimensions);
884 OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)gCloudResource.handle);
885 OCRepPayload *policy = OCRepPayloadCreate();
888 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
892 OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
893 ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
896 OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
897 uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
898 ehRequest->devAddr.flags);
899 OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
902 OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);
904 arrayPayload[childResCnt++] = add;
907 size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
909 if(!ehRequest->query ||
910 (ehRequest->query && !strcmp(ehRequest->query, "")) ||
911 (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
913 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
914 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
915 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
916 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_LL);
917 OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_BATCH);
918 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_PROV);
919 OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COL);
921 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
922 OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
924 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
926 else // link list interface
928 OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);
931 ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
934 OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
935 OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
937 OCRepPayload* repPayload = NULL;
939 repPayload = OCRepPayloadCreate();
942 OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
946 size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {3, 0, 0};
947 char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
949 interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
950 interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_LL);
951 interfaces[2] = OICStrdup(OC_RSRVD_INTERFACE_BATCH);
953 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);
955 size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
956 char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
958 resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_PROV);
959 resourceTypes[1] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COL);
961 OCRepPayloadSetStringArray(repPayload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
963 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
964 OCRepPayloadSetPropInt(repPayload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
966 OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
971 gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
976 if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
977 {// When Provisioning resource has a GET with BatchInterface
978 OCRepPayload* head = payload;
979 OCRepPayload* nextPayload = NULL;
981 nextPayload = constructResponseOfWiFi(OC_RSRVD_INTERFACE_BATCH);
982 if(nextPayload != NULL)
984 payload->next = nextPayload;
985 payload = payload->next;
988 nextPayload = constructResponseOfCloud(OC_RSRVD_INTERFACE_BATCH);
989 if(nextPayload != NULL)
991 payload->next = nextPayload;
992 payload = payload->next;
995 nextPayload = constructResponseOfDevConf(OC_RSRVD_INTERFACE_BATCH);
996 if(nextPayload != NULL)
998 payload->next = nextPayload;
1009 OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
1011 OCStackResult res = OC_STACK_ERROR;
1012 bool maskFlag = false;
1014 res = initProvResource(isSecured);
1015 if(res != OC_STACK_OK)
1017 // TODO: destroy logic will be added
1018 OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));
1023 if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
1026 res = initWiFiResource(isSecured);
1027 if(res != OC_STACK_OK)
1029 OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
1033 res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
1034 if(res != OC_STACK_OK)
1036 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
1042 if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
1045 res = initCloudServerResource(isSecured);
1046 if(res != OC_STACK_OK)
1048 OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
1052 res = OCBindResource(gProvResource.handle, gCloudResource.handle);
1053 if(res != OC_STACK_OK)
1055 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
1060 if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
1063 res = initDevConfResource(isSecured);
1064 if(res != OC_STACK_OK)
1066 OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
1070 res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
1071 if(res != OC_STACK_OK)
1073 OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
1079 if(maskFlag == false)
1081 OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
1082 return OC_STACK_ERROR;
1086 OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
1091 OCStackResult DeleteProvisioningResource()
1093 OCStackResult res = OCDeleteResource(gProvResource.handle);
1094 if (res != OC_STACK_OK)
1096 OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1102 OCStackResult DeleteEasySetupResources()
1104 OCStackResult res = OC_STACK_ERROR;
1105 if (gWiFiResource.handle != NULL)
1107 res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
1108 if(res != OC_STACK_OK)
1110 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
1113 if (gCloudResource.handle != NULL)
1115 res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
1116 if(res != OC_STACK_OK)
1118 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
1121 if (gDevConfResource.handle != NULL)
1123 res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
1124 if(res != OC_STACK_OK)
1126 OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
1130 if (gWiFiResource.handle != NULL)
1132 res = OCDeleteResource(gWiFiResource.handle);
1133 if (res != OC_STACK_OK)
1135 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
1139 if(gCloudResource.handle != NULL)
1141 res = OCDeleteResource(gCloudResource.handle);
1142 if (res != OC_STACK_OK)
1144 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
1148 if(gDevConfResource.handle != NULL)
1150 res = OCDeleteResource(gDevConfResource.handle);
1151 if (res != OC_STACK_OK)
1153 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
1157 if(gProvResource.handle != NULL)
1159 res = OCDeleteResource(gProvResource.handle);
1160 if (res != OC_STACK_OK)
1162 OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
1169 OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
1171 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1174 OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
1177 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1179 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1183 OCRepPayload *getResp = NULL;
1186 if(ehRequest->resource == gProvResource.handle)
1188 if(ehRequest->query &&
1189 strcmp(ehRequest->query, "") &&
1190 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL) &&
1191 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1192 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1194 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1195 return OC_EH_BAD_REQ;
1199 getResp = constructResponseOfProv(ehRequest);
1202 else if(ehRequest->resource == gWiFiResource.handle)
1204 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1206 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1207 return OC_EH_BAD_REQ;
1211 getResp = constructResponseOfWiFi(OC_RSRVD_INTERFACE_DEFAULT);
1214 else if(ehRequest->resource == gCloudResource.handle)
1216 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1218 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1219 return OC_EH_BAD_REQ;
1223 getResp = constructResponseOfCloud(OC_RSRVD_INTERFACE_DEFAULT);
1226 else if(ehRequest->resource == gDevConfResource.handle)
1228 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1230 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1231 return OC_EH_BAD_REQ;
1235 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1241 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1246 ehResult = OC_EH_OK;
1251 OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
1253 OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
1254 OCEntityHandlerResult ehResult = OC_EH_ERROR;
1255 if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
1257 OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1261 OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
1264 OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1268 if(ehRequest->resource == gProvResource.handle)
1270 if(ehRequest->query &&
1271 strcmp(ehRequest->query, "") &&
1272 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH) &&
1273 !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
1275 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1276 return OC_EH_BAD_REQ;
1280 updateProvResource(ehRequest, input);
1283 else if(ehRequest->resource == gWiFiResource.handle)
1285 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1287 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1288 return OC_EH_BAD_REQ;
1292 updateWiFiResource(input);
1295 else if(ehRequest->resource == gCloudResource.handle)
1297 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1299 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1300 return OC_EH_BAD_REQ;
1304 updateCloudResource(input);
1307 else if(ehRequest->resource == gDevConfResource.handle)
1309 if(CheckEhRequestPayload(ehRequest) != OC_EH_OK)
1311 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1312 return OC_EH_BAD_REQ;
1316 updateDevConfResource(input);
1320 OCRepPayload *getResp = NULL;
1321 if(ehRequest->resource == gProvResource.handle)
1323 getResp = constructResponseOfProv(ehRequest);
1325 else if(ehRequest->resource == gWiFiResource.handle)
1327 getResp = constructResponseOfWiFi(OC_RSRVD_INTERFACE_DEFAULT);
1329 else if(ehRequest->resource == gCloudResource.handle)
1331 getResp = constructResponseOfCloud(OC_RSRVD_INTERFACE_DEFAULT);
1333 else if(ehRequest->resource == gDevConfResource.handle)
1335 getResp = constructResponseOfDevConf(OC_RSRVD_INTERFACE_DEFAULT);
1340 OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1345 ehResult = OC_EH_OK;
1350 OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
1351 OCRepPayload** payload)
1355 OCEntityHandlerResult ehResult = OC_EH_BAD_REQ;
1360 * This is the entity handler for the registered resource.
1361 * This is invoked by OCStack whenever it recevies a request for this resource.
1363 OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
1364 OCEntityHandlerRequest* entityHandlerRequest, void *callback)
1367 OCEntityHandlerResult ehRet = OC_EH_OK;
1368 OCEntityHandlerResponse response =
1369 { 0, 0, OC_EH_ERROR, 0, 0,
1372 OCRepPayload* payload = NULL;
1374 if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
1376 if (OC_REST_GET == entityHandlerRequest->method)
1378 OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
1379 ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
1381 else if (OC_REST_PUT == entityHandlerRequest->method)
1383 OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
1385 //PUT request will be handled in the internal implementation
1386 if (gProvResource.handle != NULL)
1388 ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
1392 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1393 ehRet = OC_EH_ERROR;
1396 else if (OC_REST_POST == entityHandlerRequest->method)
1398 OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
1399 if (gProvResource.handle != NULL)
1401 ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
1405 OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
1406 ehRet = OC_EH_ERROR;
1410 // Format the response. Note this requires some info about the request
1411 response.requestHandle = entityHandlerRequest->requestHandle;
1412 response.resourceHandle = entityHandlerRequest->resource;
1413 response.ehResult = ehRet;
1414 //response uses OCPaylod while all get,put methodes use OCRepPayload
1415 response.payload = (OCPayload*) (payload);
1416 response.numSendVendorSpecificHeaderOptions = 0;
1417 memset(response.sendVendorSpecificHeaderOptions, 0,
1418 sizeof(response.sendVendorSpecificHeaderOptions));
1419 memset(response.resourceUri, 0, sizeof(response.resourceUri));
1420 // Indicate that response is NOT in a persistent buffer
1421 response.persistentBufferFlag = 0;
1423 // Send the response
1424 if (OCDoResponse(&response) != OC_STACK_OK)
1426 OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
1427 ehRet = OC_EH_ERROR;
1430 if (entityHandlerRequest && (flag & OC_OBSERVE_FLAG))
1432 OIC_LOG(INFO, ES_RH_TAG, "Flag includes OC_OBSERVE_FLAG");
1434 if (OC_OBSERVE_REGISTER == entityHandlerRequest->obsInfo.action)
1436 OIC_LOG (INFO, ES_RH_TAG, "Received OC_OBSERVE_REGISTER from Mediator");
1438 else if (OC_OBSERVE_DEREGISTER == entityHandlerRequest->obsInfo.action)
1440 OIC_LOG (INFO, ES_RH_TAG, "Received OC_OBSERVE_DEREGISTER from Mediator");
1446 OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
1448 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");
1450 gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
1451 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);
1454 while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
1456 gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
1457 OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
1460 gWiFiResource.numMode = modeIdx;
1462 OICStrcpy(gDevConfResource.devName, OIC_STRING_MAX_VALUE, (deviceProperty->DevConf).deviceName);
1463 OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);
1465 OICStrcpy(gDevConfResource.modelNumber, OIC_STRING_MAX_VALUE,
1466 (deviceProperty->DevConf).modelNumber);
1467 OIC_LOG_V(INFO, ES_RH_TAG, "Model Number : %s", gDevConfResource.modelNumber);
1469 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gWiFiResource.handle, OC_HIGH_QOS))
1471 OIC_LOG(INFO, ES_RH_TAG, "wifiResource doesn't have any observers.");
1474 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gDevConfResource.handle, OC_HIGH_QOS))
1476 OIC_LOG(INFO, ES_RH_TAG, "devConfResource doesn't have any observers.");
1479 OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
1483 OCStackResult SetEnrolleeState(ESEnrolleeState esState)
1485 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");
1487 gProvResource.status = esState;
1488 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);
1490 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gProvResource.handle, OC_HIGH_QOS))
1492 OIC_LOG(INFO, ES_RH_TAG, "provResource doesn't have any observers.");
1495 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
1499 OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
1501 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");
1503 gProvResource.lastErrCode = esErrCode;
1504 OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);
1506 if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(gProvResource.handle, OC_HIGH_QOS))
1508 OIC_LOG(INFO, ES_RH_TAG, "provResource doesn't have any observers.");
1511 OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
1515 OCEntityHandlerResult CheckEhRequestPayload(OCEntityHandlerRequest *ehRequest)
1517 if( !(ehRequest->query) ||
1518 (ehRequest->query &&
1519 (strcmp(ehRequest->query, "") && !CompareResourceInterface(ehRequest->query,
1520 OC_RSRVD_INTERFACE_DEFAULT))))
1522 OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
1523 return OC_EH_BAD_REQ;
1528 const char *getResult(OCStackResult result)
1533 return "OC_STACK_OK";
1534 case OC_STACK_INVALID_URI:
1535 return "OC_STACK_INVALID_URI";
1536 case OC_STACK_INVALID_QUERY:
1537 return "OC_STACK_INVALID_QUERY";
1538 case OC_STACK_INVALID_IP:
1539 return "OC_STACK_INVALID_IP";
1540 case OC_STACK_INVALID_PORT:
1541 return "OC_STACK_INVALID_PORT";
1542 case OC_STACK_INVALID_CALLBACK:
1543 return "OC_STACK_INVALID_CALLBACK";
1544 case OC_STACK_INVALID_METHOD:
1545 return "OC_STACK_INVALID_METHOD";
1546 case OC_STACK_NO_MEMORY:
1547 return "OC_STACK_NO_MEMORY";
1548 case OC_STACK_COMM_ERROR:
1549 return "OC_STACK_COMM_ERROR";
1550 case OC_STACK_INVALID_PARAM:
1551 return "OC_STACK_INVALID_PARAM";
1552 case OC_STACK_NOTIMPL:
1553 return "OC_STACK_NOTIMPL";
1554 case OC_STACK_NO_RESOURCE:
1555 return "OC_STACK_NO_RESOURCE";
1556 case OC_STACK_RESOURCE_ERROR:
1557 return "OC_STACK_RESOURCE_ERROR";
1558 case OC_STACK_SLOW_RESOURCE:
1559 return "OC_STACK_SLOW_RESOURCE";
1560 case OC_STACK_NO_OBSERVERS:
1561 return "OC_STACK_NO_OBSERVERS";
1562 case OC_STACK_ERROR:
1563 return "OC_STACK_ERROR";