View Javadoc
1   /*
2    * Copyright 2016-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.mac;
6   
7   import static oshi.util.Memoizer.memoize;
8   
9   import java.nio.charset.StandardCharsets;
10  import java.util.function.Supplier;
11  
12  import com.sun.jna.Native;
13  import com.sun.jna.platform.mac.IOKit.IORegistryEntry;
14  import com.sun.jna.platform.mac.IOKitUtil;
15  
16  import oshi.annotation.concurrent.Immutable;
17  import oshi.hardware.Baseboard;
18  import oshi.hardware.Firmware;
19  import oshi.hardware.common.AbstractComputerSystem;
20  import oshi.util.Constants;
21  import oshi.util.Util;
22  import oshi.util.tuples.Quartet;
23  
24  /**
25   * Hardware data obtained from ioreg.
26   */
27  @Immutable
28  final class MacComputerSystem extends AbstractComputerSystem {
29  
30      private final Supplier<Quartet<String, String, String, String>> manufacturerModelSerialUUID = memoize(
31              MacComputerSystem::platformExpert);
32  
33      @Override
34      public String getManufacturer() {
35          return manufacturerModelSerialUUID.get().getA();
36      }
37  
38      @Override
39      public String getModel() {
40          return manufacturerModelSerialUUID.get().getB();
41      }
42  
43      @Override
44      public String getSerialNumber() {
45          return manufacturerModelSerialUUID.get().getC();
46      }
47  
48      @Override
49      public String getHardwareUUID() {
50          return manufacturerModelSerialUUID.get().getD();
51      }
52  
53      @Override
54      public Firmware createFirmware() {
55          return new MacFirmware();
56      }
57  
58      @Override
59      public Baseboard createBaseboard() {
60          return new MacBaseboard();
61      }
62  
63      private static Quartet<String, String, String, String> platformExpert() {
64          String manufacturer = null;
65          String model = null;
66          String serialNumber = null;
67          String uuid = null;
68          IORegistryEntry platformExpert = IOKitUtil.getMatchingService("IOPlatformExpertDevice");
69          if (platformExpert != null) {
70              byte[] data = platformExpert.getByteArrayProperty("manufacturer");
71              if (data != null) {
72                  manufacturer = Native.toString(data, StandardCharsets.UTF_8);
73              }
74              data = platformExpert.getByteArrayProperty("model");
75              if (data != null) {
76                  model = Native.toString(data, StandardCharsets.UTF_8);
77              }
78              serialNumber = platformExpert.getStringProperty("IOPlatformSerialNumber");
79              uuid = platformExpert.getStringProperty("IOPlatformUUID");
80              platformExpert.release();
81          }
82          return new Quartet<>(Util.isBlank(manufacturer) ? "Apple Inc." : manufacturer,
83                  Util.isBlank(model) ? Constants.UNKNOWN : model,
84                  Util.isBlank(serialNumber) ? Constants.UNKNOWN : serialNumber,
85                  Util.isBlank(uuid) ? Constants.UNKNOWN : uuid);
86      }
87  }