summaryrefslogtreecommitdiff
path: root/samples/net/ieee802154/hw/src/ieee802154_test.c
blob: 4dc1dbcef85e0e4bc906adaf8fde0e74f30631dc (plain)
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
/*
 * Copyright (c) 2016 Intel Corporation.
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <errno.h>

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

#include <misc/printk.h>
#define PRINT	printk

#ifdef CONFIG_IEEE802154_CC2520_DRV_NAME
#define IEEE802154_DRV_NAME	CONFIG_IEEE802154_CC2520_DRV_NAME
#endif
#ifdef CONFIG_IEEE802154_MCR20A_DRV_NAME
#define IEEE802154_DRV_NAME	CONFIG_IEEE802154_MCR20A_DRV_NAME
#endif

#ifndef CONFIG_NET_L2_IEEE802154_SHELL

static struct ieee802154_req_params scan_ctx;
static struct net_mgmt_event_callback scan_cb;

static void scan_result_cb(struct net_mgmt_event_callback *cb,
			   uint32_t mgmt_event, struct net_if *iface)
{
	PRINT("Got a scan result:\n");
	PRINT("\tChannel: %u\n", scan_ctx.channel);
	PRINT("\tPAN ID : %u\n", scan_ctx.pan_id);
	PRINT("\tLQI    : %u\n", scan_ctx.lqi);
}

static inline void scan(struct net_if *iface)
{
	size_t len = sizeof(struct ieee802154_req_params);
	int ret;

	net_mgmt_init_event_callback(&scan_cb, scan_result_cb,
				     NET_EVENT_IEEE802154_SCAN_RESULT);
	memset(&scan_ctx, 0, len);
	scan_ctx.channel_set = IEEE802154_ALL_CHANNELS;
	scan_ctx.duration = K_SECONDS(1);

	ret = net_mgmt(NET_REQUEST_IEEE802154_ACTIVE_SCAN,
		       iface, &scan_ctx, len);
	if (ret < 0) {
		PRINT("Scan did not proceed well %u\n", ret);
	}
}

#else
#define scan(...)
#endif /* CONFIG_NET_L2_IEEE802154_SHELL */

static struct net_if *init_device(void)
{
	struct net_if *iface;
	struct device *dev;

	dev = device_get_binding(IEEE802154_DRV_NAME);
	if (!dev) {
		PRINT("Cannot get device binding\n");
		return NULL;
	}

	iface = net_if_lookup_by_dev(dev);
	if (!iface) {
		PRINT("Cannot get 802.15.4 network interface\n");
		return NULL;
	}

	PRINT("802.15.4 device up and running\n");

	return iface;
}
void main(void)
{
	struct net_if *iface;

	iface = init_device();
	if (!iface) {
		goto end;
	}

	k_sleep(K_SECONDS(5));

	scan(iface);
end:
	return;
}