blob: 38136227ea1122b0d8c6c4a8c4108194c6a94e0e [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(
Patrick Williamsb228bc32022-07-22 19:26:56 -050044 sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
45 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 Williamsb228bc32022-07-22 19:26:56 -050049 DbusPassive(sdbusplus::bus_t& 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);
Josh Lehanb3005752022-02-22 20:48:07 -080060 void setValue(double value, double unscaled);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070061 void setValue(double value);
Josh Lehan3e2f7582020-09-20 22:06:03 -070062
James Feist36b7d8e2018-10-05 15:39:01 -070063 void setFailed(bool value);
James Feist4b36f262020-07-07 16:56:41 -070064 void setFunctional(bool value);
Alex.Song8f73ad72021-10-07 00:18:27 +080065 void setAvailable(bool value);
Josh Lehan3e2f7582020-09-20 22:06:03 -070066
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 int64_t getScale(void);
Patrick Venture563a3562018-10-30 09:31:26 -070068 std::string getID(void);
James Feist75eb7692019-02-25 12:50:02 -080069 double getMax(void);
70 double getMin(void);
Patrick Venture863b9242018-03-08 08:29:23 -080071
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 private:
Patrick Williams457993f2021-11-19 12:24:08 -060073 sdbusplus::bus::match_t _signal;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070074 int64_t _scale;
75 std::string _id; // for debug identification
Patrick Venture8729eb92020-08-10 10:38:44 -070076 std::unique_ptr<DbusHelperInterface> _helper;
Patrick Venture863b9242018-03-08 08:29:23 -080077
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070078 std::mutex _lock;
79 double _value = 0;
Josh Lehanb3005752022-02-22 20:48:07 -080080 double _unscaled = 0;
James Feist75eb7692019-02-25 12:50:02 -080081 double _max = 0;
82 double _min = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070083 bool _failed = false;
James Feist4b36f262020-07-07 16:56:41 -070084 bool _functional = true;
Alex.Song8f73ad72021-10-07 00:18:27 +080085 bool _available = true;
86 bool _unavailableAsFailed = true;
James Feist98b704e2019-06-03 16:24:53 -070087
Josh Lehan3e2f7582020-09-20 22:06:03 -070088 bool _typeMargin = false;
Alex.Song8f73ad72021-10-07 00:18:27 +080089 bool _typeFan = false;
Josh Lehan3e2f7582020-09-20 22:06:03 -070090 bool _badReading = false;
91 bool _marginHot = false;
92
James Feist98b704e2019-06-03 16:24:53 -070093 std::string path;
94 std::shared_ptr<DbusPassiveRedundancy> redundancy;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070095 /* The last time the value was refreshed, not necessarily changed. */
96 std::chrono::high_resolution_clock::time_point _updated;
Patrick Venture863b9242018-03-08 08:29:23 -080097};
98
Patrick Williamsb228bc32022-07-22 19:26:56 -050099int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner);
Patrick Venturea0764872020-08-08 07:48:43 -0700100
101} // namespace pid_control