diff --git a/firmware/network/radio/include/radio.h b/firmware/network/radio/include/radio.h index 60bf59e5a..d2cdcc285 100644 --- a/firmware/network/radio/include/radio.h +++ b/firmware/network/radio/include/radio.h @@ -47,13 +47,13 @@ int8_t get_ieee802154_iface(void); * @brief this function is used to get data of tx power * @param[in] txpower channel you want to get */ -int8_t get_netopt_tx_power(int16_t txpower); +int8_t get_netopt_tx_power(int16_t *txpower); /** * @brief this function is used to get data of channel * @param[in] channel channel you want to get */ -int8_t get_netopt_channel(int16_t channel); +int8_t get_netopt_channel(uint16_t *channel); /** * @brief this function is used to set the channel @@ -61,7 +61,7 @@ int8_t get_netopt_channel(int16_t channel); * @param[in] channel channel you want to set * @note the device has channels between 11 to 26 */ -int8_t set_netopt_channel(int16_t channel); +int8_t set_netopt_channel(uint16_t channel); /** * @brief this function is used to set the tx power diff --git a/firmware/network/radio/radio.c b/firmware/network/radio/radio.c index 111fc649c..035d8eb8e 100644 --- a/firmware/network/radio/radio.c +++ b/firmware/network/radio/radio.c @@ -83,7 +83,7 @@ int8_t get_ieee802154_iface(void) { return -1; } -int8_t get_netopt_tx_power(int16_t txpower) { +int8_t get_netopt_tx_power(int16_t *txpower) { int res; int index = get_ieee802154_iface(); if (index < 0) { @@ -91,9 +91,9 @@ int8_t get_netopt_tx_power(int16_t txpower) { } netif_t *iface = netif_get_by_id(index); - res = netif_get_opt(iface, NETOPT_TX_POWER, 0, &txpower, sizeof(txpower)); + res = netif_get_opt(iface, NETOPT_TX_POWER, 0, txpower, sizeof(int16_t)); if (res >= 0) { - DEBUG("TX-Power: %" PRIi16 "dBm \n", txpower); + DEBUG("TX-Power: %" PRIi16 "dBm \n", *txpower); } else { return -1; } @@ -101,17 +101,16 @@ int8_t get_netopt_tx_power(int16_t txpower) { return 0; } -int8_t get_netopt_channel(int16_t channel) { - int res; +int8_t get_netopt_channel(uint16_t *channel) { int index = get_ieee802154_iface(); if (index < 0) { return -1; } netif_t *iface = netif_get_by_id(index); - res = netif_get_opt(iface, NETOPT_CHANNEL, 0, &channel, sizeof(channel)); + int res = netif_get_opt(iface, NETOPT_CHANNEL, 0, channel, sizeof(uint16_t)); if (res >= 0) { - DEBUG("Channel: %" PRIi16 " \n", channel); + DEBUG("Channel: %" PRIi16 " \n", *channel); } else { return -1; } @@ -120,8 +119,8 @@ int8_t get_netopt_channel(int16_t channel) { } int8_t set_netopt_tx_power(int16_t txpower) { - int res; int index = get_ieee802154_iface(); + uint16_t channel = 0; if (index < 0) { return -1; } @@ -131,8 +130,14 @@ int8_t set_netopt_tx_power(int16_t txpower) { TX_POWER_MAX); return -1; } - - res = netif_set_opt(iface, NETOPT_TX_POWER, 0, &txpower, sizeof(txpower)); + if (get_netopt_channel(&channel) < 0) { + return -1; + } + int res = netif_set_opt(iface, NETOPT_TX_POWER, 0, &txpower, sizeof(txpower)); + if (res < 0) { + return -1; + } + res = set_netopt_channel(channel); if (res >= 0) { DEBUG(" TX-Power: %" PRIi16 "dBm \n", txpower); } else { @@ -142,15 +147,14 @@ int8_t set_netopt_tx_power(int16_t txpower) { return 0; } -int8_t set_netopt_channel(int16_t channel) { - int res; +int8_t set_netopt_channel(uint16_t channel) { int index = get_ieee802154_iface(); if (index < 0) { return -1; } netif_t *iface = netif_get_by_id(index); #ifdef CONFIG_MODE_SUB_24GHZ - if (channel < 0 || channel > 10) { + if (channel > 10) { DEBUG("Error: the channels must be between 0 and 10 \n"); return -1; } @@ -160,7 +164,7 @@ int8_t set_netopt_channel(int16_t channel) { return -1; } #endif - res = netif_set_opt(iface, NETOPT_CHANNEL, 0, &channel, sizeof(channel)); + int res = netif_set_opt(iface, NETOPT_CHANNEL, 0, &channel, sizeof(channel)); if (res >= 0) { DEBUG(" channel: %" PRIi16 " \n", channel); } else { @@ -215,11 +219,10 @@ int8_t initial_radio_setup(void) { subtract_to_interface = 0; #endif } - int err; int16_t radio_tx = CONFIG_TX_POWER; int16_t radio_channel = CONFIG_RADIO_CHANNEL; - err = set_netopt_tx_power(radio_tx); + int err = set_netopt_tx_power(radio_tx); if (err == -1) { DEBUG("Error: Failed to add TX_power.\n"); return err; diff --git a/tests/driver_radio/Kconfig b/tests/driver_radio/Kconfig index f15dce581..2ccd666e5 100644 --- a/tests/driver_radio/Kconfig +++ b/tests/driver_radio/Kconfig @@ -1 +1,2 @@ +rsource "../../firmware/Kconfig.debug" rsource "../../firmware/network/radio/Kconfig" diff --git a/tests/driver_radio/main.c b/tests/driver_radio/main.c index 0718b1f76..7efef68e7 100644 --- a/tests/driver_radio/main.c +++ b/tests/driver_radio/main.c @@ -17,9 +17,12 @@ * @brief Radio file * * @author RocioRojas + * @author eduazocar */ +#include #include #include +#include "xtimer.h" #include "embUnit.h" #include "radio.h" @@ -28,15 +31,29 @@ void test_get_ieee802154_iface(void) { TEST_ASSERT_EQUAL_INT(0, err); } +void test_set_netopt_tx_power(void) { + int16_t txpower = 4; + int8_t err = set_netopt_tx_power(txpower); + TEST_ASSERT_EQUAL_INT(0, err); +} + void test_get_netopt_tx_power(void) { int16_t txpower=0; - int8_t err = get_netopt_tx_power(txpower); + int8_t err = get_netopt_tx_power(&txpower); + printf("Tx Power: %"PRId16"\n", txpower); + TEST_ASSERT_EQUAL_INT(0, err); +} + +void test_set_netopt_channel(void) { + uint16_t channel= 22; + int8_t err = set_netopt_channel(channel); TEST_ASSERT_EQUAL_INT(0, err); } void test_get_netopt_channel(void) { - int16_t channel=0; - int8_t err = get_netopt_channel(channel); + uint16_t channel=0; + int8_t err = get_netopt_channel(&channel); + printf("Channel: %"PRIu16"\n", channel); TEST_ASSERT_EQUAL_INT(0, err); } @@ -45,10 +62,12 @@ void test_initial_radio_setup(void) { TEST_ASSERT_EQUAL_INT(0, err); } -Test *tests_radio_module(void) { +Test *radio_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures){ new_TestFixture(test_get_ieee802154_iface), + new_TestFixture(test_set_netopt_tx_power), new_TestFixture(test_get_netopt_tx_power), + new_TestFixture(test_set_netopt_channel), new_TestFixture(test_get_netopt_channel), new_TestFixture(test_initial_radio_setup), }; @@ -60,7 +79,7 @@ Test *tests_radio_module(void) { int main(void) { TESTS_START(); - TESTS_RUN(tests_radio_module()); + TESTS_RUN(radio_tests()); TESTS_END(); return 0; }