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

sys/test_utils: add netdev Ethernet minimal processing

This commit is contained in:
Leandro Lanzieri 2022-03-16 14:38:46 +01:00
parent a2e5934ec9
commit 48ae24b545
No known key found for this signature in database
GPG Key ID: F4E9A721761C7593
9 changed files with 366 additions and 0 deletions

View File

@ -188,6 +188,9 @@ endif
ifneq (,$(filter test_utils_interactive_sync,$(USEMODULE)))
DIRS += test_utils/interactive_sync
endif
ifneq (,$(filter test_utils_netdev_eth_minimal,$(USEMODULE)))
DIRS += test_utils/netdev_eth_minimal
endif
ifneq (,$(filter test_utils_result_output,$(USEMODULE)))
DIRS += test_utils/result_output
endif

View File

@ -151,3 +151,7 @@ ifneq (,$(filter usbus_dfu riotboot_reset,$(USEMODULE)))
CFLAGS += -DCPU_RAM_BASE=$(RAM_START_ADDR)
CFLAGS += -DCPU_RAM_SIZE=$(RAM_LEN)
endif
ifneq (,$(filter test_utils_netdev_eth_minimal,$(USEMODULE)))
CFLAGS += -DCONFIG_NETDEV_REGISTER_SIGNAL
endif

View File

@ -0,0 +1,67 @@
/*
* Copyright (C) 2022 HAW Hamburg
*
* 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 test_utils_netdev_eth_minimal Minimal netdev Ethernet device processing
* @ingroup sys
*
* @{
* @file
* @brief Provides basic functionalities to interact with an Ethernet networking
* device which implements the @ref drivers_netdev_api.
*
* To use the functionalities, include the module `USEMODULE += test_utils_netdev_eth_minimal`.
* The test application should provide:
* - device initialization, via the implementation of @ref netdev_eth_minimal_init_devs
* - number of devices to test, via the definition of @ref NETDEV_ETH_MINIMAL_NUMOF in `init_dev.h`
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
*/
#ifndef TEST_UTILS_NETDEV_ETH_MINIMAL_H
#define TEST_UTILS_NETDEV_ETH_MINIMAL_H
#include "net/netdev.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef DOXYGEN
/**
* @brief Maximum number of devices to handle.
* @note Should be provided by the application via `init_dev.h`.
*/
#define NETDEV_ETH_MINIMAL_NUMOF
#endif
/**
* @brief Device-under-test initialization function.
* @note Should be implemented by the test application
*
* @param[in] cb Callback to be set to @ref netdev::event_callback
*
* @retval 0 on success
* @retval != 0 on error
*/
int netdev_eth_minimal_init_devs(netdev_event_cb_t cb);
/**
* @brief Initialize the module.
*
* @retval 0 on success
* @retval != 0 on error
*/
int netdev_eth_minimal_init(void);
#ifdef __cplusplus
}
#endif
#endif /* TEST_UTILS_NETDEV_ETH_MINIMAL_H */
/** @} */

View File

@ -10,3 +10,6 @@ ifneq (,$(filter benchmark_udp,$(USEMODULE)))
USEMODULE += sock_udp
USEMODULE += xtimer
endif
ifneq (,$(filter test_utils_netdev_eth_minimal,$(USEMODULE)))
include $(RIOTBASE)/sys/test_utils/netdev_eth_minimal/Makefile.dep
endif

View File

@ -0,0 +1,3 @@
MODULE = test_utils_netdev_eth_minimal
include $(RIOTBASE)/Makefile.base

View File

@ -0,0 +1,6 @@
USEMODULE += event
USEMODULE += event_thread
USEMODULE += l2util
USEMODULE += od
USEMODULE += od_string
USEMODULE += shell

View File

@ -0,0 +1,110 @@
/*
* Copyright (C) 2022 HAW Hamburg
*
* 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.
*/
/**
* @ingroup test_utils_netdev_eth_minimal
* @{
*
* @file
* @brief Implementation of netdev Eth minimal test utility module
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
*/
#include <stdio.h>
#include "event.h"
#include "event/thread.h"
#include "od.h"
#include "net/ethernet.h"
#include "net/ethernet/hdr.h"
#include "net/l2util.h"
#include "net/netdev.h"
#include "net/netdev/eth.h"
#include "test_utils/netdev_eth_minimal.h"
#include "netdev_eth_minimal_internal.h"
/* provided by the test application */
#include "init_dev.h"
device_reg_entry_t _devices[NETDEV_ETH_MINIMAL_NUMOF];
static uint8_t _buffer[ETHERNET_MAX_LEN];
static char _addr_str[ETHERNET_ADDR_LEN * 3];
void _recv(netdev_t *dev)
{
ssize_t data_len;
netdev_eth_rx_info_t rx_info = { 0 };
ethernet_hdr_t *header = (ethernet_hdr_t *)_buffer;
uint8_t *payload = _buffer + sizeof(ethernet_hdr_t);
putchar('\n');
data_len = dev->driver->recv(dev, _buffer, sizeof(_buffer), &rx_info);
if (data_len < 0) {
return;
}
l2util_addr_to_str(header->dst, ETHERNET_ADDR_LEN, _addr_str);
printf("Dest. addr.: %s\n", _addr_str);
l2util_addr_to_str(header->src, ETHERNET_ADDR_LEN, _addr_str);
printf("Src. addr.: %s\n", _addr_str);
data_len -= sizeof(ethernet_hdr_t);
printf("Payload (%u bytes): \n", (unsigned)data_len);
od_hex_dump(payload, data_len, 0);
}
static void _isr_event_handler(event_t *event)
{
/* recover the netdev from the event */
device_reg_entry_t *netdev_event = container_of(event, device_reg_entry_t, event);
netdev_t *netdev = netdev_event->dev;
netdev->driver->isr(netdev);
}
static void _event_cb(netdev_t *dev, netdev_event_t event)
{
device_reg_entry_t *device = dev->context;
switch (event) {
case NETDEV_EVENT_ISR:
event_post(EVENT_PRIO_HIGHEST, &device->event);
break;
case NETDEV_EVENT_RX_COMPLETE:
_recv(dev);
break;
default:
break;
}
}
/* Implement netdev_register_signal hook to associate registered devices to specific event
* structures.
*/
void netdev_register_signal(struct netdev *dev, netdev_type_t type, uint8_t index)
{
(void) type;
if (index > NETDEV_ETH_MINIMAL_NUMOF) {
return;
}
printf("Device %d registered (type: %d)\n", index, type);
dev->context = &_devices[index];
_devices[index].dev = dev;
_devices[index].event.handler = _isr_event_handler;
}
int netdev_eth_minimal_init(void)
{
return netdev_eth_minimal_init_devs(_event_cb);
}
/** @} */

View File

@ -0,0 +1,50 @@
/*
* Copyright (C) 2022 HAW Hamburg
*
* 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.
*/
/**
* @ingroup test_utils_netdev_eth_minimal
*
* @{
* @file
* @brief Internal definitions for the netdev_eth_minimal module
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
*/
#ifndef NETDEV_ETH_MINIMAL_INTERNAL_H
#define NETDEV_ETH_MINIMAL_INTERNAL_H
#include "net/netdev.h"
#include "event.h"
#include "init_dev.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Registry entry to keep track of registered Ethernet network devices
*
* Each registered device has an associated event to serve its ISRs.
*/
typedef struct {
event_t event; /**< event to serve ISR */
netdev_t *dev; /**< pointer to the device */
} device_reg_entry_t;
/**
* @brief Registry of Ethernet devices to serve ISRs.
*/
extern device_reg_entry_t _devices[NETDEV_ETH_MINIMAL_NUMOF];
#ifdef __cplusplus
}
#endif
#endif /* NETDEV_ETH_MINIMAL_INTERNAL_H */
/** @} */

View File

@ -0,0 +1,120 @@
/*
* Copyright (C) 2022 HAW Hamburg
*
* 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.
*/
/**
* @ingroup test_utils_netdev_eth_minimal
* @{
*
* @file
* @brief Shell commands for netdev Eth minimal test utility module
*
* @author Leandro Lanzieri <leandro.lanzieri@haw-hamburg.de>
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "net/ethernet/hdr.h"
#include "net/ethertype.h"
#include "net/l2util.h"
#include "byteorder.h"
#include "shell.h"
#include "iolist.h"
#include "assert.h"
#include "netdev_eth_minimal_internal.h"
#include "init_dev.h"
#define ENABLE_DEBUG 0
#include "debug.h"
int ifconfig_list(int idx)
{
int res;
netdev_t *dev = _devices[idx].dev;
uint8_t addr[ETHERNET_ADDR_LEN];
char addr_str[ETHERNET_ADDR_LEN * 3];
printf("Iface %3d HWaddr: ", idx);
res = dev->driver->get(dev, NETOPT_ADDRESS, addr, sizeof(addr));
assert(res > 0);
l2util_addr_to_str(addr, ETHERNET_ADDR_LEN, addr_str);
printf("%s\n", addr_str);
return 0;
}
int cmd_ifconfig(int argc, char **argv)
{
(void)argc;
(void)argv;
for (unsigned int i = 0; i < NETDEV_ETH_MINIMAL_NUMOF; i++) {
ifconfig_list(i);
}
return 0;
}
static int _print_txtsnd_usage(char *cmd) {
printf("usage: %s <iface> <addr> <text>\n", cmd);
return 1;
}
static int cmd_txtsnd(int argc, char **argv)
{
ethernet_hdr_t header;
int res;
if (4 != argc) {
return _print_txtsnd_usage(argv[0]);
}
int iface = atoi(argv[1]);
if (iface < 0 || (unsigned)iface >= NETDEV_ETH_MINIMAL_NUMOF) {
printf("unknown interface %d\n", iface);
return _print_txtsnd_usage(argv[0]);
}
/* build Ethernet header */
res = l2util_addr_from_str(argv[2], header.dst);
if (!res) {
puts("Could not parse address");
return _print_txtsnd_usage(argv[0]);
}
netdev_t *dev = _devices[iface].dev;
if (!dev) {
return _print_txtsnd_usage(argv[0]);
}
dev->driver->get(dev, NETOPT_ADDRESS, header.src, ETHERNET_ADDR_LEN);
header.type = byteorder_htons(ETHERTYPE_UNKNOWN);
/* prepare iolists to send */
iolist_t io_data = { 0 };
io_data.iol_base = argv[3];
io_data.iol_len = strlen(argv[3]);
iolist_t io_header = { 0 };
io_header.iol_base = &header;
io_header.iol_len = sizeof(ethernet_hdr_t);
io_header.iol_next = &io_data;
/* send */
res = dev->driver->send(dev, &io_header);
if (res < 0) {
puts("txtsnd: Could not send");
return 1;
}
puts("Successfully sent");
return 0;
}
SHELL_COMMAND(txtsnd, "Send an Ethernet frame", cmd_txtsnd);
SHELL_COMMAND(ifconfig, "List interfaces", cmd_ifconfig);