xref: /openbmc/phosphor-pid-control/dbus/dbuspassive.hpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 #pragma once
2 
3 #include "conf.hpp"
4 #include "dbushelper_interface.hpp"
5 #include "dbuspassiveredundancy.hpp"
6 #include "interfaces.hpp"
7 
8 #include <systemd/sd-bus.h>
9 
10 #include <sdbusplus/bus.hpp>
11 #include <sdbusplus/bus/match.hpp>
12 #include <sdbusplus/message.hpp>
13 
14 #include <chrono>
15 #include <cmath>
16 #include <cstdint>
17 #include <memory>
18 #include <mutex>
19 #include <string>
20 
21 namespace pid_control
22 {
23 
24 int dbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
25 
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  */
37 class DbusPassive : public ReadInterface
38 {
39   public:
40     static std::unique_ptr<ReadInterface> createDbusPassive(
41         sdbusplus::bus_t& bus, const std::string& type, const std::string& id,
42         std::unique_ptr<DbusHelperInterface> helper,
43         const conf::SensorConfig* info,
44         const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
45 
46     DbusPassive(sdbusplus::bus_t& bus, const std::string& type,
47                 const std::string& id,
48                 std::unique_ptr<DbusHelperInterface> helper,
49                 const SensorProperties& settings, bool failed,
50                 const std::string& path,
51                 const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
52 
53     ReadReturn read(void) override;
54     bool getFailed(void) const override;
55     std::string getFailReason(void) const override;
56 
57     void updateValue(double value, bool force);
58     void setValue(double value, double unscaled);
59     void setValue(double value);
60 
61     void setFailed(bool value);
62     void setFunctional(bool value);
63     void setAvailable(bool value);
64 
65     int64_t getScale(void);
66     std::string getID(void);
67     double getMax(void);
68     double getMin(void);
69 
70   private:
71     sdbusplus::bus::match_t _signal;
72     int64_t _scale;
73     std::string _id; // for debug identification
74     std::unique_ptr<DbusHelperInterface> _helper;
75 
76     std::mutex _lock;
77     double _value = 0;
78     double _unscaled = 0;
79     double _max = 0;
80     double _min = 0;
81     bool _failed = false;
82     bool _functional = true;
83     bool _available = true;
84     bool _unavailableAsFailed = true;
85 
86     bool _typeMargin = false;
87     bool _typeFan = false;
88     bool _badReading = false;
89     bool _marginHot = false;
90 
91     std::string path;
92     std::shared_ptr<DbusPassiveRedundancy> redundancy;
93     /* The last time the value was refreshed, not necessarily changed. */
94     std::chrono::high_resolution_clock::time_point _updated;
95 };
96 
97 int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner);
98 
99 } // namespace pid_control
100