resourcehandler.c 67.1 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
// TODO: Remove this flag and enable the code it guards.
// This is a temporary workaround to ignore the failure of OCLinksPayloadArrayCreate
// in some cases. This allows the response to still be made, even though links property will be
// missing.
// Bug: IOT-2762
#define ES_IGNORE_OCLinksPayloadArrayCreate_FAILIURE

37 38 39 40 41 42 43 44 45 46
/**
 * @var ES_RH_TAG
 * @brief Logging tag for module name.
 */
#define ES_RH_TAG "ES_RH"
//-----------------------------------------------------------------------------
// Private variables
//-----------------------------------------------------------------------------

/**
47
 * @var g_ESEasySetupResource
48
 * @brief Structure for holding the Provisioning status
49
 */
50 51 52
EasySetupResource g_ESEasySetupResource;
WiFiConfResource g_ESWiFiConfResource;
CoapCloudConfResource g_ESCoapCloudConfResource;
53
DevConfResource g_ESDevConfResource;
54 55 56 57

//-----------------------------------------------------------------------------
// Private internal function prototypes
//-----------------------------------------------------------------------------
58 59 60 61 62
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);
63
OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest, OCRepPayload* input);
64
void updateEasySetupConnectProperty(OCRepPayload* input);
65
OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input);
66
void updateCoapCloudConfResource(OCRepPayload* input);
67
OCEntityHandlerResult updateDevConfResource(OCRepPayload* input);
68 69
bool isAuthTypeSupported(WIFI_AUTHTYPE authType);
bool isEncTypeSupported(WIFI_ENCTYPE encType);
Jihun Ha's avatar
Jihun Ha committed
70
const char *getResult(OCStackResult result);
71

72
ESConnectRequestCB gConnectRequestEvtCb = NULL;
73 74
ESWiFiConfCB gWifiConfRsrcEvtCb = NULL;
ESCoapCloudConfCB gCoapCloudConfRsrcEvtCb = NULL;
75
ESDevConfCB gDevConfRsrcEvtCb = NULL;
76

77 78 79
ESReadUserdataCb gReadUserdataCb = NULL;
ESWriteUserdataCb gWriteUserdataCb = NULL;

80
void GetInterfaceNameFromQuery(const char *query, char **iface)
81
{
82
    if (!iface)
83
    {
84
        return;
85
    }
86 87 88
    *iface = NULL;
    char *str = OICStrdup(query);
    char *ptr = strtok(str, ";");
89

90
    while (ptr)
91
    {
92
        if (strstr(ptr, ".if."))
93 94 95 96 97
        {
            char *if_ptr = NULL;
            if_ptr = strtok(ptr, "=");
            if_ptr = strtok(NULL, "=");

98 99
            *iface = OICStrdup(if_ptr);
            break;
100
        }
101 102
        ptr = strtok(NULL, ";");
    }
103

104
    OICFree(str);
105 106 107 108
}

bool CompareResourceInterface(const char *from, const char *iface)
{
109
    char *if_ptr = NULL;
110 111 112 113 114 115 116 117 118 119 120 121
    GetInterfaceNameFromQuery(from, &if_ptr);
    if (!if_ptr)
    {
        return false;
    }
    if (!strcmp(if_ptr, iface))
    {
        OICFree(if_ptr);
        return true;
    }
    OICFree(if_ptr);

122 123 124
    return false;
}

125 126
ESResult SetCallbackForUserData(ESReadUserdataCb readCb, ESWriteUserdataCb writeCb)
{
127
    if (!readCb && !writeCb)
128
    {
129
        OIC_LOG(DEBUG, ES_RH_TAG, "Both of callbacks for user data are null");
130 131 132 133 134 135 136
        return ES_ERROR;
    }
    gReadUserdataCb = readCb;
    gWriteUserdataCb = writeCb;
    return ES_OK;
}

137
void RegisterWifiRsrcEventCallBack(ESWiFiConfCB cb)
138
{
139
    gWifiConfRsrcEvtCb = cb;
140 141
}

142
void RegisterCloudRsrcEventCallBack(ESCoapCloudConfCB cb)
143
{
144
    gCoapCloudConfRsrcEvtCb = cb;
145 146 147 148 149
}

void RegisterDevConfRsrcEventCallBack(ESDevConfCB cb)
{
    gDevConfRsrcEvtCb = cb;
150 151
}

152 153 154 155 156
void RegisterConnectRequestEventCallBack(ESConnectRequestCB cb)
{
    gConnectRequestEvtCb = cb;
}

157 158
void UnRegisterResourceEventCallBack()
{
159
    if (gWifiConfRsrcEvtCb)
160
    {
161
        gWifiConfRsrcEvtCb = NULL;
162
    }
163
    if (gCoapCloudConfRsrcEvtCb)
164
    {
165
        gCoapCloudConfRsrcEvtCb = NULL;
166 167 168 169
    }
    if (gDevConfRsrcEvtCb)
    {
        gDevConfRsrcEvtCb = NULL;
170
    }
171 172 173 174
    if (gConnectRequestEvtCb)
    {
        gConnectRequestEvtCb = NULL;
    }
175 176
}

177
OCStackResult initEasySetupResource(bool isSecured)
178
{
179 180
    g_ESEasySetupResource.status = ES_STATE_INIT;
    g_ESEasySetupResource.lastErrCode = ES_ERRCODE_NO_ERROR;
181
    for (int i = 0; i < NUM_CONNECT_TYPE; ++i)
182 183 184 185
    {
        g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
    }
    g_ESEasySetupResource.numRequest = 0;
186

187 188 189
    OCStackResult res = OC_STACK_ERROR;
    if (isSecured)
    {
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 | OC_SECURE);
194 195
    }
    else
196
    {
197
        res = OCCreateResource(&g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_EASYSETUP,
198
        OC_RSRVD_INTERFACE_DEFAULT,
199
        OC_RSRVD_ES_URI_EASYSETUP, OCEntityHandlerCb,
200
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
201
    }
202
    if (res != OC_STACK_OK)
203
    {
204
        OIC_LOG_V(ERROR, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
205 206 207
        return res;
    }

208
    res = OCBindResourceTypeToResource(g_ESEasySetupResource.handle, OC_RSRVD_ES_RES_TYPE_COL);
209
    if (res != OC_STACK_OK)
210
    {
211
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource type with result: %s", getResult(res));
212 213 214
        return res;
    }

215
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_LL);
216
    if (res != OC_STACK_OK)
217
    {
218
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
219 220
        return res;
    }
221
    res = OCBindResourceInterfaceToResource(g_ESEasySetupResource.handle, OC_RSRVD_INTERFACE_BATCH);
222
    if (res != OC_STACK_OK)
223
    {
224
        OIC_LOG_V(ERROR, ES_RH_TAG, "Binding Resource interface with result: %s", getResult(res));
225 226
        return res;
    }
227

228
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created EasySetup resource with result: %s", getResult(res));
229 230
    return res;
}
231

232
OCStackResult initWiFiConfResource(bool isSecured)
233 234 235
{
    OCStackResult res = OC_STACK_ERROR;

236 237 238
    g_ESWiFiConfResource.supportedFreq[0] = WIFI_24G;
    g_ESWiFiConfResource.supportedFreq[1] = WIFI_5G;
    g_ESWiFiConfResource.numSupportedFreq=2;
239 240 241 242 243 244 245 246 247
    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), "");
248 249 250

    if (isSecured)
    {
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
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
255 256
    }
    else
257
    {
258
        res = OCCreateResource(&g_ESWiFiConfResource.handle, OC_RSRVD_ES_RES_TYPE_WIFICONF,
259
        OC_RSRVD_INTERFACE_DEFAULT,
260
        OC_RSRVD_ES_URI_WIFICONF, OCEntityHandlerCb,
261 262 263
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

264 265 266 267 268 269 270
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Failed to create WiFiConf resource with result: %s",
            getResult(res));
        return res;
    }

271 272 273 274 275 276 277 278
    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;
    }

279
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created WiFiConf resource with result: %s", getResult(res));
280 281 282 283
    return res;

}

284
OCStackResult initCoapCloudConfResource(bool isSecured)
285 286 287
{
    OCStackResult res = OC_STACK_ERROR;

288
    OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), "");
289 290
    OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken),
            "");
291
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
292 293
    OICStrcpy(g_ESCoapCloudConfResource.authProvider,
            sizeof(g_ESCoapCloudConfResource.authProvider), "");
294
    OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), "");
295 296 297

    if (isSecured)
    {
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 | OC_SECURE);
    }
    else
305
    {
306 307 308 309 310 311 312
        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);
    }

313 314 315 316 317 318 319
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Failed to create CoapCloudConf resource with result: %s",
            getResult(res));
        return res;
    }

320 321 322 323 324 325
    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;
326 327
    }

328
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created CoapCloudConf resource with result: %s", getResult(res));
329 330 331 332 333 334 335
    return res;
}

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

336
    OICStrcpy(g_ESDevConfResource.devName, sizeof(g_ESDevConfResource.devName), "");
337 338 339

    if (isSecured)
    {
340
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
341 342
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
343
        NULL, OC_DISCOVERABLE | OC_SECURE);
344 345
    }
    else
346
    {
347
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
348 349
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
350
        NULL, OC_DISCOVERABLE);
351 352
    }

353 354 355 356 357 358 359
    if (res != OC_STACK_OK)
    {
        OIC_LOG_V(ERROR, ES_RH_TAG, "Failed to create DevConf resource with result: %s",
            getResult(res));
        return res;
    }

360 361 362 363 364 365 366
    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;
    }

367
    OIC_LOG_V(DEBUG, ES_RH_TAG, "Created DevConf resource with result: %s", getResult(res));
368 369 370 371
    return res;

}

372 373
OCEntityHandlerResult updateEasySetupResource(OCEntityHandlerRequest* ehRequest,
    OCRepPayload* input)
374
{
375
    OIC_LOG_V(DEBUG, ES_RH_TAG, "g_ESEasySetupResource.status %d", g_ESEasySetupResource.status);
376

377
    OCEntityHandlerResult ehResult = OC_EH_OK;
378 379 380 381
    if (ehRequest->query)
    {
        if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_BATCH))
        {
382
            bool hasError = false;
383 384 385 386 387
            // 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)
            {
388
                char* uri = children->uri;
389
                if (NULL == uri)
390
                {
391 392 393 394 395
                    OIC_LOG(DEBUG, ES_RH_TAG, "No URI found in request");
                }
                else
                {
                    OIC_LOG_V(DEBUG, ES_RH_TAG, "Request URI [%s]", uri);
396
                }
397

398 399
                OCRepPayload *repPayload = NULL;
                OCRepPayloadGetPropObject(children, OC_RSRVD_REPRESENTATION, &repPayload);
400
                if (NULL == repPayload)
401
                {
402
                    OIC_LOG(ERROR, ES_RH_TAG, "repPayload is null!");
403 404 405 406
                    children = children->next;
                    continue;
                }

407 408 409
                // If uri is NULL, rep is applied to all resources in collection;
                // otherwise its applied to specific target resources.
                if (NULL == uri || 0 == strlen(uri) || 0 == strcmp(uri, OC_RSRVD_ES_URI_EASYSETUP))
410
                {
411 412 413 414 415 416 417 418 419 420 421
                    // If payload has read-only properties, then the request is considered as a bad request.
                    if (!OCRepPayloadIsNull(children, OC_RSRVD_ES_PROVSTATUS) ||
                        !OCRepPayloadIsNull(children, OC_RSRVD_ES_LAST_ERRORCODE))
                    {
                        OIC_LOG(ERROR, ES_RH_TAG, "Read-only property cannot be updated.");
                        hasError = true;
                    }
                    else
                    {
                        updateEasySetupConnectProperty(repPayload);
                    }
422
                }
423 424 425

                if (NULL == uri || 0 == strlen(uri)
                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
426
                {
427 428 429 430 431
                    if (updateWiFiConfResource(repPayload) != OC_EH_OK)
                    {
                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to update WiFiConf resource.");
                        hasError = true;
                    }
432
                }
433 434 435

                if (NULL == uri ||  0 == strlen(uri)
                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_COAPCLOUDCONF))
436 437 438
                {
                    updateCoapCloudConfResource(repPayload);
                }
439 440 441

                if (NULL == uri ||  0 == strlen(uri)
                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_DEVCONF))
442
                {
443 444 445 446 447
                    if (updateDevConfResource(repPayload) != OC_EH_OK)
                    {
                        OIC_LOG(ERROR, ES_RH_TAG, "Failed to update DevConf resource.");
                        hasError = true;
                    }
448 449 450 451
                }

                children = children->next;
                OCRepPayloadDestroy(repPayload);
452
            }
453

454 455 456 457
            if (hasError)
            {
               ehResult = OC_EH_BAD_REQ;
            }
458
        }
459 460 461
        else if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Handling POST request on default interface");
462 463 464 465 466 467 468 469 470 471 472
            // If payload has read-only properties, then the request is considered as a bad request.
            if (!OCRepPayloadIsNull(input, OC_RSRVD_ES_PROVSTATUS) ||
                !OCRepPayloadIsNull(input, OC_RSRVD_ES_LAST_ERRORCODE))
            {
                OIC_LOG(ERROR, ES_RH_TAG, "Read-only property cannot be updated.");
                ehResult = OC_EH_BAD_REQ;
            }
            else
            {
                updateEasySetupConnectProperty(input);
            }
473
        }
474
    }
475

476
    OIC_LOG(DEBUG, ES_RH_TAG, "updateEasySetupResource exit");
George Nash's avatar
George Nash committed
477
    return ehResult;
478 479 480 481
}

void updateEasySetupConnectProperty(OCRepPayload* input)
{
482 483 484 485 486
    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));
487 488 489
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
490
            return;
491 492
        }

493
        int cntRequest = 0;
494
        for (unsigned int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
495 496 497 498
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

499
            if (i < dimensions[0] &&
500 501 502 503 504 505 506 507 508 509 510 511
                (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;

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

516 517
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
518 519 520 521 522 523 524 525 526
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }
527 528
}

529
OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input)
530
{
531 532 533 534 535 536 537 538 539 540
    // If payload has read-only properties, then the request is considered as a bad request.
    if (!OCRepPayloadIsNull(input, OC_RSRVD_ES_SUPPORTEDWIFIMODE) ||
        !OCRepPayloadIsNull(input, OC_RSRVD_ES_SUPPORTEDWIFIFREQ) ||
        !OCRepPayloadIsNull(input, OC_RSRVD_ES_SUPPORTEDWIFIAUTHTYPE) ||
        !OCRepPayloadIsNull(input, OC_RSRVD_ES_SUPPORTEDWIFIENCTYPE))
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Read-only property cannot be updated.");
        return OC_EH_BAD_REQ;
    }

541
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
542
    ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
543
    if (wiFiData == NULL)
544 545
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
546
        return ehResult;
547
    }
548

549 550 551 552
    char* ssid = NULL;
    char* cred = NULL;
    char *authType = NULL;
    char *encType = NULL;
553 554
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
555 556
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
557 558
    wiFiData->userdata = NULL;

559 560
    bool validAuthType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
561
    {
562 563
        WIFI_AUTHTYPE tmp;
        validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
564
        if (validAuthType && isAuthTypeSupported(tmp))
565
        {
566
            wiFiData->authtype = tmp;
567
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
568 569 570 571 572 573 574
                    wiFiData->authtype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "AuthType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
575
        }
576 577
    }

578 579
    bool validEncType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
580
    {
581 582
        WIFI_ENCTYPE tmp;
        validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
583
        if (validEncType && isEncTypeSupported(tmp))
584
        {
585
            wiFiData->enctype = tmp;
586
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
587 588 589 590 591 592 593
                    wiFiData->enctype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "EncType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
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 619 620 621 622
    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);
    }

623
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
624
    {
625
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
626
    }
627

628
    if (ssid || cred || validAuthType || validEncType)
629
    {
630
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
631

632 633
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
634
        {
635
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
636 637 638
        }
        else
        {
639
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
640 641 642
        }
    }

643
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
644
    {
645 646 647 648 649 650
        OIC_LOG(DEBUG, ES_RH_TAG, "WiFiConf resource doesn't have any observer.");
    }

    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "EasySetup resource doesn't have any observer.");
651 652
    }

653 654 655
    ehResult = OC_EH_OK;

EXIT:
656 657 658 659
    OICFree(encType);
    OICFree(authType);
    OICFree(cred);
    OICFree(ssid);
660
    OICFree(wiFiData);
661
    return ehResult;
662
}
663

664
void updateCoapCloudConfResource(OCRepPayload* input)
665
{
666
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
667

668
    if (cloudData == NULL)
669 670
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
671
        return;
672
    }
673 674

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
675
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
676
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
677
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
678
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
679 680
    cloudData->userdata = NULL;

681
    char *authCode = NULL;
682 683
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
684
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
685
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
686
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
687 688
    }

689 690 691
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
692
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
693
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
694
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
695 696 697 698 699
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
700 701
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
702
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
703 704
    }

705
    char *authProvider = NULL;
706 707
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
708
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
709
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
710
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
711 712
    }

713
    char *ciServer = NULL;
714 715
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
716
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
717
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
718
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
719
    }
720

721
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
722
    {
723
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
724
    }
725

726
    if (authCode || accessToken || authProvider || ciServer)
727
    {
728
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
729

730 731
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
732
        {
733
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
734 735 736
        }
        else
        {
737
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
738 739
        }
    }
740

741
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
742
    {
743
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
744 745
    }

746 747 748 749 750
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESEasySetupResource.handle, OC_HIGH_QOS))
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "EasySetup resource doesn't have any observer.");
    }

751
    OICFree(cloudData);
752 753
}

754
OCEntityHandlerResult updateDevConfResource(OCRepPayload* input)
755
{
756
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
757

758
    if (devConfData == NULL)
759 760
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
761
        return OC_EH_ERROR;
762 763 764
    }
    devConfData->userdata = NULL;

765
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
766
    {
767
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
768
    }
769

770 771
    // If a writable property in oic.r.devconf is added later,
    // a condition for calling a resistered callback should be implemented also.
772
    if (devConfData->userdata != NULL)
773
    {
774
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
775

776 777
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
778
        {
779
            gDevConfRsrcEvtCb(ES_OK, devConfData);
780 781 782
        }
        else
        {
783
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
784 785
        }
    }
786

787
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESDevConfResource.handle, OC_HIGH_QOS))
788
    {
789
        OIC_LOG(DEBUG, ES_RH_TAG, "devConfResource doesn't have any observer.");
790 791
    }

792
    OICFree(devConfData);
793
    return OC_EH_OK;
794 795
}

796
OCRepPayload* constructResponseOfWiFiConf(char *interface)
797 798 799 800 801 802 803 804
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

805
    if (g_ESWiFiConfResource.handle == NULL)
806
    {
807
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
808 809 810
        return NULL;
    }

811
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
812
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
813 814 815

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

        tempPayload = payload;
        payload = repPayload;

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

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
837
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
838

839
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
840 841 842

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

849
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
850

851
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
852 853 854 855
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
856
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
857
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
858
    }
859

860 861 862
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
863 864 865 866 867 <