From 154aae4197cec7cced859b5861e80ac1cadc8669 Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Wed, 22 Dec 2021 17:29:43 +0200 Subject: [PATCH 01/14] Clear the serialport and internal buffer in case of checksum errors This helps recovering from communication errors when connecting to already "busy" line mid-message. --- examples/coinacceptor/main.rs | 2 +- src/client.rs | 14 ++++++++++++++ src/coinacceptor.rs | 7 +++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/coinacceptor/main.rs b/examples/coinacceptor/main.rs index 551cb92..782baba 100644 --- a/examples/coinacceptor/main.rs +++ b/examples/coinacceptor/main.rs @@ -30,7 +30,7 @@ fn main() { let dev = matches.value_of("serial").unwrap(); let serial = serialport::new(dev, 9600) - .timeout(Duration::from_millis(500)) + .timeout(Duration::from_millis(100)) .open() .expect("Failed to open port"); diff --git a/src/client.rs b/src/client.rs index b458b04..6211403 100644 --- a/src/client.rs +++ b/src/client.rs @@ -52,6 +52,7 @@ pub trait CCTalkClient { fn set_bill_event(&mut self, bill_event: BillEvent); fn read_messages(&mut self) -> Result, ClientError>; fn send_message(&mut self, msg: &Message) -> Result<(), ClientError>; + fn clear(&mut self); } pub struct SerialClient { @@ -169,6 +170,15 @@ impl SerialClient { Ok(messages) } + + /// Clear incoming message buffer together with serial port's + /// input and output buffers. + fn clear(&mut self) { + self.buffer.clear(); + self.port + .clear(serialport::ClearBuffer::All) + .unwrap_or_else(|e| log::error!("Unable to flush serial: {:?}", e)); + } } impl CCTalkClient for SerialClient { @@ -213,6 +223,8 @@ impl CCTalkClient for SerialClient { Err(e) => Err(ClientError::IOError(e)), } } + + fn clear(&mut self) {} } pub struct DummyClient { @@ -233,6 +245,8 @@ impl DummyClient { } impl CCTalkClient for DummyClient { + fn clear(&mut self) {} + fn send_and_check_reply(&mut self, msg: &Message) -> Result { match msg.payload.header { HeaderType::ReadBufferedBillEvents => { diff --git a/src/coinacceptor.rs b/src/coinacceptor.rs index 9c5acc7..8b97fd9 100644 --- a/src/coinacceptor.rs +++ b/src/coinacceptor.rs @@ -128,13 +128,14 @@ impl CoinAcceptor { let _received = self.client.read_messages(); let received = match _received { Ok(data) => { - log::trace!("Read: {:?}", data); + // log::trace!("Read: {:?}", data); data } Err(error) => { match error { ClientError::CCTalkError(ErrorType::ChecksumError) => { - println!("Checksum error"); + self.client.clear(); + log::error!("Checksum error"); } _ => panic!("Client error: {:?}", error), } @@ -366,6 +367,8 @@ mod tests { } impl CCTalkClient for MPSCTestClient { + fn clear(&mut self) {} + fn send_and_check_reply(&mut self, _msg: &Message) -> Result { Ok(Payload { header: HeaderType::Unknown(0), From f70ff24b4a4f5b178e87ac85c31d4c61300347cd Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Wed, 22 Dec 2021 17:47:27 +0200 Subject: [PATCH 02/14] protocol: Fix some clippy warnings --- src/protocol.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/protocol.rs b/src/protocol.rs index 0a560c8..85069fc 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -853,9 +853,7 @@ impl Message { } pub fn encode(&self) -> Vec { - let mut temp = Vec::::new(); - temp.push(self.destination); - temp.push(self.length); + let mut temp = vec![self.destination, self.length]; match self.checksum_type { ChecksumType::SimpleChecksum => { @@ -946,16 +944,13 @@ impl Message { } pub fn calc_own_crc(&self) -> CRC { - let mut data = Vec::::new(); - - data.push(self.destination); - data.push(self.length); + let mut data = vec![self.destination, self.length]; data.append(&mut self.payload.encode()); - Message::calc_crc(&data) + Self::calc_crc(&data) } - pub fn calc_crc(data: &Vec) -> CRC { + pub fn calc_crc(data: &[u8]) -> CRC { let poly = 0x1021; let mut crc = 0u16; @@ -973,7 +968,7 @@ impl Message { [(crc & 0xff) as u8, (crc >> 8 & 0xff) as u8] } - pub fn validate_checksum(raw: &Vec) -> bool { + pub fn validate_checksum(raw: &[u8]) -> bool { if raw.is_empty() { log::error!("Validate checksum called on empty message!"); return false; @@ -987,15 +982,15 @@ impl Message { rem == 0 } - pub fn validate_crc(raw: &Vec) -> bool { + pub fn validate_crc(raw: &[u8]) -> bool { if raw.is_empty() { log::error!("Validate CRC called on empty message!"); return false; } - let mut data = raw.clone(); + let mut data = raw.to_owned(); let crc: [u8; 2] = [data.remove(2), data.pop().unwrap()]; - crc == Message::calc_crc(&data) + crc == Self::calc_crc(&data) } } From cb5ca022ec596ae5d589156c185d83dc3fa1e6cb Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Thu, 13 Jan 2022 16:11:01 +0200 Subject: [PATCH 03/14] Disable libudev dependency requirement for serialport This is causing some issues when using cross-compilation. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index cc090d9..96690fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ description = "CCTalk protocol implementation for handling payment devices" edition = "2018" [dependencies] -serialport = "4" +serialport = {version = "4.0.1", default-features = false} log = "0.4" [[example]] From b5485c3883d24b611d8a00d408a0d36bde8b57da Mon Sep 17 00:00:00 2001 From: Kaspar Muuk Date: Fri, 14 Jan 2022 11:01:37 +0200 Subject: [PATCH 04/14] Bypass internal buffer, change min message length (#1) --- examples/coinacceptor/main.rs | 7 ++++--- src/client.rs | 11 ++++------- src/protocol.rs | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/examples/coinacceptor/main.rs b/examples/coinacceptor/main.rs index 782baba..9ea6d38 100644 --- a/examples/coinacceptor/main.rs +++ b/examples/coinacceptor/main.rs @@ -11,8 +11,9 @@ const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); const DESCRIPTION: Option<&'static str> = option_env!("CARGO_PKG_DESCRIPTION"); fn main() { - env_logger::init(); + env_logger::init(); + let matches = App::new(PROGRAM.unwrap_or("cctalk-emulator")) .version(VERSION.unwrap_or("unknown")) .about(DESCRIPTION.unwrap_or("")) @@ -30,7 +31,7 @@ fn main() { let dev = matches.value_of("serial").unwrap(); let serial = serialport::new(dev, 9600) - .timeout(Duration::from_millis(100)) + .timeout(Duration::from_millis(20)) .open() .expect("Failed to open port"); @@ -69,6 +70,6 @@ fn main() { thread::sleep(Duration::from_millis(20)); } - thread::sleep(Duration::from_millis(100)); + thread::sleep(Duration::from_millis(50)); } } diff --git a/src/client.rs b/src/client.rs index 6211403..9d58705 100644 --- a/src/client.rs +++ b/src/client.rs @@ -79,12 +79,9 @@ impl SerialClient { received: &mut Vec, messages: &mut Vec, ) -> Result<(), ClientError> { - // log::debug!("Received: {:?}", received); - self.buffer.append(received); - // log::debug!("Buffer: {:?}", self.buffer); - - // decode will leave the remaining stuff in the buffer - let decode_res = Message::decode(&mut self.buffer); + + // decode will process latest message + let decode_res = Message::decode(received); match decode_res { Ok(message) => { if message.destination == self.address { @@ -212,7 +209,7 @@ impl CCTalkClient for SerialClient { fn set_bill_event(&mut self, _: BillEvent) {} fn read_messages(&mut self) -> Result, ClientError> { - self.read_all(1) + self.read() } fn send_message(&mut self, msg: &Message) -> Result<(), ClientError> { diff --git a/src/protocol.rs b/src/protocol.rs index 85069fc..987495d 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -877,7 +877,7 @@ impl Message { let msg_length = raw.len() as u16; - if msg_length < 2 { + if msg_length < 5 { return Err(ErrorType::PartialMessage); } From e15369f4227cd59fdce31a586c8d7d01c7050228 Mon Sep 17 00:00:00 2001 From: Kaspar Muuk Date: Thu, 27 Jan 2022 14:48:26 +0200 Subject: [PATCH 05/14] client: add trace logging cases (#2) --- src/client.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/client.rs b/src/client.rs index 9d58705..7cd768a 100644 --- a/src/client.rs +++ b/src/client.rs @@ -85,6 +85,10 @@ impl SerialClient { match decode_res { Ok(message) => { if message.destination == self.address { + log::trace!( + "Received: {:?}", + message, + ); messages.push(message); Ok(()) } else { @@ -124,7 +128,6 @@ impl SerialClient { fn send(&mut self, msg: &Message) -> Result<(), std::io::Error> { let buf: Vec = msg.encode(); - // log::debug!("Sending CCTalk message: {:?}", msg); log::trace!("Sending CCTalk message encoded: {:?}", buf); self.port.write_all(&buf[..]) } @@ -136,8 +139,10 @@ impl SerialClient { while (messages.len() < 1) && (counter < 80) { let mut received = self.read_from_serial()?; - // log::debug!("Received on serial: {:?} Counter: {}", received, counter); - self.read_and_decode(&mut received, &mut messages)?; + if received != []{ + log::trace!("Received on serial: {:?} Counter: {}", received, counter); + self.read_and_decode(&mut received, &mut messages)?; + } counter += 1; } From 0a67adea7dc39925f82cb230d2855e6cd451a414 Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 10:33:13 +0200 Subject: [PATCH 06/14] cargo: Bump clap to 3.0.x --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 96690fb..4880993 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,4 @@ name = "cctalk-host" [dev-dependencies] env_logger = "0.7" -clap = "2.33.3" +clap = "3.0" From fe27638fe74c6b113f886c4658b9747b962fb4ae Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 10:56:14 +0200 Subject: [PATCH 07/14] Bump clap to 3.0.x and update examples --- Cargo.toml | 2 +- examples/cctalk-host/main.rs | 12 ++++++------ examples/coinacceptor/main.rs | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4880993..6710f1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,4 @@ name = "cctalk-host" [dev-dependencies] env_logger = "0.7" -clap = "3.0" +clap = "~3.0.14" diff --git a/examples/cctalk-host/main.rs b/examples/cctalk-host/main.rs index 572bb37..462f5ef 100644 --- a/examples/cctalk-host/main.rs +++ b/examples/cctalk-host/main.rs @@ -1,5 +1,5 @@ use cctalk::{device::CCTalkDevice, protocol::ChecksumType}; -use clap::{value_t, App, Arg}; +use clap::{App, Arg}; use std::time::Duration; const PROGRAM: Option<&'static str> = option_env!("CARGO_PKG_NAME"); @@ -13,8 +13,8 @@ fn main() { .version(VERSION.unwrap_or("unknown")) .about(DESCRIPTION.unwrap_or("")) .arg( - Arg::with_name("serial") - .short("s") + Arg::new("serial") + .short('s') .long("serial") .value_name("DEVICE") .help("Serial Device for ccTalk host (for example /dev/ttyUSB0 or COM3)") @@ -22,8 +22,8 @@ fn main() { .required(true), ) .arg( - Arg::with_name("target") - .short("t") + Arg::new("target") + .short('t') .long("target") .value_name("TARGET_ADDRESS") .help("Address of the client device") @@ -33,7 +33,7 @@ fn main() { let dev = matches.value_of("serial").unwrap(); - let target_device_id = value_t!(matches.value_of("target"), u8).unwrap_or_else(|e| e.exit()); + let target_device_id: u8 = matches.value_of_t("target").unwrap_or_else(|e| e.exit()); let serial = serialport::new(dev, 9600) .timeout(Duration::from_millis(500)) diff --git a/examples/coinacceptor/main.rs b/examples/coinacceptor/main.rs index 9ea6d38..734b27f 100644 --- a/examples/coinacceptor/main.rs +++ b/examples/coinacceptor/main.rs @@ -18,8 +18,8 @@ fn main() { .version(VERSION.unwrap_or("unknown")) .about(DESCRIPTION.unwrap_or("")) .arg( - Arg::with_name("serial") - .short("s") + Arg::new("serial") + .short('s') .long("serial") .value_name("DEVICE") .help("Serial Device for ccTalk") From 6e7c840a671494641c32a21ce4f6c8c73c517995 Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 11:08:53 +0200 Subject: [PATCH 08/14] Bump clap to 3.1 and update examples --- Cargo.toml | 2 +- examples/cctalk-host/main.rs | 4 ++-- examples/coinacceptor/main.rs | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6710f1a..99efa0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,4 +19,4 @@ name = "cctalk-host" [dev-dependencies] env_logger = "0.7" -clap = "~3.0.14" +clap = "3.1" diff --git a/examples/cctalk-host/main.rs b/examples/cctalk-host/main.rs index 462f5ef..72202e5 100644 --- a/examples/cctalk-host/main.rs +++ b/examples/cctalk-host/main.rs @@ -1,5 +1,5 @@ use cctalk::{device::CCTalkDevice, protocol::ChecksumType}; -use clap::{App, Arg}; +use clap::{Command, Arg}; use std::time::Duration; const PROGRAM: Option<&'static str> = option_env!("CARGO_PKG_NAME"); @@ -9,7 +9,7 @@ const DESCRIPTION: Option<&'static str> = option_env!("CARGO_PKG_DESCRIPTION"); fn main() { env_logger::init(); - let matches = App::new(PROGRAM.unwrap_or("cctalk-host")) + let matches = Command::new(PROGRAM.unwrap_or("cctalk-host")) .version(VERSION.unwrap_or("unknown")) .about(DESCRIPTION.unwrap_or("")) .arg( diff --git a/examples/coinacceptor/main.rs b/examples/coinacceptor/main.rs index 734b27f..f406c59 100644 --- a/examples/coinacceptor/main.rs +++ b/examples/coinacceptor/main.rs @@ -2,7 +2,7 @@ use cctalk::{ device::{CoinAcceptor, CoinTable, CoreInfo}, protocol::{ChecksumType, Message}, }; -use clap::{App, Arg}; +use clap::{Command, Arg}; use std::thread; use std::time::Duration; @@ -14,7 +14,7 @@ fn main() { env_logger::init(); - let matches = App::new(PROGRAM.unwrap_or("cctalk-emulator")) + let matches = Command::new(PROGRAM.unwrap_or("cctalk-emulator")) .version(VERSION.unwrap_or("unknown")) .about(DESCRIPTION.unwrap_or("")) .arg( From f270a53d993abfc1ca7e27c26a39278af629158a Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 12:24:18 +0200 Subject: [PATCH 09/14] Add a hacky implementation to query/set cctalk parameters --- examples/cctalk-host/main.rs | 86 +++++++++++++++++++++++++++--------- 1 file changed, 65 insertions(+), 21 deletions(-) diff --git a/examples/cctalk-host/main.rs b/examples/cctalk-host/main.rs index 72202e5..e612aff 100644 --- a/examples/cctalk-host/main.rs +++ b/examples/cctalk-host/main.rs @@ -1,11 +1,47 @@ use cctalk::{device::CCTalkDevice, protocol::ChecksumType}; -use clap::{Command, Arg}; +use clap::{Arg, Command}; use std::time::Duration; const PROGRAM: Option<&'static str> = option_env!("CARGO_PKG_NAME"); const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION"); const DESCRIPTION: Option<&'static str> = option_env!("CARGO_PKG_DESCRIPTION"); +fn master_inhibit(dev: &str, id: u8, status: Option<&str>) { + let serial = serialport::new(dev, 9600) + .timeout(Duration::from_millis(500)) + .open() + .expect("Failed to open port"); + + let serial_dev = Box::new(cctalk::client::SerialClient::new(serial, 1).unwrap()); + let mut cctalk = CCTalkDevice::new(serial_dev, id, ChecksumType::SimpleChecksum).unwrap(); + + if let Some(b) = status { + let x = match b { + "1" => 1, + _ => 0, + }; + println!("{:?}", cctalk.modify_master_inhibit_status(x).unwrap()); + } +} + +fn request_info(dev: &str, id: u8, field: Option<&str>) { + let serial = serialport::new(dev, 9600) + .timeout(Duration::from_millis(500)) + .open() + .expect("Failed to open port"); + + let serial_dev = Box::new(cctalk::client::SerialClient::new(serial, 1).unwrap()); + + let mut cctalk = CCTalkDevice::new(serial_dev, id, ChecksumType::SimpleChecksum).unwrap(); + + match field { + Some("equipment_category_id") => { + println!("{:?}", cctalk.request_equipment_category().unwrap()); + } + _ => {} + } +} + fn main() { env_logger::init(); @@ -17,7 +53,7 @@ fn main() { .short('s') .long("serial") .value_name("DEVICE") - .help("Serial Device for ccTalk host (for example /dev/ttyUSB0 or COM3)") + .help("ccTalk serial device (for example /dev/ttyUSB0 or COM3)") .takes_value(true) .required(true), ) @@ -29,27 +65,35 @@ fn main() { .help("Address of the client device") .default_value("2"), ) + .subcommand( + Command::new("request") + .arg( + Arg::new("field") + //.possible_values(["manufacturer_id", "equipment_category_id", "serial_number"]) + .possible_values(["equipment_category_id"]), + ) + .about("Request information from device (status, manufacturer id, ...)"), + ) + .subcommand( + Command::new("set_master_inhibit").arg( + Arg::new("state") + .takes_value(true) + .possible_values(["1", "0"]) + .required(true), + ), + ) .get_matches(); let dev = matches.value_of("serial").unwrap(); + let _id: u8 = matches.value_of_t("target").unwrap_or_else(|e| e.exit()); - let target_device_id: u8 = matches.value_of_t("target").unwrap_or_else(|e| e.exit()); - - let serial = serialport::new(dev, 9600) - .timeout(Duration::from_millis(500)) - .open() - .expect("Failed to open port"); - - // As per ccTalk general usage, there is usually single "master" - // which initiates the queries and its address is 1. - let serial_dev = Box::new(cctalk::client::SerialClient::new(serial, 1).unwrap()); - - let mut cctalk = - CCTalkDevice::new(serial_dev, target_device_id, ChecksumType::SimpleChecksum).unwrap(); - - println!("Querying client device: {}", target_device_id); - - let resp = cctalk.request_equipment_category().unwrap(); - - println!("{}", resp); + match matches.subcommand() { + Some(("request", sub_matches)) => request_info(dev, _id, sub_matches.value_of("field")), + Some(("set_master_inhibit", sub_matches)) => { + master_inhibit(dev, _id, sub_matches.value_of("state")); + } + _ => { + println!("Expecting subcommand..."); + } + } } From c3ada0d220016e2e9a970e133c00b13e9fb1c049 Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 12:46:02 +0200 Subject: [PATCH 10/14] Drop redundant field names in struct initialization --- src/protocol.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/protocol.rs b/src/protocol.rs index 987495d..5cd215e 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -842,13 +842,13 @@ impl Message { payload: Payload, checksum_type: ChecksumType, ) -> Message { - let length = payload.data.len(); + let length: u8 = payload.data.len() as u8; Message { - destination: destination, - length: length as u8, - source: source, - payload: payload, - checksum_type: checksum_type, + destination, + length, + source, + payload, + checksum_type, } } @@ -920,11 +920,11 @@ impl Message { }; Ok(Message { - destination: destination, + destination, length: data_length, - source: source, - payload: payload, - checksum_type: checksum_type, + source, + payload, + checksum_type, }) } From 221357d7745b2730e1ff947b361cdc01e922dfad Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 12:49:50 +0200 Subject: [PATCH 11/14] Simplify code branching in the Message checksum validation --- src/protocol.rs | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/protocol.rs b/src/protocol.rs index 5cd215e..c70c4c4 100644 --- a/src/protocol.rs +++ b/src/protocol.rs @@ -897,14 +897,12 @@ impl Message { if Message::validate_checksum(&raw_msg) { checksum_type = ChecksumType::SimpleChecksum; source = raw_msg[2]; + } else if Message::validate_crc(&raw_msg) { + checksum_type = ChecksumType::CRCChecksum; + source = 1; // Source address is always 1 in CRC mode } else { - if Message::validate_crc(&raw_msg) { - checksum_type = ChecksumType::CRCChecksum; - source = 1; // Source address is always 1 in CRC mode - } else { - log::error!("Failed raw: {:?}", raw_msg); - return Err(ErrorType::ChecksumError); - } + log::error!("Failed raw: {:?}", raw_msg); + return Err(ErrorType::ChecksumError); } let mut raw_data = raw_msg.split_off(4); From be3d7f9abf4e73e383a1e309aa43831298fe967b Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 12:53:17 +0200 Subject: [PATCH 12/14] Fix some unneeded (de)referencing --- src/client.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 7cd768a..43b8416 100644 --- a/src/client.rs +++ b/src/client.rs @@ -34,12 +34,12 @@ impl convert::From for ClientError { impl Clone for ClientError { fn clone(&self) -> Self { - match self { - &ClientError::CCTalkError(ref e) => ClientError::CCTalkError(e.clone()), - &ClientError::IOError(ref e) => { + match *self { + ClientError::CCTalkError(ref e) => ClientError::CCTalkError(*e), + ClientError::IOError(ref e) => { ClientError::IOError(std::io::Error::new(e.kind(), e.to_string())) } - &ClientError::SerialError(ref e) => { + ClientError::SerialError(ref e) => { ClientError::SerialError(serialport::Error::new(e.kind(), e.to_string())) } } @@ -190,7 +190,7 @@ impl CCTalkClient for SerialClient { // log::debug!("Waiting for Reply"); let received = self.read()?; if received.len() > 0 { - let ref reply = received[0]; + let reply = &received[0]; match reply.payload.header { HeaderType::Reply => Ok(reply.payload.clone()), _ => Err(ClientError::CCTalkError(ErrorType::NotAReply)), @@ -218,7 +218,7 @@ impl CCTalkClient for SerialClient { } fn send_message(&mut self, msg: &Message) -> Result<(), ClientError> { - let send_result = self.send(&msg); + let send_result = self.send(msg); self.buffer.clear(); match send_result { Ok(r) => Ok(r), From fa6d146cd3ad2d29667db54f01b5574179bc750b Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 12:59:12 +0200 Subject: [PATCH 13/14] Use .is_empty() and proper bool checks in conditions --- src/client.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/client.rs b/src/client.rs index 43b8416..1d989cc 100644 --- a/src/client.rs +++ b/src/client.rs @@ -137,9 +137,9 @@ impl SerialClient { let mut counter = 0; - while (messages.len() < 1) && (counter < 80) { + while messages.is_empty() && (counter < 80) { let mut received = self.read_from_serial()?; - if received != []{ + if !received.is_empty() { log::trace!("Received on serial: {:?} Counter: {}", received, counter); self.read_and_decode(&mut received, &mut messages)?; } @@ -163,7 +163,7 @@ impl SerialClient { while !timeout { let mut received = self.read_from_serial()?; self.read_and_decode(&mut received, &mut messages)?; - if (received.len() == 0) && (self.buffer.len() == 0) { + if received.is_empty() && self.buffer.is_empty() { timeout = true; } } @@ -189,14 +189,14 @@ impl CCTalkClient for SerialClient { // log::debug!("Waiting for Reply"); let received = self.read()?; - if received.len() > 0 { + if !received.is_empty() { let reply = &received[0]; match reply.payload.header { HeaderType::Reply => Ok(reply.payload.clone()), _ => Err(ClientError::CCTalkError(ErrorType::NotAReply)), } } else { - if self.buffer.len() != 0 { + if !self.buffer.is_empty() { log::debug!( "Message not received in time, clearing partial message from buffer: {:?}", self.buffer @@ -254,7 +254,7 @@ impl CCTalkClient for DummyClient { HeaderType::ReadBufferedBillEvents => { let (byte1, byte2) = self.bill_event.to_u8(); - if self.changed == true { + if self.changed { self.counter += 1; self.changed = false; } From 6295c34e75e9ddb4a369b4fb2c5ba6b9f8e97bcc Mon Sep 17 00:00:00 2001 From: Priit Laes Date: Fri, 11 Mar 2022 13:04:13 +0200 Subject: [PATCH 14/14] Remove repetitive let statement --- src/coinacceptor.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/coinacceptor.rs b/src/coinacceptor.rs index 8b97fd9..90dde9e 100644 --- a/src/coinacceptor.rs +++ b/src/coinacceptor.rs @@ -238,12 +238,11 @@ impl CoinAcceptor { self.client.send_message(&msg) } HeaderType::RequestMasterInhibitStatus => { - let status: u8; - if self.cc_master_inhibit { - status = 0u8; + let status: u8 = if self.cc_master_inhibit { + 0u8 } else { - status = 1u8; - } + 1u8 + }; let msg = self.create_message(Payload { header: (HeaderType::Reply), data: (vec![status]),