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 7bdec147ddcfd211858ec8ca8fcb1327e6c3a24b
Author: Christopher Collins <ccoll...@apache.org>
AuthorDate: Mon Jan 29 16:05:27 2018 -0800

    stat_mgmt command handler group.
---
 cmd/CMakeLists.txt                               |   1 +
 cmd/Kconfig                                      |   1 +
 cmd/stat_mgmt/CMakeLists.txt                     |   9 ++
 cmd/stat_mgmt/Kconfig                            |  36 +++++
 cmd/stat_mgmt/include/stat_mgmt/stat_mgmt.h      |  50 +++++++
 cmd/stat_mgmt/include/stat_mgmt/stat_mgmt_impl.h |  70 ++++++++++
 cmd/stat_mgmt/port/zephyr/src/zephyr_stat_mgmt.c |  97 ++++++++++++++
 cmd/stat_mgmt/src/stat_mgmt.c                    | 160 +++++++++++++++++++++++
 cmd/stat_mgmt/src/stat_mgmt_config.h             |  41 ++++++
 cmd/stat_mgmt/src/stubs.c                        |  40 ++++++
 mgmt/include/mgmt/mgmt.h                         |   2 +-
 11 files changed, 506 insertions(+), 1 deletion(-)

diff --git a/cmd/CMakeLists.txt b/cmd/CMakeLists.txt
index be26ded..a9d5968 100644
--- a/cmd/CMakeLists.txt
+++ b/cmd/CMakeLists.txt
@@ -2,3 +2,4 @@ add_subdirectory_ifdef(CONFIG_MCUMGR_CMD_FS_MGMT   fs_mgmt)
 add_subdirectory_ifdef(CONFIG_MCUMGR_CMD_IMG_MGMT  img_mgmt)
 add_subdirectory_ifdef(CONFIG_MCUMGR_CMD_LOG_MGMT  log_mgmt)
 add_subdirectory_ifdef(CONFIG_MCUMGR_CMD_OS_MGMT   os_mgmt)
+add_subdirectory_ifdef(CONFIG_MCUMGR_CMD_STAT_MGMT stat_mgmt)
diff --git a/cmd/Kconfig b/cmd/Kconfig
index ea51804..e404d2d 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -21,5 +21,6 @@ source "ext/mcumgr/cmd/fs_mgmt/Kconfig"
 source "ext/mcumgr/cmd/img_mgmt/Kconfig"
 source "ext/mcumgr/cmd/log_mgmt/Kconfig"
 source "ext/mcumgr/cmd/os_mgmt/Kconfig"
+source "ext/mcumgr/cmd/stat_mgmt/Kconfig"
 
 endmenu
diff --git a/cmd/stat_mgmt/CMakeLists.txt b/cmd/stat_mgmt/CMakeLists.txt
new file mode 100644
index 0000000..6dc954a
--- /dev/null
+++ b/cmd/stat_mgmt/CMakeLists.txt
@@ -0,0 +1,9 @@
+target_include_directories(MCUMGR INTERFACE 
+    include
+)
+
+zephyr_library_sources(
+    cmd/stat_mgmt/port/zephyr/src/zephyr_stat_mgmt.c
+    cmd/stat_mgmt/src/stat_mgmt.c
+    cmd/stat_mgmt/src/stubs.c
+)
diff --git a/cmd/stat_mgmt/Kconfig b/cmd/stat_mgmt/Kconfig
new file mode 100644
index 0000000..ffda20f
--- /dev/null
+++ b/cmd/stat_mgmt/Kconfig
@@ -0,0 +1,36 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE log
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this log
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this log 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.
+
+menuconfig MCUMGR_CMD_STAT_MGMT
+    bool
+    prompt "Enable mcumgr handlers for statistics management"
+    depends on STATS
+    default n
+    help
+      Enables mcumgr handlers for statistics management.
+
+if MCUMGR_CMD_STAT_MGMT
+config STAT_MGMT_MAX_NAME_LEN
+    int
+    prompt "Maximum stat group name length"
+    default 32
+    help
+      Limits the maximum stat group name length in mcumgr requests.  A buffer
+      of this size gets allocated on the stack during handling of all stat read
+      commands.  If a stat group's name exceeds this limit, it will be
+      impossible to retrieve its values with a stat show command.
+endif
diff --git a/cmd/stat_mgmt/include/stat_mgmt/stat_mgmt.h 
b/cmd/stat_mgmt/include/stat_mgmt/stat_mgmt.h
new file mode 100644
index 0000000..f0f76fb
--- /dev/null
+++ b/cmd/stat_mgmt/include/stat_mgmt/stat_mgmt.h
@@ -0,0 +1,50 @@
+/*
+ * 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_STAT_MGMT_
+#define H_STAT_MGMT_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * Command IDs for statistics management group.
+ */
+#define STAT_MGMT_ID_SHOW   0
+#define STAT_MGMT_ID_LIST   1
+
+/**
+ * @brief Represents a single value in a statistics group.
+ */
+struct stat_mgmt_entry {
+    const char *name;
+    uint64_t value;
+};
+
+/**
+ * @brief Registers the statistics management command handler group.
+ */ 
+void stat_mgmt_register_group(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* H_STAT_MGMT_ */
diff --git a/cmd/stat_mgmt/include/stat_mgmt/stat_mgmt_impl.h 
b/cmd/stat_mgmt/include/stat_mgmt/stat_mgmt_impl.h
new file mode 100644
index 0000000..d60707a
--- /dev/null
+++ b/cmd/stat_mgmt/include/stat_mgmt/stat_mgmt_impl.h
@@ -0,0 +1,70 @@
+/*
+ * 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.
+ */
+
+/**
+ * @file
+ * @brief Declares implementation-specific functions required by statistics
+ *        management.  The default stubs can be overridden with functions that
+ *        are compatible with the host OS.
+ */
+
+#ifndef H_STAT_MGMT_IMPL_
+#define H_STAT_MGMT_IMPL_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct stat_mgmt_entry;
+
+typedef int stat_mgmt_foreach_entry_fn(struct stat_mgmt_entry *entry,
+                                       void *arg);
+
+/**
+ * @brief Retrieves the name of the stat group at the specified index.
+ *
+ * @param idx                   The index of the stat group to retrieve.
+ * @param out_name              On success, the name of the requested stat
+ *                                  group gets written here.
+ *
+ * @return                      0 on success;
+ *                              MGMT_ERR_ENOENT if no group with the specified
+ *                                  index exists;
+ *                              Other MGMT_ERR_[...] code on failure.
+ */
+int stat_mgmt_impl_get_group(int idx, const char **out_name);
+
+/**
+ * @brief Applies a function to every entry in the specified stat group.
+ *
+ * @param group_name            The name of the stat group to operate on.
+ * @param cb                    The callback to apply to each stat entry.
+ * @param arg                   An optional argument to pass to the callback.
+ *
+ * @return                      0 on success; MGMT_ERR_[...] code on failure.
+ */
+int stat_mgmt_impl_foreach_entry(const char *group_name,
+                                 stat_mgmt_foreach_entry_fn *cb,
+                                 void *arg);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/cmd/stat_mgmt/port/zephyr/src/zephyr_stat_mgmt.c 
b/cmd/stat_mgmt/port/zephyr/src/zephyr_stat_mgmt.c
new file mode 100644
index 0000000..dd0ba0b
--- /dev/null
+++ b/cmd/stat_mgmt/port/zephyr/src/zephyr_stat_mgmt.c
@@ -0,0 +1,97 @@
+/*
+ * 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 <misc/util.h>
+#include "stats.h"
+#include "mgmt/mgmt.h"
+#include "stat_mgmt/stat_mgmt.h"
+#include "stat_mgmt/stat_mgmt_impl.h"
+
+struct zephyr_stat_mgmt_walk_arg {
+    stat_mgmt_foreach_entry_fn *cb;
+    void *arg;
+};
+
+int
+stat_mgmt_impl_get_group(int idx, const char **out_name)
+{
+    const struct stats_hdr *cur;
+    int i;
+
+    cur = NULL;
+    for (i = 0; i <= idx; i++) {
+        cur = stats_group_get_next(cur);
+        if (cur == NULL) {
+            return MGMT_ERR_ENOENT;
+        }
+    }
+
+    *out_name = cur->s_name;
+    return 0;
+}
+
+static int
+zephyr_stat_mgmt_walk_cb(struct stats_hdr *hdr, void *arg,
+                         const char *name, uint16_t off)
+{
+    struct zephyr_stat_mgmt_walk_arg *walk_arg;
+    struct stat_mgmt_entry entry;
+    void *stat_val;
+
+    walk_arg = arg;
+
+    stat_val = (uint8_t *)hdr + off;
+    switch (hdr->s_size) {
+    case sizeof (uint16_t):
+        entry.value = *(uint16_t *) stat_val;
+        break;
+    case sizeof (uint32_t):
+        entry.value = *(uint32_t *) stat_val;
+        break;
+    case sizeof (uint64_t):
+        entry.value = *(uint64_t *) stat_val;
+        break;
+    default:
+        return MGMT_ERR_EUNKNOWN;
+    }
+    entry.name = name;
+
+    return walk_arg->cb(&entry, walk_arg->arg);
+}
+
+int
+stat_mgmt_impl_foreach_entry(const char *group_name,
+                             stat_mgmt_foreach_entry_fn *cb,
+                             void *arg)
+{
+    struct zephyr_stat_mgmt_walk_arg walk_arg;
+    struct stats_hdr *hdr;
+
+    hdr = stats_group_find(group_name);
+    if (hdr == NULL) {
+        return MGMT_ERR_ENOENT;
+    }
+
+    walk_arg = (struct zephyr_stat_mgmt_walk_arg) {
+        .cb = cb,
+        .arg = arg,
+    };
+
+    return stats_walk(hdr, zephyr_stat_mgmt_walk_cb, &walk_arg);
+}
diff --git a/cmd/stat_mgmt/src/stat_mgmt.c b/cmd/stat_mgmt/src/stat_mgmt.c
new file mode 100644
index 0000000..7eac3f9
--- /dev/null
+++ b/cmd/stat_mgmt/src/stat_mgmt.c
@@ -0,0 +1,160 @@
+/*
+ * 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 <string.h>
+#include <stdio.h>
+
+#include "mgmt/mgmt.h"
+#include "cborattr/cborattr.h"
+#include "stat_mgmt/stat_mgmt.h"
+#include "stat_mgmt/stat_mgmt_impl.h"
+#include "stat_mgmt_config.h"
+
+static mgmt_handler_fn stat_mgmt_show;
+static mgmt_handler_fn stat_mgmt_list;
+
+static struct mgmt_handler stat_mgmt_handlers[] = {
+    [STAT_MGMT_ID_SHOW] = { stat_mgmt_show, NULL },
+    [STAT_MGMT_ID_LIST] = { stat_mgmt_list, NULL },
+};
+
+#define STAT_MGMT_HANDLER_CNT \
+    sizeof stat_mgmt_handlers / sizeof stat_mgmt_handlers[0]
+
+static struct mgmt_group stat_mgmt_group = {
+    .mg_handlers = stat_mgmt_handlers,
+    .mg_handlers_count = STAT_MGMT_HANDLER_CNT,
+    .mg_group_id = MGMT_GROUP_ID_STAT,
+};
+
+static int
+stat_mgmt_cb_encode(struct stat_mgmt_entry *entry, void *arg)
+{
+    CborEncoder *enc;
+    CborError err;
+
+    enc = arg;
+
+    err = 0;
+    err |= cbor_encode_text_stringz(enc, entry->name);
+    err |= cbor_encode_uint(enc, entry->value);
+
+    if (err != 0) {
+        return MGMT_ERR_ENOMEM;
+    }
+
+    return 0;
+}
+
+/**
+ * Command handler: stat show
+ */
+static int
+stat_mgmt_show(struct mgmt_ctxt *ctxt)
+{
+    char stat_name[CONFIG_STAT_MGMT_MAX_NAME_LEN];
+    CborEncoder map_enc;
+    CborError err;
+    int rc;
+
+    struct cbor_attr_t attrs[] = {
+        {
+            .attribute = "name",
+            .type = CborAttrTextStringType,
+            .addr.string = stat_name,
+            .len = sizeof(stat_name)
+        },
+        { NULL },
+    };
+
+    err = cbor_read_object(&ctxt->it, attrs);
+    if (err != 0) {
+        return MGMT_ERR_EINVAL;
+    }
+
+    err |= cbor_encode_text_stringz(&ctxt->encoder, "rc");
+    err |= cbor_encode_int(&ctxt->encoder, MGMT_ERR_EOK);
+
+    err |= cbor_encode_text_stringz(&ctxt->encoder, "name");
+    err |= cbor_encode_text_stringz(&ctxt->encoder, stat_name);
+
+    err |= cbor_encode_text_stringz(&ctxt->encoder, "fields");
+    err |= cbor_encoder_create_map(&ctxt->encoder, &map_enc,
+                                   CborIndefiniteLength);
+
+    rc = stat_mgmt_impl_foreach_entry(stat_name, stat_mgmt_cb_encode,
+                                      &map_enc);
+    if (rc != 0) {
+        return rc;
+    }
+
+    err |= cbor_encoder_close_container(&ctxt->encoder, &map_enc);
+
+    if (err != 0) {
+        return MGMT_ERR_ENOMEM;
+    }
+
+    return 0;
+}
+
+/**
+ * Command handler: stat list
+ */
+static int
+stat_mgmt_list(struct mgmt_ctxt *ctxt)
+{
+    const char *group_name;
+    CborEncoder arr_enc;
+    CborError err;
+    int rc;
+    int i;
+
+    err = 0;
+    err |= cbor_encode_text_stringz(&ctxt->encoder, "stat_list");
+    err |= cbor_encoder_create_array(&ctxt->encoder, &arr_enc,
+                                     CborIndefiniteLength);
+
+    /* Iterate the list of stat groups, encoding each group's name in the CBOR
+     * array.
+     */
+    for (i = 0; ; i++) {
+        rc = stat_mgmt_impl_get_group(i, &group_name);
+        if (rc == MGMT_ERR_ENOENT) {
+            /* No more stat groups. */
+            break;
+        } else if (rc != 0) {
+            /* Error. */
+            return rc;
+        }
+
+        err |= cbor_encode_text_stringz(&ctxt->encoder, group_name);
+    }
+    err |= cbor_encoder_close_container(&ctxt->encoder, &arr_enc);
+
+    if (err != 0) {
+        return MGMT_ERR_ENOMEM;
+    }
+    return 0;
+}
+
+void
+stat_mgmt_register_group(void)
+{
+    mgmt_register_group(&stat_mgmt_group);
+}
diff --git a/cmd/stat_mgmt/src/stat_mgmt_config.h 
b/cmd/stat_mgmt/src/stat_mgmt_config.h
new file mode 100644
index 0000000..6c7cbca
--- /dev/null
+++ b/cmd/stat_mgmt/src/stat_mgmt_config.h
@@ -0,0 +1,41 @@
+/*
+ * 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_STAT_MGMT_CONFIG_
+#define H_STAT_MGMT_CONFIG_
+
+#if defined MYNEWT
+
+#include "syscfg/syscfg.h"
+
+#define STAT_MGMT_MAX_NAME_LEN  MYNEWT_VAL(STAT_MGMT_MAX_NAME_LEN)
+
+#elif defined __ZEPHYR__
+
+#define STAT_MGMT_MAX_NAME_LEN  CONFIG_STAT_MGMT_MAX_NAME_LEN
+
+#else
+
+/* No direct support for this OS.  The application needs to define the above
+ * settings itself.
+ */
+
+#endif
+
+#endif
diff --git a/cmd/stat_mgmt/src/stubs.c b/cmd/stat_mgmt/src/stubs.c
new file mode 100644
index 0000000..906056f
--- /dev/null
+++ b/cmd/stat_mgmt/src/stubs.c
@@ -0,0 +1,40 @@
+/*
+ * 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.
+ */
+
+/**
+ * These stubs get linked in when there is no equivalent OS-specific
+ * implementation.
+ */
+
+#include "mgmt/mgmt.h"
+#include "stat_mgmt/stat_mgmt_impl.h"
+
+int __attribute__((weak))
+stat_mgmt_impl_get_group(int idx, const char **out_name)
+{
+    return MGMT_ERR_ENOTSUP;
+}
+
+int __attribute__((weak))
+stat_mgmt_impl_foreach_entry(const char *stat_name,
+                             stat_mgmt_foreach_entry_fn *cb,
+                             void *arg)
+{
+    return MGMT_ERR_ENOTSUP;
+}
diff --git a/mgmt/include/mgmt/mgmt.h b/mgmt/include/mgmt/mgmt.h
index 2762aa8..8303d04 100644
--- a/mgmt/include/mgmt/mgmt.h
+++ b/mgmt/include/mgmt/mgmt.h
@@ -39,7 +39,7 @@ extern "C" {
  */
 #define MGMT_GROUP_ID_OS        0
 #define MGMT_GROUP_ID_IMAGE     1
-#define MGMT_GROUP_ID_STATS     2
+#define MGMT_GROUP_ID_STAT      2
 #define MGMT_GROUP_ID_CONFIG    3
 #define MGMT_GROUP_ID_LOG       4
 #define MGMT_GROUP_ID_CRASH     5

-- 
To stop receiving notification emails like this one, please contact
ccoll...@apache.org.

Reply via email to