blob: 8cff245869ed781a1eb3204962c2b6154533c4aa [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
James Feist75eb7692019-02-25 12:50:02 -08003#include "conf.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07004#include "dbus/util.hpp"
5#include "interfaces.hpp"
6
Patrick Venture863b9242018-03-08 08:29:23 -08007#include <chrono>
8#include <cmath>
9#include <iostream>
10#include <map>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070011#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080012#include <mutex>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070013#include <sdbusplus/bus.hpp>
14#include <sdbusplus/message.hpp>
15#include <sdbusplus/server.hpp>
Patrick Venture863b9242018-03-08 08:29:23 -080016#include <set>
17#include <string>
18#include <tuple>
19#include <vector>
20
Patrick Venture7af157b2018-10-30 11:24:40 -070021int dbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
Patrick Venture863b9242018-03-08 08:29:23 -080022
23/*
24 * This ReadInterface will passively listen for Value updates from whomever
25 * owns the associated dbus object.
26 *
27 * This requires another modification in phosphor-dbus-interfaces that will
28 * signal a value update every time it's read instead of only when it changes
29 * to help us:
30 * - ensure we're still receiving data (since we don't control the reader)
31 * - simplify stale data detection
32 * - simplify error detection
33 */
34class DbusPassive : public ReadInterface
35{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070036 public:
37 static std::unique_ptr<ReadInterface>
Patrick Venture563a3562018-10-30 09:31:26 -070038 createDbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
James Feist75eb7692019-02-25 12:50:02 -080039 const std::string& id, DbusHelperInterface* helper,
James Feistf81f2882019-02-26 11:26:36 -080040 const conf::SensorConfig* info);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070041
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042 DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
Patrick Venturef8cb4642018-10-30 12:02:53 -070043 const std::string& id, DbusHelperInterface* helper,
44 const struct SensorProperties& settings, bool failed);
Patrick Venture863b9242018-03-08 08:29:23 -080045
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070046 ReadReturn read(void) override;
James Feist36b7d8e2018-10-05 15:39:01 -070047 bool getFailed(void) const override;
Patrick Venture863b9242018-03-08 08:29:23 -080048
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 void setValue(double value);
James Feist36b7d8e2018-10-05 15:39:01 -070050 void setFailed(bool value);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070051 int64_t getScale(void);
Patrick Venture563a3562018-10-30 09:31:26 -070052 std::string getID(void);
James Feist75eb7692019-02-25 12:50:02 -080053 double getMax(void);
54 double getMin(void);
Patrick Venture863b9242018-03-08 08:29:23 -080055
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070056 private:
57 sdbusplus::bus::bus& _bus;
58 sdbusplus::server::match::match _signal;
59 int64_t _scale;
60 std::string _id; // for debug identification
61 DbusHelperInterface* _helper;
Patrick Venture863b9242018-03-08 08:29:23 -080062
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070063 std::mutex _lock;
64 double _value = 0;
James Feist75eb7692019-02-25 12:50:02 -080065 double _max = 0;
66 double _min = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070067 bool _failed = false;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070068 /* The last time the value was refreshed, not necessarily changed. */
69 std::chrono::high_resolution_clock::time_point _updated;
Patrick Venture863b9242018-03-08 08:29:23 -080070};
71
Patrick Venture7af157b2018-10-30 11:24:40 -070072int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner);