Skip to main content

dvb_si/descriptors/
xait_location.rs

1//! XAIT Location Descriptor — ETSI TS 102 727 §10.17.6, Table 96 (tag 0x7D).
2//!
3//! Points at the service carrying the XAIT (deprecated MHP discovery
4//! mechanism). Per the MHP PDF (etsi_ts_102_727_v01.01.01, p. 185, Table 96)
5//! the body is a fixed 5 bytes:
6//!
7//! ```text
8//! xait_original_network_id(16) + xait_service_id(16)
9//!   + xait_version_number(5) + xait_update_policy(3)
10//! ```
11//!
12//! xait_update_policy values (Table 97, p. 185): 0 = reload immediately on
13//! version change, 1 = ignore version changes until reset, 2..=7 reserved.
14
15use super::descriptor_body;
16use crate::error::{Error, Result};
17use dvb_common::{Parse, Serialize};
18
19/// Descriptor tag for xait_location_descriptor.
20pub const TAG: u8 = 0x7D;
21const HEADER_LEN: usize = 2;
22const BODY_LEN: usize = 5;
23
24/// Largest representable 5-bit xait_version_number.
25const VERSION_MAX: u8 = 0x1F;
26/// Largest representable 3-bit xait_update_policy.
27const UPDATE_POLICY_MAX: u8 = 0x07;
28
29/// XAIT Location Descriptor.
30#[derive(Debug, Clone, PartialEq, Eq)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize))]
32pub struct XaitLocationDescriptor {
33    /// 16-bit original_network_id of the service carrying the XAIT.
34    pub xait_original_network_id: u16,
35    /// 16-bit service_id of the service carrying the XAIT.
36    pub xait_service_id: u16,
37    /// 5-bit version number of the referenced XAIT.
38    pub xait_version_number: u8,
39    /// 3-bit update policy (Table 97).
40    pub xait_update_policy: u8,
41}
42
43impl<'a> Parse<'a> for XaitLocationDescriptor {
44    type Error = crate::error::Error;
45    fn parse(bytes: &'a [u8]) -> Result<Self> {
46        let body = descriptor_body(
47            bytes,
48            TAG,
49            "XaitLocationDescriptor",
50            "unexpected tag for xait_location_descriptor",
51        )?;
52        if body.len() < BODY_LEN {
53            return Err(Error::InvalidDescriptor {
54                tag: TAG,
55                reason: "xait_location_descriptor body shorter than 5 bytes",
56            });
57        }
58        let xait_original_network_id = u16::from_be_bytes([body[0], body[1]]);
59        let xait_service_id = u16::from_be_bytes([body[2], body[3]]);
60        let xait_version_number = (body[4] >> 3) & VERSION_MAX;
61        let xait_update_policy = body[4] & UPDATE_POLICY_MAX;
62        Ok(Self {
63            xait_original_network_id,
64            xait_service_id,
65            xait_version_number,
66            xait_update_policy,
67        })
68    }
69}
70
71impl Serialize for XaitLocationDescriptor {
72    type Error = crate::error::Error;
73    fn serialized_len(&self) -> usize {
74        HEADER_LEN + BODY_LEN
75    }
76
77    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
78        if self.xait_version_number > VERSION_MAX {
79            return Err(Error::InvalidDescriptor {
80                tag: TAG,
81                reason: "xait_version_number exceeds 5 bits",
82            });
83        }
84        if self.xait_update_policy > UPDATE_POLICY_MAX {
85            return Err(Error::InvalidDescriptor {
86                tag: TAG,
87                reason: "xait_update_policy exceeds 3 bits",
88            });
89        }
90        let len = self.serialized_len();
91        if buf.len() < len {
92            return Err(Error::OutputBufferTooSmall {
93                need: len,
94                have: buf.len(),
95            });
96        }
97        buf[0] = TAG;
98        buf[1] = BODY_LEN as u8;
99        buf[2..4].copy_from_slice(&self.xait_original_network_id.to_be_bytes());
100        buf[4..6].copy_from_slice(&self.xait_service_id.to_be_bytes());
101        buf[6] = ((self.xait_version_number & VERSION_MAX) << 3)
102            | (self.xait_update_policy & UPDATE_POLICY_MAX);
103        Ok(len)
104    }
105}
106impl<'a> crate::traits::DescriptorDef<'a> for XaitLocationDescriptor {
107    const TAG: u8 = TAG;
108    const NAME: &'static str = "XAIT_LOCATION";
109}
110
111#[cfg(test)]
112mod tests {
113    use super::*;
114
115    #[test]
116    fn parse_extracts_fields() {
117        // onid=0x2024, sid=0x1234, version=5, policy=1.
118        let bytes = [TAG, 5, 0x20, 0x24, 0x12, 0x34, (5 << 3) | 1];
119        let d = XaitLocationDescriptor::parse(&bytes).unwrap();
120        assert_eq!(d.xait_original_network_id, 0x2024);
121        assert_eq!(d.xait_service_id, 0x1234);
122        assert_eq!(d.xait_version_number, 5);
123        assert_eq!(d.xait_update_policy, 1);
124    }
125
126    #[test]
127    fn parse_ignores_trailing_bytes() {
128        let bytes = [TAG, 6, 0x00, 0x01, 0x00, 0x02, 0x00, 0xFF];
129        let d = XaitLocationDescriptor::parse(&bytes).unwrap();
130        assert_eq!(d.xait_original_network_id, 0x0001);
131        assert_eq!(d.xait_service_id, 0x0002);
132        assert_eq!(d.xait_version_number, 0);
133        assert_eq!(d.xait_update_policy, 0);
134    }
135
136    #[test]
137    fn parse_rejects_wrong_tag() {
138        let bytes = [0x7C, 5, 0, 0, 0, 0, 0];
139        assert!(matches!(
140            XaitLocationDescriptor::parse(&bytes).unwrap_err(),
141            Error::InvalidDescriptor { tag: 0x7C, .. }
142        ));
143    }
144
145    #[test]
146    fn parse_rejects_body_too_short() {
147        let bytes = [TAG, 4, 0, 0, 0, 0];
148        assert!(matches!(
149            XaitLocationDescriptor::parse(&bytes).unwrap_err(),
150            Error::InvalidDescriptor { .. }
151        ));
152    }
153
154    #[test]
155    fn parse_rejects_length_overrunning_buffer() {
156        let bytes = [TAG, 5, 0, 0, 0];
157        assert!(matches!(
158            XaitLocationDescriptor::parse(&bytes).unwrap_err(),
159            Error::BufferTooShort { .. }
160        ));
161    }
162
163    #[test]
164    fn serialize_round_trip() {
165        let d = XaitLocationDescriptor {
166            xait_original_network_id: 0x233A,
167            xait_service_id: 0x4470,
168            xait_version_number: 0x1F,
169            xait_update_policy: 0,
170        };
171        let mut buf = vec![0u8; d.serialized_len()];
172        d.serialize_into(&mut buf).unwrap();
173        assert_eq!(XaitLocationDescriptor::parse(&buf).unwrap(), d);
174    }
175
176    #[test]
177    fn serialize_rejects_version_over_range() {
178        let d = XaitLocationDescriptor {
179            xait_original_network_id: 0,
180            xait_service_id: 0,
181            xait_version_number: 0x20,
182            xait_update_policy: 0,
183        };
184        let mut buf = vec![0u8; d.serialized_len()];
185        assert!(matches!(
186            d.serialize_into(&mut buf).unwrap_err(),
187            Error::InvalidDescriptor { .. }
188        ));
189    }
190
191    #[test]
192    fn serialize_rejects_update_policy_over_range() {
193        let d = XaitLocationDescriptor {
194            xait_original_network_id: 0,
195            xait_service_id: 0,
196            xait_version_number: 0,
197            xait_update_policy: 0x08,
198        };
199        let mut buf = vec![0u8; d.serialized_len()];
200        assert!(matches!(
201            d.serialize_into(&mut buf).unwrap_err(),
202            Error::InvalidDescriptor { .. }
203        ));
204    }
205
206    #[cfg(feature = "serde")]
207    #[test]
208    fn serde_round_trip() {
209        let d = XaitLocationDescriptor {
210            xait_original_network_id: 0x0001,
211            xait_service_id: 0x0539,
212            xait_version_number: 3,
213            xait_update_policy: 1,
214        };
215        let j = serde_json::to_string(&d).unwrap();
216        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
217        let _v: serde_json::Value = serde_json::from_str(&j).unwrap();
218    }
219}