Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 3 | #include "sensor.hpp" |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 4 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 5 | #include <memory> |
| 6 | #include <mutex> |
| 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdbusplus/server.hpp> |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 9 | #include <type_traits> |
Patrick Venture | 1248b15 | 2018-10-30 19:09:54 -0700 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Sensor/Value/server.hpp> |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 11 | |
| 12 | template <typename... T> |
| 13 | using ServerObject = typename sdbusplus::server::object::object<T...>; |
| 14 | |
| 15 | using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value; |
| 16 | using ValueObject = ServerObject<ValueInterface>; |
| 17 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 18 | class ValueHelper : public ValueInterface |
| 19 | { |
| 20 | |
| 21 | public: |
| 22 | auto operator()() const |
| 23 | { |
| 24 | return value(); |
| 25 | } |
| 26 | }; |
| 27 | |
| 28 | constexpr bool usingDouble = |
| 29 | std::is_same_v<std::result_of_t<ValueHelper()>, double>; |
| 30 | using ValueType = std::conditional_t<usingDouble, double, int64_t>; |
| 31 | |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 32 | /* |
| 33 | * HostSensor object is a Sensor derivative that also implements a ValueObject, |
| 34 | * which comes from the dbus as an object that implements Sensor.Value. |
| 35 | */ |
| 36 | class HostSensor : public Sensor, public ValueObject |
| 37 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 38 | public: |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 39 | static std::unique_ptr<Sensor> createTemp(const std::string& name, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 40 | int64_t timeout, |
| 41 | sdbusplus::bus::bus& bus, |
| 42 | const char* objPath, bool defer); |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 43 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 44 | HostSensor(const std::string& name, int64_t timeout, |
| 45 | sdbusplus::bus::bus& bus, const char* objPath, bool defer) : |
| 46 | Sensor(name, timeout), |
| 47 | ValueObject(bus, objPath, defer) |
| 48 | { |
| 49 | } |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 50 | |
James Feist | 8154e32 | 2020-06-22 17:12:18 -0700 | [diff] [blame] | 51 | ValueType value(ValueType value) override; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 52 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 53 | ReadReturn read(void) override; |
| 54 | void write(double value) override; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 55 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 56 | private: |
| 57 | /* |
| 58 | * _lock will be used to make sure _updated & _value are updated |
| 59 | * together. |
| 60 | */ |
| 61 | std::mutex _lock; |
| 62 | std::chrono::high_resolution_clock::time_point _updated; |
| 63 | double _value = 0; |
Patrick Venture | 863b924 | 2018-03-08 08:29:23 -0800 | [diff] [blame] | 64 | }; |