mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-30 15:52:12 +01:00
OP GCS - created USB connect/disconnect signals for windows using the system API.
Need to call setUpNotifications first!!! git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2915 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
49170f0628
commit
0c2a563c34
@ -31,7 +31,6 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <QObject>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include "rawhid_global.h"
|
||||
@ -46,7 +45,11 @@
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#elif defined(Q_OS_WIN32)
|
||||
#define _WIN32_WINNT 0x0500
|
||||
#define _WIN32_WINDOWS 0x0500
|
||||
#define WINVER 0x0500
|
||||
#include <windows.h>
|
||||
#include <dbt.h>
|
||||
#include <setupapi.h>
|
||||
#include <ddk/hidsdi.h>
|
||||
#include <ddk/hidclass.h>
|
||||
@ -76,9 +79,11 @@
|
||||
#elif defined(Q_OS_WIN32)
|
||||
|
||||
typedef struct hid_struct hid_t;
|
||||
|
||||
struct hid_struct
|
||||
{
|
||||
HANDLE handle;
|
||||
int open;
|
||||
struct hid_struct *prev;
|
||||
struct hid_struct *next;
|
||||
};
|
||||
@ -87,24 +92,92 @@
|
||||
|
||||
// ************
|
||||
|
||||
//this all stuff was added by ME
|
||||
|
||||
struct USBPortInfo {
|
||||
QString friendName; ///< Friendly name.
|
||||
QString physName;
|
||||
QString enumName; ///< It seems its the only one with meaning
|
||||
int vendorID; ///< Vendor ID.
|
||||
int productID; ///< Product ID
|
||||
};
|
||||
#ifdef Q_OS_WIN
|
||||
#ifdef QT_GUI_LIB
|
||||
#include <QWidget>
|
||||
class pjrc_rawhid;
|
||||
|
||||
class USBRegistrationWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
USBRegistrationWidget( pjrc_rawhid* qese ) {
|
||||
this->qese = qese;
|
||||
}
|
||||
~USBRegistrationWidget( ) { }
|
||||
|
||||
protected:
|
||||
pjrc_rawhid* qese;
|
||||
bool winEvent( MSG* message, long* result );
|
||||
};
|
||||
#endif // QT_GUI_LIB
|
||||
#endif // Q_OS_WIN
|
||||
|
||||
class RAWHID_EXPORT pjrc_rawhid: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
#ifdef Q_OS_WIN
|
||||
public:
|
||||
LRESULT onDeviceChangeWin( WPARAM wParam, LPARAM lParam );
|
||||
|
||||
private:
|
||||
/*!
|
||||
* Get specific property from registry.
|
||||
* \param devInfo pointer to the device information set that contains the interface
|
||||
* and its underlying device. Returned by SetupDiGetClassDevs() function.
|
||||
* \param devData pointer to an SP_DEVINFO_DATA structure that defines the device instance.
|
||||
* this is returned by SetupDiGetDeviceInterfaceDetail() function.
|
||||
* \param property registry property. One of defined SPDRP_* constants.
|
||||
* \return property string.
|
||||
*/
|
||||
static QString getDeviceProperty(HDEVINFO devInfo, PSP_DEVINFO_DATA devData, DWORD property);
|
||||
|
||||
static bool getDeviceDetailsWin( USBPortInfo* portInfo, HDEVINFO devInfo,
|
||||
PSP_DEVINFO_DATA devData, WPARAM wParam = DBT_DEVICEARRIVAL );
|
||||
bool matchAndDispatchChangedDevice(const QString & deviceID, const GUID & guid, WPARAM wParam);
|
||||
#ifdef QT_GUI_LIB
|
||||
USBRegistrationWidget* notificationWidget;
|
||||
#endif
|
||||
#endif /*Q_OS_WIN*/
|
||||
public:
|
||||
pjrc_rawhid();
|
||||
~pjrc_rawhid();
|
||||
|
||||
/*!
|
||||
Enable event-driven notifications of board discovery/removal.
|
||||
*/
|
||||
void setUpNotifications( );
|
||||
int open(int max, int vid, int pid, int usage_page, int usage);
|
||||
int receive(int num, void *buf, int len, int timeout);
|
||||
void close(int num);
|
||||
int send(int num, void *buf, int len, int timeout);
|
||||
QString getserial(int num);
|
||||
void mytest(int num);
|
||||
|
||||
signals:
|
||||
void deviceUnplugged(int num);
|
||||
/*!
|
||||
A new device has been connected to the system.
|
||||
|
||||
setUpNotifications() must be called first to enable event-driven device notifications.
|
||||
Currently only implemented on Windows and OS X.
|
||||
\param info The device that has been discovered.
|
||||
*/
|
||||
void deviceDiscovered( const USBPortInfo & info );
|
||||
/*!
|
||||
A device has been disconnected from the system.
|
||||
|
||||
setUpNotifications() must be called first to enable event-driven device notifications.
|
||||
Currently only implemented on Windows and OS X.
|
||||
\param info The device that was disconnected.
|
||||
*/
|
||||
void deviceRemoved( const USBPortInfo & info );
|
||||
private:
|
||||
#if defined( Q_OS_MAC)
|
||||
|
||||
@ -135,7 +208,7 @@ private:
|
||||
hid_t * get_hid(int num);
|
||||
void free_all_hid(void);
|
||||
void hid_close(hid_t *hid);
|
||||
void print_win32_err(DWORD err);
|
||||
void print_win32_err(void);
|
||||
|
||||
#endif
|
||||
};
|
||||
|
@ -38,13 +38,18 @@
|
||||
/* See: http://msdn.microsoft.com/en-us/library/ms794141.aspx */
|
||||
|
||||
#include "pjrc_rawhid.h"
|
||||
|
||||
#include <QMetaType>
|
||||
#include <QString>
|
||||
|
||||
#include <initguid.h>
|
||||
#define printf qDebug
|
||||
|
||||
pjrc_rawhid::pjrc_rawhid()
|
||||
{
|
||||
if( !QMetaType::isRegistered( QMetaType::type("USVPortInfo") ) )
|
||||
qRegisterMetaType<USBPortInfo>("USBPortInfo");
|
||||
#if (defined QT_GUI_LIB)
|
||||
notificationWidget = 0;
|
||||
#endif // Q_OS_WIN
|
||||
first_hid = NULL;
|
||||
last_hid = NULL;
|
||||
rx_event = NULL;
|
||||
@ -53,6 +58,10 @@ pjrc_rawhid::pjrc_rawhid()
|
||||
|
||||
pjrc_rawhid::~pjrc_rawhid()
|
||||
{
|
||||
#if (defined QT_GUI_LIB)
|
||||
if( notificationWidget )
|
||||
delete notificationWidget;
|
||||
#endif
|
||||
}
|
||||
|
||||
// open - open 1 or more devices
|
||||
@ -118,6 +127,18 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
continue;
|
||||
}
|
||||
|
||||
<<<<<<< .mine
|
||||
h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
// I get ERROR_ACCESS_DENIED with most/all my input devices (mice/trackballs/tablet).
|
||||
// Let's not log it :)
|
||||
if (GetLastError() == ERROR_ACCESS_DENIED)
|
||||
{
|
||||
free(details);
|
||||
continue;
|
||||
}
|
||||
=======
|
||||
h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
||||
if (h == INVALID_HANDLE_VALUE)
|
||||
{
|
||||
@ -130,10 +151,17 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
free(details);
|
||||
continue;
|
||||
}
|
||||
>>>>>>> .r2914
|
||||
|
||||
<<<<<<< .mine
|
||||
// qDebug wipes the GetLastError() it seems, so do that after print_win32_err().
|
||||
print_win32_err();
|
||||
qDebug() << "Problem opening handle, path: " << QString().fromWCharArray(details->DevicePath);
|
||||
=======
|
||||
// qDebug wipes the GetLastError() it seems, so do that after print_win32_err().
|
||||
print_win32_err(err);
|
||||
qDebug() << "Problem opening handle, path: " << QString().fromWCharArray(details->DevicePath);
|
||||
>>>>>>> .r2914
|
||||
|
||||
free(details);
|
||||
continue;
|
||||
@ -170,6 +198,9 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
continue;
|
||||
}
|
||||
|
||||
<<<<<<< .mine
|
||||
qDebug("Open: Handle address: %li for num: %i", (long int) h, count);
|
||||
=======
|
||||
// COMMTIMEOUTS CommTimeouts;
|
||||
// CommTimeouts.ReadIntervalTimeout = 100; // 100ms
|
||||
// CommTimeouts.ReadTotalTimeoutConstant = 5; // ms
|
||||
@ -183,9 +214,16 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
// }
|
||||
|
||||
qDebug("Open: Handle address: %li for num: %i", (long int) h, count);
|
||||
>>>>>>> .r2914
|
||||
|
||||
<<<<<<< .mine
|
||||
hid->handle = h;
|
||||
hid->open = 1;
|
||||
add_hid(hid);
|
||||
=======
|
||||
hid->handle = h;
|
||||
add_hid(hid);
|
||||
>>>>>>> .r2914
|
||||
|
||||
count++;
|
||||
if (count >= max) return count;
|
||||
@ -208,11 +246,16 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
|
||||
OVERLAPPED ov;
|
||||
DWORD n;
|
||||
|
||||
<<<<<<< .mine
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid || !hid->open) return -1;
|
||||
=======
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid)
|
||||
return -1;
|
||||
if (!hid->handle)
|
||||
return -1;
|
||||
>>>>>>> .r2914
|
||||
|
||||
EnterCriticalSection(&rx_mutex);
|
||||
|
||||
@ -221,6 +264,16 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
|
||||
memset(&ov, 0, sizeof(ov));
|
||||
ov.hEvent = rx_event;
|
||||
|
||||
<<<<<<< .mine
|
||||
if (!ReadFile(hid->handle, buf, len, NULL, &ov))
|
||||
{
|
||||
if (GetLastError() != ERROR_IO_PENDING)
|
||||
{
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
=======
|
||||
if (!ReadFile(hid->handle, buf, len, NULL, &ov))
|
||||
{
|
||||
DWORD err = GetLastError();
|
||||
@ -240,7 +293,24 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
>>>>>>> .r2914
|
||||
|
||||
<<<<<<< .mine
|
||||
DWORD r = WaitForSingleObject(rx_event, timeout);
|
||||
if (r == WAIT_TIMEOUT)
|
||||
{
|
||||
CancelIo(hid->handle);
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return 0;
|
||||
}
|
||||
if (r != WAIT_OBJECT_0)
|
||||
{
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
=======
|
||||
DWORD r = WaitForSingleObject(rx_event, timeout);
|
||||
if (r == WAIT_TIMEOUT)
|
||||
{
|
||||
@ -256,7 +326,16 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
>>>>>>> .r2914
|
||||
|
||||
<<<<<<< .mine
|
||||
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE))
|
||||
{
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
=======
|
||||
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE))
|
||||
{
|
||||
DWORD err = GetLastError();
|
||||
@ -273,6 +352,7 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
>>>>>>> .r2914
|
||||
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
|
||||
@ -298,11 +378,16 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
|
||||
OVERLAPPED ov;
|
||||
DWORD n, r;
|
||||
|
||||
<<<<<<< .mine
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid || !hid->open) return -1;
|
||||
=======
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid)
|
||||
return -1;
|
||||
if (!hid->handle)
|
||||
return -1;
|
||||
>>>>>>> .r2914
|
||||
|
||||
// qDebug("Send: Handle address: %li for num: %i", (long int) hid->handle, num);
|
||||
|
||||
@ -315,6 +400,36 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
|
||||
|
||||
// qDebug("Trying to write %u bytes. First %x second %x",len, *((char *) buf), *((char *)buf + 1));
|
||||
|
||||
<<<<<<< .mine
|
||||
if (!WriteFile(hid->handle, buf, len, NULL, &ov))
|
||||
{
|
||||
DWORD err = GetLastError();
|
||||
if ( err == ERROR_SUCCESS || err == ERROR_IO_PENDING )
|
||||
{
|
||||
// qDebug("Waiting for write to finish");
|
||||
r = WaitForSingleObject(tx_event, timeout);
|
||||
if (r == WAIT_TIMEOUT)
|
||||
{
|
||||
CancelIo(hid->handle);
|
||||
LeaveCriticalSection(&tx_mutex);
|
||||
return 0;
|
||||
}
|
||||
if (r != WAIT_OBJECT_0)
|
||||
{
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&tx_mutex);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// qDebug("Error writing to file");
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&tx_mutex);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
=======
|
||||
if (!WriteFile(hid->handle, buf, len, NULL, &ov))
|
||||
{
|
||||
DWORD err = GetLastError();
|
||||
@ -353,7 +468,15 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
>>>>>>> .r2914
|
||||
|
||||
<<<<<<< .mine
|
||||
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE))
|
||||
{
|
||||
qDebug("Problem getting overlapped result");
|
||||
print_win32_err();
|
||||
}
|
||||
=======
|
||||
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE))
|
||||
{
|
||||
DWORD err = GetLastError();
|
||||
@ -369,6 +492,7 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
>>>>>>> .r2914
|
||||
|
||||
LeaveCriticalSection(&tx_mutex);
|
||||
|
||||
@ -378,12 +502,27 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
|
||||
|
||||
QString pjrc_rawhid::getserial(int num)
|
||||
{
|
||||
<<<<<<< .mine
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid || !hid->open)
|
||||
return "";
|
||||
=======
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid)
|
||||
return "";
|
||||
>>>>>>> .r2914
|
||||
if (!hid->handle)
|
||||
return "";
|
||||
|
||||
<<<<<<< .mine
|
||||
// Should we do some "critical section" stuff here??
|
||||
char temp[126];
|
||||
if (!HidD_GetSerialNumberString(hid->handle, temp, sizeof(temp)))
|
||||
{
|
||||
print_win32_err();
|
||||
return QString("Error");
|
||||
}
|
||||
=======
|
||||
// Should we do some "critical section" stuff here??
|
||||
char temp[126];
|
||||
if (!HidD_GetSerialNumberString(hid->handle, temp, sizeof(temp)))
|
||||
@ -400,6 +539,7 @@ QString pjrc_rawhid::getserial(int num)
|
||||
|
||||
return QString("Error");
|
||||
}
|
||||
>>>>>>> .r2914
|
||||
|
||||
return QString().fromUtf16((ushort*)temp,-1);
|
||||
}
|
||||
@ -413,13 +553,31 @@ QString pjrc_rawhid::getserial(int num)
|
||||
//
|
||||
void pjrc_rawhid::close(int num)
|
||||
{
|
||||
<<<<<<< .mine
|
||||
hid_t *hid = get_hid(num);
|
||||
if (hid && hid->open)
|
||||
hid_close(hid);
|
||||
=======
|
||||
hid_close(get_hid(num));
|
||||
>>>>>>> .r2914
|
||||
}
|
||||
|
||||
void pjrc_rawhid::add_hid(hid_t *h)
|
||||
{
|
||||
if (!h) return;
|
||||
|
||||
<<<<<<< .mine
|
||||
if (!first_hid || !last_hid)
|
||||
{
|
||||
first_hid = last_hid = h;
|
||||
h->next = h->prev = NULL;
|
||||
return;
|
||||
}
|
||||
last_hid->next = h;
|
||||
h->prev = last_hid;
|
||||
h->next = NULL;
|
||||
last_hid = h;
|
||||
=======
|
||||
if (!first_hid || !last_hid)
|
||||
{
|
||||
first_hid = last_hid = h;
|
||||
@ -431,6 +589,7 @@ void pjrc_rawhid::add_hid(hid_t *h)
|
||||
h->prev = last_hid;
|
||||
h->next = NULL;
|
||||
last_hid = h;
|
||||
>>>>>>> .r2914
|
||||
}
|
||||
|
||||
hid_t * pjrc_rawhid::get_hid(int num)
|
||||
@ -461,17 +620,157 @@ void pjrc_rawhid::hid_close(hid_t *hid)
|
||||
if (!hid) return;
|
||||
if (!hid->handle) return;
|
||||
|
||||
<<<<<<< .mine
|
||||
if (hid->handle)
|
||||
{
|
||||
CloseHandle(hid->handle);
|
||||
hid->handle = NULL;
|
||||
}
|
||||
=======
|
||||
CloseHandle(hid->handle);
|
||||
hid->handle = NULL;
|
||||
>>>>>>> .r2914
|
||||
}
|
||||
|
||||
void pjrc_rawhid::print_win32_err(DWORD err)
|
||||
{
|
||||
<<<<<<< .mine
|
||||
char buf[256];
|
||||
char temp[256];
|
||||
DWORD err;
|
||||
=======
|
||||
char buf[256];
|
||||
char temp[256];
|
||||
>>>>>>> .r2914
|
||||
|
||||
<<<<<<< .mine
|
||||
err = GetLastError();
|
||||
|
||||
=======
|
||||
>>>>>>> .r2914
|
||||
//FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, (WCHAR*)buf, sizeof(buf), NULL);
|
||||
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (WCHAR*)buf, sizeof(buf), NULL);
|
||||
WideCharToMultiByte( CP_ACP, 0, (WCHAR*)buf, sizeof(buf), temp, sizeof(temp), NULL, NULL );
|
||||
printf("err %ld: %s\n", err, temp);
|
||||
}
|
||||
// see http://msdn.microsoft.com/en-us/library/ms791134.aspx for list of GUID classes
|
||||
#ifndef GUID_DEVCLASS_PORTS
|
||||
DEFINE_GUID(GUID_DEVCLASS_PORTS, 0x4d1e55b2, 0xf16f, 0x11cf, 0x88, 0xcb, 0x00, 0x11, 0x11, 0x00, 0x00, 0x30);
|
||||
#endif
|
||||
|
||||
/* Gordon Schumacher's macros for TCHAR -> QString conversions and vice versa */
|
||||
#ifdef UNICODE
|
||||
#define QStringToTCHAR(x) (wchar_t*) x.utf16()
|
||||
#define PQStringToTCHAR(x) (wchar_t*) x->utf16()
|
||||
#define TCHARToQString(x) QString::fromUtf16((ushort*)(x))
|
||||
#define TCHARToQStringN(x,y) QString::fromUtf16((ushort*)(x),(y))
|
||||
#else
|
||||
#define QStringToTCHAR(x) x.local8Bit().constData()
|
||||
#define PQStringToTCHAR(x) x->local8Bit().constData()
|
||||
#define TCHARToQString(x) QString::fromLocal8Bit((x))
|
||||
#define TCHARToQStringN(x,y) QString::fromLocal8Bit((x),(y))
|
||||
#endif /*UNICODE*/
|
||||
|
||||
void pjrc_rawhid::setUpNotifications( )
|
||||
{
|
||||
#ifdef QT_GUI_LIB
|
||||
if(notificationWidget)
|
||||
return;
|
||||
notificationWidget = new USBRegistrationWidget(this);
|
||||
|
||||
DEV_BROADCAST_DEVICEINTERFACE dbh;
|
||||
ZeroMemory(&dbh, sizeof(dbh));
|
||||
dbh.dbcc_size = sizeof(dbh);
|
||||
dbh.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
|
||||
CopyMemory(&dbh.dbcc_classguid, &GUID_DEVCLASS_PORTS, sizeof(GUID));
|
||||
if( RegisterDeviceNotification( notificationWidget->winId( ), &dbh, DEVICE_NOTIFY_WINDOW_HANDLE ) == NULL)
|
||||
qWarning() << "RegisterDeviceNotification failed:" << GetLastError();
|
||||
// setting up notifications doesn't tell us about devices already connected
|
||||
// so get those manually
|
||||
#else
|
||||
qWarning("GUI not enabled - can't register for device notifications.");
|
||||
#endif // QT_GUI_LIB
|
||||
}
|
||||
LRESULT pjrc_rawhid::onDeviceChangeWin( WPARAM wParam, LPARAM lParam )
|
||||
{
|
||||
if ( DBT_DEVICEARRIVAL == wParam || DBT_DEVICEREMOVECOMPLETE == wParam )
|
||||
{
|
||||
PDEV_BROADCAST_HDR pHdr = (PDEV_BROADCAST_HDR)lParam;
|
||||
if( pHdr->dbch_devicetype == DBT_DEVTYP_DEVICEINTERFACE )
|
||||
{
|
||||
PDEV_BROADCAST_DEVICEINTERFACE pDevInf = (PDEV_BROADCAST_DEVICEINTERFACE)pHdr;
|
||||
// delimiters are different across APIs...change to backslash. ugh.
|
||||
QString deviceID = TCHARToQString(pDevInf->dbcc_name).toUpper().replace("#", "\\");
|
||||
matchAndDispatchChangedDevice(deviceID, GUID_DEVCLASS_PORTS, wParam);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#ifdef QT_GUI_LIB
|
||||
bool USBRegistrationWidget::winEvent( MSG* message, long* result )
|
||||
{
|
||||
if ( message->message == WM_DEVICECHANGE ) {
|
||||
qese->onDeviceChangeWin( message->wParam, message->lParam );
|
||||
*result = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
#endif
|
||||
bool pjrc_rawhid::matchAndDispatchChangedDevice(const QString & deviceID, const GUID & guid, WPARAM wParam)
|
||||
{
|
||||
bool rv = false;
|
||||
DWORD dwFlag = (DBT_DEVICEARRIVAL == wParam) ? DIGCF_PRESENT : DIGCF_ALLCLASSES;
|
||||
HDEVINFO devInfo;
|
||||
if( (devInfo = SetupDiGetClassDevs(&guid,NULL,NULL,dwFlag)) != INVALID_HANDLE_VALUE )
|
||||
{
|
||||
SP_DEVINFO_DATA spDevInfoData;
|
||||
spDevInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
|
||||
for(int i=0; SetupDiEnumDeviceInfo(devInfo, i, &spDevInfoData); i++)
|
||||
{
|
||||
DWORD nSize=0 ;
|
||||
TCHAR buf[MAX_PATH];
|
||||
if ( SetupDiGetDeviceInstanceId(devInfo, &spDevInfoData, buf, MAX_PATH, &nSize) &&
|
||||
deviceID.contains(TCHARToQString(buf))) // we found a match
|
||||
{
|
||||
rv = true;
|
||||
USBPortInfo info;
|
||||
//info.productID = info.vendorID = 0;
|
||||
getDeviceDetailsWin( &info, devInfo, &spDevInfoData, wParam );
|
||||
if( wParam == DBT_DEVICEARRIVAL )
|
||||
emit deviceDiscovered(info);
|
||||
else if( wParam == DBT_DEVICEREMOVECOMPLETE )
|
||||
emit deviceRemoved(info);
|
||||
break;
|
||||
}
|
||||
}
|
||||
SetupDiDestroyDeviceInfoList(devInfo);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
bool pjrc_rawhid::getDeviceDetailsWin( USBPortInfo* portInfo, HDEVINFO devInfo, PSP_DEVINFO_DATA devData, WPARAM wParam )
|
||||
{
|
||||
portInfo->friendName = getDeviceProperty(devInfo, devData, SPDRP_FRIENDLYNAME);
|
||||
if( wParam == DBT_DEVICEARRIVAL)
|
||||
portInfo->physName = getDeviceProperty(devInfo, devData, SPDRP_PHYSICAL_DEVICE_OBJECT_NAME);
|
||||
portInfo->enumName = getDeviceProperty(devInfo, devData, SPDRP_ENUMERATOR_NAME);
|
||||
QString hardwareIDs = getDeviceProperty(devInfo, devData, SPDRP_HARDWAREID);
|
||||
QRegExp idRx("VID_(\\w+)&PID_(\\w+)");
|
||||
if( hardwareIDs.toUpper().contains(idRx) )
|
||||
{
|
||||
bool dummy;
|
||||
portInfo->vendorID = idRx.cap(1).toInt(&dummy, 16);
|
||||
portInfo->productID = idRx.cap(2).toInt(&dummy, 16);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
QString pjrc_rawhid::getDeviceProperty(HDEVINFO devInfo, PSP_DEVINFO_DATA devData, DWORD property)
|
||||
{
|
||||
DWORD buffSize = 0;
|
||||
SetupDiGetDeviceRegistryProperty(devInfo, devData, property, NULL, NULL, 0, & buffSize);
|
||||
BYTE* buff = new BYTE[buffSize];
|
||||
SetupDiGetDeviceRegistryProperty(devInfo, devData, property, NULL, buff, buffSize, NULL);
|
||||
QString result = TCHARToQString(buff);
|
||||
delete [] buff;
|
||||
return result;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user