FreeCalypso > hg > fc-sim-tools
annotate libcommon/backend.c @ 45:9eb5460f51a6
main tools: support both pcsc and serial back ends
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 21 Mar 2021 01:56:49 +0000 |
parents | c9ef9e91dd8e |
children |
rev | line source |
---|---|
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module is responsible for launching and connecting |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * our SIM card communication back end. |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdlib.h> |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <unistd.h> |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
10 extern unsigned calypso_fd, pcsc_reader_num, serial_spenh; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
11 extern int use_pcsc; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
12 extern char *serial_device, *serial_baud; |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 static char calypso_be_pathname[] = "/opt/freecalypso/bin/fcsim-calypso-be"; |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
15 static char serial_be_pathname[] = "/opt/freecalypso/bin/fcsim-serial-be"; |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 static char pcsc_be_pathname[] = "/opt/freecalypso/bin/fc-pcsc-backend"; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
18 static char *backend_prog, *backend_argv[4], backend_optarg[16]; |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 FILE *cpipeF, *rpipeF; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 static void |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 setup_be_calypso() |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 backend_prog = calypso_be_pathname; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 backend_argv[0] = "fcsim-calypso-be"; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 sprintf(backend_optarg, "-C%u", calypso_fd); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 backend_argv[1] = backend_optarg; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 backend_argv[2] = 0; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 static void |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 setup_be_pcsc() |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 backend_prog = pcsc_be_pathname; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 backend_argv[0] = "fc-pcsc-backend"; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 sprintf(backend_optarg, "-p%u", pcsc_reader_num); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 backend_argv[1] = backend_optarg; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 backend_argv[2] = 0; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
42 static void |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
43 setup_be_serial() |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
44 { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
45 backend_prog = serial_be_pathname; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
46 backend_argv[0] = "fcsim-serial-be"; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
47 backend_argv[1] = serial_device; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
48 if (serial_baud) |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
49 backend_argv[2] = serial_baud; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
50 else if (serial_spenh) { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
51 sprintf(backend_optarg, "9600,55800,%u", serial_spenh); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
52 backend_argv[2] = backend_optarg; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
53 } else |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
54 backend_argv[2] = "9600"; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
55 backend_argv[3] = 0; |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
56 } |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
57 |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 launch_backend() |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 int cpipe[2], rpipe[2], rc; |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (calypso_fd) |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 setup_be_calypso(); |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
64 else if (use_pcsc) |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 setup_be_pcsc(); |
45
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
66 else if (serial_device) |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
67 setup_be_serial(); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
68 else { |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
69 fprintf(stderr, "error: no -d or -p target selected\n"); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
70 exit(1); |
9eb5460f51a6
main tools: support both pcsc and serial back ends
Mychaela Falconia <falcon@freecalypso.org>
parents:
9
diff
changeset
|
71 } |
9
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
72 if (pipe(cpipe) < 0 || pipe(rpipe) < 0) { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 perror("pipe"); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 exit(1); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 rc = vfork(); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 if (rc < 0) { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 perror("vfork for launching back end"); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 exit(1); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 if (!rc) { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 /* we are in the child - prepare to exec the BE */ |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 dup2(cpipe[0], 0); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 dup2(rpipe[1], 1); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 close(cpipe[0]); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 close(cpipe[1]); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 close(rpipe[0]); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 close(rpipe[1]); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 /* do the exec */ |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 execv(backend_prog, backend_argv); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 perror(backend_prog); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 _exit(1); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 close(cpipe[0]); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
95 close(rpipe[1]); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
96 cpipeF = fdopen(cpipe[1], "w"); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 if (!cpipeF) { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 perror("fdopen"); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
99 exit(1); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
101 rpipeF = fdopen(rpipe[0], "r"); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
102 if (!rpipeF) { |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 perror("fdopen"); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 exit(1); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
105 } |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
106 return(0); |
c9ef9e91dd8e
new libcommon, initial version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
107 } |