blob: 23800fd187df2c88f3e4369b991f4ced79cfc717 [file] [log] [blame]
Harald Welte095ac6c2016-03-19 13:39:33 +01001/* simtrace - main program for the host PC
2 *
3 * (C) 2010-2016 by Harald Welte <hwelte@hmw-consulting.de>
4 *
Kévin Redonefbcf382018-07-07 17:35:15 +02005 * 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 Welte095ac6c2016-03-19 13:39:33 +01009 *
Kévin Redonefbcf382018-07-07 17:35:15 +020010 * 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 Welte095ac6c2016-03-19 13:39:33 +010014 *
Kévin Redonefbcf382018-07-07 17:35:15 +020015 * 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 Welte095ac6c2016-03-19 13:39:33 +010018 */
Harald Welte095ac6c2016-03-19 13:39:33 +010019#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 Welte964cda32019-11-24 22:27:10 +010038#include <osmocom/simtrace2/simtrace_usb.h>
39#include <osmocom/simtrace2/simtrace_prot.h>
Harald Welte6f413492019-11-24 22:51:07 +010040#include <osmocom/simtrace2/libusb_util.h>
Harald Welte095ac6c2016-03-19 13:39:33 +010041
42#include <osmocom/core/utils.h>
43#include <osmocom/core/socket.h>
44#include <osmocom/core/select.h>
45
46struct libusb_device_handle *g_devh;
47static struct sockaddr_in g_sa_remote;
48static struct osmo_fd g_udp_ofd;
49
50static 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
56static void print_help(void)
57{
58 printf( "\t-h\t--help\n"
Harald Welte236caf62016-03-19 21:28:09 +010059 "\t-i\t--interface <0-255>\n"
Harald Welte095ac6c2016-03-19 13:39:33 +010060 "\n"
61 );
62}
63
64struct ep_buf {
65 uint8_t ep;
66 uint8_t buf[1024];
67 struct libusb_transfer *xfer;
68};
69static struct ep_buf g_buf_in;
70static struct ep_buf g_buf_out;
71
72static 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 Welte236caf62016-03-19 21:28:09 +010080 if (xfer->endpoint == g_buf_in.ep) {
Harald Welte095ac6c2016-03-19 13:39:33 +010081 /* 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 Welte236caf62016-03-19 21:28:09 +010089 } else if (xfer->endpoint == g_buf_out.ep) {
Harald Welte095ac6c2016-03-19 13:39:33 +010090 /* re-enable reading from the UDP side */
91 g_udp_ofd.when |= BSC_FD_READ;
Harald Welte095ac6c2016-03-19 13:39:33 +010092 }
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 Welte236caf62016-03-19 21:28:09 +0100100static void init_ep_buf(struct ep_buf *epb)
Harald Welte095ac6c2016-03-19 13:39:33 +0100101{
Harald Welte095ac6c2016-03-19 13:39:33 +0100102 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
114static int g_libusb_pending = 0;
115
116static 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 */
125static 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 */
142static void libusb_fd_removed_cb(int fd, void *user_data)
143{
Harald Welte095ac6c2016-03-19 13:39:33 +0100144
145 printf("%s(%u)\n", __func__, fd);
146#if 0
Harald Welte9f38ddd2017-05-10 22:49:02 +0200147 struct osmo_fd *ofd;
Harald Welte095ac6c2016-03-19 13:39:33 +0100148 /* 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 */
159static int ofd_udp_cb(struct osmo_fd *ofd, unsigned int what)
160{
161 int rc;
Harald Welte9f38ddd2017-05-10 22:49:02 +0200162 socklen_t addrlen = sizeof(g_sa_remote);
Harald Welte095ac6c2016-03-19 13:39:33 +0100163
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
182static 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
202int main(int argc, char **argv)
203{
204 int rc;
205 int c, ret = 1;
Harald Welte095ac6c2016-03-19 13:39:33 +0100206 int local_udp_port = 52342;
Harald Welte236caf62016-03-19 21:28:09 +0100207 unsigned int if_num = 0;
Harald Welte095ac6c2016-03-19 13:39:33 +0100208
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 Welte236caf62016-03-19 21:28:09 +0100215 { "interface", 1, 0, 'I' },
Harald Welte095ac6c2016-03-19 13:39:33 +0100216 { "help", 0, 0, 'h' },
217 { NULL, 0, 0, 0 }
218 };
219
Harald Welte236caf62016-03-19 21:28:09 +0100220 c = getopt_long(argc, argv, "u:I:h", opts, &option_index);
Harald Welte095ac6c2016-03-19 13:39:33 +0100221 if (c == -1)
222 break;
223 switch (c) {
224 case 'u':
225 local_udp_port = atoi(optarg);
226 break;
Harald Welte236caf62016-03-19 21:28:09 +0100227 case 'I':
228 if_num = atoi(optarg);
229 break;
Harald Welte095ac6c2016-03-19 13:39:33 +0100230 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 Redon5b5d24e2018-09-08 08:22:56 +0200245 g_devh = libusb_open_device_with_vid_pid(NULL, USB_VENDOR_OPENMOKO, USB_PRODUCT_OWHW_SAM3);
Harald Welte095ac6c2016-03-19 13:39:33 +0100246 if (!g_devh) {
247 fprintf(stderr, "can't open USB device\n");
248 goto close_exit;
249 }
250
Harald Welte236caf62016-03-19 21:28:09 +0100251 rc = libusb_claim_interface(g_devh, if_num);
Harald Welte095ac6c2016-03-19 13:39:33 +0100252 if (rc < 0) {
Harald Welte236caf62016-03-19 21:28:09 +0100253 fprintf(stderr, "can't claim interface %u; rc=%d\n", if_num, rc);
Harald Welte095ac6c2016-03-19 13:39:33 +0100254 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 Welte236caf62016-03-19 21:28:09 +0100260 osmo_sock_init_ofd(&g_udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, local_udp_port + if_num, OSMO_SOCK_F_BIND);
Harald Welte095ac6c2016-03-19 13:39:33 +0100261
Harald Welte236caf62016-03-19 21:28:09 +0100262 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 Welte095ac6c2016-03-19 13:39:33 +0100267 /* initialize USB buffers / transfers */
Harald Welte236caf62016-03-19 21:28:09 +0100268 init_ep_buf(&g_buf_out);
269 init_ep_buf(&g_buf_in);
Harald Welte095ac6c2016-03-19 13:39:33 +0100270
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);
279close_exit:
280 if (g_devh)
281 libusb_close(g_devh);
282
Harald Welte095ac6c2016-03-19 13:39:33 +0100283 libusb_exit(NULL);
284 return ret;
285}