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