Debian Codegraph @

device_linux.go

raw symbols SCIP
1 package gatt
2
3 import (
4 "encoding/binary"
5 "net"
6
7 "github.com/paypal/gatt/linux"
8 "github.com/paypal/gatt/linux/cmd"
9 )
10
11 type device struct {
12 deviceHandler
13
14 hci *linux.HCI
15 state State
16
17 // All the following fields are only used peripheralManager (server) implementation.
18 svcs []*Service
19 attrs *attrRange
20
21 devID int
22 chkLE bool
23 maxConn int
24
25 advData *cmd.LESetAdvertisingData
26 scanResp *cmd.LESetScanResponseData
27 advParam *cmd.LESetAdvertisingParameters
28 scanParam *cmd.LESetScanParameters
29 }
30
31 func NewDevice(opts ...Option) (Device, error) {
32 d := &device{
33 maxConn: 1, // Support 1 connection at a time.
34 devID: -1, // Find an available HCI device.
35 chkLE: true, // Check if the device supports LE.
36
37 advParam: &cmd.LESetAdvertisingParameters{
38 AdvertisingIntervalMin: 0x800, // [0x0800]: 0.625 ms * 0x0800 = 1280.0 ms
39 AdvertisingIntervalMax: 0x800, // [0x0800]: 0.625 ms * 0x0800 = 1280.0 ms
40 AdvertisingType: 0x00, // [0x00]: ADV_IND, 0x01: DIRECT(HIGH), 0x02: SCAN, 0x03: NONCONN, 0x04: DIRECT(LOW)
41 OwnAddressType: 0x00, // [0x00]: public, 0x01: random
42 DirectAddressType: 0x00, // [0x00]: public, 0x01: random
43 DirectAddress: [6]byte{}, // Public or Random Address of the device to be connected
44 AdvertisingChannelMap: 0x7, // [0x07] 0x01: ch37, 0x2: ch38, 0x4: ch39
45 AdvertisingFilterPolicy: 0x00,
46 },
47 scanParam: &cmd.LESetScanParameters{
48 LEScanType: 0x01, // [0x00]: passive, 0x01: active
49 LEScanInterval: 0x0010, // [0x10]: 0.625ms * 16
50 LEScanWindow: 0x0010, // [0x10]: 0.625ms * 16
51 OwnAddressType: 0x00, // [0x00]: public, 0x01: random
52 ScanningFilterPolicy: 0x00, // [0x00]: accept all, 0x01: ignore non-white-listed.
53 },
54 }
55
56 d.Option(opts...)
57 h, err := linux.NewHCI(d.devID, d.chkLE, d.maxConn)
58 if err != nil {
59 return nil, err
60 }
61
62 d.hci = h
63 return d, nil
64 }
65
66 func (d *device) Init(f func(Device, State)) error {
67 d.hci.AcceptMasterHandler = func(pd *linux.PlatData) {
68 a := pd.Address
69 c := newCentral(d.attrs, net.HardwareAddr([]byte{a[5], a[4], a[3], a[2], a[1], a[0]}), pd.Conn)
70 if d.centralConnected != nil {
71 d.centralConnected(c)
72 }
73 c.loop()
74 if d.centralDisconnected != nil {
75 d.centralDisconnected(c)
76 }
77 }
78 d.hci.AcceptSlaveHandler = func(pd *linux.PlatData) {
79 p := &peripheral{
80 d: d,
81 pd: pd,
82 l2c: pd.Conn,
83 reqc: make(chan message),
84 quitc: make(chan struct{}),
85 sub: newSubscriber(),
86 }
87 if d.peripheralConnected != nil {
88 go d.peripheralConnected(p, nil)
89 }
90 p.loop()
91 if d.peripheralDisconnected != nil {
92 d.peripheralDisconnected(p, nil)
93 }
94 }
95 d.hci.AdvertisementHandler = func(pd *linux.PlatData) {
96 a := &Advertisement{}
97 a.unmarshall(pd.Data)
98 a.Connectable = pd.Connectable
99 p := &peripheral{pd: pd, d: d}
100 if d.peripheralDiscovered != nil {
101 pd.Name = a.LocalName
102 d.peripheralDiscovered(p, a, int(pd.RSSI))
103 }
104 }
105 d.state = StatePoweredOn
106 d.stateChanged = f
107 go d.stateChanged(d, d.state)
108 return nil
109 }
110
111 func (d *device) Stop() error {
112 d.state = StatePoweredOff
113 defer d.stateChanged(d, d.state)
114 return d.hci.Close()
115 }
116
117 func (d *device) AddService(s *Service) error {
118 d.svcs = append(d.svcs, s)
119 d.attrs = generateAttributes(d.svcs, uint16(1)) // ble attrs start at 1
120 return nil
121 }
122
123 func (d *device) RemoveAllServices() error {
124 d.svcs = nil
125 d.attrs = nil
126 return nil
127 }
128
129 func (d *device) SetServices(s []*Service) error {
130 d.RemoveAllServices()
131 d.svcs = append(d.svcs, s...)
132 d.attrs = generateAttributes(d.svcs, uint16(1)) // ble attrs start at 1
133 return nil
134 }
135
136 func (d *device) Advertise(a *AdvPacket) error {
137 d.advData = &cmd.LESetAdvertisingData{
138 AdvertisingDataLength: uint8(a.Len()),
139 AdvertisingData: a.Bytes(),
140 }
141
142 if err := d.update(); err != nil {
143 return err
144 }
145
146 return d.hci.SetAdvertiseEnable(true)
147 }
148
149 func (d *device) AdvertiseNameAndServices(name string, uu []UUID) error {
150 a := &AdvPacket{}
151 a.AppendFlags(flagGeneralDiscoverable | flagLEOnly)
152 a.AppendUUIDFit(uu)
153
154 if len(a.b)+len(name)+2 < MaxEIRPacketLength {
155 a.AppendName(name)
156 d.scanResp = nil
157 } else {
158 a := &AdvPacket{}
159 a.AppendName(name)
160 d.scanResp = &cmd.LESetScanResponseData{
161 ScanResponseDataLength: uint8(a.Len()),
162 ScanResponseData: a.Bytes(),
163 }
164 }
165
166 return d.Advertise(a)
167 }
168
169 func (d *device) AdvertiseIBeaconData(b []byte) error {
170 a := &AdvPacket{}
171 a.AppendFlags(flagGeneralDiscoverable | flagLEOnly)
172 a.AppendManufacturerData(0x004C, b)
173 d.advData = &cmd.LESetAdvertisingData{
174 AdvertisingDataLength: uint8(a.Len()),
175 AdvertisingData: a.Bytes(),
176 }
177
178 return d.Advertise(a)
179 }
180
181 func (d *device) AdvertiseIBeacon(u UUID, major, minor uint16, pwr int8) error {
182 b := make([]byte, 23)
183 b[0] = 0x02 // Data type: iBeacon
184 b[1] = 0x15 // Data length: 21 bytes
185 copy(b[2:], reverse(u.b)) // Big endian
186 binary.BigEndian.PutUint16(b[18:], major) // Big endian
187 binary.BigEndian.PutUint16(b[20:], minor) // Big endian
188 b[22] = uint8(pwr) // Measured Tx Power
189 return d.AdvertiseIBeaconData(b)
190 }
191
192 func (d *device) StopAdvertising() error {
193 return d.hci.SetAdvertiseEnable(false)
194 }
195
196 func (d *device) Scan(ss []UUID, dup bool) {
197 // TODO: filter
198 d.hci.SetScanEnable(true, dup)
199 }
200
201 func (d *device) StopScanning() {
202 d.hci.SetScanEnable(false, true)
203 }
204
205 func (d *device) Connect(p Peripheral) {
206 d.hci.Connect(p.(*peripheral).pd)
207 }
208
209 func (d *device) CancelConnection(p Peripheral) {
210 d.hci.CancelConnection(p.(*peripheral).pd)
211 }
212
213 func (d *device) SendHCIRawCommand(c cmd.CmdParam) ([]byte, error) {
214 return d.hci.SendRawCommand(c)
215 }
216
217 // Flush pending advertising settings to the device.
218 func (d *device) update() error {
219 if d.advParam != nil {
220 if err := d.hci.SendCmdWithAdvOff(d.advParam); err != nil {
221 return err
222 }
223 d.advParam = nil
224 }
225 if d.scanResp != nil {
226 if err := d.hci.SendCmdWithAdvOff(d.scanResp); err != nil {
227 return err
228 }
229 d.scanResp = nil
230 }
231 if d.advData != nil {
232 if err := d.hci.SendCmdWithAdvOff(d.advData); err != nil {
233 return err
234 }
235 d.advData = nil
236 }
237 return nil
238 }