rusmpp_core/pdus/owned/
query_broadcast_sm.rs

1use rusmpp_macros::Rusmpp;
2
3use crate::{
4    pdus::owned::Pdu,
5    tlvs::owned::{Tlv, TlvValue},
6    types::owned::COctetString,
7    values::*,
8};
9
10/// This command is issued by the ESME to query the status of a previously submitted
11/// broadcast message. The message can be queried either on the basis of the Message Center
12/// assigned reference message_id returned in the broadcast_sm_resp or by the ESME
13/// assigned message reference number user_message_reference as indicated in the
14/// broadcast_sm operation associated with that message.
15///
16/// Note:  Where the broadcast is queried on the basis of the ESME assigned message
17/// reference user_message_reference this should be qualified within the service by the
18/// system_id and/or the system_type associated with the query_broadcast_sm operation
19/// (specified in the bind operation). If more than one message with the same
20/// user_message_reference value is present in the Message Center, the details of the most
21/// recently submitted message with the specified user_message_reference value will be
22/// returned in the query_broadcast_sm_resp.
23#[derive(Default, Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Rusmpp)]
24#[rusmpp(decode = owned, test = skip)]
25#[cfg_attr(feature = "arbitrary", derive(::arbitrary::Arbitrary))]
26#[cfg_attr(feature = "serde", derive(::serde::Serialize))]
27#[cfg_attr(feature = "serde-deserialize-unchecked", derive(::serde::Deserialize))]
28pub struct QueryBroadcastSm {
29    /// Message ID of the message to be queried. This must be
30    /// the MC assigned Message ID allocated to the original
31    /// short message when submitted to the MC by the
32    /// broadcast_sm command, and returned in the response
33    /// PDU by the MC.
34    ///
35    /// Set to NULL if setting user_message_reference.
36    pub message_id: COctetString<1, 65>,
37    /// Type of Number for source address.
38    ///
39    /// If not known, set to NULL (Unknown).
40    pub source_addr_ton: Ton,
41    /// Numbering Plan Indicator for source address.
42    ///
43    /// If not known, set to NULL (Unknown).
44    pub source_addr_npi: Npi,
45    /// Address of SME which originated this message.
46    ///
47    /// If not known, set to NULL (Unknown).
48    pub source_addr: COctetString<1, 21>,
49    /// ESME assigned message reference number. [`UserMessageReference`].
50    #[rusmpp(length = "checked")]
51    user_message_reference: Option<Tlv>,
52}
53
54impl QueryBroadcastSm {
55    pub fn new(
56        message_id: COctetString<1, 65>,
57        source_addr_ton: Ton,
58        source_addr_npi: Npi,
59        source_addr: COctetString<1, 21>,
60        user_message_reference: Option<UserMessageReference>,
61    ) -> Self {
62        let user_message_reference = user_message_reference
63            .map(TlvValue::UserMessageReference)
64            .map(From::from);
65
66        Self {
67            message_id,
68            source_addr_ton,
69            source_addr_npi,
70            source_addr,
71            user_message_reference,
72        }
73    }
74
75    pub const fn user_message_reference_tlv(&self) -> Option<&Tlv> {
76        self.user_message_reference.as_ref()
77    }
78
79    pub fn user_message_reference(&self) -> Option<UserMessageReference> {
80        self.user_message_reference_tlv()
81            .and_then(|tlv| match tlv.value() {
82                Some(TlvValue::UserMessageReference(value)) => Some(value),
83                _ => None,
84            })
85            .copied()
86    }
87
88    pub fn set_user_message_reference(
89        &mut self,
90        user_message_reference: Option<UserMessageReference>,
91    ) {
92        self.user_message_reference = user_message_reference
93            .map(TlvValue::UserMessageReference)
94            .map(From::from);
95    }
96
97    pub fn builder() -> QueryBroadcastSmBuilder {
98        QueryBroadcastSmBuilder::new()
99    }
100}
101
102impl From<QueryBroadcastSm> for Pdu {
103    fn from(value: QueryBroadcastSm) -> Self {
104        Self::QueryBroadcastSm(value)
105    }
106}
107
108#[derive(Debug, Default)]
109pub struct QueryBroadcastSmBuilder {
110    inner: QueryBroadcastSm,
111}
112
113impl QueryBroadcastSmBuilder {
114    pub fn new() -> Self {
115        Self::default()
116    }
117
118    pub fn message_id(mut self, message_id: COctetString<1, 65>) -> Self {
119        self.inner.message_id = message_id;
120        self
121    }
122
123    pub fn source_addr_ton(mut self, source_addr_ton: Ton) -> Self {
124        self.inner.source_addr_ton = source_addr_ton;
125        self
126    }
127
128    pub fn source_addr_npi(mut self, source_addr_npi: Npi) -> Self {
129        self.inner.source_addr_npi = source_addr_npi;
130        self
131    }
132
133    pub fn source_addr(mut self, source_addr: COctetString<1, 21>) -> Self {
134        self.inner.source_addr = source_addr;
135        self
136    }
137
138    pub fn user_message_reference(
139        mut self,
140        user_message_reference: Option<UserMessageReference>,
141    ) -> Self {
142        self.inner
143            .set_user_message_reference(user_message_reference);
144        self
145    }
146
147    pub fn build(self) -> QueryBroadcastSm {
148        self.inner
149    }
150}
151
152#[cfg(test)]
153mod tests {
154    use std::str::FromStr;
155
156    use crate::tests::TestInstance;
157
158    use super::*;
159
160    impl TestInstance for QueryBroadcastSm {
161        fn instances() -> alloc::vec::Vec<Self> {
162            alloc::vec![
163                Self::default(),
164                Self::builder()
165                    .message_id(COctetString::from_str("123456789012345678901234567890").unwrap())
166                    .source_addr_ton(Ton::International)
167                    .source_addr_npi(Npi::Isdn)
168                    .source_addr(COctetString::from_str("1234567890").unwrap())
169                    .build(),
170                Self::builder()
171                    .message_id(COctetString::from_str("123456789012345678901234567890").unwrap())
172                    .source_addr_ton(Ton::International)
173                    .source_addr_npi(Npi::Isdn)
174                    .source_addr(COctetString::from_str("1234567890").unwrap())
175                    .user_message_reference(Some(UserMessageReference::new(69)))
176                    .build(),
177            ]
178        }
179    }
180
181    #[test]
182    fn encode_decode() {
183        crate::tests::owned::encode_decode_with_length_test_instances::<QueryBroadcastSm>();
184    }
185}