Unverified Commit 1dd6d60a authored by Kevin's avatar Kevin Committed by GitHub
Browse files

Fix cancel via context and respect context deadline (#14)

* Fix cancel via context and respect context deadline

* Remove timeout overwrite and remove go1.7 from travis
parent c8fd5401
...@@ -4,7 +4,6 @@ os: ...@@ -4,7 +4,6 @@ os:
- linux - linux
go: go:
- 1.7
- 1.8 - 1.8
- 1.9 - 1.9
- tip - tip
......
...@@ -209,28 +209,35 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) { ...@@ -209,28 +209,35 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) {
if h.dialerTmo != time.Duration(0) { if h.dialerTmo != time.Duration(0) {
tmo = time.After(h.dialerTmo) tmo = time.After(h.dialerTmo)
} }
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil, ctx.Err() return h.cancelDial()
case <-tmo:
return h.cancelDial()
case <-h.done: case <-h.done:
return nil, h.err return nil, h.err
case c := <-h.chMasterConn: case c := <-h.chMasterConn:
return gatt.NewClient(c) return gatt.NewClient(c)
case <-tmo:
err := h.Send(&h.params.connCancel, nil)
if err == nil {
// The pending connection was canceled successfully.
return nil, fmt.Errorf("connection timed out")
}
// The connection has been established, the cancel command
// failed with ErrDisallowed.
if err == ErrDisallowed {
return gatt.NewClient(<-h.chMasterConn)
}
return nil, errors.Wrap(err, "cancel connection failed")
} }
} }
// cancelDial cancels the Dialing
func (h *HCI) cancelDial() (ble.Client, error) {
err := h.Send(&h.params.connCancel, nil)
if err == nil {
// The pending connection was canceled successfully.
return nil, fmt.Errorf("connection canceled")
}
// The connection has been established, the cancel command
// failed with ErrDisallowed.
if err == ErrDisallowed {
return gatt.NewClient(<-h.chMasterConn)
}
return nil, errors.Wrap(err, "cancel connection failed")
}
// Advertise starts advertising. // Advertise starts advertising.
func (h *HCI) Advertise() error { func (h *HCI) Advertise() error {
h.params.advEnable.AdvertisingEnable = 1 h.params.advEnable.AdvertisingEnable = 1
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment