FreeCalypso > hg > fc-sim-tools
comparison calypso/main.c @ 4:deeeef558279
fcsim-calypso-be put together
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 14 Mar 2021 05:07:34 +0000 |
parents | |
children | f6b03af63bf7 |
comparison
equal
deleted
inserted
replaced
3:45ea06eaa9fd | 4:deeeef558279 |
---|---|
1 #include <ctype.h> | |
2 #include <stdio.h> | |
3 #include <stdlib.h> | |
4 #include <string.h> | |
5 #include <strings.h> | |
6 | |
7 static | |
8 is_string_all_hex(str) | |
9 char *str; | |
10 { | |
11 char *cp; | |
12 | |
13 for (cp = str; *cp; cp++) | |
14 if (!isxdigit(*cp)) | |
15 return(0); | |
16 return(1); | |
17 } | |
18 | |
19 cmd_exchange(input) | |
20 char *input; | |
21 { | |
22 char *targv[3]; | |
23 int rc; | |
24 | |
25 targv[0] = "X"; | |
26 targv[1] = input; | |
27 targv[2] = 0; | |
28 | |
29 tpinterf_make_cmd(targv); | |
30 rc = tpinterf_send_cmd(); | |
31 if (rc < 0) | |
32 return(rc); | |
33 return tpinterf_pass_output(20); | |
34 } | |
35 | |
36 cmd_atr() | |
37 { | |
38 static char *atr_argv[2] = {"atr", 0}; | |
39 int rc; | |
40 | |
41 tpinterf_make_cmd(atr_argv); | |
42 rc = tpinterf_send_cmd(); | |
43 if (rc < 0) | |
44 return(rc); | |
45 return tpinterf_pass_output(1); | |
46 } | |
47 | |
48 cmd_poweroff() | |
49 { | |
50 static char *poweroff_argv[2] = {"poweroff", 0}; | |
51 | |
52 tpinterf_make_cmd(poweroff_argv); | |
53 tpinterf_send_cmd(); | |
54 } | |
55 | |
56 main(argc, argv) | |
57 char **argv; | |
58 { | |
59 char inbuf[576], *cp; | |
60 unsigned len; | |
61 | |
62 parse_target_fd_opt(argc, argv); | |
63 putchar('\n'); | |
64 | |
65 while (fgets(inbuf, sizeof inbuf, stdin)) { | |
66 cp = index(inbuf, '\n'); | |
67 if (!cp) { | |
68 printf("back end error: missing newline on input\n"); | |
69 continue; | |
70 } | |
71 *cp = '\0'; | |
72 if (!strcmp(inbuf, "atr")) { | |
73 cmd_atr(); | |
74 continue; | |
75 } | |
76 if (!strcmp(inbuf, "poweroff")) { | |
77 cmd_poweroff(); | |
78 exit(0); | |
79 } | |
80 if (!is_string_all_hex(inbuf)) { | |
81 printf("back end error: input is not all hex\n"); | |
82 continue; | |
83 } | |
84 len = strlen(inbuf); | |
85 if (len & 1) { | |
86 printf( | |
87 "back end error: input has odd number of hex digits\n"); | |
88 continue; | |
89 } | |
90 if (len < 10) { | |
91 printf( | |
92 "back end error: input is too short for command APDU\n"); | |
93 continue; | |
94 } | |
95 if (len > 520) { | |
96 printf( | |
97 "back end error: input is too long for command APDU\n"); | |
98 continue; | |
99 } | |
100 cmd_exchange(inbuf); | |
101 } | |
102 | |
103 exit(0); | |
104 } |