diff --git a/src/cli/BUILD.gn b/src/cli/BUILD.gn index deb978afc..e70ae4fc8 100644 --- a/src/cli/BUILD.gn +++ b/src/cli/BUILD.gn @@ -30,6 +30,7 @@ import("../../etc/gn/openthread.gni") openthread_cli_sources = [ "cli.cpp", "cli.hpp", + "cli_api.cpp", "cli_ba.cpp", "cli_ba.hpp", "cli_bbr.cpp", diff --git a/src/cli/CMakeLists.txt b/src/cli/CMakeLists.txt index 4c85a26da..8eadca79d 100644 --- a/src/cli/CMakeLists.txt +++ b/src/cli/CMakeLists.txt @@ -33,6 +33,7 @@ set(COMMON_INCLUDES set(COMMON_SOURCES cli.cpp + cli_api.cpp cli_ba.cpp cli_bbr.cpp cli_br.cpp diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 2370df1d0..e5a47145c 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -8826,54 +8826,5 @@ otError Interpreter::ProcessCommand(Arg aArgs[]) return error; } -extern "C" void otCliInit(otInstance *aInstance, otCliOutputCallback aCallback, void *aContext) -{ - Interpreter::Initialize(aInstance, aCallback, aContext); - -#if OPENTHREAD_CONFIG_CLI_VENDOR_COMMANDS_ENABLE && OPENTHREAD_CONFIG_CLI_MAX_USER_CMD_ENTRIES > 1 - otCliVendorSetUserCommands(); -#endif -} - -extern "C" void otCliInputLine(char *aBuf) { Interpreter::GetInterpreter().ProcessLine(aBuf); } - -extern "C" otError otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext) -{ - return Interpreter::GetInterpreter().SetUserCommands(aUserCommands, aLength, aContext); -} - -extern "C" void otCliOutputBytes(const uint8_t *aBytes, uint8_t aLength) -{ - Interpreter::GetInterpreter().OutputBytes(aBytes, aLength); -} - -extern "C" void otCliOutputFormat(const char *aFmt, ...) -{ - va_list aAp; - va_start(aAp, aFmt); - Interpreter::GetInterpreter().OutputFormatV(aFmt, aAp); - va_end(aAp); -} - -extern "C" void otCliAppendResult(otError aError) { Interpreter::GetInterpreter().OutputResult(aError); } - -extern "C" void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs) -{ - OT_UNUSED_VARIABLE(aLogLevel); - OT_UNUSED_VARIABLE(aLogRegion); - - VerifyOrExit(Interpreter::IsInitialized()); - - // CLI output is being used for logging, so we set the flag - // `EmittingCommandOutput` to false indicate this. - Interpreter::GetInterpreter().SetEmittingCommandOutput(false); - Interpreter::GetInterpreter().OutputFormatV(aFormat, aArgs); - Interpreter::GetInterpreter().OutputNewLine(); - Interpreter::GetInterpreter().SetEmittingCommandOutput(true); - -exit: - return; -} - } // namespace Cli } // namespace ot diff --git a/src/cli/cli.hpp b/src/cli/cli.hpp index eb6a4c865..a94608486 100644 --- a/src/cli/cli.hpp +++ b/src/cli/cli.hpp @@ -89,7 +89,6 @@ namespace ot { /** - * @namespace ot::Cli * * @brief * This namespace contains definitions for the CLI interpreter. diff --git a/src/cli/cli_api.cpp b/src/cli/cli_api.cpp new file mode 100644 index 000000000..82f5bc6ef --- /dev/null +++ b/src/cli/cli_api.cpp @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2016-2026, 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. + */ + +/** + * @file + * This file implements the CLI public APIs. + */ + +#include + +#include "cli.hpp" + +namespace ot { +namespace Cli { + +extern "C" void otCliInit(otInstance *aInstance, otCliOutputCallback aCallback, void *aContext) +{ + Interpreter::Initialize(aInstance, aCallback, aContext); + +#if OPENTHREAD_CONFIG_CLI_VENDOR_COMMANDS_ENABLE && OPENTHREAD_CONFIG_CLI_MAX_USER_CMD_ENTRIES > 1 + otCliVendorSetUserCommands(); +#endif +} + +extern "C" void otCliInputLine(char *aBuf) { Interpreter::GetInterpreter().ProcessLine(aBuf); } + +extern "C" otError otCliSetUserCommands(const otCliCommand *aUserCommands, uint8_t aLength, void *aContext) +{ + return Interpreter::GetInterpreter().SetUserCommands(aUserCommands, aLength, aContext); +} + +extern "C" void otCliOutputBytes(const uint8_t *aBytes, uint8_t aLength) +{ + Interpreter::GetInterpreter().OutputBytes(aBytes, aLength); +} + +extern "C" void otCliOutputFormat(const char *aFmt, ...) +{ + va_list args; + + va_start(args, aFmt); + Interpreter::GetInterpreter().OutputFormatV(aFmt, args); + va_end(args); +} + +extern "C" void otCliAppendResult(otError aError) { Interpreter::GetInterpreter().OutputResult(aError); } + +extern "C" void otCliPlatLogv(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, va_list aArgs) +{ + OT_UNUSED_VARIABLE(aLogLevel); + OT_UNUSED_VARIABLE(aLogRegion); + + VerifyOrExit(Interpreter::IsInitialized()); + + // CLI output is being used for logging, so we set the flag + // `EmittingCommandOutput` to false indicate this. + Interpreter::GetInterpreter().SetEmittingCommandOutput(false); + Interpreter::GetInterpreter().OutputFormatV(aFormat, aArgs); + Interpreter::GetInterpreter().OutputNewLine(); + Interpreter::GetInterpreter().SetEmittingCommandOutput(true); + +exit: + return; +} + +} // namespace Cli +} // namespace ot diff --git a/src/cli/radio.cmake b/src/cli/radio.cmake index 75f610e00..12d38ef0e 100644 --- a/src/cli/radio.cmake +++ b/src/cli/radio.cmake @@ -46,6 +46,7 @@ target_include_directories(openthread-cli-radio PUBLIC ${OT_PUBLIC_INCLUDES} PRI target_sources(openthread-cli-radio PRIVATE cli.cpp + cli_api.cpp cli_logging.cpp cli_utils.cpp )