1use std::fmt;
11use std::str::FromStr;
12use std::convert::{Infallible, TryFrom};
13use num_derive::FromPrimitive;
14use num_traits::FromPrimitive;
15use crate::gsm_encoding::{GsmMessageData, gsm_decode_string, decode_sms_7bit};
16use crate::{check_offset, PDUError, PDUResult};
17
18#[repr(u8)]
21#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive, Hash)]
22pub enum TypeOfNumber {
23 Unknown = 0b0_000_0000,
25 International = 0b0_001_0000,
27 National = 0b0_010_0000,
29 Special = 0b0_011_0000,
31 Gsm = 0b0_101_0000,
36 Short = 0b0_110_0000,
38 Reserved = 0b0_111_0000
40}
41#[repr(u8)]
45#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive, Hash)]
46pub enum NumberingPlanIdentification {
47 NetworkDetermined = 0b0_000_0000,
48 IsdnTelephone = 0b0_000_0001,
49 Data = 0b0_000_0011,
50 Telex = 0b0_000_0100,
51 National = 0b0_000_1000,
52 Private = 0b0_000_1001,
53 Ermes = 0b0_000_1010
54}
55#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
61pub struct AddressType {
62 pub type_of_number: TypeOfNumber,
63 pub numbering_plan_identification: NumberingPlanIdentification
64}
65impl Default for AddressType {
66 fn default() -> Self {
67 AddressType {
68 type_of_number: TypeOfNumber::International,
69 numbering_plan_identification: NumberingPlanIdentification::IsdnTelephone
70 }
71 }
72}
73impl TryFrom<u8> for AddressType {
74 type Error = PDUError;
75 fn try_from(b: u8) -> PDUResult<Self> {
76 let ton = b & 0b0_111_0000;
77 let ton = TypeOfNumber::from_u8(ton)
78 .ok_or(PDUError::InvalidPdu("invalid type_of_number"))?;
79 let npi = b & 0b0_000_1111;
80 let npi = NumberingPlanIdentification::from_u8(npi)
81 .ok_or(PDUError::InvalidPdu("invalid numbering_plan_identification"))?;
82 Ok(Self {
83 type_of_number: ton,
84 numbering_plan_identification: npi
85 })
86 }
87}
88impl Into<u8> for AddressType {
89 fn into(self) -> u8 {
90 let mut ret: u8 = 0b1_000_0000;
91 ret |= self.type_of_number as u8;
92 ret |= self.numbering_plan_identification as u8;
93 ret
94 }
95}
96#[derive(Debug, Clone, PartialEq, Eq, Hash)]
98pub struct PhoneNumber(pub Vec<u8>);
99impl<'a> From<&'a [u8]> for PhoneNumber {
104 fn from(b: &[u8]) -> Self {
105 let mut ret = vec![];
106 for b in b.iter() {
107 let first = b & 0b0000_1111;
108 let second = (b & 0b1111_0000) >> 4;
109 ret.push(first);
110 if second != 0b0000_1111 {
111 ret.push(second);
112 }
113 }
114 PhoneNumber(ret)
115 }
116}
117impl PhoneNumber {
118 pub fn from_gsm(b: &[u8], len: usize) -> Self {
120 PhoneNumber(decode_sms_7bit(b, 0, len))
121 }
122 pub fn as_bytes(&self) -> Vec<u8> {
124 let mut ret = vec![];
125 let mut cur = 0b0000_0000;
126 for (i, b) in self.0.iter().enumerate() {
127 let mut b = *b;
128 if i % 2 == 0 {
129 cur |= b;
130 }
131 else {
132 b = b << 4;
133 cur |= b;
134 ret.push(cur);
135 cur = 0b0000_0000;
136 }
137 }
138 if self.0.len() % 2 != 0 {
139 cur |= 0b1111_0000;
140 ret.push(cur);
141 }
142 ret
143 }
144}
145#[derive(Debug, Clone, PartialEq, Eq, Hash)]
152pub struct PduAddress {
153 pub type_addr: AddressType,
154 pub number: PhoneNumber
155}
156impl fmt::Display for PduAddress {
157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
158
159 let prefix = match self.type_addr.type_of_number {
160 TypeOfNumber::International => "+",
161 _ => ""
162 };
163 write!(f, "{}", prefix)?;
164 if self.type_addr.type_of_number == TypeOfNumber::Gsm {
165 write!(f, "{}", gsm_decode_string(&self.number.0))?;
166 }
167 else {
168 for b in self.number.0.iter() {
169 write!(f, "{}", b)?;
170 }
171 }
172 Ok(())
173 }
174}
175impl FromStr for PduAddress {
176 type Err = Infallible;
177 fn from_str(st: &str) -> Result<Self, Infallible> {
178 let mut int = false;
179 let buf = st.chars()
180 .filter_map(|x| {
181 match x {
182 '0'..='9' => Some(x as u8 - 48),
183 '+' => {
184 int = true;
185 None
186 },
187 _ => None
188 }
189 }).collect::<Vec<_>>();
190 let ton = if int {
191 TypeOfNumber::International
192 }
193 else {
194 TypeOfNumber::Unknown
195 };
196 Ok(PduAddress {
197 type_addr: AddressType {
198 type_of_number: ton,
199 numbering_plan_identification: NumberingPlanIdentification::IsdnTelephone
200 },
201 number: PhoneNumber(buf)
202 })
203 }
204}
205impl<'a> TryFrom<&'a [u8]> for PduAddress {
206 type Error = PDUError;
207 fn try_from(b: &[u8]) -> PDUResult<Self> {
208 if b.len() < 3 {
209 Err(PDUError::InvalidPdu("tried to make a PduAddress from less than 3 bytes"))?
210 }
211 let len = b[0] as usize;
212 let type_addr = AddressType::try_from(b[1])?;
213 let number = if type_addr.type_of_number == TypeOfNumber::Gsm {
214 let len = (len * 4) / 7;
215 PhoneNumber::from_gsm(&b[2..], len)
216 }
217 else {
218 PhoneNumber::from(&b[2..])
219 };
220 Ok(PduAddress { type_addr, number })
221 }
222}
223impl PduAddress {
224 pub fn as_bytes(&self, broken_len: bool) -> Vec<u8> {
232 let mut ret = vec![];
233 ret.push(self.type_addr.into());
234 ret.extend(self.number.as_bytes());
235 let len = if broken_len {
236 self.number.0.len()
237 } else {
238 ret.len()
239 };
240 ret.insert(0, len as u8);
241 ret
242 }
243}
244#[repr(u8)]
246#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
247pub enum MessageType {
248 SmsDeliver = 0b000000_00,
250 SmsCommand = 0b000000_10,
252 SmsSubmit = 0b000000_01,
254 Reserved = 0b000000_11
256}
257#[repr(u8)]
259#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
260pub enum VpFieldValidity {
261 Invalid = 0b00,
263 Relative = 0b10,
265 Enhanced = 0b01,
267 Absolute = 0b11,
269}
270#[derive(Debug, Copy, Clone, PartialEq, Eq)]
272pub struct PduFirstOctet {
273 pub mti: MessageType,
275 pub rd: bool,
277 pub vpf: VpFieldValidity,
279 pub srr: bool,
281 pub udhi: bool,
283 pub rp: bool
285}
286impl From<u8> for PduFirstOctet {
287 fn from(b: u8) -> Self {
288 let rd = (b & 0b0000_0100) > 0;
289 let srr = (b & 0b0010_0000) > 0;
290 let udhi = (b & 0b0100_0000) > 0;
291 let rp = (b & 0b1000_0000) > 0;
292 let mti = MessageType::from_u8(b & 0b0000_0011)
293 .expect("MessageType conversions should be exhaustive!");
294 let vpf = VpFieldValidity::from_u8((b & 0b0001_1000) >> 3)
295 .expect("VpFieldValidity conversions should be exhaustive!");
296 PduFirstOctet { rd, srr, udhi, rp, mti, vpf }
297 }
298}
299impl Into<u8> for PduFirstOctet {
300 fn into(self) -> u8 {
301 let mut ret = 0b0000_0000;
302 ret |= self.mti as u8;
303 ret |= (self.vpf as u8) << 3;
304 if self.rd {
305 ret |= 0b0000_0100;
306 }
307 if self.srr {
308 ret |= 0b0010_0000;
309 }
310 if self.udhi {
311 ret |= 0b0100_0000;
312 }
313 if self.rp {
314 ret |= 0b1000_0000;
315 }
316 ret
317 }
318}
319#[derive(Debug, Copy, Clone, PartialEq, Eq)]
328pub enum DataCodingScheme {
329 Standard {
331 compressed: bool,
333 class: Option<MessageClass>,
335 encoding: MessageEncoding
337 },
338 Reserved,
340 MessageWaitingDiscard {
342 waiting: bool,
344 type_indication: MessageWaitingType,
346 },
347 MessageWaiting {
349 waiting: bool,
351 type_indication: MessageWaitingType,
353 ucs2: bool
355 }
356}
357impl DataCodingScheme {
358 pub fn encoding(&self) -> MessageEncoding {
362 use self::DataCodingScheme::*;
363 match *self {
364 Standard { encoding, .. } => encoding,
365 Reserved => MessageEncoding::Gsm7Bit,
366 MessageWaitingDiscard { .. } => MessageEncoding::Gsm7Bit,
367 MessageWaiting { ucs2, .. } => if ucs2 {
368 MessageEncoding::Ucs2
369 }
370 else {
371 MessageEncoding::Gsm7Bit
372 }
373 }
374 }
375}
376impl From<u8> for DataCodingScheme {
377 fn from(b: u8) -> Self {
378 if (b & 0b1100_0000) == 0b0000_0000 {
379 let compressed = (b & 0b0010_0000) > 0;
380 let reserved = (b & 0b0001_0000) > 0;
381 let class = Some(if reserved {
382 MessageClass::StoreToNv
385 }
386 else {
387 MessageClass::from_u8(b & 0b0000_0011)
388 .expect("MessageClass conversions should be exhaustive!")
389 });
390 let encoding = MessageEncoding::from_u8(b & 0b0000_1100)
391 .expect("MessageEncoding conversions should be exhaustive!");
392 DataCodingScheme::Standard { compressed, class, encoding }
393 }
394 else if (b & 0b1111_0000) == 0b1111_0000 {
395 let compressed = false;
396 let class = Some(MessageClass::from_u8(b & 0b0000_0011)
397 .expect("MessageClass conversions should be exhaustive!"));
398 let encoding = if (b & 0b0000_0100) > 0 {
399 MessageEncoding::Gsm7Bit
400 }
401 else {
402 MessageEncoding::EightBit
403 };
404 DataCodingScheme::Standard { compressed, class, encoding }
405 }
406 else if (b & 0b1111_0000) == 0b1100_0000 {
407 let waiting = (b & 0b0000_1000) > 0;
408 let type_indication = MessageWaitingType::from_u8(b & 0b0000_0011)
409 .expect("MessageWaitingType conversions should be exhaustive!");
410 DataCodingScheme::MessageWaitingDiscard { waiting, type_indication }
411 }
412 else if (b & 0b1111_0000) == 0b1101_0000 || (b & 0b1111_0000) == 0b1110_0000 {
413 let ucs2 = (b & 0b1111_0000) == 0b1110_0000;
414 let waiting = (b & 0b0000_1000) > 0;
415 let type_indication = MessageWaitingType::from_u8(b & 0b0000_0011)
416 .expect("MessageWaitingType conversions should be exhaustive!");
417 DataCodingScheme::MessageWaiting { ucs2, waiting, type_indication }
418 }
419 else {
420 DataCodingScheme::Reserved
421 }
422 }
423}
424impl Into<u8> for DataCodingScheme {
425 fn into(self) -> u8 {
426 use self::DataCodingScheme::*;
427 match self {
428 Standard { compressed, class, encoding } => {
429 let mut ret = 0u8;
430 if compressed {
431 ret |= 0b0010_0000;
432 }
433 if let Some(class) = class {
434 ret |= 0b0001_0000;
435 ret |= class as u8;
436 }
437 ret |= encoding as u8;
438 ret
439 },
440 Reserved => 0b0100_0101,
441 MessageWaiting { waiting, type_indication, ucs2 } => {
442 let mut ret = if ucs2 {
443 0b1110_0000
444 }
445 else {
446 0b1101_0000
447 };
448 if waiting {
449 ret |= 0b0000_1000;
450 }
451 ret |= type_indication as u8;
452 ret
453 },
454 MessageWaitingDiscard { waiting, type_indication } => {
455 let mut ret = 0b1100_0000;
456 if waiting {
457 ret |= 0b0000_1000;
458 }
459 ret |= type_indication as u8;
460 ret
461 }
462 }
463 }
464}
465#[repr(u8)]
467#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
468pub enum MessageWaitingType {
469 Voice = 0b000000_00,
470 Fax = 0b000000_01,
471 Email = 0b000000_10,
472 Unknown = 0b000000_11
473}
474#[repr(u8)]
476#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
477pub enum MessageClass {
478 Silent = 0b000000_00,
480 StoreToNv = 0b000000_01,
482 StoreToSim = 0b000000_10,
484 StoreToTe = 0b000000_11
486}
487#[repr(u8)]
489#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
490pub enum MessageEncoding {
491 Gsm7Bit = 0b0000_00_00,
493 EightBit = 0b0000_01_00,
495 Ucs2 = 0b0000_10_00,
497 Reserved = 0b0000_11_00,
499}
500#[derive(Debug, Clone, PartialEq, Eq)]
502pub struct DeliverPduFirstOctet {
503 mti: MessageType,
505 sri: bool,
507 udhi: bool,
509 rp: bool
511}
512impl From<u8> for DeliverPduFirstOctet {
513 fn from(b: u8) -> Self {
514 let mti = MessageType::from_u8(b & 0b000000_11)
515 .expect("MessageType conversions should be exhaustive!");
516 let sri = (b & 0b00100000) > 0;
517 let udhi = (b & 0b01000000) > 0;
518 let rp = (b & 0b01000000) > 0;
519 DeliverPduFirstOctet { mti, sri, udhi, rp }
520 }
521}
522#[derive(Debug, Clone, PartialEq, Eq)]
524pub struct SmscTimestamp {
525 pub year: u8,
526 pub month: u8,
527 pub day: u8,
528 pub hour: u8,
529 pub minute: u8,
530 pub second: u8,
531 pub timezone: u8
533}
534pub(crate) fn reverse_byte(b: u8) -> u8 {
535 let units = b >> 4;
536 let tens = b & 0b0000_1111;
537 (tens * 10) + units
538}
539impl<'a> TryFrom<&'a [u8]> for SmscTimestamp {
540 type Error = PDUError;
541 fn try_from(b: &[u8]) -> PDUResult<Self> {
542 if b.len() != 7 {
543 Err(PDUError::InvalidPdu("SmscTimestamp must be 7 bytes long"))?
544 }
545 Ok(SmscTimestamp {
546 year: reverse_byte(b[0]),
547 month: reverse_byte(b[1]),
548 day: reverse_byte(b[2]),
549 hour: reverse_byte(b[3]),
550 minute: reverse_byte(b[4]),
551 second: reverse_byte(b[5]),
552 timezone: reverse_byte(b[6]),
553 })
554 }
555}
556#[derive(Debug, Clone, PartialEq, Eq)]
561pub struct DeliverPdu {
562 pub sca: Option<PduAddress>,
564 pub first_octet: DeliverPduFirstOctet,
566 pub originating_address: PduAddress,
568 pub dcs: DataCodingScheme,
570 pub scts: SmscTimestamp,
572 pub user_data: Vec<u8>,
574 pub user_data_len: u8
576}
577impl DeliverPdu {
578 pub fn get_message_data(&self) -> GsmMessageData {
582 GsmMessageData {
583 bytes: self.user_data.clone(),
584 user_data_len: self.user_data_len,
585 encoding: self.dcs.encoding(),
586 udh: self.first_octet.udhi
587 }
588 }
589}
590impl<'a> TryFrom<&'a [u8]> for DeliverPdu {
591 type Error = PDUError;
592 fn try_from(b: &[u8]) -> PDUResult<Self> {
593 if b.len() == 0 {
594 return Err(PDUError::InvalidPdu("zero-length input"));
595 }
596 let scalen = b[0];
597 let mut offset: usize = scalen as usize + 1;
598 let sca = if scalen > 0 {
599 let o = offset - 1;
600 check_offset!(b, o, "SCA");
601 Some(PduAddress::try_from(&b[0..offset])?)
602 }
603 else {
604 None
605 };
606 check_offset!(b, offset, "first octet");
607 let first_octet = DeliverPduFirstOctet::from(b[offset]);
608 offset += 1;
609 check_offset!(b, offset, "originating address len");
610 let destination_len_nybbles = b[offset];
611 let destination_len_octets = (destination_len_nybbles / 2) + destination_len_nybbles % 2;
614 let destination_offset = (destination_len_octets as usize) + 2;
618 let destination_end = offset + destination_offset;
619 let de = destination_end - 1;
620 check_offset!(b, de, "originating address");
621 let originating_address = PduAddress::try_from(&b[offset..destination_end])?;
622 offset += destination_offset;
623 check_offset!(b, offset, "protocol identifier");
624 let _pid = b[offset];
625 offset += 1;
626 check_offset!(b, offset, "data coding scheme");
627 let dcs = DataCodingScheme::from(b[offset]);
628 offset += 1;
629 let scts_end = offset + 7;
630 let ss = offset + 6;
631 check_offset!(b, ss, "service center timestamp");
632 let scts = SmscTimestamp::try_from(&b[offset..scts_end])?;
633 offset += 7;
634 check_offset!(b, offset, "user data len");
635 let user_data_len = b[offset];
636 offset += 1;
637 let user_data = if b.get(offset).is_some() {
638 b[offset..].to_owned()
639 }
640 else {
641 vec![]
642 };
643 Ok(DeliverPdu {
644 sca,
645 first_octet,
646 originating_address,
647 dcs,
648 scts,
649 user_data,
650 user_data_len
651 })
652
653 }
654}
655
656#[repr(u8)]
658#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, FromPrimitive)]
659pub enum MessageStatus {
660 ReceivedBySme = 0x00,
663 ForwardedUnconfirmed = 0x01,
665 ReplacedBySc = 0x02,
667
668 Congestion = 0x20,
671 SmeBusy = 0x21,
673 NoResponseFromSme = 0x22,
675 ServiceRejected = 0x23,
677 QualityOfServiceNotAvailable = 0x24,
679 ErrorInSme = 0x25,
681
682 RemoteProcedureError = 0x40,
685 IncompatibleDestination = 0x41,
687 ConnectionRejectedBySme = 0x42,
689 NotObtainable = 0x43,
691 QualityOfServiceNotAvailablePermanent = 0x44,
693 NoInterworkingAvailable = 0x45,
695 SmValidityPeriodExpired = 0x46,
697 SmDeletedByOriginatingSme = 0x47,
699 SmDeletedByScAdministration = 0x48,
701 SmDoesNotExist = 0x49,
703
704 CongestionNoMoreAttempts = 0x60,
707 SmeBusyNoMoreAttempts = 0x61,
709 NoResponseFromSmeNoMoreAttempts = 0x62,
711 ServiceRejectedNoMoreAttempts = 0x63,
713 QualityOfServiceNotAvailableNoMoreAttempts = 0x64,
715 ErrorInSmeNoMoreAttempts = 0x65,
717}
718
719impl MessageStatus {
720 pub fn is_success(&self) -> bool {
722 matches!(self, MessageStatus::ReceivedBySme | MessageStatus::ForwardedUnconfirmed | MessageStatus::ReplacedBySc)
723 }
724
725 pub fn is_temporary_error(&self) -> bool {
727 let status_code = *self as u8;
728 (0x20..=0x3F).contains(&status_code)
729 }
730
731 pub fn is_permanent_error(&self) -> bool {
734 let status_code = *self as u8;
735 (0x40..=0x6F).contains(&status_code)
736 }
737}
738
739#[derive(Debug, Clone, PartialEq, Eq)]
741pub struct StatusReportPduFirstOctet {
742 pub mti: MessageType,
744 pub mms: bool,
746 pub lp: bool,
748 pub srq: bool,
750 pub udhi: bool,
752}
753
754impl From<u8> for StatusReportPduFirstOctet {
755 fn from(b: u8) -> Self {
756 let mti = MessageType::from_u8(b & 0b00000011)
757 .expect("MessageType conversions should be exhaustive!");
758 let mms = (b & 0b00000100) > 0;
759 let lp = (b & 0b00001000) > 0;
760 let srq = (b & 0b00100000) > 0;
761 let udhi = (b & 0b01000000) > 0;
762
763 StatusReportPduFirstOctet { mti, mms, lp, srq, udhi }
764 }
765}
766
767#[derive(Debug, Clone, PartialEq, Eq)]
772pub struct StatusReportPdu {
773 pub sca: Option<PduAddress>,
775 pub first_octet: StatusReportPduFirstOctet,
777 pub message_reference: u8,
779 pub recipient_address: PduAddress,
781 pub scts: SmscTimestamp,
783 pub discharge_time: SmscTimestamp,
785 pub status: MessageStatus,
787 pub parameter_indicating: Option<u8>,
789 pub user_data: Vec<u8>,
791 pub user_data_len: u8
793}
794impl StatusReportPdu {
795 pub fn get_message_data(&self) -> Option<GsmMessageData> {
800 if self.user_data.is_empty() {
801 None
802 } else {
803 Some(GsmMessageData {
804 bytes: self.user_data.clone(),
805 user_data_len: self.user_data_len,
806 encoding: MessageEncoding::Gsm7Bit, udh: self.first_octet.udhi
808 })
809 }
810 }
811}
812impl<'a> TryFrom<&'a [u8]> for StatusReportPdu {
813 type Error = PDUError;
814
815 fn try_from(b: &[u8]) -> PDUResult<Self> {
816 if b.is_empty() {
817 return Err(PDUError::InvalidPdu("zero-length input"));
818 }
819
820 let scalen = b[0];
821 let mut offset: usize = scalen as usize + 1;
822
823 let sca = if scalen > 0 {
825 let o = offset - 1;
826 check_offset!(b, o, "SCA");
827 Some(PduAddress::try_from(&b[0..offset])?)
828 } else {
829 None
830 };
831
832 check_offset!(b, offset, "first octet");
834 let first_octet = StatusReportPduFirstOctet::from(b[offset]);
835 offset += 1;
836
837 check_offset!(b, offset, "message reference");
839 let message_reference = b[offset];
840 offset += 1;
841
842 check_offset!(b, offset, "recipient address len");
844 let recipient_len_nybbles = b[offset];
845 let recipient_len_octets = (recipient_len_nybbles / 2) + (recipient_len_nybbles % 2);
846 let recipient_offset = (recipient_len_octets as usize) + 2;
847 let recipient_end = offset + recipient_offset;
848 let re = recipient_end - 1;
849 check_offset!(b, re, "recipient address");
850 let recipient_address = PduAddress::try_from(&b[offset..recipient_end])?;
851 offset += recipient_offset;
852
853 let scts_end = offset + 7;
855 let ss = offset + 6;
856 check_offset!(b, ss, "service center timestamp");
857 let scts = SmscTimestamp::try_from(&b[offset..scts_end])?;
858 offset += 7;
859
860 let discharge_end = offset + 7;
862 let ds = offset + 6;
863 check_offset!(b, ds, "discharge time");
864 let discharge_time = SmscTimestamp::try_from(&b[offset..discharge_end])?;
865 offset += 7;
866
867 check_offset!(b, offset, "status");
869 let status = MessageStatus::from_u8(b[offset])
870 .unwrap_or(MessageStatus::ErrorInSme); offset += 1;
872
873 let parameter_indicating = if first_octet.udhi && offset < b.len() {
875 let pi = b[offset];
876 offset += 1;
877 Some(pi)
878 } else {
879 None
880 };
881
882 let (user_data_len, user_data) = if offset < b.len() {
884 check_offset!(b, offset, "user data len");
885 let udl = b[offset];
886 offset += 1;
887
888 let ud = if offset < b.len() {
889 b[offset..].to_owned()
890 } else {
891 vec![]
892 };
893
894 (udl, ud)
895 } else {
896 (0, vec![])
897 };
898
899 Ok(StatusReportPdu {
900 sca,
901 first_octet,
902 message_reference,
903 recipient_address,
904 scts,
905 discharge_time,
906 status,
907 parameter_indicating,
908 user_data,
909 user_data_len,
910 })
911 }
912}
913
914#[derive(Debug, Clone, PartialEq, Eq)]
919pub struct SubmitPdu {
920 pub sca: Option<PduAddress>,
926 pub first_octet: PduFirstOctet,
928 pub message_id: u8,
933 pub destination: PduAddress,
935 pub dcs: DataCodingScheme,
937 pub validity_period: u8,
941 pub user_data: Vec<u8>,
943 pub user_data_len: u8
945}
946impl SubmitPdu {
947 pub fn set_sca(&mut self, sca: PduAddress) {
949 self.sca = Some(sca);
950 }
951
952 pub fn make_simple_message(recipient: PduAddress, msg: GsmMessageData) -> Self {
953 SubmitPdu {
954 sca: None,
955 first_octet: PduFirstOctet {
956 mti: MessageType::SmsSubmit,
957 rd: false,
958 vpf: VpFieldValidity::Invalid,
959 rp: false,
960 udhi: msg.udh,
961 srr: false
962 },
963 message_id: 0,
964 destination: recipient,
965 dcs: DataCodingScheme::Standard {
966 compressed: false,
967 class: Some(MessageClass::StoreToNv),
968 encoding: msg.encoding
969 },
970 validity_period: 0,
971 user_data: msg.bytes,
972 user_data_len: msg.user_data_len as u8
973 }
974 }
975}
976impl SubmitPdu {
977
978 pub fn as_bytes(&self) -> (Vec<u8>, usize) {
980 let mut ret = vec![];
981
982 let sca_field_len = if let Some(ref sca) = self.sca {
984 let sca_bytes = sca.as_bytes(false);
985 ret.extend(&sca_bytes);
986 sca_bytes.len()
987 } else {
988 ret.push(0); 1
990 };
991
992 ret.push(self.first_octet.into());
993 ret.push(self.message_id);
994 ret.extend(self.destination.as_bytes(true));
995 ret.push(0); let dcs_byte = self.dcs.into();
997 ret.push(dcs_byte);
998
999 if self.first_octet.vpf != VpFieldValidity::Invalid {
1000 ret.push(self.validity_period);
1001 }
1002
1003 ret.push(self.user_data_len);
1004 ret.extend(self.user_data.clone());
1005
1006 let tpdu_len = ret.len() - sca_field_len;
1008 (ret, tpdu_len)
1009 }
1010}