Need help with godbus.

242 views
Skip to first unread message

Ken Bassford

unread,
Nov 19, 2019, 11:21:17 AM11/19/19
to golang-nuts
Hi Folks,

I have been trying to implement godbus's Export function to no avail.  Looking at server.go (under the examples folder) is not very helpful as an inspection of export.go contains what appear to be cheater functions that enable the example to work.  Rename "Foo" to "Ping and it fails.

With that out of the way, I have not been able to find any current projects that utilize godbus server functions as a good example (I'm readily open to suggestions).  Snippets of what I currently have are as follows ...

First I created a structure to server as a basis for follow-on functions (since you can't expand *dbus.Conn as it is predefined in another package) ...
```
type dbusobj struct {
Conn *dbus.Conn
Iface string
Path dbus.ObjectPath
}
```
.. a function to populate the structure ...
```
func newDbusObject(conn *dbus.Conn, iface string, path dbus.ObjectPath) dbusobj {
pdbo := new(dbusobj)
pdbo.Conn = conn
pdbo.Iface = iface
pdbo.Path = path
return *pdbo
}
```
... and a simple function to export a named method interface ...
```
func (co dbusobj) addResponseMethod(methodname string) error {
var methodxml = `
<node>
<interface name="` + co.Iface + `">
<method name="` + methodname + `">
<arg direction="out" type="s"/>
</method>
</interface>` + introspect.IntrospectDataString +
`</node>`
return co.Conn.Export(introspect.Introspectable(methodxml), co.Path, co.Iface)
}
```
Implementation of the above looks like this ...
```
// Create our private dbus connection structure
dbc := newDbusObject(conn, dbusiface, dbuspath)
err = dbc.addResponseMethod("Ping")
if err != nil {
fmt.Println("Failed to export method to system bus:", err)
}
```
The function does not throw any error, but an introspection of the dbus shows ...
```
DBUS_SYSTEM_BUS_ADDRESS="unix:path=/var/run/dbusalt/system_bus_socket" dbus-send --system --print-reply --type=method_call --dest='org.openxt.ndvm.wifi' '/org/openxt/ndvm/wifi' org.freedesktop.DBus.Introspectable.Introspect
method return time=1574178759.805460 sender=:1.0 -> destination=:1.1 serial=4 reply_serial=2
   string "
<node>
<interface name="org.openxt.ndvm.wifi">
<method name="Foo">
<arg direction="out" type="s"/>
</method>
</interface>
<interface name="org.freedesktop.DBus.Introspectable">
<method name="Introspect">
<arg name="out" direction="out" type="s"/>
</method>
</interface>
</node>"

```
Calls to the unaltered "Foo" work, "Ping" does not (of course).

What am I doing wrong?
Are there some good (current) examples in a project where they have not abstracted the interface so much as to be useless for learning?
Is there a mailing list more suitable for these questions?  (I could not find one and I hate to open issues just to ask questions.)

It would be nice if someone updated the examples used under godbus to reflect what an actual user would have to go through to implement a full, usable method.

Ken Bassford

unread,
Nov 19, 2019, 12:01:54 PM11/19/19
to golang-nuts
(Note:  Also tried ...)
```
func (dbo dbusobj) addSimpleMethod(method interface{}) error {
err := dbo.Conn.Export(method, dbo.Path, dbo.Iface)
if err != nil {
return err
}
node := &introspect.Node{}
node.Name = dbo.Iface
iface := &introspect.Interface{}
iface.Name = dbo.Iface
mts := introspect.Methods(method)
iface.Methods = mts
node.Interfaces = append(node.Interfaces, *iface)
dbusXMLStr := introspect.NewIntrospectable(node)
err = dbo.Conn.Export(dbusXMLStr, dbo.Path, "org.freedesktop.DBus.Introspectable")
if err != nil {
return err
}
return nil
}
``` 

Implemented as ...
```
p := ping("Ping called!")
err = dbc.addSimpleMethod(p)
```
Same result.

Ken Bassford

unread,
Nov 20, 2019, 8:23:59 AM11/20/19
to golang-nuts
Hi Folks,

Made some progress.
I removed the "Foo" routine (it was the last call I made to Export()) and the "Ping" routine I added using the above routine worked.  So it would appear that each call to Export() overwrites the last, thus it is not cumilitive as I had expected.  Now I'm focused on ExportSubtree() and ExportSubtreeWithMap() calls if one wants to map more than one call to the DBus.

Jan Mercl

unread,
Nov 20, 2019, 8:48:41 AM11/20/19
to Ken Bassford, golang-nuts
On Tue, Nov 19, 2019 at 5:21 PM Ken Bassford <kenba...@gmail.com> wrote:

Please always post source code to the mail list in plain black on
white only, thank you.
Reply all
Reply to author
Forward
0 new messages