nvidia-gpu: Use common class for mctp endpoints

The common endpoint class should be used for send and receive.

Tested: Used by MctpRequester, tested on nvl32-obmc

Change-Id: I0060a66a5bcb4decfbe663d46ba88529e01e2209
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/src/MctpAsioEndpoint.cpp b/src/MctpAsioEndpoint.cpp
new file mode 100644
index 0000000..922b51c
--- /dev/null
+++ b/src/MctpAsioEndpoint.cpp
@@ -0,0 +1,69 @@
+#include "MctpAsioEndpoint.hpp"
+
+#include <boost/asio/generic/datagram_protocol.hpp>
+#include <phosphor-logging/lg2.hpp>
+
+#include <bit>
+#include <cstdint>
+#include <optional>
+
+// Becuase of issues with glibc not matching linux, we need to make sure these
+// are included AFTER the system headers, which are implictly included by boost.
+// These show up as errors like
+// /usr/include/net/if.h:44:14: error: ‘IFF_UP’ conflicts with a previous
+// declaration
+// The bugs below are other projects working around similar issues
+// https://bugzilla.redhat.com/show_bug.cgi?id=1300256
+// https://github.com/systemd/systemd/commit/08ce521fb2546921f2642bef067d2cc02158b121
+// https://github.com/systemd/systemd/issues/2864
+// clang-format off
+#include <linux/mctp.h>
+#include <sys/socket.h>
+// clang-format on
+
+MctpAsioEndpoint::MctpAsioEndpoint(uint8_t eid, uint8_t msgType)
+{
+    endpoint.resize(sizeof(struct sockaddr_mctp));
+    struct sockaddr_mctp* sock =
+        std::bit_cast<struct sockaddr_mctp*>(endpoint.data());
+    sock->smctp_addr.s_addr = eid;
+    sock->smctp_family = AF_MCTP;
+    sock->smctp_type = msgType;
+    sock->smctp_tag = MCTP_TAG_OWNER;
+    sock->smctp_network = MCTP_NET_ANY;
+};
+
+MctpAsioEndpoint::MctpAsioEndpoint(uint8_t msgType) :
+    MctpAsioEndpoint{MCTP_ADDR_ANY, msgType}
+{}
+
+std::optional<uint8_t> MctpAsioEndpoint::eid() const
+{
+    const struct sockaddr_mctp* sock = getSockAddr();
+    if (sock == nullptr)
+    {
+        return std::nullopt;
+    }
+    return sock->smctp_addr.s_addr;
+}
+
+std::optional<uint8_t> MctpAsioEndpoint::type() const
+{
+    const struct sockaddr_mctp* sock = getSockAddr();
+    if (sock == nullptr)
+    {
+        return std::nullopt;
+    }
+    return sock->smctp_type;
+}
+
+const struct sockaddr_mctp* MctpAsioEndpoint::getSockAddr() const
+{
+    if (endpoint.size() < sizeof(struct sockaddr_mctp))
+    {
+        lg2::error("MctpRequester: Received endpoint is too small?");
+        return nullptr;
+    }
+
+    return std::bit_cast<struct sockaddr_mctp*>(endpoint.data());
+}
diff --git a/src/MctpAsioEndpoint.hpp b/src/MctpAsioEndpoint.hpp
index 89cbe01..19e7335 100644
--- a/src/MctpAsioEndpoint.hpp
+++ b/src/MctpAsioEndpoint.hpp
@@ -1,66 +1,33 @@
 #pragma once
 
 #include <boost/asio/generic/datagram_protocol.hpp>
-#include <phosphor-logging/lg2.hpp>
 
 #include <optional>
-#include <utility>
 
-// Becuase of issues with glibc not matching linux, we need to make sure these
-// are included AFTER the system headers, which are implictly included by boost.
-// These show up as errors like
-// /usr/include/net/if.h:44:14: error: ‘IFF_UP’ conflicts with a previous
-// declaration
-// The bugs below are other projects working around similar issues
-// https://bugzilla.redhat.com/show_bug.cgi?id=1300256
-// https://github.com/systemd/systemd/commit/08ce521fb2546921f2642bef067d2cc02158b121
-// https://github.com/systemd/systemd/issues/2864
-// clang-format off
-#include <linux/mctp.h>
-#include <sys/socket.h>
-// clang-format on
+// NOLINTNEXTLINE(readability-identifier-naming)
+struct sockaddr_mctp;
 
 // Wrapper around boost::asio::generic::datagram_protocol::endpoint to provide
 // MCTP specific APIs that are available to the kernel
 struct MctpAsioEndpoint
 {
+    MctpAsioEndpoint(uint8_t eid, uint8_t msgType);
+
+    // usable for binding to any eid and a given msgType
+    MctpAsioEndpoint(uint8_t msgType);
+
     MctpAsioEndpoint() = default;
     MctpAsioEndpoint(const MctpAsioEndpoint&) = delete;
     MctpAsioEndpoint(MctpAsioEndpoint&&) = delete;
     MctpAsioEndpoint& operator=(const MctpAsioEndpoint&) = delete;
     MctpAsioEndpoint& operator=(MctpAsioEndpoint&&) = delete;
-
+    ~MctpAsioEndpoint() = default;
     boost::asio::generic::datagram_protocol::endpoint endpoint;
 
-    std::optional<uint8_t> eid() const
-    {
-        const struct sockaddr_mctp* sock = getSockAddr();
-        if (sock == nullptr)
-        {
-            return std::nullopt;
-        }
-        return sock->smctp_addr.s_addr;
-    }
+    std::optional<uint8_t> eid() const;
 
-    std::optional<uint8_t> type() const
-    {
-        const struct sockaddr_mctp* sock = getSockAddr();
-        if (sock == nullptr)
-        {
-            return std::nullopt;
-        }
-        return sock->smctp_type;
-    }
+    std::optional<uint8_t> type() const;
 
   private:
-    const struct sockaddr_mctp* getSockAddr() const
-    {
-        if (endpoint.size() < sizeof(struct sockaddr_mctp))
-        {
-            lg2::error("MctpRequester: Received endpoint is too small?");
-            return nullptr;
-        }
-
-        return std::bit_cast<struct sockaddr_mctp*>(endpoint.data());
-    }
+    const struct sockaddr_mctp* getSockAddr() const;
 };
diff --git a/src/meson.build b/src/meson.build
index a5b59b7..8df4d6f 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -87,6 +87,18 @@
     dependencies: default_deps,
 )
 
+mctpasio_a = static_library(
+    'mctpasio_a',
+    'MctpAsioEndpoint.cpp',
+    dependencies: [boost, phosphor_logging_dep],
+)
+
+mctpasio_dep = declare_dependency(
+    include_directories: ['.'],
+    link_with: [mctpasio_a],
+    dependencies: default_deps,
+)
+
 notifywatch_a = static_library(
     'notifywatch_a',
     'NotifyWatch.cpp',
diff --git a/src/nvidia-gpu/MctpRequester.cpp b/src/nvidia-gpu/MctpRequester.cpp
index 25c1ea0..c01cfdc 100644
--- a/src/nvidia-gpu/MctpRequester.cpp
+++ b/src/nvidia-gpu/MctpRequester.cpp
@@ -5,7 +5,8 @@
 
 #include "MctpRequester.hpp"
 
-#include <linux/mctp.h>
+#include "MctpAsioEndpoint.hpp"
+
 #include <sys/socket.h>
 
 #include <OcpMctpVdm.hpp>
@@ -97,7 +98,7 @@
         return;
     }
 
-    if (*receivedMsgType != msgType)
+    if (*receivedMsgType != ocp::accelerator_management::messageType)
     {
         // we received a message that this handler doesn't support
         // drop it on the floor and rebind receive_from
@@ -344,16 +345,11 @@
         return;
     }
 
-    struct sockaddr_mctp addr{};
-    addr.smctp_family = AF_MCTP;
-    addr.smctp_addr.s_addr = eid;
-    addr.smctp_type = msgType;
-    addr.smctp_tag = MCTP_TAG_OWNER;
-    using endpoint = boost::asio::generic::datagram_protocol::endpoint;
-    endpoint sendEndPoint{&addr, sizeof(addr)};
-
+    MctpAsioEndpoint sendEndPoint(eid,
+                                  ocp::accelerator_management::messageType);
+    boost::asio::const_buffer buf(req.data(), req.size());
     mctpSocket.async_send_to(
-        boost::asio::const_buffer(req.data(), req.size()), sendEndPoint,
+        buf, sendEndPoint.endpoint,
         std::bind_front(&MctpRequester::handleSendMsgCompletion, this, eid));
 }
 
diff --git a/src/nvidia-gpu/meson.build b/src/nvidia-gpu/meson.build
index 304587f..3a90d9a 100644
--- a/src/nvidia-gpu/meson.build
+++ b/src/nvidia-gpu/meson.build
@@ -27,7 +27,7 @@
     gpusensor_sources + files('NvidiaGpuSensorMain.cpp'),
     implicit_include_directories: false,
     include_directories: [gpusensor_include_dir, sensor_include_dir],
-    dependencies: [thresholds_dep, utils_dep],
+    dependencies: [thresholds_dep, utils_dep, mctpasio_dep],
     install: true,
     install_dir: '/usr/libexec/dbus-sensors',
 )
diff --git a/src/tests/meson.build b/src/tests/meson.build
index 2ae41a8..1936bf6 100644
--- a/src/tests/meson.build
+++ b/src/tests/meson.build
@@ -86,7 +86,7 @@
         'test_NvidiaGpuSensorTest.cpp',
         'test_NvidiaDeviceInventoryMctpVdm.cpp',
         gpusensor_sources,
-        dependencies: [gmock_dep, ut_deps_list, utils_dep],
+        dependencies: [gmock_dep, ut_deps_list, utils_dep, mctpasio_dep],
         link_with: [thresholds_a],
         implicit_include_directories: false,
         include_directories: '../nvidia-gpu',