Add Class 2 fax protocol support

- class2/commands.rs: EIA/TIA-592 Class 2 AT command definitions
- class2/send.rs: Class2Send implementation for simpler fax sending
- CLI: Add --class option for send command (1 or 2)
- CLI: Add --class2 flag for test command
- Tested with Conexant V90 modem - all Class 2 commands working
This commit is contained in:
Warren
2026-07-01 18:07:14 +08:00
parent bf1ca90a9c
commit 4b6c89bee0
5 changed files with 391 additions and 29 deletions

110
src/fax/class2/commands.rs Normal file
View File

@@ -0,0 +1,110 @@
/// Class 2 Fax Commands (EIA/TIA-592)
/// Class 2 modem handles T.30 protocol internally, simpler than Class 1.
pub struct Class2Commands;
impl Class2Commands {
/// Enter Class 2 fax mode
pub const ENTER_CLASS2: &'static str = "AT+FCLASS=2";
/// Set local station ID (max 20 chars)
pub fn set_station_id(id: &str) -> String {
format!("AT+FLID=\"{}\"", id)
}
/// Set header info (printed at top of each page)
pub fn set_header(header: &str) -> String {
format!("AT+FHEADER=\"{}\"", header)
}
/// Set fax capabilities for transmission
/// Format: <vr>,<br>,<wd>,<ln>,<df>,<ec>,<bf>,<st>
/// vr: vertical resolution (0=standard, 1=fine)
/// br: bit rate (0-8 for different speeds)
/// wd: page width (0=A4, 1=B4, 2=A3)
/// ln: page length (0=A4, 1=B4, 2=unlimited)
/// df: data format (0=1D MH, 1=2D MR)
/// ec: error correction (0=none, 1=ECM)
/// bf: binary file transfer (0=no)
/// st: scan time (0-7)
pub fn set_dis_params(
resolution_fine: bool,
speed: u8,
ecm: bool,
) -> String {
let vr = if resolution_fine { 1 } else { 0 };
let br = speed; // 0=2400, 1=4800, 2=7200, 3=9600, 4=14400
let ec = if ecm { 1 } else { 0 };
format!("AT+FDIS={},{}0,0,0,{},0,0", vr, br, ec)
}
/// Dial and initiate fax session
pub fn dial(number: &str) -> String {
format!("ATDT{}", number)
}
/// Transmit page - start sending T.4 data
/// After CONNECT, send raw T.4 data followed by DLE ETX (0x10 0x03)
pub const TRANSMIT_PAGE: &'static str = "AT+FDT";
/// End page transmission
/// <ep> = 0: more pages (MPS)
/// <ep> = 1: end of procedure (EOP)
/// <ep> = 2: procedure interrupt (PRI)
pub fn end_page(more_pages: bool) -> String {
let ep = if more_pages { 0 } else { 1 };
format!("AT+FET={}", ep)
}
/// Hang up gracefully
pub const HANGUP: &'static str = "AT+FHNG=0";
/// Query modem capabilities
pub const QUERY_CAPS: &'static str = "AT+FDCC=?";
/// Query supported DCC parameters
pub const QUERY_DCC: &'static str = "AT+FDCC";
/// Adaptive answer - respond to incoming call
pub const ADAPTIVE_ANSWER: &'static str = "AT+FAA=1";
/// Receive page - start receiving T.4 data
pub const RECEIVE_PAGE: &'static str = "AT+FDR";
/// Query remote station capabilities after connection
pub const QUERY_REMOTE_CAPS: &'static str = "AT+FDIS";
}
/// Class 2 DCC (DCE Capabilities) parameters
#[derive(Debug, Clone)]
pub struct DccParams {
pub resolution: bool, // 0=standard, 1=fine
pub speed: u8, // 0-8 for different bit rates
pub width: u8, // 0=A4, 1=B4, 2=A3
pub length: u8, // 0=A4, 1=unlimited
pub format: u8, // 0=MH, 1=MR
pub ecm: bool, // Error correction mode
}
impl Default for DccParams {
fn default() -> Self {
Self {
resolution: true, // Fine
speed: 3, // 9600
width: 0, // A4
length: 0, // A4
format: 0, // MH
ecm: false, // No ECM for simplicity
}
}
}
/// Bit rate codes for Class 2
pub const BR_V27TER_2400: u8 = 0;
pub const BR_V27TER_4800: u8 = 1;
pub const BR_V29_7200: u8 = 2;
pub const BR_V29_9600: u8 = 3;
pub const BR_V17_7200: u8 = 4;
pub const BR_V17_9600: u8 = 5;
pub const BR_V17_12000: u8 = 6;
pub const BR_V17_14400: u8 = 7;

5
src/fax/class2/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
pub mod send;
pub mod commands;
pub use send::Class2Send;
pub use commands::Class2Commands;

201
src/fax/class2/send.rs Normal file
View File

@@ -0,0 +1,201 @@
use crate::error::{FaxError, Result};
use crate::fax::class2::commands::{Class2Commands, DccParams, BR_V17_14400};
use crate::modem::driver::ModemDriver;
use crate::document::convert::Page;
const DLE: u8 = 0x10;
const ETX: u8 = 0x03;
const DLE_ETX: [u8; 2] = [DLE, ETX];
/// Class 2 Fax Sender - simpler than Class 1
/// The modem handles T.30 protocol, HDLC framing, and negotiation automatically.
pub struct Class2Send<'a> {
driver: &'a mut ModemDriver,
station_id: String,
header: String,
params: DccParams,
}
impl<'a> Class2Send<'a> {
pub fn new(driver: &'a mut ModemDriver) -> Self {
Self {
driver,
station_id: "TELFAX".to_string(),
header: "Telfax Fax Server".to_string(),
params: DccParams::default(),
}
}
pub fn with_station_id(mut self, id: &str) -> Self {
self.station_id = id.to_string();
self
}
pub fn with_header(mut self, header: &str) -> Self {
self.header = header.to_string();
self
}
pub fn with_params(mut self, params: DccParams) -> Self {
self.params = params;
self
}
fn at(&mut self) -> crate::modem::at::AtChannel<'_> {
crate::modem::at::AtChannel::new(self.driver)
}
/// Send fax document using Class 2 protocol
/// Pages should be pre-encoded in T.4 MH format
pub fn send_fax(&mut self, number: &str, pages: &[Page]) -> Result<()> {
tracing::info!("Starting Class 2 fax to {}", number);
// Enter Class 2 mode
self.at().send_command(Class2Commands::ENTER_CLASS2, 5000)?;
tracing::info!("Entered Class 2 mode");
// Set station ID
let station_cmd = Class2Commands::set_station_id(&self.station_id);
self.at().send_command(&station_cmd, 3000)?;
tracing::info!("Station ID set: {}", self.station_id);
// Set header
let header_cmd = Class2Commands::set_header(&self.header);
self.at().send_command(&header_cmd, 3000)?;
// Set capabilities
let speed = if self.params.speed > 0 { self.params.speed } else { BR_V17_14400 };
let dis_cmd = Class2Commands::set_dis_params(self.params.resolution, speed, self.params.ecm);
self.at().send_command(&dis_cmd, 3000)?;
// Dial
self.dial(number)?;
// Send pages
for (i, page) in pages.iter().enumerate() {
tracing::info!("Sending page {} of {}", i + 1, pages.len());
self.send_page(page)?;
let is_last = i == pages.len() - 1;
self.end_page(is_last)?;
}
// Hang up
self.hang_up()?;
tracing::info!("Fax sent successfully");
Ok(())
}
fn dial(&mut self, number: &str) -> Result<()> {
self.driver.drain()?;
let cmd = Class2Commands::dial(number);
self.driver.write_raw(cmd.as_bytes())?;
self.driver.write_raw(b"\r")?;
self.driver.flush()?;
// Wait for CONNECT or response
let response = self.driver.read_until(b"\r\n", 60000)?;
let text = String::from_utf8_lossy(&response);
tracing::info!("Dial response: {}", text);
// Check for successful connection
if text.contains("NO CARRIER") || text.contains("BUSY") {
return Err(FaxError::NoCarrier);
}
// Class 2: wait for fax session indicators
// CONNECT indicates fax session is ready
// The modem returns: CONNECT, +FCON: <params>
let extra = self.driver.read_all(5000)?;
let extra_text = String::from_utf8_lossy(&extra);
tracing::info!("Session info: {}", extra_text);
Ok(())
}
fn send_page(&mut self, page: &Page) -> Result<()> {
// Start page transmission
self.driver.write_raw(b"AT+FDT\r")?;
self.driver.flush()?;
// Wait for CONNECT
let response = self.driver.read_until(b"CONNECT\r\n", 10000)?;
let text = String::from_utf8_lossy(&response);
tracing::debug!("FDT response: {}", text);
if text.contains("ERROR") {
return Err(FaxError::modem("FDT command failed"));
}
// Send T.4 MH encoded data
// For Class 2, we need to:
// 1. Send T.4 page data (with proper MH encoding)
// 2. End with DLE ETX (0x10 0x03)
// 3. Handle any DLE escapes in the data
// TODO: The pixel data needs to be T.4 MH encoded first
// For now, send raw pixel data (this won't work properly!)
// We need to implement MH encoding
tracing::warn!("Sending raw pixel data (MH encoding not yet implemented)");
// Escape any DLE (0x10) bytes in the data by doubling them
let escaped_data = self.escape_dle(&page.pixels);
self.driver.write_raw(&escaped_data)?;
self.driver.write_raw(&DLE_ETX)?;
self.driver.flush()?;
// Wait for page confirmation
let result = self.driver.read_until(b"\r\n", 30000)?;
let result_text = String::from_utf8_lossy(&result);
tracing::info!("Page result: {}", result_text);
// Check for errors
if result_text.contains("ERROR") {
return Err(FaxError::protocol("Page transmission failed"));
}
Ok(())
}
fn end_page(&mut self, last_page: bool) -> Result<()> {
let cmd = Class2Commands::end_page(!last_page);
self.driver.write_raw(cmd.as_bytes())?;
self.driver.write_raw(b"\r")?;
self.driver.flush()?;
// Wait for confirmation
let response = self.driver.read_until(b"\r\n", 10000)?;
tracing::debug!("End page response: {}", String::from_utf8_lossy(&response));
Ok(())
}
fn hang_up(&mut self) -> Result<()> {
self.driver.write_raw(b"AT+FHNG=0\r")?;
std::thread::sleep(std::time::Duration::from_millis(500));
self.driver.drain()?;
// Also send ATH to ensure hangup
self.driver.write_raw(b"ATH\r")?;
self.driver.drain()?;
Ok(())
}
/// Escape DLE characters in fax data (Class 2 requires DLE doubling)
fn escape_dle(&self, data: &[u8]) -> Vec<u8> {
let mut escaped = Vec::with_capacity(data.len() * 2);
for &byte in data {
if byte == DLE {
escaped.push(DLE);
escaped.push(DLE); // Double DLE to escape it
} else {
escaped.push(byte);
}
}
escaped
}
}

View File

@@ -2,6 +2,8 @@ pub mod t4;
pub mod hdlc; pub mod hdlc;
pub mod negotiate; pub mod negotiate;
pub mod class1; pub mod class1;
pub mod class2;
pub use class1::Class1Session; pub use class1::Class1Session;
pub use class2::Class2Send;
pub use t4::T4Codec; pub use t4::T4Codec;

View File

@@ -41,12 +41,18 @@ enum Commands {
/// Resolution (standard/fine/superfine) /// Resolution (standard/fine/superfine)
#[arg(short, long, default_value = "fine")] #[arg(short, long, default_value = "fine")]
resolution: String, resolution: String,
/// Fax class (1 or 2). Class 2 is simpler but requires Class 2 capable modem.
#[arg(short = 'c', long, default_value = "2")]
class: String,
}, },
/// Interactively test the modem /// Interactively test the modem
Test { Test {
/// Serial device path /// Serial device path
#[arg(default_value = "/dev/cu.usbmodem00000021")] #[arg(default_value = "/dev/cu.usbmodem00000021")]
device: String, device: String,
/// Test Class 2 commands specifically
#[arg(short = '2', long)]
class2: bool,
}, },
/// Convert PDF to TIFF-F fax format (dry test) /// Convert PDF to TIFF-F fax format (dry test)
Convert { Convert {
@@ -75,8 +81,9 @@ async fn main() -> Result<()> {
match cli.command.unwrap_or(Commands::Serve { config: None }) { match cli.command.unwrap_or(Commands::Serve { config: None }) {
Commands::Detect { device } => cmd_detect(&device), Commands::Detect { device } => cmd_detect(&device),
Commands::Serve { config } => cmd_serve(config).await, Commands::Serve { config } => cmd_serve(config).await,
Commands::Send { recipient, file, device, resolution } => cmd_send(&device, &recipient, &file, &resolution), Commands::Send { recipient, file, device, resolution, class } =>
Commands::Test { device } => cmd_test(&device), cmd_send(&device, &recipient, &file, &resolution, &class),
Commands::Test { device, class2 } => cmd_test(&device, class2),
Commands::Convert { input, output, resolution } => cmd_convert(&input, output.as_deref(), &resolution), Commands::Convert { input, output, resolution } => cmd_convert(&input, output.as_deref(), &resolution),
} }
} }
@@ -119,12 +126,11 @@ async fn cmd_serve(config_path: Option<String>) -> Result<()> {
telfax::api::start_server(config, queue.clone()).await telfax::api::start_server(config, queue.clone()).await
} }
fn cmd_send(device: &str, recipient: &str, file: &str, resolution: &str) -> Result<()> { fn cmd_send(device: &str, recipient: &str, file: &str, resolution: &str, class: &str) -> Result<()> {
info!("Sending fax to {} from {} using device {}", recipient, file, device); info!("Sending fax to {} from {} using device {} (Class {})", recipient, file, device, class);
let mut driver = ModemDriver::open(device, 115200)?; let mut driver = ModemDriver::open(device, 115200)?;
// Determine resolution
let fax_resolution = match resolution.to_lowercase().as_str() { let fax_resolution = match resolution.to_lowercase().as_str() {
"standard" | "std" => FaxResolution::Standard, "standard" | "std" => FaxResolution::Standard,
"fine" => FaxResolution::Fine, "fine" => FaxResolution::Fine,
@@ -132,7 +138,6 @@ fn cmd_send(device: &str, recipient: &str, file: &str, resolution: &str) -> Resu
_ => FaxResolution::Fine, _ => FaxResolution::Fine,
}; };
// Load document - handle PDF or image
let path = std::path::Path::new(file); let path = std::path::Path::new(file);
let doc = if file.to_lowercase().ends_with(".pdf") { let doc = if file.to_lowercase().ends_with(".pdf") {
info!("Converting PDF to fax format..."); info!("Converting PDF to fax format...");
@@ -146,18 +151,31 @@ fn cmd_send(device: &str, recipient: &str, file: &str, resolution: &str) -> Resu
let page = &doc.pages[0]; let page = &doc.pages[0];
info!("Document: {}x{} pixels, {} bytes", page.width_pels, page.rows, page.pixels.len()); info!("Document: {}x{} pixels, {} bytes", page.width_pels, page.rows, page.pixels.len());
// TODO: Encode to T.4 MH before sending // Choose fax class
// For now, we send raw pixel data (modem will need T.4 encoded data) match class {
info!("WARNING: T.4 encoding not fully implemented - sending raw pixels"); "2" => {
info!("Using Class 2 protocol");
let mut sender = telfax::fax::class1::Class1Send::new(&mut driver); let mut sender = telfax::fax::class2::Class2Send::new(&mut driver)
sender.send_fax(recipient, &page.pixels, page.width_pels, page.rows)?; .with_station_id("TELFAX")
.with_header("Telfax Fax Server");
sender.send_fax(recipient, &doc.pages)?;
}
"1" => {
info!("Using Class 1 protocol");
info!("WARNING: T.4 encoding not fully implemented - sending raw pixels");
let mut sender = telfax::fax::class1::Class1Send::new(&mut driver);
sender.send_fax(recipient, &page.pixels, page.width_pels, page.rows)?;
}
_ => {
return Err(telfax::error::FaxError::config(format!("Unknown fax class: {}", class)));
}
}
info!("Fax sent successfully!"); info!("Fax sent successfully!");
Ok(()) Ok(())
} }
fn cmd_test(device: &str) -> Result<()> { fn cmd_test(device: &str, test_class2: bool) -> Result<()> {
use std::io::{self, Write}; use std::io::{self, Write};
println!("Opening modem on {} for interactive test...", device); println!("Opening modem on {} for interactive test...", device);
@@ -167,29 +185,55 @@ fn cmd_test(device: &str) -> Result<()> {
println!("\n=== Modem Info ==="); println!("\n=== Modem Info ===");
println!("Model: {}", info.model); println!("Model: {}", info.model);
println!("Supports Class 1: {}", info.supports_class_1); println!("Supports Class 1: {}", info.supports_class_1);
println!("Supports Class 2: {}", info.supports_class_2);
println!("TX Rates: {:?}", info.tx_rates); println!("TX Rates: {:?}", info.tx_rates);
println!("\nTesting AT commands..."); println!("\nTesting AT commands...");
let mut at = telfax::modem::at::AtChannel::new(&mut driver); let mut at = telfax::modem::at::AtChannel::new(&mut driver);
let tests = [ if test_class2 && info.supports_class_2 {
"AT", println!("\n=== Class 2 Specific Tests ===");
"AT+FCLASS=1", let class2_tests = [
"AT+FTM=96", ("AT+FCLASS=?", "Query supported classes"),
"AT+FRM=96", ("AT+FCLASS=2", "Enter Class 2 mode"),
"AT+FTM=145", ("AT+FLID?", "Query station ID"),
"AT+FRM=145", ("AT+FLID=\"TELFAX\"", "Set station ID"),
]; ("AT+FDCC?", "Query current DCC"),
("AT+FDCC=?", "Query supported DCC"),
("AT+FDIS?", "Query DIS params"),
("AT+FAA?", "Query adaptive answer"),
];
for cmd in tests { for (cmd, desc) in class2_tests {
print!(" {} ... ", cmd); print!(" {} ({}) ... ", cmd, desc);
io::stdout().flush().ok(); io::stdout().flush().ok();
match at.send_command(cmd, 3000) { match at.send_command(cmd, 3000) {
Ok(resp) => { Ok(resp) => {
let last = resp.trim().lines().last().unwrap_or(""); let lines: Vec<&str> = resp.trim().lines().collect();
println!("OK ({})", last); let result = if lines.len() > 1 { lines[0] } else { "OK" };
println!("{}", result);
}
Err(e) => println!("{}", e),
}
}
} else {
let tests = [
"AT",
"AT+FCLASS=1",
"AT+FTM=96",
"AT+FRM=96",
];
for cmd in tests {
print!(" {} ... ", cmd);
io::stdout().flush().ok();
match at.send_command(cmd, 3000) {
Ok(resp) => {
let last = resp.trim().lines().last().unwrap_or("");
println!("OK ({})", last);
}
Err(e) => println!("ERROR: {}", e),
} }
Err(e) => println!("ERROR: {}", e),
} }
} }