resourcehandler.c 39.8 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
bool CompareResourceInterface(char *from, char *iface)
{
    char *str = OICStrdup(from);
    char *ptr = strtok(str, ";");

72 73 74
    if(ptr == NULL)
        return false;

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    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;
}

94 95 96 97 98 99 100 101 102 103 104 105
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;
}

106
void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
107
{
108 109 110 111 112 113 114 115 116 117 118
    gWifiRsrcEvtCb = cb;
}

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

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
119 120
}

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

137
OCStackResult initProvResource(bool isSecured)
138
{
139 140
    gProvResource.status = ES_STATE_INIT;
    gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
141
    OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
142

143 144 145
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
146
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
147 148 149
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
150
    }else
151
    {
152
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
153 154 155
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
156
    }
157 158 159 160 161 162 163 164 165
    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
166
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
167 168 169 170 171
        return res;
    }
    res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
    if(res)
    {
Jihun Ha's avatar
Jihun Ha committed
172
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
173 174
        return res;
    }
175

176
    OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
177 178
    return res;
}
179

180 181 182 183
OCStackResult initWiFiResource(bool isSecured)
{
    OCStackResult res = OC_STACK_ERROR;

184 185 186 187 188
    gWiFiResource.supportedFreq = WIFI_BOTH;
    gWiFiResource.supportedMode[0] = WIFI_11A;
    gWiFiResource.supportedMode[1] = WIFI_11B;
    gWiFiResource.supportedMode[2] = WIFI_11G;
    gWiFiResource.supportedMode[3] = WIFI_11N;
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 243 244 245
    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), "");
246 247
    OICStrcpy(gDevConfResource.modelNumber, sizeof(gDevConfResource.modelNumber), "");
    OICStrcpy(gDevConfResource.location, sizeof(gDevConfResource.location), "");
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    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;

}

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

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

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

297
    ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
298

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

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

311
    char* ssid = NULL;
312 313 314
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
        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);
317 318
    }

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

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

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

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

348 349 350 351 352 353 354
    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)
        {
355
            gWifiRsrcEvtCb(ES_OK, wiFiData);
356 357 358 359 360 361 362
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
        }
    }

363
    OICFree(wiFiData);
364
}
365

366
void updateCloudResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
367
{
368 369 370
    if(ehRequest &&
        strcmp(ehRequest->query, "") &&
        !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
371 372 373 374 375 376
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return ;
    }

377
    ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
378

379 380 381
    if(cloudData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
382
        return;
383
    }
384 385 386 387

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

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

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

406
    char *ciServer = NULL;
407 408 409
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
        OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
410
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
411 412
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
    }
413

414
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
415
    {
416
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
417
    }
418

419 420 421 422 423 424 425
    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)
        {
426
            gCloudRsrcEvtCb(ES_OK, cloudData);
427 428 429 430 431 432
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
        }
    }
433 434

    OICFree(cloudData);
435 436
}

437
void updateDevConfResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
438
{
439 440 441
    if(ehRequest &&
        strcmp(ehRequest->query, "") &&
        !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
442 443 444 445 446 447
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return ;
    }

448
    ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
449

450 451 452
    if(devConfData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
453
        return;
454
    }
455 456
    memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
    memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
457 458
    devConfData->userdata = NULL;

459 460 461 462 463 464 465 466
    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);
    }

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

475
    char *language = NULL;
476
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
477 478
    {
        OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
479
        OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
480 481
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
    }
482

483
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
484
    {
485
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
486
    }
487

488 489 490 491 492 493 494
    if(country || language)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");

        // TODO : Need to check appropriateness of gDevConfData
        if(gDevConfRsrcEvtCb != NULL)
        {
495
            gDevConfRsrcEvtCb(ES_OK, devConfData);
496 497 498 499 500 501
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
        }
    }
502 503

    OICFree(devConfData);
504 505
}

506
OCRepPayload* constructResponseOfWiFi(OCEntityHandlerRequest *ehRequest)
507
{
508 509 510
    if(ehRequest &&
        strcmp(ehRequest->query, "") &&
        !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
511 512 513 514 515 516
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return NULL;
    }

517 518 519 520 521 522 523 524
    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");
525
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_WIFI);
526 527

    size_t dimensions[MAX_REP_ARRAY_DEPTH] = {gWiFiResource.numMode, 0, 0};
528
    int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
529
    for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
530
    {
531
        modes_64[i] = gWiFiResource.supportedMode[i];
532
    }
533
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
534 535 536 537

    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
538 539
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
540

541
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
542
    {
543
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
Jihun Ha's avatar
Jihun Ha committed
544
    }
545

546 547 548
    return payload;
}

549
OCRepPayload* constructResponseOfCloud(OCEntityHandlerRequest *ehRequest)
550
{
551 552 553
    if(ehRequest &&
        strcmp(ehRequest->query, "") &&
        !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
554 555 556 557 558 559
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return NULL;
    }

560 561 562 563 564 565 566 567
    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");
568
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_CLOUDSERVER);
569 570 571
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, gCloudResource.authCode);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, gCloudResource.authProvider);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, gCloudResource.ciServer);
572 573

    if(gWriteUserdataCb)
574
    {
575
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
576
    }
577 578 579 580

    return payload;
}

581
OCRepPayload* constructResponseOfDevConf(OCEntityHandlerRequest *ehRequest)
582
{
583 584 585
    if(ehRequest &&
        strcmp(ehRequest->query, "") &&
        !CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
586 587 588 589 590 591
    {
        // In case of link list, batch interface
        OIC_LOG(ERROR, ES_RH_TAG, "Not supported Interface");
        return NULL;
    }

592 593 594 595 596 597 598 599
    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");
600
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_DEVCONF);
601
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, gDevConfResource.devName);
602 603
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, gDevConfResource.modelNumber);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, gDevConfResource.location);
604 605 606
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);

607
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
608
    {
609
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
Jihun Ha's avatar
Jihun Ha committed
610
    }
611

612 613 614 615 616 617 618 619 620 621 622 623
    return payload;
}

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

624 625 626 627 628 629 630 631 632 633 634 635
    // 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)
            {
636
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
637 638 639 640 641 642 643 644 645 646 647
                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)
            {
648
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
                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)
            {
670
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
671 672 673 674 675 676 677 678 679 680 681
                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)
            {
682
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
                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)
            {
704
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
705 706 707 708 709 710 711 712 713 714 715
                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)
            {
716
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
                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");
742
        OCRepPayloadSetPropString(payload, OC_RSRVD_ES_HREF, OC_RSRVD_ES_URI_PROV);
743 744 745 746
        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);
    }
747

748
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
749
    {
750
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
Jihun Ha's avatar
Jihun Ha committed
751
    }
752

753 754
    if(ehRequest->query)
    {
755
        if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
756
        {// When Provisioning resource has a GET with BatchInterface
757 758
            OCRepPayload* head = payload;
            OCRepPayload* nextPayload = NULL;
759

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

767
            nextPayload = constructResponseOfCloud(NULL);
768
            if(nextPayload != NULL)
769
            {
770 771
                payload->next = nextPayload;
                payload = payload->next;
772
            }
773

774
            nextPayload = constructResponseOfDevConf(NULL);
775
            if(nextPayload != NULL)
776
            {
777 778
                payload->next = nextPayload;
                payload = payload->next;
779
            }
780 781

            payload = head;
782 783 784 785 786 787 788
        }
    }

    return payload;
}


789
OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
790 791
{
    OCStackResult res = OC_STACK_ERROR;
792
    bool maskFlag = false;
793 794

    res = initProvResource(isSecured);
795
    if(res != OC_STACK_OK)
796 797
    {
        // TODO: destroy logic will be added
798 799
        OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));

800 801 802
        return res;
    }

803
    if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
804
    {
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
        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;
        }

820 821
    }

822
    if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
823
    {
824 825 826 827 828 829 830 831 832 833 834 835 836 837
        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;
        }
838 839
    }

840
    if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
841
    {
842 843 844 845 846 847 848 849 850 851 852 853 854 855
        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;
        }
856 857 858
    }


859 860 861 862 863 864
    if(maskFlag == false)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
        return OC_STACK_ERROR;

    }
865

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

868 869 870
    return res;
}

871 872 873 874 875
OCStackResult DeleteProvisioningResource()
{
    OCStackResult res = OCDeleteResource(gProvResource.handle);
    if (res != OC_STACK_OK)
    {
876
        OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
877 878 879 880 881
    }

    return res;
}

882 883
OCStackResult DeleteEasySetupResources()
{
884 885
    OCStackResult res = OC_STACK_ERROR;
    if (gWiFiResource.handle != NULL)
886
    {
887 888 889 890 891
        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));
        }
892
    }
893
    if (gCloudResource.handle != NULL)
894
    {
895 896 897 898 899
        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));
        }
900
    }
901
    if (gDevConfResource.handle != NULL)
902
    {
903 904 905 906 907
        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));
        }
908
    }
909

910
    if (gWiFiResource.handle != NULL)
911
    {
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
        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));
        }
944 945 946 947 948
    }

    return res;
}

949
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
950 951 952 953
{
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (!ehRequest)
    {
954
        OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
955 956 957 958
        return ehResult;
    }
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
959
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
960 961 962
        return ehResult;
    }

963 964 965
    OCRepPayload *getResp = NULL;

    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
966
    {
967
        getResp = constructResponseOfProv(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
968
    }
969
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
970
    {
971
        getResp = constructResponseOfWiFi(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
972
    }
973
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
974
    {
975
        getResp = constructResponseOfCloud(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
976
    }
977
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
978
    {
979
        getResp = constructResponseOfDevConf(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
980
    }
981

982 983
    if (!getResp)
    {
984
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
985 986 987 988 989 990 991 992 993
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

994
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
995
{
996
    OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
997 998 999
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
1000
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
1001 1002 1003 1004 1005 1006
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
1007
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
1008 1009 1010
        return ehResult;
    }

1011
    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1012
    {
1013
        updateProvResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
1014
    }
1015
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1016
    {
1017
        updateWiFiResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
1018
    }
1019
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1020
    {
1021
        updateCloudResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
1022
    }
1023
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
1024
    {
1025
        updateDevConfResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
1026
    }