2015-04-21 04:02:43 +00:00
|
|
|
#include "IHIDListener.hpp"
|
|
|
|
#include "inputdev/CDeviceFinder.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>
|
|
|
|
|
2015-04-30 07:01:55 +00:00
|
|
|
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-04-21 04:02:43 +00:00
|
|
|
|
|
|
|
class CHIDListenerIOKit final : public IHIDListener
|
|
|
|
{
|
|
|
|
CDeviceFinder& m_finder;
|
|
|
|
|
2015-04-23 00:46:32 +00:00
|
|
|
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;
|
2015-04-21 04:02:43 +00:00
|
|
|
bool m_scanningEnabled;
|
|
|
|
|
2015-05-01 01:11:25 +00:00
|
|
|
static void devicesConnectedUSBLL(CHIDListenerIOKit* listener,
|
|
|
|
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)
|
|
|
|
throw std::runtime_error("unable to open IOKit plugin interface");
|
|
|
|
|
|
|
|
IOUSBDeviceInterface182 **dev;
|
|
|
|
err = (*devServ)->QueryInterface(devServ,
|
|
|
|
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID182),
|
|
|
|
(LPVOID*)&dev);
|
|
|
|
if (err != kIOReturnSuccess)
|
|
|
|
throw std::runtime_error("unable to open IOKit device interface");
|
|
|
|
|
|
|
|
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-05-01 01:11:25 +00:00
|
|
|
if (!listener->m_finder._insertToken(CDeviceToken(CDeviceToken::DEVTYPE_USB,
|
|
|
|
vid, pid, vstr, pstr, devPath)))
|
|
|
|
{
|
|
|
|
/* 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-05-01 01:11:25 +00:00
|
|
|
static void devicesDisconnectedUSBLL(CHIDListenerIOKit* listener,
|
|
|
|
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);
|
|
|
|
}
|
2015-04-21 04:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
CHIDListenerIOKit(CDeviceFinder& finder)
|
|
|
|
: m_finder(finder)
|
|
|
|
{
|
|
|
|
|
2015-05-01 01:11:25 +00:00
|
|
|
/* Register Low-Level USB Matcher */
|
2015-04-23 00:46:32 +00:00
|
|
|
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
|
|
|
|
2015-04-21 04:02:43 +00:00
|
|
|
m_scanningEnabled = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
~CHIDListenerIOKit()
|
|
|
|
{
|
2015-04-24 00:24:15 +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);
|
2015-04-21 04:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
2015-04-21 04:02:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
IHIDListener* IHIDListenerNew(CDeviceFinder& finder)
|
|
|
|
{
|
|
|
|
return new CHIDListenerIOKit(finder);
|
|
|
|
}
|
|
|
|
|
2015-04-30 07:01:55 +00:00
|
|
|
}
|