Skip to main content

twine_codec/radio/
pan_id.rs

1// Copyright (c) 2025 Jake Swensen
2// SPDX-License-Identifier: MPL-2.0
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8use core::str::FromStr;
9
10use twine_rs_macros::Tlv;
11
12use crate::TwineCodecError;
13
14/// IEEE 802.15.4 PAN ID
15#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Tlv)]
16#[tlv(tlv_type = 0x01, tlv_length = 2, derive_inner)]
17pub struct PanId(u16);
18
19impl PanId {
20    /// Create a new IEEE 802.15.4 PAN ID
21    pub fn new(pan_id: u16) -> Self {
22        Self(pan_id)
23    }
24
25    /// Create a new IEEE 802.15.4 Broadcast PAN ID
26    pub fn broadcast() -> Self {
27        Self(0xffff)
28    }
29
30    pub fn random() -> Self {
31        let pan_id = crate::random_range_u16(0x0001..=0xfffe);
32        Self(pan_id)
33    }
34
35    pub fn get(&self) -> u16 {
36        self.0
37    }
38}
39
40impl From<PanId> for u16 {
41    fn from(value: PanId) -> Self {
42        value.0
43    }
44}
45
46impl From<u16> for PanId {
47    fn from(pan_id: u16) -> Self {
48        Self(pan_id)
49    }
50}
51
52impl FromStr for PanId {
53    type Err = TwineCodecError;
54
55    fn from_str(s: &str) -> Result<Self, Self::Err> {
56        let s = s
57            .strip_prefix("0x")
58            .or_else(|| s.strip_prefix("0X"))
59            .unwrap_or(s);
60        let pan_id = u16::from_str_radix(s, 16).map_err(|_| TwineCodecError::StringParseError)?;
61        Ok(Self::from(pan_id))
62    }
63}
64
65impl core::fmt::Display for PanId {
66    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
67        write!(f, "0x{:04x}", self.0)
68    }
69}
70
71#[cfg(test)]
72mod tests {
73    use twine_tlv::prelude::*;
74
75    use super::*;
76
77    const PAN_ID_TLV_BYTES: [u8; 4] = [0x01, 0x02, 0xde, 0xad];
78
79    #[test]
80    fn broadcast() {
81        assert_eq!(PanId::broadcast(), PanId(0xffff));
82    }
83
84    #[test]
85    fn success_try_decode_meshcop_tlv_for_pan_id() {
86        let test = PanId::decode_tlv_unchecked(PAN_ID_TLV_BYTES);
87        assert_eq!(test.0, 0xdead);
88    }
89
90    #[test]
91    fn success_try_encode_meshcop_tlv_for_pan_id() {
92        let pan_id = PanId::new(0xdead);
93        let mut test_buffer = [0_u8; 10];
94        let bytes_written = pan_id
95            .try_encode_tlv(&mut test_buffer)
96            .expect("Could not encode PanId");
97        assert_eq!(bytes_written, PanId::tlv_total_constant_len());
98        assert_eq!(PAN_ID_TLV_BYTES.as_ref(), &test_buffer[..4]);
99    }
100}