Skip to content

Commit

Permalink
Move packet_endpoint from Decoder to CaptureShared.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinling committed Aug 12, 2024
1 parent 2df0e0d commit a70d672
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
31 changes: 31 additions & 0 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,37 @@ impl std::fmt::Display for Bytes<'_> {
}
}

impl CaptureShared {
pub fn packet_endpoint(&self, pid: PID, packet: &[u8])
-> Result<EndpointId, EndpointKey>
{
match PacketFields::from_packet(packet) {
PacketFields::SOF(_) => Ok(FRAMING_EP_ID),
PacketFields::Token(token) => {
let dev_addr = token.device_address();
let ep_num = token.endpoint_number();
let direction = match (ep_num.0, pid) {
(0, _) => Direction::Out,
(_, PID::IN) => Direction::In,
(_, PID::OUT) => Direction::Out,
(_, PID::PING) => Direction::Out,
_ => panic!("PID {pid} does not indicate a direction")
};
let key = EndpointKey {
dev_addr,
ep_num,
direction
};
match self.endpoint_index.load().get(key) {
Some(id) => Ok(*id),
None => Err(key),
}
},
_ => Ok(INVALID_EP_ID),
}
}
}

impl CaptureWriter {
pub fn device_data(&self, id: DeviceId)
-> Result<Arc<DeviceData>, Error>
Expand Down
33 changes: 4 additions & 29 deletions src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,26 +567,12 @@ impl Decoder {
Ok(self.capture)
}

pub fn token_endpoint(&mut self, pid: PID, token: &TokenFields)
fn packet_endpoint(&mut self, pid: PID, packet: &[u8])
-> Result<EndpointId, Error>
{
let dev_addr = token.device_address();
let ep_num = token.endpoint_number();
let direction = match (ep_num.0, pid) {
(0, _) => Direction::Out,
(_, PID::IN) => Direction::In,
(_, PID::OUT) => Direction::Out,
(_, PID::PING) => Direction::Out,
_ => bail!("PID {pid} does not indicate a direction")
};
let key = EndpointKey {
dev_addr,
ep_num,
direction
};
Ok(match self.capture.shared.endpoint_index.load().get(key) {
Some(id) => *id,
None => {
Ok(match self.capture.shared.packet_endpoint(pid, packet) {
Ok(id) => id,
Err(key) => {
let id = self.add_endpoint(
key.dev_addr, key.ep_num, key.direction)?;
self.capture.shared.endpoint_index
Expand All @@ -596,17 +582,6 @@ impl Decoder {
})
}

fn packet_endpoint(&mut self, pid: PID, packet: &[u8])
-> Result<EndpointId, Error>
{
Ok(match PacketFields::from_packet(packet) {
PacketFields::SOF(_) => FRAMING_EP_ID,
PacketFields::Token(token) =>
self.token_endpoint(pid, &token)?,
_ => INVALID_EP_ID,
})
}

fn transaction_update(&mut self, packet_id: PacketId, packet: &[u8])
-> Result<(), Error>
{
Expand Down

0 comments on commit a70d672

Please sign in to comment.