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

Added deviceUnplugged emitted signal in windows version.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2912 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
pip 2011-02-27 23:14:04 +00:00 committed by pip
parent ed4aefbb95
commit 011abe3117
2 changed files with 97 additions and 23 deletions

View File

@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
#include <QObject>
#include <QDebug> #include <QDebug>
#include <QString> #include <QString>
#include "rawhid_global.h" #include "rawhid_global.h"
@ -78,7 +79,6 @@
struct hid_struct struct hid_struct
{ {
HANDLE handle; HANDLE handle;
int open;
struct hid_struct *prev; struct hid_struct *prev;
struct hid_struct *next; struct hid_struct *next;
}; };
@ -87,8 +87,9 @@
// ************ // ************
class RAWHID_EXPORT pjrc_rawhid class RAWHID_EXPORT pjrc_rawhid : public QObject
{ {
Q_OBJECT
public: public:
pjrc_rawhid(); pjrc_rawhid();
@ -101,6 +102,9 @@ public:
QString getserial(int num); QString getserial(int num);
void mytest(int num); void mytest(int num);
signals:
void deviceUnplugged(int num);
private: private:
#if defined( Q_OS_MAC) #if defined( Q_OS_MAC)
@ -131,7 +135,7 @@ private:
hid_t * get_hid(int num); hid_t * get_hid(int num);
void free_all_hid(void); void free_all_hid(void);
void hid_close(hid_t *hid); void hid_close(hid_t *hid);
void print_win32_err(void); void print_win32_err(DWORD err);
#endif #endif
}; };

View File

@ -121,16 +121,18 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); 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) if (h == INVALID_HANDLE_VALUE)
{ {
DWORD err = GetLastError();
// I get ERROR_ACCESS_DENIED with most/all my input devices (mice/trackballs/tablet). // I get ERROR_ACCESS_DENIED with most/all my input devices (mice/trackballs/tablet).
// Let's not log it :) // Let's not log it :)
if (GetLastError() == ERROR_ACCESS_DENIED) if (err == ERROR_ACCESS_DENIED)
{ {
free(details); free(details);
continue; continue;
} }
// qDebug wipes the GetLastError() it seems, so do that after print_win32_err(). // qDebug wipes the GetLastError() it seems, so do that after print_win32_err().
print_win32_err(); print_win32_err(err);
qDebug() << "Problem opening handle, path: " << QString().fromWCharArray(details->DevicePath); qDebug() << "Problem opening handle, path: " << QString().fromWCharArray(details->DevicePath);
free(details); free(details);
@ -168,10 +170,21 @@ int pjrc_rawhid::open(int max, int vid, int pid, int usage_page, int usage)
continue; continue;
} }
// COMMTIMEOUTS CommTimeouts;
// CommTimeouts.ReadIntervalTimeout = 100; // 100ms
// CommTimeouts.ReadTotalTimeoutConstant = 5; // ms
// CommTimeouts.ReadTotalTimeoutMultiplier = 1; //
// CommTimeouts.WriteTotalTimeoutConstant = 5; // ms
// CommTimeouts.WriteTotalTimeoutMultiplier = 1; //
// if (!SetCommTimeouts(h, &CommTimeouts))
// {
//// DWORD err = GetLastError();
//
// }
qDebug("Open: Handle address: %li for num: %i", (long int) h, count); qDebug("Open: Handle address: %li for num: %i", (long int) h, count);
hid->handle = h; hid->handle = h;
hid->open = 1;
add_hid(hid); add_hid(hid);
count++; count++;
@ -196,7 +209,10 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
DWORD n; DWORD n;
hid_t *hid = get_hid(num); hid_t *hid = get_hid(num);
if (!hid || !hid->open) return -1; if (!hid)
return -1;
if (!hid->handle)
return -1;
EnterCriticalSection(&rx_mutex); EnterCriticalSection(&rx_mutex);
@ -207,9 +223,20 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
if (!ReadFile(hid->handle, buf, len, NULL, &ov)) if (!ReadFile(hid->handle, buf, len, NULL, &ov))
{ {
if (GetLastError() != ERROR_IO_PENDING) DWORD err = GetLastError();
if (err == ERROR_DEVICE_NOT_CONNECTED)
{ // the device has been unplugged
print_win32_err(err);
hid_close(hid);
LeaveCriticalSection(&rx_mutex);
emit deviceUnplugged(num);
return -1;
}
if (err != ERROR_IO_PENDING)
{ {
print_win32_err(); print_win32_err(err);
LeaveCriticalSection(&rx_mutex); LeaveCriticalSection(&rx_mutex);
return -1; return -1;
} }
@ -223,7 +250,8 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
} }
if (r != WAIT_OBJECT_0) if (r != WAIT_OBJECT_0)
{ {
print_win32_err(); DWORD err = GetLastError();
print_win32_err(err);
LeaveCriticalSection(&rx_mutex); LeaveCriticalSection(&rx_mutex);
return -1; return -1;
} }
@ -231,7 +259,17 @@ int pjrc_rawhid::receive(int num, void *buf, int len, int timeout)
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE))
{ {
print_win32_err(); DWORD err = GetLastError();
print_win32_err(err);
if (err == ERROR_DEVICE_NOT_CONNECTED)
{ // the device has been unplugged
hid_close(hid);
LeaveCriticalSection(&rx_mutex);
emit deviceUnplugged(num);
return -1;
}
LeaveCriticalSection(&rx_mutex); LeaveCriticalSection(&rx_mutex);
return -1; return -1;
} }
@ -261,7 +299,10 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
DWORD n, r; DWORD n, r;
hid_t *hid = get_hid(num); hid_t *hid = get_hid(num);
if (!hid || !hid->open) return -1; if (!hid)
return -1;
if (!hid->handle)
return -1;
// qDebug("Send: Handle address: %li for num: %i", (long int) hid->handle, num); // qDebug("Send: Handle address: %li for num: %i", (long int) hid->handle, num);
@ -277,7 +318,16 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
if (!WriteFile(hid->handle, buf, len, NULL, &ov)) if (!WriteFile(hid->handle, buf, len, NULL, &ov))
{ {
DWORD err = GetLastError(); DWORD err = GetLastError();
if ( err == ERROR_SUCCESS || err == ERROR_IO_PENDING )
if (err == ERROR_DEVICE_NOT_CONNECTED)
{ // the device has been unplugged
hid_close(hid);
LeaveCriticalSection(&tx_mutex);
emit deviceUnplugged(num);
return -1;
}
if (err == ERROR_SUCCESS || err == ERROR_IO_PENDING)
{ {
// qDebug("Waiting for write to finish"); // qDebug("Waiting for write to finish");
r = WaitForSingleObject(tx_event, timeout); r = WaitForSingleObject(tx_event, timeout);
@ -289,7 +339,8 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
} }
if (r != WAIT_OBJECT_0) if (r != WAIT_OBJECT_0)
{ {
print_win32_err(); DWORD err = GetLastError();
print_win32_err(err);
LeaveCriticalSection(&tx_mutex); LeaveCriticalSection(&tx_mutex);
return -1; return -1;
} }
@ -297,7 +348,7 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
else else
{ {
// qDebug("Error writing to file"); // qDebug("Error writing to file");
print_win32_err(); print_win32_err(err);
LeaveCriticalSection(&tx_mutex); LeaveCriticalSection(&tx_mutex);
return -1; return -1;
} }
@ -305,8 +356,18 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE))
{ {
DWORD err = GetLastError();
qDebug("Problem getting overlapped result"); qDebug("Problem getting overlapped result");
print_win32_err(); print_win32_err(err);
if (err == ERROR_DEVICE_NOT_CONNECTED)
{ // the device has been unplugged
hid_close(hid);
LeaveCriticalSection(&tx_mutex);
emit deviceUnplugged(num);
return -1;
}
} }
LeaveCriticalSection(&tx_mutex); LeaveCriticalSection(&tx_mutex);
@ -318,14 +379,25 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
QString pjrc_rawhid::getserial(int num) QString pjrc_rawhid::getserial(int num)
{ {
hid_t *hid = get_hid(num); hid_t *hid = get_hid(num);
if (!hid || !hid->open) if (!hid)
return "";
if (!hid->handle)
return ""; return "";
// Should we do some "critical section" stuff here?? // Should we do some "critical section" stuff here??
char temp[126]; char temp[126];
if (!HidD_GetSerialNumberString(hid->handle, temp, sizeof(temp))) if (!HidD_GetSerialNumberString(hid->handle, temp, sizeof(temp)))
{ {
print_win32_err(); DWORD err = GetLastError();
print_win32_err(err);
if (err == ERROR_DEVICE_NOT_CONNECTED)
{ // the device has been unplugged
hid_close(hid);
emit deviceUnplugged(num);
return "";
}
return QString("Error"); return QString("Error");
} }
@ -354,6 +426,7 @@ void pjrc_rawhid::add_hid(hid_t *h)
h->next = h->prev = NULL; h->next = h->prev = NULL;
return; return;
} }
last_hid->next = h; last_hid->next = h;
h->prev = last_hid; h->prev = last_hid;
h->next = NULL; h->next = NULL;
@ -386,19 +459,16 @@ void pjrc_rawhid::free_all_hid(void)
void pjrc_rawhid::hid_close(hid_t *hid) void pjrc_rawhid::hid_close(hid_t *hid)
{ {
if (!hid) return; if (!hid) return;
if (!hid->handle || !hid->open) return; if (!hid->handle) return;
CloseHandle(hid->handle); CloseHandle(hid->handle);
hid->handle = NULL; hid->handle = NULL;
} }
void pjrc_rawhid::print_win32_err(void) void pjrc_rawhid::print_win32_err(DWORD err)
{ {
char buf[256]; char buf[256];
char temp[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, 0, (WCHAR*)buf, sizeof(buf), NULL);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (WCHAR*)buf, sizeof(buf), NULL); FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (WCHAR*)buf, sizeof(buf), NULL);