1use crate::args::{parse_values, split, strip_prefix, Arg};
21use serde::Serialize;
22
23fn body(line: &str, prefix: &str) -> Option<Vec<Arg>> {
24 strip_prefix(line, prefix).map(split)
25}
26
27fn int<T: std::str::FromStr>(a: &[Arg], i: usize) -> Option<T> {
28 a.get(i)?.int()
29}
30
31fn text(a: &[Arg], i: usize) -> Option<String> {
32 a.get(i)?.text().map(str::to_string)
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
40#[serde(rename_all = "camelCase")]
41pub enum RegKind {
42 Creg,
44 Cgreg,
46 Cereg,
48 C5greg,
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
54#[serde(rename_all = "camelCase")]
55pub enum RegStatus {
56 NotRegistered,
58 Home,
60 Searching,
62 Denied,
64 Unknown,
66 Roaming,
68 HomeSmsOnly,
70 RoamingSmsOnly,
72 EmergencyOnly,
74 HomeCsfbNotPreferred,
76 RoamingCsfbNotPreferred,
78 AttachedRlos,
80 Other(u8),
82}
83
84impl RegStatus {
85 pub fn from_code(v: u8) -> Self {
86 use RegStatus::*;
87 match v {
88 0 => NotRegistered,
89 1 => Home,
90 2 => Searching,
91 3 => Denied,
92 4 => Unknown,
93 5 => Roaming,
94 6 => HomeSmsOnly,
95 7 => RoamingSmsOnly,
96 8 => EmergencyOnly,
97 9 => HomeCsfbNotPreferred,
98 10 => RoamingCsfbNotPreferred,
99 11 => AttachedRlos,
100 v => Other(v),
101 }
102 }
103
104 pub fn is_registered(self) -> bool {
106 use RegStatus::*;
107 matches!(
108 self,
109 Home | Roaming
110 | HomeSmsOnly
111 | RoamingSmsOnly
112 | HomeCsfbNotPreferred
113 | RoamingCsfbNotPreferred
114 )
115 }
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
120#[serde(rename_all = "camelCase")]
121pub enum AccessTech {
122 Gsm,
124 GsmCompact,
126 Utran,
128 GsmEgprs,
130 UtranHsdpa,
132 UtranHsupa,
134 UtranHsdpaHsupa,
136 Eutran,
138 EcGsmIot,
140 EutranNbS1,
142 Eutra5gcn,
144 Nr5gcn,
146 NgRan,
148 EutraNrDual,
150 Other(u8),
152}
153
154impl AccessTech {
155 pub fn from_code(v: u8) -> Self {
156 use AccessTech::*;
157 match v {
158 0 => Gsm,
159 1 => GsmCompact,
160 2 => Utran,
161 3 => GsmEgprs,
162 4 => UtranHsdpa,
163 5 => UtranHsupa,
164 6 => UtranHsdpaHsupa,
165 7 => Eutran,
166 8 => EcGsmIot,
167 9 => EutranNbS1,
168 10 => Eutra5gcn,
169 11 => Nr5gcn,
170 12 => NgRan,
171 13 => EutraNrDual,
172 v => Other(v),
173 }
174 }
175
176 fn from_arg(a: &[Arg], i: usize) -> Option<Self> {
177 int::<u8>(a, i).map(Self::from_code)
178 }
179}
180
181#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
184#[serde(rename_all = "camelCase")]
185pub struct Registration {
186 pub kind: RegKind,
187 pub n: Option<u8>,
189 pub stat: RegStatus,
190 pub lac: Option<u32>,
192 pub ci: Option<u64>,
194 pub act: Option<AccessTech>,
195 pub rac: Option<u8>,
197 pub allowed_nssai: Option<String>,
199 pub cause_type: Option<u8>,
200 pub reject_cause: Option<u32>,
201}
202
203impl Registration {
204 pub fn parse(line: &str) -> Option<Self> {
206 let (kind, a) = [
207 ("+CREG", RegKind::Creg),
208 ("+CGREG", RegKind::Cgreg),
209 ("+CEREG", RegKind::Cereg),
210 ("+C5GREG", RegKind::C5greg),
211 ]
212 .into_iter()
213 .find_map(|(p, k)| body(line, p).map(|a| (k, a)))?;
214
215 let response = a.len() != 3
220 && a.get(1)
221 .is_some_and(|f| !f.quoted && f.text.len() <= 2 && f.int::<u8>().is_some());
222 let (n, a) = if response {
223 (Some(int::<u8>(&a, 0)?), &a[1..])
224 } else {
225 (None, &a[..])
226 };
227 let stat = RegStatus::from_code(int(a, 0)?);
228 let hex = |i: usize| a.get(i).and_then(Arg::hex);
229 let mut r = Registration {
230 kind,
231 n,
232 stat,
233 lac: hex(1).and_then(|v| u32::try_from(v).ok()),
234 ci: hex(2),
235 act: AccessTech::from_arg(a, 3),
236 rac: None,
237 allowed_nssai: None,
238 cause_type: None,
239 reject_cause: None,
240 };
241 let cause_at = match kind {
242 RegKind::Creg | RegKind::Cereg => 4,
243 RegKind::Cgreg => {
244 r.rac = hex(4).and_then(|v| u8::try_from(v).ok());
245 5
246 }
247 RegKind::C5greg => {
248 r.allowed_nssai = text(a, 5);
249 6
250 }
251 };
252 r.cause_type = int(a, cause_at);
253 r.reject_cause = int(a, cause_at + 1);
254 Some(r)
255 }
256}
257
258#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
263#[serde(rename_all = "camelCase")]
264pub struct Csq {
265 pub rssi: u8,
266 pub ber: u8,
267}
268
269impl Csq {
270 pub fn parse(line: &str) -> Option<Self> {
271 let a = body(line, "+CSQ")?;
272 Some(Csq {
273 rssi: int(&a, 0)?,
274 ber: int(&a, 1)?,
275 })
276 }
277
278 pub fn rssi_dbm(&self) -> Option<i32> {
280 (self.rssi <= 31).then(|| -113 + 2 * self.rssi as i32)
281 }
282}
283
284#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
287#[serde(rename_all = "camelCase")]
288pub struct Cesq {
289 pub rxlev: u8,
290 pub ber: u8,
291 pub rscp: u8,
292 pub ecno: u8,
293 pub rsrq: u8,
294 pub rsrp: u8,
295 pub ss_rsrq: Option<u8>,
296 pub ss_rsrp: Option<u8>,
297 pub ss_sinr: Option<u8>,
298}
299
300impl Cesq {
301 pub fn parse(line: &str) -> Option<Self> {
302 let a = body(line, "+CESQ")?;
303 Some(Cesq {
304 rxlev: int(&a, 0)?,
305 ber: int(&a, 1)?,
306 rscp: int(&a, 2)?,
307 ecno: int(&a, 3)?,
308 rsrq: int(&a, 4)?,
309 rsrp: int(&a, 5)?,
310 ss_rsrq: int(&a, 6),
311 ss_rsrp: int(&a, 7),
312 ss_sinr: int(&a, 8),
313 })
314 }
315
316 pub fn rxlev_dbm(&self) -> Option<f64> {
318 (self.rxlev <= 63).then(|| -111.0 + self.rxlev as f64)
319 }
320 pub fn rscp_dbm(&self) -> Option<f64> {
322 (self.rscp <= 96).then(|| -121.0 + self.rscp as f64)
323 }
324 pub fn ecno_db(&self) -> Option<f64> {
326 (self.ecno <= 49).then(|| -24.5 + self.ecno as f64 * 0.5)
327 }
328 pub fn rsrq_db(&self) -> Option<f64> {
330 (self.rsrq <= 34).then(|| -20.0 + self.rsrq as f64 * 0.5)
331 }
332 pub fn rsrp_dbm(&self) -> Option<f64> {
334 (self.rsrp <= 97).then(|| -141.0 + self.rsrp as f64)
335 }
336}
337
338#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
343#[serde(rename_all = "camelCase")]
344pub enum Cpin {
345 Ready,
346 SimPin,
347 SimPuk,
348 PhSimPin,
349 PhFsimPin,
350 PhFsimPuk,
351 SimPin2,
352 SimPuk2,
353 PhNetPin,
354 PhNetPuk,
355 PhNetsubPin,
356 PhNetsubPuk,
357 PhSpPin,
358 PhSpPuk,
359 PhCorpPin,
360 PhCorpPuk,
361 Other(String),
363}
364
365impl Cpin {
366 pub fn parse(line: &str) -> Option<Self> {
367 let a = body(line, "+CPIN")?;
368 let code = a.first()?.text()?.to_string();
369 use Cpin::*;
370 Some(match code.to_ascii_uppercase().as_str() {
371 "READY" => Ready,
372 "SIM PIN" => SimPin,
373 "SIM PUK" => SimPuk,
374 "PH-SIM PIN" => PhSimPin,
375 "PH-FSIM PIN" => PhFsimPin,
376 "PH-FSIM PUK" => PhFsimPuk,
377 "SIM PIN2" => SimPin2,
378 "SIM PUK2" => SimPuk2,
379 "PH-NET PIN" => PhNetPin,
380 "PH-NET PUK" => PhNetPuk,
381 "PH-NETSUB PIN" => PhNetsubPin,
382 "PH-NETSUB PUK" => PhNetsubPuk,
383 "PH-SP PIN" => PhSpPin,
384 "PH-SP PUK" => PhSpPuk,
385 "PH-CORP PIN" => PhCorpPin,
386 "PH-CORP PUK" => PhCorpPuk,
387 _ => Other(code),
388 })
389 }
390}
391
392#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
398#[serde(rename_all = "camelCase")]
399pub struct CallerId {
400 pub number: String,
401 #[serde(rename = "type")]
403 pub number_type: Option<u8>,
404 pub subaddr: Option<String>,
405 pub satype: Option<u8>,
406 pub alpha: Option<String>,
407 pub cli_validity: Option<u8>,
409}
410
411impl CallerId {
412 fn parse_with(line: &str, prefix: &str) -> Option<Self> {
413 let a = body(line, prefix)?;
414 if !a.first()?.quoted {
416 return None;
417 }
418 Some(CallerId {
419 number: a.first()?.text.clone(),
420 number_type: int(&a, 1),
421 subaddr: text(&a, 2),
422 satype: int(&a, 3),
423 alpha: text(&a, 4),
424 cli_validity: int(&a, 5),
425 })
426 }
427
428 pub fn parse_clip(line: &str) -> Option<Self> {
429 Self::parse_with(line, "+CLIP")
430 }
431
432 pub fn parse_colp(line: &str) -> Option<Self> {
433 Self::parse_with(line, "+COLP").map(|c| CallerId {
434 cli_validity: None,
435 ..c
436 })
437 }
438}
439
440#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
443#[serde(rename_all = "camelCase")]
444pub struct CallWaiting {
445 pub number: String,
446 #[serde(rename = "type")]
447 pub number_type: Option<u8>,
448 pub class: Option<u8>,
449 pub alpha: Option<String>,
450 pub cli_validity: Option<u8>,
451 pub subaddr: Option<String>,
452 pub satype: Option<u8>,
453 pub priority: Option<u8>,
454}
455
456impl CallWaiting {
457 pub fn parse(line: &str) -> Option<Self> {
458 let a = body(line, "+CCWA")?;
459 if !a.first()?.quoted && a.len() < 3 {
461 return None;
462 }
463 Some(CallWaiting {
464 number: a[0].text.clone(),
465 number_type: int(&a, 1),
466 class: int(&a, 2),
467 alpha: text(&a, 3),
468 cli_validity: int(&a, 4),
469 subaddr: text(&a, 5),
470 satype: int(&a, 6),
471 priority: int(&a, 7),
472 })
473 }
474}
475
476#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
478#[serde(rename_all = "camelCase")]
479pub enum CallDirection {
480 MobileOriginated,
481 MobileTerminated,
482}
483
484#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
486#[serde(rename_all = "camelCase")]
487pub enum CallState {
488 Active,
489 Held,
490 Dialing,
491 Alerting,
492 Incoming,
493 Waiting,
494 Other(u8),
495}
496
497#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
499#[serde(rename_all = "camelCase")]
500pub struct Clcc {
501 pub id: u32,
502 pub dir: CallDirection,
503 pub stat: CallState,
504 pub mode: u8,
506 pub mpty: bool,
507 pub number: Option<String>,
508 #[serde(rename = "type")]
509 pub number_type: Option<u8>,
510 pub alpha: Option<String>,
511 pub priority: Option<u8>,
512 pub cli_validity: Option<u8>,
513}
514
515impl Clcc {
516 pub fn parse(line: &str) -> Option<Self> {
517 let a = body(line, "+CLCC")?;
518 let dir = match int::<u8>(&a, 1)? {
519 0 => CallDirection::MobileOriginated,
520 1 => CallDirection::MobileTerminated,
521 _ => return None,
522 };
523 let stat = match int::<u8>(&a, 2)? {
524 0 => CallState::Active,
525 1 => CallState::Held,
526 2 => CallState::Dialing,
527 3 => CallState::Alerting,
528 4 => CallState::Incoming,
529 5 => CallState::Waiting,
530 v => CallState::Other(v),
531 };
532 let number = a.get(5).map(|n| n.text.clone());
533 let number_type = int(&a, 6);
534 if number.is_some() && number_type.is_none() {
536 return None;
537 }
538 Some(Clcc {
539 id: int(&a, 0)?,
540 dir,
541 stat,
542 mode: int(&a, 3)?,
543 mpty: int::<u8>(&a, 4)? == 1,
544 number,
545 number_type,
546 alpha: text(&a, 7),
547 priority: int(&a, 8),
548 cli_validity: int(&a, 9),
549 })
550 }
551}
552
553#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
558#[serde(rename_all = "camelCase")]
559pub struct Ciev {
560 pub index: Option<u32>,
561 pub name: Option<String>,
562 pub value: i64,
563}
564
565impl Ciev {
566 pub fn parse(line: &str) -> Option<Self> {
567 let a = body(line, "+CIEV")?;
568 let ind = a.first()?;
569 let value = a.get(1)?.text.parse().ok()?;
570 let index = ind.int();
571 Some(Ciev {
572 index,
573 name: if index.is_none() {
574 ind.text().map(str::to_string)
575 } else {
576 None
577 },
578 value,
579 })
580 }
581}
582
583#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
585#[serde(rename_all = "camelCase")]
586pub struct CindValues {
587 pub values: Vec<i64>,
588}
589
590impl CindValues {
591 pub fn parse(line: &str) -> Option<Self> {
592 let a = body(line, "+CIND")?;
593 let values = a
594 .iter()
595 .map(|v| v.text.parse().ok())
596 .collect::<Option<Vec<_>>>()?;
597 (!values.is_empty()).then_some(CindValues { values })
598 }
599}
600
601#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
603#[serde(rename_all = "camelCase")]
604pub struct IndicatorRange {
605 pub index: u32,
606 pub name: String,
607 pub values: Vec<u32>,
608}
609
610#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
612#[serde(rename_all = "camelCase")]
613pub struct CindRanges {
614 pub indicators: Vec<IndicatorRange>,
615}
616
617impl CindRanges {
618 pub fn parse(line: &str) -> Option<Self> {
619 let a = body(line, "+CIND")?;
620 let mut indicators = Vec::new();
621 for (i, g) in a.iter().enumerate() {
622 let g = g.group()?;
623 if g.is_empty() {
624 continue; }
626 indicators.push(IndicatorRange {
627 index: i as u32 + 1,
628 name: g.first()?.str()?.to_string(),
629 values: parse_values(&g.get(1)?.text)?,
630 });
631 }
632 (!indicators.is_empty()).then_some(CindRanges { indicators })
633 }
634}
635
636#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
641#[serde(rename_all = "camelCase")]
642pub struct MessageIndication {
643 pub mem: String,
644 pub index: u32,
645}
646
647impl MessageIndication {
648 fn parse_with(line: &str, prefix: &str) -> Option<Self> {
649 let a = body(line, prefix)?;
650 Some(MessageIndication {
651 mem: a.first()?.text()?.to_string(),
652 index: int(&a, 1)?,
653 })
654 }
655 pub fn parse_cmti(line: &str) -> Option<Self> {
656 Self::parse_with(line, "+CMTI")
657 }
658 pub fn parse_cdsi(line: &str) -> Option<Self> {
659 Self::parse_with(line, "+CDSI")
660 }
661}
662
663#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
665#[serde(rename_all = "camelCase")]
666pub enum MessageStatus {
667 ReceivedUnread,
669 ReceivedRead,
671 StoredUnsent,
673 StoredSent,
675 All,
677}
678
679impl MessageStatus {
680 fn from_arg(a: &Arg) -> Option<Self> {
681 use MessageStatus::*;
682 if let Some(v) = a.int::<u8>() {
683 return [ReceivedUnread, ReceivedRead, StoredUnsent, StoredSent, All]
684 .get(v as usize)
685 .copied();
686 }
687 match a.text.to_ascii_uppercase().as_str() {
688 "REC UNREAD" => Some(ReceivedUnread),
689 "REC READ" => Some(ReceivedRead),
690 "STO UNSENT" => Some(StoredUnsent),
691 "STO SENT" => Some(StoredSent),
692 "ALL" => Some(All),
693 _ => None,
694 }
695 }
696}
697
698#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
700#[serde(rename_all = "camelCase")]
701pub enum SmsMode {
702 Pdu,
703 Text,
704}
705
706#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
713#[serde(rename_all = "camelCase")]
714pub struct MessageHeader {
715 pub mode: SmsMode,
716 pub index: Option<u32>,
718 pub stat: MessageStatus,
719 pub alpha: Option<String>,
720 pub length: Option<u32>,
722 pub address: Option<String>,
724 pub address_type: Option<u8>,
726 pub scts: Option<String>,
728}
729
730impl MessageHeader {
731 pub fn parse_cmgl(line: &str) -> Option<Self> {
732 let a = body(line, "+CMGL")?;
733 let index = int(&a, 0)?;
734 let mut h = Self::parse_rest(&a[1..], true)?;
735 h.index = Some(index);
736 Some(h)
737 }
738
739 pub fn parse_cmgr(line: &str) -> Option<Self> {
740 let a = body(line, "+CMGR")?;
741 Self::parse_rest(&a, false)
742 }
743
744 fn parse_rest(a: &[Arg], list: bool) -> Option<Self> {
745 let st = a.first()?;
746 let stat = MessageStatus::from_arg(st)?;
747 if !st.quoted {
748 return Some(MessageHeader {
750 mode: SmsMode::Pdu,
751 index: None,
752 stat,
753 alpha: text(a, 1),
754 length: Some(int(a, 2)?),
755 address: None,
756 address_type: None,
757 scts: None,
758 });
759 }
760 if a.get(1).is_some_and(|x| !x.quoted && !x.text.is_empty()) {
763 return None;
764 }
765 let submit = !list
769 && matches!(
770 stat,
771 MessageStatus::StoredUnsent | MessageStatus::StoredSent
772 );
773 let (scts, type_at, len_at) = match (list, submit) {
774 (true, _) => (text(a, 3), 4, 5),
775 (false, true) => (None, 3, 10),
776 (false, false) => (text(a, 3), 4, 10),
777 };
778 Some(MessageHeader {
779 mode: SmsMode::Text,
780 index: None,
781 stat,
782 address: text(a, 1),
783 alpha: text(a, 2),
784 scts,
785 address_type: int(a, type_at),
786 length: int(a, len_at),
787 })
788 }
789}
790
791#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
794#[serde(rename_all = "camelCase")]
795pub struct Cmt {
796 pub mode: SmsMode,
797 pub alpha: Option<String>,
798 pub length: Option<u32>,
799 pub address: Option<String>,
800 pub address_type: Option<u8>,
801 pub scts: Option<String>,
802}
803
804impl Cmt {
805 pub fn parse(line: &str) -> Option<Self> {
806 let a = body(line, "+CMT")?;
807 if a.len() == 2 && !a[1].quoted {
808 return Some(Cmt {
809 mode: SmsMode::Pdu,
810 alpha: text(&a, 0),
811 length: Some(int(&a, 1)?),
812 address: None,
813 address_type: None,
814 scts: None,
815 });
816 }
817 if a.len() < 3 {
818 return None;
819 }
820 Some(Cmt {
821 mode: SmsMode::Text,
822 address: text(&a, 0),
823 alpha: text(&a, 1),
824 scts: text(&a, 2),
825 address_type: int(&a, 3),
826 length: int(&a, 9),
827 })
828 }
829}
830
831#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
833#[serde(rename_all = "camelCase")]
834pub struct Cds {
835 pub mode: SmsMode,
836 pub length: Option<u32>,
837 pub fo: Option<u8>,
838 pub mr: Option<u8>,
839 pub ra: Option<String>,
840 pub tora: Option<u8>,
841 pub scts: Option<String>,
842 pub dt: Option<String>,
843 pub st: Option<u8>,
844}
845
846impl Cds {
847 pub fn parse(line: &str) -> Option<Self> {
848 let a = body(line, "+CDS")?;
849 if a.len() == 1 {
850 return Some(Cds {
851 mode: SmsMode::Pdu,
852 length: Some(int(&a, 0)?),
853 fo: None,
854 mr: None,
855 ra: None,
856 tora: None,
857 scts: None,
858 dt: None,
859 st: None,
860 });
861 }
862 Some(Cds {
864 mode: SmsMode::Text,
865 length: None,
866 fo: Some(int(&a, 0)?),
867 mr: Some(int(&a, 1)?),
868 ra: text(&a, 2),
869 tora: int(&a, 3),
870 scts: text(&a, 4),
871 dt: text(&a, 5),
872 st: Some(int(&a, 6)?),
873 })
874 }
875}
876
877#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
879#[serde(rename_all = "camelCase")]
880pub struct MessageReference {
881 pub mr: u8,
882 pub ack: Option<String>,
884}
885
886impl MessageReference {
887 fn parse_with(line: &str, prefix: &str) -> Option<Self> {
888 let a = body(line, prefix)?;
889 Some(MessageReference {
890 mr: int(&a, 0)?,
891 ack: text(&a, 1),
892 })
893 }
894 pub fn parse_cmgs(line: &str) -> Option<Self> {
895 Self::parse_with(line, "+CMGS")
896 }
897 pub fn parse_cmss(line: &str) -> Option<Self> {
898 Self::parse_with(line, "+CMSS")
899 }
900}
901
902#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
904#[serde(rename_all = "camelCase")]
905pub struct StorageUsage {
906 pub mem: Option<String>,
908 pub used: u32,
909 pub total: u32,
910}
911
912#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
914#[serde(rename_all = "camelCase")]
915pub struct CpmsStatus {
916 pub storages: Vec<StorageUsage>,
917}
918
919impl CpmsStatus {
920 pub fn parse(line: &str) -> Option<Self> {
921 let a = body(line, "+CPMS")?;
922 let with_mem = a.first()?.int::<u32>().is_none();
923 let step = if with_mem { 3 } else { 2 };
924 if a.is_empty() || a.len() % step != 0 {
925 return None;
926 }
927 let storages = a
928 .chunks(step)
929 .map(|c| {
930 let o = step - 2;
931 Some(StorageUsage {
932 mem: if with_mem {
933 Some(c[0].text()?.to_string())
934 } else {
935 None
936 },
937 used: c[o].int()?,
938 total: c[o + 1].int()?,
939 })
940 })
941 .collect::<Option<_>>()?;
942 Some(CpmsStatus { storages })
943 }
944}
945
946#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
948#[serde(rename_all = "camelCase")]
949pub struct CpmsSupport {
950 pub mem1: Vec<String>,
951 pub mem2: Vec<String>,
952 pub mem3: Vec<String>,
953}
954
955impl CpmsSupport {
956 pub fn parse(line: &str) -> Option<Self> {
957 let a = body(line, "+CPMS")?;
958 let list = |i: usize| -> Vec<String> {
959 let Some(g) = a.get(i) else {
960 return Vec::new();
961 };
962 let items = g.group().unwrap_or_else(|| vec![g.clone()]);
964 items
965 .iter()
966 .filter_map(|m| m.text().map(str::to_string))
967 .collect()
968 };
969 if !a.first()?.text.starts_with('(') {
970 return None;
971 }
972 Some(CpmsSupport {
973 mem1: list(0),
974 mem2: list(1),
975 mem3: list(2),
976 })
977 }
978}
979
980#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
982#[serde(rename_all = "camelCase")]
983pub struct Cnmi {
984 pub mode: u8,
985 pub mt: Option<u8>,
986 pub bm: Option<u8>,
987 pub ds: Option<u8>,
988 pub bfr: Option<u8>,
989}
990
991impl Cnmi {
992 pub fn parse(line: &str) -> Option<Self> {
993 let a = body(line, "+CNMI")?;
994 Some(Cnmi {
995 mode: int(&a, 0)?,
996 mt: int(&a, 1),
997 bm: int(&a, 2),
998 ds: int(&a, 3),
999 bfr: int(&a, 4),
1000 })
1001 }
1002}
1003
1004#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1006#[serde(rename_all = "camelCase")]
1007pub struct CnmiSupport {
1008 pub mode: Vec<u32>,
1009 pub mt: Vec<u32>,
1010 pub bm: Vec<u32>,
1011 pub ds: Vec<u32>,
1012 pub bfr: Vec<u32>,
1013}
1014
1015impl CnmiSupport {
1016 pub fn parse(line: &str) -> Option<Self> {
1017 let a = body(line, "+CNMI")?;
1018 a.first()?; let get = |i: usize| match a.get(i) {
1020 Some(g) if g.text.starts_with('(') => parse_values(&g.text),
1021 Some(_) => None,
1022 None => Some(Vec::new()),
1023 };
1024 Some(CnmiSupport {
1025 mode: get(0)?,
1026 mt: get(1)?,
1027 bm: get(2)?,
1028 ds: get(3)?,
1029 bfr: get(4)?,
1030 })
1031 }
1032}
1033
1034#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1039#[serde(rename_all = "camelCase")]
1040pub struct Cops {
1041 pub mode: u8,
1043 pub format: Option<u8>,
1045 pub oper: Option<String>,
1046 pub act: Option<AccessTech>,
1047}
1048
1049impl Cops {
1050 pub fn parse(line: &str) -> Option<Self> {
1051 let a = body(line, "+COPS")?;
1052 if a.first()?.text.starts_with('(') {
1053 return None; }
1055 Some(Cops {
1056 mode: int(&a, 0)?,
1057 format: int(&a, 1),
1058 oper: text(&a, 2),
1059 act: AccessTech::from_arg(&a, 3),
1060 })
1061 }
1062}
1063
1064#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
1066#[serde(rename_all = "camelCase")]
1067pub enum OperatorStatus {
1068 Unknown,
1069 Available,
1070 Current,
1071 Forbidden,
1072 Other(u8),
1073}
1074
1075#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1077#[serde(rename_all = "camelCase")]
1078pub struct OperatorEntry {
1079 pub stat: OperatorStatus,
1080 pub long: Option<String>,
1081 pub short: Option<String>,
1082 pub numeric: Option<String>,
1083 pub act: Option<AccessTech>,
1084}
1085
1086#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1089#[serde(rename_all = "camelCase")]
1090pub struct CopsList {
1091 pub operators: Vec<OperatorEntry>,
1092}
1093
1094impl CopsList {
1095 pub fn parse(line: &str) -> Option<Self> {
1096 let a = body(line, "+COPS")?;
1097 if !a.first()?.text.starts_with('(') {
1098 return None;
1099 }
1100 let mut operators: Vec<OperatorEntry> = Vec::new();
1101 for f in &a {
1102 let Some(g) = f.group() else {
1104 continue;
1105 };
1106 if g.len() < 4 || !g[1..4].iter().any(|x| x.quoted) {
1108 continue;
1109 }
1110 let stat = match g[0].int::<u8>() {
1111 Some(0) => OperatorStatus::Unknown,
1112 Some(1) => OperatorStatus::Available,
1113 Some(2) => OperatorStatus::Current,
1114 Some(3) => OperatorStatus::Forbidden,
1115 Some(v) => OperatorStatus::Other(v),
1116 None => continue,
1117 };
1118 let act = AccessTech::from_arg(&g, 4);
1119 operators.push(OperatorEntry {
1120 stat,
1121 long: text(&g, 1).filter(|s| !s.is_empty()),
1122 short: text(&g, 2).filter(|s| !s.is_empty()),
1123 numeric: text(&g, 3).filter(|s| !s.is_empty()),
1124 act,
1125 });
1126 }
1127 Some(CopsList { operators })
1128 }
1129}
1130
1131#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1137#[serde(
1138 tag = "event",
1139 rename_all = "camelCase",
1140 rename_all_fields = "camelCase"
1141)]
1142pub enum CgevEvent {
1143 Reject { pdp_type: String, pdp_addr: String },
1145 NwReact {
1147 pdp_type: String,
1148 pdp_addr: String,
1149 cid: Option<u32>,
1150 },
1151 NwDeactPdp {
1153 pdp_type: String,
1154 pdp_addr: String,
1155 cid: Option<u32>,
1156 },
1157 MeDeactPdp {
1159 pdp_type: String,
1160 pdp_addr: String,
1161 cid: Option<u32>,
1162 },
1163 NwPdnAct { cid: u32 },
1165 MePdnAct {
1167 cid: u32,
1168 reason: Option<u32>,
1169 cid_other: Option<u32>,
1170 },
1171 NwPdnDeact { cid: u32 },
1173 MePdnDeact { cid: u32 },
1175 NwAct {
1177 p_cid: u32,
1178 cid: u32,
1179 event_type: u32,
1180 },
1181 MeAct {
1183 p_cid: u32,
1184 cid: u32,
1185 event_type: u32,
1186 },
1187 NwDeact {
1189 p_cid: u32,
1190 cid: u32,
1191 event_type: u32,
1192 },
1193 MeDeact {
1195 p_cid: u32,
1196 cid: u32,
1197 event_type: u32,
1198 },
1199 NwModify {
1201 cid: u32,
1202 change_reason: u32,
1203 event_type: u32,
1204 },
1205 MeModify {
1207 cid: u32,
1208 change_reason: u32,
1209 event_type: u32,
1210 },
1211 NwDetach,
1213 MeDetach,
1215 NwClass { class: String },
1217 MeClass { class: String },
1219 Other,
1221}
1222
1223#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1225#[serde(rename_all = "camelCase")]
1226pub struct Cgev {
1227 pub event: CgevEvent,
1228 pub raw: String,
1229}
1230
1231impl Cgev {
1232 pub fn parse(line: &str) -> Option<Self> {
1233 Some(Self::parse_event(strip_prefix(line, "+CGEV")?))
1234 }
1235
1236 pub fn parse_event(text: &str) -> Self {
1238 let raw = text.trim().to_string();
1239 Cgev {
1240 event: Self::decode(&raw).unwrap_or(CgevEvent::Other),
1241 raw,
1242 }
1243 }
1244
1245 fn decode(raw: &str) -> Option<CgevEvent> {
1246 use CgevEvent::*;
1247 let upper = raw.to_ascii_uppercase();
1248 const KEYS: [&str; 10] = [
1250 "NW", "ME", "PDN", "ACT", "DEACT", "REACT", "MODIFY", "DETACH", "CLASS", "REJECT",
1251 ];
1252 let mut cut = 0;
1253 let mut words = Vec::new();
1254 for w in upper.split_whitespace() {
1255 if !KEYS.contains(&w) {
1256 break;
1257 }
1258 words.push(w);
1259 cut = upper[cut..].find(w).map(|p| cut + p + w.len())?;
1260 }
1261 let a = split(&raw[cut..]);
1262 let n = |i: usize| int::<u32>(&a, i);
1263 let s = |i: usize| text(&a, i);
1264 let addr = || Some((s(0)?, s(1)?));
1265 let addr_form = a
1267 .first()
1268 .is_some_and(|f| !f.is_empty() && f.int::<u32>().is_none());
1269 Some(match words.as_slice() {
1270 ["REJECT"] => {
1271 let (pdp_type, pdp_addr) = addr()?;
1272 Reject { pdp_type, pdp_addr }
1273 }
1274 ["NW", "REACT"] => {
1275 let (pdp_type, pdp_addr) = addr()?;
1276 NwReact {
1277 pdp_type,
1278 pdp_addr,
1279 cid: n(2),
1280 }
1281 }
1282 ["NW", "DEACT"] if addr_form => {
1283 let (pdp_type, pdp_addr) = addr()?;
1284 NwDeactPdp {
1285 pdp_type,
1286 pdp_addr,
1287 cid: n(2),
1288 }
1289 }
1290 ["ME", "DEACT"] if addr_form => {
1291 let (pdp_type, pdp_addr) = addr()?;
1292 MeDeactPdp {
1293 pdp_type,
1294 pdp_addr,
1295 cid: n(2),
1296 }
1297 }
1298 ["NW", "PDN", "ACT"] => NwPdnAct { cid: n(0)? },
1299 ["ME", "PDN", "ACT"] => MePdnAct {
1300 cid: n(0)?,
1301 reason: n(1),
1302 cid_other: n(2),
1303 },
1304 ["NW", "PDN", "DEACT"] => NwPdnDeact { cid: n(0)? },
1305 ["ME", "PDN", "DEACT"] => MePdnDeact { cid: n(0)? },
1306 ["NW", "ACT"] => NwAct {
1307 p_cid: n(0)?,
1308 cid: n(1)?,
1309 event_type: n(2)?,
1310 },
1311 ["ME", "ACT"] => MeAct {
1312 p_cid: n(0)?,
1313 cid: n(1)?,
1314 event_type: n(2)?,
1315 },
1316 ["NW", "DEACT"] => NwDeact {
1317 p_cid: n(0)?,
1318 cid: n(1)?,
1319 event_type: n(2)?,
1320 },
1321 ["ME", "DEACT"] => MeDeact {
1322 p_cid: n(0)?,
1323 cid: n(1)?,
1324 event_type: n(2)?,
1325 },
1326 ["NW", "MODIFY"] => NwModify {
1327 cid: n(0)?,
1328 change_reason: n(1)?,
1329 event_type: n(2)?,
1330 },
1331 ["ME", "MODIFY"] => MeModify {
1332 cid: n(0)?,
1333 change_reason: n(1)?,
1334 event_type: n(2)?,
1335 },
1336 ["NW", "DETACH"] => NwDetach,
1337 ["ME", "DETACH"] => MeDetach,
1338 ["NW", "CLASS"] => NwClass { class: s(0)? },
1339 ["ME", "CLASS"] => MeClass { class: s(0)? },
1340 _ => return None,
1341 })
1342 }
1343}
1344
1345#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1350#[serde(tag = "urc", content = "data", rename_all = "camelCase")]
1351pub enum Urc {
1352 Registration(Registration),
1354 Clip(CallerId),
1355 Colp(CallerId),
1356 Ccwa(CallWaiting),
1357 Ciev(Ciev),
1358 Cmti(MessageIndication),
1359 Cdsi(MessageIndication),
1360 Cmt(Cmt),
1362 Cds(Cds),
1364 Cgev(Cgev),
1365}
1366
1367pub fn parse_any(line: &str) -> Option<Urc> {
1369 let head = line.trim_start().get(..3)?.to_ascii_uppercase();
1370 match head.as_str() {
1371 "+CR" | "+CG" | "+CE" | "+C5" => Registration::parse(line)
1372 .map(Urc::Registration)
1373 .or_else(|| Cgev::parse(line).map(Urc::Cgev)),
1374 _ => CallerId::parse_clip(line)
1375 .map(Urc::Clip)
1376 .or_else(|| CallerId::parse_colp(line).map(Urc::Colp))
1377 .or_else(|| CallWaiting::parse(line).map(Urc::Ccwa))
1378 .or_else(|| Ciev::parse(line).map(Urc::Ciev))
1379 .or_else(|| MessageIndication::parse_cmti(line).map(Urc::Cmti))
1380 .or_else(|| MessageIndication::parse_cdsi(line).map(Urc::Cdsi))
1381 .or_else(|| Cmt::parse(line).map(Urc::Cmt))
1382 .or_else(|| Cds::parse(line).map(Urc::Cds)),
1383 }
1384}