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:27 UTC
[7/9] incubator-mynewt-newt git commit: MYNEWT-266
MYNEWT-266
- Add address checking functionality to BLE newtmgr transport by making it support in GATT
- Currently the byte order is inverted, will be fixed shortly.
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/88405e0a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/88405e0a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/88405e0a
Branch: refs/heads/develop
Commit: 88405e0aee9e0a03cbb7eafabb294e4e6b0e39e8
Parents: fe4de7b
Author: admin <vr...@gmail.com>
Authored: Thu Jul 14 19:47:59 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Tue Jul 19 13:06:58 2016 -0700
----------------------------------------------------------------------
newtmgr/cli/connprofile.go | 38 ++++++++++++++++++++++++++++++++++++--
newtmgr/config/connprofile.go | 12 ++++++++++++
newtmgr/transport/connble.go | 22 ++++++++++++++++++++--
3 files changed, 68 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/88405e0a/newtmgr/cli/connprofile.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/connprofile.go b/newtmgr/cli/connprofile.go
index 35a059e..fa2a9af 100644
--- a/newtmgr/cli/connprofile.go
+++ b/newtmgr/cli/connprofile.go
@@ -22,6 +22,8 @@ package cli
import (
"fmt"
"strings"
+ "strconv"
+ "encoding/hex"
"mynewt.apache.org/newt/newtmgr/config"
"mynewt.apache.org/newt/util"
@@ -29,6 +31,20 @@ import (
"github.com/spf13/cobra"
)
+func isAddressValid(cp *config.ConnProfile, addrlen int) bool {
+ if cp.MyType == "ble" && addrlen != 6 {
+ return true
+ }
+ return false
+}
+
+func isAddressTypeValid(cp *config.ConnProfile, addrtype uint64) bool {
+ if cp.MyType == "ble" && addrtype < 4 {
+ return true
+ }
+ return false
+}
+
func connProfileAddCmd(cmd *cobra.Command, args []string) {
cpm, err := config.NewConnProfileMgr()
if err != nil {
@@ -50,6 +66,18 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) {
cp.MyType = s[1]
case "connstring":
cp.MyConnString = s[1]
+ case "addr":
+ deviceAddr,err := hex.DecodeString(s[1])
+ if err != nil && isAddressValid(cp, len(deviceAddr)) != 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) {
+ nmUsage(cmd, util.NewNewtError("Invalid address type"+s[1]))
+ }
+ cp.MyDeviceAddressType = uint8(deviceAddrType64)
default:
nmUsage(cmd, util.NewNewtError("Unknown variable "+s[0]))
}
@@ -90,8 +118,14 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
found = true
fmt.Printf("Connection profiles: \n")
}
- fmt.Printf(" %s: type=%s, connstring='%s'\n", cp.MyName, cp.MyType,
- cp.MyConnString)
+ 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)
+// }
+
+ fmt.Printf("\n")
}
if !found {
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/88405e0a/newtmgr/config/connprofile.go
----------------------------------------------------------------------
diff --git a/newtmgr/config/connprofile.go b/newtmgr/config/connprofile.go
index b0203cb..ea3e3a1 100644
--- a/newtmgr/config/connprofile.go
+++ b/newtmgr/config/connprofile.go
@@ -37,12 +37,16 @@ type NewtmgrConnProfile interface {
Name() string
Type() string
ConnString() string
+ DeviceAddress() [6]byte
+ DeviceAddressType() uint8
}
type ConnProfile struct {
MyName string
MyType string
MyConnString string
+ MyDeviceAddress [6]byte
+ MyDeviceAddressType uint8
}
func NewConnProfileMgr() (*ConnProfileMgr, error) {
@@ -180,6 +184,14 @@ func (cp *ConnProfile) ConnString() string {
return cp.MyConnString
}
+func (cp *ConnProfile) DeviceAddressType() uint8 {
+ return cp.MyDeviceAddressType
+}
+
+func (cp *ConnProfile) DeviceAddress() [6]byte {
+ return cp.MyDeviceAddress
+}
+
func NewConnProfile(pName string) (*ConnProfile, error) {
cp := &ConnProfile{}
cp.MyName = pName
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/88405e0a/newtmgr/transport/connble.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/connble.go b/newtmgr/transport/connble.go
index 724801c..c5970cc 100644
--- a/newtmgr/transport/connble.go
+++ b/newtmgr/transport/connble.go
@@ -39,6 +39,8 @@ 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 deviceAddressType uint8
type ConnBLE struct {
connProfile config.NewtmgrConnProfile
@@ -65,8 +67,22 @@ func onStateChanged(d gatt.Device, s gatt.State) {
}
func onPeriphDiscovered(p gatt.Peripheral, a *gatt.Advertisement, rssi int) {
- if a.LocalName == deviceName {
- log.Debugf("Peripheral Discovered: %s", p.Name())
+ var matched bool = false
+
+ if (len(deviceName) > 0) {
+ matched = a.LocalName == deviceName
+ if (matched == false) {
+ return
+ }
+ }
+
+ if (len(deviceAddress) > 0) {
+ matched = a.Address == deviceAddress && a.AddressType == deviceAddressType
+ }
+
+ if (matched == true) {
+ log.Debugf("Peripheral Discovered: %s, Address:%+v Address Type:%+v",
+ p.Name(), a.Address, a.AddressType)
p.Device().StopScanning()
p.Device().Connect(p)
}
@@ -122,6 +138,8 @@ func (cb *ConnBLE) Open(cp config.NewtmgrConnProfile, readTimeout time.Duration)
}
deviceName = cp.ConnString()
+ deviceAddress = cp.DeviceAddress()
+ deviceAddressType = cp.DeviceAddressType()
cb.bleDevice, err = gatt.NewDevice(DefaultClientOptions...)
if err != nil {
return util.NewNewtError(err.Error())