Blame view

RIOT/tests/gnrc_sixlowpan/main.c 5.71 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
  /*
   * Copyright (C) 2015 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.
   */
  
  /**
   * @ingroup     tests
   * @{
   *
   * @file
   * @brief       Tests extension header handling of gnrc stack.
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @author      Takuo Yonezawa <Yonezawa-T2@mail.dnp.co.jp>
   *
   * @}
   */
  
  #include <stdio.h>
  
  #include "shell.h"
  #include "msg.h"
  #include "net/ipv6/addr.h"
  #include "net/gnrc/ipv6/netif.h"
  #include "net/gnrc/pkt.h"
  #include "net/gnrc/pktbuf.h"
  #include "net/gnrc/netreg.h"
  #include "net/gnrc/netapi.h"
  #include "net/gnrc/netif.h"
  #include "net/gnrc/netif/hdr.h"
  #include "net/gnrc/pktdump.h"
  
  static void _init_interface(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
      ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED;
  
      gnrc_netif_get(ifs);
  
      /* fd01::01 */
      addr.u8[0] = 0xfd;
      addr.u8[1] = 0x01;
      addr.u8[15] = 0x01;
      gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST);
  }
  
  static void _send_packet(void)
  {
      kernel_pid_t ifs[GNRC_NETIF_NUMOF];
  
      gnrc_netif_get(ifs);
  
      struct {
          gnrc_netif_hdr_t netif_hdr;
          uint8_t src[8];
          uint8_t dst[8];
      } netif_hdr = {
          .src = { 0x02, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x00, 0x02 },
          .dst = { 0x02, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x00, 0x01 },
      };
  
      gnrc_netif_hdr_init(&(netif_hdr.netif_hdr), 8, 8);
  
      netif_hdr.netif_hdr.if_pid = ifs[0];
  
      uint8_t data1[] = {
          /* 6LoWPAN Header */
          /* Fragmentation Header (first) */
          0xc0, 0x94, /* 0b11000: frag1, 0b00010010100: datagram_size (148) */
          0x00, 0x01, /* datagram_tag */
          /* 0b011: LOWPAN_IPHC */
          /* 0b11: Traffic Class and Flow Label are elided */
          /* 0b1: Next Header is compressed */
          /* 0b11: The Hop Limit field is compressed and the hop limit is 255 */
          0x7f,
          /* 0b0: No additional 8-bit Context Identifier Extension is used */
          /* 0b0: Source address compression uses stateless compression */
          /* 0b11: source address mode is 0 bits */
          /* 0b0: Destination address is not a multicast address */
          /* 0x0: Destination address compression uses stateless compression */
          /* 0x00: destination address mode is 128 bits */
          0x30,
  
          /* destination address: fd01::1 */
          0xfd, 0x01, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x01,
  
          /* 0b11110: UDP LOWPAN_NHC */
          /* 0b0: Checksum is carried in-line */
          /* 0b11: First 12 bits of both Source Port and Destination Port are 0xf0b and elided */
          0xf3,
          0x00, /* Source Port and Destination Port (4 bits each) */
          0x23, 0x2f, /* Checksum */
  
          /* payload */
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      };
  
      uint8_t data2[] = {
          /* 6LoWPAN Header */
          /* Fragmentation Header (rest) */
          0xe0, 0x94, /* 0b11100: frag1, 0b00010010100: datagram_size (148) */
          0x00, 0x01, /* datagram_tag */
          0x0c,       /* datagram_offset (12 * 8 = 96) */
  
          /* payload */
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
          0x00, 0x00, 0x00, 0x00,
      };
  
      gnrc_netreg_entry_t dump_6lowpan = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, gnrc_pktdump_pid);
      gnrc_netreg_entry_t dump_ipv6 = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, gnrc_pktdump_pid);
      gnrc_netreg_entry_t dump_udp = GNRC_NETREG_ENTRY_INIT_PID(GNRC_NETREG_DEMUX_CTX_ALL, gnrc_pktdump_pid);
      gnrc_netreg_entry_t dump_udp_61616 = GNRC_NETREG_ENTRY_INIT_PID(61616, gnrc_pktdump_pid);
  
      gnrc_netreg_register(GNRC_NETTYPE_SIXLOWPAN, &dump_6lowpan);
      gnrc_netreg_register(GNRC_NETTYPE_IPV6, &dump_ipv6);
      gnrc_netreg_register(GNRC_NETTYPE_UDP, &dump_udp);
      gnrc_netreg_register(GNRC_NETTYPE_UDP, &dump_udp_61616);
  
      gnrc_pktsnip_t *netif1 = gnrc_pktbuf_add(NULL,
                                              &netif_hdr,
                                              sizeof(netif_hdr),
                                              GNRC_NETTYPE_NETIF);
      gnrc_pktsnip_t *pkt1 = gnrc_pktbuf_add(netif1,
                                             data1,
                                             sizeof(data1),
                                             GNRC_NETTYPE_SIXLOWPAN);
  
      gnrc_netapi_dispatch_receive(GNRC_NETTYPE_SIXLOWPAN, GNRC_NETREG_DEMUX_CTX_ALL, pkt1);
  
      gnrc_pktsnip_t *netif2 = gnrc_pktbuf_add(NULL,
                                               &netif_hdr,
                                               sizeof(netif_hdr),
                                               GNRC_NETTYPE_NETIF);
      gnrc_pktsnip_t *pkt2 = gnrc_pktbuf_add(netif2,
                                             data2,
                                             sizeof(data2),
                                             GNRC_NETTYPE_SIXLOWPAN);
  
      gnrc_netapi_dispatch_receive(GNRC_NETTYPE_SIXLOWPAN, GNRC_NETREG_DEMUX_CTX_ALL, pkt2);
  }
  
  int main(void)
  {
      puts("RIOT network stack example application");
  
      _init_interface();
      _send_packet();
  
      return 0;
  }