Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 1 | /* simtrace - main program for the host PC |
| 2 | * |
| 3 | * (C) 2010-2016 by Harald Welte <hwelte@hmw-consulting.de> |
| 4 | * |
Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 9 | * |
Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 14 | * |
Kévin Redon | efbcf38 | 2018-07-07 17:35:15 +0200 | [diff] [blame] | 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 18 | */ |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <unistd.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdint.h> |
| 25 | #include <time.h> |
| 26 | #define _GNU_SOURCE |
| 27 | #include <getopt.h> |
| 28 | #include <poll.h> |
| 29 | |
| 30 | #include <sys/time.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <netinet/in.h> |
| 34 | #include <arpa/inet.h> |
| 35 | |
| 36 | #include <libusb.h> |
| 37 | |
Harald Welte | 964cda3 | 2019-11-24 22:27:10 +0100 | [diff] [blame] | 38 | #include <osmocom/simtrace2/simtrace_usb.h> |
| 39 | #include <osmocom/simtrace2/simtrace_prot.h> |
Harald Welte | 6f41349 | 2019-11-24 22:51:07 +0100 | [diff] [blame] | 40 | #include <osmocom/simtrace2/libusb_util.h> |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 41 | |
| 42 | #include <osmocom/core/utils.h> |
| 43 | #include <osmocom/core/socket.h> |
| 44 | #include <osmocom/core/select.h> |
| 45 | |
| 46 | struct libusb_device_handle *g_devh; |
| 47 | static struct sockaddr_in g_sa_remote; |
| 48 | static struct osmo_fd g_udp_ofd; |
| 49 | |
| 50 | static void print_welcome(void) |
| 51 | { |
| 52 | printf("usb2udp - UDP/IP forwarding of SIMtrace card emulation\n" |
| 53 | "(C) 2016 by Harald Welte <laforge@gnumonks.org>\n\n"); |
| 54 | } |
| 55 | |
| 56 | static void print_help(void) |
| 57 | { |
| 58 | printf( "\t-h\t--help\n" |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 59 | "\t-i\t--interface <0-255>\n" |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 60 | "\n" |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | struct ep_buf { |
| 65 | uint8_t ep; |
| 66 | uint8_t buf[1024]; |
| 67 | struct libusb_transfer *xfer; |
| 68 | }; |
| 69 | static struct ep_buf g_buf_in; |
| 70 | static struct ep_buf g_buf_out; |
| 71 | |
| 72 | static void usb_in_xfer_cb(struct libusb_transfer *xfer) |
| 73 | { |
| 74 | int rc; |
| 75 | |
| 76 | printf("xfer_cb(ep=%02x): status=%d, flags=0x%x, type=%u, len=%u, act_len=%u\n", |
| 77 | xfer->endpoint, xfer->status, xfer->flags, xfer->type, xfer->length, xfer->actual_length); |
| 78 | switch (xfer->status) { |
| 79 | case LIBUSB_TRANSFER_COMPLETED: |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 80 | if (xfer->endpoint == g_buf_in.ep) { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 81 | /* process the data */ |
| 82 | printf("read %d bytes from SIMTRACE, forwarding to UDP\n", xfer->actual_length); |
| 83 | rc = sendto(g_udp_ofd.fd, xfer->buffer, xfer->actual_length, 0, (struct sockaddr *)&g_sa_remote, sizeof(g_sa_remote)); |
| 84 | if (rc <= 0) { |
| 85 | fprintf(stderr, "error writing to UDP\n"); |
| 86 | } |
| 87 | /* and re-submit the URB */ |
| 88 | libusb_submit_transfer(xfer); |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 89 | } else if (xfer->endpoint == g_buf_out.ep) { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 90 | /* re-enable reading from the UDP side */ |
| 91 | g_udp_ofd.when |= BSC_FD_READ; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 92 | } |
| 93 | break; |
| 94 | default: |
| 95 | fprintf(stderr, "xfer_cb(ERROR '%s')\n", osmo_hexdump_nospc(xfer->buffer, xfer->actual_length)); |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 100 | static void init_ep_buf(struct ep_buf *epb) |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 101 | { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 102 | if (!epb->xfer) |
| 103 | epb->xfer = libusb_alloc_transfer(0); |
| 104 | |
| 105 | epb->xfer->flags = 0; |
| 106 | |
| 107 | libusb_fill_bulk_transfer(epb->xfer, g_devh, epb->ep, epb->buf, sizeof(epb->buf), usb_in_xfer_cb, NULL, 0); |
| 108 | } |
| 109 | |
| 110 | /*********************************************************************** |
| 111 | * libosmocore main loop integration of libusb async I/O |
| 112 | ***********************************************************************/ |
| 113 | |
| 114 | static int g_libusb_pending = 0; |
| 115 | |
| 116 | static int ofd_libusb_cb(struct osmo_fd *ofd, unsigned int what) |
| 117 | { |
| 118 | /* FIXME */ |
| 119 | g_libusb_pending = 1; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* call-back when libusb adds a FD */ |
| 125 | static void libusb_fd_added_cb(int fd, short events, void *user_data) |
| 126 | { |
| 127 | struct osmo_fd *ofd = talloc_zero(NULL, struct osmo_fd); |
| 128 | |
| 129 | printf("%s(%u, %x)\n", __func__, fd, events); |
| 130 | |
| 131 | ofd->fd = fd; |
| 132 | ofd->cb = &ofd_libusb_cb; |
| 133 | if (events & POLLIN) |
| 134 | ofd->when |= BSC_FD_READ; |
| 135 | if (events & POLLOUT) |
| 136 | ofd->when |= BSC_FD_WRITE; |
| 137 | |
| 138 | osmo_fd_register(ofd); |
| 139 | } |
| 140 | |
| 141 | /* call-back when libusb removes a FD */ |
| 142 | static void libusb_fd_removed_cb(int fd, void *user_data) |
| 143 | { |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 144 | |
| 145 | printf("%s(%u)\n", __func__, fd); |
| 146 | #if 0 |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 147 | struct osmo_fd *ofd; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 148 | /* FIXME: This needs new export in libosmocore! */ |
| 149 | ofd = osmo_fd_get_by_fd(fd); |
| 150 | |
| 151 | if (ofd) { |
| 152 | osmo_fd_unregister(ofd); |
| 153 | talloc_free(ofd); |
| 154 | } |
| 155 | #endif |
| 156 | } |
| 157 | |
| 158 | /* call-back when the UDP socket is readable */ |
| 159 | static int ofd_udp_cb(struct osmo_fd *ofd, unsigned int what) |
| 160 | { |
| 161 | int rc; |
Harald Welte | 9f38ddd | 2017-05-10 22:49:02 +0200 | [diff] [blame] | 162 | socklen_t addrlen = sizeof(g_sa_remote); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 163 | |
| 164 | rc = recvfrom(ofd->fd, g_buf_out.buf, sizeof(g_buf_out.buf), 0, |
| 165 | (struct sockaddr *)&g_sa_remote, &addrlen); |
| 166 | if (rc <= 0) { |
| 167 | fprintf(stderr, "error reading from UDP\n"); |
| 168 | return 0; |
| 169 | } |
| 170 | printf("read %d bytes from UDP, forwarding to SIMTRACE\n", rc); |
| 171 | g_buf_out.xfer->length = rc; |
| 172 | |
| 173 | /* disable further READ interest for the UDP socket */ |
| 174 | ofd->when &= ~BSC_FD_READ; |
| 175 | |
| 176 | /* submit the URB on the OUT end point */ |
| 177 | libusb_submit_transfer(g_buf_out.xfer); |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | static void run_mainloop(void) |
| 183 | { |
| 184 | int rc; |
| 185 | |
| 186 | printf("Entering main loop\n"); |
| 187 | |
| 188 | while (1) { |
| 189 | osmo_select_main(0); |
| 190 | if (g_libusb_pending) { |
| 191 | struct timeval tv; |
| 192 | memset(&tv, 0, sizeof(tv)); |
| 193 | rc = libusb_handle_events_timeout_completed(NULL, &tv, NULL); |
| 194 | if (rc != 0) { |
| 195 | fprintf(stderr, "handle_events_timeout_completed == %d\n", rc); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | int main(int argc, char **argv) |
| 203 | { |
| 204 | int rc; |
| 205 | int c, ret = 1; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 206 | int local_udp_port = 52342; |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 207 | unsigned int if_num = 0; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 208 | |
| 209 | print_welcome(); |
| 210 | |
| 211 | while (1) { |
| 212 | int option_index = 0; |
| 213 | static const struct option opts[] = { |
| 214 | { "udp-port", 1, 0, 'u' }, |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 215 | { "interface", 1, 0, 'I' }, |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 216 | { "help", 0, 0, 'h' }, |
| 217 | { NULL, 0, 0, 0 } |
| 218 | }; |
| 219 | |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 220 | c = getopt_long(argc, argv, "u:I:h", opts, &option_index); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 221 | if (c == -1) |
| 222 | break; |
| 223 | switch (c) { |
| 224 | case 'u': |
| 225 | local_udp_port = atoi(optarg); |
| 226 | break; |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 227 | case 'I': |
| 228 | if_num = atoi(optarg); |
| 229 | break; |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 230 | case 'h': |
| 231 | print_help(); |
| 232 | exit(0); |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | rc = libusb_init(NULL); |
| 238 | if (rc < 0) { |
| 239 | fprintf(stderr, "libusb initialization failed\n"); |
| 240 | goto close_exit; |
| 241 | } |
| 242 | |
| 243 | libusb_set_pollfd_notifiers(NULL, &libusb_fd_added_cb, &libusb_fd_removed_cb, NULL); |
| 244 | |
Kévin Redon | 5b5d24e | 2018-09-08 08:22:56 +0200 | [diff] [blame] | 245 | g_devh = libusb_open_device_with_vid_pid(NULL, USB_VENDOR_OPENMOKO, USB_PRODUCT_OWHW_SAM3); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 246 | if (!g_devh) { |
| 247 | fprintf(stderr, "can't open USB device\n"); |
| 248 | goto close_exit; |
| 249 | } |
| 250 | |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 251 | rc = libusb_claim_interface(g_devh, if_num); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 252 | if (rc < 0) { |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 253 | fprintf(stderr, "can't claim interface %u; rc=%d\n", if_num, rc); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 254 | goto close_exit; |
| 255 | } |
| 256 | |
| 257 | /* open UDP socket, register with select handling and mark it |
| 258 | * readable */ |
| 259 | g_udp_ofd.cb = ofd_udp_cb; |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 260 | osmo_sock_init_ofd(&g_udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, local_udp_port + if_num, OSMO_SOCK_F_BIND); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 261 | |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 262 | rc = get_usb_ep_addrs(g_devh, if_num, &g_buf_out.ep, &g_buf_in.ep, NULL); |
| 263 | if (rc < 0) { |
| 264 | fprintf(stderr, "couldn't find enpdoint addresses; rc=%d\n", rc); |
| 265 | goto close_exit; |
| 266 | } |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 267 | /* initialize USB buffers / transfers */ |
Harald Welte | 236caf6 | 2016-03-19 21:28:09 +0100 | [diff] [blame] | 268 | init_ep_buf(&g_buf_out); |
| 269 | init_ep_buf(&g_buf_in); |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 270 | |
| 271 | /* submit the first transfer for the IN endpoint */ |
| 272 | libusb_submit_transfer(g_buf_in.xfer); |
| 273 | |
| 274 | run_mainloop(); |
| 275 | |
| 276 | ret = 0; |
| 277 | |
| 278 | libusb_release_interface(g_devh, 0); |
| 279 | close_exit: |
| 280 | if (g_devh) |
| 281 | libusb_close(g_devh); |
| 282 | |
Harald Welte | 095ac6c | 2016-03-19 13:39:33 +0100 | [diff] [blame] | 283 | libusb_exit(NULL); |
| 284 | return ret; |
| 285 | } |