blob: 37fcc7773d2bd5715be7b8ad2772010627ef612a [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#pragma once
2
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "conf.hpp"
4#include "controller.hpp"
James Feist22c257a2018-08-31 14:07:12 -07005#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include "sensors/manager.hpp"
7#include "sensors/sensor.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07008
Patrick Ventured8012182018-03-08 08:21:38 -08009#include <fstream>
10#include <map>
11#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070012#include <sdbusplus/bus.hpp>
13#include <sdbusplus/server.hpp>
Patrick Ventured8012182018-03-08 08:21:38 -080014#include <set>
15#include <string>
16#include <vector>
Patrick Venture1248b152018-10-30 19:09:54 -070017#include <xyz/openbmc_project/Control/Mode/server.hpp>
Patrick Ventured8012182018-03-08 08:21:38 -080018
Patrick Ventured8012182018-03-08 08:21:38 -080019template <typename... T>
20using ServerObject = typename sdbusplus::server::object::object<T...>;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070021using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
Patrick Ventured8012182018-03-08 08:21:38 -080022using ModeObject = ServerObject<ModeInterface>;
23
Patrick Venturea58197c2018-06-11 15:29:45 -070024class ZoneInterface
25{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026 public:
27 virtual ~ZoneInterface() = default;
Patrick Venturea58197c2018-06-11 15:29:45 -070028
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029 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;
Patrick Venture2d8e7852018-10-30 19:14:07 -070034 virtual Sensor* getSensor(const std::string& name) = 0;
Patrick Venturea58197c2018-06-11 15:29:45 -070035};
36
Patrick Ventured8012182018-03-08 08:21:38 -080037/*
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 */
Patrick Venturea58197c2018-06-11 15:29:45 -070042class PIDZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080043{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070044 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 {
Patrick Ventured8012182018-03-08 08:21:38 -080052#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070053 _log.open("/tmp/swampd.log");
Patrick Ventured8012182018-03-08 08:21:38 -080054#endif
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070055 }
Patrick Ventured8012182018-03-08 08:21:38 -080056
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 float getMaxRPMRequest(void) const override;
58 bool getManualMode(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080059
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 /* 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;
Patrick Venture0bbeaf82018-10-30 18:50:31 -070065 int64_t getZoneID(void) const;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066 void addRPMSetPoint(float setpoint) override;
67 void clearRPMSetPoints(void);
68 float getFailSafePercent(void) const override;
69 float getMinThermalRpmSetPt(void) const;
Patrick Ventured8012182018-03-08 08:21:38 -080070
Patrick Venture2d8e7852018-10-30 19:14:07 -070071 Sensor* getSensor(const std::string& name) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 void determineMaxRPMRequest(void);
73 void updateFanTelemetry(void);
74 void updateSensors(void);
75 void initializeCache(void);
76 void dumpCache(void);
Patrick Venture563a3562018-10-30 09:31:26 -070077 void processFans(void);
78 void processThermals(void);
Patrick Ventured8012182018-03-08 08:21:38 -080079
James Feist22c257a2018-08-31 14:07:12 -070080 void addFanPID(std::unique_ptr<Controller> pid);
81 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070082 double getCachedValue(const std::string& name) override;
Patrick Venturec399f6f2018-10-30 19:24:47 -070083 void addFanInput(const std::string& fan);
84 void addThermalInput(const std::string& therm);
Patrick Ventured8012182018-03-08 08:21:38 -080085
86#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070087 void initializeLog(void);
88 std::ofstream& getLogHandle(void);
Patrick Ventured8012182018-03-08 08:21:38 -080089#endif
90
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070091 /* 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;
Patrick Ventured8012182018-03-08 08:21:38 -080095
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070096 private:
Patrick Ventured8012182018-03-08 08:21:38 -080097#ifdef __TUNING_LOGGING__
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070098 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -080099#endif
100
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700101 const int64_t _zoneId;
102 float _maximumRPMSetPt = 0;
103 bool _manualMode = false;
104 const float _minThermalRpmSetPt;
105 const float _failSafePercent;
Patrick Ventured8012182018-03-08 08:21:38 -0800106
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700107 std::set<std::string> _failSafeSensors;
Patrick Ventured8012182018-03-08 08:21:38 -0800108
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700109 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;
Patrick Ventured8012182018-03-08 08:21:38 -0800114
James Feist22c257a2018-08-31 14:07:12 -0700115 std::vector<std::unique_ptr<Controller>> _fans;
116 std::vector<std::unique_ptr<Controller>> _thermals;
Patrick Ventured8012182018-03-08 08:21:38 -0800117};