platform-mc: Terminus name handling
`PLDM-stack: Adding sensor monitoring section` design spec details that
the `Terminus name` is required to create the terminus's sensors,
states, effecters... D-Bus interfaces and `Terminus Name` can be
encoded in the Terminus's `Entity Auxiliary Names PDR` or in the MCTP
Entity-manager endpoint EID configuration file.
[1] https://gerrit.openbmc.org/c/openbmc/docs/+/47252/34/designs/pldm-stack.md#433
Section `28.18 Entity Auxiliary Names PDR` in DSP0248 V1.2.2 details
about the PDRs response for the name of one PLDM entity. When the
containerID is `0x0000` this entity is `Overall system` or `top most
containing entity`. The name of this entity will can be used as
`Terminus name`.
Support parsing `Entity Auxiliary Names PDR` and find the `Terminus
name` if it is reponsed in the terminus' PDRs. `Terminus Name` string
will be assigned to local variable and can be used as prefix for
sensors, state, effecters... names.
Signed-off-by: Thu Nguyen <thu@os.amperecomputing.com>
Change-Id: I701537c48651b9de86de77941b752e30de112916
diff --git a/common/test/pldm_utils_test.cpp b/common/test/pldm_utils_test.cpp
index 86704c4..73cd92c 100644
--- a/common/test/pldm_utils_test.cpp
+++ b/common/test/pldm_utils_test.cpp
@@ -1094,3 +1094,15 @@
rc = isValidEID(254);
EXPECT_EQ(rc, true);
}
+
+TEST(TrimNameForDbus, goodTest)
+{
+ std::string name = "Name with space";
+ std::string_view expectedName = "Name_with__space";
+ std::string_view result = trimNameForDbus(name);
+ EXPECT_EQ(expectedName, result);
+ name = "Name 1\0";
+ expectedName = "Name_1";
+ result = trimNameForDbus(name);
+ EXPECT_EQ(expectedName, result);
+}
diff --git a/common/utils.cpp b/common/utils.cpp
index 60154be..1cd5a72 100644
--- a/common/utils.cpp
+++ b/common/utils.cpp
@@ -671,5 +671,15 @@
}
}
+std::string_view trimNameForDbus(std::string& name)
+{
+ std::replace(name.begin(), name.end(), ' ', '_');
+ auto nullTerminatorPos = name.find('\0');
+ if (nullTerminatorPos != std::string::npos)
+ {
+ name.erase(nullTerminatorPos);
+ }
+ return name;
+}
} // namespace utils
} // namespace pldm
diff --git a/common/utils.hpp b/common/utils.hpp
index 103783d..6b8ea7c 100644
--- a/common/utils.hpp
+++ b/common/utils.hpp
@@ -526,5 +526,15 @@
* @param[in] present - status to set either true/false
*/
void setFruPresence(const std::string& fruObjPath, bool present);
+
+/** @brief Trim `\0` in string and replace ` ` by `_` to use name in D-Bus
+ * object path
+ *
+ * @param[in] name - the input string
+ *
+ * @return the result string
+ */
+std::string_view trimNameForDbus(std::string& name);
+
} // namespace utils
} // namespace pldm