blob: e2b1ce2f79778fdd2524b3f6364efbeb8ee2a39a [file] [log] [blame]
Harald Weltea40c8e52019-09-27 19:22:34 +02001#include <unistd.h>
2#include <stdio.h>
3#include <errno.h>
4#include <osmocom/core/utils.h>
5
6#include "cuart.h"
7
8static struct card_uart g_cuart;
9
10
11static void handle_event(struct card_uart *cuart, enum card_uart_event evt, void *data)
12{
13 printf("Handle Event '%s'\n", get_value_string(card_uart_event_vals, evt));
14 switch (evt) {
15 case CUART_E_RX_SINGLE:
16 printf("\t%02x\n", *(uint8_t *)data);
17 break;
18 }
19
20}
21
22
23static int get_atr(uint8_t *atr, size_t max_len)
24{
25 int rc;
26 int i = 0;
27
28 card_uart_ctrl(&g_cuart, CUART_CTL_RST, true);
29 usleep(100000);
30 card_uart_ctrl(&g_cuart, CUART_CTL_RST, false);
31
32 sleep(1);
33 osmo_select_main(true);
34
35 for (i = 0; i < max_len; i++) {
36 rc = card_uart_rx(&g_cuart, &atr[i], 1);
37 if (rc <= 0)
38 break;
39 }
40
41 return i;
42}
43
44static void test_apdu(void)
45{
46 const uint8_t select_mf[] = "\xa0\xa4\x04\x00\x02\x3f\x00";
Harald Welte6603d952019-10-09 20:50:13 +020047 card_uart_tx(&g_cuart, select_mf, 5, true);
Harald Weltea40c8e52019-09-27 19:22:34 +020048
49 osmo_select_main(true);
Harald Welte18ec3222019-09-28 21:41:59 +020050 sleep(1);
51 osmo_select_main(true);
Harald Weltea40c8e52019-09-27 19:22:34 +020052 /* we should get an RX_SINGLE event here */
53}
54
55
56int main(int argc, char **argv)
57{
58 uint8_t atr[64];
59 int rc;
60
Harald Welte31313a62019-10-09 21:03:14 +020061 if (argc < 2) {
62 fprintf(stderr, "You must specify the UART tty device as argument\n");
63 exit(2);
64 }
65
66 printf("Opening UART device %s\n", argv[1]);
67
Harald Weltea40c8e52019-09-27 19:22:34 +020068 g_cuart.handle_event = &handle_event;
Harald Welte31313a62019-10-09 21:03:14 +020069 rc = card_uart_open(&g_cuart, "tty", argv[1]);
Harald Weltea40c8e52019-09-27 19:22:34 +020070 if (rc < 0) {
71 perror("opening UART");
72 exit(1);
73 }
74
75 rc = get_atr(atr, sizeof(atr));
76 if (rc < 0) {
77 perror("getting ATR");
78 exit(1);
79 }
80 printf("ATR: %s\n", osmo_hexdump(atr, rc));
81
82 test_apdu();
83
84 exit(0);
85}