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