xref: /openbmc/phosphor-pid-control/pid/zone.hpp (revision 0e8fc398)
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                 conf::CycleTime cycleTime, const SensorManager& mgr,
40                 sdbusplus::bus_t& bus, 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), _cycleTime(cycleTime), _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     uint64_t getCycleIntervalTime(void) const override;
72     uint64_t getUpdateThermalsCycle(void) const override;
73 
74     Sensor* getSensor(const std::string& name) override;
75     void determineMaxSetPointRequest(void) override;
76     void updateFanTelemetry(void) override;
77     void updateSensors(void) override;
78     void initializeCache(void) override;
79     void setOutputCache(std::string_view, const ValueCacheEntry&) override;
80     void dumpCache(void);
81 
82     void processFans(void) override;
83     void processThermals(void) override;
84 
85     void addFanPID(std::unique_ptr<Controller> pid);
86     void addThermalPID(std::unique_ptr<Controller> pid);
87     double getCachedValue(const std::string& name) override;
88     ValueCacheEntry getCachedValues(const std::string& name) override;
89 
90     void addFanInput(const std::string& fan);
91     void addThermalInput(const std::string& therm);
92 
93     void initializeLog(void) override;
94     void writeLog(const std::string& value) override;
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     std::string _maximumSetPointName;
107     std::string _maximumSetPointNamePrev;
108     bool _manualMode = false;
109     bool _redundantWrite = false;
110     const double _minThermalOutputSetPt;
111     const double _failSafePercent;
112     const conf::CycleTime _cycleTime;
113 
114     std::set<std::string> _failSafeSensors;
115 
116     std::vector<double> _SetPoints;
117     std::vector<double> _RPMCeilings;
118     std::vector<std::string> _fanInputs;
119     std::vector<std::string> _thermalInputs;
120     std::map<std::string, ValueCacheEntry> _cachedValuesByName;
121     std::map<std::string, ValueCacheEntry> _cachedFanOutputs;
122     const SensorManager& _mgr;
123 
124     std::vector<std::unique_ptr<Controller>> _fans;
125     std::vector<std::unique_ptr<Controller>> _thermals;
126 };
127 
128 } // namespace pid_control
129