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