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/21 23:35:14 UTC

[1/2] incubator-mynewt-newt git commit: Access to config variables via newtmgr.

Repository: incubator-mynewt-newt
Updated Branches:
  refs/heads/master 575118af5 -> 831c85757


Access to config variables via 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/a4d23590
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/a4d23590
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/a4d23590

Branch: refs/heads/master
Commit: a4d235905f4f36e8b664ecd26d286e06762274a7
Parents: 575118a
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Jan 21 14:33:27 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu Jan 21 14:33:27 2016 -0800

----------------------------------------------------------------------
 newtmgr/newtmgr.go         | 67 ++++++++++++++++++++++++++++++++++
 newtmgr/protocol/config.go | 79 +++++++++++++++++++++++++++++++++++++++++
 newtmgr/protocol/nmgr.go   |  1 +
 3 files changed, 147 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a4d23590/newtmgr/newtmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/newtmgr.go b/newtmgr/newtmgr.go
index e5a7a53..3899e1d 100644
--- a/newtmgr/newtmgr.go
+++ b/newtmgr/newtmgr.go
@@ -541,6 +541,72 @@ func imageCmd() *cobra.Command {
 	return imageCmd
 }
 
+func configRunCmd(cmd *cobra.Command, args []string) {
+	if len(args) < 1 {
+		nmUsage(cmd, util.NewNewtError("Need variable name"))
+	}
+	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)
+	}
+
+	config, err := protocol.NewConfig()
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+
+	config.Name = args[0]
+	if len(args) > 1 {
+		config.Value = args[1]
+	}
+	nmr, err := config.EncodeRequest()
+	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)
+	}
+
+	configRsp, err := protocol.DecodeConfigResponse(rsp.Data)
+	if err != nil {
+		nmUsage(cmd, err)
+	}
+	if configRsp.Value != "" {
+		fmt.Printf("Value: %s\n", configRsp.Value)
+	}
+}
+
+func configCmd() *cobra.Command {
+	statsCmd := &cobra.Command{
+		Use:   "config",
+		Short: "Read or write config value on target",
+		Run:   configRunCmd,
+	}
+
+	return statsCmd
+}
+
 func parseCmds() *cobra.Command {
 	nmCmd := &cobra.Command{
 		Use:   "newtmgr",
@@ -560,6 +626,7 @@ func parseCmds() *cobra.Command {
 	nmCmd.AddCommand(echoCmd())
 	nmCmd.AddCommand(imageCmd())
 	nmCmd.AddCommand(statsCmd())
+	nmCmd.AddCommand(configCmd())
 
 	return nmCmd
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a4d23590/newtmgr/protocol/config.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/config.go b/newtmgr/protocol/config.go
new file mode 100644
index 0000000..9b59fea
--- /dev/null
+++ b/newtmgr/protocol/config.go
@@ -0,0 +1,79 @@
+/*
+ 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/json"
+
+	"fmt"
+	"git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/util"
+)
+
+type Config struct {
+	Name  string `json:"name"`
+	Value string `json:"val"`
+}
+
+func NewConfig() (*Config, error) {
+	c := &Config{}
+	return c, nil
+}
+
+func (c *Config) EncodeRequest() (*NmgrReq, error) {
+	nmr, err := NewNmgrReq()
+	if err != nil {
+		return nil, err
+	}
+
+	nmr.Op = NMGR_OP_WRITE
+	nmr.Flags = 0
+	nmr.Group = NMGR_GROUP_ID_CONFIG
+	nmr.Id = 0
+	nmr.Len = 0
+
+	if c.Value == "" {
+		type ConfigReadReq struct {
+			Name string `json:"name"`
+		}
+
+		readReq := &ConfigReadReq{
+			Name: c.Name,
+		}
+		data, _ := json.Marshal(readReq)
+		nmr.Data = data
+		nmr.Op = NMGR_OP_READ
+	} else {
+		data, _ := json.Marshal(c)
+		nmr.Data = data
+	}
+	nmr.Len = uint16(len(nmr.Data))
+	return nmr, nil
+}
+
+func DecodeConfigResponse(data []byte) (*Config, error) {
+	c := &Config{}
+
+	if len(data) == 0 {
+		return c, nil
+	}
+
+	err := json.Unmarshal(data, &c)
+	if err != nil {
+		return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
+			err.Error()))
+	}
+	return c, nil
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/a4d23590/newtmgr/protocol/nmgr.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/nmgr.go b/newtmgr/protocol/nmgr.go
index 731b86b..9954347 100644
--- a/newtmgr/protocol/nmgr.go
+++ b/newtmgr/protocol/nmgr.go
@@ -42,6 +42,7 @@ const (
 const (
 	NMGR_GROUP_ID_DEFAULT = 0
 	NMGR_GROUP_ID_IMAGE   = 1
+	NMGR_GROUP_ID_CONFIG  = 3
 )
 
 func NewNmgrReq() (*NmgrReq, error) {


[2/2] incubator-mynewt-newt git commit: Go formatting.

Posted by ma...@apache.org.
Go formatting.


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

Branch: refs/heads/master
Commit: 831c85757bdd864485877ddcf6b9092599922b94
Parents: a4d2359
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Thu Jan 21 14:34:54 2016 -0800
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Thu Jan 21 14:34:54 2016 -0800

----------------------------------------------------------------------
 newtmgr/protocol/imageupload.go | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/831c8575/newtmgr/protocol/imageupload.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/imageupload.go b/newtmgr/protocol/imageupload.go
index 6158d19..a3f87f4 100644
--- a/newtmgr/protocol/imageupload.go
+++ b/newtmgr/protocol/imageupload.go
@@ -23,8 +23,8 @@ import (
 )
 
 type ImageUpload struct {
-	Offset  uint32	`json:"off"`
-	Data []byte
+	Offset uint32 `json:"off"`
+	Data   []byte
 }
 
 func NewImageUpload() (*ImageUpload, error) {
@@ -36,7 +36,7 @@ func NewImageUpload() (*ImageUpload, error) {
 
 func (i *ImageUpload) EncodeWriteRequest() (*NmgrReq, error) {
 	type UploadReq struct {
-		Off uint32 `json:"off"`
+		Off  uint32 `json:"off"`
 		Data string `json:"data"`
 	}
 	nmr, err := NewNmgrReq()
@@ -50,8 +50,8 @@ func (i *ImageUpload) EncodeWriteRequest() (*NmgrReq, error) {
 	nmr.Id = IMGMGR_NMGR_OP_UPLOAD
 
 	uploadReq := &UploadReq{
-		Off : i.Offset,
-		Data : base64.StdEncoding.EncodeToString(i.Data),
+		Off:  i.Offset,
+		Data: base64.StdEncoding.EncodeToString(i.Data),
 	}
 	data, _ := json.Marshal(uploadReq)
 	nmr.Len = uint16(len(data))