Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <string> |
| 5 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 6 | namespace hwmonio |
| 7 | { |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 8 | |
| 9 | static constexpr auto retries = 10; |
| 10 | static constexpr auto delay = std::chrono::milliseconds{100}; |
| 11 | |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 12 | /** @class HwmonIOInterface |
| 13 | * @brief Abstract base class defining a HwmonIOInterface. |
| 14 | * |
| 15 | * This is initially to provide easier testing through injection, |
| 16 | * however, could in theory support non-sysfs handling of hwmon IO. |
| 17 | */ |
| 18 | class HwmonIOInterface |
| 19 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 20 | public: |
| 21 | virtual ~HwmonIOInterface() = default; |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 22 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 23 | virtual int64_t read(const std::string& type, const std::string& id, |
| 24 | const std::string& sensor, size_t retries, |
| 25 | std::chrono::milliseconds delay) const = 0; |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 26 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 27 | virtual void write(uint32_t val, const std::string& type, |
| 28 | const std::string& id, const std::string& sensor, |
| 29 | size_t retries, |
| 30 | std::chrono::milliseconds delay) const = 0; |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 31 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 32 | virtual std::string path() const = 0; |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 33 | }; |
| 34 | |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 35 | /** @class HwmonIO |
| 36 | * @brief Convenience wrappers for HWMON sysfs attribute IO. |
| 37 | * |
| 38 | * Unburden the rest of the application from having to check |
| 39 | * ENOENT after every hwmon attribute io operation. Hwmon |
| 40 | * device drivers can be unbound at any time; the program |
| 41 | * cannot always be terminated externally before we try to |
| 42 | * do an io. |
| 43 | */ |
Patrick Venture | 5055237 | 2018-06-07 10:53:56 -0700 | [diff] [blame] | 44 | class HwmonIO : public HwmonIOInterface |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 45 | { |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 46 | public: |
| 47 | HwmonIO() = delete; |
| 48 | HwmonIO(const HwmonIO&) = default; |
| 49 | HwmonIO(HwmonIO&&) = default; |
| 50 | HwmonIO& operator=(const HwmonIO&) = default; |
| 51 | HwmonIO& operator=(HwmonIO&&) = default; |
| 52 | ~HwmonIO() = default; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 53 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 54 | /** @brief Constructor |
| 55 | * |
| 56 | * @param[in] path - hwmon instance root - eg: |
| 57 | * /sys/class/hwmon/hwmon<N> |
| 58 | */ |
| 59 | explicit HwmonIO(const std::string& path); |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 60 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 61 | /** @brief Perform formatted hwmon sysfs read. |
| 62 | * |
| 63 | * Propagates any exceptions other than ENOENT. |
| 64 | * ENOENT will result in a call to exit(0) in case |
| 65 | * the underlying hwmon driver is unbound and |
| 66 | * the program is inadvertently left running. |
| 67 | * |
| 68 | * For possibly transient errors will retry up to |
| 69 | * the specified number of times. |
| 70 | * |
| 71 | * @param[in] type - The hwmon type (ex. temp). |
| 72 | * @param[in] id - The hwmon id (ex. 1). |
| 73 | * @param[in] sensor - The hwmon sensor (ex. input). |
| 74 | * @param[in] retries - The number of times to retry. |
| 75 | * @param[in] delay - The time to sleep between retry attempts. |
| 76 | * |
| 77 | * @return val - The read value. |
| 78 | */ |
| 79 | int64_t read(const std::string& type, const std::string& id, |
| 80 | const std::string& sensor, size_t retries, |
| 81 | std::chrono::milliseconds delay) const override; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 82 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 83 | /** @brief Perform formatted hwmon sysfs write. |
| 84 | * |
| 85 | * Propagates any exceptions other than ENOENT. |
| 86 | * ENOENT will result in a call to exit(0) in case |
| 87 | * the underlying hwmon driver is unbound and |
| 88 | * the program is inadvertently left running. |
| 89 | * |
| 90 | * For possibly transient errors will retry up to |
| 91 | * the specified number of times. |
| 92 | * |
| 93 | * @param[in] val - The value to be written. |
| 94 | * @param[in] type - The hwmon type (ex. fan). |
| 95 | * @param[in] id - The hwmon id (ex. 1). |
| 96 | * @param[in] retries - The number of times to retry. |
| 97 | * @param[in] delay - The time to sleep between retry attempts. |
| 98 | */ |
| 99 | void write(uint32_t val, const std::string& type, const std::string& id, |
| 100 | const std::string& sensor, size_t retries, |
| 101 | std::chrono::milliseconds delay) const override; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 102 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 103 | /** @brief Hwmon instance path access. |
| 104 | * |
| 105 | * @return path - The hwmon instance path. |
| 106 | */ |
| 107 | std::string path() const override; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 108 | |
Patrick Venture | 043d323 | 2018-08-31 10:10:53 -0700 | [diff] [blame] | 109 | private: |
Patrick Venture | 4d9506c | 2018-12-19 14:19:34 -0800 | [diff] [blame] | 110 | std::string _p; |
Patrick Venture | 75e56c6 | 2018-04-20 18:10:15 -0700 | [diff] [blame] | 111 | }; |
| 112 | } // namespace hwmonio |
| 113 | |
| 114 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |