xref: /openbmc/phosphor-pid-control/pid/zone.hpp (revision a83a3ecc)
1 #pragma once
2 
3 #include "conf.hpp"
4 #include "controller.hpp"
5 #include "pidcontroller.hpp"
6 #include "sensors/manager.hpp"
7 #include "sensors/sensor.hpp"
8 #include "tuning.hpp"
9 
10 #include <sdbusplus/bus.hpp>
11 #include <sdbusplus/server.hpp>
12 #include <xyz/openbmc_project/Control/Mode/server.hpp>
13 
14 #include <fstream>
15 #include <map>
16 #include <memory>
17 #include <set>
18 #include <string>
19 #include <vector>
20 
21 template <typename... T>
22 using ServerObject = typename sdbusplus::server::object::object<T...>;
23 using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
24 using ModeObject = ServerObject<ModeInterface>;
25 
26 class ZoneInterface
27 {
28   public:
29     virtual ~ZoneInterface() = default;
30 
31     virtual double getCachedValue(const std::string& name) = 0;
32     virtual void addSetPoint(double setpoint) = 0;
33     virtual void addRPMCeiling(double ceiling) = 0;
34     virtual double getMaxSetPointRequest() const = 0;
35     virtual bool getFailSafeMode() const = 0;
36     virtual double getFailSafePercent() const = 0;
37     virtual Sensor* getSensor(const std::string& name) = 0;
38 };
39 
40 /*
41  * The PIDZone inherits from the Mode object so that it can listen for control
42  * mode changes.  It primarily holds all PID loops and holds the sensor value
43  * cache that's used per iteration of the PID loops.
44  */
45 class PIDZone : public ZoneInterface, public ModeObject
46 {
47   public:
48     PIDZone(int64_t zone, double minThermalOutput, double failSafePercent,
49             const SensorManager& mgr, sdbusplus::bus::bus& bus,
50             const char* objPath, bool defer) :
51         ModeObject(bus, objPath, defer),
52         _zoneId(zone), _maximumSetPoint(),
53         _minThermalOutputSetPt(minThermalOutput),
54         _failSafePercent(failSafePercent), _mgr(mgr)
55     {
56         if (loggingEnabled)
57         {
58             _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
59         }
60     }
61 
62     double getMaxSetPointRequest(void) const override;
63     bool getManualMode(void) const;
64 
65     /* Could put lock around this since it's accessed from two threads, but
66      * only one reader/one writer.
67      */
68     void setManualMode(bool mode);
69     bool getFailSafeMode(void) const override;
70     int64_t getZoneID(void) const;
71     void addSetPoint(double setpoint) override;
72     void addRPMCeiling(double ceiling) override;
73     void clearSetPoints(void);
74     void clearRPMCeilings(void);
75     double getFailSafePercent(void) const override;
76     double getMinThermalSetpoint(void) const;
77 
78     Sensor* getSensor(const std::string& name) override;
79     void determineMaxSetPointRequest(void);
80     void updateFanTelemetry(void);
81     void updateSensors(void);
82     void initializeCache(void);
83     void dumpCache(void);
84     void processFans(void);
85     void processThermals(void);
86 
87     void addFanPID(std::unique_ptr<Controller> pid);
88     void addThermalPID(std::unique_ptr<Controller> pid);
89     double getCachedValue(const std::string& name) override;
90     void addFanInput(const std::string& fan);
91     void addThermalInput(const std::string& therm);
92 
93     void initializeLog(void);
94     std::ofstream& getLogHandle(void);
95 
96     /* Method for setting the manual mode over dbus */
97     bool manual(bool value) override;
98     /* Method for reading whether in fail-safe mode over dbus */
99     bool failSafe() const override;
100 
101   private:
102     std::ofstream _log;
103 
104     const int64_t _zoneId;
105     double _maximumSetPoint = 0;
106     bool _manualMode = false;
107     const double _minThermalOutputSetPt;
108     const double _failSafePercent;
109 
110     std::set<std::string> _failSafeSensors;
111 
112     std::vector<double> _SetPoints;
113     std::vector<double> _RPMCeilings;
114     std::vector<std::string> _fanInputs;
115     std::vector<std::string> _thermalInputs;
116     std::map<std::string, double> _cachedValuesByName;
117     const SensorManager& _mgr;
118 
119     std::vector<std::unique_ptr<Controller>> _fans;
120     std::vector<std::unique_ptr<Controller>> _thermals;
121 };
122