- We organized
carinformation.h
andcarinformation.cpp
which are including variables and methods.
- 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
- Add
carinformation.xml
file toregister_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.
- Declare CarInformation Object as
carinfo
CarInformation *carinfo = new CarInformation();
- Expose interface by
CarInformationAdaptor
(inheritQDBusAbstractAdaptor
). - 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)
- 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
anddbus.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)
- Copy
carinformation.xml
from/register_server
.
- Add
carinformation.xml
file toinstrument_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.
-
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 incarinformation_interface.h
.
org::team4::Des02::CarInformation *interface;
- Definition in
car2qml.cpp
.
interface = new org::team4::Des02::CarInformation("org.team4.Des02", "/CarInformation", QDBusConnection::sessionBus());
- 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);
}
}