resourcehandler.c 68.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
#include "internal/es_util.h"
23

24 25
#include "ocpayload.h"
#include "oic_string.h"
26
#include "oic_malloc.h"
27
#include "cautilinterface.h"
28
#include "payload_logging.h"
29

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

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

//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
51 52 53 54 55
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);
56
OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
57
void updateEasySetupConnectProperty(OCRepPayload* input);
58
OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input);
59
void updateCoapCloudConfResource(OCRepPayload* input);
60
void updateDevConfResource(OCRepPayload* input);
61 62
bool isAuthTypeSupported(WIFI_AUTHTYPE authType);
bool isEncTypeSupported(WIFI_ENCTYPE encType);
Jihun Ha's avatar
Jihun Ha committed
63
const char *getResult(OCStackResult result);
64

65
ESConnectRequestCB gConnectRequestEvtCb = NULL;
66 67
ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
68
ESDevConfCB gDevConfRsrcEvtCb = NULL;
69

70 71 72
ESReadUserdataCb gReadUserdataCb = NULL;
ESWriteUserdataCb gWriteUserdataCb = NULL;

73
void GetInterfaceNameFromQuery(const char *query, char **iface)
74
{
75
    if (!iface)
76
    {
77
        return;
78
    }
79 80 81
    *iface = NULL;
    char *str = OICStrdup(query);
    char *ptr = strtok(str, ";");
82

83
    while (ptr)
84
    {
85
        if (strstr(ptr, ".if."))
86 87 88 89 90
        {
            char *if_ptr = NULL;
            if_ptr = strtok(ptr, "=");
            if_ptr = strtok(NULL, "=");

91 92
            *iface = OICStrdup(if_ptr);
            break;
93
        }
94 95
        ptr = strtok(NULL, ";");
    }
96

97
    OICFree(str);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

bool CompareResourceInterface(const char *from, const char *iface)
{
    char *if_ptr;
    GetInterfaceNameFromQuery(from, &if_ptr);
    if (!if_ptr)
    {
        return false;
    }
    if (!strcmp(if_ptr, iface))
    {
        OICFree(if_ptr);
        return true;
    }
    OICFree(if_ptr);

115 116 117
    return false;
}

118 119
ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
{
120
    if (!readCb && !writeCb)
121
    {
122
        OIC_LOG(DEBUG, ES_RH_TAG, "Both of callbacks for user data are null");
123 124 125 126 127 128 129
        return ES_ERROR;
    }
    gReadUserdataCb = readCb;
    gWriteUserdataCb = writeCb;
    return ES_OK;
}

130
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
131
{
132
    gWifiConfRsrcEvtCb = cb;
133 134
}

135
void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
136
{
137
    gCoapCloudConfRsrcEvtCb = cb;
138 139 140 141 142
}

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
143 144
}

145 146 147 148 149
void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
{
    gConnectRequestEvtCb = cb;
}

150 151
void UnRegisterResourceEventCallBack()
{
152
    if (gWifiConfRsrcEvtCb)
153
    {
154
        gWifiConfRsrcEvtCb = NULL;
155
    }
156
    if (gCoapCloudConfRsrcEvtCb)
157
    {
158
        gCoapCloudConfRsrcEvtCb = NULL;
159 160 161 162
    }
    if (gDevConfRsrcEvtCb)
    {
        gDevConfRsrcEvtCb = NULL;
163
    }
164 165 166 167
    if (gConnectRequestEvtCb)
    {
        gConnectRequestEvtCb = NULL;
    }
168 169
}

170
OCStackResult initEasySetupResource(bool isSecured)
171
{
172 173
    g_ESEasySetupResource.status = ES_STATE_INIT;
    g_ESEasySetupResource.lastErrCode = ES_ERRCODE_NO_ERROR;
174
    for (int i = 0; i < NUM_CONNECT_TYPE; ++i)
175 176 177 178
    {
        g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
    }
    g_ESEasySetupResource.numRequest = 0;
179

180 181 182
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
183
        res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
184
        OC_RSRVD_INTERFACE_DEFAULT,
185
        OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
186
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
187 188
    }
    else
189
    {
190
        res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
191
        OC_RSRVD_INTERFACE_DEFAULT,
192
        OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
193
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
194
    }
195
    if (res != OC_STACK_OK)
196
    {
197
        OIC_LOG_V(ERROR, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
198 199 200
        return res;
    }

201
    res = OCBindResourceTypeToResource(g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_COL);
202
    if (res != OC_STACK_OK)
203
    {
204
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource type with result: %s", getResult(res));
205 206 207
        return res;
    }

208
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_LL);
209
    if (res != OC_STACK_OK)
210
    {
211
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
212 213
        return res;
    }
214
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_BATCH);
215
    if (res != OC_STACK_OK)
216
    {
217
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
218 219
        return res;
    }
220

221
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
222 223
    return res;
}
224

225
OCStackResult initWiFiConfResource(bool isSecured)
226 227 228
{
    OCStackResult res = OC_STACK_ERROR;

229 230 231
    g_ESWiFiConfResource.supportedFreq[0] = WIFI_24G;
    g_ESWiFiConfResource.supportedFreq[1] = WIFI_5G;
    g_ESWiFiConfResource.numSupportedFreq=2;
232 233 234 235 236 237 238 239 240
    g_ESWiFiConfResource.supportedMode[0] = WIFI_11A;
    g_ESWiFiConfResource.supportedMode[1] = WIFI_11B;
    g_ESWiFiConfResource.supportedMode[2] = WIFI_11G;
    g_ESWiFiConfResource.supportedMode[3] = WIFI_11N;
    g_ESWiFiConfResource.numMode = 4;
    g_ESWiFiConfResource.authType = NONE_AUTH;
    g_ESWiFiConfResource.encType = NONE_ENC;
    OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), "");
    OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), "");
241 242 243

    if (isSecured)
    {
244
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
245
        OC_RSRVD_INTERFACE_DEFAULT,
246
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
247
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
248 249
    }
    else
250
    {
251
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
252
        OC_RSRVD_INTERFACE_DEFAULT,
253
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
254 255 256
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

257 258 259 260 261 262 263 264
    res = OCBindResourceInterfaceToResource(g_ESWiFiConfResource.handle,
    OC_RSRVD_INTERFACE_READ_WRITE);
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
        return res;
    }

265
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
266 267 268 269
    return res;

}

270
OCStackResult initCoapCloudConfResource(bool isSecured)
271 272 273
{
    OCStackResult res = OC_STACK_ERROR;

274
    OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
275 276
    OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken),
            "");
277
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
278 279
    OICStrcpy(g_ESCoapCloudConfResource.authProvider,
            sizeof(g_ESCoapCloudConfResource.authProvider), "");
280
    OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
281 282 283

    if (isSecured)
    {
284 285 286 287 288 289 290
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle,
                OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
                OC_RSRVD_INTERFACE_DEFAULT,
                OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
                NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }
    else
291
    {
292 293 294 295 296 297 298 299 300 301 302 303 304
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle,
                OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
                OC_RSRVD_INTERFACE_DEFAULT,
                OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
                NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

    res = OCBindResourceInterfaceToResource(g_ESCoapCloudConfResource.handle,
            OC_RSRVD_INTERFACE_READ_WRITE);
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
        return res;
305 306
    }

307
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created CoapCloudConf resource with result: %s", getResult(res));
308 309 310 311 312 313 314
    return res;
}

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

315
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
316 317 318

    if (isSecured)
    {
319
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
320 321 322
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
323 324
    }
    else
325
    {
326
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
327 328 329 330 331
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

332 333 334 335 336 337 338
    res = OCBindResourceInterfaceToResource(g_ESDevConfResource.handle, OC_RSRVD_INTERFACE_READ);
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
        return res;
    }

339
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
340 341 342 343
    return res;

}

344 345
OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest,
    OCRepPayload* input)
346
{
347
    OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
348

349
    OCEntityHandlerResult ehResult = OC_EH_OK;
350 351 352 353
    if (ehRequest->query)
    {
        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
        {
354
            bool hasError = false;
355 356 357 358 359
            // When Provisioning resource has a POST with BatchInterface
            // Parsing POST request on Batch Interface cosidering same format as GET using batch.
            OCRepPayload *children = input;
            while(children)
            {
360 361 362 363 364
                char* uri = children->uri;
                if(NULL == uri)
                {
                    OIC_LOG(DEBUG, ES_RH_TAG,
                        "No URI found in request, applying same request to all links in collection");
365

366 367 368 369 370 371 372 373 374
                    ///TODO: Need to check if "input" should be passed, or the value of property OC_RSRVD_REPRESENTATION.
                    updateEasySetupConnectProperty(input);
                    updateWiFiConfResource(input);
                    updateCoapCloudConfResource(input);
                    updateDevConfResource(input);

                    // As the request is applied to children already, no need to check next child.
                    break;
                }
375 376 377

                OIC_LOG_V(DEBUG, ES_RH_TAG, "uri [%s]", uri);

378 379 380 381
                OCRepPayload *repPayload = NULL;
                OCRepPayloadGetPropObject(children, OC_RSRVD_REPRESENTATION, &repPayload);

                if(NULL == repPayload)
382
                {
383
                    OIC_LOG(ERROR, ES_RH_TAG, "repPayload is null!");
384 385 386 387 388 389 390 391 392 393
                    children = children->next;
                    continue;
                }

                if (0 == strcmp(uri, OC_RSRVD_ES_URI_EASYSETUP))
                {
                    updateEasySetupConnectProperty(repPayload);
                }
                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
                {
394 395 396 397 398 399 400
                    if (updateWiFiConfResource(repPayload) != OC_EH_OK)
                    {
                        // Possibility of failure exist only when updating WiFiConf resource.
                        // So error code is checked only for this function.
                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to update WiFiConf resource.");
                        hasError = true;
                    }
401 402 403 404 405 406 407 408 409 410 411 412 413
                }
                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_COAPCLOUDCONF))
                {
                    updateCoapCloudConfResource(repPayload);
                }
                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_DEVCONF))
                {
                    updateDevConfResource(repPayload);
                }

                children = children->next;
                OCRepPayloadDestroy(repPayload);
             }
414 415 416 417 418

             if (hasError)
             {
                ehResult = OC_EH_BAD_REQ;
             }
419
        }
420 421 422 423 424
        else if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Handling POST request on default interface");
            updateEasySetupConnectProperty(input);
        }
425
    return ehResult;
426
    }
427 428

    OIC_LOG(DEBUG, ES_RH_TAG, "updateEasySetupResource exit");
429 430 431 432
}

void updateEasySetupConnectProperty(OCRepPayload* input)
{
433 434 435 436 437
    int64_t *connect_req = NULL;
    size_t dimensions[MAX_REP_ARRAY_DEPTH] = { 0 };
    if (OCRepPayloadGetIntArray(input, OC_RSRVD_ES_CONNECT, &connect_req, dimensions))
    {
        ESConnectRequest* connectRequest = (ESConnectRequest*)OICMalloc(sizeof(ESConnectRequest));
438 439 440
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
441
            return;
442 443
        }

444
        int cntRequest = 0;
445
        for (unsigned int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
446 447 448 449
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

450
            if (i < dimensions[0] &&
451 452 453 454 455 456 457 458 459 460 461 462
                (connect_req[i] == ES_CONNECT_WIFI || connect_req[i] == ES_CONNECT_COAPCLOUD))
            {
                g_ESEasySetupResource.connectRequest[cntRequest] = connect_req[i];
                connectRequest->connect[cntRequest] = connect_req[i];
                OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.connectType[%d] : %d",
                                                    cntRequest, g_ESEasySetupResource.connectRequest[cntRequest]);
                cntRequest++;
            }
        }
        connectRequest->numRequest = cntRequest;
        g_ESEasySetupResource.numRequest = cntRequest;

463
        if (g_ESEasySetupResource.connectRequest[0] != ES_CONNECT_NONE)
464 465 466
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Send ConnectRequest Callback To ES");

467 468
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
469 470 471 472 473 474 475 476 477
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }
478 479
}

480
OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input)
481
{
482
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
483
    ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
484
    if (wiFiData == NULL)
485 486
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
487
        return ehResult;
488
    }
489

490 491 492 493
    char* ssid = NULL;
    char* cred = NULL;
    char *authType = NULL;
    char *encType = NULL;
494 495
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
496 497
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
498 499
    wiFiData->userdata = NULL;

500 501
    bool validAuthType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
502
    {
503 504
        WIFI_AUTHTYPE tmp;
        validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
505
        if (validAuthType && isAuthTypeSupported(tmp))
506
        {
507
            wiFiData->authtype = tmp;
508
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
509 510 511 512 513 514 515
                    wiFiData->authtype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "AuthType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
516
        }
517 518
    }

519 520
    bool validEncType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
521
    {
522 523
        WIFI_ENCTYPE tmp;
        validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
524
        if (validEncType && isEncTypeSupported(tmp))
525
        {
526
            wiFiData->enctype = tmp;
527
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
528 529 530 531 532 533 534
                    wiFiData->enctype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "EncType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
535
        }
536
    }
537

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
    if (validAuthType)
    {
        g_ESWiFiConfResource.authType = wiFiData->authtype;
    }

    if (validEncType)
    {
        g_ESWiFiConfResource.encType = wiFiData->enctype;
    }

    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
                g_ESWiFiConfResource.ssid);
    }

    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
                g_ESWiFiConfResource.cred);
    }

564
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
565
    {
566
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
567
    }
568

569
    if (ssid || cred || validAuthType || validEncType)
570
    {
571
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
572

573 574
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
575
        {
576
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
577 578 579
        }
        else
        {
580
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
581 582 583
        }
    }

584
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
585
    {
586
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
587 588
    }

589 590 591
    ehResult = OC_EH_OK;

EXIT:
592 593 594 595
    OICFree(encType);
    OICFree(authType);
    OICFree(cred);
    OICFree(ssid);
596
    OICFree(wiFiData);
597
    return ehResult;
598
}
599

600
void updateCoapCloudConfResource(OCRepPayload* input)
601
{
602
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
603

604
    if (cloudData == NULL)
605 606
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
607
        return;
608
    }
609 610

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
611
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
612
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
613
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
614
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
615 616
    cloudData->userdata = NULL;

617
    char *authCode = NULL;
618 619
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
620
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
621
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
622
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
623 624
    }

625 626 627
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
628
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
629
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
630
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
631 632 633 634 635
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
636 637
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
638
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
639 640
    }

641
    char *authProvider = NULL;
642 643
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
644
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
645
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
646
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
647 648
    }

649
    char *ciServer = NULL;
650 651
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
652
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
653
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
654
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
655
    }
656

657
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
658
    {
659
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
660
    }
661

662
    if (authCode || accessToken || authProvider || ciServer)
663
    {
664
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
665

666 667
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
668
        {
669
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
670 671 672
        }
        else
        {
673
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
674 675
        }
    }
676

677
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
678
    {
679
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
680 681
    }

682
    OICFree(cloudData);
683 684
}

685
void updateDevConfResource(OCRepPayload* input)
686
{
687
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
688

689
    if (devConfData == NULL)
690 691
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
692
        return;
693 694 695
    }
    devConfData->userdata = NULL;

696
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
697
    {
698
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
699
    }
700

701 702
    // If a writable property in oic.r.devconf is added later,
    // a condition for calling a resistered callback should be implemented also.
703
    if (devConfData->userdata != NULL)
704
    {
705
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
706

707 708
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
709
        {
710
            gDevConfRsrcEvtCb(ES_OK, devConfData);
711 712 713
        }
        else
        {
714
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
715 716
        }
    }
717

718
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
719
    {
720
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
721 722
    }

723
    OICFree(devConfData);
724 725
}

726
OCRepPayload* constructResponseOfWiFiConf(char *interface)
727 728 729 730 731 732 733 734
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

735
    if (g_ESWiFiConfResource.handle == NULL)
736
    {
737
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
738 739 740
        return NULL;
    }

741
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
742
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
743 744 745

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
746
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
747 748 749 750 751 752 753 754 755 756 757
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

758
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
759
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
760 761 762 763 764
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
765 766

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
767
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
768

769
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
770 771 772

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
773 774 775 776 777
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
778

779
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
780

781
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
782 783 784 785
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
786
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
787
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
788
    }
789

790 791 792
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
        size_t dimensionsModes[MAX_REP_ARRAY_DEPTH] = { g_ESWiFiConfResource.numMode, 0, 0 };
        const char *modes[NUM_WIFIMODE] = { 0, };
        for (int i = 0; i < g_ESWiFiConfResource.numMode; ++i)
        {
            modes[i] = WiFiModeEnumToString(g_ESWiFiConfResource.supportedMode[i]);
        }
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, &modes[0],
                dimensionsModes);

        size_t dimensionsFreq[MAX_REP_ARRAY_DEPTH] = { g_ESWiFiConfResource.numSupportedFreq, 0, 0 };
        const char *freq[NUM_WIFIFREQ] = { 0, };
        for (int i = 0; i < g_ESWiFiConfResource.numSupportedFreq; ++i)
        {
            freq[i] = WiFiFreqEnumToString(g_ESWiFiConfResource.supportedFreq[i]);
        }
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, freq, dimensionsFreq);
809 810
    }

811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830
    size_t dimensionsAuthType[MAX_REP_ARRAY_DEPTH] = { g_ESWiFiConfResource.numSupportedAuthType, 0,
            0 };
    const char *authType[NUM_WIFIAUTHTYPE] = { 0, };
    for (int i = 0; i < g_ESWiFiConfResource.numSupportedAuthType; ++i)
    {
        authType[i] = WiFiAuthTypeEnumToString(g_ESWiFiConfResource.supportedAuthType[i]);
    }
    OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIAUTHTYPE, authType,
            dimensionsAuthType);

    size_t dimensionsEncType[MAX_REP_ARRAY_DEPTH] =
            { g_ESWiFiConfResource.numSupportedEncType, 0, 0 };
    const char *encType[NUM_WIFIENCTYPE] = { 0, };
    for (int i = 0; i < g_ESWiFiConfResource.numSupportedEncType; ++i)
    {
        encType[i] = WiFiEncTypeEnumToString(g_ESWiFiConfResource.supportedEncType[i]);
    }
    OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIENCTYPE, encType,
            dimensionsEncType);

831 832
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
833 834 835 836
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHTYPE,
            WiFiAuthTypeEnumToString(g_ESWiFiConfResource.authType));
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ENCTYPE,
            WiFiEncTypeEnumToString(g_ESWiFiConfResource.encType));
837

838
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
839
    {
840
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
841
    }
842

843
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
844 845 846 847 848
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

849 850 851
    return payload;
}

852
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
853 854 855 856 857 858 859 860
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

861
    if (g_ESCoapCloudConfResource.handle == NULL)
862
    {
863
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
864 865 866
        return NULL;
    }

867
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
868
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
869 870 871

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
872
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
873 874 875 876 877 878 879 880 881 882 883
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

884
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
885