blob: 5dbd3c3453a13396f86dec361da55e44843fe6ec [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "dbus/util.hpp"
4#include "interfaces.hpp"
5
Patrick Venture863b9242018-03-08 08:29:23 -08006#include <chrono>
7#include <cmath>
8#include <iostream>
9#include <map>
Patrick Venture0ef1faf2018-06-13 12:50:53 -070010#include <memory>
Patrick Venture863b9242018-03-08 08:29:23 -080011#include <mutex>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070012#include <sdbusplus/bus.hpp>
13#include <sdbusplus/message.hpp>
14#include <sdbusplus/server.hpp>
Patrick Venture863b9242018-03-08 08:29:23 -080015#include <set>
16#include <string>
17#include <tuple>
18#include <vector>
19
Patrick Venture7af157b2018-10-30 11:24:40 -070020int dbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
Patrick Venture863b9242018-03-08 08:29:23 -080021
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 */
33class DbusPassive : public ReadInterface
34{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070035 public:
36 static std::unique_ptr<ReadInterface>
Patrick Venture563a3562018-10-30 09:31:26 -070037 createDbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038 const std::string& id, DbusHelperInterface* helper);
Patrick Venture0ef1faf2018-06-13 12:50:53 -070039
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070040 DbusPassive(sdbusplus::bus::bus& bus, const std::string& type,
Patrick Venturef8cb4642018-10-30 12:02:53 -070041 const std::string& id, DbusHelperInterface* helper,
42 const struct SensorProperties& settings, bool failed);
Patrick Venture863b9242018-03-08 08:29:23 -080043
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070044 ReadReturn read(void) override;
James Feist36b7d8e2018-10-05 15:39:01 -070045 bool getFailed(void) const override;
Patrick Venture863b9242018-03-08 08:29:23 -080046
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070047 void setValue(double value);
James Feist36b7d8e2018-10-05 15:39:01 -070048 void setFailed(bool value);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 int64_t getScale(void);
Patrick Venture563a3562018-10-30 09:31:26 -070050 std::string getID(void);
Patrick Venture863b9242018-03-08 08:29:23 -080051
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 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;
Patrick Venture863b9242018-03-08 08:29:23 -080058
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070059 std::mutex _lock;
60 double _value = 0;
James Feist36b7d8e2018-10-05 15:39:01 -070061 bool _failed = false;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070062 /* The last time the value was refreshed, not necessarily changed. */
63 std::chrono::high_resolution_clock::time_point _updated;
Patrick Venture863b9242018-03-08 08:29:23 -080064};
65
Patrick Venture7af157b2018-10-30 11:24:40 -070066int handleSensorValue(sdbusplus::message::message& msg, DbusPassive* owner);