resourcehandler.c 60.3 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 69 70 71 72
bool CompareResourceInterface(char *from, char *iface)
{
    char *str = OICStrdup(from);
    char *ptr = strtok(str, ";");

73
    if (ptr == NULL)
74
    {
75
        OICFree(str);
76
        return false;
77
    }
78

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

87
            if (!strcmp(if_ptr, iface))
88
            {
89
                OICFree(str);
90 91 92 93
                return true;
            }
        }

Parkhi's avatar
Parkhi committed
94
    } while ((ptr = strtok(NULL, ";")));
95

96
    OICFree(str);
97 98 99
    return false;
}

100 101
ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
{
102
    if (!readCb && !writeCb)
103
    {
104
        OIC_LOG(DEBUG, ES_RH_TAG, "Both of callbacks for user data are null");
105 106 107 108 109 110 111
        return ES_ERROR;
    }
    gReadUserdataCb = readCb;
    gWriteUserdataCb = writeCb;
    return ES_OK;
}

112
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
113
{
114
    gWifiConfRsrcEvtCb = cb;
115 116
}

117
void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
118
{
119
    gCoapCloudConfRsrcEvtCb = cb;
120 121 122 123 124
}

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
125 126
}

127 128 129 130 131
void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
{
    gConnectRequestEvtCb = cb;
}

132 133
void UnRegisterResourceEventCallBack()
{
134
    if (gWifiConfRsrcEvtCb)
135
    {
136
        gWifiConfRsrcEvtCb = NULL;
137
    }
138
    if (gCoapCloudConfRsrcEvtCb)
139
    {
140
        gCoapCloudConfRsrcEvtCb = NULL;
141 142 143 144
    }
    if (gDevConfRsrcEvtCb)
    {
        gDevConfRsrcEvtCb = NULL;
145
    }
146 147 148 149
    if (gConnectRequestEvtCb)
    {
        gConnectRequestEvtCb = NULL;
    }
150 151
}

152
OCStackResult initEasySetupResource(bool isSecured)
153
{
154 155
    g_ESEasySetupResource.status = ES_STATE_INIT;
    g_ESEasySetupResource.lastErrCode = ES_ERRCODE_NO_ERROR;
156 157 158 159 160
    for( int i = 0 ; i < NUM_CONNECT_TYPE ; ++i )
    {
        g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
    }
    g_ESEasySetupResource.numRequest = 0;
161

162 163 164
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
165
        res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
166
        OC_RSRVD_INTERFACE_DEFAULT,
167
        OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
168
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
169
    }else
170
    {
171
        res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
172
        OC_RSRVD_INTERFACE_DEFAULT,
173
        OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
174
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
175
    }
176
    if (res != OC_STACK_OK)
177
    {
178
        OIC_LOG_V(ERROR, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
179 180 181
        return res;
    }

182
    res = OCBindResourceTypeToResource(g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_COL);
183
    if (res != OC_STACK_OK)
184
    {
185
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource type with result: %s", getResult(res));
186 187 188
        return res;
    }

189
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_LL);
190
    if (res != OC_STACK_OK)
191
    {
192
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
193 194
        return res;
    }
195
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_BATCH);
196
    if (res != OC_STACK_OK)
197
    {
198
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
199 200
        return res;
    }
201

202
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
203 204
    return res;
}
205

206
OCStackResult initWiFiConfResource(bool isSecured)
207 208 209
{
    OCStackResult res = OC_STACK_ERROR;

210 211 212 213 214 215 216 217 218 219
    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), "");
220 221 222

    if (isSecured)
    {
223
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
224
        OC_RSRVD_INTERFACE_DEFAULT,
225
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
226 227 228
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
229
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
230
        OC_RSRVD_INTERFACE_DEFAULT,
231
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
232 233 234
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

235
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
236 237 238 239
    return res;

}

240
OCStackResult initCoapCloudConfResource(bool isSecured)
241 242 243
{
    OCStackResult res = OC_STACK_ERROR;

244 245 246 247 248
    OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
    OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), "");
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
    OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), "");
    OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
249 250 251

    if (isSecured)
    {
252
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
253
        OC_RSRVD_INTERFACE_DEFAULT,
254
        OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
255 256 257
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
258
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
259
        OC_RSRVD_INTERFACE_DEFAULT,
260
        OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
261 262 263
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

264
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created CoapCloudConf resource with result: %s", getResult(res));
265 266 267 268 269 270 271 272
    return res;

}

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

273 274 275 276 277
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
    OICStrcpy(g_ESDevConfResource.modelNumber, sizeof(g_ESDevConfResource.modelNumber), "");
    OICStrcpy(g_ESDevConfResource.location, sizeof(g_ESDevConfResource.location), "");
    OICStrcpy(g_ESDevConfResource.country, sizeof(g_ESDevConfResource.country), "");
    OICStrcpy(g_ESDevConfResource.language, sizeof(g_ESDevConfResource.language), "");
278 279 280

    if (isSecured)
    {
281
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
282 283 284 285 286
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }else
    {
287
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
288 289 290 291 292
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

293
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
294 295 296 297
    return res;

}

298
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
299
{
300
    OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
301

302 303 304 305 306
    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));
307 308 309
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
310
            return;
311 312
        }

313 314 315 316 317 318
        int cntRequest = 0;
        for (int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

319
            if (i < dimensions[0] &&
320 321 322 323 324 325 326 327 328 329 330 331
                (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;

332
        if (g_ESEasySetupResource.connectRequest[0] != ES_CONNECT_NONE)
333 334 335
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Send ConnectRequest Callback To ES");

336 337
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
338 339 340 341 342 343 344 345 346 347
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }

348
    if (ehRequest->query)
349
    {
350
        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
351
        {
352
            // When Provisioning resource has a POST with BatchInterface
353 354
            updateCoapCloudConfResource(input);
            updateWiFiConfResource(input);
355
            updateDevConfResource(input);
356 357
        }
    }
358 359
}

360
void updateWiFiConfResource(OCRepPayload* input)
361
{
362
    ESWiFiConfData* wiFiData = (ESWiFiConfData*)OICMalloc(sizeof(ESWiFiConfData));
363

364
    if (wiFiData == NULL)
365 366
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
367
        return ;
368
    }
369

370 371
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
372 373
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
374 375
    wiFiData->userdata = NULL;

376
    char* ssid = NULL;
377 378
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
379
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
380
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
381
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s", g_ESWiFiConfResource.ssid);
382 383
    }

384
    char* cred = NULL;
385 386
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
387
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
388
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
389
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s", g_ESWiFiConfResource.cred);
390 391
    }

392
    int64_t authType = -1;
393 394
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
395 396
        g_ESWiFiConfResource.authType = authType;
        wiFiData->authtype = g_ESWiFiConfResource.authType;
397
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u", g_ESWiFiConfResource.authType);
398 399
    }

400
    int64_t encType = -1;
401 402
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
403 404
        g_ESWiFiConfResource.encType = encType;
        wiFiData->enctype = g_ESWiFiConfResource.encType;
405
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u", g_ESWiFiConfResource.encType);
406
    }
407

408
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
409
    {
410
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
411
    }
412

413
    if (ssid || cred || authType!= -1 || encType != -1)
414
    {
415
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
416

417 418
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
419
        {
420
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
421 422 423
        }
        else
        {
424
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
425 426 427
        }
    }

428
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
429
    {
430
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
431 432
    }

433
    OICFree(wiFiData);
434
}
435

436
void updateCoapCloudConfResource(OCRepPayload* input)
437
{
438
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
439

440
    if (cloudData == NULL)
441 442
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
443
        return;
444
    }
445 446

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
447
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
448
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
449
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
450
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
451 452
    cloudData->userdata = NULL;

453
    char *authCode = NULL;
454 455
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
456
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
457
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
458
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
459 460
    }

461 462 463
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
464
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
465
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
466
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
467 468 469 470 471
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
472 473
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
474
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
475 476
    }

477
    char *authProvider = NULL;
478 479
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
480
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
481
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
482
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
483 484
    }

485
    char *ciServer = NULL;
486 487
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
488
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
489
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
490
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
491
    }
492

493
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
494
    {
495
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
496
    }
497

498
    if (authCode || accessToken || authProvider || ciServer)
499
    {
500
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
501

502 503
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
504
        {
505
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
506 507 508
        }
        else
        {
509
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
510 511
        }
    }
512

513
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
514
    {
515
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
516 517
    }

518
    OICFree(cloudData);
519 520
}

521
void updateDevConfResource(OCRepPayload* input)
522
{
523
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
524

525
    if (devConfData == NULL)
526 527
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
528
        return;
529
    }
530 531
    memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
    memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
532 533
    devConfData->userdata = NULL;

534 535 536
    char *location = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
    {
537
        OICStrcpy(g_ESDevConfResource.location, sizeof(g_ESDevConfResource.location), location);
538
        OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
539
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESDevConfResource.location %s", g_ESDevConfResource.location);
540 541
    }

542
    char *country = NULL;
543
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_COUNTRY, &country))
544
    {
545
        OICStrcpy(g_ESDevConfResource.country, sizeof(g_ESDevConfResource.country), country);
546
        OICStrcpy(devConfData->country, sizeof(devConfData->country), country);
547
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESDevConfResource.country %s", g_ESDevConfResource.country);
548 549
    }

550
    char *language = NULL;
551
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LANGUAGE, &language))
552
    {
553
        OICStrcpy(g_ESDevConfResource.language, sizeof(g_ESDevConfResource.language), language);
554
        OICStrcpy(devConfData->language, sizeof(devConfData->language), language);
555
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESDevConfResource.language %s", g_ESDevConfResource.language);
556
    }
557

558
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
559
    {
560
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
561
    }
562

563
    if (country || language)
564
    {
565
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
566

567 568
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
569
        {
570
            gDevConfRsrcEvtCb(ES_OK, devConfData);
571 572 573
        }
        else
        {
574
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
575 576
        }
    }
577

578
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
579
    {
580
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
581 582
    }

583
    OICFree(devConfData);
584 585
}

586
OCRepPayload* constructResponseOfWiFiConf(char *interface)
587 588 589 590 591 592 593 594
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

595
    if (g_ESWiFiConfResource.handle == NULL)
596
    {
597
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
598 599 600
        return NULL;
    }

601
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
602
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
603 604 605

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
606
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
607 608 609 610 611 612 613 614 615 616 617 618 619
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
620 621 622 623 624
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
625 626 627 628 629 630 631

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
632 633 634 635 636
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
637

638
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
639 640 641 642 643 644

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
645
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
646
    }
647

648 649
    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));
650 651 652 653 654 655 656
    if (!modes_64)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

    for (int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
657
    {
658
        modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
659
    }
660
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
661

662 663 664 665 666
    OCRepPayloadSetPropInt(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, g_ESWiFiConfResource.supportedFreq);
    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);
667

668
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
669
    {
670
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
671
    }
672

673
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
674 675 676 677 678
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

679 680 681
    return payload;
}

682
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
683 684 685 686 687 688 689 690
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

691
    if (g_ESCoapCloudConfResource.handle == NULL)
692
    {
693
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
694 695 696
        return NULL;
    }

697
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
698
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
699 700 701

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

        tempPayload = payload;
        payload = repPayload;

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

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
728 729 730 731 732
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
733

734
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
735 736 737 738 739 740

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
741
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
742
    }
743

744 745 746 747 748
    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);
749

750
    if (gWriteUserdataCb)
751
    {
752
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
753
    }
754

755
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
756 757 758 759 760
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

761 762 763
    return payload;
}

764
OCRepPayload* constructResponseOfDevConf(char *interface)
765 766 767 768 769 770 771 772
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

773
    if (g_ESDevConfResource.handle == NULL)
774 775 776 777 778
    {
        OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
        return NULL;
    }

779
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
780
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
781 782 783

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
784
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
785 786 787 788 789 790 791 792 793 794 795 796 797
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
798 799 800 801 802
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
803 804 805 806 807 808 809

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (char **)interfaces, interfacesDimensions);

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
810 811 812 813 814
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
815 816 817 818 819 820 821 822 823 824

        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_DEVCONF);

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
    }
825