View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.solaris;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   
10  import oshi.annotation.concurrent.Immutable;
11  import oshi.hardware.GraphicsCard;
12  import oshi.hardware.common.AbstractGraphicsCard;
13  import oshi.util.Constants;
14  import oshi.util.ExecutingCommand;
15  import oshi.util.ParseUtil;
16  
17  /**
18   * Graphics Card info obtained from prtconf
19   */
20  @Immutable
21  final class SolarisGraphicsCard extends AbstractGraphicsCard {
22  
23      private static final String PCI_CLASS_DISPLAY = "0003";
24  
25      /**
26       * Constructor for SolarisGraphicsCard
27       *
28       * @param name        The name
29       * @param deviceId    The device ID
30       * @param vendor      The vendor
31       * @param versionInfo The version info
32       * @param vram        The VRAM
33       */
34      SolarisGraphicsCard(String name, String deviceId, String vendor, String versionInfo, long vram) {
35          super(name, deviceId, vendor, versionInfo, vram);
36      }
37  
38      /**
39       * public method used by {@link oshi.hardware.common.AbstractHardwareAbstractionLayer} to access the graphics cards.
40       *
41       * @return List of {@link oshi.hardware.platform.unix.solaris.SolarisGraphicsCard} objects.
42       */
43      public static List<GraphicsCard> getGraphicsCards() {
44          List<GraphicsCard> cardList = new ArrayList<>();
45          // Enumerate all devices and add if required
46          List<String> devices = ExecutingCommand.runNative("prtconf -pv");
47          if (devices.isEmpty()) {
48              return cardList;
49          }
50          String name = "";
51          String vendorId = "";
52          String productId = "";
53          String classCode = "";
54          List<String> versionInfoList = new ArrayList<>();
55          for (String line : devices) {
56              // Node 0x... identifies start of a new device. Save previous if it's a graphics
57              // card
58              if (line.contains("Node 0x")) {
59                  if (PCI_CLASS_DISPLAY.equals(classCode)) {
60                      cardList.add(new SolarisGraphicsCard(name.isEmpty() ? Constants.UNKNOWN : name,
61                              productId.isEmpty() ? Constants.UNKNOWN : productId,
62                              vendorId.isEmpty() ? Constants.UNKNOWN : vendorId,
63                              versionInfoList.isEmpty() ? Constants.UNKNOWN : String.join(", ", versionInfoList), 0L));
64                  }
65                  // Reset strings
66                  name = "";
67                  vendorId = Constants.UNKNOWN;
68                  productId = Constants.UNKNOWN;
69                  classCode = "";
70                  versionInfoList.clear();
71              } else {
72                  String[] split = line.trim().split(":", 2);
73                  if (split.length == 2) {
74                      if (split[0].equals("model")) {
75                          // This is preferred, always set it
76                          name = ParseUtil.getSingleQuoteStringValue(line);
77                      } else if (split[0].equals("name")) {
78                          // Name is backup for model if model doesn't exist, so only
79                          // put if name blank
80                          if (name.isEmpty()) {
81                              name = ParseUtil.getSingleQuoteStringValue(line);
82                          }
83                      } else if (split[0].equals("vendor-id")) {
84                          // Format: vendor-id: 00008086
85                          vendorId = "0x" + line.substring(line.length() - 4);
86                      } else if (split[0].equals("device-id")) {
87                          // Format: device-id: 00002440
88                          productId = "0x" + line.substring(line.length() - 4);
89                      } else if (split[0].equals("revision-id")) {
90                          // Format: revision-id: 00000002
91                          versionInfoList.add(line.trim());
92                      } else if (split[0].equals("class-code")) {
93                          // Format: 00030000
94                          // Display class is 0003xx, first 6 bytes of this code
95                          classCode = line.substring(line.length() - 8, line.length() - 4);
96                      }
97                  }
98              }
99          }
100         // In case we reached end before saving
101         if (PCI_CLASS_DISPLAY.equals(classCode)) {
102             cardList.add(new SolarisGraphicsCard(name.isEmpty() ? Constants.UNKNOWN : name,
103                     productId.isEmpty() ? Constants.UNKNOWN : productId,
104                     vendorId.isEmpty() ? Constants.UNKNOWN : vendorId,
105                     versionInfoList.isEmpty() ? Constants.UNKNOWN : String.join(", ", versionInfoList), 0L));
106         }
107         return cardList;
108     }
109 }