Skip to main content

dvb_si/descriptors/
service_move.rs

1//! Service Move Descriptor — ETSI EN 300 468 §6.2.37 (tag 0x60).
2//!
3//! Table 92 (PDF p. 102). Carried in the SDT when a service is being moved to a
4//! new location; gives the new (original_network_id, transport_stream_id,
5//! service_id) triple. Fixed 6-byte payload.
6
7use crate::error::{Error, Result};
8use crate::traits::Descriptor;
9use dvb_common::{Parse, Serialize};
10
11/// Descriptor tag for service_move_descriptor.
12pub const TAG: u8 = 0x60;
13const HEADER_LEN: usize = 2;
14/// Fixed payload length: three 16-bit identifiers (EN 300 468 Table 92).
15const BODY_LEN: u8 = 6;
16
17/// Service Move Descriptor (tag 0x60).
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20pub struct ServiceMoveDescriptor {
21    /// original_network_id of the service's new location.
22    pub new_original_network_id: u16,
23    /// transport_stream_id of the service's new location.
24    pub new_transport_stream_id: u16,
25    /// service_id of the service at its new location.
26    pub new_service_id: u16,
27}
28
29impl<'a> Parse<'a> for ServiceMoveDescriptor {
30    type Error = crate::error::Error;
31    fn parse(bytes: &'a [u8]) -> Result<Self> {
32        if bytes.len() < HEADER_LEN {
33            return Err(Error::BufferTooShort {
34                need: HEADER_LEN,
35                have: bytes.len(),
36                what: "ServiceMoveDescriptor header",
37            });
38        }
39        if bytes[0] != TAG {
40            return Err(Error::InvalidDescriptor {
41                tag: bytes[0],
42                reason: "unexpected tag for service_move_descriptor",
43            });
44        }
45        let length = bytes[1] as usize;
46        let total = HEADER_LEN + length;
47        if bytes.len() < total {
48            return Err(Error::BufferTooShort {
49                need: total,
50                have: bytes.len(),
51                what: "ServiceMoveDescriptor body",
52            });
53        }
54        if length != BODY_LEN as usize {
55            return Err(Error::InvalidDescriptor {
56                tag: TAG,
57                reason: "service_move_descriptor length must equal 6",
58            });
59        }
60        let b = HEADER_LEN;
61        Ok(Self {
62            new_original_network_id: u16::from_be_bytes([bytes[b], bytes[b + 1]]),
63            new_transport_stream_id: u16::from_be_bytes([bytes[b + 2], bytes[b + 3]]),
64            new_service_id: u16::from_be_bytes([bytes[b + 4], bytes[b + 5]]),
65        })
66    }
67}
68
69impl Serialize for ServiceMoveDescriptor {
70    type Error = crate::error::Error;
71    fn serialized_len(&self) -> usize {
72        HEADER_LEN + BODY_LEN as usize
73    }
74
75    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
76        let len = self.serialized_len();
77        if buf.len() < len {
78            return Err(Error::OutputBufferTooSmall {
79                need: len,
80                have: buf.len(),
81            });
82        }
83        buf[0] = TAG;
84        buf[1] = BODY_LEN;
85        let b = HEADER_LEN;
86        buf[b..b + 2].copy_from_slice(&self.new_original_network_id.to_be_bytes());
87        buf[b + 2..b + 4].copy_from_slice(&self.new_transport_stream_id.to_be_bytes());
88        buf[b + 4..b + 6].copy_from_slice(&self.new_service_id.to_be_bytes());
89        Ok(len)
90    }
91}
92
93impl<'a> Descriptor<'a> for ServiceMoveDescriptor {
94    const TAG: u8 = TAG;
95    fn descriptor_length(&self) -> u8 {
96        BODY_LEN
97    }
98}
99
100impl<'a> crate::traits::DescriptorDef<'a> for ServiceMoveDescriptor {
101    const TAG: u8 = TAG;
102    const NAME: &'static str = "SERVICE_MOVE";
103}
104
105#[cfg(test)]
106mod tests {
107    use super::*;
108
109    #[test]
110    fn parse_extracts_triple() {
111        let bytes = [TAG, 6, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03];
112        let d = ServiceMoveDescriptor::parse(&bytes).unwrap();
113        assert_eq!(d.new_original_network_id, 0x0001);
114        assert_eq!(d.new_transport_stream_id, 0x0002);
115        assert_eq!(d.new_service_id, 0x0003);
116    }
117
118    #[test]
119    fn parse_rejects_wrong_tag() {
120        let err = ServiceMoveDescriptor::parse(&[0x61, 6, 0, 0, 0, 0, 0, 0]).unwrap_err();
121        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x61, .. }));
122    }
123
124    #[test]
125    fn parse_rejects_short_buffer() {
126        let err = ServiceMoveDescriptor::parse(&[TAG]).unwrap_err();
127        assert!(matches!(err, Error::BufferTooShort { .. }));
128    }
129
130    #[test]
131    fn parse_rejects_truncated_body() {
132        // length=6 but only 4 payload bytes present.
133        let err = ServiceMoveDescriptor::parse(&[TAG, 6, 0, 1, 0, 2]).unwrap_err();
134        assert!(matches!(err, Error::BufferTooShort { .. }));
135    }
136
137    #[test]
138    fn parse_rejects_wrong_length() {
139        let err = ServiceMoveDescriptor::parse(&[TAG, 5, 0, 1, 0, 2, 0]).unwrap_err();
140        assert!(matches!(err, Error::InvalidDescriptor { .. }));
141    }
142
143    #[test]
144    fn serialize_round_trip() {
145        let d = ServiceMoveDescriptor {
146            new_original_network_id: 0x1234,
147            new_transport_stream_id: 0x5678,
148            new_service_id: 0x9ABC,
149        };
150        let mut buf = vec![0u8; d.serialized_len()];
151        d.serialize_into(&mut buf).unwrap();
152        let re = ServiceMoveDescriptor::parse(&buf).unwrap();
153        assert_eq!(d, re);
154    }
155
156    #[test]
157    fn serialize_rejects_too_small_buffer() {
158        let d = ServiceMoveDescriptor {
159            new_original_network_id: 1,
160            new_transport_stream_id: 2,
161            new_service_id: 3,
162        };
163        let mut tiny = [0u8; 5];
164        let err = d.serialize_into(&mut tiny).unwrap_err();
165        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
166    }
167
168    #[test]
169    fn descriptor_length_matches_payload() {
170        let d = ServiceMoveDescriptor {
171            new_original_network_id: 0,
172            new_transport_stream_id: 0,
173            new_service_id: 0,
174        };
175        assert_eq!(d.descriptor_length(), 6);
176    }
177
178    #[cfg(feature = "serde")]
179    #[test]
180    fn serde_round_trip() {
181        let d = ServiceMoveDescriptor {
182            new_original_network_id: 0x0001,
183            new_transport_stream_id: 0x0002,
184            new_service_id: 0x0003,
185        };
186        let json = serde_json::to_string(&d).unwrap();
187        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
188        let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
189    }
190}