Test USB flash drive Plugging for Linux Qt in x86 (1) --- Hal and udisks

Source: Internet
Author: User
Using QT to detect USB flash drive plugging, the idea is to use qt's q6. messages can receive messages from Hal or udisks. These two methods are feasible on the X86 platform, but it won't work on the board. There may not be Hal in the Board, but Hal has to transplant some online materials to the board. Udisk does not have any transplanted data. Even worse, whatever Hal or udisks, the messages they receive are all sent by udev. Udev requires the support of sysfs file system, but most of the current arm boards are supported by the yaffs or yaffs2 file system. Although there are materials for udev transplantation, it cannot be transplanted to the yaffs or yaffs2 file systems. The last step cannot be configured.

It is impossible to implement this function on the tiny210 Development Board ?? Also, refer to my previous blog. The overall messaging mechanism for Linux to process USB is as follows: Kernel ---- hotplug mechanism ---- udev --------- hal/udisks -------- kernel. although many people on the Internet say that hotplug has been eliminated, it seems that hotplug is used on the development board now. Therefore, if you want to use QT to detect the insertion and removal of a USB flash drive on the arm, hotplug is the right path. Do not greedy for the functions of the q6!

In this article, we will talk about how to obtain Hal or udisks messages on a PC using the q6. If you don't talk nonsense, please check the program:

1, add in widget. h:

# Include <qtdbus/qdbusinterface>
# Include <qtdbus/qdbusconnection> // qdbusconnection

 

#define  DBUS_HAL_SERVICE          "org.freedesktop.Hal"#define DBUS_HAL_PATH              "/org/freedesktop/Hal/Manager"#define DBUS_HAL_INTERFACE         "org.freedesktop.Hal.Manager"#define DBUS_HAL_DEVICE_PATH       "/org/freedesktop/Hal/devices"#define DBUS_HAL_DEVICE_INTERFACE  "org.freedesktop.Hal.Device"

Private slots:
Void slotdeviceadded (qstring UDI );
Void slotdeviceremoved (qstring UDI );

2. Add in widget. cpp

Qdbusinterface * dbusinterface = new qdbusinterface (dbus_hal_service,
Dbus_hal_path,
Dbus_hal_interface,
Qdbusconnection: systembus (),
This );

If (! Dbusinterface-> isvalid ())
Qdebug () <TC-> tounicode ("initialization error, qdbusinterface not found! ") <Qdbusconnection: systembus (). lasterror (). Message ();
Connect (dbusinterface, signal (deviceadded (qstring), this, slot (slotdeviceadded (qstring )));
Connect (dbusinterface, signal (deviceremoved (qstring), this, slot (slotdeviceremoved (qstring )));

Then there are two slot functions:

Void Widget: slotdeviceadded (qstring UDI)
{
Static int num = 0;
Qdbusinterface * Device = new qdbusinterface (dbus_hal_service, Udi, dbus_hal_device_interface, qdbusconnection: systembus (), this );

If (! Device-> isvalid ())
Qdebug () <TC-> tounicode ("device creation failed! ");
Else
Num ++;
Qstring devicepath = device-> path ();
Qdebug () <TC-> tounicode ("identifying usb_device") <num <TC-> tounicode ("No. ---- path:") <devicepath;
If (devicepath. Contains ("volume "))
{
Qdebug () <TC-> tounicode ("Congratulations, you have found the USB flash drive. Name: ") <Udi;
Currentudi = Udi;
}
Else
Qdebug () <TC-> tounicode ("recognition result: Not a USB storage device! ");

}

Void Widget: slotdeviceremoved (qstring UDI)
{

If (UDI = currentudi | Udi. Contains ("volume "))
{
Qdebug () <TC-> tounicode ("your USB flash drive has been popped up! ") <Udi;
Currentudi = "";
}
Else
Qdebug () <"usb_device =" <Udi <TC-> tounicode ("pop-up ---");
}

 

Udi here is the path for registering a USB device! You can print it out and see that seven messages are registered during registration and seven messages are printed when the USB flash drive is pulled out. If there is no define in. H of the header file, you can directly write it in the new qdbusinterface In the widget. cpp file. Here, we recommend that you declare a variable first and then connect to the slot function. Some people do the following on the Internet:

/// Qdbusconnection: systembus (). Connect (dbus_hal_service,
// Dbus_hal_path,
// Dbus_hal_interface,
// "Deviceadded ",
// This,
// Slot (slotdeviceadded (qstring )));

// Qdbusconnection: systembus (). Connect (dbus_hal_service,
// Dbus_hal_path,
// Dbus_hal_interface,
// "Deviceremoved ",
// This,
// Slot (slotdeviceremoved (qstring )));

Yes, too!

Some people say that dbus_hal_service and dbus_hal_path do not explain the previous macro definition!

In addition, if you do not want to get a message through Hal, you can get the message through udisks, as shown below:

  1. Qdbusconnection: systembus (). Connect (
  2. "Org. freedesktop. udisks ",
  3. "/Org/freedesktop/udisks ",
  4. "Org. freedesktop. udisks ",
  5. "Deviceadded ",
  6. This, slot (deviceadded (qdbusobjectpath ));
  7.  
  8. Void deviceadded (qdbusobjectpath Dev ){
  9.  
  10. Qdebug () <"device added! "<Dev. Path ();
  11. }

In this way, we use udisks for detection. As for the principle, refer to my previous blog post.

There is a relatively complete source code publicly written on the Internet using this idea: http://download.csdn.net/detail/yanzi1225627/4107716. I have not tried it if I can run it.

In addition, the foreigner's http://qt-project.org/forums/viewthread/8595is better.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.