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