Skip to content

Latest commit

 

History

History
168 lines (111 loc) · 4.01 KB

File metadata and controls

168 lines (111 loc) · 4.01 KB

Dbus

Table of Contents


1. Dbus Server Registration

in /register_server

Step 1. Code C++ file for Dbus Object

  • We organized carinformation.h and carinformation.cpp which are including variables and methods.

Step 2. Compile C++ to xml

  • Parse the C++ header file(carinformation.h) containing a QObject-derived class and produce the D-bus introspection XML.
qdbuscpp2xml -M -S carinformation.h -o carinformation.xml  

Step 3. Add xml

  • Add carinformation.xml file to register_server.pro file.
DBUS_ADAPTORS += carinformation.xml
  • If you build this program, carinformation_adaptor.h is automatically generated.
  • If you modify xml, you should clean and rebuild.

Step4. main.cpp

  • Declare CarInformation Object as carinfo
CarInformation *carinfo = new CarInformation();
  • Expose interface by CarInformationAdaptor (inherit QDBusAbstractAdaptor).
  • with this, method of carinfo can called by Dbus.
new CarInformationAdaptor(carinfo);
  • Register Service and Object to Dbus
QDBusConnection connection = QDBusConnection::sessionBus(); // Connect sessionBus 
connection.registerObject("/CarInformation", carinfo) // Register Object 
connection.registerService("org.team4.Des02") // Register service(Bus's name)

2. Send sensor data to Dbus

in /sender

  • We used python to send data such as speed, battery and brake.
  • To use dbus in python, we installed dbus-python.
pip install dbus-python 
  • Import dbus and dbus.service in python
import dbus
import dbus.service
  • Connect with an interface which is placed in Dbus.
  • If the server is not registered, it occurs an error.
bus = dbus.SessionBus() # Connect with sessionBus
service = bus.get_object("org.team4.Des02", "/CarInformation") # Connect with Object by information of service and object path
car_interface = dbus.Interface(service, "org.team4.Des02.CarInformation") # Link interface with Object
  • send data to Dbus.
  • By using interface we can access to method of Object(carinformation).
car_interface.setSpeed(speed_data)

3. Get data from Dbus

in /instrument_cluster

Step1. Copy carinformation.xml

  • Copy carinformation.xml from /register_server.

Step 2. Add xml

  • Add carinformation.xml file to instrument_cluster.pro file.
DBUS_INTERFACES += carinformation.xml
  • If you build this program, carinformation_interface.h is automatically generated.
  • If you modify xml, you should clean and rebuild.

Step 3. Set interface

  • Without registering server, project of instrument_cluster can’t get information of interface from Dbus.

  • So we include xml file to instrument_cluster, and it could notice which *interface will be registered.*

  • Declare in car2qml.h.

  • org::team4::Des02::CarInformation is placed in carinformation_interface.h.

org::team4::Des02::CarInformation *interface;
  • Definition in car2qml.cpp.
interface = new org::team4::Des02::CarInformation("org.team4.Des02", "/CarInformation", QDBusConnection::sessionBus());

Step 4. Get data

  • We can get data by interface.
speed = interface->call("getSpeed");
  • We can also check if the interface is valid or not.
void Car2Qml::checkDbusValid()
{
    if (!interface->isValid() && dbusstatus==false) {
        dbusstatus = true;
        emit dbusstatusChanged(true);
    }
    else if (interface->isValid() && dbusstatus==true) {
        dbusstatus = false;
        emit dbusstatusChanged(false);
    }
}

Reference

  1. D-Bus API Design Guidelines
  2. Qt D-Bus
  3. QDBusAbstractAdaptor Class