Boot Linux faster!

Check our new training course

Boot Linux faster!

Check our new training course
and Creative Commons CC-BY-SA
lecture and lab materials

Bootlin logo

Elixir Cross Referencer

  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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
 * Copyright (c) 2017 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

/** @file
 * @brief IEEE 802.15.4 shell module
 */

#include <zephyr.h>
#include <stdio.h>
#include <stdlib.h>
#include <shell/shell.h>
#include <misc/printk.h>

#include <net/net_if.h>
#include <net/ieee802154.h>

#include "ieee802154_mgmt.h"
#include "ieee802154_frame.h"

#define IEEE802154_SHELL_MODULE "ieee15_4"

struct ieee802154_req_params params;
static struct net_mgmt_event_callback scan_cb;

static int shell_cmd_ack(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (!strcmp(argv[1], "set") || !strcmp(argv[1], "1")) {
		net_mgmt(NET_REQUEST_IEEE802154_SET_ACK, iface, NULL, 0);
		printk("ACK flag set on outgoing packets\n");

		return 0;
	}

	if (!strcmp(argv[1], "unset") || !strcmp(argv[1], "0")) {
		net_mgmt(NET_REQUEST_IEEE802154_SET_ACK, iface, NULL, 0);
		printk("ACK flag unset on outgoing packets\n");

		return 0;
	}

	return -EINVAL;
}

static inline void parse_extended_address(char *addr, u8_t *ext_addr)
{
	char *p, *n;
	int i = 0;

	p = addr;

	do {
		n = strchr(p, ':');
		if (n) {
			*n = '\0';
		}

		ext_addr[i] = strtol(p, NULL, 16);
		p = n ? n + 1 : n;
	} while (n);
}

static int shell_cmd_associate(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	params.pan_id = atoi(argv[1]);

	if (strlen(argv[2]) == 23) {
		parse_extended_address(argv[2], params.addr);
		params.len = IEEE802154_EXT_ADDR_LENGTH;
	} else {
		params.short_addr = (u16_t) atoi(argv[2]);
		params.len = IEEE802154_SHORT_ADDR_LENGTH;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_ASSOCIATE, iface,
		     &params, sizeof(struct ieee802154_req_params))) {
		printk("Could not associate to %s on PAN ID %u\n",
		       argv[2], params.pan_id);
	} else {
		printk("Associated to PAN ID %u\n", params.pan_id);
	}

	return 0;
}

static int shell_cmd_disassociate(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	int ret;

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	ret = net_mgmt(NET_REQUEST_IEEE802154_DISASSOCIATE, iface, NULL, 0);
	if (ret == -EALREADY) {
		printk("Interface is not associated\n");
	} else if (ret) {
		printk("Could not disassociate? (status: %i)\n", ret);
	} else {
		printk("Interface is now disassociated\n");
	}

	return 0;
}

static inline u32_t parse_channel_set(char *str_set)
{
	u32_t channel_set = 0;
	char *p, *n;

	p = str_set;

	do {
		u32_t chan;

		n = strchr(p, ':');
		if (n) {
			*n = '\0';
		}

		chan = atoi(p);
		if (chan > 0 && chan < 32) {
			channel_set |= BIT(chan - 1);
		}

		p = n ? n + 1 : n;
	} while (n);

	return channel_set;
}

static inline void print_coordinator_address(void)
{
	if (params.len == IEEE802154_EXT_ADDR_LENGTH) {
		int i;

		printk("(extended) ");

		for (i = 0; i < IEEE802154_EXT_ADDR_LENGTH; i++) {
			printk("%02X", params.addr[i]);

			if (i < (IEEE802154_EXT_ADDR_LENGTH - 1)) {
				printk(":");
			}
		}
	} else {
		printk("(short) %u", params.short_addr);
	}
}

static void scan_result_cb(struct net_mgmt_event_callback *cb,
			   u32_t mgmt_event, struct net_if *iface)
{
	printk("\nChannel: %u\tPAN ID: %u\tCoordinator Address: ",
	       params.channel, params.pan_id);
	print_coordinator_address();
	printk("LQI: %u\n", params.lqi);
}

static int shell_cmd_scan(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u32_t scan_type;
	int ret;

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	memset(&params, 0, sizeof(struct ieee802154_req_params));

	net_mgmt_init_event_callback(&scan_cb, scan_result_cb,
				     NET_EVENT_IEEE802154_SCAN_RESULT);

	if (!strcmp(argv[1], "active")) {
		scan_type = NET_REQUEST_IEEE802154_ACTIVE_SCAN;
	} else if (!strcmp(argv[1], "passive")) {
		scan_type = NET_REQUEST_IEEE802154_PASSIVE_SCAN;
	} else {
		return -EINVAL;
	}

	if (!strcmp(argv[2], "all")) {
		params.channel_set = IEEE802154_ALL_CHANNELS;
	} else {
		params.channel_set = parse_channel_set(argv[2]);
	}

	if (!params.channel_set) {
		return -EINVAL;
	}

	params.duration = atoi(argv[3]);

	printk("%s Scanning (channel set: 0x%08x, duration %u ms)...",
	       scan_type == NET_REQUEST_IEEE802154_ACTIVE_SCAN ?
	       "Active" : "Passive", params.channel_set, params.duration);

	if (scan_type == NET_REQUEST_IEEE802154_ACTIVE_SCAN) {
		ret = net_mgmt(NET_REQUEST_IEEE802154_ACTIVE_SCAN, iface,
			       &params, sizeof(struct ieee802154_req_params));
	} else {
		ret = net_mgmt(NET_REQUEST_IEEE802154_PASSIVE_SCAN, iface,
			       &params, sizeof(struct ieee802154_req_params));
	}

	if (ret) {
		printk("Could not raise a scan (status: %i)\n", ret);
	} else {
		printk("Done\n");
	}

	return 0;
}

static int shell_cmd_set_chan(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u16_t channel = (u16_t) atoi(argv[1]);

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_SET_CHANNEL, iface,
		     &channel, sizeof(u16_t))) {
		printk("Could not set channel %u\n", channel);
	} else {
		printk("Channel %u set\n", channel);
	}

	return 0;
}

static int shell_cmd_get_chan(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u16_t channel;

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_GET_CHANNEL, iface,
		     &channel, sizeof(u16_t))) {
		printk("Could not get channel\n");
	} else {
		printk("Channel %u\n", channel);
	}

	return 0;
}

static int shell_cmd_set_pan_id(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u16_t pan_id = (u16_t) atoi(argv[1]);

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_SET_PAN_ID, iface,
		     &pan_id, sizeof(u16_t))) {
		printk("Could not set PAN ID %u\n", pan_id);
	} else {
		printk("PAN ID %u set\n", pan_id);
	}

	return 0;
}

static int shell_cmd_get_pan_id(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u16_t pan_id;

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_GET_PAN_ID, iface,
		     &pan_id, sizeof(u16_t))) {
		printk("Could not get PAN ID\n");
	} else {
		printk("PAN ID %u\n", pan_id);
	}

	return 0;
}

static int shell_cmd_set_ext_addr(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u8_t addr[IEEE802154_EXT_ADDR_LENGTH];

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (strlen(argv[2]) != 23) {
		printk("23 characters needed\n");
		return 0;
	}

	parse_extended_address(argv[2], addr);

	if (net_mgmt(NET_REQUEST_IEEE802154_SET_EXT_ADDR, iface,
		     addr, IEEE802154_EXT_ADDR_LENGTH)) {
		printk("Could not set extended address\n");
	} else {
		printk("Extended address %s set\n", argv[2]);
	}

	return 0;
}

static int shell_cmd_get_ext_addr(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u8_t addr[IEEE802154_EXT_ADDR_LENGTH];

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_GET_EXT_ADDR, iface,
		     addr, IEEE802154_EXT_ADDR_LENGTH)) {
		printk("Could not get extended address\n");
	} else {
		int i;

		printk("Extended address: ");
		for (i = 0; i < IEEE802154_EXT_ADDR_LENGTH; i++) {
			printk("%02X", addr[i]);

			if (i < (IEEE802154_EXT_ADDR_LENGTH - 1)) {
				printk(":");
			}
		}

		printk("\n");
	}

	return 0;
}

static int shell_cmd_set_short_addr(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u16_t short_addr = (u16_t) atoi(argv[1]);

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_SET_SHORT_ADDR, iface,
		     &short_addr, sizeof(u16_t))) {
		printk("Could not set short address %u\n", short_addr);
	} else {
		printk("Short address %u set\n", short_addr);
	}

	return 0;
}

static int shell_cmd_get_short_addr(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	u16_t short_addr;

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_GET_SHORT_ADDR, iface,
		     &short_addr, sizeof(u16_t))) {
		printk("Could not get short address\n");
	} else {
		printk("Short address %u\n", short_addr);
	}

	return 0;
}

static int shell_cmd_set_tx_power(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	s16_t tx_power = (s16_t) atoi(argv[1]);

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_SET_TX_POWER, iface,
		     &tx_power, sizeof(s16_t))) {
		printk("Could not set TX power %d\n", tx_power);
	} else {
		printk("TX power %d set\n", tx_power);
	}

	return 0;
}

static int shell_cmd_get_tx_power(int argc, char *argv[])
{
	struct net_if *iface = net_if_get_ieee802154();
	s16_t tx_power;

	if (!iface) {
		printk("No IEEE 802.15.4 interface found.\n");
		return -EINVAL;
	}

	if (net_mgmt(NET_REQUEST_IEEE802154_GET_SHORT_ADDR, iface,
		     &tx_power, sizeof(s16_t))) {
		printk("Could not get TX power\n");
	} else {
		printk("TX power (in dbm) %d\n", tx_power);
	}

	return 0;
}

static struct shell_cmd ieee802154_commands[] = {
	{ "ack",		shell_cmd_ack,
	  "<set/1 | unset/0>" },
	{ "associate",		shell_cmd_associate,
	  "<pan_id> <PAN coordinator short or long address>" },
	{ "disassociate",	shell_cmd_disassociate,
	  NULL },
	{ "scan",		shell_cmd_scan,
	  "<passive|active> <channels set n[:m:...]:x|all>"
	  " <per-channel duration in ms>" },
	{ "set_chan",		shell_cmd_set_chan,
	  "<channel>" },
	{ "get_chan",		shell_cmd_get_chan,
	  NULL },
	{ "set_pan_id",		shell_cmd_set_pan_id,
	  "<pan_id>" },
	{ "get_pan_id",		shell_cmd_get_pan_id,
	  NULL },
	{ "set_ext_addr",	shell_cmd_set_ext_addr,
	  "<long/extended address>" },
	{ "get_ext_addr",	shell_cmd_get_ext_addr,
	  NULL },
	{ "set_short_addr",	shell_cmd_set_short_addr,
	  "<short address>" },
	{ "get_short_addr",	shell_cmd_get_short_addr,
	  NULL },
	{ "set_tx_power",	shell_cmd_set_tx_power,
	  "<-18/-7/-4/-2/0/1/2/3/5>" },
	{ "get_tx_power",	shell_cmd_get_tx_power,
	  NULL },
	{ NULL, NULL, NULL },
};

void ieee802154_shell_init(void)
{
	SHELL_REGISTER(IEEE802154_SHELL_MODULE, ieee802154_commands);
}