Skip to content

Commit

Permalink
firmware/network/radio: Fixed radio functions gets
Browse files Browse the repository at this point in the history
  • Loading branch information
CW-75 committed Oct 14, 2022
1 parent 1e2cce4 commit e8f100e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 24 deletions.
6 changes: 3 additions & 3 deletions firmware/network/radio/include/radio.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ 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
*
* @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
Expand Down
35 changes: 19 additions & 16 deletions firmware/network/radio/radio.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,35 +83,34 @@ 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) {
return -1;
}
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;
}

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;
}
Expand All @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/driver_radio/Kconfig
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
rsource "../../firmware/Kconfig.debug"
rsource "../../firmware/network/radio/Kconfig"
29 changes: 24 additions & 5 deletions tests/driver_radio/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
* @brief Radio file
*
* @author RocioRojas <[email protected]>
* @author eduazocar <[email protected]>
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "xtimer.h"
#include "embUnit.h"
#include "radio.h"

Expand All @@ -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);
}

Expand All @@ -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),
};
Expand All @@ -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;
}

0 comments on commit e8f100e

Please sign in to comment.