Debian Codegraph @

linux/cmd/cmd.go

raw symbols SCIP
1 package cmd
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "io"
8 "log"
9
10 "github.com/bettercap/gatt/linux/evt"
11 "github.com/bettercap/gatt/linux/util"
12 )
13
14 type CmdParam interface {
15 Marshal([]byte)
16 Opcode() int
17 Len() int
18 }
19
20 func NewCmd(d io.Writer) *Cmd {
21 c := &Cmd{
22 dev: d,
23 sent: []*cmdPkt{},
24 compc: make(chan evt.CommandCompleteEP),
25 statusc: make(chan evt.CommandStatusEP),
26 }
27 go c.processCmdEvents()
28 return c
29 }
30
31 type cmdPkt struct {
32 op int
33 cp CmdParam
34 done chan []byte
35 }
36
37 func (c cmdPkt) Marshal() []byte {
38 b := make([]byte, 1+2+1+c.cp.Len())
39 b[0] = byte(0x1) // typCommandPkt
40 b[1] = byte(c.op)
41 b[2] = byte(c.op >> 8)
42 b[3] = byte(c.cp.Len())
43 c.cp.Marshal(b[4:])
44 return b
45 }
46
47 type Cmd struct {
48 dev io.Writer
49 sent []*cmdPkt
50 compc chan evt.CommandCompleteEP
51 statusc chan evt.CommandStatusEP
52 }
53
54 func (c Cmd) trace(fmt string, v ...interface{}) {}
55
56 func (c *Cmd) HandleComplete(b []byte) error {
57 var e evt.CommandCompleteEP
58 if err := e.Unmarshal(b); err != nil {
59 return err
60 }
61 c.compc <- e
62 return nil
63 }
64
65 func (c *Cmd) HandleStatus(b []byte) error {
66 var e evt.CommandStatusEP
67 if err := e.Unmarshal(b); err != nil {
68 return err
69 }
70 c.statusc <- e
71 return nil
72 }
73
74 func (c *Cmd) Send(cp CmdParam) ([]byte, error) {
75 op := cp.Opcode()
76 p := &cmdPkt{op: op, cp: cp, done: make(chan []byte)}
77 raw := p.Marshal()
78
79 c.sent = append(c.sent, p)
80 if n, err := c.dev.Write(raw); err != nil {
81 return nil, err
82 } else if n != len(raw) {
83 return nil, errors.New("Failed to send whole Cmd pkt to HCI socket")
84 }
85 return <-p.done, nil
86 }
87
88 func (c *Cmd) SendAndCheckResp(cp CmdParam, exp []byte) error {
89 rsp, err := c.Send(cp)
90 if err != nil {
91 return err
92 }
93 // Don't care about the response
94 if len(exp) == 0 {
95 return nil
96 }
97 // Check the if status is one of the expected value
98 if !bytes.Contains(exp, rsp[0:1]) {
99 return fmt.Errorf("HCI command: '0x%04x' return 0x%02X, expect: [%X] ", cp.Opcode(), rsp[0], exp)
100 }
101 return nil
102 }
103
104 func (c *Cmd) processCmdEvents() {
105 for {
106 select {
107 case status := <-c.statusc:
108 found := false
109 for i, p := range c.sent {
110 if uint16(p.op) == status.CommandOpcode {
111 found = true
112 c.sent = append(c.sent[:i], c.sent[i+1:]...)
113 close(p.done)
114 break
115 }
116 }
117 if !found {
118 log.Printf("Can't find the cmdPkt for this CommandStatusEP: %v", status)
119 }
120 case comp := <-c.compc:
121 found := false
122 for i, p := range c.sent {
123 if uint16(p.op) == comp.CommandOPCode {
124 found = true
125 c.sent = append(c.sent[:i], c.sent[i+1:]...)
126 p.done <- comp.ReturnParameters
127 break
128 }
129 }
130 if !found {
131 log.Printf("Can't find the cmdPkt for this CommandCompleteEP: %v", comp)
132 }
133 }
134 }
135 }
136
137 const (
138 linkCtl = 0x01
139 linkPolicy = 0x02
140 hostCtl = 0x03
141 infoParam = 0x04
142 statusParam = 0x05
143 testingCmd = 0X3E
144 leCtl = 0x08
145 vendorCmd = 0X3F
146 )
147
148 const (
149 opInquiry = linkCtl<<10 | 0x0001 // Inquiry
150 opInquiryCancel = linkCtl<<10 | 0x0002 // Inquiry Cancel
151 opPeriodicInquiry = linkCtl<<10 | 0x0003 // Periodic Inquiry Mode
152 opExitPeriodicInquiry = linkCtl<<10 | 0x0004 // Exit Periodic Inquiry Mode
153 opCreateConn = linkCtl<<10 | 0x0005 // Create Connection
154 opDisconnect = linkCtl<<10 | 0x0006 // Disconnect
155 opCreateConnCancel = linkCtl<<10 | 0x0008 // Create Connection Cancel
156 opAcceptConnReq = linkCtl<<10 | 0x0009 // Accept Connection Request
157 opRejectConnReq = linkCtl<<10 | 0x000A // Reject Connection Request
158 opLinkKeyReply = linkCtl<<10 | 0x000B // Link Key Request Reply
159 opLinkKeyNegReply = linkCtl<<10 | 0x000C // Link Key Request Negative Reply
160 opPinCodeReply = linkCtl<<10 | 0x000D // PIN Code Request Reply
161 opPinCodeNegReply = linkCtl<<10 | 0x000E // PIN Code Request Negative Reply
162 opSetConnPtype = linkCtl<<10 | 0x000F // Change Connection Packet Type
163 opAuthRequested = linkCtl<<10 | 0x0011 // Authentication Request
164 opSetConnEncrypt = linkCtl<<10 | 0x0013 // Set Connection Encryption
165 opChangeConnLinkKey = linkCtl<<10 | 0x0015 // Change Connection Link Key
166 opMasterLinkKey = linkCtl<<10 | 0x0017 // Master Link Key
167 opRemoteNameReq = linkCtl<<10 | 0x0019 // Remote Name Request
168 opRemoteNameReqCancel = linkCtl<<10 | 0x001A // Remote Name Request Cancel
169 opReadRemoteFeatures = linkCtl<<10 | 0x001B // Read Remote Supported Features
170 opReadRemoteExtFeatures = linkCtl<<10 | 0x001C // Read Remote Extended Features
171 opReadRemoteVersion = linkCtl<<10 | 0x001D // Read Remote Version Information
172 opReadClockOffset = linkCtl<<10 | 0x001F // Read Clock Offset
173 opReadLMPHandle = linkCtl<<10 | 0x0020 // Read LMP Handle
174 opSetupSyncConn = linkCtl<<10 | 0x0028 // Setup Synchronous Connection
175 opAcceptSyncConnReq = linkCtl<<10 | 0x0029 // Aceept Synchronous Connection
176 opRejectSyncConnReq = linkCtl<<10 | 0x002A // Recject Synchronous Connection
177 opIOCapabilityReply = linkCtl<<10 | 0x002B // IO Capability Request Reply
178 opUserConfirmReply = linkCtl<<10 | 0x002C // User Confirmation Request Reply
179 opUserConfirmNegReply = linkCtl<<10 | 0x002D // User Confirmation Negative Reply
180 opUserPasskeyReply = linkCtl<<10 | 0x002E // User Passkey Request Reply
181 opUserPasskeyNegReply = linkCtl<<10 | 0x002F // User Passkey Request Negative Reply
182 opRemoteOOBDataReply = linkCtl<<10 | 0x0030 // Remote OOB Data Request Reply
183 opRemoteOOBDataNegReply = linkCtl<<10 | 0x0033 // Remote OOB Data Request Negative Reply
184 opIOCapabilityNegReply = linkCtl<<10 | 0x0034 // IO Capability Request Negative Reply
185 opCreatePhysicalLink = linkCtl<<10 | 0x0035 // Create Physical Link
186 opAcceptPhysicalLink = linkCtl<<10 | 0x0036 // Accept Physical Link
187 opDisconnectPhysicalLink = linkCtl<<10 | 0x0037 // Disconnect Physical Link
188 opCreateLogicalLink = linkCtl<<10 | 0x0038 // Create Logical Link
189 opAcceptLogicalLink = linkCtl<<10 | 0x0039 // Accept Logical Link
190 opDisconnectLogicalLink = linkCtl<<10 | 0x003A // Disconnect Logical Link
191 opLogicalLinkCancel = linkCtl<<10 | 0x003B // Logical Link Cancel
192 opFlowSpecModify = linkCtl<<10 | 0x003C // Flow Spec Modify
193 )
194
195 const (
196 opHoldMode = linkPolicy<<10 | 0x0001 // Hold Mode
197 opSniffMode = linkPolicy<<10 | 0x0003 // Sniff Mode
198 opExitSniffMode = linkPolicy<<10 | 0x0004 // Exit Sniff Mode
199 opParkMode = linkPolicy<<10 | 0x0005 // Park State
200 opExitParkMode = linkPolicy<<10 | 0x0006 // Exit Park State
201 opQoSSetup = linkPolicy<<10 | 0x0007 // QoS Setup
202 opRoleDiscovery = linkPolicy<<10 | 0x0009 // Role Discovery
203 opSwitchRole = linkPolicy<<10 | 0x000B // Switch Role
204 opReadLinkPolicy = linkPolicy<<10 | 0x000C // Read Link Policy Settings
205 opWriteLinkPolicy = linkPolicy<<10 | 0x000D // Write Link Policy Settings
206 opReadDefaultLinkPolicy = linkPolicy<<10 | 0x000E // Read Default Link Policy Settings
207 opWriteDefaultLinkPolicy = linkPolicy<<10 | 0x000F // Write Default Link Policy Settings
208 opFlowSpecification = linkPolicy<<10 | 0x0010 // Flow Specification
209 opSniffSubrating = linkPolicy<<10 | 0x0011 // Sniff Subrating
210 )
211
212 const (
213 opSetEventMask = hostCtl<<10 | 0x0001 // Set Event Mask
214 opReset = hostCtl<<10 | 0x0003 // Reset
215 opSetEventFlt = hostCtl<<10 | 0x0005 // Set Event Filter
216 opFlush = hostCtl<<10 | 0x0008 // Flush
217 opReadPinType = hostCtl<<10 | 0x0009 // Read PIN Type
218 opWritePinType = hostCtl<<10 | 0x000A // Write PIN Type
219 opCreateNewUnitKey = hostCtl<<10 | 0x000B // Create New Unit Key
220 opReadStoredLinkKey = hostCtl<<10 | 0x000D // Read Stored Link Key
221 opWriteStoredLinkKey = hostCtl<<10 | 0x0011 // Write Stored Link Key
222 opDeleteStoredLinkKey = hostCtl<<10 | 0x0012 // Delete Stored Link Key
223 opWriteLocalName = hostCtl<<10 | 0x0013 // Write Local Name
224 opReadLocalName = hostCtl<<10 | 0x0014 // Read Local Name
225 opReadConnAcceptTimeout = hostCtl<<10 | 0x0015 // Read Connection Accept Timeout
226 opWriteConnAcceptTimeout = hostCtl<<10 | 0x0016 // Write Connection Accept Timeout
227 opReadPageTimeout = hostCtl<<10 | 0x0017 // Read Page Timeout
228 opWritePageTimeout = hostCtl<<10 | 0x0018 // Write Page Timeout
229 opReadScanEnable = hostCtl<<10 | 0x0019 // Read Scan Enable
230 opWriteScanEnable = hostCtl<<10 | 0x001A // Write Scan Enable
231 opReadPageActivity = hostCtl<<10 | 0x001B // Read Page Scan Activity
232 opWritePageActivity = hostCtl<<10 | 0x001C // Write Page Scan Activity
233 opReadInqActivity = hostCtl<<10 | 0x001D // Read Inquiry Scan Activity
234 opWriteInqActivity = hostCtl<<10 | 0x001E // Write Inquiry Scan Activity
235 opReadAuthEnable = hostCtl<<10 | 0x001F // Read Authentication Enable
236 opWriteAuthEnable = hostCtl<<10 | 0x0020 // Write Authentication Enable
237 opReadEncryptMode = hostCtl<<10 | 0x0021
238 opWriteEncryptMode = hostCtl<<10 | 0x0022
239 opReadClassOfDev = hostCtl<<10 | 0x0023 // Read Class of Device
240 opWriteClassOfDevice = hostCtl<<10 | 0x0024 // Write Class of Device
241 opReadVoiceSetting = hostCtl<<10 | 0x0025 // Read Voice Setting
242 opWriteVoiceSetting = hostCtl<<10 | 0x0026 // Write Voice Setting
243 opReadAutomaticFlushTimeout = hostCtl<<10 | 0x0027 // Read Automatic Flush Timeout
244 opWriteAutomaticFlushTimeout = hostCtl<<10 | 0x0028 // Write Automatic Flush Timeout
245 opReadNumBroadcastRetrans = hostCtl<<10 | 0x0029 // Read Num Broadcast Retransmissions
246 opWriteNumBroadcastRetrans = hostCtl<<10 | 0x002A // Write Num Broadcast Retransmissions
247 opReadHoldModeActivity = hostCtl<<10 | 0x002B // Read Hold Mode Activity
248 opWriteHoldModeActivity = hostCtl<<10 | 0x002C // Write Hold Mode Activity
249 opReadTransmitPowerLevel = hostCtl<<10 | 0x002D // Read Transmit Power Level
250 opReadSyncFlowEnable = hostCtl<<10 | 0x002E // Read Synchronous Flow Control
251 opWriteSyncFlowEnable = hostCtl<<10 | 0x002F // Write Synchronous Flow Control
252 opSetControllerToHostFC = hostCtl<<10 | 0x0031 // Set Controller To Host Flow Control
253 opHostBufferSize = hostCtl<<10 | 0x0033 // Host Buffer Size
254 opHostNumCompPkts = hostCtl<<10 | 0x0035 // Host Number Of Completed Packets
255 opReadLinkSupervisionTimeout = hostCtl<<10 | 0x0036 // Read Link Supervision Timeout
256 opWriteLinkSupervisionTimeout = hostCtl<<10 | 0x0037 // Write Link Supervision Timeout
257 opReadNumSupportedIAC = hostCtl<<10 | 0x0038 // Read Number Of Supported IAC
258 opReadCurrentIACLAP = hostCtl<<10 | 0x0039 // Read Current IAC LAP
259 opWriteCurrentIACLAP = hostCtl<<10 | 0x003A // Write Current IAC LAP
260 opReadPageScanPeriodMode = hostCtl<<10 | 0x003B
261 opWritePageScanPeriodMode = hostCtl<<10 | 0x003C
262 opReadPageScanMode = hostCtl<<10 | 0x003D
263 opWritePageScanMode = hostCtl<<10 | 0x003E
264 opSetAFHClassification = hostCtl<<10 | 0x003F // Set AFH Host Channel Classification
265 opReadInquiryScanType = hostCtl<<10 | 0x0042 // Read Inquiry Scan Type
266 opWriteInquiryScanType = hostCtl<<10 | 0x0043 // Write Inquiry Scan Type
267 opReadInquiryMode = hostCtl<<10 | 0x0044 // Read Inquiry Mode
268 opWriteInquiryMode = hostCtl<<10 | 0x0045 // Write Inquiry Mode
269 opReadPageScanType = hostCtl<<10 | 0x0046 // Read Page Scan Type
270 opWritePageScanType = hostCtl<<10 | 0x0047 // Write Page Scan Type
271 opReadAFHMode = hostCtl<<10 | 0x0048 // Read AFH Channel Assessment Mode
272 opWriteAFHMode = hostCtl<<10 | 0x0049 // Write AFH Channel Assesment Mode
273 opReadExtInquiryResponse = hostCtl<<10 | 0x0051 // Read Extended Inquiry Response
274 opWriteExtInquiryResponse = hostCtl<<10 | 0x0052 // Write Extended Inquiry Response
275 opRefreshEncryptionKey = hostCtl<<10 | 0x0053 // Refresh Encryption Key
276 opReadSimplePairingMode = hostCtl<<10 | 0x0055 // Read Simple Pairing Mode
277 opWriteSimplePairingMode = hostCtl<<10 | 0x0056 // Write Simple Pairing Mode
278 opReadLocalOobData = hostCtl<<10 | 0x0057 // Read Local OOB Data
279 opReadInqResponseTransmitPowerLevel = hostCtl<<10 | 0x0058 // Read Inquiry Response Transmit Power Level
280 opWriteInquiryTransmitPowerLevel = hostCtl<<10 | 0x0059 // Write Inquiry Response Transmit Power Level
281 opReadDefaultErrorDataReporting = hostCtl<<10 | 0x005A // Read Default Erroneous Data Reporting
282 opWriteDefaultErrorDataReporting = hostCtl<<10 | 0x005B // Write Default Erroneous Data Reporting
283 opEnhancedFlush = hostCtl<<10 | 0x005F // Enhanced Flush
284 opSendKeypressNotify = hostCtl<<10 | 0x0060 // send Keypress Notification
285 opReadLogicalLinkAcceptTimeout = hostCtl<<10 | 0x0061 // Read Logical Link Accept Timeout
286 opWriteLogicalLinkAcceptTimeout = hostCtl<<10 | 0x0062 // Write Logical Link Accept Timeout
287 opSetEventMaskPage2 = hostCtl<<10 | 0x0063 // Set Event Mask Page 2
288 opReadLocationData = hostCtl<<10 | 0x0064 // Read Location Data
289 opWriteLocationData = hostCtl<<10 | 0x0065 // Write Location Data
290 opReadFlowControlMode = hostCtl<<10 | 0x0066 // Read Flow Control Mode
291 opWriteFlowControlMode = hostCtl<<10 | 0x0067 // Write Flow Control Mode
292 opReadEnhancedTransmitpowerLevel = hostCtl<<10 | 0x0068 // Read Enhanced Transmit Power Level
293 opReadBestEffortFlushTimeout = hostCtl<<10 | 0x0069 // Read Best Effort Flush Timeout
294 opWriteBestEffortFlushTimeout = hostCtl<<10 | 0x006A // Write Best Effort Flush Timeout
295 opReadLEHostSupported = hostCtl<<10 | 0x006C // Read LE Host Supported
296 opWriteLEHostSupported = hostCtl<<10 | 0x006D // Write LE Host Supported
297 )
298 const (
299 opReadLocalVersionInformation = infoParam<<10 | 0x0001 // Read Local Version Information
300 opReadLocalSupportedCommands = infoParam<<10 | 0x0002 // Read Local Supported Commands
301 opReadLocalSupportedFeatures = infoParam<<10 | 0x0003 // Read Local Supported Features
302 opReadLocalExtendedFeatures = infoParam<<10 | 0x0004 // Read Local Extended Features
303 opReadBufferSize = infoParam<<10 | 0x0005 // Read Buffer Size
304 opReadBDADDR = infoParam<<10 | 0x0009 // Read BD_ADDR
305 opReadDataBlockSize = infoParam<<10 | 0x000A // Read Data Block Size
306 opReadLocalSupportedCodecs = infoParam<<10 | 0x000B // Read Local Supported Codecs
307 )
308 const (
309 opLESetEventMask = leCtl<<10 | 0x0001 // LE Set Event Mask
310 opLEReadBufferSize = leCtl<<10 | 0x0002 // LE Read Buffer Size
311 opLEReadLocalSupportedFeatures = leCtl<<10 | 0x0003 // LE Read Local Supported Features
312 opLESetRandomAddress = leCtl<<10 | 0x0005 // LE Set Random Address
313 opLESetAdvertisingParameters = leCtl<<10 | 0x0006 // LE Set Advertising Parameters
314 opLEReadAdvertisingChannelTxPower = leCtl<<10 | 0x0007 // LE Read Advertising Channel Tx Power
315 opLESetAdvertisingData = leCtl<<10 | 0x0008 // LE Set Advertising Data
316 opLESetScanResponseData = leCtl<<10 | 0x0009 // LE Set Scan Response Data
317 opLESetAdvertiseEnable = leCtl<<10 | 0x000a // LE Set Advertising Enable
318 opLESetScanParameters = leCtl<<10 | 0x000b // LE Set Scan Parameters
319 opLESetScanEnable = leCtl<<10 | 0x000c // LE Set Scan Enable
320 opLECreateConn = leCtl<<10 | 0x000d // LE Create Connection
321 opLECreateConnCancel = leCtl<<10 | 0x000e // LE Create Connection Cancel
322 opLEReadWhiteListSize = leCtl<<10 | 0x000f // LE Read White List Size
323 opLEClearWhiteList = leCtl<<10 | 0x0010 // LE Clear White List
324 opLEAddDeviceToWhiteList = leCtl<<10 | 0x0011 // LE Add Device To White List
325 opLERemoveDeviceFromWhiteList = leCtl<<10 | 0x0012 // LE Remove Device From White List
326 opLEConnUpdate = leCtl<<10 | 0x0013 // LE Connection Update
327 opLESetHostChannelClassification = leCtl<<10 | 0x0014 // LE Set Host Channel Classification
328 opLEReadChannelMap = leCtl<<10 | 0x0015 // LE Read Channel Map
329 opLEReadRemoteUsedFeatures = leCtl<<10 | 0x0016 // LE Read Remote Used Features
330 opLEEncrypt = leCtl<<10 | 0x0017 // LE Encrypt
331 opLERand = leCtl<<10 | 0x0018 // LE Rand
332 opLEStartEncryption = leCtl<<10 | 0x0019 // LE Star Encryption
333 opLELTKReply = leCtl<<10 | 0x001a // LE Long Term Key Request Reply
334 opLELTKNegReply = leCtl<<10 | 0x001b // LE Long Term Key Request Negative Reply
335 opLEReadSupportedStates = leCtl<<10 | 0x001c // LE Read Supported States
336 opLEReceiverTest = leCtl<<10 | 0x001d // LE Reciever Test
337 opLETransmitterTest = leCtl<<10 | 0x001e // LE Transmitter Test
338 opLETestEnd = leCtl<<10 | 0x001f // LE Test End
339 opLERemoteConnectionParameterReply = leCtl<<10 | 0x0020 // LE Remote Connection Parameter Request Reply
340 opLERemoteConnectionParameterNegReply = leCtl<<10 | 0x0021 // LE Remote Connection Parameter Request Negative Reply
341 )
342
343 var o = util.Order
344
345 // Link Control Commands
346
347 // Disconnect (0x0006)
348 type Disconnect struct {
349 ConnectionHandle uint16
350 Reason uint8
351 }
352
353 func (c Disconnect) Opcode() int { return opDisconnect }
354 func (c Disconnect) Len() int { return 3 }
355 func (c Disconnect) Marshal(b []byte) {
356 o.PutUint16(b[0:], c.ConnectionHandle)
357 b[2] = c.Reason
358 }
359
360 // No Return Parameters, Check for Disconnection Complete Event
361 type DisconnectRP struct{}
362
363 // Link Policy Commands
364
365 // Write Default Link Policy
366 type WriteDefaultLinkPolicy struct{ DefaultLinkPolicySettings uint16 }
367
368 func (c WriteDefaultLinkPolicy) Opcode() int { return opWriteDefaultLinkPolicy }
369 func (c WriteDefaultLinkPolicy) Len() int { return 2 }
370 func (c WriteDefaultLinkPolicy) Marshal(b []byte) { o.PutUint16(b, c.DefaultLinkPolicySettings) }
371
372 type WriteDefaultLinkPolicyRP struct{ Status uint8 }
373
374 // Host Control Commands
375
376 // Set Event Mask (0x0001)
377 type SetEventMask struct{ EventMask uint64 }
378
379 func (c SetEventMask) Opcode() int { return opSetEventMask }
380 func (c SetEventMask) Len() int { return 8 }
381 func (c SetEventMask) Marshal(b []byte) { o.PutUint64(b, c.EventMask) }
382
383 type SetEventMaskRP struct{ Status uint8 }
384
385 // Reset (0x0002)
386 type Reset struct{}
387
388 func (c Reset) Opcode() int { return opReset }
389 func (c Reset) Len() int { return 0 }
390 func (c Reset) Marshal(b []byte) {}
391
392 type ResetRP struct{ Status uint8 }
393
394 // Set Event Filter (0x0003)
395 // FIXME: This structures are overloading.
396 // Both Marshal() and Len() are just placeholder.
397 // Need more effort for decoding.
398 // type SetEventFlt struct {
399 // FilterType uint8
400 // FilterConditionType uint8
401 // Condition uint8
402 // }
403
404 // func (c SetEventFlt) Opcode() int { return opSetEventFlt }
405 // func (c SetEventFlt) Len() int { return 0 }
406 // func (c SetEventFlt) Marshal(b []byte) {}
407
408 type SetEventFltRP struct{ Status uint8 }
409
410 // Flush (0x0008)
411 type Flush struct{ ConnectionHandle uint16 }
412
413 func (c Flush) Opcode() int { return opFlush }
414 func (c Flush) Len() int { return 2 }
415 func (c Flush) Marshal(b []byte) { o.PutUint16(b, c.ConnectionHandle) }
416
417 type flushRP struct{ status uint8 }
418
419 // Write Page Timeout (0x0018)
420 type WritePageTimeout struct{ PageTimeout uint16 }
421
422 func (c WritePageTimeout) Opcode() int { return opWritePageTimeout }
423 func (c WritePageTimeout) Len() int { return 2 }
424 func (c WritePageTimeout) Marshal(b []byte) { o.PutUint16(b, c.PageTimeout) }
425
426 type WritePageTimeoutRP struct{}
427
428 // Write Class of Device (0x0024)
429 type WriteClassOfDevice struct{ ClassOfDevice [3]byte }
430
431 func (c WriteClassOfDevice) Opcode() int { return opWriteClassOfDevice }
432 func (c WriteClassOfDevice) Len() int { return 3 }
433 func (c WriteClassOfDevice) Marshal(b []byte) { copy(b, c.ClassOfDevice[:]) }
434
435 type WriteClassOfDevRP struct{ status uint8 }
436
437 // Write Host Buffer Size (0x0033)
438 type HostBufferSize struct {
439 HostACLDataPacketLength uint16
440 HostSynchronousDataPacketLength uint8
441 HostTotalNumACLDataPackets uint16
442 HostTotalNumSynchronousDataPackets uint16
443 }
444
445 func (c HostBufferSize) Opcode() int { return opHostBufferSize }
446 func (c HostBufferSize) Len() int { return 7 }
447 func (c HostBufferSize) Marshal(b []byte) {
448 o.PutUint16(b[0:], c.HostACLDataPacketLength)
449 o.PutUint8(b[2:], c.HostSynchronousDataPacketLength)
450 o.PutUint16(b[3:], c.HostTotalNumACLDataPackets)
451 o.PutUint16(b[5:], c.HostTotalNumSynchronousDataPackets)
452 }
453
454 type HostBufferSizeRP struct{ Status uint8 }
455
456 // Write Inquiry Scan Type (0x0043)
457 type WriteInquiryScanType struct{ ScanType uint8 }
458
459 func (c WriteInquiryScanType) Opcode() int { return opWriteInquiryScanType }
460 func (c WriteInquiryScanType) Len() int { return 1 }
461 func (c WriteInquiryScanType) Marshal(b []byte) { b[0] = c.ScanType }
462
463 type WriteInquiryScanTypeRP struct{ Status uint8 }
464
465 // Write Inquiry Mode (0x0045)
466 type WriteInquiryMode struct {
467 InquiryMode uint8
468 }
469
470 func (c WriteInquiryMode) Opcode() int { return opWriteInquiryMode }
471 func (c WriteInquiryMode) Len() int { return 1 }
472 func (c WriteInquiryMode) Marshal(b []byte) { b[0] = c.InquiryMode }
473
474 type WriteInquiryModeRP struct{ Status uint8 }
475
476 // Write Page Scan Type (0x0046)
477 type WritePageScanType struct{ PageScanType uint8 }
478
479 func (c WritePageScanType) Opcode() int { return opWritePageScanType }
480 func (c WritePageScanType) Len() int { return 1 }
481 func (c WritePageScanType) Marshal(b []byte) { b[0] = c.PageScanType }
482
483 type WritePageScanTypeRP struct{ Status uint8 }
484
485 // Write Simple Pairing Mode (0x0056)
486 type WriteSimplePairingMode struct{ SimplePairingMode uint8 }
487
488 func (c WriteSimplePairingMode) Opcode() int { return opWriteSimplePairingMode }
489 func (c WriteSimplePairingMode) Len() int { return 1 }
490 func (c WriteSimplePairingMode) Marshal(b []byte) { b[0] = c.SimplePairingMode }
491
492 type WriteSimplePairingModeRP struct{}
493
494 // Set Event Mask Page 2 (0x0063)
495 type SetEventMaskPage2 struct{ EventMaskPage2 uint64 }
496
497 func (c SetEventMaskPage2) Opcode() int { return opSetEventMaskPage2 }
498 func (c SetEventMaskPage2) Len() int { return 8 }
499 func (c SetEventMaskPage2) Marshal(b []byte) { o.PutUint64(b, c.EventMaskPage2) }
500
501 type SetEventMaskPage2RP struct{ Status uint8 }
502
503 // Write LE Host Supported (0x006D)
504 type WriteLEHostSupported struct {
505 LESupportedHost uint8
506 SimultaneousLEHost uint8
507 }
508
509 func (c WriteLEHostSupported) Opcode() int { return opWriteLEHostSupported }
510 func (c WriteLEHostSupported) Len() int { return 2 }
511 func (c WriteLEHostSupported) Marshal(b []byte) { b[0], b[1] = c.LESupportedHost, c.SimultaneousLEHost }
512
513 type WriteLeHostSupportedRP struct{ Status uint8 }
514
515 // LE Controller Commands
516
517 // LE Set Event Mask (0x0001)
518 type LESetEventMask struct{ LEEventMask uint64 }
519
520 func (c LESetEventMask) Opcode() int { return opLESetEventMask }
521 func (c LESetEventMask) Len() int { return 8 }
522 func (c LESetEventMask) Marshal(b []byte) { o.PutUint64(b, c.LEEventMask) }
523
524 type LESetEventMaskRP struct{ Status uint8 }
525
526 // LE Read Buffer Size (0x0002)
527 type LEReadBufferSize struct{}
528
529 func (c LEReadBufferSize) Opcode() int { return opLEReadBufferSize }
530 func (c LEReadBufferSize) Len() int { return 1 }
531 func (c LEReadBufferSize) Marshal(b []byte) {}
532
533 type LEReadBufferSizeRP struct {
534 Status uint8
535 HCLEACLDataPacketLength uint16
536 HCTotalNumLEACLDataPackets uint8
537 }
538
539 // LE Read Local Supported Features (0x0003)
540 type LEReadLocalSupportedFeatures struct{}
541
542 func (c LEReadLocalSupportedFeatures) Opcode() int { return opLEReadLocalSupportedFeatures }
543 func (c LEReadLocalSupportedFeatures) Len() int { return 0 }
544 func (c LEReadLocalSupportedFeatures) Marshal(b []byte) {}
545
546 type LEReadLocalSupportedFeaturesRP struct {
547 Status uint8
548 LEFeatures uint64
549 }
550
551 // LE Set Random Address (0x0005)
552 type LESetRandomAddress struct{ RandomAddress [6]byte }
553
554 func (c LESetRandomAddress) Opcode() int { return opLESetRandomAddress }
555 func (c LESetRandomAddress) Len() int { return 6 }
556 func (c LESetRandomAddress) Marshal(b []byte) { o.PutMAC(b, c.RandomAddress) }
557
558 type LESetRandomAddressRP struct{ Status uint8 }
559
560 // LE Set Advertising Parameters (0x0006)
561 type LESetAdvertisingParameters struct {
562 AdvertisingIntervalMin uint16
563 AdvertisingIntervalMax uint16
564 AdvertisingType uint8
565 OwnAddressType uint8
566 DirectAddressType uint8
567 DirectAddress [6]byte
568 AdvertisingChannelMap uint8
569 AdvertisingFilterPolicy uint8
570 }
571
572 func (c LESetAdvertisingParameters) Opcode() int { return opLESetAdvertisingParameters }
573 func (c LESetAdvertisingParameters) Len() int { return 15 }
574 func (c LESetAdvertisingParameters) Marshal(b []byte) {
575 o.PutUint16(b[0:], c.AdvertisingIntervalMin)
576 o.PutUint16(b[2:], c.AdvertisingIntervalMax)
577 o.PutUint8(b[4:], c.AdvertisingType)
578 o.PutUint8(b[5:], c.OwnAddressType)
579 o.PutUint8(b[6:], c.DirectAddressType)
580 o.PutMAC(b[7:], c.DirectAddress)
581 o.PutUint8(b[13:], c.AdvertisingChannelMap)
582 o.PutUint8(b[14:], c.AdvertisingFilterPolicy)
583 }
584
585 type LESetAdvertisingParametersRP struct{ Status uint8 }
586
587 // LE Read Advertising Channel Tx Power (0x0007)
588 type LEReadAdvertisingChannelTxPower struct{}
589
590 func (c LEReadAdvertisingChannelTxPower) Opcode() int { return opLEReadAdvertisingChannelTxPower }
591 func (c LEReadAdvertisingChannelTxPower) Len() int { return 0 }
592 func (c LEReadAdvertisingChannelTxPower) Marshal(b []byte) {}
593
594 type LEReadAdvertisingChannelTxPowerRP struct {
595 Status uint8
596 TransmitPowerLevel uint8
597 }
598
599 // LE Set Advertising Data (0x0008)
600 type LESetAdvertisingData struct {
601 AdvertisingDataLength uint8
602 AdvertisingData [31]byte
603 }
604
605 func (c LESetAdvertisingData) Opcode() int { return opLESetAdvertisingData }
606 func (c LESetAdvertisingData) Len() int { return 32 }
607 func (c LESetAdvertisingData) Marshal(b []byte) {
608 b[0] = c.AdvertisingDataLength
609 copy(b[1:], c.AdvertisingData[:c.AdvertisingDataLength])
610 }
611
612 type LESetAdvertisingDataRP struct{ Status uint8 }
613
614 // LE Set Scan Response Data (0x0009)
615 type LESetScanResponseData struct {
616 ScanResponseDataLength uint8
617 ScanResponseData [31]byte
618 }
619
620 func (c LESetScanResponseData) Opcode() int { return opLESetScanResponseData }
621 func (c LESetScanResponseData) Len() int { return 32 }
622 func (c LESetScanResponseData) Marshal(b []byte) {
623 b[0] = c.ScanResponseDataLength
624 copy(b[1:], c.ScanResponseData[:c.ScanResponseDataLength])
625 }
626
627 type LESetScanResponseDataRP struct{ Status uint8 }
628
629 // LE Set Advertising Enable (0x000A)
630 type LESetAdvertiseEnable struct{ AdvertisingEnable uint8 }
631
632 func (c LESetAdvertiseEnable) Opcode() int { return opLESetAdvertiseEnable }
633 func (c LESetAdvertiseEnable) Len() int { return 1 }
634 func (c LESetAdvertiseEnable) Marshal(b []byte) { b[0] = c.AdvertisingEnable }
635
636 type LESetAdvertiseEnableRP struct{ Status uint8 }
637
638 // LE Set Scan Parameters (0x000B)
639 type LESetScanParameters struct {
640 LEScanType uint8
641 LEScanInterval uint16
642 LEScanWindow uint16
643 OwnAddressType uint8
644 ScanningFilterPolicy uint8
645 }
646
647 func (c LESetScanParameters) Opcode() int { return opLESetScanParameters }
648 func (c LESetScanParameters) Len() int { return 7 }
649 func (c LESetScanParameters) Marshal(b []byte) {
650 o.PutUint8(b[0:], c.LEScanType)
651 o.PutUint16(b[1:], c.LEScanInterval)
652 o.PutUint16(b[3:], c.LEScanWindow)
653 o.PutUint8(b[5:], c.OwnAddressType)
654 o.PutUint8(b[6:], c.ScanningFilterPolicy)
655 }
656
657 type LESetScanParametersRP struct{ Status uint8 }
658
659 // LE Set Scan Enable (0x000C)
660 type LESetScanEnable struct {
661 LEScanEnable uint8
662 FilterDuplicates uint8
663 }
664
665 func (c LESetScanEnable) Opcode() int { return opLESetScanEnable }
666 func (c LESetScanEnable) Len() int { return 2 }
667 func (c LESetScanEnable) Marshal(b []byte) { b[0], b[1] = c.LEScanEnable, c.FilterDuplicates }
668
669 type LESetScanEnableRP struct{ Status uint8 }
670
671 // LE Create Connection (0x000D)
672 type LECreateConn struct {
673 LEScanInterval uint16
674 LEScanWindow uint16
675 InitiatorFilterPolicy uint8
676 PeerAddressType uint8
677 PeerAddress [6]byte
678 OwnAddressType uint8
679 ConnIntervalMin uint16
680 ConnIntervalMax uint16
681 ConnLatency uint16
682 SupervisionTimeout uint16
683 MinimumCELength uint16
684 MaximumCELength uint16
685 }
686
687 func (c LECreateConn) Opcode() int { return opLECreateConn }
688 func (c LECreateConn) Len() int { return 25 }
689 func (c LECreateConn) Marshal(b []byte) {
690 o.PutUint16(b[0:], c.LEScanInterval)
691 o.PutUint16(b[2:], c.LEScanWindow)
692 o.PutUint8(b[4:], c.InitiatorFilterPolicy)
693 o.PutUint8(b[5:], c.PeerAddressType)
694 o.PutMAC(b[6:], c.PeerAddress)
695 o.PutUint8(b[12:], c.OwnAddressType)
696 o.PutUint16(b[13:], c.ConnIntervalMin)
697 o.PutUint16(b[15:], c.ConnIntervalMax)
698 o.PutUint16(b[17:], c.ConnLatency)
699 o.PutUint16(b[19:], c.SupervisionTimeout)
700 o.PutUint16(b[21:], c.MinimumCELength)
701 o.PutUint16(b[23:], c.MaximumCELength)
702 }
703
704 type LECreateConnRP struct{}
705
706 // LE Create Connection Cancel (0x000E)
707 type LECreateConnCancel struct{}
708
709 func (c LECreateConnCancel) Opcode() int { return opLECreateConnCancel }
710 func (c LECreateConnCancel) Len() int { return 0 }
711 func (c LECreateConnCancel) Marshal(b []byte) {}
712
713 type LECreateConnCancelRP struct{ Status uint8 }
714
715 // LE Read White List Size (0x000F)
716 type LEReadWhiteListSize struct{}
717
718 func (c LEReadWhiteListSize) Opcode() int { return opLEReadWhiteListSize }
719 func (c LEReadWhiteListSize) Len() int { return 0 }
720 func (c LEReadWhiteListSize) Marshal(b []byte) {}
721
722 type LEReadWhiteListSizeRP struct {
723 Status uint8
724 WhiteListSize uint8
725 }
726
727 // LE Clear White List (0x0010)
728 type LEClearWhiteList struct{}
729
730 func (c LEClearWhiteList) Opcode() int { return opLEClearWhiteList }
731 func (c LEClearWhiteList) Len() int { return 0 }
732 func (c LEClearWhiteList) Marshal(b []byte) {}
733
734 type LEClearWhiteListRP struct{ Status uint8 }
735
736 // LE Add Device To White List (0x0011)
737 type LEAddDeviceToWhiteList struct {
738 AddressType uint8
739 Address [6]byte
740 }
741
742 func (c LEAddDeviceToWhiteList) Opcode() int { return opLEAddDeviceToWhiteList }
743 func (c LEAddDeviceToWhiteList) Len() int { return 7 }
744 func (c LEAddDeviceToWhiteList) Marshal(b []byte) {
745 b[0] = c.AddressType
746 o.PutMAC(b[1:], c.Address)
747 }
748
749 type LEAddDeviceToWhiteListRP struct{ Status uint8 }
750
751 // LE Remove Device From White List (0x0012)
752 type LERemoveDeviceFromWhiteList struct {
753 AddressType uint8
754 Address [6]byte
755 }
756
757 func (c LERemoveDeviceFromWhiteList) Opcode() int { return opLERemoveDeviceFromWhiteList }
758 func (c LERemoveDeviceFromWhiteList) Len() int { return 7 }
759 func (c LERemoveDeviceFromWhiteList) Marshal(b []byte) {
760 b[0] = c.AddressType
761 o.PutMAC(b[1:], c.Address)
762 }
763
764 type LERemoveDeviceFromWhiteListRP struct{ Status uint8 }
765
766 // LE Connection Update (0x0013)
767 type LEConnUpdate struct {
768 ConnectionHandle uint16
769 ConnIntervalMin uint16
770 ConnIntervalMax uint16
771 ConnLatency uint16
772 SupervisionTimeout uint16
773 MinimumCELength uint16
774 MaximumCELength uint16
775 }
776
777 func (c LEConnUpdate) Opcode() int { return opLEConnUpdate }
778 func (c LEConnUpdate) Len() int { return 14 }
779 func (c LEConnUpdate) Marshal(b []byte) {
780 o.PutUint16(b[0:], c.ConnectionHandle)
781 o.PutUint16(b[2:], c.ConnIntervalMin)
782 o.PutUint16(b[4:], c.ConnIntervalMax)
783 o.PutUint16(b[6:], c.ConnLatency)
784 o.PutUint16(b[8:], c.SupervisionTimeout)
785 o.PutUint16(b[10:], c.MinimumCELength)
786 o.PutUint16(b[12:], c.MaximumCELength)
787 }
788
789 type LEConnUpdateRP struct{}
790
791 // LE Set Host Channel Classification (0x0014)
792 type LESetHostChannelClassification struct{ ChannelMap [5]byte }
793
794 func (c LESetHostChannelClassification) Opcode() int { return opLESetHostChannelClassification }
795 func (c LESetHostChannelClassification) Len() int { return 5 }
796 func (c LESetHostChannelClassification) Marshal(b []byte) { copy(b, c.ChannelMap[:]) }
797
798 type LESetHostChannelClassificationRP struct{ Status uint8 }
799
800 // LE Read Channel Map (0x0015)
801 type LEReadChannelMap struct{ ConnectionHandle uint16 }
802
803 func (c LEReadChannelMap) Opcode() int { return opLEReadChannelMap }
804 func (c LEReadChannelMap) Len() int { return 2 }
805 func (c LEReadChannelMap) Marshal(b []byte) { o.PutUint16(b, c.ConnectionHandle) }
806
807 type LEReadChannelMapRP struct {
808 Status uint8
809 ConnectionHandle uint16
810 ChannelMap [5]byte
811 }
812
813 // LE Read Remote Used Features (0x0016)
814 type LEReadRemoteUsedFeatures struct{ ConnectionHandle uint16 }
815
816 func (c LEReadRemoteUsedFeatures) Opcode() int { return opLEReadRemoteUsedFeatures }
817 func (c LEReadRemoteUsedFeatures) Len() int { return 8 }
818 func (c LEReadRemoteUsedFeatures) Marshal(b []byte) { o.PutUint16(b, c.ConnectionHandle) }
819
820 type LEReadRemoteUsedFeaturesRP struct{}
821
822 // LE Encrypt (0x0017)
823 type LEEncrypt struct {
824 Key [16]byte
825 PlaintextData [16]byte
826 }
827
828 func (c LEEncrypt) Opcode() int { return opLEEncrypt }
829 func (c LEEncrypt) Len() int { return 32 }
830 func (c LEEncrypt) Marshal(b []byte) {
831 copy(b[0:], c.Key[:])
832 copy(b[16:], c.PlaintextData[:])
833 }
834
835 type LEEncryptRP struct {
836 Stauts uint8
837 EncryptedData [16]byte
838 }
839
840 // LE Rand (0x0018)
841 type LERand struct{}
842
843 func (c LERand) Opcode() int { return opLERand }
844 func (c LERand) Len() int { return 0 }
845 func (c LERand) Marshal(b []byte) {}
846
847 type LERandRP struct {
848 Status uint8
849 RandomNumber uint64
850 }
851
852 // LE Start Encryption (0x0019)
853 type LEStartEncryption struct {
854 ConnectionHandle uint16
855 RandomNumber uint64
856 EncryptedDiversifier uint16
857 LongTermKey [16]byte
858 }
859
860 func (c LEStartEncryption) Opcode() int { return opLEStartEncryption }
861 func (c LEStartEncryption) Len() int { return 28 }
862 func (c LEStartEncryption) Marshal(b []byte) {
863 o.PutUint16(b[0:], c.ConnectionHandle)
864 o.PutUint64(b[2:], c.RandomNumber)
865 o.PutUint16(b[10:], c.EncryptedDiversifier)
866 copy(b[12:], c.LongTermKey[:])
867 }
868
869 type LEStartEncryptionRP struct{}
870
871 // LE Long Term Key Reply (0x001A)
872 type LELTKReply struct {
873 ConnectionHandle uint16
874 LongTermKey [16]byte
875 }
876
877 func (c LELTKReply) Opcode() int { return opLELTKReply }
878 func (c LELTKReply) Len() int { return 18 }
879 func (c LELTKReply) Marshal(b []byte) {
880 o.PutUint16(b[0:], c.ConnectionHandle)
881 copy(b[2:], c.LongTermKey[:])
882 }
883
884 type LELTKReplyRP struct {
885 Status uint8
886 ConnectionHandle uint16
887 }
888
889 // LE Long Term Key Negative Reply (0x001B)
890 type LELTKNegReply struct{ ConnectionHandle uint16 }
891
892 func (c LELTKNegReply) Opcode() int { return opLELTKNegReply }
893 func (c LELTKNegReply) Len() int { return 2 }
894 func (c LELTKNegReply) Marshal(b []byte) { o.PutUint16(b, c.ConnectionHandle) }
895
896 type LELTKNegReplyRP struct {
897 Status uint8
898 ConnectionHandle uint16
899 }
900
901 // LE Read Supported States (0x001C)
902 type LEReadSupportedStates struct{}
903
904 func (c LEReadSupportedStates) Opcode() int { return opLEReadSupportedStates }
905 func (c LEReadSupportedStates) Len() int { return 0 }
906 func (c LEReadSupportedStates) Marshal(b []byte) {}
907
908 type LEReadSupportedStatesRP struct {
909 Status uint8
910 LEStates [8]byte
911 }
912
913 // LE Reciever Test (0x001D)
914 type LEReceiverTest struct{ RxChannel uint8 }
915
916 func (c LEReceiverTest) Opcode() int { return opLEReceiverTest }
917 func (c LEReceiverTest) Len() int { return 1 }
918 func (c LEReceiverTest) Marshal(b []byte) { b[0] = c.RxChannel }
919
920 type LEReceiverTestRP struct{ Status uint8 }
921
922 // LE Transmitter Test (0x001E)
923 type LETransmitterTest struct {
924 TxChannel uint8
925 LengthOfTestData uint8
926 PacketPayload uint8
927 }
928
929 func (c LETransmitterTest) Opcode() int { return opLETransmitterTest }
930 func (c LETransmitterTest) Len() int { return 3 }
931 func (c LETransmitterTest) Marshal(b []byte) {
932 b[0], b[1], b[2] = c.TxChannel, c.LengthOfTestData, c.PacketPayload
933 }
934
935 type LETransmitterTestRP struct{ Status uint8 }
936
937 // LE Test End (0x001F)
938 type LETestEnd struct{}
939
940 func (c LETestEnd) Opcode() int { return opLETestEnd }
941 func (c LETestEnd) Len() int { return 0 }
942 func (c LETestEnd) Marshal(b []byte) {}
943
944 type LETestEndRP struct {
945 Status uint8
946 NumberOfPackets uint16
947 }
948
949 // LE Remote Connection Parameters Reply (0x0020)
950 type LERemoteConnectionParameterReply struct {
951 ConnectionHandle uint16
952 IntervalMin uint16
953 IntervalMax uint16
954 Latency uint16
955 Timeout uint16
956 MinimumCELength uint16
957 MaximumCELength uint16
958 }
959
960 func (c LERemoteConnectionParameterReply) Opcode() int { return opLERemoteConnectionParameterReply }
961 func (c LERemoteConnectionParameterReply) Len() int { return 14 }
962 func (c LERemoteConnectionParameterReply) Marshal(b []byte) {
963 o.PutUint16(b[0:], c.ConnectionHandle)
964 o.PutUint16(b[2:], c.IntervalMin)
965 o.PutUint16(b[4:], c.IntervalMax)
966 o.PutUint16(b[6:], c.Latency)
967 o.PutUint16(b[8:], c.Timeout)
968 o.PutUint16(b[10:], c.MinimumCELength)
969 o.PutUint16(b[12:], c.MaximumCELength)
970 }
971
972 type LERemoteConnectionParameterReplyRP struct {
973 Status uint8
974 ConnectionHandle uint16
975 }
976
977 // LE Remote Connection Parameters Negative Reply (0x0021)
978 type LERemoteConnectionParameterNegReply struct {
979 ConnectionHandle uint16
980 Reason uint8
981 }
982
983 func (c LERemoteConnectionParameterNegReply) Opcode() int {
984 return opLERemoteConnectionParameterNegReply
985 }
986 func (c LERemoteConnectionParameterNegReply) Len() int { return 3 }
987 func (c LERemoteConnectionParameterNegReply) Marshal(b []byte) {
988 o.PutUint16(b[0:], c.ConnectionHandle)
989 b[2] = c.Reason
990 }
991
992 type LERemoteConnectionParameterNegReplyRP struct {
993 Status uint8
994 ConnectionHandle uint16
995 }