forked from muka/go-bluetooth
/
GattDescriptor1.go
81 lines (69 loc) · 2.2 KB
/
GattDescriptor1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package profile
import (
"errors"
"github.com/phommel/go-bluetooth/bluez"
"github.com/fatih/structs"
"github.com/godbus/dbus"
)
// NewGattDescriptor1 create a new GattDescriptor1 client
func NewGattDescriptor1(path string) (*GattDescriptor1, error) {
a := new(GattDescriptor1)
a.client = bluez.NewClient(
&bluez.Config{
Name: "org.bluez",
Iface: bluez.GattDescriptor1Interface,
Path: path,
Bus: bluez.SystemBus,
},
)
a.Properties = new(GattDescriptor1Properties)
_, err := a.GetProperties()
return a, err
}
// GattDescriptor1 client
type GattDescriptor1 struct {
client *bluez.Client
Properties *GattDescriptor1Properties
}
// GattDescriptor1Properties exposed properties for GattDescriptor1
type GattDescriptor1Properties struct {
Value []byte `dbus:"emit"`
Characteristic dbus.ObjectPath
UUID string
Flags []string
}
//ToMap serialize properties
func (d *GattDescriptor1Properties) ToMap() (map[string]interface{}, error) {
if !d.Characteristic.IsValid() {
return nil, errors.New("GattDescriptor1Properties: Characteristic ObjectPath is not valid")
}
return structs.Map(d), nil
}
// Close the connection
func (d *GattDescriptor1) Close() {
d.client.Disconnect()
}
//Register for changes signalling
func (d *GattDescriptor1) Register() (chan *dbus.Signal, error) {
return d.client.Register(d.client.Config.Path, bluez.PropertiesInterface)
}
//Unregister for changes signalling
func (d *GattDescriptor1) Unregister(signal chan *dbus.Signal) error {
return d.client.Unregister(d.client.Config.Path, bluez.PropertiesInterface, signal)
}
//GetProperties load all available properties
func (d *GattDescriptor1) GetProperties() (*GattDescriptor1Properties, error) {
err := d.client.GetProperties(d.Properties)
return d.Properties, err
}
//ReadValue read a value from a descriptor
func (d *GattDescriptor1) ReadValue(options map[string]dbus.Variant) ([]byte, error) {
var b []byte
err := d.client.Call("ReadValue", 0, options).Store(&b)
return b, err
}
//WriteValue write a value to a characteristic
func (d *GattDescriptor1) WriteValue(b []byte, options map[string]dbus.Variant) error {
err := d.client.Call("WriteValue", 0, b, options).Store()
return err
}