You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2026/10/12 16:26:21 UTC
(mynewt-core) 12/24: mgmt/mcumgr/log_mgmt: Remove OS abstraction layer
This is an automated email from the ASF dual-hosted git repository.
sjanc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit efd89f86186c688a6215d07a33256e0883de21aa
Author: Szymon Janc <sz...@codecoup.pl>
AuthorDate: Tue Oct 6 19:11:35 2026 +0200
mgmt/mcumgr/log_mgmt: Remove OS abstraction layer
mcumgr is now part of Mynewt core and there is no need for OS
abstraction layer. Mynewt implementation is merged into log_mgmt.c and
log_mgmt_impl_* functions are made static (log_mgmt_impl_clear is
renamed to log_mgmt_flush_log as log_mgmt_clear is already used by
command handler). log_mgmt_config.h is removed and syscfg values are
used directly. Weak stubs and Mynewt port package are removed.
Settings and package init are moved from port package to log_mgmt
package.
No functional changes.
Assisted-by: Claude:claude-opus-5-5
---
.../cmd/log_mgmt/include/log_mgmt/log_mgmt.h | 7 +-
.../log_mgmt/include/log_mgmt/log_mgmt_config.h | 50 ----
.../cmd/log_mgmt/include/log_mgmt/log_mgmt_impl.h | 131 ---------
mgmt/mcumgr/cmd/log_mgmt/pkg.yml | 4 +-
mgmt/mcumgr/cmd/log_mgmt/port/mynewt/pkg.yml | 31 ---
.../cmd/log_mgmt/port/mynewt/src/mynewt_log_mgmt.c | 295 --------------------
mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c | 305 +++++++++++++++++++--
mgmt/mcumgr/cmd/log_mgmt/src/stubs.c | 71 -----
.../cmd/log_mgmt/{port/mynewt => }/syscfg.yml | 0
9 files changed, 295 insertions(+), 599 deletions(-)
diff --git a/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt.h b/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt.h
index 1494e6426..b4eef3eda 100644
--- a/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt.h
+++ b/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt.h
@@ -24,7 +24,7 @@
extern "C" {
#endif
-#include "log_mgmt_config.h"
+#include "syscfg/syscfg.h"
/**
* LOG MGMT specific error codes, 0 -> 6, 8 are same as mcumgr,
@@ -69,11 +69,14 @@ extern "C" {
#define LOG_MGMT_ETYPE_CBOR 1
#define LOG_MGMT_ETYPE_BINARY 2
+/* Length of image hash included in log entries */
+#define LOG_MGMT_IMG_HASHLEN 4
+
/** @brief Generic descriptor for an OS-specific log. */
struct log_mgmt_log {
const char *name;
int type;
-#if !LOG_MGMT_GLOBAL_IDX
+#if !MYNEWT_VAL(LOG_GLOBAL_IDX)
uint32_t index;
#endif
};
diff --git a/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt_config.h b/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt_config.h
deleted file mode 100644
index 37d02f9ae..000000000
--- a/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt_config.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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_LOG_MGMT_CONFIG_
-#define H_LOG_MGMT_CONFIG_
-
-#if defined MYNEWT
-
-#include "syscfg/syscfg.h"
-
-#define LOG_MGMT_CHUNK_LEN MYNEWT_VAL(LOG_MGMT_CHUNK_LEN)
-#define LOG_MGMT_NAME_LEN MYNEWT_VAL(LOG_MGMT_NAME_LEN)
-#define LOG_MGMT_MAX_RSP_LEN MYNEWT_VAL(LOG_MGMT_MAX_RSP_LEN)
-#define LOG_MGMT_READ_WATERMARK_UPDATE MYNEWT_VAL(LOG_READ_WATERMARK_UPDATE)
-#define LOG_MGMT_GLOBAL_IDX MYNEWT_VAL(LOG_GLOBAL_IDX)
-#define LOG_MGMT_IMG_HASHLEN 4
-
-#elif defined __ZEPHYR__
-
-#define LOG_MGMT_CHUNK_LEN CONFIG_LOG_MGMT_CHUNK_LEN
-#define LOG_MGMT_NAME_LEN CONFIG_LOG_MGMT_NAME_LEN
-#define LOG_MGMT_MAX_RSP_LEN CONFIG_LOG_MGMT_MAX_RSP_LEN
-#define LOG_MGMT_READ_WATERMARK_UPDATE CONFIG_LOG_READ_WATERMARK_UPDATE
-#define LOG_MGMT_GLOBAL_IDX CONFIG_LOG_GLOBAL_IDX
-
-#else
-
-/* No direct support for this OS. The application needs to define the above
- * settings itself.
- */
-
-#endif
-
-#endif
diff --git a/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt_impl.h b/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt_impl.h
deleted file mode 100644
index 81b65d343..000000000
--- a/mgmt/mcumgr/cmd/log_mgmt/include/log_mgmt/log_mgmt_impl.h
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * 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 log
- * management. The default stubs can be overridden with functions that
- * are compatible with the host OS.
- */
-
-#ifndef H_LOG_MGMT_IMPL_
-#define H_LOG_MGMT_IMPL_
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-struct log_mgmt_filter;
-struct log_mgmt_entry;
-struct log_mgmt_log;
-
-typedef int log_mgmt_foreach_entry_fn(struct log_mgmt_entry *entry,
- void *arg);
-
-/**
- * @brief Retrieves the log at the specified index.
- *
- * @param idx The index of the log to retrieve.
- * @param out_name On success, the requested log gets written
- * here.
- *
- * @return 0 on success;
- * MGMT_ERR_ENOENT if no log with the specified
- * index exists;
- * Other MGMT_ERR_[...] code on failure.
- */
-int log_mgmt_impl_get_log(int idx, struct log_mgmt_log *out_log);
-
-/**
- * @brief Retrieves the name of log module at the specified index.
- *
- * @param idx The index of the log module to retrieve.
- * @param out_name On success, the requested module's name gets
- * written here.
- *
- * @return 0 on success;
- * MGMT_ERR_ENOENT if no log module with the
- * specified index exists;
- * Other MGMT_ERR_[...] code on failure.
- */
-int log_mgmt_impl_get_module(int idx, const char **out_module_name);
-
-/**
- * @brief Retrieves the name of log level at the specified index.
- *
- * @param idx The index of the log level to retrieve.
- * @param out_name On success, the requested level's name gets
- * written here.
- *
- * @return 0 on success;
- * MGMT_ERR_ENOENT if no log level with the
- * specified index exists;
- * Other MGMT_ERR_[...] code on failure.
- */
-int log_mgmt_impl_get_level(int idx, const char **out_level_name);
-
-/**
- * @brief Retrieves the index that the next log entry will use.
- *
- * @param out_idx On success, the next index gets written here.
- *
- * @return 0 on success; MGMT_ERR_[...] code on failure.
- */
-int log_mgmt_impl_get_next_idx(uint32_t *out_idx);
-
-/**
- * @brief Applies a function to every matching entry in the specified log.
- *
- * @param log_name The name of the log to operate on.
- * @param filter Specifies which log entries to operate on.
- * @param cb The callback to apply to each log entry.
- * @param arg An optional argument to pass to the callback.
- *
- * @return 0 on success; MGMT_ERR_[...] code on failure.
- */
-int log_mgmt_impl_foreach_entry(const char *log_name,
- const struct log_mgmt_filter *filter,
- log_mgmt_foreach_entry_fn *cb,
- void *arg);
-
-/**
- * @brief Clear the log with the specified name.
- *
- * @param log_name The name of the log to clear.
- *
- * @return 0 on success; MGMT_ERR_[...] code on failure.
- */
-int log_mgmt_impl_clear(const char *log_name);
-
-/**
- * @brief set watermark for specified log index
- *
- * @param log Log pointer
- * @param index Log ndex
- *
- * @return 0 on success, non-zero on failure
- */
-int
-log_mgmt_impl_set_watermark(const struct log_mgmt_log *log, int index);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
diff --git a/mgmt/mcumgr/cmd/log_mgmt/pkg.yml b/mgmt/mcumgr/cmd/log_mgmt/pkg.yml
index 71943ce24..2d0d357d0 100644
--- a/mgmt/mcumgr/cmd/log_mgmt/pkg.yml
+++ b/mgmt/mcumgr/cmd/log_mgmt/pkg.yml
@@ -26,5 +26,7 @@ pkg.keywords:
pkg.deps:
- '@apache-mynewt-core/kernel/os'
- '@apache-mynewt-core/sys/log/full'
- - '@apache-mynewt-core/mgmt/mcumgr/cmd/log_mgmt/port/mynewt'
- '@apache-mynewt-core/mgmt/mcumgr/mgmt'
+
+pkg.init:
+ log_mgmt_module_init: 'MYNEWT_VAL(LOG_MGMT_SYSINIT_STAGE)'
diff --git a/mgmt/mcumgr/cmd/log_mgmt/port/mynewt/pkg.yml b/mgmt/mcumgr/cmd/log_mgmt/port/mynewt/pkg.yml
deleted file mode 100644
index 27912e8d4..000000000
--- a/mgmt/mcumgr/cmd/log_mgmt/port/mynewt/pkg.yml
+++ /dev/null
@@ -1,31 +0,0 @@
-#
-# 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.
-#
-
-pkg.name: mgmt/mcumgr/cmd/log_mgmt/port/mynewt
-pkg.description: 'Log management command handlers for mcumgr.'
-pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-
-pkg.deps:
- - '@apache-mynewt-core/mgmt/mcumgr/cmd/log_mgmt'
- - '@apache-mynewt-core/mgmt/mcumgr/mgmt'
-
-pkg.init:
- log_mgmt_module_init: 'MYNEWT_VAL(LOG_MGMT_SYSINIT_STAGE)'
diff --git a/mgmt/mcumgr/cmd/log_mgmt/port/mynewt/src/mynewt_log_mgmt.c b/mgmt/mcumgr/cmd/log_mgmt/port/mynewt/src/mynewt_log_mgmt.c
deleted file mode 100644
index 587e9c36f..000000000
--- a/mgmt/mcumgr/cmd/log_mgmt/port/mynewt/src/mynewt_log_mgmt.c
+++ /dev/null
@@ -1,295 +0,0 @@
-/*
- * 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 "log/log.h"
-#include "mgmt/mgmt.h"
-#include "log_mgmt/log_mgmt.h"
-#include "log_mgmt/log_mgmt_impl.h"
-#include "log_mgmt/log_mgmt_config.h"
-
-struct mynewt_log_mgmt_walk_arg {
- log_mgmt_foreach_entry_fn *cb;
- uint8_t chunk[LOG_MGMT_CHUNK_LEN];
- void *arg;
-};
-
-static struct log *
-mynewt_log_mgmt_find_log(const char *log_name)
-{
- struct log *log;
-
- log = NULL;
- while (1) {
- log = log_list_get_next(log);
- if (log == NULL) {
- return NULL;
- }
-
- if (strcmp(log->l_name, log_name) == 0) {
- return log;
- }
- }
-}
-
-__attribute__((__unused__)) static int
-log_mgmt_mynewt_err_map(int mynewt_os_err)
-{
- switch (mynewt_os_err) {
- case OS_ENOENT:
- /* no break */
- case SYS_ENOENT:
- return LOG_MGMT_ERR_ENOENT;
- case OS_ENOMEM:
- /* no break */
- case SYS_ENOMEM:
- return LOG_MGMT_ERR_ENOMEM;
- case OS_OK:
- return LOG_MGMT_ERR_EOK;
- case OS_EINVAL:
- /* no break */
- case OS_INVALID_PARM:
- /* no break */
- case SYS_EINVAL:
- return LOG_MGMT_ERR_EINVAL;
- case SYS_ENOTSUP:
- return LOG_MGMT_ERR_ENOTSUP;
- default:
- return LOG_MGMT_ERR_EUNKNOWN;
- }
-}
-
-int
-log_mgmt_impl_set_watermark(const struct log_mgmt_log *log, int index)
-{
-#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
- struct log *tmplog;
-
- tmplog = mynewt_log_mgmt_find_log(log->name);
- if (tmplog) {
- return log_mgmt_mynewt_err_map(log_set_watermark(tmplog, index));
- } else {
- return LOG_MGMT_ERR_ENOENT;
- }
-#else
- return LOG_MGMT_ERR_ENOTSUP;
-#endif
-}
-
-int
-log_mgmt_impl_get_log(int idx, struct log_mgmt_log *out_log)
-{
- struct log *log;
- int i;
-
- log = NULL;
- for (i = 0; i <= idx; i++) {
- log = log_list_get_next(log);
- if (log == NULL) {
- return LOG_MGMT_ERR_ENOENT;
- }
- }
-
- out_log->name = log->l_name;
- out_log->type = log->l_log->log_type;
-#if !MYNEWT_VAL(LOG_GLOBAL_IDX)
- out_log->index = log->l_idx;
-#endif
- return 0;
-}
-
-int
-log_mgmt_impl_get_module(int idx, const char **out_module_name)
-{
- const char *name;
-
- name = LOG_MODULE_STR(idx);
- if (name == NULL) {
- return LOG_MGMT_ERR_ENOENT;
- } else {
- *out_module_name = name;
- return 0;
- }
-}
-
-int
-log_mgmt_impl_get_level(int idx, const char **out_level_name)
-{
- const char *name;
-
- if (idx >= LOG_LEVEL_MAX) {
- return LOG_MGMT_ERR_ENOENT;
- }
-
- name = LOG_LEVEL_STR(idx);
- if (!strcmp(name, "UNKNOWN")) {
- return LOG_MGMT_ERR_ENOENT;
- } else {
- *out_level_name = name;
- return 0;
- }
-}
-
-#if MYNEWT_VAL(LOG_GLOBAL_IDX)
-int
-log_mgmt_impl_get_next_idx(uint32_t *out_idx)
-{
- *out_idx = g_log_info.li_next_index;
- return 0;
-}
-#endif
-
-static int
-mynewt_log_mgmt_walk_cb(struct log *log, struct log_offset *log_offset,
- const struct log_entry_hdr *leh,
- const void *dptr, uint16_t len)
-{
- struct mynewt_log_mgmt_walk_arg *mynewt_log_mgmt_walk_arg;
- struct log_mgmt_entry entry;
- uint16_t trailer_len = 0;
- int read_len;
- int offset;
- int rc;
-
- rc = 0;
- mynewt_log_mgmt_walk_arg = log_offset->lo_arg;
-
- (void)trailer_len;
-
- /* If specified timestamp is nonzero, it is the primary criterion, and the
- * specified index is the secondary criterion. If specified timetsamp is
- * zero, specified index is the only criterion.
- *
- * If specified timestamp == 0: encode entries whose index >=
- * specified index.
- * Else: encode entries whose timestamp >= specified timestamp and whose
- * index >= specified index
- */
- if (log_offset->lo_ts == 0) {
- if (log_offset->lo_index > leh->ue_index) {
- return 0;
- }
- } else if (leh->ue_ts < log_offset->lo_ts ||
- (leh->ue_ts == log_offset->lo_ts &&
- leh->ue_index < log_offset->lo_index)) {
- return 0;
- }
-
-#if MYNEWT_VAL(LOG_FLAGS_TRAILER)
- trailer_len = log_read_trailer_len(log, dptr);
- if (trailer_len) {
- /* If trailer is present, so is the length of the trailer */
- trailer_len += LOG_TRAILER_LEN_SIZE;
- }
-
- len -= trailer_len;
-#endif
-
- entry.ts = leh->ue_ts;
- entry.index = leh->ue_index;
- entry.module = leh->ue_module;
- entry.level = leh->ue_level;
-
- entry.type = leh->ue_etype;
- entry.flags = leh->ue_flags;
- entry.imghash = (leh->ue_flags & LOG_FLAGS_IMG_HASH) ?
- leh->ue_imghash : NULL;
- entry.len = len;
- entry.data = mynewt_log_mgmt_walk_arg->chunk;
-
- for (offset = 0; offset < len; offset += LOG_MGMT_CHUNK_LEN) {
- if (len - offset < LOG_MGMT_CHUNK_LEN) {
- read_len = len - offset;
- } else {
- read_len = LOG_MGMT_CHUNK_LEN;
- }
- entry.offset = offset;
- entry.chunklen = read_len;
-
- rc = log_read_body(log, dptr, mynewt_log_mgmt_walk_arg->chunk, offset,
- read_len);
- if (rc < 0) {
- return LOG_MGMT_ERR_EUNKNOWN;
- }
- rc = mynewt_log_mgmt_walk_arg->cb(&entry, mynewt_log_mgmt_walk_arg->arg);
- if (rc) {
- break;
- }
- }
-
- return rc;
-}
-
-int
-log_mgmt_impl_foreach_entry(const char *log_name,
- const struct log_mgmt_filter *filter,
- log_mgmt_foreach_entry_fn *cb, void *arg)
-{
- struct mynewt_log_mgmt_walk_arg walk_arg;
- struct log_offset offset = {};
- struct log *log;
-
- walk_arg = (struct mynewt_log_mgmt_walk_arg) {
- .cb = cb,
- .arg = arg,
- };
-
- log = mynewt_log_mgmt_find_log(log_name);
- if (log == NULL) {
- return LOG_MGMT_ERR_ENOENT;
- }
-
- if (strcmp(log->l_name, log_name) == 0) {
- offset.lo_arg = &walk_arg;
- offset.lo_ts = filter->min_timestamp;
- offset.lo_index = filter->min_index;
- offset.lo_data_len = 0;
-
- return log_walk_body(log, mynewt_log_mgmt_walk_cb, &offset);
- }
-
- return LOG_MGMT_ERR_ENOENT;
-}
-
-int
-log_mgmt_impl_clear(const char *log_name)
-{
- struct log *log;
- int rc;
-
- log = mynewt_log_mgmt_find_log(log_name);
- if (log == NULL) {
- return LOG_MGMT_ERR_ENOENT;
- }
-
- rc = log_flush(log);
- if (rc != 0) {
- return LOG_MGMT_ERR_EUNKNOWN;
- }
-
- return 0;
-}
-
-void
-log_mgmt_module_init(void)
-{
- /* Ensure this function only gets called by sysinit. */
- SYSINIT_ASSERT_ACTIVE();
-
- log_mgmt_register_group();
-}
diff --git a/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c b/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c
index 4adcc4173..1850d3858 100644
--- a/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c
+++ b/mgmt/mcumgr/cmd/log_mgmt/src/log_mgmt.c
@@ -20,12 +20,11 @@
#include <string.h>
#include <stdio.h>
+#include "os/mynewt.h"
#include "mgmt/mgmt.h"
#include "cborattr/cborattr.h"
#include "tinycbor/cbor_cnt_writer.h"
#include "log_mgmt/log_mgmt.h"
-#include "log_mgmt/log_mgmt_impl.h"
-#include "log_mgmt/log_mgmt_config.h"
#include "log/log.h"
/* Log mgmt encoder context used for multiple calls of the
@@ -52,6 +51,267 @@ struct log_walk_ctxt {
struct log_mgmt_enc_ctxt lmec;
};
+typedef int log_mgmt_foreach_entry_fn(struct log_mgmt_entry *entry, void *arg);
+
+struct log_mgmt_walk_arg {
+ log_mgmt_foreach_entry_fn *cb;
+ uint8_t chunk[MYNEWT_VAL(LOG_MGMT_CHUNK_LEN)];
+ void *arg;
+};
+
+static struct log *
+log_mgmt_find_log(const char *log_name)
+{
+ struct log *log;
+
+ log = NULL;
+ while (1) {
+ log = log_list_get_next(log);
+ if (log == NULL) {
+ return NULL;
+ }
+
+ if (strcmp(log->l_name, log_name) == 0) {
+ return log;
+ }
+ }
+}
+
+__attribute__((__unused__)) static int
+log_mgmt_err_map(int mynewt_os_err)
+{
+ switch (mynewt_os_err) {
+ case OS_ENOENT:
+ /* no break */
+ case SYS_ENOENT:
+ return LOG_MGMT_ERR_ENOENT;
+ case OS_ENOMEM:
+ /* no break */
+ case SYS_ENOMEM:
+ return LOG_MGMT_ERR_ENOMEM;
+ case OS_OK:
+ return LOG_MGMT_ERR_EOK;
+ case OS_EINVAL:
+ /* no break */
+ case OS_INVALID_PARM:
+ /* no break */
+ case SYS_EINVAL:
+ return LOG_MGMT_ERR_EINVAL;
+ case SYS_ENOTSUP:
+ return LOG_MGMT_ERR_ENOTSUP;
+ default:
+ return LOG_MGMT_ERR_EUNKNOWN;
+ }
+}
+
+#if MYNEWT_VAL(LOG_READ_WATERMARK_UPDATE)
+static int
+log_mgmt_set_watermark(const struct log_mgmt_log *log, int index)
+{
+#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
+ struct log *tmplog;
+
+ tmplog = log_mgmt_find_log(log->name);
+ if (tmplog) {
+ return log_mgmt_err_map(log_set_watermark(tmplog, index));
+ } else {
+ return LOG_MGMT_ERR_ENOENT;
+ }
+#else
+ return LOG_MGMT_ERR_ENOTSUP;
+#endif
+}
+#endif
+
+static int
+log_mgmt_get_log(int idx, struct log_mgmt_log *out_log)
+{
+ struct log *log;
+ int i;
+
+ log = NULL;
+ for (i = 0; i <= idx; i++) {
+ log = log_list_get_next(log);
+ if (log == NULL) {
+ return LOG_MGMT_ERR_ENOENT;
+ }
+ }
+
+ out_log->name = log->l_name;
+ out_log->type = log->l_log->log_type;
+#if !MYNEWT_VAL(LOG_GLOBAL_IDX)
+ out_log->index = log->l_idx;
+#endif
+ return 0;
+}
+
+static int
+log_mgmt_get_module(int idx, const char **out_module_name)
+{
+ const char *name;
+
+ name = LOG_MODULE_STR(idx);
+ if (name == NULL) {
+ return LOG_MGMT_ERR_ENOENT;
+ } else {
+ *out_module_name = name;
+ return 0;
+ }
+}
+
+static int
+log_mgmt_get_level(int idx, const char **out_level_name)
+{
+ const char *name;
+
+ if (idx >= LOG_LEVEL_MAX) {
+ return LOG_MGMT_ERR_ENOENT;
+ }
+
+ name = LOG_LEVEL_STR(idx);
+ if (!strcmp(name, "UNKNOWN")) {
+ return LOG_MGMT_ERR_ENOENT;
+ } else {
+ *out_level_name = name;
+ return 0;
+ }
+}
+
+#if MYNEWT_VAL(LOG_GLOBAL_IDX)
+static int
+log_mgmt_get_next_idx(uint32_t *out_idx)
+{
+ *out_idx = g_log_info.li_next_index;
+ return 0;
+}
+#endif
+
+static int
+log_mgmt_walk_cb(struct log *log, struct log_offset *log_offset,
+ const struct log_entry_hdr *leh, const void *dptr, uint16_t len)
+{
+ struct log_mgmt_walk_arg *log_mgmt_walk_arg;
+ struct log_mgmt_entry entry;
+ uint16_t trailer_len = 0;
+ int read_len;
+ int offset;
+ int rc;
+
+ rc = 0;
+ log_mgmt_walk_arg = log_offset->lo_arg;
+
+ (void)trailer_len;
+
+ /* If specified timestamp is nonzero, it is the primary criterion, and the
+ * specified index is the secondary criterion. If specified timetsamp is
+ * zero, specified index is the only criterion.
+ *
+ * If specified timestamp == 0: encode entries whose index >=
+ * specified index.
+ * Else: encode entries whose timestamp >= specified timestamp and whose
+ * index >= specified index
+ */
+ if (log_offset->lo_ts == 0) {
+ if (log_offset->lo_index > leh->ue_index) {
+ return 0;
+ }
+ } else if (leh->ue_ts < log_offset->lo_ts ||
+ (leh->ue_ts == log_offset->lo_ts && leh->ue_index < log_offset->lo_index)) {
+ return 0;
+ }
+
+#if MYNEWT_VAL(LOG_FLAGS_TRAILER)
+ trailer_len = log_read_trailer_len(log, dptr);
+ if (trailer_len) {
+ /* If trailer is present, so is the length of the trailer */
+ trailer_len += LOG_TRAILER_LEN_SIZE;
+ }
+
+ len -= trailer_len;
+#endif
+
+ entry.ts = leh->ue_ts;
+ entry.index = leh->ue_index;
+ entry.module = leh->ue_module;
+ entry.level = leh->ue_level;
+
+ entry.type = leh->ue_etype;
+ entry.flags = leh->ue_flags;
+ entry.imghash = (leh->ue_flags & LOG_FLAGS_IMG_HASH) ? leh->ue_imghash : NULL;
+ entry.len = len;
+ entry.data = log_mgmt_walk_arg->chunk;
+
+ for (offset = 0; offset < len; offset += MYNEWT_VAL(LOG_MGMT_CHUNK_LEN)) {
+ if (len - offset < MYNEWT_VAL(LOG_MGMT_CHUNK_LEN)) {
+ read_len = len - offset;
+ } else {
+ read_len = MYNEWT_VAL(LOG_MGMT_CHUNK_LEN);
+ }
+ entry.offset = offset;
+ entry.chunklen = read_len;
+
+ rc = log_read_body(log, dptr, log_mgmt_walk_arg->chunk, offset, read_len);
+ if (rc < 0) {
+ return LOG_MGMT_ERR_EUNKNOWN;
+ }
+ rc = log_mgmt_walk_arg->cb(&entry, log_mgmt_walk_arg->arg);
+ if (rc) {
+ break;
+ }
+ }
+
+ return rc;
+}
+
+static int
+log_mgmt_foreach_entry(const char *log_name, const struct log_mgmt_filter *filter,
+ log_mgmt_foreach_entry_fn *cb, void *arg)
+{
+ struct log_mgmt_walk_arg walk_arg;
+ struct log_offset offset = {};
+ struct log *log;
+
+ walk_arg = (struct log_mgmt_walk_arg){
+ .cb = cb,
+ .arg = arg,
+ };
+
+ log = log_mgmt_find_log(log_name);
+ if (log == NULL) {
+ return LOG_MGMT_ERR_ENOENT;
+ }
+
+ if (strcmp(log->l_name, log_name) == 0) {
+ offset.lo_arg = &walk_arg;
+ offset.lo_ts = filter->min_timestamp;
+ offset.lo_index = filter->min_index;
+ offset.lo_data_len = 0;
+
+ return log_walk_body(log, log_mgmt_walk_cb, &offset);
+ }
+
+ return LOG_MGMT_ERR_ENOENT;
+}
+
+static int
+log_mgmt_flush_log(const char *log_name)
+{
+ struct log *log;
+ int rc;
+
+ log = log_mgmt_find_log(log_name);
+ if (log == NULL) {
+ return LOG_MGMT_ERR_ENOENT;
+ }
+
+ rc = log_flush(log);
+ if (rc != 0) {
+ return LOG_MGMT_ERR_EUNKNOWN;
+ }
+
+ return 0;
+}
+
static mgmt_handler_fn log_mgmt_show;
static mgmt_handler_fn log_mgmt_clear;
static mgmt_handler_fn log_mgmt_module_list;
@@ -217,14 +477,14 @@ log_mgmt_cb_encode(struct log_mgmt_entry *entry, void *arg)
* is just a single entry we add the generic too long message text.
*/
/* `+ 1` to account for the CBOR array terminator. */
- if (ctxt->rsp_len + entry_len + 1 > LOG_MGMT_MAX_RSP_LEN) {
+ if (ctxt->rsp_len + entry_len + 1 > MYNEWT_VAL(LOG_MGMT_MAX_RSP_LEN)) {
/*
* Is this just a single entry? If so, encode the generic error
* message in the "msg" field of the response
*/
if (ctxt->counter == 0) {
entry->type = LOG_ETYPE_STRING;
- snprintf((char *)entry->data, LOG_MGMT_CHUNK_LEN,
+ snprintf((char *)entry->data, MYNEWT_VAL(LOG_MGMT_CHUNK_LEN),
"error: entry too large (%zu bytes)", entry_len);
}
@@ -269,7 +529,7 @@ log_encode_entries(const struct log_mgmt_log *log, CborEncoder *enc,
err |= cbor_encoder_create_array(&cnt_encoder, &entries, CborIndefiniteLength);
err |= cbor_encoder_close_container(&cnt_encoder, &entries);
rsp_len = cbor_encode_bytes_written(enc) + cbor_encode_bytes_written(&cnt_encoder);
- if (rsp_len > LOG_MGMT_MAX_RSP_LEN) {
+ if (rsp_len > MYNEWT_VAL(LOG_MGMT_MAX_RSP_LEN)) {
/* No entries were processed, don't update watermark */
return LOG_MGMT_ERR_EUNKNOWN;
}
@@ -286,7 +546,7 @@ log_encode_entries(const struct log_mgmt_log *log, CborEncoder *enc,
.rsp_len = cbor_encode_bytes_written(enc),
};
- rc = log_mgmt_impl_foreach_entry(log->name, &filter, log_mgmt_cb_encode, &ctxt);
+ rc = log_mgmt_foreach_entry(log->name, &filter, log_mgmt_cb_encode, &ctxt);
if (rc < 0) {
/*
* If we receive negative error code from the walk function,
@@ -301,9 +561,9 @@ log_encode_entries(const struct log_mgmt_log *log, CborEncoder *enc,
return LOG_MGMT_ERR_ENOMEM;
}
-#if LOG_MGMT_READ_WATERMARK_UPDATE
+#if MYNEWT_VAL(LOG_READ_WATERMARK_UPDATE)
if (!rc || rc == LOG_MGMT_ERR_EUNKNOWN) {
- log_mgmt_impl_set_watermark(log, ctxt.last_enc_index);
+ log_mgmt_set_watermark(log, ctxt.last_enc_index);
}
#endif
return rc;
@@ -345,7 +605,7 @@ log_encode(const struct log_mgmt_log *log, CborEncoder *ctxt,
static int
log_mgmt_show(struct mgmt_ctxt *ctxt)
{
- char name[LOG_MGMT_NAME_LEN];
+ char name[MYNEWT_VAL(LOG_MGMT_NAME_LEN)];
struct log_mgmt_log log;
CborEncoder logs;
CborError err;
@@ -386,9 +646,9 @@ log_mgmt_show(struct mgmt_ctxt *ctxt)
name_len = strlen(name);
err = 0;
-#if LOG_MGMT_GLOBAL_IDX
+#if MYNEWT_VAL(LOG_GLOBAL_IDX)
/* Determine the index that the next log entry would use. */
- rc = log_mgmt_impl_get_next_idx(&next_idx);
+ rc = log_mgmt_get_next_idx(&next_idx);
if (rc != 0) {
return LOG_MGMT_ERR_EUNKNOWN;
}
@@ -405,7 +665,7 @@ log_mgmt_show(struct mgmt_ctxt *ctxt)
/* Iterate list of logs, encoding each that matches the client request. */
for (log_idx = 0;; log_idx++) {
- rc = log_mgmt_impl_get_log(log_idx, &log);
+ rc = log_mgmt_get_log(log_idx, &log);
if (rc == LOG_MGMT_ERR_ENOENT) {
/* Log list fully iterated. */
if (name_len != 0) {
@@ -468,7 +728,7 @@ log_mgmt_module_list(struct mgmt_ctxt *ctxt)
err |= cbor_encoder_create_map(&ctxt->encoder, &modules, CborIndefiniteLength);
for (module = 0;; module++) {
- rc = log_mgmt_impl_get_module(module, &module_name);
+ rc = log_mgmt_get_module(module, &module_name);
if (rc == LOG_MGMT_ERR_ENOENT) {
break;
}
@@ -511,7 +771,7 @@ log_mgmt_logs_list(struct mgmt_ctxt *ctxt)
err |= cbor_encoder_create_array(&ctxt->encoder, &log_list, CborIndefiniteLength);
for (log_idx = 0;; log_idx++) {
- rc = log_mgmt_impl_get_log(log_idx, &log);
+ rc = log_mgmt_get_log(log_idx, &log);
if (rc == LOG_MGMT_ERR_ENOENT) {
break;
}
@@ -553,7 +813,7 @@ log_mgmt_level_list(struct mgmt_ctxt *ctxt)
err |= cbor_encoder_create_map(&ctxt->encoder, &level_map, CborIndefiniteLength);
for (level = 0;; level++) {
- rc = log_mgmt_impl_get_level(level, &level_name);
+ rc = log_mgmt_get_level(level, &level_name);
if (rc == LOG_MGMT_ERR_ENOENT) {
break;
}
@@ -584,7 +844,7 @@ static int
log_mgmt_clear(struct mgmt_ctxt *ctxt)
{
struct log_mgmt_log log;
- char name[LOG_MGMT_NAME_LEN] = { 0 };
+ char name[MYNEWT_VAL(LOG_MGMT_NAME_LEN)] = { 0 };
int name_len;
int log_idx;
int rc;
@@ -605,7 +865,7 @@ log_mgmt_clear(struct mgmt_ctxt *ctxt)
name_len = strlen(name);
for (log_idx = 0;; log_idx++) {
- rc = log_mgmt_impl_get_log(log_idx, &log);
+ rc = log_mgmt_get_log(log_idx, &log);
if (rc == LOG_MGMT_ERR_ENOENT) {
return 0;
}
@@ -615,7 +875,7 @@ log_mgmt_clear(struct mgmt_ctxt *ctxt)
if (log.type != LOG_MGMT_TYPE_STREAM) {
if (name_len == 0 || strcmp(log.name, name) == 0) {
- rc = log_mgmt_impl_clear(log.name);
+ rc = log_mgmt_flush_log(log.name);
if (rc != 0) {
return rc;
}
@@ -639,3 +899,12 @@ log_mgmt_register_group(void)
{
mgmt_register_group(&log_mgmt_group);
}
+
+void
+log_mgmt_module_init(void)
+{
+ /* Ensure this function only gets called by sysinit. */
+ SYSINIT_ASSERT_ACTIVE();
+
+ log_mgmt_register_group();
+}
diff --git a/mgmt/mcumgr/cmd/log_mgmt/src/stubs.c b/mgmt/mcumgr/cmd/log_mgmt/src/stubs.c
deleted file mode 100644
index db1b5b7d9..000000000
--- a/mgmt/mcumgr/cmd/log_mgmt/src/stubs.c
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * 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 "log_mgmt/log_mgmt_impl.h"
-
-int __attribute__((weak))
-log_mgmt_impl_get_log(int idx, struct log_mgmt_log *out_log)
-{
- return MGMT_ERR_ENOTSUP;
-}
-
-int __attribute__((weak))
-log_mgmt_impl_get_module(int idx, const char **out_module_name)
-{
- return MGMT_ERR_ENOTSUP;
-}
-
-int __attribute__((weak))
-log_mgmt_impl_get_level(int idx, const char **out_level_name)
-{
- return MGMT_ERR_ENOTSUP;
-}
-
-int __attribute__((weak))
-log_mgmt_impl_get_next_idx(uint32_t *out_idx)
-{
- return MGMT_ERR_ENOTSUP;
-}
-
-int __attribute__((weak))
-log_mgmt_impl_foreach_entry(const char *log_name,
- const struct log_mgmt_filter *filter,
- log_mgmt_foreach_entry_fn *cb,
- void *arg)
-{
- return MGMT_ERR_ENOTSUP;
-}
-
-int __attribute__((weak))
-log_mgmt_impl_clear(const char *log_name)
-{
- return MGMT_ERR_ENOTSUP;
-}
-
-int __attribute__((weak))
-log_mgmt_impl_set_watermark(const struct log_mgmt_log *log, int index)
-{
- return MGMT_ERR_ENOTSUP;
-}
diff --git a/mgmt/mcumgr/cmd/log_mgmt/port/mynewt/syscfg.yml b/mgmt/mcumgr/cmd/log_mgmt/syscfg.yml
similarity index 100%
rename from mgmt/mcumgr/cmd/log_mgmt/port/mynewt/syscfg.yml
rename to mgmt/mcumgr/cmd/log_mgmt/syscfg.yml