diff --git a/.travis.yml b/.travis.yml index 37dfd94c..d9b98257 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ os: - linux go: - - 1.7 - 1.8 - 1.9 - tip diff --git a/linux/hci/gap.go b/linux/hci/gap.go index d114a0a0..6bb1f1ea 100644 --- a/linux/hci/gap.go +++ b/linux/hci/gap.go @@ -209,28 +209,35 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) { if h.dialerTmo != time.Duration(0) { tmo = time.After(h.dialerTmo) } + select { case <-ctx.Done(): - return nil, ctx.Err() + return h.cancelDial() + case <-tmo: + return h.cancelDial() case <-h.done: return nil, h.err case c := <-h.chMasterConn: 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. func (h *HCI) Advertise() error { h.params.advEnable.AdvertisingEnable = 1