svrdbeditorpstat.c 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* *****************************************************************
 *
 * Copyright 2017 Samsung Electronics All Rights Reserved.
 *
 *
 *
 * 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.
 *
 * *****************************************************************/

#include "octypes.h"
#include "oic_malloc.h"

#include "experimental/securevirtualresourcetypes.h"
25
#include "srmresourcestrings.h"
26 27 28 29 30
#include "psinterface.h"

#include "pstatresource.h"
#include "svrdbeditorpstat.h"

31 32 33 34
#define DPM_CNT (8)
#define DPOM_CNT (3)
#define DOSTYPE_CNT (5)

35 36
static OicSecPstat_t *g_pstat = NULL;

37 38 39 40 41 42 43 44 45 46
typedef enum PstatModifyType
{
    PSTAT_EDIT_DOS = 1,
    PSTAT_EDIT_ISOP,
    PSTAT_EDIT_CM,
    PSTAT_EDIT_TM,
    PSTAT_EDIT_OM,
    PSTAT_EDIT_SM,
    PSTAT_EDIT_ROWNERID = 7
} PstatModifyType_t;
47

48
void DeInitPstat(void)
49 50 51 52 53
{
    DeletePstatBinData(g_pstat);
    g_pstat = NULL;
}

54
void RefreshPstat(void)
55
{
56
    OCStackResult ocResult = OC_STACK_ERROR;
57 58 59 60 61 62 63 64 65 66
    OicSecPstat_t *tmpPstat = NULL;
    uint8_t *secPayload = NULL;
    size_t payloadSize = 0;

    ocResult = GetSecureVirtualDatabaseFromPS(OIC_JSON_PSTAT_NAME, &secPayload, &payloadSize);
    if (OC_STACK_OK != ocResult)
    {
        PRINT_WARN("GetSecureVirtualDatabaseFromPS : %d", ocResult);
        return;
    }
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    ocResult = CBORPayloadToPstat(secPayload, payloadSize, &tmpPstat);
    if (OC_STACK_OK != ocResult)
    {
        PRINT_ERR("CBORPayloadToPstat : %d", ocResult);
        OICFree(secPayload);
        return;
    }
    OICFree(secPayload);

    if (g_pstat)
    {
        DeInitPstat();
    }
    g_pstat = tmpPstat;
}
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

static void UpdatePstat(void)
{
    OCStackResult pstatResult = OC_STACK_ERROR;
    uint8_t *pstatPayload = NULL;
    size_t pstatPayloadSize = 0;

    pstatResult = PstatToCBORPayload(g_pstat, &pstatPayload, &pstatPayloadSize);
    if (OC_STACK_OK != pstatResult)
    {
        PRINT_ERR("PstatToCBORPayload error : %d", pstatResult);
        return;
    }
    pstatResult = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, pstatPayload, pstatPayloadSize);
    if (OC_STACK_OK != pstatResult)
    {
        PRINT_ERR("UpdateSecureResourceInPS error : %d", pstatResult);
        OICFree(pstatPayload);
        return;
    }
    OICFree(pstatPayload);
}

static void PrintDostype(const OicSecDostype_t dos)
{
    PRINT_DATA("Device Onboarding State : %d (", dos.state);
    if (DOS_RESET == dos.state)
    {
        PRINT_DATA(" RESET ");
    }
    else if (DOS_RFOTM == dos.state)
    {
        PRINT_DATA(" RFOTM ");
    }
    else if (DOS_RFPRO == dos.state)
    {
        PRINT_DATA(" RFPRO ");
    }
    else if (DOS_RFNOP == dos.state)
    {
        PRINT_DATA(" RFNOP ");
    }
    else if (DOS_SRESET == dos.state)
    {
        PRINT_DATA(" SRESET ");
    }
    PRINT_DATA("), Pending state : %s\n", (dos.pending ? "True" : "False"));
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
static void PrintDpm(const OicSecDpm_t dpm)
{
    PRINT_DATA("%d (", dpm);

    if (NORMAL == dpm)
    {
        PRINT_DATA(" NORMAL ");
    }
    if (dpm & RESET)
    {
        PRINT_DATA(" RESET ");
    }
    if (dpm & TAKE_OWNER)
    {
        PRINT_DATA(" TAKE_OWNER ");
    }
    if (dpm & BOOTSTRAP_SERVICE)
    {
        PRINT_DATA(" BOOTSTRAP_SERVICE ");
    }
    if (dpm & SECURITY_MANAGEMENT_SERVICES)
    {
        PRINT_DATA(" SECURITY_MANAGEMENT_SERVICES ");
    }
    if (dpm & PROVISION_CREDENTIALS)
    {
        PRINT_DATA(" PROVISION_CREDENTIALS ");
    }
    if (dpm & PROVISION_ACLS)
    {
        PRINT_DATA(" PROVISION_ACLS ");
    }
164 165 166 167 168 169 170 171 172
    if (dpm & VERIFY_SOFTWARE_VERSION)
    {
        PRINT_DATA(" VERIFY_SOFTWARE_VERSION ");
    }
    if (dpm & UPDATE_SOFTWARE)
    {
        PRINT_DATA(" UPDATE_SOFTWARE ");
    }

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    PRINT_DATA(") \n");
}

static void PrintDpom(const OicSecDpom_t dpom)
{
    PRINT_DATA("%d (", dpom);

    if (dpom & MULTIPLE_SERVICE_SERVER_DRIVEN)
    {
        PRINT_DATA(" MULTIPLE_SERVICE_SERVER_DRIVEN ");
    }
    if (dpom & SINGLE_SERVICE_SERVER_DRIVEN)
    {
        PRINT_DATA(" SINGLE_SERVICE_SERVER_DRIVEN ");
    }
    if (dpom & SINGLE_SERVICE_CLIENT_DRIVEN)
    {
        PRINT_DATA(" SINGLE_SERVICE_CLIENT_DRIVEN ");
    }
    PRINT_DATA(") \n");
}

195
void PrintPstat(void)
196 197 198
{
    PRINT_INFO("\n\n********************* [%-20s] *********************",
               "PSTAT Resource");
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    if (g_pstat)
    {
        //dos
        PRINT_PROG("%15s : ", OIC_JSON_DOS_NAME);
        PrintDostype(g_pstat->dos);

        //isop
        PRINT_PROG("%15s : ", OIC_JSON_ISOP_NAME);
        (g_pstat->isOp ? PrintString("True") : PrintString("False"));

        //cm
        PRINT_PROG("%15s : ", OIC_JSON_CM_NAME);
        PrintDpm(g_pstat->cm);

        //tm
        PRINT_PROG("%15s : ", OIC_JSON_TM_NAME);
        PrintDpm(g_pstat->tm);

        //om
        PRINT_PROG("%15s : ", OIC_JSON_OM_NAME);
        PrintDpom(g_pstat->om);

        //sm
        PRINT_PROG("%15s : \n", OIC_JSON_SM_NAME);
        for (size_t i = 0; i < g_pstat->smLen; i++)
        {
            PRINT_DATA("supported mode #%zu : ", i + 1);
            PrintDpom(g_pstat->sm[i]);
        }

        PRINT_PROG("%15s : ", OIC_JSON_ROWNERID_NAME);
        PrintUuid(&(g_pstat->rownerID));
    }
    else
    {
        PRINT_PROG("pstat is empty.\n");
    }
    PRINT_INFO("********************* [%-20s] *********************",
               "PSTAT Resource");
}

static int InputDostype(OicSecDostype_t *dos)
{
    int state = 0;
    int pending = 0;
    if (NULL == dos)
    {
        PRINT_ERR("Invalid parameter");
        return -1;
    }
    state = InputNumber("Input the Device Onboarding State"
                        "( 0.RESET 1.RFOTM 2.RFPRO 3.RFNOP 4.SRESET ) : ");
    if (state < 0 || DOSTYPE_CNT <= state)
    {
        PRINT_ERR("Invalid number");
        return -1;
    }
    pending = InputNumber("Input the Pending State ( 0.False, 1.True ) : ");
    if (pending < 0 || 1 < pending)
    {
        PRINT_ERR("Invalid number");
        return -1;
    }
    dos->state = state;
    dos->pending = (1 == pending ? true : false);
    return 0;
}

static unsigned int InputDpm(void)
{
    unsigned int dpm = 0;
    unsigned int dpm_msk = 1;  //default mask
    const char *Operation_Mode[DPM_CNT] =
    {
        "RESET", "TAKE_OWNER", "BOOTSTRAP_SERVICE", "SECURITY_MANAGEMENT_SERVICES",
        "PROVISION_CREDENTIALS", "PROVISION_ACLS", "VERIFY_SOFTWARE_VERSION", "UPDATE_SOFTWARE"
    };

    for (int i = 0; i < DPM_CNT; i++)
    {
        char ans = 0;
        for (;;)
        {
            PRINT_NORMAL("\tEnter %s Mode (y/n): ", Operation_Mode[i]);
            for (int ret = 0; 1 != ret; )
            {
                ret = scanf("%c", &ans);
286 287 288 289
                if ('\n' !=  ans)
                {
                    while ('\n' != getchar());
                }
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
            }
            if ('y' == ans || 'Y' == ans || 'n' == ans || 'N' == ans)
            {
                ans &= ~0x20;  // for masking lower case, 'y/n'
                break;
            }
            PRINT_NORMAL("\tEntered Wrong Answer. Please Enter 'y/n' Again\n");
        }
        if ('Y' == ans)
        {
            dpm |= dpm_msk;
        }
        dpm_msk <<= 1;
    }
    return dpm;
}

static unsigned int InputDpom()
{
    unsigned int dpom = 0;
    unsigned int dpom_msk = 1;  //default mask
    const char *Operation_Mode[DPOM_CNT] =
    {
        "MULTIPLE_SERVICE_SERVER_DRIVEN", "SINGLE_SERVICE_SERVER_DRIVEN",
        "SINGLE_SERVICE_CLIENT_DRIVEN"
    };

    PRINT_PROG("SINGLE_SERVICE_CLIENT_DRIVEN is the only mode IoTivity supports currently\n");

    for (int i = 0; i < DPOM_CNT; i++)
    {
        char ans = 0;
        for (;;)
        {
            PRINT_NORMAL("\tEnter %s Mode (y/n): ", Operation_Mode[i]);
            for (int ret = 0; 1 != ret; )
            {
                ret = scanf("%c", &ans);
328 329 330 331
                if ('\n' !=  ans)
                {
                    while ('\n' != getchar());
                }
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
            }
            if ('y' == ans || 'Y' == ans || 'n' == ans || 'N' == ans)
            {
                ans &= ~0x20;  // for masking lower case, 'y/n'
                break;
            }
            PRINT_NORMAL("\tEntered Wrong Answer. Please Enter 'y/n' Again\n");
        }
        if ('Y' == ans)
        {
            dpom |= dpom_msk;
        }
        dpom_msk <<= 1;
    }
    return dpom;
}
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
static OicSecPstat_t *InputPstatData(void)
{
    OicSecPstat_t *pstat = NULL;
    int isop = 0;

    pstat = (OicSecPstat_t *)OICCalloc(1, sizeof(OicSecPstat_t));
    if (NULL == pstat)
    {
        PRINT_ERR("Failed to allocate memory");
        return NULL;
    }

    PRINT_PROG("\n\nPlease input the each entity of new pstat.\n");

    if (0 != InputDostype(&pstat->dos))
    {
        PRINT_ERR("Failed InputDostype");
        OICFree(pstat);
        return NULL;
    }

    isop = InputNumber("Input the isop value ( 0.False, 1.True ) : ");
    if (isop < 0 || 1 < isop)
    {
        PRINT_ERR("Invalid input");
        OICFree(pstat);
        return NULL;
    }
    else
    {
        pstat->isOp = (1 == isop ? true : false);
    }

    PRINT_PROG("Input Current Mode\n");
    pstat->cm = InputDpm();
    PRINT_PROG("Input Target Mode\n");
    pstat->tm = InputDpm();
    PRINT_PROG("Input Operational Mode\n");

    pstat->om = InputDpom();
    pstat->smLen = 1; // supported only one dpom mode in current iotivity
    pstat->sm = (OicSecDpom_t *)OICCalloc(pstat->smLen, sizeof(OicSecDpom_t));
    if (NULL == pstat->sm)
    {
        PRINT_ERR("InputPstatData : Failed to allocate memory");
        OICFree(pstat);
        return NULL;
    }
397 398
    for (size_t i = 0; i < pstat->smLen; i++)
    {
399 400
        PRINT_PROG("Input dpom #%zu", i);
        pstat->sm[i] = InputDpom();
401 402
    }

403 404 405 406 407 408 409 410 411 412 413
    PRINT_PROG(
        "\tInput the ROWNER UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
    if (0 != InputUuid(&(pstat->rownerID)))
    {
        PRINT_ERR("InputUuid error");
        OICFree(pstat->sm);
        OICFree(pstat);
        return NULL;
    }
    return pstat;
}
414

415 416 417 418 419 420 421 422 423
static int ModifyPstat(void)
{
    int ret = 0;
    int modifyMenu = 0;
    if (NULL == g_pstat)
    {
        PRINT_ERR("pstat is NULL");
        return -1;
    }
424

425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
    PRINT_PROG("\n\nPlease input the attribute you want to modify\n");
    PRINT_DATA("\t%2d. Edit dos\n", PSTAT_EDIT_DOS);
    PRINT_DATA("\t%2d. Edit isop\n", PSTAT_EDIT_ISOP);
    PRINT_DATA("\t%2d. Edit cm\n", PSTAT_EDIT_CM);
    PRINT_DATA("\t%2d. Edit tm\n", PSTAT_EDIT_TM);
    PRINT_DATA("\t%2d. Edit om\n", PSTAT_EDIT_OM);
    PRINT_DATA("\t%2d. Edit sm\n", PSTAT_EDIT_SM);
    PRINT_DATA("\t%2d. Edit rownerID\n", PSTAT_EDIT_ROWNERID);
    PRINT_DATA("\t%2d. Back to the previous\n", BACK);
    modifyMenu = InputNumber("Select the menu : ");
    switch (modifyMenu)
    {
        case PSTAT_EDIT_DOS:
            if (0 != InputDostype(&(g_pstat->dos)))
            {
                PRINT_ERR("Failed InputDostype");
            }
            break;
        case PSTAT_EDIT_ISOP:
            {
                int isop = InputNumber("Input the isop value ( 0.False, 1.True ) : ");
                if (isop < 0 || 1 < isop)
                {
                    PRINT_ERR("Invalid input");
                    return -1;
                }
                else
                {
                    g_pstat->isOp = (1 == isop ? true : false);
                }
                break;
            }
        case PSTAT_EDIT_CM :
            PRINT_PROG("Input Current Mode\n");
            g_pstat->cm = InputDpm();
            break;
        case PSTAT_EDIT_TM:
            PRINT_PROG("Input Target Mode\n");
            g_pstat->tm = InputDpm();
            break;
        case PSTAT_EDIT_OM:
            PRINT_PROG("Input Operational Mode\n");
            g_pstat->om = InputDpom();
            break;
        case PSTAT_EDIT_SM:
            {
                OicSecDpom_t *sm = NULL;
                size_t smLen = 1;
473

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
                sm = (OicSecDpom_t *)OICCalloc(smLen, sizeof(OicSecDpom_t));
                if (NULL == sm)
                {
                    PRINT_ERR("Failed to allocate memory");
                    return -1;
                }
                for (size_t i = 0; i < smLen; i++)
                {
                    PRINT_PROG("Input dpom #%zu", i);
                    sm[i] = InputDpom();
                }
                if (g_pstat->sm)
                {
                    OICFree(g_pstat->sm);
                }
                g_pstat->smLen = smLen;
                g_pstat->sm = sm;
                break;
            }
        case PSTAT_EDIT_ROWNERID:
            PRINT_PROG(
                "\tInput the ROWNER UUID (e.g. 61646D69-6E44-6576-6963-655575696430) : ");
            ret = InputUuid(&(g_pstat->rownerID));
            if (0 != ret)
            {
                PRINT_ERR("InputUuid error");
                return ret;
            }
            break;
        case BACK:
            PRINT_INFO("Back to the previous menu.");
            break;
        default:
            PRINT_ERR("Wrong type Number");
            ret = -1;
            break;
    }
    return ret;
512 513 514 515
}

void HandlePstatOperation(const SubOperationType_t cmd)
{
516
    OCStackResult pstatResult = OC_STACK_ERROR;
517

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
    if (SVR_EDIT_IDX_SIZE <= cmd)
    {
        PRINT_ERR("Invalid menu for pstat");
        return;
    }
    switch (cmd)
    {
        case SVR_PRINT:
            PrintPstat();
            break;
        case SVR_ADD:
            if (g_pstat)
            {
                PRINT_ERR("Already exist");
                return;
            }
            else
            {
                g_pstat = InputPstatData();
                if (NULL == g_pstat)
                {
                    PRINT_ERR("Failed to InputPstatData");
                    return;
                }
                UpdatePstat();
            }
            break;
        case SVR_REMOVE:
            if (g_pstat)
            {
                DeInitPstat();
                pstatResult = UpdateSecureResourceInPS(OIC_JSON_PSTAT_NAME, NULL, 0);
                if (OC_STACK_OK != pstatResult)
                {
                    PRINT_ERR("UpdateSecureResourceInPS error : %d", pstatResult);
                    return;
                }
            }
            else
            {
                PRINT_ERR("pstat is NULL");
                return;
            }
            break;
        case SVR_MODIFY:
            if (0 != ModifyPstat())
            {
                PRINT_ERR("Failed Modify");
                return;
            }
            PRINT_INFO("\n\nPstat Modified");
            PrintPstat();
            UpdatePstat();
            break;
        default:
            PRINT_ERR("Invalid menu for pstat");
            break;
    }
}