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/07 17:15:16 UTC

[1/2] incubator-mynewt-newtmgr git commit: nmxact - Add missing BLE advertisement fields.

Repository: incubator-mynewt-newtmgr
Updated Branches:
  refs/heads/master d42718972 -> f573ad10e


nmxact - Add missing BLE advertisement fields.


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

Branch: refs/heads/master
Commit: 25097817c6c8b1b2ce904082ddf4a25aa0ee40d7
Parents: d427189
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Apr 6 12:37:53 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Fri Apr 7 10:09:56 2017 -0700

----------------------------------------------------------------------
 nmxact/bledefs/bledefs.go | 102 +++++++++++++++++++++++++++++++++++++++--
 nmxact/nmble/ble_proto.go |  72 ++++++++++-------------------
 nmxact/nmble/ble_util.go  |  58 ++++++++++-------------
 3 files changed, 147 insertions(+), 85 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/25097817/nmxact/bledefs/bledefs.go
----------------------------------------------------------------------
diff --git a/nmxact/bledefs/bledefs.go b/nmxact/bledefs/bledefs.go
index b8d558c..a64da61 100644
--- a/nmxact/bledefs/bledefs.go
+++ b/nmxact/bledefs/bledefs.go
@@ -147,6 +147,81 @@ func (bd *BleDev) String() string {
 		bd.Addr.String())
 }
 
+type BleUuid struct {
+	Bytes [16]byte
+}
+
+func (bu *BleUuid) String() string {
+	var buf bytes.Buffer
+	buf.Grow(len(bu.Bytes)*2 + 3)
+
+	// XXX: For now, only support 128-bit UUIDs.
+
+	for i, b := range bu.Bytes {
+		switch i {
+		case 4, 6, 8, 10:
+			buf.WriteString("-")
+		}
+
+		fmt.Fprintf(&buf, "%02x", b)
+	}
+
+	return buf.String()
+}
+
+func ParseUuid(uuidStr string) (BleUuid, error) {
+	bu := BleUuid{}
+
+	if len(uuidStr) != 36 {
+		return bu, fmt.Errorf("Invalid UUID: %s", uuidStr)
+	}
+
+	boff := 0
+	for i := 0; i < 36; {
+		switch i {
+		case 8, 13, 18, 23:
+			if uuidStr[i] != '-' {
+				return bu, fmt.Errorf("Invalid UUID: %s", uuidStr)
+			}
+			i++
+
+		default:
+			u64, err := strconv.ParseUint(uuidStr[i:i+2], 16, 8)
+			if err != nil {
+				return bu, fmt.Errorf("Invalid UUID: %s", uuidStr)
+			}
+			bu.Bytes[boff] = byte(u64)
+			i += 2
+			boff++
+		}
+	}
+
+	return bu, nil
+}
+
+func (bu *BleUuid) MarshalJSON() ([]byte, error) {
+	return json.Marshal(bu.String())
+}
+
+func (bu *BleUuid) UnmarshalJSON(data []byte) error {
+	var s string
+	if err := json.Unmarshal(data, &s); err != nil {
+		return err
+	}
+
+	var err error
+	*bu, err = ParseUuid(s)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func CompareUuids(a BleUuid, b BleUuid) int {
+	return bytes.Compare(a.Bytes[:], b.Bytes[:])
+}
+
 type BleScanFilterPolicy int
 
 const (
@@ -262,9 +337,30 @@ type BleAdvReport struct {
 
 	// These fields are only present if the sender included them in its
 	// advertisement.
-	Flags          uint8  // 0 if not present.
-	Name           string // "" if not present.
-	NameIsComplete bool   // false if not present.
+	Flags               uint8     // 0 if not present.
+	Uuids16             []uint16  // nil if not present
+	Uuids16IsComplete   bool      // false if not present
+	Uuids32             []uint32  // false if not present
+	Uuids32IsComplete   bool      // false if not present
+	Uuids128            []BleUuid // false if not present
+	Uuids128IsComplete  bool      // false if not present
+	Name                string    // "" if not present.
+	NameIsComplete      bool      // false if not present.
+	TxPwrLvl            int8      // Check TxPwrLvlIsPresent
+	TxPwrLvlIsPresent   bool      // false if not present
+	SlaveItvlMin        uint16    // Check SlaveItvlIsPresent
+	SlaveItvlMax        uint16    // Check SlaveItvlIsPresent
+	SlaveItvlIsPresent  bool      // false if not present
+	SvcDataUuid16       []byte    // false if not present
+	PublicTgtAddrs      []BleAddr // false if not present
+	Appearance          uint16    // Check AppearanceIsPresent
+	AppearanceIsPresent bool      // false if not present
+	AdvItvl             uint16    // Check AdvItvlIsPresent
+	AdvItvlIsPresent    bool      // false if not present
+	SvcDataUuid32       []byte    // false if not present
+	SvcDataUuid128      []byte    // false if not present
+	Uri                 []byte    // false if not present
+	MfgData             []byte    // false if not present
 }
 
 type BleAdvPredicate func(adv BleAdvReport) bool

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/25097817/nmxact/nmble/ble_proto.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_proto.go b/nmxact/nmble/ble_proto.go
index f955fab..69c456f 100644
--- a/nmxact/nmble/ble_proto.go
+++ b/nmxact/nmble/ble_proto.go
@@ -19,10 +19,6 @@ type BleBytes struct {
 	Bytes []byte
 }
 
-type BleUuid struct {
-	Bytes [16]byte
-}
-
 const BLE_SEQ_NONE BleSeq = 0xffffffff
 
 const ERR_CODE_ATT_BASE = 0x100
@@ -671,9 +667,30 @@ type BleScanEvt struct {
 	Data      BleBytes        `json:"data"`
 
 	// Optional
-	DataFlags          uint8  `json:"data_flags"`
-	DataName           string `json:"data_name"`
-	DataNameIsComplete bool   `json:"data_name_is_complete"`
+	DataFlags               uint8     `json:"data_flags"`
+	DataUuids16             []uint16  `json:"data_uuids16"`
+	DataUuids16IsComplete   bool      `json:"data_uuids16_is_complete"`
+	DataUuids32             []uint32  `json:"data_uuids32"`
+	DataUuids32IsComplete   bool      `json:"data_uuids32_is_complete"`
+	DataUuids128            []BleUuid `json:"data_uuids128"`
+	DataUuids128IsComplete  bool      `json:"data_uuids128_is_complete"`
+	DataName                string    `json:"data_name"`
+	DataNameIsComplete      bool      `json:"data_name_is_complete"`
+	DataTxPwrLvl            int8      `json:"data_tx_pwr_lvl"`
+	DataTxPwrLvlIsPresent   bool
+	DataSlaveItvlMin        uint16 `json:"data_slave_itvl_min"`
+	DataSlaveItvlMax        uint16 `json:"data_slave_itvl_max"`
+	DataSlaveItvlIsPresent  bool
+	DataSvcDataUuid16       BleBytes  `json:"data_svc_data_uuid16"`
+	DataPublicTgtAddrs      []BleAddr `json:"data_public_tgt_addrs"`
+	DataAppearance          uint16    `json:"data_appearance"`
+	DataAppearanceIsPresent bool
+	DataAdvItvl             uint16 `json:"data_adv_itvl"`
+	DataAdvItvlIsPresent    bool
+	DataSvcDataUuid32       BleBytes `json:"data_svc_data_uuid32"`
+	DataSvcDataUuid128      BleBytes `json:"data_svc_data_uuid128"`
+	DataUri                 BleBytes `json:"data_uri"`
+	DataMfgData             BleBytes `json:"data_mfg_data"`
 }
 
 type BleScanCancelReq struct {
@@ -880,44 +897,3 @@ func (bb *BleBytes) UnmarshalJSON(data []byte) error {
 
 	return nil
 }
-
-func (bu *BleUuid) String() string {
-	var buf bytes.Buffer
-	buf.Grow(len(bu.Bytes)*2 + 3)
-
-	// XXX: For now, only support 128-bit UUIDs.
-
-	for i, b := range bu.Bytes {
-		switch i {
-		case 4, 6, 8, 10:
-			buf.WriteString("-")
-		}
-
-		fmt.Fprintf(&buf, "%02x", b)
-	}
-
-	return buf.String()
-}
-
-func (bu *BleUuid) MarshalJSON() ([]byte, error) {
-	return json.Marshal(bu.String())
-}
-
-func (bu *BleUuid) UnmarshalJSON(data []byte) error {
-	var s string
-	if err := json.Unmarshal(data, &s); err != nil {
-		return err
-	}
-
-	var err error
-	*bu, err = ParseUuid(s)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func CompareUuids(a BleUuid, b BleUuid) int {
-	return bytes.Compare(a.Bytes[:], b.Bytes[:])
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newtmgr/blob/25097817/nmxact/nmble/ble_util.go
----------------------------------------------------------------------
diff --git a/nmxact/nmble/ble_util.go b/nmxact/nmble/ble_util.go
index 0cd17f3..f0a6c04 100644
--- a/nmxact/nmble/ble_util.go
+++ b/nmxact/nmble/ble_util.go
@@ -2,7 +2,6 @@ package nmble
 
 import (
 	"fmt"
-	"strconv"
 	"sync/atomic"
 
 	log "github.com/Sirupsen/logrus"
@@ -26,36 +25,6 @@ func NextSeq() BleSeq {
 	return BleSeq(atomic.AddUint32(&nextSeq, 1))
 }
 
-func ParseUuid(uuidStr string) (BleUuid, error) {
-	bu := BleUuid{}
-
-	if len(uuidStr) != 36 {
-		return bu, fmt.Errorf("Invalid UUID: %s", uuidStr)
-	}
-
-	boff := 0
-	for i := 0; i < 36; {
-		switch i {
-		case 8, 13, 18, 23:
-			if uuidStr[i] != '-' {
-				return bu, fmt.Errorf("Invalid UUID: %s", uuidStr)
-			}
-			i++
-
-		default:
-			u64, err := strconv.ParseUint(uuidStr[i:i+2], 16, 8)
-			if err != nil {
-				return bu, fmt.Errorf("Invalid UUID: %s", uuidStr)
-			}
-			bu.Bytes[boff] = byte(u64)
-			i += 2
-			boff++
-		}
-	}
-
-	return bu, nil
-}
-
 func BhdTimeoutError(rspType MsgType) error {
 	str := fmt.Sprintf("Timeout waiting for blehostd to send %s response",
 		MsgTypeToString(rspType))
@@ -99,9 +68,30 @@ func BleAdvReportFromScanEvt(e *BleScanEvt) BleAdvReport {
 		Rssi: e.Rssi,
 		Data: e.Data.Bytes,
 
-		Flags:          e.DataFlags,
-		Name:           e.DataName,
-		NameIsComplete: e.DataNameIsComplete,
+		Flags:               e.DataFlags,
+		Uuids16:             e.DataUuids16,
+		Uuids16IsComplete:   e.DataUuids16IsComplete,
+		Uuids32:             e.DataUuids32,
+		Uuids32IsComplete:   e.DataUuids32IsComplete,
+		Uuids128:            e.DataUuids128,
+		Uuids128IsComplete:  e.DataUuids128IsComplete,
+		Name:                e.DataName,
+		NameIsComplete:      e.DataNameIsComplete,
+		TxPwrLvl:            e.DataTxPwrLvl,
+		TxPwrLvlIsPresent:   e.DataTxPwrLvlIsPresent,
+		SlaveItvlMin:        e.DataSlaveItvlMin,
+		SlaveItvlMax:        e.DataSlaveItvlMax,
+		SlaveItvlIsPresent:  e.DataSlaveItvlIsPresent,
+		SvcDataUuid16:       e.DataSvcDataUuid16.Bytes,
+		PublicTgtAddrs:      e.DataPublicTgtAddrs,
+		Appearance:          e.DataAppearance,
+		AppearanceIsPresent: e.DataAppearanceIsPresent,
+		AdvItvl:             e.DataAdvItvl,
+		AdvItvlIsPresent:    e.DataAdvItvlIsPresent,
+		SvcDataUuid32:       e.DataSvcDataUuid32.Bytes,
+		SvcDataUuid128:      e.DataSvcDataUuid128.Bytes,
+		Uri:                 e.DataUri.Bytes,
+		MfgData:             e.DataMfgData.Bytes,
 	}
 }
 


[2/2] incubator-mynewt-newtmgr git commit: nmxact - Robustness fixes for ble_xport failures.

Posted by cc...@apache.org.
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 <cc...@apache.org>
Authored: Thu Apr 6 12:37:25 2017 -0700
Committer: Christopher Collins <cc...@apache.org>
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