doushao1948 2016-01-26 17:51
浏览 260

Golang和DBUS

I would like to convert this Python code in Go:

#!/usr/bin/python3

import sys
import dbus

if (len(sys.argv) < 2):
    print("Usage: %s <modem> <ussd-string>" % (sys.argv[0]))
    sys.exit(1)

bus = dbus.SystemBus()
path = sys.argv[1]
ussdstring = sys.argv[2]

ussd = dbus.Interface(bus.get_object('org.ofono', path),
                      'org.ofono.SupplementaryServices')

properties = ussd.GetProperties()
state = properties["State"]

if state == "idle":
    result = ussd.Initiate(ussdstring, timeout=100)[1]
elif state == "user-response":
    result = ussd.Respond(ussdstring, timeout=100)
else:
    sys.exit(1);

properties = ussd.GetProperties()
state = properties["State"]
print('USSD RESPONSE:
', result)
print('USSD SESSION:
', state)

I made a try with the github.com/guelfey/go.dbus library:

package main

import (
    "fmt"
    "os"

    "github.com/guelfey/go.dbus"
)

func main() {
    fmt.Printf("DBUS Test.
")
    conn, err := dbus.SessionBus()
    if err != nil {
        fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
        os.Exit(1)
    }

    busObject := conn.Object("org.ofono", "/ril_0")
    fmt.Println("busObject:", busObject)
    var list []string
    busObject.Call("org.ofono.SupplementaryServices.Initiate", 0, "#101#").Store(&list)
    fmt.Println("list:", list)
    for _, v := range list {
        fmt.Println(v)
    }
}

But I got the following response:

DBUS Test.
Failed to connect to session bus: user: Current not implemented on linux/arm

Do you know how to use this DBUS library? Is this library the best one for go on ARM7?

Thank you

  • 写回答

1条回答 默认 最新

  • dongyan5141 2016-01-26 18:07
    关注

    This particular problem is right in the error message:

    ... user: Current not implemented on linux/arm"

    user.Current is only called for auth, so if you supply your own Auth method it won't call user.Current.

    It looks like you'll have to create your own conn though, instead of using the global sessionBus. (see the source of SessionBus and Conn.Auth for more details)

    conn, err := dbusSessionBusPrivate()
    if err != nil {
        return
    }
    
    auths := []dbus.Auth{dbus.AuthExternal(username), dbus.AuthCookieSha1(username, homedir)}
    if err := conn.Auth(auths); err != nil {
        conn.Close()
        return
    }
    

    You could also patch go.dbus to use an alternative method for finding the username and homedir for arm, like checking $USER and $HOME (or file an issue, or open a pull request).

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配