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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
package evt
import (
"bytes"
"encoding/binary"
"errors"
"github.com/paypal/gatt/linux/util"
)
type EventHandler interface {
HandleEvent([]byte) error
}
type HandlerFunc func(b []byte) error
func (f HandlerFunc) HandleEvent(b []byte) error {
return f(b)
}
type Evt struct {
evtHandlers map[int]EventHandler
}
func NewEvt() *Evt {
return &Evt{
evtHandlers: map[int]EventHandler{},
}
}
func (e *Evt) HandleEvent(c int, h EventHandler) {
e.evtHandlers[c] = h
}
func (e *Evt) Dispatch(b []byte) error {
h := &EventHeader{}
if err := h.Unmarshal(b); err != nil {
return err
}
b = b[2:] // Skip Event Header (uint8 + uint8)
if f, found := e.evtHandlers[h.code]; found {
e.trace("> HCI Event: %s (0x%02X) plen %d: [ % X ])\n", h.code, uint8(h.code), h.plen, b)
return f.HandleEvent(b)
}
e.trace("> HCI Event: no handler for %s (0x%02X)\n", h.code, uint8(h.code))
return nil
}
func (e *Evt) trace(fmt string, v ...interface{}) {}
const (
InquiryComplete = 0x01 // Inquiry Complete
InquiryResult = 0x02 // Inquiry Result
ConnectionComplete = 0x03 // Connection Complete
ConnectionRequest = 0x04 // Connection Request
DisconnectionComplete = 0x05 // Disconnection Complete
AuthenticationComplete = 0x06 // Authentication
RemoteNameReqComplete = 0x07 // Remote Name Request Complete
EncryptionChange = 0x08 // Encryption Change
ChangeConnectionLinkKeyComplete = 0x09 // Change Conection Link Key Complete
MasterLinkKeyComplete = 0x0A // Master Link Keye Complete
ReadRemoteSupportedFeaturesComplete = 0x0B // Read Remote Supported Features Complete
ReadRemoteVersionInformationComplete = 0x0C // Read Remote Version Information Complete
QoSSetupComplete = 0x0D // QoSSetupComplete
CommandComplete = 0x0E // Command Complete
CommandStatus = 0x0F // Command status
HardwareError = 0x10 // Hardware Error
FlushOccurred = 0x11 // Flush Occured
RoleChange = 0x12 // Role Change
NumberOfCompletedPkts = 0x13 // Number Of Completed Packets
ModeChange = 0x14 // Mode Change
ReturnLinkKeys = 0x15 // Return Link Keys
PinCodeRequest = 0x16 // PIN Code Request
LinkKeyRequest = 0x17 // Link Key Request
LinkKeyNotification = 0x18 // Link Key Notification
LoopbackCommand = 0x19 // Loopback Command
DataBufferOverflow = 0x1A // Data Buffer Overflow
MaxSlotsChange = 0x1B // Max Slots Change
ReadClockOffsetComplete = 0x1C // Read Clock Offset Complete
ConnectionPtypeChanged = 0x1D // Connection Packet Type Changed
QoSViolation = 0x1E // QoS Violation
PageScanRepetitionModeChange = 0x20 // Page Scan Repetition Mode Change
FlowSpecificationComplete = 0x21 // Flow Specification
InquiryResultWithRssi = 0x22 // Inquery Result with RSSI
ReadRemoteExtendedFeaturesComplete = 0x23 // Read Remote Extended Features Complete
SyncConnectionComplete = 0x2C // Synchronous Connection Complete
SyncConnectionChanged = 0x2D // Synchronous Connection Changed
SniffSubrating = 0x2E // Sniff Subrating
ExtendedInquiryResult = 0x2F // Extended Inquiry Result
EncryptionKeyRefreshComplete = 0x30 // Encryption Key Refresh Complete
IOCapabilityRequest = 0x31 // IO Capability Request
IOCapabilityResponse = 0x32 // IO Capability Changed
UserConfirmationRequest = 0x33 // User Confirmation Request
UserPasskeyRequest = 0x34 // User Passkey Request
RemoteOOBDataRequest = 0x35 // Remote OOB Data
SimplePairingComplete = 0x36 // Simple Pairing Complete
LinkSupervisionTimeoutChanged = 0x38 // Link Supervision Timeout Changed
EnhancedFlushComplete = 0x39 // Enhanced Flush Complete
UserPasskeyNotify = 0x3B // User Passkey Notification
KeypressNotify = 0x3C // Keypass Notification
RemoteHostFeaturesNotify = 0x3D // Remote Host Supported Features Notification
LEMeta = 0x3E // LE Meta
PhysicalLinkComplete = 0x40 // Physical Link Complete
ChannelSelected = 0x41 // Channel Selected
DisconnectionPhysicalLinkComplete = 0x42 // Disconnection Physical Link Complete
PhysicalLinkLossEarlyWarning = 0x43 // Physical Link Loss Early Warning
PhysicalLinkRecovery = 0x44 // Physical Link Recovery
LogicalLinkComplete = 0x45 // Logical Link Complete
DisconnectionLogicalLinkComplete = 0x46 // Disconnection Logical Link Complete
FlowSpecModifyComplete = 0x47 // Flow Spec Modify Complete
NumberOfCompletedBlocks = 0x48 // Number Of Completed Data Blocks
AMPStartTest = 0x49 // AMP Start Test
AMPTestEnd = 0x4A // AMP Test End
AMPReceiverReport = 0x4b // AMP Receiver Report
AMPStatusChange = 0x4D // AMP status Change
TriggeredClockCapture = 0x4e // Triggered Clock Capture
SynchronizationTrainComplete = 0x4F // Synchronization Train Complete
SynchronizationTrainReceived = 0x50 // Synchronization Train Received
ConnectionlessSlaveBroadcastReceive = 0x51 // Connectionless Slave Broadcast Receive
ConnectionlessSlaveBroadcastTimeout = 0x52 // Connectionless Slave Broadcast Timeout
TruncatedPageComplete = 0x53 // Truncated Page Complete
SlavePageResponseTimeout = 0x54 // Slave Page Response Timeout
ConnectionlessSlaveBroadcastChannelMapChange = 0x55 // Connectionless Slave Broadcast Channel Map Change
InquiryResponseNotification = 0x56 // Inquiry Response Notification
AuthenticatedPayloadTimeoutExpired = 0x57 // Authenticated Payload Timeout Expired
)
type LEEventCode int
const (
LEConnectionComplete LEEventCode = 0x01 // LE Connection Complete
LEAdvertisingReport = 0x02 // LE Advertising Report
LEConnectionUpdateComplete = 0x03 // LE Connection Update Complete
LEReadRemoteUsedFeaturesComplete = 0x04 // LE Read Remote Used Features Complete
LELTKRequest = 0x05 // LE LTK Request
LERemoteConnectionParameterRequest = 0x06 // LE Remote Connection Parameter Request
)
type EventHeader struct {
code int
plen uint8
}
func (h *EventHeader) Unmarshal(b []byte) error {
if len(b) < 2 {
return errors.New("malformed header")
}
h.code = int(b[0])
h.plen = b[1]
if uint8(len(b)) != 2+h.plen {
return errors.New("wrong length")
}
return nil
}
var o = util.Order
// Event Parameters
type InquiryCompleteEP struct {
Status uint8
}
type InquiryResultEP struct {
NumResponses uint8
BDAddr [][6]byte
PageScanRepetitionMode []uint8
Reserved1 []byte
Reserved2 []byte
ClassOfDevice [][3]byte
ClockOffset []uint16
}
type ConnectionCompleteEP struct {
Status uint8
ConnectionHandle uint16
BDAddr [6]byte
LinkType uint8
EncryptionEnabled uint8
}
type ConnectionRequestEP struct {
BDAddr [6]byte
ClassofDevice [3]byte
LinkType uint8
}
type DisconnectionCompleteEP struct {
Status uint8
ConnectionHandle uint16
Reason uint8
}
func (e *DisconnectionCompleteEP) Unmarshal(b []byte) error {
buf := bytes.NewBuffer(b)
binary.Read(buf, binary.LittleEndian, &e.Status)
binary.Read(buf, binary.LittleEndian, &e.ConnectionHandle)
return binary.Read(buf, binary.LittleEndian, &e.Reason)
}
type CommandCompleteEP struct {
NumHCICommandPackets uint8
CommandOPCode uint16
ReturnParameters []byte
}
func (e *CommandCompleteEP) Unmarshal(b []byte) error {
buf := bytes.NewBuffer(b)
if err := binary.Read(buf, binary.LittleEndian, &e.NumHCICommandPackets); err != nil {
return err
}
if err := binary.Read(buf, binary.LittleEndian, &e.CommandOPCode); err != nil {
return err
}
e.ReturnParameters = buf.Bytes()
return nil
}
type CommandStatusEP struct {
Status uint8
NumHCICommandPackets uint8
CommandOpcode uint16
}
func (e *CommandStatusEP) Unmarshal(b []byte) error {
buf := bytes.NewBuffer(b)
binary.Read(buf, binary.LittleEndian, &e.Status)
binary.Read(buf, binary.LittleEndian, &e.NumHCICommandPackets)
return binary.Read(buf, binary.LittleEndian, &e.CommandOpcode)
}
type NumOfCompletedPkt struct {
ConnectionHandle uint16
NumOfCompletedPkts uint16
}
type NumberOfCompletedPktsEP struct {
NumberOfHandles uint8
Packets []NumOfCompletedPkt
}
func (e *NumberOfCompletedPktsEP) Unmarshal(b []byte) error {
e.NumberOfHandles = b[0]
n := int(e.NumberOfHandles)
buf := bytes.NewBuffer(b[1:])
e.Packets = make([]NumOfCompletedPkt, n)
for i := 0; i < n; i++ {
binary.Read(buf, binary.LittleEndian, &e.Packets[i].ConnectionHandle)
binary.Read(buf, binary.LittleEndian, &e.Packets[i].NumOfCompletedPkts)
e.Packets[i].ConnectionHandle &= 0xfff
}
return nil
}
// LE Meta Subevents
type LEConnectionCompleteEP struct {
SubeventCode uint8
Status uint8
ConnectionHandle uint16
Role uint8
PeerAddressType uint8
PeerAddress [6]byte
ConnInterval uint16
ConnLatency uint16
SupervisionTimeout uint16
MasterClockAccuracy uint8
}
func (e *LEConnectionCompleteEP) Unmarshal(b []byte) error {
e.SubeventCode = o.Uint8(b[0:])
e.Status = o.Uint8(b[1:])
e.ConnectionHandle = o.Uint16(b[2:])
e.Role = o.Uint8(b[4:])
e.PeerAddressType = o.Uint8(b[5:])
e.PeerAddress = o.MAC(b[6:])
e.ConnInterval = o.Uint16(b[12:])
e.ConnLatency = o.Uint16(b[14:])
e.SupervisionTimeout = o.Uint16(b[16:])
e.MasterClockAccuracy = o.Uint8(b[17:])
return nil
}
type LEAdvertisingReportEP struct {
SubeventCode uint8
NumReports uint8
EventType []uint8
AddressType []uint8
Address [][6]byte
Length []uint8
Data [][]byte
RSSI []int8
}
func (e *LEAdvertisingReportEP) Unmarshal(b []byte) error {
e.SubeventCode = o.Uint8(b)
b = b[1:]
e.NumReports = o.Uint8(b)
b = b[1:]
n := int(e.NumReports)
e.EventType = make([]uint8, n)
e.AddressType = make([]uint8, n)
e.Address = make([][6]byte, n)
e.Length = make([]uint8, n)
e.Data = make([][]byte, n)
e.RSSI = make([]int8, n)
for i := 0; i < n; i++ {
e.EventType[i] = o.Uint8(b)
b = b[1:]
}
for i := 0; i < n; i++ {
e.AddressType[i] = o.Uint8(b)
b = b[1:]
}
for i := 0; i < n; i++ {
e.Address[i] = o.MAC(b)
b = b[6:]
}
for i := 0; i < n; i++ {
e.Length[i] = o.Uint8(b)
b = b[1:]
}
for i := 0; i < n; i++ {
e.Data[i] = make([]byte, e.Length[i])
copy(e.Data[i], b)
b = b[e.Length[i]:]
}
for i := 0; i < n; i++ {
e.RSSI[i] = o.Int8(b)
b = b[1:]
}
return nil
}
type LEConnectionUpdateCompleteEP struct {
SubeventCode uint8
Status uint8
ConnectionHandle uint16
ConnInterval uint16
ConnLatency uint16
SupervisionTimeout uint16
}
func (e *LEConnectionUpdateCompleteEP) Unmarshal(b []byte) error {
return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
}
type LEReadRemoteUsedFeaturesCompleteEP struct {
SubeventCode uint8
Status uint8
ConnectionHandle uint16
LEFeatures uint64
}
func (e *LEReadRemoteUsedFeaturesCompleteEP) Unmarshal(b []byte) error {
return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
}
type LELTKRequestEP struct {
SubeventCode uint8
ConnectionHandle uint16
RandomNumber uint64
EncryptionDiversifier uint16
}
func (e *LELTKRequestEP) Unmarshal(b []byte) error {
return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
}
type LERemoteConnectionParameterRequestEP struct {
SubeventCode uint8
ConnectionHandle uint16
IntervalMin uint16
IntervalMax uint16
Latency uint16
Timeout uint16
}
func (e *LERemoteConnectionParameterRequestEP) Unmarshal(b []byte) error {
return binary.Read(bytes.NewBuffer(b), binary.LittleEndian, e)
}
|