func main() { bus, err := dbus.SystemBus() if err != nil { log.Fatal(err) } // run as a child if len(spawnUUID) != 0 && len(spawnDbusServiceId) != 0 && len(spawnDbusPath) != 0 { allJoynBridge := NewAllJoynBridge(bus) allJoynBridge.addService(spawnUUID, spawnDbusServiceId, spawnDbusService, spawnDbusPath, spawnAlljoynService) select {} // exit? return } res, err := bus.RequestName("com.devicehive.alljoyn.bridge", dbus.NameFlagDoNotQueue) if err != nil { log.Fatalf("Failed to request dbus name: %s", err) } if res != dbus.RequestNameReplyPrimaryOwner { log.Fatalf("Failed to request dbus name: %+v", res) } allJoynBridge := NewAllJoynBridge(bus) bus.Export(allJoynBridge, "/com/devicehive/alljoyn/bridge", "com.devicehive.alljoyn.bridge") n := &introspect.Node{ Interfaces: []introspect.Interface{ { Name: "com.devicehive.alljoyn.bridge", Methods: introspect.Methods(allJoynBridge), Signals: []introspect.Signal{}, }, }, } root := &introspect.Node{ Children: []introspect.Node{ { Name: "com/devicehive/alljoyn/bridge", }, }, } bus.Export(introspect.NewIntrospectable(n), "/com/devicehive/alljoyn/bridge", "org.freedesktop.DBus.Introspectable") bus.Export(introspect.NewIntrospectable(root), "/", "org.freedesktop.DBus.Introspectable") // workaroud for dbus issue #14 log.Printf("Bridge is Running.") select {} }
func main() { conn, err := dbus.SessionBus() if err != nil { panic(err) } reply, err := conn.RequestName("com.github.guelfey.Demo", dbus.NameFlagDoNotQueue) if err != nil { panic(err) } if reply != dbus.RequestNameReplyPrimaryOwner { fmt.Fprintln(os.Stderr, "name already taken") os.Exit(1) } propsSpec := map[string]map[string]*prop.Prop{ "com.github.guelfey.Demo": { "SomeInt": { int32(0), true, prop.EmitTrue, func(c *prop.Change) *dbus.Error { fmt.Println(c.Name, "changed to", c.Value) return nil }, }, }, } f := foo("Bar") conn.Export(f, "/com/github/guelfey/Demo", "com.github.guelfey.Demo") props := prop.New(conn, "/com/github/guelfey/Demo", propsSpec) n := &introspect.Node{ Name: "/com/github/guelfey/Demo", Interfaces: []introspect.Interface{ introspect.IntrospectData, prop.IntrospectData, { Name: "com.github.guelfey.Demo", Methods: introspect.Methods(f), Properties: props.Introspection("com.github.guelfey.Demo"), }, }, } conn.Export(introspect.NewIntrospectable(n), "/com/github/guelfey/Demo", "org.freedesktop.DBus.Introspectable") fmt.Println("Listening on com.github.guelfey.Demo / /com/github/guelfey/Demo ...") c := make(chan *dbus.Signal) conn.Signal(c) for _ = range c { } }
func main() { conf := WMATADbus{} if err := config.Load("wmatadbusd", &conf); err != nil { panic(err) } wmata.SetAPIKey(conf.APIKey) conn, err := dbus.SessionBus() if err != nil { panic(err) } reply, err := conn.RequestName("org.anized.wmata.Rail", dbus.NameFlagDoNotQueue) if err != nil { panic(err) } if reply != dbus.RequestNameReplyPrimaryOwner { fmt.Fprintln(os.Stderr, "name already taken") os.Exit(1) } wmata := WMATADbusInterface{} introspectedMethods := introspect.Methods(wmata) node := introspect.Node{ Name: "/org/anized/wmata", Interfaces: []introspect.Interface{ introspect.Interface{ Name: "org.anized.wmata.Rail", Methods: introspectedMethods, }, }, } export := introspect.NewIntrospectable(&node) // str, err := export.Introspect() // fmt.Printf("%s %s\n", str, err) conn.Export(wmata, "/org/anized/wmata/Rail", "org.anized.wmata.Rail") conn.Export( export, "/org/anized/wmata/Rail", "org.freedesktop.DBus.Introspectable", ) select {} }
// Introspect provides introspection data for the DBus service to start. // // propsSpec example: // // var propsSpec = map[string]map[string]*prop.Prop{ // SrvObj: { // "Restart": { // int32(0), // true, // prop.EmitTrue, // func(c *prop.Change) *dbus.Error { // fmt.Println(c.Name, "changed to", c.Value) // return nil // }, // }, // }, // } // func (load *Server) Introspect(obj interface{}, propsSpec map[string]map[string]*prop.Prop) *introspect.Node { var props []introspect.Property if propsSpec != nil { tmp := prop.New(load.Conn, dbus.ObjectPath(load.srvPath), propsSpec) props = tmp.Introspection(load.srvObj) } return &introspect.Node{ Name: load.srvPath, Interfaces: []introspect.Interface{ prop.IntrospectData, { Name: load.srvObj, Methods: introspect.Methods(obj), Properties: props, }, }, } }
// export main + introspectable DBus objects func exportDBusObject(bus *dbus.Conn, w *DBusWrapper) { bus.Export(w, ComDevicehiveCloudPath, ComDevicehiveCloudIface) // main service interface serviceInterface := introspect.Interface{ Name: ComDevicehiveCloudIface, Methods: introspect.Methods(w), Signals: []introspect.Signal{ { Name: "CommandReceived", Args: []introspect.Arg{ {"id", "t", "in"}, {"name", "s", "in"}, {"parameters", "s", "in"}, // JSON string }, }, }, } // main service node n := &introspect.Node{ Name: ComDevicehiveCloudPath, Interfaces: []introspect.Interface{ introspect.IntrospectData, prop.IntrospectData, serviceInterface}, } n_obj := introspect.NewIntrospectable(n) log.Tracef("%q introspectable: %s", ComDevicehiveCloudPath, n_obj) bus.Export(n_obj, ComDevicehiveCloudPath, "org.freedesktop.DBus.Introspectable") // root node root := &introspect.Node{ Children: []introspect.Node{ {Name: ComDevicehiveCloudPath}, }, } root_obj := introspect.NewIntrospectable(root) log.Tracef("%q introspectable: %s", "/", root_obj) bus.Export(root_obj, "/", "org.freedesktop.DBus.Introspectable") }
func wsImplementation(bus *dbus.Conn, config conf.Conf) { var conn *ws.Conn for { info, err := rest.GetApiInfo(config.URL) if err == nil { say.Verbosef("API info: %+v", info) c := ws.New(info.WebSocketServerUrl, config.DeviceID, config.SendNotificationQueueCapacity, func(m map[string]interface{}) { p := m["parameters"] params := "" if p != nil { b, err := json.Marshal(p) if err != nil { say.Fatalf("Could not generete JSON from command %+v\nWith error %s", m, err.Error()) } params = string(b) } say.Verbosef("COMMAND %s -> %s(%v)", info.WebSocketServerUrl, m["command"].(string), params) bus.Emit("/com/devicehive/cloud", "com.devicehive.cloud.CommandReceived", uint32(m["id"].(float64)), m["command"].(string), params) }) conn = &c if err == nil { break } } say.Infof("API info error: %s", err.Error()) time.Sleep(5 * time.Second) } w := NewDbusObjectWrapper(conn) go conn.Run(config.AccessKey, func() { conn.RegisterDevice(config.DeviceID, config.DeviceName, config.DeviceKey, config.NetworkName, config.NetworkKey, config.NetworkDesc) conn.Authenticate(config.DeviceID, config.DeviceKey) conn.SubscribeCommands() }) bus.Export(w, "/com/devicehive/cloud", DBusConnName) // Introspectable n := &introspect.Node{ Name: "/com/devicehive/cloud", Interfaces: []introspect.Interface{ introspect.IntrospectData, prop.IntrospectData, { Name: "com.devicehive.cloud", Methods: introspect.Methods(w), }, }, } bus.Export(introspect.NewIntrospectable(n), "/com/devicehive/cloud", "org.freedesktop.DBus.Introspectable") root := &introspect.Node{ Children: []introspect.Node{ { Name: "com/devicehive/cloud", }, }, } bus.Export(introspect.NewIntrospectable(root), "/", "org.freedesktop.DBus.Introspectable") select {} }
func main() { bus, err := dbus.SystemBus() if err != nil { log.Printf("Cannot get system bus with error: %s\n", err.Error()) log.Printf("Trying to use session bus for testing purposes...\n") if bus, err = dbus.SessionBus(); err != nil { log.Fatalf("Cannot get session bus with error: %s\n", err.Error()) return } } reply, err := bus.RequestName(DBusConnName, dbus.NameFlagDoNotQueue) switch { case err != nil: log.Fatalf("Cannot request name '%s' with error: %s\n", DBusConnName, err.Error()) case reply != dbus.RequestNameReplyPrimaryOwner: log.Fatalf("The name '%s' already taken.", DBusConnName) } // configFile, config, err := conf.FromArgs() // println(configFile) // switch { // case err != nil: // log.Fatalf("Cannot read configuration in `%s` with error: %s\n", configFile, err.Error()) // case configFile == "": // log.Printf("You should specify configuration file.\n Starting with test configuration: %+v\n", config) // default: // log.Printf("Starting DeviceHive gateway with configuration in '%s': %+v\n", configFile, config) // } // var conn *ws.Conn var w CloudWrapper // w = NewDbusObjectWrapper(conn) w = NewMockCloudWrapper(bus) // for { // info, err := rest.GetDHServerInfo(config.URL) // if err == nil { // c := ws.New(info.WebSocketServerUrl, config.DeviceID, func(m map[string]interface{}) { // log.Printf("Successfully received command: %s", m) // p := m["parameters"] // params := "{}" // if p != nil { // b, err := json.Marshal(p) // if err != nil { // log.Panic(err) // } // params = string(b) // } // log.Printf("Parameters: %v", params) // bus.Emit("/com/devicehive/cloud", // "com.devicehive.cloud.CommandReceived", // uint32(m["id"].(float64)), // m["command"].(string), // params) // }) // conn = &c // if err == nil { // break // } // } // time.Sleep(5 * time.Second) // } // go conn.Run(func() { // conn.RegisterDevice(config.DeviceID, config.DeviceName) // conn.Authenticate() // conn.SubscribeCommands() // }) go func() { id := uint32(0) w.NotifyCommandReceived(id, "scan/start", "{}") id += 1 time.Sleep(1 * time.Second) w.NotifyCommandReceived(id, "connect", `{"mac" : "b4994c6433be"}`) id += 1 time.Sleep(10 * time.Second) w.NotifyCommandReceived(id, "gatt/write", `{"mac" : "b4994c6433be", "uuid" : "F000AA1204514000b000000000000000", "value" : "01"}`) id += 1 time.Sleep(1 * time.Second) w.NotifyCommandReceived(id, "gatt/write", `{"mac" : "b4994c6433be", "uuid" : "F000AA1304514000b000000000000000", "value" : "0A"}`) id += 1 time.Sleep(1 * time.Second) w.NotifyCommandReceived(id, "gatt/notifications", `{"mac" : "b4994c6433be", "uuid" : "F000AA1104514000b000000000000000"}`) }() bus.Export(w, "/com/devicehive/cloud", DBusConnName) // Introspectable n := &introspect.Node{ Name: "/com/devicehive/cloud", Interfaces: []introspect.Interface{ introspect.IntrospectData, prop.IntrospectData, { Name: "com.devicehive.cloud", Methods: introspect.Methods(w), }, }, } bus.Export(introspect.NewIntrospectable(n), "/com/devicehive/cloud", "org.freedesktop.DBus.Introspectable") select {} }
func restImplementation(bus *dbus.Conn, config conf.Conf) { err := rest.DeviceRegisterEasy(config.URL, config.DeviceID, config.AccessKey, config.DeviceName, config.DeviceKey, config.NetworkName, config.NetworkKey, config.NetworkDesc) if err != nil { say.Infof("DeviceRegisterEasy error: %s", err.Error()) return } go func() { nControl := rest.NewPollAsync() cControl := rest.NewPollAsync() nOut := make(chan rest.DeviceNotificationResource, 16) cOut := make(chan rest.DeviceCmdResource, 16) go rest.DeviceNotificationPollAsync(config.URL, config.DeviceID, config.AccessKey, nOut, nControl) go rest.DeviceCmdPollAsync(config.URL, config.DeviceID, config.AccessKey, cOut, cControl) for { select { case n := <-nOut: parameters := "" if n.Parameters != nil { b, err := json.Marshal(n.Parameters) if err != nil { say.Infof("Could not generate JSON from parameters of notification %+v\nWith error %s", n, err.Error()) continue } parameters = string(b) } say.Verbosef("NOTIFICATION %s -> %s(%v)", config.URL, n.Notification, parameters) bus.Emit(restObjectPath, restCommandName, uint32(n.Id), n.Notification, parameters) case c := <-cOut: parameters := "" if c.Parameters != nil { b, err := json.Marshal(c.Parameters) if err != nil { say.Infof("Could not generate JSON from parameters of command %+v\nWith error %s", c, err.Error()) continue } parameters = string(b) } say.Verbosef("COMMAND %s -> %s(%v)", config.URL, c.Command, parameters) bus.Emit(restObjectPath, restCommandName, uint32(c.Id), c.Command, parameters) } } }() w := NewDbusRestWrapper(config) bus.Export(w, "/com/devicehive/cloud", DBusConnName) // Introspectable n := &introspect.Node{ Name: "/com/devicehive/cloud", Interfaces: []introspect.Interface{ introspect.IntrospectData, prop.IntrospectData, { Name: "com.devicehive.cloud", Methods: introspect.Methods(w), }, }, } bus.Export(introspect.NewIntrospectable(n), "/com/devicehive/cloud", "org.freedesktop.DBus.Introspectable") root := &introspect.Node{ Children: []introspect.Node{ { Name: "com/devicehive/cloud", }, }, } bus.Export(introspect.NewIntrospectable(root), "/", "org.freedesktop.DBus.Introspectable") select {} }
func main() { // go func() { // log.Println(http.ListenAndServe("localhost:6060", nil)) // }() var err error var bus *dbus.Conn bus, err = dbus.SystemBus() if err != nil { log.Panic(err) } reply, err := bus.RequestName("com.devicehive.bluetooth", dbus.NameFlagDoNotQueue) if err != nil { log.Panic(err) } if reply != dbus.RequestNameReplyPrimaryOwner { log.Fatal("name already taken") } w := NewBleDbusWrapper(bus) bus.Export(w, dbus.ObjectPath("/com/devicehive/bluetooth"), "com.devicehive.bluetooth") // Introspectable n := &introspect.Node{ Interfaces: []introspect.Interface{ { Name: "com.devicehive.bluetooth", Methods: introspect.Methods(w), Signals: []introspect.Signal{ introspect.Signal{ Name: "PeripheralDiscovered", Args: []introspect.Arg{{"id", "s", "out"}, {"name", "s", "out"}, {"rssi", "i", "out"}}, }, introspect.Signal{ Name: "PeripheralConnected", Args: []introspect.Arg{{"id", "s", "out"}}, }, introspect.Signal{ Name: "PeripheralDisconnected", Args: []introspect.Arg{{"id", "s", "out"}}, }, introspect.Signal{ Name: "NotificationReceived", Args: []introspect.Arg{{"mac", "s", "out"}, {"uuid", "s", "out"}, {"value", "s", "out"}}, }, introspect.Signal{ Name: "IndicationReceived", Args: []introspect.Arg{{"mac", "s", "out"}, {"uuid", "s", "out"}, {"value", "s", "out"}}, }, }, }, }, } root := &introspect.Node{ Children: []introspect.Node{ { Name: "com/devicehive/bluetooth", }, }, } bus.Export(introspect.NewIntrospectable(n), dbus.ObjectPath("/com/devicehive/bluetooth"), "org.freedesktop.DBus.Introspectable") bus.Export(introspect.NewIntrospectable(root), "/", "org.freedesktop.DBus.Introspectable") // workaroud for dbus issue #14 select {} }