Merge pull request #4400 from OlegHahm/netif_exist

gnrc netif: add a check for interface existence
This commit is contained in:
Cenk Gündoğan 2015-12-04 14:26:35 +01:00
commit de8ea8b206
3 changed files with 40 additions and 2 deletions

View File

@ -20,11 +20,13 @@
* @brief Definitions for GNRC's network interfaces
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
* @author Oliver Hahm <oliver.hahm@inria.fr>
*/
#ifndef GNRC_NETIF_H_
#define GNRC_NETIF_H_
#include <stdlib.h>
#include <stdbool.h>
#include "kernel_types.h"
#ifdef __cplusplus
@ -90,6 +92,16 @@ void gnrc_netif_remove(kernel_pid_t pid);
*/
size_t gnrc_netif_get(kernel_pid_t *netifs);
/**
* @brief Check if an interface exist.
*
* @param[in] pid The PID to be checked.
*
* @return True, if an interface @p pid exists.
* @return False, otherwise
*/
bool gnrc_netif_exist(kernel_pid_t pid);
/**
* @brief Converts a hardware address to a human readable string.
*

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2015 Martine Lenders <mlenders@inf.fu-berlin.de>
* Copyright (C) 2015 INRIA
*
* 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
@ -12,8 +13,6 @@
* @file
*/
#include <stdio.h>
#include <errno.h>
#include "kernel_types.h"
#include "net/gnrc/netif.h"
@ -98,4 +97,14 @@ size_t gnrc_netif_get(kernel_pid_t *netifs)
return size;
}
bool gnrc_netif_exist(kernel_pid_t pid)
{
for (int i = 0; i < GNRC_NETIF_NUMOF; i++) {
if (ifs[i] == pid) {
return true;
}
}
return false;
}
/** @} */

View File

@ -159,6 +159,22 @@ static void test_ng_netif_get__full(void)
TEST_ASSERT_EQUAL_INT(GNRC_NETIF_NUMOF, size);
}
static void test_ng_netif_exist(void)
{
TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(0));
TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(1));
TEST_ASSERT_EQUAL_INT(0, gnrc_netif_add(TEST_UINT8));
for (int i = 0; i < UINT8_MAX; i++) {
if ((i == 0) || (i == 1) || (i == TEST_UINT8)) {
TEST_ASSERT(gnrc_netif_exist(i));
}
else {
TEST_ASSERT(!gnrc_netif_exist(i));
}
}
}
static void test_ng_netif_addr_to_str__out_too_short(void)
{
static const uint8_t addr[] = {0x05, 0xcd};
@ -280,6 +296,7 @@ Test *tests_netif_tests(void)
new_TestFixture(test_ng_netif_get__empty),
new_TestFixture(test_ng_netif_get__success_3_minus_one),
new_TestFixture(test_ng_netif_get__full),
new_TestFixture(test_ng_netif_exist),
new_TestFixture(test_ng_netif_addr_to_str__out_too_short),
new_TestFixture(test_ng_netif_addr_to_str__success),
new_TestFixture(test_ng_netif_addr_from_str__out_too_short),