blob: 3bcca53a794fc3b2a1fdd7d6957a008b9f5f7f99 [file] [log] [blame]
Brad Bishop26b815f2017-01-04 13:32:47 -05001#pragma once
Patrick Williams3667cf32015-10-20 22:39:11 -05002
Brad Bishop754d38c2017-09-08 00:46:58 -04003#include <chrono>
Matt Spinler3b8e36e2017-07-28 10:44:45 -05004#include <exception>
Patrick Williams3667cf32015-10-20 22:39:11 -05005#include <fstream>
6#include <string>
7
Patrick Venture1e6324f2017-06-01 14:07:05 -07008namespace sysfs {
9
Brad Bishopa9b5f052017-01-17 14:50:08 -050010inline 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 Williams3667cf32015-10-20 22:39:11 -050014{
15 using namespace std::literals;
16
Patrick Venture9331ab72018-01-29 09:48:47 -080017 if (entry.empty()) {
18 return path + "/"s + type + id;
19 }
20
Patrick Williams3667cf32015-10-20 22:39:11 -050021 return path + "/"s + type + id + "_"s + entry;
22}
23
Brad Bishopf4bf63a2017-08-28 15:39:19 -040024/** @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 */
38std::string findPhandleMatch(
39 const std::string& iochanneldir,
40 const std::string& phandledir);
Brad Bishop613a5b32017-01-05 20:58:13 -050041
42/** @brief Find hwmon instances
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 */
52std::string findHwmon(const std::string& ofNode);
53
Brad Bishop431d26a2017-08-25 09:47:58 -040054/** @brief Return the path to use for a call out.
55 *
56 * Return an empty string if a callout path cannot be
57 * found.
58 *
59 * @param[in] instancePath - /sys/class/hwmon/hwmon<N> path.
60 *
61 * @return Path to use for call out
62 */
63std::string findCalloutPath(const std::string& instancePath);
64
Brad Bishop8b574a72017-08-25 16:17:19 -040065namespace hwmonio
66{
Brad Bishop754d38c2017-09-08 00:46:58 -040067static constexpr auto retries = 10;
68static constexpr auto delay = std::chrono::milliseconds{100};
Brad Bishop8b574a72017-08-25 16:17:19 -040069
70/** @class HwmonIO
71 * @brief Convenience wrappers for HWMON sysfs attribute IO.
72 *
73 * Unburden the rest of the application from having to check
74 * ENOENT after every hwmon attribute io operation. Hwmon
75 * device drivers can be unbound at any time; the program
76 * cannot always be terminated externally before we try to
77 * do an io.
78 */
79class HwmonIO
80{
81 public:
82 HwmonIO() = delete;
83 HwmonIO(const HwmonIO&) = default;
84 HwmonIO(HwmonIO&&) = default;
85 HwmonIO& operator=(const HwmonIO&) = default;
86 HwmonIO& operator=(HwmonIO&&) = default;
87 ~HwmonIO() = default;
88
89 /** @brief Constructor
90 *
91 * @param[in] path - hwmon instance root - eg:
92 * /sys/class/hwmon/hwmon<N>
93 */
94 explicit HwmonIO(const std::string& path);
95
96 /** @brief Perform formatted hwmon sysfs read.
97 *
Gunnar Millsca64c252017-10-25 17:13:45 -050098 * Propagates any exceptions other than ENOENT.
Brad Bishop8b574a72017-08-25 16:17:19 -040099 * ENOENT will result in a call to exit(0) in case
100 * the underlying hwmon driver is unbound and
101 * the program is inadvertently left running.
102 *
Brad Bishop754d38c2017-09-08 00:46:58 -0400103 * For possibly transient errors will retry up to
104 * the specified number of times.
105 *
Brad Bishop8b574a72017-08-25 16:17:19 -0400106 * @param[in] type - The hwmon type (ex. temp).
107 * @param[in] id - The hwmon id (ex. 1).
108 * @param[in] sensor - The hwmon sensor (ex. input).
Brad Bishop754d38c2017-09-08 00:46:58 -0400109 * @param[in] retries - The number of times to retry.
110 * @param[in] delay - The time to sleep between retry attempts.
Brad Bishop8b574a72017-08-25 16:17:19 -0400111 *
112 * @return val - The read value.
113 */
Matt Spinlerfee106b2017-11-29 15:18:05 -0600114 int64_t read(
Brad Bishop8b574a72017-08-25 16:17:19 -0400115 const std::string& type,
116 const std::string& id,
Brad Bishop754d38c2017-09-08 00:46:58 -0400117 const std::string& sensor,
118 size_t retries,
119 std::chrono::milliseconds delay) const;
Brad Bishop8b574a72017-08-25 16:17:19 -0400120
121 /** @brief Perform formatted hwmon sysfs write.
122 *
Gunnar Millsca64c252017-10-25 17:13:45 -0500123 * Propagates any exceptions other than ENOENT.
Brad Bishop8b574a72017-08-25 16:17:19 -0400124 * ENOENT will result in a call to exit(0) in case
125 * the underlying hwmon driver is unbound and
126 * the program is inadvertently left running.
127 *
Brad Bishop754d38c2017-09-08 00:46:58 -0400128 * For possibly transient errors will retry up to
129 * the specified number of times.
130 *
Brad Bishop8b574a72017-08-25 16:17:19 -0400131 * @param[in] val - The value to be written.
132 * @param[in] type - The hwmon type (ex. fan).
133 * @param[in] id - The hwmon id (ex. 1).
Brad Bishop754d38c2017-09-08 00:46:58 -0400134 * @param[in] retries - The number of times to retry.
135 * @param[in] delay - The time to sleep between retry attempts.
Brad Bishop8b574a72017-08-25 16:17:19 -0400136 */
137 void write(
138 uint32_t val,
139 const std::string& type,
140 const std::string& id,
Brad Bishop754d38c2017-09-08 00:46:58 -0400141 const std::string& sensor,
142 size_t retries,
143 std::chrono::milliseconds delay) const;
144
Brad Bishop8b574a72017-08-25 16:17:19 -0400145
146 /** @brief Hwmon instance path access.
147 *
148 * @return path - The hwmon instance path.
149 */
150 std::string path() const;
151
152 private:
153 std::string p;
154};
155} // namespace hwmonio
Patrick Venture1e6324f2017-06-01 14:07:05 -0700156}
157
Brad Bishop03476f12016-12-19 13:09:12 -0500158// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4