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:25:57 UTC

incubator-mynewt-core git commit: Adding string date get and set command handler

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 199d3941d -> 8c8018438


Adding string date get and set command handler

This command takes Date format as per 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


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/8c801843
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/8c801843
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/8c801843

Branch: refs/heads/develop
Commit: 8c8018438c5e781d706a9944f102a16ef01e6696
Parents: 199d394
Author: Vipul Rahane <vi...@runtime.io>
Authored: Mon Apr 11 00:33:14 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Mon Apr 11 19:25:51 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr/include/newtmgr/newtmgr.h | 11 ++--
 libs/newtmgr/src/newtmgr.c             | 57 +++++++++---------
 libs/newtmgr/src/newtmgr_os.c          | 91 +++++++++++++++++++++++++++--
 3 files changed, 121 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c801843/libs/newtmgr/include/newtmgr/newtmgr.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/include/newtmgr/newtmgr.h b/libs/newtmgr/include/newtmgr/newtmgr.h
index 79ee70d..346a61c 100644
--- a/libs/newtmgr/include/newtmgr/newtmgr.h
+++ b/libs/newtmgr/include/newtmgr/newtmgr.h
@@ -6,7 +6,7 @@
  * 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,
@@ -20,7 +20,7 @@
 #ifndef _NEWTMGR_H_
 #define _NEWTMGR_H_
 
-#include <json/json.h> 
+#include <json/json.h>
 #include <inttypes.h>
 #include <os/os.h>
 
@@ -40,24 +40,25 @@
 #define NMGR_OP_WRITE_RSP       (3)
 
 
-/** 
+/**
  * Newtmgr JSON error codes
  */
 #define NMGR_ERR_EOK      (0)
 #define NMGR_ERR_EUNKNOWN (1)
 #define NMGR_ERR_ENOMEM   (2)
 #define NMGR_ERR_EINVAL   (3)
-#define NMGR_ERR_ETIMEOUT (4) 
+#define NMGR_ERR_ETIMEOUT (4)
 #define NMGR_ERR_EPERUSER (256)
 
 
 /*
  * Id's for default group commands
  */
-#define NMGR_ID_ECHO	        0
+#define NMGR_ID_ECHO	          0
 #define NMGR_ID_CONS_ECHO_CTRL  1
 #define NMGR_ID_TASKSTATS       2
 #define NMGR_ID_MPSTATS         3
+#define NMGR_ID_DATETIME_STR    4
 
 struct nmgr_hdr {
     uint8_t nh_op;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c801843/libs/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/src/newtmgr.c b/libs/newtmgr/src/newtmgr.c
index 6cefd9c..78f74ce 100644
--- a/libs/newtmgr/src/newtmgr.c
+++ b/libs/newtmgr/src/newtmgr.c
@@ -35,7 +35,7 @@ struct os_mutex g_nmgr_group_list_lock;
 struct os_eventq g_nmgr_evq;
 struct os_task g_nmgr_task;
 
-STAILQ_HEAD(, nmgr_group) g_nmgr_group_list = 
+STAILQ_HEAD(, nmgr_group) g_nmgr_group_list =
     STAILQ_HEAD_INITIALIZER(g_nmgr_group_list);
 
 static int nmgr_def_echo(struct nmgr_jbuf *);
@@ -45,6 +45,8 @@ static int nmgr_def_console_echo(struct nmgr_jbuf *);
 int nmgr_def_taskstat_read(struct nmgr_jbuf *);
 int nmgr_def_mpstat_read(struct nmgr_jbuf *);
 int nmgr_def_logs_read(struct nmgr_jbuf *);
+int nmgr_datetime_get(struct nmgr_jbuf *njb);
+int nmgr_datetime_set(struct nmgr_jbuf *njb);
 
 static struct nmgr_group nmgr_def_group;
 /* ORDER MATTERS HERE.
@@ -55,6 +57,7 @@ static const struct nmgr_handler nmgr_def_group_handlers[] = {
     [NMGR_ID_CONS_ECHO_CTRL] = {nmgr_def_console_echo, nmgr_def_console_echo},
     [NMGR_ID_TASKSTATS] = {nmgr_def_taskstat_read, NULL},
     [NMGR_ID_MPSTATS] = {nmgr_def_mpstat_read, NULL},
+    [NMGR_ID_DATETIME_STR] = {nmgr_datetime_get, nmgr_datetime_set},
 };
 
 /* JSON buffer for NMGR task
@@ -66,7 +69,7 @@ nmgr_def_echo(struct nmgr_jbuf *njb)
 {
     uint8_t echo_buf[128];
     struct json_attr_t attrs[] = {
-        { "d", t_string, .addr.string = (char *) &echo_buf[0], 
+        { "d", t_string, .addr.string = (char *) &echo_buf[0],
             .len = sizeof(echo_buf) },
         { NULL },
     };
@@ -137,7 +140,7 @@ err:
     return (rc);
 }
 
-int 
+int
 nmgr_group_list_unlock(void)
 {
     int rc;
@@ -248,7 +251,7 @@ err:
     return (rc);
 }
 
-static char 
+static char
 nmgr_jbuf_read_next(struct json_buffer *jb)
 {
     struct nmgr_jbuf *njb;
@@ -270,7 +273,7 @@ nmgr_jbuf_read_next(struct json_buffer *jb)
     return (c);
 }
 
-static char 
+static char
 nmgr_jbuf_read_prev(struct json_buffer *jb)
 {
     struct nmgr_jbuf *njb;
@@ -292,7 +295,7 @@ nmgr_jbuf_read_prev(struct json_buffer *jb)
     return (c);
 }
 
-static int 
+static int
 nmgr_jbuf_readn(struct json_buffer *jb, char *buf, int size)
 {
     struct nmgr_jbuf *njb;
@@ -301,7 +304,7 @@ nmgr_jbuf_readn(struct json_buffer *jb, char *buf, int size)
     int rc;
 
     njb = (struct nmgr_jbuf *) jb;
-   
+
     left = njb->njb_end - njb->njb_off;
     read = size > left ? left : size;
 
@@ -315,7 +318,7 @@ err:
     return (rc);
 }
 
-int 
+int
 nmgr_jbuf_write(void *arg, char *data, int len)
 {
     struct nmgr_jbuf *njb;
@@ -334,7 +337,7 @@ err:
     return (rc);
 }
 
-int 
+int
 nmgr_jbuf_init(struct nmgr_jbuf *njb)
 {
     memset(njb, 0, sizeof(*njb));
@@ -343,13 +346,13 @@ nmgr_jbuf_init(struct nmgr_jbuf *njb)
     njb->njb_buf.jb_read_prev = nmgr_jbuf_read_prev;
     njb->njb_buf.jb_readn = nmgr_jbuf_readn;
     njb->njb_enc.je_write = nmgr_jbuf_write;
-    njb->njb_enc.je_arg = njb; 
+    njb->njb_enc.je_arg = njb;
 
     return (0);
 }
 
-static int 
-nmgr_jbuf_setibuf(struct nmgr_jbuf *njb, struct os_mbuf *m, 
+static int
+nmgr_jbuf_setibuf(struct nmgr_jbuf *njb, struct os_mbuf *m,
         uint16_t off, uint16_t len)
 {
     njb->njb_off = off;
@@ -360,7 +363,7 @@ nmgr_jbuf_setibuf(struct nmgr_jbuf *njb, struct os_mbuf *m,
     return (0);
 }
 
-static int 
+static int
 nmgr_jbuf_setobuf(struct nmgr_jbuf *njb, struct nmgr_hdr *hdr,
         struct os_mbuf *m)
 {
@@ -370,9 +373,9 @@ nmgr_jbuf_setobuf(struct nmgr_jbuf *njb, struct nmgr_hdr *hdr,
     return (0);
 }
 
-int 
+int
 nmgr_jbuf_setoerr(struct nmgr_jbuf *njb, int errcode)
-{   
+{
     struct json_value jv;
 
     json_encode_object_start(&njb->njb_enc);
@@ -383,7 +386,7 @@ nmgr_jbuf_setoerr(struct nmgr_jbuf *njb, int errcode)
     return (0);
 }
 
-static int 
+static int
 nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
 {
     struct os_mbuf *rsp;
@@ -426,7 +429,7 @@ nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
         /* Build response header apriori.  Then pass to the handlers
          * to fill out the response data, and adjust length & flags.
          */
-        rsp_hdr = (struct nmgr_hdr *) os_mbuf_extend(rsp, 
+        rsp_hdr = (struct nmgr_hdr *) os_mbuf_extend(rsp,
                 sizeof(struct nmgr_hdr));
         if (!rsp_hdr) {
             rc = OS_EINVAL;
@@ -434,7 +437,7 @@ nmgr_handle_req(struct nmgr_transport *nt, struct os_mbuf *req)
         }
         rsp_hdr->nh_len = 0;
         rsp_hdr->nh_flags = 0;
-        rsp_hdr->nh_op = (hdr.nh_op == NMGR_OP_READ) ? NMGR_OP_READ_RSP : 
+        rsp_hdr->nh_op = (hdr.nh_op == NMGR_OP_READ) ? NMGR_OP_READ_RSP :
             NMGR_OP_WRITE_RSP;
         rsp_hdr->nh_group = hdr.nh_group;
         rsp_hdr->nh_id = hdr.nh_id;
@@ -523,7 +526,7 @@ nmgr_task(void *arg)
     }
 }
 
-int 
+int
 nmgr_transport_init(struct nmgr_transport *nt,
         nmgr_transport_out_func_t output_func)
 {
@@ -565,7 +568,7 @@ nmgr_rx_req(struct nmgr_transport *nt, struct os_mbuf *req)
     return rc;
 }
 
-static int 
+static int
 nmgr_shell_out(struct nmgr_transport *nt, struct os_mbuf *m)
 {
     int rc;
@@ -599,7 +602,7 @@ err:
 }
 
 
-static int 
+static int
 nmgr_default_groups_register(void)
 {
     int rc;
@@ -619,26 +622,26 @@ nmgr_default_groups_register(void)
 err:
     return (rc);
 }
-    
-int 
+
+int
 nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
 {
     int rc;
 
     os_eventq_init(&g_nmgr_evq);
-    
+
     rc = nmgr_transport_init(&g_nmgr_shell_transport, nmgr_shell_out);
     if (rc != 0) {
         goto err;
     }
 
-    rc = shell_nlip_input_register(nmgr_shell_in, 
+    rc = shell_nlip_input_register(nmgr_shell_in,
             (void *) &g_nmgr_shell_transport);
     if (rc != 0) {
         goto err;
     }
 
-    rc = os_task_init(&g_nmgr_task, "newtmgr", nmgr_task, NULL, prio, 
+    rc = os_task_init(&g_nmgr_task, "newtmgr", nmgr_task, NULL, prio,
             OS_WAIT_FOREVER, stack_ptr, stack_len);
     if (rc != 0) {
         goto err;
@@ -653,5 +656,3 @@ nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
 err:
     return (rc);
 }
-
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8c801843/libs/newtmgr/src/newtmgr_os.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/src/newtmgr_os.c b/libs/newtmgr/src/newtmgr_os.c
index d319b8c..6a61a41 100644
--- a/libs/newtmgr/src/newtmgr_os.c
+++ b/libs/newtmgr/src/newtmgr_os.c
@@ -6,7 +6,7 @@
  * 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,
@@ -23,10 +23,11 @@
 #include <assert.h>
 
 #include <newtmgr/newtmgr.h>
+#include <util/datetime.h>
 
 #include <string.h>
 
-int 
+int
 nmgr_def_taskstat_read(struct nmgr_jbuf *njb)
 {
     struct os_task *prev_task;
@@ -76,13 +77,13 @@ nmgr_def_taskstat_read(struct nmgr_jbuf *njb)
     return (0);
 }
 
-int 
+int
 nmgr_def_taskstat_write(struct nmgr_jbuf *njb)
 {
     return (OS_EINVAL);
 }
 
-int 
+int
 nmgr_def_mpstat_read(struct nmgr_jbuf *njb)
 {
     struct os_mempool *prev_mp;
@@ -121,8 +122,88 @@ nmgr_def_mpstat_read(struct nmgr_jbuf *njb)
     return (0);
 }
 
-int 
+int
 nmgr_def_mpstat_write(struct nmgr_jbuf *njb)
 {
     return (OS_EINVAL);
 }
+
+int
+nmgr_datetime_get(struct nmgr_jbuf *njb)
+{
+    struct os_timeval tv;
+    struct os_timezone tz;
+    char buf[DATETIME_BUFSIZE];
+    struct json_value jv;
+    int rc;
+
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+
+    /* Display the current datetime */
+    rc = os_gettimeofday(&tv, &tz);
+    assert(rc == 0);
+    rc = format_datetime(&tv, &tz, buf, DATETIME_BUFSIZE);
+    if (rc) {
+        rc = OS_EINVAL;
+        goto err;
+    }
+
+    JSON_VALUE_STRING(&jv, buf)
+    json_encode_object_entry(&njb->njb_enc, "datetime", &jv);
+    json_encode_object_finish(&njb->njb_enc);
+
+    return OS_OK;
+err:
+    return (rc);
+}
+
+int
+nmgr_datetime_set(struct nmgr_jbuf *njb)
+{
+    struct os_timeval tv;
+    struct os_timezone tz;
+    struct json_value jv;
+    char buf[DATETIME_BUFSIZE];
+    int rc = OS_OK;
+    const struct json_attr_t datetime_write_attr[2] = {
+        [0] = {
+            .attribute = "datetime",
+            .type = t_string,
+            .addr.string = buf,
+            .len = sizeof(buf),
+        },
+        [1] = {
+            .attribute = "rc",
+            .type = t_uinteger,
+
+        }
+    };
+
+    rc = json_read_object(&njb->njb_buf, datetime_write_attr);
+    if (rc) {
+        rc = OS_EINVAL;
+        goto out;
+    }
+
+    /* Set the current datetime */
+    rc = parse_datetime(buf, &tv, &tz);
+    if (!rc) {
+        rc = os_settimeofday(&tv, &tz);
+        if (rc) {
+          rc = OS_EINVAL;
+          goto out;
+        }
+    } else {
+        rc = OS_EINVAL;
+        goto out;
+    }
+
+out:
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_INT(&jv, rc);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+    json_encode_object_finish(&njb->njb_enc);
+    return OS_OK;
+}