| 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:] |
| 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 |
| 53 |
InquiryResult = 0x02 |
| 54 |
ConnectionComplete = 0x03 |
| 55 |
ConnectionRequest = 0x04 |
| 56 |
DisconnectionComplete = 0x05 |
| 57 |
AuthenticationComplete = 0x06 |
| 58 |
RemoteNameReqComplete = 0x07 |
| 59 |
EncryptionChange = 0x08 |
| 60 |
ChangeConnectionLinkKeyComplete = 0x09 |
| 61 |
MasterLinkKeyComplete = 0x0A |
| 62 |
ReadRemoteSupportedFeaturesComplete = 0x0B |
| 63 |
ReadRemoteVersionInformationComplete = 0x0C |
| 64 |
QoSSetupComplete = 0x0D |
| 65 |
CommandComplete = 0x0E |
| 66 |
CommandStatus = 0x0F |
| 67 |
HardwareError = 0x10 |
| 68 |
FlushOccurred = 0x11 |
| 69 |
RoleChange = 0x12 |
| 70 |
NumberOfCompletedPkts = 0x13 |
| 71 |
ModeChange = 0x14 |
| 72 |
ReturnLinkKeys = 0x15 |
| 73 |
PinCodeRequest = 0x16 |
| 74 |
LinkKeyRequest = 0x17 |
| 75 |
LinkKeyNotification = 0x18 |
| 76 |
LoopbackCommand = 0x19 |
| 77 |
DataBufferOverflow = 0x1A |
| 78 |
MaxSlotsChange = 0x1B |
| 79 |
ReadClockOffsetComplete = 0x1C |
| 80 |
ConnectionPtypeChanged = 0x1D |
| 81 |
QoSViolation = 0x1E |
| 82 |
PageScanRepetitionModeChange = 0x20 |
| 83 |
FlowSpecificationComplete = 0x21 |
| 84 |
InquiryResultWithRssi = 0x22 |
| 85 |
ReadRemoteExtendedFeaturesComplete = 0x23 |
| 86 |
SyncConnectionComplete = 0x2C |
| 87 |
SyncConnectionChanged = 0x2D |
| 88 |
SniffSubrating = 0x2E |
| 89 |
ExtendedInquiryResult = 0x2F |
| 90 |
EncryptionKeyRefreshComplete = 0x30 |
| 91 |
IOCapabilityRequest = 0x31 |
| 92 |
IOCapabilityResponse = 0x32 |
| 93 |
UserConfirmationRequest = 0x33 |
| 94 |
UserPasskeyRequest = 0x34 |
| 95 |
RemoteOOBDataRequest = 0x35 |
| 96 |
SimplePairingComplete = 0x36 |
| 97 |
LinkSupervisionTimeoutChanged = 0x38 |
| 98 |
EnhancedFlushComplete = 0x39 |
| 99 |
UserPasskeyNotify = 0x3B |
| 100 |
KeypressNotify = 0x3C |
| 101 |
RemoteHostFeaturesNotify = 0x3D |
| 102 |
LEMeta = 0x3E |
| 103 |
PhysicalLinkComplete = 0x40 |
| 104 |
ChannelSelected = 0x41 |
| 105 |
DisconnectionPhysicalLinkComplete = 0x42 |
| 106 |
PhysicalLinkLossEarlyWarning = 0x43 |
| 107 |
PhysicalLinkRecovery = 0x44 |
| 108 |
LogicalLinkComplete = 0x45 |
| 109 |
DisconnectionLogicalLinkComplete = 0x46 |
| 110 |
FlowSpecModifyComplete = 0x47 |
| 111 |
NumberOfCompletedBlocks = 0x48 |
| 112 |
AMPStartTest = 0x49 |
| 113 |
AMPTestEnd = 0x4A |
| 114 |
AMPReceiverReport = 0x4b |
| 115 |
AMPStatusChange = 0x4D |
| 116 |
TriggeredClockCapture = 0x4e |
| 117 |
SynchronizationTrainComplete = 0x4F |
| 118 |
SynchronizationTrainReceived = 0x50 |
| 119 |
ConnectionlessSlaveBroadcastReceive = 0x51 |
| 120 |
ConnectionlessSlaveBroadcastTimeout = 0x52 |
| 121 |
TruncatedPageComplete = 0x53 |
| 122 |
SlavePageResponseTimeout = 0x54 |
| 123 |
ConnectionlessSlaveBroadcastChannelMapChange = 0x55 |
| 124 |
InquiryResponseNotification = 0x56 |
| 125 |
AuthenticatedPayloadTimeoutExpired = 0x57 |
| 126 |
) |
| 127 |
|
| 128 |
type LEEventCode int |
| 129 |
|
| 130 |
const ( |
| 131 |
LEConnectionComplete LEEventCode = 0x01 |
| 132 |
LEAdvertisingReport = 0x02 |
| 133 |
LEConnectionUpdateComplete = 0x03 |
| 134 |
LEReadRemoteUsedFeaturesComplete = 0x04 |
| 135 |
LELTKRequest = 0x05 |
| 136 |
LERemoteConnectionParameterRequest = 0x06 |
| 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 |
|
| 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 |
|
| 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 |
} |