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

21
#include "resourcehandler.h"
22

23 24
#include "ocpayload.h"
#include "oic_string.h"
25
#include "oic_malloc.h"
26
#include "cautilinterface.h"
27

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

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

//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
49 50 51 52 53
OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag, OCEntityHandlerRequest *ehRequest,
        void *callback);
OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest, OCRepPayload** payload);
54 55 56
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
void updateWiFiConfResource(OCRepPayload* input);
void updateCoapCloudConfResource(OCRepPayload* input);
57
void updateDevConfResource(OCRepPayload* input);
Jihun Ha's avatar
Jihun Ha committed
58
const char *getResult(OCStackResult result);
59

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

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

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

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

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

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

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

110 111 112
    return false;
}

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

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

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

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

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

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

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

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

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

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

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

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

224 225 226 227 228 229 230 231 232 233
    g_ESWiFiConfResource.supportedFreq = WIFI_BOTH;
    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), "");
234 235 236

    if (isSecured)
    {
237
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
238
        OC_RSRVD_INTERFACE_DEFAULT,
239
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
240
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
241 242
    }
    else
243
    {
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 248 249
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

250 251 252 253 254 255 256 257
    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;
    }

258
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
259 260 261 262
    return res;

}

263
OCStackResult initCoapCloudConfResource(bool isSecured)
264 265 266
{
    OCStackResult res = OC_STACK_ERROR;

267
    OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
268 269
    OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken),
            "");
270
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
271 272
    OICStrcpy(g_ESCoapCloudConfResource.authProvider,
            sizeof(g_ESCoapCloudConfResource.authProvider), "");
273
    OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
274 275 276

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

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

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

308
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
309 310 311

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

325 326 327 328 329 330 331
    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;
    }

332
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
333 334 335 336
    return res;

}

337
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
338
{
339
    OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
340

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

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

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

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

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

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

399
void updateWiFiConfResource(OCRepPayload* input)
400
{
401
    ESWiFiConfData* wiFiData = (ESWiFiConfData*)OICMalloc(sizeof(ESWiFiConfData));
402

403
    if (wiFiData == NULL)
404 405
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
406
        return ;
407
    }
408

409 410
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
411 412
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
413 414
    wiFiData->userdata = NULL;

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

423
    char* cred = NULL;
424 425
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
426
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
427
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
428
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s", g_ESWiFiConfResource.cred);
429 430
    }

431
    int64_t authType = -1;
432 433
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
434 435
        g_ESWiFiConfResource.authType = authType;
        wiFiData->authtype = g_ESWiFiConfResource.authType;
436
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u", g_ESWiFiConfResource.authType);
437 438
    }

439
    int64_t encType = -1;
440 441
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
442 443
        g_ESWiFiConfResource.encType = encType;
        wiFiData->enctype = g_ESWiFiConfResource.encType;
444
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u", g_ESWiFiConfResource.encType);
445
    }
446

447
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
448
    {
449
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
450
    }
451

452
    if (ssid || cred || authType!= -1 || encType != -1)
453
    {
454
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
455

456 457
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
458
        {
459
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
460 461 462
        }
        else
        {
463
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
464 465 466
        }
    }

467
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
468
    {
469
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
470 471
    }

472
    OICFree(wiFiData);
473
}
474

475
void updateCoapCloudConfResource(OCRepPayload* input)
476
{
477
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
478

479
    if (cloudData == NULL)
480 481
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
482
        return;
483
    }
484 485

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
486
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
487
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
488
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
489
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
490 491
    cloudData->userdata = NULL;

492
    char *authCode = NULL;
493 494
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
495
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
496
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
497
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
498 499
    }

500 501 502
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
503
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
504
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
505
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
506 507 508 509 510
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
511 512
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
513
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
514 515
    }

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

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

532
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
533
    {
534
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
535
    }
536

537
    if (authCode || accessToken || authProvider || ciServer)
538
    {
539
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
540

541 542
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
543
        {
544
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
545 546 547
        }
        else
        {
548
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
549 550
        }
    }
551

552
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
553
    {
554
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
555 556
    }

557
    OICFree(cloudData);
558 559
}

560
void updateDevConfResource(OCRepPayload* input)
561
{
562
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
563

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

571
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
572
    {
573
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
574
    }
575

576 577 578
    // 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 )
579
    {
580
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
581

582 583
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
584
        {
585
            gDevConfRsrcEvtCb(ES_OK, devConfData);
586 587 588
        }
        else
        {
589
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
590 591
        }
    }
592

593
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
594
    {
595
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
596 597
    }

598
    OICFree(devConfData);
599 600
}

601
OCRepPayload* constructResponseOfWiFiConf(char *interface)
602 603 604 605 606 607 608 609
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

610
    if (g_ESWiFiConfResource.handle == NULL)
611
    {
612
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
613 614 615
        return NULL;
    }

616
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
617
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
618 619 620

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
621
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
622 623 624 625 626 627 628 629 630 631 632
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

633
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
634
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
635 636 637 638 639
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
640 641

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
642
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
643

644
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
645 646 647

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
648 649 650 651 652
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
653

654
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
655

656
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
657 658 659 660
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
661
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
662
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
663
    }
664

665 666
    size_t dimensions[MAX_REP_ARRAY_DEPTH] = {g_ESWiFiConfResource.numMode, 0, 0};
    int64_t *modes_64 = (int64_t *)OICMalloc(g_ESWiFiConfResource.numMode * sizeof(int64_t));
667 668 669 670 671 672 673
    if (!modes_64)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

    for (int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
674
    {
675
        modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
676
    }
677

678 679 680 681 682 683 684 685 686
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
        OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *) modes_64,
                dimensions);
        OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ,
                g_ESWiFiConfResource.supportedFreq);
    }

687 688 689 690
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_AUTHTYPE, (int) g_ESWiFiConfResource.authType);
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_ENCTYPE, (int) g_ESWiFiConfResource.encType);
691

692
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
693
    {
694
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
695
    }
696

697
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
698 699 700 701 702
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

703 704 705
    return payload;
}

706
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
707 708 709 710 711 712 713 714
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

715
    if (g_ESCoapCloudConfResource.handle == NULL)
716
    {
717
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
718 719 720
        return NULL;
    }

721
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
722
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
723 724 725

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
726
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
727 728 729 730 731 732 733 734 735 736 737
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

738
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
739
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
740 741 742 743 744
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
745 746

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
747
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
748

749
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
750 751 752

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
753 754 755 756 757
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
758

759
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
760

761
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
762 763 764 765
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
766
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
767
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
768
    }
769

770 771 772 773 774
    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);
775

776
    if (gWriteUserdataCb)
777
    {
778
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
779
    }
780

781
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
782 783 784 785 786
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

787 788 789
    return payload;
}

790
OCRepPayload* constructResponseOfDevConf(char *interface)
791 792 793 794 795 796 797 798
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

799
    if (g_ESDevConfResource.handle == NULL)
800 801 802 803 804
    {
        OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
        return NULL;
    }

805
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
806
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
807 808 809

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
810
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
811 812 813 814 815 816 817 818 819 820 821
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

822
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
823
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
824 825 <