This repository has been archived by the owner on Jul 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
app_char.go
170 lines (132 loc) · 3.5 KB
/
app_char.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package service
import (
"fmt"
"github.com/godbus/dbus/v5"
"github.com/muka/go-bluetooth/api"
"github.com/muka/go-bluetooth/bluez"
"github.com/muka/go-bluetooth/bluez/profile/gatt"
log "github.com/sirupsen/logrus"
)
type CharReadCallback func(c *Char, options map[string]interface{}) ([]byte, error)
type CharWriteCallback func(c *Char, value []byte) ([]byte, error)
type CharNotifyCallback func(c *Char, notify bool) error
type Char struct {
UUID string
app *App
service *Service
path dbus.ObjectPath
descr map[dbus.ObjectPath]*Descr
Properties *gatt.GattCharacteristic1Properties
iprops *api.DBusProperties
readCallback CharReadCallback
writeCallback CharWriteCallback
notifyCallback CharNotifyCallback
}
func (s *Char) Path() dbus.ObjectPath {
return s.path
}
func (s *Char) DBusProperties() *api.DBusProperties {
return s.iprops
}
func (s *Char) Interface() string {
return gatt.GattCharacteristic1Interface
}
func (s *Char) GetProperties() bluez.Properties {
descr := []dbus.ObjectPath{}
for dpath := range s.descr {
descr = append(descr, dpath)
}
s.Properties.Descriptors = descr
s.Properties.Service = s.Service().Path()
return s.Properties
}
func (c *Char) GetDescr() map[dbus.ObjectPath]*Descr {
return c.descr
}
func (c *Char) App() *App {
return c.app
}
func (c *Char) Service() *Service {
return c.service
}
func (s *Char) DBusObjectManager() *api.DBusObjectManager {
return s.App().DBusObjectManager()
}
func (s *Char) DBusConn() *dbus.Conn {
return s.App().DBusConn()
}
func (s *Char) RemoveDescr(descr *Descr) error {
if _, ok := s.descr[descr.Path()]; !ok {
return nil
}
err := descr.Remove()
if err != nil {
return err
}
delete(s.descr, descr.Path())
return nil
}
// Expose char to dbus
func (s *Char) Expose() error {
return api.ExposeDBusService(s)
}
// Remove char from dbus
func (s *Char) Remove() error {
return api.RemoveDBusService(s)
}
// NewDescr Init new descr
func (s *Char) NewDescr(uuid string) (*Descr, error) {
descr := new(Descr)
descr.UUID = s.App().GenerateUUID(uuid)
descr.app = s.App()
descr.char = s
descr.Properties = NewGattDescriptor1Properties(descr.UUID)
descr.path = dbus.ObjectPath(
fmt.Sprintf("%s/descriptor%d", s.Path(), len(s.GetDescr())),
)
iprops, err := api.NewDBusProperties(s.App().DBusConn())
if err != nil {
return nil, err
}
descr.iprops = iprops
return descr, nil
}
// AddDescr Add descr to dbus
func (s *Char) AddDescr(descr *Descr) error {
err := api.ExposeDBusService(descr)
if err != nil {
return err
}
s.descr[descr.Path()] = descr
err = s.DBusObjectManager().AddObject(descr.Path(), map[string]bluez.Properties{
descr.Interface(): descr.GetProperties(),
})
if err != nil {
return err
}
// update OM char too
err = s.DBusObjectManager().AddObject(s.Path(), map[string]bluez.Properties{
s.Interface(): s.GetProperties(),
})
if err != nil {
return err
}
log.Tracef("Added GATT Descriptor UUID=%s %s", descr.UUID, descr.Path())
err = s.App().ExportTree()
return err
}
// OnRead Set the Read callback, called when a client attempt to read
func (s *Char) OnRead(fx CharReadCallback) *Char {
s.readCallback = fx
return s
}
// OnWrite Set the Write callback, called when a client attempt to write
func (s *Char) OnWrite(fx CharWriteCallback) *Char {
s.writeCallback = fx
return s
}
// OnNotify Set the Notify callback, called when a client attempt to start/stop notifications for a characteristic
func (s *Char) OnNotify(fx CharNotifyCallback) *Char {
s.notifyCallback = fx
return s
}