mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2024-12-01 09:24:10 +01:00
Some initial modification on uavtalk plugin to interface with the connection manager.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@365 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
cc7c7f8a24
commit
03283b2572
@ -1,351 +0,0 @@
|
|||||||
/* Simple Raw HID functions for Windows - for use with Teensy RawHID example
|
|
||||||
* http://www.pjrc.com/teensy/rawhid.html
|
|
||||||
* Copyright (c) 2009 PJRC.COM, LLC
|
|
||||||
*
|
|
||||||
* rawhid_open - open 1 or more devices
|
|
||||||
* rawhid_recv - receive a packet
|
|
||||||
* rawhid_send - send a packet
|
|
||||||
* rawhid_close - close a device
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above description, website URL and copyright notice and this permission
|
|
||||||
* notice shall be included in all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*
|
|
||||||
* Version 1.0: Initial Release
|
|
||||||
*/
|
|
||||||
/* See: http://msdn.microsoft.com/en-us/library/ms794141.aspx */
|
|
||||||
|
|
||||||
#include "rawhid.h"
|
|
||||||
|
|
||||||
#include <windows.h>
|
|
||||||
#include <setupapi.h>
|
|
||||||
#include <ddk/hidsdi.h>
|
|
||||||
#include <ddk/hidclass.h>
|
|
||||||
|
|
||||||
#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);
|
|
||||||
|
|
||||||
rawhid::rawhid()
|
|
||||||
{
|
|
||||||
first_hid = NULL;
|
|
||||||
last_hid = NULL;
|
|
||||||
rx_event = NULL;
|
|
||||||
tx_event = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
// open - open 1 or more devices
|
|
||||||
//
|
|
||||||
// Inputs:
|
|
||||||
// max = maximum number of devices to open
|
|
||||||
// vid = Vendor ID, or -1 if any
|
|
||||||
// pid = Product ID, or -1 if any
|
|
||||||
// usage_page = top level usage page, or -1 if any
|
|
||||||
// usage = top level usage number, or -1 if any
|
|
||||||
// Output:
|
|
||||||
// actual number of devices opened
|
|
||||||
//
|
|
||||||
int rawhid::open(int max, int vid, int pid, int usage_page, int usage)
|
|
||||||
{
|
|
||||||
GUID guid;
|
|
||||||
HDEVINFO info;
|
|
||||||
DWORD index=0, reqd_size;
|
|
||||||
SP_DEVICE_INTERFACE_DATA iface;
|
|
||||||
SP_DEVICE_INTERFACE_DETAIL_DATA *details;
|
|
||||||
HIDD_ATTRIBUTES attrib;
|
|
||||||
PHIDP_PREPARSED_DATA hid_data;
|
|
||||||
HIDP_CAPS capabilities;
|
|
||||||
HANDLE h;
|
|
||||||
BOOL ret;
|
|
||||||
hid_t *hid;
|
|
||||||
int count=0;
|
|
||||||
|
|
||||||
if (first_hid) free_all_hid();
|
|
||||||
if (max < 1) return 0;
|
|
||||||
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++) {
|
|
||||||
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) {
|
|
||||||
free(details);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
h = CreateFile(details->DevicePath, GENERIC_READ|GENERIC_WRITE,
|
|
||||||
FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
|
|
||||||
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
|
||||||
free(details);
|
|
||||||
if (h == INVALID_HANDLE_VALUE) continue;
|
|
||||||
attrib.Size = sizeof(HIDD_ATTRIBUTES);
|
|
||||||
ret = HidD_GetAttributes(h, &attrib);
|
|
||||||
printf("vid: %4x\n", attrib.VendorID);
|
|
||||||
if (!ret || (vid > 0 && attrib.VendorID != vid) ||
|
|
||||||
(pid > 0 && attrib.ProductID != pid) ||
|
|
||||||
!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)) {
|
|
||||||
HidD_FreePreparsedData(hid_data);
|
|
||||||
CloseHandle(h);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
HidD_FreePreparsedData(hid_data);
|
|
||||||
hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
|
|
||||||
if (!hid) {
|
|
||||||
CloseHandle(h);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
hid->handle = h;
|
|
||||||
hid->open = 1;
|
|
||||||
add_hid(hid);
|
|
||||||
count++;
|
|
||||||
if (count >= max) return count;
|
|
||||||
}
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
// recveive - receive a packet
|
|
||||||
// Inputs:
|
|
||||||
// num = device to receive from (zero based)
|
|
||||||
// buf = buffer to receive packet
|
|
||||||
// len = buffer's size
|
|
||||||
// timeout = time to wait, in milliseconds
|
|
||||||
// Output:
|
|
||||||
// number of bytes received, or -1 on error
|
|
||||||
//
|
|
||||||
int rawhid::receive(int num, void *buf, int len, int timeout)
|
|
||||||
{
|
|
||||||
hid_t *hid;
|
|
||||||
unsigned char tmpbuf[516];
|
|
||||||
OVERLAPPED ov;
|
|
||||||
DWORD n, r;
|
|
||||||
|
|
||||||
if (sizeof(tmpbuf) < len + 1) return -1;
|
|
||||||
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, tmpbuf, len + 1, 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 (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) goto return_error;
|
|
||||||
LeaveCriticalSection(&rx_mutex);
|
|
||||||
if (n <= 0) return -1;
|
|
||||||
n--;
|
|
||||||
if (n > len) n = len;
|
|
||||||
memcpy(buf, tmpbuf + 1, n);
|
|
||||||
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
|
|
||||||
// Inputs:
|
|
||||||
// num = device to transmit to (zero based)
|
|
||||||
// buf = buffer containing packet to send
|
|
||||||
// len = number of bytes to transmit
|
|
||||||
// timeout = time to wait, in milliseconds
|
|
||||||
// Output:
|
|
||||||
// number of bytes sent, or -1 on error
|
|
||||||
//
|
|
||||||
int rawhid::send(int num, void *buf, int len, int timeout)
|
|
||||||
{
|
|
||||||
hid_t *hid;
|
|
||||||
unsigned char tmpbuf[516];
|
|
||||||
OVERLAPPED ov;
|
|
||||||
DWORD n, r;
|
|
||||||
|
|
||||||
if (sizeof(tmpbuf) < len + 1) return -1;
|
|
||||||
hid = get_hid(num);
|
|
||||||
if (!hid || !hid->open) return -1;
|
|
||||||
EnterCriticalSection(&tx_mutex);
|
|
||||||
ResetEvent(&tx_event);
|
|
||||||
memset(&ov, 0, sizeof(ov));
|
|
||||||
ov.hEvent = tx_event;
|
|
||||||
tmpbuf[0] = 0;
|
|
||||||
memcpy(tmpbuf + 1, buf, len);
|
|
||||||
if (!WriteFile(hid->handle, tmpbuf, len + 1, NULL, &ov)) {
|
|
||||||
if (GetLastError() != ERROR_IO_PENDING) goto return_error;
|
|
||||||
r = WaitForSingleObject(tx_event, timeout);
|
|
||||||
if (r == WAIT_TIMEOUT) goto return_timeout;
|
|
||||||
if (r != WAIT_OBJECT_0) goto return_error;
|
|
||||||
}
|
|
||||||
if (!GetOverlappedResult(hid->handle, &ov, &n, FALSE)) goto return_error;
|
|
||||||
LeaveCriticalSection(&tx_mutex);
|
|
||||||
if (n <= 0) return -1;
|
|
||||||
return n - 1;
|
|
||||||
return_timeout:
|
|
||||||
CancelIo(hid->handle);
|
|
||||||
LeaveCriticalSection(&tx_mutex);
|
|
||||||
return 0;
|
|
||||||
return_error:
|
|
||||||
print_win32_err();
|
|
||||||
LeaveCriticalSection(&tx_mutex);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int rawhid::getserial(int num, char *buf)
|
|
||||||
{
|
|
||||||
hid_t *hid;
|
|
||||||
char temp[126];
|
|
||||||
char buf2[24];
|
|
||||||
char *bufptr = (char *)buf;
|
|
||||||
|
|
||||||
hid = get_hid(num);
|
|
||||||
if (!hid || !hid->open) return -1;
|
|
||||||
|
|
||||||
/* Should we do some "critical section" stuff here?? */
|
|
||||||
if(!HidD_GetSerialNumberString(hid->handle, temp, sizeof(temp))) {
|
|
||||||
print_win32_err();
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Is there a better way to do this? */
|
|
||||||
/* Every second char in temp is a NULL */
|
|
||||||
for(int i = 0; i < 48; i++) {
|
|
||||||
char temp2 = temp[i++];
|
|
||||||
if(temp2 == 0) break;
|
|
||||||
buf2[i/2] = temp2;
|
|
||||||
*(bufptr++) = temp2;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// close - close a device
|
|
||||||
//
|
|
||||||
// Inputs:
|
|
||||||
// num = device to close (zero based)
|
|
||||||
// Output
|
|
||||||
// (nothing)
|
|
||||||
//
|
|
||||||
void rawhid::close(int num)
|
|
||||||
{
|
|
||||||
hid_t *hid;
|
|
||||||
|
|
||||||
hid = get_hid(num);
|
|
||||||
if (!hid || !hid->open) return;
|
|
||||||
hid_close(hid);
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// Private Functions
|
|
||||||
//
|
|
||||||
//
|
|
||||||
static void add_hid(hid_t *h)
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static hid_t * 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)
|
|
||||||
{
|
|
||||||
hid_t *p, *q;
|
|
||||||
|
|
||||||
for (p = first_hid; p; p = p->next) {
|
|
||||||
hid_close(p);
|
|
||||||
}
|
|
||||||
p = first_hid;
|
|
||||||
while (p) {
|
|
||||||
q = p;
|
|
||||||
p = p->next;
|
|
||||||
free(q);
|
|
||||||
}
|
|
||||||
first_hid = last_hid = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void hid_close(hid_t *hid)
|
|
||||||
{
|
|
||||||
CloseHandle(hid->handle);
|
|
||||||
hid->handle = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void print_win32_err(void)
|
|
||||||
{
|
|
||||||
char buf[256];
|
|
||||||
DWORD err;
|
|
||||||
|
|
||||||
err = GetLastError();
|
|
||||||
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, err, 0, (WCHAR*)buf, sizeof(buf), NULL);
|
|
||||||
printf("err %ld: %s\n", err, buf);
|
|
||||||
}
|
|
@ -27,6 +27,9 @@
|
|||||||
*/
|
*/
|
||||||
#include "uavtalkplugin.h"
|
#include "uavtalkplugin.h"
|
||||||
|
|
||||||
|
#include <coreplugin/icore.h>
|
||||||
|
#include <coreplugin/connectionmanager.h>
|
||||||
|
|
||||||
UAVTalkPlugin::UAVTalkPlugin()
|
UAVTalkPlugin::UAVTalkPlugin()
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -44,12 +47,15 @@ void UAVTalkPlugin::extensionsInitialized()
|
|||||||
objMngr = pm->getObject<UAVObjectManager>();
|
objMngr = pm->getObject<UAVObjectManager>();
|
||||||
|
|
||||||
// TODO: Initialize serial port and USB libraries, get QIODevice from each
|
// TODO: Initialize serial port and USB libraries, get QIODevice from each
|
||||||
|
// ---Julien: Actually I changed a little bit from the initial plan,
|
||||||
|
// now you have to connect to signal emited by the connection manager
|
||||||
|
Core::ConnectionManager *cm = Core::ICore::instance()->connectionManager();
|
||||||
|
QObject::connect(cm, SIGNAL(deviceConnected(QIODevice *)),
|
||||||
|
this, SLOT(onDeviceConnect(QIODevice *)));
|
||||||
|
QObject::connect(cm, SIGNAL(deviceDisconnected()),
|
||||||
|
this, SLOT(onDeviceDisconnect()));
|
||||||
|
|
||||||
// TODO: Initialize UAVTalk object
|
|
||||||
//utalk = new UAVTalk(io, objMngr);
|
|
||||||
|
|
||||||
// TODO: Initialize telemetry object
|
|
||||||
//telemetry = new Telemetry(utalk, objMngr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UAVTalkPlugin::initialize(const QStringList & arguments, QString * errorString)
|
bool UAVTalkPlugin::initialize(const QStringList & arguments, QString * errorString)
|
||||||
@ -65,4 +71,15 @@ void UAVTalkPlugin::shutdown()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UAVTalkPlugin::onDeviceConnect(QIODevice *dev)
|
||||||
|
{
|
||||||
|
utalk = new UAVTalk(dev, objMngr);
|
||||||
|
telemetry = new Telemetry(utalk, objMngr);
|
||||||
|
}
|
||||||
|
void UAVTalkPlugin::onDeviceDisconnect()
|
||||||
|
{
|
||||||
|
delete telemetry;
|
||||||
|
delete utalk;
|
||||||
|
}
|
||||||
|
|
||||||
Q_EXPORT_PLUGIN(UAVTalkPlugin)
|
Q_EXPORT_PLUGIN(UAVTalkPlugin)
|
||||||
|
@ -37,6 +37,8 @@
|
|||||||
|
|
||||||
class UAVTalkPlugin: public ExtensionSystem::IPlugin
|
class UAVTalkPlugin: public ExtensionSystem::IPlugin
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
UAVTalkPlugin();
|
UAVTalkPlugin();
|
||||||
~UAVTalkPlugin();
|
~UAVTalkPlugin();
|
||||||
@ -45,6 +47,10 @@ public:
|
|||||||
bool initialize(const QStringList & arguments, QString * errorString);
|
bool initialize(const QStringList & arguments, QString * errorString);
|
||||||
void shutdown();
|
void shutdown();
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
void onDeviceConnect(QIODevice *dev);
|
||||||
|
void onDeviceDisconnect();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UAVObjectManager* objMngr;
|
UAVObjectManager* objMngr;
|
||||||
UAVTalk* utalk;
|
UAVTalk* utalk;
|
||||||
|
Loading…
Reference in New Issue
Block a user