xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision 3b8e36e2)
126b815faSBrad Bishop #pragma once
26292aeedSMatthew Barth 
3*3b8e36e2SMatt Spinler #include <exception>
46292aeedSMatthew Barth #include <fstream>
56292aeedSMatthew Barth #include <string>
66292aeedSMatthew Barth 
71e6324faSPatrick Venture namespace sysfs {
81e6324faSPatrick Venture 
9*3b8e36e2SMatt Spinler /**
10*3b8e36e2SMatt Spinler  * @class DeviceBusyException
11*3b8e36e2SMatt Spinler  *
12*3b8e36e2SMatt Spinler  * An internal exception which will be thrown when
13*3b8e36e2SMatt Spinler  * readSysfsWithCallout() hits an EAGAIN.  Will never bubble
14*3b8e36e2SMatt Spinler  * up to terminate the application, nor does it need to be
15*3b8e36e2SMatt Spinler  * reported.
16*3b8e36e2SMatt Spinler  */
17*3b8e36e2SMatt Spinler class DeviceBusyException : public std::runtime_error
18*3b8e36e2SMatt Spinler {
19*3b8e36e2SMatt Spinler     public:
20*3b8e36e2SMatt Spinler 
21*3b8e36e2SMatt Spinler         DeviceBusyException(const std::string& path) :
22*3b8e36e2SMatt Spinler             std::runtime_error(path + " busy")
23*3b8e36e2SMatt Spinler         {
24*3b8e36e2SMatt Spinler         }
25*3b8e36e2SMatt Spinler };
26*3b8e36e2SMatt 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 
37613a5b37SBrad Bishop 
38613a5b37SBrad Bishop /** @brief Find hwmon instances
39613a5b37SBrad Bishop  *
40613a5b37SBrad Bishop  *  Look for a matching hwmon instance given an
41613a5b37SBrad Bishop  *  open firmware device path.
42613a5b37SBrad Bishop  *
43613a5b37SBrad Bishop  *  @param[in] ofNode- The open firmware device path.
44613a5b37SBrad Bishop  *
45613a5b37SBrad Bishop  *  @returns[in] - The hwmon instance path or an empty
46613a5b37SBrad Bishop  *                 string if no match is found.
47613a5b37SBrad Bishop  */
48613a5b37SBrad Bishop std::string findHwmon(const std::string& ofNode);
49613a5b37SBrad Bishop 
504db64422SBrad Bishop /** @brief Read an hwmon sysfs value.
514db64422SBrad Bishop  *
524db64422SBrad Bishop  *  Calls exit(3) with bad status on failure.
534db64422SBrad Bishop  *
544db64422SBrad Bishop  *  @param[in] root - The hwmon class root.
554db64422SBrad Bishop  *  @param[in] instance - The hwmon instance (ex. hwmon1).
564db64422SBrad Bishop  *  @param[in] type - The hwmon type (ex. temp).
574db64422SBrad Bishop  *  @param[in] id - The hwmon id (ex. 1).
584db64422SBrad Bishop  *  @param[in] sensor - The hwmon sensor (ex. input).
59*3b8e36e2SMatt Spinler  *  @param[in] throwDeviceBusy - will throw a DeviceBusyException
60*3b8e36e2SMatt Spinler  *             on an EAGAIN errno instead of an error log exception.
614db64422SBrad Bishop  *
624db64422SBrad Bishop  *  @returns - The read value.
634db64422SBrad Bishop  */
644db64422SBrad Bishop int readSysfsWithCallout(const std::string& root,
654db64422SBrad Bishop                          const std::string& instance,
664db64422SBrad Bishop                          const std::string& type,
674db64422SBrad Bishop                          const std::string& id,
68*3b8e36e2SMatt Spinler                          const std::string& sensor,
69*3b8e36e2SMatt Spinler                          bool throwDeviceBusy = true);
704db64422SBrad Bishop 
71048ac87fSMatthew Barth  /** @brief Write a hwmon sysfs value
72048ac87fSMatthew Barth   *
73048ac87fSMatthew Barth   *  Calls exit(3) with bad status on failure
74048ac87fSMatthew Barth   *
75048ac87fSMatthew Barth   *  @param[in] value - The value to be written
76048ac87fSMatthew Barth   *  @param[in] root - The hwmon class root.
77048ac87fSMatthew Barth   *  @param[in] instance - The hwmon instance (ex. hwmon1).
78048ac87fSMatthew Barth   *  @param[in] type - The hwmon type (ex. fan).
79048ac87fSMatthew Barth   *  @param[in] id - The hwmon id (ex. 1).
80048ac87fSMatthew Barth   *  @param[in] sensor - The hwmon sensor (ex. target).
81048ac87fSMatthew Barth   *
82048ac87fSMatthew Barth   *  @returns - The value written
83048ac87fSMatthew Barth   */
84048ac87fSMatthew Barth uint64_t writeSysfsWithCallout(const uint64_t& value,
85048ac87fSMatthew Barth                                const std::string& root,
86048ac87fSMatthew Barth                                const std::string& instance,
87048ac87fSMatthew Barth                                const std::string& type,
88048ac87fSMatthew Barth                                const std::string& id,
89048ac87fSMatthew Barth                                const std::string& sensor);
90048ac87fSMatthew Barth 
911e6324faSPatrick Venture }
921e6324faSPatrick Venture 
9303476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
94