From 898450b893c91ba2e5ff6278a2eb354619925e60 Mon Sep 17 00:00:00 2001 From: Sean Young Date: Thu, 23 May 2024 11:42:30 +0100 Subject: [PATCH] Teach cir decode to decode using all linux kernel protocols Signed-off-by: Sean Young --- cir/src/bin/cir.rs | 8 ++++++-- cir/src/bin/commands/decode.rs | 27 ++++++++++++++++++++++++--- cir/src/keymap/mod.rs | 1 + cir/src/keymap/protocol.rs | 2 +- 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/cir/src/bin/cir.rs b/cir/src/bin/cir.rs index deed769..9a8d418 100644 --- a/cir/src/bin/cir.rs +++ b/cir/src/bin/cir.rs @@ -89,14 +89,18 @@ struct Decode { )] rawir: Vec, - /// IRP Notation + /// Decode using IRP Notation #[arg(long = "irp", short = 'i')] irp: Vec, - /// Keymap or lircd.conf file + /// Decode using Keymap or lircd.conf file #[arg(long = "keymap", short = 'k')] keymap: Vec, + /// Decode using all Linux Kernel Protocols + #[arg(long = "builtin-kernel-protocols", short = 'b')] + linux_kernel: bool, + #[clap(flatten)] options: DecodeOptions, } diff --git a/cir/src/bin/commands/decode.rs b/cir/src/bin/commands/decode.rs index ef26677..3f48a1c 100644 --- a/cir/src/bin/commands/decode.rs +++ b/cir/src/bin/commands/decode.rs @@ -3,7 +3,10 @@ use super::keymap::{find_devices, Purpose}; use crate::get_irp_protocols; #[cfg(target_os = "linux")] use cir::lirc::Lirc; -use cir::{keymap::Keymap, lircd_conf::parse}; +use cir::{ + keymap::{Keymap, LINUX_PROTOCOLS}, + lircd_conf::parse, +}; use irp::{Decoder, InfraredData, Irp, Message, Options}; use itertools::Itertools; use log::{error, info}; @@ -20,7 +23,7 @@ pub fn decode(global: &crate::App, decode: &crate::Decode) { let mut lircd_remotes = Vec::new(); let mut rc_keymaps = Vec::new(); - let mut irps = Vec::new(); + let mut irps: Vec<(&str, &str, Irp)> = Vec::new(); for irp_arg in &decode.irp { match get_irp_protocols(&global.irp_protocols) { @@ -70,7 +73,25 @@ pub fn decode(global: &crate::App, decode: &crate::Decode) { } } - if decode.irp.is_empty() && decode.keymap.is_empty() { + if decode.linux_kernel { + for protocol in LINUX_PROTOCOLS { + if let Some(irp_notation) = protocol.irp { + log::debug!("decoding kernel {}: {}", protocol.name, irp_notation); + + let irp = match Irp::parse(irp_notation) { + Ok(m) => m, + Err(s) => { + eprintln!("unable to parse irp ‘{}’: {s}", irp_notation); + std::process::exit(2); + } + }; + + irps.push((protocol.name, irp_notation, irp)); + } + } + } + + if !decode.linux_kernel && decode.irp.is_empty() && decode.keymap.is_empty() { match get_irp_protocols(&global.irp_protocols) { Ok(res) => { irp_protocols_xml = res; diff --git a/cir/src/keymap/mod.rs b/cir/src/keymap/mod.rs index a8bb7f7..7a2ecbc 100644 --- a/cir/src/keymap/mod.rs +++ b/cir/src/keymap/mod.rs @@ -9,6 +9,7 @@ mod parse; mod protocol; pub use encode::encode; +pub use protocol::LINUX_PROTOCOLS; /// A Linux keymap, either toml or text format used by ir-keytable #[derive(PartialEq, Debug, Default)] diff --git a/cir/src/keymap/protocol.rs b/cir/src/keymap/protocol.rs index 4a2b434..0b8b76e 100644 --- a/cir/src/keymap/protocol.rs +++ b/cir/src/keymap/protocol.rs @@ -47,7 +47,7 @@ impl LinuxProtocol { } } -const LINUX_PROTOCOLS: &[LinuxProtocol] = &[ +pub const LINUX_PROTOCOLS: &[LinuxProtocol] = &[ LinuxProtocol { name: "rc5", decoder: "rc-5",