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/plugins/test.lua b/plugins/test.lua index 1b44e7e..c3ac5ef 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.serial_tx(msg) + 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.serial_tx(str2bytes(arg_list[1])) + scope.connect('COM1', 115200) + --scope.serial_tx({ 1, 2, 3 }) 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 690ed11..ba74610 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, + } + } } } } @@ -419,20 +508,28 @@ 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(), - }, + // PluginRequest::SerialTx { + // msg: msg.as_bytes().to_vec(), + // }, PluginRequest::Println { msg: format!("Sent {}", msg), }, 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() { @@ -455,25 +552,25 @@ 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(), - }, - 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]), @@ -513,20 +610,29 @@ 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(), - }, + // PluginRequest::SerialTx { + // msg: "Hello".as_bytes().to_vec(), + // }, PluginRequest::Println { msg: "Sent World!".to_string(), }, 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() { @@ -557,25 +663,25 @@ 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(), - }, - 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]), 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 + } +}