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

21
#include "resourcehandler.h"
22
#include "internal/es_util.h"
23

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

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

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

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

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

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

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

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

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

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

bool CompareResourceInterface(const char *from, const char *iface)
{
    char *if_ptr;
    GetInterfaceNameFromQuery(from, &if_ptr);
    if (!if_ptr)
    {
        return false;
    }
    if (!strcmp(if_ptr, iface))
    {
        OICFree(if_ptr);
        return true;
    }
    OICFree(if_ptr);

112 113 114
    return false;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

254 255 256 257 258 259 260 261
    res = OCBindResourceInterfaceToResource(g_ESWiFiConfResource.handle,
    OC_RSRVD_INTERFACE_READ_WRITE);
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
        return res;
    }

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

}

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

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

    if (isSecured)
    {
281 282 283 284 285 286 287
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle,
                OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
                OC_RSRVD_INTERFACE_DEFAULT,
                OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
                NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
    }
    else
288
    {
289 290 291 292 293 294 295 296 297 298 299 300 301
        res = OCCreateResource(&g_ESCoapCloudConfResource.handle,
                OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF,
                OC_RSRVD_INTERFACE_DEFAULT,
                OC_RSRVD_ES_URI_COAPCLOUDCONF, OCEntityHandlerCb,
                NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

    res = OCBindResourceInterfaceToResource(g_ESCoapCloudConfResource.handle,
            OC_RSRVD_INTERFACE_READ_WRITE);
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
        return res;
302 303
    }

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

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

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

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

329 330 331 332 333 334 335
    res = OCBindResourceInterfaceToResource(g_ESDevConfResource.handle, OC_RSRVD_INTERFACE_READ);
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
        return res;
    }

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

}

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

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
    // Below call is to allow cn update without 'rep' property.
    // Can remove if no longer needed to support this way.
    updateEasySetupConnectProperty(input);

    if (ehRequest->query)
    {
        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
        {
            // When Provisioning resource has a POST with BatchInterface
            // Parsing POST request on Batch Interface cosidering same format as GET using batch.
            OCRepPayload *children = input;

            while(children)
            {
                char* href = NULL;
                OCRepPayloadGetPropString(children, OC_RSRVD_HREF, &href);
                OIC_LOG_V(DEBUG, ES_RH_TAG, "href [%s]", href);
                ///TODO: Check why href value is null even though available in payload

                OCRepPayload *repPayload = NULL;
                OCRepPayloadGetPropObject(children, OC_RSRVD_REPRESENTATION, &repPayload);

                char* uri = children->uri;
                OIC_LOG_V(DEBUG, ES_RH_TAG, "uri [%s]", uri);

                if(NULL == uri || NULL == repPayload)
                {
                    children = children->next;
Abhishek Pandey's avatar
Abhishek Pandey committed
373 374
                    OCRepPayloadDestroy(repPayload);
                    OICFree(href);
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
                    continue;
                }

                if (0 == strcmp(uri, OC_RSRVD_ES_URI_EASYSETUP))
                {
                    updateEasySetupConnectProperty(repPayload);
                }
                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
                {
                    updateWiFiConfResource(repPayload);
                }
                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_COAPCLOUDCONF))
                {
                    updateCoapCloudConfResource(repPayload);
                }
                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_DEVCONF))
                {
                    updateDevConfResource(repPayload);
                }

                children = children->next;
                OCRepPayloadDestroy(repPayload);
                OICFree(href);
             }
        }
    }
}

void updateEasySetupConnectProperty(OCRepPayload* input)
{
405 406 407 408 409
    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));
410 411 412
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
413
            return;
414 415
        }

416
        int cntRequest = 0;
417
        for (unsigned int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
418 419 420 421
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

422
            if (i < dimensions[0] &&
423 424 425 426 427 428 429 430 431 432 433 434
                (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;

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

439 440
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
441 442 443 444 445 446 447 448 449
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }
450 451
}

452
void updateWiFiConfResource(OCRepPayload* input)
453
{
454
    ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
455

456
    if (wiFiData == NULL)
457 458
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
459
        return;
460
    }
461

462 463
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
464 465
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
466 467
    wiFiData->userdata = NULL;

468
    char* ssid = NULL;
469 470
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
471
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
472
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
473 474
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
                g_ESWiFiConfResource.ssid);
475 476
    }

477
    char* cred = NULL;
478 479
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
480
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
481
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
482 483
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
                g_ESWiFiConfResource.cred);
484 485
    }

486 487 488
    bool validAuthType = false;
    char *authType = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
489
    {
490 491 492 493 494 495 496 497 498
        WIFI_AUTHTYPE tmp;
        validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
        if (validAuthType == true)
        {
            g_ESWiFiConfResource.authType = tmp;
            wiFiData->authtype = g_ESWiFiConfResource.authType;
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
                    g_ESWiFiConfResource.authType);
        }
499 500
    }

501 502 503
    bool validEncType = false;
    char *encType = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
504
    {
505 506 507 508 509 510 511 512 513
        WIFI_ENCTYPE tmp;
        validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
        if (validEncType == true)
        {
            g_ESWiFiConfResource.encType = tmp;
            wiFiData->enctype = g_ESWiFiConfResource.encType;
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
                    g_ESWiFiConfResource.encType);
        }
514
    }
515

516
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
517
    {
518
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
519
    }
520

521
    if (ssid || cred || validAuthType || validEncType)
522
    {
523
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
524

525 526
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
527
        {
528
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
529 530 531
        }
        else
        {
532
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
533 534 535
        }
    }

536
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
537
    {
538
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
539 540
    }

541 542 543 544
    OICFree(encType);
    OICFree(authType);
    OICFree(cred);
    OICFree(ssid);
545
    OICFree(wiFiData);
546
}
547

548
void updateCoapCloudConfResource(OCRepPayload* input)
549
{
550
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
551

552
    if (cloudData == NULL)
553 554
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
555
        return;
556
    }
557 558

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
559
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
560
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
561
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
562
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
563 564
    cloudData->userdata = NULL;

565
    char *authCode = NULL;
566 567
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
568
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
569
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
570
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
571 572
    }

573 574 575
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
576
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
577
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
578
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
579 580 581 582 583
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
584 585
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
586
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
587 588
    }

589
    char *authProvider = NULL;
590 591
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
592
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
593
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
594
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
595 596
    }

597
    char *ciServer = NULL;
598 599
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
600
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
601
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
602
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
603
    }
604

605
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
606
    {
607
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
608
    }
609

610
    if (authCode || accessToken || authProvider || ciServer)
611
    {
612
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
613

614 615
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
616
        {
617
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
618 619 620
        }
        else
        {
621
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
622 623
        }
    }
624

625
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
626
    {
627
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
628 629
    }

630
    OICFree(cloudData);
631 632
}

633
void updateDevConfResource(OCRepPayload* input)
634
{
635
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
636

637
    if (devConfData == NULL)
638 639
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
640
        return;
641 642 643
    }
    devConfData->userdata = NULL;

644
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
645
    {
646
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
647
    }
648

649 650 651
    // 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 )
652
    {
653
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
654

655 656
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
657
        {
658
            gDevConfRsrcEvtCb(ES_OK, devConfData);
659 660 661
        }
        else
        {
662
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
663 664
        }
    }
665

666
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
667
    {
668
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
669 670
    }

671
    OICFree(devConfData);
672 673
}

674
OCRepPayload* constructResponseOfWiFiConf(char *interface)
675 676 677 678 679 680 681 682
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

683
    if (g_ESWiFiConfResource.handle == NULL)
684
    {
685
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
686 687 688
        return NULL;
    }

689
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
690
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
691 692 693

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
694
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
695 696 697 698 699 700 701 702 703 704 705
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

706
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
707
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
708 709 710 711 712
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
713 714

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
715
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
716

717
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
718 719 720

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
721 722 723 724 725
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
726

727
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
728

729
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
730 731 732 733
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
734
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
735
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
736
    }
737

738 739 740
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
        size_t dimensionsModes[MAX_REP_ARRAY_DEPTH] = { g_ESWiFiConfResource.numMode, 0, 0 };
        const char *modes[NUM_WIFIMODE] = { 0, };
        for (int i = 0; i < g_ESWiFiConfResource.numMode; ++i)
        {
            modes[i] = WiFiModeEnumToString(g_ESWiFiConfResource.supportedMode[i]);
        }
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIMODE, &modes[0],
                dimensionsModes);

        size_t dimensionsFreq[MAX_REP_ARRAY_DEPTH] = { g_ESWiFiConfResource.numSupportedFreq, 0, 0 };
        const char *freq[NUM_WIFIFREQ] = { 0, };
        for (int i = 0; i < g_ESWiFiConfResource.numSupportedFreq; ++i)
        {
            freq[i] = WiFiFreqEnumToString(g_ESWiFiConfResource.supportedFreq[i]);
        }
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIFREQ, freq, dimensionsFreq);
757 758
    }

759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
    size_t dimensionsAuthType[MAX_REP_ARRAY_DEPTH] = { g_ESWiFiConfResource.numSupportedAuthType, 0,
            0 };
    const char *authType[NUM_WIFIAUTHTYPE] = { 0, };
    for (int i = 0; i < g_ESWiFiConfResource.numSupportedAuthType; ++i)
    {
        authType[i] = WiFiAuthTypeEnumToString(g_ESWiFiConfResource.supportedAuthType[i]);
    }
    OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIAUTHTYPE, authType,
            dimensionsAuthType);

    size_t dimensionsEncType[MAX_REP_ARRAY_DEPTH] =
            { g_ESWiFiConfResource.numSupportedEncType, 0, 0 };
    const char *encType[NUM_WIFIENCTYPE] = { 0, };
    for (int i = 0; i < g_ESWiFiConfResource.numSupportedEncType; ++i)
    {
        encType[i] = WiFiEncTypeEnumToString(g_ESWiFiConfResource.supportedEncType[i]);
    }
    OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_SUPPORTEDWIFIENCTYPE, encType,
            dimensionsEncType);

779 780
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
781 782 783 784
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHTYPE,
            WiFiAuthTypeEnumToString(g_ESWiFiConfResource.authType));
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ENCTYPE,
            WiFiEncTypeEnumToString(g_ESWiFiConfResource.encType));
785

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

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

797 798 799
    return payload;
}

800
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
801 802 803 804 805 806 807 808
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

809
    if (g_ESCoapCloudConfResource.handle == NULL)
810
    {
811
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
812 813 814
        return NULL;
    }

815
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
816
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
817 818 819

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
820
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
821 822 823 824 825 826 827 828 829 830 831
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

832
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
833
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
834 835 836 837 838
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
839 840

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
841
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
842

843
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
844 845 846

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
847 848 849 850 851
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
852

853
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
854

855
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
856 857 858 859
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
860
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
861
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
862
    }
863

864 865 866 867 868
    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);
869

870
    if (gWriteUserdataCb)
871
    {
872
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF);
873
    }
874

875
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
876 877 878 879 880
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }