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