Repository: incubator-mynewt-newtmgr Updated Branches: refs/heads/master 3cb138aba -> e61c99df8
nmxact - Stop scanner before xport teardown. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/commit/e61c99df Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/e61c99df Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/e61c99df Branch: refs/heads/master Commit: e61c99df814c5e9e943adfc7373febaaa8d8c45f Parents: 3cb138a Author: Christopher Collins <[email protected]> Authored: Mon May 22 17:11:11 2017 -0700 Committer: Christopher Collins <[email protected]> Committed: Mon May 22 17:33:31 2017 -0700 ---------------------------------------------------------------------- nmxact/nmble/ble_scanner.go | 45 ++++++++++++++++++++++++++++------------ nmxact/nmble/ble_xport.go | 4 ++++ nmxact/nmble/discover.go | 7 +++++-- nmxact/nmxutil/nmxerr.go | 22 ++++++++++++++++++++ 4 files changed, 63 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e61c99df/nmxact/nmble/ble_scanner.go ---------------------------------------------------------------------- diff --git a/nmxact/nmble/ble_scanner.go b/nmxact/nmble/ble_scanner.go index 338b7cd..ba849b7 100644 --- a/nmxact/nmble/ble_scanner.go +++ b/nmxact/nmble/ble_scanner.go @@ -8,6 +8,7 @@ import ( log "github.com/Sirupsen/logrus" . "mynewt.apache.org/newtmgr/nmxact/bledefs" + "mynewt.apache.org/newtmgr/nmxact/nmxutil" "mynewt.apache.org/newtmgr/nmxact/omp" "mynewt.apache.org/newtmgr/nmxact/scan" "mynewt.apache.org/newtmgr/nmxact/sesn" @@ -26,7 +27,7 @@ type BleScanner struct { enabled bool // Protects accesses to the reported devices map. - devMapMtx sync.Mutex + mtx sync.Mutex } func NewBleScanner(bx *BleXport) *BleScanner { @@ -37,13 +38,20 @@ func NewBleScanner(bx *BleXport) *BleScanner { } func (s *BleScanner) discover() (*BleDev, error) { + s.mtx.Lock() s.discoverer = NewDiscoverer(DiscovererParams{ Bx: s.bx, OwnAddrType: s.cfg.SesnCfg.Ble.OwnAddrType, Passive: false, Duration: 15 * time.Second, }) - defer func() { s.discoverer = nil }() + s.mtx.Unlock() + + defer func() { + s.mtx.Lock() + defer s.mtx.Unlock() + s.discoverer = nil + }() var dev *BleDev advRptCb := func(r BleAdvReport) { @@ -65,7 +73,10 @@ func (s *BleScanner) connect(dev BleDev) error { if err != nil { return err } + + s.mtx.Lock() s.bos = session.(*BleOicSesn) + s.mtx.Unlock() if err := s.bos.Open(); err != nil { return err @@ -131,16 +142,16 @@ func (s *BleScanner) scan() (*scan.ScanPeer, error) { func (s *BleScanner) Start(cfg scan.Cfg) error { if s.enabled { - return fmt.Errorf("Attempt to start BLE scanner twice") + return nmxutil.NewAlreadyError("Attempt to start BLE scanner twice") } // Wrap predicate with logic that discards duplicates. innerPred := cfg.Ble.ScanPred cfg.Ble.ScanPred = func(adv BleAdvReport) bool { // Filter devices that have already been reported. - s.devMapMtx.Lock() + s.mtx.Lock() seen := s.reportedDevs[adv.Sender] != "" - s.devMapMtx.Unlock() + s.mtx.Unlock() if seen { return false @@ -159,9 +170,9 @@ func (s *BleScanner) Start(cfg scan.Cfg) error { if err != nil { log.Debugf("Scan error: %s", err.Error()) } else if p != nil { - s.devMapMtx.Lock() + s.mtx.Lock() s.reportedDevs[p.PeerSpec.Ble] = p.HwId - s.devMapMtx.Unlock() + s.mtx.Unlock() s.cfg.ScanCb(*p) } @@ -173,15 +184,23 @@ func (s *BleScanner) Start(cfg scan.Cfg) error { func (s *BleScanner) Stop() error { if !s.enabled { - return fmt.Errorf("Attempt to stop BLE scanner twice") + return nmxutil.NewAlreadyError("Attempt to stop BLE scanner twice") } s.enabled = false + s.mtx.Lock() discoverer := s.discoverer + bos := s.bos + s.mtx.Unlock() + if discoverer != nil { discoverer.Stop() } - s.bos.Close() + + if bos != nil { + bos.Close() + } + return nil } @@ -189,8 +208,8 @@ func (s *BleScanner) Stop() error { // forgetten; // false if the specified device is unknown. func (s *BleScanner) ForgetDevice(hwId string) bool { - s.devMapMtx.Lock() - defer s.devMapMtx.Unlock() + s.mtx.Lock() + defer s.mtx.Unlock() for discoverer, h := range s.reportedDevs { if h == hwId { @@ -203,8 +222,8 @@ func (s *BleScanner) ForgetDevice(hwId string) bool { } func (s *BleScanner) ForgetAllDevices() { - s.devMapMtx.Lock() - defer s.devMapMtx.Unlock() + s.mtx.Lock() + defer s.mtx.Unlock() for discoverer, _ := range s.reportedDevs { delete(s.reportedDevs, discoverer) http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e61c99df/nmxact/nmble/ble_xport.go ---------------------------------------------------------------------- diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go index 5501255..c8e626f 100644 --- a/nmxact/nmble/ble_xport.go +++ b/nmxact/nmble/ble_xport.go @@ -261,6 +261,10 @@ func (bx *BleXport) shutdown(restart bool, err error) { return } + if bx.scanner != nil { + bx.scanner.Stop() + } + // Stop the unixchild instance (blehostd + socket). if bx.client != nil { bx.client.Stop() http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e61c99df/nmxact/nmble/discover.go ---------------------------------------------------------------------- diff --git a/nmxact/nmble/discover.go b/nmxact/nmble/discover.go index 7650044..45ccb05 100644 --- a/nmxact/nmble/discover.go +++ b/nmxact/nmble/discover.go @@ -1,7 +1,6 @@ package nmble import ( - "fmt" "time" log "github.com/Sirupsen/logrus" @@ -54,6 +53,10 @@ func (d *Discoverer) scanCancel() error { } func (d *Discoverer) Start(advRptCb BleAdvRptFn) error { + if d.abortChan != nil { + return nmxutil.NewAlreadyError("Attempt to start BLE discoverer twice") + } + // Scanning requires dedicated master privileges. if err := d.params.Bx.AcquireMaster(d); err != nil { return err @@ -102,7 +105,7 @@ func (d *Discoverer) Stop() error { ch := d.abortChan if ch == nil { - return fmt.Errorf("Attempt to stop inactive discoverer") + return nmxutil.NewAlreadyError("Attempt to stop inactive discoverer") } ch <- struct{}{} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e61c99df/nmxact/nmxutil/nmxerr.go ---------------------------------------------------------------------- diff --git a/nmxact/nmxutil/nmxerr.go b/nmxact/nmxutil/nmxerr.go index 28930e3..3a6185e 100644 --- a/nmxact/nmxutil/nmxerr.go +++ b/nmxact/nmxutil/nmxerr.go @@ -164,3 +164,25 @@ func ToBleHost(err error) *BleHostError { return nil } } + +// Indicates an attempt to transition to the already-current state. +type AlreadyError struct { + Text string +} + +func NewAlreadyError(text string) *AlreadyError { + return &AlreadyError{text} +} + +func (err *AlreadyError) Error() string { + return err.Text +} + +func IsAlready(err error) bool { + if err == nil { + return false + } + + _, ok := err.(*AlreadyError) + return ok +}
