diff --git a/server/Makefile b/server/Makefile index 395ec399e12788736a90c6cb405b6eaeb9bafb51..f7a7878016812eefd2f41b3407a4e7d7c4e8e033 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 0000000000000000000000000000000000000000..6bcbddfc380d9d05ad3696f0d54351c480794023 --- /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 0000000000000000000000000000000000000000..85368b03d0295236077aa06bbe5eaf3287eabcf5 --- /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 f74dba4d1c74778f7d8e1a9f9c6077c82850eeb4..0df601e2c7558edb7ab37057ab093ecfa29eb2f6 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 6f0030d1a567461bb4221d90a5264f01d404454d..ab7e6f03d0c9f980aeb607bedae1de0d59e499c1 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 2582ec442948a8e3b016235bcb6917a07fcd173d..00ffeeea4e8d233cfbfcf02fbcf081fda9933f1e 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));