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"
|
2015-08-17 15:41:29 +02:00
|
|
|
#include "net/gnrc/netif.h"
|
2015-02-09 19:42:36 +01:00
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
#ifdef MODULE_GNRC_IPV6_NETIF
|
|
|
|
#include "net/gnrc/ipv6/netif.h"
|
2015-02-11 14:10:34 +01:00
|
|
|
#endif
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
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
|
2015-08-17 15:41:29 +02:00
|
|
|
/* #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 }
|
|
|
|
};
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
static kernel_pid_t ifs[GNRC_NETIF_NUMOF];
|
2015-02-09 19:42:36 +01:00
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
void gnrc_netif_init(void)
|
2015-02-09 19:42:36 +01:00
|
|
|
{
|
2015-08-17 15:41:29 +02:00
|
|
|
for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
|
2015-02-09 19:42:36 +01:00
|
|
|
ifs[i] = KERNEL_PID_UNDEF;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
int gnrc_netif_add(kernel_pid_t pid)
|
2015-02-09 19:42:36 +01:00
|
|
|
{
|
2015-05-19 17:56:11 +02:00
|
|
|
kernel_pid_t *free_entry = NULL;
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
|
2015-05-19 17:56:11 +02:00
|
|
|
if (ifs[i] == pid) {
|
2015-02-09 19:42:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-19 17:56:11 +02:00
|
|
|
else if (ifs[i] == KERNEL_PID_UNDEF && !free_entry) {
|
|
|
|
free_entry = &ifs[i];
|
|
|
|
}
|
|
|
|
}
|
2015-02-09 19:42:36 +01:00
|
|
|
|
2015-05-19 17:56:11 +02:00
|
|
|
if (!free_entry) {
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
2015-02-09 19:42:36 +01:00
|
|
|
|
2015-05-19 17:56:11 +02: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
|
|
|
}
|
|
|
|
|
2015-05-19 17:56:11 +02:00
|
|
|
return 0;
|
2015-02-09 19:42:36 +01:00
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02: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;
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2015-05-26 19:33:09 +02:00
|
|
|
return;
|
2015-02-09 19:42:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
size_t gnrc_netif_get(kernel_pid_t *netifs)
|
2015-02-09 19:42:36 +01:00
|
|
|
{
|
2015-05-25 22:00:44 +02:00
|
|
|
size_t size = 0;
|
2015-02-09 19:42:36 +01:00
|
|
|
|
2015-08-17 15:41:29 +02:00
|
|
|
for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
|
2015-05-25 22:00:44 +02:00
|
|
|
if (ifs[i] != KERNEL_PID_UNDEF) {
|
|
|
|
netifs[size++] = ifs[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
2015-02-09 19:42:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @} */
|