1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

Merge pull request #7482 from aabadie/auto_init_sx127x_ifconfig

sys/shell: adapt ifconfig to work with sx1272/76 devices
This commit is contained in:
Martine Lenders 2017-12-14 16:37:05 +01:00 committed by GitHub
commit d15c7559f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 207 additions and 9 deletions

View File

@ -268,6 +268,7 @@ ifneq (,$(filter sx127%,$(USEMODULE)))
USEMODULE += xtimer
USEMODULE += sx127x
USEMODULE += netif
USEMODULE += lora
endif
ifneq (,$(filter tcs37727,$(USEMODULE)))

View File

@ -298,7 +298,7 @@ static int _get(netdev_t *netdev, netopt_t opt, void *val, size_t max_len)
*((uint16_t*) val) = NETDEV_TYPE_LORA;
return sizeof(uint16_t);
case NETOPT_CHANNEL:
case NETOPT_CHANNEL_FREQUENCY:
assert(max_len >= sizeof(uint32_t));
*((uint32_t*) val) = sx127x_get_channel(dev);
return sizeof(uint32_t);
@ -344,9 +344,9 @@ static int _get(netdev_t *netdev, netopt_t opt, void *val, size_t max_len)
return sizeof(netopt_enable_t);
case NETOPT_TX_POWER:
assert(max_len >= sizeof(int8_t));
*((int8_t*) val) = sx127x_get_tx_power(dev);
return sizeof(int8_t);
assert(max_len >= sizeof(int16_t));
*((int16_t*) val) = (int16_t)sx127x_get_tx_power(dev);
return sizeof(int16_t);
case NETOPT_IQ_INVERT:
assert(max_len >= sizeof(uint8_t));
@ -387,7 +387,7 @@ static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t len)
return -EINVAL;
}
case NETOPT_CHANNEL:
case NETOPT_CHANNEL_FREQUENCY:
assert(len <= sizeof(uint32_t));
sx127x_set_channel(dev, *((const uint32_t*) val));
return sizeof(uint32_t);
@ -458,9 +458,14 @@ static int _set(netdev_t *netdev, netopt_t opt, const void *val, size_t len)
return sizeof(uint32_t);
case NETOPT_TX_POWER:
assert(len <= sizeof(int8_t));
sx127x_set_tx_power(dev, *((const int8_t*) val));
return sizeof(int8_t);
assert(len <= sizeof(int16_t));
int16_t power = *((const int16_t *)val);
if ((power < INT8_MIN) || (power > INT8_MAX)) {
res = -EINVAL;
break;
}
sx127x_set_tx_power(dev, (int8_t)power);
return sizeof(int16_t);
case NETOPT_FIXED_HEADER:
assert(len <= sizeof(netopt_enable_t));

View File

@ -33,6 +33,7 @@ PSEUDOMODULES += l2filter_blacklist
PSEUDOMODULES += l2filter_whitelist
PSEUDOMODULES += log
PSEUDOMODULES += log_printfnoformat
PSEUDOMODULES += lora
PSEUDOMODULES += lwip_arp
PSEUDOMODULES += lwip_autoip
PSEUDOMODULES += lwip_dhcp

View File

@ -375,6 +375,17 @@ typedef enum {
*/
NETOPT_INTEGRITY_CHECK,
/**
* @brief Get/Set the channel center frequency.
*
* Values are retrieved/passed as uint32_t in host byte order.
*
* For example, with LoRa, this corresponds to the center frequency of
* each channel (867300000, etc) for a given frequency band
* (868, 915, etc).
*/
NETOPT_CHANNEL_FREQUENCY,
/**
* @brief Enable/disable channel hopping.
*/

View File

@ -80,6 +80,7 @@ static const char *_netopt_strmap[] = {
[NETOPT_TX_TIMEOUT] = "NETOPT_TX_TIMEOUT",
[NETOPT_PREAMBLE_LENGTH] = "NETOPT_PREAMBLE_LENGTH",
[NETOPT_INTEGRITY_CHECK] = "NETOPT_INTEGRITY_CHECK",
[NETOPT_CHANNEL_FREQUENCY] = "NETOPT_CHANNEL_FREQUENCY",
[NETOPT_CHANNEL_HOP] = "NETOPT_CHANNEL_HOP",
[NETOPT_CHANNEL_HOP_PERIOD] = "NETOPT_CHANNEL_HOP_PERIOD",
[NETOPT_FIXED_HEADER] = "NETOPT_FIXED_HEADER",

View File

@ -24,6 +24,7 @@
#include "net/gnrc.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netif/hdr.h"
#include "net/lora.h"
#ifdef MODULE_NETSTATS
#include "net/netstats.h"
@ -68,6 +69,9 @@ static const struct {
{ "promisc", NETOPT_PROMISCUOUSMODE },
{ "raw", NETOPT_RAWMODE },
{ "rtr_adv", NETOPT_IPV6_SND_RTR_ADV },
{ "iq_invert", NETOPT_IQ_INVERT },
{ "rx_single", NETOPT_SINGLE_RECEIVE },
{ "chan_hop", NETOPT_CHANNEL_HOP }
};
/* utility functions */
@ -143,6 +147,7 @@ static void _set_usage(char *cmd_name)
" * \"addr_long\" - sets long address\n"
" * \"addr_short\" - alias for \"addr\"\n"
" * \"cca_threshold\" - set ED threshold during CCA in dBm\n"
" * \"freq\" - sets the \"channel\" center frequency\n"
" * \"channel\" - sets the frequency channel\n"
" * \"chan\" - alias for \"channel\"\n"
" * \"csma_retries\" - set max. number of channel access attempts\n"
@ -155,6 +160,9 @@ static void _set_usage(char *cmd_name)
" * \"page\" - set the channel page (IEEE 802.15.4)\n"
" * \"pan\" - alias for \"nid\"\n"
" * \"pan_id\" - alias for \"nid\"\n"
" * \"bw\" - alias for channel bandwidth\n"
" * \"sf\" - alias for spreading factor\n"
" * \"cr\" - alias for coding rate\n"
" * \"power\" - TX power in dBm\n"
" * \"retrans\" - max. number of retransmissions\n"
" * \"src_len\" - sets the source address length in byte\n"
@ -212,6 +220,10 @@ static void _print_netopt(netopt_t opt)
printf("channel");
break;
case NETOPT_CHANNEL_FREQUENCY:
printf("frequency [in Hz]");
break;
case NETOPT_CHANNEL_PAGE:
printf("page");
break;
@ -252,6 +264,18 @@ static void _print_netopt(netopt_t opt)
printf("encryption key");
break;
case NETOPT_BANDWIDTH:
printf("bandwidth");
break;
case NETOPT_SPREADING_FACTOR:
printf("spreading factor");
break;
case NETOPT_CODING_RATE:
printf("coding rate");
break;
default:
/* we don't serve these options here */
break;
@ -268,6 +292,19 @@ static const char *_netopt_state_str[] = {
[NETOPT_STATE_STANDBY] = "STANDBY"
};
static const char *_netopt_bandwidth_str[] = {
[LORA_BW_125_KHZ] = "125",
[LORA_BW_250_KHZ] = "250",
[LORA_BW_500_KHZ] = "500"
};
static const char *_netopt_coding_rate_str[] = {
[LORA_CR_4_5] = "4/5",
[LORA_CR_4_6] = "4/6",
[LORA_CR_4_7] = "4/7",
[LORA_CR_4_8] = "4/8"
};
/* for some lines threshold might just be 0, so we can't use _LINE_THRESHOLD
* here */
static unsigned _newline(unsigned threshold, unsigned line_thresh)
@ -346,6 +383,7 @@ static void _netif_list(kernel_pid_t iface)
ipv6_addr_t ipv6_groups[GNRC_NETIF_IPV6_GROUPS_NUMOF];
#endif
uint8_t hwaddr[GNRC_NETIF_L2ADDR_MAXLEN];
uint32_t u32;
uint16_t u16;
int16_t i16;
uint8_t u8;
@ -364,6 +402,10 @@ static void _netif_list(kernel_pid_t iface)
if (res >= 0) {
printf(" Channel: %" PRIu16 " ", u16);
}
res = gnrc_netapi_get(iface, NETOPT_CHANNEL_FREQUENCY, 0, &u32, sizeof(u32));
if (res >= 0) {
printf(" Frequency: %" PRIu32 "Hz ", u32);
}
res = gnrc_netapi_get(iface, NETOPT_CHANNEL_PAGE, 0, &u16, sizeof(u16));
if (res >= 0) {
printf(" Page: %" PRIu16 " ", u16);
@ -372,6 +414,18 @@ static void _netif_list(kernel_pid_t iface)
if (res >= 0) {
printf(" NID: 0x%" PRIx16, u16);
}
res = gnrc_netapi_get(iface, NETOPT_BANDWIDTH, 0, &u8, sizeof(u8));
if (res >= 0) {
printf(" BW: %skHz ", _netopt_bandwidth_str[u8]);
}
res = gnrc_netapi_get(iface, NETOPT_SPREADING_FACTOR, 0, &u8, sizeof(u8));
if (res >= 0) {
printf(" SF: %u ", u8);
}
res = gnrc_netapi_get(iface, NETOPT_CODING_RATE, 0, &u8, sizeof(u8));
if (res >= 0) {
printf(" CR: %s ", _netopt_coding_rate_str[u8]);
}
line_thresh = _newline(0U, line_thresh);
res = gnrc_netapi_get(iface, NETOPT_ADDRESS_LONG, 0, hwaddr, sizeof(hwaddr));
if (res >= 0) {
@ -422,6 +476,12 @@ static void _netif_list(kernel_pid_t iface)
line_thresh += _LINE_THRESHOLD + 1; /* enforce linebreak after this option */
line_thresh = _netif_list_flag(iface, NETOPT_AUTOCCA, "AUTOCCA",
line_thresh);
line_thresh = _netif_list_flag(iface, NETOPT_IQ_INVERT, "IQ_INVERT",
line_thresh);
line_thresh = _netif_list_flag(iface, NETOPT_SINGLE_RECEIVE, "RX_SINGLE",
line_thresh);
line_thresh = _netif_list_flag(iface, NETOPT_CHANNEL_HOP, "CHAN_HOP",
line_thresh);
#ifdef MODULE_GNRC_IPV6
res = gnrc_netapi_get(iface, NETOPT_MAX_PACKET_SIZE, GNRC_NETTYPE_IPV6, &u16, sizeof(u16));
if (res > 0) {
@ -514,6 +574,112 @@ static void _netif_list(kernel_pid_t iface)
puts("");
}
static int _netif_set_u32(kernel_pid_t iface, netopt_t opt, uint32_t context,
char *u32_str)
{
unsigned long int res;
bool hex = false;
if (_is_number(u32_str)) {
if ((res = strtoul(u32_str, NULL, 10)) == ULONG_MAX) {
puts("error: unable to parse value.\n"
"Must be a 32-bit unsigned integer (dec or hex)\n");
return 1;
}
}
else {
if ((res = strtoul(u32_str, NULL, 32)) == ULONG_MAX) {
puts("error: unable to parse value.\n"
"Must be a 32-bit unsigned integer (dec or hex)\n");
return 1;
}
hex = true;
}
assert(res <= ULONG_MAX);
if (gnrc_netapi_set(iface, opt, context, (uint32_t *)&res,
sizeof(uint32_t)) < 0) {
printf("error: unable to set ");
_print_netopt(opt);
puts("");
return 1;
}
printf("success: set ");
_print_netopt(opt);
printf(" on interface %" PRIkernel_pid " to ", iface);
if (hex) {
printf("0x%04lx\n", res);
}
else {
printf("%lu\n", res);
}
return 0;
}
static int _netif_set_bandwidth(kernel_pid_t iface, char *value)
{
uint8_t bw;
if (strcmp("125", value) == 0) {
bw = LORA_BW_125_KHZ;
}
else if (strcmp("250", value) == 0) {
bw = LORA_BW_250_KHZ;
}
else if (strcmp("500", value) == 0) {
bw = LORA_BW_500_KHZ;
}
else {
puts("usage: ifconfig <if_id> set bw [125|250|500]");
return 1;
}
if (gnrc_netapi_set(iface, NETOPT_BANDWIDTH, 0,
&bw, sizeof(uint8_t)) < 0) {
printf("error: unable to set bandwidth to %s\n", value);
return 1;
}
printf("success: set bandwidth of interface %" PRIkernel_pid " to %s\n",
iface, value);
return 0;
}
static int _netif_set_coding_rate(kernel_pid_t iface, char *value)
{
uint8_t cr;
if (strcmp("4/5", value) == 0) {
cr = LORA_CR_4_5;
}
else if (strcmp("4/6", value) == 0) {
cr = LORA_CR_4_6;
}
else if (strcmp("4/7", value) == 0) {
cr = LORA_CR_4_7;
}
else if (strcmp("4/8", value) == 0) {
cr = LORA_CR_4_8;
}
else {
puts("usage: ifconfig <if_id> set cr [4/5|4/6|4/7|4/8]");
return 1;
}
if (gnrc_netapi_set(iface, NETOPT_CODING_RATE, 0,
&cr, sizeof(uint8_t)) < 0) {
printf("error: unable to set coding rate to %s\n", value);
return 1;
}
printf("success: set coding rate of interface %" PRIkernel_pid " to %s\n",
iface, value);
return 0;
}
static int _netif_set_u16(kernel_pid_t iface, netopt_t opt, uint16_t context,
char *u16_str)
{
@ -817,6 +983,18 @@ static int _netif_set(char *cmd_name, kernel_pid_t iface, char *key, char *value
else if (strcmp("cca_threshold", key) == 0) {
return _netif_set_u8(iface, NETOPT_CCA_THRESHOLD, 0, value);
}
else if ((strcmp("frequency", key) == 0) || (strcmp("freq", key) == 0)) {
return _netif_set_u32(iface, NETOPT_CHANNEL_FREQUENCY, 0, value);
}
else if ((strcmp("bandwidth", key) == 0) || (strcmp("bw", key) == 0)) {
return _netif_set_bandwidth(iface, value);
}
else if ((strcmp("spreading_factor", key) == 0) || (strcmp("sf", key) == 0)) {
return _netif_set_u8(iface, NETOPT_SPREADING_FACTOR, 0, value);
}
else if ((strcmp("coding_rate", key) == 0) || (strcmp("cr", key) == 0)) {
return _netif_set_coding_rate(iface, value);
}
else if ((strcmp("channel", key) == 0) || (strcmp("chan", key) == 0)) {
return _netif_set_u16(iface, NETOPT_CHANNEL, 0, value);
}

View File

@ -4,7 +4,8 @@ FEATURES_REQUIRED = periph_spi periph_gpio
BOARD_INSUFFICIENT_MEMORY := msb-430 msb-430h nucleo-f334 nucleo-l053 \
nucleo32-f031 nucleo32-f042 nucleo32-l031 \
stm32f0discovery telosb wsn430-v1_3b wsn430-v1_4
stm32f0discovery telosb wsn430-v1_3b wsn430-v1_4 \
z1
USEMODULE += auto_init_gnrc_netif
USEMODULE += enc28j60