Loading...
Searching...
No Matches
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(&dev->netdev->netdev)
// 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;
}
Global UNIX address family definitions.
#define AF_INET6
internetwork address family with IPv6: UDP, TCP, etc.
Definition af.h:40
void mutex_unlock(mutex_t *mutex)
Unlocks the mutex.
#define MUTEX_INIT
Static initializer for mutex_t.
Definition mutex.h:224
static void mutex_lock(mutex_t *mutex)
Locks a mutex, blocking.
Definition mutex.h:312
#define printf(...)
A wrapper for the printf() function that passes arguments through unmodified, but fails to compile if...
Definition stdio.h:60
#define IPV6_ADDR_UNSPECIFIED
Static initializer for the unspecified IPv6 address (::)
Definition addr.h:86
void netdev_test_setup(netdev_test_t *dev, void *state)
Setup a given netdev dummy test driver device.
static uint32_t xtimer_now_usec(void)
get the current system time in microseconds since start
Definitions for IPv6 addresses.
Mutex for thread synchronization.
netdev dummy test driver definitions
iolist structure definition
Definition iolist.h:39
Mutex structure.
Definition mutex.h:146
int(* init)(netdev_t *dev)
the driver's initialization function
Definition netdev.h:533
netdev_t netdev
netdev_t base class
Definition ieee802154.h:101
Device descriptor for netdev dummy test driver devices.
netdev_ieee802154_t netdev
netdev fields
Structure to hold driver state.
Definition netdev.h:363
const struct netdev_driver * driver
ptr to that driver's interface.
Definition netdev.h:364
Data type to represent an IPv6 address.
Definition addr.h:72
xtimer interface definitions

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.
 
typedef int(* netdev_test_recv_cb_t) (netdev_t *dev, char *buf, int len, void *info)
 Callback type to handle receive command.
 
typedef int(* netdev_test_init_cb_t) (netdev_t *dev)
 Callback type to handle device initialization.
 
typedef void(* netdev_test_isr_cb_t) (netdev_t *dev)
 Callback type to handle user-space ISR events.
 
typedef int(* netdev_test_get_cb_t) (netdev_t *dev, void *value, size_t max_len)
 Callback type to handle get commands.
 
typedef int(* netdev_test_set_cb_t) (netdev_t *dev, const void *value, size_t value_len)
 Callback type to handle set commands.
 

Functions

static void netdev_test_set_send_cb (netdev_test_t *dev, netdev_test_send_cb_t send_cb)
 override send callback
 
static void netdev_test_set_recv_cb (netdev_test_t *dev, netdev_test_recv_cb_t recv_cb)
 override receive callback
 
static void netdev_test_set_init_cb (netdev_test_t *dev, netdev_test_init_cb_t init_cb)
 override initialization callback
 
static void netdev_test_set_isr_cb (netdev_test_t *dev, netdev_test_isr_cb_t isr_cb)
 override ISR event handler callback
 
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
 
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
 
void netdev_test_setup (netdev_test_t *dev, void *state)
 Setup a given netdev dummy test driver device.
 
void netdev_test_reset (netdev_test_t *dev)
 Resets all callbacks for the device to NULL.
 

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 145 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 126 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 133 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 115 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 99 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 158 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 252 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 223 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 237 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 209 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 195 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 267 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