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/sysfs.hpp b/sysfs.hpp
index 57c6769..2e078b2 100644
--- a/sysfs.hpp
+++ b/sysfs.hpp
@@ -16,11 +16,25 @@
#pragma once
#include <filesystem>
+#include <optional>
+
+static constexpr auto devParent = "/sys/class/leds/";
namespace phosphor
{
namespace led
{
+
+struct LedDescr
+{
+ // at least one of the members shall be non-empty
+ // after initialization
+
+ std::optional<std::string> devicename;
+ std::optional<std::string> color;
+ std::optional<std::string> function;
+};
+
class SysfsLed
{
public:
@@ -43,6 +57,12 @@
virtual unsigned long getDelayOff();
virtual void setDelayOff(unsigned long ms);
+ /** @brief parse LED name in sysfs
+ * Parse sysfs LED name and sets corresponding
+ * fields in LedDescr struct.
+ */
+ LedDescr getLedDescr();
+
protected:
static constexpr const char* attrBrightness = "brightness";
static constexpr const char* attrMaxBrightness = "max_brightness";