From 3061fa19de7a59bfb16774d1e001ca51ff76bbfa Mon Sep 17 00:00:00 2001 From: Esko Dijk Date: Thu, 22 Jan 2026 19:49:37 +0100 Subject: [PATCH] [otns] add default (weak) OTNS platform API implementation in core (#12308) A default OTNS platform API function otPlatOtnsStatus() is now provided that writes the status push to the log always (regardless of configured log level). This is useful as a default handling for apps built with -DOT_OTNS=ON, avoiding linker errors while not mandating each platform to implement the API by itself. Specifically it enables the Posix CLI app ot-cli to be built with OTNS support, which is required to run Posix nodes in OTNS and receive the status-push events via stdout logging. The logging is emitted from the module named "Otns" so that a simulator or other tool can easily detect the OTNS format status push events in the log output. --- examples/platforms/simulation/misc.c | 4 -- examples/platforms/utils/CMakeLists.txt | 1 - examples/platforms/utils/otns_utils.cpp | 52 ------------------------- src/core/utils/otns.cpp | 9 +++++ tests/gtest/fake_platform.cpp | 4 -- tests/unit/test_platform.cpp | 4 -- 6 files changed, 9 insertions(+), 65 deletions(-) delete mode 100644 examples/platforms/utils/otns_utils.cpp diff --git a/examples/platforms/simulation/misc.c b/examples/platforms/simulation/misc.c index 79c548041..235f0859e 100644 --- a/examples/platforms/simulation/misc.c +++ b/examples/platforms/simulation/misc.c @@ -122,7 +122,3 @@ void otPlatAssertFail(const char *aFilename, int aLineNumber) assert(false); exit(1); } - -#if OPENTHREAD_CONFIG_OTNS_ENABLE && !OPENTHREAD_SIMULATION_VIRTUAL_TIME -void otPlatOtnsStatus(const char *aStatus) { OT_UNUSED_VARIABLE(aStatus); } -#endif diff --git a/examples/platforms/utils/CMakeLists.txt b/examples/platforms/utils/CMakeLists.txt index d4d26ba58..22bd46657 100644 --- a/examples/platforms/utils/CMakeLists.txt +++ b/examples/platforms/utils/CMakeLists.txt @@ -31,7 +31,6 @@ add_library(openthread-platform-utils OBJECT link_metrics.cpp logging_rtt.c mac_frame.cpp - otns_utils.cpp settings_ram.c soft_source_match_table.c uart_rtt.c diff --git a/examples/platforms/utils/otns_utils.cpp b/examples/platforms/utils/otns_utils.cpp deleted file mode 100644 index e432a6355..000000000 --- a/examples/platforms/utils/otns_utils.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2020, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ -#include -#include - -#include -#include - -#include "common/log.hpp" - -using namespace ot; - -/* - * Implementation note: - * These are all "weak" so that a platform may if it chooses to override the instance. - */ - -#if OPENTHREAD_CONFIG_OTNS_ENABLE - -OT_TOOL_WEAK -void otPlatOtnsStatus(const char *aStatus) -{ - OT_UNUSED_VARIABLE(aStatus); - LogAlways("[OTNS] %s", aStatus); -} - -#endif // OPENTHREAD_CONFIG_OTNS_ENABLE diff --git a/src/core/utils/otns.cpp b/src/core/utils/otns.cpp index 3d5559f37..4ddecf6df 100644 --- a/src/core/utils/otns.cpp +++ b/src/core/utils/otns.cpp @@ -220,6 +220,15 @@ exit: } #endif // OPENTHREAD_MTD || OPENTHREAD_FTD +//--------------------------------------------------------------------------------------------------------------------- +// Default/weak implementation of OTNS platform APIs + +extern "C" OT_TOOL_WEAK void otPlatOtnsStatus(const char *aStatus) +{ + OT_UNUSED_VARIABLE(aStatus); + LogAt(kLogLevelNone, "%s", aStatus); +} + } // namespace Utils } // namespace ot diff --git a/tests/gtest/fake_platform.cpp b/tests/gtest/fake_platform.cpp index 38831d618..25353f8e5 100644 --- a/tests/gtest/fake_platform.cpp +++ b/tests/gtest/fake_platform.cpp @@ -666,9 +666,5 @@ void otPlatDnssdStartRecordQuerier(otInstance *, const otPlatDnssdRecordQuerier void otPlatDnssdStopRecordQuerier(otInstance *, const otPlatDnssdRecordQuerier *) {} -#if OPENTHREAD_CONFIG_OTNS_ENABLE -void otPlatOtnsStatus(const char *aStatus) { OT_UNUSED_VARIABLE(aStatus); } -#endif - void otPlatAssertFail(const char *, int) {} } // extern "C" diff --git a/tests/unit/test_platform.cpp b/tests/unit/test_platform.cpp index 02b3a79d0..224fd4342 100644 --- a/tests/unit/test_platform.cpp +++ b/tests/unit/test_platform.cpp @@ -441,10 +441,6 @@ OT_TOOL_WEAK uint8_t otPlatRadioGetCslAccuracy(otInstance *) } #endif -#if OPENTHREAD_CONFIG_OTNS_ENABLE -OT_TOOL_WEAK void otPlatOtnsStatus(const char *) {} -#endif - #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE OT_TOOL_WEAK void otPlatTrelEnable(otInstance *, uint16_t *) {}