Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmds_gnrc_netif: Support enabling/disabling lwIP netifs from gnrc_netif shell #19972

Merged
merged 5 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions pkg/lwip/contrib/_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "fmt.h"
#include "lwip/netif/compat.h"
#include "lwip/netifapi.h"
#include "net/netif.h"

int netif_get_name(netif_t *iface, char *name)
Expand All @@ -42,6 +43,17 @@ int netif_get_opt(netif_t *iface, netopt_t opt, uint16_t context,
struct netdev *dev = netif->state;
int res = -ENOTSUP;
switch (opt) {
case NETOPT_ACTIVE: {
assert(max_len >= sizeof(netopt_enable_t));
netopt_enable_t *tgt = value;
if (netif_is_up(netif)) {
*tgt = NETOPT_ENABLE;
} else {
*tgt = NETOPT_DISABLE;
}
res = 0;
}
break;
#ifdef MODULE_LWIP_IPV6
case NETOPT_IPV6_ADDR: {
assert(max_len >= sizeof(ipv6_addr_t));
Expand Down Expand Up @@ -74,13 +86,28 @@ int netif_get_opt(netif_t *iface, netopt_t opt, uint16_t context,
int netif_set_opt(netif_t *iface, netopt_t opt, uint16_t context,
void *value, size_t value_len)
{
(void)iface;
(void)opt;
(void)context;
(void)value;
(void)value_len;
int res = -ENOTSUP;
lwip_netif_t *lwip_netif = (lwip_netif_t*) iface;
struct netif *netif = &lwip_netif->lwip_netif;

return -ENOTSUP;
switch (opt) {
case NETOPT_ACTIVE: {
assert(value_len >= sizeof(netopt_enable_t));
netopt_enable_t *state = value;
if (*state == NETOPT_ENABLE) {
netifapi_netif_set_up(netif);
} else {
netifapi_netif_set_down(netif);
}
res = 0;
}
break;
default:
break;
}

return res;
}

/** @} */
10 changes: 10 additions & 0 deletions sys/include/net/netopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,16 @@
*/
NETOPT_LINK,

/**
* @brief (@ref netopt_enable_t) network interface status.
*
* This option is used check state or to enable/disable the interface,
* regardless of link status.
*
* @note On error this option should return a negative number.
*/
NETOPT_ACTIVE,

/**
* @brief (@ref netopt_enable_t) CSMA/CA support
*
Expand Down Expand Up @@ -1018,7 +1028,7 @@
/**
* @brief Basic callback type on network disconnection @ref NETOPT_CONNECT
*/
typedef void (*netopt_on_disconnect_result_t) (void *netif, const struct netopt_disconnect_result *res);

Check warning on line 1031 in sys/include/net/netopt.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters

/**
* @brief Basic network connect request
Expand Down
1 change: 1 addition & 0 deletions sys/net/crosslayer/netopt/netopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static const char *_netopt_strmap[] = {
[NETOPT_MAC_NO_SLEEP] = "NETOPT_MAC_NO_SLEEP",
[NETOPT_IS_WIRED] = "NETOPT_IS_WIRED",
[NETOPT_LINK] = "NETOPT_LINK",
[NETOPT_ACTIVE] = "NETOPT_ACTIVE",
[NETOPT_DEVICE_TYPE] = "NETOPT_DEVICE_TYPE",
[NETOPT_CHANNEL_PAGE] = "NETOPT_CHANNEL_PAGE",
[NETOPT_CCA_THRESHOLD] = "NETOPT_CCA_THRESHOLD",
Expand Down
25 changes: 19 additions & 6 deletions sys/shell/cmds/gnrc_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -776,11 +776,17 @@
}
}
#endif /* MODULE_NETDEV_IEEE802154 */
netopt_enable_t link;
res = netif_get_opt(iface, NETOPT_LINK, 0, &link, sizeof(netopt_enable_t));
netopt_enable_t enabled;
res = netif_get_opt(iface, NETOPT_LINK, 0, &enabled, sizeof(enabled));
if (res >= 0) {
printf(" Link: %s ", (link == NETOPT_ENABLE) ? "up" : "down" );
printf(" Link: %s ", (enabled == NETOPT_ENABLE) ? "up" : "down" );
}
#if IS_USED(MODULE_LWIP_NETIF) /* only supported on lwIP for now */
res = netif_get_opt(iface, NETOPT_ACTIVE, 0, &enabled, sizeof(enabled));
if (res >= 0) {
printf(" State: %s ", (enabled == NETOPT_ENABLE) ? "up" : "down" );
}
#endif /* MODULE_LWIP_NETIF */
line_thresh = _newline(0U, line_thresh);
res = netif_get_opt(iface, NETOPT_ADDRESS_LONG, 0, hwaddr, sizeof(hwaddr));
if (res >= 0) {
Expand All @@ -806,9 +812,9 @@
}
res = netif_get_opt(iface, NETOPT_CSMA_RETRIES, 0, &u8, sizeof(u8));
if (res >= 0) {
netopt_enable_t enable = NETOPT_DISABLE;
res = netif_get_opt(iface, NETOPT_CSMA, 0, &enable, sizeof(enable));
if ((res >= 0) && (enable == NETOPT_ENABLE)) {
enabled = NETOPT_DISABLE;
res = netif_get_opt(iface, NETOPT_CSMA, 0, &enabled, sizeof(enabled));
if ((res >= 0) && (enabled == NETOPT_ENABLE)) {
printf(" CSMA Retries: %u ", (unsigned)u8);
}
line_thresh++;
Expand Down Expand Up @@ -1678,10 +1684,17 @@

static int _netif_link(netif_t *iface, netopt_enable_t en)
{
#if IS_USED(MODULE_LWIP_NETIF) /* lwIP sets netif state, not link state */
if (netif_set_opt(iface, NETOPT_ACTIVE, 0, &en, sizeof(en)) < 0) {
printf("error: unable to set state %s\n", en == NETOPT_ENABLE ? "up" : "down");
return 1;
}
#else
if (netif_set_opt(iface, NETOPT_LINK, 0, &en, sizeof(en)) < 0) {
printf("error: unable to set link %s\n", en == NETOPT_ENABLE ? "up" : "down");
return 1;
}
#endif
return 0;
}

Expand Down Expand Up @@ -1991,4 +2004,4 @@
return 1;
}

SHELL_COMMAND(ifconfig, "Configure network interfaces", _gnrc_netif_config);

Check warning on line 2007 in sys/shell/cmds/gnrc_netif.c

View workflow job for this annotation

GitHub Actions / static-tests

source file is too long
Loading