util: Add a function for converting MAC addresses
We need to be able to convert a mac addresses from byte form into our
typical human readable string form.
Tested:
Unit tests pass.
Change-Id: I6e68cfefd4d5962e1125c1b5229e61fce475729a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/util.hpp b/util.hpp
index 51c2e04..e01c744 100644
--- a/util.hpp
+++ b/util.hpp
@@ -8,6 +8,7 @@
#include <regex>
#include <sdbusplus/bus.hpp>
+#include <string>
namespace phosphor
{
@@ -47,6 +48,12 @@
*/
std::string getfromInventory(sdbusplus::bus::bus& bus);
+/** @brief Converts the given mac address bytes into a string
+ * @param[in] bytes - The mac address
+ * @returns A valid mac address string
+ */
+std::string toString(const MacAddr& mac);
+
namespace internal
{
/** @brief Converts the given mac address into unsigned 64 bit integer
@@ -66,6 +73,13 @@
static_cast<uint64_t>(mac[4]) << 8 | static_cast<uint64_t>(mac[5]);
}
+/** @brief Converts the lower nibble of a byte value to a hex digit
+ */
+inline char toHex(std::byte byte)
+{
+ uint8_t val = std::to_integer<uint8_t>(byte) & 0xf;
+ return val < 10 ? '0' + val : 'A' + (val - 10);
+}
} // namespace internal
} // namespace mac_address