resourcehandler.c 28.4 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 25 26
#include <stdio.h>

#include "ocpayload.h"
#include "oic_string.h"
27

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

/**
38
 * @var gProvResource
39 40
 * @brief Structure for holding the Provisioning status and target information required to
 * connect to the target network
41
 */
42
static ProvResource gProvResource;
43 44 45
static WiFiResource gWiFiResource;
static CloudResource gCloudResource;
static DevConfResource gDevConfResource;
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/**
 * @var gWiFiData
 * @brief Structure for holding the target information required to
 * connect to the target network
 */
 static ESWiFiProvData gWiFiData;

/**
 * @var gDevConfData
 * @brief Structure for holding the device information
 */
 static ESDevConfProvData gDevConfData;

/**
 * @var gCloudData
 * @brief Structure for holding the cloud information required to
 * connect to CI Server
 */
 static ESCloudProvData gCloudData;



69 70 71
//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
72 73
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
        void *callback);
74
const char *getResult(OCStackResult result);
75 76 77
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
78
void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
79 80 81
void updateWiFiResource(OCRepPayload* input);
void updateCloudResource(OCRepPayload* input);
void updateDevConfResource(OCRepPayload* input);
82

83 84 85
ESWiFiCB gWifiRsrcEvtCb = NULL;
ESCloudCB gCloudRsrcEvtCb = NULL;
ESDevConfCB gDevConfRsrcEvtCb = NULL;
86

87
void RegisterWifiRsrcEventCallBack(ESWiFiCB cb)
88
{
89 90 91 92 93 94 95 96 97 98 99
    gWifiRsrcEvtCb = cb;
}

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

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
100 101
}

102 103
void UnRegisterResourceEventCallBack()
{
104
    if (gWifiRsrcEvtCb)
105
    {
106 107 108 109 110 111 112 113 114
        gWifiRsrcEvtCb = NULL;
    }
    if (gCloudRsrcEvtCb)
    {
        gCloudRsrcEvtCb = NULL;
    }
    if (gDevConfRsrcEvtCb)
    {
        gDevConfRsrcEvtCb = NULL;
115 116 117
    }
}

118 119 120 121
void GetTargetNetworkInfoFromProvResource(char *name, char *pass)
{
    if (name != NULL && pass != NULL)
    {
122 123
        OICStrcpy(name, MAX_SSIDLEN, gWiFiResource.ssid);
        OICStrcpy(pass, MAX_CREDLEN, gWiFiResource.cred);
124 125 126
    }
}

127
OCStackResult initProvResource(bool isSecured)
128
{
129 130
    gProvResource.status = ES_STATE_INIT;
    gProvResource.lastErrCode = ES_ERRCODE_NO_ERROR;
131
    OICStrcpy(gProvResource.ocfWebLinks, MAX_WEBLINKLEN, "");
132

133 134 135
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
136
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
137 138 139
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
140
    }else
141
    {
142
        res = OCCreateResource(&gProvResource.handle, OC_RSRVD_ES_RES_TYPE_PROV,
143 144 145
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_PROV, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
146
    }
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    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)
    {
        OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
        return res;
    }
    res = OCBindResourceInterfaceToResource(gProvResource.handle, OC_RSRVD_INTERFACE_BATCH);
    if(res)
    {
        OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
        return res;
    }
165

166
    OIC_LOG_V(INFO, ES_RH_TAG, "Created Prov resource with result: %s", getResult(res));
167 168
    return res;
}
169

170 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 250 251 252 253 254 255 256 257
OCStackResult initWiFiResource(bool isSecured)
{
    OCStackResult res = OC_STACK_ERROR;

    gWiFiResource.supportedFreq = WiFi_BOTH;
    gWiFiResource.supportedMode[0] = WiFi_11A;
    gWiFiResource.supportedMode[1] = WiFi_11B;
    gWiFiResource.supportedMode[2] = WiFi_11G;
    gWiFiResource.supportedMode[3] = WiFi_11N;
    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;

}

258
void updateProvResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
259
{
260
    OIC_LOG_V(INFO, ES_RH_TAG, "gProvResource.status %d", gProvResource.status);
261 262 263 264

    if(ehRequest->query)
    {
        if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
265 266 267 268 269
        {
        // When Provisioning resource has a POST with BatchInterface
            updateCloudResource(input);
            updateWiFiResource(input);
            updateDevConfResource(input);
270 271
        }
    }
272 273
}

274
void updateWiFiResource(OCRepPayload* input)
275
{
276
    char* ssid = NULL;
277 278 279
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
        OICStrcpy(gWiFiResource.ssid, sizeof(gWiFiResource.ssid), ssid);
280 281
        OICStrcpy(gWiFiData.ssid, sizeof(gWiFiData.ssid), ssid);
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.ssid : %s", gWiFiResource.ssid);
282 283
    }

284
    char* cred = NULL;
285 286 287
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
        OICStrcpy(gWiFiResource.cred, sizeof(gWiFiResource.cred), cred);
288
        OICStrcpy(gWiFiData.pwd, sizeof(gWiFiData.pwd), cred);
289 290 291
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.cred %s", gWiFiResource.cred);
    }

292
    int64_t authType = -1;
293 294 295
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
        gWiFiResource.authType = authType;
296
        gWiFiData.authtype = gWiFiResource.authType;
297 298 299
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.authType %u", gWiFiResource.authType);
    }

300
    int64_t encType = -1;
301 302 303
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
        gWiFiResource.encType = encType;
304
        gWiFiData.enctype = gWiFiResource.encType;
305 306
        OIC_LOG_V(INFO, ES_RH_TAG, "gWiFiResource.encType %u", gWiFiResource.encType);
    }
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

    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)
        {
            gWifiRsrcEvtCb(ES_OK, &gWiFiData);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiRsrcEvtCb is NULL");
        }
    }

323
}
324 325

void updateCloudResource(OCRepPayload* input)
326
{
327
    char *authCode = NULL;
328 329 330
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
        OICStrcpy(gCloudResource.authCode, sizeof(gCloudResource.authCode), authCode);
331
        OICStrcpy(gCloudData.authCode, sizeof(gCloudData.authCode), authCode);
332 333 334
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authCode %s", gCloudResource.authCode);
    }

335
    char *authProvider = NULL;
336 337 338
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
        OICStrcpy(gCloudResource.authProvider, sizeof(gCloudResource.authProvider), authProvider);
339
        OICStrcpy(gCloudData.authProvider, sizeof(gCloudData.authProvider), authProvider);
340 341 342
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.authServerUrl %s", gCloudResource.authProvider);
    }

343
    char *ciServer = NULL;
344 345 346
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
        OICStrcpy(gCloudResource.ciServer, sizeof(gCloudResource.ciServer), ciServer);
347
        OICStrcpy(gCloudData.ciServer, sizeof(gCloudData.ciServer), ciServer);
348 349
        OIC_LOG_V(INFO, ES_RH_TAG, "gCloudResource.ciServer %s", gCloudResource.ciServer);
    }
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

    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)
        {
            gCloudRsrcEvtCb(ES_OK, &gCloudData);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gCloudRsrcEvtCb is NULL");
        }
    }
365 366
}

367
void updateDevConfResource(OCRepPayload* input)
368
{
369
    char *country = NULL;
370
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
371 372
    {
        OICStrcpy(gDevConfResource.country, sizeof(gDevConfResource.country), country);
373
        OICStrcpy(gDevConfData.country, sizeof(gDevConfData.country), country);
374 375 376
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.country %s", gDevConfResource.country);
    }

377
    char *language = NULL;
378
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
379 380
    {
        OICStrcpy(gDevConfResource.language, sizeof(gDevConfResource.language), language);
381
        OICStrcpy(gDevConfData.language, sizeof(gDevConfData.language), language);
382 383
        OIC_LOG_V(INFO, ES_RH_TAG, "gDevConfResource.language %s", gDevConfResource.language);
    }
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

    if(country || language)
    {
        OIC_LOG(INFO, ES_RH_TAG, "Send DevConfRsrc Callback To ES");

        // TODO : Need to check appropriateness of gDevConfData
        if(gDevConfRsrcEvtCb != NULL)
        {
            gDevConfRsrcEvtCb(ES_OK, &gDevConfData);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
        }
    }
399 400
}

401
OCRepPayload* constructResponseOfWiFi()
402 403 404 405 406 407 408 409 410 411 412 413
{
    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};
414 415 416 417
    int64_t *modes_64 = (int64_t *)malloc(gWiFiResource.numMode * sizeof(int64_t));
    for(int i = 0 ; i < gWiFiResource.numMode ; ++i)
        modes_64[i] = gWiFiResource.supportedMode[i];
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
418 419 420 421

    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, gWiFiResource.supportedFreq);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, gWiFiResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, gWiFiResource.cred);
422 423
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) gWiFiResource.authType);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) gWiFiResource.encType);
424 425 426 427

    return payload;
}

428
OCRepPayload* constructResponseOfCloud()
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
{
    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);

    return payload;
}

446
OCRepPayload* constructResponseOfDevConf()
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
{
    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);

    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);
476 477
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_LAST_ERRORCODE, gProvResource.lastErrCode);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LINKS, gProvResource.ocfWebLinks);
478 479 480 481 482

    if(ehRequest->query)
    {
        if(strstr(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
        {// When Provisioning resource has a GET with BatchInterface
483
            payload->next = constructResponseOfWiFi();
484 485

            if(payload->next)
486
                payload->next->next = constructResponseOfCloud();
487 488 489 490
            else
                return payload;

            if(payload->next->next)
491
                payload->next->next->next = constructResponseOfDevConf();
492 493 494 495 496 497 498 499 500
            else
                return payload;
        }
    }

    return payload;
}


501
OCStackResult CreateEasySetupResources(bool isSecured, ESResourceMask resourceMask)
502 503
{
    OCStackResult res = OC_STACK_ERROR;
504
    bool maskFlag = false;
505 506

    res = initProvResource(isSecured);
507
    if(res != OC_STACK_OK)
508 509
    {
        // TODO: destroy logic will be added
510 511
        OIC_LOG_V(ERROR, ES_RH_TAG, "initProvResource result: %s", getResult(res));

512 513 514
        return res;
    }

515
    if((resourceMask & ES_WIFI_RESOURCE) == ES_WIFI_RESOURCE)
516
    {
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
        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;
        }

532 533
    }

534
    if((resourceMask & ES_CLOUD_RESOURCE) == ES_CLOUD_RESOURCE)
535
    {
536 537 538 539 540 541 542 543 544 545 546 547 548 549
        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;
        }
550 551
    }

552
    if((resourceMask & ES_DEVCONF_RESOURCE) == ES_DEVCONF_RESOURCE)
553
    {
554 555 556 557 558 559 560 561 562 563 564 565 566 567
        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;
        }
568 569 570
    }


571 572 573 574 575 576
    if(maskFlag == false)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Invalid ResourceMask");
        return OC_STACK_ERROR;

    }
577

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

580 581 582
    return res;
}

583 584 585 586 587
OCStackResult DeleteProvisioningResource()
{
    OCStackResult res = OCDeleteResource(gProvResource.handle);
    if (res != OC_STACK_OK)
    {
588
        OIC_LOG_V(INFO, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
589 590 591 592 593
    }

    return res;
}

594 595
OCStackResult DeleteEasySetupResources()
{
596
    OCStackResult res = OCDeleteResource(gWiFiResource.handle);
597 598
    if (res != OC_STACK_OK)
    {
599
        OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting WiFi resource error with result: %s", getResult(res));
600
    }
601
    res = OCDeleteResource(gCloudResource.handle);
602 603
    if (res != OC_STACK_OK)
    {
604
        OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting CloudServer resource error with result: %s", getResult(res));
605
    }
606
    res = OCDeleteResource(gDevConfResource.handle);
607 608
    if (res != OC_STACK_OK)
    {
609
        OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting DevConf resource error with result: %s", getResult(res));
610
    }
611 612

    res = OCDeleteResource(gProvResource.handle);
613 614
    if (res != OC_STACK_OK)
    {
615
        OIC_LOG_V(ERROR, ES_RH_TAG, "Deleting Prov resource error with result: %s", getResult(res));
616 617 618 619 620
    }

    return res;
}

621
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload **payload)
622 623 624 625
{
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (!ehRequest)
    {
626
        OIC_LOG(ERROR, ES_RH_TAG, "Request is Null");
627 628 629 630
        return ehResult;
    }
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
631
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
632 633 634
        return ehResult;
    }

635 636 637 638 639
    OCRepPayload *getResp = NULL;

    if(ehRequest->resource == gProvResource.handle)
        getResp = constructResponseOfProv(ehRequest);
    else if(ehRequest->resource == gWiFiResource.handle)
640
        getResp = constructResponseOfWiFi();
641
    else if(ehRequest->resource == gCloudResource.handle)
642
        getResp = constructResponseOfCloud();
643
    else if(ehRequest->resource == gDevConfResource.handle)
644
        getResp = constructResponseOfDevConf();
645

646 647
    if (!getResp)
    {
648
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
649 650 651 652 653 654 655 656 657
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

658
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload)
659
{
660
    OIC_LOG(INFO, ES_RH_TAG, "ProcessPostRequest enter");
661 662 663
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
    if (ehRequest->payload && ehRequest->payload->type != PAYLOAD_TYPE_REPRESENTATION)
    {
664
        OIC_LOG(ERROR, ES_RH_TAG, "Incoming payload not a representation");
665 666 667 668 669 670
        return ehResult;
    }

    OCRepPayload* input = (OCRepPayload*) (ehRequest->payload);
    if (!input)
    {
671
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to parse");
672 673 674
        return ehResult;
    }

675 676 677 678
    // TBD : Discuss about triggering flag (to be existed or not)
    // ES_PS_PROVISIONING_COMPLETED state indicates that already provisioning is completed.
    // A new request for provisioning means overriding existing network provisioning information.  

679
    if(ehRequest->resource == gProvResource.handle)
680
        updateProvResource(ehRequest, input);
681
    else if(ehRequest->resource == gWiFiResource.handle)
682
        updateWiFiResource(input);
683
    else if(ehRequest->resource == gCloudResource.handle)
684
        updateCloudResource(input);
685
    else if(ehRequest->resource == gDevConfResource.handle)
686
        updateDevConfResource(input);      
687

688 689 690 691
    OCRepPayload *getResp = NULL;
    if(ehRequest->resource == gProvResource.handle)
        getResp = constructResponseOfProv(ehRequest);
    else if(ehRequest->resource == gWiFiResource.handle)
692
        getResp = constructResponseOfWiFi();
693
    else if(ehRequest->resource == gCloudResource.handle)
694
        getResp = constructResponseOfCloud();
695
    else if(ehRequest->resource == gDevConfResource.handle)
696
        getResp = constructResponseOfDevConf();
697

698 699
    if (!getResp)
    {
700
        OIC_LOG(ERROR, ES_RH_TAG, "constructResponse failed");
701 702 703 704 705 706 707 708 709
        return OC_EH_ERROR;
    }

    *payload = getResp;
    ehResult = OC_EH_OK;

    return ehResult;
}

710 711
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest * ehRequest,
        OCRepPayload** payload)
712
{
713 714
    (void) ehRequest;
    (void) payload;
715 716 717 718
    OCEntityHandlerResult ehResult = OC_EH_ERROR;

    return ehResult;
}
719 720 721 722
/**
 * This is the entity handler for the registered resource.
 * This is invoked by OCStack whenever it recevies a request for this resource.
 */
723 724 725 726 727
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
        OCEntityHandlerRequest* entityHandlerRequest, void *callback)
{
    (void) callback;
    OCEntityHandlerResult ehRet = OC_EH_OK;
728 729 730 731
    OCEntityHandlerResponse response =
    { 0, 0, OC_EH_ERROR, 0, 0,
    { },
    { 0 }, false };
732
    OCRepPayload* payload = NULL;
733

734 735 736 737
    if (entityHandlerRequest && (flag & OC_REQUEST_FLAG))
    {
        if (OC_REST_GET == entityHandlerRequest->method)
        {
738
            OIC_LOG(INFO, ES_RH_TAG, "Received GET request");
739 740 741 742
            ehRet = ProcessGetRequest(entityHandlerRequest, &payload);
        }
        else if (OC_REST_PUT == entityHandlerRequest->method)
        {
743
            OIC_LOG(INFO, ES_RH_TAG, "Received PUT request");
744

745
            //PUT request will be handled in the internal implementation
746
            if (gProvResource.handle != NULL)
747 748 749 750 751
            {
                ehRet = ProcessPutRequest(entityHandlerRequest, &payload);
            }
            else
            {
752
                OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
753 754 755 756 757
                ehRet = OC_EH_ERROR;
            }
        }
        else if (OC_REST_POST == entityHandlerRequest->method)
        {
758
            OIC_LOG(INFO, ES_RH_TAG, "Received OC_REST_POST from client");
759
            if (gProvResource.handle != NULL)
760 761 762 763 764 765 766 767
            {
                ehRet = ProcessPostRequest(entityHandlerRequest, &payload);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "Cannot process put");
                ehRet = OC_EH_ERROR;
            }
768 769 770 771 772 773 774 775 776 777 778 779
        }

        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,
780 781
                    sizeof(response.sendVendorSpecificHeaderOptions));
            memset(response.resourceUri, 0, sizeof(response.resourceUri));
782 783 784 785 786 787
            // Indicate that response is NOT in a persistent buffer
            response.persistentBufferFlag = 0;

            // Send the response
            if (OCDoResponse(&response) != OC_STACK_OK)
            {
788
                OIC_LOG(ERROR, ES_RH_TAG, "Error sending response");
789 790 791 792 793 794 795 796
                ehRet = OC_EH_ERROR;
            }
        }
    }

    return ehRet;
}

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
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");
817 818 819 820 821 822 823 824 825 826 827 828
    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;
829 830
}

831 832 833 834 835 836 837 838 839 840
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;
}
841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
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";
    }
}