RADLib
RADical C++ application framework
RADPower.h
Go to the documentation of this file.
1 #ifndef RADPOWER_H
2 #define RADPOWER_H
3 
9 #pragma once
10 
11 #include <RADCore/RADCore.h>
12 
13 #include <string>
14 #include <vector>
15 
16 namespace RADPower {
17 
19  struct PowerPaths {
21  std::string sysClassPowerSupply = "/sys/class/power_supply";
23  std::string sysClassThermal = "/sys/class/thermal";
25  std::string sysDevicesCpu = "/sys/devices/system/cpu";
26  };
27 
29  struct PowerSupplyInfo {
31  std::string name;
33  std::string type;
35  std::string status;
37  int capacityPercent = -1;
38  };
39 
41  struct ThermalZoneInfo {
43  std::string name;
45  std::string type;
47  double temperatureC = 0.0;
48  };
49 
51  struct CpuGovernorInfo {
53  std::string cpu;
55  std::string governor;
57  std::vector<std::string> availableGovernors;
58  };
59 
61  enum class PowerAction {
63  Suspend,
65  Reboot,
67  Shutdown
68  };
69 
71  struct PowerActionPlan {
75  std::string command;
77  bool executed = false;
79  std::string error;
80  };
81 
83  class PowerManager {
84  public:
86  explicit PowerManager(PowerPaths paths = {});
88  std::vector<PowerSupplyInfo> powerSupplies() const;
90  std::vector<ThermalZoneInfo> thermalZones() const;
92  std::vector<CpuGovernorInfo> cpuGovernors() const;
94  bool setCpuGovernor(const std::string& cpu, const std::string& governor, std::string* error = nullptr) const;
96  PowerActionPlan requestAction(PowerAction action, bool execute = false) const;
98  const PowerPaths& paths() const;
99 
100  private:
101  PowerPaths paths_;
102  };
103 
104 } // namespace RADPower
105 
106 #endif
Power manager with read-only status helpers and explicit dangerous actions.
Definition: RADPower.h:83
std::vector< PowerSupplyInfo > powerSupplies() const
Lists power supplies.
PowerActionPlan requestAction(PowerAction action, bool execute=false) const
Prepares or executes a system power action. execute must be true to mutate host state.
std::vector< CpuGovernorInfo > cpuGovernors() const
Lists CPU governors.
bool setCpuGovernor(const std::string &cpu, const std::string &governor, std::string *error=nullptr) const
Sets CPU governor for one CPU directory.
const PowerPaths & paths() const
Returns configured paths.
PowerManager(PowerPaths paths={})
Creates a power manager using paths.
std::vector< ThermalZoneInfo > thermalZones() const
Lists thermal zones.
Definition: RADPower.h:16
PowerAction
Guarded system power action.
Definition: RADPower.h:61
@ Shutdown
Shut down the system.
@ Reboot
Reboot the system.
@ Suspend
Suspend the system.
CPU governor snapshot.
Definition: RADPower.h:51
std::string governor
Current governor.
Definition: RADPower.h:55
std::vector< std::string > availableGovernors
Available governors.
Definition: RADPower.h:57
std::string cpu
CPU directory name.
Definition: RADPower.h:53
Prepared action result.
Definition: RADPower.h:71
std::string error
Error text when execution failed.
Definition: RADPower.h:79
std::string command
Shell command that would be executed.
Definition: RADPower.h:75
bool executed
True when command was actually executed.
Definition: RADPower.h:77
PowerAction action
Action requested.
Definition: RADPower.h:73
Filesystem roots used for power discovery.
Definition: RADPower.h:19
std::string sysClassThermal
Thermal sysfs root.
Definition: RADPower.h:23
std::string sysDevicesCpu
CPU sysfs root.
Definition: RADPower.h:25
std::string sysClassPowerSupply
Power supply sysfs root.
Definition: RADPower.h:21
Power supply snapshot.
Definition: RADPower.h:29
int capacityPercent
Capacity percent, or -1 when unknown.
Definition: RADPower.h:37
std::string type
Supply type.
Definition: RADPower.h:33
std::string name
Supply name.
Definition: RADPower.h:31
std::string status
Charging/discharging status.
Definition: RADPower.h:35
Thermal zone snapshot.
Definition: RADPower.h:41
double temperatureC
Temperature in Celsius, or NaN when unknown.
Definition: RADPower.h:47
std::string name
Zone name.
Definition: RADPower.h:43
std::string type
Zone type.
Definition: RADPower.h:45