Search code examples
linuxipcdbus

How can I define a parametric signal on d-bus?


I am currently developing a sensor information distributor inside a linux system. I receive serialized message via UART interface that provides information of many sensors and this information is distributed to various Application running on the same system.

Each sensor provides the same information but from different places.

Therefore from each sensor I receive; ID, data1,data2,dataN

Applications running need information of only some specific sensors. This means that Applications A may be interested in sensor 10 and 15 while Application B may be interested in sensor 10.

I am planning to send the information from my "Distributor" to the Application via d-BUS. This meant that

Application A will subscribe to signal_10 and signal_15 which contains data1,data2,dataN from sensor 10 and 15 respectively, while Application B would only subscribe to signal_10.

This would allow to have each Application receiving only the relevant information and avoiding parsing of relevant signals in each application. Furthermore it will allow adding new application and subscribe to any combination of the sensors.

From what I can see in the dbus specification signal names need to be clearly defined.

<signal name="changed_value_10">
      <arg type="s" name="change_source_name" direction="out"/>
</signal>

Thus defining a parametric signal is not possible. I would like to define a generic signal and reuse it for all the sensors. As concept:

<signal name="changed_value_%ID%">
      <arg type="s" name="change_source_name" direction="out"/>
</signal>

Even though I can define all the +200 sensor into the introspection XML by hand, I do not find it elegant. Is there a way to define a signal such a way that it can be reused to define multiple signals? Similarly to a MACRO in C programming or by any other means.

Notes:

  • I have decided to use d-bus because I am somewhat familiar with it and will be using it in other application running on the system. However, I am open to any feedback or other options.
  • Applications are written in C

Solution

  • Even though I can define all the +200 sensor into the introspection XML by hand, I do not find it elegant. Is there a way to define a signal such a way that it can be reused to define multiple signals?

    There is no way to do macros in the GDBus introspection XML format itself. You could generate the XML file using a custom preprocessing program, before passing the generated output to gdbus-codegen.

    If all the signal signatures are going to be identical, and only the name is going to vary, I’d consider just having one signal and providing the sensor ID as an argument in that signal:

    <signal name="changed_value">
          <arg type="s" name="sensor_id" direction="out"/>
          <arg type="s" name="change_source_name" direction="out"/>
    </signal>
    

    Your client code can use arg0 matching in its signal subscriptions so that it only receives signals for the sensors it’s interested in.