blob: 63a5ba6b1fbf637faba4860b5bdc7aaecb586708 [file] [log] [blame]
Harshit Agheraa3f24f42025-04-21 20:04:56 +05301/*
2 * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
3 * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0
4 */
5
6#pragma once
7
8#include <boost/asio/io_context.hpp>
9#include <boost/asio/local/datagram_protocol.hpp>
10
11#include <cstddef>
12#include <cstdint>
13#include <functional>
14#include <vector>
15
16// Define MCTP EID type
17using mctp_eid_t = uint8_t;
18
19namespace mctp
20{
21/**
22 * @brief MCTP requester class
23 *
24 * This class provides a simple interface for sending and receiving MCTP
25 * messages.
26 */
27class MctpRequester
28{
29 public:
30 MctpRequester() = delete;
31
32 MctpRequester(const MctpRequester&) = delete;
33
34 MctpRequester(MctpRequester&&) = delete;
35
36 MctpRequester& operator=(const MctpRequester&) = delete;
37
38 MctpRequester& operator=(MctpRequester&&) = delete;
39
40 /**
41 * @brief Constructor
42 * @param ctx - The IO context to use
43 * @param msgType - The message type to use
44 */
45 MctpRequester(boost::asio::io_context& ctx, uint8_t msgType);
46
47 /**
48 * @brief Send an MCTP request message and receive the response
49 *
50 * This function sends a request message to the specified endpoint ID and
51 * asynchronously waits for a response. It uses the MCTP socket to handle
52 * the communication. Results are provided via the callback.
53 *
54 * @param[in] eid - The endpoint ID to send the message to
55 * @param[in] reqMsg - The request message to send
56 * @param[in] callback - Callback function to be invoked when response is
57 * received The callback takes two parameters:
58 * - An integer status code (0 for success, negative for error)
59 * - A vector containing the response message bytes
60 */
61 void sendRecvMsg(
62 mctp_eid_t eid, const std::vector<uint8_t>& reqMsg,
63 const std::function<void(int, std::vector<uint8_t>)>& callback);
64
65 private:
66 /**
67 * @brief Process received message
68 *
69 * This function processes a received message and invokes the callback with
70 * the appropriate status code and response message bytes.
71 *
72 * @param[in] eid - The endpoint ID from which the message was received
73 * @param[in] reqMsg - The received request message bytes
74 * @param[in] callback - The callback function to invoke with the result
75 * @param[in] peekedLength - The length of the peeked data
76 */
77 void processRecvMsg(
78 mctp_eid_t eid, const std::vector<uint8_t>& reqMsg,
79 const std::function<void(int, std::vector<uint8_t>)>& callback,
80 size_t peekedLength) const;
81
82 /** @brief IO context to use */
83 boost::asio::io_context& ctx;
84
85 /** @brief Socket file descriptor */
86 int sockfd = -1;
87
88 /** @brief Local socket */
89 boost::asio::local::datagram_protocol::socket mctpSocket;
90
91 /** @brief MCTP message type */
92 uint8_t msgType;
93};
94} // namespace mctp