1 #pragma once
2 
3 #include "conf.hpp"
4 #include "dbuspassiveredundancy.hpp"
5 #include "interfaces.hpp"
6 #include "util.hpp"
7 
8 #include <sdbusplus/bus.hpp>
9 #include <sdbusplus/message.hpp>
10 #include <sdbusplus/server.hpp>
11 
12 #include <chrono>
13 #include <cmath>
14 #include <iostream>
15 #include <map>
16 #include <memory>
17 #include <mutex>
18 #include <set>
19 #include <string>
20 #include <tuple>
21 #include <vector>
22 
23 namespace pid_control
24 {
25 
26 int dbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
27 
28 /*
29  * This ReadInterface will passively listen for Value updates from whomever
30  * owns the associated dbus object.
31  *
32  * This requires another modification in phosphor-dbus-interfaces that will
33  * signal a value update every time it's read instead of only when it changes
34  * to help us:
35  * - ensure we're still receiving data (since we don't control the reader)
36  * - simplify stale data detection
37  * - simplify error detection
38  */
39 class DbusPassive : public ReadInterface
40 {
41   public:
42     static std::unique_ptr<ReadInterface> createDbusPassive(
43         sdbusplus::bus::bus& bus, const std::string& type,
44         const std::string& id, DbusHelperInterface* helper,
45         const conf::SensorConfig* info,
46         const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
47 
48     DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
49                 const std::string& id, DbusHelperInterface* helper,
50                 const struct SensorProperties& settings, bool failed,
51                 const std::string& path,
52                 const std::shared_ptr<DbusPassiveRedundancy>& redundancy);
53 
54     ReadReturn read(void) override;
55     bool getFailed(void) const override;
56 
57     void setValue(double value);
58     void setFailed(bool value);
59     void setFunctional(bool value);
60     int64_t getScale(void);
61     std::string getID(void);
62     double getMax(void);
63     double getMin(void);
64 
65   private:
66     sdbusplus::bus::bus& _bus;
67     sdbusplus::server::match::match _signal;
68     int64_t _scale;
69     std::string _id; // for debug identification
70     DbusHelperInterface* _helper;
71 
72     std::mutex _lock;
73     double _value = 0;
74     double _max = 0;
75     double _min = 0;
76     bool _failed = false;
77     bool _functional = true;
78 
79     std::string path;
80     std::shared_ptr<DbusPassiveRedundancy> redundancy;
81     /* The last time the value was refreshed, not necessarily changed. */
82     std::chrono::high_resolution_clock::time_point _updated;
83 };
84 
85 int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner);
86 
87 } // namespace pid_control
88