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