You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ma...@apache.org on 2016/01/27 02:19:17 UTC

incubator-mynewt-newt git commit: Add command to download files from target.

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/master ea6254c1e -> d1008e6d9


Add command to download files from target.


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

Branch: refs/heads/master
Commit: d1008e6d96ce83f58764e8194d54de180adf09be
Parents: ea6254c
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Jan 26 17:18:56 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Jan 26 17:18:56 2016 -0800

----------------------------------------------------------------------
 newtmgr/newtmgr.go                    | 101 ++++++++++++++++++++++++++++-
 newtmgr/protocol/imagefiledownload.go |  91 ++++++++++++++++++++++++++
 2 files changed, 191 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/d1008e6d/newtmgr/newtmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
index a51efda..d2d9992 100644
--- a/newtmgr/newtmgr.go
+++ b/newtmgr/newtmgr.go
@@ -17,6 +17,7 @@ package main
 
 import (
 	"fmt"
+	"io"
 	"io/ioutil"
 	"log"
 	"os"
@@ -551,6 +552,8 @@ func fileUploadCmd(cmd *cobra.Command, args []string) {
 		nmUsage(cmd, err)
 	}
 	var currOff uint32 = 0
+	var cnt int = 0
+
 	fileSz := uint32(len(file))
 
 	for currOff < fileSz {
@@ -592,7 +595,8 @@ func fileUploadCmd(cmd *cobra.Command, args []string) {
 			nmUsage(cmd, err)
 		}
 		currOff = ersp.Offset
-		fmt.Println(currOff)
+		cnt++
+		fmt.Println(cnt, currOff)
 	}
 	err = echoCtrl(runner, "1")
 	if err != nil {
@@ -601,6 +605,94 @@ func fileUploadCmd(cmd *cobra.Command, args []string) {
 	fmt.Println("Done")
 }
 
+func fileDownloadCmd(cmd *cobra.Command, args []string) {
+	if len(args) < 2 {
+		nmUsage(cmd, util.NewNewtError(
+			"Need to specify file and target filename to download"))
+	}
+
+	filename := args[0]
+	if len(filename) > 64 {
+		nmUsage(cmd, util.NewNewtError("Target filename too long"))
+	}
+
+	cpm, err := cli.NewCpMgr()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	profile, err := cpm.GetConnProfile(ConnProfileName)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	conn, err := transport.NewConn(profile)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	runner, err := protocol.NewCmdRunner(conn)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	var currOff uint32 = 0
+	var cnt int = 0
+	var fileSz uint32 = 1
+
+	file, err := os.OpenFile(args[1], os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660)
+    	if err != nil {
+		nmUsage(cmd, util.NewNewtError(fmt.Sprintf(
+			"Cannot open file %s - %s", args[1], err.Error())))
+	}
+	for currOff < fileSz {
+		fileDownload, err := protocol.NewFileDownload()
+		if err != nil {
+			nmUsage(cmd, err)
+		}
+
+		fileDownload.Offset = currOff
+		fileDownload.Name = filename
+
+		nmr, err := fileDownload.EncodeWriteRequest()
+		if err != nil {
+			nmUsage(cmd, err)
+		}
+
+		if err := runner.WriteReq(nmr); err != nil {
+			nmUsage(cmd, err)
+		}
+
+		rsp, err := runner.ReadResp()
+		if err != nil {
+			nmUsage(cmd, err)
+		}
+
+		ersp, err := protocol.DecodeFileDownloadResponse(rsp.Data)
+		if err != nil {
+			nmUsage(cmd, err)
+		}
+		if currOff == ersp.Offset {
+			n, err := file.Write(ersp.Data)
+			if err == nil && n < len(ersp.Data) {
+				err = io.ErrShortWrite
+				nmUsage(cmd, util.NewNewtError(fmt.Sprintf(
+					"Cannot write file %s - %s", args[1],
+					err.Error())))
+			}
+		}
+		if currOff == 0 {
+			fileSz = ersp.Size
+		}
+		cnt++
+		currOff += uint32(len(ersp.Data))
+		fmt.Println(cnt, currOff)
+
+	}
+	file.Close()
+	fmt.Println("Done")
+}
+
 func imageCmd() *cobra.Command {
 	imageCmd := &cobra.Command{
 		Use:   "image",
@@ -636,6 +728,13 @@ func imageCmd() *cobra.Command {
 	}
 	imageCmd.AddCommand(fileUploadCmd)
 
+	fileDownloadCmd := &cobra.Command{
+		Use:   "filedownload",
+		Short: "Download file from target",
+		Run:   fileDownloadCmd,
+	}
+	imageCmd.AddCommand(fileDownloadCmd)
+
 	return imageCmd
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/d1008e6d/newtmgr/protocol/imagefiledownload.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagefiledownload.go b/newtmgr/protocol/imagefiledownload.go
new file mode 100644
index 0000000..5dc9988
--- /dev/null
+++ b/newtmgr/protocol/imagefiledownload.go
@@ -0,0 +1,91 @@
+/*
+ Copyright 2015 Runtime Inc.
+ Licensed 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 protocol
+
+import (
+	"encoding/base64"
+	"encoding/json"
+	"fmt"
+	"git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/util"
+)
+
+type FileDownload struct {
+	Offset uint32
+	Size   uint32
+	Name   string
+	Data   []byte
+}
+
+func NewFileDownload() (*FileDownload, error) {
+	f := &FileDownload{}
+	f.Offset = 0
+
+	return f, nil
+}
+
+func (f *FileDownload) EncodeWriteRequest() (*NmgrReq, error) {
+	type DownloadReq struct {
+		Off  uint32 `json:"off"`
+		Name string `json:"name"`
+	}
+	nmr, err := NewNmgrReq()
+	if err != nil {
+		return nil, err
+	}
+
+	nmr.Op = NMGR_OP_READ
+	nmr.Flags = 0
+	nmr.Group = NMGR_GROUP_ID_IMAGE
+	nmr.Id = IMGMGR_NMGR_OP_FILE
+
+	data := []byte{}
+
+	downloadReq := &DownloadReq{
+		Off:  f.Offset,
+		Name: f.Name,
+	}
+	data, _ = json.Marshal(downloadReq)
+	nmr.Len = uint16(len(data))
+	nmr.Data = data
+
+	return nmr, nil
+}
+
+func DecodeFileDownloadResponse(data []byte) (*FileDownload, error) {
+	type DownloadResp struct {
+		Off  uint32 `json:"off"`
+		Size uint32 `json:"len"`
+		Data string `json:"data"`
+	}
+	resp := &DownloadResp{}
+
+	err := json.Unmarshal(data, &resp)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+	decodedData, err := base64.StdEncoding.DecodeString(resp.Data)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+	f := &FileDownload{
+		Offset : resp.Off,
+		Data   : decodedData,
+		Size   : resp.Size,
+	}
+	return f, nil
+}