summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Spinler <spinler@us.ibm.com>2018-03-05 12:03:55 -0600
committerMatt Spinler <spinler@us.ibm.com>2018-03-07 13:12:07 -0600
commit626df17a5e2b7fecbdfa36045cdd19efda667f81 (patch)
tree47fc56c9683ab8ff17dca84c470f0c2c16ec6c48
parent31dbe06252d4fe41815fe783e4f720ce0333cef3 (diff)
downloadphosphor-hwmon-626df17a5e2b7fecbdfa36045cdd19efda667f81.tar.gz
phosphor-hwmon-626df17a5e2b7fecbdfa36045cdd19efda667f81.zip
Add findHwmonFromDevPath()
This function can find the hwmon path from a /devices path. This path is similar to what udev lists as the device path, but with the /hwmon/hwmonN directories removed from the end. In addition, any ':' characters in the path have been converted to '--', so the code needs to convert them back. This is done because some devices are not in the device tree, and hence wouldn't have the open firmware device path that the code previously only supported. Tested: Tested with the new /devices path. Change-Id: Ic26646c6f609323cff86d4cd10bbe0a44d7d61ac Signed-off-by: Matt Spinler <spinler@us.ibm.com>
-rw-r--r--sysfs.cpp38
-rw-r--r--sysfs.hpp13
-rw-r--r--test/Makefile.am6
3 files changed, 57 insertions, 0 deletions
diff --git a/sysfs.cpp b/sysfs.cpp
index 4ea32b2..0eb7863 100644
--- a/sysfs.cpp
+++ b/sysfs.cpp
@@ -19,6 +19,7 @@
#include <experimental/filesystem>
#include <fstream>
#include <memory>
+#include <phosphor-logging/log.hpp>
#include <thread>
#include "sysfs.hpp"
@@ -238,6 +239,43 @@ std::string findHwmonFromOFPath(const std::string& ofNode)
return emptyString;
}
+std::string findHwmonFromDevPath(const std::string& devPath)
+{
+ fs::path p{"/sys"};
+ p /= devPath;
+ p /= "hwmon";
+
+ try
+ {
+ //This path is also used as a filesystem path to an environment
+ //file, and that has issues with ':'s in the path so they've
+ //been converted to '--'s. Convert them back now.
+ size_t pos = 0;
+ std::string path = p;
+ while ((pos = path.find("--")) != std::string::npos)
+ {
+ path.replace(pos, 2, ":");
+ }
+
+ for (const auto& hwmonInst : fs::directory_iterator(path))
+ {
+ if ((hwmonInst.path().filename().string().find("hwmon") !=
+ std::string::npos))
+ {
+ return hwmonInst.path();
+ }
+ }
+ }
+ catch (const std::exception& e)
+ {
+ using namespace phosphor::logging;
+ log<level::ERR>(
+ "Unable to find hwmon directory from the dev path",
+ entry("PATH=%s", devPath.c_str()));
+ }
+ return emptyString;
+}
+
namespace hwmonio
{
diff --git a/sysfs.hpp b/sysfs.hpp
index e673205..70568cd 100644
--- a/sysfs.hpp
+++ b/sysfs.hpp
@@ -51,6 +51,19 @@ std::string findPhandleMatch(
*/
std::string findHwmonFromOFPath(const std::string& ofNode);
+/** @brief Find hwmon instances from a device path
+ *
+ * Look for a matching hwmon instance given a device path that
+ * starts with /devices. This path is the DEVPATH udev attribute
+ * for the device except it has the '/hwmon/hwmonN' stripped off.
+ *
+ * @param[in] devPath - The device path.
+ *
+ * @return - The hwmon instance path or an empty
+ * string if no match is found.
+ */
+std::string findHwmonFromDevPath(const std::string& devPath);
+
/** @brief Return the path to use for a call out.
*
* Return an empty string if a callout path cannot be
diff --git a/test/Makefile.am b/test/Makefile.am
index 0825659..a52a992 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -11,6 +11,8 @@ find_callout_path_LDFLAGS = \
-static
find_callout_path_LDADD = \
-lstdc++fs \
+ $(SDBUSPLUS_LIBS) \
+ $(PHOSPHOR_LOGGING_LIBS) \
${top_builddir}/sysfs.o
find_callout_path_CXXFLAGS =
@@ -21,6 +23,8 @@ find_hwmon_LDFLAGS = \
-static
find_hwmon_LDADD = \
-lstdc++fs \
+ $(SDBUSPLUS_LIBS) \
+ $(PHOSPHOR_LOGGING_LIBS) \
${top_builddir}/sysfs.o
find_hwmon_CXXFLAGS =
@@ -31,6 +35,8 @@ hwmonio_LDFLAGS = \
-static
hwmonio_LDADD = \
-lstdc++fs \
+ $(SDBUSPLUS_LIBS) \
+ $(PHOSPHOR_LOGGING_LIBS) \
${top_builddir}/sysfs.o
hwmonio_CXXFLAGS =
OpenPOWER on IntegriCloud