blob: c1799f67dc845411efcc122537032e58ce44f6de [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 Venture043d3232018-08-31 10:10:53 -07008namespace sysfs
9{
Patrick Venture1e6324f2017-06-01 14:07:05 -070010
Brad Bishopa9b5f052017-01-17 14:50:08 -050011inline std::string make_sysfs_path(const std::string& path,
12 const std::string& type,
13 const std::string& id,
14 const std::string& entry)
Patrick Williams3667cf32015-10-20 22:39:11 -050015{
16 using namespace std::literals;
17
Patrick Venture043d3232018-08-31 10:10:53 -070018 if (entry.empty())
19 {
Patrick Venture9331ab72018-01-29 09:48:47 -080020 return path + "/"s + type + id;
21 }
22
Patrick Williams3667cf32015-10-20 22:39:11 -050023 return path + "/"s + type + id + "_"s + entry;
24}
25
Brad Bishopf4bf63a2017-08-28 15:39:19 -040026/** @brief Return the path to the phandle file matching value in io-channels.
27 *
28 * This function will take two passed in paths.
29 * One path is used to find the io-channels file.
30 * The other path is used to find the phandle file.
31 * The 4 byte phandle value is read from the phandle file(s).
32 * The 4 byte phandle value and 4 byte index value is read from io-channels.
33 * When a match is found, the path to the matching phandle file is returned.
34 *
35 * @param[in] iochanneldir - Path to file for getting phandle from io-channels
36 * @param[in] phandledir - Path to use for reading from phandle file
37 *
38 * @return Path to phandle file with value matching that in io-channels
39 */
Patrick Venture043d3232018-08-31 10:10:53 -070040std::string findPhandleMatch(const std::string& iochanneldir,
41 const std::string& phandledir);
Brad Bishop613a5b32017-01-05 20:58:13 -050042
Matt Spinler31dbe062018-03-05 12:01:48 -060043/** @brief Find hwmon instances from an open-firmware device tree path
Brad Bishop613a5b32017-01-05 20:58:13 -050044 *
45 * Look for a matching hwmon instance given an
46 * open firmware device path.
47 *
48 * @param[in] ofNode- The open firmware device path.
49 *
50 * @returns[in] - The hwmon instance path or an empty
51 * string if no match is found.
52 */
Matt Spinler31dbe062018-03-05 12:01:48 -060053std::string findHwmonFromOFPath(const std::string& ofNode);
Brad Bishop613a5b32017-01-05 20:58:13 -050054
Matt Spinler626df172018-03-05 12:03:55 -060055/** @brief Find hwmon instances from a device path
56 *
57 * Look for a matching hwmon instance given a device path that
58 * starts with /devices. This path is the DEVPATH udev attribute
59 * for the device except it has the '/hwmon/hwmonN' stripped off.
60 *
61 * @param[in] devPath - The device path.
62 *
63 * @return - The hwmon instance path or an empty
64 * string if no match is found.
65 */
66std::string findHwmonFromDevPath(const std::string& devPath);
67
Brad Bishop431d26a2017-08-25 09:47:58 -040068/** @brief Return the path to use for a call out.
69 *
70 * Return an empty string if a callout path cannot be
71 * found.
72 *
73 * @param[in] instancePath - /sys/class/hwmon/hwmon<N> path.
74 *
75 * @return Path to use for call out
76 */
77std::string findCalloutPath(const std::string& instancePath);
78
Patrick Venturedd467392018-04-20 17:22:49 -070079} // namespace sysfs
Patrick Venture1e6324f2017-06-01 14:07:05 -070080
Brad Bishop03476f12016-12-19 13:09:12 -050081// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4