nmxact - Robustness fixes for ble_xport failures.
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/f573ad10 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/f573ad10 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/f573ad10 Branch: refs/heads/master Commit: f573ad10e48bbeada01d3ea0d621f05f5476a91f Parents: 2509781 Author: Christopher Collins <[email protected]> Authored: Thu Apr 6 12:37:25 2017 -0700 Committer: Christopher Collins <[email protected]> Committed: Fri Apr 7 10:12:38 2017 -0700 ---------------------------------------------------------------------- newtmgr/config/ble_config.go | 4 ++-- nmxact/nmble/ble_fsm.go | 7 ++++++- nmxact/nmble/ble_xport.go | 40 ++++++++++++++++++++++++--------------- 3 files changed, 33 insertions(+), 18 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f573ad10/newtmgr/config/ble_config.go ---------------------------------------------------------------------- diff --git a/newtmgr/config/ble_config.go b/newtmgr/config/ble_config.go index d8fd643..5cb93df 100644 --- a/newtmgr/config/ble_config.go +++ b/newtmgr/config/ble_config.go @@ -24,10 +24,10 @@ import ( "strings" "time" + "mynewt.apache.org/newt/util" "mynewt.apache.org/newtmgr/nmxact/bledefs" "mynewt.apache.org/newtmgr/nmxact/nmble" "mynewt.apache.org/newtmgr/nmxact/sesn" - "mynewt.apache.org/newt/util" ) type BleConfig struct { @@ -125,7 +125,7 @@ func BuildBleXport(bc *BleConfig) (*nmble.BleXport, error) { params.BlehostdPath = bc.BlehostdPath params.DevPath = bc.ControllerPath params.BlehostdAcceptTimeout = 2 * time.Second - params.BlehostdRestart = false + params.Restart = false bx, err := nmble.NewBleXport(params) if err != nil { http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f573ad10/nmxact/nmble/ble_fsm.go ---------------------------------------------------------------------- diff --git a/nmxact/nmble/ble_fsm.go b/nmxact/nmble/ble_fsm.go index 10c2bee..7bf429e 100644 --- a/nmxact/nmble/ble_fsm.go +++ b/nmxact/nmble/ble_fsm.go @@ -641,7 +641,7 @@ func (bf *BleFsm) tryFillPeerDev() bool { // error The error that caused the start attempt to // fail; nil on success. func (bf *BleFsm) Start() (bool, error) { - if bf.getState() != SESN_STATE_UNCONNECTED { + if !bf.IsClosed() { return false, nmxutil.NewSesnAlreadyOpenError( "Attempt to open an already-open BLE session") } @@ -678,6 +678,7 @@ func (bf *BleFsm) Start() (bool, error) { } if err != nil { + bf.setState(SESN_STATE_UNCONNECTED) return false, err } @@ -691,6 +692,7 @@ func (bf *BleFsm) Start() (bool, error) { if err != nil { bhe := nmxutil.ToBleHost(err) retry := bhe != nil && bhe.Status == ERR_CODE_ENOTCONN + bf.setState(SESN_STATE_UNCONNECTED) return retry, err } @@ -702,6 +704,7 @@ func (bf *BleFsm) Start() (bool, error) { SESN_STATE_DISCOVERED_SVC, cb) if err != nil { + bf.setState(SESN_STATE_UNCONNECTED) return false, err } @@ -716,10 +719,12 @@ func (bf *BleFsm) Start() (bool, error) { SESN_STATE_DISCOVERED_CHR, cb) if err != nil { + bf.setState(SESN_STATE_UNCONNECTED) return false, err } if err := bf.subscribe(); err != nil { + bf.setState(SESN_STATE_UNCONNECTED) return false, err } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f573ad10/nmxact/nmble/ble_xport.go ---------------------------------------------------------------------- diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go index 970bb00..93fece1 100644 --- a/nmxact/nmble/ble_xport.go +++ b/nmxact/nmble/ble_xport.go @@ -42,9 +42,10 @@ type XportCfg struct { // Default: 1 second. BlehostdRspTimeout time.Duration - // Whether to restart the blehostd process if it terminates. + // Whether to restart the transport if it goes down or fails to start in + // the first place. // Default: true. - BlehostdRestart bool + Restart bool // How long to allow for the host and controller to sync at startup. // Default: 10 seconds. @@ -64,9 +65,9 @@ func NewXportCfg() XportCfg { return XportCfg{ BlehostdAcceptTimeout: time.Second, BlehostdRspTimeout: time.Second, - BlehostdRestart: true, + Restart: true, SyncTimeout: 10 * time.Second, - PreferredMtu: 512, + PreferredMtu: 264, } } @@ -212,18 +213,28 @@ func (bx *BleXport) initialSyncCheck() (bool, *BleListener, error) { } func (bx *BleXport) shutdown(restart bool, err error) { - var fullyStarted bool + bx.mtx.Lock() - if bx.setStateFrom(BLE_XPORT_STATE_STARTED, - BLE_XPORT_STATE_STOPPING) { + var fullyStarted bool + var already bool + switch bx.state { + case BLE_XPORT_STATE_STARTED: + already = false fullyStarted = true - } else if bx.setStateFrom(BLE_XPORT_STATE_STARTING, - BLE_XPORT_STATE_STOPPING) { - + bx.state = BLE_XPORT_STATE_STOPPING + case BLE_XPORT_STATE_STARTING: + already = false fullyStarted = false - } else { - // Stop already in progress. + bx.state = BLE_XPORT_STATE_STOPPING + default: + already = true + } + + bx.mtx.Unlock() + + if already { + // Shutdown already in progress. return } @@ -291,7 +302,7 @@ func (bx *BleXport) setStateFrom(from BleXportState, to BleXportState) bool { switch bx.state { case BLE_XPORT_STATE_STARTED: bx.notifyReadyListeners(nil) - case BLE_XPORT_STATE_STOPPED: + case BLE_XPORT_STATE_STOPPED, BLE_XPORT_STATE_DORMANT: bx.notifyReadyListeners(fmt.Errorf("BLE transport stopped")) default: } @@ -319,7 +330,6 @@ func (bx *BleXport) startOnce() error { "blehostd did not connect to socket; " + "controller not attached?") } else { - panic(err.Error()) err = nmxutil.NewXportError( "Failed to start child process: " + err.Error()) } @@ -462,7 +472,7 @@ func (bx *BleXport) Start() error { // If restarts are disabled, or if the shutdown was a result of an // explicit stop call (instead of an unexpected error), stop // restarting the transport. - if !bx.cfg.BlehostdRestart || !restart { + if !bx.cfg.Restart || !restart { bx.setStateFrom(BLE_XPORT_STATE_STOPPED, BLE_XPORT_STATE_DORMANT) break
