blob: 86b5c5a65fc487fbe5bc310d2fcf5ff3633e4c8c [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"
Ed Tanousf8b6e552025-06-27 13:27:50 -07007
8#include <systemd/sd-bus.h>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07009
Patrick Venturea83a3ec2020-08-04 09:52:05 -070010#include <sdbusplus/bus.hpp>
Patrick Williams457993f2021-11-19 12:24:08 -060011#include <sdbusplus/bus/match.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070012#include <sdbusplus/message.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070013
Patrick Venture863b9242018-03-08 08:29:23 -080014#include <chrono>
15#include <cmath>
Ed Tanousf8b6e552025-06-27 13:27:50 -070016#include <cstdint>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070017#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080018#include <mutex>
Patrick Venture863b9242018-03-08 08:29:23 -080019#include <string>
Patrick Venture863b9242018-03-08 08:29:23 -080020
Patrick Venturea0764872020-08-08 07:48:43 -070021namespace pid_control
22{
23
Patrick Venture7af157b2018-10-30 11:24:40 -070024int dbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
Patrick Venture863b9242018-03-08 08:29:23 -080025
26/*
27 * This ReadInterface will passively listen for Value updates from whomever
28 * owns the associated dbus object.
29 *
30 * This requires another modification in phosphor-dbus-interfaces that will
31 * signal a value update every time it's read instead of only when it changes
32 * to help us:
33 * - ensure we're still receiving data (since we don't control the reader)
34 * - simplify stale data detection
35 * - simplify error detection
36 */
37class DbusPassive : public ReadInterface
38{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039 public:
James Feist98b704e2019-06-03 16:24:53 -070040 static std::unique_ptr<ReadInterface> createDbusPassive(
Patrick Williamsb228bc32022-07-22 19:26:56 -050041 sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
42 std::unique_ptr<DbusHelperInterface> helper,
James Feist98b704e2019-06-03 16:24:53 -070043 const conf::SensorConfig* info,
44 const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070045
Patrick Williamsb228bc32022-07-22 19:26:56 -050046 DbusPassive(sdbusplus::bus_t& bus, const std::string& type,
Patrick Venture8729eb92020-08-10 10:38:44 -070047 const std::string& id,
48 std::unique_ptr<DbusHelperInterface> helper,
Patrick Venture1df9e872020-10-08 15:35:01 -070049 const SensorProperties& settings, bool failed,
James Feist98b704e2019-06-03 16:24:53 -070050 const std::string& path,
51 const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
Patrick Venture863b9242018-03-08 08:29:23 -080052
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070053 ReadReturn read(void) override;
James Feist36b7d8e2018-10-05 15:39:01 -070054 bool getFailed(void) const override;
Harvey Wua4270072024-05-29 16:11:13 +080055 std::string getFailReason(void) const override;
Patrick Venture863b9242018-03-08 08:29:23 -080056
Josh Lehan3e2f7582020-09-20 22:06:03 -070057 void updateValue(double value, bool force);
Josh Lehanb3005752022-02-22 20:48:07 -080058 void setValue(double value, double unscaled);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070059 void setValue(double value);
Josh Lehan3e2f7582020-09-20 22:06:03 -070060
James Feist36b7d8e2018-10-05 15:39:01 -070061 void setFailed(bool value);
James Feist4b36f262020-07-07 16:56:41 -070062 void setFunctional(bool value);
Alex.Song8f73ad72021-10-07 00:18:27 +080063 void setAvailable(bool value);
Josh Lehan3e2f7582020-09-20 22:06:03 -070064
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065 int64_t getScale(void);
Patrick Venture563a3562018-10-30 09:31:26 -070066 std::string getID(void);
James Feist75eb7692019-02-25 12:50:02 -080067 double getMax(void);
68 double getMin(void);
Patrick Venture863b9242018-03-08 08:29:23 -080069
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070070 private:
Patrick Williams457993f2021-11-19 12:24:08 -060071 sdbusplus::bus::match_t _signal;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 int64_t _scale;
73 std::string _id; // for debug identification
Patrick Venture8729eb92020-08-10 10:38:44 -070074 std::unique_ptr<DbusHelperInterface> _helper;
Patrick Venture863b9242018-03-08 08:29:23 -080075
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070076 std::mutex _lock;
77 double _value = 0;
Josh Lehanb3005752022-02-22 20:48:07 -080078 double _unscaled = 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 Williamsb228bc32022-07-22 19:26:56 -050097int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner);
Patrick Venturea0764872020-08-08 07:48:43 -070098
99} // namespace pid_control