blob: bc16719c3a9a76339fc7c4ab927e33aa0a2872ed [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
James Feist75eb7692019-02-25 12:50:02 -08003#include "conf.hpp"
Patrick Ventureaadb30d2020-08-10 09:17:11 -07004#include "dbushelper_interface.hpp"
James Feist98b704e2019-06-03 16:24:53 -07005#include "dbuspassiveredundancy.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include "interfaces.hpp"
James Feist0c8223b2019-05-08 15:33:33 -07007#include "util.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07008
Patrick Venturea83a3ec2020-08-04 09:52:05 -07009#include <sdbusplus/bus.hpp>
Patrick Williams457993f2021-11-19 12:24:08 -060010#include <sdbusplus/bus/match.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070011#include <sdbusplus/message.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070012
Patrick Venture863b9242018-03-08 08:29:23 -080013#include <chrono>
14#include <cmath>
15#include <iostream>
16#include <map>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070017#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080018#include <mutex>
19#include <set>
20#include <string>
21#include <tuple>
22#include <vector>
23
Patrick Venturea0764872020-08-08 07:48:43 -070024namespace pid_control
25{
26
Patrick Venture7af157b2018-10-30 11:24:40 -070027int dbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
Patrick Venture863b9242018-03-08 08:29:23 -080028
29/*
30 * This ReadInterface will passively listen for Value updates from whomever
31 * owns the associated dbus object.
32 *
33 * This requires another modification in phosphor-dbus-interfaces that will
34 * signal a value update every time it's read instead of only when it changes
35 * to help us:
36 * - ensure we're still receiving data (since we don't control the reader)
37 * - simplify stale data detection
38 * - simplify error detection
39 */
40class DbusPassive : public ReadInterface
41{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042 public:
James Feist98b704e2019-06-03 16:24:53 -070043 static std::unique_ptr<ReadInterface> createDbusPassive(
44 sdbusplus::bus::bus& bus, const std::string& type,
Patrick Venture8729eb92020-08-10 10:38:44 -070045 const std::string& id, std::unique_ptr<DbusHelperInterface> helper,
James Feist98b704e2019-06-03 16:24:53 -070046 const conf::SensorConfig* info,
47 const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070048
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
Patrick Venture8729eb92020-08-10 10:38:44 -070050 const std::string& id,
51 std::unique_ptr<DbusHelperInterface> helper,
Patrick Venture1df9e872020-10-08 15:35:01 -070052 const SensorProperties& settings, bool failed,
James Feist98b704e2019-06-03 16:24:53 -070053 const std::string& path,
54 const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
Patrick Venture863b9242018-03-08 08:29:23 -080055
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070056 ReadReturn read(void) override;
James Feist36b7d8e2018-10-05 15:39:01 -070057 bool getFailed(void) const override;
Patrick Venture863b9242018-03-08 08:29:23 -080058
Josh Lehan3e2f7582020-09-20 22:06:03 -070059 void updateValue(double value, bool force);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 void setValue(double value);
Josh Lehan3e2f7582020-09-20 22:06:03 -070061
James Feist36b7d8e2018-10-05 15:39:01 -070062 void setFailed(bool value);
James Feist4b36f262020-07-07 16:56:41 -070063 void setFunctional(bool value);
Alex.Song8f73ad72021-10-07 00:18:27 +080064 void setAvailable(bool value);
Josh Lehan3e2f7582020-09-20 22:06:03 -070065
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070066 int64_t getScale(void);
Patrick Venture563a3562018-10-30 09:31:26 -070067 std::string getID(void);
James Feist75eb7692019-02-25 12:50:02 -080068 double getMax(void);
69 double getMin(void);
Patrick Venture863b9242018-03-08 08:29:23 -080070
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070071 private:
Patrick Williams457993f2021-11-19 12:24:08 -060072 sdbusplus::bus::match_t _signal;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070073 int64_t _scale;
74 std::string _id; // for debug identification
Patrick Venture8729eb92020-08-10 10:38:44 -070075 std::unique_ptr<DbusHelperInterface> _helper;
Patrick Venture863b9242018-03-08 08:29:23 -080076
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070077 std::mutex _lock;
78 double _value = 0;
James Feist75eb7692019-02-25 12:50:02 -080079 double _max = 0;
80 double _min = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070081 bool _failed = false;
James Feist4b36f262020-07-07 16:56:41 -070082 bool _functional = true;
Alex.Song8f73ad72021-10-07 00:18:27 +080083 bool _available = true;
84 bool _unavailableAsFailed = true;
James Feist98b704e2019-06-03 16:24:53 -070085
Josh Lehan3e2f7582020-09-20 22:06:03 -070086 bool _typeMargin = false;
Alex.Song8f73ad72021-10-07 00:18:27 +080087 bool _typeFan = false;
Josh Lehan3e2f7582020-09-20 22:06:03 -070088 bool _badReading = false;
89 bool _marginHot = false;
90
James Feist98b704e2019-06-03 16:24:53 -070091 std::string path;
92 std::shared_ptr<DbusPassiveRedundancy> redundancy;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070093 /* The last time the value was refreshed, not necessarily changed. */
94 std::chrono::high_resolution_clock::time_point _updated;
Patrick Venture863b9242018-03-08 08:29:23 -080095};
96
Patrick Venture7af157b2018-10-30 11:24:40 -070097int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner);
Patrick Venturea0764872020-08-08 07:48:43 -070098
99} // namespace pid_control