Skip to content

Commit

Permalink
refactor handle_command to avoid passing in uinput device
Browse files Browse the repository at this point in the history
  • Loading branch information
kshen0 committed Feb 15, 2024
1 parent 43ef878 commit 7f21a30
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions apps/mark-scan/accessible-controller/src/controllerd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fn send_key(device: &mut Device, key: keyboard::Key) -> Result<(), CommandError>
Ok(())
}

fn handle_command(device: &mut Device, data: &[u8]) -> Result<(), CommandError> {
fn handle_command(data: &[u8]) -> Result<Option<keyboard::Key>, CommandError> {
let ButtonStatusCommand { button, action } = data.try_into()?;

let key: keyboard::Key;
Expand Down Expand Up @@ -252,11 +252,10 @@ fn handle_command(device: &mut Device, data: &[u8]) -> Result<(), CommandError>
},
Action::Released => {
// Button release is a no-op since we already sent the keypress event
return Ok(());
return Ok(None);
}
}

send_key(device, key)
Ok(Some(key))
}

fn validate_connection(port: &mut Box<dyn SerialPort>) -> Result<(), io::Error> {
Expand Down Expand Up @@ -394,17 +393,23 @@ fn main() {
exit(0);
}
match port.read(serial_buf.as_mut_slice()) {
Ok(size) => {
if let Err(e) = handle_command(&mut device, &serial_buf[..size]) {
log!(
event_id: EventId::UnknownError,
message: format!(
"Unexpected error when handling controller command: {e}"
),
event_type: EventType::SystemStatus
);
Ok(size) => match handle_command(&serial_buf[..size]) {
Ok(key_option) => {
if key_option.is_some() {
let key = key_option.unwrap();
if let Err(err) = send_key(&mut device, key) {
log!(EventId::UnknownError, "Error sending key: {err}");
}
}
}
}
Err(err) => log!(
event_id: EventId::UnknownError,
message: format!(
"Unexpected error when handling controller command: {err}"
),
event_type: EventType::SystemStatus
),
},
// Timeout error just means no event was sent in the current polling period
Err(ref e) if e.kind() == io::ErrorKind::TimedOut => (),
Err(e) => {
Expand Down Expand Up @@ -437,9 +442,8 @@ mod tests {

#[test]
fn test_handle_command_packet_length_error() {
let mut device = create_virtual_device();
let bad_data = [0x01];
match handle_command(&mut device, &bad_data) {
match handle_command(&bad_data) {
Err(CommandError::UnexpectedPacketSize(size)) => assert_eq!(size, 1),
result => panic!("Unexpected result: {result:?}"),
}
Expand All @@ -448,9 +452,8 @@ mod tests {
#[test]
fn test_handle_command_data_length() {
let bad_data_length: u8 = 0x03;
let mut device = create_virtual_device();
let bad_data = [0x30, 0x00, bad_data_length, 0x00, 0x00, 0x00, 0x00];
match handle_command(&mut device, &bad_data) {
match handle_command(&bad_data) {
Err(CommandError::UnexpectedDataLength(length)) => {
assert_eq!(length, bad_data_length as u16)
}
Expand All @@ -460,7 +463,6 @@ mod tests {

#[test]
fn test_handle_command_success() {
let mut device = create_virtual_device();
// In prod we wait 1s for the device to register.
// We can afford to be riskier to speed up tests.
thread::sleep(DEVICE_WAIT_DURATION);
Expand All @@ -474,6 +476,8 @@ mod tests {
0xc8,
0x37,
];
handle_command(&mut device, &data).unwrap();
let key_option = handle_command(&data).unwrap();
assert!(key_option.is_some());
assert_eq!(key_option.unwrap(), keyboard::Key::R);
}
}

0 comments on commit 7f21a30

Please sign in to comment.