resourcehandler.c 31.7 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(OCRepPayload* input);
void updateCloudResource(OCRepPayload* input);
void updateDevConfResource(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 67 68 69 70 71 72 73 74 75 76 77 78
ESReadUserdataCb gReadUserdataCb = NULL;
ESWriteUserdataCb gWriteUserdataCb = NULL;

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;
}

79
void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
80
{
81 82 83 84 85 86 87 88 89 90 91
    gWifiRsrcEvtCb = cb;
}

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

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
92 93
}

94 95
void UnRegisterResourceEventCallBack()
{
96
    if (gWifiRsrcEvtCb)
97
    {
98 99 100 101 102 103 104 105 106
        gWifiRsrcEvtCb = NULL;
    }
    if (gCloudRsrcEvtCb)
    {
        gCloudRsrcEvtCb = NULL;
    }
    if (gDevConfRsrcEvtCb)
    {
        gDevConfRsrcEvtCb = NULL;
107 108 109
    }
}

110 111 112 113
void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
{
    if (name != NULL && pass != NULL)
    {
114 115
        OICStrcpy(name, MAX_SSIDLEN, gWiFiResource.ssid);
        OICStrcpy(pass, MAX_CREDLEN, gWiFiResource.cred);
116 117 118
    }
}

119
OCStackResult initProvResource(bool isSecured)
120
{
121 122
    gProvResource.status = ES_STATE_INIT;
    gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
123
    OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
124

125 126 127
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
128
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
129 130 131
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
132
    }else
133
    {
134
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
135 136 137
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
138
    }
139 140 141 142 143 144 145 146 147
    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
148
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
149 150 151 152 153
        return res;
    }
    res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
    if(res)
    {
Jihun Ha's avatar
Jihun Ha committed
154
        OIC_LOG_V(INFO, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
155 156
        return res;
    }
157

158
    OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
159 160
    return res;
}
161

162 163 164 165
OCStackResult initWiFiResource(bool isSecured)
{
    OCStackResult res = OC_STACK_ERROR;

166 167 168 169 170
    gWiFiResource.supportedFreq = WIFI_BOTH;
    gWiFiResource.supportedMode[0] = WIFI_11A;
    gWiFiResource.supportedMode[1] = WIFI_11B;
    gWiFiResource.supportedMode[2] = WIFI_11G;
    gWiFiResource.supportedMode[3] = WIFI_11N;
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 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 243 244 245 246 247 248 249
    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), "");
    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;

}

250
void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
251
{
252
    OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
253 254 255 256

    if(ehRequest->query)
    {
        if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
257 258 259 260 261
        {
        // When Provisioning resource has a POST with BatchInterface
            updateCloudResource(input);
            updateWiFiResource(input);
            updateDevConfResource(input);
262 263
        }
    }
264 265
}

266
void updateWiFiResource(OCRepPayload* input)
267
{
268
    ESWiFiProvData* wiFiData = (ESWiFiProvData*)OICMalloc(sizeof(ESWiFiProvData));
269

270 271 272
    if(wiFiData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
273
        return ;
274
    }
275 276 277 278 279

    memset(wiFiData->ssid, 0, MAX_SSIDLEN);
    memset(wiFiData->pwd, 0, MAX_CREDLEN);
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
280 281
    wiFiData->userdata = NULL;

282
    char* ssid = NULL;
283 284 285
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
        OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
286
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
287
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
288 289
    }

290
    char* cred = NULL;
291 292 293
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
        OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
294
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
295 296 297
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
    }

298
    int64_t authType = -1;
299 300 301
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
        gWiFiResource.authType = authType;
302
        wiFiData->authtype = gWiFiResource.authType;
303 304 305
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
    }

306
    int64_t encType = -1;
307 308 309
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
        gWiFiResource.encType = encType;
310
        wiFiData->enctype = gWiFiResource.encType;
311 312
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
    }
313

314
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
315
    {
316
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFI, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
317
    }
318

319 320 321 322 323 324 325
    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)
        {
326
            gWifiRsrcEvtCb(ES_OK, wiFiData);
327 328 329 330 331 332 333
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
        }
    }

334
    OICFree(wiFiData);
335
}
336 337

void updateCloudResource(OCRepPayload* input)
338
{
339
    ESCloudProvData* cloudData = (ESCloudProvData*)OICMalloc(sizeof(ESCloudProvData));
340

341 342 343
    if(cloudData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
344
        return;
345
    }
346 347 348 349

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

352
    char *authCode = NULL;
353 354 355
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
        OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
356
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
357 358 359
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
    }

360
    char *authProvider = NULL;
361 362 363
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
        OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
364
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
365 366 367
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
    }

368
    char *ciServer = NULL;
369 370 371
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
        OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
372
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
373 374
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
    }
375

376
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
377
    {
378
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
379
    }
380

381 382 383 384 385 386 387
    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)
        {
388
            gCloudRsrcEvtCb(ES_OK, cloudData);
389 390 391 392 393 394
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
        }
    }
395 396

    OICFree(cloudData);
397 398
}

399
void updateDevConfResource(OCRepPayload* input)
400
{
401
    ESDevConfProvData* devConfData = (ESDevConfProvData*)OICMalloc(sizeof(ESDevConfProvData));
402

403 404 405
    if(devConfData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
406
        return;
407
    }
408 409
    memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
    memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
410 411
    devConfData->userdata = NULL;

412
    char *country = NULL;
413
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
414 415
    {
        OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
416
        OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
417 418 419
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
    }

420
    char *language = NULL;
421
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
422 423
    {
        OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
424
        OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
425 426
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
    }
427

428
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
429
    {
430
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
431
    }
432

433 434 435 436 437 438 439
    if(country || language)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");

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

    OICFree(devConfData);
449 450
}

451
OCRepPayload* constructResponseOfWiFi()
452 453 454 455 456 457 458 459 460 461 462 463
{
    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};
464
    int64_t *modes_64 = (int64_t *)OICMalloc(gWiFiResource.numMode * sizeof(int64_t));
465
    for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
466
    {
467
        modes_64[i] = gWiFiResource.supportedMode[i];
468
    }
469
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
470 471 472 473

    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
474 475
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
476

477
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
478
    {
479
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFI);
Jihun Ha's avatar
Jihun Ha committed
480
    }
481

482 483 484
    return payload;
}

485
OCRepPayload* constructResponseOfCloud()
486 487 488 489 490 491 492 493 494 495 496 497 498
{
    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);
499 500

    if(gWriteUserdataCb)
501
    {
502
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_CLOUDSERVER);
503
    }
504 505 506 507

    return payload;
}

508
OCRepPayload* constructResponseOfDevConf()
509 510 511 512 513 514 515 516 517 518 519 520 521 522
{
    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);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, gDevConfResource.language);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, gDevConfResource.country);

523
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
524
    {
525
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
Jihun Ha's avatar
Jihun Ha committed
526
    }
527

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
    return payload;
}

OCRepPayload* constructResponseOfProv(OCEntityHandlerRequest *ehRequest)
{
    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_PROV);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_PROVSTATUS, gProvResource.status);
543 544
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
545

546
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
547
    {
548
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_PROV);
Jihun Ha's avatar
Jihun Ha committed
549
    }
550

551 552 553 554
    if(ehRequest->query)
    {
        if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
        {// When Provisioning resource has a GET with BatchInterface
555 556
            OCRepPayload* head = payload;
            OCRepPayload* nextPayload = NULL;
557

558 559
            nextPayload = constructResponseOfWiFi();
            if(nextPayload != NULL)
560
            {
561 562
                payload->next = nextPayload;
                payload = payload->next;
563
            }
564

565 566
            nextPayload = constructResponseOfCloud();
            if(nextPayload != NULL)
567
            {
568 569
                payload->next = nextPayload;
                payload = payload->next;
570
            }
571 572 573

            nextPayload = constructResponseOfDevConf();
            if(nextPayload != NULL)
574
            {
575 576
                payload->next = nextPayload;
                payload = payload->next;
577
            }
578 579

            payload = head;
580 581 582 583 584 585 586
        }
    }

    return payload;
}


587
OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
588 589
{
    OCStackResult res = OC_STACK_ERROR;
590
    bool maskFlag = false;
591 592

    res = initProvResource(isSecured);
593
    if(res != OC_STACK_OK)
594 595
    {
        // TODO: destroy logic will be added
596 597
        OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));

598 599 600
        return res;
    }

601
    if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
602
    {
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
        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;
        }

618 619
    }

620
    if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
621
    {
622 623 624 625 626 627 628 629 630 631 632 633 634 635
        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;
        }
636 637
    }

638
    if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
639
    {
640 641 642 643 644 645 646 647 648 649 650 651 652 653
        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;
        }
654 655 656
    }


657 658 659 660 661 662
    if(maskFlag == false)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
        return OC_STACK_ERROR;

    }
663

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

666 667 668
    return res;
}

669 670 671 672 673
OCStackResult DeleteProvisioningResource()
{
    OCStackResult res = OCDeleteResource(gProvResource.handle);
    if (res != OC_STACK_OK)
    {
674
        OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
675 676 677 678 679
    }

    return res;
}

680 681
OCStackResult DeleteEasySetupResources()
{
682 683
    OCStackResult res = OC_STACK_ERROR;
    if (gWiFiResource.handle != NULL)
684
    {
685 686 687 688 689
        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));
        }
690
    }
691
    if (gCloudResource.handle != NULL)
692
    {
693 694 695 696 697
        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));
        }
698
    }
699
    if (gDevConfResource.handle != NULL)
700
    {
701 702 703 704 705
        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));
        }
706
    }
707

708
    if (gWiFiResource.handle != NULL)
709
    {
710 711 712 713 714 715 716 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
        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));
        }
742 743 744 745 746
    }

    return res;
}

747
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
748 749 750 751
{
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (!ehRequest)
    {
752
        OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
753 754 755 756
        return ehResult;
    }
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
757
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
758 759 760
        return ehResult;
    }

761 762 763
    OCRepPayload *getResp = NULL;

    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
764
    {
765
        getResp = constructResponseOfProv(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
766
    }
767
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
768
    {
769
        getResp = constructResponseOfWiFi();
Jihun Ha's avatar
Jihun Ha committed
770
    }
771
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
772
    {
773
        getResp = constructResponseOfCloud();
Jihun Ha's avatar
Jihun Ha committed
774
    }
775
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
776
    {
777
        getResp = constructResponseOfDevConf();
Jihun Ha's avatar
Jihun Ha committed
778
    }
779

780 781
    if (!getResp)
    {
782
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
783 784 785 786 787 788 789 790 791
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

792
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
793
{
794
    OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
795 796 797
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
798
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
799 800 801 802 803 804
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
805
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
806 807 808
        return ehResult;
    }

809
    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
810
    {
811
        updateProvResource(ehRequest, input);
Jihun Ha's avatar
Jihun Ha committed
812
    }
813
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
814
    {
815
        updateWiFiResource(input);
Jihun Ha's avatar
Jihun Ha committed
816
    }
817
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
818
    {
819
        updateCloudResource(input);
Jihun Ha's avatar
Jihun Ha committed
820
    }
821
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
822
    {
823
        updateDevConfResource(input);
Jihun Ha's avatar
Jihun Ha committed
824
    }
825

826 827
    OCRepPayload *getResp = NULL;
    if(ehRequest->resource == gProvResource.handle)
Jihun Ha's avatar
Jihun Ha committed
828
    {
829
        getResp = constructResponseOfProv(ehRequest);
Jihun Ha's avatar
Jihun Ha committed
830
    }
831
    else if(ehRequest->resource == gWiFiResource.handle)
Jihun Ha's avatar
Jihun Ha committed
832
    {
833
        getResp = constructResponseOfWiFi();
Jihun Ha's avatar
Jihun Ha committed
834
    }
835
    else if(ehRequest->resource == gCloudResource.handle)
Jihun Ha's avatar
Jihun Ha committed
836
    {
837
        getResp = constructResponseOfCloud();
Jihun Ha's avatar
Jihun Ha committed
838
    }
839
    else if(ehRequest->resource == gDevConfResource.handle)
Jihun Ha's avatar
Jihun Ha committed
840
    {
841
        getResp = constructResponseOfDevConf();
Jihun Ha's avatar
Jihun Ha committed
842
    }
843

844 845
    if (!getResp)
    {
846
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
847 848 849 850 851 852 853 854 855
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

856 857
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
        OCRepPayload** payload)
858
{
859 860
    (void) ehRequest;
    (void) payload;
861 862 863 864
    OCEntityHandlerResult ehResult = OC_EH_ERROR;

    return ehResult;
}
865 866 867 868
/**
 * This is the entity handler for the registered resource.
 * This is invoked by OCStack whenever it recevies a request for this resource.
 */
869 870 871 872 873
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
        OCEntityHandlerRequest* entityHandlerRequest, void *callback)
{
    (void) callback;
    OCEntityHandlerResult ehRet = OC_EH_OK;
874 875 876 877
    OCEntityHandlerResponse response =
    { 0, 0, OC_EH_ERROR, 0, 0,
    { },
    { 0 }, false };
878
    OCRepPayload* payload = NULL;
879

880 881 882 883
    if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
    {
        if (OC_REST_GET == entityHandlerRequest->method)
        {
884
            OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
885 886 887 888
            ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
        }
        else if (OC_REST_PUT == entityHandlerRequest->method)
        {
889
            OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
890

891
            //PUT request will be handled in the internal implementation
892
            if (gProvResource.handle != NULL)
893 894 895 896 897
            {
                ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
            }
            else
            {
898
                OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
899 900 901 902 903
                ehRet = OC_EH_ERROR;
            }
        }
        else if (OC_REST_POST == entityHandlerRequest->method)
        {
904
            OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
905
            if (gProvResource.handle != NULL)
906 907 908 909 910 911 912 913
            {
                ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
                ehRet = OC_EH_ERROR;
            }
914 915 916 917 918 919 920 921 922 923 924 925
        }

        if (ehRet == OC_EH_OK)
        {
            // Format the response.  Note this requires some info about the request
            response.requestHandle = entityHandlerRequest->requestHandle;
            response.resourceHandle = entityHandlerRequest->resource;
            response.ehResult = ehRet;
            //response uses OCPaylod while all get,put methodes use OCRepPayload
            response.payload = (OCPayload*) (payload);
            response.numSendVendorSpecificHeaderOptions = 0;
            memset(response.sendVendorSpecificHeaderOptions, 0,
926 927
                    sizeof(response.sendVendorSpecificHeaderOptions));
            memset(response.resourceUri, 0, sizeof(response.resourceUri));
928 929 930 931 932 933
            // Indicate that response is NOT in a persistent buffer
            response.persistentBufferFlag = 0;

            // Send the response
            if (OCDoResponse(&response) != OC_STACK_OK)
            {
934
                OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
935 936 937 938 939 940 941 942
                ehRet = OC_EH_ERROR;
            }
        }
    }

    return ehRet;
}

943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962
OCStackResult SetDeviceProperty(ESDeviceProperty *deviceProperty)
{
    OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty IN");

    gWiFiResource.supportedFreq = (deviceProperty->WiFi).freq;
    OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Freq : %d", gWiFiResource.supportedFreq);

    int modeIdx = 0;
    while((deviceProperty->WiFi).mode[modeIdx] != WiFi_EOF)
    {
        gWiFiResource.supportedMode[modeIdx] = (deviceProperty->WiFi).mode[modeIdx];
        OIC_LOG_V(INFO, ES_RH_TAG, "WiFi Mode : %d", gWiFiResource.supportedMode[modeIdx]);
        modeIdx ++;
    }
    gWiFiResource.numMode = modeIdx;

    OICStrcpy(gDevConfResource.devName, MAX_DEVICELEN, (deviceProperty->DevConf).deviceName);
    OIC_LOG_V(INFO, ES_RH_TAG, "Device Name : %s", gDevConfResource.devName);

    OIC_LOG(INFO, ES_RH_TAG, "SetDeviceProperty OUT");
963 964 965 966 967 968 969 970 971 972 973 974
    return OC_STACK_OK;
}

OCStackResult SetEnrolleeState(ESEnrolleeState esState)
{
    OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState IN");

    gProvResource.status = esState;
    OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee Status : %d", gProvResource.status);

    OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeState OUT");
    return OC_STACK_OK;
975 976
}

977 978 979 980 981 982 983 984 985 986
OCStackResult SetEnrolleeErrCode(ESErrorCode esErrCode)
{
    OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode IN");

    gProvResource.lastErrCode = esErrCode;
    OIC_LOG_V(INFO, ES_RH_TAG, "Enrollee ErrorCode : %d", gProvResource.lastErrCode);

    OIC_LOG(INFO, ES_RH_TAG, "SetEnrolleeErrCode OUT");
    return OC_STACK_OK;
}
Jihun Ha's avatar
Jihun Ha committed
987

988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
const char *getResult(OCStackResult result)
{
    switch (result)
    {
        case OC_STACK_OK:
            return "OC_STACK_OK";
        case OC_STACK_INVALID_URI:
            return "OC_STACK_INVALID_URI";
        case OC_STACK_INVALID_QUERY:
            return "OC_STACK_INVALID_QUERY";
        case OC_STACK_INVALID_IP:
            return "OC_STACK_INVALID_IP";
        case OC_STACK_INVALID_PORT:
            return "OC_STACK_INVALID_PORT";
        case OC_STACK_INVALID_CALLBACK:
            return "OC_STACK_INVALID_CALLBACK";
        case OC_STACK_INVALID_METHOD:
            return "OC_STACK_INVALID_METHOD";
        case OC_STACK_NO_MEMORY:
            return "OC_STACK_NO_MEMORY";
        case OC_STACK_COMM_ERROR:
            return "OC_STACK_COMM_ERROR";
        case OC_STACK_INVALID_PARAM:
            return "OC_STACK_INVALID_PARAM";
        case OC_STACK_NOTIMPL:
            return "OC_STACK_NOTIMPL";
        case OC_STACK_NO_RESOURCE:
            return "OC_STACK_NO_RESOURCE";
        case OC_STACK_RESOURCE_ERROR:
            return "OC_STACK_RESOURCE_ERROR";
        case OC_STACK_SLOW_RESOURCE:
            return "OC_STACK_SLOW_RESOURCE";
        case OC_STACK_NO_OBSERVERS:
            return "OC_STACK_NO_OBSERVERS";
        case OC_STACK_ERROR:
            return "OC_STACK_ERROR";
        default:
            return "UNKNOWN";
    }
1027
}