mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-18 03:52:11 +01:00
Cleaned up and made the pjrc modules purely C++ - they were a mix of C++ and static C functions and data :( ... although NOT altered the MAC code - someone who knows MAC code needs to do that.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2906 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
e3b6a29bfd
commit
d38c44f529
@ -35,18 +35,105 @@
|
||||
#include <QString>
|
||||
#include "rawhid_global.h"
|
||||
|
||||
#if defined( Q_OS_MAC)
|
||||
|
||||
// todo:
|
||||
|
||||
#elif defined(Q_OS_UNIX)
|
||||
//#elif defined(Q_OS_LINUX)
|
||||
#include <usb.h>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#elif defined(Q_OS_WIN32)
|
||||
#include <windows.h>
|
||||
#include <setupapi.h>
|
||||
#include <ddk/hidsdi.h>
|
||||
#include <ddk/hidclass.h>
|
||||
#endif
|
||||
|
||||
// ************
|
||||
|
||||
#if defined( Q_OS_MAC)
|
||||
|
||||
// todo:
|
||||
|
||||
#elif defined(Q_OS_UNIX)
|
||||
//#elif defined(Q_OS_LINUX)
|
||||
|
||||
typedef struct hid_struct hid_t;
|
||||
struct hid_struct
|
||||
{
|
||||
usb_dev_handle *usb;
|
||||
int open;
|
||||
int iface;
|
||||
int ep_in;
|
||||
int ep_out;
|
||||
struct hid_struct *prev;
|
||||
struct hid_struct *next;
|
||||
};
|
||||
|
||||
#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;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
// ************
|
||||
|
||||
class RAWHID_EXPORT pjrc_rawhid
|
||||
{
|
||||
|
||||
public:
|
||||
pjrc_rawhid();
|
||||
~pjrc_rawhid();
|
||||
|
||||
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);
|
||||
private:
|
||||
|
||||
private:
|
||||
#if defined( Q_OS_MAC)
|
||||
|
||||
// todo:
|
||||
|
||||
#elif defined(Q_OS_UNIX)
|
||||
//#elif defined(Q_OS_LINUX)
|
||||
|
||||
hid_t *first_hid;
|
||||
hid_t *last_hid;
|
||||
|
||||
void add_hid(hid_t *h);
|
||||
hid_t * get_hid(int num);
|
||||
void free_all_hid(void);
|
||||
void hid_close(hid_t *hid);
|
||||
int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end);
|
||||
|
||||
#elif defined(Q_OS_WIN32)
|
||||
|
||||
hid_t *first_hid;
|
||||
hid_t *last_hid;
|
||||
HANDLE rx_event;
|
||||
HANDLE tx_event;
|
||||
CRITICAL_SECTION rx_mutex;
|
||||
CRITICAL_SECTION tx_mutex;
|
||||
|
||||
void add_hid(hid_t *h);
|
||||
hid_t * get_hid(int num);
|
||||
void free_all_hid(void);
|
||||
void hid_close(hid_t *hid);
|
||||
void print_win32_err(void);
|
||||
|
||||
#endif
|
||||
};
|
||||
|
||||
#endif // PJRC_RAWHID_H
|
||||
#endif
|
||||
|
@ -85,6 +85,10 @@ pjrc_rawhid::pjrc_rawhid()
|
||||
last_hid = NULL;
|
||||
}
|
||||
|
||||
pjrc_rawhid::~pjrc_rawhid()
|
||||
{
|
||||
}
|
||||
|
||||
// open - open 1 or more devices
|
||||
//
|
||||
// Inputs:
|
||||
|
@ -37,28 +37,6 @@
|
||||
*/
|
||||
|
||||
#include "pjrc_rawhid.h"
|
||||
#include <usb.h>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
|
||||
typedef struct hid_struct hid_t;
|
||||
static hid_t *first_hid;
|
||||
static hid_t *last_hid;
|
||||
struct hid_struct {
|
||||
usb_dev_handle *usb;
|
||||
int open;
|
||||
int iface;
|
||||
int ep_in;
|
||||
int ep_out;
|
||||
struct hid_struct *prev;
|
||||
struct hid_struct *next;
|
||||
};
|
||||
|
||||
static void add_hid(hid_t *h);
|
||||
static hid_t * get_hid(int num);
|
||||
static void free_all_hid(void);
|
||||
static void hid_close(hid_t *hid);
|
||||
static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end);
|
||||
|
||||
#define printf qDebug
|
||||
|
||||
@ -68,6 +46,10 @@ pjrc_rawhid::pjrc_rawhid()
|
||||
last_hid = NULL;
|
||||
}
|
||||
|
||||
pjrc_rawhid::~pjrc_rawhid()
|
||||
{
|
||||
}
|
||||
|
||||
// open - open 1 or more devices
|
||||
//
|
||||
// Inputs:
|
||||
@ -94,12 +76,17 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
|
||||
if (first_hid) free_all_hid();
|
||||
//printf("pjrc_rawhid_open, max=%d\n", max);
|
||||
|
||||
if (max < 1) return 0;
|
||||
|
||||
usb_init();
|
||||
usb_find_busses();
|
||||
usb_find_devices();
|
||||
for (bus = usb_get_busses(); bus; bus = bus->next) {
|
||||
for (dev = bus->devices; dev; dev = dev->next) {
|
||||
|
||||
for (bus = usb_get_busses(); bus; bus = bus->next)
|
||||
{
|
||||
for (dev = bus->devices; dev; dev = dev->next)
|
||||
{
|
||||
if (vid > 0 && dev->descriptor.idVendor != vid) continue;
|
||||
if (pid > 0 && dev->descriptor.idProduct != pid) continue;
|
||||
if (!dev->config) continue;
|
||||
@ -111,54 +98,72 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
iface = dev->config->interface;
|
||||
u = NULL;
|
||||
claimed = 0;
|
||||
for (i=0; i<dev->config->bNumInterfaces && iface; i++, iface++) {
|
||||
for (i=0; i<dev->config->bNumInterfaces && iface; i++, iface++)
|
||||
{
|
||||
desc = iface->altsetting;
|
||||
if (!desc) continue;
|
||||
printf(" type %d, %d, %d", desc->bInterfaceClass,
|
||||
desc->bInterfaceSubClass, desc->bInterfaceProtocol);
|
||||
|
||||
printf(" type %d, %d, %d", desc->bInterfaceClass, desc->bInterfaceSubClass, desc->bInterfaceProtocol);
|
||||
|
||||
if (desc->bInterfaceClass != 3) continue;
|
||||
if (desc->bInterfaceSubClass != 0) continue;
|
||||
if (desc->bInterfaceProtocol != 0) continue;
|
||||
|
||||
ep = desc->endpoint;
|
||||
ep_in = ep_out = 0;
|
||||
for (n = 0; n < desc->bNumEndpoints; n++, ep++) {
|
||||
if (ep->bEndpointAddress & 0x80) {
|
||||
for (n = 0; n < desc->bNumEndpoints; n++, ep++)
|
||||
{
|
||||
if (ep->bEndpointAddress & 0x80)
|
||||
{
|
||||
if (!ep_in) ep_in = ep->bEndpointAddress & 0x7F;
|
||||
qDebug() << " IN endpoint " << ep_in;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!ep_out) ep_out = ep->bEndpointAddress;
|
||||
qDebug() << " OUT endpoint " << ep_out;
|
||||
}
|
||||
}
|
||||
if (!ep_in) continue;
|
||||
if (!u) {
|
||||
|
||||
if (!u)
|
||||
{
|
||||
u = usb_open(dev);
|
||||
if (!u) {
|
||||
if (!u)
|
||||
{
|
||||
qDebug() << " unable to open device";
|
||||
break;
|
||||
}
|
||||
}
|
||||
qDebug() << " hid interface (generic)";
|
||||
if (usb_get_driver_np(u, i, (char *)buf, sizeof(buf)) >= 0) {
|
||||
if (usb_get_driver_np(u, i, (char *)buf, sizeof(buf)) >= 0)
|
||||
{
|
||||
printf(" in use by driver \"%s\"", buf);
|
||||
if (usb_detach_kernel_driver_np(u, i) < 0) {
|
||||
if (usb_detach_kernel_driver_np(u, i) < 0)
|
||||
{
|
||||
printf(" unable to detach from kernel");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (usb_claim_interface(u, i) < 0) {
|
||||
|
||||
if (usb_claim_interface(u, i) < 0)
|
||||
{
|
||||
printf(" unable claim interface %d", i);
|
||||
continue;
|
||||
}
|
||||
|
||||
len = usb_control_msg(u, 0x81, 6, 0x2200, i, (char *)buf, sizeof(buf), 250);
|
||||
printf(" descriptor, len=%d", len);
|
||||
if (len < 2) {
|
||||
if (len < 2)
|
||||
{
|
||||
usb_release_interface(u, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
p = buf;
|
||||
parsed_usage_page = parsed_usage = 0;
|
||||
while ((tag = hid_parse_item(&val, &p, buf + len)) >= 0) {
|
||||
while ((tag = hid_parse_item(&val, &p, buf + len)) >= 0)
|
||||
{
|
||||
printf(" tag: %X, val %X", tag, val);
|
||||
if (tag == 4) parsed_usage_page = val;
|
||||
if (tag == 8) parsed_usage = val;
|
||||
@ -166,28 +171,35 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
}
|
||||
if ((!parsed_usage_page) || (!parsed_usage) ||
|
||||
(usage_page > 0 && parsed_usage_page != (uint32_t)usage_page) ||
|
||||
(usage > 0 && parsed_usage != (uint32_t)usage)) {
|
||||
(usage > 0 && parsed_usage != (uint32_t)usage))
|
||||
{
|
||||
usb_release_interface(u, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
|
||||
if (!hid) {
|
||||
if (!hid)
|
||||
{
|
||||
usb_release_interface(u, i);
|
||||
continue;
|
||||
}
|
||||
|
||||
hid->usb = u;
|
||||
hid->iface = i;
|
||||
hid->ep_in = ep_in;
|
||||
hid->ep_out = ep_out;
|
||||
hid->open = 1;
|
||||
add_hid(hid);
|
||||
|
||||
claimed++;
|
||||
count++;
|
||||
if (count >= max) return count;
|
||||
}
|
||||
|
||||
if (u && !claimed) usb_close(u);
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@ -202,14 +214,15 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
//
|
||||
int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
|
||||
{
|
||||
hid_t *hid;
|
||||
int r;
|
||||
if (!buf) return -1;
|
||||
|
||||
hid = get_hid(num);
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid || !hid->open) return -1;
|
||||
r = usb_interrupt_read(hid->usb, hid->ep_in, (char *)buf, len, timeout);
|
||||
|
||||
int r = usb_interrupt_read(hid->usb, hid->ep_in, (char *)buf, len, timeout);
|
||||
if (r >= 0) return r;
|
||||
if (r == -110) return 0; // timeout
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -262,26 +275,18 @@ QString pjrc_rawhid::getserial(int num) {
|
||||
//
|
||||
void pjrc_rawhid::close(int num)
|
||||
{
|
||||
hid_t *hid;
|
||||
|
||||
hid = get_hid(num);
|
||||
if (!hid || !hid->open) return;
|
||||
hid_t *hid = get_hid(num);
|
||||
if (hid && !hid->open)
|
||||
hid_close(hid);
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
// Private Functions
|
||||
//
|
||||
//
|
||||
|
||||
// Chuck Robey wrote a real HID report parser
|
||||
// (chuckr@telenix.org) chuckr@chuckr.org
|
||||
// http://people.freebsd.org/~chuckr/code/python/uhidParser-0.2.tbz
|
||||
// this tiny thing only needs to extract the top-level usage page
|
||||
// and usage, and even then is may not be truly correct, but it does
|
||||
// work with the Teensy Raw HID example.
|
||||
static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end)
|
||||
int pjrc_rawhid::hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end)
|
||||
{
|
||||
const uint8_t *p = *data;
|
||||
uint8_t tag;
|
||||
@ -311,10 +316,12 @@ static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end)
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
static void add_hid(hid_t *h)
|
||||
void pjrc_rawhid::add_hid(hid_t *h)
|
||||
{
|
||||
if (!first_hid || !last_hid) {
|
||||
if (!h) return;
|
||||
|
||||
if (!first_hid || !last_hid)
|
||||
{
|
||||
first_hid = last_hid = h;
|
||||
h->next = h->prev = NULL;
|
||||
return;
|
||||
@ -325,41 +332,43 @@ static void add_hid(hid_t *h)
|
||||
last_hid = h;
|
||||
}
|
||||
|
||||
|
||||
static hid_t * get_hid(int num)
|
||||
hid_t * pjrc_rawhid::get_hid(int num)
|
||||
{
|
||||
hid_t *p;
|
||||
hid_t *p = NULL;
|
||||
for (p = first_hid; p && num > 0; p = p->next, num--) ;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static void free_all_hid(void)
|
||||
void pjrc_rawhid::free_all_hid(void)
|
||||
{
|
||||
hid_t *p, *q;
|
||||
|
||||
for (p = first_hid; p; p = p->next) {
|
||||
for (hid_t *p = first_hid; p; p = p->next)
|
||||
hid_close(p);
|
||||
}
|
||||
p = first_hid;
|
||||
while (p) {
|
||||
q = p;
|
||||
|
||||
hid_t *p = first_hid;
|
||||
while (p)
|
||||
{
|
||||
hid_t *q = p;
|
||||
p = p->next;
|
||||
free(q);
|
||||
}
|
||||
|
||||
first_hid = last_hid = NULL;
|
||||
}
|
||||
|
||||
|
||||
static void hid_close(hid_t *hid)
|
||||
void pjrc_rawhid::hid_close(hid_t *hid)
|
||||
{
|
||||
hid_t *p;
|
||||
int others=0;
|
||||
if (!hid) return;
|
||||
|
||||
usb_release_interface(hid->usb, hid->iface);
|
||||
for (p = first_hid; p; p = p->next) {
|
||||
if (p->open && p->usb == hid->usb) others++;
|
||||
|
||||
int others = 0;
|
||||
for (hid_t *p = first_hid; p; p = p->next)
|
||||
{
|
||||
if (p->open && p->usb == hid->usb)
|
||||
others++;
|
||||
}
|
||||
if (!others) usb_close(hid->usb);
|
||||
if (!others)
|
||||
usb_close(hid->usb);
|
||||
|
||||
hid->usb = NULL;
|
||||
}
|
||||
|
@ -39,34 +39,10 @@
|
||||
|
||||
#include "pjrc_rawhid.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <setupapi.h>
|
||||
#include <ddk/hidsdi.h>
|
||||
#include <ddk/hidclass.h>
|
||||
#include <QString>
|
||||
|
||||
#define printf qDebug
|
||||
|
||||
typedef struct hid_struct hid_t;
|
||||
struct hid_struct {
|
||||
HANDLE handle;
|
||||
int open;
|
||||
struct hid_struct *prev;
|
||||
struct hid_struct *next;
|
||||
};
|
||||
hid_t *first_hid;
|
||||
hid_t *last_hid;
|
||||
HANDLE rx_event;
|
||||
HANDLE tx_event;
|
||||
CRITICAL_SECTION rx_mutex;
|
||||
CRITICAL_SECTION tx_mutex;
|
||||
|
||||
static void add_hid(hid_t *h);
|
||||
static hid_t* get_hid(int num);
|
||||
static void free_all_hid(void);
|
||||
static void hid_close(hid_t *hid);
|
||||
static void print_win32_err(void);
|
||||
|
||||
pjrc_rawhid::pjrc_rawhid()
|
||||
{
|
||||
first_hid = NULL;
|
||||
@ -75,6 +51,10 @@ pjrc_rawhid::pjrc_rawhid()
|
||||
tx_event = NULL;
|
||||
}
|
||||
|
||||
pjrc_rawhid::~pjrc_rawhid()
|
||||
{
|
||||
}
|
||||
|
||||
// open - open 1 or more devices
|
||||
//
|
||||
// Inputs:
|
||||
@ -99,45 +79,52 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
HANDLE h;
|
||||
BOOL ret;
|
||||
hid_t *hid;
|
||||
|
||||
int count=0;
|
||||
|
||||
if (first_hid) free_all_hid();
|
||||
|
||||
if (max < 1) return 0;
|
||||
if (!rx_event) {
|
||||
|
||||
if (!rx_event)
|
||||
{
|
||||
rx_event = CreateEvent(NULL, TRUE, TRUE, NULL);
|
||||
tx_event = CreateEvent(NULL, TRUE, TRUE, NULL);
|
||||
InitializeCriticalSection(&rx_mutex);
|
||||
InitializeCriticalSection(&tx_mutex);
|
||||
}
|
||||
|
||||
HidD_GetHidGuid(&guid);
|
||||
|
||||
info = SetupDiGetClassDevs(&guid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
|
||||
if (info == INVALID_HANDLE_VALUE) return 0;
|
||||
for (index=0; 1 ;index++) {
|
||||
|
||||
for (index=0; 1 ;index++)
|
||||
{
|
||||
iface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
||||
ret = SetupDiEnumDeviceInterfaces(info, NULL, &guid, index, &iface);
|
||||
if (!ret) return count;
|
||||
|
||||
SetupDiGetInterfaceDeviceDetail(info, &iface, NULL, 0, &reqd_size, NULL);
|
||||
details = (SP_DEVICE_INTERFACE_DETAIL_DATA *)malloc(reqd_size);
|
||||
if (details == NULL) continue;
|
||||
|
||||
memset(details, 0, reqd_size);
|
||||
details->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
||||
ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details,
|
||||
reqd_size, NULL, NULL);
|
||||
if (!ret) {
|
||||
ret = SetupDiGetDeviceInterfaceDetail(info, &iface, details, reqd_size, NULL, NULL);
|
||||
if (!ret)
|
||||
{
|
||||
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 )
|
||||
|
||||
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) {
|
||||
if (GetLastError() == ERROR_ACCESS_DENIED)
|
||||
{
|
||||
free(details);
|
||||
continue;
|
||||
}
|
||||
@ -145,9 +132,11 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
// 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);
|
||||
|
||||
free(details);
|
||||
continue;
|
||||
}
|
||||
|
||||
free(details);
|
||||
|
||||
attrib.Size = sizeof(HIDD_ATTRIBUTES);
|
||||
@ -155,30 +144,40 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
//printf("vid: %4x\n", attrib.VendorID);
|
||||
if (!ret || (vid > 0 && attrib.VendorID != vid) ||
|
||||
(pid > 0 && attrib.ProductID != pid) ||
|
||||
!HidD_GetPreparsedData(h, &hid_data)) {
|
||||
!HidD_GetPreparsedData(h, &hid_data))
|
||||
{
|
||||
CloseHandle(h);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!HidP_GetCaps(hid_data, &capabilities) ||
|
||||
(usage_page > 0 && capabilities.UsagePage != usage_page) ||
|
||||
(usage > 0 && capabilities.Usage != usage)) {
|
||||
(usage > 0 && capabilities.Usage != usage))
|
||||
{
|
||||
HidD_FreePreparsedData(hid_data);
|
||||
CloseHandle(h);
|
||||
continue;
|
||||
}
|
||||
|
||||
HidD_FreePreparsedData(hid_data);
|
||||
|
||||
hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
|
||||
if (!hid) {
|
||||
if (!hid)
|
||||
{
|
||||
CloseHandle(h);
|
||||
continue;
|
||||
}
|
||||
|
||||
qDebug("Open: Handle address: %li for num: %i", (long int) h, count);
|
||||
|
||||
hid->handle = h;
|
||||
hid->open = 1;
|
||||
add_hid(hid);
|
||||
|
||||
count++;
|
||||
if (count >= max) return count;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
@ -193,41 +192,58 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
||||
//
|
||||
int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
|
||||
{
|
||||
hid_t *hid;
|
||||
OVERLAPPED ov;
|
||||
DWORD n, r;
|
||||
DWORD n;
|
||||
|
||||
hid = get_hid(num);
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid || !hid->open) return -1;
|
||||
|
||||
EnterCriticalSection(&rx_mutex);
|
||||
|
||||
ResetEvent(&rx_event);
|
||||
|
||||
memset(&ov, 0, sizeof(ov));
|
||||
ov.hEvent = rx_event;
|
||||
|
||||
if (!ReadFile(hid->handle, buf, len, NULL, &ov)) {
|
||||
if (GetLastError() != ERROR_IO_PENDING) goto return_error;
|
||||
r = WaitForSingleObject(rx_event, timeout);
|
||||
if (r == WAIT_TIMEOUT) goto return_timeout;
|
||||
if (r != WAIT_OBJECT_0) goto return_error;
|
||||
if (!ReadFile(hid->handle, buf, len, NULL, &ov))
|
||||
{
|
||||
if (GetLastError() != ERROR_IO_PENDING)
|
||||
{
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) goto return_error;
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE))
|
||||
{
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
|
||||
if (n <= 0) return -1;
|
||||
|
||||
// qDebug("Received %i bytes, first %x, second %x", len, *((char *) buf),*((char *)buf + 1));
|
||||
|
||||
if ((int)n > len) n = len;
|
||||
return n;
|
||||
return_timeout:
|
||||
CancelIo(hid->handle);
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return 0;
|
||||
return_error:
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&rx_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// send - send a packet
|
||||
@ -241,30 +257,42 @@ return_error:
|
||||
//
|
||||
int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
|
||||
{
|
||||
hid_t *hid;
|
||||
OVERLAPPED ov;
|
||||
DWORD n, r;
|
||||
|
||||
hid = get_hid(num);
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid || !hid->open) return -1;
|
||||
|
||||
// qDebug("Send: Handle address: %li for num: %i", (long int) hid->handle, num);
|
||||
|
||||
EnterCriticalSection(&tx_mutex);
|
||||
|
||||
ResetEvent(&tx_event);
|
||||
|
||||
memset(&ov, 0, sizeof(ov));
|
||||
ov.hEvent = tx_event;
|
||||
|
||||
// qDebug("Trying to write %u bytes. First %x second %x",len, *((char *) buf), *((char *)buf + 1));
|
||||
|
||||
if (!WriteFile(hid->handle, buf, len, NULL, &ov)) {
|
||||
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) goto return_timeout;
|
||||
if (r != WAIT_OBJECT_0) goto return_error;
|
||||
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
|
||||
{
|
||||
@ -280,30 +308,23 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
|
||||
qDebug("Problem getting overlapped result");
|
||||
print_win32_err();
|
||||
}
|
||||
|
||||
LeaveCriticalSection(&tx_mutex);
|
||||
|
||||
if (n <= 0) return -1;
|
||||
return n;
|
||||
return_timeout:
|
||||
CancelIo(hid->handle);
|
||||
LeaveCriticalSection(&tx_mutex);
|
||||
return 0;
|
||||
return_error:
|
||||
print_win32_err();
|
||||
LeaveCriticalSection(&tx_mutex);
|
||||
return -1;
|
||||
}
|
||||
|
||||
QString pjrc_rawhid::getserial(int num)
|
||||
{
|
||||
hid_t *hid;
|
||||
char temp[126];
|
||||
|
||||
hid = get_hid(num);
|
||||
hid_t *hid = get_hid(num);
|
||||
if (!hid || !hid->open)
|
||||
return "";
|
||||
|
||||
/* Should we do some "critical section" stuff here?? */
|
||||
if(!HidD_GetSerialNumberString(hid->handle, temp, sizeof(temp))) {
|
||||
// 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");
|
||||
}
|
||||
@ -320,21 +341,17 @@ QString pjrc_rawhid::getserial(int num)
|
||||
//
|
||||
void pjrc_rawhid::close(int num)
|
||||
{
|
||||
hid_t *hid;
|
||||
|
||||
hid = get_hid(num);
|
||||
if (!hid || !hid->open) return;
|
||||
hid_t *hid = get_hid(num);
|
||||
if (hid && hid->open)
|
||||
hid_close(hid);
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
// Private Functions
|
||||
//
|
||||
//
|
||||
static void add_hid(hid_t *h)
|
||||
void pjrc_rawhid::add_hid(hid_t *h)
|
||||
{
|
||||
if (!first_hid || !last_hid) {
|
||||
if (!h) return;
|
||||
|
||||
if (!first_hid || !last_hid)
|
||||
{
|
||||
first_hid = last_hid = h;
|
||||
h->next = h->prev = NULL;
|
||||
return;
|
||||
@ -345,45 +362,48 @@ static void add_hid(hid_t *h)
|
||||
last_hid = h;
|
||||
}
|
||||
|
||||
|
||||
static hid_t * get_hid(int num)
|
||||
hid_t * pjrc_rawhid::get_hid(int num)
|
||||
{
|
||||
hid_t *p;
|
||||
for (p = first_hid; p && num > 0; p = p->next, num--) ;
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
static void free_all_hid(void)
|
||||
void pjrc_rawhid::free_all_hid(void)
|
||||
{
|
||||
hid_t *p, *q;
|
||||
|
||||
for (p = first_hid; p; p = p->next) {
|
||||
for (hid_t *p = first_hid; p; p = p->next)
|
||||
hid_close(p);
|
||||
}
|
||||
p = first_hid;
|
||||
while (p) {
|
||||
q = p;
|
||||
|
||||
hid_t *p = first_hid;
|
||||
while (p)
|
||||
{
|
||||
hid_t *q = p;
|
||||
p = p->next;
|
||||
free(q);
|
||||
}
|
||||
|
||||
first_hid = last_hid = NULL;
|
||||
}
|
||||
|
||||
|
||||
static void hid_close(hid_t *hid)
|
||||
void pjrc_rawhid::hid_close(hid_t *hid)
|
||||
{
|
||||
if (!hid) return;
|
||||
|
||||
if (hid->handle)
|
||||
{
|
||||
CloseHandle(hid->handle);
|
||||
hid->handle = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static void print_win32_err(void)
|
||||
void pjrc_rawhid::print_win32_err(void)
|
||||
{
|
||||
char buf[256];
|
||||
char temp[256];
|
||||
DWORD err;
|
||||
|
||||
err = GetLastError();
|
||||
|
||||
//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 );
|
||||
|
@ -291,10 +291,10 @@ RawHID::RawHID(const QString &deviceName)
|
||||
m_writeThread(NULL)
|
||||
{
|
||||
//find the device the user want to open and close the other
|
||||
int opened = dev.open(MAX_DEVICES, VID, PID, USAGE_PAGE, USAGE);
|
||||
int opened = dev.open(USB_MAX_DEVICES, USB_VID, USB_PID, USB_USAGE_PAGE, USB_USAGE);
|
||||
|
||||
//for each devices found, get serial number and close
|
||||
for(int i=0; i<opened; i++)
|
||||
for (int i=0; i<opened; i++)
|
||||
{
|
||||
if(deviceName == dev.getserial(i))
|
||||
m_deviceNo = i;
|
||||
@ -303,7 +303,7 @@ RawHID::RawHID(const QString &deviceName)
|
||||
}
|
||||
|
||||
//didn't find the device we are trying to open (shouldnt happen)
|
||||
if(m_deviceNo < 0)
|
||||
if (m_deviceNo < 0)
|
||||
{
|
||||
qDebug() << "Error: cannot open device " << deviceName;
|
||||
return;
|
||||
|
@ -28,15 +28,14 @@
|
||||
#ifndef RAWHID_CONST_H
|
||||
#define RAWHID_CONST_H
|
||||
|
||||
static const int USB_MAX_DEVICES = 10;
|
||||
|
||||
static const int MAX_DEVICES = 10;
|
||||
static const int USB_VID = 0x20A0;
|
||||
static const int USB_PID = 0x4117;
|
||||
|
||||
static const int VID = 0x20A0;
|
||||
static const int PID = 0x4117;
|
||||
static const int USB_USAGE_PAGE = 0xFF9C;
|
||||
static const int USB_USAGE = 0x0001;
|
||||
|
||||
static const int USAGE_PAGE = 0xFF9C;
|
||||
static const int USAGE = 0x0001;
|
||||
|
||||
static const int DEV_SERIAL_LEN = 24;
|
||||
static const int USB_DEV_SERIAL_LEN = 24;
|
||||
|
||||
#endif // RAWHID_CONST_H
|
||||
|
@ -156,7 +156,7 @@ QStringList RawHIDConnection::availableDevices()
|
||||
if (enablePolling) {
|
||||
pjrc_rawhid dev;
|
||||
//open all device we can
|
||||
int opened = dev.open(MAX_DEVICES, VID, PID, USAGE_PAGE, USAGE);
|
||||
int opened = dev.open(USB_MAX_DEVICES, USB_VID, USB_PID, USB_USAGE_PAGE, USB_USAGE);
|
||||
|
||||
//for each devices found, get serial number and close it back
|
||||
for(int i=0; i<opened; i++)
|
||||
|
Loading…
x
Reference in New Issue
Block a user