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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题