resourcehandler.c 63.8 KB
Newer Older
1 2
//******************************************************************
//
3
// Copyright 2015 Samsung Electronics All Rights Reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

21
#include "resourcehandler.h"
22
#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

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

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

//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
50 51 52 53 54
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);
55 56 57
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
void updateWiFiConfResource(OCRepPayload* input);
void updateCoapCloudConfResource(OCRepPayload* input);
58
void updateDevConfResource(OCRepPayload* input);
Jihun Ha's avatar
Jihun Ha committed
59
const char *getResult(OCStackResult result);
60

61
ESConnectRequestCB gConnectRequestEvtCb = NULL;
62 63
ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
64
ESDevConfCB gDevConfRsrcEvtCb = NULL;
65

66 67 68
ESReadUserdataCb gReadUserdataCb = NULL;
ESWriteUserdataCb gWriteUserdataCb = NULL;

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

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

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

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

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

111 112 113
    return false;
}

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

126
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
127
{
128
    gWifiConfRsrcEvtCb = cb;
129 130
}

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

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
139 140
}

141 142 143 144 145
void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
{
    gConnectRequestEvtCb = cb;
}

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

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

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

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

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

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

221
OCStackResult initWiFiConfResource(bool isSecured)
222 223 224
{
    OCStackResult res = OC_STACK_ERROR;

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

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

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

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

}

266
OCStackResult initCoapCloudConfResource(bool isSecured)
267 268 269
{
    OCStackResult res = OC_STACK_ERROR;

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

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

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

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

311
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
312 313 314

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

328 329 330 331 332 333 334
    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;
    }

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

}

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

344 345 346 347 348
    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));
349 350 351
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
352
            return;
353 354
        }

355
        int cntRequest = 0;
356
        for (unsigned int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
357 358 359 360
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

361
            if (i < dimensions[0] &&
362 363 364 365 366 367 368 369 370 371 372 373
                (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;

374
        if (g_ESEasySetupResource.connectRequest[0] != ES_CONNECT_NONE)
375 376 377
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Send ConnectRequest Callback To ES");

378 379
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
380 381 382 383 384 385 386 387 388 389
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }

390
    if (ehRequest->query)
391
    {
392
        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
393
        {
394
            // When Provisioning resource has a POST with BatchInterface
395 396
            updateCoapCloudConfResource(input);
            updateWiFiConfResource(input);
397
            updateDevConfResource(input);
398 399
        }
    }
400 401
}

402
void updateWiFiConfResource(OCRepPayload* input)
403
{
404
    ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
405

406
    if (wiFiData == NULL)
407 408
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
409
        return;
410
    }
411

412 413
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
414 415
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
416 417
    wiFiData->userdata = NULL;

418
    char* ssid = NULL;
419 420
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
421
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
422
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
423 424
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
                g_ESWiFiConfResource.ssid);
425 426
    }

427
    char* cred = NULL;
428 429
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
430
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
431
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
432 433
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
                g_ESWiFiConfResource.cred);
434 435
    }

436 437 438
    bool validAuthType = false;
    char *authType = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
439
    {
440 441 442 443 444 445 446 447 448
        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);
        }
449 450
    }

451 452 453
    bool validEncType = false;
    char *encType = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
454
    {
455 456 457 458 459 460 461 462 463
        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);
        }
464
    }
465

466
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
467
    {
468
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
469
    }
470

471
    if (ssid || cred || validAuthType || validEncType)
472
    {
473
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
474

475 476
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
477
        {
478
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
479 480 481
        }
        else
        {
482
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
483 484 485
        }
    }

486
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
487
    {
488
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
489 490
    }

491 492 493 494
    OICFree(encType);
    OICFree(authType);
    OICFree(cred);
    OICFree(ssid);
495
    OICFree(wiFiData);
496
}
497

498
void updateCoapCloudConfResource(OCRepPayload* input)
499
{
500
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
501

502
    if (cloudData == NULL)
503 504
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
505
        return;
506
    }
507 508

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
509
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
510
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
511
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
512
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
513 514
    cloudData->userdata = NULL;

515
    char *authCode = NULL;
516 517
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
518
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
519
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
520
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
521 522
    }

523 524 525
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
526
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
527
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
528
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
529 530 531 532 533
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
534 535
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
536
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
537 538
    }

539
    char *authProvider = NULL;
540 541
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
542
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
543
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
544
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
545 546
    }

547
    char *ciServer = NULL;
548 549
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
550
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
551
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
552
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
553
    }
554

555
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
556
    {
557
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
558
    }
559

560
    if (authCode || accessToken || authProvider || ciServer)
561
    {
562
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
563

564 565
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
566
        {
567
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
568 569 570
        }
        else
        {
571
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
572 573
        }
    }
574

575
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
576
    {
577
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
578 579
    }

580
    OICFree(cloudData);
581 582
}

583
void updateDevConfResource(OCRepPayload* input)
584
{
585
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
586

587
    if (devConfData == NULL)
588 589
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
590
        return;
591 592 593
    }
    devConfData->userdata = NULL;

594
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
595
    {
596
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
597
    }
598

599 600 601
    // 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 )
602
    {
603
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
604

605 606
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
607
        {
608
            gDevConfRsrcEvtCb(ES_OK, devConfData);
609 610 611
        }
        else
        {
612
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
613 614
        }
    }
615

616
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
617
    {
618
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
619 620
    }

621
    OICFree(devConfData);
622 623
}

624
OCRepPayload* constructResponseOfWiFiConf(char *interface)
625 626 627 628 629 630 631 632
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

633
    if (g_ESWiFiConfResource.handle == NULL)
634
    {
635
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
636 637 638
        return NULL;
    }

639
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
640
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
641 642 643

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
644
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
645 646 647 648 649 650 651 652 653 654 655
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

656
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
657
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
658 659 660 661 662
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
663 664

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
665
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
666

667
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
668 669 670

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
671 672 673 674 675
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
676

677
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
678

679
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
680 681 682 683
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
684
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
685
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
686
    }
687

688 689 690
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
        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);
707 708
    }

709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    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);

729 730
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
731 732 733 734
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHTYPE,
            WiFiAuthTypeEnumToString(g_ESWiFiConfResource.authType));
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ENCTYPE,
            WiFiEncTypeEnumToString(g_ESWiFiConfResource.encType));
735

736
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
737
    {
738
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
739
    }
740

741
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
742 743 744 745 746
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

747 748 749
    return payload;
}

750
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
751 752 753 754 755 756 757 758
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

759
    if (g_ESCoapCloudConfResource.handle == NULL)
760
    {
761
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
762 763 764
        return NULL;
    }

765
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
766
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
767 768 769

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
770
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
771 772 773 774 775 776 777 778 779 780 781
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

782
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
783
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
784 785 786 787 788
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
789 790

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
791
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
792

793
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
794 795 796

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
797 798 799 800 801
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
802

803
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
804

805
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
806 807 808 809
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
810
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
811
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
812
    }
813

814 815 816 817 818
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHCODE, g_ESCoapCloudConfResource.authCode);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ACCESSTOKEN, g_ESCoapCloudConfResource.accessToken);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ACCESSTOKEN_TYPE, (int)g_ESCoapCloudConfResource.accessTokenType);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHPROVIDER, g_ESCoapCloudConfResource.authProvider);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CISERVER, g_ESCoapCloudConfResource.ciServer);
819

820
    if (gWriteUserdataCb)
821
    {
822
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
823
    }
824

825
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
826 827 828 829 830
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

831 832 833
    return payload;
}

834
OCRepPayload* constructResponseOfDevConf(char *interface)
835 836 837 838 839 840 841 842
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

843
    if (g_ESDevConfResource.handle == NULL)
844 845 846 847 848
    {
        OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
        return NULL;
    }

849
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");