resourcehandler.c 66.2 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
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
57
void updateEasySetupConnectProperty(OCRepPayload* input);
58 59
void updateWiFiConfResource(OCRepPayload* input);
void updateCoapCloudConfResource(OCRepPayload* input);
60
void updateDevConfResource(OCRepPayload* input);
Jihun Ha's avatar
Jihun Ha committed
61
const char *getResult(OCStackResult result);
62

63
ESConnectRequestCB gConnectRequestEvtCb = NULL;
64 65
ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
66
ESDevConfCB gDevConfRsrcEvtCb = NULL;
67

68 69 70
ESReadUserdataCb gReadUserdataCb = NULL;
ESWriteUserdataCb gWriteUserdataCb = NULL;

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

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

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

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

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

113 114 115
    return false;
}

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

128
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
129
{
130
    gWifiConfRsrcEvtCb = cb;
131 132
}

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

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
141 142
}

143 144 145 146 147
void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
{
    gConnectRequestEvtCb = cb;
}

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

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

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

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

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

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

223
OCStackResult initWiFiConfResource(bool isSecured)
224 225 226
{
    OCStackResult res = OC_STACK_ERROR;

227 228 229
    g_ESWiFiConfResource.supportedFreq[0] = WIFI_24G;
    g_ESWiFiConfResource.supportedFreq[1] = WIFI_5G;
    g_ESWiFiConfResource.numSupportedFreq=2;
230 231 232 233 234 235 236 237 238
    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), "");
239 240 241

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

255 256 257 258 259 260 261 262
    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;
    }

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

}

268
OCStackResult initCoapCloudConfResource(bool isSecured)
269 270 271
{
    OCStackResult res = OC_STACK_ERROR;

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

    if (isSecured)
    {
282 283 284 285 286 287 288
        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
289
    {
290 291 292 293 294 295 296 297 298 299 300 301 302
        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;
303 304
    }

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

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

313
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
314 315 316

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

330 331 332 333 334 335 336
    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;
    }

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

}

342
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
343
{
344
    OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
345

346 347 348 349 350 351 352 353 354 355
    if (ehRequest->query)
    {
        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
        {
            // 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)
            {
356 357 358 359 360
                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");
361

362 363 364 365 366 367 368 369 370
                    ///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;
                }
371 372 373

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

374 375 376 377
                OCRepPayload *repPayload = NULL;
                OCRepPayloadGetPropObject(children, OC_RSRVD_REPRESENTATION, &repPayload);

                if(NULL == repPayload)
378
                {
379
                    OIC_LOG(ERROR, ES_RH_TAG, "repPayload is null!");
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
                    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))
                {
                    updateWiFiConfResource(repPayload);
                }
                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);
             }
        }
405 406 407 408 409
        else if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Handling POST request on default interface");
            updateEasySetupConnectProperty(input);
        }
410
    }
411 412

    OIC_LOG(DEBUG, ES_RH_TAG, "updateEasySetupResource exit");
413 414 415 416
}

void updateEasySetupConnectProperty(OCRepPayload* input)
{
417 418 419 420 421
    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));
422 423 424
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
425
            return;
426 427
        }

428
        int cntRequest = 0;
429
        for (unsigned int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
430 431 432 433
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

434
            if (i < dimensions[0] &&
435 436 437 438 439 440 441 442 443 444 445 446
                (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;

447
        if (g_ESEasySetupResource.connectRequest[0] != ES_CONNECT_NONE)
448 449 450
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Send ConnectRequest Callback To ES");

451 452
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
453 454 455 456 457 458 459 460 461
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }
462 463
}

464
void updateWiFiConfResource(OCRepPayload* input)
465
{
466
    ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
467

468
    if (wiFiData == NULL)
469 470
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
471
        return;
472
    }
473

474 475
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
476 477
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
478 479
    wiFiData->userdata = NULL;

480
    char* ssid = NULL;
481 482
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
483
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
484
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
485 486
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
                g_ESWiFiConfResource.ssid);
487 488
    }

489
    char* cred = NULL;
490 491
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
492
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
493
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
494 495
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
                g_ESWiFiConfResource.cred);
496 497
    }

498 499 500
    bool validAuthType = false;
    char *authType = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
501
    {
502 503 504 505 506 507 508 509 510
        WIFI_AUTHTYPE tmp;
        validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
        if (validAuthType == true)
        {
            g_ESWiFiConfResource.authType = tmp;
            wiFiData->authtype = g_ESWiFiConfResource.authType;
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
                    g_ESWiFiConfResource.authType);
        }
511 512
    }

513 514 515
    bool validEncType = false;
    char *encType = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
516
    {
517 518 519 520 521 522 523 524 525
        WIFI_ENCTYPE tmp;
        validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
        if (validEncType == true)
        {
            g_ESWiFiConfResource.encType = tmp;
            wiFiData->enctype = g_ESWiFiConfResource.encType;
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
                    g_ESWiFiConfResource.encType);
        }
526
    }
527

528
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
529
    {
530
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
531
    }
532

533
    if (ssid || cred || validAuthType || validEncType)
534
    {
535
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
536

537 538
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
539
        {
540
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
541 542 543
        }
        else
        {
544
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
545 546 547
        }
    }

548
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
549
    {
550
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
551 552
    }

553 554 555 556
    OICFree(encType);
    OICFree(authType);
    OICFree(cred);
    OICFree(ssid);
557
    OICFree(wiFiData);
558
}
559

560
void updateCoapCloudConfResource(OCRepPayload* input)
561
{
562
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
563

564
    if (cloudData == NULL)
565 566
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
567
        return;
568
    }
569 570

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
571
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
572
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
573
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
574
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
575 576
    cloudData->userdata = NULL;

577
    char *authCode = NULL;
578 579
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
580
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
581
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
582
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
583 584
    }

585 586 587
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
588
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
589
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
590
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
591 592 593 594 595
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
596 597
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
598
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
599 600
    }

601
    char *authProvider = NULL;
602 603
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
604
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
605
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
606
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
607 608
    }

609
    char *ciServer = NULL;
610 611
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
612
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
613
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
614
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
615
    }
616

617
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
618
    {
619
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
620
    }
621

622
    if (authCode || accessToken || authProvider || ciServer)
623
    {
624
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
625

626 627
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
628
        {
629
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
630 631 632
        }
        else
        {
633
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
634 635
        }
    }
636

637
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
638
    {
639
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
640 641
    }

642
    OICFree(cloudData);
643 644
}

645
void updateDevConfResource(OCRepPayload* input)
646
{
647
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
648

649
    if (devConfData == NULL)
650 651
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
652
        return;
653 654 655
    }
    devConfData->userdata = NULL;

656
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
657
    {
658
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
659
    }
660

661 662 663
    // If a writable property in oic.r.devconf is added later,
    // a condition for calling a resistered callback should be implemented also.
    if( devConfData->userdata != NULL )
664
    {
665
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
666

667 668
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
669
        {
670
            gDevConfRsrcEvtCb(ES_OK, devConfData);
671 672 673
        }
        else
        {
674
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
675 676
        }
    }
677

678
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
679
    {
680
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
681 682
    }

683
    OICFree(devConfData);
684 685
}

686
OCRepPayload* constructResponseOfWiFiConf(char *interface)
687 688 689 690 691 692 693 694
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

695
    if (g_ESWiFiConfResource.handle == NULL)
696
    {
697
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
698 699 700
        return NULL;
    }

701
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
702
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
703 704 705

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
706
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
707 708 709 710 711 712 713 714 715 716 717
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

718
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
719
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
720 721 722 723 724
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
725 726

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
727
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
728

729
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
730 731 732

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
733 734 735 736 737
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
738

739
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
740

741
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
742 743 744 745
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
746
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
747
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
748
    }
749

750 751 752
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
        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);
769 770
    }

771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    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);

791 792
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
793 794 795 796
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHTYPE,
            WiFiAuthTypeEnumToString(g_ESWiFiConfResource.authType));
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ENCTYPE,
            WiFiEncTypeEnumToString(g_ESWiFiConfResource.encType));
797

798
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
799
    {
800
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
801
    }
802

803
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
804 805 806 807 808
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

809 810 811
    return payload;
}

812
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
813 814 815 816 817 818 819 820
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

821
    if (g_ESCoapCloudConfResource.handle == NULL)
822
    {
823
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
824 825 826
        return NULL;
    }

827
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
828
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
829 830 831

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
832
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
833 834 835 836 837 838 839 840 841 842 843
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

844
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
845
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
846 847 848 849 850
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
851 852

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
853
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
854

855
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
856 857 858

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
859 860 861 862 863
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
864

865
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
866

867
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
868 869 870 871
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
872
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
873
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
874
    }
875