netdev dummy test driver

Provides a test dummy for the netdev interface. More...

Detailed Description

Provides a test dummy for the netdev interface.

See the following simple packet traversal timer for an example. Note that this example assumes that the stack doesn't require any option values from the device and that the stack doesn't create packets on its own or looses packets (neither of those can be assumed for in-production stacks).

#include <stdint.h>
#include "mutex.h"
#include "net/af.h"
#include "net/conn/udp.h"
#include "net/ipv6/addr.h"
#include "xtimer.h"
#define PKT_NUMBER (1000)
static netdev_test_t dev;
static uint32_t last_start;
static uint32_t sum = 0;
static mutex_t wait = MUTEX_INIT;
int _send_timer(netdev_t *dev, const iolist_t *iolist)
{
(void)dev;
(void)iolist;
sum += (xtimer_now_usec() - last_start);
mutex_unlock(&wait);
}
int main(void) {
netdev_test_setup(&dev, NULL);
dev->driver->init((netdev_t *)&dev)
// initialize stack and connect `dev` to it
// ...
mutex_lock(&wait);
for (int i = 0; i < PKT_NUMBER; i++) {
last_start = xtimer_now_usec();
conn_udp_sendto("abcd", sizeof("abcd"), NULL, 0, &dst, sizeof(dst),
AF_INET6, 0xcafe, 0xcafe);
mutex_lock(&wait);
}
printf("Average send packet traversal time: %u\n", sum / PKT_NUMBER);
mutex_unlock(&wait);
return 0;
}

To provide options to the stack, the get callbacks for the specific options need to be set. To catch lost packets and additional sent by the stack the send handler needs to be adapted accordingly.

Files

file  netdev_test.h
 netdev dummy test driver definitions
 

Data Structures

struct  netdev_test_t
 Device descriptor for netdev dummy test driver devices. More...
 

Typedefs

typedef int(* netdev_test_send_cb_t) (netdev_t *dev, const iolist_t *iolist)
 Callback type to handle send command. More...
 
typedef int(* netdev_test_recv_cb_t) (netdev_t *dev, char *buf, int len, void *info)
 Callback type to handle receive command. More...
 
typedef int(* netdev_test_init_cb_t) (netdev_t *dev)
 Callback type to handle device initialization. More...
 
typedef void(* netdev_test_isr_cb_t) (netdev_t *dev)
 Callback type to handle user-space ISR events. More...
 
typedef int(* netdev_test_get_cb_t) (netdev_t *dev, void *value, size_t max_len)
 Callback type to handle get commands. More...
 
typedef int(* netdev_test_set_cb_t) (netdev_t *dev, const void *value, size_t value_len)
 Callback type to handle set commands. More...
 

Functions

static void netdev_test_set_send_cb (netdev_test_t *dev, netdev_test_send_cb_t send_cb)
 override send callback More...
 
static void netdev_test_set_recv_cb (netdev_test_t *dev, netdev_test_recv_cb_t recv_cb)
 override receive callback More...
 
static void netdev_test_set_init_cb (netdev_test_t *dev, netdev_test_init_cb_t init_cb)
 override initialization callback More...
 
static void netdev_test_set_isr_cb (netdev_test_t *dev, netdev_test_isr_cb_t isr_cb)
 override ISR event handler callback More...
 
static void netdev_test_set_get_cb (netdev_test_t *dev, netopt_t opt, netdev_test_get_cb_t get_cb)
 override get callback for a certain option type More...
 
static void netdev_test_set_set_cb (netdev_test_t *dev, netopt_t opt, netdev_test_set_cb_t set_cb)
 override get callback for a certain option type More...
 
void netdev_test_setup (netdev_test_t *dev, void *state)
 Setup a given netdev dummy test driver device. More...
 
void netdev_test_reset (netdev_test_t *dev)
 Resets all callbacks for the device to NULL. More...
 

Typedef Documentation

◆ netdev_test_get_cb_t

typedef int(* netdev_test_get_cb_t) (netdev_t *dev, void *value, size_t max_len)

Callback type to handle get commands.

Parameters
[in]devnetwork device descriptor
[out]valuepointer to store the option's value in
[in]max_lenmaximal amount of bytes that fit into value
Returns
number of bytes written to value
<0 on error

Definition at line 148 of file netdev_test.h.

◆ netdev_test_init_cb_t

typedef int(* netdev_test_init_cb_t) (netdev_t *dev)

Callback type to handle device initialization.

Parameters
[in]devnetwork device descriptor
Returns
<= on error
0 on success

Definition at line 129 of file netdev_test.h.

◆ netdev_test_isr_cb_t

typedef void(* netdev_test_isr_cb_t) (netdev_t *dev)

Callback type to handle user-space ISR events.

Parameters
[in]devnetwork device descriptor

Definition at line 136 of file netdev_test.h.

◆ netdev_test_recv_cb_t

typedef int(* netdev_test_recv_cb_t) (netdev_t *dev, char *buf, int len, void *info)

Callback type to handle receive command.

Parameters
[in]devnetwork device descriptor
[out]bufbuffer to write into or NULL
[in]lenmaximum number of bytes to read
[out]infostatus information for the received packet. Might be of different type for different netdev devices. May be NULL if not needed or applicable
Returns
<=0 on error
number of bytes read if buf != NULL
currently received packet size if buf == NULL

Definition at line 118 of file netdev_test.h.

◆ netdev_test_send_cb_t

typedef int(* netdev_test_send_cb_t) (netdev_t *dev, const iolist_t *iolist)

Callback type to handle send command.

Parameters
[in]devnetwork device descriptor
[in]iolistio vector list to send
Returns
number of bytes sent
<= 0 on error

Definition at line 102 of file netdev_test.h.

◆ netdev_test_set_cb_t

typedef int(* netdev_test_set_cb_t) (netdev_t *dev, const void *value, size_t value_len)

Callback type to handle set commands.

Parameters
[in]devnetwork device descriptor
[in]valuevalue to set
[in]value_lenthe length of value
Returns
number of bytes used from value
<0 on error

Definition at line 161 of file netdev_test.h.

Function Documentation

◆ netdev_test_reset()

void netdev_test_reset ( netdev_test_t dev)

Resets all callbacks for the device to NULL.

Parameters
[in]deva netdev dummy test driver device to initialize

◆ netdev_test_set_get_cb()

static void netdev_test_set_get_cb ( netdev_test_t dev,
netopt_t  opt,
netdev_test_get_cb_t  get_cb 
)
inlinestatic

override get callback for a certain option type

Parameters
[in]deva netdev dummy test driver device
[in]optan option type
[in]get_cba get callback for opt

Definition at line 259 of file netdev_test.h.

◆ netdev_test_set_init_cb()

static void netdev_test_set_init_cb ( netdev_test_t dev,
netdev_test_init_cb_t  init_cb 
)
inlinestatic

override initialization callback

Parameters
[in]deva netdev dummy test driver device
[in]init_cban initialization callback

Definition at line 230 of file netdev_test.h.

◆ netdev_test_set_isr_cb()

static void netdev_test_set_isr_cb ( netdev_test_t dev,
netdev_test_isr_cb_t  isr_cb 
)
inlinestatic

override ISR event handler callback

Parameters
[in]deva netdev dummy test driver device
[in]isr_cban ISR event handler callback

Definition at line 244 of file netdev_test.h.

◆ netdev_test_set_recv_cb()

static void netdev_test_set_recv_cb ( netdev_test_t dev,
netdev_test_recv_cb_t  recv_cb 
)
inlinestatic

override receive callback

Parameters
[in]deva netdev dummy test driver device
[in]recv_cba receive callback

Definition at line 216 of file netdev_test.h.

◆ netdev_test_set_send_cb()

static void netdev_test_set_send_cb ( netdev_test_t dev,
netdev_test_send_cb_t  send_cb 
)
inlinestatic

override send callback

Parameters
[in]deva netdev dummy test driver device
[in]send_cba send callback

Definition at line 202 of file netdev_test.h.

◆ netdev_test_set_set_cb()

static void netdev_test_set_set_cb ( netdev_test_t dev,
netopt_t  opt,
netdev_test_set_cb_t  set_cb 
)
inlinestatic

override get callback for a certain option type

Parameters
[in]deva netdev dummy test driver device
[in]optan option type
[in]set_cba set callback for opt

Definition at line 274 of file netdev_test.h.

◆ netdev_test_setup()

void netdev_test_setup ( netdev_test_t dev,
void *  state 
)

Setup a given netdev dummy test driver device.

Parameters
[in]deva netdev dummy test driver device to initialize
[in]stateexternal state for the device
mutex_lock
void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
netdev::driver
const struct netdev_driver * driver
ptr to that driver's interface.
Definition: netdev.h:303
netdev_test.h
netdev dummy test driver definitions
ipv6_addr_t
Data type to represent an IPv6 address.
Definition: addr.h:74
addr.h
Definitions for IPv6 addresses.
xtimer_now_usec
static uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
netdev_test_t
Device descriptor for netdev dummy test driver devices.
Definition: netdev_test.h:169
iolist
iolist structure definition
Definition: iolist.h:39
netdev
Structure to hold driver state.
Definition: netdev.h:302
af.h
Global UNIX address family definitions.
AF_INET6
@ AF_INET6
internetwork address family with IPv6: UDP, TCP, etc.
Definition: af.h:38
mutex.h
Mutex for thread synchronization.
netdev_test_setup
void netdev_test_setup(netdev_test_t *dev, void *state)
Setup a given netdev dummy test driver device.
MUTEX_INIT
#define MUTEX_INIT
Static initializer for mutex_t.
Definition: mutex.h:133
IPV6_ADDR_UNSPECIFIED
#define IPV6_ADDR_UNSPECIFIED
Static initializer for the unspecified IPv6 address (::)
Definition: addr.h:88
netdev_driver::init
int(* init)(netdev_t *dev)
the driver's initialization function
Definition: netdev.h:409
mutex_unlock
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
xtimer.h
xtimer interface definitions
mutex_t
Mutex structure.
Definition: mutex.h:120