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