View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.windows;
6   
7   import static oshi.util.Memoizer.memoize;
8   
9   import java.util.function.Supplier;
10  
11  import com.sun.jna.platform.win32.COM.WbemcliUtil.WmiResult;
12  
13  import oshi.annotation.concurrent.Immutable;
14  import oshi.driver.windows.wmi.Win32Bios;
15  import oshi.driver.windows.wmi.Win32Bios.BiosSerialProperty;
16  import oshi.driver.windows.wmi.Win32ComputerSystem;
17  import oshi.driver.windows.wmi.Win32ComputerSystem.ComputerSystemProperty;
18  import oshi.driver.windows.wmi.Win32ComputerSystemProduct;
19  import oshi.driver.windows.wmi.Win32ComputerSystemProduct.ComputerSystemProductProperty;
20  import oshi.hardware.Baseboard;
21  import oshi.hardware.Firmware;
22  import oshi.hardware.common.AbstractComputerSystem;
23  import oshi.util.Constants;
24  import oshi.util.Util;
25  import oshi.util.platform.windows.WmiUtil;
26  import oshi.util.tuples.Pair;
27  
28  /**
29   * Hardware data obtained from WMI.
30   */
31  @Immutable
32  final class WindowsComputerSystem extends AbstractComputerSystem {
33  
34      private final Supplier<Pair<String, String>> manufacturerModel = memoize(
35              WindowsComputerSystem::queryManufacturerModel);
36      private final Supplier<Pair<String, String>> serialNumberUUID = memoize(
37              WindowsComputerSystem::querySystemSerialNumberUUID);
38  
39      @Override
40      public String getManufacturer() {
41          return manufacturerModel.get().getA();
42      }
43  
44      @Override
45      public String getModel() {
46          return manufacturerModel.get().getB();
47      }
48  
49      @Override
50      public String getSerialNumber() {
51          return serialNumberUUID.get().getA();
52      }
53  
54      @Override
55      public String getHardwareUUID() {
56          return serialNumberUUID.get().getB();
57      }
58  
59      @Override
60      public Firmware createFirmware() {
61          return new WindowsFirmware();
62      }
63  
64      @Override
65      public Baseboard createBaseboard() {
66          return new WindowsBaseboard();
67      }
68  
69      private static Pair<String, String> queryManufacturerModel() {
70          String manufacturer = null;
71          String model = null;
72          WmiResult<ComputerSystemProperty> win32ComputerSystem = Win32ComputerSystem.queryComputerSystem();
73          if (win32ComputerSystem.getResultCount() > 0) {
74              manufacturer = WmiUtil.getString(win32ComputerSystem, ComputerSystemProperty.MANUFACTURER, 0);
75              model = WmiUtil.getString(win32ComputerSystem, ComputerSystemProperty.MODEL, 0);
76          }
77          return new Pair<>(Util.isBlank(manufacturer) ? Constants.UNKNOWN : manufacturer,
78                  Util.isBlank(model) ? Constants.UNKNOWN : model);
79      }
80  
81      private static Pair<String, String> querySystemSerialNumberUUID() {
82          String serialNumber = null;
83          String uuid = null;
84          WmiResult<ComputerSystemProductProperty> win32ComputerSystemProduct = Win32ComputerSystemProduct
85                  .queryIdentifyingNumberUUID();
86          if (win32ComputerSystemProduct.getResultCount() > 0) {
87              serialNumber = WmiUtil.getString(win32ComputerSystemProduct,
88                      ComputerSystemProductProperty.IDENTIFYINGNUMBER, 0);
89              uuid = WmiUtil.getString(win32ComputerSystemProduct, ComputerSystemProductProperty.UUID, 0);
90          }
91          if (Util.isBlank(serialNumber)) {
92              serialNumber = querySerialFromBios();
93          }
94          if (Util.isBlank(serialNumber)) {
95              serialNumber = Constants.UNKNOWN;
96          }
97          if (Util.isBlank(uuid)) {
98              uuid = Constants.UNKNOWN;
99          }
100         return new Pair<>(serialNumber, uuid);
101     }
102 
103     private static String querySerialFromBios() {
104         WmiResult<BiosSerialProperty> serialNum = Win32Bios.querySerialNumber();
105         if (serialNum.getResultCount() > 0) {
106             return WmiUtil.getString(serialNum, BiosSerialProperty.SERIALNUMBER, 0);
107         }
108         return null;
109     }
110 }