From a470e8bf0d1c9368f862d3627cafc02e13b351f7 Mon Sep 17 00:00:00 2001 From: Zhanglong Xia Date: Wed, 5 Mar 2025 13:47:45 +0800 Subject: [PATCH] [cli] add `targetpower` command support (#11321) --- src/cli/README.md | 13 +++++++++++++ src/cli/cli.cpp | 30 ++++++++++++++++++++++++++++++ src/core/radio/radio_platform.cpp | 2 ++ tests/scripts/expect/cli-misc.exp | 9 +++++++++ 4 files changed, 54 insertions(+) diff --git a/src/cli/README.md b/src/cli/README.md index a80c18590..319d0b994 100644 --- a/src/cli/README.md +++ b/src/cli/README.md @@ -120,6 +120,7 @@ Done - [sntp](#sntp-query-sntp-server-ip-sntp-server-port) - [state](#state) - [srp](README_SRP.md) +- [targetpower](#targetpower-channel-targetpower) - [tcat](README_TCAT.md) - [tcp](README_TCP.md) - [test](#test-tmforiginfilter-enabledisable) @@ -3809,6 +3810,18 @@ Try to switch to state `detached`, `child`, `router`. Done ``` +### targetpower \ \ + +Set the target power. + +- `channel` : Thread channel. +- `targetpower` : The target power in the unit of 0.01dBm. + +```bash +> targetpower 12 1000 +Done +``` + ### test tmforiginfilter \[enable|disable\] Enable/disable filter that drops UDP messages sent to the TMF port from untrusted origin. Also get the current state of the filter if no argument is specified. diff --git a/src/cli/cli.cpp b/src/cli/cli.cpp index 2d48fac3f..048a86c63 100644 --- a/src/cli/cli.cpp +++ b/src/cli/cli.cpp @@ -6698,6 +6698,35 @@ exit: return error; } +/** + * @cli targetpower (set) + * @code + * targetpower 12 2000 + * Done + * @endcode + * @cparam targetpower @ca{channel} @ca{targetpower} + * @par + * Sets the target power in the unit of 0.01 dBm. + * @sa otPlatRadioSetChannelTargetPower + */ +template <> otError Interpreter::Process(Arg aArgs[]) +{ + otError error = OT_ERROR_NONE; + uint8_t channel; + int16_t targetPower; + uint32_t channelMask; + + SuccessOrExit(error = aArgs[0].ParseAsUint8(channel)); + channelMask = otLinkGetSupportedChannelMask(GetInstancePtr()); + VerifyOrExit((1 << channel) & channelMask, error = OT_ERROR_INVALID_ARGS); + SuccessOrExit(error = aArgs[1].ParseAsInt16(targetPower)); + + error = otPlatRadioSetChannelTargetPower(GetInstancePtr(), channel, targetPower); + +exit: + return error; +} + /** * @cli debug * @par @@ -8338,6 +8367,7 @@ otError Interpreter::ProcessCommand(Arg aArgs[]) CmdEntry("srp"), #endif CmdEntry("state"), + CmdEntry("targetpower"), #if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE && OPENTHREAD_CONFIG_CLI_BLE_SECURE_ENABLE CmdEntry("tcat"), #endif diff --git a/src/core/radio/radio_platform.cpp b/src/core/radio/radio_platform.cpp index 1db6af755..e6bf9d844 100644 --- a/src/core/radio/radio_platform.cpp +++ b/src/core/radio/radio_platform.cpp @@ -364,3 +364,5 @@ extern "C" OT_TOOL_WEAK void otPlatRadioSetRxOnWhenIdle(otInstance *aInstance, b OT_UNUSED_VARIABLE(aInstance); OT_UNUSED_VARIABLE(aEnable); } + +OT_TOOL_WEAK otError otPlatRadioSetChannelTargetPower(otInstance *, uint8_t, int16_t) { return kErrorNotImplemented; } diff --git a/tests/scripts/expect/cli-misc.exp b/tests/scripts/expect/cli-misc.exp index 471eb9277..0a0f0241a 100755 --- a/tests/scripts/expect/cli-misc.exp +++ b/tests/scripts/expect/cli-misc.exp @@ -123,6 +123,15 @@ send "txpower\n" expect -- "-10 dBm" expect_line "Done" +send "targetpower 12 1000\n" +expect_line "Done" + +send "targetpower 12 -1000\n" +expect_line "Done" + +send "targetpower 10 1000\n" +expect_line "Error 7: InvalidArgs" + send "thread version\n" expect_line "Done"