netdev2_test.h
8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Copyright (C) 2016 Freie Universität Berlin
*
* 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 sys_netdev2_test Netdev2 dummy test driver
* @ingroup drivers_netdev_netdev2
* @brief Provides a test dummy for the netdev2 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).
*
* ~~~~~~~~~~~ {.c}
* #include <stdint.h>
*
* #include "mutex.h"
* #include "net/af.h"
* #include "net/conn/udp.h"
* #include "net/ipv6/addr.h"
* #include "net/netdev2_test.h"
* #include "xtimer.h"
*
* #define PKT_NUMBER (1000)
*
* static netdev2_test_t dev;
* static uint32_t last_start;
* static uint32_t sum = 0;
* static mutex_t wait = MUTEX_INIT;
*
* int _send_timer(netdev2_t *dev, const struct iovec *vector, int count)
* {
* (void)dev;
* (void)vector;
* (void)count;
*
* sum += (xtimer_now_usec() - last_start);
* mutex_unlock(&wait);
* }
*
* int main(void) {
* ipv6_addr_t dst = IPV6_ADDR_UNSPECIFIED;
*
* netdev2_test_setup(&dev, NULL);
* dev->driver->init((netdev2_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
* @ref netdev2_test_t::get_cbs "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.
*
* @{
*
* @file
* @brief @ref sys_netdev2_test definitions
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/
#ifndef NETDEV2_TEST_H_
#define NETDEV2_TEST_H_
#include "mutex.h"
#ifdef MODULE_NETDEV2_IEEE802154
#include "net/netdev2/ieee802154.h"
#endif
#include "net/netdev2.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Callback type to handle send command
*
* @param[in] dev network device descriptor
* @param[in] vector io vector array to send
* @param[in] count number of entries in vector
*
* @return number of bytes sent
* @return <= 0 on error
*/
typedef int (*netdev2_test_send_cb_t)(netdev2_t *dev,
const struct iovec *vector,
int count);
/**
* @brief Callback type to handle receive command
*
* @param[in] dev network device descriptor
* @param[out] buf buffer to write into or `NULL`
* @param[in] len maximum number of bytes to read
* @param[out] info status information for the received packet. Might
* be of different type for different netdev2 devices.
* May be NULL if not needed or applicable
*
* @return <=0 on error
* @return number of bytes read if buf != NULL
* @return currently received packet size if buf == NULL
*/
typedef int (*netdev2_test_recv_cb_t)(netdev2_t *dev, char *buf, int len,
void *info);
/**
* @brief Callback type to handle device initialization
*
* @param[in] dev network device descriptor
*
* @return <= on error
* @return 0 on success
*/
typedef int (*netdev2_test_init_cb_t)(netdev2_t *dev);
/**
* @brief Callback type to handle user-space ISR events
*
* @param[in] dev network device descriptor
*/
typedef void (*netdev2_test_isr_cb_t)(netdev2_t *dev);
/**
* @brief Callback type to handle get commands
*
* @param[in] dev network device descriptor
* @param[out] value pointer to store the option's value in
* @param[in] max_len maximal amount of bytes that fit into @p value
*
* @return number of bytes written to @p value
* @return <0 on error
*/
typedef int (*netdev2_test_get_cb_t)(netdev2_t *dev, void *value,
size_t max_len);
/**
* @brief Callback type to handle set commands
*
* @param[in] dev network device descriptor
* @param[out] value value to set
* @param[in] value_len the length of @p value
*
* @return number of bytes used from @p value
* @return <0 on error
*/
typedef int (*netdev2_test_set_cb_t)(netdev2_t *dev, void *value,
size_t value_len);
/**
* @brief Device descriptor for @ref sys_netdev2_test devices
*
* @extends netdev2_t
*/
typedef struct {
/**
* @brief netdev2 fields
* @{
*/
#ifdef MODULE_NETDEV2_IEEE802154
netdev2_ieee802154_t netdev; /**< superclass */
#else /* MODULE_NETDEV2_IEEE802154 */
netdev2_t netdev; /**< superclass */
#endif /* MODULE_NETDEV2_IEEE802154 */
/** @} */
/**
* @brief device specific fields
* @{
*/
netdev2_test_send_cb_t send_cb; /**< callback to handle send command */
netdev2_test_recv_cb_t recv_cb; /**< callback to handle receive command */
netdev2_test_init_cb_t init_cb; /**< callback to handle initialization events */
netdev2_test_isr_cb_t isr_cb; /**< callback to handle ISR events */
netdev2_test_get_cb_t get_cbs[NETOPT_NUMOF]; /**< callback to handle get command */
netdev2_test_set_cb_t set_cbs[NETOPT_NUMOF]; /**< callback to handle set command */
void *state; /**< external state for the device */
mutex_t mutex; /**< mutex for the device */
/** @} */
} netdev2_test_t;
/**
* @brief override send callback
*
* @param[in] dev a @ref sys_netdev2_test device
* @param[in] send_cb a send callback
*/
static inline void netdev2_test_set_send_cb(netdev2_test_t *dev,
netdev2_test_send_cb_t send_cb)
{
mutex_lock(&dev->mutex);
dev->send_cb = send_cb;
mutex_unlock(&dev->mutex);
}
/**
* @brief override receive callback
*
* @param[in] dev a @ref sys_netdev2_test device
* @param[in] recv_cb a receive callback
*/
static inline void netdev2_test_set_recv_cb(netdev2_test_t *dev,
netdev2_test_recv_cb_t recv_cb)
{
mutex_lock(&dev->mutex);
dev->recv_cb = recv_cb;
mutex_unlock(&dev->mutex);
}
/**
* @brief override initialization callback
*
* @param[in] dev a @ref sys_netdev2_test device
* @param[in] init_cb an initialization callback
*/
static inline void netdev2_test_set_init_cb(netdev2_test_t *dev,
netdev2_test_init_cb_t init_cb)
{
mutex_lock(&dev->mutex);
dev->init_cb = init_cb;
mutex_unlock(&dev->mutex);
}
/**
* @brief override ISR event handler callback
*
* @param[in] dev a @ref sys_netdev2_test device
* @param[in] isr_cb an ISR event handler callback
*/
static inline void netdev2_test_set_isr_cb(netdev2_test_t *dev,
netdev2_test_isr_cb_t isr_cb)
{
mutex_lock(&dev->mutex);
dev->isr_cb = isr_cb;
mutex_unlock(&dev->mutex);
}
/**
* @brief override get callback for a certain option type
*
* @param[in] dev a @ref sys_netdev2_test device
* @param[in] opt an option type
* @param[in] get_cb a get callback for @p opt
*/
static inline void netdev2_test_set_get_cb(netdev2_test_t *dev, netopt_t opt,
netdev2_test_get_cb_t get_cb)
{
mutex_lock(&dev->mutex);
dev->get_cbs[opt] = get_cb;
mutex_unlock(&dev->mutex);
}
/**
* @brief override get callback for a certain option type
*
* @param[in] dev a @ref sys_netdev2_test device
* @param[in] opt an option type
* @param[in] set_cb a set callback for @p opt
*/
static inline void netdev2_test_set_set_cb(netdev2_test_t *dev, netopt_t opt,
netdev2_test_set_cb_t set_cb)
{
mutex_lock(&dev->mutex);
dev->set_cbs[opt] = set_cb;
mutex_unlock(&dev->mutex);
}
/**
* @brief Setup a given @ref sys_netdev2_test device
*
* @param[in] dev a @ref sys_netdev2_test device to initialize
* @param[in] state external state for the device
*/
void netdev2_test_setup(netdev2_test_t *dev, void *state);
/**
* @brief Resets all callbacks for the device to NULL
*
* @param[in] dev a @ref sys_netdev2_test device to initialize
*/
void netdev2_test_reset(netdev2_test_t *dev);
#ifdef __cplusplus
}
#endif
#endif /* NETDEV2_TEST_H_ */
/** @} */