1 /* IEEE 802.15.4 settings code */
2 
3 /*
4  * Copyright (c) 2017 Intel Corporation.
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/logging/log.h>
10 LOG_MODULE_DECLARE(net_config, CONFIG_NET_CONFIG_LOG_LEVEL);
11 
12 #include <zephyr/kernel.h>
13 #include <errno.h>
14 
15 #include <zephyr/net/net_if.h>
16 #include <zephyr/net/net_core.h>
17 #include <zephyr/net/net_mgmt.h>
18 #include <zephyr/net/ieee802154_mgmt.h>
19 
z_net_config_ieee802154_setup(struct net_if * iface)20 int z_net_config_ieee802154_setup(struct net_if *iface)
21 {
22 	uint16_t channel = CONFIG_NET_CONFIG_IEEE802154_CHANNEL;
23 	uint16_t pan_id = CONFIG_NET_CONFIG_IEEE802154_PAN_ID;
24 	const struct device *const dev = iface == NULL ? DEVICE_DT_GET(DT_CHOSEN(zephyr_ieee802154))
25 						       : net_if_get_device(iface);
26 	int16_t tx_power = CONFIG_NET_CONFIG_IEEE802154_RADIO_TX_POWER;
27 
28 #ifdef CONFIG_NET_L2_IEEE802154_SECURITY
29 	struct ieee802154_security_params sec_params = {
30 		.key = CONFIG_NET_CONFIG_IEEE802154_SECURITY_KEY,
31 		.key_len = sizeof(CONFIG_NET_CONFIG_IEEE802154_SECURITY_KEY),
32 		.key_mode = CONFIG_NET_CONFIG_IEEE802154_SECURITY_KEY_MODE,
33 		.level = CONFIG_NET_CONFIG_IEEE802154_SECURITY_LEVEL,
34 	};
35 #endif /* CONFIG_NET_L2_IEEE802154_SECURITY */
36 
37 	if (!device_is_ready(dev)) {
38 		return -ENODEV;
39 	}
40 
41 	if (!iface) {
42 		iface = net_if_lookup_by_dev(dev);
43 		if (!iface) {
44 			return -ENOENT;
45 		}
46 	}
47 
48 	if (IS_ENABLED(CONFIG_NET_CONFIG_IEEE802154_ACK_REQUIRED)) {
49 		if (net_mgmt(NET_REQUEST_IEEE802154_SET_ACK, iface, NULL, 0)) {
50 			return -EIO;
51 		}
52 	}
53 
54 	if (net_mgmt(NET_REQUEST_IEEE802154_SET_PAN_ID,
55 		     iface, &pan_id, sizeof(uint16_t)) ||
56 	    net_mgmt(NET_REQUEST_IEEE802154_SET_CHANNEL,
57 		     iface, &channel, sizeof(uint16_t)) ||
58 	    net_mgmt(NET_REQUEST_IEEE802154_SET_TX_POWER,
59 		     iface, &tx_power, sizeof(int16_t))) {
60 		return -EINVAL;
61 	}
62 
63 #ifdef CONFIG_NET_L2_IEEE802154_SECURITY
64 	if (net_mgmt(NET_REQUEST_IEEE802154_SET_SECURITY_SETTINGS, iface,
65 		     &sec_params, sizeof(struct ieee802154_security_params))) {
66 		return -EINVAL;
67 	}
68 #endif /* CONFIG_NET_L2_IEEE802154_SECURITY */
69 
70 	if (!IS_ENABLED(CONFIG_IEEE802154_NET_IF_NO_AUTO_START)) {
71 		/* The NET_IF_NO_AUTO_START flag was set by the driver, see
72 		 * ieee802154_init() to allow for configuration before starting
73 		 * up the interface.
74 		 */
75 		net_if_flag_clear(iface, NET_IF_NO_AUTO_START);
76 		net_if_up(iface);
77 	}
78 
79 	return 0;
80 }
81