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

102 lines
1.9 KiB
C
Raw Normal View History

2015-02-09 19:42:36 +01:00
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @{
*
* @file
*/
#include <stdio.h>
#include <errno.h>
#include "kernel_types.h"
#include "net/gnrc/netif.h"
2015-02-09 19:42:36 +01:00
#ifdef MODULE_GNRC_IPV6_NETIF
#include "net/gnrc/ipv6/netif.h"
2015-02-11 14:10:34 +01:00
#endif
static gnrc_netif_handler_t if_handler[] = {
#ifdef MODULE_GNRC_IPV6_NETIF
{ gnrc_ipv6_netif_add, gnrc_ipv6_netif_remove },
2015-02-09 19:42:36 +01:00
#endif
/* #ifdef MODULE_GNRC_IPV4_NETIF
2015-02-11 14:10:34 +01:00
* { ipv4_netif_add, ipv4_netif_remove },
2015-02-09 19:42:36 +01:00
* #endif ... you get the idea
*/
{ NULL, NULL }
};
static kernel_pid_t ifs[GNRC_NETIF_NUMOF];
2015-02-09 19:42:36 +01:00
void gnrc_netif_init(void)
2015-02-09 19:42:36 +01:00
{
for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
2015-02-09 19:42:36 +01:00
ifs[i] = KERNEL_PID_UNDEF;
}
}
int gnrc_netif_add(kernel_pid_t pid)
2015-02-09 19:42:36 +01:00
{
kernel_pid_t *free_entry = NULL;
for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
if (ifs[i] == pid) {
2015-02-09 19:42:36 +01:00
return 0;
}
else if (ifs[i] == KERNEL_PID_UNDEF && !free_entry) {
free_entry = &ifs[i];
}
}
2015-02-09 19:42:36 +01:00
if (!free_entry) {
return -ENOMEM;
}
2015-02-09 19:42:36 +01:00
*free_entry = pid;
for (int j = 0; if_handler[j].add != NULL; j++) {
if_handler[j].add(pid);
2015-02-09 19:42:36 +01:00
}
return 0;
2015-02-09 19:42:36 +01:00
}
void gnrc_netif_remove(kernel_pid_t pid)
2015-02-09 19:42:36 +01:00
{
2015-03-16 16:13:03 +01:00
int i;
for (i = 0; i < GNRC_NETIF_NUMOF; i++) {
2015-02-09 19:42:36 +01:00
if (ifs[i] == pid) {
ifs[i] = KERNEL_PID_UNDEF;
for (int j = 0; if_handler[j].remove != NULL; j++) {
if_handler[j].remove(pid);
}
return;
2015-02-09 19:42:36 +01:00
}
}
}
size_t gnrc_netif_get(kernel_pid_t *netifs)
2015-02-09 19:42:36 +01:00
{
size_t size = 0;
2015-02-09 19:42:36 +01:00
for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
if (ifs[i] != KERNEL_PID_UNDEF) {
netifs[size++] = ifs[i];
}
}
return size;
2015-02-09 19:42:36 +01:00
}
/** @} */