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 let get = |i: usize| match a.get(i) {
1019 Some(g) if g.text.starts_with('(') => parse_values(&g.text),
1020 Some(_) => None,
1021 None => Some(Vec::new()),
1022 };
1023 Some(CnmiSupport {
1024 mode: get(0)?,
1025 mt: get(1)?,
1026 bm: get(2)?,
1027 ds: get(3)?,
1028 bfr: get(4)?,
1029 })
1030 }
1031}
1032
1033#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1038#[serde(rename_all = "camelCase")]
1039pub struct Cops {
1040 pub mode: u8,
1042 pub format: Option<u8>,
1044 pub oper: Option<String>,
1045 pub act: Option<AccessTech>,
1046}
1047
1048impl Cops {
1049 pub fn parse(line: &str) -> Option<Self> {
1050 let a = body(line, "+COPS")?;
1051 if a.first()?.text.starts_with('(') {
1052 return None; }
1054 Some(Cops {
1055 mode: int(&a, 0)?,
1056 format: int(&a, 1),
1057 oper: text(&a, 2),
1058 act: AccessTech::from_arg(&a, 3),
1059 })
1060 }
1061}
1062
1063#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
1065#[serde(rename_all = "camelCase")]
1066pub enum OperatorStatus {
1067 Unknown,
1068 Available,
1069 Current,
1070 Forbidden,
1071 Other(u8),
1072}
1073
1074#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1076#[serde(rename_all = "camelCase")]
1077pub struct OperatorEntry {
1078 pub stat: OperatorStatus,
1079 pub long: Option<String>,
1080 pub short: Option<String>,
1081 pub numeric: Option<String>,
1082 pub act: Option<AccessTech>,
1083}
1084
1085#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1088#[serde(rename_all = "camelCase")]
1089pub struct CopsList {
1090 pub operators: Vec<OperatorEntry>,
1091}
1092
1093impl CopsList {
1094 pub fn parse(line: &str) -> Option<Self> {
1095 let a = body(line, "+COPS")?;
1096 if !a.first()?.text.starts_with('(') {
1097 return None;
1098 }
1099 let mut operators: Vec<OperatorEntry> = Vec::new();
1100 for f in &a {
1101 let Some(g) = f.group() else {
1103 continue;
1104 };
1105 if g.len() < 4 || !g[1..4].iter().any(|x| x.quoted) {
1107 continue;
1108 }
1109 let stat = match g[0].int::<u8>() {
1110 Some(0) => OperatorStatus::Unknown,
1111 Some(1) => OperatorStatus::Available,
1112 Some(2) => OperatorStatus::Current,
1113 Some(3) => OperatorStatus::Forbidden,
1114 Some(v) => OperatorStatus::Other(v),
1115 None => continue,
1116 };
1117 let act = AccessTech::from_arg(&g, 4);
1118 operators.push(OperatorEntry {
1119 stat,
1120 long: text(&g, 1).filter(|s| !s.is_empty()),
1121 short: text(&g, 2).filter(|s| !s.is_empty()),
1122 numeric: text(&g, 3).filter(|s| !s.is_empty()),
1123 act,
1124 });
1125 }
1126 Some(CopsList { operators })
1127 }
1128}
1129
1130#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1136#[serde(
1137 tag = "event",
1138 rename_all = "camelCase",
1139 rename_all_fields = "camelCase"
1140)]
1141pub enum CgevEvent {
1142 Reject { pdp_type: String, pdp_addr: String },
1144 NwReact {
1146 pdp_type: String,
1147 pdp_addr: String,
1148 cid: Option<u32>,
1149 },
1150 NwDeactPdp {
1152 pdp_type: String,
1153 pdp_addr: String,
1154 cid: Option<u32>,
1155 },
1156 MeDeactPdp {
1158 pdp_type: String,
1159 pdp_addr: String,
1160 cid: Option<u32>,
1161 },
1162 NwPdnAct { cid: u32 },
1164 MePdnAct {
1166 cid: u32,
1167 reason: Option<u32>,
1168 cid_other: Option<u32>,
1169 },
1170 NwPdnDeact { cid: u32 },
1172 MePdnDeact { cid: u32 },
1174 NwAct {
1176 p_cid: u32,
1177 cid: u32,
1178 event_type: u32,
1179 },
1180 MeAct {
1182 p_cid: u32,
1183 cid: u32,
1184 event_type: u32,
1185 },
1186 NwDeact {
1188 p_cid: u32,
1189 cid: u32,
1190 event_type: u32,
1191 },
1192 MeDeact {
1194 p_cid: u32,
1195 cid: u32,
1196 event_type: u32,
1197 },
1198 NwModify {
1200 cid: u32,
1201 change_reason: u32,
1202 event_type: u32,
1203 },
1204 MeModify {
1206 cid: u32,
1207 change_reason: u32,
1208 event_type: u32,
1209 },
1210 NwDetach,
1212 MeDetach,
1214 NwClass { class: String },
1216 MeClass { class: String },
1218 Other,
1220}
1221
1222#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1224#[serde(rename_all = "camelCase")]
1225pub struct Cgev {
1226 pub event: CgevEvent,
1227 pub raw: String,
1228}
1229
1230impl Cgev {
1231 pub fn parse(line: &str) -> Option<Self> {
1232 Some(Self::parse_event(strip_prefix(line, "+CGEV")?))
1233 }
1234
1235 pub fn parse_event(text: &str) -> Self {
1237 let raw = text.trim().to_string();
1238 Cgev {
1239 event: Self::decode(&raw).unwrap_or(CgevEvent::Other),
1240 raw,
1241 }
1242 }
1243
1244 fn decode(raw: &str) -> Option<CgevEvent> {
1245 use CgevEvent::*;
1246 let upper = raw.to_ascii_uppercase();
1247 const KEYS: [&str; 10] = [
1249 "NW", "ME", "PDN", "ACT", "DEACT", "REACT", "MODIFY", "DETACH", "CLASS", "REJECT",
1250 ];
1251 let mut cut = 0;
1252 let mut words = Vec::new();
1253 for w in upper.split_whitespace() {
1254 if !KEYS.contains(&w) {
1255 break;
1256 }
1257 words.push(w);
1258 cut = upper[cut..].find(w).map(|p| cut + p + w.len())?;
1259 }
1260 let a = split(&raw[cut..]);
1261 let n = |i: usize| int::<u32>(&a, i);
1262 let s = |i: usize| text(&a, i);
1263 let addr = || Some((s(0)?, s(1)?));
1264 let addr_form = a
1266 .first()
1267 .is_some_and(|f| !f.is_empty() && f.int::<u32>().is_none());
1268 Some(match words.as_slice() {
1269 ["REJECT"] => {
1270 let (pdp_type, pdp_addr) = addr()?;
1271 Reject { pdp_type, pdp_addr }
1272 }
1273 ["NW", "REACT"] => {
1274 let (pdp_type, pdp_addr) = addr()?;
1275 NwReact {
1276 pdp_type,
1277 pdp_addr,
1278 cid: n(2),
1279 }
1280 }
1281 ["NW", "DEACT"] if addr_form => {
1282 let (pdp_type, pdp_addr) = addr()?;
1283 NwDeactPdp {
1284 pdp_type,
1285 pdp_addr,
1286 cid: n(2),
1287 }
1288 }
1289 ["ME", "DEACT"] if addr_form => {
1290 let (pdp_type, pdp_addr) = addr()?;
1291 MeDeactPdp {
1292 pdp_type,
1293 pdp_addr,
1294 cid: n(2),
1295 }
1296 }
1297 ["NW", "PDN", "ACT"] => NwPdnAct { cid: n(0)? },
1298 ["ME", "PDN", "ACT"] => MePdnAct {
1299 cid: n(0)?,
1300 reason: n(1),
1301 cid_other: n(2),
1302 },
1303 ["NW", "PDN", "DEACT"] => NwPdnDeact { cid: n(0)? },
1304 ["ME", "PDN", "DEACT"] => MePdnDeact { cid: n(0)? },
1305 ["NW", "ACT"] => NwAct {
1306 p_cid: n(0)?,
1307 cid: n(1)?,
1308 event_type: n(2)?,
1309 },
1310 ["ME", "ACT"] => MeAct {
1311 p_cid: n(0)?,
1312 cid: n(1)?,
1313 event_type: n(2)?,
1314 },
1315 ["NW", "DEACT"] => NwDeact {
1316 p_cid: n(0)?,
1317 cid: n(1)?,
1318 event_type: n(2)?,
1319 },
1320 ["ME", "DEACT"] => MeDeact {
1321 p_cid: n(0)?,
1322 cid: n(1)?,
1323 event_type: n(2)?,
1324 },
1325 ["NW", "MODIFY"] => NwModify {
1326 cid: n(0)?,
1327 change_reason: n(1)?,
1328 event_type: n(2)?,
1329 },
1330 ["ME", "MODIFY"] => MeModify {
1331 cid: n(0)?,
1332 change_reason: n(1)?,
1333 event_type: n(2)?,
1334 },
1335 ["NW", "DETACH"] => NwDetach,
1336 ["ME", "DETACH"] => MeDetach,
1337 ["NW", "CLASS"] => NwClass { class: s(0)? },
1338 ["ME", "CLASS"] => MeClass { class: s(0)? },
1339 _ => return None,
1340 })
1341 }
1342}
1343
1344#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1349#[serde(tag = "urc", content = "data", rename_all = "camelCase")]
1350pub enum Urc {
1351 Registration(Registration),
1353 Clip(CallerId),
1354 Colp(CallerId),
1355 Ccwa(CallWaiting),
1356 Ciev(Ciev),
1357 Cmti(MessageIndication),
1358 Cdsi(MessageIndication),
1359 Cmt(Cmt),
1361 Cds(Cds),
1363 Cgev(Cgev),
1364}
1365
1366pub fn parse_any(line: &str) -> Option<Urc> {
1368 let head = line.trim_start().get(..3)?.to_ascii_uppercase();
1369 match head.as_str() {
1370 "+CR" | "+CG" | "+CE" | "+C5" => Registration::parse(line)
1371 .map(Urc::Registration)
1372 .or_else(|| Cgev::parse(line).map(Urc::Cgev)),
1373 _ => CallerId::parse_clip(line)
1374 .map(Urc::Clip)
1375 .or_else(|| CallerId::parse_colp(line).map(Urc::Colp))
1376 .or_else(|| CallWaiting::parse(line).map(Urc::Ccwa))
1377 .or_else(|| Ciev::parse(line).map(Urc::Ciev))
1378 .or_else(|| MessageIndication::parse_cmti(line).map(Urc::Cmti))
1379 .or_else(|| MessageIndication::parse_cdsi(line).map(Urc::Cdsi))
1380 .or_else(|| Cmt::parse(line).map(Urc::Cmt))
1381 .or_else(|| Cds::parse(line).map(Urc::Cds)),
1382 }
1383}