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

Fixing platform dependent string type handling

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1179 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
peabody124 2010-07-29 19:36:42 +00:00 committed by peabody124
parent ab711777e5
commit 561d42c73a
6 changed files with 21 additions and 36 deletions

View File

@ -32,6 +32,7 @@
#include <stdlib.h>
#include <stdint.h>
#include <QDebug>
#include <QString>
class pjrc_rawhid
{
@ -41,7 +42,7 @@ public:
int receive(int num, void *buf, int len, int timeout);
void close(int num);
int send(int num, void *buf, int len, int timeout);
int getserial(int num, char *buf);
QString getserial(int num);
void mytest(int num);
private:

View File

@ -37,13 +37,12 @@
*/
#include <QDebug>
#include "pjrc_rawhid.h"
#include <unistd.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/hid/IOHIDLib.h>
#include <QDebug>
#include <QString>
#define BUFFER_SIZE 64
@ -273,24 +272,22 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
return result;
}
int pjrc_rawhid::getserial(int num, char *buf) {
QString pjrc_rawhid::getserial(int num) {
hid_t *hid;
char buf[128];
hid = get_hid(num);
if (!hid || !hid->open) return -1;
if (!hid || !hid->open) return QString("Error");
CFTypeRef serialnum = IOHIDDeviceGetProperty(hid->ref, CFSTR(kIOHIDSerialNumberKey));
if(serialnum && CFGetTypeID(serialnum) == CFStringGetTypeID())
{
/* For some reason the first 8 bytes of 'serialnum' are useless (a struct?) */
char *strptr = (char *)serialnum;
for(int i = 8; i < 33; i++) {
*(buf++) = strptr[i];
}
return 0;
/* For some reason the first 9 bytes of 'serialnum' are useless (a struct?) */
return QString().fromAscii((char *)serialnum+9);
}
return -1;
return QString("Error");
}
// close - close a device

View File

@ -39,6 +39,7 @@
#include "pjrc_rawhid.h"
#include <usb.h>
#include <QDebug>
#include <QString>
typedef struct hid_struct hid_t;
static hid_t *first_hid;
@ -242,13 +243,14 @@ int pjrc_rawhid::send(int num, void *buf, int len, int timeout)
// Output
// number of bytes in found, or -1 on error
//
int pjrc_rawhid::getserial(int num, char *buf) {
QString pjrc_rawhid::getserial(int num) {
hid_t *hid;
char buf[128];
hid = get_hid(num);
if (!hid || !hid->open) return -1;
return usb_get_string_simple(hid->usb, 3, (char *)buf, 25);
int retlen = usb_get_string_simple(hid->usb, 3, buf, 128);
return QString().fromAscii(buf,-1);
}
// close - close a device

View File

@ -43,6 +43,7 @@
#include <setupapi.h>
#include <ddk/hidsdi.h>
#include <ddk/hidclass.h>
#include <QString>
#define printf qDebug
@ -254,12 +255,10 @@ return_error:
return -1;
}
int pjrc_rawhid::getserial(int num, char *buf)
QString pjrc_rawhid::getserial(int num)
{
hid_t *hid;
char temp[126];
char buf2[24];
char *bufptr = (char *)buf;
hid = get_hid(num);
if (!hid || !hid->open) return -1;
@ -267,19 +266,10 @@ int pjrc_rawhid::getserial(int num, char *buf)
/* Should we do some "critical section" stuff here?? */
if(!HidD_GetSerialNumberString(hid->handle, temp, sizeof(temp))) {
print_win32_err();
return -1;
return QString("Error");
}
/* 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;
return QString().fromUtf16(temp,-1);
}
// close - close a device

View File

@ -276,9 +276,7 @@ RawHID::RawHID(const QString &deviceName)
//for each devices found, get serial number and close
for(int i=0; i<opened; i++)
{
char serial[256];
dev.getserial(i, serial);
if(deviceName == QString::fromAscii(serial, DEV_SERIAL_LEN))
if(deviceName == dev.getserial(i))
m_deviceNo = i;
else
dev.close(i);

View File

@ -105,10 +105,7 @@ QStringList RawHIDConnection::availableDevices()
//for each devices found, get serial number and close it back
for(int i=0; i<opened; i++)
{
char serial[256];
dev.getserial(i, serial);
QString sn = QString::fromAscii(serial, DEV_SERIAL_LEN);
devices.append(sn);
devices.append(dev.getserial(i));
dev.close(i);
}