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

21
#include "resourcehandler.h"
22
#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
                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
                {
                    updateEasySetupConnectProperty(repPayload);
                }
413 414 415

                if (NULL == uri || 0 == strlen(uri)
                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_WIFICONF))
416
                {
417 418 419 420 421 422 423
                    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;
                    }
424
                }
425 426 427

                if (NULL == uri ||  0 == strlen(uri)
                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_COAPCLOUDCONF))
428 429 430
                {
                    updateCoapCloudConfResource(repPayload);
                }
431 432 433

                if (NULL == uri ||  0 == strlen(uri)
                    || 0 == strcmp(uri, OC_RSRVD_ES_URI_DEVCONF))
434 435 436 437 438 439
                {
                    updateDevConfResource(repPayload);
                }

                children = children->next;
                OCRepPayloadDestroy(repPayload);
440
            }
441

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

616 617 618
    ehResult = OC_EH_OK;

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

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

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

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

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

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

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

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

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

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

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

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

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

709
    OICFree(cloudData);
710 711
}

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

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

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

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

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

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

750
    OICFree(devConfData);
751 752
}

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

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

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

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

        tempPayload = payload;
        payload = repPayload;

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

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

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

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

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

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

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

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

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

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