xref: /openbmc/phosphor-pid-control/pid/zone.hpp (revision 3dcfd546)
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_t<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_t& bus,
40                 const char* objPath, bool defer) :
41         ModeObject(bus, objPath,
42                    defer ? ModeObject::action::defer_emit
43                          : ModeObject::action::emit_object_added),
44         _zoneId(zone), _maximumSetPoint(),
45         _minThermalOutputSetPt(minThermalOutput),
46         _failSafePercent(failSafePercent), _mgr(mgr)
47     {
48         if (loggingEnabled)
49         {
50             _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
51         }
52     }
53 
54     bool getManualMode(void) const override;
55     /* Could put lock around this since it's accessed from two threads, but
56      * only one reader/one writer.
57      */
58 
59     bool getRedundantWrite(void) const override;
60     void setManualMode(bool mode);
61     bool getFailSafeMode(void) const override;
62 
63     int64_t getZoneID(void) const;
64     void addSetPoint(double setPoint, const std::string& name) override;
65     double getMaxSetPointRequest(void) const override;
66     void addRPMCeiling(double ceiling) override;
67     void clearSetPoints(void) override;
68     void clearRPMCeilings(void) override;
69     double getFailSafePercent(void) const override;
70     double getMinThermalSetPoint(void) const;
71 
72     Sensor* getSensor(const std::string& name) override;
73     void determineMaxSetPointRequest(void) override;
74     void updateFanTelemetry(void) override;
75     void updateSensors(void) override;
76     void initializeCache(void) override;
77     void dumpCache(void);
78 
79     void processFans(void) override;
80     void processThermals(void) override;
81 
82     void addFanPID(std::unique_ptr<Controller> pid);
83     void addThermalPID(std::unique_ptr<Controller> pid);
84     double getCachedValue(const std::string& name) override;
85     void addFanInput(const std::string& fan);
86     void addThermalInput(const std::string& therm);
87 
88     void initializeLog(void) override;
89     void writeLog(const std::string& value) override;
90 
91     /* Method for setting the manual mode over dbus */
92     bool manual(bool value) override;
93     /* Method for reading whether in fail-safe mode over dbus */
94     bool failSafe() const override;
95 
96   private:
97     std::ofstream _log;
98 
99     const int64_t _zoneId;
100     double _maximumSetPoint = 0;
101     std::string _maximumSetPointName;
102     std::string _maximumSetPointNamePrev;
103     bool _manualMode = false;
104     bool _redundantWrite = false;
105     const double _minThermalOutputSetPt;
106     const double _failSafePercent;
107 
108     std::set<std::string> _failSafeSensors;
109 
110     std::vector<double> _SetPoints;
111     std::vector<double> _RPMCeilings;
112     std::vector<std::string> _fanInputs;
113     std::vector<std::string> _thermalInputs;
114     std::map<std::string, double> _cachedValuesByName;
115     const SensorManager& _mgr;
116 
117     std::vector<std::unique_ptr<Controller>> _fans;
118     std::vector<std::unique_ptr<Controller>> _thermals;
119 };
120 
121 } // namespace pid_control
122