blob: f0e92db37841c463ec9c1b349699779f2097b6d7 [file] [log] [blame]
Brad Bishop26b815f2017-01-04 13:32:47 -05001#pragma once
Patrick Williams3667cf32015-10-20 22:39:11 -05002
Matt Spinler3b8e36e2017-07-28 10:44:45 -05003#include <exception>
Patrick Williams3667cf32015-10-20 22:39:11 -05004#include <fstream>
5#include <string>
6
Patrick Venture1e6324f2017-06-01 14:07:05 -07007namespace sysfs {
8
Matt Spinler3b8e36e2017-07-28 10:44:45 -05009/**
10 * @class DeviceBusyException
11 *
12 * An internal exception which will be thrown when
13 * readSysfsWithCallout() hits an EAGAIN. Will never bubble
14 * up to terminate the application, nor does it need to be
15 * reported.
16 */
17class DeviceBusyException : public std::runtime_error
18{
19 public:
20
21 DeviceBusyException(const std::string& path) :
22 std::runtime_error(path + " busy")
23 {
24 }
25};
26
Brad Bishopa9b5f052017-01-17 14:50:08 -050027inline std::string make_sysfs_path(const std::string& path,
28 const std::string& type,
29 const std::string& id,
30 const std::string& entry)
Patrick Williams3667cf32015-10-20 22:39:11 -050031{
32 using namespace std::literals;
33
34 return path + "/"s + type + id + "_"s + entry;
35}
36
Brad Bishopf4bf63a2017-08-28 15:39:19 -040037/** @brief Return the path to the phandle file matching value in io-channels.
38 *
39 * This function will take two passed in paths.
40 * One path is used to find the io-channels file.
41 * The other path is used to find the phandle file.
42 * The 4 byte phandle value is read from the phandle file(s).
43 * The 4 byte phandle value and 4 byte index value is read from io-channels.
44 * When a match is found, the path to the matching phandle file is returned.
45 *
46 * @param[in] iochanneldir - Path to file for getting phandle from io-channels
47 * @param[in] phandledir - Path to use for reading from phandle file
48 *
49 * @return Path to phandle file with value matching that in io-channels
50 */
51std::string findPhandleMatch(
52 const std::string& iochanneldir,
53 const std::string& phandledir);
Brad Bishop613a5b32017-01-05 20:58:13 -050054
55/** @brief Find hwmon instances
56 *
57 * Look for a matching hwmon instance given an
58 * open firmware device path.
59 *
60 * @param[in] ofNode- The open firmware device path.
61 *
62 * @returns[in] - The hwmon instance path or an empty
63 * string if no match is found.
64 */
65std::string findHwmon(const std::string& ofNode);
66
Brad Bishop431d26a2017-08-25 09:47:58 -040067/** @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 */
76std::string findCalloutPath(const std::string& instancePath);
77
Brad Bishop4db64422017-02-16 11:33:32 -050078/** @brief Read an hwmon sysfs value.
79 *
80 * Calls exit(3) with bad status on failure.
81 *
82 * @param[in] root - The hwmon class root.
83 * @param[in] instance - The hwmon instance (ex. hwmon1).
84 * @param[in] type - The hwmon type (ex. temp).
85 * @param[in] id - The hwmon id (ex. 1).
86 * @param[in] sensor - The hwmon sensor (ex. input).
Matt Spinler3b8e36e2017-07-28 10:44:45 -050087 * @param[in] throwDeviceBusy - will throw a DeviceBusyException
88 * on an EAGAIN errno instead of an error log exception.
Brad Bishop4db64422017-02-16 11:33:32 -050089 *
90 * @returns - The read value.
91 */
92int readSysfsWithCallout(const std::string& root,
93 const std::string& instance,
94 const std::string& type,
95 const std::string& id,
Matt Spinler3b8e36e2017-07-28 10:44:45 -050096 const std::string& sensor,
97 bool throwDeviceBusy = true);
Brad Bishop4db64422017-02-16 11:33:32 -050098
Matthew Barth048ac872017-03-09 14:36:08 -060099 /** @brief Write a hwmon sysfs value
100 *
101 * Calls exit(3) with bad status on failure
102 *
103 * @param[in] value - The value to be written
104 * @param[in] root - The hwmon class root.
105 * @param[in] instance - The hwmon instance (ex. hwmon1).
106 * @param[in] type - The hwmon type (ex. fan).
107 * @param[in] id - The hwmon id (ex. 1).
108 * @param[in] sensor - The hwmon sensor (ex. target).
109 *
110 * @returns - The value written
111 */
112uint64_t writeSysfsWithCallout(const uint64_t& value,
113 const std::string& root,
114 const std::string& instance,
115 const std::string& type,
116 const std::string& id,
117 const std::string& sensor);
118
Patrick Venture1e6324f2017-06-01 14:07:05 -0700119}
120
Brad Bishop03476f12016-12-19 13:09:12 -0500121// vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4