Skip to content

Commit 0105c68

Browse files
Merge pull request currantlabs#50 from rkjdid/conn-handlers
hci: add Connect/DisconnectHandler options
2 parents 6e59c69 + 5ba052e commit 0105c68

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

linux/hci/hci.go

+9
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ type HCI struct {
110110
chMasterConn chan *Conn // Dial returns master connections.
111111
chSlaveConn chan *Conn // Peripheral accept slave connections.
112112

113+
connectedHandler func(evt.LEConnectionComplete)
114+
disconnectedHandler func(evt.DisconnectionComplete)
115+
113116
dialerTmo time.Duration
114117
listenerTmo time.Duration
115118

@@ -505,6 +508,9 @@ func (h *HCI) handleLEConnectionComplete(b []byte) error {
505508
}
506509
h.params.RUnlock()
507510
}
511+
if h.connectedHandler != nil {
512+
h.connectedHandler(e)
513+
}
508514
return nil
509515
}
510516

@@ -546,6 +552,9 @@ func (h *HCI) handleDisconnectionComplete(b []byte) error {
546552
c.txBuffer.LockPool()
547553
c.txBuffer.PutAll()
548554
c.txBuffer.UnlockPool()
555+
if h.disconnectedHandler != nil {
556+
h.disconnectedHandler(e)
557+
}
549558
return nil
550559
}
551560

linux/hci/option.go

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package hci
22

33
import (
44
"errors"
5+
"github.com/go-ble/ble/linux/hci/evt"
56
"time"
67

78
"github.com/go-ble/ble/linux/hci/cmd"
@@ -37,6 +38,18 @@ func (h *HCI) SetScanParams(param cmd.LESetScanParameters) error {
3738
return nil
3839
}
3940

41+
// SetConnectedHandler sets handler to be called when new connection is established.
42+
func (h *HCI) SetConnectedHandler(f func(complete evt.LEConnectionComplete)) error {
43+
h.connectedHandler = f
44+
return nil
45+
}
46+
47+
// SetDisconnectedHandler sets handler to be called on disconnect.
48+
func (h *HCI) SetDisconnectedHandler(f func(evt.DisconnectionComplete)) error {
49+
h.disconnectedHandler = f
50+
return nil
51+
}
52+
4053
// SetAdvParams overrides default advertising parameters.
4154
func (h *HCI) SetAdvParams(param cmd.LESetAdvertisingParameters) error {
4255
h.params.advParams = param

option.go

+17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ble
22

33
import (
4+
"github.com/go-ble/ble/linux/hci/evt"
45
"time"
56

67
"github.com/go-ble/ble/linux/hci/cmd"
@@ -14,6 +15,8 @@ type DeviceOption interface {
1415
SetConnParams(cmd.LECreateConnection) error
1516
SetScanParams(cmd.LESetScanParameters) error
1617
SetAdvParams(cmd.LESetAdvertisingParameters) error
18+
SetConnectedHandler(f func(evt.LEConnectionComplete)) error
19+
SetDisconnectedHandler(f func(evt.DisconnectionComplete)) error
1720
SetPeripheralRole() error
1821
SetCentralRole() error
1922
}
@@ -69,6 +72,20 @@ func OptAdvParams(param cmd.LESetAdvertisingParameters) Option {
6972
}
7073
}
7174

75+
func OptConnectHandler(f func(evt.LEConnectionComplete)) Option {
76+
return func(opt DeviceOption) error {
77+
opt.SetConnectedHandler(f)
78+
return nil
79+
}
80+
}
81+
82+
func OptDisconnectHandler(f func(evt.DisconnectionComplete)) Option {
83+
return func(opt DeviceOption) error {
84+
opt.SetDisconnectedHandler(f)
85+
return nil
86+
}
87+
}
88+
7289
// OptPeripheralRole configures the device to perform Peripheral tasks.
7390
func OptPeripheralRole() Option {
7491
return func(opt DeviceOption) error {

0 commit comments

Comments
 (0)