projects:qtusb
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
projects:qtusb [2013/05/30 16:10] – mkucia | projects:qtusb [2013/06/04 23:13] – [Building systemd (libudev)] mkucia | ||
---|---|---|---|
Line 174: | Line 174: | ||
</ | </ | ||
===== Linux ===== | ===== Linux ===== | ||
- | I am using Debian | + | {{: |
+ | I am using Debian 7 with LXDE. | ||
- | We cannot use windows-specific routines under Linux. To check if device is connected or not we will pool udev monitor. Additionally we need to set udev rules to allow non-root users access device. | + | We cannot use windows-specific routines under Linux. To check if device is connected or not we will pool udev monitor. |
+ | Additionally we need to set udev rules to allow non-root users access device. | ||
- | * Qt Linux source code [TODO] | + | At this moment |
+ | If true plug-and-play is required it is better to use standard USB class (for example CDC). | ||
- | ==== Building libusbx ==== | + | |
+ | ==== Linking libraries ==== | ||
+ | If your system is up-to-date you will most likely have both libraries installed. You can link them using following qmake code: | ||
+ | < | ||
+ | LIBS += -L/ | ||
+ | LIBS += -L/usr/lib/ -ludev | ||
+ | </ | ||
+ | ==== udev rules ==== | ||
- | - Download the code from libusbx source forge download page | + | Create a rule for device by making a new file: |
- | - Extract files | + | |
- | - Run standard sequence: | + | |
- | - '' | + | |
- | - '' | + | |
- | - '' | + | |
- | After that user should find '' | + | nano /etc/udev/rules.d/ |
- | It might be necessary to install required packages, see README. | + | with following content: |
+ | SUBSYSTEMS==" | ||
- | ==== Building systemd (libudev) ==== | + | This will allow non-root users to access the device. Also device will be accessible through path ''/ |
- | | + | |
- | - Extract | + | |
- | - Go to project directory | + | |
- | - Build library (additional packages might be required I had to build libdbus, see README): | + | |
- | - '' | + | |
- | - '' | + | |
- | - '' | + | |
- | TODO | + | |
- | ==== udev Rules ==== | + | Restart |
+ | There are more actions that udev rules can implement. For example launching application when device is connected. | ||
+ | |||
+ | |||
+ | ==== Detecting device mount/ | ||
+ | |||
+ | udev allow user applications to receive messages on changes in devices filesystem. | ||
+ | One can filter messages by subsystem and type. | ||
+ | In our case we want to receive USB device notifications from USB subsystem (it also emits USB interface messages). | ||
+ | Finally monitor is activated and file descriptor ([[wp> | ||
+ | |||
+ | < | ||
+ | // Create the udev object | ||
+ | if (!(udev = udev_new())) | ||
+ | [..] | ||
+ | |||
+ | // Set up a monitor to monitor usb devices | ||
+ | mon = udev_monitor_new_from_netlink(udev, | ||
+ | // We want only to receive information about usb devices | ||
+ | udev_monitor_filter_add_match_subsystem_devtype(mon, | ||
+ | udev_monitor_enable_receiving(mon); | ||
+ | fd = udev_monitor_get_fd(mon); | ||
+ | </ | ||
+ | |||
+ | The messages need to be pooled periodically. I created timer that fires every 250 [ms]. | ||
+ | < | ||
+ | // Start timer that will periodicaly check if hardware is connected | ||
+ | monitorTimer = new QTimer(this); | ||
+ | connect(monitorTimer, | ||
+ | monitorTimer-> | ||
+ | </ | ||
+ | |||
+ | Timer code checks if there is a new message. select() returns amount of messages. In this case function is non-blocking but one can implement timeout easy here. | ||
+ | < | ||
+ | void MainWindow:: | ||
+ | { | ||
+ | // Set up select | ||
+ | fd_set fds; | ||
+ | struct timeval tv; | ||
+ | int ret; | ||
+ | FD_ZERO(& | ||
+ | FD_SET(fd, &fds); | ||
+ | tv.tv_sec = 0; | ||
+ | tv.tv_usec = 0; | ||
+ | |||
+ | ret = select(fd+1, | ||
+ | |||
+ | // Check if our file descriptor has received data. | ||
+ | if (ret > 0 && FD_ISSET(fd, | ||
+ | { | ||
+ | </ | ||
+ | |||
+ | When new message is confirmed code check if it from our hardware by looking into vendor id and product id. | ||
+ | If device matches, code checks type of event and emits appropriate Qt signal. | ||
+ | |||
+ | < | ||
+ | // Get the device | ||
+ | if ( (dev = udev_monitor_receive_device(mon)) == NULL) | ||
+ | [...] | ||
+ | // Now check if the device is our rgb_led launchpad | ||
+ | |||
+ | const char* str_action = udev_device_get_action(dev); | ||
+ | const char* str_PID = udev_device_get_property_value(dev, | ||
+ | const char* str_VID = udev_device_get_property_value(dev, | ||
+ | |||
+ | [...] | ||
+ | // Compare strings and send signals | ||
+ | if ( (strcmp(STR_PRODUCT_ID, | ||
+ | { | ||
+ | if (strcmp(" | ||
+ | { | ||
+ | emit signal_DeviceConnected(); | ||
+ | } | ||
+ | else if (strcmp(" | ||
+ | { | ||
+ | emit signal_DeviceDisconnected(); | ||
+ | } | ||
+ | [...] | ||
+ | |||
+ | // If there are more events to process, do not wait for next tick! | ||
+ | if (ret-1 > 0) | ||
+ | monitorTimerTick(); | ||
+ | </ | ||
====== References ====== | ====== References ====== | ||
* http:// | * http:// | ||
* http:// | * http:// | ||
* http:// | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
+ | * http:// | ||
<WRAP todo> | <WRAP todo> | ||
* Build single software package for MCU, Win and Linux | * Build single software package for MCU, Win and Linux | ||
- | * add more references | + | * Finish description for Linux |
</ | </ |
projects/qtusb.txt · Last modified: 2014/07/06 21:34 by mkucia