Blame view

RIOT/tests/netdev_test/main.c 11.5 KB
a752c7ab   elopes   add first test an...
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  /*
   * 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.
   */
  
  /**
   * @{
   *
   * @file
   * @brief       Show case application for netdev_test
   *
   * @author      Martine Lenders <mlenders@inf.fu-berlin.de>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "msg.h"
  #include "net/ethernet.h"
  #include "net/gnrc.h"
  #include "net/gnrc/netdev/eth.h"
  #include "net/netdev_test.h"
  #include "od.h"
  #include "thread.h"
  #include "utlist.h"
  
  #define _EXP_LENGTH     (64)
  
  #define _MAC_STACKSIZE  (THREAD_STACKSIZE_DEFAULT + THREAD_EXTRA_STACKSIZE_PRINTF)
  #define _MAC_PRIO       (THREAD_PRIORITY_MAIN - 4)
  
  #define _MAIN_MSG_QUEUE_SIZE (2)
  
  #define _TEST_PAYLOAD1  "gO3Xt,fP)6* MR161Auk?W^mTb\"LmY^Qc5w1h:C<+n(*/@4k("
  #define _TEST_PAYLOAD2  "*b/'XKkraEBexaU\\O-X&<Bl'n%35Ll+nDy,jQ+[Oe4:9( 4cI"
  
  #define EXECUTE(test) \
      puts("Executing " # test "()"); \
      if (!test()) { \
          puts(" + failed."); \
          return 1; \
      } \
      else { \
          puts(" + succeeded."); \
      }
  
  static uint8_t _dev_addr[] = { 0x6c, 0x5d, 0xff, 0x73, 0x84, 0x6f };
  static const uint8_t _test_dst[] = { 0xf5, 0x19, 0x9a, 0x1d, 0xd8, 0x8f };
  static const uint8_t _test_src[] = { 0x41, 0x9b, 0x9f, 0x56, 0x36, 0x46 };
  
  static char _mac_stack[_MAC_STACKSIZE];
  static gnrc_netdev_t _gnrc_dev;
  static netdev_test_t _dev;
  static msg_t _main_msg_queue[_MAIN_MSG_QUEUE_SIZE];
  static uint8_t _tmp[_EXP_LENGTH];
  static kernel_pid_t _mac_pid;
  static uint8_t _tmp_len = 0;
  
  static void _dev_isr(netdev_t *dev);
  static int _dev_recv(netdev_t *dev, char *buf, int len, void *info);
  static int _dev_send(netdev_t *dev, const struct iovec *vector, int count);
  static int _dev_get_addr(netdev_t *dev, void *value, size_t max_len);
  static int _dev_set_addr(netdev_t *dev, const void *value, size_t max_len);
  
  /* tests getter */
  static int test_get_addr(void)
  {
      uint8_t tmp[sizeof(_dev_addr)];
  
      if (gnrc_netapi_get(_mac_pid, NETOPT_ADDRESS, 0, tmp, sizeof(tmp)) != sizeof(_dev_addr)) {
          puts("Error getting device address");
          return 0;
      }
      else if (memcmp(tmp, _dev_addr, sizeof(_dev_addr)) != 0) {
          puts("Got wrong device address");
          return 0;
      }
      return 1;
  }
  
  /* tests sending */
  static int test_send(void)
  {
      ethernet_hdr_t *exp_mac = (ethernet_hdr_t *)_tmp;
      uint8_t *exp_payload = _tmp + sizeof(ethernet_hdr_t);
      gnrc_pktsnip_t *pkt, *hdr;
      msg_t msg;
  
      /* prepare packet for sending */
      pkt = gnrc_pktbuf_add(NULL, _TEST_PAYLOAD1, sizeof(_TEST_PAYLOAD1) - 1,
                            GNRC_NETTYPE_UNDEF);
      if (pkt == NULL) {
          puts("Could not allocate send payload");
          return 0;
      }
      hdr = gnrc_netif_hdr_build(NULL, 0, (uint8_t *)_test_dst, sizeof(_test_dst));
      if (hdr == NULL) {
          gnrc_pktbuf_release(pkt);
          puts("Could not allocate send header");
          return 0;
      }
      LL_PREPEND(pkt, hdr);
      /* prepare expected data */
      memcpy(exp_mac->dst, _test_dst, sizeof(_test_dst));
      memcpy(exp_mac->src, _dev_addr, sizeof(_dev_addr));
      exp_mac->type = byteorder_htons(ETHERTYPE_UNKNOWN);
      memcpy(exp_payload, _TEST_PAYLOAD1, sizeof(_TEST_PAYLOAD1) - 1);
      _tmp_len = sizeof(_TEST_PAYLOAD1) + sizeof(ethernet_hdr_t) - 1;
      /* register for returned packet status */
      if (gnrc_neterr_reg(pkt) != 0) {
          puts("Can not register for error reporting");
          return 0;
      }
      /* send packet to MAC layer */
      gnrc_netapi_send(_mac_pid, pkt);
      /* wait for packet status and check */
      msg_receive(&msg);
      if ((msg.type != GNRC_NETERR_MSG_TYPE) ||
          (msg.content.value != GNRC_NETERR_SUCCESS)) {
          puts("Error sending packet");
          return 0;
      }
      return 1;
  }
  
  /* tests receiving */
  static int test_receive(void)
  {
      ethernet_hdr_t *rcv_mac = (ethernet_hdr_t *)_tmp;
      uint8_t *rcv_payload = _tmp + sizeof(ethernet_hdr_t);
      gnrc_pktsnip_t *pkt, *hdr;
      gnrc_netreg_entry_t me = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL,
                                                          sched_active_pid);
      msg_t msg;
  
      if (_dev.netdev.event_callback == NULL) {
          puts("Device's event_callback not set");
          return 0;
      }
      /* prepare receive buffer */
      memcpy(rcv_mac->dst, _dev_addr, sizeof(_dev_addr));
      memcpy(rcv_mac->src, _test_src, sizeof(_test_src));
      /* no gnrc_ipv6 in compile unit => ETHERTYPE_IPV6 translates to
       * GNRC_NETTYPE_UNDEF */
      rcv_mac->type = byteorder_htons(ETHERTYPE_IPV6);
      memcpy(rcv_payload, _TEST_PAYLOAD2, sizeof(_TEST_PAYLOAD2) - 1);
      _tmp_len = sizeof(_TEST_PAYLOAD2) + sizeof(ethernet_hdr_t) - 1;
  
      /* register for GNRC_NETTYPE_UNDEF */
      gnrc_netreg_register(GNRC_NETTYPE_UNDEF, &me);
      /* fire ISR event */
      _dev.netdev.event_callback((netdev_t *)&_dev.netdev, NETDEV_EVENT_ISR);
      /* wait for packet from MAC layer*/
      msg_receive(&msg);
      /* check message */
      if (msg.sender_pid != _mac_pid) {
          puts("Unexpected sender of netapi receive message");
          return 0;
      }
      if (msg.type != GNRC_NETAPI_MSG_TYPE_RCV) {
          puts("Expected netapi receive message");
          return 0;
      }
      pkt = msg.content.ptr;
      /* check payload */
      if (pkt->size != _tmp_len - sizeof(ethernet_hdr_t)) {
          puts("Payload of unexpected size");
      }
      if ((pkt->type != GNRC_NETTYPE_UNDEF) ||
          (memcmp(pkt->data, _TEST_PAYLOAD2, pkt->size) != 0)) {
          puts("Unexpected payload");
          puts("===========================================================");
          puts("expected");
          puts("===========================================================");
          od_hex_dump(_TEST_PAYLOAD2, pkt->size, OD_WIDTH_DEFAULT);
          puts("===========================================================");
          puts("send data");
          puts("===========================================================");
          od_hex_dump(pkt->data, pkt->size, OD_WIDTH_DEFAULT);
          return 0;
      }
      hdr = pkt->next;
      /* check netif header */
      if ((hdr->type != GNRC_NETTYPE_NETIF) || (hdr->next != NULL) ||
          (hdr->size) != (sizeof(gnrc_netif_hdr_t) + (2 * ETHERNET_ADDR_LEN))) {
          puts("Malformed header received");
          return 0;
      }
      if (memcmp(gnrc_netif_hdr_get_src_addr(hdr->data), _test_src,
                 ETHERNET_ADDR_LEN) != 0) {
          char addr_str[ETHERNET_ADDR_LEN * 3];
          puts("Unexpected source received");
          puts("=================");
          puts("expected");
          puts("=================");
          puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                      _test_src,
                                      ETHERNET_ADDR_LEN));
          puts("=================");
          puts("received source");
          puts("=================");
          puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                      gnrc_netif_hdr_get_src_addr(hdr->data),
                                      ETHERNET_ADDR_LEN));
          return 0;
      }
      if (memcmp(gnrc_netif_hdr_get_dst_addr(hdr->data), _dev_addr,
                 ETHERNET_ADDR_LEN) != 0) {
          char addr_str[ETHERNET_ADDR_LEN * 3];
          puts("Unexpected destination received");
          puts("=================");
          puts("expected");
          puts("=================");
          puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                      _dev_addr,
                                      ETHERNET_ADDR_LEN));
          puts("====================");
          puts("received destination");
          puts("====================");
          puts(gnrc_netif_addr_to_str(addr_str, sizeof(addr_str),
                                      gnrc_netif_hdr_get_dst_addr(hdr->data),
                                      ETHERNET_ADDR_LEN));
          return 0;
      }
  
      gnrc_pktbuf_release(pkt);
      gnrc_netreg_unregister(GNRC_NETTYPE_UNDEF, &me);
      return 1;
  }
  
  static int test_set_addr(void)
  {
      static const uint8_t new_addr[] = { 0x71, 0x29, 0x5b, 0xc8, 0x52, 0x65 };
      uint8_t tmp[sizeof(new_addr)];
  
      if (gnrc_netapi_set(_mac_pid, NETOPT_ADDRESS, 0, (void *)new_addr,
                          sizeof(new_addr)) != sizeof(new_addr)) {
          puts("Error setting device address");
          return 0;
      }
      if (gnrc_netapi_get(_mac_pid, NETOPT_ADDRESS, 0, tmp,
                          sizeof(tmp)) != sizeof(tmp)) {
          puts("Error setting device address");
          return 0;
      }
      else if (memcmp(tmp, new_addr, sizeof(new_addr)) != 0) {
          puts("Set to wrong device address");
          return 0;
      }
      return 1;
  }
  
  int main(void)
  {
      /* initialization */
      gnrc_pktbuf_init();
      msg_init_queue(_main_msg_queue, _MAIN_MSG_QUEUE_SIZE);
      netdev_test_setup(&_dev, NULL);
      netdev_test_set_isr_cb(&_dev, _dev_isr);
      netdev_test_set_recv_cb(&_dev, _dev_recv);
      netdev_test_set_send_cb(&_dev, _dev_send);
      netdev_test_set_get_cb(&_dev, NETOPT_ADDRESS, _dev_get_addr);
      netdev_test_set_set_cb(&_dev, NETOPT_ADDRESS, _dev_set_addr);
      gnrc_netdev_eth_init(&_gnrc_dev, (netdev_t *)(&_dev));
      _mac_pid = gnrc_netdev_init(_mac_stack, _MAC_STACKSIZE, _MAC_PRIO,
                                  "gnrc_netdev_eth_test", &_gnrc_dev);
      if (_mac_pid <= KERNEL_PID_UNDEF) {
          puts("Could not start MAC thread\n");
          return 1;
      }
  
      /* test execution */
      EXECUTE(test_get_addr);
      EXECUTE(test_send);
      EXECUTE(test_receive);
      EXECUTE(test_set_addr);
      puts("ALL TESTS SUCCESSFUL");
  
      return 0;
  }
  
  /* netdev_test callbacks */
  static void _dev_isr(netdev_t *dev)
  {
      (void)dev;
      if (dev->event_callback) {
          dev->event_callback(dev, NETDEV_EVENT_RX_COMPLETE);
      }
  }
  
  static int _dev_recv(netdev_t *dev, char *buf, int len, void *info)
  {
      (void)dev;
      (void)info;
      if (buf == NULL) {
          return _tmp_len;
      }
      else if (len < _tmp_len) {
          return -ENOBUFS;
      }
      else {
          memcpy(buf, _tmp, _tmp_len);
          return _tmp_len;
      }
  }
  
  static int _dev_send(netdev_t *dev, const struct iovec *vector, int count)
  {
      int idx = 0;
  
      (void)dev;
      /* check packet content with expected data */
      for (int i = 0; i < count; i++) {
          if (memcmp(&(_tmp[idx]), vector[i].iov_base, vector[i].iov_len) != 0) {
              printf("Unexpected send data (vector index = %d)\n", i);
              puts("===========================================================");
              puts("expected");
              puts("===========================================================");
              od_hex_dump(&_tmp[idx], vector[i].iov_len, OD_WIDTH_DEFAULT);
              puts("===========================================================");
              puts("send data");
              puts("===========================================================");
              od_hex_dump(vector[i].iov_base, vector[i].iov_len, OD_WIDTH_DEFAULT);
              return -EINVAL;
          }
          idx += vector[i].iov_len;
      }
      if (idx != _tmp_len) {
          printf("Unexpected send length: %d (expected: %d)\n", idx, _tmp_len);
          return -EINVAL;
      }
  
      return idx;
  }
  
  static int _dev_get_addr(netdev_t *dev, void *value, size_t max_len)
  {
      (void)dev;
      if (max_len < sizeof(_dev_addr)) {
          return -ENOBUFS;
      }
      memcpy(value, _dev_addr, sizeof(_dev_addr));
      return sizeof(_dev_addr);
  }
  
  static int _dev_set_addr(netdev_t *dev, const void *value, size_t value_len)
  {
      (void)dev;
      if (value_len != sizeof(_dev_addr)) {
          return -EOVERFLOW;
      }
      memcpy(_dev_addr, value, sizeof(_dev_addr));
      return sizeof(_dev_addr);
  }