Skip to content
Snippets Groups Projects
Commit 4873e46a authored by Micah Elizabeth Scott's avatar Micah Elizabeth Scott
Browse files

Hotplug events working

parent 5e81f277
Branches
Tags
No related merge requests found
...@@ -12,6 +12,7 @@ CPP_FILES = \ ...@@ -12,6 +12,7 @@ CPP_FILES = \
main.cpp \ main.cpp \
opcsink.cpp \ opcsink.cpp \
libusbev.cpp \ libusbev.cpp \
fcdevice.cpp \
fcserver.cpp fcserver.cpp
# CPPFLAGS = compiler options for C and C++ # CPPFLAGS = compiler options for C and C++
......
/*
* 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"
/*
* 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;
};
...@@ -34,7 +34,7 @@ FCServer::FCServer(rapidjson::Document &config) ...@@ -34,7 +34,7 @@ FCServer::FCServer(rapidjson::Document &config)
mDevices(config["devices"]), mDevices(config["devices"]),
mVerbose(config["verbose"].IsTrue()), mVerbose(config["verbose"].IsTrue()),
mListenAddr(0), mListenAddr(0),
mOPCSink(opcCallback, this, mVerbose), mOPCSink(cbMessage, this, mVerbose),
mUSB(0) mUSB(0)
{ {
/* /*
...@@ -92,13 +92,34 @@ void FCServer::startUSB(struct ev_loop *loop) ...@@ -92,13 +92,34 @@ void FCServer::startUSB(struct ev_loop *loop)
return; return;
} }
// Attach to our libev event loop
mUSBEvent.init(mUSB, 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); FCServer *self = static_cast<FCServer*>(context);
printf("Msg %d bytes\n", msg.length()); 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;
}
...@@ -58,6 +58,8 @@ private: ...@@ -58,6 +58,8 @@ private:
libusb_context *mUSB; libusb_context *mUSB;
LibUSBEventBridge mUSBEvent; 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); void startUSB(struct ev_loop *loop);
}; };
...@@ -42,6 +42,7 @@ void LibUSBEventBridge::cbAdded(int fd, short events, void *user_data) ...@@ -42,6 +42,7 @@ void LibUSBEventBridge::cbAdded(int fd, short events, void *user_data)
LibUSBEventBridge *self = static_cast<LibUSBEventBridge*>(user_data); LibUSBEventBridge *self = static_cast<LibUSBEventBridge*>(user_data);
Watcher *w = new Watcher(); Watcher *w = new Watcher();
w->self = self;
ev_io_init(&w->io, cbEvent, fd, ev_io_init(&w->io, cbEvent, fd,
((events & POLLIN) ? EV_READ : 0) | ((events & POLLIN) ? EV_READ : 0) |
((events & POLLOUT) ? EV_WRITE : 0)); ((events & POLLOUT) ? EV_WRITE : 0));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment