Skip to content

Commit

Permalink
Merge branch 'nusb' of github.com:tuna-f1sh/cyme into nusb
Browse files Browse the repository at this point in the history
  • Loading branch information
tuna-f1sh committed Oct 10, 2024
2 parents 371eaeb + 5e9a492 commit 41d5a5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
21 changes: 13 additions & 8 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,11 @@ impl Block<BusBlocks, Bus> for BusBlocks {
.flat_map(|d| d.host_controller_device.as_ref().map(|v| v.len()))
.max()
.unwrap_or(0),
BusBlocks::PortPath => d.iter().map(|d| d.path().len()).max().unwrap_or(0),
BusBlocks::PortPath => d
.iter()
.map(|d| d.path().unwrap_or("-".to_string()).len())
.max()
.unwrap_or(0),
_ => self.block_length().len(),
}
}
Expand Down Expand Up @@ -987,7 +991,10 @@ impl Block<BusBlocks, Bus> for BusBlocks {
settings: &PrintSettings,
) -> Option<String> {
match self {
BusBlocks::BusNumber => Some(format!("{:3}", bus.get_bus_number())),
BusBlocks::BusNumber => bus
.get_bus_number()
.map(|v| format!("{:3}", v))
.or(Some("---".to_string())),
BusBlocks::Icon => settings
.icons
.as_ref()
Expand Down Expand Up @@ -1023,12 +1030,10 @@ impl Block<BusBlocks, Bus> for BusBlocks {
Some(v) => format!("{:pad$}", v, pad = pad.get(self).unwrap_or(&0)),
None => format!("{:pad$}", "-", pad = pad.get(self).unwrap_or(&0)),
}),
BusBlocks::PortPath => Some(format!(
"{:pad$}",
bus.path(),
pad = pad.get(self).unwrap_or(&0)
)),
// _ => None,
BusBlocks::PortPath => Some(match bus.path() {
Some(v) => format!("{:pad$}", v, pad = pad.get(self).unwrap_or(&0)),
None => format!("{:pad$}", "-", pad = pad.get(self).unwrap_or(&0)),
}),
}
}

Expand Down
34 changes: 17 additions & 17 deletions src/profiler/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,23 @@ impl Bus {
}

/// usb_bus_number is not always present in system_profiler output so try to get from first device instead
pub fn get_bus_number(&self) -> u8 {
self.usb_bus_number.unwrap_or(
pub fn get_bus_number(&self) -> Option<u8> {
self.usb_bus_number.or_else(|| {
self.devices
.as_ref()
.and_then(|d| d.first().map(|dd| dd.location_id.bus))
.unwrap_or(0xFF),
)
})
}

/// syspath style path to bus
pub fn path(&self) -> String {
get_trunk_path(self.get_bus_number(), &[])
pub fn path(&self) -> Option<String> {
self.get_bus_number().map(|n| get_trunk_path(n, &[]))
}

/// sysfs style path to bus interface
pub fn interface(&self) -> String {
get_interface_path(self.get_bus_number(), &Vec::new(), 1, 0)
pub fn interface(&self) -> Option<String> {
self.get_bus_number()
.map(|n| get_interface_path(n, &Vec::new(), 1, 0))
}

/// Remove the root_hub if existing in bus
Expand All @@ -295,12 +295,12 @@ impl Bus {

/// Gets the device that is the root_hub associated with this bus - Linux only but exists in case of using --from-json
pub fn get_root_hub_device(&self) -> Option<&Device> {
self.get_node(&self.interface())
self.interface().and_then(|i| self.get_node(&i))
}

/// Gets a mutable device that is the root_hub associated with this bus - Linux only but exists in case of using --from-json
pub fn get_root_hub_device_mut(&mut self) -> Option<&mut Device> {
self.get_node_mut(&self.interface())
self.interface().and_then(|i| self.get_node_mut(&i))
}

/// Search for [`Device`] in branches of bus and return reference
Expand Down Expand Up @@ -335,7 +335,7 @@ impl Bus {
pub fn to_lsusb_string(&self) -> String {
format!(
"Bus {:03} Device 000: ID {:04x}:{:04x} {} {}",
self.get_bus_number(),
self.get_bus_number().unwrap_or(0xff),
self.pci_vendor.unwrap_or(0xffff),
self.pci_device.unwrap_or(0xffff),
self.name,
Expand Down Expand Up @@ -375,7 +375,7 @@ impl Bus {
Vec::from([(
format!(
"Bus {:02}.Port 1: Dev 1, Class=root_hub, Driver={}, {}",
self.get_bus_number(),
self.get_bus_number().unwrap_or(0xff),
driver,
speed
),
Expand All @@ -388,16 +388,16 @@ impl Bus {
),
format!(
"/sys/bus/usb/devices/usb{} {}",
self.get_bus_number(),
get_dev_path(self.get_bus_number(), None)
self.get_bus_number().unwrap_or(0xff),
get_dev_path(self.get_bus_number().unwrap_or(0xff), None)
),
)])
} else {
log::warn!("Failed to get root_device in bus");
Vec::from([(
format!(
"Bus {:02}.Port 1: Dev 1, Class=root_hub, Driver=[none],",
self.get_bus_number(),
self.get_bus_number().unwrap_or(0xff),
),
format!(
"ID {:04x}:{:04x} {} {}",
Expand All @@ -408,8 +408,8 @@ impl Bus {
),
format!(
"/sys/bus/usb/devices/usb{} {}",
self.get_bus_number(),
get_dev_path(self.get_bus_number(), None)
self.get_bus_number().unwrap_or(0xff),
get_dev_path(self.get_bus_number().unwrap_or(0xff), None)
),
)])
}
Expand Down

0 comments on commit 41d5a5f

Please sign in to comment.