Debian Codegraph @

linux/evt/evt.go

raw symbols SCIP
1 package evt
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7
8 "github.com/paypal/gatt/linux/util"
9 )
10
11 type EventHandler interface {
12 HandleEvent([]byte) error
13 }
14
15 type HandlerFunc func(b []byte) error
16
17 func (f HandlerFunc) HandleEvent(b []byte) error {
18 return f(b)
19 }
20
21 type Evt struct {
22 evtHandlers map[int]EventHandler
23 }
24
25 func NewEvt() *Evt {
26 return &Evt{
27 evtHandlers: map[int]EventHandler{},
28 }
29 }
30
31 func (e *Evt) HandleEvent(c int, h EventHandler) {
32 e.evtHandlers[c] = h
33 }
34
35 func (e *Evt) Dispatch(b []byte) error {
36 h := &EventHeader{}
37 if err := h.Unmarshal(b); err != nil {
38 return err
39 }
40 b = b[2:] // Skip Event Header (uint8 + uint8)
41 if f, found := e.evtHandlers[h.code]; found {
42 e.trace("> HCI Event: %s (0x%02X) plen %d: [ % X ])\n", h.code, uint8(h.code), h.plen, b)
43 return f.HandleEvent(b)
44 }
45 e.trace("> HCI Event: no handler for %s (0x%02X)\n", h.code, uint8(h.code))
46 return nil
47 }
48
49 func (e *Evt) trace(fmt string, v ...interface{}) {}
50
51 const (
52 InquiryComplete = 0x01 // Inquiry Complete
53 InquiryResult = 0x02 // Inquiry Result
54 ConnectionComplete = 0x03 // Connection Complete
55 ConnectionRequest = 0x04 // Connection Request
56 DisconnectionComplete = 0x05 // Disconnection Complete
57 AuthenticationComplete = 0x06 // Authentication
58 RemoteNameReqComplete = 0x07 // Remote Name Request Complete
59 EncryptionChange = 0x08 // Encryption Change
60 ChangeConnectionLinkKeyComplete = 0x09 // Change Conection Link Key Complete
61 MasterLinkKeyComplete = 0x0A // Master Link Keye Complete
62 ReadRemoteSupportedFeaturesComplete = 0x0B // Read Remote Supported Features Complete
63 ReadRemoteVersionInformationComplete = 0x0C // Read Remote Version Information Complete
64 QoSSetupComplete = 0x0D // QoSSetupComplete
65 CommandComplete = 0x0E // Command Complete
66 CommandStatus = 0x0F // Command status
67 HardwareError = 0x10 // Hardware Error
68 FlushOccurred = 0x11 // Flush Occured
69 RoleChange = 0x12 // Role Change
70 NumberOfCompletedPkts = 0x13 // Number Of Completed Packets
71 ModeChange = 0x14 // Mode Change
72 ReturnLinkKeys = 0x15 // Return Link Keys
73 PinCodeRequest = 0x16 // PIN Code Request
74 LinkKeyRequest = 0x17 // Link Key Request
75 LinkKeyNotification = 0x18 // Link Key Notification
76 LoopbackCommand = 0x19 // Loopback Command
77 DataBufferOverflow = 0x1A // Data Buffer Overflow
78 MaxSlotsChange = 0x1B // Max Slots Change
79 ReadClockOffsetComplete = 0x1C // Read Clock Offset Complete
80 ConnectionPtypeChanged = 0x1D // Connection Packet Type Changed
81 QoSViolation = 0x1E // QoS Violation
82 PageScanRepetitionModeChange = 0x20 // Page Scan Repetition Mode Change
83 FlowSpecificationComplete = 0x21 // Flow Specification
84 InquiryResultWithRssi = 0x22 // Inquery Result with RSSI
85 ReadRemoteExtendedFeaturesComplete = 0x23 // Read Remote Extended Features Complete
86 SyncConnectionComplete = 0x2C // Synchronous Connection Complete
87 SyncConnectionChanged = 0x2D // Synchronous Connection Changed
88 SniffSubrating = 0x2E // Sniff Subrating
89 ExtendedInquiryResult = 0x2F // Extended Inquiry Result
90 EncryptionKeyRefreshComplete = 0x30 // Encryption Key Refresh Complete
91 IOCapabilityRequest = 0x31 // IO Capability Request
92 IOCapabilityResponse = 0x32 // IO Capability Changed
93 UserConfirmationRequest = 0x33 // User Confirmation Request
94 UserPasskeyRequest = 0x34 // User Passkey Request
95 RemoteOOBDataRequest = 0x35 // Remote OOB Data
96 SimplePairingComplete = 0x36 // Simple Pairing Complete
97 LinkSupervisionTimeoutChanged = 0x38 // Link Supervision Timeout Changed
98 EnhancedFlushComplete = 0x39 // Enhanced Flush Complete
99 UserPasskeyNotify = 0x3B // User Passkey Notification
100 KeypressNotify = 0x3C // Keypass Notification
101 RemoteHostFeaturesNotify = 0x3D // Remote Host Supported Features Notification
102 LEMeta = 0x3E // LE Meta
103 PhysicalLinkComplete = 0x40 // Physical Link Complete
104 ChannelSelected = 0x41 // Channel Selected
105 DisconnectionPhysicalLinkComplete = 0x42 // Disconnection Physical Link Complete
106 PhysicalLinkLossEarlyWarning = 0x43 // Physical Link Loss Early Warning
107 PhysicalLinkRecovery = 0x44 // Physical Link Recovery
108 LogicalLinkComplete = 0x45 // Logical Link Complete
109 DisconnectionLogicalLinkComplete = 0x46 // Disconnection Logical Link Complete
110 FlowSpecModifyComplete = 0x47 // Flow Spec Modify Complete
111 NumberOfCompletedBlocks = 0x48 // Number Of Completed Data Blocks
112 AMPStartTest = 0x49 // AMP Start Test
113 AMPTestEnd = 0x4A // AMP Test End
114 AMPReceiverReport = 0x4b // AMP Receiver Report
115 AMPStatusChange = 0x4D // AMP status Change
116 TriggeredClockCapture = 0x4e // Triggered Clock Capture
117 SynchronizationTrainComplete = 0x4F // Synchronization Train Complete
118 SynchronizationTrainReceived = 0x50 // Synchronization Train Received
119 ConnectionlessSlaveBroadcastReceive = 0x51 // Connectionless Slave Broadcast Receive
120 ConnectionlessSlaveBroadcastTimeout = 0x52 // Connectionless Slave Broadcast Timeout
121 TruncatedPageComplete = 0x53 // Truncated Page Complete
122 SlavePageResponseTimeout = 0x54 // Slave Page Response Timeout
123 ConnectionlessSlaveBroadcastChannelMapChange = 0x55 // Connectionless Slave Broadcast Channel Map Change
124 InquiryResponseNotification = 0x56 // Inquiry Response Notification
125 AuthenticatedPayloadTimeoutExpired = 0x57 // Authenticated Payload Timeout Expired
126 )
127
128 type LEEventCode int
129
130 const (
131 LEConnectionComplete LEEventCode = 0x01 // LE Connection Complete
132 LEAdvertisingReport = 0x02 // LE Advertising Report
133 LEConnectionUpdateComplete = 0x03 // LE Connection Update Complete
134 LEReadRemoteUsedFeaturesComplete = 0x04 // LE Read Remote Used Features Complete
135 LELTKRequest = 0x05 // LE LTK Request
136 LERemoteConnectionParameterRequest = 0x06 // LE Remote Connection Parameter Request
137 )
138
139 type EventHeader struct {
140 code int
141 plen uint8
142 }
143
144 func (h *EventHeader) Unmarshal(b []byte) error {
145 if len(b) < 2 {
146 return errors.New("malformed header")
147 }
148 h.code = int(b[0])
149 h.plen = b[1]
150 if uint8(len(b)) != 2+h.plen {
151 return errors.New("wrong length")
152 }
153 return nil
154 }
155
156 var o = util.Order
157
158 // Event Parameters
159
160 type InquiryCompleteEP struct {
161 Status uint8
162 }
163
164 type InquiryResultEP struct {
165 NumResponses uint8
166 BDAddr [][6]byte
167 PageScanRepetitionMode []uint8
168 Reserved1 []byte
169 Reserved2 []byte
170 ClassOfDevice [][3]byte
171 ClockOffset []uint16
172 }
173
174 type ConnectionCompleteEP struct {
175 Status uint8
176 ConnectionHandle uint16
177 BDAddr [6]byte
178 LinkType uint8
179 EncryptionEnabled uint8
180 }
181
182 type ConnectionRequestEP struct {
183 BDAddr [6]byte
184 ClassofDevice [3]byte
185 LinkType uint8
186 }
187
188 type DisconnectionCompleteEP struct {
189 Status uint8
190 ConnectionHandle uint16
191 Reason uint8
192 }
193
194 func (e *DisconnectionCompleteEP) Unmarshal(b []byte) error {
195 buf := bytes.NewBuffer(b)
196 binary.Read(buf, binary.LittleEndian, &e.Status)
197 binary.Read(buf, binary.LittleEndian, &e.ConnectionHandle)
198 return binary.Read(buf, binary.LittleEndian, &e.Reason)
199 }
200
201 type CommandCompleteEP struct {
202 NumHCICommandPackets uint8
203 CommandOPCode uint16
204 ReturnParameters []byte
205 }
206
207 func (e *CommandCompleteEP) Unmarshal(b []byte) error {
208 buf := bytes.NewBuffer(b)
209 if err := binary.Read(buf, binary.LittleEndian, &e.NumHCICommandPackets); err != nil {
210 return err
211 }
212 if err := binary.Read(buf, binary.LittleEndian, &e.CommandOPCode); err != nil {
213 return err
214 }
215 e.ReturnParameters = buf.Bytes()
216 return nil
217 }
218
219 type CommandStatusEP struct {
220 Status uint8
221 NumHCICommandPackets uint8
222 CommandOpcode uint16
223 }
224
225 func (e *CommandStatusEP) Unmarshal(b []byte) error {
226 buf := bytes.NewBuffer(b)
227 binary.Read(buf, binary.LittleEndian, &e.Status)
228 binary.Read(buf, binary.LittleEndian, &e.NumHCICommandPackets)
229 return binary.Read(buf, binary.LittleEndian, &e.CommandOpcode)
230 }
231
232 type NumOfCompletedPkt struct {
233 ConnectionHandle uint16
234 NumOfCompletedPkts uint16
235 }
236
237 type NumberOfCompletedPktsEP struct {
238 NumberOfHandles uint8
239 Packets []NumOfCompletedPkt
240 }
241
242 func (e *NumberOfCompletedPktsEP) Unmarshal(b []byte) error {
243 e.NumberOfHandles = b[0]
244 n := int(e.NumberOfHandles)
245 buf := bytes.NewBuffer(b[1:])
246 e.Packets = make([]NumOfCompletedPkt, n)
247 for i := 0; i < n; i++ {
248 binary.Read(buf, binary.LittleEndian, &e.Packets[i].ConnectionHandle)
249 binary.Read(buf, binary.LittleEndian, &e.Packets[i].NumOfCompletedPkts)
250
251 e.Packets[i].ConnectionHandle &= 0xfff
252 }
253 return nil
254 }
255
256 // LE Meta Subevents
257 type LEConnectionCompleteEP struct {
258 SubeventCode uint8
259 Status uint8
260 ConnectionHandle uint16
261 Role uint8
262 PeerAddressType uint8
263 PeerAddress [6]byte
264 ConnInterval uint16
265 ConnLatency uint16
266 SupervisionTimeout uint16
267 MasterClockAccuracy uint8
268 }
269
270 func (e *LEConnectionCompleteEP) Unmarshal(b []byte) error {
271 e.SubeventCode = o.Uint8(b[0:])
272 e.Status = o.Uint8(b[1:])
273 e.ConnectionHandle = o.Uint16(b[2:])
274 e.Role = o.Uint8(b[4:])
275 e.PeerAddressType = o.Uint8(b[5:])
276 e.PeerAddress = o.MAC(b[6:])
277 e.ConnInterval = o.Uint16(b[12:])
278 e.ConnLatency = o.Uint16(b[14:])
279 e.SupervisionTimeout = o.Uint16(b[16:])
280 e.MasterClockAccuracy = o.Uint8(b[17:])
281 return nil
282 }
283
284 type LEAdvertisingReportEP struct {
285 SubeventCode uint8
286 NumReports uint8
287 EventType []uint8
288 AddressType []uint8
289 Address [][6]byte
290 Length []uint8
291 Data [][]byte
292 RSSI []int8
293 }
294
295 func (e *LEAdvertisingReportEP) Unmarshal(b []byte) error {
296 e.SubeventCode = o.Uint8(b)
297 b = b[1:]
298 e.NumReports = o.Uint8(b)
299 b = b[1:]
300 n := int(e.NumReports)
301 e.EventType = make([]uint8, n)
302 e.AddressType = make([]uint8, n)
303 e.Address = make([][6]byte, n)
304 e.Length = make([]uint8, n)
305 e.Data = make([][]byte, n)
306 e.RSSI = make([]int8, n)
307
308 for i := 0; i < n; i++ {
309 e.EventType[i] = o.Uint8(b)
310 b = b[1:]
311 }
312 for i := 0; i < n; i++ {
313 e.AddressType[i] = o.Uint8(b)
314 b = b[1:]
315 }
316 for i := 0; i < n; i++ {
317 e.Address[i] = o.MAC(b)
318 b = b[6:]
319 }
320 for i := 0; i < n; i++ {
321 e.Length[i] = o.Uint8(b)
322 b = b[1:]
323 }
324 for i := 0; i < n; i++ {
325 e.Data[i] = make([]byte, e.Length[i])
326 copy(e.Data[i], b)
327 b = b[e.Length[i]:]
328 }
329 for i := 0; i < n; i++ {
330 e.RSSI[i] = o.Int8(b)
331 b = b[1:]
332 }
333 return nil
334 }
335
336 type LEConnectionUpdateCompleteEP struct {
337 SubeventCode uint8
338 Status uint8
339 ConnectionHandle uint16
340 ConnInterval uint16
341 ConnLatency uint16
342 SupervisionTimeout uint16
343 }
344
345 func (e *LEConnectionUpdateCompleteEP) Unmarshal(b []byte) error {
346 return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
347 }
348
349 type LEReadRemoteUsedFeaturesCompleteEP struct {
350 SubeventCode uint8
351 Status uint8
352 ConnectionHandle uint16
353 LEFeatures uint64
354 }
355
356 func (e *LEReadRemoteUsedFeaturesCompleteEP) Unmarshal(b []byte) error {
357 return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
358 }
359
360 type LELTKRequestEP struct {
361 SubeventCode uint8
362 ConnectionHandle uint16
363 RandomNumber uint64
364 EncryptionDiversifier uint16
365 }
366
367 func (e *LELTKRequestEP) Unmarshal(b []byte) error {
368 return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
369 }
370
371 type LERemoteConnectionParameterRequestEP struct {
372 SubeventCode uint8
373 ConnectionHandle uint16
374 IntervalMin uint16
375 IntervalMax uint16
376 Latency uint16
377 Timeout uint16
378 }
379
380 func (e *LERemoteConnectionParameterRequestEP) Unmarshal(b []byte) error {
381 return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
382 }