migrate to libosmousb

the code we used to have in libusb_util.c has been migrated to
libosmousb, a new part of libosmocore.git.  Let's remove our historic
copy and use the new shared library instead.

Change-Id: Ib588c08f873c4da8f3a02815ee2767674a6a5061
This commit is contained in:
Harald Welte 2019-12-15 20:17:44 +01:00
parent 8591495d74
commit 7f7de1ec1d
10 changed files with 17 additions and 429 deletions

View File

@ -58,6 +58,7 @@ AC_SUBST(SYMBOL_VISIBILITY)
PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore >= 1.0.0)
PKG_CHECK_MODULES(LIBOSMOSIM, libosmosim >= 1.0.0)
PKG_CHECK_MODULES(LIBOSMOUSB, libosmousb >= 0.0.0)
PKG_CHECK_MODULES(LIBUSB, libusb-1.0)
AC_ARG_ENABLE(sanitize,

View File

@ -1,6 +1,5 @@
nobase_include_HEADERS = \
osmocom/simtrace2/apdu_dispatch.h \
osmocom/simtrace2/libusb_util.h \
osmocom/simtrace2/simtrace2_api.h \
osmocom/simtrace2/simtrace_usb.h \
osmocom/simtrace2/simtrace_prot.h \

View File

@ -1,73 +0,0 @@
/* libisb utilities
*
* (C) 2010-2016 by Harald Welte <hwelte@hmw-consulting.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#pragma once
#include <libusb.h>
#define USB_MAX_PATH_LEN 20
struct dev_id {
uint16_t vendor_id;
uint16_t product_id;
};
/* Find any USB devices in the system matching the given Vendor and
* Product ID */
libusb_device **find_matching_usb_devs(const struct dev_id *dev_ids);
/* structure describing a single matching interface found */
struct usb_interface_match {
/* libusb device E*/
libusb_device *usb_dev;
/* Vendor ID of the device running matching interface */
uint16_t vendor;
/* Product ID of the device running matching interface */
uint16_t product;
/* USB Bus Address */
uint8_t addr;
/* physical path */
char path[USB_MAX_PATH_LEN];
/* configuration of matching interface */
uint8_t configuration;
/* interface number of matching interface */
uint8_t interface;
/* altsetting of matching interface */
uint8_t altsetting;
/* bInterfaceClass of matching interface */
uint8_t class;
/* bInterfaceSubClass of matching interface */
uint8_t sub_class;
/* bInterfaceProtocol of matching interface */
uint8_t protocol;
/* index of string descriptor of matching interface */
uint8_t string_idx;
};
int dev_find_matching_interfaces(libusb_device *dev, int class, int sub_class, int protocol,
struct usb_interface_match *out, unsigned int out_len);
int usb_match_interfaces(libusb_context *ctx, const struct dev_id *dev_ids,
int class, int sub_class, int protocol,
struct usb_interface_match *out, unsigned int out_len);
libusb_device_handle *usb_open_claim_interface(libusb_context *ctx,
const struct usb_interface_match *ifm);
int get_usb_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
uint8_t *out, uint8_t *in, uint8_t *irq);

View File

@ -15,5 +15,4 @@ libosmo_simtrace2_la_LIBADD = $(COMMONLIBS)
libosmo_simtrace2_la_SOURCES = \
apdu_dispatch.c \
gsmtap.c \
libusb_util.c \
simtrace2_api.c

View File

@ -1,338 +0,0 @@
/* libusb utilities
*
* (C) 2010-2019 by Harald Welte <hwelte@hmw-consulting.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <libusb.h>
#include <osmocom/simtrace2/libusb_util.h>
static char path_buf[USB_MAX_PATH_LEN];
static char *get_path(libusb_device *dev)
{
#if (defined(LIBUSB_API_VERSION) && LIBUSB_API_VERSION >= 0x01000102) || (defined(LIBUSBX_API_VERSION) && LIBUSBX_API_VERSION >= 0x01000102)
uint8_t path[8];
int r,j;
r = libusb_get_port_numbers(dev, path, sizeof(path));
if (r > 0) {
sprintf(path_buf,"%d-%d",libusb_get_bus_number(dev),path[0]);
for (j = 1; j < r; j++){
sprintf(path_buf+strlen(path_buf),".%d",path[j]);
};
}
return path_buf;
#else
# warning "libusb too old - building without USB path support!"
return NULL;
#endif
}
static int match_dev_id(const struct libusb_device_descriptor *desc, const struct dev_id *id)
{
if ((desc->idVendor == id->vendor_id) && (desc->idProduct == id->product_id))
return 1;
return 0;
}
static int match_dev_ids(const struct libusb_device_descriptor *desc, const struct dev_id *ids)
{
const struct dev_id *id;
for (id = ids; id->vendor_id || id->product_id; id++) {
if (match_dev_id(desc, id))
return 1;
}
return 0;
}
libusb_device **find_matching_usb_devs(const struct dev_id *dev_ids)
{
libusb_device **list;
libusb_device **out = calloc(256, sizeof(libusb_device *));
libusb_device **cur = out;
unsigned int i;
int rc;
if (!out)
return NULL;
rc = libusb_get_device_list(NULL, &list);
if (rc <= 0) {
perror("No USB devices found");
free(out);
return NULL;
}
for (i = 0; list[i] != NULL; i++) {
struct libusb_device_descriptor dev_desc;
libusb_device *dev = list[i];
rc = libusb_get_device_descriptor(dev, &dev_desc);
if (rc < 0) {
perror("Couldn't get device descriptor\n");
libusb_unref_device(dev);
continue;
}
if (match_dev_ids(&dev_desc, dev_ids)) {
*cur = dev;
cur++;
/* FIXME: overflow check */
} else
libusb_unref_device(dev);
}
if (cur == out) {
libusb_free_device_list(list, 1);
free(out);
return NULL;
}
libusb_free_device_list(list, 0);
return out;
}
int dev_find_matching_interfaces(libusb_device *dev, int class, int sub_class, int protocol,
struct usb_interface_match *out, unsigned int out_len)
{
struct libusb_device_descriptor dev_desc;
int rc, i, out_idx = 0;
uint8_t addr;
char *path;
rc = libusb_get_device_descriptor(dev, &dev_desc);
if (rc < 0) {
perror("Couldn't get device descriptor\n");
return -EIO;
}
addr = libusb_get_device_address(dev);
path = get_path(dev);
/* iterate over all configurations */
for (i = 0; i < dev_desc.bNumConfigurations; i++) {
struct libusb_config_descriptor *conf_desc;
int j;
rc = libusb_get_config_descriptor(dev, i, &conf_desc);
if (rc < 0) {
fprintf(stderr, "Couldn't get config descriptor %u\n", i);
continue;
}
/* iterate over all interfaces */
for (j = 0; j < conf_desc->bNumInterfaces; j++) {
const struct libusb_interface *intf = &conf_desc->interface[j];
int k;
/* iterate over all alternate settings */
for (k = 0; k < intf->num_altsetting; k++) {
const struct libusb_interface_descriptor *if_desc;
if_desc = &intf->altsetting[k];
if (class >= 0 && if_desc->bInterfaceClass != class)
continue;
if (sub_class >= 0 && if_desc->bInterfaceSubClass != sub_class)
continue;
if (protocol >= 0 && if_desc->bInterfaceProtocol != protocol)
continue;
/* MATCH! */
out[out_idx].usb_dev = dev;
out[out_idx].vendor = dev_desc.idVendor;
out[out_idx].product = dev_desc.idProduct;
out[out_idx].addr = addr;
strncpy(out[out_idx].path, path, sizeof(out[out_idx].path)-1);
out[out_idx].path[sizeof(out[out_idx].path)-1] = '\0';
out[out_idx].configuration = conf_desc->bConfigurationValue;
out[out_idx].interface = if_desc->bInterfaceNumber;
out[out_idx].altsetting = if_desc->bAlternateSetting;
out[out_idx].class = if_desc->bInterfaceClass;
out[out_idx].sub_class = if_desc->bInterfaceSubClass;
out[out_idx].protocol = if_desc->bInterfaceProtocol;
out[out_idx].string_idx = if_desc->iInterface;
out_idx++;
if (out_idx >= out_len)
return out_idx;
}
}
}
return out_idx;
}
int usb_match_interfaces(libusb_context *ctx, const struct dev_id *dev_ids,
int class, int sub_class, int protocol,
struct usb_interface_match *out, unsigned int out_len)
{
struct usb_interface_match *out_cur = out;
unsigned int out_len_remain = out_len;
libusb_device **list;
libusb_device **dev;
list = find_matching_usb_devs(dev_ids);
if (!list)
return 0;
for (dev = list; *dev; dev++) {
int rc;
#if 0
struct libusb_device_descriptor dev_desc;
uint8_t ports[8];
uint8_t addr;
rc = libusb_get_device_descriptor(*dev, &dev_desc);
if (rc < 0) {
perror("Cannot get device descriptor");
continue;
}
addr = libusb_get_device_address(*dev);
rc = libusb_get_port_numbers(*dev, ports, sizeof(ports));
if (rc < 0) {
perror("Cannot get device path");
continue;
}
printf("Found USB Device %04x:%04x at address %d\n",
dev_desc.idVendor, dev_desc.idProduct, addr);
#endif
rc = dev_find_matching_interfaces(*dev, class, sub_class, protocol, out_cur, out_len_remain);
if (rc < 0)
continue;
out_cur += rc;
out_len_remain -= rc;
}
return out_len - out_len_remain;
}
libusb_device_handle *usb_open_claim_interface(libusb_context *ctx,
const struct usb_interface_match *ifm)
{
int rc, config;
struct dev_id dev_ids[] = { { ifm->vendor, ifm->product }, { 0, 0 } };
libusb_device **list;
libusb_device **dev;
libusb_device_handle *usb_devh = NULL;
list = find_matching_usb_devs(dev_ids);
if (!list) {
perror("No USB device with matching VID/PID");
return NULL;
}
for (dev = list; *dev; dev++) {
int addr;
char *path;
addr = libusb_get_device_address(*dev);
path = get_path(*dev);
if ((ifm->addr && addr == ifm->addr) ||
(strlen(ifm->path) && !strcmp(path, ifm->path))) {
rc = libusb_open(*dev, &usb_devh);
if (rc < 0) {
fprintf(stderr, "Cannot open device: %s\n", libusb_error_name(rc));
usb_devh = NULL;
break;
}
rc = libusb_get_configuration(usb_devh, &config);
if (rc < 0) {
fprintf(stderr, "Cannot get current configuration: %s\n", libusb_error_name(rc));
libusb_close(usb_devh);
usb_devh = NULL;
break;
}
if (config != ifm->configuration) {
rc = libusb_set_configuration(usb_devh, ifm->configuration);
if (rc < 0) {
fprintf(stderr, "Cannot set configuration: %s\n", libusb_error_name(rc));
libusb_close(usb_devh);
usb_devh = NULL;
break;
}
}
rc = libusb_claim_interface(usb_devh, ifm->interface);
if (rc < 0) {
fprintf(stderr, "Cannot claim interface: %s\n", libusb_error_name(rc));
libusb_close(usb_devh);
usb_devh = NULL;
break;
}
rc = libusb_set_interface_alt_setting(usb_devh, ifm->interface, ifm->altsetting);
if (rc < 0) {
fprintf(stderr, "Cannot set interface altsetting: %s\n", libusb_error_name(rc));
libusb_release_interface(usb_devh, ifm->interface);
libusb_close(usb_devh);
usb_devh = NULL;
break;
}
}
}
/* unref / free list */
for (dev = list; *dev; dev++)
libusb_unref_device(*dev);
free(list);
return usb_devh;
}
/*! \brief obtain the endpoint addresses for a given USB interface */
int get_usb_ep_addrs(libusb_device_handle *devh, unsigned int if_num,
uint8_t *out, uint8_t *in, uint8_t *irq)
{
libusb_device *dev = libusb_get_device(devh);
struct libusb_config_descriptor *cdesc;
const struct libusb_interface_descriptor *idesc;
const struct libusb_interface *iface;
int rc, l;
rc = libusb_get_active_config_descriptor(dev, &cdesc);
if (rc < 0)
return rc;
iface = &cdesc->interface[if_num];
/* FIXME: we assume there's no altsetting */
idesc = &iface->altsetting[0];
for (l = 0; l < idesc->bNumEndpoints; l++) {
const struct libusb_endpoint_descriptor *edesc = &idesc->endpoint[l];
switch (edesc->bmAttributes & 3) {
case LIBUSB_TRANSFER_TYPE_BULK:
if (edesc->bEndpointAddress & 0x80) {
if (in)
*in = edesc->bEndpointAddress;
} else {
if (out)
*out = edesc->bEndpointAddress;
}
break;
case LIBUSB_TRANSFER_TYPE_INTERRUPT:
if (irq)
*irq = edesc->bEndpointAddress;
break;
default:
break;
}
}
return 0;
}

View File

@ -1,9 +1,9 @@
AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
AM_CFLAGS=-Wall -g $(LIBOSMOCORE_CFLAGS) $(LIBOSMOSIM_CFLAGS) $(LIBUSB_CFLAGS) $(COVERAGE_FLAGS)
AM_CFLAGS=-Wall -g $(LIBOSMOCORE_CFLAGS) $(LIBOSMOSIM_CFLAGS) $(LIBOSMOUSB_CFLAGS) $(LIBUSB_CFLAGS) $(COVERAGE_FLAGS)
AM_LDFLAGS=$(COVERAGE_LDFLAGS)
LDADD= $(top_builddir)/lib/libosmo-simtrace2.la \
$(LIBOSMOCORE_LIBS) $(LIBOSMOSIM_LIBS) $(LIBUSB_LIBS)
$(LIBOSMOCORE_LIBS) $(LIBOSMOSIM_LIBS) $(LIBOSMOUSB_LIBS) $(LIBUSB_LIBS)
bin_PROGRAMS = simtrace2-remsim simtrace2-remsim-usb2udp simtrace2-list simtrace2-sniff

View File

@ -38,7 +38,7 @@
#include <libusb.h>
#include <osmocom/simtrace2/libusb_util.h>
#include <osmocom/usb/libusb.h>
#include <osmocom/simtrace2/simtrace2_api.h>
#include <osmocom/simtrace2/simtrace_prot.h>
#include <osmocom/simtrace2/apdu_dispatch.h>
@ -404,7 +404,7 @@ int main(int argc, char **argv)
ifm->addr = addr;
if (path)
osmo_strlcpy(ifm->path, path, sizeof(ifm->path));
transp->usb_devh = usb_open_claim_interface(NULL, ifm);
transp->usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, ifm);
if (!transp->usb_devh) {
fprintf(stderr, "can't open USB device\n");
goto close_exit;
@ -416,7 +416,7 @@ int main(int argc, char **argv)
goto close_exit;
}
rc = get_usb_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
rc = osmo_libusb_get_ep_addrs(transp->usb_devh, if_num, &transp->usb_ep.out,
&transp->usb_ep.in, &transp->usb_ep.irq_in);
if (rc < 0) {
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);

View File

@ -37,7 +37,7 @@
#include <libusb.h>
#include <osmocom/simtrace2/libusb_util.h>
#include <osmocom/usb/libusb.h>
#include <osmocom/simtrace2/simtrace_usb.h>
#include <osmocom/simtrace2/simtrace_prot.h>
@ -384,7 +384,7 @@ int main(int argc, char **argv)
goto do_exit;
}
struct usb_interface_match ifm_scan[16];
int num_interfaces = usb_match_interfaces(NULL, compatible_dev_ids,
int num_interfaces = osmo_libusb_find_matching_interfaces(NULL, compatible_dev_ids,
USB_CLASS_PROPRIETARY, SIMTRACE_SNIFFER_USB_SUBCLASS, -1, ifm_scan, ARRAY_SIZE(ifm_scan));
if (num_interfaces <= 0) {
perror("No compatible USB devices found");
@ -473,7 +473,7 @@ int main(int argc, char **argv)
signal(SIGINT, &signal_handler);
do {
_transp.usb_devh = usb_open_claim_interface(NULL, &ifm_selected);
_transp.usb_devh = osmo_libusb_open_claim_interface(NULL, NULL, &ifm_selected);
if (!_transp.usb_devh) {
fprintf(stderr, "can't open USB device\n");
goto close_exit;
@ -485,8 +485,8 @@ int main(int argc, char **argv)
goto close_exit;
}
rc = get_usb_ep_addrs(_transp.usb_devh, ifm_selected.interface, &_transp.usb_ep.out,
&_transp.usb_ep.in, &_transp.usb_ep.irq_in);
rc = osmo_libusb_get_ep_addrs(_transp.usb_devh, ifm_selected.interface, &_transp.usb_ep.out,
&_transp.usb_ep.in, &_transp.usb_ep.irq_in);
if (rc < 0) {
fprintf(stderr, "can't obtain EP addrs; rc=%d\n", rc);
goto close_exit;

View File

@ -23,7 +23,7 @@
#include <osmocom/core/utils.h>
#include <osmocom/simtrace2/libusb_util.h>
#include <osmocom/usb/libusb.h>
#include <osmocom/simtrace2/simtrace_usb.h>
static const struct dev_id compatible_dev_ids[] = {
@ -38,9 +38,9 @@ static int find_devices(void)
struct usb_interface_match ifm[16];
int rc, i, num_interfaces;
/* scan for USB devices matching SIMtrace USB ID with proprietary class */
rc = usb_match_interfaces(NULL, compatible_dev_ids,
USB_CLASS_PROPRIETARY, -1, -1, ifm, ARRAY_SIZE(ifm));
/* scan for USB devices matching SIMtrace USB ID with proprietary class */
rc = osmo_libusb_find_matching_interfaces(NULL, compatible_dev_ids,
USB_CLASS_PROPRIETARY, -1, -1, ifm, ARRAY_SIZE(ifm));
printf("USB matches: %d\n", rc);
if (rc < 0)
return rc;

View File

@ -35,9 +35,9 @@
#include <libusb.h>
#include <osmocom/usb/libusb.h>
#include <osmocom/simtrace2/simtrace_usb.h>
#include <osmocom/simtrace2/simtrace_prot.h>
#include <osmocom/simtrace2/libusb_util.h>
#include <osmocom/core/utils.h>
#include <osmocom/core/socket.h>
@ -259,7 +259,7 @@ int main(int argc, char **argv)
g_udp_ofd.cb = ofd_udp_cb;
osmo_sock_init_ofd(&g_udp_ofd, AF_INET, SOCK_DGRAM, IPPROTO_UDP, NULL, local_udp_port + if_num, OSMO_SOCK_F_BIND);
rc = get_usb_ep_addrs(g_devh, if_num, &g_buf_out.ep, &g_buf_in.ep, NULL);
rc = osmo_libusb_get_ep_addrs(g_devh, if_num, &g_buf_out.ep, &g_buf_in.ep, NULL);
if (rc < 0) {
fprintf(stderr, "couldn't find enpdoint addresses; rc=%d\n", rc);
goto close_exit;