forked from currantlabs/gatt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
sim_device.go
204 lines (165 loc) · 4.55 KB
/
sim_device.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package gatt
import (
"errors"
)
const (
DefaultMTU = 1024
)
type simDevice struct {
deviceHandler
s *Service
advertisedName string
}
func NewSimDeviceClient(service *Service, advertisedName string) *simDevice {
return &simDevice{
s: service,
advertisedName: advertisedName,
}
}
func (d *simDevice) Init(stateChanged func(Device, State)) error {
d.stateChanged = stateChanged
go stateChanged(d, StatePoweredOn)
return nil
}
func (d *simDevice) Advertise(a *AdvPacket) error {
return errors.New("Method not supported")
}
func (d *simDevice) AdvertiseNameAndServices(name string, ss []UUID) error {
return errors.New("Method not supported")
}
func (d *simDevice) AdvertiseIBeaconData(b []byte) error {
return errors.New("Method not supported")
}
func (d *simDevice) AdvertiseIBeacon(u UUID, major, minor uint16, pwr int8) error {
return errors.New("Method not supported")
}
func (d *simDevice) StopAdvertising() error {
return errors.New("Method not supported")
}
func (d *simDevice) RemoveAllServices() error {
return errors.New("Method not supported")
}
func (d *simDevice) AddService(s *Service) error {
return errors.New("Method not supported")
}
func (d *simDevice) SetServices(ss []*Service) error {
return errors.New("Method not supported")
}
func (d *simDevice) Scan(ss []UUID, dup bool) {
for _, s := range ss {
if s.Equal(d.s.UUID()) {
go d.peripheralDiscovered(
&simPeripheral{d},
&Advertisement{LocalName: d.advertisedName},
0,
)
}
}
}
func (d *simDevice) StopScanning() {
}
func (d *simDevice) Stop() error {
go d.stateChanged(d, StatePoweredOff)
return nil
}
func (d *simDevice) Connect(p Peripheral) {
go d.peripheralConnected(p, nil)
}
func (d *simDevice) CancelConnection(p Peripheral) {
go d.peripheralDisconnected(p, nil)
}
func (d *simDevice) Handle(hh ...Handler) {
for _, h := range hh {
h(d)
}
}
func (d *simDevice) Option(o ...Option) error {
return errors.New("Method not supported")
}
type simPeripheral struct {
d *simDevice
}
func (p *simPeripheral) Device() Device {
return p.d
}
func (p *simPeripheral) ID() string {
return "Sim ID"
}
func (p *simPeripheral) Name() string {
return "Sim"
}
func (p *simPeripheral) Services() []*Service {
return []*Service{p.d.s}
}
func (p *simPeripheral) DiscoverServices(ss []UUID) ([]*Service, error) {
for _, s := range ss {
if s.Equal(p.d.s.UUID()) {
return []*Service{p.d.s}, nil
}
}
return []*Service{}, nil
}
func (p *simPeripheral) DiscoverIncludedServices(ss []UUID, s *Service) ([]*Service, error) {
return nil, errors.New("Method not supported")
}
func (p *simPeripheral) DiscoverCharacteristics(cc []UUID, s *Service) ([]*Characteristic, error) {
requestedUUIDs := make(map[string]bool)
for _, c := range cc {
requestedUUIDs[c.String()] = true
}
foundChars := make([]*Characteristic, 0)
for _, c := range p.d.s.Characteristics() {
if _, present := requestedUUIDs[c.UUID().String()]; present {
foundChars = append(foundChars, c)
}
}
return foundChars, nil
}
func (p *simPeripheral) DiscoverDescriptors(d []UUID, c *Characteristic) ([]*Descriptor, error) {
return nil, errors.New("Method not supported")
}
func (p *simPeripheral) ReadCharacteristic(c *Characteristic) ([]byte, error) {
rhandler := c.GetReadHandler()
if rhandler != nil {
rsp := newResponseWriter(DefaultMTU)
req := &ReadRequest{}
rhandler.ServeRead(rsp, req)
return rsp.buf.Bytes(), nil
} else {
return nil, AttEcodeReadNotPerm
}
}
func (p *simPeripheral) ReadLongCharacteristic(c *Characteristic) ([]byte, error) {
return p.ReadCharacteristic(c)
}
func (p *simPeripheral) ReadDescriptor(d *Descriptor) ([]byte, error) {
return nil, errors.New("Method not supported")
}
func (p *simPeripheral) WriteCharacteristic(c *Characteristic, b []byte, noRsp bool) error {
whandler := c.GetWriteHandler()
if whandler != nil {
r := Request{}
if res := whandler.ServeWrite(r, b); res != 0 {
return AttEcode(res)
} else {
return nil
}
} else {
return AttEcodeWriteNotPerm
}
}
func (p *simPeripheral) WriteDescriptor(d *Descriptor, b []byte) error {
return errors.New("Method not supported")
}
func (p *simPeripheral) SetNotifyValue(c *Characteristic, f func(*Characteristic, []byte, error)) error {
return errors.New("Method not supported")
}
func (p *simPeripheral) SetIndicateValue(c *Characteristic, f func(*Characteristic, []byte, error)) error {
return errors.New("Method not supported")
}
func (p *simPeripheral) ReadRSSI() int {
return 0
}
func (p *simPeripheral) SetMTU(mtu uint16) error {
return errors.New("Method not supported")
}