1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-30 15:52:12 +01:00

OP-958: uncrustify sources

This commit is contained in:
Oleg Semyonov 2013-05-25 01:23:02 +03:00
parent f6d2f152b9
commit d1dd944bc3
13 changed files with 596 additions and 637 deletions

View File

@ -44,8 +44,7 @@ class RawHIDWriteThread;
* The actual IO device that will be used to communicate * The actual IO device that will be used to communicate
* with the board. * with the board.
*/ */
class OPHID_EXPORT RawHID : public QIODevice class OPHID_EXPORT RawHID : public QIODevice {
{
Q_OBJECT Q_OBJECT
friend class RawHIDReadThread; friend class RawHIDReadThread;
@ -72,10 +71,10 @@ protected:
virtual qint64 bytesAvailable() const; virtual qint64 bytesAvailable() const;
virtual qint64 bytesToWrite() const; virtual qint64 bytesToWrite() const;
//! Callback from the read thread to open the device // Callback from the read thread to open the device
bool openDevice(); bool openDevice();
//! Callback from teh read thread to close the device // Callback from teh read thread to close the device
bool closeDevice(); bool closeDevice();
QString serialNumber; QString serialNumber;
@ -92,4 +91,3 @@ protected:
}; };
#endif // OPHID_H #endif // OPHID_H

View File

@ -43,7 +43,6 @@
#endif #endif
// USB // USB
#define USB_MAX_DEVICES 10 #define USB_MAX_DEVICES 10
#define USB_VID 0x20A0 #define USB_VID 0x20A0
@ -63,4 +62,3 @@
#define OPHID_ERROR_INIT -5 #define OPHID_ERROR_INIT -5
#endif // OPHID_CONST_H #endif // OPHID_CONST_H

View File

@ -37,4 +37,3 @@
#endif #endif
#endif // OPHID_GLOBAL_H #endif // OPHID_GLOBAL_H

View File

@ -41,9 +41,7 @@
#include "opHID_global.h" #include "opHID_global.h"
class OPHID_EXPORT opHID_hidapi: public QObject class OPHID_EXPORT opHID_hidapi : public QObject {
{
Q_OBJECT Q_OBJECT
public: public:
@ -78,4 +76,4 @@ signals:
void deviceUnplugged(int); void deviceUnplugged(int);
}; };
#endif #endif // ifndef OPHID_HIDAPI_H

View File

@ -46,8 +46,7 @@ class RawHIDConnection;
* Plugin will add a instance of this class to the pool, * Plugin will add a instance of this class to the pool,
* so the connection manager can use it. * so the connection manager can use it.
*/ */
class OPHID_EXPORT RawHIDConnection: public Core::IConnection class OPHID_EXPORT RawHIDConnection : public Core::IConnection {
{
Q_OBJECT Q_OBJECT
public: public:
@ -63,7 +62,10 @@ public:
virtual void suspendPolling(); virtual void suspendPolling();
virtual void resumePolling(); virtual void resumePolling();
bool deviceOpened() { return (RawHidHandle != NULL); } // Pip bool deviceOpened()
{
return RawHidHandle != NULL;
} // Pip
protected slots: protected slots:
void onDeviceConnected(); void onDeviceConnected();
@ -80,8 +82,7 @@ protected:
}; };
class OPHID_EXPORT RawHIDPlugin class OPHID_EXPORT RawHIDPlugin
: public ExtensionSystem::IPlugin : public ExtensionSystem::IPlugin {
{
Q_OBJECT Q_OBJECT
public: public:
@ -94,8 +95,6 @@ public:
private: private:
RawHIDConnection *hidConnection; RawHIDConnection *hidConnection;
USBMonitor *m_usbMonitor; USBMonitor *m_usbMonitor;
}; };
#endif // OPHID_PLUGIN_H #endif // OPHID_PLUGIN_H

View File

@ -55,7 +55,7 @@
#include <setupapi.h> #include <setupapi.h>
#include <ddk/hidsdi.h> #include <ddk/hidsdi.h>
#include <ddk/hidclass.h> #include <ddk/hidclass.h>
#endif #endif // if defined(Q_OS_MAC)
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
@ -63,11 +63,11 @@
#include <QWidget> #include <QWidget>
class USBMonitor; class USBMonitor;
class USBRegistrationWidget : public QWidget class USBRegistrationWidget : public QWidget {
{
Q_OBJECT Q_OBJECT
public: public:
USBRegistrationWidget( USBMonitor* qese ) { USBRegistrationWidget(USBMonitor *qese)
{
this->qese = qese; this->qese = qese;
} }
~USBRegistrationWidget() {} ~USBRegistrationWidget() {}
@ -102,8 +102,7 @@ struct USBPortInfo {
* A monitoring thread which will wait for device events. * A monitoring thread which will wait for device events.
*/ */
class OPHID_EXPORT USBMonitor : public QThread class OPHID_EXPORT USBMonitor : public QThread {
{
Q_OBJECT Q_OBJECT
public: public:
@ -202,7 +201,7 @@ private:
#ifdef QT_GUI_LIB #ifdef QT_GUI_LIB
USBRegistrationWidget *notificationWidget; USBRegistrationWidget *notificationWidget;
#endif #endif
#endif #endif // if defined(Q_OS_MAC)
#ifdef __APPLE__ #ifdef __APPLE__
protected: protected:
@ -210,7 +209,5 @@ protected:
void run(); void run();
#endif // __APPLE__ #endif // __APPLE__
}; };
#endif // OPHID_USBMON_H #endif // OPHID_USBMON_H

View File

@ -31,8 +31,7 @@
#include <QObject> #include <QObject>
#include "opHID_usbmon.h" #include "opHID_usbmon.h"
class OPHID_EXPORT USBSignalFilter : public QObject class OPHID_EXPORT USBSignalFilter : public QObject {
{
Q_OBJECT Q_OBJECT
private: private:
@ -52,4 +51,3 @@ public:
}; };
#endif // OPHID_USBSIGNAL_H #endif // OPHID_USBSIGNAL_H

View File

@ -44,14 +44,12 @@ static const int WRITE_TIMEOUT = 1000;
static const int WRITE_SIZE = 64; static const int WRITE_SIZE = 64;
// ********************************************************************************* // *********************************************************************************
/** /**
* Thread to desynchronize reading from the device * Thread to desynchronize reading from the device
*/ */
class RawHIDReadThread : public QThread class RawHIDReadThread : public QThread {
{
public: public:
RawHIDReadThread(RawHID *hid); RawHIDReadThread(RawHID *hid);
virtual ~RawHIDReadThread(); virtual ~RawHIDReadThread();
@ -63,7 +61,8 @@ public:
qint64 getBytesAvailable(); qint64 getBytesAvailable();
public slots: public slots:
void terminate() { void terminate()
{
m_running = false; m_running = false;
} }
@ -91,8 +90,7 @@ protected:
/** /**
* This class is nearly the same than RawHIDReadThread but for writing * This class is nearly the same than RawHIDReadThread but for writing
*/ */
class RawHIDWriteThread : public QThread class RawHIDWriteThread : public QThread {
{
public: public:
RawHIDWriteThread(RawHID *hid); RawHIDWriteThread(RawHID *hid);
virtual ~RawHIDWriteThread(); virtual ~RawHIDWriteThread();
@ -104,7 +102,8 @@ public:
qint64 getBytesToWrite(); qint64 getBytesToWrite();
public slots: public slots:
void terminate() { void terminate()
{
m_running = false; m_running = false;
} }
@ -146,9 +145,10 @@ RawHIDReadThread::~RawHIDReadThread()
{ {
m_running = false; m_running = false;
// wait for the thread to terminate // wait for the thread to terminate
if(wait(10000) == false) if (wait(10000) == false) {
qDebug() << "Cannot terminate RawHIDReadThread"; qDebug() << "Cannot terminate RawHIDReadThread";
} }
}
void RawHIDReadThread::run() void RawHIDReadThread::run()
{ {
@ -156,8 +156,7 @@ void RawHIDReadThread::run()
m_running = m_hid->openDevice(); m_running = m_hid->openDevice();
while(m_running) while (m_running) {
{
// here we use a temporary buffer so we don't need to lock // here we use a temporary buffer so we don't need to lock
// the mutex while we are reading from the device // the mutex while we are reading from the device
@ -169,20 +168,15 @@ void RawHIDReadThread::run()
int ret = hiddev->receive(hidno, buffer, READ_SIZE, READ_TIMEOUT); int ret = hiddev->receive(hidno, buffer, READ_SIZE, READ_TIMEOUT);
if(ret > 0) //read some data if (ret > 0) { // read some data
{
QMutexLocker lock(&m_readBufMtx); QMutexLocker lock(&m_readBufMtx);
// Note: Preprocess the USB packets in this OS independent code // Note: Preprocess the USB packets in this OS independent code
// First byte is report ID, second byte is the number of valid bytes // First byte is report ID, second byte is the number of valid bytes
m_readBuffer.append(&buffer[2], buffer[1]); m_readBuffer.append(&buffer[2], buffer[1]);
emit m_hid->readyRead(); emit m_hid->readyRead();
} } else if (ret == 0) { // nothing read
else if(ret == 0) //nothing read } else { // < 0 => error
{
}
else // < 0 => error
{
// TODO! make proper error handling, this only quick hack for unplug freeze // TODO! make proper error handling, this only quick hack for unplug freeze
m_running = false; m_running = false;
} }
@ -207,6 +201,7 @@ int RawHIDReadThread::getReadData(char *data, int size)
qint64 RawHIDReadThread::getBytesAvailable() qint64 RawHIDReadThread::getBytesAvailable()
{ {
QMutexLocker lock(&m_readBufMtx); QMutexLocker lock(&m_readBufMtx);
return m_readBuffer.size(); return m_readBuffer.size();
} }
@ -215,8 +210,7 @@ RawHIDWriteThread::RawHIDWriteThread(RawHID *hid)
hiddev(&hid->dev), hiddev(&hid->dev),
hidno(hid->m_deviceNo), hidno(hid->m_deviceNo),
m_running(true) m_running(true)
{ {}
}
// ********************************************************************************* // *********************************************************************************
@ -224,25 +218,25 @@ RawHIDWriteThread::~RawHIDWriteThread()
{ {
m_running = false; m_running = false;
// wait for the thread to terminate // wait for the thread to terminate
if(wait(10000) == false) if (wait(10000) == false) {
qDebug() << "Cannot terminate RawHIDReadThread"; qDebug() << "Cannot terminate RawHIDReadThread";
} }
}
void RawHIDWriteThread::run() void RawHIDWriteThread::run()
{ {
while(m_running) while (m_running) {
{
char buffer[WRITE_SIZE] = { 0 }; char buffer[WRITE_SIZE] = { 0 };
m_writeBufMtx.lock(); m_writeBufMtx.lock();
int size = qMin(WRITE_SIZE - 2, m_writeBuffer.size()); int size = qMin(WRITE_SIZE - 2, m_writeBuffer.size());
while(size <= 0) while (size <= 0) {
{
// wait on new data to write condition, the timeout // wait on new data to write condition, the timeout
// enable the thread to shutdown properly // enable the thread to shutdown properly
m_newDataToWrite.wait(&m_writeBufMtx, 200); m_newDataToWrite.wait(&m_writeBufMtx, 200);
if(!m_running) if (!m_running) {
return; return;
}
size = m_writeBuffer.size(); size = m_writeBuffer.size();
} }
@ -259,22 +253,17 @@ void RawHIDWriteThread::run()
// must hold lock through the send to know how much was sent // must hold lock through the send to know how much was sent
int ret = hiddev->send(hidno, buffer, WRITE_SIZE, WRITE_TIMEOUT); int ret = hiddev->send(hidno, buffer, WRITE_SIZE, WRITE_TIMEOUT);
if(ret > 0) if (ret > 0) {
{
// only remove the size actually written to the device // only remove the size actually written to the device
QMutexLocker lock(&m_writeBufMtx); QMutexLocker lock(&m_writeBufMtx);
m_writeBuffer.remove(0, size); m_writeBuffer.remove(0, size);
emit m_hid->bytesWritten(ret - 2); emit m_hid->bytesWritten(ret - 2);
} } else if (ret < 0) { // < 0 => error
else if(ret < 0) // < 0 => error
{
// TODO! make proper error handling, this only quick hack for unplug freeze // TODO! make proper error handling, this only quick hack for unplug freeze
m_running = false; m_running = false;
qDebug() << "Error writing to device (" << ret << ")"; qDebug() << "Error writing to device (" << ret << ")";
} } else {
else
{
qDebug() << "No data written to device ??"; qDebug() << "No data written to device ??";
} }
} }
@ -334,27 +323,27 @@ RawHID::RawHID(const QString &deviceName)
* system code is registered in that thread instead of the calling * system code is registered in that thread instead of the calling
* thread (usually UI) * thread (usually UI)
*/ */
bool RawHID::openDevice() { bool RawHID::openDevice()
{
OPHID_TRACE("IN"); OPHID_TRACE("IN");
uint32_t opened = dev.open(USB_MAX_DEVICES, USB_VID, USB_PID_ANY, USB_USAGE_PAGE, USB_USAGE); uint32_t opened = dev.open(USB_MAX_DEVICES, USB_VID, USB_PID_ANY, USB_USAGE_PAGE, USB_USAGE);
OPHID_DEBUG("opened %d devices", opened); OPHID_DEBUG("opened %d devices", opened);
for (uint32_t i = 0; i < opened; i++) { for (uint32_t i = 0; i < opened; i++) {
if (serialNumber == dev.getserial(i)) if (serialNumber == dev.getserial(i)) {
m_deviceNo = i; m_deviceNo = i;
else } else {
dev.close(i); dev.close(i);
} }
}
// Now things are opened or not (from read thread) allow the constructor to complete // Now things are opened or not (from read thread) allow the constructor to complete
m_startedMutex->unlock(); m_startedMutex->unlock();
// Leave if we have not found one device // Leave if we have not found one device
// It should be the one we are looking for // It should be the one we are looking for
if (!opened) if (!opened) {
{
OPHID_TRACE("OUT"); OPHID_TRACE("OUT");
return false; return false;
} }
@ -370,8 +359,8 @@ bool RawHID::openDevice() {
* It is uses as a callback from the read thread so that the USB * It is uses as a callback from the read thread so that the USB
* system code is unregistered from that thread\ * system code is unregistered from that thread\
*/ */
bool RawHID::closeDevice() { bool RawHID::closeDevice()
{
OPHID_TRACE("IN"); OPHID_TRACE("IN");
dev.close(m_deviceNo); dev.close(m_deviceNo);
@ -386,16 +375,18 @@ RawHID::~RawHID()
// OPHID_TRACE("IN"); // OPHID_TRACE("IN");
// If the read thread exists then the device is open // If the read thread exists then the device is open
if (m_readThread) if (m_readThread) {
close(); close();
}
// OPHID_TRACE("OUT"); // OPHID_TRACE("OUT");
} }
void RawHID::onDeviceUnplugged(int num) void RawHID::onDeviceUnplugged(int num)
{ {
if (num != m_deviceNo) if (num != m_deviceNo) {
return; return;
}
// The USB device has been unplugged // The USB device has been unplugged
close(); close();
@ -405,15 +396,20 @@ bool RawHID::open(OpenMode mode)
{ {
QMutexLocker locker(m_mutex); QMutexLocker locker(m_mutex);
if (m_deviceNo < 0) if (m_deviceNo < 0) {
return false; return false;
}
QIODevice::open(mode); QIODevice::open(mode);
Q_ASSERT(m_readThread); Q_ASSERT(m_readThread);
Q_ASSERT(m_writeThread); Q_ASSERT(m_writeThread);
if (m_readThread) m_readThread->start(); if (m_readThread) {
if (m_writeThread) m_writeThread->start(); m_readThread->start();
}
if (m_writeThread) {
m_writeThread->start();
}
return true; return true;
} }
@ -424,8 +420,7 @@ void RawHID::close()
emit aboutToClose(); emit aboutToClose();
if (m_writeThread) if (m_writeThread) {
{
OPHID_DEBUG("Terminating write thread"); OPHID_DEBUG("Terminating write thread");
m_writeThread->terminate(); m_writeThread->terminate();
delete m_writeThread; delete m_writeThread;
@ -434,8 +429,7 @@ void RawHID::close()
} }
if (m_readThread) if (m_readThread) {
{
OPHID_DEBUG("Terminating read thread"); OPHID_DEBUG("Terminating read thread");
m_readThread->terminate(); m_readThread->terminate();
delete m_readThread; delete m_readThread;
@ -459,8 +453,9 @@ qint64 RawHID::bytesAvailable() const
{ {
QMutexLocker locker(m_mutex); QMutexLocker locker(m_mutex);
if (!m_readThread) if (!m_readThread) {
return -1; return -1;
}
return m_readThread->getBytesAvailable() + QIODevice::bytesAvailable(); return m_readThread->getBytesAvailable() + QIODevice::bytesAvailable();
} }
@ -469,8 +464,9 @@ qint64 RawHID::bytesToWrite() const
{ {
QMutexLocker locker(m_mutex); QMutexLocker locker(m_mutex);
if (!m_writeThread) if (!m_writeThread) {
return -1; return -1;
}
return m_writeThread->getBytesToWrite() + QIODevice::bytesToWrite(); return m_writeThread->getBytesToWrite() + QIODevice::bytesToWrite();
} }
@ -479,8 +475,9 @@ qint64 RawHID::readData(char *data, qint64 maxSize)
{ {
QMutexLocker locker(m_mutex); QMutexLocker locker(m_mutex);
if (!m_readThread || !data) if (!m_readThread || !data) {
return -1; return -1;
}
return m_readThread->getReadData(data, maxSize); return m_readThread->getReadData(data, maxSize);
} }
@ -489,9 +486,9 @@ qint64 RawHID::writeData(const char *data, qint64 maxSize)
{ {
QMutexLocker locker(m_mutex); QMutexLocker locker(m_mutex);
if (!m_writeThread || !data) if (!m_writeThread || !data) {
return -1; return -1;
}
return m_writeThread->pushDataToWrite(data, maxSize); return m_writeThread->pushDataToWrite(data, maxSize);
} }

View File

@ -46,8 +46,9 @@ opHID_hidapi::opHID_hidapi()
handle = NULL; handle = NULL;
// Make sure hidapi lib is ready // Make sure hidapi lib is ready
if (hid_init()) if (hid_init()) {
OPHID_ERROR("Lib initialization (hidpai)."); OPHID_ERROR("Lib initialization (hidpai).");
}
OPHID_TRACE("OUT"); OPHID_TRACE("OUT");
} }
@ -82,14 +83,14 @@ opHID_hidapi::~opHID_hidapi()
int opHID_hidapi::enumerate(struct hid_device_info * *current_device_pptr, int *devices_found) int opHID_hidapi::enumerate(struct hid_device_info * *current_device_pptr, int *devices_found)
{ {
int retry = 5; int retry = 5;
*devices_found = 0; *devices_found = 0;
struct hid_device_info *current_device_ptr = NULL; struct hid_device_info *current_device_ptr = NULL;
OPHID_TRACE("IN"); OPHID_TRACE("IN");
while(retry--) while (retry--) {
{
// Enumerate // Enumerate
*current_device_pptr = hid_enumerate(USB_VID, 0x0); *current_device_pptr = hid_enumerate(USB_VID, 0x0);
@ -106,9 +107,10 @@ int opHID_hidapi::enumerate(struct hid_device_info **current_device_pptr, int *d
(*devices_found)++; (*devices_found)++;
} }
if (*devices_found) if (*devices_found) {
break; break;
} }
}
OPHID_TRACE("OUT"); OPHID_TRACE("OUT");
return OPHID_NO_ERROR; return OPHID_NO_ERROR;
@ -136,50 +138,44 @@ int opHID_hidapi::open(int max, int vid, int pid, int usage_page, int usage)
OPHID_DEBUG("max: %d, vid: 0x%X, pid: 0x%X, usage_page: %d, usage: %d.", max, vid, pid, usage_page, usage); OPHID_DEBUG("max: %d, vid: 0x%X, pid: 0x%X, usage_page: %d, usage: %d.", max, vid, pid, usage_page, usage);
if (handle) if (handle) {
{
OPHID_WARNING("HID device seems already open."); OPHID_WARNING("HID device seems already open.");
} }
// This is a hack to prevent changing all the callers (for now) // This is a hack to prevent changing all the callers (for now)
if (vid == -1) if (vid == -1) {
vid = 0; vid = 0;
if (pid == -1) }
if (pid == -1) {
pid = 0; pid = 0;
}
// If caller knows which one to look for open it right away // If caller knows which one to look for open it right away
if (vid != 0 && pid != 0) if (vid != 0 && pid != 0) {
{
handle = hid_open(vid, pid, NULL); handle = hid_open(vid, pid, NULL);
if (!handle) if (!handle) {
{
OPHID_ERROR("Unable to open device."); OPHID_ERROR("Unable to open device.");
devices_found = false; devices_found = false;
} } else {
else
{
OPHID_DEBUG("HID Device Found"); OPHID_DEBUG("HID Device Found");
OPHID_DEBUG(" type:............VID(%04hx).PID(%04hx)", vid, pid); OPHID_DEBUG(" type:............VID(%04hx).PID(%04hx)", vid, pid);
devices_found = true; devices_found = true;
} }
} } else {
else
{
// Get the list of available hid devices // Get the list of available hid devices
if (enumerate(current_device_pptr, &devices_found) != OPHID_NO_ERROR) if (enumerate(current_device_pptr, &devices_found) != OPHID_NO_ERROR) {
{
OPHID_ERROR("Error during enumeration"); OPHID_ERROR("Error during enumeration");
return 0; return 0;
} }
if (devices_found) if (devices_found) {
{
// Look for the last one in the list // Look for the last one in the list
// WARNING: for now this prevent to have devices chained // WARNING: for now this prevent to have devices chained
last_device_ptr = current_device_ptr; last_device_ptr = current_device_ptr;
while (last_device_ptr->next) while (last_device_ptr->next) {
last_device_ptr = last_device_ptr->next; last_device_ptr = last_device_ptr->next;
}
OPHID_DEBUG("Opening device VID(%04hx).PID(%04hx)", OPHID_DEBUG("Opening device VID(%04hx).PID(%04hx)",
last_device_ptr->vendor_id, last_device_ptr->vendor_id,
@ -191,15 +187,11 @@ int opHID_hidapi::open(int max, int vid, int pid, int usage_page, int usage)
hid_free_enumeration(current_device_ptr); hid_free_enumeration(current_device_ptr);
if (!handle) if (!handle) {
{
OPHID_ERROR("Unable to open device."); OPHID_ERROR("Unable to open device.");
devices_found = false; devices_found = false;
} }
} else {
}
else
{
OPHID_WARNING("Unable to find any HID device."); OPHID_WARNING("Unable to find any HID device.");
} }
} }
@ -232,20 +224,17 @@ int opHID_hidapi::receive(int num, void *buf, int len, int timeout)
int bytes_read = 0; int bytes_read = 0;
if (!buf) if (!buf) {
{
OPHID_ERROR("Unexpected parameter value (ptr)."); OPHID_ERROR("Unexpected parameter value (ptr).");
return OPHID_ERROR_POINTER; return OPHID_ERROR_POINTER;
} }
if (!len) if (!len) {
{
OPHID_ERROR("Unexpected parameter value (incorrect lenght)."); OPHID_ERROR("Unexpected parameter value (incorrect lenght).");
return OPHID_ERROR_PARAMETER; return OPHID_ERROR_PARAMETER;
} }
if (handle == NULL) if (handle == NULL) {
{
OPHID_ERROR("Handle invalid."); OPHID_ERROR("Handle invalid.");
return OPHID_ERROR_HANDLE; return OPHID_ERROR_HANDLE;
} }
@ -255,8 +244,7 @@ int opHID_hidapi::receive(int num, void *buf, int len, int timeout)
hid_read_Mtx.unlock(); hid_read_Mtx.unlock();
// hidapi lib does not expose the libusb errors. // hidapi lib does not expose the libusb errors.
if (bytes_read == -1) if (bytes_read == -1) {
{
OPHID_ERROR("hidapi: %d", bytes_read); OPHID_ERROR("hidapi: %d", bytes_read);
} }
@ -284,39 +272,33 @@ int opHID_hidapi::send(int num, void *buf, int len, int timeout)
int bytes_written = 0; int bytes_written = 0;
int retry = 5; int retry = 5;
if (!buf) if (!buf) {
{
OPHID_ERROR("Unexpected parameter value (ptr)."); OPHID_ERROR("Unexpected parameter value (ptr).");
return OPHID_ERROR_POINTER; return OPHID_ERROR_POINTER;
} }
if (!len) if (!len) {
{
OPHID_ERROR("Unexpected parameter value (incorrect lenght)."); OPHID_ERROR("Unexpected parameter value (incorrect lenght).");
return OPHID_ERROR_PARAMETER; return OPHID_ERROR_PARAMETER;
} }
if (handle == NULL) if (handle == NULL) {
{
OPHID_ERROR("Handle invalid."); OPHID_ERROR("Handle invalid.");
return OPHID_ERROR_HANDLE; return OPHID_ERROR_HANDLE;
} }
// hidapi has a timeout hardcoded to 1000ms, retry 5 times // hidapi has a timeout hardcoded to 1000ms, retry 5 times
while(retry--) while (retry--) {
{
hid_write_Mtx.lock(); hid_write_Mtx.lock();
bytes_written = hid_write(handle, (const unsigned char *)buf, len); bytes_written = hid_write(handle, (const unsigned char *)buf, len);
hid_write_Mtx.unlock(); hid_write_Mtx.unlock();
if (bytes_written >= 0) if (bytes_written >= 0) {
{
break; break;
} }
} }
// hidapi lib does not expose the libusb errors. // hidapi lib does not expose the libusb errors.
if (bytes_written < 0) if (bytes_written < 0) {
{
OPHID_ERROR("hidapi: %d", bytes_written); OPHID_ERROR("hidapi: %d", bytes_written);
} }
@ -343,14 +325,12 @@ QString opHID_hidapi::getserial(int num)
wchar_t buf[USB_MAX_STRING_SIZE]; wchar_t buf[USB_MAX_STRING_SIZE];
if (handle == NULL) if (handle == NULL) {
{
OPHID_ERROR("Handle invalid."); OPHID_ERROR("Handle invalid.");
return QString(""); return QString("");
} }
if (hid_get_serial_number_string(handle, buf, USB_MAX_STRING_SIZE) < 0) if (hid_get_serial_number_string(handle, buf, USB_MAX_STRING_SIZE) < 0) {
{
OPHID_ERROR("Unable to read serial number string."); OPHID_ERROR("Unable to read serial number string.");
return QString(""); return QString("");
} }
@ -374,11 +354,11 @@ void opHID_hidapi::close(int num)
OPHID_TRACE("IN"); OPHID_TRACE("IN");
if (handle) if (handle) {
hid_close(handle); hid_close(handle);
}
handle = NULL; handle = NULL;
OPHID_TRACE("OUT"); OPHID_TRACE("OUT");
} }

View File

@ -56,7 +56,6 @@ RawHIDConnection::RawHIDConnection()
connect(m_usbMonitor, SIGNAL(deviceDiscovered()), this, SLOT(onDeviceConnected())); connect(m_usbMonitor, SIGNAL(deviceDiscovered()), this, SLOT(onDeviceConnected()));
connect(m_usbMonitor, SIGNAL(deviceRemoved()), this, SLOT(onDeviceDisconnected())); connect(m_usbMonitor, SIGNAL(deviceRemoved()), this, SLOT(onDeviceDisconnected()));
#endif #endif
} }
@ -68,10 +67,12 @@ RawHIDConnection::RawHIDConnection()
*/ */
RawHIDConnection::~RawHIDConnection() RawHIDConnection::~RawHIDConnection()
{ {
if (RawHidHandle) if (RawHidHandle) {
if (RawHidHandle->isOpen()) if (RawHidHandle->isOpen()) {
RawHidHandle->close(); RawHidHandle->close();
} }
}
}
/** /**
@ -95,9 +96,10 @@ void RawHIDConnection::onDeviceConnected()
void RawHIDConnection::onDeviceDisconnected() void RawHIDConnection::onDeviceDisconnected()
{ {
qDebug() << "onDeviceDisconnected()"; qDebug() << "onDeviceDisconnected()";
if (enablePolling) if (enablePolling) {
emit availableDevChanged(this); emit availableDevChanged(this);
} }
}
/** /**
@ -132,13 +134,13 @@ QIODevice *RawHIDConnection::openDevice(const QString &deviceName)
{ {
OPHID_TRACE("IN"); OPHID_TRACE("IN");
if (RawHidHandle) if (RawHidHandle) {
closeDevice(deviceName); closeDevice(deviceName);
}
RawHidHandle = new RawHID(deviceName); RawHidHandle = new RawHID(deviceName);
if (!RawHidHandle) if (!RawHidHandle) {
{
OPHID_ERROR("Could not instentiate HID device"); OPHID_ERROR("Could not instentiate HID device");
} }
@ -159,8 +161,7 @@ void RawHIDConnection::closeDevice(const QString &deviceName)
Q_UNUSED(deviceName); Q_UNUSED(deviceName);
if (RawHidHandle) if (RawHidHandle) {
{
OPHID_DEBUG("Closing device"); OPHID_DEBUG("Closing device");
RawHidHandle->close(); RawHidHandle->close();
delete RawHidHandle; delete RawHidHandle;
@ -274,4 +275,3 @@ bool RawHIDPlugin::initialize(const QStringList & arguments, QString * errorStri
} }
Q_EXPORT_PLUGIN(RawHIDPlugin) Q_EXPORT_PLUGIN(RawHIDPlugin)

View File

@ -54,7 +54,6 @@ void printPortInfo(struct udev_device *dev)
udev_device_get_sysattr_value(dev, "product")); udev_device_get_sysattr_value(dev, "product"));
OPHID_DEBUG(" serial: %s", OPHID_DEBUG(" serial: %s",
udev_device_get_sysattr_value(dev, "serial")); udev_device_get_sysattr_value(dev, "serial"));
} }
@ -64,8 +63,8 @@ void printPortInfo(struct udev_device *dev)
* \note * \note
* *
*/ */
void USBMonitor::deviceEventReceived() { void USBMonitor::deviceEventReceived()
{
qDebug() << "Device event"; qDebug() << "Device event";
struct udev_device *dev; struct udev_device *dev;
@ -83,11 +82,9 @@ void USBMonitor::deviceEventReceived() {
} }
udev_device_unref(dev); udev_device_unref(dev);
} } else {
else {
printf("No Device from receive_device(). An error occured."); printf("No Device from receive_device(). An error occured.");
} }
} }
@ -114,8 +111,8 @@ USBMonitor* USBMonitor::m_instance = 0;
* \note * \note
* *
*/ */
USBMonitor::USBMonitor(QObject *parent): QThread(parent) { USBMonitor::USBMonitor(QObject *parent) : QThread(parent)
{
m_instance = this; m_instance = this;
this->context = udev_new(); this->context = udev_new();
@ -174,8 +171,9 @@ QList<USBPortInfo> USBMonitor::availableDevices()
and create a udev_device object (dev) representing it */ and create a udev_device object (dev) representing it */
path = udev_list_entry_get_name(dev_list_entry); path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(this->context, path); dev = udev_device_new_from_syspath(this->context, path);
if (QString(udev_device_get_devtype(dev)) == "usb_device") if (QString(udev_device_get_devtype(dev)) == "usb_device") {
devicesList.append(makePortInfo(dev)); devicesList.append(makePortInfo(dev));
}
udev_device_unref(dev); udev_device_unref(dev);
} }
// free the enumerator object // free the enumerator object
@ -206,9 +204,10 @@ QList<USBPortInfo> USBMonitor::availableDevices(int vid, int pid, int bcdDeviceM
foreach(USBPortInfo port, allPorts) { foreach(USBPortInfo port, allPorts) {
if ((port.vendorID == vid || vid == -1) && (port.productID == pid || pid == -1) && ((port.bcdDevice >> 8) == bcdDeviceMSB || bcdDeviceMSB == -1) && if ((port.vendorID == vid || vid == -1) && (port.productID == pid || pid == -1) && ((port.bcdDevice >> 8) == bcdDeviceMSB || bcdDeviceMSB == -1) &&
( (port.bcdDevice&0x00ff) ==bcdDeviceLSB || bcdDeviceLSB==-1)) ((port.bcdDevice & 0x00ff) == bcdDeviceLSB || bcdDeviceLSB == -1)) {
thePortsWeWant.append(port); thePortsWeWant.append(port);
} }
}
return thePortsWeWant; return thePortsWeWant;
} }
@ -242,4 +241,3 @@ USBPortInfo USBMonitor::makePortInfo(struct udev_device *dev)
return prtInfo; return prtInfo;
} }

View File

@ -41,8 +41,7 @@ void USBSignalFilter::m_deviceDiscovered(USBPortInfo port)
if ((port.vendorID == m_vid || m_vid == -1) && if ((port.vendorID == m_vid || m_vid == -1) &&
(port.productID == m_pid || m_pid == -1) && (port.productID == m_pid || m_pid == -1) &&
((port.bcdDevice >> 8) == m_boardModel || m_boardModel == -1) && ((port.bcdDevice >> 8) == m_boardModel || m_boardModel == -1) &&
((port.bcdDevice&0x00ff) ==m_runState || m_runState==-1)) ((port.bcdDevice & 0x00ff) == m_runState || m_runState == -1)) {
{
qDebug() << "USBSignalFilter emit device discovered"; qDebug() << "USBSignalFilter emit device discovered";
emit deviceDiscovered(); emit deviceDiscovered();
} }
@ -71,5 +70,3 @@ USBSignalFilter::USBSignalFilter(int vid, int pid, int boardModel, int runState)
SLOT(m_deviceDiscovered(USBPortInfo)), SLOT(m_deviceDiscovered(USBPortInfo)),
Qt::QueuedConnection); Qt::QueuedConnection);
} }