mirror of
https://gitea.osmocom.org/sim-card/simtrace.git
synced 2026-03-16 21:28:36 +03:00
Use libusb-1.0 for USB communication
This commit is contained in:
committed by
Harald Welte
parent
7b76e0ce7c
commit
18ad51fe76
55
host/main.c
55
host/main.c
@@ -32,9 +32,8 @@
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#include <usb.h>
|
||||
#include <libusb.h>
|
||||
|
||||
#include "usb_helper.h"
|
||||
#include "simtrace.h"
|
||||
#include "simtrace_usb.h"
|
||||
#include "apdu_split.h"
|
||||
@@ -43,7 +42,7 @@
|
||||
#include <osmocom/core/gsmtap_util.h>
|
||||
#include <osmocom/core/utils.h>
|
||||
|
||||
static struct usb_dev_handle *udev;
|
||||
static struct libusb_device_handle *devh;
|
||||
static struct apdu_split *as;
|
||||
static struct gsmtap_inst *g_gti;
|
||||
|
||||
@@ -140,8 +139,9 @@ int main(int argc, char **argv)
|
||||
{
|
||||
char buf[16*265];
|
||||
char *gsmtap_host = "127.0.0.1";
|
||||
int rc, c;
|
||||
int rc, c, ret = 1;
|
||||
int skip_atr = 0;
|
||||
int xfer_len;
|
||||
unsigned int msg_count, byte_count = 0;
|
||||
|
||||
print_welcome();
|
||||
@@ -166,35 +166,56 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
rc = libusb_init(NULL);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "libusb initialization failed\n");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
g_gti = gsmtap_source_init(gsmtap_host, GSMTAP_UDP_PORT, 0);
|
||||
if (!g_gti) {
|
||||
perror("unable to open GSMTAP");
|
||||
exit(1);
|
||||
goto close_exit;
|
||||
}
|
||||
gsmtap_source_add_sink(g_gti);
|
||||
|
||||
udev = usb_find_open(SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT);
|
||||
if (!udev) {
|
||||
perror("opening USB device");
|
||||
exit(1);
|
||||
devh = libusb_open_device_with_vid_pid(NULL, SIMTRACE_USB_VENDOR, SIMTRACE_USB_PRODUCT);
|
||||
if (!devh) {
|
||||
fprintf(stderr, "can't open USB device\n");
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
rc = libusb_claim_interface(devh, 0);
|
||||
if (rc < 0) {
|
||||
fprintf(stderr, "can't claim interface; rc=%d\n", rc);
|
||||
goto close_exit;
|
||||
}
|
||||
|
||||
as = apdu_split_init(&apdu_out_cb, NULL);
|
||||
if (!as)
|
||||
exit(1);
|
||||
goto release_exit;
|
||||
|
||||
printf("Entering main loop\n");
|
||||
while (1) {
|
||||
rc = usb_bulk_read(udev, SIMTRACE_IN_EP, buf, sizeof(buf), 100000);
|
||||
if (rc < 0 && rc != -EAGAIN) {
|
||||
fprintf(stderr, "Error submitting BULK IN urb: %s\n", usb_strerror());
|
||||
exit(1);
|
||||
rc = libusb_bulk_transfer(devh, SIMTRACE_IN_EP, buf, sizeof(buf), &xfer_len, 100000);
|
||||
if (rc < 0 && rc != LIBUSB_ERROR_TIMEOUT) {
|
||||
fprintf(stderr, "BULK IN transfer error; rc=%d\n", rc);
|
||||
goto release_exit;
|
||||
}
|
||||
if (rc > 0) {
|
||||
if (xfer_len > 0) {
|
||||
//printf("URB: %s\n", osmo_hexdump(buf, rc));
|
||||
process_usb_msg(buf, rc);
|
||||
process_usb_msg(buf, xfer_len);
|
||||
msg_count++;
|
||||
byte_count += rc;
|
||||
byte_count += xfer_len;
|
||||
}
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
release_exit:
|
||||
libusb_release_interface(devh, 0);
|
||||
close_exit:
|
||||
if (devh)
|
||||
libusb_close(devh);
|
||||
libusb_exit(NULL);
|
||||
return ret;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user