blob: 3b091ddc6224c99e098503d28b85a17a19b0e3ee [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>
10#include <sdbusplus/message.hpp>
11#include <sdbusplus/server.hpp>
12
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,
James Feist98b704e2019-06-03 16:24:53 -070052 const struct SensorProperties& settings, bool failed,
53 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);
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 Ventureda4a5dd2018-08-31 09:42:48 -070071 sdbusplus::server::match::match _signal;
72 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;
James Feist75eb7692019-02-25 12:50:02 -080078 double _max = 0;
79 double _min = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070080 bool _failed = false;
James Feist4b36f262020-07-07 16:56:41 -070081 bool _functional = true;
James Feist98b704e2019-06-03 16:24:53 -070082
Josh Lehan3e2f7582020-09-20 22:06:03 -070083 bool _typeMargin = false;
84 bool _badReading = false;
85 bool _marginHot = false;
86
James Feist98b704e2019-06-03 16:24:53 -070087 std::string path;
88 std::shared_ptr<DbusPassiveRedundancy> redundancy;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070089 /* The last time the value was refreshed, not necessarily changed. */
90 std::chrono::high_resolution_clock::time_point _updated;
Patrick Venture863b9242018-03-08 08:29:23 -080091};
92
Patrick Venture7af157b2018-10-30 11:24:40 -070093int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner);
Patrick Venturea0764872020-08-08 07:48:43 -070094
95} // namespace pid_control