Skip to content

Commit b46f720

Browse files
maciejbaczmanskikartben
authored andcommitted
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 <[email protected]>
1 parent 48ac31c commit b46f720

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

modules/openthread/platform/radio.c

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -756,43 +756,50 @@ bool otPlatRadioIsEnabled(otInstance *aInstance)
756756

757757
otError otPlatRadioEnable(otInstance *aInstance)
758758
{
759-
if (!otPlatRadioIsEnabled(aInstance)) {
760-
sState = OT_RADIO_STATE_SLEEP;
759+
ARG_UNUSED(aInstance);
760+
761+
if (sState != OT_RADIO_STATE_DISABLED && sState != OT_RADIO_STATE_SLEEP) {
762+
return OT_ERROR_INVALID_STATE;
761763
}
762764

765+
sState = OT_RADIO_STATE_SLEEP;
763766
return OT_ERROR_NONE;
764767
}
765768

766769
otError otPlatRadioDisable(otInstance *aInstance)
767770
{
768-
if (otPlatRadioIsEnabled(aInstance)) {
769-
sState = OT_RADIO_STATE_DISABLED;
771+
ARG_UNUSED(aInstance);
772+
773+
if (sState != OT_RADIO_STATE_DISABLED && sState != OT_RADIO_STATE_SLEEP) {
774+
return OT_ERROR_INVALID_STATE;
770775
}
771776

777+
sState = OT_RADIO_STATE_DISABLED;
772778
return OT_ERROR_NONE;
773779
}
774780

775781
otError otPlatRadioSleep(otInstance *aInstance)
776782
{
777783
ARG_UNUSED(aInstance);
778784

779-
otError error = OT_ERROR_INVALID_STATE;
780-
781-
if (sState == OT_RADIO_STATE_SLEEP ||
782-
sState == OT_RADIO_STATE_RECEIVE ||
783-
sState == OT_RADIO_STATE_TRANSMIT) {
784-
error = OT_ERROR_NONE;
785-
radio_api->stop(radio_dev);
786-
sState = OT_RADIO_STATE_SLEEP;
785+
if (sState != OT_RADIO_STATE_SLEEP && sState != OT_RADIO_STATE_RECEIVE) {
786+
return OT_ERROR_INVALID_STATE;
787787
}
788788

789-
return error;
789+
radio_api->stop(radio_dev);
790+
sState = OT_RADIO_STATE_SLEEP;
791+
792+
return OT_ERROR_NONE;
790793
}
791794

792795
otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel)
793796
{
794797
ARG_UNUSED(aInstance);
795798

799+
if (sState == OT_RADIO_STATE_DISABLED) {
800+
return OT_ERROR_INVALID_STATE;
801+
}
802+
796803
channel = aChannel;
797804

798805
radio_api->set_channel(radio_dev, aChannel);
@@ -897,7 +904,9 @@ otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aPacket)
897904

898905
radio_caps = radio_api->get_capabilities(radio_dev);
899906

900-
if ((sState == OT_RADIO_STATE_RECEIVE) || (radio_caps & IEEE802154_HW_SLEEP_TO_TX)) {
907+
if (sState == OT_RADIO_STATE_RECEIVE ||
908+
(sState == OT_RADIO_STATE_SLEEP &&
909+
radio_caps & IEEE802154_HW_SLEEP_TO_TX)) {
901910
if (run_tx_task(aInstance) == 0) {
902911
error = OT_ERROR_NONE;
903912
}

tests/subsys/openthread/radio_test.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,13 +625,19 @@ ZTEST(openthread_radio, test_radio_state_test)
625625

626626
zassert_equal(otPlatRadioSetTransmitPower(ot, power), OT_ERROR_NONE,
627627
"Failed to set TX power.");
628+
629+
zassert_equal(otPlatRadioSleep(ot), OT_ERROR_NONE, "Failed to switch to sleep mode.");
630+
628631
zassert_equal(otPlatRadioDisable(ot), OT_ERROR_NONE, "Failed to disable radio.");
629632

630633
zassert_false(otPlatRadioIsEnabled(ot), "Radio reports as enabled.");
631634

632635
zassert_equal(otPlatRadioSleep(ot), OT_ERROR_INVALID_STATE,
633636
"Changed to sleep regardless being disabled.");
634637

638+
zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_INVALID_STATE,
639+
"Changed to receive regardless being disabled.");
640+
635641
zassert_equal(otPlatRadioEnable(ot), OT_ERROR_NONE, "Enabling radio failed.");
636642

637643
zassert_true(otPlatRadioIsEnabled(ot), "Radio reports disabled.");
@@ -644,14 +650,17 @@ ZTEST(openthread_radio, test_radio_state_test)
644650
zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_NONE, "Failed to receive.");
645651
zassert_equal(platformRadioChannelGet(ot), channel, "Channel number not remembered.");
646652

653+
zassert_equal(otPlatRadioDisable(ot), OT_ERROR_INVALID_STATE,
654+
"Changed to disabled regardless being in receive state.");
655+
647656
zassert_true(otPlatRadioIsEnabled(ot), "Radio reports as disabled.");
648657
zassert_equal(1, set_channel_mock_fake.call_count);
649658
zassert_equal(channel, set_channel_mock_fake.arg1_val);
650659
zassert_equal(1, set_txpower_mock_fake.call_count);
651660
zassert_equal(power, set_txpower_mock_fake.arg1_val);
652661
zassert_equal(1, start_mock_fake.call_count);
653662
zassert_equal_ptr(radio, start_mock_fake.arg0_val, NULL);
654-
zassert_equal(1, stop_mock_fake.call_count);
663+
zassert_equal(2, stop_mock_fake.call_count);
655664
zassert_equal_ptr(radio, stop_mock_fake.arg0_val, NULL);
656665
}
657666

@@ -814,6 +823,7 @@ ZTEST(openthread_radio, test_net_pkt_transmit)
814823
"Failed to set TX power.");
815824

816825
set_channel_mock_fake.return_val = 0;
826+
zassert_equal(otPlatRadioEnable(ot), OT_ERROR_NONE, "Failed to enable.");
817827
zassert_equal(otPlatRadioReceive(ot, channel), OT_ERROR_NONE, "Failed to receive.");
818828
zassert_equal(1, set_channel_mock_fake.call_count);
819829
zassert_equal(channel, set_channel_mock_fake.arg1_val);

0 commit comments

Comments
 (0)