boo/lib/inputdev/HIDListenerIOKit.cpp

224 lines
7.4 KiB
C++
Raw Normal View History

2015-09-02 19:09:13 +00:00
#include "boo/inputdev/IHIDListener.hpp"
#include "boo/inputdev/DeviceFinder.hpp"
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/hid/IOHIDLib.h>
2015-04-24 00:24:15 +00:00
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/IOCFPlugIn.h>
namespace boo
{
2015-04-29 10:24:39 +00:00
/*
* Reference: http://oroboro.com/usb-serial-number-osx/
2015-04-24 00:24:15 +00:00
*/
static bool getUSBStringDescriptor(IOUSBDeviceInterface182** usbDevice, UInt8 idx, char* out)
{
UInt16 buffer[128];
// wow... we're actually forced to make hard coded bus requests. Its like
// hard disk programming in the 80's!
IOUSBDevRequest request;
request.bmRequestType = USBmakebmRequestType(kUSBIn,
kUSBStandard,
kUSBDevice);
request.bRequest = kUSBRqGetDescriptor;
request.wValue = (kUSBStringDesc << 8) | idx;
request.wIndex = 0x409; // english
request.wLength = sizeof(buffer);
request.pData = buffer;
kern_return_t err = (*usbDevice)->DeviceRequest(usbDevice, &request);
if (err != 0)
{
// the request failed... fairly uncommon for the USB disk driver, but not
// so uncommon for other devices. This can also be less reliable if your
// disk is mounted through an external USB hub. At this level we actually
// have to worry about hardware issues like this.
return false;
}
// we're mallocing this string just as an example. But you probably will want
// to do something smarter, like pre-allocated buffers in the info class, or
// use a string class.
unsigned count = (request.wLenDone - 1) / 2;
unsigned i;
for (i=0 ; i<count ; ++i)
out[i] = buffer[i+1];
out[i] = '\0';
return true;
}
2015-09-02 19:09:13 +00:00
class HIDListenerIOKit : public IHIDListener
{
2015-09-02 19:09:13 +00:00
DeviceFinder& m_finder;
CFRunLoopRef m_listenerRunLoop;
2015-04-24 00:24:15 +00:00
IONotificationPortRef m_llPort;
2015-05-01 01:11:25 +00:00
io_iterator_t m_llAddNotif, m_llRemoveNotif;
bool m_scanningEnabled;
2015-09-02 19:09:13 +00:00
static void devicesConnectedUSBLL(HIDListenerIOKit* listener,
2015-05-01 01:11:25 +00:00
io_iterator_t iterator)
2015-04-24 00:24:15 +00:00
{
io_object_t obj;
while ((obj = IOIteratorNext(iterator)))
{
io_string_t devPath;
if (IORegistryEntryGetPath(obj, kIOServicePlane, devPath) != 0)
continue;
if (!listener->m_scanningEnabled ||
listener->m_finder._hasToken(devPath))
{
IOObjectRelease(obj);
continue;
}
IOCFPlugInInterface** devServ;
SInt32 score;
IOReturn err;
err = IOCreatePlugInInterfaceForService(obj, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &devServ, &score);
if (err != kIOReturnSuccess)
2015-09-02 19:09:13 +00:00
{
fprintf(stderr, "unable to open IOKit plugin interface\n");
return;
}
2015-04-24 00:24:15 +00:00
IOUSBDeviceInterface182 **dev;
err = (*devServ)->QueryInterface(devServ,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID182),
(LPVOID*)&dev);
if (err != kIOReturnSuccess)
2015-09-02 19:09:13 +00:00
{
fprintf(stderr, "unable to open IOKit device interface\n");
return;
}
2015-04-24 00:24:15 +00:00
UInt16 vid, pid;
(*dev)->GetDeviceVendor(dev, &vid);
(*dev)->GetDeviceProduct(dev, &pid);
UInt8 vstridx, pstridx;
(*dev)->USBGetManufacturerStringIndex(dev, &vstridx);
(*dev)->USBGetProductStringIndex(dev, &pstridx);
char vstr[128] = {0};
char pstr[128] = {0};
getUSBStringDescriptor(dev, vstridx, vstr);
getUSBStringDescriptor(dev, pstridx, pstr);
2015-11-21 02:16:15 +00:00
if (!listener->m_finder._insertToken(DeviceToken(DeviceToken::DeviceType::USB,
2015-09-02 19:09:13 +00:00
vid, pid, vstr, pstr, devPath)))
2015-05-01 01:11:25 +00:00
{
/* Matched-insertion failed; see if generic HID interface is available */
/* TODO: Do */
}
2015-04-24 00:24:15 +00:00
//printf("ADDED %08X %s\n", obj, devPath);
(*dev)->Release(dev);
IODestroyPlugInInterface(devServ);
IOObjectRelease(obj);
}
}
2015-09-02 19:09:13 +00:00
static void devicesDisconnectedUSBLL(HIDListenerIOKit* listener,
2015-05-01 01:11:25 +00:00
io_iterator_t iterator)
2015-04-24 00:24:15 +00:00
{
if (CFRunLoopGetCurrent() != listener->m_listenerRunLoop)
{
CFRunLoopPerformBlock(listener->m_listenerRunLoop, kCFRunLoopDefaultMode, ^{
2015-05-01 01:11:25 +00:00
devicesDisconnectedUSBLL(listener, iterator);
2015-04-24 00:24:15 +00:00
});
CFRunLoopWakeUp(listener->m_listenerRunLoop);
return;
}
io_object_t obj;
while ((obj = IOIteratorNext(iterator)))
{
io_string_t devPath;
if (IORegistryEntryGetPath(obj, kIOServicePlane, devPath) != 0)
continue;
listener->m_finder._removeToken(devPath);
//printf("REMOVED %08X %s\n", obj, devPath);
IOObjectRelease(obj);
}
}
public:
2015-09-02 19:09:13 +00:00
HIDListenerIOKit(DeviceFinder& finder)
: m_finder(finder)
{
2015-05-01 01:11:25 +00:00
/* Register Low-Level USB Matcher */
m_listenerRunLoop = CFRunLoopGetCurrent();
2015-04-24 00:24:15 +00:00
m_llPort = IONotificationPortCreate(kIOMasterPortDefault);
2015-05-01 01:11:25 +00:00
CFRunLoopSourceRef rlSrc = IONotificationPortGetRunLoopSource(m_llPort);
CFRunLoopAddSource(m_listenerRunLoop, rlSrc, kCFRunLoopDefaultMode);
2015-04-24 00:24:15 +00:00
CFMutableDictionaryRef matchDict = IOServiceMatching(kIOUSBDeviceClassName);
CFRetain(matchDict);
2015-05-01 01:11:25 +00:00
m_scanningEnabled = true;
2015-04-24 00:24:15 +00:00
kern_return_t llRet =
IOServiceAddMatchingNotification(m_llPort, kIOMatchedNotification, matchDict,
2015-05-01 01:11:25 +00:00
(IOServiceMatchingCallback)devicesConnectedUSBLL, this, &m_llAddNotif);
if (llRet == kIOReturnSuccess)
devicesConnectedUSBLL(this, m_llAddNotif);
2015-04-24 00:24:15 +00:00
llRet =
IOServiceAddMatchingNotification(m_llPort, kIOTerminatedNotification, matchDict,
2015-05-01 01:11:25 +00:00
(IOServiceMatchingCallback)devicesDisconnectedUSBLL, this, &m_llRemoveNotif);
if (llRet == kIOReturnSuccess)
devicesDisconnectedUSBLL(this, m_llRemoveNotif);
2015-04-24 00:24:15 +00:00
m_scanningEnabled = false;
}
2015-09-02 19:09:13 +00:00
~HIDListenerIOKit()
{
2015-11-01 00:06:56 +00:00
//CFRunLoopRemoveSource(m_listenerRunLoop, IONotificationPortGetRunLoopSource(m_llPort), kCFRunLoopDefaultMode);
2015-05-01 01:11:25 +00:00
IOObjectRelease(m_llAddNotif);
IOObjectRelease(m_llRemoveNotif);
2015-04-24 00:24:15 +00:00
IONotificationPortDestroy(m_llPort);
}
/* Automatic device scanning */
bool startScanning()
{
m_scanningEnabled = true;
return true;
}
bool stopScanning()
{
m_scanningEnabled = false;
return true;
}
/* Manual device scanning */
bool scanNow()
{
2015-05-01 01:11:25 +00:00
io_iterator_t iter;
if (IOServiceGetMatchingServices(kIOMasterPortDefault,
IOServiceMatching(kIOUSBDeviceClassName), &iter) == kIOReturnSuccess)
{
devicesConnectedUSBLL(this, iter);
IOObjectRelease(iter);
}
return true;
}
};
2015-09-02 19:09:13 +00:00
IHIDListener* IHIDListenerNew(DeviceFinder& finder)
{
2015-09-02 19:09:13 +00:00
return new HIDListenerIOKit(finder);
}
}