types: Migrate to stdplus EtherAddr

Change-Id: I1ec2cd0b57a9f02fe96bbe9a068d1a437805b43a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/src/ethernet_interface.cpp b/src/ethernet_interface.cpp
index e4108f3..91e5ed5 100644
--- a/src/ethernet_interface.cpp
+++ b/src/ethernet_interface.cpp
@@ -136,7 +136,7 @@
     EthernetInterfaceIntf::linkUp(info.flags & IFF_RUNNING, skipSignal);
     if (info.mac)
     {
-        MacAddressIntf::macAddress(std::to_string(*info.mac), skipSignal);
+        MacAddressIntf::macAddress(stdplus::toStr(*info.mac), skipSignal);
     }
     if (info.mtu)
     {
@@ -203,7 +203,7 @@
 
     if (auto it = staticNeighbors.find(*info.addr); it != staticNeighbors.end())
     {
-        it->second->NeighborObj::macAddress(std::to_string(*info.mac));
+        it->second->NeighborObj::macAddress(stdplus::toStr(*info.mac));
     }
     else
     {
@@ -292,10 +292,10 @@
                               Argument::ARGUMENT_VALUE(ipAddress.c_str()));
     }
 
-    ether_addr lladdr;
+    stdplus::EtherAddr lladdr;
     try
     {
-        lladdr = ToAddr<ether_addr>{}(macAddress);
+        lladdr = stdplus::fromStr<stdplus::EtherAddr>(macAddress);
     }
     catch (const std::exception& e)
     {
@@ -315,7 +315,7 @@
     }
     else
     {
-        auto str = std::to_string(lladdr);
+        auto str = stdplus::toStr(lladdr);
         if (it->second->macAddress() == str)
         {
             return it->second->getObjPath();
@@ -558,10 +558,10 @@
 
     auto objRoot = std::string_view(objPath).substr(0, objPath.rfind('/'));
     auto macStr = MacAddressIntf::macAddress();
-    std::optional<ether_addr> mac;
+    std::optional<stdplus::EtherAddr> mac;
     if (!macStr.empty())
     {
-        mac.emplace(ToAddr<ether_addr>{}(macStr));
+        mac.emplace(stdplus::fromStr<stdplus::EtherAddr>(macStr));
     }
     auto info = AllIntfInfo{InterfaceInfo{
         .type = ARPHRD_ETHER,
@@ -739,10 +739,10 @@
         elog<InternalFailure>();
     }
 #ifdef PERSIST_MAC
-    ether_addr newMAC;
+    stdplus::EtherAddr newMAC;
     try
     {
-        newMAC = ToAddr<ether_addr>{}(value);
+        newMAC = stdplus::fromStr<stdplus::EtherAddr>(value);
     }
     catch (const std::invalid_argument&)
     {
@@ -750,7 +750,7 @@
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
                               Argument::ARGUMENT_VALUE(value.c_str()));
     }
-    if (!mac_address::isUnicast(newMAC))
+    if (!newMAC.isUnicast())
     {
         lg2::error("MAC Address {NET_MAC} is not valid", "NET_MAC", value);
         elog<InvalidArgument>(Argument::ARGUMENT_NAME("MACAddress"),
@@ -758,10 +758,11 @@
     }
 
     auto interface = interfaceName();
-    auto validMAC = std::to_string(newMAC);
+    auto validMAC = stdplus::toStr(newMAC);
 
     // We don't need to update the system if the address is unchanged
-    ether_addr oldMAC = ToAddr<ether_addr>{}(MacAddressIntf::macAddress());
+    auto oldMAC =
+        stdplus::fromStr<stdplus::EtherAddr>(MacAddressIntf::macAddress());
     if (newMAC != oldMAC)
     {
         // Update everything that depends on the MAC value
diff --git a/src/inventory_mac.cpp b/src/inventory_mac.cpp
index c9769d1..49beecd 100644
--- a/src/inventory_mac.cpp
+++ b/src/inventory_mac.cpp
@@ -76,7 +76,8 @@
     }
 }
 
-ether_addr getfromInventory(sdbusplus::bus_t& bus, const std::string& intfName)
+stdplus::EtherAddr getfromInventory(sdbusplus::bus_t& bus,
+                                    const std::string& intfName)
 {
     std::string interfaceName = configJson[intfName];
 
@@ -156,7 +157,7 @@
 
     std::variant<std::string> value;
     reply.read(value);
-    return ToAddr<ether_addr>{}(std::get<std::string>(value));
+    return stdplus::fromStr<stdplus::EtherAddr>(std::get<std::string>(value));
 }
 
 bool setInventoryMACOnSystem(sdbusplus::bus_t& bus, const std::string& intfname)
@@ -164,9 +165,9 @@
     try
     {
         auto inventoryMAC = getfromInventory(bus, intfname);
-        if (inventoryMAC != ether_addr{})
+        if (inventoryMAC != stdplus::EtherAddr{})
         {
-            auto macStr = std::to_string(inventoryMAC);
+            auto macStr = stdplus::toStr(inventoryMAC);
             lg2::info(
                 "Mac Address {NET_MAC} in Inventory on Interface {NET_INTF}",
                 "NET_MAC", macStr, "NET_INTF", intfname);
diff --git a/src/neighbor.cpp b/src/neighbor.cpp
index d9f2436..afbc062 100644
--- a/src/neighbor.cpp
+++ b/src/neighbor.cpp
@@ -23,19 +23,19 @@
 
 Neighbor::Neighbor(sdbusplus::bus_t& bus, std::string_view objRoot,
                    stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
-                   ether_addr lladdr, State state) :
+                   stdplus::EtherAddr lladdr, State state) :
     Neighbor(bus, makeObjPath(objRoot, addr), parent, addr, lladdr, state)
 {}
 
 Neighbor::Neighbor(sdbusplus::bus_t& bus,
                    sdbusplus::message::object_path objPath,
                    stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
-                   ether_addr lladdr, State state) :
+                   stdplus::EtherAddr lladdr, State state) :
     NeighborObj(bus, objPath.str.c_str(), NeighborObj::action::defer_emit),
     parent(parent), objPath(std::move(objPath))
 {
     NeighborObj::ipAddress(std::to_string(addr), true);
-    NeighborObj::macAddress(std::to_string(lladdr), true);
+    NeighborObj::macAddress(stdplus::toStr(lladdr), true);
     NeighborObj::state(state, true);
     emit_object_added();
 }
diff --git a/src/neighbor.hpp b/src/neighbor.hpp
index f36371a..9f964e8 100644
--- a/src/neighbor.hpp
+++ b/src/neighbor.hpp
@@ -40,7 +40,7 @@
      */
     Neighbor(sdbusplus::bus_t& bus, std::string_view objRoot,
              stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
-             ether_addr lladdr, State state);
+             stdplus::EtherAddr lladdr, State state);
 
     /** @brief Delete this d-bus object.
      */
@@ -67,7 +67,7 @@
 
     Neighbor(sdbusplus::bus_t& bus, sdbusplus::message::object_path objPath,
              stdplus::PinnedRef<EthernetInterface> parent, InAddrAny addr,
-             ether_addr lladdr, State state);
+             stdplus::EtherAddr lladdr, State state);
 };
 
 } // namespace network
diff --git a/src/rtnetlink.cpp b/src/rtnetlink.cpp
index 1c80c1d..4308d23 100644
--- a/src/rtnetlink.cpp
+++ b/src/rtnetlink.cpp
@@ -67,9 +67,10 @@
                 ret.name.emplace(data.begin(), data.end() - 1);
                 break;
             case IFLA_ADDRESS:
-                if (data.size() == sizeof(ether_addr))
+                if (data.size() == sizeof(stdplus::EtherAddr))
                 {
-                    ret.mac.emplace(stdplus::raw::copyFrom<ether_addr>(data));
+                    ret.mac.emplace(
+                        stdplus::raw::copyFrom<stdplus::EtherAddr>(data));
                 }
                 break;
             case IFLA_MTU:
@@ -171,7 +172,7 @@
         auto [hdr, data] = netlink::extractRtAttr(msg);
         if (hdr.rta_type == NDA_LLADDR)
         {
-            ret.mac = stdplus::raw::copyFrom<ether_addr>(data);
+            ret.mac = stdplus::raw::copyFrom<stdplus::EtherAddr>(data);
         }
         else if (hdr.rta_type == NDA_DST)
         {
diff --git a/src/types.cpp b/src/types.cpp
index 0a46b1f..901d77a 100644
--- a/src/types.cpp
+++ b/src/types.cpp
@@ -25,10 +25,6 @@
     return stdplus::hashMulti(addr.getAddr(), addr.getPfx());
 }
 
-std::string std::to_string(ether_addr value)
-{
-    return string(phosphor::network::detail::ToStrBuf<ether_addr>{}(value));
-}
 std::string std::to_string(in_addr value)
 {
     return string(phosphor::network::detail::ToStrBuf<in_addr>{}(value));
diff --git a/src/types.hpp b/src/types.hpp
index 53a3c89..f7c0fa3 100644
--- a/src/types.hpp
+++ b/src/types.hpp
@@ -1,8 +1,9 @@
 #pragma once
 #include <fmt/core.h>
-#include <net/ethernet.h>
 #include <netinet/in.h>
 
+#include <stdplus/net/addr/ether.hpp>
+
 #include <algorithm>
 #include <array>
 #include <numeric>
@@ -14,12 +15,6 @@
 #include <unordered_set>
 #include <variant>
 
-constexpr bool operator==(ether_addr lhs, ether_addr rhs) noexcept
-{
-    return std::equal(lhs.ether_addr_octet, lhs.ether_addr_octet + 6,
-                      rhs.ether_addr_octet);
-}
-
 constexpr bool operator==(in_addr lhs, in_addr rhs) noexcept
 {
     return lhs.s_addr == rhs.s_addr;
@@ -85,7 +80,7 @@
     unsigned idx;
     unsigned flags;
     std::optional<std::string> name = std::nullopt;
-    std::optional<ether_addr> mac = std::nullopt;
+    std::optional<stdplus::EtherAddr> mac = std::nullopt;
     std::optional<unsigned> mtu = std::nullopt;
     std::optional<unsigned> parent_idx = std::nullopt;
     std::optional<std::string> kind = std::nullopt;
@@ -125,7 +120,7 @@
     unsigned ifidx;
     uint16_t state;
     std::optional<InAddrAny> addr;
-    std::optional<ether_addr> mac;
+    std::optional<stdplus::EtherAddr> mac;
 
     constexpr bool operator==(const NeighborInfo& rhs) const noexcept
     {
@@ -356,38 +351,6 @@
 {};
 
 template <>
-struct ToAddr<ether_addr>
-{
-    constexpr ether_addr operator()(std::string_view str) const
-    {
-        constexpr DecodeInt<uint8_t, 16> di;
-        ether_addr ret;
-        if (str.size() == 12 && str.find(":") == str.npos)
-        {
-            for (size_t i = 0; i < 6; ++i)
-            {
-                ret.ether_addr_octet[i] = di(str.substr(i * 2, 2));
-            }
-        }
-        else
-        {
-            for (size_t i = 0; i < 5; ++i)
-            {
-                auto loc = str.find(":");
-                ret.ether_addr_octet[i] = di(str.substr(0, loc));
-                str.remove_prefix(loc == str.npos ? str.size() : loc + 1);
-                if (str.empty())
-                {
-                    throw std::invalid_argument("Missing mac data");
-                }
-            }
-            ret.ether_addr_octet[5] = di(str);
-        }
-        return ret;
-    }
-};
-
-template <>
 struct ToAddr<in_addr>
 {
     constexpr in_addr operator()(std::string_view str) const
@@ -516,29 +479,6 @@
 };
 
 template <>
-struct ToStr<ether_addr>
-{
-    // 6 octets * 2 hex chars + 5 separators
-    static constexpr uint8_t buf_size = 17;
-    using buf_type = std::array<char, buf_size>;
-
-    constexpr char* operator()(char* buf, ether_addr v) const noexcept
-    {
-        for (char* ptr = buf + 2; ptr < buf + buf_size; ptr += 3)
-        {
-            *ptr = ':';
-        }
-        for (size_t i = 0; i < 6; ++i)
-        {
-            char* tmp = buf + i * 3;
-            uint8_t byte = v.ether_addr_octet[i];
-            EncodeInt<uint8_t, 16>{}(tmp, byte, 2);
-        }
-        return buf + buf_size;
-    }
-};
-
-template <>
 struct ToStr<in_addr>
 {
     // 4 octets * 3 dec chars + 3 separators
@@ -742,9 +682,6 @@
 namespace fmt
 {
 template <>
-struct formatter<ether_addr> : phosphor::network::detail::Format<ether_addr>
-{};
-template <>
 struct formatter<in_addr> : phosphor::network::detail::Format<in_addr>
 {};
 template <>
@@ -762,7 +699,6 @@
 
 namespace std
 {
-string to_string(ether_addr value);
 string to_string(in_addr value);
 string to_string(in6_addr value);
 string to_string(phosphor::network::InAddrAny value);
@@ -777,11 +713,6 @@
     return phosphor::network::detail::veq(rhs, lhs);
 }
 
-auto& operator<<(auto& os, ether_addr v)
-{
-    return os << phosphor::network::detail::ToStrBuf<ether_addr>{}(v);
-}
-
 auto& operator<<(auto& os, in_addr v)
 {
     return os << phosphor::network::detail::ToStrBuf<in_addr>{}(v);
diff --git a/src/util.cpp b/src/util.cpp
index dbc474a..7102372 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -216,24 +216,5 @@
         .value_or(true);
 }
 
-namespace mac_address
-{
-
-bool isEmpty(const ether_addr& mac)
-{
-    return mac == ether_addr{};
-}
-
-bool isMulticast(const ether_addr& mac)
-{
-    return mac.ether_addr_octet[0] & 0b1;
-}
-
-bool isUnicast(const ether_addr& mac)
-{
-    return !isEmpty(mac) && !isMulticast(mac);
-}
-
-} // namespace mac_address
 } // namespace network
 } // namespace phosphor
diff --git a/src/util.hpp b/src/util.hpp
index f35e512..7736f67 100644
--- a/src/util.hpp
+++ b/src/util.hpp
@@ -18,29 +18,6 @@
 class Parser;
 }
 
-namespace mac_address
-{
-
-/** @brief Determines if the mac address is empty
- *  @param[in] mac - The mac address
- *  @return True if 00:00:00:00:00:00
- */
-bool isEmpty(const ether_addr& mac);
-
-/** @brief Determines if the mac address is a multicast address
- *  @param[in] mac - The mac address
- *  @return True if multicast bit is set
- */
-bool isMulticast(const ether_addr& mac);
-
-/** @brief Determines if the mac address is a unicast address
- *  @param[in] mac - The mac address
- *  @return True if not multicast or empty
- */
-bool isUnicast(const ether_addr& mac);
-
-} // namespace mac_address
-
 /* @brief converts a sockaddr for the specified address family into
  *        a type_safe InAddrAny.
  * @param[in] family - The address family of the buf