Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2021 The Chromium Authors |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // The System Management Controller (SMC) is a hardware component that controls |
| 6 | // the power functions of Intel-based Macs. This file defines a class to read |
| 7 | // known SMC keys. |
| 8 | |
| 9 | #ifndef COMPONENTS_POWER_METRICS_SMC_MAC_H_ |
| 10 | #define COMPONENTS_POWER_METRICS_SMC_MAC_H_ |
| 11 | |
| 12 | #import <Foundation/Foundation.h> |
| 13 | |
| 14 | #include <memory> |
Arthur Sonzogni | 4059441 | 2024-02-26 15:49:43 | [diff] [blame] | 15 | #include <optional> |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 16 | |
Francois Doray | 04f94177 | 2021-12-02 22:04:50 | [diff] [blame] | 17 | #include "base/containers/flat_map.h" |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 18 | #include "base/mac/scoped_ioobject.h" |
| 19 | #include "components/power_metrics/smc_internal_types_mac.h" |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 20 | |
| 21 | namespace power_metrics { |
| 22 | |
| 23 | class SMCReader { |
| 24 | public: |
| 25 | // Creates an SMC Reader. Returns nullptr in case of failure. |
| 26 | static std::unique_ptr<SMCReader> Create(); |
| 27 | |
| 28 | virtual ~SMCReader(); |
| 29 | |
Francois Doray | 04f94177 | 2021-12-02 22:04:50 | [diff] [blame] | 30 | // Returns the value of a key, or nullopt if not available. |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 31 | // Virtual for testing. |
Arthur Sonzogni | 4059441 | 2024-02-26 15:49:43 | [diff] [blame] | 32 | virtual std::optional<double> ReadKey(SMCKeyIdentifier identifier); |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 33 | |
| 34 | protected: |
| 35 | explicit SMCReader(base::mac::ScopedIOObject<io_object_t> connect); |
| 36 | |
| 37 | private: |
| 38 | class SMCKey { |
| 39 | public: |
| 40 | SMCKey(base::mac::ScopedIOObject<io_object_t> connect, |
| 41 | SMCKeyIdentifier key_identifier); |
Francois Doray | 04f94177 | 2021-12-02 22:04:50 | [diff] [blame] | 42 | SMCKey(SMCKey&&); |
| 43 | SMCKey& operator=(SMCKey&&); |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 44 | ~SMCKey(); |
| 45 | |
| 46 | bool Exists() const; |
Arthur Sonzogni | 4059441 | 2024-02-26 15:49:43 | [diff] [blame] | 47 | std::optional<double> Read(); |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 48 | |
| 49 | private: |
| 50 | bool CallSMCFunction(uint8_t function, SMCParamStruct* out); |
| 51 | |
| 52 | base::mac::ScopedIOObject<io_object_t> connect_; |
Francois Doray | 04f94177 | 2021-12-02 22:04:50 | [diff] [blame] | 53 | SMCKeyIdentifier key_identifier_; |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 54 | SMCKeyInfoData key_info_; |
| 55 | }; |
| 56 | |
Francois Doray | 04f94177 | 2021-12-02 22:04:50 | [diff] [blame] | 57 | base::mac::ScopedIOObject<io_object_t> connect_; |
| 58 | base::flat_map<SMCKeyIdentifier, SMCKey> keys_; |
Francois Doray | 9b7f7b5 | 2021-11-29 21:22:10 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | } // namespace power_metrics |
| 62 | |
| 63 | #endif // COMPONENTS_POWER_METRICS_SMC_MAC_H_ |