xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision f4bf63ad)
126b815faSBrad Bishop #pragma once
26292aeedSMatthew Barth 
33b8e36e2SMatt Spinler #include <exception>
46292aeedSMatthew Barth #include <fstream>
56292aeedSMatthew Barth #include <string>
66292aeedSMatthew Barth 
71e6324faSPatrick Venture namespace sysfs {
81e6324faSPatrick Venture 
93b8e36e2SMatt Spinler /**
103b8e36e2SMatt Spinler  * @class DeviceBusyException
113b8e36e2SMatt Spinler  *
123b8e36e2SMatt Spinler  * An internal exception which will be thrown when
133b8e36e2SMatt Spinler  * readSysfsWithCallout() hits an EAGAIN.  Will never bubble
143b8e36e2SMatt Spinler  * up to terminate the application, nor does it need to be
153b8e36e2SMatt Spinler  * reported.
163b8e36e2SMatt Spinler  */
173b8e36e2SMatt Spinler class DeviceBusyException : public std::runtime_error
183b8e36e2SMatt Spinler {
193b8e36e2SMatt Spinler     public:
203b8e36e2SMatt Spinler 
213b8e36e2SMatt Spinler         DeviceBusyException(const std::string& path) :
223b8e36e2SMatt Spinler             std::runtime_error(path + " busy")
233b8e36e2SMatt Spinler         {
243b8e36e2SMatt Spinler         }
253b8e36e2SMatt Spinler };
263b8e36e2SMatt Spinler 
27a9b5f05bSBrad Bishop inline std::string make_sysfs_path(const std::string& path,
286292aeedSMatthew Barth                                    const std::string& type,
296292aeedSMatthew Barth                                    const std::string& id,
306292aeedSMatthew Barth                                    const std::string& entry)
316292aeedSMatthew Barth {
326292aeedSMatthew Barth     using namespace std::literals;
336292aeedSMatthew Barth 
346292aeedSMatthew Barth     return path + "/"s + type + id + "_"s + entry;
356292aeedSMatthew Barth }
366292aeedSMatthew Barth 
37*f4bf63adSBrad Bishop /** @brief Return the path to the phandle file matching value in io-channels.
38*f4bf63adSBrad Bishop  *
39*f4bf63adSBrad Bishop  *  This function will take two passed in paths.
40*f4bf63adSBrad Bishop  *  One path is used to find the io-channels file.
41*f4bf63adSBrad Bishop  *  The other path is used to find the phandle file.
42*f4bf63adSBrad Bishop  *  The 4 byte phandle value is read from the phandle file(s).
43*f4bf63adSBrad Bishop  *  The 4 byte phandle value and 4 byte index value is read from io-channels.
44*f4bf63adSBrad Bishop  *  When a match is found, the path to the matching phandle file is returned.
45*f4bf63adSBrad Bishop  *
46*f4bf63adSBrad Bishop  *  @param[in] iochanneldir - Path to file for getting phandle from io-channels
47*f4bf63adSBrad Bishop  *  @param[in] phandledir - Path to use for reading from phandle file
48*f4bf63adSBrad Bishop  *
49*f4bf63adSBrad Bishop  *  @return Path to phandle file with value matching that in io-channels
50*f4bf63adSBrad Bishop  */
51*f4bf63adSBrad Bishop std::string findPhandleMatch(
52*f4bf63adSBrad Bishop         const std::string& iochanneldir,
53*f4bf63adSBrad Bishop         const std::string& phandledir);
54613a5b37SBrad Bishop 
55613a5b37SBrad Bishop /** @brief Find hwmon instances
56613a5b37SBrad Bishop  *
57613a5b37SBrad Bishop  *  Look for a matching hwmon instance given an
58613a5b37SBrad Bishop  *  open firmware device path.
59613a5b37SBrad Bishop  *
60613a5b37SBrad Bishop  *  @param[in] ofNode- The open firmware device path.
61613a5b37SBrad Bishop  *
62613a5b37SBrad Bishop  *  @returns[in] - The hwmon instance path or an empty
63613a5b37SBrad Bishop  *                 string if no match is found.
64613a5b37SBrad Bishop  */
65613a5b37SBrad Bishop std::string findHwmon(const std::string& ofNode);
66613a5b37SBrad Bishop 
674db64422SBrad Bishop /** @brief Read an hwmon sysfs value.
684db64422SBrad Bishop  *
694db64422SBrad Bishop  *  Calls exit(3) with bad status on failure.
704db64422SBrad Bishop  *
714db64422SBrad Bishop  *  @param[in] root - The hwmon class root.
724db64422SBrad Bishop  *  @param[in] instance - The hwmon instance (ex. hwmon1).
734db64422SBrad Bishop  *  @param[in] type - The hwmon type (ex. temp).
744db64422SBrad Bishop  *  @param[in] id - The hwmon id (ex. 1).
754db64422SBrad Bishop  *  @param[in] sensor - The hwmon sensor (ex. input).
763b8e36e2SMatt Spinler  *  @param[in] throwDeviceBusy - will throw a DeviceBusyException
773b8e36e2SMatt Spinler  *             on an EAGAIN errno instead of an error log exception.
784db64422SBrad Bishop  *
794db64422SBrad Bishop  *  @returns - The read value.
804db64422SBrad Bishop  */
814db64422SBrad Bishop int readSysfsWithCallout(const std::string& root,
824db64422SBrad Bishop                          const std::string& instance,
834db64422SBrad Bishop                          const std::string& type,
844db64422SBrad Bishop                          const std::string& id,
853b8e36e2SMatt Spinler                          const std::string& sensor,
863b8e36e2SMatt Spinler                          bool throwDeviceBusy = true);
874db64422SBrad Bishop 
88048ac87fSMatthew Barth  /** @brief Write a hwmon sysfs value
89048ac87fSMatthew Barth   *
90048ac87fSMatthew Barth   *  Calls exit(3) with bad status on failure
91048ac87fSMatthew Barth   *
92048ac87fSMatthew Barth   *  @param[in] value - The value to be written
93048ac87fSMatthew Barth   *  @param[in] root - The hwmon class root.
94048ac87fSMatthew Barth   *  @param[in] instance - The hwmon instance (ex. hwmon1).
95048ac87fSMatthew Barth   *  @param[in] type - The hwmon type (ex. fan).
96048ac87fSMatthew Barth   *  @param[in] id - The hwmon id (ex. 1).
97048ac87fSMatthew Barth   *  @param[in] sensor - The hwmon sensor (ex. target).
98048ac87fSMatthew Barth   *
99048ac87fSMatthew Barth   *  @returns - The value written
100048ac87fSMatthew Barth   */
101048ac87fSMatthew Barth uint64_t writeSysfsWithCallout(const uint64_t& value,
102048ac87fSMatthew Barth                                const std::string& root,
103048ac87fSMatthew Barth                                const std::string& instance,
104048ac87fSMatthew Barth                                const std::string& type,
105048ac87fSMatthew Barth                                const std::string& id,
106048ac87fSMatthew Barth                                const std::string& sensor);
107048ac87fSMatthew Barth 
1081e6324faSPatrick Venture }
1091e6324faSPatrick Venture 
11003476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
111