Skip to content

Commit 4ef741f

Browse files
committed
netif: add functions to get and get by identifier
1 parent 18ad1e7 commit 4ef741f

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

sys/include/net/netif.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ netif_t *netif_iter(netif_t *last);
8989

9090
int netif_get_name(netif_t *netif, char *name);
9191

92+
/**
93+
* @brief Gets the numeric identifier of an interface
94+
*
95+
* @param[in] netif A network interface.
96+
*
97+
* @return The numeric identifier of an interface
98+
* @return -1 if @p netif is not registered
99+
*/
100+
int16_t netif_get_id(const netif_t *netif);
101+
92102
/**
93103
* @brief Gets interface by name
94104
*
@@ -103,6 +113,16 @@ int netif_get_name(netif_t *netif, char *name);
103113
*/
104114
netif_t *netif_get_by_name(const char *name);
105115

116+
/**
117+
* @brief Gets interface by a numeric identifier.
118+
*
119+
* @param[in] id A numeric identifier.
120+
*
121+
* @return The interface on success.
122+
* @return NULL if no interface with identifier @p id.
123+
*/
124+
netif_t *netif_get_by_id(int16_t id);
125+
106126
/**
107127
* @brief Gets option from an interface
108128
*

sys/net/netif/netif.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ netif_t *netif_iter(netif_t *last)
4040
return (netif_t *)last->node.next;
4141
}
4242

43+
int16_t netif_get_id(const netif_t *netif)
44+
{
45+
list_node_t *node = netif_list.next;
46+
for (int16_t i = 0; node; i++, node = node->next) {
47+
if (netif == (netif_t *)node) {
48+
return i;
49+
}
50+
}
51+
return -1;
52+
}
53+
4354
netif_t *netif_get_by_name(const char *name)
4455
{
4556
assert(name);
@@ -57,4 +68,16 @@ netif_t *netif_get_by_name(const char *name)
5768

5869
return NULL;
5970
}
71+
72+
int16_t netif_get_by_id(int16_t netif)
73+
{
74+
list_node_t *node = netif_list.next;
75+
for (int16_t i = 0; node; i++, node = node->next) {
76+
if (i == id) {
77+
return (netif_t *)node;
78+
}
79+
}
80+
return -1;
81+
}
82+
6083
/** @} */

0 commit comments

Comments
 (0)