<p>laforge <strong>submitted</strong> this change.</p><p><a href="https://gerrit.osmocom.org/c/libosmocore/+/16606">View Change</a></p><div style="white-space:pre-wrap">Approvals:
  Jenkins Builder: Verified
  laforge: Looks good to me, approved

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">libosmocore libusb integration<br><br>Osmocom applications typically use libosmocore select.[ch] event loop<br>code as their main event dispatch mechanism.  When they want to deal<br>with libusb in a non-blocking/asynchronous way, they need to integrate<br>libusb into that select().<br><br>The new libosmousb is doing exactly that: Providing a shared utility<br>library for Osmocom programs that wish to use libusb.  This is useful<br>for example in simtrace2 host utilitie as well as osmo-e1d.<br><br>Change-Id: I656a1a38cbb5b1f3a9145d2869d3b4d0adefcae3<br>Closes: OS#4299<br>---<br>M Makefile.am<br>M configure.ac<br>M contrib/jenkins_arm.sh<br>M debian/control<br>A debian/libosmousb0.install<br>M include/Makefile.am<br>A include/osmocom/usb/libusb.h<br>A libosmousb.pc.in<br>A src/usb/Makefile.am<br>A src/usb/osmo_libusb.c<br>10 files changed, 218 insertions(+), 2 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/Makefile.am b/Makefile.am</span><br><span>index 7b6ba0a..7390f98 100644</span><br><span>--- a/Makefile.am</span><br><span>+++ b/Makefile.am</span><br><span>@@ -1,11 +1,12 @@</span><br><span> ACLOCAL_AMFLAGS = -I m4</span><br><span> </span><br><span> AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include</span><br><span style="color: hsl(0, 100%, 40%);">-SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim src/pseudotalloc utils tests</span><br><span style="color: hsl(120, 100%, 40%);">+SUBDIRS = include src src/vty src/codec src/gsm src/coding src/gb src/ctrl src/sim src/pseudotalloc src/usb utils tests</span><br><span> </span><br><span> pkgconfigdir = $(libdir)/pkgconfig</span><br><span> pkgconfig_DATA = libosmocore.pc libosmocodec.pc libosmovty.pc libosmogsm.pc \</span><br><span style="color: hsl(0, 100%, 40%);">-            libosmogb.pc libosmoctrl.pc libosmocoding.pc libosmosim.pc</span><br><span style="color: hsl(120, 100%, 40%);">+            libosmogb.pc libosmoctrl.pc libosmocoding.pc libosmosim.pc \</span><br><span style="color: hsl(120, 100%, 40%);">+          libosmousb.pc</span><br><span> </span><br><span> aclocaldir = $(datadir)/aclocal</span><br><span> dist_aclocal_DATA = m4/osmo_ac_code_coverage.m4 \</span><br><span>diff --git a/configure.ac b/configure.ac</span><br><span>index 060d081..e45ec9f 100644</span><br><span>--- a/configure.ac</span><br><span>+++ b/configure.ac</span><br><span>@@ -146,6 +146,19 @@</span><br><span> AM_CONDITIONAL(ENABLE_PCSC, test "x$ENABLE_PCSC" = "xyes")</span><br><span> AC_SUBST(ENABLE_PCSC)</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+AC_ARG_ENABLE([libusb], [AS_HELP_STRING([--disable-libusb], [Build without libusb support])],</span><br><span style="color: hsl(120, 100%, 40%);">+    [</span><br><span style="color: hsl(120, 100%, 40%);">+        ENABLE_LIBUSB=$enableval</span><br><span style="color: hsl(120, 100%, 40%);">+    ],</span><br><span style="color: hsl(120, 100%, 40%);">+    [</span><br><span style="color: hsl(120, 100%, 40%);">+        ENABLE_LIBUSB="yes"</span><br><span style="color: hsl(120, 100%, 40%);">+    ])</span><br><span style="color: hsl(120, 100%, 40%);">+AS_IF([test "x$ENABLE_LIBUSB" = "xyes"], [</span><br><span style="color: hsl(120, 100%, 40%);">+   PKG_CHECK_MODULES(LIBUSB, libusb-1.0)</span><br><span style="color: hsl(120, 100%, 40%);">+])</span><br><span style="color: hsl(120, 100%, 40%);">+AM_CONDITIONAL(ENABLE_LIBUSB, test "x$ENABLE_LIBUSB" = "xyes")</span><br><span style="color: hsl(120, 100%, 40%);">+AC_SUBST(ENABLE_LIBUSB)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> AC_ARG_ENABLE([gnutls], [AS_HELP_STRING([--disable-gnutls], [Do not use GnuTLS fallback for missing getrandom()])],</span><br><span>      [ENABLE_GNUTLS=$enableval], [ENABLE_GNUTLS="yes"])</span><br><span> AM_CONDITIONAL(ENABLE_GNUTLS, test x"$ENABLE_GNUTLS" = x"yes")</span><br><span>@@ -398,12 +411,14 @@</span><br><span>   libosmogb.pc</span><br><span>         libosmoctrl.pc</span><br><span>       libosmosim.pc</span><br><span style="color: hsl(120, 100%, 40%);">+ libosmousb.pc</span><br><span>        include/Makefile</span><br><span>     src/Makefile</span><br><span>         src/vty/Makefile</span><br><span>     src/codec/Makefile</span><br><span>   src/coding/Makefile</span><br><span>  src/sim/Makefile</span><br><span style="color: hsl(120, 100%, 40%);">+      src/usb/Makefile</span><br><span>     src/gsm/Makefile</span><br><span>     src/gb/Makefile</span><br><span>      src/ctrl/Makefile</span><br><span>diff --git a/contrib/jenkins_arm.sh b/contrib/jenkins_arm.sh</span><br><span>index 8ee90f0..c9cd922 100755</span><br><span>--- a/contrib/jenkins_arm.sh</span><br><span>+++ b/contrib/jenkins_arm.sh</span><br><span>@@ -19,6 +19,7 @@</span><br><span>   --disable-doxygen \</span><br><span>  --disable-shared \</span><br><span>   --disable-libsctp \</span><br><span style="color: hsl(120, 100%, 40%);">+   --disable-libusb \</span><br><span>   --enable-external-tests \</span><br><span>    CFLAGS="-Os -ffunction-sections -fdata-sections -nostartfiles -nodefaultlibs $WERROR_FLAGS"</span><br><span> </span><br><span>diff --git a/debian/control b/debian/control</span><br><span>index cf7a853..d2c232f 100644</span><br><span>--- a/debian/control</span><br><span>+++ b/debian/control</span><br><span>@@ -34,6 +34,7 @@</span><br><span>          libosmovty4 (= ${binary:Version}),</span><br><span>          libosmoctrl0 (= ${binary:Version}),</span><br><span>          libosmosim0 (= ${binary:Version}),</span><br><span style="color: hsl(120, 100%, 40%);">+         libosmousb0 (= ${binary:Version}),</span><br><span>          ${misc:Depends}</span><br><span> Description: Open Source MObile COMmunications CORE library (metapackage)</span><br><span>  The libraries provided by this package contain various utility functions.</span><br><span>@@ -293,6 +294,23 @@</span><br><span>  .</span><br><span>  The libosmosim library in particular contains routines for SIM card access.</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+Package: libosmousb0</span><br><span style="color: hsl(120, 100%, 40%);">+Section: libs</span><br><span style="color: hsl(120, 100%, 40%);">+Architecture: any</span><br><span style="color: hsl(120, 100%, 40%);">+Multi-Arch: same</span><br><span style="color: hsl(120, 100%, 40%);">+Depends: ${shlibs:Depends},</span><br><span style="color: hsl(120, 100%, 40%);">+         ${misc:Depends}</span><br><span style="color: hsl(120, 100%, 40%);">+Pre-Depends: ${misc:Pre-Depends}</span><br><span style="color: hsl(120, 100%, 40%);">+Description: Osmo USB library</span><br><span style="color: hsl(120, 100%, 40%);">+ This is part of the libosmocore "meta"-library. The libosmocore library</span><br><span style="color: hsl(120, 100%, 40%);">+ contains various utility functions that were originally developed as part of</span><br><span style="color: hsl(120, 100%, 40%);">+ the OpenBSC project, but which are of a more generic nature and thus useful to</span><br><span style="color: hsl(120, 100%, 40%);">+ (at least) other programs that are developed in the sphere of Free Software /</span><br><span style="color: hsl(120, 100%, 40%);">+ Open Source mobile communication.</span><br><span style="color: hsl(120, 100%, 40%);">+ .</span><br><span style="color: hsl(120, 100%, 40%);">+ The libosmosub library in particular contains routines for USB device access</span><br><span style="color: hsl(120, 100%, 40%);">+ via libusb-1.0, integrated into the libosmocore select event loop.</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> Package: libosmocore-dev</span><br><span> Architecture: any</span><br><span> Multi-Arch: same</span><br><span>diff --git a/debian/libosmousb0.install b/debian/libosmousb0.install</span><br><span>new file mode 100644</span><br><span>index 0000000..4031b9f</span><br><span>--- /dev/null</span><br><span>+++ b/debian/libosmousb0.install</span><br><span>@@ -0,0 +1 @@</span><br><span style="color: hsl(120, 100%, 40%);">+usr/lib/*/libosmousb*.so.*</span><br><span>diff --git a/include/Makefile.am b/include/Makefile.am</span><br><span>index d76addd..dc6eaa7 100644</span><br><span>--- a/include/Makefile.am</span><br><span>+++ b/include/Makefile.am</span><br><span>@@ -164,6 +164,11 @@</span><br><span>                           osmocom/ctrl/control_vty.h</span><br><span> endif</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+if ENABLE_LIBUSB</span><br><span style="color: hsl(120, 100%, 40%);">+nobase_include_HEADERS += \</span><br><span style="color: hsl(120, 100%, 40%);">+                        osmocom/usb/libusb.h</span><br><span style="color: hsl(120, 100%, 40%);">+endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> noinst_HEADERS = \</span><br><span>        osmocom/gsm/kasumi.h \</span><br><span>       osmocom/gsm/gea.h \</span><br><span>diff --git a/include/osmocom/usb/libusb.h b/include/osmocom/usb/libusb.h</span><br><span>new file mode 100644</span><br><span>index 0000000..7f10f74</span><br><span>--- /dev/null</span><br><span>+++ b/include/osmocom/usb/libusb.h</span><br><span>@@ -0,0 +1,6 @@</span><br><span style="color: hsl(120, 100%, 40%);">+#pragma once</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <libusb.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int osmo_libusb_init(libusb_context **luctx);</span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_libusb_exit(libusb_context *luctx);</span><br><span>diff --git a/libosmousb.pc.in b/libosmousb.pc.in</span><br><span>new file mode 100644</span><br><span>index 0000000..ce6d271</span><br><span>--- /dev/null</span><br><span>+++ b/libosmousb.pc.in</span><br><span>@@ -0,0 +1,11 @@</span><br><span style="color: hsl(120, 100%, 40%);">+prefix=@prefix@</span><br><span style="color: hsl(120, 100%, 40%);">+exec_prefix=@exec_prefix@</span><br><span style="color: hsl(120, 100%, 40%);">+libdir=@libdir@</span><br><span style="color: hsl(120, 100%, 40%);">+includedir=@includedir@</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+Name: Osmocom libusb (USB) integration</span><br><span style="color: hsl(120, 100%, 40%);">+Description: C Utility Library</span><br><span style="color: hsl(120, 100%, 40%);">+Version: @VERSION@</span><br><span style="color: hsl(120, 100%, 40%);">+Libs: -L${libdir} @TALLOC_LIBS@ -losmousb -losmocore</span><br><span style="color: hsl(120, 100%, 40%);">+Cflags: -I${includedir}/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span>diff --git a/src/usb/Makefile.am b/src/usb/Makefile.am</span><br><span>new file mode 100644</span><br><span>index 0000000..bca39bf</span><br><span>--- /dev/null</span><br><span>+++ b/src/usb/Makefile.am</span><br><span>@@ -0,0 +1,21 @@</span><br><span style="color: hsl(120, 100%, 40%);">+# This is _NOT_ the library release version, it's an API version.</span><br><span style="color: hsl(120, 100%, 40%);">+# Please read chapter "Library interface versions" of the libtool documentation</span><br><span style="color: hsl(120, 100%, 40%);">+# before making any modifications: https://www.gnu.org/software/libtool/manual/html_node/Versioning.html</span><br><span style="color: hsl(120, 100%, 40%);">+LIBVERSION=0:0:0</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)/include</span><br><span style="color: hsl(120, 100%, 40%);">+AM_CFLAGS = -fPIC -Wall $(LIBUSB_CFLAGS) $(TALLOC_CFLAGS)</span><br><span style="color: hsl(120, 100%, 40%);">+AM_LDFLAGS = $(COVERAGE_LDFLAGS)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+if ENABLE_LIBUSB</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+lib_LTLIBRARIES = libosmousb.la</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+libosmousb_la_SOURCES = osmo_libusb.c</span><br><span style="color: hsl(120, 100%, 40%);">+libosmosim_la_LDFLAGS = -version-info $(LIBVERSION)</span><br><span style="color: hsl(120, 100%, 40%);">+libosmosim_la_LIBADD = \</span><br><span style="color: hsl(120, 100%, 40%);">+    $(top_builddir)/src/libosmocore.la \</span><br><span style="color: hsl(120, 100%, 40%);">+  $(TALLOC_LIBS) \</span><br><span style="color: hsl(120, 100%, 40%);">+      $(LIBUSB_LIBS)</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+endif</span><br><span>diff --git a/src/usb/osmo_libusb.c b/src/usb/osmo_libusb.c</span><br><span>new file mode 100644</span><br><span>index 0000000..fda9573</span><br><span>--- /dev/null</span><br><span>+++ b/src/usb/osmo_libusb.c</span><br><span>@@ -0,0 +1,137 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/* libosmocore integration with libusb-1.0</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * (C) 2019 by Harald Welte <laforge@gnumonks.org></span><br><span style="color: hsl(120, 100%, 40%);">+ * All Rights Reserved.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * SPDX-License-Identifier: GPL-2.0+</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software; you can redistribute it and/or</span><br><span style="color: hsl(120, 100%, 40%);">+ * modify it under the terms of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * as published by the Free Software Foundation; either version 2</span><br><span style="color: hsl(120, 100%, 40%);">+ * of the License, or (at your option) any later version.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is distributed in the hope that it will be useful,</span><br><span style="color: hsl(120, 100%, 40%);">+ * but WITHOUT ANY WARRANTY; without even the implied warranty of</span><br><span style="color: hsl(120, 100%, 40%);">+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span><br><span style="color: hsl(120, 100%, 40%);">+ * GNU General Public License for more details.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * You should have received a copy of the GNU General Public License</span><br><span style="color: hsl(120, 100%, 40%);">+ * along with this program; if not, write to the Free Software</span><br><span style="color: hsl(120, 100%, 40%);">+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#include <errno.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <unistd.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdlib.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <stdint.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <string.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <poll.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/utils.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/logging.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/select.h></span><br><span style="color: hsl(120, 100%, 40%);">+#include <osmocom/core/talloc.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include <libusb.h></span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/***********************************************************************</span><br><span style="color: hsl(120, 100%, 40%);">+ * logging integration</span><br><span style="color: hsl(120, 100%, 40%);">+ ***********************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#define DLUSB  DLINP</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef LIBUSB_LOG_CB_CONTEXT /* introduced in 1.0.23 */</span><br><span style="color: hsl(120, 100%, 40%);">+static const int usb2logl[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+     [LIBUSB_LOG_LEVEL_NONE] = LOGL_FATAL,</span><br><span style="color: hsl(120, 100%, 40%);">+ [LIBUSB_LOG_LEVEL_ERROR] = LOGL_ERROR,</span><br><span style="color: hsl(120, 100%, 40%);">+        [LIBUSB_LOG_LEVEL_WARNING] = LOGL_NOTICE,</span><br><span style="color: hsl(120, 100%, 40%);">+     [LIBUSB_LOG_LEVEL_INFO] = LOGL_INFO,</span><br><span style="color: hsl(120, 100%, 40%);">+  [LIBUSB_LOG_LEVEL_DEBUG] = LOGL_DEBUG,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* called by libusb if it wants to log something */</span><br><span style="color: hsl(120, 100%, 40%);">+static void libosmo_usb_log_cb(libusb_context *luctx, enum libusb_log_level level_usb, const char *str)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int level = LOGL_NOTICE;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+    if (level_usb < ARRAY_SIZE(usb2logl))</span><br><span style="color: hsl(120, 100%, 40%);">+              level = usb2logl[level_usb];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+        LOGP(DLUSB, level, "%s", str);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+#endif /* LIBUSB_LOG_CB_CONTEXT */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/***********************************************************************</span><br><span style="color: hsl(120, 100%, 40%);">+ * select loop integration</span><br><span style="color: hsl(120, 100%, 40%);">+ ***********************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static int osmo_usb_fd_cb(struct osmo_fd *ofd, unsigned int what)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+    libusb_context *luctx = ofd->data;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+       /* we assume that we're running Linux v2.6.27 with timerfd support here</span><br><span style="color: hsl(120, 100%, 40%);">+    * and hence don't have to perform manual timeout handling.  See</span><br><span style="color: hsl(120, 100%, 40%);">+   * "Notes on time-based events" at</span><br><span style="color: hsl(120, 100%, 40%);">+   * http://libusb.sourceforge.net/api-1.0/group__libusb__poll.html */</span><br><span style="color: hsl(120, 100%, 40%);">+  struct timeval zero_tv = { 0, 0 };</span><br><span style="color: hsl(120, 100%, 40%);">+    libusb_handle_events_timeout(luctx, &zero_tv);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* called by libusb if it wants to add a file-descriptor */</span><br><span style="color: hsl(120, 100%, 40%);">+static void osmo_usb_added_cb(int fd, short events, void *user_data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  struct osmo_fd *ofd = talloc_zero(OTC_GLOBAL, struct osmo_fd);</span><br><span style="color: hsl(120, 100%, 40%);">+        libusb_context *luctx = user_data;</span><br><span style="color: hsl(120, 100%, 40%);">+    unsigned int when = 0;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      if (events & POLLIN)</span><br><span style="color: hsl(120, 100%, 40%);">+              when |= OSMO_FD_READ;</span><br><span style="color: hsl(120, 100%, 40%);">+ if (events & POLLOUT)</span><br><span style="color: hsl(120, 100%, 40%);">+             when |= OSMO_FD_WRITE;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+      osmo_fd_setup(ofd, fd, when, osmo_usb_fd_cb, luctx, 0);</span><br><span style="color: hsl(120, 100%, 40%);">+       osmo_fd_register(ofd);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/* called by libusb if it wants to remove a file-descriptor */</span><br><span style="color: hsl(120, 100%, 40%);">+static void osmo_usb_removed_cb(int fd, void *user_data)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+      struct osmo_fd *ofd = osmo_fd_get_by_fd(fd);</span><br><span style="color: hsl(120, 100%, 40%);">+  if (!fd)</span><br><span style="color: hsl(120, 100%, 40%);">+              return;</span><br><span style="color: hsl(120, 100%, 40%);">+       osmo_fd_unregister(ofd);</span><br><span style="color: hsl(120, 100%, 40%);">+      talloc_free(ofd);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/***********************************************************************</span><br><span style="color: hsl(120, 100%, 40%);">+ * initialization</span><br><span style="color: hsl(120, 100%, 40%);">+ ***********************************************************************/</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int osmo_libusb_init(libusb_context **pluctx)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+  libusb_context *luctx = NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+     rc = libusb_init(pluctx);</span><br><span style="color: hsl(120, 100%, 40%);">+     if (rc != 0)</span><br><span style="color: hsl(120, 100%, 40%);">+          return rc;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  if (pluctx)</span><br><span style="color: hsl(120, 100%, 40%);">+           luctx = *pluctx;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#ifdef LIBUSB_LOG_CB_CONTEXT /* introduced in 1.0.23 */</span><br><span style="color: hsl(120, 100%, 40%);">+   libusb_set_log_cb(luctx, &libosmo_usb_log_cb, LIBUSB_LOG_CB_CONTEXT);</span><br><span style="color: hsl(120, 100%, 40%);">+#endif</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+   libusb_set_pollfd_notifiers(luctx, osmo_usb_added_cb, osmo_usb_removed_cb, luctx);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+  return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+void osmo_libusb_exit(libusb_context *luctx)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+       /* we just assume libusb is cleaning up all the osmo_Fd's we've allocated */</span><br><span style="color: hsl(120, 100%, 40%);">+  libusb_exit(luctx);</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.osmocom.org/c/libosmocore/+/16606">change 16606</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.osmocom.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.osmocom.org/c/libosmocore/+/16606"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: libosmocore </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: I656a1a38cbb5b1f3a9145d2869d3b4d0adefcae3 </div>
<div style="display:none"> Gerrit-Change-Number: 16606 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins Builder </div>
<div style="display:none"> Gerrit-Reviewer: laforge <laforge@osmocom.org> </div>
<div style="display:none"> Gerrit-Reviewer: tnt <tnt@246tNt.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>