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