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