View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.aix;
6   
7   import java.util.ArrayList;
8   import java.util.Arrays;
9   import java.util.Collections;
10  import java.util.List;
11  import java.util.function.Supplier;
12  
13  import oshi.annotation.concurrent.Immutable;
14  import oshi.hardware.UsbDevice;
15  import oshi.hardware.common.AbstractUsbDevice;
16  import oshi.util.Constants;
17  import oshi.util.ParseUtil;
18  
19  /**
20   * AIX Usb Device
21   */
22  @Immutable
23  public class AixUsbDevice extends AbstractUsbDevice {
24  
25      public AixUsbDevice(String name, String vendor, String vendorId, String productId, String serialNumber,
26              String uniqueDeviceId, List<UsbDevice> connectedDevices) {
27          super(name, vendor, vendorId, productId, serialNumber, uniqueDeviceId, connectedDevices);
28      }
29  
30      /**
31       * Instantiates a list of {@link oshi.hardware.UsbDevice} objects, representing devices connected via a usb port
32       * (including internal devices).
33       * <p>
34       * If the value of {@code tree} is true, the top level devices returned from this method are the USB Controllers;
35       * connected hubs and devices in its device tree share that controller's bandwidth. If the value of {@code tree} is
36       * false, USB devices (not controllers) are listed in a single flat list.
37       *
38       * @param tree  If true, returns a list of controllers, which requires recursive iteration of connected devices. If
39       *              false, returns a flat list of devices excluding controllers.
40       * @param lscfg A memoized lscfg list
41       * @return a list of {@link oshi.hardware.UsbDevice} objects.
42       */
43      public static List<UsbDevice> getUsbDevices(boolean tree, Supplier<List<String>> lscfg) {
44          List<UsbDevice> deviceList = new ArrayList<>();
45          for (String line : lscfg.get()) {
46              String s = line.trim();
47              if (s.startsWith("usb")) {
48                  String[] split = ParseUtil.whitespaces.split(s, 3);
49                  if (split.length == 3) {
50                      deviceList.add(new AixUsbDevice(split[2], Constants.UNKNOWN, Constants.UNKNOWN, Constants.UNKNOWN,
51                              Constants.UNKNOWN, split[0], Collections.emptyList()));
52                  }
53              }
54          }
55          if (tree) {
56              return Arrays.asList(new AixUsbDevice("USB Controller", "", "0000", "0000", "", "", deviceList));
57          }
58          return deviceList;
59      }
60  }