smc_mac.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. // The System Management Controller (SMC) is a hardware component that controls
  5. // the power functions of Intel-based Macs. This file defines a class to read
  6. // known SMC keys.
  7. #ifndef COMPONENTS_POWER_METRICS_SMC_MAC_H_
  8. #define COMPONENTS_POWER_METRICS_SMC_MAC_H_
  9. #import <Foundation/Foundation.h>
  10. #include <memory>
  11. #include "base/containers/flat_map.h"
  12. #include "base/mac/scoped_ioobject.h"
  13. #include "components/power_metrics/smc_internal_types_mac.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace power_metrics {
  16. class SMCReader {
  17. public:
  18. // Creates an SMC Reader. Returns nullptr in case of failure.
  19. static std::unique_ptr<SMCReader> Create();
  20. virtual ~SMCReader();
  21. // Returns the value of a key, or nullopt if not available.
  22. // Virtual for testing.
  23. virtual absl::optional<double> ReadKey(SMCKeyIdentifier identifier);
  24. protected:
  25. explicit SMCReader(base::mac::ScopedIOObject<io_object_t> connect);
  26. private:
  27. class SMCKey {
  28. public:
  29. SMCKey(base::mac::ScopedIOObject<io_object_t> connect,
  30. SMCKeyIdentifier key_identifier);
  31. SMCKey(SMCKey&&);
  32. SMCKey& operator=(SMCKey&&);
  33. ~SMCKey();
  34. bool Exists() const;
  35. absl::optional<double> Read();
  36. private:
  37. bool CallSMCFunction(uint8_t function, SMCParamStruct* out);
  38. base::mac::ScopedIOObject<io_object_t> connect_;
  39. SMCKeyIdentifier key_identifier_;
  40. SMCKeyInfoData key_info_;
  41. };
  42. base::mac::ScopedIOObject<io_object_t> connect_;
  43. base::flat_map<SMCKeyIdentifier, SMCKey> keys_;
  44. };
  45. } // namespace power_metrics
  46. #endif // COMPONENTS_POWER_METRICS_SMC_MAC_H_