Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop a8ca2f6da -> c6ae94b40


newtmgr - Allow baud to be specified in connstring


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

Branch: refs/heads/develop
Commit: efbbabb22a85864a7411f38c1b30186b6d6eaa5d
Parents: a8ca2f6
Author: Christopher Collins <ccoll...@apache.org>
Authored: Wed Oct 12 19:03:47 2016 -0700
Committer: Christopher Collins <ccoll...@apache.org>
Committed: Wed Oct 12 19:03:47 2016 -0700

----------------------------------------------------------------------
 newtmgr/cli/connprofile.go      | 22 +++++++--------
 newtmgr/transport/connserial.go | 53 ++++++++++++++++++++++++++++++++----
 2 files changed, 59 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/efbbabb2/newtmgr/cli/connprofile.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/connprofile.go b/newtmgr/cli/connprofile.go
index 2bd5ea7..fa90088 100644
--- a/newtmgr/cli/connprofile.go
+++ b/newtmgr/cli/connprofile.go
@@ -20,10 +20,10 @@
 package cli
 
 import (
+       "encoding/hex"
        "fmt"
-       "strings"
        "strconv"
-       "encoding/hex"
+       "strings"
 
        "mynewt.apache.org/newt/newtmgr/config"
        "mynewt.apache.org/newt/util"
@@ -32,13 +32,13 @@ import (
 )
 
 func copyValidAddress(cp *config.ConnProfile, addrString string) bool {
-       switch(cp.MyType) {
-       case "ble" :
-               deviceAddr,err := hex.DecodeString(strings.Replace(addrString, 
":", "", -1))
+       switch cp.MyType {
+       case "ble":
+               deviceAddr, err := hex.DecodeString(strings.Replace(addrString, 
":", "", -1))
                if err != nil {
                        return false
                }
-               if (len(deviceAddr) > 6) {
+               if len(deviceAddr) > 6 {
                        return false
                }
                cp.MyDeviceAddress = deviceAddr
@@ -69,7 +69,7 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) {
        }
 
        for _, vdef := range args[1:] {
-               s := strings.Split(vdef, "=")
+               s := strings.SplitN(vdef, "=", 2)
                switch s[0] {
                case "name":
                        cp.MyName = s[1]
@@ -101,11 +101,11 @@ func connProfileAddCmd(cmd *cobra.Command, args []string) 
{
 
 func print_addr_hex(addr []byte, sep string) string {
        var str string = ""
-       for _, a:= range addr {
+       for _, a := range addr {
                str += fmt.Sprintf("%02x", a)
                str += fmt.Sprintf(sep)
        }
-       return str[:len(addr)*3 - 1]
+       return str[:len(addr)*3-1]
 }
 
 func connProfileShowCmd(cmd *cobra.Command, args []string) {
@@ -137,8 +137,8 @@ func connProfileShowCmd(cmd *cobra.Command, args []string) {
                        fmt.Printf("Connection profiles: \n")
                }
                fmt.Printf("  %s: type=%s, connstring='%s'", cp.MyName, 
cp.MyType,
-                          cp.MyConnString)
-               if (len(cp.MyDeviceAddress) > 0) {
+                       cp.MyConnString)
+               if len(cp.MyDeviceAddress) > 0 {
                        fmt.Printf(", addr=%s", 
print_addr_hex(cp.MyDeviceAddress, ":"))
                        fmt.Printf(", addrtype=%+v", cp.MyDeviceAddressType)
                }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/efbbabb2/newtmgr/transport/connserial.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/connserial.go b/newtmgr/transport/connserial.go
index 223e1db..6f7da9a 100644
--- a/newtmgr/transport/connserial.go
+++ b/newtmgr/transport/connserial.go
@@ -25,6 +25,8 @@ import (
        "encoding/binary"
        "encoding/hex"
        "fmt"
+       "strconv"
+       "strings"
        "time"
 
        log "github.com/Sirupsen/logrus"
@@ -44,18 +46,59 @@ type ConnSerial struct {
        serialChannel *serial.Port
 }
 
-func (cs *ConnSerial) Open(cp config.NewtmgrConnProfile, readTimeout 
time.Duration) error {
-       var err error
+func newSerialConfig(
+       connString string, readTimeout time.Duration) (*serial.Config, error) {
+
+       fields := strings.Split(connString, ":")
+       if len(fields) == 0 {
+               return nil, util.FmtNewtError("invalid connstring: %s", 
connString)
+       }
+
+       name := ""
+       baud := 115200
+
+       for _, field := range fields {
+               parts := strings.Split(field, "=")
+               if len(parts) == 2 {
+                       if parts[0] == "baud" {
+                               var err error
+                               baud, err = strconv.Atoi(parts[1])
+                               if err != nil {
+                                       return nil, util.ChildNewtError(err)
+                               }
+
+                       }
+
+                       if parts[0] == "dev" {
+                               name = parts[1]
+                       }
+               }
+       }
+
+       // Handle old-style conn string (single token indicating dev file).
+       if name == "" {
+               name = fields[0]
+       }
 
        c := &serial.Config{
-               Name:        cp.ConnString(),
-               Baud:        115200,
+               Name:        name,
+               Baud:        baud,
                ReadTimeout: readTimeout,
        }
 
+       return c, nil
+}
+
+func (cs *ConnSerial) Open(cp config.NewtmgrConnProfile, readTimeout 
time.Duration) error {
+
+       c, err := newSerialConfig(cp.ConnString(), readTimeout)
+       if err != nil {
+               return err
+       }
+
        cs.serialChannel, err = serial.OpenPort(c)
        if err != nil {
-               return util.NewNewtError(err.Error())
+               return util.ChildNewtError(err)
        }
        //defer cs.serialChannel.Close()
 

Reply via email to