From 561d42c73af414c6cbb1ac54067d91a1043572ba Mon Sep 17 00:00:00 2001 From: peabody124 Date: Thu, 29 Jul 2010 19:36:42 +0000 Subject: [PATCH] Fixing platform dependent string type handling git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1179 ebee16cc-31ac-478f-84a7-5cbb03baadba --- ground/src/plugins/rawhid/pjrc_rawhid.h | 3 ++- ground/src/plugins/rawhid/pjrc_rawhid_mac.cpp | 19 ++++++++----------- .../src/plugins/rawhid/pjrc_rawhid_unix.cpp | 8 +++++--- ground/src/plugins/rawhid/pjrc_rawhid_win.cpp | 18 ++++-------------- ground/src/plugins/rawhid/rawhid.cpp | 4 +--- ground/src/plugins/rawhid/rawhidplugin.cpp | 5 +---- 6 files changed, 21 insertions(+), 36 deletions(-) diff --git a/ground/src/plugins/rawhid/pjrc_rawhid.h b/ground/src/plugins/rawhid/pjrc_rawhid.h index 7e0247ceb..683cf81d7 100644 --- a/ground/src/plugins/rawhid/pjrc_rawhid.h +++ b/ground/src/plugins/rawhid/pjrc_rawhid.h @@ -32,6 +32,7 @@ #include #include #include +#include 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: diff --git a/ground/src/plugins/rawhid/pjrc_rawhid_mac.cpp b/ground/src/plugins/rawhid/pjrc_rawhid_mac.cpp index 63db1b0ef..e62ba1ca8 100644 --- a/ground/src/plugins/rawhid/pjrc_rawhid_mac.cpp +++ b/ground/src/plugins/rawhid/pjrc_rawhid_mac.cpp @@ -37,13 +37,12 @@ */ -#include #include "pjrc_rawhid.h" #include #include #include -#include +#include #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 diff --git a/ground/src/plugins/rawhid/pjrc_rawhid_unix.cpp b/ground/src/plugins/rawhid/pjrc_rawhid_unix.cpp index aaddf59a6..d0ab3e1ae 100644 --- a/ground/src/plugins/rawhid/pjrc_rawhid_unix.cpp +++ b/ground/src/plugins/rawhid/pjrc_rawhid_unix.cpp @@ -39,6 +39,7 @@ #include "pjrc_rawhid.h" #include #include +#include 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 diff --git a/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp b/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp index 51a3cc779..be276e3af 100644 --- a/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp +++ b/ground/src/plugins/rawhid/pjrc_rawhid_win.cpp @@ -43,6 +43,7 @@ #include #include #include +#include #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 diff --git a/ground/src/plugins/rawhid/rawhid.cpp b/ground/src/plugins/rawhid/rawhid.cpp index 578d2cc1c..54c90d9fc 100644 --- a/ground/src/plugins/rawhid/rawhid.cpp +++ b/ground/src/plugins/rawhid/rawhid.cpp @@ -276,9 +276,7 @@ RawHID::RawHID(const QString &deviceName) //for each devices found, get serial number and close for(int i=0; i