From 4407c8069d12853cc8e9bcbcaf1063097cddab20 Mon Sep 17 00:00:00 2001 From: Nehal Patel Date: Mon, 24 Nov 2025 22:06:46 -0800 Subject: [PATCH] Refactor I2C and UART interfaces to use std::byte MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace uint8_t with std::byte for all I2C and UART data transmission APIs to improve type safety and prevent accidental arithmetic operations on raw byte data. This change aligns with modern C++ best practices and makes the intent of byte-oriented operations more explicit throughout the codebase. Updated interfaces, implementations, applications, and tests. Added custom JSON serialization support for std::byte in the host emulator message encoder. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- src/apps/i2c_demo/i2c_demo.cpp | 4 +- src/apps/uart_echo/uart_echo.cpp | 8 ++-- .../host/emulator_message_json_encoder.hpp | 16 +++++++ src/libs/mcu/host/host_emulator_messages.hpp | 15 ++++--- src/libs/mcu/host/host_i2c.cpp | 16 +++---- src/libs/mcu/host/host_i2c.hpp | 14 +++--- src/libs/mcu/host/host_uart.cpp | 16 +++---- src/libs/mcu/host/host_uart.hpp | 14 +++--- src/libs/mcu/host/test_host_i2c.cpp | 43 ++++++++++++------- src/libs/mcu/host/test_host_uart.cpp | 30 ++++++++----- src/libs/mcu/i2c.hpp | 13 +++--- src/libs/mcu/uart.hpp | 12 +++--- 12 files changed, 123 insertions(+), 78 deletions(-) diff --git a/src/apps/i2c_demo/i2c_demo.cpp b/src/apps/i2c_demo/i2c_demo.cpp index b9c92d8..1f6aea0 100644 --- a/src/apps/i2c_demo/i2c_demo.cpp +++ b/src/apps/i2c_demo/i2c_demo.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include "apps/app.hpp" @@ -35,7 +36,8 @@ auto I2CDemo::Run() -> std::expected { constexpr uint16_t kDeviceAddress{0x50}; // Test pattern to write/read - const std::array test_pattern{0xDE, 0xAD, 0xBE, 0xEF}; + const std::array test_pattern{std::byte{0xDE}, std::byte{0xAD}, + std::byte{0xBE}, std::byte{0xEF}}; // Main loop - write pattern, read it back, verify while (true) { diff --git a/src/apps/uart_echo/uart_echo.cpp b/src/apps/uart_echo/uart_echo.cpp index 3585e57..670b641 100644 --- a/src/apps/uart_echo/uart_echo.cpp +++ b/src/apps/uart_echo/uart_echo.cpp @@ -33,9 +33,9 @@ auto UartEcho::Init() -> std::expected { [this, &uart_config]() { return board_.Uart1().Init(uart_config); }) .and_then([this]() { return board_.Uart1().SetRxHandler( - [this](const uint8_t* data, size_t size) { + [this](const std::byte* data, size_t size) { // Echo the data back - const std::vector echo_data(data, data + size); + const std::vector echo_data(data, data + size); std::ignore = board_.Uart1().Send(echo_data); // Toggle LED1 to indicate data received @@ -47,8 +47,10 @@ auto UartEcho::Init() -> std::expected { auto UartEcho::Run() -> std::expected { // Send initial greeting message const std::string greeting{"UART Echo ready! Send data to echo it back.\n"}; + const auto* greeting_bytes{ + reinterpret_cast(greeting.data())}; auto send_result{board_.Uart1().Send( - std::vector(greeting.begin(), greeting.end()))}; + std::span{greeting_bytes, greeting.size()})}; if (!send_result) { return std::unexpected(send_result.error()); } diff --git a/src/libs/mcu/host/emulator_message_json_encoder.hpp b/src/libs/mcu/host/emulator_message_json_encoder.hpp index b8f4278..3f1645b 100644 --- a/src/libs/mcu/host/emulator_message_json_encoder.hpp +++ b/src/libs/mcu/host/emulator_message_json_encoder.hpp @@ -1,12 +1,28 @@ #pragma once +#include #include #include +#include #include "libs/common/error.hpp" #include "libs/mcu/host/host_emulator_messages.hpp" #include "libs/mcu/pin.hpp" +// Custom JSON serialization for std::byte +namespace nlohmann { +template <> +struct adl_serializer { + static void to_json(json& j, const std::byte& b) { + j = std::to_integer(b); + } + + static void from_json(const json& j, std::byte& b) { + b = static_cast(j.get()); + } +}; +} // namespace nlohmann + namespace common { NLOHMANN_JSON_SERIALIZE_ENUM(Error, diff --git a/src/libs/mcu/host/host_emulator_messages.hpp b/src/libs/mcu/host/host_emulator_messages.hpp index 0288196..bb66177 100644 --- a/src/libs/mcu/host/host_emulator_messages.hpp +++ b/src/libs/mcu/host/host_emulator_messages.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -43,9 +44,9 @@ struct UartEmulatorRequest { ObjectType object{ObjectType::kUart}; std::string name; OperationType operation; - std::vector data; // For Send operation - size_t size{0}; // For Receive operation (buffer size) - uint32_t timeout_ms{0}; // For Receive operation + std::vector data; // For Send operation + size_t size{0}; // For Receive operation (buffer size) + uint32_t timeout_ms{0}; // For Receive operation auto operator==(const UartEmulatorRequest& other) const -> bool { return type == other.type && object == other.object && name == other.name && operation == other.operation && data == other.data && @@ -57,7 +58,7 @@ struct UartEmulatorResponse { MessageType type{MessageType::kResponse}; ObjectType object{ObjectType::kUart}; std::string name; - std::vector data; // Received data + std::vector data; // Received data size_t bytes_transferred{0}; common::Error status; auto operator==(const UartEmulatorResponse& other) const -> bool { @@ -73,8 +74,8 @@ struct I2CEmulatorRequest { std::string name; OperationType operation; uint16_t address{0}; - std::vector data; // For Send operation - size_t size{0}; // For Receive operation (buffer size) + std::vector data; // For Send operation + size_t size{0}; // For Receive operation (buffer size) auto operator==(const I2CEmulatorRequest& other) const -> bool { return type == other.type && object == other.object && name == other.name && operation == other.operation && address == other.address && @@ -87,7 +88,7 @@ struct I2CEmulatorResponse { ObjectType object{ObjectType::kI2C}; std::string name; uint16_t address{0}; - std::vector data; // Received data + std::vector data; // Received data size_t bytes_transferred{0}; common::Error status; auto operator==(const I2CEmulatorResponse& other) const -> bool { diff --git a/src/libs/mcu/host/host_i2c.cpp b/src/libs/mcu/host/host_i2c.cpp index 2fae354..e080733 100644 --- a/src/libs/mcu/host/host_i2c.cpp +++ b/src/libs/mcu/host/host_i2c.cpp @@ -15,7 +15,7 @@ namespace mcu { auto HostI2CController::SendData(uint16_t address, - std::span data) + std::span data) -> std::expected { const I2CEmulatorRequest request{ .type = MessageType::kRequest, @@ -23,7 +23,7 @@ auto HostI2CController::SendData(uint16_t address, .name = name_, .operation = OperationType::kSend, .address = address, - .data = std::vector(data.begin(), data.end()), + .data = std::vector(data.begin(), data.end()), .size = 0, }; @@ -46,7 +46,7 @@ auto HostI2CController::SendData(uint16_t address, } auto HostI2CController::ReceiveData(uint16_t address, size_t size) - -> std::expected, common::Error> { + -> std::expected, common::Error> { const I2CEmulatorRequest request{ .type = MessageType::kRequest, .object = ObjectType::kI2C, @@ -77,11 +77,11 @@ auto HostI2CController::ReceiveData(uint16_t address, size_t size) const size_t bytes_to_copy{std::min(response.data.size(), buffer.size())}; std::copy_n(response.data.begin(), bytes_to_copy, buffer.begin()); - return std::span{buffer.data(), bytes_to_copy}; + return std::span{buffer.data(), bytes_to_copy}; } auto HostI2CController::SendDataInterrupt( - uint16_t address, std::span data, + uint16_t address, std::span data, std::function)> callback) -> std::expected { callback(SendData(address, data)); @@ -90,14 +90,14 @@ auto HostI2CController::SendDataInterrupt( auto HostI2CController::ReceiveDataInterrupt( uint16_t address, size_t size, - std::function, common::Error>)> + std::function, common::Error>)> callback) -> std::expected { callback(ReceiveData(address, size)); return {}; } auto HostI2CController::SendDataDma( - uint16_t address, std::span data, + uint16_t address, std::span data, std::function)> callback) -> std::expected { callback(SendData(address, data)); @@ -106,7 +106,7 @@ auto HostI2CController::SendDataDma( auto HostI2CController::ReceiveDataDma( uint16_t address, size_t size, - std::function, common::Error>)> + std::function, common::Error>)> callback) -> std::expected { callback(ReceiveData(address, size)); return {}; diff --git a/src/libs/mcu/host/host_i2c.hpp b/src/libs/mcu/host/host_i2c.hpp index c46c1c9..abdc2fa 100644 --- a/src/libs/mcu/host/host_i2c.hpp +++ b/src/libs/mcu/host/host_i2c.hpp @@ -23,27 +23,27 @@ class HostI2CController final : public I2CController, public Receiver { auto operator=(HostI2CController&&) -> HostI2CController& = delete; ~HostI2CController() override = default; - auto SendData(uint16_t address, std::span data) + auto SendData(uint16_t address, std::span data) -> std::expected override; auto ReceiveData(uint16_t address, size_t size) - -> std::expected, common::Error> override; + -> std::expected, common::Error> override; auto SendDataInterrupt( - uint16_t address, std::span data, + uint16_t address, std::span data, std::function)> callback) -> std::expected override; auto ReceiveDataInterrupt( uint16_t address, size_t size, - std::function, common::Error>)> + std::function, common::Error>)> callback) -> std::expected override; - auto SendDataDma(uint16_t address, std::span data, + auto SendDataDma(uint16_t address, std::span data, std::function)> callback) -> std::expected override; auto ReceiveDataDma( uint16_t address, size_t size, - std::function, common::Error>)> + std::function, common::Error>)> callback) -> std::expected override; auto Receive(const std::string_view& message) -> std::expected override; @@ -51,6 +51,6 @@ class HostI2CController final : public I2CController, public Receiver { private: const std::string name_; Transport& transport_; - std::unordered_map> data_buffers_; + std::unordered_map> data_buffers_; }; } // namespace mcu diff --git a/src/libs/mcu/host/host_uart.cpp b/src/libs/mcu/host/host_uart.cpp index b7450b6..09af567 100644 --- a/src/libs/mcu/host/host_uart.cpp +++ b/src/libs/mcu/host/host_uart.cpp @@ -27,7 +27,7 @@ auto HostUart::Init(const UartConfig& config) return {}; } -auto HostUart::Send(std::span data) +auto HostUart::Send(std::span data) -> std::expected { if (!initialized_) { return std::unexpected(common::Error::kInvalidState); @@ -42,7 +42,7 @@ auto HostUart::Send(std::span data) .object = ObjectType::kUart, .name = name_, .operation = OperationType::kSend, - .data = std::vector(data.begin(), data.end()), + .data = std::vector(data.begin(), data.end()), .size = 0, .timeout_ms = 0, }; @@ -59,7 +59,7 @@ auto HostUart::Send(std::span data) }); } -auto HostUart::Receive(std::span buffer, uint32_t timeout_ms) +auto HostUart::Receive(std::span buffer, uint32_t timeout_ms) -> std::expected { if (!initialized_) { return std::unexpected(common::Error::kInvalidState); @@ -99,7 +99,7 @@ auto HostUart::Receive(std::span buffer, uint32_t timeout_ms) }); } -auto HostUart::SendAsync(std::span data, +auto HostUart::SendAsync(std::span data, std::function)> callback) -> std::expected { if (!initialized_) { @@ -118,7 +118,7 @@ auto HostUart::SendAsync(std::span data, .object = ObjectType::kUart, .name = name_, .operation = OperationType::kSend, - .data = std::vector(data.begin(), data.end()), + .data = std::vector(data.begin(), data.end()), .size = 0, .timeout_ms = 0, }; @@ -135,7 +135,7 @@ auto HostUart::SendAsync(std::span data, } auto HostUart::ReceiveAsync( - std::span buffer, + std::span buffer, std::function)> callback) -> std::expected { if (!initialized_) { @@ -191,8 +191,8 @@ auto HostUart::Flush() -> std::expected { return {}; } -auto HostUart::SetRxHandler(std::function handler) - -> std::expected { +auto HostUart::SetRxHandler(std::function + handler) -> std::expected { if (!initialized_) { return std::unexpected(common::Error::kInvalidState); } diff --git a/src/libs/mcu/host/host_uart.hpp b/src/libs/mcu/host/host_uart.hpp index 335f6b7..2a849d3 100644 --- a/src/libs/mcu/host/host_uart.hpp +++ b/src/libs/mcu/host/host_uart.hpp @@ -25,18 +25,18 @@ class HostUart final : public Uart, public Receiver { auto Init(const UartConfig& config) -> std::expected override; - auto Send(std::span data) + auto Send(std::span data) -> std::expected override; - auto Receive(std::span buffer, uint32_t timeout_ms) + auto Receive(std::span buffer, uint32_t timeout_ms) -> std::expected override; - auto SendAsync(std::span data, + auto SendAsync(std::span data, std::function)> callback) -> std::expected override; auto ReceiveAsync( - std::span buffer, + std::span buffer, std::function)> callback) -> std::expected override; @@ -44,7 +44,7 @@ class HostUart final : public Uart, public Receiver { auto Available() const -> size_t override; auto Flush() -> std::expected override; - auto SetRxHandler(std::function handler) + auto SetRxHandler(std::function handler) -> std::expected override; // Receiver interface for handling async responses from emulator @@ -63,10 +63,10 @@ class HostUart final : public Uart, public Receiver { std::function)> receive_callback_{}; // Receive handler for unsolicited incoming data - std::function rx_handler_{}; + std::function rx_handler_{}; // Receive buffer for async operations - std::vector receive_buffer_{}; + std::vector receive_buffer_{}; }; } // namespace mcu diff --git a/src/libs/mcu/host/test_host_i2c.cpp b/src/libs/mcu/host/test_host_i2c.cpp index ecbe777..6a05f0f 100644 --- a/src/libs/mcu/host/test_host_i2c.cpp +++ b/src/libs/mcu/host/test_host_i2c.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -64,7 +65,7 @@ class HostI2CTest : public ::testing::Test { void EmulatorLoop() { // Simulate I2C device buffers (address -> data) - std::map> i2c_device_buffers; + std::map> i2c_device_buffers; try { zmq::socket_t socket{emulator_context_, zmq::socket_type::pair}; @@ -116,7 +117,7 @@ class HostI2CTest : public ::testing::Test { if (i2c_device_buffers.contains(request.address)) { const auto& buffer = i2c_device_buffers[request.address]; const size_t bytes_to_send{std::min(request.size, buffer.size())}; - response.data = std::vector( + response.data = std::vector( buffer.begin(), buffer.begin() + static_cast(bytes_to_send)); response.bytes_transferred = bytes_to_send; @@ -147,7 +148,8 @@ class HostI2CTest : public ::testing::Test { TEST_F(HostI2CTest, SendData) { const uint16_t device_address{0x42}; - const std::array send_data{0xDE, 0xAD, 0xBE, 0xEF}; + const std::array send_data{std::byte{0xDE}, std::byte{0xAD}, + std::byte{0xBE}, std::byte{0xEF}}; auto result = i2c_->SendData(device_address, send_data); EXPECT_TRUE(result); @@ -155,7 +157,9 @@ TEST_F(HostI2CTest, SendData) { TEST_F(HostI2CTest, SendReceiveData) { const uint16_t device_address{0x50}; - const std::array send_data{0x01, 0x02, 0x03, 0x04, 0x05}; + const std::array send_data{std::byte{0x01}, std::byte{0x02}, + std::byte{0x03}, std::byte{0x04}, + std::byte{0x05}}; // Send data to device auto send_result = i2c_->SendData(device_address, send_data); @@ -176,8 +180,10 @@ TEST_F(HostI2CTest, SendReceiveData) { TEST_F(HostI2CTest, MultipleAddresses) { const uint16_t address1{0x50}; const uint16_t address2{0x51}; - const std::array data1{0xAA, 0xBB, 0xCC}; - const std::array data2{0x11, 0x22, 0x33, 0x44}; + const std::array data1{std::byte{0xAA}, std::byte{0xBB}, + std::byte{0xCC}}; + const std::array data2{std::byte{0x11}, std::byte{0x22}, + std::byte{0x33}, std::byte{0x44}}; // Send to first address auto send1_result = i2c_->SendData(address1, data1); @@ -218,7 +224,9 @@ TEST_F(HostI2CTest, ReceiveWithoutSend) { TEST_F(HostI2CTest, ReceivePartialData) { const uint16_t device_address{0x70}; - const std::array send_data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + const std::array send_data{ + std::byte{0}, std::byte{1}, std::byte{2}, std::byte{3}, std::byte{4}, + std::byte{5}, std::byte{6}, std::byte{7}, std::byte{8}, std::byte{9}}; // Send 10 bytes auto send_result = i2c_->SendData(device_address, send_data); @@ -238,7 +246,8 @@ TEST_F(HostI2CTest, ReceivePartialData) { TEST_F(HostI2CTest, SendDataInterrupt) { const uint16_t device_address{0x42}; - const std::array send_data{0xAA, 0xBB, 0xCC}; + const std::array send_data{std::byte{0xAA}, std::byte{0xBB}, + std::byte{0xCC}}; bool callback_called{false}; std::expected callback_result{}; @@ -258,20 +267,21 @@ TEST_F(HostI2CTest, SendDataInterrupt) { TEST_F(HostI2CTest, ReceiveDataInterrupt) { const uint16_t device_address{0x50}; - const std::array send_data{0x01, 0x02, 0x03, 0x04}; + const std::array send_data{std::byte{0x01}, std::byte{0x02}, + std::byte{0x03}, std::byte{0x04}}; // First send data auto send_result = i2c_->SendData(device_address, send_data); ASSERT_TRUE(send_result); bool callback_called{false}; - std::expected, common::Error> callback_result{ + std::expected, common::Error> callback_result{ std::unexpected(common::Error::kUnknown)}; auto result = i2c_->ReceiveDataInterrupt( device_address, send_data.size(), [&callback_called, &callback_result]( - std::expected, common::Error> result) { + std::expected, common::Error> result) { callback_called = true; callback_result = result; }); @@ -288,7 +298,8 @@ TEST_F(HostI2CTest, ReceiveDataInterrupt) { TEST_F(HostI2CTest, SendDataDma) { const uint16_t device_address{0x42}; - const std::array send_data{0xDE, 0xAD, 0xBE}; + const std::array send_data{std::byte{0xDE}, std::byte{0xAD}, + std::byte{0xBE}}; bool callback_called{false}; std::expected callback_result{}; @@ -308,20 +319,22 @@ TEST_F(HostI2CTest, SendDataDma) { TEST_F(HostI2CTest, ReceiveDataDma) { const uint16_t device_address{0x55}; - const std::array send_data{0x10, 0x20, 0x30, 0x40, 0x50}; + const std::array send_data{std::byte{0x10}, std::byte{0x20}, + std::byte{0x30}, std::byte{0x40}, + std::byte{0x50}}; // First send data auto send_result = i2c_->SendData(device_address, send_data); ASSERT_TRUE(send_result); bool callback_called{false}; - std::expected, common::Error> callback_result{ + std::expected, common::Error> callback_result{ std::unexpected(common::Error::kUnknown)}; auto result = i2c_->ReceiveDataDma( device_address, send_data.size(), [&callback_called, &callback_result]( - std::expected, common::Error> result) { + std::expected, common::Error> result) { callback_called = true; callback_result = result; }); diff --git a/src/libs/mcu/host/test_host_uart.cpp b/src/libs/mcu/host/test_host_uart.cpp index 3fe5dba..89f4d31 100644 --- a/src/libs/mcu/host/test_host_uart.cpp +++ b/src/libs/mcu/host/test_host_uart.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -63,7 +64,7 @@ class HostUartTest : public ::testing::Test { } void EmulatorLoop() { - std::vector uart_rx_buffer; + std::vector uart_rx_buffer; try { zmq::socket_t socket{emulator_context_, zmq::socket_type::pair}; @@ -114,7 +115,7 @@ class HostUartTest : public ::testing::Test { // Device wants to receive data - send from our buffer const size_t bytes_to_send{ std::min(request.size, uart_rx_buffer.size())}; - response.data = std::vector( + response.data = std::vector( uart_rx_buffer.begin(), uart_rx_buffer.begin() + static_cast(bytes_to_send)); @@ -167,12 +168,14 @@ TEST_F(HostUartTest, SendReceiveBlocking) { ASSERT_TRUE(init_result); // Send data - const std::array send_data{0x01, 0x02, 0x03, 0x04, 0x05}; + const std::array send_data{std::byte{0x01}, std::byte{0x02}, + std::byte{0x03}, std::byte{0x04}, + std::byte{0x05}}; auto send_result = uart_->Send(send_data); EXPECT_TRUE(send_result); // Receive data back (emulator echoes to buffer) - std::array recv_buffer = {0}; + std::array recv_buffer = {}; auto recv_result = uart_->Receive(recv_buffer, 1000); ASSERT_TRUE(recv_result); EXPECT_EQ(recv_result.value(), 5); @@ -180,14 +183,16 @@ TEST_F(HostUartTest, SendReceiveBlocking) { } TEST_F(HostUartTest, SendWithoutInit) { - const std::array send_data{0x01, 0x02, 0x03, 0x04, 0x05}; + const std::array send_data{std::byte{0x01}, std::byte{0x02}, + std::byte{0x03}, std::byte{0x04}, + std::byte{0x05}}; auto result = uart_->Send(send_data); EXPECT_FALSE(result); EXPECT_EQ(result.error(), common::Error::kInvalidState); } TEST_F(HostUartTest, ReceiveWithoutInit) { - std::array recv_buffer = {0}; + std::array recv_buffer = {}; auto result = uart_->Receive(recv_buffer, 1000); EXPECT_FALSE(result); EXPECT_EQ(result.error(), common::Error::kInvalidState); @@ -200,7 +205,9 @@ TEST_F(HostUartTest, IsBusy) { EXPECT_FALSE(uart_->IsBusy()); - const std::array send_data{0x01, 0x02, 0x03, 0x04, 0x05}; + const std::array send_data{std::byte{0x01}, std::byte{0x02}, + std::byte{0x03}, std::byte{0x04}, + std::byte{0x05}}; std::ignore = uart_->Send(send_data); EXPECT_FALSE(uart_->IsBusy()); // Blocking operation completes immediately @@ -232,19 +239,20 @@ TEST_F(HostUartTest, RxHandlerUnsolicitedData) { ASSERT_TRUE(init_result); // Track received data via handler - std::vector received_data{}; + std::vector received_data{}; bool handler_called{false}; // Register RxHandler auto handler_result = uart_->SetRxHandler( - [&received_data, &handler_called](const uint8_t* data, size_t size) { + [&received_data, &handler_called](const std::byte* data, size_t size) { received_data.assign(data, data + size); handler_called = true; }); ASSERT_TRUE(handler_result); // Simulate emulator sending unsolicited data to device - const std::vector test_data{0xDE, 0xAD, 0xBE, 0xEF}; + const std::vector test_data{std::byte{0xDE}, std::byte{0xAD}, + std::byte{0xBE}, std::byte{0xEF}}; const mcu::UartEmulatorRequest unsolicited_request{ .type = mcu::MessageType::kRequest, .object = mcu::ObjectType::kUart, @@ -281,7 +289,7 @@ TEST_F(HostUartTest, RxHandlerUnsolicitedData) { TEST_F(HostUartTest, RxHandlerWithoutInit) { // Try to set handler before initialization - auto result = uart_->SetRxHandler([](const uint8_t*, size_t) {}); + auto result = uart_->SetRxHandler([](const std::byte*, size_t) {}); EXPECT_FALSE(result); EXPECT_EQ(result.error(), common::Error::kInvalidState); } diff --git a/src/libs/mcu/i2c.hpp b/src/libs/mcu/i2c.hpp index d5e2973..54dc1bb 100644 --- a/src/libs/mcu/i2c.hpp +++ b/src/libs/mcu/i2c.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -13,28 +14,28 @@ class I2CController { public: virtual ~I2CController() = default; - virtual auto SendData(uint16_t address, std::span data) + virtual auto SendData(uint16_t address, std::span data) -> std::expected = 0; virtual auto ReceiveData(uint16_t address, size_t size) - -> std::expected, common::Error> = 0; + -> std::expected, common::Error> = 0; virtual auto SendDataInterrupt( - uint16_t address, std::span data, + uint16_t address, std::span data, std::function)> callback) -> std::expected = 0; virtual auto ReceiveDataInterrupt( uint16_t address, size_t size, - std::function, common::Error>)> + std::function, common::Error>)> callback) -> std::expected = 0; virtual auto SendDataDma( - uint16_t address, std::span data, + uint16_t address, std::span data, std::function)> callback) -> std::expected = 0; virtual auto ReceiveDataDma( uint16_t address, size_t size, - std::function, common::Error>)> + std::function, common::Error>)> callback) -> std::expected = 0; }; diff --git a/src/libs/mcu/uart.hpp b/src/libs/mcu/uart.hpp index 1c74a0d..d6b258b 100644 --- a/src/libs/mcu/uart.hpp +++ b/src/libs/mcu/uart.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -52,14 +53,14 @@ class Uart { /// @brief Send data (blocking) /// @param data Span of bytes to send /// @return Success or error code - virtual auto Send(std::span data) + virtual auto Send(std::span data) -> std::expected = 0; /// @brief Receive data (blocking with timeout) /// @param buffer Buffer to store received data /// @param timeout_ms Timeout in milliseconds (0 = wait forever) /// @return Number of bytes received or error - virtual auto Receive(std::span buffer, uint32_t timeout_ms = 0) + virtual auto Receive(std::span buffer, uint32_t timeout_ms = 0) -> std::expected = 0; /// @brief Send data asynchronously @@ -68,7 +69,7 @@ class Uart { /// @param callback Called when transfer completes /// @return Success or error code virtual auto SendAsync( - std::span data, + std::span data, std::function)> callback) -> std::expected = 0; @@ -78,7 +79,7 @@ class Uart { /// @param callback Called when data is received (with number of bytes) /// @return Success or error code virtual auto ReceiveAsync( - std::span buffer, + std::span buffer, std::function)> callback) -> std::expected = 0; @@ -99,7 +100,8 @@ class Uart { /// application when data arrives asynchronously (e.g., from external source) /// @param handler Callback invoked when data arrives (data pointer and size) /// @return Success or error code - virtual auto SetRxHandler(std::function handler) + virtual auto SetRxHandler( + std::function handler) -> std::expected = 0; };