From 8dfece7633e45f8160e98336ebe9b2bbca7ef898 Mon Sep 17 00:00:00 2001 From: Johannes Paehr Date: Sun, 30 Dec 2018 21:34:34 +0100 Subject: [PATCH] version bump of software suppport for Hardware Version 2 + added readme --- CMakeLists.txt | 34 ++++++++++- hardwaresetup.cpp | 149 +++++++++++++++------------------------------- hardwaresetup.h | 12 ++-- readme.txt | 21 +++++++ 4 files changed, 109 insertions(+), 107 deletions(-) create mode 100644 readme.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index deb4686..ae2cd52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,38 @@ find_package(Qt5Widgets CONFIG REQUIRED) find_package(Qt5Sql REQUIRED) +# check dependency for atmega + +find_path(LIBUSB_INCLUDE_DIR + NAMES "libusb.h" + PATH_SUFFIXES "include" "libusb" "libusb-1.0") + +set(CMAKE_PREFIX_PATH usr) + +find_library(LIBUSB_LIBRARY + NAMES + "usb" + "libusb" + "libusb-1.0" + "libusb-1.0.so" + PATH_SUFFIXES + "lib" + "lib32" + "lib64") +if(LIBUSB_LIBRARY) + message("usb lib found" ${LIBUSB_LIBRARY}) +else() + message("usb lib not found") +endif() + +if(LIBUSB_INCLUDE_DIR) + add_definitions(-DATMEGA) + message("usb include dir found") + include_directories(${LIBUSB_INCLUDE_DIR}) +else() + message("usb include dir not found") +endif() + # Populate a CMake variable with the sources set(helloworld_SRCS main.cpp @@ -73,5 +105,5 @@ set(helloworld_SRCS add_executable(Rennbahn ${helloworld_SRCS} ) # Use the Widgets module from Qt 5 -target_link_libraries(Rennbahn Qt5::Widgets Qt5::Core Qt5::Sql) +target_link_libraries(Rennbahn Qt5::Widgets Qt5::Core Qt5::Sql ${LIBUSB_LIBRARY}) target_link_libraries(Rennbahn ${Boost_LIBRARIES}) diff --git a/hardwaresetup.cpp b/hardwaresetup.cpp index 28f2820..b93a098 100644 --- a/hardwaresetup.cpp +++ b/hardwaresetup.cpp @@ -10,7 +10,7 @@ #include #include #ifdef ATMEGA -#include +#include #endif #define USB_LED_ON 1 @@ -37,6 +37,7 @@ HardwareSetup::HardwareSetup() { this->stop = 0; #ifdef ATMEGA this->handle = nullptr; + #endif this->fd = 0; } @@ -55,9 +56,12 @@ HardwareSetup::~HardwareSetup() { if (HARDWARE_VERSION == 2) { #ifdef ATMEGA if (this->handle) { - usb_close(this->handle); + + libusb_exit(nullptr); + + libusb_close(this->handle); } - this->handle = NULL; + this->handle = nullptr; #endif } else if (HARDWARE_VERSION == 3) { @@ -72,99 +76,42 @@ HardwareSetup::~HardwareSetup() { } #ifdef ATMEGA -/* Used to get descriptor strings for device identification */ -int HardwareSetup::usbGetDescriptorString(usb_dev_handle * dev, int index, - int langid, char * buf, int buflen) { - char buffer[256]; - int rval, i; +libusb_device_handle * HardwareSetup::usbOpenDevice(int vendor, int product) { - // make standard request GET_DESCRIPTOR, type string and given index - // (e.g. dev->iProduct) - rval = usb_control_msg( - dev, USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN, - USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, - sizeof(buffer), 1000); + libusb_device ** devs; + int r; + ssize_t cnt; - if (rval < 0) // error - return rval; - - // rval should be bytes read, but buffer[0] contains the actual response - // size - if ((unsigned char)buffer[0] < rval) - rval = (unsigned char)buffer[0]; // string is shorter than bytes read - - if (buffer[1] != USB_DT_STRING) // second byte is the data type - return 0; // invalid return type - - // we're dealing with UTF-16LE here so actual chars is half of rval, - // and index 0 doesn't count - rval /= 2; - - /* lossy conversion to ISO Latin1 */ - for (i = 1; i < rval && i < buflen; i++) { - if (buffer[2 * i + 1] == 0) - buf[i - 1] = buffer[2 * i]; - else - buf[i - 1] = '?'; /* outside of ISO Latin1 range */ + r = libusb_init(nullptr); + if (r < 0) { + // return r; + } + cnt = libusb_get_device_list(nullptr, &devs); + if (cnt < 0) { + libusb_exit(nullptr); + // return cnt; } - buf[i - 1] = 0; - return i - 1; -} + libusb_device * dev; + this->handle = nullptr; + int i = 0; -usb_dev_handle * HardwareSetup::usbOpenDevice(int vendor, char * vendorName, - int product, char * productName) { - struct usb_bus * bus; - struct usb_device * dev; - char devVendor[256], devProduct[256]; + while ((dev = devs[i++]) != nullptr) { + struct libusb_device_descriptor desc; + int r = libusb_get_device_descriptor(dev, &desc); + if (r < 0) { + fprintf(stderr, "failed to get device descriptor"); + } + if (desc.idVendor == vendor && desc.idProduct == product) { + cout << "id vendor passt und id product auch" << endl; + libusb_open(dev, &this->handle); - usb_dev_handle * handle = NULL; - - 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) { - if (dev->descriptor.idVendor != vendor || - dev->descriptor.idProduct != product) - continue; - - /* we need to open the device in order to query strings */ - if (!(handle = usb_open(dev))) { - fprintf(stderr, "Warning: cannot open USB device: %sn", - usb_strerror()); - continue; - } - - /* get vendor name */ - if (usbGetDescriptorString(handle, dev->descriptor.iManufacturer, - 0x0409, devVendor, - sizeof(devVendor)) < 0) { - fprintf(stderr, - "Warning: cannot query manufacturer for device: %sn", - usb_strerror()); - usb_close(handle); - continue; - } - - /* get product name */ - if (usbGetDescriptorString(handle, dev->descriptor.iProduct, 0x0409, - devProduct, sizeof(devVendor)) < 0) { - fprintf(stderr, "Warning: cannot query product for device: %sn", - usb_strerror()); - usb_close(handle); - continue; - } - - if (strcmp(devVendor, vendorName) == 0 && - strcmp(devProduct, productName) == 0) - return handle; - else - usb_close(handle); + cout << "handle: " << this->handle << endl; + return this->handle; } } - return NULL; + + return nullptr; } #endif @@ -188,18 +135,18 @@ void HardwareSetup::run() { // exit(1); // } if (!testmode) { + string vendorName = "test01"; + string productName = "USBExample"; while (!this->handle) { - this->handle = - usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample"); + this->handle = usbOpenDevice(0x16C0, 0x05DC); - if (this->handle == NULL) { + if (this->handle == nullptr) { fprintf(stderr, "Could not find USB device!\n"); } sleep(1); } } - int index; while (!this->stop) { // test mode @@ -242,14 +189,14 @@ void HardwareSetup::run() { } else { // normal mode -> no testmode - usleep(150000); // 100ms - + usleep(150000); // 150ms // while(1){ - nBytes = usb_control_msg( + nBytes = libusb_control_transfer( this->handle, - USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, + LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | + LIBUSB_ENDPOINT_IN, USB_DATA_OUT, 0, 0, - static_cast(static_cast(buffer)), + static_cast(static_cast(buffer)), sizeof(buffer), 5000); for (int i = 0; i < 6; i++) { @@ -296,10 +243,12 @@ void HardwareSetup::run() { //} if (nBytes < 0) { - fprintf(stderr, "USB error: %sn", usb_strerror()); + // fprintf(stderr, "USB error: %sn", libusb_error()); + // string vendorName = "test01"; + string vendorName = "test01"; + string productName = "USBExample"; while (!this->handle) { - this->handle = usbOpenDevice(0x16C0, "test01", 0x05DC, - "USBExample"); + this->handle = usbOpenDevice(0x16C0, 0x05DC); if (this->handle == nullptr) { fprintf(stderr, "Could not find USB device!\n"); diff --git a/hardwaresetup.h b/hardwaresetup.h index 037ec69..2ee80b8 100644 --- a/hardwaresetup.h +++ b/hardwaresetup.h @@ -7,7 +7,7 @@ #include #include #ifdef ATMEGA -#include +#include #endif struct TransStruct { uint16_t time; @@ -30,11 +30,11 @@ class HardwareSetup : public QThread { bool shellBefore; bool deaBefore; #ifdef ATMEGA - usb_dev_handle * handle; - int usbGetDescriptorString(usb_dev_handle * dev, int index, int langid, - char * buf, int buflen); - usb_dev_handle * usbOpenDevice(int vendor, char * vendorName, int product, - char * productName); + libusb_device_handle * handle; + libusb_context * usbContext; + int usbGetDescriptorString(libusb_device_handle * dev, int index, + int langid, char * buf, int buflen); + libusb_device_handle * usbOpenDevice(int vendor, int product); #endif int fd; diff --git a/readme.txt b/readme.txt new file mode 100644 index 0000000..5101fc0 --- /dev/null +++ b/readme.txt @@ -0,0 +1,21 @@ +Software to measure slotcar times + +1. Hardware Version 1 +Parallelport - not used anymore + +2. Hardware Version 2 +Based on VUSB https://www.obdev.at/products/vusb/index.html +libusb is required + udev rules needs to be created: + +place +SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="16c0", ATTRS{idProduct}=="05dc", GROUP="users" +in /etc/udev/rules.d/60-objdev.rules +and reload udev +# udevadm control --reload +if thats not working -> check +$ udevadm monitor --env +to solve the problem + +3. Hardware Version 3 +Based on STM32F07 +user need to be in group uucp \ No newline at end of file