blob: 1934a6630913a4ccf8f20d753de7d51e575efaab [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#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
Patrick Venturedc3b7902018-03-24 10:41:19 -070015#include "xyz/openbmc_project/Control/Mode/server.hpp"
Patrick Ventured8012182018-03-08 08:21:38 -080016#include <sdbusplus/bus.hpp>
17#include <sdbusplus/server.hpp>
18
19
20template <typename... T>
21using ServerObject = typename sdbusplus::server::object::object<T...>;
22using ModeInterface =
Patrick Venturedc3b7902018-03-24 10:41:19 -070023 sdbusplus::xyz::openbmc_project::Control::server::Mode;
Patrick Ventured8012182018-03-08 08:21:38 -080024using ModeObject = ServerObject<ModeInterface>;
25
Patrick Venturea58197c2018-06-11 15:29:45 -070026class ZoneInterface
27{
28 public:
29 virtual ~ZoneInterface() = default;
30
31 virtual double getCachedValue(const std::string& name) = 0;
32 virtual void addRPMSetPoint(float setpoint) = 0;
33 virtual float getMaxRPMRequest() const = 0;
34 virtual bool getFailSafeMode() const = 0;
35 virtual float getFailSafePercent() const = 0;
36 virtual Sensor* getSensor(std::string name) = 0;
37};
38
Patrick Ventured8012182018-03-08 08:21:38 -080039/*
40 * The PIDZone inherits from the Mode object so that it can listen for control
41 * mode changes. It primarily holds all PID loops and holds the sensor value
42 * cache that's used per iteration of the PID loops.
43 */
Patrick Venturea58197c2018-06-11 15:29:45 -070044class PIDZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080045{
46 public:
47 PIDZone(int64_t zone,
48 float minThermalRpm,
49 float failSafePercent,
Patrick Venturefe75b192018-06-08 11:19:43 -070050 const SensorManager& mgr,
Patrick Ventured8012182018-03-08 08:21:38 -080051 sdbusplus::bus::bus& bus,
52 const char* objPath,
53 bool defer)
54 : ModeObject(bus, objPath, defer),
55 _zoneId(zone),
56 _maximumRPMSetPt(),
57 _minThermalRpmSetPt(minThermalRpm),
58 _failSafePercent(failSafePercent),
59 _mgr(mgr)
60 {
61#ifdef __TUNING_LOGGING__
62 _log.open("/tmp/swampd.log");
63#endif
64 }
65
Patrick Venturea58197c2018-06-11 15:29:45 -070066 float getMaxRPMRequest(void) const override;
Patrick Ventured8012182018-03-08 08:21:38 -080067 bool getManualMode(void) const;
68
69 /* Could put lock around this since it's accessed from two threads, but
70 * only one reader/one writer.
71 */
72 void setManualMode(bool mode);
Patrick Venturea58197c2018-06-11 15:29:45 -070073 bool getFailSafeMode(void) const override;
Patrick Ventured8012182018-03-08 08:21:38 -080074 int64_t getZoneId(void) const;
Patrick Venturea58197c2018-06-11 15:29:45 -070075 void addRPMSetPoint(float setpoint) override;
Patrick Ventured8012182018-03-08 08:21:38 -080076 void clearRPMSetPoints(void);
Patrick Venturea58197c2018-06-11 15:29:45 -070077 float getFailSafePercent(void) const override;
Patrick Ventured8012182018-03-08 08:21:38 -080078 float getMinThermalRpmSetPt(void) const;
79
Patrick Venturea58197c2018-06-11 15:29:45 -070080 Sensor* getSensor(std::string name) override;
Patrick Ventured8012182018-03-08 08:21:38 -080081 void determineMaxRPMRequest(void);
82 void updateFanTelemetry(void);
83 void updateSensors(void);
84 void initializeCache(void);
85 void dumpCache(void);
86 void process_fans(void);
87 void process_thermals(void);
88
89 void addFanPID(std::unique_ptr<PIDController> pid);
90 void addThermalPID(std::unique_ptr<PIDController> pid);
Patrick Venturea58197c2018-06-11 15:29:45 -070091 double getCachedValue(const std::string& name) override;
Patrick Ventured8012182018-03-08 08:21:38 -080092 void addFanInput(std::string fan);
93 void addThermalInput(std::string therm);
94
95#ifdef __TUNING_LOGGING__
96 void initializeLog(void);
97 std::ofstream& getLogHandle(void);
98#endif
99
100 /* Method for setting the manual mode over dbus */
101 bool manual(bool value) override;
102 /* Method for reading whether in fail-safe mode over dbus */
103 bool failSafe() const override;
104
105 private:
106#ifdef __TUNING_LOGGING__
107 std::ofstream _log;
108#endif
109
110 const int64_t _zoneId;
111 float _maximumRPMSetPt = 0;
112 bool _manualMode = false;
113 const float _minThermalRpmSetPt;
114 const float _failSafePercent;
115
116 std::set<std::string> _failSafeSensors;
117
118 std::vector<float> _RPMSetPoints;
119 std::vector<std::string> _fanInputs;
120 std::vector<std::string> _thermalInputs;
121 std::map<std::string, double> _cachedValuesByName;
Patrick Venturefe75b192018-06-08 11:19:43 -0700122 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800123
124 std::vector<std::unique_ptr<PIDController>> _fans;
125 std::vector<std::unique_ptr<PIDController>> _thermals;
126};