1 #pragma once 2 3 #include "conf.hpp" 4 #include "dbushelper_interface.hpp" 5 #include "dbuspassiveredundancy.hpp" 6 #include "interfaces.hpp" 7 8 #include <systemd/sd-bus.h> 9 10 #include <sdbusplus/bus.hpp> 11 #include <sdbusplus/bus/match.hpp> 12 #include <sdbusplus/message.hpp> 13 14 #include <chrono> 15 #include <cmath> 16 #include <cstdint> 17 #include <memory> 18 #include <mutex> 19 #include <string> 20 21 namespace pid_control 22 { 23 24 int dbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err); 25 26 /* 27 * This ReadInterface will passively listen for Value updates from whomever 28 * owns the associated dbus object. 29 * 30 * This requires another modification in phosphor-dbus-interfaces that will 31 * signal a value update every time it's read instead of only when it changes 32 * to help us: 33 * - ensure we're still receiving data (since we don't control the reader) 34 * - simplify stale data detection 35 * - simplify error detection 36 */ 37 class DbusPassive : public ReadInterface 38 { 39 public: 40 static std::unique_ptr<ReadInterface> createDbusPassive( 41 sdbusplus::bus_t& bus, const std::string& type, const std::string& id, 42 std::unique_ptr<DbusHelperInterface> helper, 43 const conf::SensorConfig* info, 44 const std::shared_ptr<DbusPassiveRedundancy>& redundancy); 45 46 DbusPassive(sdbusplus::bus_t& bus, const std::string& type, 47 const std::string& id, 48 std::unique_ptr<DbusHelperInterface> helper, bool objectMissing, 49 const std::string& path, 50 const std::shared_ptr<DbusPassiveRedundancy>& redundancy); 51 52 ReadReturn read(void) override; 53 bool getFailed(void) const override; 54 std::string getFailReason(void) const override; 55 56 void updateValue(double value, bool force); 57 void setValue(double value, double unscaled); 58 void setValue(double value); 59 60 void setFailed(bool value); 61 void setFunctional(bool value); 62 void setAvailable(bool value); 63 64 int64_t getScale(void); 65 std::string getID(void); 66 double getMax(void); 67 double getMin(void); 68 void setAvailableFromProperty(bool value); 69 void initFromSettings(const SensorProperties& settings, bool failed); 70 71 private: 72 sdbusplus::bus::match_t _signal; 73 int64_t _scale; 74 std::string _id; // for debug identification 75 std::unique_ptr<DbusHelperInterface> _helper; 76 77 std::mutex _lock; 78 double _value = 0; 79 double _unscaled = 0; 80 double _max = 0; 81 double _min = 0; 82 bool _failed = false; 83 bool _functional = true; 84 bool _available = true; 85 bool _availableOverridden = false; 86 bool _unavailableAsFailed = true; 87 88 bool _typeMargin = false; 89 bool _typeFan = false; 90 bool _badReading = false; 91 bool _marginHot = false; 92 bool _objectMissing = false; 93 94 std::string path; 95 std::shared_ptr<DbusPassiveRedundancy> redundancy; 96 /* The last time the value was refreshed, not necessarily changed. */ 97 std::chrono::high_resolution_clock::time_point _updated; 98 }; 99 100 int handleSensorValue(sdbusplus::message_t& msg, DbusPassive* owner); 101 102 } // namespace pid_control 103