From f35376b26a1721070407be98b787e02be5d68714 Mon Sep 17 00:00:00 2001 From: Przemyslaw Bida Date: Thu, 28 Nov 2024 12:17:03 +0100 Subject: [PATCH] [nrf fromtree] net: openthread: Add `modcarrier` command to OT diag module. Commit add `modcarrier` shell command for Openthread diagnostic mode. Command can transmit modulated carrier out of device for test purposes. Signed-off-by: Przemyslaw Bida (cherry picked from commit 63e1bb41b103426a15b53e2dcaaf53655b3bafb8) --- drivers/ieee802154/Kconfig | 1 + modules/openthread/Kconfig.features | 1 - modules/openthread/platform/diag.c | 37 +++++++++++++++++++ modules/openthread/platform/platform-zephyr.h | 9 ++++- modules/openthread/platform/radio.c | 29 +++++++++++++++ 5 files changed, 75 insertions(+), 2 deletions(-) diff --git a/drivers/ieee802154/Kconfig b/drivers/ieee802154/Kconfig index af405551f63..629b492a579 100644 --- a/drivers/ieee802154/Kconfig +++ b/drivers/ieee802154/Kconfig @@ -102,6 +102,7 @@ config IEEE802154_SELECTIVE_TXCHANNEL config IEEE802154_CARRIER_FUNCTIONS bool "Support for carrier functions" + default y if OPENTHREAD_DIAG help Enable support for functions such as modulated carrier and continuous carrier. diff --git a/modules/openthread/Kconfig.features b/modules/openthread/Kconfig.features index 514b9b85745..ccdd3236d38 100644 --- a/modules/openthread/Kconfig.features +++ b/modules/openthread/Kconfig.features @@ -136,7 +136,6 @@ config OPENTHREAD_DHCP6_SERVER config OPENTHREAD_DIAG bool "Diagnostic functions support" - depends on IEEE802154_CARRIER_FUNCTIONS help Enable OpenThread CLI diagnostic commands diff --git a/modules/openthread/platform/diag.c b/modules/openthread/platform/diag.c index b92e34209b8..23ea3b97986 100644 --- a/modules/openthread/platform/diag.c +++ b/modules/openthread/platform/diag.c @@ -7,9 +7,11 @@ #include #include +#include #include #include "platform-zephyr.h" +#include "zephyr/sys/util.h" /** * Diagnostics mode variables. @@ -19,6 +21,8 @@ static bool sDiagMode; static void *sDiagCallbackContext; static otPlatDiagOutputCallback sDiagOutputCallback; +static otError startModCarrier(otInstance *aInstance, uint8_t aArgsLength, char *aArgs[]); + static void diag_output(const char *aFormat, ...) { va_list args; @@ -47,6 +51,12 @@ otError otPlatDiagProcess(otInstance *aInstance, uint8_t aArgsLength, char *aArg ARG_UNUSED(aInstance); ARG_UNUSED(aArgsLength); +#if defined(CONFIG_IEEE802154_CARRIER_FUNCTIONS) + if (strcmp(aArgs[0], "modcarrier") == 0) { + return startModCarrier(aInstance, aArgsLength - 1, aArgs + 1); + } +#endif + /* Add more platform specific diagnostics features here. */ diag_output("diag feature '%s' is not supported\r\n", aArgs[0]); @@ -276,3 +286,30 @@ otError otPlatDiagGpioGetMode(uint32_t aGpio, otGpioMode *aMode) #endif /* DT_HAS_COMPAT_STATUS_OKAY(openthread_config) && \ * DT_NODE_HAS_PROP(DT_COMPAT_GET_ANY_STATUS_OKAY(openthread_config), diag_gpios) */ + +#if defined(CONFIG_IEEE802154_CARRIER_FUNCTIONS) + +static otError startModCarrier(otInstance *aInstance, uint8_t aArgsLength, char *aArgs[]) +{ + ARG_UNUSED(aInstance); + ARG_UNUSED(aArgsLength); + + bool enable = true; + uint8_t data[OT_RADIO_FRAME_MAX_SIZE + 1]; + + if (aArgsLength <= 0) { + return OT_ERROR_INVALID_ARGS; + } + + if (strcmp(aArgs[0], "stop") == 0) { + enable = false; + } else { + if (hex2bin(aArgs[0], strlen(aArgs[0]), data, ARRAY_SIZE(data)) == 0) { + return OT_ERROR_INVALID_ARGS; + } + } + + return platformRadioTransmitModulatedCarrier(aInstance, enable, data); +} + +#endif diff --git a/modules/openthread/platform/platform-zephyr.h b/modules/openthread/platform/platform-zephyr.h index 94fd04d7057..ca25fedf2b1 100644 --- a/modules/openthread/platform/platform-zephyr.h +++ b/modules/openthread/platform/platform-zephyr.h @@ -77,13 +77,20 @@ uint16_t platformRadioChannelGet(otInstance *aInstance); otError platformRadioTransmitCarrier(otInstance *aInstance, bool aEnable); #endif /* CONFIG_IEEE802154_CARRIER_FUNCTIONS */ +#if defined(CONFIG_IEEE802154_CARRIER_FUNCTIONS) +/** + * Start/stop modulated carrier wave transmission. + */ +otError platformRadioTransmitModulatedCarrier(otInstance *aInstance, bool aEnable, + const uint8_t *aData); +#endif + /** * This function initializes the random number service used by OpenThread. * */ void platformRandomInit(void); - /** * Initialize platform Shell driver. */ diff --git a/modules/openthread/platform/radio.c b/modules/openthread/platform/radio.c index 33fc7b42bbf..73f6f4dc9cc 100644 --- a/modules/openthread/platform/radio.c +++ b/modules/openthread/platform/radio.c @@ -11,6 +11,7 @@ * */ +#include #define LOG_MODULE_NAME net_otPlat_radio #include @@ -832,6 +833,34 @@ otError platformRadioTransmitCarrier(otInstance *aInstance, bool aEnable) return OT_ERROR_NONE; } + +otError platformRadioTransmitModulatedCarrier(otInstance *aInstance, bool aEnable, + const uint8_t *aData) +{ + if (radio_api->modulated_carrier == NULL) { + return OT_ERROR_NOT_IMPLEMENTED; + } + + if (aEnable && sState == OT_RADIO_STATE_RECEIVE) { + if (aData == NULL) { + return OT_ERROR_INVALID_ARGS; + } + + radio_api->set_txpower(radio_dev, get_transmit_power_for_channel(channel)); + + if (radio_api->modulated_carrier(radio_dev, aData) != 0) { + return OT_ERROR_FAILED; + } + sState = OT_RADIO_STATE_TRANSMIT; + } else if ((!aEnable) && sState == OT_RADIO_STATE_TRANSMIT) { + return otPlatRadioReceive(aInstance, channel); + } else { + return OT_ERROR_INVALID_STATE; + } + + return OT_ERROR_NONE; +} + #endif /* CONFIG_IEEE802154_CARRIER_FUNCTIONS */ otRadioState otPlatRadioGetState(otInstance *aInstance)