rusmpp_core/pdus/borrowed/
broadcast_sm.rs

1use rusmpp_macros::Rusmpp;
2
3use crate::{
4    pdus::borrowed::Pdu,
5    tlvs::borrowed::{BroadcastRequestTlvValue, Tlv},
6    types::borrowed::{COctetString, EmptyOrFullCOctetString},
7    values::{borrowed::*, *},
8};
9
10/// This operation is issued by the ESME to submit a message to the Message Centre for
11/// broadcast to a specified geographical area or set of geographical areas.
12#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Default, Rusmpp)]
13#[rusmpp(decode = borrowed, test = skip)]
14#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
15#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
16pub struct BroadcastSm<'a, const N: usize> {
17    /// The service_type parameter can be used to
18    /// indicate the SMS Application service
19    /// associated with the message. Specifying the
20    /// service_type allows the ESME to avail of enhanced
21    /// messaging services such as “replace by
22    /// service_type” or control the teleservice used on the air
23    /// interface.
24    ///
25    /// Set to NULL for default MC settings.
26    pub service_type: ServiceType<'a>,
27    /// Type of Number for source address.
28    ///
29    /// If not known, set to NULL (Unknown).
30    pub source_addr_ton: Ton,
31    /// Numbering Plan Indicator for source address.
32    ///
33    /// If not known, set to NULL (Unknown).
34    pub source_addr_npi: Npi,
35    /// Address of SME which originated this message.
36    ///
37    /// If not known, set to NULL (Unknown).
38    pub source_addr: COctetString<'a, 1, 21>,
39    /// If using broadcast_sm to replace a message,
40    /// previously submitted for broadcast, then set
41    /// message_id to the MC assigned message ID
42    /// allocated to the original message and returned in
43    /// the broadcast_sm_resp  (to the original broadcast_sm request).
44    ///
45    /// Note: For "broadcast replace", either the message_id or the
46    /// user_message_reference field should be used. Both
47    /// fields must not be used simultaneously.
48    /// Set to NULL:
49    ///
50    /// * if not using MC message ID in broadcast_sm to
51    ///   replace a message, previously submitted for broadcast.
52    /// * if setting user_message_reference TLV.
53    pub message_id: COctetString<'a, 1, 65>,
54    /// Designates the propriety level of the message.
55    pub priority_flag: PriorityFlag,
56    /// The short message is to be scheduled by the MC for
57    /// delivery.
58    ///
59    /// Set to NULL for immediate message broadcast.
60    pub schedule_delivery_time: EmptyOrFullCOctetString<'a, 17>,
61    /// The validity period of this message.
62    ///
63    /// Set to NULL to specify that a ‘broadcast_rep_num’
64    /// parameter and a ‘broadcast_frequency_interval’
65    /// parameter have been specified from which a
66    /// default value should be derived.
67    pub validity_period: EmptyOrFullCOctetString<'a, 17>,
68    /// Flag indicating if the submitted message should
69    /// replace an existing message which has:
70    /// * (1) MC message ID matching the ID supplied in
71    ///   the message_id field.
72    /// * (2) or ESME assigned message reference number
73    ///   supplied in the user_message_reference field.
74    pub replace_if_present_flag: ReplaceIfPresentFlag,
75    /// Defines the encoding scheme of the short
76    /// message user data.
77    pub data_coding: DataCoding,
78    /// Indicates the short message to send from a list of pre-
79    /// defined (‘canned’) short messages stored on the MC.
80    ///
81    /// If not using a MC canned message, set to NULL.
82    pub sm_default_msg_id: u8,
83    /// Broadcast request TLVs ([`BroadcastRequestTlvValue`]).
84    #[rusmpp(length = "unchecked")]
85    #[cfg_attr(feature = "arbitrary", arbitrary(default))]
86    tlvs: heapless::vec::Vec<Tlv<'a>, N>,
87}
88
89impl<'a, const N: usize> BroadcastSm<'a, N> {
90    #[allow(clippy::too_many_arguments)]
91    pub fn new(
92        service_type: ServiceType<'a>,
93        source_addr_ton: Ton,
94        source_addr_npi: Npi,
95        source_addr: COctetString<'a, 1, 21>,
96        message_id: COctetString<'a, 1, 65>,
97        priority_flag: PriorityFlag,
98        schedule_delivery_time: EmptyOrFullCOctetString<'a, 17>,
99        validity_period: EmptyOrFullCOctetString<'a, 17>,
100        replace_if_present_flag: ReplaceIfPresentFlag,
101        data_coding: DataCoding,
102        sm_default_msg_id: u8,
103        tlvs: heapless::vec::Vec<BroadcastRequestTlvValue<'a>, N>,
104    ) -> Self {
105        let tlvs = tlvs.into_iter().map(From::from).collect();
106
107        Self {
108            service_type,
109            source_addr_ton,
110            source_addr_npi,
111            source_addr,
112            message_id,
113            priority_flag,
114            schedule_delivery_time,
115            validity_period,
116            replace_if_present_flag,
117            data_coding,
118            sm_default_msg_id,
119            tlvs,
120        }
121    }
122
123    pub fn tlvs(&'_ self) -> &'_ [Tlv<'_>] {
124        &self.tlvs
125    }
126
127    pub fn set_tlvs(&mut self, tlvs: heapless::vec::Vec<BroadcastRequestTlvValue<'a>, N>) {
128        self.tlvs = tlvs.into_iter().map(From::from).collect();
129    }
130
131    pub fn clear_tlvs(&mut self) {
132        self.tlvs.clear();
133    }
134
135    pub fn push_tlv(
136        &mut self,
137        tlv: impl Into<BroadcastRequestTlvValue<'a>>,
138    ) -> Result<(), Tlv<'a>> {
139        self.tlvs.push(Tlv::from(tlv.into()))?;
140        Ok(())
141    }
142
143    pub fn builder() -> BroadcastSmBuilder<'a, N> {
144        BroadcastSmBuilder::new()
145    }
146}
147
148impl<'a, const N: usize> From<BroadcastSm<'a, N>> for Pdu<'a, N> {
149    fn from(value: BroadcastSm<'a, N>) -> Self {
150        Self::BroadcastSm(value)
151    }
152}
153
154#[derive(Debug, Default)]
155pub struct BroadcastSmBuilder<'a, const N: usize> {
156    inner: BroadcastSm<'a, N>,
157}
158
159impl<'a, const N: usize> BroadcastSmBuilder<'a, N> {
160    pub fn new() -> Self {
161        Self::default()
162    }
163
164    pub fn service_type(mut self, service_type: ServiceType<'a>) -> Self {
165        self.inner.service_type = service_type;
166        self
167    }
168
169    pub fn source_addr_ton(mut self, source_addr_ton: Ton) -> Self {
170        self.inner.source_addr_ton = source_addr_ton;
171        self
172    }
173
174    pub fn source_addr_npi(mut self, source_addr_npi: Npi) -> Self {
175        self.inner.source_addr_npi = source_addr_npi;
176        self
177    }
178
179    pub fn source_addr(mut self, source_addr: COctetString<'a, 1, 21>) -> Self {
180        self.inner.source_addr = source_addr;
181        self
182    }
183
184    pub fn message_id(mut self, message_id: COctetString<'a, 1, 65>) -> Self {
185        self.inner.message_id = message_id;
186        self
187    }
188
189    pub fn priority_flag(mut self, priority_flag: PriorityFlag) -> Self {
190        self.inner.priority_flag = priority_flag;
191        self
192    }
193
194    pub fn schedule_delivery_time(
195        mut self,
196        schedule_delivery_time: EmptyOrFullCOctetString<'a, 17>,
197    ) -> Self {
198        self.inner.schedule_delivery_time = schedule_delivery_time;
199        self
200    }
201
202    pub fn validity_period(mut self, validity_period: EmptyOrFullCOctetString<'a, 17>) -> Self {
203        self.inner.validity_period = validity_period;
204        self
205    }
206
207    pub fn replace_if_present_flag(
208        mut self,
209        replace_if_present_flag: ReplaceIfPresentFlag,
210    ) -> Self {
211        self.inner.replace_if_present_flag = replace_if_present_flag;
212        self
213    }
214
215    pub fn data_coding(mut self, data_coding: DataCoding) -> Self {
216        self.inner.data_coding = data_coding;
217        self
218    }
219
220    pub fn sm_default_msg_id(mut self, sm_default_msg_id: u8) -> Self {
221        self.inner.sm_default_msg_id = sm_default_msg_id;
222        self
223    }
224
225    pub fn tlvs(mut self, tlvs: heapless::vec::Vec<BroadcastRequestTlvValue<'a>, N>) -> Self {
226        self.inner.set_tlvs(tlvs);
227        self
228    }
229
230    pub fn clear_tlvs(mut self) -> Self {
231        self.inner.clear_tlvs();
232        self
233    }
234
235    pub fn push_tlv(
236        mut self,
237        tlv: impl Into<BroadcastRequestTlvValue<'a>>,
238    ) -> Result<Self, Tlv<'a>> {
239        self.inner.push_tlv(tlv)?;
240        Ok(self)
241    }
242
243    pub fn build(self) -> BroadcastSm<'a, N> {
244        self.inner
245    }
246}
247
248#[cfg(test)]
249mod tests {
250    use crate::{tests::TestInstance, types::borrowed::OctetString};
251
252    use super::*;
253
254    impl<const N: usize> TestInstance for BroadcastSm<'_, N> {
255        fn instances() -> alloc::vec::Vec<Self> {
256            alloc::vec![
257                Self::default(),
258                Self::builder()
259                    .service_type(ServiceType::new(
260                        GenericServiceType::CellularMessaging.into(),
261                    ))
262                    .source_addr_ton(Ton::International)
263                    .source_addr_npi(Npi::Isdn)
264                    .source_addr(COctetString::new(b"SourceAddr\0").unwrap())
265                    .message_id(COctetString::new(b"MessageId\0").unwrap())
266                    .priority_flag(PriorityFlag::from(PriorityFlagType::from(GsmSms::from(1))))
267                    .schedule_delivery_time(EmptyOrFullCOctetString::empty())
268                    .validity_period(EmptyOrFullCOctetString::empty())
269                    .replace_if_present_flag(ReplaceIfPresentFlag::Replace)
270                    .data_coding(DataCoding::LatinHebrew)
271                    .sm_default_msg_id(0)
272                    .build(),
273                Self::builder()
274                    .service_type(ServiceType::new(
275                        GenericServiceType::UnstructuredSupplementaryServicesData.into(),
276                    ))
277                    .source_addr_ton(Ton::International)
278                    .source_addr_npi(Npi::Isdn)
279                    .source_addr(COctetString::new(b"SourceAddr\0").unwrap())
280                    .message_id(COctetString::new(b"MessageId\0").unwrap())
281                    .priority_flag(PriorityFlag::from(PriorityFlagType::from(Ansi136::Bulk)))
282                    .schedule_delivery_time(
283                        EmptyOrFullCOctetString::new(b"2023-10-01T00:00\0").unwrap(),
284                    )
285                    .validity_period(EmptyOrFullCOctetString::empty())
286                    .replace_if_present_flag(ReplaceIfPresentFlag::DoNotReplace)
287                    .data_coding(DataCoding::GsmMessageClassControl)
288                    .sm_default_msg_id(255)
289                    .tlvs(
290                        [
291                            BroadcastRequestTlvValue::CallbackNum(
292                                OctetString::new(b"1234567890").unwrap(),
293                            ),
294                            BroadcastRequestTlvValue::LanguageIndicator(LanguageIndicator::German),
295                            BroadcastRequestTlvValue::SmsSignal(1024),
296                        ]
297                        .into()
298                    )
299                    .build(),
300            ]
301        }
302    }
303
304    #[test]
305    fn encode_decode() {
306        crate::tests::borrowed::encode_decode_with_length_test_instances::<BroadcastSm<'static, 16>>(
307        );
308    }
309}