Skip to content

Commit

Permalink
light refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mark2b committed Mar 18, 2023
1 parent f9028a7 commit c6fc6e0
Show file tree
Hide file tree
Showing 90 changed files with 9,547 additions and 106 deletions.
40 changes: 22 additions & 18 deletions bluez.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package bluez

import (
"fmt"
"github.com/godbus/dbus"
"github.com/godbus/dbus/introspect"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
"github.com/mark2b/bluez-connect/internal/log"
"github.com/pkg/errors"
"strings"
)
Expand Down Expand Up @@ -47,8 +48,8 @@ func (self *BlueZ) GetAdapter(hostId string) (a *BlueZAdapter, e error) {
func (self *BlueZ) GetAdapters() (adapters []*BlueZAdapter, e error) {
if managedObjects, err := self.getManagedObjects(); err == nil {
for path, o := range managedObjects {
if data, exists := o["org.bluez.Adapter1"]; exists {
adapter := &BlueZAdapter{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, bluez: self, data: data}
if adapterObject, exists := o["org.bluez.Adapter1"]; exists {
adapter := &BlueZAdapter{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, bluez: self, adapterObject: adapterObject}
adapters = append(adapters, adapter)
}
}
Expand All @@ -58,9 +59,9 @@ func (self *BlueZ) GetAdapters() (adapters []*BlueZAdapter, e error) {
return
}

func (self *BlueZ) RegisterAgent(agent Agent, path string, iface string) (e error) {
func (self *BlueZ) RegisterAgent(agent Agent, name string, path string, iface string) (e error) {
agentPath := dbus.ObjectPath(path)
if err := self.export(agent, agentPath, iface, Agent1Intro); err == nil {
if err := self.export(agent, name, agentPath, iface, Agent1Intro); err == nil {
agentManager := self.Conn.Object("org.bluez", dbus.ObjectPath("/org/bluez"))
if err := agentManager.Call("org.bluez.AgentManager1.RegisterAgent", 0, agentPath, agent.Capability()).Err; err == nil {

Expand Down Expand Up @@ -123,13 +124,12 @@ func (self *BlueZ) getManagedObjects() (managedObjects map[dbus.ObjectPath]map[s
return
}

func (self *BlueZ) export(instance interface{}, path dbus.ObjectPath, iface string, ifaceIntrospectable string) (e error) {
if reply, err := self.Conn.RequestName(iface,
dbus.NameFlagDoNotQueue&dbus.NameFlagReplaceExisting); err == nil {
func (self *BlueZ) export(instance interface{}, name string, path dbus.ObjectPath, iface string, ifaceIntrospectable string) (e error) {
if reply, err := self.Conn.RequestName(name, dbus.NameFlagDoNotQueue&dbus.NameFlagReplaceExisting); err == nil {
if reply == dbus.RequestNameReplyPrimaryOwner {
if err := self.Conn.Export(instance, path, iface); err == nil {
if err := self.Conn.Export(introspect.Introspectable(ifaceIntrospectable), path,
"org.freedesktop.DBus.Introspectable"); err == nil {
DBusIntrospectableInterface); err == nil {
} else {
e = err
}
Expand All @@ -146,10 +146,11 @@ func (self *BlueZ) export(instance interface{}, path dbus.ObjectPath, iface stri
}

func (self *BlueZ) exportWithProperties(instance interface{}, path dbus.ObjectPath, iface string, ifaceIntrospectable string) (e error) {
log.Log.Debugf("Exporting %s on %s", path, self.Object.Destination())
if err := self.Conn.Export(instance, path, iface); err == nil {
if err := self.Conn.Export(instance, path, "org.freedesktop.DBus.Properties"); err == nil {
if err := self.Conn.Export(instance, path, DBusPropertiesInterface); err == nil {
if err := self.Conn.Export(introspect.Introspectable(ifaceIntrospectable), path,
"org.freedesktop.DBus.Introspectable"); err == nil {
DBusIntrospectableInterface); err == nil {
} else {
e = err
}
Expand All @@ -162,14 +163,13 @@ func (self *BlueZ) exportWithProperties(instance interface{}, path dbus.ObjectPa
return
}

func (self *BlueZ) exportSingletonWithProperties(instance interface{}, path dbus.ObjectPath, iface string, ifaceIntrospectable string) (e error) {
if reply, err := self.Conn.RequestName(iface,
dbus.NameFlagDoNotQueue&dbus.NameFlagReplaceExisting); err == nil {
func (self *BlueZ) exportSingletonWithProperties(instance interface{}, name string, path dbus.ObjectPath, iface string, ifaceIntrospectable string) (e error) {
if reply, err := self.Conn.RequestName(name, dbus.NameFlagDoNotQueue&dbus.NameFlagReplaceExisting); err == nil {
if reply == dbus.RequestNameReplyPrimaryOwner {
if err := self.Conn.Export(instance, path, iface); err == nil {
if err := self.Conn.Export(instance, path, "org.freedesktop.DBus.Properties"); err == nil {
if err := self.Conn.Export(instance, path, DBusPropertiesInterface); err == nil {
if err := self.Conn.Export(introspect.Introspectable(ifaceIntrospectable), path,
"org.freedesktop.DBus.Introspectable"); err == nil {
DBusIntrospectableInterface); err == nil {
} else {
e = err
}
Expand Down Expand Up @@ -231,6 +231,10 @@ func MakeFailedError(err error) *dbus.Error {
}
}

func HasPrefix(path1 dbus.ObjectPath, path2 dbus.ObjectPath) bool {
func hasPrefix(path1 dbus.ObjectPath, path2 dbus.ObjectPath) bool {
return strings.HasPrefix(strings.ToLower(string(path1)), strings.ToLower(string(path2)))
}

func SetLogOutputMode(debug bool, verbose bool, systemd bool) {
log.SetOutputMode(debug, verbose, systemd)
}
17 changes: 9 additions & 8 deletions bluez_adapter.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package bluez

import (
"github.com/godbus/dbus"
"github.com/godbus/dbus/v5"
"github.com/pkg/errors"
"strings"
)

func (self *BlueZAdapter) GetDevices() (devices []*BlueZDevice, e error) {
if managedObjects, err := self.bluez.getManagedObjects(); err == nil {
for path, o := range managedObjects {
if HasPrefix(path, self.Object.Path()) {
if data, exists := o["org.bluez.Device1"]; exists {
device := &BlueZDevice{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, adapter: self, data: data}
if hasPrefix(path, self.Object.Path()) {
if deviceObject, exists := o["org.bluez.Device1"]; exists {
device := &BlueZDevice{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, adapter: self, deviceObject: deviceObject}
devices = append(devices, device)
}
}
Expand All @@ -25,10 +25,11 @@ func (self *BlueZAdapter) GetDevices() (devices []*BlueZDevice, e error) {
func (self *BlueZAdapter) GetGattManager() (gattManager *BlueZGattManager, e error) {
if managedObjects, err := self.bluez.getManagedObjects(); err == nil {
for path, o := range managedObjects {
if HasPrefix(path, self.Object.Path()) {
if data, exists := o["org.bluez.GattManager1"]; exists {
blueZGattManager = &BlueZGattManager{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, adapter: self, data: data}
if hasPrefix(path, self.Object.Path()) {
if gattManagerObject, exists := o["org.bluez.GattManager1"]; exists {
blueZGattManager = &BlueZGattManager{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, adapter: self, gattManagerObject: gattManagerObject}
gattManager = blueZGattManager

return
}
}
Expand Down Expand Up @@ -84,6 +85,6 @@ func (self *BlueZAdapter) FindPeripheral(nameOrAddress string) (foundDevice *Blu
type BlueZAdapter struct {
BlueZObject
bluez *BlueZ
data map[string]dbus.Variant
adapterObject map[string]dbus.Variant
advertisements map[dbus.ObjectPath]*LEAdvertisement1
}
6 changes: 3 additions & 3 deletions bluez_adapter_adv.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package bluez

import (
"github.com/godbus/dbus"
"github.com/godbus/dbus/v5"
"github.com/pkg/errors"
)

func (self *BlueZAdapter) StartAdvertise(path string, localName string, serviceUUIDs []string) (e error) {
func (self *BlueZAdapter) StartAdvertise(name string, path string, localName string, serviceUUIDs []string) (e error) {
advertisementObjectPath := dbus.ObjectPath(path)

if obj := self.Conn.Object("org.bluez", self.Object.Path()); obj != nil {
Expand All @@ -22,7 +22,7 @@ func (self *BlueZAdapter) StartAdvertise(path string, localName string, serviceU
}
self.advertisements[advertisementObjectPath] = advertisement

if err := self.bluez.exportSingletonWithProperties(advertisement, advertisementObjectPath, LEAdvertisement1Interface, LEAdvertisement1Intro); err == nil {
if err := self.bluez.exportSingletonWithProperties(advertisement, name, advertisementObjectPath, LEAdvertisement1Interface, LEAdvertisement1Intro); err == nil {
options := make(map[string]dbus.Variant)
if call := obj.Call("org.bluez.LEAdvertisingManager1.RegisterAdvertisement", 0, advertisementObjectPath, options); call.Err == nil {

Expand Down
2 changes: 1 addition & 1 deletion bluez_agent.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package bluez

import (
"github.com/godbus/dbus"
"github.com/godbus/dbus/v5"
)

type AgentCapability string
Expand Down
7 changes: 6 additions & 1 deletion bluez_characteristic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package bluez

import (
"fmt"
"github.com/godbus/dbus"
"github.com/godbus/dbus/v5"
"github.com/mark2b/bluez-connect/internal/log"
)

func (self *BlueZCharacteristic) ReadValue() (data []byte, e error) {
log.Log.Debugf("ReadValue %s", self.Object.Path())
options := make(map[string]dbus.Variant)
if call := self.Object.Call("org.bluez.GattCharacteristic1.ReadValue", 0, options); call.Err == nil {
if err := call.Store(&data); err == nil {
Expand All @@ -19,6 +21,7 @@ func (self *BlueZCharacteristic) ReadValue() (data []byte, e error) {
}

func (self *BlueZCharacteristic) WriteValue(value []byte) (e error) {
log.Log.Debugf("WriteValue %s", self.Object.Path())
options := make(map[string]dbus.Variant)
if call := self.Object.Call("org.bluez.GattCharacteristic1.WriteValue", 0, value, options); call.Err == nil {
} else {
Expand All @@ -28,6 +31,7 @@ func (self *BlueZCharacteristic) WriteValue(value []byte) (e error) {
}

func (self *BlueZCharacteristic) StartNotify() (e error) {
log.Log.Debugf("StartNotify %s", self.Object.Path())
if call := self.Object.Call("org.bluez.GattCharacteristic1.StartNotify", 0); call.Err == nil {
} else {
e = call.Err
Expand All @@ -36,6 +40,7 @@ func (self *BlueZCharacteristic) StartNotify() (e error) {
}

func (self *BlueZCharacteristic) StopNotify() (e error) {
log.Log.Debugf("StopNotify %s", self.Object.Path())
if call := self.Object.Call("org.bluez.GattCharacteristic1.StopNotify", 0); call.Err == nil {
} else {
e = call.Err
Expand Down
37 changes: 34 additions & 3 deletions bluez_const.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package bluez

import "github.com/godbus/dbus/introspect"
import "github.com/godbus/dbus/v5/introspect"

const (
DBusIntrospectableInterface = "org.freedesktop.DBus.Introspectable"
DBusPropertiesInterface = "org.freedesktop.DBus.Properties"
DBusPropertiesIntro = `
AdapterInterface = "org.bluez.Adapter1"

DBusPropertiesInterface = "org.freedesktop.DBus.Properties"
DBusPropertiesIntro = `
<interface name="org.freedesktop.DBus.Properties">
<method name="Get">
<arg name="interface" type="s" direction="in" />
Expand Down Expand Up @@ -39,6 +41,10 @@ const (
GattCharacteristic1Intro = `<node>
<interface name="org.bluez.GattCharacteristic1">
<property name="UUID" type="s" access="read" />
<property name="Service" type="o" access="read" />
<property name="Value" type="ay" access="read" />
<property name="Notifying" type="b" access="read" />
<property name="Flags" type="as" access="read" />
</interface>` + DBusPropertiesIntro + introspect.IntrospectDataString + `</node>`

ObjectManagerInterface = "org.freedesktop.DBus.ObjectManager"
Expand Down Expand Up @@ -132,4 +138,29 @@ const (
<method name="Cancel" />
</interface>` + DBusPropertiesIntro + introspect.IntrospectDataString + `</node>`

Profile1Interface = "org.bluez.Profile1"
Profile1Intro = `
<node>
<interface name='org.bluez.Profile1'>
<method name='Release' />
<method name='NewConnection'>
<arg type='o' name='device' direction='in' />
<arg type='h' name='fd' direction='in' />
<arg type='a{sv}' name='fd_properties' direction='in' />
</method>
<method name='RequestDisconnection'>
<arg type='o' name='device' direction='in' />
</method>
</interface>` +
DBusPropertiesIntro + introspect.IntrospectDataString + `
</node>`

GattService1PropUUID = "UUID"
GattService1PropPrimary = "Primary"

GattCharacteristic1PropUUID = "UUID"
GattCharacteristic1PropService = "Service"
GattCharacteristic1PropValue = "Value"
GattCharacteristic1PropNotifying = "Notifying"
GattCharacteristic1PropFlags = "Flags"
)
26 changes: 13 additions & 13 deletions bluez_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bluez

import (
"fmt"
"github.com/godbus/dbus"
"github.com/godbus/dbus/v5"
"github.com/pkg/errors"
"strings"
)
Expand All @@ -12,12 +12,12 @@ func (self *BlueZDevice) GetCharacteristic(uuid string, serviceUuid string) (fou
serviceUuid = strings.ToLower(serviceUuid)
if managedObjects, err := self.adapter.bluez.getManagedObjects(); err == nil {
for path, o := range managedObjects {
if HasPrefix(path, self.Object.Path()) {
if hasPrefix(path, self.Object.Path()) {
if data, exists := o["org.bluez.GattService1"]; exists {
if strings.ToLower(data["UUID"].Value().(string)) == serviceUuid {
foundService = &BlueZService{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, device: self, data: data}
for path, o := range managedObjects {
if HasPrefix(path, self.Object.Path()) {
if hasPrefix(path, self.Object.Path()) {
if data, exists := o["org.bluez.GattCharacteristic1"]; exists {
if strings.ToLower(data["UUID"].Value().(string)) == uuid {
foundCharacteristic = &BlueZCharacteristic{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, service: foundService, data: data}
Expand All @@ -44,7 +44,7 @@ func (self *BlueZDevice) GetCharacteristic(uuid string, serviceUuid string) (fou
func (self *BlueZDevice) GetServices() (services []BlueZService, e error) {
if managedObjects, err := self.adapter.bluez.getManagedObjects(); err == nil {
for path, o := range managedObjects {
if HasPrefix(path, self.Object.Path()) {
if hasPrefix(path, self.Object.Path()) {
if data, exists := o["org.bluez.GattService1"]; exists {
service := BlueZService{BlueZObject: BlueZObject{self.Conn, self.Conn.Object("org.bluez", path)}, device: self, data: data}
services = append(services, service)
Expand Down Expand Up @@ -94,58 +94,58 @@ func (self *BlueZDevice) Disconnect() (e error) {
}

func (self *BlueZDevice) Name() (value string) {
if v, exists := self.data["Name"]; exists {
if v, exists := self.deviceObject["Name"]; exists {
value = v.Value().(string)
}
return
}

func (self *BlueZDevice) Connected() (value bool) {
if v, exists := self.data["Connected"]; exists {
if v, exists := self.deviceObject["Connected"]; exists {
value = v.Value().(bool)
}
return
}

func (self *BlueZDevice) ServicesResolved() (value bool) {
if v, exists := self.data["ServicesResolved"]; exists {
if v, exists := self.deviceObject["ServicesResolved"]; exists {
value = v.Value().(bool)
}
return
}

func (self *BlueZDevice) Address() (value string) {
if v, exists := self.data["Address"]; exists {
if v, exists := self.deviceObject["Address"]; exists {
value = v.Value().(string)
}
return
}

func (self *BlueZDevice) UUIDs() (value []string) {
if v, exists := self.data["UUIDs"]; exists {
if v, exists := self.deviceObject["UUIDs"]; exists {
value = v.Value().([]string)
}
return
}

func (self *BlueZDevice) Refresh() (e error) {
if managedObject, err := self.adapter.bluez.getManagedObject(self.Object.Path()); err == nil && len(managedObject) != 0 {
self.data = managedObject["org.bluez.Device1"]
self.deviceObject = managedObject["org.bluez.Device1"]
} else {
e = err
}
return
}

func (self *BlueZDevice) ToDisplayString() (text string) {
for k, v := range self.data {
for k, v := range self.deviceObject {
text += fmt.Sprintf("\t%v %v\n", k, v)
}
return
}

type BlueZDevice struct {
BlueZObject
adapter *BlueZAdapter
data map[string]dbus.Variant
adapter *BlueZAdapter
deviceObject map[string]dbus.Variant
}
Loading

0 comments on commit c6fc6e0

Please sign in to comment.