Use proper dbus path when possible.
Following methods and properties were updated to use full dbus path,
instead of internal telemetry id:
- TriggerManager.AddTrigger() - 'reportIds' arg
- Trigger.ReportIds - renamed to 'Reports'
- Report.TriggerIds - renamed to 'Triggers'
Testing done:
- UTs were updated and are passing.
Signed-off-by: Szymon Dompke <szymon.dompke@intel.com>
Change-Id: I78d812d38289fac575d25b48503cc8b9c6f736fe
diff --git a/src/utils/dbus_path_utils.cpp b/src/utils/dbus_path_utils.cpp
new file mode 100644
index 0000000..9d37cfc
--- /dev/null
+++ b/src/utils/dbus_path_utils.cpp
@@ -0,0 +1,50 @@
+#include "utils/dbus_path_utils.hpp"
+
+namespace utils
+{
+sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path,
+ const std::string& appended)
+{
+ if (appended.starts_with('/') || !isValidDbusPath(appended))
+ {
+ throw sdbusplus::exception::SdBusError(
+ static_cast<int>(std::errc::invalid_argument),
+ "Invalid appended string");
+ }
+
+ size_t pos_start = 0;
+ size_t pos_end = 0;
+ while ((pos_end = appended.find('/', pos_start)) != std::string::npos)
+ {
+ if (pos_start == pos_end)
+ {
+ throw sdbusplus::exception::SdBusError(
+ static_cast<int>(std::errc::invalid_argument),
+ "Invalid appended string");
+ }
+ path /= std::string_view(appended.begin() + pos_start,
+ appended.begin() + pos_end);
+ pos_start = pos_end + 1;
+ }
+ path /= std::string_view(appended.begin() + pos_start, appended.end());
+ return path;
+}
+
+std::string reportPathToId(const sdbusplus::message::object_path& path)
+{
+ if (path.str.starts_with(constants::reportDirStr))
+ {
+ auto id = path.str.substr(constants::reportDirStr.length());
+ if (std::cmp_greater(std::count(id.begin(), id.end(), '/'),
+ constants::maxPrefixesInId))
+ {
+ throw sdbusplus::exception::SdBusError(
+ static_cast<int>(std::errc::invalid_argument),
+ "Too many prefixes in id");
+ }
+ return id;
+ }
+ throw sdbusplus::exception::SdBusError(
+ static_cast<int>(std::errc::invalid_argument), "Invalid path prefix");
+}
+} // namespace utils
diff --git a/src/utils/dbus_path_utils.hpp b/src/utils/dbus_path_utils.hpp
new file mode 100644
index 0000000..a1b5709
--- /dev/null
+++ b/src/utils/dbus_path_utils.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <sdbusplus/message.hpp>
+
+#include <algorithm>
+#include <ranges>
+#include <string_view>
+
+namespace utils
+{
+
+namespace constants
+{
+constexpr std::string_view triggerDirStr =
+ "/xyz/openbmc_project/Telemetry/Triggers/";
+constexpr std::string_view reportDirStr =
+ "/xyz/openbmc_project/Telemetry/Reports/";
+
+constexpr std::string_view allowedCharactersInPath =
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/";
+constexpr size_t maxPrefixesInId = 1;
+
+const sdbusplus::message::object_path triggerDirPath =
+ sdbusplus::message::object_path(std::string(triggerDirStr));
+const sdbusplus::message::object_path reportDirPath =
+ sdbusplus::message::object_path(std::string(reportDirStr));
+} // namespace constants
+
+inline bool isValidDbusPath(const std::string& path)
+{
+ return (path.find_first_not_of(constants::allowedCharactersInPath) ==
+ std::string::npos) &&
+ !path.ends_with('/');
+}
+
+sdbusplus::message::object_path pathAppend(sdbusplus::message::object_path path,
+ const std::string& appended);
+
+std::string reportPathToId(const sdbusplus::message::object_path& path);
+
+} // namespace utils
diff --git a/src/utils/generate_id.cpp b/src/utils/generate_id.cpp
index c648481..42c9155 100644
--- a/src/utils/generate_id.cpp
+++ b/src/utils/generate_id.cpp
@@ -1,5 +1,7 @@
#include "utils/generate_id.hpp"
+#include "utils/dbus_path_utils.hpp"
+
#include <sdbusplus/exception.hpp>
#include <algorithm>
@@ -10,9 +12,6 @@
namespace details
{
-static constexpr std::string_view allowedCharactersInId =
- "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_/";
-
size_t countDigits(size_t value)
{
size_t result = 1;
@@ -55,12 +54,13 @@
std::string strippedId(name);
strippedId.erase(
- std::remove_if(strippedId.begin(), strippedId.end(),
- [](char c) {
- return c == '/' ||
- details::allowedCharactersInId.find(c) ==
- std::string_view::npos;
- }),
+ std::remove_if(
+ strippedId.begin(), strippedId.end(),
+ [](char c) {
+ return c == '/' ||
+ utils::constants::allowedCharactersInPath.find(c) ==
+ std::string_view::npos;
+ }),
strippedId.end());
strippedId = std::string(id) + strippedId;
@@ -92,7 +92,7 @@
void verifyIdCharacters(std::string_view id)
{
- if (id.find_first_not_of(details::allowedCharactersInId) !=
+ if (id.find_first_not_of(utils::constants::allowedCharactersInPath) !=
std::string::npos)
{
throw sdbusplus::exception::SdBusError(