Skip to content

Commit

Permalink
feature: implement connect handler for Advertiser for HCI
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Jan 9, 2025
1 parent cdf3fe1 commit 62565e6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
45 changes: 40 additions & 5 deletions gap_hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
}()
Expand Down
5 changes: 5 additions & 0 deletions hci.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type leAdvertisingReport struct {

type leConnectData struct {
connected bool
disconnected bool
status uint8
handle uint16
role uint8
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 62565e6

Please sign in to comment.