resourcehandler.c 39.3 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

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

/**
37
 * @var gProvResource
38 39
 * @brief Structure for holding the Provisioning status and target information required to
 * connect to the target network
40
 */
41
static ProvResource gProvResource;
42 43 44
static WiFiResource gWiFiResource;
static CloudResource gCloudResource;
static DevConfResource gDevConfResource;
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
void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
55 56 57
void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
Jihun Ha's avatar
Jihun Ha committed
58
const char *getResult(OCStackResult result);
59

60 61 62
ESWiFiCB gWifiRsrcEvtCb = NULL;
ESCloudCB gCloudRsrcEvtCb = NULL;
ESDevConfCB gDevConfRsrcEvtCb = NULL;
63

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

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
bool CompareResourceInterface(char *from, char *iface)
{
    char *str = OICStrdup(from);
    char *ptr = strtok(str, ";");

    do
    {
        if(strstr(ptr, ".if."))
        {
            char *if_ptr = NULL;
            if_ptr = strtok(ptr, "=");
            if_ptr = strtok(NULL, "=");

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

    } while (ptr = strtok(NULL, ";"));

    return false;
}

91 92 93 94 95 96 97 98 99 100 101 102
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;
}

103
void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
104
{
105 106 107 108 109 110 111 112 113 114 115
    gWifiRsrcEvtCb = cb;
}

void RegisterCloudRsrcEventCallBack(ESCloudCB cb)
{
    gCloudRsrcEvtCb = cb;
}

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
116 117
}

118 119
void UnRegisterResourceEventCallBack()
{
120
    if (gWifiRsrcEvtCb)
121
    {
122 123 124 125 126 127 128 129 130
        gWifiRsrcEvtCb = NULL;
    }
    if (gCloudRsrcEvtCb)
    {
        gCloudRsrcEvtCb = NULL;
    }
    if (gDevConfRsrcEvtCb)
    {
        gDevConfRsrcEvtCb = NULL;
131 132 133
    }
}

134
OCStackResult initProvResource(bool isSecured)
135
{
136 137
    gProvResource.status = ES_STATE_INIT;
    gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
138
    OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
139

140 141 142
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
143
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
144 145 146
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
147
    }else
148
    {
149
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
150 151 152
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
153
    }
154 155 156 157 158 159 160 161 162
    if(res)
    {
        OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
        return res;
    }

    res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_LL);
    if(res)
    {
Jihun Ha's avatar
Jihun Ha committed
163
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
164 165 166 167 168
        return res;
    }
    res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
    if(res)
    {
Jihun Ha's avatar
Jihun Ha committed
169
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
170 171
        return res;
    }
172

173
    OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
174 175
    return res;
}
176

177 178 179 180
OCStackResult initWiFiResource(bool isSecured)
{
    OCStackResult res = OC_STACK_ERROR;

181 182 183 184 185
    gWiFiResource.supportedFreq = WIFI_BOTH;
    gWiFiResource.supportedMode[0] = WIFI_11A;
    gWiFiResource.supportedMode[1] = WIFI_11B;
    gWiFiResource.supportedMode[2] = WIFI_11G;
    gWiFiResource.supportedMode[3] = WIFI_11N;
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    gWiFiResource.numMode = 4;
    gWiFiResource.authType = NONE_AUTH;
    gWiFiResource.encType = NONE_ENC;
    OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), "");
    OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), "");

    if (isSecured)
    {
        res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
        res = OCCreateResource(&gWiFiResource.handle, OC_RSRVD_ES_RES_TYPE_WIFI,
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_WIFI, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

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

}

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

    OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), "");
    OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), "");
    OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), "");

    if (isSecured)
    {
        res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
        res = OCCreateResource(&gCloudResource.handle, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER,
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_CLOUDSERVER, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

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

}

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

    OICStrcpy(gDevConfResource.devName, sizeof(gDevConfResource.devName), "");
243 244
    OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
    OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), "");
    OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), "");

    if (isSecured)
    {
        res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
        res = OCCreateResource(&gDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
        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;

}

267
void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
268
{
269
    OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
270 271 272

    if(ehRequest->query)
    {
273
        if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
274
        {
275 276 277 278
            // When Provisioning resource has a POST with BatchInterface
            updateCloudResource(NULL, input);
            updateWiFiResource(NULL, input);
            updateDevConfResource(NULL, input);
279 280
        }
    }
281 282
}

283
void updateWiFiResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
284
{
285 286 287 288 289 290 291
    if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return ;
    }

292
    ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
293

294 295 296
    if(wiFiData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
297
        return ;
298
    }
299

300 301
    memset(wiFiData->ssid, 0, MAX_WEBLINKLEN);
    memset(wiFiData->pwd, 0, MAX_WEBLINKLEN);
302 303
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
304 305
    wiFiData->userdata = NULL;

306
    char* ssid = NULL;
307 308 309
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
        OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
310
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
311
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
312 313
    }

314
    char* cred = NULL;
315 316 317
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
        OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
318
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
319 320 321
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
    }

322
    int64_t authType = -1;
323 324 325
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
        gWiFiResource.authType = authType;
326
        wiFiData->authtype = gWiFiResource.authType;
327 328 329
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
    }

330
    int64_t encType = -1;
331 332 333
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
        gWiFiResource.encType = encType;
334
        wiFiData->enctype = gWiFiResource.encType;
335 336
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
    }
337

338
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
339
    {
340
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
341
    }
342

343 344 345 346 347 348 349
    if(ssid || cred || authType!= -1 || encType != -1)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Send WiFiRsrc Callback To ES");

        // TODO : Need to check appropriateness of gWiFiData
        if(gWifiRsrcEvtCb != NULL)
        {
350
            gWifiRsrcEvtCb(ES_OK, wiFiData);
351 352 353 354 355 356 357
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
        }
    }

358
    OICFree(wiFiData);
359
}
360

361
void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
362
{
363 364 365 366 367 368 369
    if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return ;
    }

370
    ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
371

372 373 374
    if(cloudData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
375
        return;
376
    }
377 378 379 380

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
    memset(cloudData->ciServer, 0, OIC_STRING_MAX_VALUE);
381 382
    cloudData->userdata = NULL;

383
    char *authCode = NULL;
384 385 386
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
        OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
387
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
388 389 390
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
    }

391
    char *authProvider = NULL;
392 393 394
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
        OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
395
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
396 397 398
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
    }

399
    char *ciServer = NULL;
400 401 402
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
        OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
403
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
404 405
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
    }
406

407
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
408
    {
409
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
410
    }
411

412 413 414 415 416 417 418
    if(authCode || authProvider || ciServer)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Send CloudRsrc Callback To ES");

        // TODO : Need to check appropriateness of gCloudData
        if(gCloudRsrcEvtCb != NULL)
        {
419
            gCloudRsrcEvtCb(ES_OK, cloudData);
420 421 422 423 424 425
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
        }
    }
426 427

    OICFree(cloudData);
428 429
}

430
void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
431
{
432 433 434 435 436 437 438
    if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return ;
    }

439
    ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
440

441 442 443
    if(devConfData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
444
        return;
445
    }
446 447
    memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
    memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
448 449
    devConfData->userdata = NULL;

450 451 452 453 454 455 456 457
    char *location = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
    {
        OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), location);
        OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.location %s", gDevConfResource.location);
    }

458
    char *country = NULL;
459
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
460 461
    {
        OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
462
        OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
463 464 465
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
    }

466
    char *language = NULL;
467
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
468 469
    {
        OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
470
        OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
471 472
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
    }
473

474
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
475
    {
476
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
477
    }
478

479 480 481 482 483 484 485
    if(country || language)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");

        // TODO : Need to check appropriateness of gDevConfData
        if(gDevConfRsrcEvtCb != NULL)
        {
486
            gDevConfRsrcEvtCb(ES_OK, devConfData);
487 488 489 490 491 492
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
        }
    }
493 494

    OICFree(devConfData);
495 496
}

497
OCRepPayload* constructResponseOfWiFi(OCEntityHandlerRequest *ehRequest)
498
{
499 500 501 502 503 504 505
    if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return NULL;
    }

506 507 508 509 510 511 512 513 514 515 516
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

    OIC_LOG(INFO, ES_RH_TAG, "constructResponse wifi res");
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFI);

    size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
517
    int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
518
    for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
519
    {
520
        modes_64[i] = gWiFiResource.supportedMode[i];
521
    }
522
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
523 524 525 526

    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
527 528
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
529

530
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
531
    {
532
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
Jihun Ha's avatar
Jihun Ha committed
533
    }
534

535 536 537
    return payload;
}

538
OCRepPayload* constructResponseOfCloud(OCEntityHandlerRequest *ehRequest)
539
{
540 541 542 543 544 545 546
    if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return NULL;
    }

547 548 549 550 551 552 553 554 555 556 557 558
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

    OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_CLOUDSERVER);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
559 560

    if(gWriteUserdataCb)
561
    {
562
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
563
    }
564 565 566 567

    return payload;
}

568
OCRepPayload* constructResponseOfDevConf(OCEntityHandlerRequest *ehRequest)
569
{
570 571 572 573 574 575 576
    if(ehRequest && !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return NULL;
    }

577 578 579 580 581 582 583 584 585 586
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

    OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
587 588
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
589 590 591
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);

592
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
593
    {
594
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
Jihun Ha's avatar
Jihun Ha committed
595
    }
596

597 598 599 600 601 602 603 604 605 606 607 608
    return payload;
}

OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

609 610 611 612 613 614 615 616 617 618 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 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 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 725 726 727 728 729 730 731
    // Requested interface is Link list interface
    if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
    {
        const OCRepPayload *arrayPayload[3] = {NULL};

        int childResCnt = 0;

        if(gWiFiResource.handle != NULL)
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
                OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
                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)
            {
                OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
                return NULL;
            }

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

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

            arrayPayload[childResCnt++] = add;
        }

        if(gDevConfResource.handle != NULL)
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
                OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
                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)
            {
                OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
                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);

            arrayPayload[childResCnt++] = add;
        }

        if(gCloudResource.handle != NULL)
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
                OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
                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)
            {
                OIC_LOG(ERROR, TAG, "Failed to allocate Payload");
                return NULL;
            }

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

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

            arrayPayload[childResCnt++] = add;
        }

        size_t dimensions[MAX_REP_ARRAY_DEPTH] = {childResCnt, 0, 0};
        OCRepPayloadSetPropObjectArray(payload, OC_RSRVD_ES_LINKS, arrayPayload, dimensions);

        return payload;
    } else if (!ehRequest->query ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH)) ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
    {
        OIC_LOG(INFO, ES_RH_TAG, "constructResponse prov res");
        OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_PROV);
        OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
        OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
        OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
    }
732

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

738 739
    if(ehRequest->query)
    {
740
        if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
741
        {// When Provisioning resource has a GET with BatchInterface
742 743
            OCRepPayload* head = payload;
            OCRepPayload* nextPayload = NULL;
744

745
            nextPayload = constructResponseOfWiFi(NULL);
746
            if(nextPayload != NULL)
747
            {
748 749
                payload->next = nextPayload;
                payload = payload->next;
750
            }
751

752
            nextPayload = constructResponseOfCloud(NULL);
753
            if(nextPayload != NULL)
754
            {
755 756
                payload->next = nextPayload;
                payload = payload->next;
757
            }
758

759
            nextPayload = constructResponseOfDevConf(NULL);
760
            if(nextPayload != NULL)
761
            {
762 763
                payload->next = nextPayload;
                payload = payload->next;
764
            }
765 766

            payload = head;
767 768 769 770 771 772 773
        }
    }

    return payload;
}


774
OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
775 776
{
    OCStackResult res = OC_STACK_ERROR;
777
    bool maskFlag = false;
778 779

    res = initProvResource(isSecured);
780
    if(res != OC_STACK_OK)
781 782
    {
        // TODO: destroy logic will be added
783 784
        OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));

785 786 787
        return res;
    }

788
    if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
789
    {
790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
        maskFlag = true;
        res = initWiFiResource(isSecured);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "initWiFiResource result: %s", getResult(res));
            return res;
        }

        res = OCBindResource(gProvResource.handle, gWiFiResource.handle);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Bind WiFiResource result: %s", getResult(res));
            return res;
        }

805 806
    }

807
    if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
808
    {
809 810 811 812 813 814 815 816 817 818 819 820 821 822
        maskFlag = true;
        res = initCloudServerResource(isSecured);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "initCloudResource result: %s", getResult(res));
            return res;
        }

        res = OCBindResource(gProvResource.handle, gCloudResource.handle);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Bind CloudResource result: %s", getResult(res));
            return res;
        }
823 824
    }

825
    if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
826
    {
827 828 829 830 831 832 833 834 835 836 837 838 839 840
        maskFlag = true;
        res = initDevConfResource(isSecured);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "initDevConf result: %s", getResult(res));
            return res;
        }

        res = OCBindResource(gProvResource.handle, gDevConfResource.handle);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Bind DevConfResource result: %s", getResult(res));
            return res;
        }
841 842 843
    }


844 845 846 847 848 849
    if(maskFlag == false)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
        return OC_STACK_ERROR;

    }
850

851
    OIC_LOG_V(INFO, ES_RH_TAG, "Created all resources with result: %s", getResult(res));
852

853 854 855
    return res;
}

856 857 858 859 860
OCStackResult DeleteProvisioningResource()
{
    OCStackResult res = OCDeleteResource(gProvResource.handle);
    if (res != OC_STACK_OK)
    {
861
        OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
862 863 864 865 866
    }

    return res;
}

867 868
OCStackResult DeleteEasySetupResources()
{
869 870
    OCStackResult res = OC_STACK_ERROR;
    if (gWiFiResource.handle != NULL)
871
    {
872 873 874 875 876
        res = OCUnBindResource(gProvResource.handle, gWiFiResource.handle);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind WiFi resource error with result: %s", getResult(res));
        }
877
    }
878
    if (gCloudResource.handle != NULL)
879
    {
880 881 882 883 884
        res = OCUnBindResource(gProvResource.handle, gCloudResource.handle);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind CloudServer resource error with result: %s", getResult(res));
        }
885
    }
886
    if (gDevConfResource.handle != NULL)
887
    {
888 889 890 891 892
        res = OCUnBindResource(gProvResource.handle, gDevConfResource.handle);
        if(res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Unbind DevConf resource error with result: %s", getResult(res));
        }
893
    }
894

895
    if (gWiFiResource.handle != NULL)
896
    {
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
        res = OCDeleteResource(gWiFiResource.handle);
        if (res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
        }
    }

    if(gCloudResource.handle != NULL)
    {
        res = OCDeleteResource(gCloudResource.handle);
        if (res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
        }
    }

    if(gDevConfResource.handle != NULL)
    {
        res = OCDeleteResource(gDevConfResource.handle);
        if (res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
        }
    }

    if(gProvResource.handle != NULL)
    {
        res = OCDeleteResource(gProvResource.handle);
        if (res != OC_STACK_OK)
        {
            OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
        }
929 930 931 932 933
    }

    return res;
}

934
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
935 936 937 938
{
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (!ehRequest)
    {
939
        OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
940 941 942 943
        return ehResult;
    }
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
944
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
945 946 947
        return ehResult;
    }

948 949 950
    OCRepPayload *getResp = NULL;

    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
951
    {
952
        getResp = constructResponseOfProv(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
953
    }
954
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
955
    {
956
        getResp = constructResponseOfWiFi(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
957
    }
958
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
959
    {
960
        getResp = constructResponseOfCloud(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
961
    }
962
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
963
    {
964
        getResp = constructResponseOfDevConf(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
965
    }
966

967 968
    if (!getResp)
    {
969
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
970 971 972 973 974 975 976 977 978
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

979
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
980
{
981
    OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
982 983 984
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
985
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
986 987 988 989 990 991
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
992
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
993 994 995
        return ehResult;
    }

996
    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
997
    {
998
        updateProvResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
999
    }
1000
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1001
    {
1002
        updateWiFiResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
1003
    }
1004
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1005
    {
1006
        updateCloudResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
1007
    }
1008
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1009
    {
1010
        updateDevConfResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
1011
    }
1012

1013 1014
    OCRepPayload *getResp = NULL;
    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1015
    {
1016
        getResp = constructResponseOfProv(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
1017
    }
1018
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1019
    {
1020
        getResp = constructResponseOfWiFi(NULL);
Jihun Ha's avatar
Jihun Ha committed
1021
    }
1022
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1023
    {
1024
        getResp = constructResponseOfCloud(NULL);
Jihun Ha's avatar
Jihun Ha committed
1025
    }
1026
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1027
    {
1028
        getResp = constructResponseOfDevConf(NULL);
Jihun Ha's avatar
Jihun Ha committed
1029
    }
1030

1031 1032
    if (!getResp)
    {
1033
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
1034 1035 1036 1037 1038 1039 1040 1041 1042
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

1043 1044
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
        OCRepPayload** payload)
1045
{
1046 1047
    (void) ehRequest;
    (void) payload;