blob: c8f1b5fa22f0dcbfb9d78fb49e532e45e28208c9 [file] [log] [blame]
Harald Welte1200c822020-02-13 20:43:27 +01001#include <errno.h>
2#include <unistd.h>
3
4#include <osmocom/core/select.h>
5
6#include "client.h"
7
8/* This is a remsim-client with an interactive 'shell', where the user
9 * can type in C-APDUs in hex formats, which will be sent to the bankd /
10 * SIM-card. Responses received from SIM Card via bankd will be printed
11 * in return. */
12
13
14/***********************************************************************
Harald Welte0e968cc2020-02-22 18:16:16 +010015 * stdin frontend code to remsim-client
Harald Welte1200c822020-02-13 20:43:27 +010016 ***********************************************************************/
17
Harald Welte0e968cc2020-02-22 18:16:16 +010018int frontend_request_card_insert(struct bankd_client *bc)
Harald Welte1200c822020-02-13 20:43:27 +010019{
Harald Welte0e968cc2020-02-22 18:16:16 +010020 return 0;
21}
Harald Welte1200c822020-02-13 20:43:27 +010022
Harald Welte0e968cc2020-02-22 18:16:16 +010023int frontend_request_sim_remote(struct bankd_client *bc)
24{
25 return 0;
26}
Harald Welte1200c822020-02-13 20:43:27 +010027
Harald Welte0e968cc2020-02-22 18:16:16 +010028int frontend_request_modem_reset(struct bankd_client *bc)
29{
30 return 0;
31}
32
33int frontend_handle_card2modem(struct bankd_client *bc, const uint8_t *data, size_t len)
34{
35 OSMO_ASSERT(data);
36 printf("R-APDU: %s\n", osmo_hexdump(data, len));
Harald Welte82368402020-02-18 22:59:37 +010037 fflush(stdout);
Harald Welte1200c822020-02-13 20:43:27 +010038
39 return 0;
40}
41
Harald Welte0e968cc2020-02-22 18:16:16 +010042int frontend_handle_set_atr(struct bankd_client *bc, const uint8_t *data, size_t len)
Harald Welte1200c822020-02-13 20:43:27 +010043{
Harald Welte0e968cc2020-02-22 18:16:16 +010044 OSMO_ASSERT(data);
Harald Welte1200c822020-02-13 20:43:27 +010045
Harald Welte0e968cc2020-02-22 18:16:16 +010046 printf("SET_ATR: %s\n", osmo_hexdump(data, len));
Harald Welte82368402020-02-18 22:59:37 +010047 fflush(stdout);
Harald Welte1200c822020-02-13 20:43:27 +010048
Harald Welte1200c822020-02-13 20:43:27 +010049 return 0;
50}
51
Harald Welte0e968cc2020-02-22 18:16:16 +010052int frontend_handle_slot_status(struct bankd_client *bc, const SlotPhysStatus_t *sts)
Harald Welte1200c822020-02-13 20:43:27 +010053{
Harald Welte1200c822020-02-13 20:43:27 +010054 return 0;
55}
56
Harald Weltee580c932020-05-24 16:03:56 +020057int frontend_append_script_env(struct bankd_client *bc, char **env, int idx, size_t max_env)
Harald Welte0e968cc2020-02-22 18:16:16 +010058{
Harald Weltee580c932020-05-24 16:03:56 +020059 return idx;
Harald Welte0e968cc2020-02-22 18:16:16 +010060}
61
62
Harald Welte1200c822020-02-13 20:43:27 +010063/***********************************************************************
64 * Incoming command from the user application (stdin shell in our case)
65 ***********************************************************************/
66
67struct stdin_state {
68 struct osmo_fd ofd;
69 struct msgb *rx_msg;
Harald Welte8e46ab62020-02-14 12:55:43 +010070 struct bankd_client *bc;
Harald Welte1200c822020-02-13 20:43:27 +010071};
72
73/* called every time a command on stdin was received */
74static void handle_stdin_command(struct stdin_state *ss, char *cmd)
75{
Harald Welte8e46ab62020-02-14 12:55:43 +010076 struct bankd_client *bc = ss->bc;
Harald Welte1200c822020-02-13 20:43:27 +010077 int rc;
78
Harald Welte1200c822020-02-13 20:43:27 +010079 OSMO_ASSERT(ss->rx_msg);
80
Harald Welte1200c822020-02-13 20:43:27 +010081 if (!strcasecmp(cmd, "RESET")) {
82 /* reset the [remote] card */
Harald Welteaa66d912020-05-25 23:05:31 +020083 struct frontend_phys_status pstatus = {
84 .flags = {
85 .reset_active = true,
86 .vcc_present = false,
87 .clk_active = false,
88 .card_present = true,
89 },
90 .voltage_mv = 0,
91 .fi = 0,
92 .di = 0,
93 .wi = 0,
94 .waiting_time = 0,
95 };
96 osmo_fsm_inst_dispatch(bc->main_fi, MF_E_MDM_STATUS_IND, &pstatus);
Harald Welte1200c822020-02-13 20:43:27 +010097 } else {
Harald Welteaa66d912020-05-25 23:05:31 +020098 struct frontend_tpdu ftpdu;
99 uint8_t buf[1024];
100
Harald Welte1200c822020-02-13 20:43:27 +0100101 /* we assume the user has entered a C-APDU as hex string. parse + send */
102 rc = osmo_hexparse(cmd, buf, sizeof(buf));
103 if (rc < 0) {
104 fprintf(stderr, "ERROR parsing C-APDU `%s'!\n", cmd);
105 return;
106 }
Harald Welte8e46ab62020-02-14 12:55:43 +0100107 if (!bc->srv_conn.clslot) {
Harald Welte1200c822020-02-13 20:43:27 +0100108 fprintf(stderr, "Cannot send command; no client slot\n");
109 return;
110 }
111
112 /* Send CMD APDU to [remote] card */
Harald Welteaa66d912020-05-25 23:05:31 +0200113 ftpdu.buf = buf;
114 ftpdu.len = rc;
115 osmo_fsm_inst_dispatch(bc->main_fi, MF_E_MDM_TPDU, &ftpdu);
Harald Welte1200c822020-02-13 20:43:27 +0100116 }
117}
118
119/* call-back function for stdin read. Gather bytes in buffer until CR/LF received */
120static int stdin_fd_cb(struct osmo_fd *ofd, unsigned int what)
121{
122 struct stdin_state *ss = ofd->data;
123 char *cur;
124 int rc, i;
125
126 OSMO_ASSERT(what & OSMO_FD_READ);
127
128 if (!ss->rx_msg) {
129 ss->rx_msg = msgb_alloc(1024, "stdin");
130 OSMO_ASSERT(ss->rx_msg);
131 }
132
133 cur = (char *) ss->rx_msg->tail;
134 rc = read(ofd->fd, cur, msgb_tailroom(ss->rx_msg));
135 if (rc < 0)
136 return rc;
137 msgb_put(ss->rx_msg, rc);
138
139 for (i = 0; i < rc; i++) {
140 if (cur[i] == '\r' || cur[i] == '\n') {
141 cur[i] = '\0';
142 /* dispatch the command */
143 handle_stdin_command(ss, cur);
144 /* FIXME: possibly other commands */
145 msgb_free(ss->rx_msg);
146 ss->rx_msg = NULL;
147 }
148 }
149
150 return 0;
151}
152
153
154
155/* main function */
Harald Welte8e46ab62020-02-14 12:55:43 +0100156int client_user_main(struct bankd_client *bc)
Harald Welte1200c822020-02-13 20:43:27 +0100157{
158 struct stdin_state ss;
159
160 /* register stdin file descriptor with osmocom select loop abstraction */
161 memset(&ss, 0, sizeof(ss));
162 osmo_fd_setup(&ss.ofd, fileno(stdin), OSMO_FD_READ, &stdin_fd_cb, &ss, 0);
163 osmo_fd_register(&ss.ofd);
Harald Welte8e46ab62020-02-14 12:55:43 +0100164 ss.bc = bc;
Harald Welte1200c822020-02-13 20:43:27 +0100165
166 while (1) {
167 osmo_select_main(0);
168 }
169}