1 #pragma once 2 3 #include <chrono> 4 #include <string> 5 6 namespace hwmonio 7 { 8 9 static constexpr auto retries = 10; 10 static constexpr auto delay = std::chrono::milliseconds{100}; 11 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 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 { 42 public: 43 virtual ~HwmonIOInterface() = default; 44 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; 48 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; 53 54 virtual std::string path() const = 0; 55 }; 56 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 */ 66 class HwmonIO : public HwmonIOInterface 67 { 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; 75 76 /** @brief Constructor 77 * 78 * @param[in] path - hwmon instance root - eg: 79 * /sys/class/hwmon/hwmon<N> 80 */ 81 explicit HwmonIO(const std::string& path, 82 const FileSystemInterface* intf = &fileSystemImpl); 83 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; 105 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; 125 126 /** @brief Hwmon instance path access. 127 * 128 * @return path - The hwmon instance path. 129 */ 130 std::string path() const override; 131 132 private: 133 std::string _p; 134 const FileSystemInterface* _intf; 135 }; 136 137 } // namespace hwmonio 138 139 // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 140