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