Change in libosmocore[master]: libosmocore libusb integration

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

laforge gerrit-no-reply at lists.osmocom.org
Tue Dec 17 01:01:02 UTC 2019


laforge has submitted this change. ( https://gerrit.osmocom.org/c/libosmocore/+/16606 )

Change subject: libosmocore libusb integration
......................................................................

libosmocore libusb integration

Osmocom applications typically use libosmocore select.[ch] event loop
code as their main event dispatch mechanism.  When they want to deal
with libusb in a non-blocking/asynchronous way, they need to integrate
libusb into that select().

The new libosmousb is doing exactly that: Providing a shared utility
library for Osmocom programs that wish to use libusb.  This is useful
for example in simtrace2 host utilitie as well as osmo-e1d.

Change-Id: I656a1a38cbb5b1f3a9145d2869d3b4d0adefcae3
Closes: OS#4299
---
M Makefile.am
M configure.ac
M contrib/jenkins_arm.sh
M debian/control
A debian/libosmousb0.install
M include/Makefile.am
A include/osmocom/usb/libusb.h
A libosmousb.pc.in
A src/usb/Makefile.am
A src/usb/osmo_libusb.c
10 files changed, 218 insertions(+), 2 deletions(-)

Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved



diff --git a/Makefile.am b/Makefile.am
index 7b6ba0a..7390f98 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,11 +1,12 @@
 ACLOCAL_AMFLAGS = -I m4
 
 AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include
-SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim src/pseudotalloc utils tests
+SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim src/pseudotalloc src/usb utils tests
 
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = libosmocore.pc libosmocodec.pc libosmovty.pc libosmogsm.pc \
-		 libosmogb.pc libosmoctrl.pc libosmocoding.pc libosmosim.pc
+		 libosmogb.pc libosmoctrl.pc libosmocoding.pc libosmosim.pc \
+		 libosmousb.pc
 
 aclocaldir = $(datadir)/aclocal
 dist_aclocal_DATA = m4/osmo_ac_code_coverage.m4 \
diff --git a/configure.ac b/configure.ac
index 060d081..e45ec9f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -146,6 +146,19 @@
 AM_CONDITIONAL(ENABLE_PCSC, test "x$ENABLE_PCSC" = "xyes")
 AC_SUBST(ENABLE_PCSC)
 
+AC_ARG_ENABLE([libusb], [AS_HELP_STRING([--disable-libusb], [Build without libusb support])],
+    [
+        ENABLE_LIBUSB=$enableval
+    ],
+    [
+        ENABLE_LIBUSB="yes"
+    ])
+AS_IF([test "x$ENABLE_LIBUSB" = "xyes"], [
+	PKG_CHECK_MODULES(LIBUSB, libusb-1.0)
+])
+AM_CONDITIONAL(ENABLE_LIBUSB, test "x$ENABLE_LIBUSB" = "xyes")
+AC_SUBST(ENABLE_LIBUSB)
+
 AC_ARG_ENABLE([gnutls], [AS_HELP_STRING([--disable-gnutls], [Do not use GnuTLS fallback for missing getrandom()])],
 	[ENABLE_GNUTLS=$enableval], [ENABLE_GNUTLS="yes"])
 AM_CONDITIONAL(ENABLE_GNUTLS, test x"$ENABLE_GNUTLS" = x"yes")
@@ -398,12 +411,14 @@
 	libosmogb.pc
 	libosmoctrl.pc
 	libosmosim.pc
+	libosmousb.pc
 	include/Makefile
 	src/Makefile
 	src/vty/Makefile
 	src/codec/Makefile
 	src/coding/Makefile
 	src/sim/Makefile
+	src/usb/Makefile
 	src/gsm/Makefile
 	src/gb/Makefile
 	src/ctrl/Makefile
diff --git a/contrib/jenkins_arm.sh b/contrib/jenkins_arm.sh
index 8ee90f0..c9cd922 100755
--- a/contrib/jenkins_arm.sh
+++ b/contrib/jenkins_arm.sh
@@ -19,6 +19,7 @@
 	--disable-doxygen \
 	--disable-shared \
 	--disable-libsctp \
+	--disable-libusb \
 	--enable-external-tests \
 	CFLAGS="-Os -ffunction-sections -fdata-sections -nostartfiles -nodefaultlibs $WERROR_FLAGS"
 
diff --git a/debian/control b/debian/control
index cf7a853..d2c232f 100644
--- a/debian/control
+++ b/debian/control
@@ -34,6 +34,7 @@
          libosmovty4 (= ${binary:Version}),
          libosmoctrl0 (= ${binary:Version}),
          libosmosim0 (= ${binary:Version}),
+         libosmousb0 (= ${binary:Version}),
          ${misc:Depends}
 Description: Open Source MObile COMmunications CORE library (metapackage)
  The libraries provided by this package contain various utility functions.
@@ -293,6 +294,23 @@
  .
  The libosmosim library in particular contains routines for SIM card access.
 
+Package: libosmousb0
+Section: libs
+Architecture: any
+Multi-Arch: same
+Depends: ${shlibs:Depends},
+         ${misc:Depends}
+Pre-Depends: ${misc:Pre-Depends}
+Description: Osmo USB library
+ This is part of the libosmocore "meta"-library. The libosmocore library
+ contains various utility functions that were originally developed as part of
+ the OpenBSC project, but which are of a more generic nature and thus useful to
+ (at least) other programs that are developed in the sphere of Free Software /
+ Open Source mobile communication.
+ .
+ The libosmosub library in particular contains routines for USB device access
+ via libusb-1.0, integrated into the libosmocore select event loop.
+
 Package: libosmocore-dev
 Architecture: any
 Multi-Arch: same
diff --git a/debian/libosmousb0.install b/debian/libosmousb0.install
new file mode 100644
index 0000000..4031b9f
--- /dev/null
+++ b/debian/libosmousb0.install
@@ -0,0 +1 @@
+usr/lib/*/libosmousb*.so.*
diff --git a/include/Makefile.am b/include/Makefile.am
index d76addd..dc6eaa7 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -164,6 +164,11 @@
                           osmocom/ctrl/control_vty.h
 endif
 
+if ENABLE_LIBUSB
+nobase_include_HEADERS += \
+			  osmocom/usb/libusb.h
+endif
+
 noinst_HEADERS = \
 	osmocom/gsm/kasumi.h \
 	osmocom/gsm/gea.h \
diff --git a/include/osmocom/usb/libusb.h b/include/osmocom/usb/libusb.h
new file mode 100644
index 0000000..7f10f74
--- /dev/null
+++ b/include/osmocom/usb/libusb.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <libusb.h>
+
+int osmo_libusb_init(libusb_context **luctx);
+void osmo_libusb_exit(libusb_context *luctx);
diff --git a/libosmousb.pc.in b/libosmousb.pc.in
new file mode 100644
index 0000000..ce6d271
--- /dev/null
+++ b/libosmousb.pc.in
@@ -0,0 +1,11 @@
+prefix=@prefix@
+exec_prefix=@exec_prefix@
+libdir=@libdir@
+includedir=@includedir@
+
+Name: Osmocom libusb (USB) integration
+Description: C Utility Library
+Version: @VERSION@
+Libs: -L${libdir} @TALLOC_LIBS@ -losmousb -losmocore
+Cflags: -I${includedir}/
+
diff --git a/src/usb/Makefile.am b/src/usb/Makefile.am
new file mode 100644
index 0000000..bca39bf
--- /dev/null
+++ b/src/usb/Makefile.am
@@ -0,0 +1,21 @@
+# This is _NOT_ the library release version, it's an API version.
+# Please read chapter "Library interface versions" of the libtool documentation
+# before making any modifications: https://www.gnu.org/software/libtool/manual/html_node/Versioning.html
+LIBVERSION=0:0:0
+
+AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/include
+AM_CFLAGS = -fPIC -Wall $(LIBUSB_CFLAGS) $(TALLOC_CFLAGS)
+AM_LDFLAGS = $(COVERAGE_LDFLAGS)
+
+if ENABLE_LIBUSB
+
+lib_LTLIBRARIES = libosmousb.la
+
+libosmousb_la_SOURCES = osmo_libusb.c
+libosmosim_la_LDFLAGS = -version-info $(LIBVERSION)
+libosmosim_la_LIBADD = \
+	$(top_builddir)/src/libosmocore.la \
+	$(TALLOC_LIBS) \
+	$(LIBUSB_LIBS)
+
+endif
diff --git a/src/usb/osmo_libusb.c b/src/usb/osmo_libusb.c
new file mode 100644
index 0000000..fda9573
--- /dev/null
+++ b/src/usb/osmo_libusb.c
@@ -0,0 +1,137 @@
+/* libosmocore integration with libusb-1.0
+ *
+ * (C) 2019 by Harald Welte <laforge at gnumonks.org>
+ * All Rights Reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0+
+ *
+ * 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 <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+#include <poll.h>
+
+#include <osmocom/core/utils.h>
+#include <osmocom/core/logging.h>
+#include <osmocom/core/select.h>
+#include <osmocom/core/talloc.h>
+
+#include <libusb.h>
+
+/***********************************************************************
+ * logging integration
+ ***********************************************************************/
+
+#define DLUSB	DLINP
+
+#ifdef LIBUSB_LOG_CB_CONTEXT /* introduced in 1.0.23 */
+static const int usb2logl[] = {
+	[LIBUSB_LOG_LEVEL_NONE] = LOGL_FATAL,
+	[LIBUSB_LOG_LEVEL_ERROR] = LOGL_ERROR,
+	[LIBUSB_LOG_LEVEL_WARNING] = LOGL_NOTICE,
+	[LIBUSB_LOG_LEVEL_INFO] = LOGL_INFO,
+	[LIBUSB_LOG_LEVEL_DEBUG] = LOGL_DEBUG,
+};
+
+/* called by libusb if it wants to log something */
+static void libosmo_usb_log_cb(libusb_context *luctx, enum libusb_log_level level_usb, const char *str)
+{
+	int level = LOGL_NOTICE;
+
+	if (level_usb < ARRAY_SIZE(usb2logl))
+		level = usb2logl[level_usb];
+
+	LOGP(DLUSB, level, "%s", str);
+}
+#endif /* LIBUSB_LOG_CB_CONTEXT */
+
+/***********************************************************************
+ * select loop integration
+ ***********************************************************************/
+
+static int osmo_usb_fd_cb(struct osmo_fd *ofd, unsigned int what)
+{
+	libusb_context *luctx = ofd->data;
+
+	/* we assume that we're running Linux v2.6.27 with timerfd support here
+	 * and hence don't have to perform manual timeout handling.  See
+	 * "Notes on time-based events" at
+	 * http://libusb.sourceforge.net/api-1.0/group__libusb__poll.html */
+	struct timeval zero_tv = { 0, 0 };
+	libusb_handle_events_timeout(luctx, &zero_tv);
+
+	return 0;
+}
+
+/* called by libusb if it wants to add a file-descriptor */
+static void osmo_usb_added_cb(int fd, short events, void *user_data)
+{
+	struct osmo_fd *ofd = talloc_zero(OTC_GLOBAL, struct osmo_fd);
+	libusb_context *luctx = user_data;
+	unsigned int when = 0;
+
+	if (events & POLLIN)
+		when |= OSMO_FD_READ;
+	if (events & POLLOUT)
+		when |= OSMO_FD_WRITE;
+
+	osmo_fd_setup(ofd, fd, when, osmo_usb_fd_cb, luctx, 0);
+	osmo_fd_register(ofd);
+}
+
+/* called by libusb if it wants to remove a file-descriptor */
+static void osmo_usb_removed_cb(int fd, void *user_data)
+{
+	struct osmo_fd *ofd = osmo_fd_get_by_fd(fd);
+	if (!fd)
+		return;
+	osmo_fd_unregister(ofd);
+	talloc_free(ofd);
+}
+
+
+/***********************************************************************
+ * initialization
+ ***********************************************************************/
+
+int osmo_libusb_init(libusb_context **pluctx)
+{
+	libusb_context *luctx = NULL;
+	int rc;
+
+	rc = libusb_init(pluctx);
+	if (rc != 0)
+		return rc;
+
+	if (pluctx)
+		luctx = *pluctx;
+
+#ifdef LIBUSB_LOG_CB_CONTEXT /* introduced in 1.0.23 */
+	libusb_set_log_cb(luctx, &libosmo_usb_log_cb, LIBUSB_LOG_CB_CONTEXT);
+#endif
+
+	libusb_set_pollfd_notifiers(luctx, osmo_usb_added_cb, osmo_usb_removed_cb, luctx);
+
+	return 0;
+}
+
+void osmo_libusb_exit(libusb_context *luctx)
+{
+	/* we just assume libusb is cleaning up all the osmo_Fd's we've allocated */
+	libusb_exit(luctx);
+}

-- 
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/16606
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I656a1a38cbb5b1f3a9145d2869d3b4d0adefcae3
Gerrit-Change-Number: 16606
Gerrit-PatchSet: 4
Gerrit-Owner: laforge <laforge at osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge at osmocom.org>
Gerrit-Reviewer: tnt <tnt at 246tNt.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20191217/3219db4f/attachment.htm>


More information about the gerrit-log mailing list