You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/04/12 04:29:19 UTC
incubator-mynewt-newt git commit: Adding newtmgr support for date set
and get
Repository: incubator-mynewt-newt
Updated Branches:
refs/heads/develop bf7889773 -> 2d80b5230
Adding newtmgr support for date set and get
Adding newtmgr support for datetime command
- For writing datetime <argument>
Need to specify a datetime in RFC 3339 format
2016-03-02T22:44:00 UTC time (implicit)
2016-03-02T22:44:00Z UTC time (explicit)
2016-03-02T22:44:00-08:00 PST timezone
2016-03-02T22:44:00.1 fractional seconds
2016-03-02T22:44:00.101+05:30 fractional seconds with timezone
- For reading it back specify datetime without arguments.
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/2d80b523
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/tree/2d80b523
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/diff/2d80b523
Branch: refs/heads/develop
Commit: 2d80b5230cc533c61f4467b0f385e4f2b73659d0
Parents: bf78897
Author: Vipul Rahane <vi...@runtime.io>
Authored: Mon Apr 11 00:43:42 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Mon Apr 11 11:00:57 2016 -0700
----------------------------------------------------------------------
newtmgr/cli/commands.go | 1 +
newtmgr/cli/datetime.go | 111 ++++++++++++++++++++++++++++++++++++++
newtmgr/protocol/datetime.go | 84 +++++++++++++++++++++++++++++
newtmgr/protocol/defs.go | 1 +
4 files changed, 197 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/2d80b523/newtmgr/cli/commands.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/commands.go b/newtmgr/cli/commands.go
index 5a4f090..29d2d86 100644
--- a/newtmgr/cli/commands.go
+++ b/newtmgr/cli/commands.go
@@ -60,6 +60,7 @@ func Commands() *cobra.Command {
nmCmd.AddCommand(mempoolStatsCmd())
nmCmd.AddCommand(configCmd())
nmCmd.AddCommand(logsCmd())
+ nmCmd.AddCommand(dTimeCmd())
return nmCmd
}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/2d80b523/newtmgr/cli/datetime.go
----------------------------------------------------------------------
diff --git a/newtmgr/cli/datetime.go b/newtmgr/cli/datetime.go
new file mode 100644
index 0000000..da0a2a9
--- /dev/null
+++ b/newtmgr/cli/datetime.go
@@ -0,0 +1,111 @@
+/**
+ * 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 cli
+
+import (
+ "fmt"
+
+ "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"
+)
+
+func dateTimeCmd(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(cmd, err)
+ }
+
+ runner, err := protocol.NewCmdRunner(conn)
+ if err != nil {
+ nmUsage(cmd, err)
+ }
+
+ dateTime, err := protocol.NewDateTime()
+ if err != nil {
+ nmUsage(cmd, err)
+ }
+
+ if len(args) > 0 {
+ dateTime.DateTime = args[0]
+ }
+ nmr, err := dateTime.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)
+ }
+
+ iRsp, err := protocol.DecodeDateTimeResponse(rsp.Data)
+ if err != nil {
+ nmUsage(cmd, err)
+ }
+
+ err_str := "Command: datetime\n" +
+ "For writing datetime <argument>\n" +
+ "Need to specify a datetime in RFC 3339 format\n" +
+ "2016-03-02T22:44:00 UTC time (implicit)\n" +
+ "2016-03-02T22:44:00Z UTC time (explicit)\n" +
+ "2016-03-02T22:44:00-08:00 PST timezone\n" +
+ "2016-03-02T22:44:00.1 fractional seconds\n" +
+ "2016-03-02T22:44:00.101+05:30 fractional seconds with timezone\n"
+
+ if len(args) > 1 {
+ nmUsage(cmd, util.NewNewtError(err_str))
+ } else if len(args) == 1 {
+ if iRsp.Return != 0 {
+ nmUsage(cmd, util.NewNewtError(fmt.Sprintf("Return:%d\n%s",
+ iRsp.Return, err_str)))
+ } else {
+ fmt.Println("Return:", iRsp.Return)
+ }
+ } else if len(args) == 0 {
+ fmt.Println("Datetime(RFC 3339 format):", iRsp.DateTime)
+ }
+}
+
+func dTimeCmd() *cobra.Command {
+ dateTCmd := &cobra.Command{
+ Use: "datetime",
+ Short: "Manage datetime on the device",
+ Run: dateTimeCmd,
+ }
+
+ return dateTCmd
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/2d80b523/newtmgr/protocol/datetime.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/datetime.go b/newtmgr/protocol/datetime.go
new file mode 100644
index 0000000..808fe14
--- /dev/null
+++ b/newtmgr/protocol/datetime.go
@@ -0,0 +1,84 @@
+/**
+ * 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 DateTime struct {
+ DateTime string `json:"datetime"`
+ Return uint64 `json:"rc"`
+}
+
+func NewDateTime() (*DateTime, error) {
+ dt := &DateTime{}
+ return dt, nil
+}
+
+func (i *DateTime) EncodeRequest() (*NmgrReq, error) {
+ nmr, err := NewNmgrReq()
+ if err != nil {
+ return nil, err
+ }
+
+ nmr.Len = 0
+ nmr.Op = NMGR_OP_WRITE
+ nmr.Flags = 0
+ nmr.Group = NMGR_GROUP_ID_DEFAULT
+ nmr.Id = NMGR_ID_DATETIME_STR
+
+ if i.DateTime != "" {
+ data, _ := json.Marshal(i)
+ nmr.Data = data
+ nmr.Len = uint16(len(data))
+ } else {
+ type DateTimeReq struct {
+ DtStr string `json:"datetime"`
+ }
+
+ dtReq := &DateTimeReq{
+ DtStr: i.DateTime,
+ }
+
+ nmr.Op = NMGR_OP_READ
+ data, _ := json.Marshal(dtReq)
+ nmr.Data = data
+ nmr.Len = uint16(len(data))
+ }
+ return nmr, nil
+}
+
+func DecodeDateTimeResponse(data []byte) (*DateTime, error) {
+ i := &DateTime{}
+
+ 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()))
+ }
+ return i, nil
+}
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-newt/blob/2d80b523/newtmgr/protocol/defs.go
----------------------------------------------------------------------
diff --git a/newtmgr/protocol/defs.go b/newtmgr/protocol/defs.go
index 6d3a5f1..efb2a05 100644
--- a/newtmgr/protocol/defs.go
+++ b/newtmgr/protocol/defs.go
@@ -34,4 +34,5 @@ const (
NMGR_ID_CONS_ECHO_CTRL = 1
NMGR_ID_TASKSTATS = 2
NMGR_ID_MPSTATS = 3
+ NMGR_ID_DATETIME_STR = 4
)