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