英文:
Golang and DBUS
问题
我将帮您将这段Python代码翻译成Go语言:
package main
import (
"fmt"
"os"
"github.com/guelfey/go.dbus"
)
func main() {
fmt.Printf("DBUS测试。\n")
conn, err := dbus.SessionBus()
if err != nil {
fmt.Fprintln(os.Stderr, "无法连接到会话总线:", 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)
}
}
但是我注意到您在ARM7上遇到了问题。这个错误信息表示在Linux/ARM上当前尚未实现该功能。对于ARM7,您可以尝试使用github.com/godbus/dbus
库,它是Go语言中使用DBus的另一个流行选择。您可以使用以下代码进行尝试:
package main
import (
"fmt"
"os"
"github.com/godbus/dbus/v5"
)
func main() {
fmt.Printf("DBUS测试。\n")
conn, err := dbus.SessionBus()
if err != nil {
fmt.Fprintln(os.Stderr, "无法连接到会话总线:", err)
os.Exit(1)
}
busObject := conn.Object("org.ofono", "/ril_0")
fmt.Println("busObject:", busObject)
call := busObject.CallWithContext(nil, "org.ofono.SupplementaryServices.Initiate", 0, "#101#")
if call.Err != nil {
fmt.Fprintln(os.Stderr, "调用失败:", call.Err)
os.Exit(1)
}
list := call.Body[0].([]string)
fmt.Println("list:", list)
for _, v := range list {
fmt.Println(v)
}
}
这个代码使用了github.com/godbus/dbus
库,并对调用进行了一些修改以适应该库的使用方式。请注意,您需要先使用go get github.com/godbus/dbus/v5
命令安装该库。
希望这可以帮助到您!如果您有任何其他问题,请随时问我。
英文:
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:\n', result)
print('USSD SESSION:\n', 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.\n")
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
得分: 1
这个特定的问题在错误信息中已经指出:
> ... user: Current not implemented on linux/arm"
user.Current
只在进行身份验证时调用,所以如果你提供了自己的 Auth
方法,它就不会调用 user.Current
。
看起来你需要创建自己的连接,而不是使用全局的 sessionBus
。(详见 SessionBus
和 Conn.Auth
的源代码以获取更多细节)
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
}
你也可以修改 go.dbus
的代码,使用另一种方法来找到 arm 架构下的用户名和主目录,比如检查 $USER
和 $HOME
(或者提交一个问题,或者发起一个拉取请求)。
英文:
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论