You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by pa...@apache.org on 2016/08/24 22:51:24 UTC

incubator-mynewt-newt git commit: add a split image command to newtmgr

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/develop d255509e9 -> d82d6bfaa


add a split image command to newtmgr


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

Branch: refs/heads/develop
Commit: d82d6bfaa4dd8b257c43b9c2a68448b5aad4074c
Parents: d255509
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Mon Aug 8 16:58:55 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Mon Aug 8 16:58:55 2016 -0700

----------------------------------------------------------------------
 newtmgr/cli/image.go           |  62 ++++++++++++++++++++++
 newtmgr/protocol/imagelist.go  |   1 +
 newtmgr/protocol/imagesplit.go | 102 ++++++++++++++++++++++++++++++++++++
 3 files changed, 165 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/d82d6bfa/newtmgr/cli/image.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/image.go b/newtmgr/cli/image.go
index 69fa922..9565a96 100644
--- a/newtmgr/cli/image.go
+++ b/newtmgr/cli/image.go
@@ -25,6 +25,7 @@ import (
 	"io"
 	"io/ioutil"
 	"os"
+	"strconv"
 	"time"
 
 	"mynewt.apache.org/newt/newtmgr/config"
@@ -628,6 +629,55 @@ func coreEraseCmd(cmd *cobra.Command, args []string) {
 	}
 }
 
+func splitCmd(cmd *cobra.Command, args []string) {
+	runner, err := getTargetCmdRunner()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	defer runner.Conn.Close()
+
+	split, err := protocol.NewSplit()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	var nmr *protocol.NmgrReq
+	if len(args) == 0 {
+		nmr, err = split.EncoderReadRequest()
+	} else if len(args) == 1 {
+		b, err := strconv.ParseBool(args[0])
+
+		if err != nil {
+			nmUsage(cmd, util.NewNewtError("Invalid Boolean Argument"))
+		}
+		split.Split = b
+		nmr, err = split.EncoderWriteRequest()
+	} else {
+		nmUsage(cmd, nil)
+		return
+	}
+
+	if err := runner.WriteReq(nmr); err != nil {
+		nmUsage(cmd, err)
+	}
+
+	rsp, err := runner.ReadResp()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	srsp, err := protocol.DecodeSplitReadResponse(rsp.Data)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	if len(args) == 0 {
+		fmt.Printf("Split value is %v\n", srsp.Split)
+	}
+	if srsp.ReturnCode != 0 {
+		fmt.Printf("Error executing split command: rc=%d\n", srsp.ReturnCode)
+	}
+}
+
 func imageCmd() *cobra.Command {
 	imageCmd := &cobra.Command{
 		Use:   "image",
@@ -738,5 +788,17 @@ func imageCmd() *cobra.Command {
 	}
 	imageCmd.AddCommand(coreEraseCmd)
 
+	splitEx := "  newtmgr -c olimex image split 1\n"
+	splitEx += "  newtmgr -c olimex image split 0\n"
+	splitEx += "  newtmgr -c olimex image split\n"
+
+	splitCmd := &cobra.Command{
+		Use:     "split",
+		Short:   "Erase core on target",
+		Example: splitEx,
+		Run:     splitCmd,
+	}
+	imageCmd.AddCommand(splitCmd)
+
 	return imageCmd
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/d82d6bfa/newtmgr/protocol/imagelist.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagelist.go b/newtmgr/protocol/imagelist.go
index 8f4fe8b..18b5f03 100644
--- a/newtmgr/protocol/imagelist.go
+++ b/newtmgr/protocol/imagelist.go
@@ -41,6 +41,7 @@ const (
 	IMGMGR_NMGR_OP_BOOT2    = 5
 	IMGMGR_NMGR_OP_CORELIST = 6
 	IMGMGR_NMGR_OP_CORELOAD = 7
+	IMGMGR_NMGR_OP_SPLITAPP = 8
 )
 
 func HashDecode(src string) (string, error) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/d82d6bfa/newtmgr/protocol/imagesplit.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imagesplit.go b/newtmgr/protocol/imagesplit.go
new file mode 100644
index 0000000..0386b2a
--- /dev/null
+++ b/newtmgr/protocol/imagesplit.go
@@ -0,0 +1,102 @@
+/**
+ * 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 Split struct {
+	Split      bool
+	ReturnCode int `json:"rc"`
+}
+
+func NewSplit() (*Split, error) {
+	s := &Split{}
+	return s, nil
+}
+
+func (s *Split) EncoderReadRequest() (*NmgrReq, error) {
+	msg := "{}"
+
+	data := []byte(msg)
+
+	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_SPLITAPP
+	nmr.Len = uint16(len(data))
+	nmr.Data = data
+
+	return nmr, nil
+}
+
+func (s *Split) EncoderWriteRequest() (*NmgrReq, error) {
+	msg := "{\"split\": "
+	if s.Split {
+		msg += "true"
+	} else {
+		msg += "false"
+	}
+	msg += "}"
+
+	data := []byte(msg)
+
+	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_SPLITAPP
+	nmr.Len = uint16(len(data))
+	nmr.Data = data
+
+	return nmr, nil
+}
+
+func DecodeSplitReadResponse(data []byte) (*Split, error) {
+	i := &Split{}
+
+	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.ReturnCode != 0 {
+		return nil, util.NewNewtError(fmt.Sprintf("Target error: %d",
+			i.ReturnCode))
+	}
+	return i, nil
+}