]> mj.ucw.cz Git - ursary.git/commitdiff
Generalized glue between LibUSB and LibUCW mainloop
authorMartin Mares <mj@ucw.cz>
Sat, 11 Apr 2020 21:21:00 +0000 (23:21 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 11 Apr 2020 21:21:00 +0000 (23:21 +0200)
Makefile
nocturn.c
ursaryd.c
usb.c [new file with mode: 0644]
usb.h [new file with mode: 0644]

index a1bdabe2b33ea50d44fcd335cc678b10621cd087..40de57e52d3e8733277b33443644cdead500bbfc 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -15,13 +15,14 @@ LDLIBS=$(LIBUCW_LIBS) $(LIBUSB_LIBS) $(LIBPULSE_LIBS)
 
 all: ursaryd
 
-ursaryd: ursaryd.o mpd.o nocturn.o pulse.o pulse-ucw.o
+ursaryd: ursaryd.o mpd.o nocturn.o pulse.o pulse-ucw.o usb.o
 
-ursaryd.o: ursaryd.c ursaryd.h
+ursaryd.o: ursaryd.c ursaryd.h usb.h
 mpd.o: mpd.c ursaryd.h
-nocturn.o: nocturn.c ursaryd.h
+nocturn.o: nocturn.c ursaryd.h usb.h
 pulse.o: pulse.c ursaryd.h
 pulse-ucw.o: pulse-ucw.c ursaryd.h
+usb.o: usb.c ursaryd.h usb.h
 
 clean:
        rm -f `find . -name "*~" -or -name "*.[oa]" -or -name "\#*\#" -or -name TAGS -or -name core -or -name .depend -or -name .#*`
index 7353822fd5ee682edf679f61bf64e717ed57e785..96cda662647c0c09a6a1e01976503e5cc44d9937 100644 (file)
--- a/nocturn.c
+++ b/nocturn.c
 
 #include <ucw/lib.h>
 #include <ucw/bitops.h>
-#include <ucw/clists.h>
-#include <ucw/gary.h>
 #include <ucw/mainloop.h>
 #include <ucw/stkstring.h>
-#include <ucw/string.h>
 
 #include <stdio.h>
 #include <string.h>
-#include <stdlib.h>
-#include <sys/poll.h>
-
-#include <libusb.h>
 
 #include "ursaryd.h"
+#include "usb.h"
 
-static libusb_context *usb_ctx;
 static libusb_device_handle *usb_dev;
 static bool usb_iface_claimed;
 
 static void noct_error(int usb_err, char *text);
 
-static struct main_file **usb_fds;
-
-static int usb_fd_ready(struct main_file *f UNUSED)
-{
-  DBG("USB: Handling events (ready on fd %d)", f->fd);
-  struct timeval tv = { 0, 0 };
-  int comp = 0;
-  int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp);
-  if (err < 0)
-    msg(L_ERROR, "libusb_handle_events: error %d", err);
-  return HOOK_IDLE;
-}
-
-static void usb_added_fd(int fd, short events, void *user_data UNUSED)
-{
-  if (fd >= (int) GARY_SIZE(usb_fds))
-    GARY_RESIZE(usb_fds, fd + 1);
-
-  struct main_file *f = usb_fds[fd];
-  if (!f)
-    {
-      f = xmalloc_zero(sizeof(*f));
-      usb_fds[fd] = f;
-    }
-  else if (file_is_active(f))
-    {
-      DBG("USB: Releasing fd %d", fd);
-      file_del(f);
-    }
-
-  DBG("USB: Adding fd %d with event mask %u", fd, events);
-  f->fd = fd;
-  f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL;
-  f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL;
-  file_add(f);
-}
-
-static void usb_removed_fd(int fd, void *user_data UNUSED)
-{
-  DBG("USB: Releasing fd %d", fd);
-  ASSERT(fd < (int) GARY_SIZE(usb_fds));
-  struct main_file *f = usb_fds[fd];
-  ASSERT(f);
-  ASSERT(file_is_active(f));
-  file_del(f);
-}
-
 static const char noct_magic[4][9] = {
   { 3, 0xb0, 0x00, 0x00 },
   { 8, 0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35 },
@@ -488,26 +434,6 @@ bool noct_is_ready(void)
 
 void noct_init(void)
 {
-  int err;
-
-  // Initialize libusb
-  if ((err = libusb_init(&usb_ctx)) < 0)
-    die("libusb_init failed: error %d", err);
-
-  // Connect libusb to UCW mainloop
-
-  if (!libusb_pollfds_handle_timeouts(usb_ctx))
-    die("Unsupported version of libusb, please fix me");
-
-  GARY_INIT_ZERO(usb_fds, 0);
-  libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL);
-
-  const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx);
-  ASSERT(fds);
-  for (int i=0; fds[i]; i++)
-    usb_added_fd(fds[i]->fd, fds[i]->events, NULL);
-  free(fds);
-
   // Prepare error handling hook
   noct_error_hook.handler = noct_error_handler;
 
index 943348843535ad8a3ebd6acd4c3d06eea7b1c45d..8df65df31a8493eee81ea6cb22a0889c6e453ede 100644 (file)
--- a/ursaryd.c
+++ b/ursaryd.c
@@ -21,6 +21,7 @@
 #include <syslog.h>
 
 #include "ursaryd.h"
+#include "usb.h"
 
 /*
  *     Map of all controls
@@ -729,6 +730,7 @@ static void daemon_body(struct daemon_params *dp)
   main_init();
   update_timer.handler = do_update;
 
+  usb_init();
   noct_init();
   pulse_init();
   mpd_init();
diff --git a/usb.c b/usb.c
new file mode 100644 (file)
index 0000000..706cc00
--- /dev/null
+++ b/usb.c
@@ -0,0 +1,92 @@
+/*
+ *     LibUSB over LibUCW Mainloop
+ *
+ *     (c) 2014--2020 Martin Mares <mj@ucw.cz>
+ */
+
+#undef LOCAL_DEBUG
+
+#include <ucw/lib.h>
+#include <ucw/clists.h>
+#include <ucw/gary.h>
+#include <ucw/mainloop.h>
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/poll.h>
+
+#include "ursaryd.h"
+#include "usb.h"
+
+libusb_context *usb_ctx;
+
+static struct main_file **usb_fds;
+
+static int usb_fd_ready(struct main_file *f UNUSED)
+{
+  DBG("USB: Handling events (ready on fd %d)", f->fd);
+  struct timeval tv = { 0, 0 };
+  int comp = 0;
+  int err = libusb_handle_events_timeout_completed(usb_ctx, &tv, &comp);
+  if (err < 0)
+    msg(L_ERROR, "libusb_handle_events: error %d", err);
+  return HOOK_IDLE;
+}
+
+static void usb_added_fd(int fd, short events, void *user_data UNUSED)
+{
+  if (fd >= (int) GARY_SIZE(usb_fds))
+    GARY_RESIZE(usb_fds, fd + 1);
+
+  struct main_file *f = usb_fds[fd];
+  if (!f)
+    {
+      f = xmalloc_zero(sizeof(*f));
+      usb_fds[fd] = f;
+    }
+  else if (file_is_active(f))
+    {
+      DBG("USB: Releasing fd %d", fd);
+      file_del(f);
+    }
+
+  DBG("USB: Adding fd %d with event mask %u", fd, events);
+  f->fd = fd;
+  f->read_handler = (events & POLLIN) ? usb_fd_ready : NULL;
+  f->write_handler = (events & POLLOUT) ? usb_fd_ready : NULL;
+  file_add(f);
+}
+
+static void usb_removed_fd(int fd, void *user_data UNUSED)
+{
+  DBG("USB: Releasing fd %d", fd);
+  ASSERT(fd < (int) GARY_SIZE(usb_fds));
+  struct main_file *f = usb_fds[fd];
+  ASSERT(f);
+  ASSERT(file_is_active(f));
+  file_del(f);
+}
+
+void usb_init(void)
+{
+  int err;
+
+  // Initialize libusb
+  if ((err = libusb_init(&usb_ctx)) < 0)
+    die("libusb_init failed: error %d", err);
+
+  // Connect libusb to UCW mainloop
+
+  if (!libusb_pollfds_handle_timeouts(usb_ctx))
+    die("Unsupported version of libusb, please fix me");
+
+  GARY_INIT_ZERO(usb_fds, 0);
+  libusb_set_pollfd_notifiers(usb_ctx, usb_added_fd, usb_removed_fd, NULL);
+
+  const struct libusb_pollfd **fds = libusb_get_pollfds(usb_ctx);
+  ASSERT(fds);
+  for (int i=0; fds[i]; i++)
+    usb_added_fd(fds[i]->fd, fds[i]->events, NULL);
+  free(fds);
+}
diff --git a/usb.h b/usb.h
new file mode 100644 (file)
index 0000000..9ca372b
--- /dev/null
+++ b/usb.h
@@ -0,0 +1,11 @@
+/*
+ *     LibUSB over LibUCW Mainloop
+ *
+ *     (c) 2014--2020 Martin Mares <mj@ucw.cz>
+ */
+
+#include <libusb.h>
+
+extern libusb_context *usb_ctx;
+
+void usb_init(void);