rustrtc 0.3.66

A high-performance real-time communication library — WebRTC, RTP/SRTP, T.38 Fax, and RTP Latching
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use crate::errors::RtcResult;
use std::collections::VecDeque;

/// T.30 fax phases.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum T30Phase {
    Idle,
    CallingToneSent,
    CalledToneReceived,
    Premessage,
    Training,
    ReadyToTransmit,
    TransmittingPage,
    PostPage,
    ReadyToReceive,
    ReceivingPage,
    Disconnecting,
}

/// T.30 fax resolution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum T30Resolution {
    Standard,  // 1728 x 215 dpi (approx)
    Fine,      // 1728 x 215 dpi × 2
    SuperFine, // 1728 x 391 dpi × 2
}

/// T.30 HDLC frame type identifiers (T.30 Annex A).
///
/// Note: Some frames share the same byte value in the protocol,
/// distinguished by direction. We assign unique enum discriminants
/// by extending the byte value with a direction offset (bytes 0x01-0x1F
/// for regular frames, 0x40+ for control frames).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HdlcFrameType {
    /// Digital Identification Signal (called station capabilities)
    Dis = 0x01,
    /// Digital Command Signal (transmitter capabilities)
    Dcs = 0x02,
    /// Called Subscriber Identification
    Csi = 0x03,
    /// Non-standard Facilities
    Nsf = 0x04,
    /// Confirmation to Receive (ready for page)
    Cfr = 0x05,
    /// Training Check Field identifier
    Tcf = 0x06,
    /// Transmitter Subscriber Identification
    Tsi = 0x07,
    /// Subaddress
    Sub = 0x08,
    /// Selective Polling
    Sep = 0x09,
    /// Password
    Pwd = 0x0A,
    /// Frame Number Information (ECM mode)
    Ftt = 0x0B,
    /// End of Procedure
    Eop = 0x10,
    /// Message Confirmation
    Mcf = 0x11,
    /// Message Polling
    Mps = 0x12,
    /// Procedure Interrupt / End of Message
    Eom = 0x13,
    /// Partial Page Signal
    Pps = 0x14,
    /// Partial Page Request
    Ppr = 0x15,
    /// End of Retransmission
    Eor = 0x16,
    /// End of Retransmission Message
    EorMsg = 0x17,
    /// Training Re-check
    Trc = 0x18,
    /// Disconnect
    Dcn = 0x19,
    /// Pause
    Pause = 0x1A,
    /// Continue
    Continue = 0x1B,
    /// Received line count (RTN)
    Rtn = 0x1C,
}

/// Configuration for a T.30 fax session.
#[derive(Debug, Clone)]
pub struct T30FaxConfig {
    pub max_bitrate: u32,
    pub resolutions: Vec<T30Resolution>,
    pub ecm_supported: bool,
    pub local_id: String,
}

impl Default for T30FaxConfig {
    fn default() -> Self {
        Self {
            max_bitrate: 14400,
            resolutions: vec![T30Resolution::Standard, T30Resolution::Fine],
            ecm_supported: true,
            local_id: String::new(),
        }
    }
}

/// T.30 fax session state machine.
#[derive(Debug, Clone)]
pub struct T30Session {
    pub phase: T30Phase,
    pub local_config: T30FaxConfig,
    pub remote_config: Option<T30FaxConfig>,
    pub page_number: u32,
    pub page_data: Vec<u8>,
    /// V.21 HDLC frame buffer (for reassembly)
    pub hdlc_buffer: Vec<u8>,
    /// Event log for debugging/testing
    pub events: VecDeque<T30Event>,
}

/// Events that can occur during T.30 session.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum T30Event {
    PhaseChange(T30Phase, T30Phase),
    RemoteIdentification { id: String },
    LocalIdentification { id: String },
    PageTransferred { page: u32, size: usize },
    PageReceived { page: u32, size: usize },
    Disconnected,
    Error(String),
}

impl T30Session {
    /// Create a new T.30 session.
    pub fn new(local_config: T30FaxConfig) -> Self {
        Self {
            phase: T30Phase::Idle,
            local_config,
            remote_config: None,
            page_number: 0,
            page_data: Vec::new(),
            hdlc_buffer: Vec::new(),
            events: VecDeque::new(),
        }
    }

    /// Start a fax transmission as the calling station.
    pub fn start_calling(&mut self) {
        self.change_phase(T30Phase::CallingToneSent);
    }

    /// Answer a fax call as the called station.
    pub fn start_called(&mut self) {
        self.change_phase(T30Phase::CalledToneReceived);
    }

    /// Process a received DIS (Digital Identification Signal) frame data.
    /// The data should contain the HDLC frame payload without flags/FCS.
    pub fn receive_dis(&mut self, data: &[u8]) -> RtcResult<()> {
        if self.phase == T30Phase::CalledToneReceived || self.phase == T30Phase::Premessage {
            // Parse DIS data to extract remote capabilities
            if data.len() >= 2 {
                let _dis_bits = (data[0] as u16) | ((data[1] as u16) << 8);
                // Bit 7 indicates ECM support
                let ecm_supported = (data[0] & 0x80) != 0;
                // Bits 8-9 indicate resolution
                let _fine = (data[1] & 0x01) != 0;
                let _super_fine = (data[1] & 0x02) != 0;
                // Bits 10-12 indicate max bitrate
                let _max_rate_bits = (data[1] >> 3) & 0x07;

                self.remote_config = Some(T30FaxConfig {
                    max_bitrate: Self::dis_bitrate(data[1]),
                    resolutions: vec![T30Resolution::Standard],
                    ecm_supported,
                    local_id: String::new(),
                });

                self.change_phase(T30Phase::Premessage);
            }
        }
        Ok(())
    }

    /// Send DCS (Digital Command Signal) to begin transmission.
    /// Returns the DCS frame data (without flags/FCS).
    pub fn send_dcs(&mut self) -> Vec<u8> {
        let mut dcs = vec![0x00, 0x00];
        // Set ECM if supported by both sides
        let remote_supports_ecm = self
            .remote_config
            .as_ref()
            .map(|r| r.ecm_supported)
            .unwrap_or(false);
        if self.local_config.ecm_supported && remote_supports_ecm {
            dcs[0] |= 0x80; // Bit 7 = ECM
        }
        dcs
    }

    /// Confirm receipt (CFR), transitioning to TransmittingPage.
    pub fn confirm_receipt(&mut self) {
        if self.phase == T30Phase::Training {
            self.change_phase(T30Phase::ReadyToTransmit);
            let page_size = self.page_data.len();
            self.page_number += 1;
            self.events.push_back(T30Event::PageTransferred {
                page: self.page_number,
                size: page_size,
            });
        }
    }

    /// Signal end of page (EOP).
    pub fn end_of_page(&mut self) {
        if self.phase == T30Phase::TransmittingPage {
            self.change_phase(T30Phase::PostPage);
        }
    }

    /// Receive MCF (Message Confirmation).
    pub fn receive_mcf(&mut self) {
        if self.phase == T30Phase::PostPage {
            // In a multi-page scenario would go back to Premessage
            self.change_phase(T30Phase::Disconnecting);
        }
    }

    /// Send DCN (Disconnect).
    pub fn send_dcn(&mut self) {
        self.change_phase(T30Phase::Disconnecting);
        self.events.push_back(T30Event::Disconnected);
    }

    /// Receive page data (T.4 image data).
    pub fn receive_page_data(&mut self, data: &[u8]) {
        self.page_data.extend_from_slice(data);
    }

    /// Complete page reception.
    pub fn complete_page_reception(&mut self) {
        let size = self.page_data.len();
        self.page_number += 1;
        self.events.push_back(T30Event::PageReceived {
            page: self.page_number,
            size,
        });
        self.page_data.clear();
    }

    /// Reset the session to idle.
    pub fn reset(&mut self) {
        self.phase = T30Phase::Idle;
        self.remote_config = None;
        self.page_number = 0;
        self.page_data.clear();
        self.hdlc_buffer.clear();
    }

    pub fn change_phase(&mut self, new_phase: T30Phase) {
        if self.phase != new_phase {
            let old = self.phase;
            self.phase = new_phase;
            self.events.push_back(T30Event::PhaseChange(old, new_phase));
        }
    }

    fn dis_bitrate(data_byte: u8) -> u32 {
        match (data_byte >> 3) & 0x07 {
            0 => 2400,
            1 => 4800,
            2 => 9600,
            3 => 12000,
            4 => 14400,
            _ => 2400,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_t30_session_initial_state() {
        let session = T30Session::new(T30FaxConfig::default());
        assert_eq!(session.phase, T30Phase::Idle);
        assert_eq!(session.page_number, 0);
        assert!(session.page_data.is_empty());
    }

    #[test]
    fn test_t30_start_calling() {
        let mut session = T30Session::new(T30FaxConfig::default());
        session.start_calling();
        assert_eq!(session.phase, T30Phase::CallingToneSent);
    }

    #[test]
    fn test_t30_start_called() {
        let mut session = T30Session::new(T30FaxConfig::default());
        session.start_called();
        assert_eq!(session.phase, T30Phase::CalledToneReceived);
    }

    #[test]
    fn test_t30_receive_dis() {
        let config = T30FaxConfig {
            ecm_supported: true,
            ..T30FaxConfig::default()
        };
        let mut session = T30Session::new(config);
        session.start_called();

        // DIS frame with ECM bit set and 14400 max rate
        // BIT 7 (0x80) = ECM, BIT 8 = Fine
        // Bits 10-12 = 4 for 14400
        let dis_data = vec![0x80, 0x20 | 0x01]; // ECM + Fine, rate=4 (14400)

        session.receive_dis(&dis_data).unwrap();
        assert_eq!(session.phase, T30Phase::Premessage);
        assert!(session.remote_config.is_some());

        let remote = session.remote_config.as_ref().unwrap();
        assert!(remote.ecm_supported);
    }

    #[test]
    fn test_t30_send_dcs() {
        let mut session = T30Session::new(T30FaxConfig {
            ecm_supported: true,
            ..T30FaxConfig::default()
        });
        session.start_called();

        // Remote also supports ECM
        session.remote_config = Some(T30FaxConfig {
            ecm_supported: true,
            ..T30FaxConfig::default()
        });

        let dcs = session.send_dcs();
        assert_eq!(dcs.len(), 2);
        assert!(dcs[0] & 0x80 != 0); // ECM bit should be set
    }

    #[test]
    fn test_t30_confirm_receipt() {
        let mut session = T30Session::new(T30FaxConfig::default());
        session.page_data = vec![0x00; 100];
        session.change_phase(T30Phase::Training);

        session.confirm_receipt();
        assert_eq!(session.phase, T30Phase::ReadyToTransmit);
        assert_eq!(session.page_number, 1);

        let event = session.events.back().unwrap();
        match event {
            T30Event::PageTransferred { page, size } => {
                assert_eq!(*page, 1);
                assert_eq!(*size, 100);
            }
            _ => panic!("expected PageTransferred event"),
        }
    }

    #[test]
    fn test_t30_end_of_page_and_mcf() {
        let mut session = T30Session::new(T30FaxConfig::default());
        session.change_phase(T30Phase::TransmittingPage);

        session.end_of_page();
        assert_eq!(session.phase, T30Phase::PostPage);

        session.receive_mcf();
        assert_eq!(session.phase, T30Phase::Disconnecting);
    }

    #[test]
    fn test_t30_send_dcn() {
        let mut session = T30Session::new(T30FaxConfig::default());
        session.send_dcn();
        assert_eq!(session.phase, T30Phase::Disconnecting);

        let event = session.events.back().unwrap();
        assert_eq!(*event, T30Event::Disconnected);
    }

    #[test]
    fn test_t30_receive_page_data() {
        let mut session = T30Session::new(T30FaxConfig::default());
        assert!(session.page_data.is_empty());

        session.receive_page_data(&[0x00, 0x01, 0x02]);
        assert_eq!(session.page_data.len(), 3);

        session.complete_page_reception();
        assert_eq!(session.page_number, 1);
        assert!(session.page_data.is_empty());

        let event = session.events.back().unwrap();
        match event {
            T30Event::PageReceived { page, size } => {
                assert_eq!(*page, 1);
                assert_eq!(*size, 3);
            }
            _ => panic!("expected PageReceived event"),
        }
    }

    #[test]
    fn test_t30_full_session_flow() {
        let config = T30FaxConfig {
            ecm_supported: true,
            local_id: "TEST001".to_string(),
            ..T30FaxConfig::default()
        };
        let mut session = T30Session::new(config);

        // Calling side flow
        session.start_calling();
        assert_eq!(session.phase, T30Phase::CallingToneSent);

        // Wait for CED from called side - simulate phase change
        // In real impl, CNG/CED detection would trigger this
        // For now, move to premessage via DIS reception
        let mut called_session = T30Session::new(T30FaxConfig::default());
        called_session.start_called();

        // Called side sends DIS
        let dis_data = vec![0x00, 0x00]; // no ECM, standard resolution
        called_session.receive_dis(&dis_data).unwrap();
        assert_eq!(called_session.phase, T30Phase::Premessage);

        // Send DCS from called side
        let _dcs = called_session.send_dcs();
        called_session.change_phase(T30Phase::Training);

        // Confirm receipt
        called_session.confirm_receipt();
        assert_eq!(called_session.phase, T30Phase::ReadyToTransmit);

        // Transmit page
        called_session.change_phase(T30Phase::TransmittingPage);

        // End page
        called_session.end_of_page();
        assert_eq!(called_session.phase, T30Phase::PostPage);

        // Receive MCF
        called_session.receive_mcf();
        assert_eq!(called_session.phase, T30Phase::Disconnecting);

        // Disconnect
        called_session.send_dcn();
        assert_eq!(called_session.phase, T30Phase::Disconnecting);
    }

    #[test]
    fn test_t30_reset() {
        let mut session = T30Session::new(T30FaxConfig::default());
        session.start_calling();
        session.page_number = 5;
        session.page_data = vec![0x00; 100];
        session.remote_config = Some(T30FaxConfig::default());

        session.reset();
        assert_eq!(session.phase, T30Phase::Idle);
        assert_eq!(session.page_number, 0);
        assert!(session.page_data.is_empty());
        assert!(session.remote_config.is_none());
    }

    #[test]
    fn test_t30_events_logged_on_phase_transitions() {
        let mut session = T30Session::new(T30FaxConfig::default());

        // Phase transitions should generate events
        session.start_calling();
        session.start_called();

        let events: Vec<_> = session.events.iter().collect();
        assert_eq!(events.len(), 2);

        match events[0] {
            T30Event::PhaseChange(T30Phase::Idle, T30Phase::CallingToneSent) => {}
            _ => panic!("unexpected event"),
        }

        match events[1] {
            T30Event::PhaseChange(T30Phase::CallingToneSent, T30Phase::CalledToneReceived) => {}
            _ => panic!("unexpected event"),
        }
    }
}