boo/lib/inputdev/HIDDeviceUdev.cpp

277 lines
8.0 KiB
C++
Raw Normal View History

2015-04-19 20:16:50 +00:00
#include "IHIDDevice.hpp"
2015-08-18 22:43:30 +00:00
#include "boo/inputdev/DeviceToken.hpp"
#include "boo/inputdev/DeviceBase.hpp"
#include <thread>
#include <mutex>
#include <condition_variable>
2015-08-18 18:00:24 +00:00
#include <stdio.h>
#include <libudev.h>
#include <stropts.h>
#include <linux/usb/ch9.h>
#include <linux/usbdevice_fs.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <string.h>
2015-04-29 10:24:39 +00:00
namespace boo
{
udev* GetUdev();
2015-04-29 10:24:39 +00:00
/*
* Reference: http://tali.admingilde.org/linux-docbook/usb/ch07s06.html
*/
2015-08-18 19:40:26 +00:00
class HIDDeviceUdev final : public IHIDDevice
{
2015-08-18 19:40:26 +00:00
DeviceToken& m_token;
DeviceBase& m_devImp;
int m_devFd = 0;
unsigned m_usbIntfInPipe = 0;
unsigned m_usbIntfOutPipe = 0;
bool m_runningTransferLoop = false;
const std::string& m_devPath;
std::mutex m_initMutex;
std::condition_variable m_initCond;
std::thread m_thread;
2015-05-10 07:02:18 +00:00
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
{
if (m_devFd)
{
usbdevfs_bulktransfer xfer =
{
2016-02-18 14:25:47 +00:00
m_usbIntfOutPipe | USB_DIR_OUT,
(unsigned)length,
2016-02-18 14:14:59 +00:00
30,
(void*)data
};
int ret = ioctl(m_devFd, USBDEVFS_BULK, &xfer);
if (ret != (int)length)
return false;
return true;
}
return false;
}
2015-05-15 01:16:36 +00:00
2015-05-10 07:02:18 +00:00
size_t _receiveUSBInterruptTransfer(uint8_t* data, size_t length)
{
if (m_devFd)
{
usbdevfs_bulktransfer xfer =
{
m_usbIntfInPipe | USB_DIR_IN,
(unsigned)length,
2016-02-18 14:14:59 +00:00
30,
data
};
return ioctl(m_devFd, USBDEVFS_BULK, &xfer);
}
return 0;
}
2015-08-18 19:40:26 +00:00
static void _threadProcUSBLL(HIDDeviceUdev* device)
{
unsigned i;
2015-05-03 06:40:20 +00:00
char errStr[256];
std::unique_lock<std::mutex> lk(device->m_initMutex);
udev_device* udevDev = udev_device_new_from_syspath(GetUdev(), device->m_devPath.c_str());
2015-05-03 06:40:20 +00:00
/* Get device file */
const char* dp = udev_device_get_devnode(udevDev);
2015-08-19 05:47:46 +00:00
int fd = open(dp, O_RDWR);
if (fd < 0)
{
snprintf(errStr, 256, "Unable to open %s@%s: %s\n",
device->m_token.getProductName().c_str(), dp, strerror(errno));
device->m_devImp.deviceError(errStr);
lk.unlock();
device->m_initCond.notify_one();
udev_device_unref(udevDev);
return;
}
2015-08-19 05:47:46 +00:00
device->m_devFd = fd;
2015-05-10 07:02:18 +00:00
usb_device_descriptor devDesc = {};
2015-08-19 05:47:46 +00:00
read(fd, &devDesc, 1);
read(fd, &devDesc.bDescriptorType, devDesc.bLength-1);
if (devDesc.bNumConfigurations)
{
2015-05-10 07:02:18 +00:00
usb_config_descriptor confDesc = {};
2015-08-19 05:47:46 +00:00
read(fd, &confDesc, 1);
read(fd, &confDesc.bDescriptorType, confDesc.bLength-1);
if (confDesc.bNumInterfaces)
{
2015-05-10 07:02:18 +00:00
usb_interface_descriptor intfDesc = {};
2015-08-19 05:47:46 +00:00
read(fd, &intfDesc, 1);
read(fd, &intfDesc.bDescriptorType, intfDesc.bLength-1);
for (i=0 ; i<intfDesc.bNumEndpoints+1 ; ++i)
{
2015-05-10 07:02:18 +00:00
usb_endpoint_descriptor endpDesc = {};
2015-08-19 05:47:46 +00:00
read(fd, &endpDesc, 1);
read(fd, &endpDesc.bDescriptorType, endpDesc.bLength-1);
if ((endpDesc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
{
if ((endpDesc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
device->m_usbIntfInPipe = endpDesc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
else if ((endpDesc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
device->m_usbIntfOutPipe = endpDesc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
}
}
}
}
/* Request that kernel disconnects existing driver */
usbdevfs_ioctl disconnectReq = {
0,
USBDEVFS_DISCONNECT,
NULL
};
2015-08-19 05:47:46 +00:00
ioctl(fd, USBDEVFS_IOCTL, &disconnectReq);
/* Return control to main thread */
device->m_runningTransferLoop = true;
lk.unlock();
device->m_initCond.notify_one();
/* Start transfer loop */
device->m_devImp.initialCycle();
while (device->m_runningTransferLoop)
device->m_devImp.transferCycle();
device->m_devImp.finalCycle();
/* Cleanup */
2015-08-19 05:47:46 +00:00
close(fd);
device->m_devFd = 0;
udev_device_unref(udevDev);
}
2015-08-18 19:40:26 +00:00
static void _threadProcBTLL(HIDDeviceUdev* device)
{
std::unique_lock<std::mutex> lk(device->m_initMutex);
udev_device* udevDev = udev_device_new_from_syspath(GetUdev(), device->m_devPath.c_str());
/* Return control to main thread */
device->m_runningTransferLoop = true;
lk.unlock();
device->m_initCond.notify_one();
/* Start transfer loop */
device->m_devImp.initialCycle();
while (device->m_runningTransferLoop)
device->m_devImp.transferCycle();
device->m_devImp.finalCycle();
udev_device_unref(udevDev);
}
2015-08-18 19:40:26 +00:00
static void _threadProcHID(HIDDeviceUdev* device)
{
std::unique_lock<std::mutex> lk(device->m_initMutex);
udev_device* udevDev = udev_device_new_from_syspath(GetUdev(), device->m_devPath.c_str());
/* Return control to main thread */
device->m_runningTransferLoop = true;
lk.unlock();
device->m_initCond.notify_one();
/* Start transfer loop */
device->m_devImp.initialCycle();
while (device->m_runningTransferLoop)
device->m_devImp.transferCycle();
device->m_devImp.finalCycle();
udev_device_unref(udevDev);
}
void _deviceDisconnected()
{
m_runningTransferLoop = false;
}
2015-05-15 01:16:36 +00:00
bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message)
{
2015-05-15 01:16:36 +00:00
if (m_devFd)
{
usbdevfs_ctrltransfer xfer =
{
USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
0x09, // HID_SET_REPORT
message,
0,
(uint16_t)length,
2016-02-18 14:14:59 +00:00
30,
2015-05-15 01:16:36 +00:00
(void*)data
};
int ret = ioctl(m_devFd, USBDEVFS_CONTROL, &xfer);
if (ret != (int)length)
return false;
return true;
}
return false;
}
2015-05-15 01:16:36 +00:00
size_t _recieveReport(const uint8_t *data, size_t length, uint16_t message)
{
if (m_devFd)
{
usbdevfs_ctrltransfer xfer =
{
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
0x01, // HID_GET_REPORT
message,
0,
(uint16_t)length,
0,
(void*)data
};
return ioctl(m_devFd, USBDEVFS_CONTROL, &xfer);
}
return 0;
}
public:
2015-08-18 19:40:26 +00:00
HIDDeviceUdev(DeviceToken& token, DeviceBase& devImp)
: m_token(token),
m_devImp(devImp),
m_devPath(token.getDevicePath())
{
devImp.m_hidDev = this;
std::unique_lock<std::mutex> lk(m_initMutex);
2015-11-21 01:12:22 +00:00
DeviceToken::DeviceType dType = token.getDeviceType();
if (dType == DeviceToken::DeviceType::USB)
m_thread = std::thread(_threadProcUSBLL, this);
2015-11-21 01:12:22 +00:00
else if (dType == DeviceToken::DeviceType::Bluetooth)
m_thread = std::thread(_threadProcBTLL, this);
2015-11-21 01:12:22 +00:00
else if (dType == DeviceToken::DeviceType::GenericHID)
m_thread = std::thread(_threadProcHID, this);
else
2015-08-18 18:00:24 +00:00
{
fprintf(stderr, "invalid token supplied to device constructor");
abort();
}
m_initCond.wait(lk);
}
2015-08-18 19:40:26 +00:00
~HIDDeviceUdev()
{
m_runningTransferLoop = false;
if (m_thread.joinable())
m_thread.join();
}
};
2015-08-18 19:40:26 +00:00
IHIDDevice* IHIDDeviceNew(DeviceToken& token, DeviceBase& devImp)
{
2015-08-18 19:40:26 +00:00
return new HIDDeviceUdev(token, devImp);
}
2015-04-29 10:24:39 +00:00
}