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

21
#include "resourcehandler.h"
22

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
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
274 275 276

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

289
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
290 291 292 293
    return res;

}

294
void updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input)
295
{
296
    OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
297

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

309 310 311 312 313 314
        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;

315
            if (i < dimensions[0] &&
316 317 318 319 320 321 322 323 324 325 326 327
                (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;

328
        if (g_ESEasySetupResource.connectRequest[0] != ES_CONNECT_NONE)
329 330 331
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Send ConnectRequest Callback To ES");

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

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

356
void updateWiFiConfResource(OCRepPayload* input)
357
{
358
    ESWiFiConfData* wiFiData = (ESWiFiConfData*)OICMalloc(sizeof(ESWiFiConfData));
359

360
    if (wiFiData == NULL)
361 362
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
363
        return ;
364
    }
365

366 367
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
368 369
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
370 371
    wiFiData->userdata = NULL;

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

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

388
    int64_t authType = -1;
389 390
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
391 392
        g_ESWiFiConfResource.authType = authType;
        wiFiData->authtype = g_ESWiFiConfResource.authType;
393
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u", g_ESWiFiConfResource.authType);
394 395
    }

396
    int64_t encType = -1;
397 398
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
399 400
        g_ESWiFiConfResource.encType = encType;
        wiFiData->enctype = g_ESWiFiConfResource.encType;
401
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u", g_ESWiFiConfResource.encType);
402
    }
403

404
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
405
    {
406
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
407
    }
408

409
    if (ssid || cred || authType!= -1 || encType != -1)
410
    {
411
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
412

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

424
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
425
    {
426
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
427 428
    }

429
    OICFree(wiFiData);
430
}
431

432
void updateCoapCloudConfResource(OCRepPayload* input)
433
{
434
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
435

436
    if (cloudData == NULL)
437 438
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
439
        return;
440
    }
441 442

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
443
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
444
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
445
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
446
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
447 448
    cloudData->userdata = NULL;

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

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

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
468 469
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
470
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
471 472
    }

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

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

489
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
490
    {
491
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
492
    }
493

494
    if (authCode || accessToken || authProvider || ciServer)
495
    {
496
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
497

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

509
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
510
    {
511
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
512 513
    }

514
    OICFree(cloudData);
515 516
}

517
void updateDevConfResource(OCRepPayload* input)
518
{
519
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
520

521
    if (devConfData == NULL)
522 523
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
524
        return;
525 526 527
    }
    devConfData->userdata = NULL;

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

533 534 535
    // 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 )
536
    {
537
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
538

539 540
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
541
        {
542
            gDevConfRsrcEvtCb(ES_OK, devConfData);
543 544 545
        }
        else
        {
546
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
547 548
        }
    }
549

550
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
551
    {
552
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
553 554
    }

555
    OICFree(devConfData);
556 557
}

558
OCRepPayload* constructResponseOfWiFiConf(char *interface)
559 560 561 562 563 564 565 566
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

567
    if (g_ESWiFiConfResource.handle == NULL)
568
    {
569
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
570 571 572
        return NULL;
    }

573
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
574
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
575 576 577

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
578
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
579 580 581 582 583 584 585 586 587 588 589 590 591
    {
        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*));
592 593 594 595 596
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
597 598 599 600 601 602 603

        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*));
604 605 606 607 608
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
609

610
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
611 612 613 614 615 616

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
617
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
618
    }
619

620 621
    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));
622 623 624 625 626 627 628
    if (!modes_64)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

    for (int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
629
    {
630
        modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
631
    }
632
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
633

634 635 636 637 638
    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);
639

640
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
641
    {
642
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
643
    }
644

645
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
646 647 648 649 650
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

651 652 653
    return payload;
}

654
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
655 656 657 658 659 660 661 662
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

663
    if (g_ESCoapCloudConfResource.handle == NULL)
664
    {
665
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
666 667 668
        return NULL;
    }

669
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
670
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
671 672 673

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
674
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
675 676 677 678 679 680 681 682 683 684 685 686 687
    {
        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*));
688 689 690 691 692
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
693 694 695 696 697 698 699

        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*));
700 701 702 703 704
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
705

706
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
707 708 709 710 711 712

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
713
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
714
    }
715

716 717 718 719 720
    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);
721

722
    if (gWriteUserdataCb)
723
    {
724
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
725
    }
726

727
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
728 729 730 731 732
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

733 734 735
    return payload;
}

736
OCRepPayload* constructResponseOfDevConf(char *interface)
737 738 739 740 741 742 743 744
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

745
    if (g_ESDevConfResource.handle == NULL)
746 747 748 749 750
    {
        OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
        return NULL;
    }

751
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
752
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
753 754 755

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
756
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
757 758 759 760 761 762 763 764 765 766 767 768 769
    {
        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*));
770 771 772 773 774
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
775 776 777 778 779 780 781

        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*));
782 783 784 785 786
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
787 788 789 790 791 792 793 794 795 796

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

798
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, g_ESDevConfResource.devName);
799

800
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
801
    {
802
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
Jihun Ha's avatar
Jihun Ha committed
803
    }
804

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

811 812 813
    return payload;
}

814
OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
815 816 817 818 819 820 821 822
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

823
    // Requested interface is Link list interface
824
    if (!ehRequest->query ||
825 826 827
        (ehRequest->query && !strcmp(ehRequest->query, "")) ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
828 829 830 831 832
    {
        const OCRepPayload *arrayPayload[3] = {NULL};

        int childResCnt = 0;

833
        if (g_ESWiFiConfResource.handle != NULL)
834 835
        {
            OCRepPayload *add = OCRepPayloadCreate();
836
            if (!add)
837
            {
838
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");