2021-08-31 15:03:44 +00:00
|
|
|
// Copyright © 2021 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
//
|
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
use std::{
|
|
|
|
collections::VecDeque,
|
|
|
|
io::Write,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicBool, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
};
|
2021-08-31 15:03:44 +00:00
|
|
|
|
2022-08-23 07:53:25 +00:00
|
|
|
const MAX_BUFFER_SIZE: usize = 1 << 20;
|
|
|
|
|
2021-08-31 15:03:44 +00:00
|
|
|
// Circular buffer implementation for serial output.
|
|
|
|
// Read from head; push to tail
|
2022-08-25 08:26:59 +00:00
|
|
|
pub struct SerialBuffer {
|
2022-08-18 16:22:33 +00:00
|
|
|
buffer: VecDeque<u8>,
|
2021-08-31 15:03:44 +00:00
|
|
|
out: Box<dyn Write + Send>,
|
2022-08-18 16:22:33 +00:00
|
|
|
write_out: Arc<AtomicBool>,
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SerialBuffer {
|
2022-08-25 08:26:59 +00:00
|
|
|
pub fn new(out: Box<dyn Write + Send>, write_out: Arc<AtomicBool>) -> Self {
|
2021-08-31 15:03:44 +00:00
|
|
|
Self {
|
2022-08-18 16:22:33 +00:00
|
|
|
buffer: VecDeque::new(),
|
2021-08-31 15:03:44 +00:00
|
|
|
out,
|
2022-08-18 16:22:33 +00:00
|
|
|
write_out,
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-23 07:53:25 +00:00
|
|
|
|
|
|
|
fn fill_buffer(&mut self, buf: &[u8]) {
|
|
|
|
if buf.len() >= MAX_BUFFER_SIZE {
|
|
|
|
let offset = buf.len() - MAX_BUFFER_SIZE;
|
|
|
|
self.buffer = VecDeque::from(buf[offset..].to_vec());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let num_allowed_bytes = MAX_BUFFER_SIZE - buf.len();
|
|
|
|
if self.buffer.len() > num_allowed_bytes {
|
|
|
|
let num_bytes_to_remove = self.buffer.len() - num_allowed_bytes;
|
|
|
|
self.buffer.drain(..num_bytes_to_remove);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.buffer.extend(buf);
|
|
|
|
}
|
2022-08-18 16:22:33 +00:00
|
|
|
}
|
2021-08-31 15:03:44 +00:00
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
impl Write for SerialBuffer {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, std::io::Error> {
|
|
|
|
// Simply fill the buffer if we're not allowed to write to the out
|
|
|
|
// device.
|
|
|
|
if !self.write_out.load(Ordering::Acquire) {
|
2022-08-23 07:53:25 +00:00
|
|
|
self.fill_buffer(buf);
|
2022-08-18 16:22:33 +00:00
|
|
|
return Ok(buf.len());
|
|
|
|
}
|
2021-09-24 05:32:41 +00:00
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
// In case we're allowed to write to the out device, we flush the
|
|
|
|
// content of the buffer.
|
|
|
|
self.flush()?;
|
|
|
|
|
|
|
|
// If after flushing the buffer, it's still not empty, that means
|
|
|
|
// only a subset of the bytes was written and we should fill the buffer
|
|
|
|
// with what's coming from the serial.
|
|
|
|
if !self.buffer.is_empty() {
|
2022-08-23 07:53:25 +00:00
|
|
|
self.fill_buffer(buf);
|
2022-08-18 16:22:33 +00:00
|
|
|
return Ok(buf.len());
|
|
|
|
}
|
2021-09-24 05:32:41 +00:00
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
// We reach this point if we're allowed to write to the out device
|
|
|
|
// and we know there's nothing left in the buffer.
|
|
|
|
let mut offset = 0;
|
|
|
|
loop {
|
|
|
|
match self.out.write(&buf[offset..]) {
|
|
|
|
Ok(written_bytes) => {
|
|
|
|
if written_bytes < buf.len() - offset {
|
|
|
|
offset += written_bytes;
|
|
|
|
continue;
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
if !matches!(e.kind(), std::io::ErrorKind::WouldBlock) {
|
|
|
|
return Err(e);
|
|
|
|
}
|
2022-08-23 07:53:25 +00:00
|
|
|
self.fill_buffer(&buf[offset..]);
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-18 16:22:33 +00:00
|
|
|
break;
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
// Make sure we flush anything that might have been written to the
|
|
|
|
// out device.
|
|
|
|
self.out.flush()?;
|
2021-08-31 15:03:44 +00:00
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
Ok(buf.len())
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
2021-09-24 05:32:41 +00:00
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
// This function flushes the content of the buffer to the out device if
|
|
|
|
// it is allowed to, otherwise this is a no-op.
|
|
|
|
fn flush(&mut self) -> Result<(), std::io::Error> {
|
|
|
|
if !self.write_out.load(Ordering::Acquire) {
|
|
|
|
return Ok(());
|
2021-09-24 05:32:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-18 16:22:33 +00:00
|
|
|
while let Some(byte) = self.buffer.pop_front() {
|
|
|
|
if self.out.write_all(&[byte]).is_err() {
|
|
|
|
self.buffer.push_front(byte);
|
|
|
|
break;
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-18 16:22:33 +00:00
|
|
|
self.out.flush()
|
2021-08-31 15:03:44 +00:00
|
|
|
}
|
|
|
|
}
|