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/25 20:01:18 UTC
incubator-mynewt-newt git commit: Add option to do file upload.
Repository: incubator-mynewt-newt
Updated Branches:
refs/heads/master 7f5231438 -> 333f5dbd8
Add option to do file upload.
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/333f5dbd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/333f5dbd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/333f5dbd
Branch: refs/heads/master
Commit: 333f5dbd81a4addd316e1a849158b318473f4fc3
Parents: 7f52314
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Jan 25 11:01:00 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Jan 25 11:01:00 2016 -0800
----------------------------------------------------------------------
newtmgr/newtmgr.go | 103 ++++++++++++++++++++++++++++++-
newtmgr/protocol/imagefileupload.go | 93 ++++++++++++++++++++++++++++
newtmgr/protocol/imagelist.go | 1 +
3 files changed, 194 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/333f5dbd/newtmgr/newtmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
index 8d84e4c..a51efda 100644
--- a/newtmgr/newtmgr.go
+++ b/newtmgr/newtmgr.go
@@ -421,8 +421,8 @@ func imageUploadCmd(cmd *cobra.Command, args []string) {
}
blockSz := imageSz - currOff
- if blockSz > 32 {
- blockSz = 32
+ if blockSz > 36 {
+ blockSz = 36
}
imageUpload.Offset = currOff
@@ -511,6 +511,96 @@ func imageBootCmd(cmd *cobra.Command, args []string) {
}
}
+func fileUploadCmd(cmd *cobra.Command, args []string) {
+ if len(args) < 2 {
+ nmUsage(cmd, util.NewNewtError(
+ "Need to specify file and target filename to upload"))
+ }
+
+ file, err := ioutil.ReadFile(args[0])
+ if err != nil {
+ nmUsage(cmd, util.NewNewtError(err.Error()))
+ }
+
+ filename := args[1]
+ 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)
+ }
+ err = echoCtrl(runner, "0")
+ if err != nil {
+ nmUsage(cmd, err)
+ }
+ var currOff uint32 = 0
+ fileSz := uint32(len(file))
+
+ for currOff < fileSz {
+ fileUpload, err := protocol.NewFileUpload()
+ if err != nil {
+ nmUsage(cmd, err)
+ }
+
+ blockSz := fileSz - currOff
+ if currOff == 0 {
+ blockSz = 3
+ } else {
+ if blockSz > 36 {
+ blockSz = 36
+ }
+ }
+
+ fileUpload.Offset = currOff
+ fileUpload.Size = fileSz
+ fileUpload.Name = filename
+ fileUpload.Data = file[currOff : currOff+blockSz]
+
+ nmr, err := fileUpload.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.DecodeFileUploadResponse(rsp.Data)
+ if err != nil {
+ nmUsage(cmd, err)
+ }
+ currOff = ersp.Offset
+ fmt.Println(currOff)
+ }
+ err = echoCtrl(runner, "1")
+ if err != nil {
+ nmUsage(cmd, err)
+ }
+ fmt.Println("Done")
+}
+
func imageCmd() *cobra.Command {
imageCmd := &cobra.Command{
Use: "image",
@@ -527,7 +617,7 @@ func imageCmd() *cobra.Command {
uploadCmd := &cobra.Command{
Use: "upload",
- Short: "Upload image to a target",
+ Short: "Upload image to target",
Run: imageUploadCmd,
}
imageCmd.AddCommand(uploadCmd)
@@ -539,6 +629,13 @@ func imageCmd() *cobra.Command {
}
imageCmd.AddCommand(bootCmd)
+ fileUploadCmd := &cobra.Command{
+ Use: "fileupload",
+ Short: "Upload file to target",
+ Run: fileUploadCmd,
+ }
+ imageCmd.AddCommand(fileUploadCmd)
+
return imageCmd
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/333f5dbd/newtmgr/protocol/imagefileupload.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagefileupload.go b/newtmgr/protocol/imagefileupload.go
new file mode 100644
index 0000000..7061e07
--- /dev/null
+++ b/newtmgr/protocol/imagefileupload.go
@@ -0,0 +1,93 @@
+/*
+ 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 FileUpload struct {
+ Offset uint32 `json:"off"`
+ Name string
+ Size uint32
+ Data []byte
+}
+
+func NewFileUpload() (*FileUpload, error) {
+ f := &FileUpload{}
+ f.Offset = 0
+
+ return f, nil
+}
+
+func (f *FileUpload) EncodeWriteRequest() (*NmgrReq, error) {
+ type UploadReq struct {
+ Off uint32 `json:"off"`
+ Data string `json:"data"`
+ }
+ type UploadFirstReq struct {
+ Off uint32 `json:"off"`
+ Size uint32 `json:"len"`
+ Name string `json:"name"`
+ Data string `json:"data"`
+ }
+ nmr, err := NewNmgrReq()
+ if err != nil {
+ return nil, err
+ }
+
+ nmr.Op = NMGR_OP_WRITE
+ nmr.Flags = 0
+ nmr.Group = NMGR_GROUP_ID_IMAGE
+ nmr.Id = IMGMGR_NMGR_OP_FILE
+
+ data := []byte{}
+
+ if f.Offset == 0 {
+ uploadReq := &UploadFirstReq{
+ Off: f.Offset,
+ Size: f.Size,
+ Name: f.Name,
+ Data: base64.StdEncoding.EncodeToString(f.Data),
+ }
+ data, _ = json.Marshal(uploadReq)
+ } else {
+ uploadReq := &UploadReq{
+ Off: f.Offset,
+ Data: base64.StdEncoding.EncodeToString(f.Data),
+ }
+ data, _ = json.Marshal(uploadReq)
+ }
+ nmr.Len = uint16(len(data))
+ nmr.Data = data
+ fmt.Printf("File upload %s\n", data)
+
+ return nmr, nil
+}
+
+func DecodeFileUploadResponse(data []byte) (*FileUpload, error) {
+ f := &FileUpload{}
+
+ err := json.Unmarshal(data, &f)
+ if err != nil {
+ return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+ err.Error()))
+ }
+ return f, nil
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/333f5dbd/newtmgr/protocol/imagelist.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagelist.go b/newtmgr/protocol/imagelist.go
index 024dfe5..f7be7af 100644
--- a/newtmgr/protocol/imagelist.go
+++ b/newtmgr/protocol/imagelist.go
@@ -30,6 +30,7 @@ const (
IMGMGR_NMGR_OP_LIST = 0
IMGMGR_NMGR_OP_UPLOAD = 1
IMGMGR_NMGR_OP_BOOT = 2
+ IMGMGR_NMGR_OP_FILE = 3
)
func NewImageList() (*ImageList, error) {