xref: /openbmc/phosphor-hwmon/sysfs.hpp (revision 5c014d2b)
126b815faSBrad Bishop #pragma once
26292aeedSMatthew Barth 
3754d38cfSBrad Bishop #include <chrono>
43b8e36e2SMatt Spinler #include <exception>
56292aeedSMatthew Barth #include <fstream>
66292aeedSMatthew Barth #include <string>
76292aeedSMatthew Barth 
8043d3230SPatrick Venture namespace sysfs
9043d3230SPatrick Venture {
101e6324faSPatrick Venture 
11a9b5f05bSBrad Bishop inline std::string make_sysfs_path(const std::string& path,
126292aeedSMatthew Barth                                    const std::string& type,
136292aeedSMatthew Barth                                    const std::string& id,
146292aeedSMatthew Barth                                    const std::string& entry)
156292aeedSMatthew Barth {
166292aeedSMatthew Barth     using namespace std::literals;
176292aeedSMatthew Barth 
18043d3230SPatrick Venture     if (entry.empty())
19043d3230SPatrick Venture     {
209331ab78SPatrick Venture         return path + "/"s + type + id;
219331ab78SPatrick Venture     }
229331ab78SPatrick Venture 
236292aeedSMatthew Barth     return path + "/"s + type + id + "_"s + entry;
246292aeedSMatthew Barth }
256292aeedSMatthew Barth 
26f4bf63adSBrad Bishop /** @brief Return the path to the phandle file matching value in io-channels.
27f4bf63adSBrad Bishop  *
28f4bf63adSBrad Bishop  *  This function will take two passed in paths.
29f4bf63adSBrad Bishop  *  One path is used to find the io-channels file.
30f4bf63adSBrad Bishop  *  The other path is used to find the phandle file.
31f4bf63adSBrad Bishop  *  The 4 byte phandle value is read from the phandle file(s).
32f4bf63adSBrad Bishop  *  The 4 byte phandle value and 4 byte index value is read from io-channels.
33f4bf63adSBrad Bishop  *  When a match is found, the path to the matching phandle file is returned.
34f4bf63adSBrad Bishop  *
35f4bf63adSBrad Bishop  *  @param[in] iochanneldir - Path to file for getting phandle from io-channels
36f4bf63adSBrad Bishop  *  @param[in] phandledir - Path to use for reading from phandle file
37f4bf63adSBrad Bishop  *
38f4bf63adSBrad Bishop  *  @return Path to phandle file with value matching that in io-channels
39f4bf63adSBrad Bishop  */
40043d3230SPatrick Venture std::string findPhandleMatch(const std::string& iochanneldir,
41f4bf63adSBrad Bishop                              const std::string& phandledir);
42613a5b37SBrad Bishop 
4331dbe062SMatt Spinler /** @brief Find hwmon instances from an open-firmware device tree path
44613a5b37SBrad Bishop  *
45613a5b37SBrad Bishop  *  Look for a matching hwmon instance given an
46613a5b37SBrad Bishop  *  open firmware device path.
47613a5b37SBrad Bishop  *
48613a5b37SBrad Bishop  *  @param[in] ofNode- The open firmware device path.
49613a5b37SBrad Bishop  *
50613a5b37SBrad Bishop  *  @returns[in] - The hwmon instance path or an empty
51613a5b37SBrad Bishop  *                 string if no match is found.
52613a5b37SBrad Bishop  */
53*5c014d2bSMatt Spinler std::string findHwmonFromOFPath(const std::string& ofNode);
54613a5b37SBrad Bishop 
55626df17aSMatt Spinler /** @brief Find hwmon instances from a device path
56626df17aSMatt Spinler  *
57626df17aSMatt Spinler  *  Look for a matching hwmon instance given a device path that
58626df17aSMatt Spinler  *  starts with /devices.  This path is the DEVPATH udev attribute
59626df17aSMatt Spinler  *  for the device except it has the '/hwmon/hwmonN' stripped off.
60626df17aSMatt Spinler  *
61626df17aSMatt Spinler  *  @param[in] devPath - The device path.
62626df17aSMatt Spinler  *
63626df17aSMatt Spinler  *  @return - The hwmon instance path or an empty
64626df17aSMatt Spinler  *            string if no match is found.
65626df17aSMatt Spinler  */
66*5c014d2bSMatt Spinler std::string findHwmonFromDevPath(const std::string& devPath);
67626df17aSMatt Spinler 
68431d26a5SBrad Bishop /** @brief Return the path to use for a call out.
69431d26a5SBrad Bishop  *
70431d26a5SBrad Bishop  *  Return an empty string if a callout path cannot be
71431d26a5SBrad Bishop  *  found.
72431d26a5SBrad Bishop  *
73431d26a5SBrad Bishop  *  @param[in] instancePath - /sys/class/hwmon/hwmon<N> path.
74431d26a5SBrad Bishop  *
75431d26a5SBrad Bishop  *  @return Path to use for call out
76431d26a5SBrad Bishop  */
77431d26a5SBrad Bishop std::string findCalloutPath(const std::string& instancePath);
78431d26a5SBrad Bishop 
79dd467396SPatrick Venture } // namespace sysfs
801e6324faSPatrick Venture 
8103476f11SBrad Bishop // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
82