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