|
4 | 4 |
|
5 | 5 | use crate::proto::unsafe_protocol; |
6 | 6 | use crate::{Error, Result, Status, StatusExt}; |
| 7 | +use core::fmt; |
7 | 8 | use core::fmt::Write; |
8 | 9 | use uefi_raw::protocol::console::serial::{ |
9 | 10 | SerialIoProtocol, SerialIoProtocol_1_1, SerialIoProtocolRevision, |
@@ -131,28 +132,66 @@ impl Serial { |
131 | 132 | unsafe { (self.0.set_control_bits)(&mut self.0, bits) }.to_result() |
132 | 133 | } |
133 | 134 |
|
134 | | - /// Reads data from this device. |
| 135 | + /// Reads data from the device. This function has the raw semantics of the |
| 136 | + /// underlying UEFI protocol. |
135 | 137 | /// |
136 | | - /// This operation will block until the buffer has been filled with data or |
137 | | - /// an error occurs. In the latter case, the error will indicate how many |
138 | | - /// bytes were actually read from the device. |
139 | | - pub fn read(&mut self, data: &mut [u8]) -> Result<(), usize> { |
140 | | - let mut buffer_size = data.len(); |
141 | | - unsafe { (self.0.read)(&mut self.0, &mut buffer_size, data.as_mut_ptr()) }.to_result_with( |
142 | | - || debug_assert_eq!(buffer_size, data.len()), |
| 138 | + /// The function will read bytes until either the buffer is full or a |
| 139 | + /// timeout or overrun error occurs. |
| 140 | + /// |
| 141 | + /// # Arguments |
| 142 | + /// |
| 143 | + /// - `buffer`: buffer to fill |
| 144 | + /// |
| 145 | + /// # Tips |
| 146 | + /// |
| 147 | + /// Consider setting non-default properties via [`Self::set_attributes`] |
| 148 | + /// and [`Self::set_control_bits`] matching your use-case. For more info, |
| 149 | + /// please read the general [documentation](Self) of the protocol. |
| 150 | + /// |
| 151 | + /// # Errors |
| 152 | + /// |
| 153 | + /// - [`Status::DEVICE_ERROR`]: serial device reported an error |
| 154 | + /// - [`Status::TIMEOUT`]: operation was stopped due to a timeout or overrun |
| 155 | + pub fn read(&mut self, buffer: &mut [u8]) -> Result<(), usize /* read bytes on timeout*/> { |
| 156 | + let mut buffer_size = buffer.len(); |
| 157 | + unsafe { (self.0.read)(&mut self.0, &mut buffer_size, buffer.as_mut_ptr()) }.to_result_with( |
| 158 | + || { |
| 159 | + // By spec: Either reads all requested bytes (and blocks) or |
| 160 | + // returns early with an error. |
| 161 | + assert_eq!(buffer_size, buffer.len()) |
| 162 | + }, |
143 | 163 | |_| buffer_size, |
144 | 164 | ) |
145 | 165 | } |
146 | 166 |
|
147 | | - /// Writes data to this device. |
| 167 | + /// Writes data to this device. This function has the raw semantics of the |
| 168 | + /// underlying UEFI protocol. |
| 169 | + /// |
| 170 | + /// The function will try to write all provided bytes in the configured |
| 171 | + /// timeout. |
| 172 | + /// |
| 173 | + /// # Arguments |
| 174 | + /// |
| 175 | + /// - `data`: bytes to write |
| 176 | + /// |
| 177 | + /// # Tips |
| 178 | + /// |
| 179 | + /// Consider setting non-default properties via [`Self::set_attributes`] |
| 180 | + /// and [`Self::set_control_bits`] matching your use-case. For more info, |
| 181 | + /// please read the general [documentation](Self) of the protocol. |
| 182 | + /// |
| 183 | + /// # Errors |
148 | 184 | /// |
149 | | - /// This operation will block until the data has been fully written or an |
150 | | - /// error occurs. In the latter case, the error will indicate how many bytes |
151 | | - /// were actually written to the device. |
152 | | - pub fn write(&mut self, data: &[u8]) -> Result<(), usize> { |
| 185 | + /// - [`Status::DEVICE_ERROR`]: serial device reported an error |
| 186 | + /// - [`Status::TIMEOUT`]: data write was stopped due to a timeout |
| 187 | + pub fn write(&mut self, data: &[u8]) -> Result<(), usize /* bytes written on timeout */> { |
153 | 188 | let mut buffer_size = data.len(); |
154 | 189 | unsafe { (self.0.write)(&mut self.0, &mut buffer_size, data.as_ptr()) }.to_result_with( |
155 | | - || debug_assert_eq!(buffer_size, data.len()), |
| 190 | + || { |
| 191 | + // By spec: Either reads all requested bytes (and blocks) or |
| 192 | + // returns early with an error. |
| 193 | + assert_eq!(buffer_size, data.len()) |
| 194 | + }, |
156 | 195 | |_| buffer_size, |
157 | 196 | ) |
158 | 197 | } |
@@ -199,7 +238,7 @@ impl Serial { |
199 | 238 | } |
200 | 239 |
|
201 | 240 | impl Write for Serial { |
202 | | - fn write_str(&mut self, s: &str) -> core::fmt::Result { |
203 | | - self.write(s.as_bytes()).map_err(|_| core::fmt::Error) |
| 241 | + fn write_str(&mut self, s: &str) -> fmt::Result { |
| 242 | + self.write(s.as_bytes()).map(|_| ()).map_err(|_| fmt::Error) |
204 | 243 | } |
205 | 244 | } |
0 commit comments