1 /*
2  * Copyright (c) 2023 Florian Grandel, Zephyr Project
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * @brief IEEE 802.15.4 internal MAC and PHY Utils Implementation
10  *
11  * All references to the standard in this file cite IEEE 802.15.4-2020.
12  */
13 
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_DECLARE(net_ieee802154, CONFIG_NET_L2_IEEE802154_LOG_LEVEL);
16 
17 #include "ieee802154_utils.h"
18 
19 /**
20  * PHY utilities
21  */
22 
ieee802154_radio_verify_channel(struct net_if * iface,uint16_t channel)23 bool ieee802154_radio_verify_channel(struct net_if *iface, uint16_t channel)
24 {
25 	struct ieee802154_attr_value value;
26 
27 	if (channel == IEEE802154_NO_CHANNEL) {
28 		return false;
29 	}
30 
31 	if (ieee802154_radio_attr_get(iface, IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES,
32 				      &value)) {
33 		return false;
34 	}
35 
36 	for (int channel_range_index = 0;
37 	     channel_range_index < value.phy_supported_channels->num_ranges;
38 	     channel_range_index++) {
39 		const struct ieee802154_phy_channel_range *const channel_range =
40 			&value.phy_supported_channels->ranges[channel_range_index];
41 
42 		if (channel >= channel_range->from_channel &&
43 		    channel <= channel_range->to_channel) {
44 			return true;
45 		}
46 	}
47 
48 	return false;
49 }
50 
ieee802154_radio_number_of_channels(struct net_if * iface)51 uint16_t ieee802154_radio_number_of_channels(struct net_if *iface)
52 {
53 	struct ieee802154_attr_value value;
54 	uint16_t num_channels = 0;
55 
56 	if (ieee802154_radio_attr_get(iface, IEEE802154_ATTR_PHY_SUPPORTED_CHANNEL_RANGES,
57 				      &value)) {
58 		return 0;
59 	}
60 
61 	for (int channel_range_index = 0;
62 	     channel_range_index < value.phy_supported_channels->num_ranges;
63 	     channel_range_index++) {
64 		const struct ieee802154_phy_channel_range *const channel_range =
65 			&value.phy_supported_channels->ranges[channel_range_index];
66 
67 		__ASSERT_NO_MSG(channel_range->to_channel >= channel_range->from_channel);
68 		num_channels += channel_range->to_channel - channel_range->from_channel + 1U;
69 	}
70 
71 	return num_channels;
72 }
73