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/10/12 23:47:04 UTC

incubator-mynewt-newt git commit: newtmgr - add ability to log in / out bytes.

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop a8d6065d9 -> 28cb03fd8


newtmgr - add ability to log in / out bytes.


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

Branch: refs/heads/develop
Commit: 28cb03fd80297f6ecb7f9184e53f7f78cd7468d4
Parents: a8d6065
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Oct 12 16:46:37 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Oct 12 16:46:37 2016 -0700

----------------------------------------------------------------------
 newtmgr/nmutil/nmutil.go        | 74 ++++++++++++++++++++++++++++++++++++
 newtmgr/transport/connserial.go |  7 ++++
 2 files changed, 81 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/28cb03fd/newtmgr/nmutil/nmutil.go
----------------------------------------------------------------------
diff --git a/newtmgr/nmutil/nmutil.go b/newtmgr/nmutil/nmutil.go
new file mode 100644
index 0000000..5b63ed6
--- /dev/null
+++ b/newtmgr/nmutil/nmutil.go
@@ -0,0 +1,74 @@
+/**
+ * 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 nmutil
+
+import (
+	"encoding/hex"
+	"fmt"
+	"os"
+	"time"
+)
+
+var PacketTraceDir string
+var traceFile *os.File
+
+// @return                      true if the file can be used;
+//                              false otherwise.
+func ensureTraceFileOpen() bool {
+	if traceFile != nil {
+		return true
+	}
+	if PacketTraceDir == "" {
+		return false
+	}
+
+	now := time.Now()
+	secs := now.Unix()
+
+	filename := fmt.Sprintf("%s/%d", PacketTraceDir, secs)
+
+	var err error
+	traceFile, err = os.Create(filename)
+	if err != nil {
+		return false
+	}
+
+	return true
+}
+
+func LogIncoming(bytes []byte) {
+	if !ensureTraceFileOpen() {
+		return
+	}
+
+	fmt.Fprintf(traceFile, "Incoming:\n%s\n", hex.Dump(bytes))
+}
+
+func LogOutgoing(bytes []byte) {
+	if !ensureTraceFileOpen() {
+		return
+	}
+
+	fmt.Fprintf(traceFile, "Outgoing:\n%s\n", hex.Dump(bytes))
+}
+
+func LogMessage(msg string) {
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/28cb03fd/newtmgr/transport/connserial.go
----------------------------------------------------------------------
diff --git a/newtmgr/transport/connserial.go b/newtmgr/transport/connserial.go
index 11ceadc..223e1db 100644
--- a/newtmgr/transport/connserial.go
+++ b/newtmgr/transport/connserial.go
@@ -32,6 +32,7 @@ import (
 	"github.com/tarm/serial"
 
 	"mynewt.apache.org/newt/newtmgr/config"
+	"mynewt.apache.org/newt/newtmgr/nmutil"
 	"mynewt.apache.org/newt/util"
 )
 
@@ -70,6 +71,8 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 	for scanner.Scan() {
 		line := []byte(scanner.Text())
 
+		nmutil.LogIncoming(line)
+
 		for {
 			if len(line) > 1 && line[0] == '\r' {
 				line = line[1:]
@@ -87,6 +90,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 
 		data, err := base64.StdEncoding.DecodeString(base64Data)
 		if err != nil {
+			nmutil.LogMessage("base64 decode error\n")
 			return nil, util.NewNewtError(
 				fmt.Sprintf("Couldn't decode base64 string: %s\n"+
 					"Packet hex dump:\n%s",
@@ -113,6 +117,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 		full := cs.currentPacket.AddBytes(data)
 		if full {
 			if crc16.Crc16(cs.currentPacket.GetBytes()) != 0 {
+				nmutil.LogMessage("CRC error\n")
 				return nil, util.NewNewtError("CRC error")
 			}
 
@@ -130,6 +135,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 	if err == nil {
 		// Scanner hit EOF, so we'll need to create a new one.  This only
 		// happens on timeouts.
+		nmutil.LogMessage("Timeout reading from serial connection\n")
 		err = util.NewNewtError("Timeout reading from serial connection")
 		cs.scanner = bufio.NewScanner(cs.serialChannel)
 	}
@@ -137,6 +143,7 @@ func (cs *ConnSerial) ReadPacket() (*Packet, error) {
 }
 
 func (cs *ConnSerial) writeData(bytes []byte) {
+	nmutil.LogOutgoing(bytes)
 	log.Debugf("Writing %+v to data channel", bytes)
 	cs.serialChannel.Write(bytes)
 }