From ca8bc643f2c056ffeaf24436149a2bf14823cd32 Mon Sep 17 00:00:00 2001 From: "Matheus T. dos Santos" Date: Thu, 9 May 2024 21:15:14 -0300 Subject: [PATCH 1/3] feat: add `scope.info` to get serial info and other infos further --- plugins/scope.lua | 4 ++ src/plugin.rs | 89 +++++++++++++++++++++++++++++++++++++++++++ src/plugin_manager.rs | 17 ++++++++- src/serial.rs | 14 +++++++ 4 files changed, 122 insertions(+), 2 deletions(-) diff --git a/plugins/scope.lua b/plugins/scope.lua index 4234873..10b9eff 100644 --- a/plugins/scope.lua +++ b/plugins/scope.lua @@ -53,5 +53,9 @@ scope = { quiet = quiet or false local _, stdout, stderr = coroutine.yield({ ":exec", cmd, quiet }) return stdout, stderr + end, + info = function() + local _, output = coroutine.yield({ ":info" }) + return output end } diff --git a/src/plugin.rs b/src/plugin.rs index 690ed11..b27c71b 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -38,6 +38,7 @@ pub enum PluginRequest { cmd: String, quiet: bool, }, + Info, } pub enum PluginRequestResult { @@ -45,6 +46,25 @@ pub enum PluginRequestResult { stdout: Vec, stderr: Vec, }, + Info { + serial: SerialInfoResult, + }, +} + +pub struct SerialInfoResult { + port: String, + baudrate: u32, + is_connected: bool, +} + +impl SerialInfoResult { + pub fn new(port: String, baudrate: u32, is_connected: bool) -> Self { + Self { + port, + baudrate, + is_connected, + } + } } pub struct SerialRxCall { @@ -153,6 +173,7 @@ impl<'a> TryFrom> for PluginRequest { cmd: value.get(2).map_err(|err| err.to_string())?, quiet: value.get(3).map_err(|err| err.to_string())?, }), + ":info" => Ok(PluginRequest::Info), _ => Err("Unknown function".to_string()), } } @@ -349,6 +370,40 @@ impl Iterator for SerialRxCall { Err(_) => None, } } + PluginRequestResult::Info { + serial: + SerialInfoResult { + port, + baudrate, + is_connected, + }, + } => { + let serial = self + .lua + .create_table() + .expect("Cannot create serial lua table"); + serial.set("port", port).expect("Cannot add port"); + serial + .set("baudrate", baudrate) + .expect("Cannot add baudrate"); + serial + .set("is_connected", is_connected) + .expect("Cannot add baudrate"); + + let table = self.lua.create_table().expect("Cannot create a lua table"); + table.set("serial", serial).expect("Cannot add serial"); + + match serial_rx.resume::<_, Table>((msg, table)) { + Ok(req) => { + let req: PluginRequest = match req.try_into() { + Ok(req) => req, + Err(msg) => return Some(PluginRequest::Eprintln { msg }), + }; + Some(req) + } + Err(_) => None, + } + } } } } @@ -383,6 +438,40 @@ impl Iterator for UserCommandCall { Err(_) => None, } } + PluginRequestResult::Info { + serial: + SerialInfoResult { + port, + baudrate, + is_connected, + }, + } => { + let serial = self + .lua + .create_table() + .expect("Cannot create serial lua table"); + serial.set("port", port).expect("Cannot add port"); + serial + .set("baudrate", baudrate) + .expect("Cannot add baudrate"); + serial + .set("is_connected", is_connected) + .expect("Cannot add baudrate"); + + let table = self.lua.create_table().expect("Cannot create a lua table"); + table.set("serial", serial).expect("Cannot add serial"); + + match user_command.resume::<_, Table>((arg_list, table)) { + Ok(req) => { + let req: PluginRequest = match req.try_into() { + Ok(req) => req, + Err(msg) => return Some(PluginRequest::Eprintln { msg }), + }; + Some(req) + } + Err(_) => None, + } + } } } } diff --git a/src/plugin_manager.rs b/src/plugin_manager.rs index 7336344..9ea2c0c 100644 --- a/src/plugin_manager.rs +++ b/src/plugin_manager.rs @@ -1,7 +1,7 @@ use crate::messages::{SerialRxData, UserTxData}; use crate::plugin::{ - Plugin, PluginRequest, PluginRequestResult, PluginRequestResultHolder, SerialRxCall, - UserCommandCall, + Plugin, PluginRequest, PluginRequestResult, PluginRequestResultHolder, SerialInfoResult, + SerialRxCall, UserCommandCall, }; use crate::process::ProcessRunner; use crate::serial::SerialIF; @@ -322,6 +322,19 @@ impl PluginManager { return res; } }, + PluginRequest::Info => { + let serial_if = interface.lock().await; + let serial_info = serial_if.get_info(); + let serial_is_connected = serial_if.is_connected(); + + return Some(PluginRequestResult::Info { + serial: SerialInfoResult::new( + serial_info.port().to_owned(), + serial_info.baudrate(), + serial_is_connected, + ), + }); + } } None diff --git a/src/serial.rs b/src/serial.rs index 1b929de..90be24f 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -50,6 +50,10 @@ impl SerialIF { ) } + pub fn get_info(&self) -> SerialInfo { + self.synchronized().info.clone() + } + pub fn is_connected(&self) -> bool { self.synchronized().is_connected.load(Ordering::SeqCst) } @@ -321,3 +325,13 @@ pub struct SerialInfo { port: String, baudrate: u32, } + +impl SerialInfo { + pub fn port(&self) -> &str { + &self.port + } + + pub fn baudrate(&self) -> u32 { + self.baudrate + } +} From da142186f6bda39436018eb26620f0e3c86ebda1 Mon Sep 17 00:00:00 2001 From: "Matheus T. dos Santos" Date: Fri, 10 May 2024 09:56:53 -0300 Subject: [PATCH 2/3] fix: fix unit tests and test.lua plugin --- plugins/test.lua | 10 ++++++++-- src/plugin.rs | 37 +++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/plugins/test.lua b/plugins/test.lua index 1b44e7e..b0d7610 100644 --- a/plugins/test.lua +++ b/plugins/test.lua @@ -7,17 +7,23 @@ require "scope" function serial_rx(msg) - scope.connect('/dev/ttyACM0', 115200) scope.disconnect() + scope.connect('COM1', 115200) scope.serial_tx(msg) scope.println('Sent ' .. bytes2str(msg)) scope.eprintln('Timeout') end function user_command(arg_list) - scope.connect('/dev/ttyACM0', 115200) scope.disconnect() + scope.connect('COM1', 115200) scope.serial_tx(str2bytes(arg_list[1])) scope.println('Sent ' .. arg_list[2]) scope.eprintln('Timeout') + scope.exec('echo hello', true) + scope.exec('echo hello') + info = scope.info() + -- info: {'serial': {port: 'COM1', baudrate: '115200', is_connected: true}} + text = '[' .. tostring(info.serial.is_connected) .. '] serial: ' .. info.serial.port .. '@' .. tostring(info.serial.baudrate) .. 'bps' + scope.println(text) end diff --git a/src/plugin.rs b/src/plugin.rs index b27c71b..aad7d89 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -508,11 +508,11 @@ mod tests { let plugin = Plugin::new(PathBuf::from("plugins/test.lua"))?; let serial_rx_call = plugin.serial_rx_call(msg.as_bytes().to_vec()); let expected = [ + PluginRequest::Disconnect, PluginRequest::Connect { - port: Some("/dev/ttyACM0".to_string()), + port: Some("COM1".to_string()), baud_rate: Some(115200), }, - PluginRequest::Disconnect, PluginRequest::SerialTx { msg: msg.as_bytes().to_vec(), }, @@ -522,6 +522,14 @@ mod tests { PluginRequest::Eprintln { msg: "Timeout".to_string(), }, + PluginRequest::Exec { + cmd: "echo hello".to_string(), + quiet: false, + }, + PluginRequest::Info, + PluginRequest::Println { + msg: "".to_string(), + }, ]; for (i, req) in serial_rx_call.enumerate() { @@ -544,17 +552,17 @@ mod tests { plugin[1].serial_rx_call(msg[1].as_bytes().to_vec()), ]; let expected = vec![ + (PluginRequest::Disconnect, PluginRequest::Disconnect), ( PluginRequest::Connect { - port: Some("/dev/ttyACM0".to_string()), + port: Some("COM1".to_string()), baud_rate: Some(115200), }, PluginRequest::Connect { - port: Some("/dev/ttyACM0".to_string()), + port: Some("COM1".to_string()), baud_rate: Some(115200), }, ), - (PluginRequest::Disconnect, PluginRequest::Disconnect), ( PluginRequest::SerialTx { msg: msg[0].as_bytes().to_vec(), @@ -602,11 +610,11 @@ mod tests { let plugin = Plugin::new(PathBuf::from("plugins/test.lua"))?; let user_command_call = plugin.user_command_call(arg_list); let expected = [ + PluginRequest::Disconnect, PluginRequest::Connect { - port: Some("/dev/ttyACM0".to_string()), + port: Some("COM1".to_string()), baud_rate: Some(115200), }, - PluginRequest::Disconnect, PluginRequest::SerialTx { msg: "Hello".as_bytes().to_vec(), }, @@ -616,6 +624,15 @@ mod tests { PluginRequest::Eprintln { msg: "Timeout".to_string(), }, + PluginRequest::Exec { + cmd: "echo hello".to_string(), + quiet: true, + }, + PluginRequest::Exec { + cmd: "echo hello".to_string(), + quiet: false, + }, + PluginRequest::Info, ]; for (i, req) in user_command_call.enumerate() { @@ -646,17 +663,17 @@ mod tests { plugin[1].user_command_call(arg_list[1].clone()), ]; let expected = vec![ + (PluginRequest::Disconnect, PluginRequest::Disconnect), ( PluginRequest::Connect { - port: Some("/dev/ttyACM0".to_string()), + port: Some("COM1".to_string()), baud_rate: Some(115200), }, PluginRequest::Connect { - port: Some("/dev/ttyACM0".to_string()), + port: Some("COM1".to_string()), baud_rate: Some(115200), }, ), - (PluginRequest::Disconnect, PluginRequest::Disconnect), ( PluginRequest::SerialTx { msg: arg_list[0][0].as_bytes().to_vec(), From 25b816ed785e81263adfd0bf4df530620b93522d Mon Sep 17 00:00:00 2001 From: "Matheus T. dos Santos" Date: Fri, 10 May 2024 10:16:30 -0300 Subject: [PATCH 3/3] fix: disable scope.serial_tx from test.lua --- plugins/test.lua | 4 ++-- src/plugin.rs | 44 ++++++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/plugins/test.lua b/plugins/test.lua index b0d7610..c3ac5ef 100644 --- a/plugins/test.lua +++ b/plugins/test.lua @@ -9,7 +9,7 @@ require "scope" function serial_rx(msg) scope.disconnect() scope.connect('COM1', 115200) - scope.serial_tx(msg) + --scope.serial_tx(msg) scope.println('Sent ' .. bytes2str(msg)) scope.eprintln('Timeout') end @@ -17,7 +17,7 @@ end function user_command(arg_list) scope.disconnect() scope.connect('COM1', 115200) - scope.serial_tx(str2bytes(arg_list[1])) + --scope.serial_tx({ 1, 2, 3 }) scope.println('Sent ' .. arg_list[2]) scope.eprintln('Timeout') scope.exec('echo hello', true) diff --git a/src/plugin.rs b/src/plugin.rs index aad7d89..ba74610 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -513,9 +513,9 @@ mod tests { port: Some("COM1".to_string()), baud_rate: Some(115200), }, - PluginRequest::SerialTx { - msg: msg.as_bytes().to_vec(), - }, + // PluginRequest::SerialTx { + // msg: msg.as_bytes().to_vec(), + // }, PluginRequest::Println { msg: format!("Sent {}", msg), }, @@ -563,14 +563,14 @@ mod tests { baud_rate: Some(115200), }, ), - ( - PluginRequest::SerialTx { - msg: msg[0].as_bytes().to_vec(), - }, - PluginRequest::SerialTx { - msg: msg[1].as_bytes().to_vec(), - }, - ), + // ( + // PluginRequest::SerialTx { + // msg: msg[0].as_bytes().to_vec(), + // }, + // PluginRequest::SerialTx { + // msg: msg[1].as_bytes().to_vec(), + // }, + // ), ( PluginRequest::Println { msg: format!("Sent {}", msg[0]), @@ -615,9 +615,9 @@ mod tests { port: Some("COM1".to_string()), baud_rate: Some(115200), }, - PluginRequest::SerialTx { - msg: "Hello".as_bytes().to_vec(), - }, + // PluginRequest::SerialTx { + // msg: "Hello".as_bytes().to_vec(), + // }, PluginRequest::Println { msg: "Sent World!".to_string(), }, @@ -674,14 +674,14 @@ mod tests { baud_rate: Some(115200), }, ), - ( - PluginRequest::SerialTx { - msg: arg_list[0][0].as_bytes().to_vec(), - }, - PluginRequest::SerialTx { - msg: arg_list[1][0].as_bytes().to_vec(), - }, - ), + // ( + // PluginRequest::SerialTx { + // msg: arg_list[0][0].as_bytes().to_vec(), + // }, + // PluginRequest::SerialTx { + // msg: arg_list[1][0].as_bytes().to_vec(), + // }, + // ), ( PluginRequest::Println { msg: format!("Sent {}", arg_list[0][1]),