b.liu | d440f9f | 2025-04-18 10:44:31 +0800 | [diff] [blame] | 1 | /*-----------------------------------------------------------------------------------------------*/ |
| 2 | /** |
| 3 | @file ql_sim_test.h |
| 4 | @brief SIM service API |
| 5 | */ |
| 6 | /*-----------------------------------------------------------------------------------------------*/ |
| 7 | |
| 8 | /*------------------------------------------------------------------------------------------------- |
| 9 | Copyright (c) 2024 mobiletek Wireless Solution, Co., Ltd. All Rights Reserved. |
| 10 | mobiletek Wireless Solution Proprietary and Confidential. |
| 11 | -------------------------------------------------------------------------------------------------*/ |
| 12 | |
| 13 | /*------------------------------------------------------------------------------------------------- |
| 14 | EDIT HISTORY |
| 15 | This section contains comments describing changes made to the file. |
| 16 | Notice that changes are listed in reverse chronological order. |
| 17 | $Header: $ |
| 18 | when who what, where, why |
| 19 | -------- --------- ----------------------------------------------------------------- |
| 20 | 20241022 yq.wang Created . |
| 21 | -------------------------------------------------------------------------------------------------*/ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdint.h> |
| 27 | #include "ql_type.h" |
| 28 | #include "ql_sim.h" |
| 29 | #include "ql_test_utils.h" |
| 30 | |
| 31 | |
| 32 | static void item_ql_sim_init(void) |
| 33 | { |
| 34 | int ret = 0; |
| 35 | |
| 36 | printf("test ql_sim_init: "); |
| 37 | ret = ql_sim_init(); |
| 38 | if(ret == QL_ERR_OK) |
| 39 | { |
| 40 | printf("ok\n"); |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | printf("failed, ret = %d\n", ret); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | static void item_ql_sim_deinit(void) |
| 49 | { |
| 50 | int ret = 0; |
| 51 | |
| 52 | printf("test ql_sim_deinit: "); |
| 53 | ret = ql_sim_deinit(); |
| 54 | if (ret == QL_ERR_OK) |
| 55 | { |
| 56 | printf("ok\n"); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | printf("failed, ret = %d\n", ret); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void item_ql_sim_get_imsi(void) |
| 65 | { |
| 66 | int ret = 0; |
| 67 | char imsi[QL_SIM_IMSI_LENGTH+1] = {0}; |
| 68 | int input = 0; |
| 69 | QL_SIM_SLOT_E slot; |
| 70 | QL_SIM_APP_TYPE_E app_type; |
| 71 | |
| 72 | printf("test ql_sim_get_imsi: \n"); |
| 73 | printf("please enter slot(1 or 2): "); |
| 74 | scanf("%d", &input); |
| 75 | getchar(); |
| 76 | if (1 == input) |
| 77 | { |
| 78 | slot = QL_SIM_SLOT_1; |
| 79 | } |
| 80 | else if (2 == input) |
| 81 | { |
| 82 | slot = QL_SIM_SLOT_2; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | printf("bad slot: %d\n", input); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 91 | scanf("%d", &input); |
| 92 | getchar(); |
| 93 | switch (input) |
| 94 | { |
| 95 | case 0: |
| 96 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 97 | break; |
| 98 | case 1: |
| 99 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 100 | break; |
| 101 | case 2: |
| 102 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 103 | break; |
| 104 | case 3: |
| 105 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 106 | break; |
| 107 | default: |
| 108 | printf("bad app type: %d\n", input); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | ret = ql_sim_get_imsi(slot, app_type, imsi, sizeof(imsi)); |
| 113 | if (ret == QL_ERR_OK) |
| 114 | { |
| 115 | printf("IMSI: %s\n", imsi); |
| 116 | } |
| 117 | else |
| 118 | { |
| 119 | printf("failed, ret = %d\n", ret); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void item_ql_sim_get_iccid(void) |
| 124 | { |
| 125 | int ret = 0; |
| 126 | char iccid[QL_SIM_ICCID_LENGTH+1] = {0}; |
| 127 | int input = 0; |
| 128 | QL_SIM_SLOT_E slot; |
| 129 | |
| 130 | printf("test ql_sim_get_iccid: \n"); |
| 131 | printf("please enter slot(1 or 2): "); |
| 132 | scanf("%d", &input); |
| 133 | getchar(); |
| 134 | if (1 == input) |
| 135 | { |
| 136 | slot = QL_SIM_SLOT_1; |
| 137 | } |
| 138 | else if (2 == input) |
| 139 | { |
| 140 | slot = QL_SIM_SLOT_2; |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | printf("bad slot: %d\n", input); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | ret = ql_sim_get_iccid(slot, iccid, sizeof(iccid)); |
| 149 | if (ret == QL_ERR_OK) |
| 150 | { |
| 151 | printf("ICCID: %s\n", iccid); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | printf("failed, ret = %d\n", ret); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | static void item_ql_sim_get_phone_num(void) |
| 160 | { |
| 161 | int ret = 0; |
| 162 | char num[QL_SIM_PHONE_NUMBER_MAX+1] = {0}; |
| 163 | int input = 0; |
| 164 | QL_SIM_SLOT_E slot; |
| 165 | QL_SIM_APP_TYPE_E app_type; |
| 166 | |
| 167 | printf("test ql_sim_get_phone_num: \n"); |
| 168 | printf("please enter slot(1 or 2): "); |
| 169 | scanf("%d", &input); |
| 170 | getchar(); |
| 171 | if (1 == input) |
| 172 | { |
| 173 | slot = QL_SIM_SLOT_1; |
| 174 | } |
| 175 | else if (2 == input) |
| 176 | { |
| 177 | slot = QL_SIM_SLOT_2; |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | printf("bad slot: %d\n", input); |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 186 | scanf("%d", &input); |
| 187 | getchar(); |
| 188 | switch (input) |
| 189 | { |
| 190 | case 0: |
| 191 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 192 | break; |
| 193 | case 1: |
| 194 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 195 | break; |
| 196 | case 2: |
| 197 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 198 | break; |
| 199 | case 3: |
| 200 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 201 | break; |
| 202 | default: |
| 203 | printf("bad app type: %d\n", input); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | ret = ql_sim_get_phone_num(slot, app_type, num, sizeof(num)); |
| 208 | if (ret == QL_ERR_OK) |
| 209 | { |
| 210 | printf("Phone number: %s\n", num); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | printf("failed, ret = %d\n", ret); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | static void print_ascii(char *prefix, char *postfix, char *ascii, int len) |
| 219 | { |
| 220 | int i = 0; |
| 221 | printf("%s", prefix); |
| 222 | for (i = 0; i < len; i++) |
| 223 | { |
| 224 | putchar(ascii[i]); |
| 225 | } |
| 226 | printf("%s", postfix); |
| 227 | } |
| 228 | |
| 229 | static void item_ql_sim_get_operators(void) |
| 230 | { |
| 231 | int ret = 0; |
| 232 | ql_sim_operator_list_t list = {0}; |
| 233 | int input = 0; |
| 234 | QL_SIM_SLOT_E slot; |
| 235 | |
| 236 | printf("test ql_sim_get_operators: \n"); |
| 237 | printf("please enter slot(1 or 2): "); |
| 238 | scanf("%d", &input); |
| 239 | getchar(); |
| 240 | if (1 == input) |
| 241 | { |
| 242 | slot = QL_SIM_SLOT_1; |
| 243 | } |
| 244 | else if (2 == input) |
| 245 | { |
| 246 | slot = QL_SIM_SLOT_2; |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | printf("bad slot: %d\n", input); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | ret = ql_sim_get_operators(slot, &list); |
| 255 | if (ret == QL_ERR_OK) |
| 256 | { |
| 257 | if (0 == list.len) |
| 258 | { |
| 259 | printf("No operators found\n"); |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | int i = 0; |
| 264 | printf("found %d opertators:\n", list.len); |
| 265 | for (i = 0; i < list.len; i++) |
| 266 | { |
| 267 | printf(" #%02d: ", i + 1); |
| 268 | print_ascii("MCC: ", "", list.operators[i].mcc, (int)sizeof(list.operators[i].mcc)); |
| 269 | print_ascii(", MNC: ", "\n",list.operators[i].mnc, list.operators[i].mnc_len); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | printf("failed, ret = %d\n", ret); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | static void item_ql_sim_enable_pin(void) |
| 280 | { |
| 281 | int ret = 0; |
| 282 | int len = 0; |
| 283 | char c; |
| 284 | char pin_value[QL_SIM_PIN_MAX*2] = {0}; |
| 285 | int input = 0; |
| 286 | QL_SIM_SLOT_E slot; |
| 287 | QL_SIM_APP_TYPE_E app_type; |
| 288 | QL_SIM_PIN_E pin; |
| 289 | |
| 290 | printf("test ql_sim_enable_pin: \n"); |
| 291 | printf("please enter slot(1 or 2): "); |
| 292 | scanf("%d", &input); |
| 293 | getchar(); |
| 294 | if (1 == input) |
| 295 | { |
| 296 | slot = QL_SIM_SLOT_1; |
| 297 | } |
| 298 | else if (2 == input) |
| 299 | { |
| 300 | slot = QL_SIM_SLOT_2; |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | printf("bad slot: %d\n", input); |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 309 | scanf("%d", &input); |
| 310 | getchar(); |
| 311 | switch (input) |
| 312 | { |
| 313 | case 0: |
| 314 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 315 | break; |
| 316 | case 1: |
| 317 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 318 | break; |
| 319 | case 2: |
| 320 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 321 | break; |
| 322 | case 3: |
| 323 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 324 | break; |
| 325 | default: |
| 326 | printf("bad app type: %d\n", input); |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | printf("please enter pin(1 or 2): "); |
| 331 | scanf("%d", &input); |
| 332 | getchar(); |
| 333 | if (1 == input) |
| 334 | { |
| 335 | pin = QL_SIM_PIN_1; |
| 336 | } |
| 337 | else if (2 == input) |
| 338 | { |
| 339 | pin = QL_SIM_PIN_2; |
| 340 | } |
| 341 | else |
| 342 | { |
| 343 | printf("bad pin: %d\n", input); |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | printf("please enter pin value(at most %d digit): ", QL_SIM_PIN_MAX); |
| 348 | if (NULL == fgets(pin_value, sizeof(pin_value), stdin)) |
| 349 | { |
| 350 | printf("can not read pin value\n"); |
| 351 | return; |
| 352 | } |
| 353 | len = strlen(pin_value); |
| 354 | if ('\n' == pin_value[len-1]) |
| 355 | { |
| 356 | pin_value[len-1] = 0; |
| 357 | len--; |
| 358 | } |
| 359 | printf("pin value: %s\n", pin_value); |
| 360 | |
| 361 | printf("proceed? [y/n]: "); |
| 362 | c = getchar(); |
| 363 | if ('\n' != c) |
| 364 | { |
| 365 | getchar(); |
| 366 | } |
| 367 | if ('Y' != c && 'y' != c) |
| 368 | { |
| 369 | printf("abort\n"); |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | ret = ql_sim_enable_pin(slot, app_type, pin, pin_value); |
| 374 | if (ret == QL_ERR_OK) |
| 375 | { |
| 376 | printf("ok\n"); |
| 377 | } |
| 378 | else |
| 379 | { |
| 380 | printf("failed, ret = %d\n", ret); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | static void item_ql_sim_disable_pin(void) |
| 385 | { |
| 386 | int ret = 0; |
| 387 | int len = 0; |
| 388 | char c; |
| 389 | char pin_value[QL_SIM_PIN_MAX*2] = {0}; |
| 390 | int input = 0; |
| 391 | QL_SIM_SLOT_E slot; |
| 392 | QL_SIM_APP_TYPE_E app_type; |
| 393 | QL_SIM_PIN_E pin; |
| 394 | |
| 395 | printf("test ql_sim_disable_pin: \n"); |
| 396 | printf("please enter slot(1 or 2): "); |
| 397 | scanf("%d", &input); |
| 398 | getchar(); |
| 399 | if (1 == input) |
| 400 | { |
| 401 | slot = QL_SIM_SLOT_1; |
| 402 | } |
| 403 | else if (2 == input) |
| 404 | { |
| 405 | slot = QL_SIM_SLOT_2; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | printf("bad slot: %d\n", input); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 414 | scanf("%d", &input); |
| 415 | getchar(); |
| 416 | switch (input) |
| 417 | { |
| 418 | case 0: |
| 419 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 420 | break; |
| 421 | case 1: |
| 422 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 423 | break; |
| 424 | case 2: |
| 425 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 426 | break; |
| 427 | case 3: |
| 428 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 429 | break; |
| 430 | default: |
| 431 | printf("bad app type: %d\n", input); |
| 432 | return; |
| 433 | } |
| 434 | |
| 435 | printf("please enter pin(1 or 2): "); |
| 436 | scanf("%d", &input); |
| 437 | getchar(); |
| 438 | if (1 == input) |
| 439 | { |
| 440 | pin = QL_SIM_PIN_1; |
| 441 | } |
| 442 | else if (2 == input) |
| 443 | { |
| 444 | pin = QL_SIM_PIN_2; |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | printf("bad pin: %d\n", input); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | printf("please enter pin value(at most %d digit): ", QL_SIM_PIN_MAX); |
| 453 | if (NULL == fgets(pin_value, sizeof(pin_value), stdin)) |
| 454 | { |
| 455 | printf("can not read pin value\n"); |
| 456 | return; |
| 457 | } |
| 458 | len = strlen(pin_value); |
| 459 | if ('\n' == pin_value[len-1]) |
| 460 | { |
| 461 | pin_value[len-1] = 0; |
| 462 | len--; |
| 463 | } |
| 464 | printf("pin value: %s\n", pin_value); |
| 465 | |
| 466 | printf("proceed? [y/n]: "); |
| 467 | c = getchar(); |
| 468 | if ('\n' != c) |
| 469 | { |
| 470 | getchar(); |
| 471 | } |
| 472 | if ('Y' != c && 'y' != c) |
| 473 | { |
| 474 | printf("abort\n"); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | ret = ql_sim_disable_pin(slot, app_type, pin, pin_value); |
| 479 | if (ret == QL_ERR_OK) |
| 480 | { |
| 481 | printf("ok\n"); |
| 482 | } |
| 483 | else |
| 484 | { |
| 485 | printf("failed, ret = %d\n", ret); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | static void item_ql_sim_verify_pin(void) |
| 490 | { |
| 491 | int ret = 0; |
| 492 | int len = 0; |
| 493 | char c; |
| 494 | char pin_value[QL_SIM_PIN_MAX*2] = {0}; |
| 495 | int input = 0; |
| 496 | QL_SIM_SLOT_E slot; |
| 497 | QL_SIM_APP_TYPE_E app_type; |
| 498 | QL_SIM_PIN_E pin; |
| 499 | |
| 500 | printf("test ql_sim_verify_pin: \n"); |
| 501 | printf("please enter slot(1 or 2): "); |
| 502 | scanf("%d", &input); |
| 503 | getchar(); |
| 504 | if (1 == input) |
| 505 | { |
| 506 | slot = QL_SIM_SLOT_1; |
| 507 | } |
| 508 | else if (2 == input) |
| 509 | { |
| 510 | slot = QL_SIM_SLOT_2; |
| 511 | } |
| 512 | else |
| 513 | { |
| 514 | printf("bad slot: %d\n", input); |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 519 | scanf("%d", &input); |
| 520 | getchar(); |
| 521 | switch (input) |
| 522 | { |
| 523 | case 0: |
| 524 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 525 | break; |
| 526 | case 1: |
| 527 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 528 | break; |
| 529 | case 2: |
| 530 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 531 | break; |
| 532 | case 3: |
| 533 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 534 | break; |
| 535 | default: |
| 536 | printf("bad app type: %d\n", input); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | printf("please enter pin(1 or 2): "); |
| 541 | scanf("%d", &input); |
| 542 | getchar(); |
| 543 | if (1 == input) |
| 544 | { |
| 545 | pin = QL_SIM_PIN_1; |
| 546 | } |
| 547 | else if (2 == input) |
| 548 | { |
| 549 | pin = QL_SIM_PIN_2; |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | printf("bad pin: %d\n", input); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | printf("please enter pin value(at most %d digit): ", QL_SIM_PIN_MAX); |
| 558 | if (NULL == fgets(pin_value, sizeof(pin_value), stdin)) |
| 559 | { |
| 560 | printf("can not read pin value\n"); |
| 561 | return; |
| 562 | } |
| 563 | len = strlen(pin_value); |
| 564 | if ('\n' == pin_value[len-1]) |
| 565 | { |
| 566 | pin_value[len-1] = 0; |
| 567 | len--; |
| 568 | } |
| 569 | printf("pin value: %s\n", pin_value); |
| 570 | |
| 571 | printf("proceed? [y/n]: "); |
| 572 | c = getchar(); |
| 573 | if ('\n' != c) |
| 574 | { |
| 575 | getchar(); |
| 576 | } |
| 577 | if ('Y' != c && 'y' != c) |
| 578 | { |
| 579 | printf("abort\n"); |
| 580 | return; |
| 581 | } |
| 582 | |
| 583 | ret = ql_sim_verify_pin(slot, app_type, pin, pin_value); |
| 584 | if (ret == QL_ERR_OK) |
| 585 | { |
| 586 | printf("ok\n"); |
| 587 | } |
| 588 | else |
| 589 | { |
| 590 | printf("failed, ret = %d\n", ret); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | static void item_ql_sim_change_pin(void) |
| 595 | { |
| 596 | int ret = 0; |
| 597 | int old_len = 0; |
| 598 | int new_len = 0; |
| 599 | char c; |
| 600 | char old_pin_value[QL_SIM_PIN_MAX*2] = {0}; |
| 601 | char new_pin_value[QL_SIM_PIN_MAX*2] = {0}; |
| 602 | int input = 0; |
| 603 | QL_SIM_SLOT_E slot; |
| 604 | QL_SIM_APP_TYPE_E app_type; |
| 605 | QL_SIM_PIN_E pin; |
| 606 | |
| 607 | printf("test ql_sim_change_pin: \n"); |
| 608 | printf("please enter slot(1 or 2): "); |
| 609 | scanf("%d", &input); |
| 610 | getchar(); |
| 611 | if (1 == input) |
| 612 | { |
| 613 | slot = QL_SIM_SLOT_1; |
| 614 | } |
| 615 | else if (2 == input) |
| 616 | { |
| 617 | slot = QL_SIM_SLOT_2; |
| 618 | } |
| 619 | else |
| 620 | { |
| 621 | printf("bad slot: %d\n", input); |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 626 | scanf("%d", &input); |
| 627 | getchar(); |
| 628 | switch (input) |
| 629 | { |
| 630 | case 0: |
| 631 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 632 | break; |
| 633 | case 1: |
| 634 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 635 | break; |
| 636 | case 2: |
| 637 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 638 | break; |
| 639 | case 3: |
| 640 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 641 | break; |
| 642 | default: |
| 643 | printf("bad app type: %d\n", input); |
| 644 | return; |
| 645 | } |
| 646 | |
| 647 | printf("please enter pin(1 or 2): "); |
| 648 | scanf("%d", &input); |
| 649 | getchar(); |
| 650 | if (1 == input) |
| 651 | { |
| 652 | pin = QL_SIM_PIN_1; |
| 653 | } |
| 654 | else if (2 == input) |
| 655 | { |
| 656 | pin = QL_SIM_PIN_2; |
| 657 | } |
| 658 | else |
| 659 | { |
| 660 | printf("bad pin: %d\n", input); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | printf("please enter old pin value(at most %d digit): ", QL_SIM_PIN_MAX); |
| 665 | if (NULL == fgets(old_pin_value, sizeof(old_pin_value), stdin)) |
| 666 | { |
| 667 | printf("can not read old pin value\n"); |
| 668 | return; |
| 669 | } |
| 670 | old_len = strlen(old_pin_value); |
| 671 | if ('\n' == old_pin_value[old_len-1]) |
| 672 | { |
| 673 | old_pin_value[old_len-1] = 0; |
| 674 | old_len--; |
| 675 | } |
| 676 | |
| 677 | printf("please enter new pin value(at most %d digit): ", QL_SIM_PIN_MAX); |
| 678 | if (NULL == fgets(new_pin_value, sizeof(new_pin_value), stdin)) |
| 679 | { |
| 680 | printf("can not read new pin value\n"); |
| 681 | return; |
| 682 | } |
| 683 | new_len = strlen(new_pin_value); |
| 684 | if ('\n' == new_pin_value[new_len-1]) |
| 685 | { |
| 686 | new_pin_value[new_len-1] = 0; |
| 687 | new_len--; |
| 688 | } |
| 689 | printf("old pin value: %s\n", old_pin_value); |
| 690 | printf("new pin value: %s\n", new_pin_value); |
| 691 | |
| 692 | printf("proceed? [y/n]: "); |
| 693 | c = getchar(); |
| 694 | if ('\n' != c) |
| 695 | { |
| 696 | getchar(); |
| 697 | } |
| 698 | if ('Y' != c && 'y' != c) |
| 699 | { |
| 700 | printf("abort\n"); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | ret = ql_sim_change_pin(slot, app_type, pin, old_pin_value, new_pin_value); |
| 705 | if (ret == QL_ERR_OK) |
| 706 | { |
| 707 | printf("ok\n"); |
| 708 | } |
| 709 | else |
| 710 | { |
| 711 | printf("failed, ret = %d\n", ret); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | static void item_ql_sim_unblock_pin(void) |
| 716 | { |
| 717 | int ret = 0; |
| 718 | int len = 0; |
| 719 | int new_len = 0; |
| 720 | char c; |
| 721 | char puk_value[QL_SIM_PIN_MAX*2] = {0}; |
| 722 | char new_pin_value[QL_SIM_PIN_MAX*2] = {0}; |
| 723 | int input = 0; |
| 724 | QL_SIM_SLOT_E slot; |
| 725 | QL_SIM_APP_TYPE_E app_type; |
| 726 | QL_SIM_PIN_E pin; |
| 727 | |
| 728 | printf("test ql_sim_unblock_pin: \n"); |
| 729 | printf("please enter slot(1 or 2): "); |
| 730 | scanf("%d", &input); |
| 731 | getchar(); |
| 732 | if (1 == input) |
| 733 | { |
| 734 | slot = QL_SIM_SLOT_1; |
| 735 | } |
| 736 | else if (2 == input) |
| 737 | { |
| 738 | slot = QL_SIM_SLOT_2; |
| 739 | } |
| 740 | else |
| 741 | { |
| 742 | printf("bad slot: %d\n", input); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 747 | scanf("%d", &input); |
| 748 | getchar(); |
| 749 | switch (input) |
| 750 | { |
| 751 | case 0: |
| 752 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 753 | break; |
| 754 | case 1: |
| 755 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 756 | break; |
| 757 | case 2: |
| 758 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 759 | break; |
| 760 | case 3: |
| 761 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 762 | break; |
| 763 | default: |
| 764 | printf("bad app type: %d\n", input); |
| 765 | return; |
| 766 | } |
| 767 | |
| 768 | printf("please enter pin(1 or 2): "); |
| 769 | scanf("%d", &input); |
| 770 | getchar(); |
| 771 | if (1 == input) |
| 772 | { |
| 773 | pin = QL_SIM_PIN_1; |
| 774 | } |
| 775 | else if (2 == input) |
| 776 | { |
| 777 | pin = QL_SIM_PIN_2; |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | printf("bad pin: %d\n", input); |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | printf("please enter puk value(at most %d digit): ", QL_SIM_PIN_MAX); |
| 786 | if (NULL == fgets(puk_value, sizeof(puk_value), stdin)) |
| 787 | { |
| 788 | printf("can not read old pin value\n"); |
| 789 | return; |
| 790 | } |
| 791 | len = strlen(puk_value); |
| 792 | if ('\n' == puk_value[len-1]) |
| 793 | { |
| 794 | puk_value[len-1] = 0; |
| 795 | len--; |
| 796 | } |
| 797 | |
| 798 | printf("please enter new pin value(at most %d digit): ", QL_SIM_PIN_MAX); |
| 799 | if (NULL == fgets(new_pin_value, sizeof(new_pin_value), stdin)) |
| 800 | { |
| 801 | printf("can not read new pin value\n"); |
| 802 | return; |
| 803 | } |
| 804 | new_len = strlen(new_pin_value); |
| 805 | if ('\n' == new_pin_value[new_len-1]) |
| 806 | { |
| 807 | new_pin_value[new_len-1] = 0; |
| 808 | new_len--; |
| 809 | } |
| 810 | printf(" puk value: %s\n", puk_value); |
| 811 | printf("new pin value: %s\n", new_pin_value); |
| 812 | |
| 813 | printf("proceed? [y/n]: "); |
| 814 | c = getchar(); |
| 815 | if ('\n' != c) |
| 816 | { |
| 817 | getchar(); |
| 818 | } |
| 819 | if ('Y' != c && 'y' != c) |
| 820 | { |
| 821 | printf("abort\n"); |
| 822 | return; |
| 823 | } |
| 824 | |
| 825 | ret = ql_sim_unblock_pin(slot, app_type, pin, puk_value, new_pin_value); |
| 826 | if (ret == QL_ERR_OK) |
| 827 | { |
| 828 | printf("ok\n"); |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | printf("failed, ret = %d\n", ret); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | static char *card_state_desc(QL_SIM_CARD_STATE_E state) |
| 837 | { |
| 838 | switch (state) |
| 839 | { |
| 840 | case QL_SIM_CARD_STATE_UNKNOWN: |
| 841 | return "unknown"; |
| 842 | case QL_SIM_CARD_STATE_ABSENT: |
| 843 | return "absent"; |
| 844 | case QL_SIM_CARD_STATE_PRESENT: |
| 845 | return "present"; |
| 846 | case QL_SIM_CARD_STATE_ERROR_UNKNOWN: |
| 847 | return "unknown error"; |
| 848 | case QL_SIM_CARD_STATE_ERROR_POWER_DOWN: |
| 849 | return "power down"; |
| 850 | case QL_SIM_CARD_STATE_ERROR_POLL_ERROR: |
| 851 | return "poll error"; |
| 852 | case QL_SIM_CARD_STATE_ERROR_NO_ATR_RECEIVED: |
| 853 | return "failed to receive an answer to reset"; |
| 854 | case QL_SIM_CARD_STATE_ERROR_VOLT_MISMATCH: |
| 855 | return "voltage mismatch"; |
| 856 | case QL_SIM_CARD_STATE_ERROR_PARITY_ERROR: |
| 857 | return "parity error"; |
| 858 | case QL_SIM_CARD_STATE_ERROR_SIM_TECHNICAL_PROBLEMS: |
| 859 | return "technical problems"; |
| 860 | default: |
| 861 | return "N/A"; |
| 862 | } |
| 863 | } |
| 864 | |
| 865 | static char *card_type_desc(QL_SIM_CARD_TYPE_E type) |
| 866 | { |
| 867 | switch (type) |
| 868 | { |
| 869 | case QL_SIM_CARD_TYPE_UNKNOWN: |
| 870 | return "unknown"; |
| 871 | case QL_SIM_CARD_TYPE_ICC: |
| 872 | return "ICC"; |
| 873 | case QL_SIM_CARD_TYPE_UICC: |
| 874 | return "UICC"; |
| 875 | default: |
| 876 | return "N/A"; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | static char *card_subscription_desc(QL_SIM_SUBSCRIPTION_E subscription) |
| 881 | { |
| 882 | switch (subscription) |
| 883 | { |
| 884 | case QL_SIM_SUBSCRIPTION_NONE: |
| 885 | return "nonprovisioning"; |
| 886 | case QL_SIM_SUBSCRIPTION_PRI: |
| 887 | return "primary provisioning subscription"; |
| 888 | case QL_SIM_SUBSCRIPTION_SEC: |
| 889 | return "secondary provisioning subscription"; |
| 890 | default: |
| 891 | return "N/A"; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | static char *card_app_state_desc(QL_SIM_APP_STATE_E state) |
| 896 | { |
| 897 | switch (state) |
| 898 | { |
| 899 | case QL_SIM_APP_STATE_UNKNOWN: |
| 900 | return "unknown"; |
| 901 | case QL_SIM_APP_STATE_DETECTED: |
| 902 | return "detected"; |
| 903 | case QL_SIM_APP_STATE_PIN1_REQ: |
| 904 | return "PIN1 required"; |
| 905 | case QL_SIM_APP_STATE_PUK1_REQ: |
| 906 | return "PUK1 required"; |
| 907 | case QL_SIM_APP_STATE_INITALIZATING: |
| 908 | return "initializing"; |
| 909 | case QL_SIM_APP_STATE_PERSO_CK_REQ: |
| 910 | return "personalization control key required"; |
| 911 | case QL_SIM_APP_STATE_PERSO_PUK_REQ: |
| 912 | return "personalization unblock key required"; |
| 913 | case QL_SIM_APP_STATE_PERSO_PERMANENTLY_BLOCKED: |
| 914 | return "personalization is permanently blocked"; |
| 915 | case QL_SIM_APP_STATE_PIN1_PERM_BLOCKED: |
| 916 | return "PIN1 is permanently blocked"; |
| 917 | case QL_SIM_APP_STATE_ILLEGAL: |
| 918 | return "illegal"; |
| 919 | case QL_SIM_APP_STATE_READY: |
| 920 | return "ready"; |
| 921 | default: |
| 922 | return "N/A"; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | static char *card_perso_feature_desc(QL_SIM_PERSO_FEATURE_E feature) |
| 927 | { |
| 928 | switch (feature) |
| 929 | { |
| 930 | case QL_SIM_PERSO_FEATURE_UNKNOWN: |
| 931 | return "unknown"; |
| 932 | case QL_SIM_PERSO_FEATURE_3GPP_NETWORK: |
| 933 | return "featurization based on 3GPP MCC and MNC"; |
| 934 | case QL_SIM_PERSO_FEATURE_3GPP_NETWORK_SUBSET: |
| 935 | return "featurization based on 3GPP MCC, MNC, and IMSI digits 6 and 7"; |
| 936 | case QL_SIM_PERSO_FEATURE_3GPP_SERVICE_PROVIDER: |
| 937 | return "featurization based on 3GPP MCC, MNC, and GID1"; |
| 938 | case QL_SIM_PERSO_FEATURE_3GPP_CORPORATE: |
| 939 | return "featurization based on 3GPP MCC, MNC, GID1, and GID2"; |
| 940 | case QL_SIM_PERSO_FEATURE_3GPP_SIM: |
| 941 | return "featurization based on the 3GPP IMSI"; |
| 942 | case QL_SIM_PERSO_FEATURE_3GPP2_NETWORK_TYPE_1: |
| 943 | return "featurization based on 3GPP2 MCC and MNC"; |
| 944 | case QL_SIM_PERSO_FEATURE_3GPP2_NETWORK_TYPE_2: |
| 945 | return "featurization based on 3GPP2 IRM code"; |
| 946 | case QL_SIM_PERSO_FEATURE_3GPP2_RUIM: |
| 947 | return "featurization based on 3GPP2 IMSI_M"; |
| 948 | default: |
| 949 | return "N/A"; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | static char *card_pin_state_desc(QL_SIM_PIN_STATE_E state) |
| 954 | { |
| 955 | switch (state) |
| 956 | { |
| 957 | case QL_SIM_PIN_STATE_UNKNOWN: |
| 958 | return "unknown"; |
| 959 | case QL_SIM_PIN_STATE_ENABLED_NOT_VERIFIED: |
| 960 | return "PIN required, but has not been verified"; |
| 961 | case QL_SIM_PIN_STATE_ENABLED_VERIFIED: |
| 962 | return "PIN required and has been verified"; |
| 963 | case QL_SIM_PIN_STATE_DISABLED: |
| 964 | return "PIN not required"; |
| 965 | case QL_SIM_PIN_STATE_BLOCKED: |
| 966 | return "PIN verification has failed too many times and is blocked. " |
| 967 | "Recoverable through PUK verification"; |
| 968 | case QL_SIM_PIN_STATE_PERMANENTLY_BLOCKED: |
| 969 | return "PUK verification has failed too many times and is not recoverable"; |
| 970 | default: |
| 971 | return "N/A"; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | |
| 976 | static void item_ql_sim_get_card_info(void) |
| 977 | { |
| 978 | int ret = 0; |
| 979 | ql_sim_card_info_t info = {0}; |
| 980 | int input = 0; |
| 981 | QL_SIM_SLOT_E slot; |
| 982 | |
| 983 | printf("test ql_sim_get_card_info: \n"); |
| 984 | printf("please enter slot(1 or 2): "); |
| 985 | scanf("%d", &input); |
| 986 | getchar(); |
| 987 | if (1 == input) |
| 988 | { |
| 989 | slot = QL_SIM_SLOT_1; |
| 990 | } |
| 991 | else if (2 == input) |
| 992 | { |
| 993 | slot = QL_SIM_SLOT_2; |
| 994 | } |
| 995 | else |
| 996 | { |
| 997 | printf("bad slot: %d\n", input); |
| 998 | return; |
| 999 | } |
| 1000 | |
| 1001 | ret = ql_sim_get_card_info(slot, &info); |
| 1002 | if (ret != QL_ERR_OK) |
| 1003 | { |
| 1004 | printf("failed, ret = %d\n", ret); |
| 1005 | return; |
| 1006 | } |
| 1007 | printf("========= CARD INFO =========\n"); |
| 1008 | printf("state: %s\n", card_state_desc(info.state)); |
| 1009 | printf("type: %s\n", card_type_desc(info.type)); |
| 1010 | printf("3gpp:\n"); |
| 1011 | printf(" app state: %s\n", card_app_state_desc(info.app_3gpp.app_state)); |
| 1012 | printf(" PIN 1 retries: %hhu\n", info.app_3gpp.pin1_num_retries); |
| 1013 | printf(" PUK 1 retries: %hhu\n", info.app_3gpp.puk1_num_retries); |
| 1014 | printf(" PIN 2 retries: %hhu\n", info.app_3gpp.pin2_num_retries); |
| 1015 | printf(" PUK 2 retries: %hhu\n", info.app_3gpp.puk2_num_retries); |
| 1016 | printf("3gpp2:\n"); |
| 1017 | printf(" app state: %s\n", card_app_state_desc(info.app_3gpp2.app_state)); |
| 1018 | printf(" PIN 1 retries: %hhu\n", info.app_3gpp2.pin1_num_retries); |
| 1019 | printf(" PUK 1 retries: %hhu\n", info.app_3gpp2.puk1_num_retries); |
| 1020 | printf(" PIN 2 retries: %hhu\n", info.app_3gpp2.pin2_num_retries); |
| 1021 | printf(" PUK 2 retries: %hhu\n", info.app_3gpp2.puk2_num_retries); |
| 1022 | printf("isim:\n"); |
| 1023 | printf(" app state: %s\n", card_app_state_desc(info.app_isim.app_state)); |
| 1024 | printf(" PIN 1 retries: %hhu\n", info.app_isim.pin1_num_retries); |
| 1025 | printf(" PUK 1 retries: %hhu\n", info.app_isim.puk1_num_retries); |
| 1026 | printf(" PIN 2 retries: %hhu\n", info.app_isim.pin2_num_retries); |
| 1027 | printf(" PUK 2 retries: %hhu\n", info.app_isim.puk2_num_retries); |
| 1028 | } |
| 1029 | |
| 1030 | #if 0 |
| 1031 | static void item_ql_sim_read_file(void) |
| 1032 | { |
| 1033 | int ret = 0; |
| 1034 | int input = 0; |
| 1035 | int len = 0; |
| 1036 | QL_SIM_SLOT_E slot; |
| 1037 | QL_SIM_APP_TYPE_E app_type; |
| 1038 | ql_sim_file_t file = {0}; |
| 1039 | |
| 1040 | printf("test ql_sim_read_file: \n"); |
| 1041 | printf("please enter slot(1 or 2): "); |
| 1042 | scanf("%d", &input); |
| 1043 | getchar(); |
| 1044 | if (1 == input) |
| 1045 | { |
| 1046 | slot = QL_SIM_SLOT_1; |
| 1047 | } |
| 1048 | else if (2 == input) |
| 1049 | { |
| 1050 | slot = QL_SIM_SLOT_2; |
| 1051 | } |
| 1052 | else |
| 1053 | { |
| 1054 | printf("bad slot: %d\n", input); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 1059 | scanf("%d", &input); |
| 1060 | getchar(); |
| 1061 | switch (input) |
| 1062 | { |
| 1063 | case 0: |
| 1064 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 1065 | break; |
| 1066 | case 1: |
| 1067 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 1068 | break; |
| 1069 | case 2: |
| 1070 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 1071 | break; |
| 1072 | case 3: |
| 1073 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 1074 | break; |
| 1075 | default: |
| 1076 | printf("bad app type: %d\n", input); |
| 1077 | return; |
| 1078 | } |
| 1079 | |
| 1080 | printf("please enter file path(at most %d hex[0·9A-F], e.g 3F002FE2): ", QL_SIM_PATH_MAX); |
| 1081 | if (NULL == fgets(file.path, QL_SIM_PATH_MAX, stdin)) |
| 1082 | { |
| 1083 | printf("can not read file path\n"); |
| 1084 | return; |
| 1085 | } |
| 1086 | len = strlen(file.path); |
| 1087 | if ('\n' == file.path[len-1]) |
| 1088 | { |
| 1089 | file.path[len-1] = 0; |
| 1090 | len--; |
| 1091 | } |
| 1092 | file.path_len = (uint32_t)len; |
| 1093 | |
| 1094 | printf("please enter record index(0 for transparent access): "); |
| 1095 | scanf("%hhu", (uint8_t *)&file.record_idx); |
| 1096 | getchar(); |
| 1097 | |
| 1098 | ret = ql_sim_read_file(slot, app_type, &file); |
| 1099 | if (ret == QL_ERR_OK) |
| 1100 | { |
| 1101 | printf("data length: %u\n", file.data_len); |
| 1102 | uint32_t i = 0; |
| 1103 | printf("data: "); |
| 1104 | for (i = 0; i < file.data_len; i++) |
| 1105 | { |
| 1106 | printf("%02x ", file.data[i]); |
| 1107 | } |
| 1108 | printf("\n"); |
| 1109 | } |
| 1110 | else |
| 1111 | { |
| 1112 | printf("failed, ret = %d\n", ret); |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | static void item_ql_sim_write_file(void) |
| 1117 | { |
| 1118 | int ret = 0; |
| 1119 | int input = 0; |
| 1120 | int len = 0; |
| 1121 | uint8_t v; |
| 1122 | QL_SIM_SLOT_E slot; |
| 1123 | QL_SIM_APP_TYPE_E app_type; |
| 1124 | ql_sim_file_t file = {0}; |
| 1125 | |
| 1126 | printf("test ql_sim_write_file: \n"); |
| 1127 | printf("please enter slot(1 or 2): "); |
| 1128 | scanf("%d", &input); |
| 1129 | getchar(); |
| 1130 | if (1 == input) |
| 1131 | { |
| 1132 | slot = QL_SIM_SLOT_1; |
| 1133 | } |
| 1134 | else if (2 == input) |
| 1135 | { |
| 1136 | slot = QL_SIM_SLOT_2; |
| 1137 | } |
| 1138 | else |
| 1139 | { |
| 1140 | printf("bad slot: %d\n", input); |
| 1141 | return; |
| 1142 | } |
| 1143 | |
| 1144 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 1145 | scanf("%d", &input); |
| 1146 | getchar(); |
| 1147 | switch (input) |
| 1148 | { |
| 1149 | case 0: |
| 1150 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 1151 | break; |
| 1152 | case 1: |
| 1153 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 1154 | break; |
| 1155 | case 2: |
| 1156 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 1157 | break; |
| 1158 | case 3: |
| 1159 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 1160 | break; |
| 1161 | default: |
| 1162 | printf("bad app type: %d\n", input); |
| 1163 | return; |
| 1164 | } |
| 1165 | |
| 1166 | printf("please enter file path(at most %d hex[0·9A-F], e.g 3F002FE2): ", QL_SIM_PATH_MAX); |
| 1167 | if (NULL == fgets(file.path, QL_SIM_PATH_MAX, stdin)) |
| 1168 | { |
| 1169 | printf("can not read file path\n"); |
| 1170 | return; |
| 1171 | } |
| 1172 | len = strlen(file.path); |
| 1173 | if ('\n' == file.path[len-1]) |
| 1174 | { |
| 1175 | file.path[len-1] = 0; |
| 1176 | len--; |
| 1177 | } |
| 1178 | file.path_len = (uint32_t)len; |
| 1179 | |
| 1180 | printf("please enter record index(0 for transparent access): "); |
| 1181 | scanf("%hhu", (uint8_t *)&file.record_idx); |
| 1182 | getchar(); |
| 1183 | |
| 1184 | printf("please enter data(hex, end with `q'): "); |
| 1185 | while (1 == scanf("%hhx", &v)) |
| 1186 | { |
| 1187 | file.data[file.data_len++] = v; |
| 1188 | } |
| 1189 | getchar(); // read `q' |
| 1190 | getchar(); // read '\n' |
| 1191 | |
| 1192 | printf("please enter data offset: "); |
| 1193 | scanf("%hu", &file.offset); |
| 1194 | getchar(); |
| 1195 | |
| 1196 | ret = ql_sim_write_file(slot, app_type, &file); |
| 1197 | if (ret == QL_ERR_OK) |
| 1198 | { |
| 1199 | printf("ok\n"); |
| 1200 | } |
| 1201 | else |
| 1202 | { |
| 1203 | printf("failed, ret = %d\n", ret); |
| 1204 | } |
| 1205 | } |
| 1206 | |
| 1207 | static char *file_type_desc(QL_SIM_FILE_TYPE_E type) |
| 1208 | { |
| 1209 | switch (type) |
| 1210 | { |
| 1211 | case QL_SIM_FILE_TYPE_UNKNOWN: |
| 1212 | return "unknown"; |
| 1213 | case QL_SIM_FILE_TYPE_TRANSPARENT: |
| 1214 | return "transparent"; |
| 1215 | case QL_SIM_FILE_TYPE_CYCLIC: |
| 1216 | return "cyclic"; |
| 1217 | case QL_SIM_FILE_TYPE_LINEAR_FIXED: |
| 1218 | return "linear fixed"; |
| 1219 | default: |
| 1220 | return "N/A"; |
| 1221 | } |
| 1222 | } |
| 1223 | |
| 1224 | static void item_ql_sim_get_file_info(void) |
| 1225 | { |
| 1226 | int ret = 0; |
| 1227 | int input = 0; |
| 1228 | int len = 0; |
| 1229 | QL_SIM_SLOT_E slot; |
| 1230 | QL_SIM_APP_TYPE_E app_type; |
| 1231 | ql_sim_file_info_t info = {0}; |
| 1232 | |
| 1233 | printf("test ql_sim_get_file_info: \n"); |
| 1234 | printf("please enter slot(1 or 2): "); |
| 1235 | scanf("%d", &input); |
| 1236 | getchar(); |
| 1237 | if (1 == input) |
| 1238 | { |
| 1239 | slot = QL_SIM_SLOT_1; |
| 1240 | } |
| 1241 | else if (2 == input) |
| 1242 | { |
| 1243 | slot = QL_SIM_SLOT_2; |
| 1244 | } |
| 1245 | else |
| 1246 | { |
| 1247 | printf("bad slot: %d\n", input); |
| 1248 | return; |
| 1249 | } |
| 1250 | |
| 1251 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 1252 | scanf("%d", &input); |
| 1253 | getchar(); |
| 1254 | switch (input) |
| 1255 | { |
| 1256 | case 0: |
| 1257 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 1258 | break; |
| 1259 | case 1: |
| 1260 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 1261 | break; |
| 1262 | case 2: |
| 1263 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 1264 | break; |
| 1265 | case 3: |
| 1266 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 1267 | break; |
| 1268 | default: |
| 1269 | printf("bad app type: %d\n", input); |
| 1270 | return; |
| 1271 | } |
| 1272 | |
| 1273 | printf("please enter file path(at most %d hex[0·9A-F], e.g 3F002FE2): ", QL_SIM_PATH_MAX); |
| 1274 | if (NULL == fgets(info.path, QL_SIM_PATH_MAX, stdin)) |
| 1275 | { |
| 1276 | printf("can not read file path\n"); |
| 1277 | return; |
| 1278 | } |
| 1279 | len = strlen(info.path); |
| 1280 | if ('\n' == info.path[len-1]) |
| 1281 | { |
| 1282 | info.path[len-1] = 0; |
| 1283 | len--; |
| 1284 | } |
| 1285 | info.path_len = (uint32_t)len; |
| 1286 | |
| 1287 | ret = ql_sim_get_file_info(slot, app_type, &info); |
| 1288 | if (ret == QL_ERR_OK) |
| 1289 | { |
| 1290 | printf("========= FILE INFO =========\n"); |
| 1291 | printf("path: %s\n", info.path); |
| 1292 | printf("type: %s\n", file_type_desc(info.file_type)); |
| 1293 | printf("file size: %hu\n", info.file_size); |
| 1294 | printf("record size: %hu\n", info.record_size); |
| 1295 | printf("record count: %hu\n", info.record_count); |
| 1296 | } |
| 1297 | else |
| 1298 | { |
| 1299 | printf("failed, ret = %d\n", ret); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | static void item_ql_sim_read_phone_book(void) |
| 1304 | { |
| 1305 | int ret = 0; |
| 1306 | int input = 0; |
| 1307 | QL_SIM_SLOT_E slot; |
| 1308 | QL_SIM_APP_TYPE_E app_type; |
| 1309 | uint8_t record_idx = 0; |
| 1310 | ql_sim_phone_book_record_t record; |
| 1311 | |
| 1312 | memset(&record,0,sizeof(ql_sim_phone_book_record_t)); |
| 1313 | printf("test ql_sim_read_phone_book: \n"); |
| 1314 | printf("please enter slot(1 or 2): "); |
| 1315 | scanf("%d", &input); |
| 1316 | getchar(); |
| 1317 | if (1 == input) |
| 1318 | { |
| 1319 | slot = QL_SIM_SLOT_1; |
| 1320 | } |
| 1321 | else if (2 == input) |
| 1322 | { |
| 1323 | slot = QL_SIM_SLOT_2; |
| 1324 | } |
| 1325 | else |
| 1326 | { |
| 1327 | printf("bad slot: %d\n", input); |
| 1328 | return; |
| 1329 | } |
| 1330 | |
| 1331 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 1332 | scanf("%d", &input); |
| 1333 | getchar(); |
| 1334 | switch (input) |
| 1335 | { |
| 1336 | case 0: |
| 1337 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 1338 | break; |
| 1339 | case 1: |
| 1340 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 1341 | break; |
| 1342 | case 2: |
| 1343 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 1344 | break; |
| 1345 | case 3: |
| 1346 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 1347 | break; |
| 1348 | default: |
| 1349 | printf("bad app type: %d\n", input); |
| 1350 | return; |
| 1351 | } |
| 1352 | |
| 1353 | printf("please enter record index: "); |
| 1354 | scanf("%hhu", &record_idx); |
| 1355 | getchar(); |
| 1356 | |
| 1357 | |
| 1358 | ret = ql_sim_read_phone_book(slot, app_type, QL_SIM_PB_DEFAULT_PATH, record_idx, &record); |
| 1359 | if (ret == QL_ERR_OK) |
| 1360 | { |
| 1361 | printf("Name: %s\n", record.name); |
| 1362 | printf("Number: %s\n", record.number); |
| 1363 | } |
| 1364 | else |
| 1365 | { |
| 1366 | printf("failed, ret = %d\n", ret); |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | static void item_ql_sim_write_phone_book(void) |
| 1371 | { |
| 1372 | int ret = 0; |
| 1373 | int input = 0; |
| 1374 | int len = 0; |
| 1375 | QL_SIM_SLOT_E slot; |
| 1376 | QL_SIM_APP_TYPE_E app_type; |
| 1377 | uint8_t record_idx = 0; |
| 1378 | ql_sim_phone_book_record_t record; |
| 1379 | |
| 1380 | memset(&record,0,sizeof(ql_sim_phone_book_record_t)); |
| 1381 | printf("test ql_sim_write_phone_book: \n"); |
| 1382 | printf("please enter slot(1 or 2): "); |
| 1383 | scanf("%d", &input); |
| 1384 | getchar(); |
| 1385 | if (1 == input) |
| 1386 | { |
| 1387 | slot = QL_SIM_SLOT_1; |
| 1388 | } |
| 1389 | else if (2 == input) |
| 1390 | { |
| 1391 | slot = QL_SIM_SLOT_2; |
| 1392 | } |
| 1393 | else |
| 1394 | { |
| 1395 | printf("bad slot: %d\n", input); |
| 1396 | return; |
| 1397 | } |
| 1398 | |
| 1399 | printf("please enter app type(0-unknown, 1-3gpp, 2-3gpp2, 3-isim): "); |
| 1400 | scanf("%d", &input); |
| 1401 | getchar(); |
| 1402 | switch (input) |
| 1403 | { |
| 1404 | case 0: |
| 1405 | app_type = QL_SIM_APP_TYPE_UNKNOWN; |
| 1406 | break; |
| 1407 | case 1: |
| 1408 | app_type = QL_SIM_APP_TYPE_3GPP; |
| 1409 | break; |
| 1410 | case 2: |
| 1411 | app_type = QL_SIM_APP_TYPE_3GPP2; |
| 1412 | break; |
| 1413 | case 3: |
| 1414 | app_type = QL_SIM_APP_TYPE_ISIM; |
| 1415 | break; |
| 1416 | default: |
| 1417 | printf("bad app type: %d\n", input); |
| 1418 | return; |
| 1419 | } |
| 1420 | |
| 1421 | printf("please enter record index: "); |
| 1422 | scanf("%hhu", &record_idx); |
| 1423 | getchar(); |
| 1424 | |
| 1425 | printf("please enter name(at most %d chars): ", QL_SIM_PHONE_BOOK_NAME_MAX - 1); |
| 1426 | if (NULL == fgets(record.name, QL_SIM_PHONE_BOOK_NAME_MAX, stdin)) |
| 1427 | { |
| 1428 | printf("\nname will be set to 0\n"); |
| 1429 | } |
| 1430 | else |
| 1431 | { |
| 1432 | len = strlen(record.name); |
| 1433 | if ('\n' == record.name[len-1]) |
| 1434 | { |
| 1435 | record.name[len-1] = 0; |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | printf("please enter number(at most %d digits): ", QL_SIM_PHONE_BOOK_NUMBER_MAX - 1); |
| 1441 | if (NULL == fgets(record.number, QL_SIM_PHONE_BOOK_NUMBER_MAX, stdin)) |
| 1442 | { |
| 1443 | printf("\nnumber will be set to 0\n"); |
| 1444 | } |
| 1445 | else |
| 1446 | { |
| 1447 | len = strlen(record.number); |
| 1448 | if ('\n' == record.number[len-1]) |
| 1449 | { |
| 1450 | record.number[len-1] = 0; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | ret = ql_sim_write_phone_book(slot, app_type, QL_SIM_PB_DEFAULT_PATH, record_idx, &record); |
| 1455 | if (ret == QL_ERR_OK) |
| 1456 | { |
| 1457 | printf("ok\n"); |
| 1458 | } |
| 1459 | else |
| 1460 | { |
| 1461 | printf("failed, ret = %d\n", ret); |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | static void item_ql_sim_open_logical_channel(void) |
| 1466 | { |
| 1467 | int ret = 0; |
| 1468 | int input = 0; |
| 1469 | QL_SIM_SLOT_E slot; |
| 1470 | uint8_t channel_id = 0; |
| 1471 | |
| 1472 | printf("test ql_sim_open_logical_channel: \n"); |
| 1473 | printf("please enter slot(1 or 2): "); |
| 1474 | scanf("%d", &input); |
| 1475 | getchar(); |
| 1476 | if (1 == input) |
| 1477 | { |
| 1478 | slot = QL_SIM_SLOT_1; |
| 1479 | } |
| 1480 | else if (2 == input) |
| 1481 | { |
| 1482 | slot = QL_SIM_SLOT_2; |
| 1483 | } |
| 1484 | else |
| 1485 | { |
| 1486 | printf("bad slot: %d\n", input); |
| 1487 | return; |
| 1488 | } |
| 1489 | |
| 1490 | ret = ql_sim_open_logical_channel(slot, &channel_id); |
| 1491 | if (ret == QL_ERR_OK) |
| 1492 | { |
| 1493 | printf("channel id: %hhu\n", channel_id); |
| 1494 | } |
| 1495 | else |
| 1496 | { |
| 1497 | printf("failed, ret = %d\n", ret); |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | static void item_ql_sim_close_logical_channel(void) |
| 1502 | { |
| 1503 | int ret = 0; |
| 1504 | int input = 0; |
| 1505 | QL_SIM_SLOT_E slot; |
| 1506 | uint8_t channel_id = 0; |
| 1507 | |
| 1508 | printf("test ql_sim_close_logical_channel: \n"); |
| 1509 | printf("please enter slot(1 or 2): "); |
| 1510 | scanf("%d", &input); |
| 1511 | getchar(); |
| 1512 | if (1 == input) |
| 1513 | { |
| 1514 | slot = QL_SIM_SLOT_1; |
| 1515 | } |
| 1516 | else if (2 == input) |
| 1517 | { |
| 1518 | slot = QL_SIM_SLOT_2; |
| 1519 | } |
| 1520 | else |
| 1521 | { |
| 1522 | printf("bad slot: %d\n", input); |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | printf("please enter channel id: "); |
| 1527 | scanf("%hhu", &channel_id); |
| 1528 | getchar(); |
| 1529 | |
| 1530 | ret = ql_sim_close_logical_channel(slot, channel_id); |
| 1531 | if (ret == QL_ERR_OK) |
| 1532 | { |
| 1533 | printf("ok\n"); |
| 1534 | } |
| 1535 | else |
| 1536 | { |
| 1537 | printf("failed, ret = %d\n", ret); |
| 1538 | } |
| 1539 | } |
| 1540 | |
| 1541 | static void item_ql_sim_send_apdu(void) |
| 1542 | { |
| 1543 | int ret = 0; |
| 1544 | int input = 0; |
| 1545 | uint8_t v = 0; |
| 1546 | QL_SIM_SLOT_E slot; |
| 1547 | ql_sim_apdu_t *p_apdu = NULL; |
| 1548 | uint8_t channel_id = 0; |
| 1549 | |
| 1550 | printf("test ql_sim_send_apdu: \n"); |
| 1551 | |
| 1552 | p_apdu = calloc(1, sizeof(*p_apdu)); |
| 1553 | if (NULL == p_apdu) |
| 1554 | { |
| 1555 | printf("run out of memory\n"); |
| 1556 | return; |
| 1557 | } |
| 1558 | |
| 1559 | printf("please enter slot(1 or 2): "); |
| 1560 | scanf("%d", &input); |
| 1561 | getchar(); |
| 1562 | if (1 == input) |
| 1563 | { |
| 1564 | slot = QL_SIM_SLOT_1; |
| 1565 | } |
| 1566 | else if (2 == input) |
| 1567 | { |
| 1568 | slot = QL_SIM_SLOT_2; |
| 1569 | } |
| 1570 | else |
| 1571 | { |
| 1572 | printf("bad slot: %d\n", input); |
| 1573 | free(p_apdu); |
| 1574 | p_apdu = NULL; |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | printf("please enter channel id: "); |
| 1579 | scanf("%hhu", &channel_id); |
| 1580 | getchar(); |
| 1581 | |
| 1582 | printf("please enter apdu data(hex, end with `q'): "); |
| 1583 | while (1 == scanf("%hhx", &v)) |
| 1584 | { |
| 1585 | p_apdu->req_apdu[p_apdu->req_apdu_len++] = v; |
| 1586 | } |
| 1587 | getchar(); // read `q' |
| 1588 | getchar(); // read '\n' |
| 1589 | |
| 1590 | ret = ql_sim_send_apdu(slot, channel_id, p_apdu); |
| 1591 | if (ret == QL_ERR_OK) |
| 1592 | { |
| 1593 | uint32_t i = 0; |
| 1594 | printf("repsonse apdu: "); |
| 1595 | for (i = 0; i < p_apdu->resp_apdu_len; i++) |
| 1596 | { |
| 1597 | printf("%c", p_apdu->resp_apdu[i]); |
| 1598 | } |
| 1599 | printf("\n"); |
| 1600 | } |
| 1601 | else |
| 1602 | { |
| 1603 | printf("failed, ret = %d\n", ret); |
| 1604 | } |
| 1605 | free(p_apdu); |
| 1606 | p_apdu = NULL; |
| 1607 | } |
| 1608 | #endif |
| 1609 | |
| 1610 | static void sim_card_status_cb(int slot, ql_sim_card_info_t *p_info) |
| 1611 | { |
| 1612 | printf("========= CARD STATUS =========\n"); |
| 1613 | switch (slot) |
| 1614 | { |
| 1615 | case 0: |
| 1616 | printf("slot: invalid\n"); |
| 1617 | break; |
| 1618 | case 1: |
| 1619 | printf("slot: 1\n"); |
| 1620 | break; |
| 1621 | case 2: |
| 1622 | printf("slot: 2\n"); |
| 1623 | break; |
| 1624 | } |
| 1625 | |
| 1626 | if (NULL == p_info) |
| 1627 | { |
| 1628 | printf("status: unavailable\n"); |
| 1629 | return; |
| 1630 | } |
| 1631 | |
| 1632 | printf("state: %s\n", card_state_desc(p_info->state)); |
| 1633 | printf("type: %s\n", card_type_desc(p_info->type)); |
| 1634 | printf("3gpp:\n"); |
| 1635 | printf(" app state: %s\n", card_app_state_desc(p_info->app_3gpp.app_state)); |
| 1636 | printf(" PIN 1 retries: %hhu\n", p_info->app_3gpp.pin1_num_retries); |
| 1637 | printf(" PUK 1 retries: %hhu\n", p_info->app_3gpp.puk1_num_retries); |
| 1638 | printf(" PIN 2 retries: %hhu\n", p_info->app_3gpp.pin2_num_retries); |
| 1639 | printf(" PUK 2 retries: %hhu\n", p_info->app_3gpp.puk2_num_retries); |
| 1640 | printf("3gpp2:\n"); |
| 1641 | printf(" app state: %s\n", card_app_state_desc(p_info->app_3gpp2.app_state)); |
| 1642 | printf(" PIN 1 retries: %hhu\n", p_info->app_3gpp2.pin1_num_retries); |
| 1643 | printf(" PUK 1 retries: %hhu\n", p_info->app_3gpp2.puk1_num_retries); |
| 1644 | printf(" PIN 2 retries: %hhu\n", p_info->app_3gpp2.pin2_num_retries); |
| 1645 | printf(" PUK 2 retries: %hhu\n", p_info->app_3gpp2.puk2_num_retries); |
| 1646 | printf("isim:\n"); |
| 1647 | printf(" app state: %s\n", card_app_state_desc(p_info->app_isim.app_state)); |
| 1648 | printf(" PIN 1 retries: %hhu\n", p_info->app_isim.pin1_num_retries); |
| 1649 | printf(" PUK 1 retries: %hhu\n", p_info->app_isim.puk1_num_retries); |
| 1650 | printf(" PIN 2 retries: %hhu\n", p_info->app_isim.pin2_num_retries); |
| 1651 | printf(" PUK 2 retries: %hhu\n", p_info->app_isim.puk2_num_retries); |
| 1652 | } |
| 1653 | |
| 1654 | static void item_ql_sim_set_card_status_cb(void) |
| 1655 | { |
| 1656 | int ret = 0; |
| 1657 | |
| 1658 | printf("test ql_sim_set_card_status_cb: "); |
| 1659 | ret = ql_sim_set_card_status_cb((ql_sim_card_status_cb_f)sim_card_status_cb); |
| 1660 | if (ret == QL_ERR_OK) |
| 1661 | { |
| 1662 | printf("ok\n"); |
| 1663 | } |
| 1664 | else |
| 1665 | { |
| 1666 | printf("failed, ret = %d\n", ret); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | void sim_servicie_error_cb(int error) |
| 1671 | { |
| 1672 | printf("===== SIM Service Abort =====\n"); |
| 1673 | } |
| 1674 | |
| 1675 | void item_ql_sim_set_service_error_cb(void) |
| 1676 | { |
| 1677 | int ret = 0; |
| 1678 | printf("test ql_sim_set_service_error_cb: \n"); |
| 1679 | |
| 1680 | ret = ql_sim_set_service_error_cb(sim_servicie_error_cb); |
| 1681 | if (ret == QL_ERR_OK) |
| 1682 | { |
| 1683 | printf("ok\n"); |
| 1684 | } |
| 1685 | else |
| 1686 | { |
| 1687 | printf("failed, ret = %d\n", ret); |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | #if 0 |
| 1692 | void item_ql_sim_switch_slot(void) |
| 1693 | { |
| 1694 | int ret = 0; |
| 1695 | int input = 0; |
| 1696 | QL_SIM_PHY_SLOT_E phy_slot = 0; |
| 1697 | printf("test item_ql_sim_switch_slot: \n"); |
| 1698 | printf("please enter slot(1 or 2): "); |
| 1699 | scanf("%d", &input); |
| 1700 | getchar(); |
| 1701 | if (1 == input) |
| 1702 | { |
| 1703 | phy_slot = QL_SIM_PHY_SLOT_1; |
| 1704 | } |
| 1705 | else if (2 == input) |
| 1706 | { |
| 1707 | phy_slot = QL_SIM_PHY_SLOT_2; |
| 1708 | } |
| 1709 | else |
| 1710 | { |
| 1711 | printf("bad slot: %d\n", input); |
| 1712 | return; |
| 1713 | } |
| 1714 | ret = ql_sim_switch_slot(QL_SIM_SLOT_1, phy_slot); |
| 1715 | if (ret == QL_ERR_OK) |
| 1716 | { |
| 1717 | printf("ok\n"); |
| 1718 | } |
| 1719 | else |
| 1720 | { |
| 1721 | printf("failed, ret = %d\n", ret); |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | void item_ql_sim_get_active_slots(void) |
| 1726 | { |
| 1727 | int ret = 0; |
| 1728 | ql_sim_active_slots_t *p_active_slots = (ql_sim_active_slots_t *)malloc(sizeof(ql_sim_active_slots_t)); |
| 1729 | if (p_active_slots == NULL) { |
| 1730 | printf("Memory allocation failed.\n"); |
| 1731 | return -1; |
| 1732 | } |
| 1733 | p_active_slots->active_slots_len = 0; |
| 1734 | p_active_slots->active_slots[0] = 0; |
| 1735 | printf("test item_ql_sim_get_active_slots: \n"); |
| 1736 | ret = ql_sim_get_active_slots(p_active_slots); |
| 1737 | if (ret == QL_ERR_OK) |
| 1738 | { |
| 1739 | printf("ok\n"); |
| 1740 | printf("p_active_slots(QL_SIM_PHY_SLOT_1/2 = B01/B02) = %X \n",p_active_slots->active_slots[0]); |
| 1741 | } |
| 1742 | else |
| 1743 | { |
| 1744 | printf("failed, ret = %d\n", ret); |
| 1745 | } |
| 1746 | free(p_active_slots); |
| 1747 | } |
| 1748 | #endif |
| 1749 | |
| 1750 | static t_item_t ql_sim_items[] = |
| 1751 | { |
| 1752 | {"ql_sim_init", item_ql_sim_init}, |
| 1753 | {"ql_sim_get_imsi", item_ql_sim_get_imsi}, |
| 1754 | {"ql_sim_get_iccid", item_ql_sim_get_iccid}, |
| 1755 | {"ql_sim_get_phone_num", item_ql_sim_get_phone_num}, |
| 1756 | {"ql_sim_get_operators", item_ql_sim_get_operators}, |
| 1757 | {"ql_sim_enable_pin", item_ql_sim_enable_pin}, |
| 1758 | {"ql_sim_disable_pin", item_ql_sim_disable_pin}, |
| 1759 | {"ql_sim_verify_pin", item_ql_sim_verify_pin}, |
| 1760 | {"ql_sim_change_pin", item_ql_sim_change_pin}, |
| 1761 | {"ql_sim_unblock_pin", item_ql_sim_unblock_pin}, |
| 1762 | {"ql_sim_get_card_info", item_ql_sim_get_card_info}, |
| 1763 | // {"ql_sim_read_file", item_ql_sim_read_file}, |
| 1764 | // {"ql_sim_write_file", item_ql_sim_write_file}, |
| 1765 | // {"ql_sim_get_file_info", item_ql_sim_get_file_info}, |
| 1766 | // {"ql_sim_read_phone_book", item_ql_sim_read_phone_book}, |
| 1767 | // {"ql_sim_write_phone_book", item_ql_sim_write_phone_book}, |
| 1768 | // {"ql_sim_open_logical_channel", item_ql_sim_open_logical_channel}, |
| 1769 | // {"ql_sim_close_logical_channel", item_ql_sim_close_logical_channel}, |
| 1770 | // {"ql_sim_send_apdu", item_ql_sim_send_apdu}, |
| 1771 | {"ql_sim_set_card_status_cb", item_ql_sim_set_card_status_cb}, |
| 1772 | {"ql_sim_set_service_error_cb", item_ql_sim_set_service_error_cb}, |
| 1773 | // {"ql_sim_switch_slot", item_ql_sim_switch_slot}, |
| 1774 | // {"ql_sim_get_active_slots", item_ql_sim_get_active_slots}, |
| 1775 | {"ql_sim_deinit", item_ql_sim_deinit}, |
| 1776 | }; |
| 1777 | |
| 1778 | t_module_t ql_sim_module = |
| 1779 | { |
| 1780 | "sim", |
| 1781 | T_ARRAY_SIZE(ql_sim_items), |
| 1782 | ql_sim_items |
| 1783 | }; |
| 1784 | |
| 1785 | /*-----------------------------------------------------------------------------------------------*/ |
| 1786 | /** |
| 1787 | @brief Read a int value from stdin |
| 1788 | @param[out] val, Return read data |
| 1789 | @return |
| 1790 | 0 - successful |
| 1791 | 1 - read an enter |
| 1792 | -1 - invalid input |
| 1793 | */ |
| 1794 | /*-----------------------------------------------------------------------------------------------*/ |
| 1795 | int t_get_int(int *val) |
| 1796 | { |
| 1797 | int dat; |
| 1798 | char *ptr_end = NULL; |
| 1799 | char buf[256] = {0}; |
| 1800 | |
| 1801 | if(NULL == fgets(buf, sizeof(buf)-1, stdin)) |
| 1802 | { |
| 1803 | return -1; |
| 1804 | } |
| 1805 | #if 0 |
| 1806 | if(0 == buf[0]) |
| 1807 | { |
| 1808 | return -1; |
| 1809 | } |
| 1810 | #endif |
| 1811 | if(buf[0] == '\n') |
| 1812 | { |
| 1813 | return 1; |
| 1814 | } |
| 1815 | |
| 1816 | dat = strtol(buf, &ptr_end, 10); |
| 1817 | if(ptr_end!=NULL && ptr_end[0]!='\n') |
| 1818 | { |
| 1819 | return -1; |
| 1820 | } |
| 1821 | |
| 1822 | if(val) |
| 1823 | { |
| 1824 | val[0] = dat; |
| 1825 | } |
| 1826 | |
| 1827 | return 0; |
| 1828 | } |
| 1829 | |
| 1830 | void dump_items() |
| 1831 | { |
| 1832 | int i; |
| 1833 | |
| 1834 | printf("\n"); |
| 1835 | printf("The current module is: \n"); |
| 1836 | |
| 1837 | for(i=0; i< ql_sim_module.item_len; i++) |
| 1838 | { |
| 1839 | printf("%d\t%s\n", i, ql_sim_module.item_list[i].name); |
| 1840 | } |
| 1841 | printf("-1\texit\n"); |
| 1842 | } |
| 1843 | |
| 1844 | int main(int argc, char *argv[]) |
| 1845 | { |
| 1846 | int ret; |
| 1847 | int idx; |
| 1848 | |
| 1849 | dump_items(); |
| 1850 | |
| 1851 | while(1) |
| 1852 | { |
| 1853 | printf("Please enter your choice: "); |
| 1854 | ret = t_get_int(&idx); |
| 1855 | printf("\n"); |
| 1856 | if(ret < 0) |
| 1857 | { |
| 1858 | printf("Invalid input\n"); |
| 1859 | continue; |
| 1860 | } |
| 1861 | else if(ret == 1) |
| 1862 | { |
| 1863 | dump_items(); |
| 1864 | continue; |
| 1865 | } |
| 1866 | |
| 1867 | if(idx == -1) |
| 1868 | { |
| 1869 | break; |
| 1870 | } |
| 1871 | |
| 1872 | if(idx<0 || idx>=ql_sim_module.item_len) |
| 1873 | { |
| 1874 | printf("Not support idx: %d\n", idx); |
| 1875 | continue; |
| 1876 | } |
| 1877 | |
| 1878 | printf("->Item : %s\n", ql_sim_module.item_list[idx].name); |
| 1879 | ql_sim_module.item_list[idx].handle(); |
| 1880 | } |
| 1881 | |
| 1882 | return 0; |
| 1883 | } |
| 1884 | |
| 1885 | |
| 1886 | |