Blame view

RIOT/tests/driver_at86rf2xx/cmd.c 7.69 KB
fb11e647   vrobic   reseau statique a...
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.
   */
  
  /**
   * @{
   *
   * @file
   * @author  Martine Lenders <mlenders@inf.fu-berlin.de>
   */
  
  #include <errno.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  
  #include "net/netdev2/ieee802154.h"
  #include "net/ieee802154.h"
  
  #include "common.h"
  
  #include "od.h"
  
  #define _MAX_ADDR_LEN   (8)
  
  static int _parse_addr(uint8_t *out, size_t out_len, const char *in);
  static int send(int iface, le_uint16_t dst_pan, uint8_t *dst_addr,
                  size_t dst_len, char *data);
  
  int ifconfig_list(int idx)
  {
      int res;
      uint8_t array_val[_MAX_ADDR_LEN];
      netdev2_ieee802154_t *dev = (netdev2_ieee802154_t *)(&devs[idx]);
  
      int (*get)(netdev2_t *, netopt_t, void *, size_t) = dev->netdev.driver->get;
      netopt_enable_t enable_val;
      uint16_t u16_val;
  
      printf("Iface %3d  HWaddr: ", idx);
      print_addr(dev->short_addr, IEEE802154_SHORT_ADDRESS_LEN);
      printf(", Long HWaddr: ");
      print_addr(dev->long_addr, IEEE802154_LONG_ADDRESS_LEN);
      printf(", PAN: 0x%04x", dev->pan);
  
      res = get((netdev2_t *)dev, NETOPT_ADDR_LEN, &u16_val, sizeof(u16_val));
      if (res < 0) {
          puts("(err)");
          return 1;
      }
      printf("\n           Address length: %u", (unsigned)u16_val);
  
      res = get((netdev2_t *)dev, NETOPT_SRC_LEN, &u16_val, sizeof(u16_val));
      if (res < 0) {
          puts("(err)");
          return 1;
      }
      printf(", Source address length: %u", (unsigned)u16_val);
  
      res = get((netdev2_t *)dev, NETOPT_MAX_PACKET_SIZE, &u16_val,
                sizeof(u16_val));
      if (res < 0) {
          puts("(err)");
          return 1;
      }
      printf(", Max.Payload: %u", (unsigned)u16_val);
  
      res = get((netdev2_t *)dev, NETOPT_IPV6_IID, array_val, sizeof(array_val));
      if (res > 0) {
          printf("\n           IPv6 IID: ");
          print_addr(array_val, res);
      }
  
      printf("\n           Channel: %u", dev->chan);
  
      res = get((netdev2_t *)dev, NETOPT_CHANNEL_PAGE, &u16_val, sizeof(u16_val));
      if (res < 0) {
          puts("(err)");
          return 1;
      }
      printf(", Ch.page: %u", (unsigned)u16_val);
  
      res = get((netdev2_t *)dev, NETOPT_TX_POWER, &u16_val, sizeof(u16_val));
      if (res < 0) {
          puts("(err)");
          return 1;
      }
      printf(", TXPower: %d dBm", (int)u16_val);
      res = get((netdev2_t *)dev, NETOPT_IS_WIRED, &u16_val, sizeof(u16_val));
      if (res < 0) {
          puts(", wireless");
      }
      else {
          puts(", wired");
      }
  
      printf("         ");
      res = get((netdev2_t *)dev, NETOPT_PRELOADING, &enable_val,
                sizeof(netopt_enable_t));
      if ((res > 0) && (enable_val == NETOPT_ENABLE)) {
          printf("  PRELOAD");
      }
      res = get((netdev2_t *)dev, NETOPT_AUTOACK, &enable_val,
                sizeof(netopt_enable_t));
      if ((res > 0) && (enable_val == NETOPT_ENABLE)) {
          printf("  AUTOACK");
      }
      res = get((netdev2_t *)dev, NETOPT_RAWMODE, &enable_val,
                sizeof(netopt_enable_t));
      if ((res > 0) && (enable_val == NETOPT_ENABLE)) {
          printf("  RAW");
      }
      res = get((netdev2_t *)dev, NETOPT_AUTOCCA, &enable_val,
                sizeof(netopt_enable_t));
      if ((res > 0) && (enable_val == NETOPT_ENABLE)) {
          printf("  AUTOCCA");
      }
      res = get((netdev2_t *)dev, NETOPT_CSMA, &enable_val,
                sizeof(netopt_enable_t));
      if ((res > 0) && (enable_val == NETOPT_ENABLE)) {
          printf("  CSMA");
      }
      puts("");
  
      return 0;
  }
  
  int ifconfig(int argc, char **argv)
  {
      (void)argc;
      (void)argv;
      for (int i = 0; i < AT86RF2XX_NUM; i++) {
          ifconfig_list(i);
      }
      return 0;
  }
  
  static void txtsnd_usage(char *cmd_name)
  {
      printf("usage: %s <iface> [<pan>] <addr> <text>\n", cmd_name);
  }
  
  int txtsnd(int argc, char **argv)
  {
      char *text;
      uint8_t addr[_MAX_ADDR_LEN];
      int iface, idx = 2, res;
      le_uint16_t pan = { 0 };
  
      switch (argc) {
          case 4:
              break;
          case 5:
              res = _parse_addr((uint8_t *)&pan, sizeof(pan), argv[idx++]);
              if ((res <= 0) || (res > sizeof(pan))) {
                  txtsnd_usage(argv[0]);
                  return 1;
              }
              pan.u16 = byteorder_swaps(pan.u16);
              break;
          default:
              txtsnd_usage(argv[0]);
              return 1;
      }
  
      iface = atoi(argv[1]);
      res = _parse_addr(addr, sizeof(addr), argv[idx++]);
      if (res <= 0) {
          txtsnd_usage(argv[0]);
          return 1;
      }
      text = argv[idx++];
      return send(iface, pan, addr, (size_t)res, text);
  }
  
  static inline int _dehex(char c, int default_)
  {
      if ('0' <= c && c <= '9') {
          return c - '0';
      }
      else if ('A' <= c && c <= 'F') {
          return c - 'A' + 10;
      }
      else if ('a' <= c && c <= 'f') {
          return c - 'a' + 10;
      }
      else {
          return default_;
      }
  }
  
  static int _parse_addr(uint8_t *out, size_t out_len, const char *in)
  {
      const char *end_str = in;
      uint8_t *out_end = out;
      size_t count = 0;
      int assert_cell = 1;
  
      if (!in || !*in) {
          return 0;
      }
      while (end_str[1]) {
          ++end_str;
      }
  
      while (end_str >= in) {
          int a = 0, b = _dehex(*end_str--, -1);
          if (b < 0) {
              if (assert_cell) {
                  return 0;
              }
              else {
                  assert_cell = 1;
                  continue;
              }
          }
          assert_cell = 0;
  
          if (end_str >= in) {
              a = _dehex(*end_str--, 0);
          }
  
          if (++count > out_len) {
              return 0;
          }
          *out_end++ = (a << 4) | b;
      }
      if (assert_cell) {
          return 0;
      }
      /* out is reversed */
  
      while (out < --out_end) {
          uint8_t tmp = *out_end;
          *out_end = *out;
          *out++ = tmp;
      }
  
      return count;
  }
  
  static int send(int iface, le_uint16_t dst_pan, uint8_t *dst, size_t dst_len,
                  char *data)
  {
      int res;
      netdev2_ieee802154_t *dev;
      const size_t count = 2;         /* mhr + payload */
      struct iovec vector[count];
      uint8_t *src;
      size_t src_len;
      uint8_t mhr[IEEE802154_MAX_HDR_LEN];
      uint8_t flags;
      le_uint16_t src_pan;
  
      if (((unsigned)iface) > (AT86RF2XX_NUM - 1)) {
          printf("txtsnd: %d is not an interface\n", iface);
          return 1;
      }
  
      dev = (netdev2_ieee802154_t *)&devs[iface];
      flags = (uint8_t)(dev->flags & NETDEV2_IEEE802154_SEND_MASK);
      flags |= IEEE802154_FCF_TYPE_DATA;
      vector[1].iov_base = data;
      vector[1].iov_len = strlen(data);
      src_pan = byteorder_btols(byteorder_htons(dev->pan));
      if (dst_pan.u16 == 0) {
          dst_pan = src_pan;
      }
      if (dev->flags & NETDEV2_IEEE802154_SRC_MODE_LONG) {
          src_len = 8;
          src = dev->long_addr;
      }
      else {
          src_len = 2;
          src = dev->short_addr;
      }
      /* fill MAC header, seq should be set by device */
      if ((res = ieee802154_set_frame_hdr(mhr, src, src_len,
                                          dst, dst_len,
                                          src_pan, dst_pan,
                                          flags, dev->seq++)) < 0) {
          puts("txtsnd: Error preperaring frame");
          return 1;
      }
      vector[0].iov_base = mhr;
      vector[0].iov_len = (size_t)res;
      res = dev->netdev.driver->send((netdev2_t *)dev, vector, count);
      if (res < 0) {
          puts("txtsnd: Error on sending");
          return 1;
      }
      else {
          printf("txtsnd: send %u bytes to ", (unsigned)vector[1].iov_len);
          print_addr(dst, dst_len);
          printf(" (PAN: ");
          print_addr((uint8_t *)&dst_pan, sizeof(dst_pan));
          puts(")");
      }
      return 0;
  }
  
  /** @} */