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 2016/07/19 20:30:29 UTC

[9/9] incubator-mynewt-newt git commit: MYNEWT-266 newtmgr over BLE

MYNEWT-266 newtmgr over BLE

- Support generic addressing for the connection manager
- Advertisement address will be matched with inverted byte order


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/commit/101612ce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/101612ce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/101612ce

Branch: refs/heads/develop
Commit: 101612ce7e6e4f6d5804f14da7b978791eba5e10
Parents: 88405e0
Author: admin <vr...@gmail.com>
Authored: Fri Jul 15 20:40:20 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Tue Jul 19 13:06:58 2016 -0700

----------------------------------------------------------------------
 newtmgr/cli/connprofile.go    | 40 +++++++++++++++++++++++++++-----------
 newtmgr/config/connprofile.go |  6 +++---
 newtmgr/transport/connble.go  | 17 ++++++++++++----
 3 files changed, 45 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/101612ce/newtmgr/cli/connprofile.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/connprofile.go b/newtmgr/cli/connprofile.go
index fa2a9af..701dacf 100644
--- a/newtmgr/cli/connprofile.go
+++ b/newtmgr/cli/connprofile.go
@@ -31,11 +31,22 @@ import (
 	"github.com/spf13/cobra"
 )
 
-func isAddressValid(cp *config.ConnProfile, addrlen int) bool {
-	if cp.MyType == "ble" && addrlen != 6 {
-		return true
+func copyValidAddress(cp *config.ConnProfile, addrString string) bool {
+	switch(cp.MyType) {
+	case "ble" :
+		deviceAddr,err := hex.DecodeString(strings.Replace(addrString, ":", "", -1))
+		if err != nil {
+			return false
+		}
+		if (len(deviceAddr) > 6) {
+			return false
+		}
+		cp.MyDeviceAddress = deviceAddr
+	default:
+		return false
 	}
-	return false
+
+	return true
 }
 
 func isAddressTypeValid(cp *config.ConnProfile, addrtype uint64) bool {
@@ -67,11 +78,9 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) {
 		case "connstring":
 			cp.MyConnString = s[1]
 		case "addr":
-			deviceAddr,err := hex.DecodeString(s[1])
-			if err != nil && isAddressValid(cp, len(deviceAddr)) != true {
+			if copyValidAddress(cp, s[1]) != true {
 				nmUsage(cmd, util.NewNewtError("Invalid address"+s[1]))
 			}
-			copy(cp.MyDeviceAddress[:], deviceAddr[0:6])
 		case "addrtype":
 			deviceAddrType64, err := strconv.ParseUint(s[1], 10, 8)
 			if err != nil && isAddressTypeValid(cp, deviceAddrType64) {
@@ -90,6 +99,15 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) {
 	fmt.Printf("Connection profile %s successfully added\n", name)
 }
 
+func print_addr_hex(addr []byte, sep string) string {
+	var str string = ""
+	for _, a:= range addr {
+		str += fmt.Sprintf("%02x", a)
+		str += fmt.Sprintf(sep)
+	}
+	return str[:len(addr)*3 - 1]
+}
+
 func connProfileShowCmd(cmd *cobra.Command, args []string) {
 	cpm, err := config.NewConnProfileMgr()
 	if err != nil {
@@ -120,10 +138,10 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
 		}
 		fmt.Printf("  %s: type=%s, connstring='%s'", cp.MyName, cp.MyType,
 			   cp.MyConnString)
-		//if (len(cp.MyDeviceAddress) > 0) {
-			fmt.Printf(" addr=%x, addrtype=%+v", cp.MyDeviceAddress,
-			cp.MyDeviceAddressType)
-//		}
+		if (len(cp.MyDeviceAddress) > 0) {
+			fmt.Printf("addr=%s", print_addr_hex(cp.MyDeviceAddress, ":"))
+			fmt.Printf(" addrtype=%+v", cp.MyDeviceAddressType)
+		}
 
 		fmt.Printf("\n")
 	}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/101612ce/newtmgr/config/connprofile.go
----------------------------------------------------------------------
diff --git a/newtmgr/config/connprofile.go b/newtmgr/config/connprofile.go
index ea3e3a1..d03c536 100644
--- a/newtmgr/config/connprofile.go
+++ b/newtmgr/config/connprofile.go
@@ -37,7 +37,7 @@ type NewtmgrConnProfile interface {
 	Name() string
 	Type() string
 	ConnString() string
-	DeviceAddress() [6]byte
+	DeviceAddress() []byte
 	DeviceAddressType() uint8
 }
 
@@ -45,7 +45,7 @@ type ConnProfile struct {
 	MyName       string
 	MyType       string
 	MyConnString string
-	MyDeviceAddress [6]byte
+	MyDeviceAddress []byte
 	MyDeviceAddressType uint8
 }
 
@@ -188,7 +188,7 @@ func (cp *ConnProfile) DeviceAddressType() uint8 {
 	return cp.MyDeviceAddressType
 }
 
-func (cp *ConnProfile) DeviceAddress() [6]byte {
+func (cp *ConnProfile) DeviceAddress() []byte {
 	return cp.MyDeviceAddress
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/101612ce/newtmgr/transport/connble.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/connble.go b/newtmgr/transport/connble.go
index c5970cc..18dcf1c 100644
--- a/newtmgr/transport/connble.go
+++ b/newtmgr/transport/connble.go
@@ -39,7 +39,7 @@ var CharDisc = make(chan bool)
 var newtmgrServiceId = gatt.MustParseUUID("8D53DC1D-1DB7-4CD3-868B-8A527460AA84")
 var newtmgrServiceCharId = gatt.MustParseUUID("DA2E7828-FBCE-4E01-AE9E-261174997C48")
 var deviceName string
-var deviceAddress [6]byte
+var deviceAddress []byte
 var deviceAddressType uint8
 
 type ConnBLE struct {
@@ -54,6 +54,13 @@ var devicePerph gatt.Peripheral
 
 var bleTxData []byte
 
+func reverseBytes(arr []byte) []byte {
+	if len(arr) == 0 {
+		return arr
+	}
+	return append(reverseBytes(arr[1:]), arr[0])
+}
+
 func onStateChanged(d gatt.Device, s gatt.State) {
 	log.Debugf("State:%+v", s)
 	switch s {
@@ -77,7 +84,9 @@ func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
 	}
 
 	if (len(deviceAddress) > 0) {
-		matched = a.Address == deviceAddress && a.AddressType == deviceAddressType
+		var deviceAddrArr [6]byte
+		copy(deviceAddrArr[:], deviceAddress[0:6])
+		matched = a.Address == deviceAddrArr && a.AddressType == deviceAddressType
 	}
 
 	if (matched == true) {
@@ -89,7 +98,6 @@ func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
 }
 
 func newtmgrNotifyCB(c *gatt.Characteristic, incomingDatabuf []byte, err error) {
-	log.Debugf("BLE Newtmgr rx data:%+v", incomingDatabuf)
         err = nil
         rxBLEPkt <- incomingDatabuf
         return
@@ -138,7 +146,8 @@ func (cb *ConnBLE) Open(cp config.NewtmgrConnProfile, readTimeout time.Duration)
 	}
 
 	deviceName = cp.ConnString()
-	deviceAddress = cp.DeviceAddress()
+	deviceAddress = reverseBytes(cp.DeviceAddress())
+	log.Debugf("BLE Connection devaddr:%+v", deviceAddress)
 	deviceAddressType = cp.DeviceAddressType()
 	cb.bleDevice, err = gatt.NewDevice(DefaultClientOptions...)
 	if err != nil {