zephyr/subsys/net/lib/shell/iface_dynamic.h
Jukka Rissanen 10def6cac5 net: shell: Allow dynamic interface name expansion
Allow multiple commands use the same dynamic shell command
completion when expecting network interface index.

For example "net iface" and "net stats" are such commands.

The network interface expansion cannot be used in "net ipv6 add",
"net ipv4 add" and "net route" commands as they require more
data after the network interface index argument.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2023-10-23 10:40:28 +02:00

76 lines
1.8 KiB
C

/*
* Copyright (c) 2016 Intel Corporation
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
/* Dynamic shell command completion for network interface.
* This is be used by multiple commands.
*/
#define MAX_IFACE_HELP_STR_LEN sizeof("longbearername (0xabcd0123)")
#define MAX_IFACE_STR_LEN sizeof("xxx")
static char iface_help_buffer[MAX_IFACE_COUNT][MAX_IFACE_HELP_STR_LEN];
static char iface_index_buffer[MAX_IFACE_COUNT][MAX_IFACE_STR_LEN];
static void iface_index_get(size_t idx, struct shell_static_entry *entry);
SHELL_DYNAMIC_CMD_CREATE(iface_index, iface_index_get);
static char *set_iface_index_buffer(size_t idx)
{
struct net_if *iface = net_if_get_by_index(idx);
/* Network interfaces start at 1 */
if (idx == 0) {
return "";
}
if (!iface) {
return NULL;
}
snprintk(iface_index_buffer[idx], MAX_IFACE_STR_LEN, "%d", (uint8_t)idx);
return iface_index_buffer[idx];
}
static char *set_iface_index_help(size_t idx)
{
struct net_if *iface = net_if_get_by_index(idx);
/* Network interfaces start at 1 */
if (idx == 0) {
return "";
}
if (!iface) {
return NULL;
}
#if defined(CONFIG_NET_INTERFACE_NAME)
char name[CONFIG_NET_INTERFACE_NAME_LEN + 1];
net_if_get_name(iface, name, CONFIG_NET_INTERFACE_NAME_LEN);
name[CONFIG_NET_INTERFACE_NAME_LEN] = '\0';
snprintk(iface_help_buffer[idx], MAX_IFACE_HELP_STR_LEN,
"%s [%s] (%p)", name, iface2str(iface, NULL), iface);
#else
snprintk(iface_help_buffer[idx], MAX_IFACE_HELP_STR_LEN,
"[%s] (%p)", iface2str(iface, NULL), iface);
#endif
return iface_help_buffer[idx];
}
static void iface_index_get(size_t idx, struct shell_static_entry *entry)
{
entry->handler = NULL;
entry->help = set_iface_index_help(idx);
entry->subcmd = &iface_index;
entry->syntax = set_iface_index_buffer(idx);
}