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/05/20 01:22:15 UTC

[06/13] incubator-mynewt-newt git commit: imgmgr; add boot2/list2 commands which include image hash.

imgmgr; add boot2/list2 commands which include image hash.


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

Branch: refs/heads/develop
Commit: e5b61950abaf8d6072b663ff9673e051f930cede
Parents: 5c938a5
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Apr 25 11:01:02 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Tue May 17 12:39:53 2016 -0700

----------------------------------------------------------------------
 newtmgr/cli/image.go           | 138 +++++++++++++++++++++++++++++++++---
 newtmgr/protocol/imageboot2.go | 107 ++++++++++++++++++++++++++++
 newtmgr/protocol/imagelist.go  |  32 ++++++---
 newtmgr/protocol/imagelist2.go |  80 +++++++++++++++++++++
 4 files changed, 337 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/e5b61950/newtmgr/cli/image.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/image.go b/newtmgr/cli/image.go
index 8f4f52b..138eed8 100644
--- a/newtmgr/cli/image.go
+++ b/newtmgr/cli/image.go
@@ -25,11 +25,11 @@ import (
 	"io/ioutil"
 	"os"
 
+	"github.com/spf13/cobra"
 	"mynewt.apache.org/newt/newtmgr/config"
 	"mynewt.apache.org/newt/newtmgr/protocol"
 	"mynewt.apache.org/newt/newtmgr/transport"
 	"mynewt.apache.org/newt/util"
-	"github.com/spf13/cobra"
 )
 
 func imageListCmd(cmd *cobra.Command, args []string) {
@@ -45,7 +45,7 @@ func imageListCmd(cmd *cobra.Command, args []string) {
 
 	conn, err := transport.NewConn(profile)
 	if err != nil {
-		nmUsage(cmd, err)
+		nmUsage(nil, err)
 	}
 
 	runner, err := protocol.NewCmdRunner(conn)
@@ -82,6 +82,56 @@ func imageListCmd(cmd *cobra.Command, args []string) {
 	}
 }
 
+func imageListCmd2(cmd *cobra.Command, args []string) {
+	cpm, err := config.NewConnProfileMgr()
+	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(nil, err)
+	}
+
+	runner, err := protocol.NewCmdRunner(conn)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	imageList, err := protocol.NewImageList2()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	nmr, err := imageList.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)
+	}
+
+	iRsp, err := protocol.DecodeImageListResponse2(rsp.Data)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	fmt.Println("Images:")
+	for hash, ver := range iRsp.Images {
+		fmt.Printf(" %8s %s\n", ver, hash)
+	}
+}
+
 func imageUploadCmd(cmd *cobra.Command, args []string) {
 	if len(args) < 1 {
 		nmUsage(cmd, util.NewNewtError("Need to specify image to upload"))
@@ -104,7 +154,7 @@ func imageUploadCmd(cmd *cobra.Command, args []string) {
 
 	conn, err := transport.NewConn(profile)
 	if err != nil {
-		nmUsage(cmd, err)
+		nmUsage(nil, err)
 	}
 
 	runner, err := protocol.NewCmdRunner(conn)
@@ -174,7 +224,7 @@ func imageBootCmd(cmd *cobra.Command, args []string) {
 
 	conn, err := transport.NewConn(profile)
 	if err != nil {
-		nmUsage(cmd, err)
+		nmUsage(nil, err)
 	}
 
 	runner, err := protocol.NewCmdRunner(conn)
@@ -209,9 +259,63 @@ func imageBootCmd(cmd *cobra.Command, args []string) {
 		nmUsage(cmd, err)
 	}
 	if len(args) == 0 {
-		fmt.Println("    Test image :", iRsp.Test)
-		fmt.Println("    Main image :", iRsp.Main)
-		fmt.Println("    Active img :", iRsp.Active)
+		fmt.Println("    Test image:", iRsp.Test)
+		fmt.Println("    Main image:", iRsp.Main)
+		fmt.Println("    Active img:", iRsp.Active)
+	}
+}
+
+func imageBoot2Cmd(cmd *cobra.Command, args []string) {
+	cpm, err := config.NewConnProfileMgr()
+	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(nil, err)
+	}
+
+	runner, err := protocol.NewCmdRunner(conn)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	imageBoot, err := protocol.NewImageBoot2()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	if len(args) >= 1 {
+		imageBoot.BootTarget = args[0]
+	}
+	nmr, err := imageBoot.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)
+	}
+
+	iRsp, err := protocol.DecodeImageBoot2Response(rsp.Data)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	if len(args) == 0 {
+		fmt.Println("   Test image:", iRsp.Test)
+		fmt.Println("   Main image:", iRsp.Main)
+		fmt.Println("   Active img:", iRsp.Active)
 	}
 }
 
@@ -243,7 +347,7 @@ func fileUploadCmd(cmd *cobra.Command, args []string) {
 
 	conn, err := transport.NewConn(profile)
 	if err != nil {
-		nmUsage(cmd, err)
+		nmUsage(nil, err)
 	}
 
 	runner, err := protocol.NewCmdRunner(conn)
@@ -331,7 +435,7 @@ func fileDownloadCmd(cmd *cobra.Command, args []string) {
 
 	conn, err := transport.NewConn(profile)
 	if err != nil {
-		nmUsage(cmd, err)
+		nmUsage(nil, err)
 	}
 
 	runner, err := protocol.NewCmdRunner(conn)
@@ -406,11 +510,18 @@ func imageCmd() *cobra.Command {
 	}
 
 	listCmd := &cobra.Command{
+		Use:   "list2",
+		Short: "Show target images",
+		Run:   imageListCmd2,
+	}
+	imageCmd.AddCommand(listCmd)
+
+	listOldCmd := &cobra.Command{
 		Use:   "list",
 		Short: "Show target images",
 		Run:   imageListCmd,
 	}
-	imageCmd.AddCommand(listCmd)
+	imageCmd.AddCommand(listOldCmd)
 
 	uploadCmd := &cobra.Command{
 		Use:   "upload",
@@ -426,6 +537,13 @@ func imageCmd() *cobra.Command {
 	}
 	imageCmd.AddCommand(bootCmd)
 
+	boot2Cmd := &cobra.Command{
+		Use:   "boot2",
+		Short: "Which image to boot",
+		Run:   imageBoot2Cmd,
+	}
+	imageCmd.AddCommand(boot2Cmd)
+
 	fileUploadCmd := &cobra.Command{
 		Use:   "fileupload",
 		Short: "Upload file to target",

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/e5b61950/newtmgr/protocol/imageboot2.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imageboot2.go b/newtmgr/protocol/imageboot2.go
new file mode 100644
index 0000000..c8d4cf8
--- /dev/null
+++ b/newtmgr/protocol/imageboot2.go
@@ -0,0 +1,107 @@
+/**
+ * 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 protocol
+
+import (
+	"encoding/json"
+	"fmt"
+
+	"mynewt.apache.org/newt/util"
+)
+
+type ImageBoot2 struct {
+	BootTarget string
+	Test       string
+	Main       string
+	Active     string
+}
+
+func NewImageBoot2() (*ImageBoot2, error) {
+	s := &ImageBoot2{}
+	s.BootTarget = ""
+	s.Test = ""
+	s.Main = ""
+	s.Active = ""
+	return s, nil
+}
+
+func (i *ImageBoot2) EncodeWriteRequest() (*NmgrReq, error) {
+	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_BOOT2
+	nmr.Len = 0
+
+	if i.BootTarget != "" {
+		type BootReq struct {
+			Test string `json:"test"`
+		}
+
+		hash, err := HashEncode(i.BootTarget)
+		if err != nil {
+			return nil, err
+		}
+		bReq := &BootReq{
+			Test: hash,
+		}
+		data, _ := json.Marshal(bReq)
+		nmr.Data = data
+		nmr.Len = uint16(len(data))
+		nmr.Op = NMGR_OP_WRITE
+	}
+	return nmr, nil
+}
+
+func DecodeImageBoot2Response(data []byte) (*ImageBoot2, error) {
+	i := &ImageBoot2{}
+
+	if len(data) == 0 {
+		return i, nil
+	}
+	err := json.Unmarshal(data, &i)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+	if i.Test != "" {
+		i.Test, err = HashDecode(i.Test)
+		if err != nil {
+			return nil, err
+		}
+	}
+	if i.Main != "" {
+		i.Main, err = HashDecode(i.Main)
+		if err != nil {
+			return nil, err
+		}
+	}
+	if i.Active != "" {
+		i.Active, err = HashDecode(i.Active)
+		if err != nil {
+			return nil, err
+		}
+	}
+	return i, nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/e5b61950/newtmgr/protocol/imagelist.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagelist.go b/newtmgr/protocol/imagelist.go
index e6e8172..8f28045 100644
--- a/newtmgr/protocol/imagelist.go
+++ b/newtmgr/protocol/imagelist.go
@@ -20,6 +20,8 @@
 package protocol
 
 import (
+	"encoding/base64"
+	"encoding/hex"
 	"encoding/json"
 	"fmt"
 
@@ -35,24 +37,34 @@ const (
 	IMGMGR_NMGR_OP_UPLOAD = 1
 	IMGMGR_NMGR_OP_BOOT   = 2
 	IMGMGR_NMGR_OP_FILE   = 3
+	IMGMGR_NMGR_OP_LIST2  = 4
+	IMGMGR_NMGR_OP_BOOT2  = 5
 )
 
+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
+}
+
 func NewImageList() (*ImageList, error) {
 	s := &ImageList{}
 	s.Images = []string{}
 	return s, nil
 }
 
-func ImageVersStr(major uint8, minor uint8, revision uint16, buildNum uint32) string {
-	if major == 0xff && minor == 0xff && revision == 0xffff &&
-		buildNum == 0xffffffff {
-		return "Not set"
-	} else {
-		versStr := fmt.Sprintf("%d.%d.%d.%d", major, minor, revision, buildNum)
-		return versStr
-	}
-}
-
 func (i *ImageList) EncodeWriteRequest() (*NmgrReq, error) {
 	nmr, err := NewNmgrReq()
 	if err != nil {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/e5b61950/newtmgr/protocol/imagelist2.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagelist2.go b/newtmgr/protocol/imagelist2.go
new file mode 100644
index 0000000..173919d
--- /dev/null
+++ b/newtmgr/protocol/imagelist2.go
@@ -0,0 +1,80 @@
+/**
+ * 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 protocol
+
+import (
+	"encoding/json"
+	"fmt"
+
+	"mynewt.apache.org/newt/util"
+)
+
+type ImageList2 struct {
+	Images map[string]string
+}
+
+func NewImageList2() (*ImageList2, error) {
+	s := &ImageList2{}
+	s.Images = map[string]string{}
+	return s, nil
+}
+
+func (i *ImageList2) EncodeWriteRequest() (*NmgrReq, error) {
+	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_LIST2
+	nmr.Len = 0
+
+	return nmr, nil
+}
+
+func DecodeImageListResponse2(data []byte) (*ImageList2, error) {
+	type ImageInfoJson map[string]string
+
+	type ImageListJson struct {
+		Images []ImageInfoJson
+	}
+
+	list := &ImageListJson{}
+
+	err := json.Unmarshal(data, &list)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+
+	list2, _ := NewImageList2()
+	for _, info := range list.Images {
+		for hash, ver := range info {
+			hash, err := HashDecode(hash)
+			if err != nil {
+				return nil, err
+			}
+			list2.Images[hash] = ver
+		}
+	}
+	return list2, nil
+}