Snap for 12763142 from f4195bbb64be0163fe87b50f816e01d7bf4f12b1 to 25Q1-release

Change-Id: I299ffcbd2c4465733395d43757c6d68c40fb1fe9
diff --git a/typec_connector_class_helper/src/typec_class_utils.rs b/typec_connector_class_helper/src/typec_class_utils.rs
index 41e96a1..a4017dc 100644
--- a/typec_connector_class_helper/src/typec_class_utils.rs
+++ b/typec_connector_class_helper/src/typec_class_utils.rs
@@ -35,6 +35,8 @@
 pub const PDO_CAPABILITIES_REGEX: &str = "^(sink|source)-capabilities$";
 pub const PDO_TYPE_REGEX: &str =
     "^[0-9]+:(battery|fixed_supply|programmable_supply|variable_supply)$";
+pub const USB_PORT_REGEX: &str = "^usb[0-9]+\\-port[0-9]+$";
+pub const USB_DEVICE_REGEX: &str = "^[0-9]+\\-[0-9]+(\\.[0-9]+)*$";
 
 pub const INDENT_STEP: usize = 2;
 
@@ -567,13 +569,98 @@
     Ok(())
 }
 
+/// Prints the panel and horizontal_position in the physical_location directory of a port.
+pub fn print_physical_location<W: Write>(
+    port_path: &Path,
+    out_writer: &mut OutputWriter<W>,
+) -> Result<()> {
+    let physical_location_dir = port_path.join("physical_location");
+    if !(physical_location_dir.is_dir() | physical_location_dir.is_symlink()) {
+        bail!("{ERR_PATH_NOT_DIR_OR_SYMLINK}: {:?}", physical_location_dir);
+    }
+    out_writer.print_str_indent("physical_location")?;
+
+    out_writer.indent += INDENT_STEP;
+    out_writer.print_file_formatted(&physical_location_dir.join("panel"))?;
+    out_writer.print_file_formatted(&physical_location_dir.join("horizontal_position"))?;
+    out_writer.indent -= INDENT_STEP;
+
+    Ok(())
+}
+
+// Prints the `busnum`, `devnum`, `devpath` in the usb device directory, which are minimal
+// info needed to map to corresponding peripheral.
+pub fn print_usb_device_info<W: Write>(usb_device: &Path, out_writer: &mut OutputWriter<W>) {
+    out_writer.print_str_indent("usb_device").inspect_err(|err| warn!("{:#}", err)).ok();
+
+    out_writer.indent += INDENT_STEP;
+    out_writer
+        .print_file_formatted(&usb_device.join("busnum"))
+        .inspect_err(|err| warn!("{:#}", err))
+        .ok();
+    out_writer
+        .print_file_formatted(&usb_device.join("devnum"))
+        .inspect_err(|err| warn!("{:#}", err))
+        .ok();
+    out_writer
+        .print_file_formatted(&usb_device.join("devpath"))
+        .inspect_err(|err| warn!("{:#}", err))
+        .ok();
+    parse_dirs_and_execute(usb_device, out_writer, USB_DEVICE_REGEX, print_usb_device_info)
+        .inspect_err(|err| warn!("{:#}", err))
+        .ok();
+    out_writer.indent -= INDENT_STEP;
+}
+
+// Finds and prints information about the usb device in the usb port directory.
+pub fn print_usb_device<W: Write>(usb_port: &Path, out_writer: &mut OutputWriter<W>) {
+    let usb_device_dir = usb_port.join("device");
+    if !(usb_device_dir.is_dir() | usb_device_dir.is_symlink()) {
+        warn!("{ERR_PATH_NOT_DIR_OR_SYMLINK}: {:?}", usb_device_dir);
+        return;
+    }
+
+    print_usb_device_info(&usb_device_dir, out_writer);
+}
+
+/// Finds the type-c port's linked usb ports and print relevant usb subsystem information.
+pub fn print_usb_subsystem<W: Write>(
+    port_path: &Path,
+    out_writer: &mut OutputWriter<W>,
+) -> Result<()> {
+    let _ = parse_dirs_and_execute(port_path, out_writer, USB_PORT_REGEX, print_usb_device);
+    Ok(())
+}
+
+/// Prints `connector_id` and `status` in the drm_connector directory which is a symlink
+/// to the corresponding DP connector.
+pub fn print_drm_subsystem<W: Write>(
+    port_path: &Path,
+    out_writer: &mut OutputWriter<W>,
+) -> Result<()> {
+    let drm_path = port_path.join("drm_connector");
+    if !(drm_path.is_dir() | drm_path.is_symlink()) {
+        bail!("{ERR_PATH_NOT_DIR_OR_SYMLINK}: {:?}", drm_path);
+    }
+    out_writer.print_str_indent("dp_connector")?;
+
+    out_writer.indent += INDENT_STEP;
+    out_writer.print_file_formatted(&drm_path.join("connector_id"))?;
+    out_writer.print_file_formatted(&drm_path.join("status"))?;
+    out_writer.indent -= INDENT_STEP;
+
+    Ok(())
+}
+
 /// Prints relevant type-c connector class information for the port located at the sysfs path "port_path".
 pub fn print_port_info<W: Write>(port_path: &Path, out_writer: &mut OutputWriter<W>) {
     let _ = out_writer.print_dir_files(port_path).inspect_err(|err| warn!("{:#}", err));
     out_writer.indent += INDENT_STEP;
     let _ = print_partner(port_path, out_writer).inspect_err(|err| warn!("{:#}", err));
     print_cable(port_path, out_writer).inspect_err(|err| warn!("{:#}", err)).ok();
-
+    print_physical_location(port_path, out_writer).inspect_err(|err| warn!("{:#}", err)).ok();
+    print_usb_subsystem(port_path, out_writer).inspect_err(|err| warn!("{:#}", err)).ok();
+    print_drm_subsystem(port_path, out_writer).inspect_err(|err| warn!("{:#}", err)).ok();
     out_writer.indent -= INDENT_STEP;
 }
 
diff --git a/typec_connector_class_helper/src/typec_class_utils_tests.rs b/typec_connector_class_helper/src/typec_class_utils_tests.rs
index 1a5319d..2ddbbfd 100644
--- a/typec_connector_class_helper/src/typec_class_utils_tests.rs
+++ b/typec_connector_class_helper/src/typec_class_utils_tests.rs
@@ -76,7 +76,7 @@
             &[
                 ("uevent", "DEVTYPE=typec_port\nTYPEC_PORT=port0"),
                 ("vconn_source", "yes"),
-                ("another_file", "content"),
+                ("data_role", "[host] device"),
             ],
         ),
         (
@@ -118,20 +118,49 @@
             "port0/port0-partner/pd0/source-capabilities/2:fixed_supply",
             &[("maximum_current", "3000mA"), ("voltage", "9000mV")],
         ),
-        ("port1", &[]),
-        ("portignored", &[("ignored", "ignored content")]),
-        ("port0-cable", &[("usb_power_delivery_revision", "3.0")]),
+        ("port0/port0-cable", &[("usb_power_delivery_revision", "3.0")]),
         (
-            "port0-cable/identity",
+            "port0/port0-cable/identity",
             &[
-                ("cert_stat", "0x00000000"),
-                ("id_header", "0x1c6003f0"),
+                ("cert_stat", "0x290"),
+                ("id_header", "0x1800228a"),
                 ("product", "0x94880001"),
                 ("product_type_vdo1", "0x11082043"),
                 ("product_type_vdo2", "0x00000000"),
                 ("product_type_vdo3", "0x00000000"),
             ],
         ),
+        ("port0/port0-cable/port0-plug0", &[("number_of_alternate_modes", "2")]),
+        ("port0/port0-cable/port0-plug0/port0-plug0.0", &[("mode", "1"), ("svid", "1e4e")]),
+        (
+            "port0/port0-cable/port0-plug0/port0-plug0.0/mode1",
+            &[("active", "no"), ("vdo", "0x9031011c")],
+        ),
+        ("port0/port0-cable/port0-plug0/port0-plug0.1", &[("mode", "1"), ("svid", "8087")]),
+        (
+            "port0/port0-cable/port0-plug0/port0-plug0.1/mode1",
+            &[("active", "no"), ("vdo", "0x00030001")],
+        ),
+        ("port0/port0-cable/port0-plug0/port0-plug0.ignore", &[("ignored", "ignored content")]),
+        (
+            "port0/physical_location",
+            &[
+                ("panel", "right"),
+                ("horizontal_position", "right"),
+                ("vertical_position", "ignored"),
+            ],
+        ),
+        ("port0/usb3-port1", &[]),
+        ("port0/usb3-port1/device", &[("busnum", "3"), ("devnum", "9"), ("devpath", "1")]),
+        ("port0/usb3-port1/device/3-1.1", &[("busnum", "3"), ("devnum", "10"), ("devpath", "1.1")]),
+        ("port0/usb3-port1/device/3-1.2", &[("busnum", "3"), ("devnum", "11"), ("devpath", "1.2")]),
+        (
+            "port0/usb3-port1/device/3-1.2/3-1.2.4",
+            &[("busnum", "3"), ("devnum", "12"), ("devpath", "1.2.4")],
+        ),
+        ("port0/drm_connector", &[("connector_id", "258"), ("status", "disconnected")]),
+        ("port1", &[]),
+        ("portignored", &[("ignored", "ignored content")]),
     ])
 }
 
@@ -141,7 +170,7 @@
     let mut outp_buffer: Cursor<Vec<u8>> = Cursor::new(Vec::new());
     let mut outp_writer = OutputWriter::new(&mut outp_buffer, 0);
     let expected_outp = "port0
-  another_file: content
+  data_role: [host] device
   uevent: DEVTYPE=typec_port
   TYPEC_PORT=port0
   vconn_source: yes
@@ -210,7 +239,7 @@
     let mut outp_buffer: Cursor<Vec<u8>> = Cursor::new(Vec::new());
     let mut outp_writer = OutputWriter::new(&mut outp_buffer, 0);
     let expected_outp = "port0
-  another_file: content
+  data_role: [host] device
   uevent: DEVTYPE=typec_port
   TYPEC_PORT=port0
   vconn_source: yes
@@ -308,10 +337,153 @@
 
 #[test]
 fn test_get_cable_product_type() {
-    let temp_dir_path = create_full_typec_connector_class_temp_file_hierarchy();
+    let temp_dir_path = create_temp_dir_with_dirs_files_content(&[
+        ("port0-cable", &[("usb_power_delivery_revision", "3.0")]),
+        ("port0-cable/identity", &[("id_header", "0x1c6003f0")]),
+    ]);
     let expected_outp: ProductType = ProductType::Cable((CableType::Passive, PdRev::Pd30));
 
     let prod_type = get_cable_product_type(&temp_dir_path.join("port0-cable"));
 
     assert_eq!(prod_type, expected_outp)
 }
+
+#[test]
+fn test_full_command_output() {
+    let temp_dir_path = create_full_typec_connector_class_temp_file_hierarchy();
+    let mut outp_buffer: Cursor<Vec<u8>> = Cursor::new(Vec::new());
+    let mut outp_writer = OutputWriter::new(&mut outp_buffer, 0);
+    let expected_outp = "port0
+  data_role: [host] device
+  uevent: DEVTYPE=typec_port
+  TYPEC_PORT=port0
+  vconn_source: yes
+  port0-partner
+    number_of_alternate_modes: 1
+    usb_power_delivery_revision: 3.0
+    identity
+      id_header: 0x4c4018d1
+        USB Vendor ID: 0x18d1
+        Reserved: 0x40
+        Product Type (DFP): 0x0
+        Modal Operation Supported: 0x1
+        Product Type (UFP/Cable Plug): 0x1
+        USB Capable as a USB Device: 0x1
+        USB Capable as a USB Host: 0x0
+      cert_stat: 0x0
+        XID: 0x0
+      product: 0x50490001
+        bcdDevice: 0x1
+        USB Product ID: 0x5049
+      product_type_vdo1: 0x45800012
+        USB Highest Speed: 0x2
+        Alternate Modes: 0x2
+        Reserved: 0x20000
+        Device Capability: 0x5
+        Reserved: 0x0
+        UFP VDO Version: 0x2
+      product_type_vdo2: 0x0
+        USB3 Max Power: 0x0
+        USB3 Min Power: 0x0
+        Reserved: 0x0
+        USB4 Max Power: 0x0
+        USB4 Min Power: 0x0
+        Reserved: 0x0
+      product_type_vdo3: 0x0
+    port0-partner.0
+      description: DisplayPort
+      vdo: 0x001c0045
+      mode1
+        description: DisplayPort
+        vdo: 0x001c0045
+    pd0
+      revision: 3.0
+      sink-capabilities
+        uevent:\x20
+        1:fixed_supply
+          operational_current: 0mA
+          voltage: 5000mV
+      source-capabilities
+        uevent:\x20
+        2:fixed_supply
+          maximum_current: 3000mA
+          voltage: 9000mV
+        4:fixed_supply
+          maximum_current: 3000mA
+          voltage: 20000mV
+  port0-cable
+    usb_power_delivery_revision: 3.0
+    identity
+      id_header: 0x1800228a
+        USB Vendor ID: 0x228a
+        Reserved: 0x0
+        Product Type (DFP): 0x0
+        Modal Operation Supported: 0x0
+        Product Type (UFP/Cable Plug): 0x3
+        USB Capable as a USB Device: 0x0
+        USB Capable as a USB Host: 0x0
+      cert_stat: 0x290
+        XID: 0x290
+      product: 0x94880001
+        bcdDevice: 0x1
+        USB Product ID: 0x9488
+      product_type_vdo1: 0x11082043
+        USB Speed: 0x3
+        Reserved: 0x0
+        Vbus Current Handling: 0x2
+        Reserved: 0x0
+        Maximum Vbus Voltage: 0x0
+        Cable Termination Type: 0x0
+        Cable Latency: 0x1
+        Reserved: 0x0
+        USB Type-C Plug to USB Type: 0x2
+        Reserved: 0x0
+        VDO Version: 0x0
+        Firmware Version: 0x1
+        HW Version: 0x1
+      product_type_vdo2: 0x0
+      product_type_vdo3: 0x0
+    port0-plug0
+      number_of_alternate_modes: 2
+      port0-plug0.0
+        mode: 1
+        svid: 1e4e
+        mode1
+          active: no
+          vdo: 0x9031011c
+      port0-plug0.1
+        mode: 1
+        svid: 8087
+        mode1
+          active: no
+          vdo: 0x00030001
+  physical_location
+    panel: right
+    horizontal_position: right
+  usb_device
+    busnum: 3
+    devnum: 9
+    devpath: 1
+    usb_device
+      busnum: 3
+      devnum: 10
+      devpath: 1.1
+    usb_device
+      busnum: 3
+      devnum: 11
+      devpath: 1.2
+      usb_device
+        busnum: 3
+        devnum: 12
+        devpath: 1.2.4
+  dp_connector
+    connector_id: 258
+    status: disconnected
+port1
+";
+    let _ = parse_dirs_and_execute(&temp_dir_path, &mut outp_writer, PORT_REGEX, print_port_info);
+    assert_eq!(
+        &String::from_utf8(outp_buffer.into_inner()).expect("Cannot decode output buffer"),
+        expected_outp
+    )
+}