blob: af3e19f2c4fe3b5bd67269509c71bd7f57e62a73 [file] [log] [blame]
Francois Doray9b7f7b52021-11-29 21:22:101// 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
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>
15
Francois Doray04f941772021-12-02 22:04:5016#include "base/containers/flat_map.h"
Francois Doray9b7f7b52021-11-29 21:22:1017#include "base/mac/scoped_ioobject.h"
18#include "components/power_metrics/smc_internal_types_mac.h"
19#include "third_party/abseil-cpp/absl/types/optional.h"
20
21namespace power_metrics {
22
23class 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 Doray04f941772021-12-02 22:04:5030 // Returns the value of a key, or nullopt if not available.
Francois Doray9b7f7b52021-11-29 21:22:1031 // Virtual for testing.
Francois Doray04f941772021-12-02 22:04:5032 virtual absl::optional<double> ReadKey(SMCKeyIdentifier identifier);
Francois Doray9b7f7b52021-11-29 21:22:1033
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 Doray04f941772021-12-02 22:04:5042 SMCKey(SMCKey&&);
43 SMCKey& operator=(SMCKey&&);
Francois Doray9b7f7b52021-11-29 21:22:1044 ~SMCKey();
45
46 bool Exists() const;
47 absl::optional<double> Read();
48
49 private:
50 bool CallSMCFunction(uint8_t function, SMCParamStruct* out);
51
52 base::mac::ScopedIOObject<io_object_t> connect_;
Francois Doray04f941772021-12-02 22:04:5053 SMCKeyIdentifier key_identifier_;
Francois Doray9b7f7b52021-11-29 21:22:1054 SMCKeyInfoData key_info_;
55 };
56
Francois Doray04f941772021-12-02 22:04:5057 base::mac::ScopedIOObject<io_object_t> connect_;
58 base::flat_map<SMCKeyIdentifier, SMCKey> keys_;
Francois Doray9b7f7b52021-11-29 21:22:1059};
60
61} // namespace power_metrics
62
63#endif // COMPONENTS_POWER_METRICS_SMC_MAC_H_