psu-ng: Throw if gpio line is not found
The find_line() API does not throw if errno is ENOENT and instead
returns an empty line:
https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git/tree/bindings/cxx/chip.cpp?h=v1.6.x#n131
Add a throw if the line is empty to signal the caller that an error
occurred, otherwise the caller won't know that the line is empty until
a read() request returns an error, in which case is unknown if the
read() failed because the line didn't exist or because of some other
error.
This change also serves as a test to determine if a gpio exists. For
gpios that are not expected to exist on all systems, it can be
determined at the time the gpio is constructed and avoid making read()
calls to it that would be expected to fail.
Tested: Tried to initialize a non-existent gpio and verified an
exception was thrown:
Oct 04 14:30:10 rain111bmc phosphor-psu-monitor[1744]: Failed to find line: Line does not exist: test-exception
Oct 04 14:30:10 rain111bmc phosphor-psu-monitor[1744]: Line does not exist: test-exception
Oct 04 14:30:10 rain111bmc systemd[1]: phosphor-psu-monitor.service: Main process exited, code=exited, status=254/n/a
Oct 04 14:30:10 rain111bmc systemd[1]: phosphor-psu-monitor.service: Failed with result 'exit-code'.
Change-Id: Ic48eec566f5fb6e15ea8f553744cbd33dc49ec69
Signed-off-by: Adriana Kobylak <anoo@us.ibm.com>
diff --git a/phosphor-power-supply/util.cpp b/phosphor-power-supply/util.cpp
index 5942ce7..73f86e0 100644
--- a/phosphor-power-supply/util.cpp
+++ b/phosphor-power-supply/util.cpp
@@ -16,6 +16,10 @@
try
{
line = gpiod::find_line(namedGpio);
+ if (!line)
+ {
+ throw std::runtime_error("Line does not exist: " + namedGpio);
+ }
}
catch (std::exception& e)
{