mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-18 12:52:44 +01:00
sys/net/netif: add netif_get_by_name_buffer function
This commit is contained in:
parent
5c2b599efa
commit
d8cf157b26
@ -34,6 +34,7 @@
|
||||
#define NET_NETIF_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "list.h"
|
||||
#include "net/netopt.h"
|
||||
@ -113,6 +114,19 @@ int netif_get_name(netif_t *netif, char *name);
|
||||
*/
|
||||
int16_t netif_get_id(const netif_t *netif);
|
||||
|
||||
/**
|
||||
* @brief Gets interface by name, from a buffer
|
||||
*
|
||||
* @pre `name != NULL`
|
||||
*
|
||||
* @param[in] name The name of an interface as an array of chars. Must not be `NULL`.
|
||||
* @param[in] name_len Number of characters in @p name.
|
||||
*
|
||||
* @return Pointer to the interface that matches the name
|
||||
* @retval NULL if no interface is named @p name.
|
||||
*/
|
||||
netif_t *netif_get_by_name_buffer(const char *name, size_t name_len);
|
||||
|
||||
/**
|
||||
* @brief Gets interface by name
|
||||
*
|
||||
@ -125,7 +139,10 @@ int16_t netif_get_id(const netif_t *netif);
|
||||
* @return The interface on success.
|
||||
* @return NULL if no interface is named @p name.
|
||||
*/
|
||||
netif_t *netif_get_by_name(const char *name);
|
||||
static inline netif_t *netif_get_by_name(const char *name)
|
||||
{
|
||||
return netif_get_by_name_buffer(name, strlen(name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets interface by a numeric identifier.
|
||||
|
@ -56,16 +56,22 @@ __attribute__((weak)) int16_t netif_get_id(const netif_t *netif)
|
||||
return -1;
|
||||
}
|
||||
|
||||
netif_t *netif_get_by_name(const char *name)
|
||||
netif_t *netif_get_by_name_buffer(const char *name, size_t name_len)
|
||||
{
|
||||
assert(name);
|
||||
|
||||
if (name_len > CONFIG_NETIF_NAMELENMAX) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
list_node_t *node = netif_list.next;
|
||||
|
||||
char tmp[CONFIG_NETIF_NAMELENMAX];
|
||||
|
||||
while (node) {
|
||||
netif_get_name((netif_t *)node, tmp);
|
||||
if (strncmp(name, tmp, CONFIG_NETIF_NAMELENMAX) == 0) {
|
||||
size_t len = strlen(tmp);
|
||||
if ((len == name_len) && (strncmp(name, tmp, name_len) == 0)) {
|
||||
return (netif_t *)node;
|
||||
}
|
||||
node = node->next;
|
||||
|
Loading…
Reference in New Issue
Block a user