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/message.hpp>
11 #include <sdbusplus/server.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::bus& bus, const std::string& type,
45         const std::string& id, std::unique_ptr<DbusHelperInterface> helper,
46         const conf::SensorConfig* info,
47         const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
48 
49     DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
50                 const std::string& id,
51                 std::unique_ptr<DbusHelperInterface> helper,
52                 const struct 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 
59     void updateValue(double value, bool force);
60     void setValue(double value);
61 
62     void setFailed(bool value);
63     void setFunctional(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::server::match::match _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 _max = 0;
79     double _min = 0;
80     bool _failed = false;
81     bool _functional = true;
82 
83     bool _typeMargin = false;
84     bool _badReading = false;
85     bool _marginHot = false;
86 
87     std::string path;
88     std::shared_ptr<DbusPassiveRedundancy> redundancy;
89     /* The last time the value was refreshed, not necessarily changed. */
90     std::chrono::high_resolution_clock::time_point _updated;
91 };
92 
93 int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner);
94 
95 } // namespace pid_control
96