Repository: incubator-mynewt-newtmgr Updated Branches: refs/heads/master 822cea97d -> 5af109bf9
misc 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/c2770f19 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/c2770f19 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/c2770f19 Branch: refs/heads/master Commit: c2770f19ceebc27e8ec61410d3bc03e6327bc857 Parents: 822cea9 Author: Christopher Collins <[email protected]> Authored: Wed May 17 17:12:44 2017 -0700 Committer: Christopher Collins <[email protected]> Committed: Wed May 17 17:12:44 2017 -0700 ---------------------------------------------------------------------- nmxact/nmble/ble_scanner.go | 47 ++++++++++++++++++++++++++++++++++++---- nmxact/nmble/ble_xport.go | 3 +++ nmxact/scan/scan.go | 2 ++ 3 files changed, 48 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/c2770f19/nmxact/nmble/ble_scanner.go ---------------------------------------------------------------------- diff --git a/nmxact/nmble/ble_scanner.go b/nmxact/nmble/ble_scanner.go index 4cdcf07..28605df 100644 --- a/nmxact/nmble/ble_scanner.go +++ b/nmxact/nmble/ble_scanner.go @@ -1,8 +1,10 @@ package nmble import ( + "bytes" "encoding/base64" "fmt" + "sync" log "github.com/Sirupsen/logrus" @@ -21,6 +23,9 @@ type BleScanner struct { bos *BleOicSesn od *omp.OmpDispatcher enabled bool + + // Protects accesses to the reported devices map. + devMapMtx sync.Mutex } func NewBleScanner(bx *BleXport) *BleScanner { @@ -36,8 +41,8 @@ func (s *BleScanner) scan() (scan.ScanPeer, error) { } defer s.bos.Close() - // Now we are connected and paired. Read the peer's hardware ID and report - // it upstream. + // Now we are connected (and paired if required). Read the peer's hardware + // ID and report it upstream. desc, err := s.bos.ConnInfo() if err != nil { @@ -85,10 +90,15 @@ func (s *BleScanner) Start(cfg scan.Cfg) error { innerPred := cfg.SesnCfg.Ble.PeerSpec.ScanPred cfg.SesnCfg.Ble.PeerSpec.ScanPred = func(adv BleAdvReport) bool { // Filter devices that have already been reported. - if s.reportedDevs[adv.Sender] != nil { + s.devMapMtx.Lock() + seen := s.reportedDevs[adv.Sender] != nil + s.devMapMtx.Unlock() + + if seen { return false + } else { + return innerPred(adv) } - return innerPred(adv) } session, err := s.bx.BuildSesn(cfg.SesnCfg) @@ -107,7 +117,10 @@ func (s *BleScanner) Start(cfg scan.Cfg) error { if err != nil { log.Debugf("Scan error: %s", err.Error()) } else { + s.devMapMtx.Lock() s.reportedDevs[p.Opaque.(BleDev)] = p.HwId + s.devMapMtx.Unlock() + s.cfg.ScanCb(p) } } @@ -125,3 +138,29 @@ func (s *BleScanner) Stop() error { s.bos.Close() return nil } + +// @return true if the specified device was found and +// forgetten; +// false if the specified device is unknown. +func (s *BleScanner) ForgetDevice(hwid []byte) bool { + s.devMapMtx.Lock() + defer s.devMapMtx.Unlock() + + for d, h := range s.reportedDevs { + if bytes.Compare(h, hwid) == 0 { + delete(s.reportedDevs, d) + return true + } + } + + return false +} + +func (s *BleScanner) ForgetAllDevices() { + s.devMapMtx.Lock() + defer s.devMapMtx.Unlock() + + for d, _ := range s.reportedDevs { + delete(s.reportedDevs, d) + } +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/c2770f19/nmxact/nmble/ble_xport.go ---------------------------------------------------------------------- diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go index fdaf07c..5501255 100644 --- a/nmxact/nmble/ble_xport.go +++ b/nmxact/nmble/ble_xport.go @@ -126,6 +126,9 @@ func (bx *BleXport) createUnixChild() { } func (bx *BleXport) BuildScanner() (scan.Scanner, error) { + // The transport only allows a single scanner. This is because the + // master privileges need to managed among the scanner and the + // sessions. if bx.scanner == nil { bx.scanner = NewBleScanner(bx) } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/c2770f19/nmxact/scan/scan.go ---------------------------------------------------------------------- diff --git a/nmxact/scan/scan.go b/nmxact/scan/scan.go index c90546e..cadf5f9 100644 --- a/nmxact/scan/scan.go +++ b/nmxact/scan/scan.go @@ -22,6 +22,8 @@ type Scanner interface { Stop() error } +// Constructs a scan configuration suitable for discovery of OMP +// (Newtmgr-over-CoAP) Mynewt devices. func BleOmpScanCfg(ScanCb ScanFn) Cfg { sc := sesn.NewSesnCfg() sc.MgmtProto = sesn.MGMT_PROTO_OMP
