sysfs: Refactor LED property parsing
Added new test for parsing led description from sysfs.
Since there are some edge cases that can happen, to make sure the
parsing happens as expected in all cases.
The edge cases primarily come from the different led properties that can
be present or absent in devicetree. I have tested some combinations
thereof and would prefer the label to be generated by led sysfs instead
of manually providing the 3-component label.
However for that to work phosphor-led-sysfs must be able to extract the
labels components in all cases.
This modifies the behavior slightly but it will stay the same
for led names that have 1 or 3 components.
Change-Id: I8def089e4c8dc5d3a341cf6f6b1d6356f5aefe48
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/interfaces/internal_interface.cpp b/interfaces/internal_interface.cpp
index 95a61e9..917b8ec 100644
--- a/interfaces/internal_interface.cpp
+++ b/interfaces/internal_interface.cpp
@@ -31,36 +31,21 @@
bus(bus), serverInterface(bus, path, internalInterface, vtable.data(), this)
{}
-void InternalInterface::getLedDescr(const std::string& name, LedDescr& ledDescr)
-{
- std::vector<std::string> words;
- boost::split(words, name, boost::is_any_of(":"));
- try
- {
- ledDescr.devicename = words.at(0);
- ledDescr.color = words.at(1);
- ledDescr.function = words.at(2);
- }
- catch (const std::out_of_range& e)
- {
- lg2::warning("LED description {DESC} not well formed, error {ERR}",
- "DESC", name, "ERR", e.what());
- throw e;
- }
-}
-
std::string InternalInterface::getDbusName(const LedDescr& ledDescr)
{
std::vector<std::string> words;
- words.emplace_back(ledDescr.devicename);
- if (!ledDescr.function.empty())
+ if (ledDescr.devicename.has_value())
{
- words.emplace_back(ledDescr.function);
+ words.emplace_back(ledDescr.devicename.value());
+ }
+ if (ledDescr.function.has_value())
+ {
+ words.emplace_back(ledDescr.function.value());
}
- if (!ledDescr.color.empty())
+ if (ledDescr.color.has_value())
{
- words.emplace_back(ledDescr.color);
+ words.emplace_back(ledDescr.color.value());
}
std::string s = boost::join(words, "_");
@@ -81,17 +66,11 @@
return;
}
+ auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path));
+
// Convert LED name in sysfs into DBus name
- LedDescr ledDescr;
- try
- {
- getLedDescr(ledName, ledDescr);
- }
- catch (...)
- {
- // Ignore the error, for simple LED which was not added in 3-part form.
- // The simple LED can appear with it's plain name
- }
+ const LedDescr ledDescr = sled->getLedDescr();
+
name = getDbusName(ledDescr);
lg2::debug("LED {NAME} receives dbus name {DBUSNAME}", "NAME", ledName,
@@ -106,10 +85,9 @@
return;
}
- auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path));
-
leds.emplace(objPath, std::make_unique<phosphor::led::Physical>(
- bus, objPath, std::move(sled), ledDescr.color));
+ bus, objPath, std::move(sled),
+ ledDescr.color.value_or("")));
}
void InternalInterface::addLED(const std::string& name)