Harald Welte | 0b778c3 | 2019-09-29 12:10:02 +0200 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | #include <stdio.h> |
| 3 | #include <errno.h> |
| 4 | #include <signal.h> |
| 5 | #include <osmocom/core/utils.h> |
| 6 | #include <osmocom/core/logging.h> |
| 7 | #include <osmocom/core/application.h> |
| 8 | #include <osmocom/core/msgb.h> |
| 9 | #include <osmocom/sim/sim.h> |
| 10 | |
| 11 | #include "logging.h" |
| 12 | #include "cuart.h" |
| 13 | #include "iso7816_fsm.h" |
| 14 | |
| 15 | static struct card_uart g_cuart; |
| 16 | |
| 17 | enum test_state { |
| 18 | ST_WAIT_ATR, |
| 19 | ST_ATR_DONE, |
| 20 | ST_IN_TPDU, |
| 21 | }; |
| 22 | static enum test_state g_tstate = ST_WAIT_ATR; |
| 23 | |
| 24 | static void fsm_user_cb(struct osmo_fsm_inst *fi, int event, int cause, void *data) |
| 25 | { |
| 26 | printf("Handle FSM User Event %d: cause=%d, data=%p\n", event, cause, data); |
| 27 | switch (event) { |
| 28 | case ISO7816_E_ATR_DONE_IND: |
| 29 | g_tstate = ST_ATR_DONE; |
| 30 | break; |
| 31 | case ISO7816_E_TPDU_DONE_IND: |
| 32 | printf("======= TPDU: %s\n", msgb_hexdump(data)); |
| 33 | msgb_free(data); |
| 34 | g_tstate = ST_ATR_DONE; |
| 35 | default: |
| 36 | break; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static void *tall_main_ctx; |
| 41 | |
| 42 | static void signal_handler(int signal) |
| 43 | { |
| 44 | switch (signal) { |
| 45 | case SIGUSR1: |
| 46 | talloc_report_full(tall_main_ctx, stderr); |
| 47 | break; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | int main(int argc, char **argv) |
| 52 | { |
| 53 | struct osmo_fsm_inst *fi; |
| 54 | uint8_t atr[64]; |
| 55 | int rc; |
| 56 | |
| 57 | tall_main_ctx = talloc_named_const(NULL, 0, "main"); |
| 58 | msgb_talloc_ctx_init(tall_main_ctx, 0); |
| 59 | osmo_init_logging2(tall_main_ctx, &log_info); |
| 60 | osmo_fsm_log_addr(false); |
| 61 | |
| 62 | signal(SIGUSR1, &signal_handler); |
| 63 | |
| 64 | rc = card_uart_open(&g_cuart, "tty", "/dev/ttyUSB5"); |
| 65 | if (rc < 0) { |
| 66 | perror("opening UART"); |
| 67 | exit(1); |
| 68 | } |
| 69 | |
| 70 | fi = iso7816_fsm_alloc(NULL, LOGL_DEBUG, "SIM0", &g_cuart, fsm_user_cb, NULL); |
| 71 | OSMO_ASSERT(fi); |
| 72 | |
| 73 | /* activate reset, then power up */ |
| 74 | card_uart_ctrl(&g_cuart, CUART_CTL_RST, true); |
| 75 | card_uart_ctrl(&g_cuart, CUART_CTL_POWER, true); |
| 76 | osmo_fsm_inst_dispatch(fi, ISO7816_E_POWER_UP_IND, NULL); |
| 77 | |
| 78 | /* activate clock */ |
| 79 | card_uart_ctrl(&g_cuart, CUART_CTL_CLOCK, true); |
| 80 | |
| 81 | /* wait some time and release reset */ |
| 82 | usleep(10000); |
| 83 | card_uart_ctrl(&g_cuart, CUART_CTL_RST, false); |
| 84 | osmo_fsm_inst_dispatch(fi, ISO7816_E_RESET_REL_IND, NULL); |
| 85 | |
| 86 | /* process any events in polling mode for initial change */ |
| 87 | osmo_select_main(1); |
| 88 | |
| 89 | struct msgb *apdu; |
| 90 | while (1) { |
| 91 | /* check if the new state requires us to do something */ |
| 92 | switch (g_tstate) { |
| 93 | case ST_ATR_DONE: |
| 94 | apdu = msgb_alloc(512, "TPDU"); |
| 95 | msgb_put_u8(apdu, 0x00); |
| 96 | msgb_put_u8(apdu, 0xa4); |
| 97 | msgb_put_u8(apdu, 0x00); |
| 98 | msgb_put_u8(apdu, 0x04); |
| 99 | msgb_put_u8(apdu, 0x02); |
| 100 | msgb_put_u8(apdu, 0x2f); |
| 101 | msgb_put_u8(apdu, 0x00); |
| 102 | osmo_fsm_inst_dispatch(fi, ISO7816_E_XCEIVE_TPDU_CMD, apdu); |
| 103 | g_tstate = ST_IN_TPDU; |
| 104 | break; |
| 105 | default: |
| 106 | break; |
| 107 | } |
| 108 | osmo_select_main(0); |
| 109 | } |
| 110 | |
| 111 | exit(0); |
| 112 | } |