resourcehandler.c 68.4 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
#include "payload_logging.h"
29

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

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

//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
51 52 53 54 55
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);
56
OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
57
void updateEasySetupConnectProperty(OCRepPayload* input);
58
OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input);
59
void updateCoapCloudConfResource(OCRepPayload* input);
60
void updateDevConfResource(OCRepPayload* input);
61 62
bool isAuthTypeSupported(WIFI_AUTHTYPE authType);
bool isEncTypeSupported(WIFI_ENCTYPE encType);
Jihun Ha's avatar
Jihun Ha committed
63
const char *getResult(OCStackResult result);
64

65
ESConnectRequestCB gConnectRequestEvtCb = NULL;
66 67
ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
68
ESDevConfCB gDevConfRsrcEvtCb = NULL;
69

70 71 72
ESReadUserdataCb gReadUserdataCb = NULL;
ESWriteUserdataCb gWriteUserdataCb = NULL;

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

83
    while (ptr)
84
    {
85
        if (strstr(ptr, ".if."))
86 87 88 89 90
        {
            char *if_ptr = NULL;
            if_ptr = strtok(ptr, "=");
            if_ptr = strtok(NULL, "=");

91 92
            *iface = OICStrdup(if_ptr);
            break;
93
        }
94 95
        ptr = strtok(NULL, ";");
    }
96

97
    OICFree(str);
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
}

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

115 116 117
    return false;
}

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

130
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
131
{
132
    gWifiConfRsrcEvtCb = cb;
133 134
}

135
void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
136
{
137
    gCoapCloudConfRsrcEvtCb = cb;
138 139 140 141 142
}

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
143 144
}

145 146 147 148 149
void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
{
    gConnectRequestEvtCb = cb;
}

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

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

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

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

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

221
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
222 223
    return res;
}
224

225
OCStackResult initWiFiConfResource(bool isSecured)
226 227 228
{
    OCStackResult res = OC_STACK_ERROR;

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

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

257 258 259 260 261 262 263 264
    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;
    }

265
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
266 267 268 269
    return res;

}

270
OCStackResult initCoapCloudConfResource(bool isSecured)
271 272 273
{
    OCStackResult res = OC_STACK_ERROR;

274
    OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
275 276
    OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken),
            "");
277
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
278 279
    OICStrcpy(g_ESCoapCloudConfResource.authProvider,
            sizeof(g_ESCoapCloudConfResource.authProvider), "");
280
    OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
281 282 283

    if (isSecured)
    {
284 285 286 287 288 289 290
        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
291
    {
292 293 294 295 296 297 298 299 300 301 302 303 304
        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;
305 306
    }

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

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

315
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
316 317 318

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

332 333 334 335 336 337 338
    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;
    }

339
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
340 341 342 343
    return res;

}

344 345
OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest,
    OCRepPayload* input)
346
{
347
    OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
348

349
    OCEntityHandlerResult ehResult = OC_EH_OK;
350 351 352 353
    if (ehRequest->query)
    {
        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
        {
354
            bool hasError = false;
355 356 357 358 359
            // 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)
            {
360 361 362 363 364
                char* uri = children->uri;
                if(NULL == uri)
                {
                    OIC_LOG(DEBUG, ES_RH_TAG,
                        "No URI found in request, applying same request to all links in collection");
365

366 367 368 369 370 371 372 373 374
                    ///TODO: Need to check if "input" should be passed, or the value of property OC_RSRVD_REPRESENTATION.
                    updateEasySetupConnectProperty(input);
                    updateWiFiConfResource(input);
                    updateCoapCloudConfResource(input);
                    updateDevConfResource(input);

                    // As the request is applied to children already, no need to check next child.
                    break;
                }
375 376 377

                OIC_LOG_V(DEBUG, ES_RH_TAG, "uri [%s]", uri);

378 379 380 381
                OCRepPayload *repPayload = NULL;
                OCRepPayloadGetPropObject(children, OC_RSRVD_REPRESENTATION, &repPayload);

                if(NULL == repPayload)
382
                {
383
                    OIC_LOG(ERROR, ES_RH_TAG, "repPayload is null!");
384 385 386 387 388 389 390 391 392 393
                    children = children->next;
                    continue;
                }

                if (0 == strcmp(uri, OC_RSRVD_ES_URI_EASYSETUP))
                {
                    updateEasySetupConnectProperty(repPayload);
                }
                else if (0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
                {
394 395 396 397 398 399 400
                    if (updateWiFiConfResource(repPayload) != OC_EH_OK)
                    {
                        // Possibility of failure exist only when updating WiFiConf resource.
                        // So error code is checked only for this function.
                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to update WiFiConf resource.");
                        hasError = true;
                    }
401 402 403 404 405 406 407 408 409 410 411 412 413
                }
                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);
             }
414 415 416 417 418

             if (hasError)
             {
                ehResult = OC_EH_BAD_REQ;
             }
419
        }
420 421 422 423 424
        else if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Handling POST request on default interface");
            updateEasySetupConnectProperty(input);
        }
425
    }
426
    OIC_LOG(DEBUG, ES_RH_TAG, "updateEasySetupResource exit");
George Nash's avatar
George Nash committed
427
    return ehResult;
428 429 430 431
}

void updateEasySetupConnectProperty(OCRepPayload* input)
{
432 433 434 435 436
    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));
437 438 439
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
440
            return;
441 442
        }

443
        int cntRequest = 0;
444
        for (unsigned int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
445 446 447 448
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

449
            if (i < dimensions[0] &&
450 451 452 453 454 455 456 457 458 459 460 461
                (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;

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

466 467
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
468 469 470 471 472 473 474 475 476
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }
477 478
}

479
OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input)
480
{
481
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
482
    ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
483
    if (wiFiData == NULL)
484 485
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
486
        return ehResult;
487
    }
488

489 490 491 492
    char* ssid = NULL;
    char* cred = NULL;
    char *authType = NULL;
    char *encType = NULL;
493 494
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
495 496
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
497 498
    wiFiData->userdata = NULL;

499 500
    bool validAuthType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
501
    {
502 503
        WIFI_AUTHTYPE tmp;
        validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
504
        if (validAuthType && isAuthTypeSupported(tmp))
505
        {
506
            wiFiData->authtype = tmp;
507
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
508 509 510 511 512 513 514
                    wiFiData->authtype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "AuthType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
515
        }
516 517
    }

518 519
    bool validEncType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
520
    {
521 522
        WIFI_ENCTYPE tmp;
        validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
523
        if (validEncType && isEncTypeSupported(tmp))
524
        {
525
            wiFiData->enctype = tmp;
526
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
527 528 529 530 531 532 533
                    wiFiData->enctype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "EncType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
534
        }
535
    }
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    if (validAuthType)
    {
        g_ESWiFiConfResource.authType = wiFiData->authtype;
    }

    if (validEncType)
    {
        g_ESWiFiConfResource.encType = wiFiData->enctype;
    }

    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_SSID, &ssid))
    {
        OICStrcpy(g_ESWiFiConfResource.ssid, sizeof(g_ESWiFiConfResource.ssid), ssid);
        OICStrcpy(wiFiData->ssid, sizeof(wiFiData->ssid), ssid);
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.ssid : %s",
                g_ESWiFiConfResource.ssid);
    }

    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CRED, &cred))
    {
        OICStrcpy(g_ESWiFiConfResource.cred, sizeof(g_ESWiFiConfResource.cred), cred);
        OICStrcpy(wiFiData->pwd, sizeof(wiFiData->pwd), cred);
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.cred %s",
                g_ESWiFiConfResource.cred);
    }

563
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
564
    {
565
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
566
    }
567

568
    if (ssid || cred || validAuthType || validEncType)
569
    {
570
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
571

572 573
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
574
        {
575
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
576 577 578
        }
        else
        {
579
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
580 581 582
        }
    }

583
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
584
    {
585
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
586 587
    }

588 589 590
    ehResult = OC_EH_OK;

EXIT:
591 592 593 594
    OICFree(encType);
    OICFree(authType);
    OICFree(cred);
    OICFree(ssid);
595
    OICFree(wiFiData);
596
    return ehResult;
597
}
598

599
void updateCoapCloudConfResource(OCRepPayload* input)
600
{
601
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
602

603
    if (cloudData == NULL)
604 605
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
606
        return;
607
    }
608 609

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
610
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
611
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
612
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
613
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
614 615
    cloudData->userdata = NULL;

616
    char *authCode = NULL;
617 618
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
619
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
620
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
621
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
622 623
    }

624 625 626
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
627
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
628
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
629
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
630 631 632 633 634
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
635 636
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
637
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
638 639
    }

640
    char *authProvider = NULL;
641 642
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
643
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
644
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
645
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
646 647
    }

648
    char *ciServer = NULL;
649 650
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
651
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
652
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
653
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
654
    }
655

656
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
657
    {
658
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
659
    }
660

661
    if (authCode || accessToken || authProvider || ciServer)
662
    {
663
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
664

665 666
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
667
        {
668
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
669 670 671
        }
        else
        {
672
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
673 674
        }
    }
675

676
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
677
    {
678
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
679 680
    }

681
    OICFree(cloudData);
682 683
}

684
void updateDevConfResource(OCRepPayload* input)
685
{
686
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
687

688
    if (devConfData == NULL)
689 690
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
691
        return;
692 693 694
    }
    devConfData->userdata = NULL;

695
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
696
    {
697
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
698
    }
699

700 701
    // If a writable property in oic.r.devconf is added later,
    // a condition for calling a resistered callback should be implemented also.
702
    if (devConfData->userdata != NULL)
703
    {
704
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
705

706 707
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
708
        {
709
            gDevConfRsrcEvtCb(ES_OK, devConfData);
710 711 712
        }
        else
        {
713
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
714 715
        }
    }
716

717
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
718
    {
719
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
720 721
    }

722
    OICFree(devConfData);
723 724
}

725
OCRepPayload* constructResponseOfWiFiConf(char *interface)
726 727 728 729 730 731 732 733
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

734
    if (g_ESWiFiConfResource.handle == NULL)
735
    {
736
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
737 738 739
        return NULL;
    }

740
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
741
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
742 743 744

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
745
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
746 747 748 749 750 751 752 753 754 755 756
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

757
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
758
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
759 760 761 762 763
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
764 765

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
766
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
767

768
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
769 770 771

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
772 773 774 775 776
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
777

778
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
779

780
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
781 782 783 784
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
785
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
786
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
787
    }
788

789 790 791
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
        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);
808 809
    }

810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
    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);

830 831
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
832 833 834 835
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHTYPE,
            WiFiAuthTypeEnumToString(g_ESWiFiConfResource.authType));
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ENCTYPE,
            WiFiEncTypeEnumToString(g_ESWiFiConfResource.encType));
836

837
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
838
    {
839
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
840
    }
841

842
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
843 844 845 846 847
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

848 849 850
    return payload;
}

851
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
852 853 854 855 856 857 858 859
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

860
    if (g_ESCoapCloudConfResource.handle == NULL)
861
    {
862
        OIC_LOG(ERROR, ES_RH_TAG, "CoapCloudConf resource is not created");
863 864 865
        return NULL;
    }

866
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse CoapCloudConf res");
867
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_COAPCLOUDCONF);
868 869 870

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
871
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
872 873 874 875 876 877 878 879 880 881 882
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

883
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
884
        char **interfaces = (char **)OICMalloc(3