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