resourcehandler.c 59.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
        return false;
76
    }
77

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

            if(!strcmp(if_ptr, iface))
            {
                return true;
            }
        }

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

    return false;
}

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

109
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
110
{
111
    gWifiConfRsrcEvtCb = cb;
112 113
}

114
void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
115
{
116
    gCoapCloudConfRsrcEvtCb = cb;
117 118 119 120 121
}

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
122 123
}

124 125 126 127 128
void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
{
    gConnectRequestEvtCb = cb;
}

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

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

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

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

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

199
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
200 201
    return res;
}
202

203
OCStackResult initWiFiConfResource(bool isSecured)
204 205 206
{
    OCStackResult res = OC_STACK_ERROR;

207 208 209 210 211 212 213 214 215 216
    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), "");
217 218 219

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

232
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
233 234 235 236
    return res;

}

237
OCStackResult initCoapCloudConfResource(bool isSecured)
238 239 240
{
    OCStackResult res = OC_STACK_ERROR;

241 242 243 244 245
    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), "");
246 247 248

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

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

}

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

270 271 272 273 274
    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), "");
275 276 277

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

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

}

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

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
    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));
        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;

            if(i < dimensions[0] &&
                (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;

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

            // TODO : Need to check appropriateness of gWiFiData
            if(gConnectRequestEvtCb != NULL)
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }

339 340
    if(ehRequest->query)
    {
341
        if(CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
342
        {
343
            // When Provisioning resource has a POST with BatchInterface
344 345
            updateCoapCloudConfResource(input);
            updateWiFiConfResource(input);
346
            updateDevConfResource(input);
347 348
        }
    }
349 350
}

351
void updateWiFiConfResource(OCRepPayload* input)
352
{
353
    ESWiFiConfData* wiFiData = (ESWiFiConfData*)OICMalloc(sizeof(ESWiFiConfData));
354

355 356 357
    if(wiFiData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
358
        return ;
359
    }
360

361 362
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
363 364
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
365 366
    wiFiData->userdata = NULL;

367
    char* ssid = NULL;
368 369
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
370
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
371
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
372
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s", g_ESWiFiConfResource.ssid);
373 374
    }

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

383
    int64_t authType = -1;
384 385
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_AUTHTYPE, &authType))
    {
386 387
        g_ESWiFiConfResource.authType = authType;
        wiFiData->authtype = g_ESWiFiConfResource.authType;
388
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u", g_ESWiFiConfResource.authType);
389 390
    }

391
    int64_t encType = -1;
392 393
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ENCTYPE, &encType))
    {
394 395
        g_ESWiFiConfResource.encType = encType;
        wiFiData->enctype = g_ESWiFiConfResource.encType;
396
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u", g_ESWiFiConfResource.encType);
397
    }
398

399
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
400
    {
401
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
402
    }
403

404 405
    if(ssid || cred || authType!= -1 || encType != -1)
    {
406
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
407 408

        // TODO : Need to check appropriateness of gWiFiData
409
        if(gWifiConfRsrcEvtCb != NULL)
410
        {
411
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
412 413 414
        }
        else
        {
415
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
416 417 418
        }
    }

419
    if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
420
    {
421
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
422 423
    }

424
    OICFree(wiFiData);
425
}
426

427
void updateCoapCloudConfResource(OCRepPayload* input)
428
{
429
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
430

431 432 433
    if(cloudData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
434
        return;
435
    }
436 437

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
438
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
439
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
440
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
441
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
442 443
    cloudData->userdata = NULL;

444
    char *authCode = NULL;
445 446
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
447
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
448
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
449
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
450 451
    }

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

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
463 464
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
465
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
466 467
    }

468
    char *authProvider = NULL;
469 470
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
471
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
472
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
473
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
474 475
    }

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

484
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
485
    {
486
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
487
    }
488

489
    if(authCode || accessToken || authProvider || ciServer)
490
    {
491
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
492 493

        // TODO : Need to check appropriateness of gCloudData
494
        if(gCoapCloudConfRsrcEvtCb != NULL)
495
        {
496
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
497 498 499
        }
        else
        {
500
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
501 502
        }
    }
503

504
    if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
505
    {
506
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
507 508
    }

509
    OICFree(cloudData);
510 511
}

512
void updateDevConfResource(OCRepPayload* input)
513
{
514
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
515

516 517 518
    if(devConfData == NULL)
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
519
        return;
520
    }
521 522
    memset(devConfData->language, 0, OIC_STRING_MAX_VALUE);
    memset(devConfData->country, 0, OIC_STRING_MAX_VALUE);
523 524
    devConfData->userdata = NULL;

525 526 527
    char *location = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_LOCATION, &location))
    {
528
        OICStrcpy(g_ESDevConfResource.location, sizeof(g_ESDevConfResource.location), location);
529
        OICStrcpy(devConfData->location, sizeof(devConfData->location), location);
530
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESDevConfResource.location %s", g_ESDevConfResource.location);
531 532
    }

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

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

549
    if(gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
550
    {
551
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
552
    }
553

554 555
    if(country || language)
    {
556
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
557 558 559 560

        // TODO : Need to check appropriateness of gDevConfData
        if(gDevConfRsrcEvtCb != NULL)
        {
561
            gDevConfRsrcEvtCb(ES_OK, devConfData);
562 563 564
        }
        else
        {
565
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
566 567
        }
    }
568

569
    if(OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
570
    {
571
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
572 573
    }

574
    OICFree(devConfData);
575 576
}

577
OCRepPayload* constructResponseOfWiFiConf(char *interface)
578 579 580 581 582 583 584 585
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

586
    if(g_ESWiFiConfResource.handle == NULL)
587
    {
588
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
589 590 591
        return NULL;
    }

592
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
593
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        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*));

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

619
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
620 621 622 623 624 625

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
626
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
627
    }
628

629 630 631
    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));
    for(int i = 0 ; i < g_ESWiFiConfResource.numMode ; ++i)
632
    {
633
        modes_64[i] = g_ESWiFiConfResource.supportedMode[i];
634
    }
635
    OCRepPayloadSetIntArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, (int64_t *)modes_64, dimensions);
636

637 638 639 640 641
    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);
642

643
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
644
    {
645
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
646
    }
647

648 649 650 651 652 653
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

654 655 656
    return payload;
}

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

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

672
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
673
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        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*));

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

699
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
700 701 702 703 704 705

        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (char **)resourceTypes, resourceTypesDimensions);
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
706
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
707
    }
708

709 710 711 712 713
    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);
714 715

    if(gWriteUserdataCb)
716
    {
717
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
718
    }
719

720 721 722 723 724 725
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

726 727 728
    return payload;
}

729
OCRepPayload* constructResponseOfDevConf(char *interface)
730 731 732 733 734 735 736 737
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

738
    if(g_ESDevConfResource.handle == NULL)
739 740 741 742 743
    {
        OIC_LOG(ERROR, ES_RH_TAG, "DevConf resource is not created");
        return NULL;
    }

744
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse DevConf res");
745
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_DEVCONF);
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        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*));

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

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

781 782 783 784 785
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_DEVNAME, g_ESDevConfResource.devName);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_MODELNUMBER, g_ESDevConfResource.modelNumber);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LOCATION, g_ESDevConfResource.location);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_LANGUAGE, g_ESDevConfResource.language);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_COUNTRY, g_ESDevConfResource.country);
786

787
    if(gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
788
    {
789
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_DEVCONF);
Jihun Ha's avatar
Jihun Ha committed
790
    }
791

792 793 794 795 796 797
    if(!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

798 799 800
    return payload;
}

801
OCRepPayload* constructResponseOfEasySetup(OCEntityHandlerRequest *ehRequest)
802 803 804 805 806 807 808 809
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

810
    // Requested interface is Link list interface
811 812 813 814 815
    //if(ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL))
    if(!ehRequest->query ||
        (ehRequest->query && !strcmp(ehRequest->query, "")) ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_LL)) ||
        (ehRequest->query && CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT)))
816 817 818 819 820
    {
        const OCRepPayload *arrayPayload[3] = {NULL};

        int childResCnt = 0;

821
        if(g_ESWiFiConfResource.handle != NULL)
822 823 824 825
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
826
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
827 828 829 830 831 832 833 834 835 836 837
                return NULL;
            }

            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
            char **resourceType = NULL;
            resourceType = (char **)OICMalloc(sizeof(char *) * 1);
            char **resourceInterface = NULL;
            resourceInterface = (char **)OICMalloc(sizeof(char *) * 1);

            if(!resourceType || !resourceInterface)
            {
838
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
839 840 841
                return NULL;
            }

842
            resourceType[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
843 844 845
            resourceInterface[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);

            add->base.type = PAYLOAD_TYPE_REPRESENTATION;
846
            OCRepPayloadSetPropString(add, OC_RSRVD_HREF, OC_RSRVD_ES_URI_WIFICONF);
847 848 849 850 851
            OCRepPayloadSetStringArray(add, OC_RSRVD_RESOURCE_TYPE,
                                            (const char **)resourceType, dimensions);
            OCRepPayloadSetStringArray(add, OC_RSRVD_INTERFACE,
                                            (const char **)resourceInterface, dimensions);

852
            OCResourceProperty p = OCGetResourceProperties((OCResourceHandle *)g_ESWiFiConfResource.handle);
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
            OCRepPayload *policy = OCRepPayloadCreate();
            if (!policy)
            {
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
                return NULL;
            }

            OCRepPayloadSetPropInt(policy, OC_RSRVD_BITMAP,
                                    ((p & OC_DISCOVERABLE) | (p & OC_OBSERVABLE)));
            if (p & OC_SECURE)
            {
                OCRepPayloadSetPropBool(policy, OC_RSRVD_SECURE, p & OC_SECURE);
                uint16_t securePort = CAGetAssignedPortNumber(ehRequest->devAddr.adapter,
                                                                    ehRequest->devAddr.flags);
                OCRepPayloadSetPropInt(policy, OC_RSRVD_HOSTING_PORT, securePort);
            }

            OCRepPayloadSetPropObject(add, OC_RSRVD_POLICY, policy);

872 873 874
            arrayPayload[childResCnt++] = add;
        }

875
        if(g_ESDevConfResource.handle != NULL)
876 877 878 879
        {
            OCRepPayload *add = OCRepPayloadCreate();
            if(!add)
            {
880
                OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
881 882 883 884 885 886 887 888 889 890 891
                return NULL;
            }

            size_t dimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
            char **resourceType = NULL;
            resourceType = (