Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix USBIP_CMD_UNLINK behaviour #43

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 4 additions & 58 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,32 +388,13 @@ pub async fn handler<T: AsyncReadExt + AsyncWriteExt + Unpin>(
}
UsbIpCommand::UsbIpCmdUnlink {
mut header,
unlink_seqnum: _,
unlink_seqnum,
} => {
trace!("Got USBIP_CMD_UNLINK");

std::mem::drop(used_devices);

let mut used_devices = server.used_devices.write().await;
let mut available_devices = server.available_devices.write().await;

let dev = current_import_device_id
.clone()
.and_then(|ref k| used_devices.remove(k));
trace!("Got USBIP_CMD_UNLINK for {:10x?}", unlink_seqnum);

header.command = USBIP_RET_UNLINK.into();

let res = match dev {
Some(dev) => {
available_devices.push(dev);
current_import_device_id = None;
UsbIpResponse::usbip_ret_unlink_success(&header)
}
None => {
warn!("Device not found");
UsbIpResponse::usbip_ret_unlink_fail(&header)
}
};
let res = UsbIpResponse::usbip_ret_unlink_success(&header);
res.write_to_socket(socket).await?;
trace!("Sent USBIP_RET_UNLINK");
}
Expand Down Expand Up @@ -452,7 +433,7 @@ mod tests {

use super::*;
use crate::{
usbip_protocol::{UsbIpHeaderBasic, USBIP_CMD_SUBMIT, USBIP_CMD_UNLINK},
usbip_protocol::{UsbIpHeaderBasic, USBIP_CMD_SUBMIT},
util::tests::*,
};

Expand Down Expand Up @@ -658,41 +639,6 @@ mod tests {
assert_eq!(result, 1);
}

#[tokio::test]
async fn device_gets_released_on_cmd_unlink() {
setup_test_logger();
let server_ = Arc::new(new_server_with_single_device());

let addr = get_free_address().await;
tokio::spawn(server(addr, server_.clone()));

let mut connection = poll_connect(addr).await;

let result = attach_device(&mut connection, SINGLE_DEVICE_BUSID).await;
assert_eq!(result, 0);

let unlink_req = UsbIpCommand::UsbIpCmdUnlink {
header: UsbIpHeaderBasic {
command: USBIP_CMD_UNLINK.into(),
seqnum: 1,
devid: 0,
direction: 0,
ep: 0,
},
unlink_seqnum: 0,
}
.to_bytes();

connection.write_all(unlink_req.as_slice()).await.unwrap();
connection.read_exact(&mut [0; 4 * 5]).await.unwrap();
let result = connection.read_u32().await.unwrap();
connection.read_exact(&mut [0; 4 * 6]).await.unwrap();
assert_eq!(result, 0);

let result = attach_device(&mut connection, SINGLE_DEVICE_BUSID).await;
assert_eq!(result, 0);
}

#[tokio::test]
async fn device_gets_released_on_closed_socket() {
setup_test_logger();
Expand Down
Loading