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