175e56c67SPatrick Venture #pragma once 275e56c67SPatrick Venture 375e56c67SPatrick Venture #include <chrono> 475e56c67SPatrick Venture #include <string> 575e56c67SPatrick Venture 6043d3230SPatrick Venture namespace hwmonio 7043d3230SPatrick Venture { 875e56c67SPatrick Venture 975e56c67SPatrick Venture static constexpr auto retries = 10; 1075e56c67SPatrick Venture static constexpr auto delay = std::chrono::milliseconds{100}; 1175e56c67SPatrick Venture 12*caaebd1fSPatrick Venture /** @class FileSystemInterface 13*caaebd1fSPatrick Venture * @brief Abstract base class allowing testing of HwmonIO. 14*caaebd1fSPatrick Venture * 15*caaebd1fSPatrick Venture * This is used to provide testing of behaviors within HwmonIO. 16*caaebd1fSPatrick Venture */ 17*caaebd1fSPatrick Venture class FileSystemInterface 18*caaebd1fSPatrick Venture { 19*caaebd1fSPatrick Venture public: 20*caaebd1fSPatrick Venture virtual ~FileSystemInterface() = default; 21*caaebd1fSPatrick Venture virtual int64_t read(const std::string& path) const = 0; 22*caaebd1fSPatrick Venture virtual void write(const std::string& path, uint32_t value) const = 0; 23*caaebd1fSPatrick Venture }; 24*caaebd1fSPatrick Venture 25*caaebd1fSPatrick Venture class FileSystem : public FileSystemInterface 26*caaebd1fSPatrick Venture { 27*caaebd1fSPatrick Venture public: 28*caaebd1fSPatrick Venture int64_t read(const std::string& path) const override; 29*caaebd1fSPatrick Venture void write(const std::string& path, uint32_t value) const override; 30*caaebd1fSPatrick Venture }; 31*caaebd1fSPatrick Venture 32*caaebd1fSPatrick Venture extern FileSystem fileSystemImpl; 33*caaebd1fSPatrick Venture 3450552377SPatrick Venture /** @class HwmonIOInterface 3550552377SPatrick Venture * @brief Abstract base class defining a HwmonIOInterface. 3650552377SPatrick Venture * 3750552377SPatrick Venture * This is initially to provide easier testing through injection, 3850552377SPatrick Venture * however, could in theory support non-sysfs handling of hwmon IO. 3950552377SPatrick Venture */ 4050552377SPatrick Venture class HwmonIOInterface 4150552377SPatrick Venture { 4250552377SPatrick Venture public: 4350552377SPatrick Venture virtual ~HwmonIOInterface() = default; 4450552377SPatrick Venture 45043d3230SPatrick Venture virtual int64_t read(const std::string& type, const std::string& id, 46043d3230SPatrick Venture const std::string& sensor, size_t retries, 4750552377SPatrick Venture std::chrono::milliseconds delay) const = 0; 4850552377SPatrick Venture 49043d3230SPatrick Venture virtual void write(uint32_t val, const std::string& type, 50043d3230SPatrick Venture const std::string& id, const std::string& sensor, 5150552377SPatrick Venture size_t retries, 5250552377SPatrick Venture std::chrono::milliseconds delay) const = 0; 5350552377SPatrick Venture 5450552377SPatrick Venture virtual std::string path() const = 0; 5550552377SPatrick Venture }; 5650552377SPatrick Venture 5775e56c67SPatrick Venture /** @class HwmonIO 5875e56c67SPatrick Venture * @brief Convenience wrappers for HWMON sysfs attribute IO. 5975e56c67SPatrick Venture * 6075e56c67SPatrick Venture * Unburden the rest of the application from having to check 6175e56c67SPatrick Venture * ENOENT after every hwmon attribute io operation. Hwmon 6275e56c67SPatrick Venture * device drivers can be unbound at any time; the program 6375e56c67SPatrick Venture * cannot always be terminated externally before we try to 6475e56c67SPatrick Venture * do an io. 6575e56c67SPatrick Venture */ 6650552377SPatrick Venture class HwmonIO : public HwmonIOInterface 6775e56c67SPatrick Venture { 6875e56c67SPatrick Venture public: 6975e56c67SPatrick Venture HwmonIO() = delete; 7075e56c67SPatrick Venture HwmonIO(const HwmonIO&) = default; 7175e56c67SPatrick Venture HwmonIO(HwmonIO&&) = default; 7275e56c67SPatrick Venture HwmonIO& operator=(const HwmonIO&) = default; 7375e56c67SPatrick Venture HwmonIO& operator=(HwmonIO&&) = default; 7475e56c67SPatrick Venture ~HwmonIO() = default; 7575e56c67SPatrick Venture 7675e56c67SPatrick Venture /** @brief Constructor 7775e56c67SPatrick Venture * 7875e56c67SPatrick Venture * @param[in] path - hwmon instance root - eg: 7975e56c67SPatrick Venture * /sys/class/hwmon/hwmon<N> 8075e56c67SPatrick Venture */ 81*caaebd1fSPatrick Venture explicit HwmonIO(const std::string& path, 82*caaebd1fSPatrick Venture const FileSystemInterface* intf = &fileSystemImpl); 8375e56c67SPatrick Venture 8475e56c67SPatrick Venture /** @brief Perform formatted hwmon sysfs read. 8575e56c67SPatrick Venture * 8675e56c67SPatrick Venture * Propagates any exceptions other than ENOENT. 8775e56c67SPatrick Venture * ENOENT will result in a call to exit(0) in case 8875e56c67SPatrick Venture * the underlying hwmon driver is unbound and 8975e56c67SPatrick Venture * the program is inadvertently left running. 9075e56c67SPatrick Venture * 9175e56c67SPatrick Venture * For possibly transient errors will retry up to 9275e56c67SPatrick Venture * the specified number of times. 9375e56c67SPatrick Venture * 9475e56c67SPatrick Venture * @param[in] type - The hwmon type (ex. temp). 9575e56c67SPatrick Venture * @param[in] id - The hwmon id (ex. 1). 9675e56c67SPatrick Venture * @param[in] sensor - The hwmon sensor (ex. input). 9775e56c67SPatrick Venture * @param[in] retries - The number of times to retry. 9875e56c67SPatrick Venture * @param[in] delay - The time to sleep between retry attempts. 9975e56c67SPatrick Venture * 10075e56c67SPatrick Venture * @return val - The read value. 10175e56c67SPatrick Venture */ 102043d3230SPatrick Venture int64_t read(const std::string& type, const std::string& id, 103043d3230SPatrick Venture const std::string& sensor, size_t retries, 10450552377SPatrick Venture std::chrono::milliseconds delay) const override; 10575e56c67SPatrick Venture 10675e56c67SPatrick Venture /** @brief Perform formatted hwmon sysfs write. 10775e56c67SPatrick Venture * 10875e56c67SPatrick Venture * Propagates any exceptions other than ENOENT. 10975e56c67SPatrick Venture * ENOENT will result in a call to exit(0) in case 11075e56c67SPatrick Venture * the underlying hwmon driver is unbound and 11175e56c67SPatrick Venture * the program is inadvertently left running. 11275e56c67SPatrick Venture * 11375e56c67SPatrick Venture * For possibly transient errors will retry up to 11475e56c67SPatrick Venture * the specified number of times. 11575e56c67SPatrick Venture * 11675e56c67SPatrick Venture * @param[in] val - The value to be written. 11775e56c67SPatrick Venture * @param[in] type - The hwmon type (ex. fan). 11875e56c67SPatrick Venture * @param[in] id - The hwmon id (ex. 1). 11975e56c67SPatrick Venture * @param[in] retries - The number of times to retry. 12075e56c67SPatrick Venture * @param[in] delay - The time to sleep between retry attempts. 12175e56c67SPatrick Venture */ 122043d3230SPatrick Venture void write(uint32_t val, const std::string& type, const std::string& id, 123043d3230SPatrick Venture const std::string& sensor, size_t retries, 12450552377SPatrick Venture std::chrono::milliseconds delay) const override; 12575e56c67SPatrick Venture 12675e56c67SPatrick Venture /** @brief Hwmon instance path access. 12775e56c67SPatrick Venture * 12875e56c67SPatrick Venture * @return path - The hwmon instance path. 12975e56c67SPatrick Venture */ 13050552377SPatrick Venture std::string path() const override; 13175e56c67SPatrick Venture 13275e56c67SPatrick Venture private: 1334d9506ccSPatrick Venture std::string _p; 134*caaebd1fSPatrick Venture const FileSystemInterface* _intf; 13575e56c67SPatrick Venture }; 13616805a6aSPatrick Venture 13775e56c67SPatrick Venture } // namespace hwmonio 138