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 2018/01/05 20:43:42 UTC

[mynewt-mcumgr] 05/05: OS command group

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-mcumgr.git

commit 79f78f0e932e8dee55e8afe7dc09c4caf22e31fa
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Fri Jan 5 10:39:32 2018 -0800

    OS command group
---
 cmd/os_mgmt/Makefile                         |  17 ++
 cmd/os_mgmt/include/os_mgmt/os_mgmt.h        |  45 ++++
 cmd/os_mgmt/include/os_mgmt/os_mgmt_impl.h   |   6 +
 cmd/os_mgmt/port/mynewt/src/mynewt_os_mgmt.c |  37 ++++
 cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c |  23 ++
 cmd/os_mgmt/src/os_mgmt.c                    | 320 +++++++++++++++++++++++++++
 6 files changed, 448 insertions(+)

diff --git a/cmd/os_mgmt/Makefile b/cmd/os_mgmt/Makefile
new file mode 100644
index 0000000..8b6287f
--- /dev/null
+++ b/cmd/os_mgmt/Makefile
@@ -0,0 +1,17 @@
+#
+# Copyright (c) 2017 Intel Corporation
+#
+# SPDX-License-Identifier: Apache-2.0
+#
+
+PREFIX ?= .
+OBJ_DIR ?= $(PREFIX)/obj
+LIB_DIR ?= $(PREFIX)/lib
+
+all:
+	mkdir -p $(OBJ_DIR) $(LIB_DIR)
+	$(CC) -c $(CFLAGS) -Iinclude -I$(CURDIR)/../tinycbor/src -I$(CURDIR)/../cborattr/include -I$(CURDIR)/../mcumgr/mgmt/include src/mgmt_os.c -o $(OBJ_DIR)/mgmt_os.o
+	$(AR) -rcs $(LIB_DIR)/libmgmt_os.a $(OBJ_DIR)/mgmt_os.o
+
+clean:
+	rm -rf $(OBJ_DIR) $(LIB_DIR)
diff --git a/cmd/os_mgmt/include/os_mgmt/os_mgmt.h b/cmd/os_mgmt/include/os_mgmt/os_mgmt.h
new file mode 100644
index 0000000..6037946
--- /dev/null
+++ b/cmd/os_mgmt/include/os_mgmt/os_mgmt.h
@@ -0,0 +1,45 @@
+/*
+ * 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.
+ */
+
+#ifndef H_OS_MGMT_
+#define H_OS_MGMT_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct mgmt_cbuf;
+
+/*
+ * Command IDs for OS management group.
+ */
+#define OS_MGMT_ID_ECHO             0
+#define OS_MGMT_ID_CONS_ECHO_CTRL   1
+#define OS_MGMT_ID_TASKSTATS        2
+#define OS_MGMT_ID_MPSTATS          3
+#define OS_MGMT_ID_DATETIME_STR     4
+#define OS_MGMT_ID_RESET            5
+
+int os_mgmt_group_register(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_OS_MGMT_ */
diff --git a/cmd/os_mgmt/include/os_mgmt/os_mgmt_impl.h b/cmd/os_mgmt/include/os_mgmt/os_mgmt_impl.h
new file mode 100644
index 0000000..e3b3308
--- /dev/null
+++ b/cmd/os_mgmt/include/os_mgmt/os_mgmt_impl.h
@@ -0,0 +1,6 @@
+#ifndef H_OS_MGMT_IMPL_
+#define H_OS_MGMT_IMPL_
+
+int os_mgmt_impl_reset(void);
+
+#endif
diff --git a/cmd/os_mgmt/port/mynewt/src/mynewt_os_mgmt.c b/cmd/os_mgmt/port/mynewt/src/mynewt_os_mgmt.c
new file mode 100644
index 0000000..9763ce9
--- /dev/null
+++ b/cmd/os_mgmt/port/mynewt/src/mynewt_os_mgmt.c
@@ -0,0 +1,37 @@
+#include "hal/hal_system.h"
+#include "hal/hal_watchdog.h"
+#include "os/os.h"
+#if MYNEWT_VAL(LOG_SOFT_RESET)
+#include "reboot/log_reboot.h"
+#endif
+#include "os_mgmt/os_mgmt_impl.h"
+
+static struct os_callout mynewt_os_mgmt_reset_callout;
+
+static void
+mynewt_os_mgmt_reset_tmo(struct os_event *ev)
+{
+    /*
+     * Tickle watchdog just before re-entering bootloader.
+     * Depending on what system has been doing lately, watchdog
+     * timer might be close to firing.
+     */
+    hal_watchdog_tickle();
+    hal_system_reset();
+}
+
+int
+os_mgmt_impl_reset(void)
+{
+    int rc;
+
+    os_callout_init(&mynewt_os_mgmt_reset_callout, mgmt_evq_get(),
+                    nmgr_reset_tmo, NULL);
+
+#if MYNEWT_VAL(LOG_SOFT_RESET)
+    log_reboot(HAL_RESET_REQUESTED);
+#endif
+    os_callout_reset(&nmgr_reset_callout, OS_TICKS_PER_SEC / 4);
+
+    return 0;
+}
diff --git a/cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c b/cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c
new file mode 100644
index 0000000..95e9b8f
--- /dev/null
+++ b/cmd/os_mgmt/port/zephyr/src/zephyr_os_mgmt.c
@@ -0,0 +1,23 @@
+#include <zephyr.h>
+#include <misc/reboot.h>
+#include "os_mgmt/os_mgmt_impl.h"
+
+static void zephyr_os_mgmt_reset_cb(struct k_timer *timer);
+
+static K_TIMER_DEFINE(zephyr_os_mgmt_reset_timer,
+                      zephyr_os_mgmt_reset_cb, NULL);
+
+
+static void
+zephyr_os_mgmt_reset_cb(struct k_timer *timer)
+{
+    sys_reboot(SYS_REBOOT_WARM);
+}
+
+int
+os_mgmt_impl_reset(void)
+{
+    k_timer_start(&zephyr_os_mgmt_reset_timer, K_MSEC(250), 0);
+
+    return 0;
+}
diff --git a/cmd/os_mgmt/src/os_mgmt.c b/cmd/os_mgmt/src/os_mgmt.c
new file mode 100644
index 0000000..5429d74
--- /dev/null
+++ b/cmd/os_mgmt/src/os_mgmt.c
@@ -0,0 +1,320 @@
+/*
+ * 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.
+ */
+
+#include <assert.h>
+#include <string.h>
+#include "mgmt/mgmt.h"
+#include "os_mgmt/os_mgmt.h"
+#include "os_mgmt/os_mgmt_impl.h"
+#include "cbor.h"
+#include "cborattr/cborattr.h"
+
+static mgmt_handler_fn os_mgmt_echo;
+static mgmt_handler_fn os_mgmt_reset;
+
+#if 0
+static int os_mgmt_console_echo(struct mgmt_cbuf *);
+static int os_mgmt_taskstat_read(struct mgmt_cbuf *njb);
+static int os_mgmt_mpstat_read(struct mgmt_cbuf *njb);
+static int os_mgmt_datetime_get(struct mgmt_cbuf *njb);
+static int os_mgmt_datetime_set(struct mgmt_cbuf *njb);
+#endif
+
+static const struct mgmt_handler os_mgmt_group_handlers[] = {
+    [OS_MGMT_ID_ECHO] = {
+        os_mgmt_echo, os_mgmt_echo
+    },
+#if 0
+    [OS_MGMT_ID_CONS_ECHO_CTRL] = {
+        os_mgmt_console_echo, os_mgmt_console_echo
+    },
+    [OS_MGMT_ID_TASKSTATS] = {
+        os_mgmt_taskstat_read, NULL
+    },
+    [OS_MGMT_ID_MPSTATS] = {
+        os_mgmt_mpstat_read, NULL
+    },
+    [OS_MGMT_ID_DATETIME_STR] = {
+        os_mgmt_datetime_get, os_mgmt_datetime_set
+    },
+#endif
+    [OS_MGMT_ID_RESET] = {
+        NULL, os_mgmt_reset
+    },
+};
+
+#define OS_MGMT_GROUP_SZ    \
+    (sizeof os_mgmt_group_handlers / sizeof os_mgmt_group_handlers[0])
+
+static struct mgmt_group os_mgmt_group = {
+    .mg_handlers = os_mgmt_group_handlers,
+    .mg_handlers_count = OS_MGMT_GROUP_SZ,
+    .mg_group_id = MGMT_GROUP_ID_OS,
+};
+
+static int
+os_mgmt_echo(struct mgmt_cbuf *cb)
+{
+    char echo_buf[128] = {'\0'};
+    CborError g_err = CborNoError;
+
+    const struct cbor_attr_t attrs[2] = {
+        [0] = {
+            .attribute = "d",
+            .type = CborAttrTextStringType,
+            .addr.string = echo_buf,
+            .nodefault = 1,
+            .len = sizeof(echo_buf),
+        },
+        [1] = {
+            .attribute = NULL
+        }
+    };
+
+    g_err |= cbor_encode_text_stringz(&cb->encoder, "r");
+    g_err |= cbor_read_object(&cb->it, attrs);
+    g_err |= cbor_encode_text_string(&cb->encoder, echo_buf, strlen(echo_buf));
+
+    if (g_err) {
+        return MGMT_ERR_ENOMEM;
+    }
+    return (0);
+}
+
+#if 0
+static int
+os_mgmt_console_echo(struct mgmt_cbuf *cb)
+{
+    long long int echo_on = 1;
+    int rc;
+    const struct cbor_attr_t attrs[2] = {
+        [0] = {
+            .attribute = "echo",
+            .type = CborAttrIntegerType,
+            .addr.integer = &echo_on,
+            .nodefault = 1
+        },
+        [1] = { 0 },
+    };
+
+    rc = cbor_read_object(&cb->it, attrs);
+    if (rc) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    if (echo_on) {
+        console_echo(1);
+    } else {
+        console_echo(0);
+    }
+    return (0);
+}
+
+static int
+os_mgmt_taskstat_read(struct mgmt_cbuf *cb)
+{
+    struct os_task *prev_task;
+    struct os_task_info oti;
+    CborError g_err = CborNoError;
+    CborEncoder tasks;
+    CborEncoder task;
+
+    g_err |= cbor_encode_text_stringz(&cb->encoder, "rc");
+    g_err |= cbor_encode_int(&cb->encoder, MGMT_ERR_EOK);
+    g_err |= cbor_encode_text_stringz(&cb->encoder, "tasks");
+    g_err |= cbor_encoder_create_map(&cb->encoder, &tasks,
+                                     CborIndefiniteLength);
+
+    prev_task = NULL;
+    while (1) {
+        prev_task = os_task_info_get_next(prev_task, &oti);
+        if (prev_task == NULL) {
+            break;
+        }
+
+        g_err |= cbor_encode_text_stringz(&tasks, oti.oti_name);
+        g_err |= cbor_encoder_create_map(&tasks, &task, CborIndefiniteLength);
+        g_err |= cbor_encode_text_stringz(&task, "prio");
+        g_err |= cbor_encode_uint(&task, oti.oti_prio);
+        g_err |= cbor_encode_text_stringz(&task, "tid");
+        g_err |= cbor_encode_uint(&task, oti.oti_taskid);
+        g_err |= cbor_encode_text_stringz(&task, "state");
+        g_err |= cbor_encode_uint(&task, oti.oti_state);
+        g_err |= cbor_encode_text_stringz(&task, "stkuse");
+        g_err |= cbor_encode_uint(&task, oti.oti_stkusage);
+        g_err |= cbor_encode_text_stringz(&task, "stksiz");
+        g_err |= cbor_encode_uint(&task, oti.oti_stksize);
+        g_err |= cbor_encode_text_stringz(&task, "cswcnt");
+        g_err |= cbor_encode_uint(&task, oti.oti_cswcnt);
+        g_err |= cbor_encode_text_stringz(&task, "runtime");
+        g_err |= cbor_encode_uint(&task, oti.oti_runtime);
+        g_err |= cbor_encode_text_stringz(&task, "last_checkin");
+        g_err |= cbor_encode_uint(&task, oti.oti_last_checkin);
+        g_err |= cbor_encode_text_stringz(&task, "next_checkin");
+        g_err |= cbor_encode_uint(&task, oti.oti_next_checkin);
+        g_err |= cbor_encoder_close_container(&tasks, &task);
+    }
+    g_err |= cbor_encoder_close_container(&cb->encoder, &tasks);
+
+    if (g_err) {
+        return MGMT_ERR_ENOMEM;
+    }
+    return (0);
+}
+
+static int
+os_mgmt_mpstat_read(struct mgmt_cbuf *cb)
+{
+    struct os_mempool *prev_mp;
+    struct os_mempool_info omi;
+    CborError g_err = CborNoError;
+    CborEncoder pools;
+    CborEncoder pool;
+
+    g_err |= cbor_encode_text_stringz(&cb->encoder, "rc");
+    g_err |= cbor_encode_int(&cb->encoder, MGMT_ERR_EOK);
+    g_err |= cbor_encode_text_stringz(&cb->encoder, "mpools");
+    g_err |= cbor_encoder_create_map(&cb->encoder, &pools,
+                                     CborIndefiniteLength);
+
+    prev_mp = NULL;
+    while (1) {
+        prev_mp = os_mempool_info_get_next(prev_mp, &omi);
+        if (prev_mp == NULL) {
+            break;
+        }
+
+        g_err |= cbor_encode_text_stringz(&pools, omi.omi_name);
+        g_err |= cbor_encoder_create_map(&pools, &pool, CborIndefiniteLength);
+        g_err |= cbor_encode_text_stringz(&pool, "blksiz");
+        g_err |= cbor_encode_uint(&pool, omi.omi_block_size);
+        g_err |= cbor_encode_text_stringz(&pool, "nblks");
+        g_err |= cbor_encode_uint(&pool, omi.omi_num_blocks);
+        g_err |= cbor_encode_text_stringz(&pool, "nfree");
+        g_err |= cbor_encode_uint(&pool, omi.omi_num_free);
+        g_err |= cbor_encode_text_stringz(&pool, "min");
+        g_err |= cbor_encode_uint(&pool, omi.omi_min_free);
+        g_err |= cbor_encoder_close_container(&pools, &pool);
+    }
+
+    g_err |= cbor_encoder_close_container(&cb->encoder, &pools);
+
+    if (g_err) {
+        return MGMT_ERR_ENOMEM;
+    }
+    return (0);
+}
+
+static int
+os_mgmt_datetime_get(struct mgmt_cbuf *cb)
+{
+    struct os_timeval tv;
+    struct os_timezone tz;
+    char buf[DATETIME_BUFSIZE];
+    int rc;
+    CborError g_err = CborNoError;
+
+    g_err |= cbor_encode_text_stringz(&cb->encoder, "rc");
+    g_err |= cbor_encode_int(&cb->encoder, MGMT_ERR_EOK);
+
+    /* Display the current datetime */
+    rc = os_gettimeofday(&tv, &tz);
+    assert(rc == 0);
+    rc = datetime_format(&tv, &tz, buf, DATETIME_BUFSIZE);
+    if (rc) {
+        rc = MGMT_ERR_EINVAL;
+        goto err;
+    }
+    g_err |= cbor_encode_text_stringz(&cb->encoder, "datetime");
+    g_err |= cbor_encode_text_stringz(&cb->encoder, buf);
+
+    if (g_err) {
+        return MGMT_ERR_ENOMEM;
+    }
+    return 0;
+
+err:
+    return (rc);
+}
+
+static int
+os_mgmt_datetime_set(struct mgmt_cbuf *cb)
+{
+    struct os_timeval tv;
+    struct os_timezone tz;
+    char buf[DATETIME_BUFSIZE];
+    int rc = 0;
+    const struct cbor_attr_t datetime_write_attr[] = {
+        [0] = {
+            .attribute = "datetime",
+            .type = CborAttrTextStringType,
+            .addr.string = buf,
+            .len = sizeof(buf),
+        },
+        { 0 },
+    };
+
+    rc = cbor_read_object(&cb->it, datetime_write_attr);
+    if (rc) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    /* Set the current datetime */
+    rc = datetime_parse(buf, &tv, &tz);
+    if (!rc) {
+        rc = os_settimeofday(&tv, &tz);
+        if (rc) {
+          return MGMT_ERR_EINVAL;
+        }
+    } else {
+        return MGMT_ERR_EINVAL;
+    }
+
+    rc = mgmt_cbuf_setoerr(cb, 0);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}
+
+static void
+os_mgmt_reset_tmo(struct os_event *ev)
+{
+    /*
+     * Tickle watchdog just before re-entering bootloader.
+     * Depending on what system has been doing lately, watchdog
+     * timer might be close to firing.
+     */
+    hal_watchdog_tickle();
+    hal_system_reset();
+}
+#endif
+
+static int
+os_mgmt_reset(struct mgmt_cbuf *cb)
+{
+    return os_mgmt_impl_reset();
+}
+
+int
+os_mgmt_group_register(void)
+{
+    return mgmt_group_register(&os_mgmt_group);
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@mynewt.apache.org" <co...@mynewt.apache.org>.