0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

dbus Tips

Last updated at Posted at 2025-08-23

System Bus

/etc/dbus-1/system.d/com.example.calculator.conf
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <policy user="<username>">
    <allow own="com.example.calculator" />
    <allow send_destination="com.example.calculator" send_interface="org.freedesktop.DBus.Introspectable" send_member="Introspect" />
    <allow send_destination="com.example.calculator" send_interface="com.example.calculator_interface" send_member="Add" />
  </policy>
</busconfig>
calculator.py
import dbus
import dbus.service
import dbus.mainloop.glib
from gi.repository import GLib

mainloop = None

class Calculator(dbus.service.Object):
    def __init__(self,bus):
        self.path = "/com/example/calculator"
        super().__init__(bus,self.path)
    @dbus.service.method("com.example.calculator_interface",in_signature="ii",out_signature="i")
    def Add(self,a1,a2):
        sum = a1+a2
        print(a1," + ",a2," = ",sum)
        return sum


dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SystemBus()

name = dbus.service.BusName("com.example.calculator",bus)

calc = Calculator(bus)
mainloop = GLib.MainLoop()
print("waiting for some calculations to do...")
try:
    mainloop.run()
except KeyboardInterrupt:
    pass

# システムバスに登録されてるか確認
$ busctl tree --system | grep example
Service com.example.calculator:
  └─ /com/example
    └─ /com/example/calculator
# インターフェースやメソッド等を表示
$ busctl introspect --system com.example.calculator /com/example/calculator
NAME                                TYPE      SIGNATURE RESULT/VALUE FLAGS
com.example.calculator_interface    interface -         -            -
.Add                                method    ii        i            -
org.freedesktop.DBus.Introspectable interface -         -            -
.Introspect                         method    -         s            -
# Addメソッドを実行
$ dbus-send --print-reply --system --type=method_call  --dest=com.example.calculator   /com/
example/calculator   com.example.calculator_interface.Add   int32:1 int32:2
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?