From 3a936aaf7ad11fa1920b65ce9c66bc1455c03e47 Mon Sep 17 00:00:00 2001 From: Hannes Matuschek Date: Fri, 19 Nov 2021 23:39:37 +0100 Subject: [PATCH] Fixed HID interface for MacOS. --- lib/hid_macos.cc | 43 +++++++++++++++++++------------------------ lib/hid_macos.hh | 6 ++++-- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/lib/hid_macos.cc b/lib/hid_macos.cc index 69c69762..76f26d23 100644 --- a/lib/hid_macos.cc +++ b/lib/hid_macos.cc @@ -3,10 +3,10 @@ #include #include #include -#include +#include -HIDevice::HIDevice(int vid, int pid, QObject *parent) +HIDevice::HIDevice(int vid, int pid, const ErrorStack &err, QObject *parent) : QObject(parent), _dev(nullptr) { // Create the USB HID Manager. @@ -35,8 +35,8 @@ HIDevice::HIDevice(int vid, int pid, QObject *parent) // Open the HID mangager IOReturn IOReturn = IOHIDManagerOpen(_HIDManager, kIOHIDOptionsTypeNone); if (IOReturn != kIOReturnSuccess) { - errMsg() << "Cannot open HID manager for USB device " - << QString::number(vid, 16) << ":" << QString::number(pid,16) << "."; + errMsg(err) << "Cannot open HID manager for USB device " + << QString::number(vid, 16) << ":" << QString::number(pid,16) << "."; IOHIDManagerUnscheduleFromRunLoop(_HIDManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); _HIDManager = nullptr; return; @@ -50,8 +50,8 @@ HIDevice::HIDevice(int vid, int pid, QObject *parent) usleep(10000); } - errMsg() << "Cannot open USB device " - << QString::number(vid, 16) << ":" << QString::number(pid,16) << "."; + errMsg(err) << "Cannot open USB device " + << QString::number(vid, 16) << ":" << QString::number(pid,16) << "."; IOHIDManagerUnscheduleFromRunLoop(_HIDManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); IOHIDManagerClose(_HIDManager, kIOHIDOptionsTypeNone); _HIDManager = nullptr; @@ -76,7 +76,8 @@ HIDevice::isOpen() const { // Terminate in case of errors. // bool -HIDevice::hid_send_recv(const unsigned char *data, unsigned nbytes, unsigned char *rdata, unsigned rlength) +HIDevice::hid_send_recv(const unsigned char *data, unsigned nbytes, + unsigned char *rdata, unsigned rlength, const ErrorStack &err) { unsigned char buf[42]; unsigned k; @@ -102,7 +103,7 @@ HIDevice::hid_send_recv(const unsigned char *data, unsigned nbytes, unsigned cha // Write to HID device. result = IOHIDDeviceSetReport(_dev, kIOHIDReportTypeOutput, 0, buf, sizeof(buf)); if (result != kIOReturnSuccess) { - errMsg() << "HID output error: " << result << "!"; + errMsg(err) << "HID output error: " << result << "!"; return false; } @@ -115,27 +116,27 @@ HIDevice::hid_send_recv(const unsigned char *data, unsigned nbytes, unsigned cha retrycount++; if (retrycount<100) goto again; - errMsg() << "HID IO error: Exceeded max. retry count."; + errMsg(err) << "HID IO error: Exceeded max. retry count."; return false; } } usleep(100); if (_nbytes_received != sizeof(_receive_buf)) { - errMsg() << "Short read: " << _nbytes_received << " bytes instead of " - << (int)sizeof(_receive_buf) << "!"; + errMsg(err) << "Short read: " << _nbytes_received << " bytes instead of " + << (int)sizeof(_receive_buf) << "!"; return false; } if ((_receive_buf[0] != 3) || (_receive_buf[1] != 0) || (_receive_buf[3] != 0)) { - errMsg() << "Incorrect reply. Expected {3,0,0}, got {" << int(_receive_buf[0]) << "," - << int(_receive_buf[1]) << "," << int(_receive_buf[3]) << "}."; + errMsg(err) << "Incorrect reply. Expected {3,0,0}, got {" << int(_receive_buf[0]) << "," + << int(_receive_buf[1]) << "," << int(_receive_buf[3]) << "}."; return false; } if (_receive_buf[2] != rlength) { - errMsg() << "Incorrect reply length " << (int)_receive_buf[2] - << ", expected " << rlength << "!"; + errMsg(err) << "Incorrect reply length " << (int)_receive_buf[2] + << ", expected " << rlength << "!"; return false; } @@ -158,17 +159,13 @@ HIDevice::callback_input(void *context, IOReturn result, void *sender, IOHIDRepo HIDevice *self = reinterpret_cast(context); if (result != kIOReturnSuccess) { - self->pushErrorMessage( - ErrorStack::Message( - __FILE__, __LINE__, QString("HID input error: %1!").arg(result))); + logError() << "HID input error: " << result << "."; self->close(); return; } if (nbytes > CFIndex(sizeof(self->_receive_buf))) { - self->pushErrorMessage( - ErrorStack::Message( - __FILE__, __LINE__, QString("Too large HID input: %1 bytes!").arg((int)nbytes))); + logError() << "Too large HID input: " << (int)nbytes << " bytes!"; self->close(); return; } @@ -191,9 +188,7 @@ HIDevice::callback_open(void *context, IOReturn result, void *sender, IOHIDDevic IOReturn o = IOHIDDeviceOpen(deviceRef, kIOHIDOptionsTypeSeizeDevice); if (o != kIOReturnSuccess) { - self->pushErrorMessage( - ErrorStack::Message( - __FILE__, __LINE__, "Cannot open HID device!")); + logError() << "Cannot open HID device!"; return; } diff --git a/lib/hid_macos.hh b/lib/hid_macos.hh index 9f6d8184..2104fb6e 100644 --- a/lib/hid_macos.hh +++ b/lib/hid_macos.hh @@ -13,7 +13,7 @@ class HIDevice: public QObject public: /** Opens a connection to the device with given vendor and product ID. */ - HIDevice(int vid, int pid, QObject *parent=nullptr); + HIDevice(int vid, int pid, const ErrorStack &err=ErrorStack(), QObject *parent=nullptr); /** Destrutor. */ virtual ~HIDevice(); @@ -25,7 +25,9 @@ public: * @param nbytes The number of bytes to send. * @param rdata Pointer to receive buffer. * @param rlength Size of receive buffer. */ - bool hid_send_recv(const unsigned char *data, unsigned nbytes, unsigned char *rdata, unsigned rlength); + bool hid_send_recv(const unsigned char *data, unsigned nbytes, + unsigned char *rdata, unsigned rlength, + const ErrorStack &err=ErrorStack()); /** Close connection to device. */ void close();