You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2017/04/05 01:44:30 UTC

[1/6] incubator-mynewt-newtmgr git commit: nmxact - Use uint32 for bhd sequence numbers.

Repository: incubator-mynewt-newtmgr
Updated Branches:
  refs/heads/master ad32cdd3d -> 3b818c385


nmxact - Use uint32 for bhd sequence numbers.


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/f1e662fa
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/f1e662fa
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/f1e662fa

Branch: refs/heads/master
Commit: f1e662fa78aedbe0746edc602248de47cc39e2a8
Parents: e3144c6
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Apr 4 15:18:24 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Apr 4 18:09:43 2017 -0700

----------------------------------------------------------------------
 nmxact/nmble/ble_fsm.go   |  8 ++---
 nmxact/nmble/ble_proto.go | 67 ++++++++++++++++++++++--------------------
 nmxact/nmble/ble_util.go  |  4 +--
 nmxact/nmble/ble_xport.go |  4 +--
 nmxact/nmble/dispatch.go  | 14 ++++-----
 5 files changed, 50 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f1e662fa/nmxact/nmble/ble_fsm.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_fsm.go b/nmxact/nmble/ble_fsm.go
index 39bfdfc..f9492c8 100644
--- a/nmxact/nmble/ble_fsm.go
+++ b/nmxact/nmble/ble_fsm.go
@@ -146,7 +146,7 @@ func (bf *BleFsm) addBleListener(base BleMsgBase) (*BleListener, error) {
 	return bl, nil
 }
 
-func (bf *BleFsm) addBleSeqListener(seq int) (*BleListener, error) {
+func (bf *BleFsm) addBleSeqListener(seq BleSeq) (*BleListener, error) {
 	base := BleMsgBase{
 		Op:         -1,
 		Type:       -1,
@@ -170,7 +170,7 @@ func (bf *BleFsm) removeBleListener(base BleMsgBase) {
 	}
 }
 
-func (bf *BleFsm) removeBleSeqListener(seq int) {
+func (bf *BleFsm) removeBleSeqListener(seq BleSeq) {
 	base := BleMsgBase{
 		Op:         -1,
 		Type:       -1,
@@ -240,7 +240,7 @@ func (bf *BleFsm) onDisconnect(err error) {
 	bf.params.DisconnectCb(dt, peer, err)
 }
 
-func (bf *BleFsm) connectListen(seq int) error {
+func (bf *BleFsm) connectListen(seq BleSeq) error {
 	bf.connChan = make(chan error, 1)
 
 	bl, err := bf.addBleSeqListener(seq)
@@ -326,7 +326,7 @@ func (bf *BleFsm) nmpRspListen() error {
 	base := BleMsgBase{
 		Op:         MSG_OP_EVT,
 		Type:       MSG_TYPE_NOTIFY_RX_EVT,
-		Seq:        -1,
+		Seq:        BLE_SEQ_NONE,
 		ConnHandle: bf.connHandle,
 	}
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f1e662fa/nmxact/nmble/ble_proto.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_proto.go b/nmxact/nmble/ble_proto.go
index fe9112d..8545335 100644
--- a/nmxact/nmble/ble_proto.go
+++ b/nmxact/nmble/ble_proto.go
@@ -13,6 +13,7 @@ import (
 
 type MsgOp int
 type MsgType int
+type BleSeq uint32
 
 type BleBytes struct {
 	Bytes []byte
@@ -22,6 +23,8 @@ type BleUuid struct {
 	Bytes [16]byte
 }
 
+const BLE_SEQ_NONE BleSeq = 0xffffffff
+
 const ERR_CODE_ATT_BASE = 0x100
 const ERR_CODE_HCI_BASE = 0x200
 const ERR_CODE_L2C_BASE = 0x300
@@ -274,7 +277,7 @@ var MsgTypeStringMap = map[MsgType]string{
 type BleHdr struct {
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleMsg interface{}
@@ -296,14 +299,14 @@ type BleSyncReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleConnectReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	OwnAddrType  BleAddrType `json:"own_addr_type"`
@@ -326,7 +329,7 @@ type BleConnectRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -336,7 +339,7 @@ type BleConnectEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status          int         `json:"status"`
@@ -355,7 +358,7 @@ type BleTerminateReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	ConnHandle int `json:"conn_handle"`
 	HciReason  int `json:"hci_reason"`
@@ -365,7 +368,7 @@ type BleTerminateRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -375,14 +378,14 @@ type BleConnCancelReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleConnCancelRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -392,7 +395,7 @@ type BleDisconnectEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Reason     int `json:"reason"`
@@ -403,7 +406,7 @@ type BleDiscSvcUuidReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int     `json:"conn_handle"`
@@ -414,7 +417,7 @@ type BleDiscSvcUuidRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -424,7 +427,7 @@ type BleDiscSvcEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int    `json:"status"`
@@ -435,7 +438,7 @@ type BleDiscChrUuidReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle  int     `json:"conn_handle"`
@@ -448,7 +451,7 @@ type BleDiscAllChrsReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle  int `json:"conn_handle"`
@@ -460,7 +463,7 @@ type BleDiscAllChrsRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -470,7 +473,7 @@ type BleErrRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int    `json:"status"`
@@ -481,7 +484,7 @@ type BleSyncRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Synced bool `json:"synced"`
@@ -491,7 +494,7 @@ type BleDiscChrUuidRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -501,7 +504,7 @@ type BleDiscChrEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int    `json:"status"`
@@ -512,7 +515,7 @@ type BleWriteCmdReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int      `json:"conn_handle"`
@@ -524,7 +527,7 @@ type BleWriteCmdRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -534,7 +537,7 @@ type BleSyncEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Synced bool `json:"synced"`
@@ -544,7 +547,7 @@ type BleNotifyRxEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int      `json:"conn_handle"`
@@ -557,7 +560,7 @@ type BleExchangeMtuReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int `json:"conn_handle"`
@@ -567,7 +570,7 @@ type BleExchangeMtuRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -577,7 +580,7 @@ type BleMtuChangeEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status     int `json:"status"`
@@ -589,7 +592,7 @@ type BleScanReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	OwnAddrType      BleAddrType         `json:"own_addr_type"`
@@ -606,7 +609,7 @@ type BleScanRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -616,7 +619,7 @@ type BleScanEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	EventType BleAdvEventType `json:"event_type"`
@@ -635,14 +638,14 @@ type BleScanCancelReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleScanCancelRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f1e662fa/nmxact/nmble/ble_util.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_util.go b/nmxact/nmble/ble_util.go
index 31f0e62..07cd8c3 100644
--- a/nmxact/nmble/ble_util.go
+++ b/nmxact/nmble/ble_util.go
@@ -22,8 +22,8 @@ const NOTIFY_CMD_BASE_SZ = 3
 
 var nextSeq uint32
 
-func NextSeq() int {
-	return int(atomic.AddUint32(&nextSeq, 1))
+func NextSeq() BleSeq {
+	return BleSeq(atomic.AddUint32(&nextSeq, 1))
 }
 
 func ParseUuid(uuidStr string) (BleUuid, error) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f1e662fa/nmxact/nmble/ble_xport.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go
index 335986f..8a8b909 100644
--- a/nmxact/nmble/ble_xport.go
+++ b/nmxact/nmble/ble_xport.go
@@ -113,7 +113,7 @@ func (bx *BleXport) addSyncListener() (*BleListener, error) {
 	base := BleMsgBase{
 		Op:         MSG_OP_EVT,
 		Type:       MSG_TYPE_SYNC_EVT,
-		Seq:        -1,
+		Seq:        BLE_SEQ_NONE,
 		ConnHandle: -1,
 	}
 	if err := bx.Bd.AddListener(base, bl); err != nil {
@@ -127,7 +127,7 @@ func (bx *BleXport) removeSyncListener() {
 	base := BleMsgBase{
 		Op:         MSG_OP_EVT,
 		Type:       MSG_TYPE_SYNC_EVT,
-		Seq:        -1,
+		Seq:        BLE_SEQ_NONE,
 		ConnHandle: -1,
 	}
 	bx.Bd.RemoveListener(base)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/f1e662fa/nmxact/nmble/dispatch.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/dispatch.go b/nmxact/nmble/dispatch.go
index c9ef39e..aba7504 100644
--- a/nmxact/nmble/dispatch.go
+++ b/nmxact/nmble/dispatch.go
@@ -37,7 +37,7 @@ type BleMsgBase struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Optional
 	ConnHandle int `json:"conn_handle" json:",omitempty"`
@@ -77,7 +77,7 @@ func (bl *BleListener) Stop() {
 }
 
 type BleDispatcher struct {
-	seqMap  map[int]*BleListener
+	seqMap  map[BleSeq]*BleListener
 	baseMap map[BleMsgBase]*BleListener
 	mutex   sync.Mutex
 }
@@ -132,7 +132,7 @@ var msgCtorMap = map[OpTypePair]msgCtor{
 
 func NewBleDispatcher() *BleDispatcher {
 	return &BleDispatcher{
-		seqMap:  map[int]*BleListener{},
+		seqMap:  map[BleSeq]*BleListener{},
 		baseMap: map[BleMsgBase]*BleListener{},
 	}
 }
@@ -162,7 +162,7 @@ func (bd *BleDispatcher) findBaseListener(base BleMsgBase) (
 func (bd *BleDispatcher) findDupListener(base BleMsgBase) (
 	BleMsgBase, *BleListener) {
 
-	if base.Seq != -1 {
+	if base.Seq != BLE_SEQ_NONE {
 		return base, bd.seqMap[base.Seq]
 	}
 
@@ -172,7 +172,7 @@ func (bd *BleDispatcher) findDupListener(base BleMsgBase) (
 func (bd *BleDispatcher) findListener(base BleMsgBase) (
 	BleMsgBase, *BleListener) {
 
-	if base.Seq != -1 {
+	if base.Seq != BLE_SEQ_NONE {
 		if bl := bd.seqMap[base.Seq]; bl != nil {
 			return base, bl
 		}
@@ -196,7 +196,7 @@ func (bd *BleDispatcher) AddListener(base BleMsgBase,
 			base.Op, base.Type, base.Seq, base.ConnHandle)
 	}
 
-	if base.Seq != -1 {
+	if base.Seq != BLE_SEQ_NONE {
 		if base.Op != -1 ||
 			base.Type != -1 ||
 			base.ConnHandle != -1 {
@@ -219,7 +219,7 @@ func (bd *BleDispatcher) RemoveListener(base BleMsgBase) *BleListener {
 	base, bl := bd.findListener(base)
 	if bl != nil {
 		bl.Stop()
-		if base.Seq != -1 {
+		if base.Seq != BLE_SEQ_NONE {
 			delete(bd.seqMap, base.Seq)
 		} else {
 			delete(bd.baseMap, base)


[5/6] incubator-mynewt-newtmgr git commit: nmxact - Add ble_loop example.

Posted by cc...@apache.org.
nmxact - Add ble_loop example.


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/2f603eac
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/2f603eac
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/2f603eac

Branch: refs/heads/master
Commit: 2f603eac7ab7ef428fe8b83a3050c6ef8007ef2c
Parents: 2d55058
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Apr 4 18:08:33 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Apr 4 18:10:25 2017 -0700

----------------------------------------------------------------------
 nmxact/build-examples.sh            |   1 +
 nmxact/example/ble_loop/ble_loop.go | 104 +++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/2f603eac/nmxact/build-examples.sh
----------------------------------------------------------------------
diff --git a/nmxact/build-examples.sh b/nmxact/build-examples.sh
index 7898948..a751f5f 100755
--- a/nmxact/build-examples.sh
+++ b/nmxact/build-examples.sh
@@ -1,6 +1,7 @@
 #!/bin/sh
 
 read -r -d '' exdirs <<-'EOS'
+    ble_loop
     ble_plain
     serial_plain
 EOS

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/2f603eac/nmxact/example/ble_loop/ble_loop.go
----------------------------------------------------------------------
diff --git a/nmxact/example/ble_loop/ble_loop.go b/nmxact/example/ble_loop/ble_loop.go
new file mode 100644
index 0000000..c76b11c
--- /dev/null
+++ b/nmxact/example/ble_loop/ble_loop.go
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package main
+
+import (
+	"fmt"
+	"os"
+
+	"mynewt.apache.org/newtmgr/nmxact/bledefs"
+	"mynewt.apache.org/newtmgr/nmxact/nmble"
+	"mynewt.apache.org/newtmgr/nmxact/sesn"
+	"mynewt.apache.org/newtmgr/nmxact/xact"
+)
+
+func main() {
+	// Initialize the BLE transport.
+	params := nmble.NewXportCfg()
+	params.SockPath = "/tmp/blehostd-uds"
+	params.BlehostdPath = "blehostd.elf"
+	params.DevPath = "/dev/cu.usbmodem142111"
+
+	x, err := nmble.NewBleXport(params)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "error creating BLE transport: %s\n",
+			err.Error())
+		os.Exit(1)
+	}
+
+	// Start the BLE transport.
+	if err := x.Start(); err != nil {
+		fmt.Fprintf(os.Stderr, "error starting BLE transport: %s\n",
+			err.Error())
+		os.Exit(1)
+	}
+	defer x.Stop()
+
+	// Prepare a BLE session:
+	//     * Plain NMP (not tunnelled over OIC).
+	//     * We use a random address.
+	//     * Peer has name "nimble-bleprph".
+	sc := sesn.NewSesnCfg()
+	sc.MgmtProto = sesn.MGMT_PROTO_NMP
+	sc.Ble.OwnAddrType = bledefs.BLE_ADDR_TYPE_RANDOM
+	sc.Ble.PeerSpec = sesn.BlePeerSpecName("nimble-bleprph")
+
+	s, err := x.BuildSesn(sc)
+	if err != nil {
+		fmt.Fprintf(os.Stderr, "error creating BLE session: %s\n", err.Error())
+		os.Exit(1)
+	}
+
+	// Repeatedly:
+	//     * Connect to peer if unconnected.
+	//     * Send an echo command to peer.
+	//
+	// If blehostd crashes or the controller is unplugged, nmxact should
+	// recover on the next connect attempt.
+	for {
+		if !s.IsOpen() {
+			// Connect to the peer (open the session).
+			if err := s.Open(); err != nil {
+				fmt.Fprintf(os.Stderr, "error starting BLE session: %s\n",
+					err.Error())
+				continue
+			}
+		}
+
+		// Send an echo command to the peer.
+		c := xact.NewEchoCmd()
+		c.Payload = "hello"
+
+		res, err := c.Run(s)
+		if err != nil {
+			fmt.Fprintf(os.Stderr, "error executing echo command: %s\n",
+				err.Error())
+			continue
+		}
+
+		if res.Status() != 0 {
+			fmt.Printf("Peer responded negatively to echo command; status=%d\n",
+				res.Status())
+		}
+
+		eres := res.(*xact.EchoResult)
+		fmt.Printf("Peer echoed back: %s\n", eres.Rsp.Payload)
+	}
+}


[2/6] incubator-mynewt-newtmgr git commit: nmxact - Remove unnecessary signal on blexport stop.

Posted by cc...@apache.org.
nmxact - Remove unnecessary signal on blexport stop.


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/e3144c66
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/e3144c66
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/e3144c66

Branch: refs/heads/master
Commit: e3144c662a9316fa99b569b7f5784ec67f883e0d
Parents: ad32cdd
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Apr 4 12:23:45 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Apr 4 18:09:43 2017 -0700

----------------------------------------------------------------------
 nmxact/nmble/ble_xport.go | 3 ---
 1 file changed, 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/e3144c66/nmxact/nmble/ble_xport.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go
index 02854c6..335986f 100644
--- a/nmxact/nmble/ble_xport.go
+++ b/nmxact/nmble/ble_xport.go
@@ -205,9 +205,6 @@ func (bx *BleXport) shutdown(restart bool, err error) {
 	// Stop the unixchild instance (blehostd + socket).
 	if bx.client != nil {
 		bx.client.Stop()
-
-		// Unblock the unixchild instance.
-		bx.client.FromChild <- nil
 	}
 
 	// Indicate an error to all of this transport's listeners.  This prevents


[4/6] incubator-mynewt-newtmgr git commit: nmxact - Fix hang on some ble_xport restarts.

Posted by cc...@apache.org.
nmxact - Fix hang on some ble_xport restarts.


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/2d55058f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/2d55058f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/2d55058f

Branch: refs/heads/master
Commit: 2d55058fe3ee691e69289c4d0eb8d4160feae800
Parents: 7dfbeb0
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Apr 4 18:05:59 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Apr 4 18:10:25 2017 -0700

----------------------------------------------------------------------
 nmxact/nmble/ble_xport.go | 2 +-
 nmxact/nmble/dispatch.go  | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/2d55058f/nmxact/nmble/ble_xport.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_xport.go b/nmxact/nmble/ble_xport.go
index 8a8b909..f957341 100644
--- a/nmxact/nmble/ble_xport.go
+++ b/nmxact/nmble/ble_xport.go
@@ -76,6 +76,7 @@ func NewBleXport(cfg XportCfg) (*BleXport, error) {
 	bx := &BleXport{
 		Bd:           NewBleDispatcher(),
 		shutdownChan: make(chan bool),
+		readyChan:    make(chan error),
 		cfg:          cfg,
 	}
 
@@ -286,7 +287,6 @@ func (bx *BleXport) startOnce() error {
 
 	bx.stopChan = make(chan struct{})
 	bx.numStopListeners = 0
-	bx.Bd.Clear()
 
 	bx.createUnixChild()
 	if err := bx.client.Start(); err != nil {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/2d55058f/nmxact/nmble/dispatch.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/dispatch.go b/nmxact/nmble/dispatch.go
index aba7504..583f3a4 100644
--- a/nmxact/nmble/dispatch.go
+++ b/nmxact/nmble/dispatch.go
@@ -292,6 +292,8 @@ func (bd *BleDispatcher) ErrorAll(err error) {
 		listeners = append(listeners, v)
 	}
 
+	bd.clear()
+
 	bd.mutex.Unlock()
 
 	for _, listener := range listeners {
@@ -299,10 +301,8 @@ func (bd *BleDispatcher) ErrorAll(err error) {
 	}
 }
 
-func (bd *BleDispatcher) Clear() {
-	bd.mutex.Lock()
-	defer bd.mutex.Unlock()
-
+// The caller must lock the mutex.
+func (bd *BleDispatcher) clear() {
 	for s, _ := range bd.seqMap {
 		delete(bd.seqMap, s)
 	}


[6/6] incubator-mynewt-newtmgr git commit: newtmgr - revendor

Posted by cc...@apache.org.
newtmgr - revendor


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/3b818c38
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/3b818c38
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/3b818c38

Branch: refs/heads/master
Commit: 3b818c3855ec1e29564e7c2070120db0798686d5
Parents: 2f603ea
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Apr 4 12:23:34 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Apr 4 18:10:25 2017 -0700

----------------------------------------------------------------------
 newtmgr/Godeps/Godeps.json                      | 36 +++++-----
 .../newtmgr/nmxact/nmble/ble_fsm.go             | 69 +++++++++++---------
 .../newtmgr/nmxact/nmble/ble_oic_sesn.go        | 29 +++++---
 .../newtmgr/nmxact/nmble/ble_plain_sesn.go      | 29 +++++---
 .../newtmgr/nmxact/nmble/ble_proto.go           | 67 ++++++++++---------
 .../newtmgr/nmxact/nmble/ble_util.go            |  4 +-
 .../newtmgr/nmxact/nmble/ble_xport.go           |  9 +--
 .../newtmgr/nmxact/nmble/dispatch.go            | 22 +++----
 8 files changed, 145 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/Godeps/Godeps.json
----------------------------------------------------------------------
diff --git a/newtmgr/Godeps/Godeps.json b/newtmgr/Godeps/Godeps.json
index dec9341..a1a8585 100644
--- a/newtmgr/Godeps/Godeps.json
+++ b/newtmgr/Godeps/Godeps.json
@@ -96,48 +96,48 @@
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/bledefs",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/nmble",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/nmp",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/nmserial",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/nmxutil",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/omp",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/sesn",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/xact",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		},
 		{
 			"ImportPath": "mynewt.apache.org/newtmgr/nmxact/xport",
-			"Comment": "mynewt_0_9_0_tag-456-gb056251",
-			"Rev": "b05625178026773b707953f73d54a1963d186959"
+			"Comment": "mynewt_0_9_0_tag-464-gf662168",
+			"Rev": "f662168755e02e16a9f0eab99683560707e9cf79"
 		}
 	]
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go
index 39bfdfc..dc94ca9 100644
--- a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_fsm.go
@@ -59,14 +59,13 @@ type BleFsmParams struct {
 type BleFsm struct {
 	params BleFsmParams
 
-	peerDev    *BleDev
-	connHandle int
-	nmpSvc     *BleSvc
-	nmpReqChr  *BleChr
-	nmpRspChr  *BleChr
-	attMtu     int
-	connChan   chan error
-
+	peerDev         *BleDev
+	connHandle      int
+	nmpSvc          *BleSvc
+	nmpReqChr       *BleChr
+	nmpRspChr       *BleChr
+	attMtu          int
+	connChan        chan error
 	mtx             sync.Mutex
 	lastStateChange time.Time
 
@@ -106,12 +105,16 @@ func (bf *BleFsm) getState() BleSesnState {
 	return bf.state
 }
 
+func (bf *BleFsm) setStateNoLock(toState BleSesnState) {
+	bf.state = toState
+	bf.lastStateChange = time.Now()
+}
+
 func (bf *BleFsm) setState(toState BleSesnState) {
 	bf.mtx.Lock()
 	defer bf.mtx.Unlock()
 
-	bf.state = toState
-	bf.lastStateChange = time.Now()
+	bf.setStateNoLock(toState)
 }
 
 func (bf *BleFsm) transitionState(fromState BleSesnState,
@@ -127,7 +130,7 @@ func (bf *BleFsm) transitionState(fromState BleSesnState,
 			toState, fromState)
 	}
 
-	bf.state = toState
+	bf.setStateNoLock(toState)
 	return nil
 }
 
@@ -135,18 +138,17 @@ func (bf *BleFsm) addBleListener(base BleMsgBase) (*BleListener, error) {
 	bl := NewBleListener()
 
 	bf.mtx.Lock()
-	bf.bls[bl] = struct{}{}
-	bf.mtx.Unlock()
+	defer bf.mtx.Unlock()
 
 	if err := bf.params.Bx.Bd.AddListener(base, bl); err != nil {
-		delete(bf.bls, bl)
 		return nil, err
 	}
 
+	bf.bls[bl] = struct{}{}
 	return bl, nil
 }
 
-func (bf *BleFsm) addBleSeqListener(seq int) (*BleListener, error) {
+func (bf *BleFsm) addBleSeqListener(seq BleSeq) (*BleListener, error) {
 	base := BleMsgBase{
 		Op:         -1,
 		Type:       -1,
@@ -162,15 +164,16 @@ func (bf *BleFsm) addBleSeqListener(seq int) (*BleListener, error) {
 }
 
 func (bf *BleFsm) removeBleListener(base BleMsgBase) {
+	bf.mtx.Lock()
+	defer bf.mtx.Unlock()
+
 	bl := bf.params.Bx.Bd.RemoveListener(base)
 	if bl != nil {
-		bf.mtx.Lock()
 		delete(bf.bls, bl)
-		bf.mtx.Unlock()
 	}
 }
 
-func (bf *BleFsm) removeBleSeqListener(seq int) {
+func (bf *BleFsm) removeBleSeqListener(seq BleSeq) {
 	base := BleMsgBase{
 		Op:         -1,
 		Type:       -1,
@@ -200,8 +203,8 @@ func (bf *BleFsm) action(
 	return nil
 }
 
-func (bf *BleFsm) calcDisconnectType() BleFsmDisconnectType {
-	switch bf.getState() {
+func calcDisconnectType(state BleSesnState) BleFsmDisconnectType {
+	switch state {
 	case SESN_STATE_EXCHANGING_MTU:
 		return FSM_DISCONNECT_TYPE_IMMEDIATE_TIMEOUT
 
@@ -220,19 +223,23 @@ func (bf *BleFsm) onDisconnect(err error) {
 	log.Debugf(err.Error())
 
 	bf.mtx.Lock()
-	bls := make([]*BleListener, 0, len(bf.bls))
-	for bl, _ := range bf.bls {
-		bls = append(bls, bl)
-	}
-	bf.mtx.Unlock()
 
 	// Remember some fields before we clear them.
-	dt := bf.calcDisconnectType()
+	dt := calcDisconnectType(bf.state)
 	peer := *bf.peerDev
 
-	bf.setState(SESN_STATE_UNCONNECTED)
+	bf.setStateNoLock(SESN_STATE_UNCONNECTED)
 	bf.peerDev = nil
 
+	// Make a copy of all the listeners so we don't have to keep the mutex
+	// locked while we send error signals to them.
+	bls := make([]*BleListener, 0, len(bf.bls))
+	for bl, _ := range bf.bls {
+		bls = append(bls, bl)
+	}
+
+	bf.mtx.Unlock()
+
 	for _, bl := range bls {
 		bl.ErrChan <- err
 	}
@@ -240,7 +247,7 @@ func (bf *BleFsm) onDisconnect(err error) {
 	bf.params.DisconnectCb(dt, peer, err)
 }
 
-func (bf *BleFsm) connectListen(seq int) error {
+func (bf *BleFsm) connectListen(seq BleSeq) error {
 	bf.connChan = make(chan error, 1)
 
 	bl, err := bf.addBleSeqListener(seq)
@@ -253,7 +260,7 @@ func (bf *BleFsm) connectListen(seq int) error {
 		for {
 			select {
 			case err := <-bl.ErrChan:
-				// Transport reported error.  Assume all connections have
+				// Transport reported error.  Assume the connection has
 				// dropped.
 				bf.onDisconnect(err)
 				return
@@ -326,7 +333,7 @@ func (bf *BleFsm) nmpRspListen() error {
 	base := BleMsgBase{
 		Op:         MSG_OP_EVT,
 		Type:       MSG_TYPE_NOTIFY_RX_EVT,
-		Seq:        -1,
+		Seq:        BLE_SEQ_NONE,
 		ConnHandle: bf.connHandle,
 	}
 
@@ -453,7 +460,7 @@ func (bf *BleFsm) terminateSetState() error {
 		return fmt.Errorf(
 			"BLE terminate failed; session already being closed")
 	default:
-		bf.state = SESN_STATE_TERMINATING
+		bf.setStateNoLock(SESN_STATE_TERMINATING)
 	}
 
 	return nil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go
index 5fa9291..e5139be 100644
--- a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_oic_sesn.go
@@ -23,7 +23,7 @@ type BleOicSesn struct {
 	onCloseCb    sesn.BleOnCloseFn
 
 	closeChan chan error
-	mx        sync.Mutex
+	mtx       sync.Mutex
 }
 
 func NewBleOicSesn(bx *BleXport, cfg sesn.SesnCfg) *BleOicSesn {
@@ -67,18 +67,22 @@ func NewBleOicSesn(bx *BleXport, cfg sesn.SesnCfg) *BleOicSesn {
 }
 
 func (bos *BleOicSesn) addNmpListener(seq uint8) (*nmp.NmpListener, error) {
-	nl := nmp.NewNmpListener()
-	bos.nls[nl] = struct{}{}
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
 
+	nl := nmp.NewNmpListener()
 	if err := bos.od.AddListener(seq, nl); err != nil {
-		delete(bos.nls, nl)
 		return nil, err
 	}
 
+	bos.nls[nl] = struct{}{}
 	return nl, nil
 }
 
 func (bos *BleOicSesn) removeNmpListener(seq uint8) {
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
+
 	listener := bos.od.RemoveListener(seq)
 	if listener != nil {
 		delete(bos.nls, listener)
@@ -87,8 +91,8 @@ func (bos *BleOicSesn) removeNmpListener(seq uint8) {
 
 // Returns true if a new channel was assigned.
 func (bos *BleOicSesn) setCloseChan() error {
-	bos.mx.Lock()
-	defer bos.mx.Unlock()
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
 
 	if bos.closeChan != nil {
 		return fmt.Errorf("Multiple listeners waiting for session to close")
@@ -99,8 +103,8 @@ func (bos *BleOicSesn) setCloseChan() error {
 }
 
 func (bos *BleOicSesn) clearCloseChan() {
-	bos.mx.Lock()
-	defer bos.mx.Unlock()
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
 
 	bos.closeChan = nil
 }
@@ -126,7 +130,7 @@ func (bos *BleOicSesn) blockUntilClosed(timeout time.Duration) error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bos.listenForClose(timeout)
 }
 
@@ -173,7 +177,7 @@ func (bos *BleOicSesn) Close() error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bos.listenForClose(bos.closeTimeout)
 }
 
@@ -185,9 +189,12 @@ func (bos *BleOicSesn) onRxNmp(data []byte) {
 	bos.od.Dispatch(data)
 }
 
+// Called by the FSM when a blehostd disconnect event is received.
 func (bos *BleOicSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 	err error) {
 
+	bos.mtx.Lock()
+
 	for nl, _ := range bos.nls {
 		nl.ErrChan <- err
 	}
@@ -197,6 +204,8 @@ func (bos *BleOicSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 		bos.closeChan <- err
 	}
 
+	bos.mtx.Unlock()
+
 	// Only execute client's disconnect callback if the disconnect was
 	// unsolicited and the session was fully open.
 	if dt == FSM_DISCONNECT_TYPE_OPENED && bos.onCloseCb != nil {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_plain_sesn.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_plain_sesn.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_plain_sesn.go
index 15419a9..803eb45 100644
--- a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_plain_sesn.go
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_plain_sesn.go
@@ -22,7 +22,7 @@ type BlePlainSesn struct {
 	onCloseCb    sesn.BleOnCloseFn
 
 	closeChan chan error
-	mx        sync.Mutex
+	mtx       sync.Mutex
 }
 
 func NewBlePlainSesn(bx *BleXport, cfg sesn.SesnCfg) *BlePlainSesn {
@@ -61,18 +61,22 @@ func NewBlePlainSesn(bx *BleXport, cfg sesn.SesnCfg) *BlePlainSesn {
 }
 
 func (bps *BlePlainSesn) addNmpListener(seq uint8) (*nmp.NmpListener, error) {
-	nl := nmp.NewNmpListener()
-	bps.nls[nl] = struct{}{}
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
 
+	nl := nmp.NewNmpListener()
 	if err := bps.nd.AddListener(seq, nl); err != nil {
-		delete(bps.nls, nl)
 		return nil, err
 	}
 
+	bps.nls[nl] = struct{}{}
 	return nl, nil
 }
 
 func (bps *BlePlainSesn) removeNmpListener(seq uint8) {
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
+
 	listener := bps.nd.RemoveListener(seq)
 	if listener != nil {
 		delete(bps.nls, listener)
@@ -80,8 +84,8 @@ func (bps *BlePlainSesn) removeNmpListener(seq uint8) {
 }
 
 func (bps *BlePlainSesn) setCloseChan() error {
-	bps.mx.Lock()
-	defer bps.mx.Unlock()
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
 
 	if bps.closeChan != nil {
 		return fmt.Errorf("Multiple listeners waiting for session to close")
@@ -92,8 +96,8 @@ func (bps *BlePlainSesn) setCloseChan() error {
 }
 
 func (bps *BlePlainSesn) clearCloseChan() {
-	bps.mx.Lock()
-	defer bps.mx.Unlock()
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
 
 	bps.closeChan = nil
 }
@@ -119,7 +123,7 @@ func (bps *BlePlainSesn) blockUntilClosed(timeout time.Duration) error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bps.listenForClose(timeout)
 }
 
@@ -166,7 +170,7 @@ func (bps *BlePlainSesn) Close() error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bps.listenForClose(bps.closeTimeout)
 }
 
@@ -178,9 +182,12 @@ func (bps *BlePlainSesn) onRxNmp(data []byte) {
 	bps.nd.Dispatch(data)
 }
 
+// Called by the FSM when a blehostd disconnect event is received.
 func (bps *BlePlainSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 	err error) {
 
+	bps.mtx.Lock()
+
 	for nl, _ := range bps.nls {
 		nl.ErrChan <- err
 	}
@@ -190,6 +197,8 @@ func (bps *BlePlainSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 		bps.closeChan <- err
 	}
 
+	bps.mtx.Unlock()
+
 	// Only execute client's disconnect callback if the disconnect was
 	// unsolicited and the session was fully open.
 	if dt == FSM_DISCONNECT_TYPE_OPENED && bps.onCloseCb != nil {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_proto.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_proto.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_proto.go
index fe9112d..8545335 100644
--- a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_proto.go
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_proto.go
@@ -13,6 +13,7 @@ import (
 
 type MsgOp int
 type MsgType int
+type BleSeq uint32
 
 type BleBytes struct {
 	Bytes []byte
@@ -22,6 +23,8 @@ type BleUuid struct {
 	Bytes [16]byte
 }
 
+const BLE_SEQ_NONE BleSeq = 0xffffffff
+
 const ERR_CODE_ATT_BASE = 0x100
 const ERR_CODE_HCI_BASE = 0x200
 const ERR_CODE_L2C_BASE = 0x300
@@ -274,7 +277,7 @@ var MsgTypeStringMap = map[MsgType]string{
 type BleHdr struct {
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleMsg interface{}
@@ -296,14 +299,14 @@ type BleSyncReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleConnectReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	OwnAddrType  BleAddrType `json:"own_addr_type"`
@@ -326,7 +329,7 @@ type BleConnectRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -336,7 +339,7 @@ type BleConnectEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status          int         `json:"status"`
@@ -355,7 +358,7 @@ type BleTerminateReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	ConnHandle int `json:"conn_handle"`
 	HciReason  int `json:"hci_reason"`
@@ -365,7 +368,7 @@ type BleTerminateRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -375,14 +378,14 @@ type BleConnCancelReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleConnCancelRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -392,7 +395,7 @@ type BleDisconnectEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Reason     int `json:"reason"`
@@ -403,7 +406,7 @@ type BleDiscSvcUuidReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int     `json:"conn_handle"`
@@ -414,7 +417,7 @@ type BleDiscSvcUuidRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -424,7 +427,7 @@ type BleDiscSvcEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int    `json:"status"`
@@ -435,7 +438,7 @@ type BleDiscChrUuidReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle  int     `json:"conn_handle"`
@@ -448,7 +451,7 @@ type BleDiscAllChrsReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle  int `json:"conn_handle"`
@@ -460,7 +463,7 @@ type BleDiscAllChrsRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -470,7 +473,7 @@ type BleErrRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int    `json:"status"`
@@ -481,7 +484,7 @@ type BleSyncRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Synced bool `json:"synced"`
@@ -491,7 +494,7 @@ type BleDiscChrUuidRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -501,7 +504,7 @@ type BleDiscChrEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int    `json:"status"`
@@ -512,7 +515,7 @@ type BleWriteCmdReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int      `json:"conn_handle"`
@@ -524,7 +527,7 @@ type BleWriteCmdRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -534,7 +537,7 @@ type BleSyncEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Synced bool `json:"synced"`
@@ -544,7 +547,7 @@ type BleNotifyRxEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int      `json:"conn_handle"`
@@ -557,7 +560,7 @@ type BleExchangeMtuReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	ConnHandle int `json:"conn_handle"`
@@ -567,7 +570,7 @@ type BleExchangeMtuRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -577,7 +580,7 @@ type BleMtuChangeEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status     int `json:"status"`
@@ -589,7 +592,7 @@ type BleScanReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	OwnAddrType      BleAddrType         `json:"own_addr_type"`
@@ -606,7 +609,7 @@ type BleScanRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`
@@ -616,7 +619,7 @@ type BleScanEvt struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	EventType BleAdvEventType `json:"event_type"`
@@ -635,14 +638,14 @@ type BleScanCancelReq struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 }
 
 type BleScanCancelRsp struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Mandatory
 	Status int `json:"status"`

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_util.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_util.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_util.go
index 31f0e62..07cd8c3 100644
--- a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_util.go
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_util.go
@@ -22,8 +22,8 @@ const NOTIFY_CMD_BASE_SZ = 3
 
 var nextSeq uint32
 
-func NextSeq() int {
-	return int(atomic.AddUint32(&nextSeq, 1))
+func NextSeq() BleSeq {
+	return BleSeq(atomic.AddUint32(&nextSeq, 1))
 }
 
 func ParseUuid(uuidStr string) (BleUuid, error) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_xport.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_xport.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_xport.go
index 02854c6..f957341 100644
--- a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_xport.go
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/ble_xport.go
@@ -76,6 +76,7 @@ func NewBleXport(cfg XportCfg) (*BleXport, error) {
 	bx := &BleXport{
 		Bd:           NewBleDispatcher(),
 		shutdownChan: make(chan bool),
+		readyChan:    make(chan error),
 		cfg:          cfg,
 	}
 
@@ -113,7 +114,7 @@ func (bx *BleXport) addSyncListener() (*BleListener, error) {
 	base := BleMsgBase{
 		Op:         MSG_OP_EVT,
 		Type:       MSG_TYPE_SYNC_EVT,
-		Seq:        -1,
+		Seq:        BLE_SEQ_NONE,
 		ConnHandle: -1,
 	}
 	if err := bx.Bd.AddListener(base, bl); err != nil {
@@ -127,7 +128,7 @@ func (bx *BleXport) removeSyncListener() {
 	base := BleMsgBase{
 		Op:         MSG_OP_EVT,
 		Type:       MSG_TYPE_SYNC_EVT,
-		Seq:        -1,
+		Seq:        BLE_SEQ_NONE,
 		ConnHandle: -1,
 	}
 	bx.Bd.RemoveListener(base)
@@ -205,9 +206,6 @@ func (bx *BleXport) shutdown(restart bool, err error) {
 	// Stop the unixchild instance (blehostd + socket).
 	if bx.client != nil {
 		bx.client.Stop()
-
-		// Unblock the unixchild instance.
-		bx.client.FromChild <- nil
 	}
 
 	// Indicate an error to all of this transport's listeners.  This prevents
@@ -289,7 +287,6 @@ func (bx *BleXport) startOnce() error {
 
 	bx.stopChan = make(chan struct{})
 	bx.numStopListeners = 0
-	bx.Bd.Clear()
 
 	bx.createUnixChild()
 	if err := bx.client.Start(); err != nil {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/3b818c38/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/dispatch.go
----------------------------------------------------------------------
diff --git a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/dispatch.go b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/dispatch.go
index c9ef39e..583f3a4 100644
--- a/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/dispatch.go
+++ b/newtmgr/vendor/mynewt.apache.org/newtmgr/nmxact/nmble/dispatch.go
@@ -37,7 +37,7 @@ type BleMsgBase struct {
 	// Header
 	Op   MsgOp   `json:"op"`
 	Type MsgType `json:"type"`
-	Seq  int     `json:"seq"`
+	Seq  BleSeq  `json:"seq"`
 
 	// Optional
 	ConnHandle int `json:"conn_handle" json:",omitempty"`
@@ -77,7 +77,7 @@ func (bl *BleListener) Stop() {
 }
 
 type BleDispatcher struct {
-	seqMap  map[int]*BleListener
+	seqMap  map[BleSeq]*BleListener
 	baseMap map[BleMsgBase]*BleListener
 	mutex   sync.Mutex
 }
@@ -132,7 +132,7 @@ var msgCtorMap = map[OpTypePair]msgCtor{
 
 func NewBleDispatcher() *BleDispatcher {
 	return &BleDispatcher{
-		seqMap:  map[int]*BleListener{},
+		seqMap:  map[BleSeq]*BleListener{},
 		baseMap: map[BleMsgBase]*BleListener{},
 	}
 }
@@ -162,7 +162,7 @@ func (bd *BleDispatcher) findBaseListener(base BleMsgBase) (
 func (bd *BleDispatcher) findDupListener(base BleMsgBase) (
 	BleMsgBase, *BleListener) {
 
-	if base.Seq != -1 {
+	if base.Seq != BLE_SEQ_NONE {
 		return base, bd.seqMap[base.Seq]
 	}
 
@@ -172,7 +172,7 @@ func (bd *BleDispatcher) findDupListener(base BleMsgBase) (
 func (bd *BleDispatcher) findListener(base BleMsgBase) (
 	BleMsgBase, *BleListener) {
 
-	if base.Seq != -1 {
+	if base.Seq != BLE_SEQ_NONE {
 		if bl := bd.seqMap[base.Seq]; bl != nil {
 			return base, bl
 		}
@@ -196,7 +196,7 @@ func (bd *BleDispatcher) AddListener(base BleMsgBase,
 			base.Op, base.Type, base.Seq, base.ConnHandle)
 	}
 
-	if base.Seq != -1 {
+	if base.Seq != BLE_SEQ_NONE {
 		if base.Op != -1 ||
 			base.Type != -1 ||
 			base.ConnHandle != -1 {
@@ -219,7 +219,7 @@ func (bd *BleDispatcher) RemoveListener(base BleMsgBase) *BleListener {
 	base, bl := bd.findListener(base)
 	if bl != nil {
 		bl.Stop()
-		if base.Seq != -1 {
+		if base.Seq != BLE_SEQ_NONE {
 			delete(bd.seqMap, base.Seq)
 		} else {
 			delete(bd.baseMap, base)
@@ -292,6 +292,8 @@ func (bd *BleDispatcher) ErrorAll(err error) {
 		listeners = append(listeners, v)
 	}
 
+	bd.clear()
+
 	bd.mutex.Unlock()
 
 	for _, listener := range listeners {
@@ -299,10 +301,8 @@ func (bd *BleDispatcher) ErrorAll(err error) {
 	}
 }
 
-func (bd *BleDispatcher) Clear() {
-	bd.mutex.Lock()
-	defer bd.mutex.Unlock()
-
+// The caller must lock the mutex.
+func (bd *BleDispatcher) clear() {
 	for s, _ := range bd.seqMap {
 		delete(bd.seqMap, s)
 	}


[3/6] incubator-mynewt-newtmgr git commit: nmxact - Clean up BLE session code.

Posted by cc...@apache.org.
nmxact - Clean up BLE session code.


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/7dfbeb0d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/tree/7dfbeb0d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/diff/7dfbeb0d

Branch: refs/heads/master
Commit: 7dfbeb0db8c16148290522e862fd9630dca25e5c
Parents: f1e662f
Author: Christopher Collins <cc...@apache.org>
Authored: Tue Apr 4 15:07:18 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Apr 4 18:10:19 2017 -0700

----------------------------------------------------------------------
 nmxact/nmble/ble_fsm.go        | 61 +++++++++++++++++++++----------------
 nmxact/nmble/ble_oic_sesn.go   | 29 ++++++++++++------
 nmxact/nmble/ble_plain_sesn.go | 29 ++++++++++++------
 3 files changed, 72 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/7dfbeb0d/nmxact/nmble/ble_fsm.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_fsm.go b/nmxact/nmble/ble_fsm.go
index f9492c8..dc94ca9 100644
--- a/nmxact/nmble/ble_fsm.go
+++ b/nmxact/nmble/ble_fsm.go
@@ -59,14 +59,13 @@ type BleFsmParams struct {
 type BleFsm struct {
 	params BleFsmParams
 
-	peerDev    *BleDev
-	connHandle int
-	nmpSvc     *BleSvc
-	nmpReqChr  *BleChr
-	nmpRspChr  *BleChr
-	attMtu     int
-	connChan   chan error
-
+	peerDev         *BleDev
+	connHandle      int
+	nmpSvc          *BleSvc
+	nmpReqChr       *BleChr
+	nmpRspChr       *BleChr
+	attMtu          int
+	connChan        chan error
 	mtx             sync.Mutex
 	lastStateChange time.Time
 
@@ -106,12 +105,16 @@ func (bf *BleFsm) getState() BleSesnState {
 	return bf.state
 }
 
+func (bf *BleFsm) setStateNoLock(toState BleSesnState) {
+	bf.state = toState
+	bf.lastStateChange = time.Now()
+}
+
 func (bf *BleFsm) setState(toState BleSesnState) {
 	bf.mtx.Lock()
 	defer bf.mtx.Unlock()
 
-	bf.state = toState
-	bf.lastStateChange = time.Now()
+	bf.setStateNoLock(toState)
 }
 
 func (bf *BleFsm) transitionState(fromState BleSesnState,
@@ -127,7 +130,7 @@ func (bf *BleFsm) transitionState(fromState BleSesnState,
 			toState, fromState)
 	}
 
-	bf.state = toState
+	bf.setStateNoLock(toState)
 	return nil
 }
 
@@ -135,14 +138,13 @@ func (bf *BleFsm) addBleListener(base BleMsgBase) (*BleListener, error) {
 	bl := NewBleListener()
 
 	bf.mtx.Lock()
-	bf.bls[bl] = struct{}{}
-	bf.mtx.Unlock()
+	defer bf.mtx.Unlock()
 
 	if err := bf.params.Bx.Bd.AddListener(base, bl); err != nil {
-		delete(bf.bls, bl)
 		return nil, err
 	}
 
+	bf.bls[bl] = struct{}{}
 	return bl, nil
 }
 
@@ -162,11 +164,12 @@ func (bf *BleFsm) addBleSeqListener(seq BleSeq) (*BleListener, error) {
 }
 
 func (bf *BleFsm) removeBleListener(base BleMsgBase) {
+	bf.mtx.Lock()
+	defer bf.mtx.Unlock()
+
 	bl := bf.params.Bx.Bd.RemoveListener(base)
 	if bl != nil {
-		bf.mtx.Lock()
 		delete(bf.bls, bl)
-		bf.mtx.Unlock()
 	}
 }
 
@@ -200,8 +203,8 @@ func (bf *BleFsm) action(
 	return nil
 }
 
-func (bf *BleFsm) calcDisconnectType() BleFsmDisconnectType {
-	switch bf.getState() {
+func calcDisconnectType(state BleSesnState) BleFsmDisconnectType {
+	switch state {
 	case SESN_STATE_EXCHANGING_MTU:
 		return FSM_DISCONNECT_TYPE_IMMEDIATE_TIMEOUT
 
@@ -220,19 +223,23 @@ func (bf *BleFsm) onDisconnect(err error) {
 	log.Debugf(err.Error())
 
 	bf.mtx.Lock()
-	bls := make([]*BleListener, 0, len(bf.bls))
-	for bl, _ := range bf.bls {
-		bls = append(bls, bl)
-	}
-	bf.mtx.Unlock()
 
 	// Remember some fields before we clear them.
-	dt := bf.calcDisconnectType()
+	dt := calcDisconnectType(bf.state)
 	peer := *bf.peerDev
 
-	bf.setState(SESN_STATE_UNCONNECTED)
+	bf.setStateNoLock(SESN_STATE_UNCONNECTED)
 	bf.peerDev = nil
 
+	// Make a copy of all the listeners so we don't have to keep the mutex
+	// locked while we send error signals to them.
+	bls := make([]*BleListener, 0, len(bf.bls))
+	for bl, _ := range bf.bls {
+		bls = append(bls, bl)
+	}
+
+	bf.mtx.Unlock()
+
 	for _, bl := range bls {
 		bl.ErrChan <- err
 	}
@@ -253,7 +260,7 @@ func (bf *BleFsm) connectListen(seq BleSeq) error {
 		for {
 			select {
 			case err := <-bl.ErrChan:
-				// Transport reported error.  Assume all connections have
+				// Transport reported error.  Assume the connection has
 				// dropped.
 				bf.onDisconnect(err)
 				return
@@ -453,7 +460,7 @@ func (bf *BleFsm) terminateSetState() error {
 		return fmt.Errorf(
 			"BLE terminate failed; session already being closed")
 	default:
-		bf.state = SESN_STATE_TERMINATING
+		bf.setStateNoLock(SESN_STATE_TERMINATING)
 	}
 
 	return nil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/7dfbeb0d/nmxact/nmble/ble_oic_sesn.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_oic_sesn.go b/nmxact/nmble/ble_oic_sesn.go
index 5fa9291..e5139be 100644
--- a/nmxact/nmble/ble_oic_sesn.go
+++ b/nmxact/nmble/ble_oic_sesn.go
@@ -23,7 +23,7 @@ type BleOicSesn struct {
 	onCloseCb    sesn.BleOnCloseFn
 
 	closeChan chan error
-	mx        sync.Mutex
+	mtx       sync.Mutex
 }
 
 func NewBleOicSesn(bx *BleXport, cfg sesn.SesnCfg) *BleOicSesn {
@@ -67,18 +67,22 @@ func NewBleOicSesn(bx *BleXport, cfg sesn.SesnCfg) *BleOicSesn {
 }
 
 func (bos *BleOicSesn) addNmpListener(seq uint8) (*nmp.NmpListener, error) {
-	nl := nmp.NewNmpListener()
-	bos.nls[nl] = struct{}{}
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
 
+	nl := nmp.NewNmpListener()
 	if err := bos.od.AddListener(seq, nl); err != nil {
-		delete(bos.nls, nl)
 		return nil, err
 	}
 
+	bos.nls[nl] = struct{}{}
 	return nl, nil
 }
 
 func (bos *BleOicSesn) removeNmpListener(seq uint8) {
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
+
 	listener := bos.od.RemoveListener(seq)
 	if listener != nil {
 		delete(bos.nls, listener)
@@ -87,8 +91,8 @@ func (bos *BleOicSesn) removeNmpListener(seq uint8) {
 
 // Returns true if a new channel was assigned.
 func (bos *BleOicSesn) setCloseChan() error {
-	bos.mx.Lock()
-	defer bos.mx.Unlock()
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
 
 	if bos.closeChan != nil {
 		return fmt.Errorf("Multiple listeners waiting for session to close")
@@ -99,8 +103,8 @@ func (bos *BleOicSesn) setCloseChan() error {
 }
 
 func (bos *BleOicSesn) clearCloseChan() {
-	bos.mx.Lock()
-	defer bos.mx.Unlock()
+	bos.mtx.Lock()
+	defer bos.mtx.Unlock()
 
 	bos.closeChan = nil
 }
@@ -126,7 +130,7 @@ func (bos *BleOicSesn) blockUntilClosed(timeout time.Duration) error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bos.listenForClose(timeout)
 }
 
@@ -173,7 +177,7 @@ func (bos *BleOicSesn) Close() error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bos.listenForClose(bos.closeTimeout)
 }
 
@@ -185,9 +189,12 @@ func (bos *BleOicSesn) onRxNmp(data []byte) {
 	bos.od.Dispatch(data)
 }
 
+// Called by the FSM when a blehostd disconnect event is received.
 func (bos *BleOicSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 	err error) {
 
+	bos.mtx.Lock()
+
 	for nl, _ := range bos.nls {
 		nl.ErrChan <- err
 	}
@@ -197,6 +204,8 @@ func (bos *BleOicSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 		bos.closeChan <- err
 	}
 
+	bos.mtx.Unlock()
+
 	// Only execute client's disconnect callback if the disconnect was
 	// unsolicited and the session was fully open.
 	if dt == FSM_DISCONNECT_TYPE_OPENED && bos.onCloseCb != nil {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/7dfbeb0d/nmxact/nmble/ble_plain_sesn.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_plain_sesn.go b/nmxact/nmble/ble_plain_sesn.go
index 15419a9..803eb45 100644
--- a/nmxact/nmble/ble_plain_sesn.go
+++ b/nmxact/nmble/ble_plain_sesn.go
@@ -22,7 +22,7 @@ type BlePlainSesn struct {
 	onCloseCb    sesn.BleOnCloseFn
 
 	closeChan chan error
-	mx        sync.Mutex
+	mtx       sync.Mutex
 }
 
 func NewBlePlainSesn(bx *BleXport, cfg sesn.SesnCfg) *BlePlainSesn {
@@ -61,18 +61,22 @@ func NewBlePlainSesn(bx *BleXport, cfg sesn.SesnCfg) *BlePlainSesn {
 }
 
 func (bps *BlePlainSesn) addNmpListener(seq uint8) (*nmp.NmpListener, error) {
-	nl := nmp.NewNmpListener()
-	bps.nls[nl] = struct{}{}
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
 
+	nl := nmp.NewNmpListener()
 	if err := bps.nd.AddListener(seq, nl); err != nil {
-		delete(bps.nls, nl)
 		return nil, err
 	}
 
+	bps.nls[nl] = struct{}{}
 	return nl, nil
 }
 
 func (bps *BlePlainSesn) removeNmpListener(seq uint8) {
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
+
 	listener := bps.nd.RemoveListener(seq)
 	if listener != nil {
 		delete(bps.nls, listener)
@@ -80,8 +84,8 @@ func (bps *BlePlainSesn) removeNmpListener(seq uint8) {
 }
 
 func (bps *BlePlainSesn) setCloseChan() error {
-	bps.mx.Lock()
-	defer bps.mx.Unlock()
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
 
 	if bps.closeChan != nil {
 		return fmt.Errorf("Multiple listeners waiting for session to close")
@@ -92,8 +96,8 @@ func (bps *BlePlainSesn) setCloseChan() error {
 }
 
 func (bps *BlePlainSesn) clearCloseChan() {
-	bps.mx.Lock()
-	defer bps.mx.Unlock()
+	bps.mtx.Lock()
+	defer bps.mtx.Unlock()
 
 	bps.closeChan = nil
 }
@@ -119,7 +123,7 @@ func (bps *BlePlainSesn) blockUntilClosed(timeout time.Duration) error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bps.listenForClose(timeout)
 }
 
@@ -166,7 +170,7 @@ func (bps *BlePlainSesn) Close() error {
 		return nil
 	}
 
-	// Block until close completes or timeout.
+	// Block until close completes or times out.
 	return bps.listenForClose(bps.closeTimeout)
 }
 
@@ -178,9 +182,12 @@ func (bps *BlePlainSesn) onRxNmp(data []byte) {
 	bps.nd.Dispatch(data)
 }
 
+// Called by the FSM when a blehostd disconnect event is received.
 func (bps *BlePlainSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 	err error) {
 
+	bps.mtx.Lock()
+
 	for nl, _ := range bps.nls {
 		nl.ErrChan <- err
 	}
@@ -190,6 +197,8 @@ func (bps *BlePlainSesn) onDisconnect(dt BleFsmDisconnectType, peer BleDev,
 		bps.closeChan <- err
 	}
 
+	bps.mtx.Unlock()
+
 	// Only execute client's disconnect callback if the disconnect was
 	// unsolicited and the session was fully open.
 	if dt == FSM_DISCONNECT_TYPE_OPENED && bps.onCloseCb != nil {