resourcehandler.c 65.3 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
void 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 343
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE);
344 345
    }
    else
346
    {
347
        res = OCCreateResource(&g_ESDevConfResource.handle, OC_RSRVD_ES_RES_TYPE_DEVCONF,
348 349 350 351 352
        OC_RSRVD_INTERFACE_DEFAULT,
        OC_RSRVD_ES_URI_DEVCONF, OCEntityHandlerCb,
        NULL, OC_DISCOVERABLE | OC_OBSERVABLE);
    }

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 389 390 391 392
                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");
393

394 395 396 397 398 399 400 401 402
                    ///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;
                }
403 404 405

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

406 407 408 409
                OCRepPayload *repPayload = NULL;
                OCRepPayloadGetPropObject(children, OC_RSRVD_REPRESENTATION, &repPayload);

                if(NULL == repPayload)
410
                {
411
                    OIC_LOG(ERROR, ES_RH_TAG, "repPayload is null!");
412 413 414 415 416 417 418 419 420 421
                    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))
                {
422 423 424 425 426 427 428
                    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;
                    }
429 430 431 432 433 434 435 436 437 438 439 440
                }
                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);
441
            }
442

443 444 445 446
            if (hasError)
            {
               ehResult = OC_EH_BAD_REQ;
            }
447
        }
448 449 450 451 452
        else if (CompareResourceInterface(ehRequest->query, OC_RSRVD_INTERFACE_DEFAULT))
        {
            OIC_LOG(DEBUG, ES_RH_TAG, "Handling POST request on default interface");
            updateEasySetupConnectProperty(input);
        }
453
    }
454

455
    OIC_LOG(DEBUG, ES_RH_TAG, "updateEasySetupResource exit");
George Nash's avatar
George Nash committed
456
    return ehResult;
457 458 459 460
}

void updateEasySetupConnectProperty(OCRepPayload* input)
{
461 462 463 464 465
    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));
466 467 468
        if (!connectRequest)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
469
            return;
470 471
        }

472
        int cntRequest = 0;
473
        for (unsigned int i = 0 ; i < NUM_CONNECT_TYPE ; ++i)
474 475 476 477
        {
            g_ESEasySetupResource.connectRequest[i] = ES_CONNECT_NONE;
            connectRequest->connect[i] = ES_CONNECT_NONE;

478
            if (i < dimensions[0] &&
479 480 481 482 483 484 485 486 487 488 489 490
                (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;

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

495 496
            //@todo Need to check appropriateness of gWiFiData
            if (gConnectRequestEvtCb != NULL)
497 498 499 500 501 502 503 504 505
            {
                gConnectRequestEvtCb(ES_OK, connectRequest);
            }
            else
            {
                OIC_LOG(ERROR, ES_RH_TAG, "gConnectRequestEvtCb is NULL");
            }
        }
    }
506 507
}

508
OCEntityHandlerResult updateWiFiConfResource(OCRepPayload* input)
509
{
510
    OCEntityHandlerResult ehResult = OC_EH_ERROR;
511
    ESWiFiConfData* wiFiData = (ESWiFiConfData*) OICMalloc(sizeof(ESWiFiConfData));
512
    if (wiFiData == NULL)
513 514
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
515
        return ehResult;
516
    }
517

518 519 520 521
    char* ssid = NULL;
    char* cred = NULL;
    char *authType = NULL;
    char *encType = NULL;
522 523
    memset(wiFiData->ssid, 0, OIC_STRING_MAX_VALUE);
    memset(wiFiData->pwd, 0, OIC_STRING_MAX_VALUE);
524 525
    wiFiData->authtype = NONE_AUTH;
    wiFiData->enctype = NONE_AUTH;
526 527
    wiFiData->userdata = NULL;

528 529
    bool validAuthType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHTYPE, &authType))
530
    {
531 532
        WIFI_AUTHTYPE tmp;
        validAuthType = WiFiAuthTypeStringToEnum(authType, &tmp);
533
        if (validAuthType && isAuthTypeSupported(tmp))
534
        {
535
            wiFiData->authtype = tmp;
536
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.authType %u",
537 538 539 540 541 542 543
                    wiFiData->authtype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "AuthType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
544
        }
545 546
    }

547 548
    bool validEncType = false;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ENCTYPE, &encType))
549
    {
550 551
        WIFI_ENCTYPE tmp;
        validEncType = WiFiEncTypeStringToEnum(encType, &tmp);
552
        if (validEncType && isEncTypeSupported(tmp))
553
        {
554
            wiFiData->enctype = tmp;
555
            OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESWiFiConfResource.encType %u",
556 557 558 559 560 561 562
                    wiFiData->enctype);
        }
        else
        {
            OIC_LOG(ERROR, ES_RH_TAG, "EncType is not supported.");
            ehResult = OC_EH_BAD_REQ;
            goto EXIT;
563
        }
564
    }
565

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    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);
    }

592
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
593
    {
594
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_WIFICONF, &wiFiData->userdata);
Jihun Ha's avatar
Jihun Ha committed
595
    }
596

597
    if (ssid || cred || validAuthType || validEncType)
598
    {
599
        OIC_LOG(DEBUG, ES_RH_TAG, "Send WiFiConfRsrc Callback To ES");
600

601 602
        //@todo Need to check appropriateness of gWiFiData
        if (gWifiConfRsrcEvtCb != NULL)
603
        {
604
            gWifiConfRsrcEvtCb(ES_OK, wiFiData);
605 606 607
        }
        else
        {
608
            OIC_LOG(ERROR, ES_RH_TAG, "gWifiConfRsrcEvtCb is NULL");
609 610 611
        }
    }

612
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESWiFiConfResource.handle, OC_HIGH_QOS))
613
    {
614
        OIC_LOG(DEBUG, ES_RH_TAG, "Enrollee doesn't have any observer.");
615 616
    }

617 618 619
    ehResult = OC_EH_OK;

EXIT:
620 621 622 623
    OICFree(encType);
    OICFree(authType);
    OICFree(cred);
    OICFree(ssid);
624
    OICFree(wiFiData);
625
    return ehResult;
626
}
627

628
void updateCoapCloudConfResource(OCRepPayload* input)
629
{
630
    ESCoapCloudConfData* cloudData = (ESCoapCloudConfData*)OICMalloc(sizeof(ESCoapCloudConfData));
631

632
    if (cloudData == NULL)
633 634
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
635
        return;
636
    }
637 638

    memset(cloudData->authCode, 0, OIC_STRING_MAX_VALUE);
639
    memset(cloudData->accessToken, 0, OIC_STRING_MAX_VALUE);
640
    g_ESCoapCloudConfResource.accessTokenType = NONE_OAUTH_TOKENTYPE;
641
    memset(cloudData->authProvider, 0, OIC_STRING_MAX_VALUE);
642
    memset(cloudData->ciServer, 0, OIC_URI_STRING_MAX_VALUE);
643 644
    cloudData->userdata = NULL;

645
    char *authCode = NULL;
646 647
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHCODE, &authCode))
    {
648
        OICStrcpy(g_ESCoapCloudConfResource.authCode, sizeof(g_ESCoapCloudConfResource.authCode), authCode);
649
        OICStrcpy(cloudData->authCode, sizeof(cloudData->authCode), authCode);
650
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authCode %s", g_ESCoapCloudConfResource.authCode);
651 652
    }

653 654 655
    char *accessToken = NULL;
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_ACCESSTOKEN, &accessToken))
    {
656
        OICStrcpy(g_ESCoapCloudConfResource.accessToken, sizeof(g_ESCoapCloudConfResource.accessToken), accessToken);
657
        OICStrcpy(cloudData->accessToken, sizeof(cloudData->accessToken), accessToken);
658
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessToken %s", g_ESCoapCloudConfResource.accessToken);
659 660 661 662 663
    }

    int64_t accessTokenType = -1;
    if (OCRepPayloadGetPropInt(input, OC_RSRVD_ES_ACCESSTOKEN_TYPE, &accessTokenType))
    {
664 665
        g_ESCoapCloudConfResource.accessTokenType = accessTokenType;
        cloudData->accessTokenType = g_ESCoapCloudConfResource.accessTokenType;
666
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.accessTokenType %d", g_ESCoapCloudConfResource.accessTokenType);
667 668
    }

669
    char *authProvider = NULL;
670 671
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_AUTHPROVIDER, &authProvider))
    {
672
        OICStrcpy(g_ESCoapCloudConfResource.authProvider, sizeof(g_ESCoapCloudConfResource.authProvider), authProvider);
673
        OICStrcpy(cloudData->authProvider, sizeof(cloudData->authProvider), authProvider);
674
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.authServerUrl %s", g_ESCoapCloudConfResource.authProvider);
675 676
    }

677
    char *ciServer = NULL;
678 679
    if (OCRepPayloadGetPropString(input, OC_RSRVD_ES_CISERVER, &ciServer))
    {
680
        OICStrcpy(g_ESCoapCloudConfResource.ciServer, sizeof(g_ESCoapCloudConfResource.ciServer), ciServer);
681
        OICStrcpy(cloudData->ciServer, sizeof(cloudData->ciServer), ciServer);
682
        OIC_LOG_V(INFO_PRIVATE, ES_RH_TAG, "g_ESCoapCloudConfResource.ciServer %s", g_ESCoapCloudConfResource.ciServer);
683
    }
684

685
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
686
    {
687
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_COAPCLOUDCONF, &cloudData->userdata);
Jihun Ha's avatar
Jihun Ha committed
688
    }
689

690
    if (authCode || accessToken || authProvider || ciServer)
691
    {
692
        OIC_LOG(DEBUG, ES_RH_TAG, "Send CoapCloudConfRsrc Callback To ES");
693

694 695
        //@todo Need to check appropriateness of gCloudData
        if (gCoapCloudConfRsrcEvtCb != NULL)
696
        {
697
            gCoapCloudConfRsrcEvtCb(ES_OK, cloudData);
698 699 700
        }
        else
        {
701
            OIC_LOG(DEBUG, ES_RH_TAG, "gCoapCloudConfRsrcEvtCb is NULL");
702 703
        }
    }
704

705
    if (OC_STACK_NO_OBSERVERS == OCNotifyAllObservers(g_ESCoapCloudConfResource.handle, OC_HIGH_QOS))
706
    {
707
        OIC_LOG(DEBUG, ES_RH_TAG, "CoapCloudConf resource doesn't have any observer.");
708 709
    }

710
    OICFree(cloudData);
711 712
}

713
void updateDevConfResource(OCRepPayload* input)
714
{
715
    ESDevConfData* devConfData = (ESDevConfData*)OICMalloc(sizeof(ESDevConfData));
716

717
    if (devConfData == NULL)
718 719
    {
        OIC_LOG(DEBUG, ES_RH_TAG, "OICMalloc is failed");
720
        return;
721 722 723
    }
    devConfData->userdata = NULL;

724
    if (gReadUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
725
    {
726
        gReadUserdataCb(input, OC_RSRVD_ES_RES_TYPE_DEVCONF, &devConfData->userdata);
Jihun Ha's avatar
Jihun Ha committed
727
    }
728

729 730
    // If a writable property in oic.r.devconf is added later,
    // a condition for calling a resistered callback should be implemented also.
731
    if (devConfData->userdata != NULL)
732
    {
733
        OIC_LOG(DEBUG, ES_RH_TAG, "Send DevConfRsrc Callback To ES");
734

735 736
        //@todo : Need to check appropriateness of gDevConfData
        if (gDevConfRsrcEvtCb != NULL)
737
        {
738
            gDevConfRsrcEvtCb(ES_OK, devConfData);
739 740 741
        }
        else
        {
742
            OIC_LOG(DEBUG, ES_RH_TAG, "gDevConfRsrcEvtCb is NULL");
743 744
        }
    }
745

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

751
    OICFree(devConfData);
752 753
}

754
OCRepPayload* constructResponseOfWiFiConf(char *interface)
755 756 757 758 759 760 761 762
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }

763
    if (g_ESWiFiConfResource.handle == NULL)
764
    {
765
        OIC_LOG(ERROR, ES_RH_TAG, "WiFiConf resource is not created");
766 767 768
        return NULL;
    }

769
    OIC_LOG(DEBUG, ES_RH_TAG, "constructResponse WiFiConf res");
770
    OCRepPayloadSetUri(payload, OC_RSRVD_ES_URI_WIFICONF);
771 772 773

    OCRepPayload* repPayload = NULL;
    OCRepPayload* tempPayload = NULL;
774
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
775 776 777 778 779 780 781 782 783 784 785
    {
        repPayload = OCRepPayloadCreate();
        if (!repPayload)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }

        tempPayload = payload;
        payload = repPayload;

786
        size_t interfacesDimensions[MAX_REP_ARRAY_DEPTH] = {2, 0, 0};
787
        char **interfaces = (char **)OICMalloc(3 * sizeof(char*));
788 789 790 791 792
        if (!interfaces)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
793 794

        interfaces[0] = OICStrdup(OC_RSRVD_INTERFACE_DEFAULT);
795
        interfaces[1] = OICStrdup(OC_RSRVD_INTERFACE_READ_WRITE);
796

797
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_INTERFACE, (const char **)interfaces, interfacesDimensions);
798 799 800

        size_t resourceTypesDimensions[MAX_REP_ARRAY_DEPTH] = {1, 0, 0};
        char **resourceTypes = (char **)OICMalloc(2 * sizeof(char*));
801 802 803 804 805
        if (!resourceTypes)
        {
            OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
            return NULL;
        }
806

807
        resourceTypes[0] = OICStrdup(OC_RSRVD_ES_RES_TYPE_WIFICONF);
808

809
        OCRepPayloadSetStringArray(payload, OC_RSRVD_ES_RES_TYPE, (const char **)resourceTypes, resourceTypesDimensions);
810 811 812 813
    }
    else
    {
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_DEFAULT);
814
        OCRepPayloadAddInterface(payload, OC_RSRVD_INTERFACE_READ_WRITE);
815
        OCRepPayloadAddResourceType(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
816
    }
817

818 819 820
    // Do not add Read Only Properties when using OC_RSRVD_INTERFACE_READ_WRITE
    if (strcmp(interface, OC_RSRVD_INTERFACE_READ_WRITE) != 0)
    {
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
        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);
837 838
    }

839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
    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);

859 860
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_SSID, g_ESWiFiConfResource.ssid);
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_CRED, g_ESWiFiConfResource.cred);
861 862 863 864
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_AUTHTYPE,
            WiFiAuthTypeEnumToString(g_ESWiFiConfResource.authType));
    OCRepPayloadSetPropString(payload, OC_RSRVD_ES_ENCTYPE,
            WiFiEncTypeEnumToString(g_ESWiFiConfResource.encType));
865

866
    if (gWriteUserdataCb)
Jihun Ha's avatar
Jihun Ha committed
867
    {
868
        gWriteUserdataCb(payload, OC_RSRVD_ES_RES_TYPE_WIFICONF);
Jihun Ha's avatar
Jihun Ha committed
869
    }
870

871
    if (!strcmp(interface, OC_RSRVD_INTERFACE_BATCH))
872 873 874 875 876
    {
        payload = tempPayload;
        OCRepPayloadSetPropObject(payload, OC_RSRVD_REPRESENTATION, repPayload);
    }

877 878 879
    return payload;
}

880
OCRepPayload* constructResponseOfCoapCloudConf(char *interface)
881 882 883 884 885 886 887 888
{
    OCRepPayload* payload = OCRepPayloadCreate();
    if (!payload)
    {
        OIC_LOG(ERROR, ES_RH_TAG, "Failed to allocate Payload");
        return NULL;
    }