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.common.AbstractBaseboard;
18  import oshi.util.Constants;
19  import oshi.util.Util;
20  import oshi.util.tuples.Quartet;
21  
22  /**
23   * Baseboard data obtained from ioreg
24   */
25  @Immutable
26  final class MacBaseboard extends AbstractBaseboard {
27  
28      private final Supplier<Quartet<String, String, String, String>> manufModelVersSerial = memoize(
29              MacBaseboard::queryPlatform);
30  
31      @Override
32      public String getManufacturer() {
33          return manufModelVersSerial.get().getA();
34      }
35  
36      @Override
37      public String getModel() {
38          return manufModelVersSerial.get().getB();
39      }
40  
41      @Override
42      public String getVersion() {
43          return manufModelVersSerial.get().getC();
44      }
45  
46      @Override
47      public String getSerialNumber() {
48          return manufModelVersSerial.get().getD();
49      }
50  
51      private static Quartet<String, String, String, String> queryPlatform() {
52          String manufacturer = null;
53          String model = null;
54          String version = null;
55          String serialNumber = null;
56  
57          IORegistryEntry platformExpert = IOKitUtil.getMatchingService("IOPlatformExpertDevice");
58          if (platformExpert != null) {
59              byte[] data = platformExpert.getByteArrayProperty("manufacturer");
60              if (data != null) {
61                  manufacturer = Native.toString(data, StandardCharsets.UTF_8);
62              }
63              data = platformExpert.getByteArrayProperty("board-id");
64              if (data != null) {
65                  model = Native.toString(data, StandardCharsets.UTF_8);
66              }
67              if (Util.isBlank(model)) {
68                  data = platformExpert.getByteArrayProperty("model-number");
69                  if (data != null) {
70                      model = Native.toString(data, StandardCharsets.UTF_8);
71                  }
72              }
73              data = platformExpert.getByteArrayProperty("version");
74              if (data != null) {
75                  version = Native.toString(data, StandardCharsets.UTF_8);
76              }
77              data = platformExpert.getByteArrayProperty("mlb-serial-number");
78              if (data != null) {
79                  serialNumber = Native.toString(data, StandardCharsets.UTF_8);
80              }
81              if (Util.isBlank(serialNumber)) {
82                  serialNumber = platformExpert.getStringProperty("IOPlatformSerialNumber");
83              }
84              platformExpert.release();
85          }
86          return new Quartet<>(Util.isBlank(manufacturer) ? "Apple Inc." : manufacturer,
87                  Util.isBlank(model) ? Constants.UNKNOWN : model, Util.isBlank(version) ? Constants.UNKNOWN : version,
88                  Util.isBlank(serialNumber) ? Constants.UNKNOWN : serialNumber);
89      }
90  }