xref: /openbmc/phosphor-pid-control/pid/zone.hpp (revision b300575e)
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 setOutputCache(std::string_view, const ValueCacheEntry&) override;
78     void dumpCache(void);
79 
80     void processFans(void) override;
81     void processThermals(void) override;
82 
83     void addFanPID(std::unique_ptr<Controller> pid);
84     void addThermalPID(std::unique_ptr<Controller> pid);
85     double getCachedValue(const std::string& name) override;
86     ValueCacheEntry getCachedValues(const std::string& name) override;
87 
88     void addFanInput(const std::string& fan);
89     void addThermalInput(const std::string& therm);
90 
91     void initializeLog(void) override;
92     void writeLog(const std::string& value) override;
93 
94     /* Method for setting the manual mode over dbus */
95     bool manual(bool value) override;
96     /* Method for reading whether in fail-safe mode over dbus */
97     bool failSafe() const override;
98 
99   private:
100     std::ofstream _log;
101 
102     const int64_t _zoneId;
103     double _maximumSetPoint = 0;
104     std::string _maximumSetPointName;
105     std::string _maximumSetPointNamePrev;
106     bool _manualMode = false;
107     bool _redundantWrite = false;
108     const double _minThermalOutputSetPt;
109     const double _failSafePercent;
110 
111     std::set<std::string> _failSafeSensors;
112 
113     std::vector<double> _SetPoints;
114     std::vector<double> _RPMCeilings;
115     std::vector<std::string> _fanInputs;
116     std::vector<std::string> _thermalInputs;
117     std::map<std::string, ValueCacheEntry> _cachedValuesByName;
118     std::map<std::string, ValueCacheEntry> _cachedFanOutputs;
119     const SensorManager& _mgr;
120 
121     std::vector<std::unique_ptr<Controller>> _fans;
122     std::vector<std::unique_ptr<Controller>> _thermals;
123 };
124 
125 } // namespace pid_control
126