

8 changed files with 381 additions and 0 deletions
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @defgroup ng_netif Network interfaces |
||||
* @ingroup net |
||||
* @brief Abstraction layer for network interfaces (i. .e. threads for |
||||
* protocols that are below the network layer) |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @brief Definitions for network interfaces |
||||
* |
||||
* @author Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
*/ |
||||
#ifndef NG_NETIF_H_ |
||||
#define NG_NETIF_H_ |
||||
|
||||
#include "kernel_types.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/**
|
||||
* @brief Maximum number of network interfaces |
||||
*/ |
||||
#ifndef NG_NETIF_NUMOF |
||||
#define NG_NETIF_NUMOF (1) |
||||
#endif |
||||
|
||||
/**
|
||||
* @brief The add/remove operation to set network layer protocol |
||||
* specific options for an interface. |
||||
* |
||||
* @param[in] pid The PID to the new interface. |
||||
*/ |
||||
typedef void (*ng_netif_op_t)(kernel_pid_t pid); |
||||
|
||||
/**
|
||||
* @brief The add and remove handlers to set network layer protocol |
||||
* specific options for an interface. |
||||
* |
||||
* @details If you implement a pair, please add it to the list in ng_netif.c |
||||
* statically. |
||||
*/ |
||||
typedef struct { |
||||
ng_netif_op_t add; /**< The add operation */ |
||||
ng_netif_op_t remove; /**< The remove operation */ |
||||
} ng_netif_handler_t; |
||||
|
||||
/**
|
||||
* @brief Initializes module. |
||||
*/ |
||||
void ng_netif_init(void); |
||||
|
||||
/**
|
||||
* @brief Adds a thread as interface. |
||||
* |
||||
* @param[in] pid PID of the added thread. |
||||
* |
||||
* @return 0, on success, |
||||
* @return -ENOMEM, if maximum number of interfaces has been exceeded. |
||||
*/ |
||||
int ng_netif_add(kernel_pid_t pid); |
||||
|
||||
/**
|
||||
* @brief Removes a thread as interface. |
||||
* |
||||
* @param[in] pid PID of the removed thread. |
||||
*/ |
||||
void ng_netif_remove(kernel_pid_t pid); |
||||
|
||||
/**
|
||||
* @brief Get all active interfaces. |
||||
* |
||||
* @param[out] size Size of the resulting array. |
||||
* |
||||
* @return All active interfaces. If @p size is 0 on return, the return value |
||||
* is undefined. No order must be ensured. |
||||
*/ |
||||
kernel_pid_t *ng_netif_get(size_t *size); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* NG_NETIF_H_ */ |
||||
/** @} */ |
@ -0,0 +1 @@
|
||||
include $(RIOTBASE)/Makefile.base |
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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/ng_netif.h" |
||||
|
||||
static ng_netif_handler_t if_handler[] = { |
||||
#ifdef MODULE_NG_IPV6 |
||||
{ ipv6_if_add, ipv6_if_remove }, |
||||
#endif |
||||
/* #ifdef MODULE_NG_IPV4
|
||||
* { ipv4_if_add, ipv4_if_remove }, |
||||
* #endif ... you get the idea |
||||
*/ |
||||
{ NULL, NULL } |
||||
}; |
||||
|
||||
static kernel_pid_t ifs[NG_NETIF_NUMOF]; |
||||
|
||||
void ng_netif_init(void) |
||||
{ |
||||
for (int i = 0; i < NG_NETIF_NUMOF; i++) { |
||||
ifs[i] = KERNEL_PID_UNDEF; |
||||
} |
||||
} |
||||
|
||||
int ng_netif_add(kernel_pid_t pid) |
||||
{ |
||||
for (int i = 0; i < NG_NETIF_NUMOF; i++) { |
||||
if (ifs[i] == pid) { /* avoid duplicates */ |
||||
return 0; |
||||
} |
||||
else if (ifs[i] == KERNEL_PID_UNDEF) { |
||||
ifs[i] = pid; |
||||
|
||||
for (int j = 0; if_handler[j].add != NULL; j++) { |
||||
if_handler[j].add(pid); |
||||
} |
||||
|
||||
return 0; |
||||
} |
||||
} |
||||
|
||||
return -ENOMEM; |
||||
} |
||||
|
||||
void ng_netif_remove(kernel_pid_t pid) |
||||
{ |
||||
for (int i = 0; i < NG_NETIF_NUMOF; i++) { |
||||
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; |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
kernel_pid_t *ng_netif_get(size_t *size) |
||||
{ |
||||
for (*size = 0; |
||||
(*size < NG_NETIF_NUMOF) && (ifs[*size] != KERNEL_PID_UNDEF); |
||||
(*size)++); |
||||
|
||||
return ifs; |
||||
} |
||||
|
||||
/** @} */ |
@ -0,0 +1,3 @@
|
||||
MODULE = tests-netif
|
||||
|
||||
include $(RIOTBASE)/Makefile.base |
@ -0,0 +1 @@
|
||||
USEMODULE += ng_netif
|
@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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 tests-netif.c |
||||
*/ |
||||
#include <errno.h> |
||||
#include <stdint.h> |
||||
#include <stdlib.h> |
||||
|
||||
#include "embUnit/embUnit.h" |
||||
#include "kernel_types.h" |
||||
#include "net/ng_netif.h" |
||||
|
||||
#include "tests-netif.h" |
||||
|
||||
static void set_up(void) |
||||
{ |
||||
ng_netif_init(); |
||||
} |
||||
|
||||
static void test_ng_netif_add__KERNEL_PID_UNDEF(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, ng_netif_add(KERNEL_PID_UNDEF)); |
||||
ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(0, size); |
||||
} |
||||
|
||||
static void test_ng_netif_add__memfull(void) |
||||
{ |
||||
for (int i = 0; i < NG_NETIF_NUMOF; i++) { |
||||
TEST_ASSERT_EQUAL_INT(0, ng_netif_add(TEST_UINT8 + i)); |
||||
} |
||||
|
||||
TEST_ASSERT_EQUAL_INT(-ENOMEM, ng_netif_add(TEST_UINT8 - 1)); |
||||
} |
||||
|
||||
static void test_ng_netif_add__success(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
kernel_pid_t *res = NULL; |
||||
|
||||
TEST_ASSERT_EQUAL_INT(0, ng_netif_add(TEST_UINT8)); |
||||
|
||||
res = ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(1, size); |
||||
TEST_ASSERT_EQUAL_INT(TEST_UINT8, res[0]); |
||||
} |
||||
|
||||
static void test_ng_netif_add__duplicate_entry(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
kernel_pid_t *res = NULL; |
||||
|
||||
for (int i = 0; i < NG_NETIF_NUMOF + 4; i++) { |
||||
TEST_ASSERT_EQUAL_INT(0, ng_netif_add(TEST_UINT8)); |
||||
} |
||||
|
||||
res = ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(1, size); |
||||
TEST_ASSERT_EQUAL_INT(TEST_UINT8, res[0]); |
||||
} |
||||
|
||||
static void test_ng_netif_remove__KERNEL_PID_UNDEF(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
kernel_pid_t *res = NULL; |
||||
|
||||
test_ng_netif_add__success(); |
||||
|
||||
ng_netif_remove(KERNEL_PID_UNDEF); |
||||
|
||||
res = ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(1, size); |
||||
TEST_ASSERT_EQUAL_INT(TEST_UINT8, res[0]); |
||||
} |
||||
|
||||
static void test_ng_netif_remove__not_an_if(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
kernel_pid_t *res = NULL; |
||||
|
||||
test_ng_netif_add__success(); |
||||
|
||||
ng_netif_remove(TEST_UINT8 + 1); |
||||
|
||||
res = ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(1, size); |
||||
TEST_ASSERT_EQUAL_INT(TEST_UINT8, res[0]); |
||||
} |
||||
|
||||
static void test_ng_netif_remove__success(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
|
||||
test_ng_netif_add__success(); |
||||
|
||||
ng_netif_remove(TEST_UINT8); |
||||
|
||||
ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(0, size); |
||||
} |
||||
|
||||
static void test_ng_netif_get__empty(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
|
||||
ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(0, size); |
||||
} |
||||
|
||||
static void test_ng_netif_get__full(void) |
||||
{ |
||||
size_t size = TEST_UINT8; |
||||
|
||||
for (int i = 0; i < NG_NETIF_NUMOF; i++) { |
||||
TEST_ASSERT_EQUAL_INT(0, ng_netif_add(TEST_UINT8 + i)); |
||||
} |
||||
|
||||
ng_netif_get(&size); |
||||
TEST_ASSERT_EQUAL_INT(NG_NETIF_NUMOF, size); |
||||
} |
||||
|
||||
Test *tests_netif_tests(void) |
||||
{ |
||||
EMB_UNIT_TESTFIXTURES(fixtures) { |
||||
new_TestFixture(test_ng_netif_add__KERNEL_PID_UNDEF), |
||||
new_TestFixture(test_ng_netif_add__memfull), |
||||
new_TestFixture(test_ng_netif_add__success), |
||||
new_TestFixture(test_ng_netif_add__duplicate_entry), |
||||
new_TestFixture(test_ng_netif_remove__KERNEL_PID_UNDEF), |
||||
new_TestFixture(test_ng_netif_remove__not_an_if), |
||||
new_TestFixture(test_ng_netif_remove__success), |
||||
new_TestFixture(test_ng_netif_get__empty), |
||||
new_TestFixture(test_ng_netif_get__full), |
||||
}; |
||||
|
||||
EMB_UNIT_TESTCALLER(netif_tests, set_up, NULL, fixtures); |
||||
|
||||
return (Test *)&netif_tests; |
||||
} |
||||
|
||||
void tests_netif(void) |
||||
{ |
||||
TESTS_RUN(tests_netif_tests()); |
||||
} |
||||
/** @} */ |
@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2014 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. |
||||
*/ |
||||
|
||||
/**
|
||||
* @addtogroup unittests |
||||
* @{ |
||||
* |
||||
* @file |
||||
* @brief Unittests for the ``netif`` module |
||||
* |
||||
* @author Martine Lenders <mlenders@inf.fu-berlin.de> |
||||
*/ |
||||
#ifndef __TESTS_PKTBUF_H_ |
||||
#define __TESTS_PKTBUF_H_ |
||||
|
||||
#include "embUnit.h" |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
/**
|
||||
* @brief The entry point of this test suite. |
||||
*/ |
||||
void tests_netif(void); |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* __TESTS_PKTBUF_H_ */ |
||||
/** @} */ |
Loading…
Reference in new issue