Repository: incubator-mynewt-newt Updated Branches: refs/heads/develop 8d09f1306 -> f936c989e
changes to allow the image state command to use cbor instead of json 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/f936c989 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/f936c989 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/f936c989 Branch: refs/heads/develop Commit: f936c989e94f5ce32313035a09d2d6a7035f6a34 Parents: ade80cf Author: Paul Dietrich <paulfdietr...@yahoo.com> Authored: Thu Oct 13 11:06:41 2016 -0700 Committer: Paul Dietrich <paulfdietr...@yahoo.com> Committed: Thu Oct 13 11:10:47 2016 -0700 ---------------------------------------------------------------------- newtmgr/cli/image.go | 26 ++++++++++++++------------ newtmgr/protocol/imagelist.go | 26 -------------------------- newtmgr/protocol/imagestate.go | 14 +++++++------- 3 files changed, 21 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f936c989/newtmgr/cli/image.go ---------------------------------------------------------------------- diff --git a/newtmgr/cli/image.go b/newtmgr/cli/image.go index 087d57c..48090f9 100644 --- a/newtmgr/cli/image.go +++ b/newtmgr/cli/image.go @@ -128,8 +128,10 @@ func imageStateCmd(cmd *cobra.Command, args []string) { nmUsage(cmd, nil) } + hex_bytes, _ := hex.DecodeString(args[1]) + req := protocol.ImageStateWriteReq{ - Hash: args[1], + Hash: hex_bytes, Confirm: false, } nmr, err = req.Encode() @@ -138,7 +140,7 @@ func imageStateCmd(cmd *cobra.Command, args []string) { } } else if args[0] == "confirm" { req := protocol.ImageStateWriteReq{ - Hash: "", + Hash: make([]byte, 0), Confirm: true, } nmr, err = req.Encode() @@ -172,15 +174,10 @@ func imageStateCmd(cmd *cobra.Command, args []string) { fmt.Printf(" version: %s\n", img.Version) fmt.Printf(" bootable: %v\n", img.Bootable) fmt.Printf(" flags: %s\n", imageFlagsStr(img)) - if img.Hash == "" { + if len(img.Hash) == 0 { fmt.Printf(" hash: Unavailable\n") } else { - dec, err := base64.StdEncoding.DecodeString(img.Hash) - if err != nil { - fmt.Printf(" hash: Unable to Decode") - } else { - fmt.Printf(" hash: %s\n", hex.EncodeToString(dec[:])) - } + fmt.Printf(" hash: %x\n", img.Hash) } } @@ -672,10 +669,15 @@ func imageCmd() *cobra.Command { } imageCmd.AddCommand(listCmd) + stateEx := " newtmgr -c olimex image state show\n" + stateEx += " newtmgr -c olimex image state test <hash>\n" + stateEx += " newtmgr -c olimex image state confirm" + stateCmd := &cobra.Command{ - Use: "state", - Short: "Show target images", - Run: imageStateCmd, + Use: "state", + Short: "Show target images", + Example: stateEx, + Run: imageStateCmd, } imageCmd.AddCommand(stateCmd) http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f936c989/newtmgr/protocol/imagelist.go ---------------------------------------------------------------------- diff --git a/newtmgr/protocol/imagelist.go b/newtmgr/protocol/imagelist.go index 67def59..1c31bf8 100644 --- a/newtmgr/protocol/imagelist.go +++ b/newtmgr/protocol/imagelist.go @@ -19,14 +19,6 @@ package protocol -import ( - "encoding/base64" - "encoding/hex" - "fmt" - - "mynewt.apache.org/newt/util" -) - const ( IMGMGR_NMGR_OP_LIST = 0 IMGMGR_NMGR_OP_UPLOAD = 1 @@ -38,21 +30,3 @@ const ( IMGMGR_NMGR_OP_CORELOAD = 7 IMGMGR_NMGR_OP_STATE = 8 ) - -func HashDecode(src string) (string, error) { - imgHex, err := base64.StdEncoding.DecodeString(src) - if err != nil { - return "", util.NewNewtError(fmt.Sprintf("Hash decode error: %s", - err.Error())) - } - return hex.EncodeToString(imgHex), nil -} - -func HashEncode(src string) (string, error) { - imgHex, err := hex.DecodeString(src) - if err != nil { - return "", util.NewNewtError(fmt.Sprintf("Hash encode error: %s", - err.Error())) - } - return base64.StdEncoding.EncodeToString(imgHex), nil -} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/f936c989/newtmgr/protocol/imagestate.go ---------------------------------------------------------------------- diff --git a/newtmgr/protocol/imagestate.go b/newtmgr/protocol/imagestate.go index f64ad0f..dc2cff9 100644 --- a/newtmgr/protocol/imagestate.go +++ b/newtmgr/protocol/imagestate.go @@ -37,7 +37,7 @@ import ( type ImageStateEntry struct { Slot int `codec:"slot"` Version string `codec:"version"` - Hash string `codec:"hash"` + Hash []byte `codec:"hash"` Bootable bool `codec:"bootable"` Pending bool `codec:"pending"` Confirmed bool `codec:"confirmed"` @@ -54,7 +54,7 @@ type ImageStateReadReq struct { } type ImageStateWriteReq struct { - Hash string `codec:"hash"` + Hash []byte `codec:"hash"` Confirm bool `codec:"confirm"` } @@ -88,8 +88,8 @@ func (i *ImageStateWriteReq) Encode() (*NmgrReq, error) { Confirm: i.Confirm, } - if i.Hash != "" { - clone.Hash, err = HashEncode(i.Hash) + if len(i.Hash) != 0 { + clone.Hash = i.Hash if err != nil { return nil, err } @@ -101,7 +101,7 @@ func (i *ImageStateWriteReq) Encode() (*NmgrReq, error) { nmr.Id = IMGMGR_NMGR_OP_STATE data := make([]byte, 0) - enc := codec.NewEncoderBytes(&data, new(codec.JsonHandle)) + enc := codec.NewEncoderBytes(&data, new(codec.CborHandle)) enc.Encode(clone) nmr.Data = data nmr.Len = uint16(len(data)) @@ -112,10 +112,10 @@ func (i *ImageStateWriteReq) Encode() (*NmgrReq, error) { func DecodeImageStateResponse(data []byte) (*ImageStateRsp, error) { rsp := &ImageStateRsp{} - dec := codec.NewDecoderBytes(data, new(codec.JsonHandle)) + dec := codec.NewDecoderBytes(data, new(codec.CborHandle)) err := dec.Decode(&rsp) if err != nil { - return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s", + return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming cbor: %s", err.Error())) } return rsp, nil