resourcehandler.c 55 KB
Newer Older
1 2
//******************************************************************
//
3
// Copyright 2015 Samsung Electronics All Rights Reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

21
#include "resourcehandler.h"
22

23 24
#include "ocpayload.h"
#include "oic_string.h"
25
#include "oic_malloc.h"
26
#include "cautilinterface.h"
27

28 29 30 31 32 33 34 35 36 37
/**
 * @var ES_RH_TAG
 * @brief Logging tag for module name.
 */
#define ES_RH_TAG "ES_RH"
//-----------------------------------------------------------------------------
// Private variables
//-----------------------------------------------------------------------------

/**
38
 * @var g_ESEasySetupResource
39
 * @brief Structure for holding the Provisioning status
40
 */
41 42 43
EasySetupResource g_ESEasySetupResource;
WiFiConfResource g_ESWiFiConfResource;
CoapCloudConfResource g_ESCoapCloudConfResource;
44
DevConfResource g_ESDevConfResource;
45 46 47 48

//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
49 50 51 52 53
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
        void *callback);
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
54 55 56
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
void updateWiFiConfResource(OCRepPayload* input);
void updateCoapCloudConfResource(OCRepPayload* input);
57
void updateDevConfResource(OCRepPayload* input);
Jihun Ha's avatar
Jihun Ha committed
58
const char *getResult(OCStackResult result);
59

60 61
ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
62
ESDevConfCB gDevConfRsrcEvtCb = NULL;
63

64 65 66
ESReadUserdataCb gReadUserdataCb = NULL;
ESWriteUserdataCb gWriteUserdataCb = NULL;

67 68 69 70 71
bool CompareResourceInterface(char *from, char *iface)
{
    char *str = OICStrdup(from);
    char *ptr = strtok(str, ";");

72
    if(ptr == NULL)
73
    {
74
        return false;
75
    }
76

77 78 79 80 81 82 83 84 85 86 87 88 89 90
    do
    {
        if(strstr(ptr, ".if."))
        {
            char *if_ptr = NULL;
            if_ptr = strtok(ptr, "=");
            if_ptr = strtok(NULL, "=");

            if(!strcmp(if_ptr, iface))
            {
                return true;
            }
        }

Parkhi's avatar
Parkhi committed
91
    } while ((ptr = strtok(NULL, ";")));
92 93 94 95

    return false;
}

96 97 98 99 100 101 102 103 104 105 106 107
ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
{
    if(!readCb && !writeCb)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Both of callbacks for user data are null");
        return ES_ERROR;
    }
    gReadUserdataCb = readCb;
    gWriteUserdataCb = writeCb;
    return ES_OK;
}

108
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
109
{
110
    gWifiConfRsrcEvtCb = cb;
111 112
}

113
void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
114
{
115
    gCoapCloudConfRsrcEvtCb = cb;
116 117 118 119 120
}

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
121 122
}

123 124
void UnRegisterResourceEventCallBack()
{
125
    if (gWifiConfRsrcEvtCb)
126
    {
127
        gWifiConfRsrcEvtCb = NULL;
128
    }
129
    if (gCoapCloudConfRsrcEvtCb)
130
    {
131
        gCoapCloudConfRsrcEvtCb = NULL;
132 133 134 135
    }
    if (gDevConfRsrcEvtCb)
    {
        gDevConfRsrcEvtCb = NULL;
136 137 138
    }
}

139
OCStackResult initEasySetupResource(bool isSecured)
140
{
141 142
    g_ESEasySetupResource.status = ES_STATE_INIT;
    g_ESEasySetupResource.lastErrCode = ES_ERRCODE_NO_ERROR;
143

144 145 146
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
147
        res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
148
        OC_RSRVD_INTERFACE_DEFAULT,
149
        OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
150
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
151
    }else
152
    {
153
        res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
154
        OC_RSRVD_INTERFACE_DEFAULT,
155
        OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
156
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
157
    }
158
    if(res != OC_STACK_OK)
159
    {
160
        OIC_LOG_V(INFO, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
161 162 163
        return res;
    }

164
    res = OCBindResourceTypeToResource(g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_COL);
165
    if(res != OC_STACK_OK)
166 167 168 169 170
    {
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource type with result: %s", getResult(res));
        return res;
    }

171
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_LL);
172
    if(res != OC_STACK_OK)
173
    {
Jihun Ha's avatar
Jihun Ha committed
174
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
175 176
        return res;
    }
177
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_BATCH);
178
    if(res != OC_STACK_OK)
179
    {
Jihun Ha's avatar
Jihun Ha committed
180
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
181 182
        return res;
    }
183

184
    OIC_LOG_V(INFO, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
185 186
    return res;
}
187

188
OCStackResult initWiFiConfResource(bool isSecured)
189 190 191
{
    OCStackResult res = OC_STACK_ERROR;

192 193 194 195 196 197 198 199 200 201
    g_ESWiFiConfResource.supportedFreq = WIFI_BOTH;
    g_ESWiFiConfResource.supportedMode[0] = WIFI_11A;
    g_ESWiFiConfResource.supportedMode[1] = WIFI_11B;
    g_ESWiFiConfResource.supportedMode[2] = WIFI_11G;
    g_ESWiFiConfResource.supportedMode[3] = WIFI_11N;
    g_ESWiFiConfResource.numMode = 4;
    g_ESWiFiConfResource.authType = NONE_AUTH;
    g_ESWiFiConfResource.encType = NONE_ENC;
    OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), "");
    OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), "");
202 203 204

    if (isSecured)
    {
205
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
206
        OC_RSRVD_INTERFACE_DEFAULT,
207
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
208 209 210
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
211
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
212
        OC_RSRVD_INTERFACE_DEFAULT,
213
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
214 215 216
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

217
    OIC_LOG_V(INFO, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
218 219 220 221
    return res;

}

222
OCStackResult initCoapCloudConfResource(bool isSecured)
223 224 225
{
    OCStackResult res = OC_STACK_ERROR;

226 227 228 229 230
    OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
    OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), "");
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
    OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), "");
    OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
231 232 233

    if (isSecured)
    {
234
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
235
        OC_RSRVD_INTERFACE_DEFAULT,
236
        OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
237 238 239
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
240
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
241
        OC_RSRVD_INTERFACE_DEFAULT,
242
        OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
243 244 245
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

246
    OIC_LOG_V(INFO, ES_RH_TAG, "Created CoapCloudConf resource with result: %s", getResult(res));
247 248 249 250 251 252 253 254
    return res;

}

OCStackResult initDevConfResource(bool isSecured)
{
    OCStackResult res = OC_STACK_ERROR;

255 256 257 258 259
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
    OICStrcpy(g_ESDevConfResource.modelNumber, sizeof(g_ESDevConfResource.modelNumber), "");
    OICStrcpy(g_ESDevConfResource.location, sizeof(g_ESDevConfResource.location), "");
    OICStrcpy(g_ESDevConfResource.country, sizeof(g_ESDevConfResource.country), "");
    OICStrcpy(g_ESDevConfResource.language, sizeof(g_ESDevConfResource.language), "");
260 261 262

    if (isSecured)
    {
263
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
264 265 266 267 268
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
269
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
270 271 272 273 274 275 276 277 278 279
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

    OIC_LOG_V(INFO, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
    return res;

}

280
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
281
{
282
    OIC_LOG_V(INFO, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
283 284 285

    if(ehRequest->query)
    {
286
        if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
287
        {
288
            // When Provisioning resource has a POST with BatchInterface
289 290
            updateCoapCloudConfResource(input);
            updateWiFiConfResource(input);
291
            updateDevConfResource(input);
292 293
        }
    }
294 295
}

296
void updateWiFiConfResource(OCRepPayload* input)
297
{
298
    ESWiFiConfData* wiFiData = (ESWiFiConfData*)OICMalloc(sizeof(ESWiFiConfData));
299

300 301 302
    if(wiFiData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
303
        return ;
304
    }
305

306 307
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
308 309
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
310 311
    wiFiData->userdata = NULL;

312
    char* ssid = NULL;
313 314
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
315
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
316
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
317
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s", g_ESWiFiConfResource.ssid);
318 319
    }

320
    char* cred = NULL;
321 322
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
323
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
324
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
325
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESWiFiConfResource.cred %s", g_ESWiFiConfResource.cred);
326 327
    }

328
    int64_t authType = -1;
329 330
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
331 332 333
        g_ESWiFiConfResource.authType = authType;
        wiFiData->authtype = g_ESWiFiConfResource.authType;
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESWiFiConfResource.authType %u", g_ESWiFiConfResource.authType);
334 335
    }

336
    int64_t encType = -1;
337 338
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
339 340 341
        g_ESWiFiConfResource.encType = encType;
        wiFiData->enctype = g_ESWiFiConfResource.encType;
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESWiFiConfResource.encType %u", g_ESWiFiConfResource.encType);
342
    }
343

344
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
345
    {
346
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
347
    }
348

349 350
    if(ssid || cred || authType!= -1 || encType != -1)
    {
351
        OIC_LOG(INFO, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
352 353

        // TODO : Need to check appropriateness of gWiFiData
354
        if(gWifiConfRsrcEvtCb != NULL)
355
        {
356
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
357 358 359
        }
        else
        {
360
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
361 362 363
        }
    }

364
    if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
365
    {
366
        OIC_LOG(INFO, ES_RH_TAG, "Enrollee doesn't have any observer.");
367 368
    }

369
    OICFree(wiFiData);
370
}
371

372
void updateCoapCloudConfResource(OCRepPayload* input)
373
{
374
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
375

376 377 378
    if(cloudData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
379
        return;
380
    }
381 382

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
383
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
384
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
385 386
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
    memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
387 388
    cloudData->userdata = NULL;

389
    char *authCode = NULL;
390 391
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
392
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
393
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
394
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
395 396
    }

397 398 399
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
400
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
401
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
402
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
403 404 405 406 407
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
408 409 410
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
411 412
    }

413
    char *authProvider = NULL;
414 415
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
416
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
417
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
418
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
419 420
    }

421
    char *ciServer = NULL;
422 423
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
424
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
425
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
426
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
427
    }
428

429
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
430
    {
431
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
432
    }
433

434
    if(authCode || accessToken || authProvider || ciServer)
435
    {
436
        OIC_LOG(INFO, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
437 438

        // TODO : Need to check appropriateness of gCloudData
439
        if(gCoapCloudConfRsrcEvtCb != NULL)
440
        {
441
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
442 443 444
        }
        else
        {
445
            OIC_LOG(ERROR, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
446 447
        }
    }
448

449
    if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
450
    {
451
        OIC_LOG(INFO, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
452 453
    }

454
    OICFree(cloudData);
455 456
}

457
void updateDevConfResource(OCRepPayload* input)
458
{
459
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
460

461 462 463
    if(devConfData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
464
        return;
465
    }
466 467
    memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
    memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
468 469
    devConfData->userdata = NULL;

470 471 472
    char *location = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
    {
473
        OICStrcpy(g_ESDevConfResource.location, sizeof(g_ESDevConfResource.location), location);
474
        OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
475
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESDevConfResource.location %s", g_ESDevConfResource.location);
476 477
    }

478
    char *country = NULL;
479
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
480
    {
481
        OICStrcpy(g_ESDevConfResource.country, sizeof(g_ESDevConfResource.country), country);
482
        OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
483
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESDevConfResource.country %s", g_ESDevConfResource.country);
484 485
    }

486
    char *language = NULL;
487
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
488
    {
489
        OICStrcpy(g_ESDevConfResource.language, sizeof(g_ESDevConfResource.language), language);
490
        OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
491
        OIC_LOG_V(INFO, ES_RH_TAG, "g_ESDevConfResource.language %s", g_ESDevConfResource.language);
492
    }
493

494
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
495
    {
496
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
497
    }
498

499 500 501 502 503 504 505
    if(country || language)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");

        // TODO : Need to check appropriateness of gDevConfData
        if(gDevConfRsrcEvtCb != NULL)
        {
506
            gDevConfRsrcEvtCb(ES_OK, devConfData);
507 508 509 510 511 512
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
        }
    }
513

514
    if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
515
    {
516
        OIC_LOG(INFO, ES_RH_TAG, "devConfResource doesn't have any observer.");
517 518
    }

519
    OICFree(devConfData);
520 521
}

522
OCRepPayload* constructResponseOfWiFiConf(char *interface)
523 524 525 526 527 528 529 530
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

531
    if(g_ESWiFiConfResource.handle == NULL)
532
    {
533
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
534 535 536
        return NULL;
    }

537 538
    OIC_LOG(INFO, ES_RH_TAG, "constructResponse WiFiConf res");
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));

564
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
565 566 567 568 569 570

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
571
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
572
    }
573

574 575 576
    size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESWiFiConfResource.numMode, 0, 0};
    int64_t *modes_64 = (int64_t *)OICMalloc(g_ESWiFiConfResource.numMode * sizeof(int64_t));
    for(int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
577
    {
578
        modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
579
    }
580
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
581

582 583 584 585 586
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, g_ESWiFiConfResource.supportedFreq);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) g_ESWiFiConfResource.authType);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) g_ESWiFiConfResource.encType);
587

588
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
589
    {
590
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
591
    }
592

593 594 595 596 597 598
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

599 600 601
    return payload;
}

602
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
603 604 605 606 607 608 609 610
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

611
    if(g_ESCoapCloudConfResource.handle == NULL)
612
    {
613
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
614 615 616
        return NULL;
    }

617 618
    OIC_LOG(INFO, ES_RH_TAG, "constructResponse CoapCloudConf res");
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));

644
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
645 646 647 648 649 650

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
651
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
652
    }
653

654 655 656 657 658
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, g_ESCoapCloudConfResource.authCode);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ACCESSTOKEN, g_ESCoapCloudConfResource.accessToken);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ACCESSTOKEN_TYPE, (int)g_ESCoapCloudConfResource.accessTokenType);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, g_ESCoapCloudConfResource.authProvider);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, g_ESCoapCloudConfResource.ciServer);
659 660

    if(gWriteUserdataCb)
661
    {
662
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
663
    }
664

665 666 667 668 669 670
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

671 672 673
    return payload;
}

674
OCRepPayload* constructResponseOfDevConf(char *interface)
675 676 677 678 679 680 681 682
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

683
    if(g_ESDevConfResource.handle == NULL)
684 685 686 687 688
    {
        OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
        return NULL;
    }

689
    OIC_LOG(INFO, ES_RH_TAG, "constructResponse DevConf res");
690
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));

        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
    }
725

726 727 728 729 730
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, g_ESDevConfResource.devName);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, g_ESDevConfResource.modelNumber);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, g_ESDevConfResource.location);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, g_ESDevConfResource.language);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, g_ESDevConfResource.country);
731

732
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
733
    {
734
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
Jihun Ha's avatar
Jihun Ha committed
735
    }
736

737 738 739 740 741 742
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

743 744 745
    return payload;
}

746
OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
747 748 749 750 751 752 753 754
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

755
    // Requested interface is Link list interface
756 757 758 759 760
    //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
    if(!ehRequest->query ||
        (ehRequest->query && !strcmp(ehRequest->query, "")) ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
761 762 763 764 765
    {
        const OCRepPayload *arrayPayload[3] = {NULL};

        int childResCnt = 0;

766
        if(g_ESWiFiConfResource.handle != NULL)
767 768 769 770
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
771
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
772 773 774 775 776 777 778 779 780 781 782
                return NULL;
            }

            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
            char **resourceType = NULL;
            resourceType = (char **)OICMalloc(sizeof(char *) * 1);
            char **resourceInterface = NULL;
            resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);

            if(!resourceType || !resourceInterface)
            {
783
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
784 785 786
                return NULL;
            }

787
            resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
788 789 790
            resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

            add->base.type = PAYLOAD_TYPE_REPRESENTATION;
791
            OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
792 793 794 795 796
            OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
                                            (const char **)resourceType, dimensions);
            OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
                                            (const char **)resourceInterface, dimensions);

797
            OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
            OCRepPayload *policy = OCRepPayloadCreate();
            if (!policy)
            {
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
                return NULL;
            }

            OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
                                    ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
            if (p & OC_SECURE)
            {
                OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
                uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
                                                                    ehRequest->devAddr.flags);
                OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
            }

            OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);

817 818 819
            arrayPayload[childResCnt++] = add;
        }

820
        if(g_ESDevConfResource.handle != NULL)
821 822 823 824
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
825
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
826 827 828 829 830 831 832 833 834 835 836
                return NULL;
            }

            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
            char **resourceType = NULL;
            resourceType = (char **)OICMalloc(sizeof(char *) * 1);
            char **resourceInterface = NULL;
            resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);

            if(!resourceType || !resourceInterface)
            {
837
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
838 839 840 841 842 843 844 845 846 847 848 849 850
                return NULL;
            }

            resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);
            resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

            add->base.type = PAYLOAD_TYPE_REPRESENTATION;
            OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_DEVCONF);
            OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
                                            (const char **)resourceType, dimensions);
            OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
                                            (const char **)resourceInterface, dimensions);

851
            OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESDevConfResource.handle);
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
            OCRepPayload *policy = OCRepPayloadCreate();
            if (!policy)
            {
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
                return NULL;
            }

            OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
                                    ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
            if (p & OC_SECURE)
            {
                OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
                uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
                                                                    ehRequest->devAddr.flags);
                OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
            }

            OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);

871 872 873
            arrayPayload[childResCnt++] = add;
        }

874
        if(g_ESCoapCloudConfResource.handle != NULL)
875 876 877 878
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
879
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
880 881 882 883 884 885 886 887 888 889 890
                return NULL;
            }

            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
            char **resourceType = NULL;
            resourceType = (char **)OICMalloc(sizeof(char *) * 1);
            char **resourceInterface = NULL;
            resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);

            if(!resourceType || !resourceInterface)
            {
891
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
892 893 894
                return NULL;
            }

895
            resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
896 897 898
            resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

            add->base.type = PAYLOAD_TYPE_REPRESENTATION;
899
            OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_COAPCLOUDCONF);
900 901 902 903 904
            OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
                                            (const char **)resourceType, dimensions);
            OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
                                            (const char **)resourceInterface, dimensions);

905
            OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESCoapCloudConfResource.handle);
Parkhi's avatar