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 
59     void updateValue(double value, bool force);
60     void setValue(double value);
61 
62     void setFailed(bool value);
63     void setFunctional(bool value);
64     void setAvailable(bool value);
65 
66     int64_t getScale(void);
67     std::string getID(void);
68     double getMax(void);
69     double getMin(void);
70 
71   private:
72     sdbusplus::bus::match_t _signal;
73     int64_t _scale;
74     std::string _id; // for debug identification
75     std::unique_ptr<DbusHelperInterface> _helper;
76 
77     std::mutex _lock;
78     double _value = 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