From 9247a1e21999fc91b30d7d0b2799e11f207cd255 Mon Sep 17 00:00:00 2001 From: Kevin Seidel Date: Wed, 14 Mar 2018 10:38:02 +0100 Subject: [PATCH 1/2] Fix cancel via context and respect context deadline --- linux/hci/gap.go | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/linux/hci/gap.go b/linux/hci/gap.go index d114a0a0..ac5bbb9e 100644 --- a/linux/hci/gap.go +++ b/linux/hci/gap.go @@ -206,31 +206,39 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) { return nil, err } var tmo <-chan time.Time - if h.dialerTmo != time.Duration(0) { + if deadline, ok := ctx.Deadline(); ok { + tmo = time.After(time.Until(deadline)) + } else 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 From d6a9d8cbc1621eafba02732d574751d41216027b Mon Sep 17 00:00:00 2001 From: Kevin Seidel Date: Thu, 15 Mar 2018 17:24:12 +0100 Subject: [PATCH 2/2] Remove timeout overwrite and remove go1.7 from travis --- .travis.yml | 1 - linux/hci/gap.go | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) 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 ac5bbb9e..6bb1f1ea 100644 --- a/linux/hci/gap.go +++ b/linux/hci/gap.go @@ -206,11 +206,10 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) { return nil, err } var tmo <-chan time.Time - if deadline, ok := ctx.Deadline(); ok { - tmo = time.After(time.Until(deadline)) - } else if h.dialerTmo != time.Duration(0) { + if h.dialerTmo != time.Duration(0) { tmo = time.After(h.dialerTmo) } + select { case <-ctx.Done(): return h.cancelDial()