interfaces: use old dbus encoding
Due to backwards compatibility with old configurations, this is an easy
fix to bring back the old object paths.
Tests are provided to capture this behavior.
Change-Id: Ic89f2eb02bb908cf23b833b08fdca941bef2a07a
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/interfaces/internal_interface.cpp b/interfaces/internal_interface.cpp
index 917b8ec..c990632 100644
--- a/interfaces/internal_interface.cpp
+++ b/interfaces/internal_interface.cpp
@@ -18,6 +18,8 @@
#include <sdbusplus/message.hpp>
+#include <algorithm>
+
namespace phosphor
{
namespace led
@@ -50,9 +52,11 @@
std::string s = boost::join(words, "_");
- sdbusplus::message::object_path path(s);
+ // we assume the string to be a correct dbus name besides
+ // this detail
+ std::replace(s.begin(), s.end(), '-', '_');
- return path.str;
+ return s;
}
void InternalInterface::createLEDPath(const std::string& ledName)
@@ -77,8 +81,7 @@
"DBUSNAME", name);
// Unique path name representing a single LED.
- sdbusplus::message::object_path objPath = std::string(physParent);
- objPath /= name;
+ std::string objPath = std::string(physParent) + "/" + name;
if (leds.contains(objPath))
{
diff --git a/interfaces/internal_interface.hpp b/interfaces/internal_interface.hpp
index a588226..e0b2b0b 100644
--- a/interfaces/internal_interface.hpp
+++ b/interfaces/internal_interface.hpp
@@ -63,6 +63,14 @@
void removeLED(const std::string& name);
+ /** @brief Generates LED DBus name from LED description
+ *
+ * @param[in] name - LED description
+ * @return - DBus LED name
+ */
+
+ static std::string getDbusName(const LedDescr& ledDescr);
+
private:
/**
* @brief Unordered map to declare the sysfs LEDs
@@ -112,14 +120,6 @@
*/
void createLEDPath(const std::string& ledName);
-
- /** @brief Generates LED DBus name from LED description
- *
- * @param[in] name - LED description
- * @return - DBus LED name
- */
-
- static std::string getDbusName(const LedDescr& ledDescr);
};
} // namespace interface
diff --git a/test/meson.build b/test/meson.build
index eb82249..e864298 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -22,12 +22,14 @@
test_sources = [
'../physical.cpp',
'../sysfs.cpp',
+ '../interfaces/internal_interface.cpp',
]
tests = [
'physical.cpp',
'sysfs.cpp',
'test_led_description.cpp',
+ 'test_dbus_name.cpp',
]
foreach t : tests
diff --git a/test/test_dbus_name.cpp b/test/test_dbus_name.cpp
new file mode 100644
index 0000000..d9afabe
--- /dev/null
+++ b/test/test_dbus_name.cpp
@@ -0,0 +1,59 @@
+/**
+ * Copyright © 2024 9elements
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "interfaces/internal_interface.hpp"
+#include "sysfs.hpp"
+
+#include <gtest/gtest.h>
+
+using namespace phosphor::led;
+using namespace phosphor::led::sysfs;
+
+TEST(DbusName, Has3Parts)
+{
+ LedDescr d = {"devicename", "color", "function"};
+
+ std::string name = interface::InternalInterface::getDbusName(d);
+
+ ASSERT_EQ("devicename_function_color", name);
+}
+
+TEST(DbusName, WithDash)
+{
+ LedDescr d = {"enclosure-identify", std::nullopt, std::nullopt};
+
+ std::string name = interface::InternalInterface::getDbusName(d);
+
+ ASSERT_EQ("enclosure_identify", name);
+}
+
+TEST(DbusName, With2Dashes)
+{
+ LedDescr d = {"enclosure-identify-fault", std::nullopt, std::nullopt};
+
+ std::string name = interface::InternalInterface::getDbusName(d);
+
+ ASSERT_EQ("enclosure_identify_fault", name);
+}
+
+TEST(DbusName, WithUnderscore)
+{
+ LedDescr d = {"enclosure_identify", std::nullopt, std::nullopt};
+
+ std::string name = interface::InternalInterface::getDbusName(d);
+
+ ASSERT_EQ("enclosure_identify", name);
+}