From 62565e6244b941384216c4238ce2bf280c695cdf Mon Sep 17 00:00:00 2001 From: deadprogram Date: Fri, 3 Jan 2025 20:25:31 +0100 Subject: [PATCH] feature: implement connect handler for Advertiser for HCI Signed-off-by: deadprogram --- gap_hci.go | 45 ++++++++++++++++++++++++++++++++++++++++----- hci.go | 5 +++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/gap_hci.go b/gap_hci.go index 092b676..68bb557 100644 --- a/gap_hci.go +++ b/gap_hci.go @@ -262,11 +262,6 @@ func (d Device) Disconnect() error { } d.adapter.removeConnection(d) - - if d.adapter.connectHandler != nil { - d.adapter.connectHandler(d, false) - } - return nil } @@ -436,6 +431,46 @@ func (a *Advertisement) Start() error { } } + switch { + case a.adapter.hci.connectData.connected: + random := a.adapter.hci.connectData.peerBdaddrType == 0x01 + + d := Device{ + Address: Address{ + MACAddress{ + MAC: makeAddress(a.adapter.hci.connectData.peerBdaddr), + isRandom: random}, + }, + deviceInternal: &deviceInternal{ + adapter: a.adapter, + handle: a.adapter.hci.connectData.handle, + mtu: defaultMTU, + notificationRegistrations: make([]notificationRegistration, 0), + }, + } + a.adapter.addConnection(d) + + if a.adapter.connectHandler != nil { + a.adapter.connectHandler(d, true) + } + + a.adapter.hci.clearConnectData() + case a.adapter.hci.connectData.disconnected: + d := Device{ + deviceInternal: &deviceInternal{ + adapter: a.adapter, + handle: a.adapter.hci.connectData.handle, + }, + } + a.adapter.removeConnection(d) + + if a.adapter.connectHandler != nil { + a.adapter.connectHandler(d, false) + } + + a.adapter.hci.clearConnectData() + } + time.Sleep(5 * time.Millisecond) } }() diff --git a/hci.go b/hci.go index a66f874..241b2f3 100644 --- a/hci.go +++ b/hci.go @@ -113,6 +113,7 @@ type leAdvertisingReport struct { type leConnectData struct { connected bool + disconnected bool status uint8 handle uint16 role uint8 @@ -613,6 +614,9 @@ func (h *hci) handleEventData(buf []byte) error { h.att.removeConnection(handle) h.l2cap.removeConnection(handle) + h.connectData.disconnected = true + h.connectData.handle = handle + return h.leSetAdvertiseEnable(true) case evtEncryptionChange: @@ -818,6 +822,7 @@ func (h *hci) clearAdvData() error { func (h *hci) clearConnectData() error { h.connectData.connected = false + h.connectData.disconnected = false h.connectData.status = 0 h.connectData.handle = 0 h.connectData.role = 0