Brad Bishop | 26b815f | 2017-01-04 13:32:47 -0500 | [diff] [blame] | 1 | #pragma once |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 2 | |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 3 | #include <chrono> |
Matt Spinler | 3b8e36e | 2017-07-28 10:44:45 -0500 | [diff] [blame] | 4 | #include <exception> |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 5 | #include <fstream> |
| 6 | #include <string> |
| 7 | |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 8 | namespace sysfs { |
| 9 | |
Brad Bishop | a9b5f05 | 2017-01-17 14:50:08 -0500 | [diff] [blame] | 10 | inline std::string make_sysfs_path(const std::string& path, |
| 11 | const std::string& type, |
| 12 | const std::string& id, |
| 13 | const std::string& entry) |
Patrick Williams | 3667cf3 | 2015-10-20 22:39:11 -0500 | [diff] [blame] | 14 | { |
| 15 | using namespace std::literals; |
| 16 | |
| 17 | return path + "/"s + type + id + "_"s + entry; |
| 18 | } |
| 19 | |
Brad Bishop | f4bf63a | 2017-08-28 15:39:19 -0400 | [diff] [blame] | 20 | /** @brief Return the path to the phandle file matching value in io-channels. |
| 21 | * |
| 22 | * This function will take two passed in paths. |
| 23 | * One path is used to find the io-channels file. |
| 24 | * The other path is used to find the phandle file. |
| 25 | * The 4 byte phandle value is read from the phandle file(s). |
| 26 | * The 4 byte phandle value and 4 byte index value is read from io-channels. |
| 27 | * When a match is found, the path to the matching phandle file is returned. |
| 28 | * |
| 29 | * @param[in] iochanneldir - Path to file for getting phandle from io-channels |
| 30 | * @param[in] phandledir - Path to use for reading from phandle file |
| 31 | * |
| 32 | * @return Path to phandle file with value matching that in io-channels |
| 33 | */ |
| 34 | std::string findPhandleMatch( |
| 35 | const std::string& iochanneldir, |
| 36 | const std::string& phandledir); |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 37 | |
| 38 | /** @brief Find hwmon instances |
| 39 | * |
| 40 | * Look for a matching hwmon instance given an |
| 41 | * open firmware device path. |
| 42 | * |
| 43 | * @param[in] ofNode- The open firmware device path. |
| 44 | * |
| 45 | * @returns[in] - The hwmon instance path or an empty |
| 46 | * string if no match is found. |
| 47 | */ |
| 48 | std::string findHwmon(const std::string& ofNode); |
| 49 | |
Brad Bishop | 431d26a | 2017-08-25 09:47:58 -0400 | [diff] [blame] | 50 | /** @brief Return the path to use for a call out. |
| 51 | * |
| 52 | * Return an empty string if a callout path cannot be |
| 53 | * found. |
| 54 | * |
| 55 | * @param[in] instancePath - /sys/class/hwmon/hwmon<N> path. |
| 56 | * |
| 57 | * @return Path to use for call out |
| 58 | */ |
| 59 | std::string findCalloutPath(const std::string& instancePath); |
| 60 | |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 61 | namespace hwmonio |
| 62 | { |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 63 | static constexpr auto retries = 10; |
| 64 | static constexpr auto delay = std::chrono::milliseconds{100}; |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 65 | |
| 66 | /** @class HwmonIO |
| 67 | * @brief Convenience wrappers for HWMON sysfs attribute IO. |
| 68 | * |
| 69 | * Unburden the rest of the application from having to check |
| 70 | * ENOENT after every hwmon attribute io operation. Hwmon |
| 71 | * device drivers can be unbound at any time; the program |
| 72 | * cannot always be terminated externally before we try to |
| 73 | * do an io. |
| 74 | */ |
| 75 | class HwmonIO |
| 76 | { |
| 77 | public: |
| 78 | HwmonIO() = delete; |
| 79 | HwmonIO(const HwmonIO&) = default; |
| 80 | HwmonIO(HwmonIO&&) = default; |
| 81 | HwmonIO& operator=(const HwmonIO&) = default; |
| 82 | HwmonIO& operator=(HwmonIO&&) = default; |
| 83 | ~HwmonIO() = default; |
| 84 | |
| 85 | /** @brief Constructor |
| 86 | * |
| 87 | * @param[in] path - hwmon instance root - eg: |
| 88 | * /sys/class/hwmon/hwmon<N> |
| 89 | */ |
| 90 | explicit HwmonIO(const std::string& path); |
| 91 | |
| 92 | /** @brief Perform formatted hwmon sysfs read. |
| 93 | * |
Gunnar Mills | ca64c25 | 2017-10-25 17:13:45 -0500 | [diff] [blame] | 94 | * Propagates any exceptions other than ENOENT. |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 95 | * ENOENT will result in a call to exit(0) in case |
| 96 | * the underlying hwmon driver is unbound and |
| 97 | * the program is inadvertently left running. |
| 98 | * |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 99 | * For possibly transient errors will retry up to |
| 100 | * the specified number of times. |
| 101 | * |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 102 | * @param[in] type - The hwmon type (ex. temp). |
| 103 | * @param[in] id - The hwmon id (ex. 1). |
| 104 | * @param[in] sensor - The hwmon sensor (ex. input). |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 105 | * @param[in] retries - The number of times to retry. |
| 106 | * @param[in] delay - The time to sleep between retry attempts. |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 107 | * |
| 108 | * @return val - The read value. |
| 109 | */ |
| 110 | uint32_t read( |
| 111 | const std::string& type, |
| 112 | const std::string& id, |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 113 | const std::string& sensor, |
| 114 | size_t retries, |
| 115 | std::chrono::milliseconds delay) const; |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 116 | |
| 117 | /** @brief Perform formatted hwmon sysfs write. |
| 118 | * |
Gunnar Mills | ca64c25 | 2017-10-25 17:13:45 -0500 | [diff] [blame] | 119 | * Propagates any exceptions other than ENOENT. |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 120 | * ENOENT will result in a call to exit(0) in case |
| 121 | * the underlying hwmon driver is unbound and |
| 122 | * the program is inadvertently left running. |
| 123 | * |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 124 | * For possibly transient errors will retry up to |
| 125 | * the specified number of times. |
| 126 | * |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 127 | * @param[in] val - The value to be written. |
| 128 | * @param[in] type - The hwmon type (ex. fan). |
| 129 | * @param[in] id - The hwmon id (ex. 1). |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 130 | * @param[in] retries - The number of times to retry. |
| 131 | * @param[in] delay - The time to sleep between retry attempts. |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 132 | */ |
| 133 | void write( |
| 134 | uint32_t val, |
| 135 | const std::string& type, |
| 136 | const std::string& id, |
Brad Bishop | 754d38c | 2017-09-08 00:46:58 -0400 | [diff] [blame] | 137 | const std::string& sensor, |
| 138 | size_t retries, |
| 139 | std::chrono::milliseconds delay) const; |
| 140 | |
Brad Bishop | 8b574a7 | 2017-08-25 16:17:19 -0400 | [diff] [blame] | 141 | |
| 142 | /** @brief Hwmon instance path access. |
| 143 | * |
| 144 | * @return path - The hwmon instance path. |
| 145 | */ |
| 146 | std::string path() const; |
| 147 | |
| 148 | private: |
| 149 | std::string p; |
| 150 | }; |
| 151 | } // namespace hwmonio |
Patrick Venture | 1e6324f | 2017-06-01 14:07:05 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Brad Bishop | 03476f1 | 2016-12-19 13:09:12 -0500 | [diff] [blame] | 154 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |