From 4873e46a6d7bc37f72c3e2d845b252e91faa690d Mon Sep 17 00:00:00 2001
From: Micah Elizabeth Scott <micah@scanlime.org>
Date: Wed, 24 Jul 2013 02:00:25 -0700
Subject: [PATCH] Hotplug events working

---
 server/Makefile     |  1 +
 server/fcdevice.cpp | 24 ++++++++++++++++++++++++
 server/fcdevice.h   | 37 +++++++++++++++++++++++++++++++++++++
 server/fcserver.cpp | 25 +++++++++++++++++++++++--
 server/fcserver.h   |  4 +++-
 server/libusbev.cpp |  1 +
 6 files changed, 89 insertions(+), 3 deletions(-)
 create mode 100644 server/fcdevice.cpp
 create mode 100644 server/fcdevice.h

diff --git a/server/Makefile b/server/Makefile
index 395ec39..f7a7878 100644
--- a/server/Makefile
+++ b/server/Makefile
@@ -12,6 +12,7 @@ CPP_FILES = \
 	main.cpp \
 	opcsink.cpp \
 	libusbev.cpp \
+	fcdevice.cpp \
 	fcserver.cpp
 
 # CPPFLAGS = compiler options for C and C++
diff --git a/server/fcdevice.cpp b/server/fcdevice.cpp
new file mode 100644
index 0000000..6bcbddf
--- /dev/null
+++ b/server/fcdevice.cpp
@@ -0,0 +1,24 @@
+/*
+ * Fadecandy device interface
+ * 
+ * Copyright (c) 2013 Micah Elizabeth Scott
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include "fcdevice.h"
diff --git a/server/fcdevice.h b/server/fcdevice.h
new file mode 100644
index 0000000..85368b0
--- /dev/null
+++ b/server/fcdevice.h
@@ -0,0 +1,37 @@
+/*
+ * Fadecandy device interface
+ * 
+ * Copyright (c) 2013 Micah Elizabeth Scott
+ * 
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of
+ * this software and associated documentation files (the "Software"), to deal in
+ * the Software without restriction, including without limitation the rights to
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+ * the Software, and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+#include <libusb.h>
+
+
+class FCDevice {
+public:
+    FCDevice(libusb_device *device);
+
+    int open();
+
+private:
+    libusb_device *mDevice;
+    libusb_device_handle *mHandle;
+};
diff --git a/server/fcserver.cpp b/server/fcserver.cpp
index f74dba4..0df601e 100644
--- a/server/fcserver.cpp
+++ b/server/fcserver.cpp
@@ -34,7 +34,7 @@ FCServer::FCServer(rapidjson::Document &config)
 	  mDevices(config["devices"]),
 	  mVerbose(config["verbose"].IsTrue()),
 	  mListenAddr(0),
-	  mOPCSink(opcCallback, this, mVerbose),
+	  mOPCSink(cbMessage, this, mVerbose),
 	  mUSB(0)
 {
 	/*
@@ -92,13 +92,34 @@ void FCServer::startUSB(struct ev_loop *loop)
 		return;
 	}
 
+	// Attach to our libev event loop
 	mUSBEvent.init(mUSB, loop);
+
+	// Enumerate all attached devices, and get notified of hotplug events
+	libusb_hotplug_register_callback(mUSB,
+		libusb_hotplug_event(LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED |
+				             LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT),
+		LIBUSB_HOTPLUG_ENUMERATE,
+		LIBUSB_HOTPLUG_MATCH_ANY,
+		LIBUSB_HOTPLUG_MATCH_ANY,
+		LIBUSB_HOTPLUG_MATCH_ANY,
+		cbHotplug, this, 0);
 }
 
-void FCServer::opcCallback(OPCSink::Message &msg, void *context)
+void FCServer::cbMessage(OPCSink::Message &msg, void *context)
 {
 	FCServer *self = static_cast<FCServer*>(context);
 
 	printf("Msg %d bytes\n", msg.length());
 }
 
+int FCServer::cbHotplug(libusb_context *ctx, libusb_device *device, libusb_hotplug_event event, void *user_data)
+{
+	FCServer *self = static_cast<FCServer*>(user_data);
+
+	printf("Hotplug %d\n", event);
+
+	return false;
+}
+
+
diff --git a/server/fcserver.h b/server/fcserver.h
index 6f0030d..ab7e6f0 100644
--- a/server/fcserver.h
+++ b/server/fcserver.h
@@ -58,6 +58,8 @@ private:
     libusb_context *mUSB;
     LibUSBEventBridge mUSBEvent;
 
-    static void opcCallback(OPCSink::Message &msg, void *context);
+    static void cbMessage(OPCSink::Message &msg, void *context);
+    static int cbHotplug(libusb_context *ctx, libusb_device *device, libusb_hotplug_event event, void *user_data);
+
     void startUSB(struct ev_loop *loop);
 };
diff --git a/server/libusbev.cpp b/server/libusbev.cpp
index 2582ec4..00ffeee 100644
--- a/server/libusbev.cpp
+++ b/server/libusbev.cpp
@@ -42,6 +42,7 @@ void LibUSBEventBridge::cbAdded(int fd, short events, void *user_data)
 	LibUSBEventBridge *self = static_cast<LibUSBEventBridge*>(user_data);
 	Watcher *w = new Watcher();
 
+	w->self = self;
 	ev_io_init(&w->io, cbEvent, fd,
 		((events & POLLIN) ? EV_READ : 0) |
 		((events & POLLOUT) ? EV_WRITE : 0));
-- 
GitLab