From 667fadc96850fdc68d7b2c68c98fd4bc0aa5406b Mon Sep 17 00:00:00 2001 From: Maciej Baczmanski Date: Tue, 7 Jan 2025 14:45:35 +0100 Subject: [PATCH 1/3] [nrf fromtree] net: openthread: radio: Fix platform radio state machine Fix platform radio state machine to be compliant with one shown in OpenThread's `include/openthread/platform/radio.h` Align twister tests to verify proper behavior of the state machine. Signed-off-by: Maciej Baczmanski (cherry picked from commit b46f72009a72b8ea7bcc141e1bd8b31eaf6303f7) --- modules/openthread/platform/radio.c | 37 +++++++++++++++++----------- tests/subsys/openthread/radio_test.c | 12 ++++++++- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/modules/openthread/platform/radio.c b/modules/openthread/platform/radio.c index 73f6f4dc9cc..2ce7dd81d0d 100644 --- a/modules/openthread/platform/radio.c +++ b/modules/openthread/platform/radio.c @@ -742,19 +742,25 @@ bool otPlatRadioIsEnabled(otInstance *aInstance) otError otPlatRadioEnable(otInstance *aInstance) { - if (!otPlatRadioIsEnabled(aInstance)) { - sState = OT_RADIO_STATE_SLEEP; + ARG_UNUSED(aInstance); + + if (sState != OT_RADIO_STATE_DISABLED && sState != OT_RADIO_STATE_SLEEP) { + return OT_ERROR_INVALID_STATE; } + sState = OT_RADIO_STATE_SLEEP; return OT_ERROR_NONE; } otError otPlatRadioDisable(otInstance *aInstance) { - if (otPlatRadioIsEnabled(aInstance)) { - sState = OT_RADIO_STATE_DISABLED; + ARG_UNUSED(aInstance); + + if (sState != OT_RADIO_STATE_DISABLED && sState != OT_RADIO_STATE_SLEEP) { + return OT_ERROR_INVALID_STATE; } + sState = OT_RADIO_STATE_DISABLED; return OT_ERROR_NONE; } @@ -762,23 +768,24 @@ otError otPlatRadioSleep(otInstance *aInstance) { ARG_UNUSED(aInstance); - otError error = OT_ERROR_INVALID_STATE; - - if (sState == OT_RADIO_STATE_SLEEP || - sState == OT_RADIO_STATE_RECEIVE || - sState == OT_RADIO_STATE_TRANSMIT) { - error = OT_ERROR_NONE; - radio_api->stop(radio_dev); - sState = OT_RADIO_STATE_SLEEP; + if (sState != OT_RADIO_STATE_SLEEP && sState != OT_RADIO_STATE_RECEIVE) { + return OT_ERROR_INVALID_STATE; } - return error; + radio_api->stop(radio_dev); + sState = OT_RADIO_STATE_SLEEP; + + return OT_ERROR_NONE; } otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel) { ARG_UNUSED(aInstance); + if (sState == OT_RADIO_STATE_DISABLED) { + return OT_ERROR_INVALID_STATE; + } + channel = aChannel; radio_api->set_channel(radio_dev, aChannel); @@ -883,7 +890,9 @@ otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket) radio_caps = radio_api->get_capabilities(radio_dev); - if ((sState == OT_RADIO_STATE_RECEIVE) || (radio_caps & IEEE802154_HW_SLEEP_TO_TX)) { + if (sState == OT_RADIO_STATE_RECEIVE || + (sState == OT_RADIO_STATE_SLEEP && + radio_caps & IEEE802154_HW_SLEEP_TO_TX)) { if (run_tx_task(aInstance) == 0) { error = OT_ERROR_NONE; } diff --git a/tests/subsys/openthread/radio_test.c b/tests/subsys/openthread/radio_test.c index 8c7a9fcb79e..6e407cab9c6 100644 --- a/tests/subsys/openthread/radio_test.c +++ b/tests/subsys/openthread/radio_test.c @@ -625,6 +625,9 @@ ZTEST(openthread_radio, test_radio_state_test) zassert_equal(otPlatRadioSetTransmitPower(ot, power), OT_ERROR_NONE, "Failed to set TX power."); + + zassert_equal(otPlatRadioSleep(ot), OT_ERROR_NONE, "Failed to switch to sleep mode."); + zassert_equal(otPlatRadioDisable(ot), OT_ERROR_NONE, "Failed to disable radio."); zassert_false(otPlatRadioIsEnabled(ot), "Radio reports as enabled."); @@ -632,6 +635,9 @@ ZTEST(openthread_radio, test_radio_state_test) zassert_equal(otPlatRadioSleep(ot), OT_ERROR_INVALID_STATE, "Changed to sleep regardless being disabled."); + zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_INVALID_STATE, + "Changed to receive regardless being disabled."); + zassert_equal(otPlatRadioEnable(ot), OT_ERROR_NONE, "Enabling radio failed."); zassert_true(otPlatRadioIsEnabled(ot), "Radio reports disabled."); @@ -644,6 +650,9 @@ ZTEST(openthread_radio, test_radio_state_test) zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_NONE, "Failed to receive."); zassert_equal(platformRadioChannelGet(ot), channel, "Channel number not remembered."); + zassert_equal(otPlatRadioDisable(ot), OT_ERROR_INVALID_STATE, + "Changed to disabled regardless being in receive state."); + zassert_true(otPlatRadioIsEnabled(ot), "Radio reports as disabled."); zassert_equal(1, set_channel_mock_fake.call_count); zassert_equal(channel, set_channel_mock_fake.arg1_val); @@ -651,7 +660,7 @@ ZTEST(openthread_radio, test_radio_state_test) zassert_equal(power, set_txpower_mock_fake.arg1_val); zassert_equal(1, start_mock_fake.call_count); zassert_equal_ptr(radio, start_mock_fake.arg0_val, NULL); - zassert_equal(1, stop_mock_fake.call_count); + zassert_equal(2, stop_mock_fake.call_count); zassert_equal_ptr(radio, stop_mock_fake.arg0_val, NULL); } @@ -814,6 +823,7 @@ ZTEST(openthread_radio, test_net_pkt_transmit) "Failed to set TX power."); set_channel_mock_fake.return_val = 0; + zassert_equal(otPlatRadioEnable(ot), OT_ERROR_NONE, "Failed to enable."); zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_NONE, "Failed to receive."); zassert_equal(1, set_channel_mock_fake.call_count); zassert_equal(channel, set_channel_mock_fake.arg1_val); From 61c7dbdbe43521da01c24e02d6b9a9d74c18e80a Mon Sep 17 00:00:00 2001 From: Maciej Baczmanski Date: Tue, 7 Jan 2025 14:46:13 +0100 Subject: [PATCH 2/3] [nrf fromtree] net: openthread: radio: Add setting channel in diag mode Add possibility to set channel in diag mode by implementing `platformRadioChannelSet` API. Signed-off-by: Maciej Baczmanski (cherry picked from commit 712da726be9aaec7a1569b5e92dc4bdb7f756ac9) --- modules/openthread/platform/diag.c | 2 +- modules/openthread/platform/platform-zephyr.h | 10 ++++++++++ modules/openthread/platform/radio.c | 7 +++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/openthread/platform/diag.c b/modules/openthread/platform/diag.c index edf8ad09dd5..23f82985812 100644 --- a/modules/openthread/platform/diag.c +++ b/modules/openthread/platform/diag.c @@ -104,8 +104,8 @@ bool otPlatDiagModeGet(void) void otPlatDiagChannelSet(uint8_t aChannel) { - ARG_UNUSED(aChannel); sChannel = aChannel; + platformRadioChannelSet(aChannel); } void otPlatDiagTxPowerSet(int8_t aTxPower) diff --git a/modules/openthread/platform/platform-zephyr.h b/modules/openthread/platform/platform-zephyr.h index ca25fedf2b1..d6dd26adea0 100644 --- a/modules/openthread/platform/platform-zephyr.h +++ b/modules/openthread/platform/platform-zephyr.h @@ -70,6 +70,16 @@ void platformUartPanic(void); */ uint16_t platformRadioChannelGet(otInstance *aInstance); +#if defined(CONFIG_OPENTHREAD_DIAG) +/** + * Set channel on radio driver. + * + * @param[in] aChannel The channel that the radio driver should use for operation. + * + */ +void platformRadioChannelSet(uint8_t aChannel); +#endif /* CONFIG_OPENTHREAD_DIAG */ + #if defined(CONFIG_IEEE802154_CARRIER_FUNCTIONS) /** * Start/stop continuous carrier wave transmission. diff --git a/modules/openthread/platform/radio.c b/modules/openthread/platform/radio.c index 2ce7dd81d0d..97c6e3a1d12 100644 --- a/modules/openthread/platform/radio.c +++ b/modules/openthread/platform/radio.c @@ -708,6 +708,13 @@ uint16_t platformRadioChannelGet(otInstance *aInstance) return channel; } +#if defined(CONFIG_OPENTHREAD_DIAG) +void platformRadioChannelSet(uint8_t aChannel) +{ + channel = aChannel; +} +#endif + void otPlatRadioSetPanId(otInstance *aInstance, uint16_t aPanId) { ARG_UNUSED(aInstance); From 12e7d289ea55db0bb7abf55bb803e7ef97680423 Mon Sep 17 00:00:00 2001 From: Maciej Baczmanski Date: Fri, 10 Jan 2025 08:47:20 +0100 Subject: [PATCH 3/3] [nrf fromtree] net: openthread: cleanup diag commands After https://github.com/openthread/openthread/pull/11055, platform is not required to check diagnostics mode while processing commands. Signed-off-by: Maciej Baczmanski (cherry picked from commit 74bbbdccf874afd4b9f2974e0e1a62378aee8719) --- modules/openthread/platform/diag.c | 16 ++++------------ west.yml | 2 +- 2 files changed, 5 insertions(+), 13 deletions(-) diff --git a/modules/openthread/platform/diag.c b/modules/openthread/platform/diag.c index 23f82985812..79f4e38984a 100644 --- a/modules/openthread/platform/diag.c +++ b/modules/openthread/platform/diag.c @@ -125,8 +125,8 @@ void otPlatDiagRadioReceived(otInstance *aInstance, #if defined(CONFIG_IEEE802154_CARRIER_FUNCTIONS) otError otPlatDiagRadioTransmitCarrier(otInstance *aInstance, bool aEnable) { - if (!otPlatDiagModeGet() || (sTransmitMode != DIAG_TRANSMIT_MODE_IDLE && - sTransmitMode != DIAG_TRANSMIT_MODE_CARRIER)) { + if (sTransmitMode != DIAG_TRANSMIT_MODE_IDLE && + sTransmitMode != DIAG_TRANSMIT_MODE_CARRIER) { return OT_ERROR_INVALID_STATE; } @@ -175,10 +175,6 @@ static otError gpio_get_spec(uint32_t gpio_idx, const struct gpio_dt_spec **spec *spec = &gpio_spec[gpio_idx]; - if (!otPlatDiagModeGet()) { - return OT_ERROR_INVALID_STATE; - } - if (!gpio_is_ready_dt(*spec)) { return OT_ERROR_INVALID_ARGS; } @@ -326,8 +322,8 @@ static otError startModCarrier(otInstance *aInstance, uint8_t aArgsLength, char return OT_ERROR_INVALID_ARGS; } - if (!otPlatDiagModeGet() || (sTransmitMode != DIAG_TRANSMIT_MODE_IDLE && - sTransmitMode != DIAG_TRANSMIT_MODE_MODCARRIER)) { + if (sTransmitMode != DIAG_TRANSMIT_MODE_IDLE && + sTransmitMode != DIAG_TRANSMIT_MODE_MODCARRIER) { return OT_ERROR_INVALID_STATE; } @@ -396,10 +392,6 @@ static otError processTransmit(otInstance *aInstance, uint8_t aArgsLength, char long value; uint32_t now; - if (!otPlatDiagModeGet()) { - return OT_ERROR_INVALID_STATE; - } - if (aArgsLength == 0) { diag_output("transmit will send %" PRId32 " diagnostic messages with %" PRIu32 " ms interval\r\n", diff --git a/west.yml b/west.yml index b99c5d1212d..6d1ad7df18c 100644 --- a/west.yml +++ b/west.yml @@ -309,7 +309,7 @@ manifest: revision: b735edbc739ad59156eb55bb8ce2583d74537719 path: modules/lib/open-amp - name: openthread - revision: 2aeb8b833ba760ec29d5f340dd1ce7bcb61c5d56 + revision: 3ae741f95e7dfb391dec35c48742862049eb62e8 path: modules/lib/openthread - name: percepio path: modules/debug/percepio