From ae3f5b31c4d723200bf9e31ee8558d979e1b90fa Mon Sep 17 00:00:00 2001 From: David Tarazi Date: Wed, 25 Nov 2026 11:28:54 -0800 Subject: [PATCH 1/3] feat(data): allow for reading raw data such as serial --- axiomatic_adapter/README.md | 5 +- .../axiomatic_adapter/axiomatic_adapter.hpp | 19 ++++- .../axiomatic_adapter/axiomatic_protocol.hpp | 5 +- .../axiomatic_socketcan_bridge.hpp | 8 +- .../axiomatic_adapter/models/ax142100a.hpp | 2 +- axiomatic_adapter/src/axiomatic_adapter.cpp | 65 +++++++++++--- axiomatic_adapter/src/axiomatic_model.cpp | 2 +- .../src/axiomatic_socketcan_bridge.cpp | 7 +- axiomatic_adapter/src/models/ax142100a.cpp | 6 +- .../test/axiomatic_model_test.cpp | 85 ++++++++++++++++++- 10 files changed, 178 insertions(+), 26 deletions(-) diff --git a/axiomatic_adapter/README.md b/axiomatic_adapter/README.md index 01aabcc..2364d58 100644 --- a/axiomatic_adapter/README.md +++ b/axiomatic_adapter/README.md @@ -66,7 +66,8 @@ polymath::can::AxiomaticAdapter adapter( [](polymath::can::AxiomaticAdapter::socket_error_string_t /*error*/) { /*do nothing*/ }, receive_timeout_ms, /*tcp_nodelay=*/true, // optional, defaults to true; see below - std::make_unique() // optional, defaults to ax140900::Ax140900 + std::make_unique(), // optional, defaults to ax140900::Ax140900 + [](std::vector /*data*/) { /* serial data of one TCP read, ax142100a only */ } // optional ); // open the socket @@ -103,5 +104,5 @@ If you have any doubt, leave it at the default. 2. Axiomatic-specific status messages are ignored and deliberately skipped. In future revisions this should be handled and reported as necessary. 3. CAN FD is not supported 4. Only TCP mode is supported; no UDP support (heartbeats are required for UDP support) -5. AX142100A raw data (serial) payloads are skipped; only CAN frames are delivered. +5. AX142100A raw data (serial) payloads are never put on the CAN bus. The adapter hands the raw data of each TCP read, joined in wire order, to an optional raw data callback, or returns it from `receive(std::optional &, std::vector &)`. The bridge executable does not set a callback, so it drops raw data. Data is not reassembled into lines: one print can span several reads. 6. AX142100A 11-bit IDs are assumed to be 2 bytes on the wire; the manual only shows a 29-bit example. diff --git a/axiomatic_adapter/include/axiomatic_adapter/axiomatic_adapter.hpp b/axiomatic_adapter/include/axiomatic_adapter/axiomatic_adapter.hpp index 5b541b0..d04a0ed 100644 --- a/axiomatic_adapter/include/axiomatic_adapter/axiomatic_adapter.hpp +++ b/axiomatic_adapter/include/axiomatic_adapter/axiomatic_adapter.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include "axiomatic_adapter/axiomatic_model.hpp" #include "axiomatic_adapter/models/ax140900.hpp" @@ -67,6 +68,8 @@ class AxiomaticAdapter : public std::enable_shared_from_this /// efficiency for bulk transfers). Better for real-time, but can, in constrained /// resource environments, cause issues /// @param model converter model, e.g. std::make_unique(); must not be null + /// @param raw_data_callback_function called from the reception thread with the raw (serial) data of each TCP read, + /// joined in wire order; only models that carry serial data call it AxiomaticAdapter( const std::string & ip_address, const std::string & port, @@ -76,7 +79,9 @@ class AxiomaticAdapter : public std::enable_shared_from_this [](socket_error_string_t /*error*/) { /*do nothing*/ }, const std::chrono::milliseconds & receive_timeout_ms = AxiomaticAdapter::DEFAULT_SOCKET_RECEIVE_TIMEOUT_MS, bool tcp_nodelay = true, - std::unique_ptr model = std::make_unique()); + std::unique_ptr model = std::make_unique(), + const std::function data)> && raw_data_callback_function = + [](std::vector /*data*/) { /*do nothing*/ }); /// @brief Destructor for AxiomaticAdapter virtual ~AxiomaticAdapter(); @@ -90,10 +95,22 @@ class AxiomaticAdapter : public std::enable_shared_from_this bool closeSocket(); /// @brief Receive with a reference to a CanFrame to fill + /// Raw data in the read is dropped, and a read with only raw data returns an error; use the overload with raw_data + /// to get it. /// @param frame OUTPUT CanFrame to fill /// @return optional error string filled with an error message if any std::optional receive(polymath::socketcan::CanFrame & can_frame); + /// @brief Receive both CAN frames and raw (serial) data + /// Does one TCP read unless CAN frames from an earlier read are still pending, and never calls a callback. + /// @param can_frame OUTPUT reset on entry; set when a CAN frame is available + /// @param raw_data OUTPUT cleared on entry; the raw data payloads of this read joined in wire order, empty if none. + /// Always empty when a pending CAN frame is returned without a new read + /// @return optional error string, filled for socket errors and reads with neither CAN frames nor raw data. + /// On success, at least one of can_frame and raw_data is set + std::optional receive( + std::optional & can_frame, std::vector & raw_data); + /// @brief Receive returns the received CanFrame /// @return optional CanFrame /// nullopt is returned if no frame received, acts like null diff --git a/axiomatic_adapter/include/axiomatic_adapter/axiomatic_protocol.hpp b/axiomatic_adapter/include/axiomatic_adapter/axiomatic_protocol.hpp index a3034e5..42d5314 100644 --- a/axiomatic_adapter/include/axiomatic_adapter/axiomatic_protocol.hpp +++ b/axiomatic_adapter/include/axiomatic_adapter/axiomatic_protocol.hpp @@ -57,11 +57,14 @@ struct ParsedMessages std::vector diagnostics; }; -/// @brief CAN frames decoded from a buffer, plus a description of every byte range skipped or dropped +/// @brief CAN frames and raw serial payloads decoded from a buffer, plus a description of every byte range skipped +/// or dropped struct DecodeResult { std::vector frames; std::vector diagnostics; + /// @brief One entry per raw data message, in wire order; only models that carry serial data fill it + std::vector> raw_data; }; /// @brief Read an unsigned little-endian integer diff --git a/axiomatic_adapter/include/axiomatic_adapter/axiomatic_socketcan_bridge.hpp b/axiomatic_adapter/include/axiomatic_adapter/axiomatic_socketcan_bridge.hpp index 271d3b7..7adc76d 100644 --- a/axiomatic_adapter/include/axiomatic_adapter/axiomatic_socketcan_bridge.hpp +++ b/axiomatic_adapter/include/axiomatic_adapter/axiomatic_socketcan_bridge.hpp @@ -15,8 +15,10 @@ #ifndef AXIOMATIC_ADAPTER__AXIOMATIC_SOCKETCAN_BRIDGE_NODE_HPP_ #define AXIOMATIC_ADAPTER__AXIOMATIC_SOCKETCAN_BRIDGE_NODE_HPP_ +#include #include #include +#include #include "axiomatic_adapter/axiomatic_adapter.hpp" #include "axiomatic_adapter/models/ax140900.hpp" @@ -40,13 +42,17 @@ class AxiomaticSocketcanBridge /// AxiomaticAdapter socket. See AxiomaticAdapter for the /// full tradeoff discussion. /// @param model converter model, e.g. std::make_unique(); must not be null + /// @param raw_data_callback_function called with the raw (serial) data of each TCP read from the converter; + /// raw data is never put on the CAN bus AxiomaticSocketcanBridge( const std::string & can_interface_name, const std::string & ip, const std::string & port, bool verbose = false, bool tcp_nodelay = true, - std::unique_ptr model = std::make_unique()); + std::unique_ptr model = std::make_unique(), + const std::function data)> && raw_data_callback_function = + [](std::vector /*data*/) { /*do nothing*/ }); /// @brief Destruct axiomatic socketcan bridge ~AxiomaticSocketcanBridge(); diff --git a/axiomatic_adapter/include/axiomatic_adapter/models/ax142100a.hpp b/axiomatic_adapter/include/axiomatic_adapter/models/ax142100a.hpp index 8392895..295f462 100644 --- a/axiomatic_adapter/include/axiomatic_adapter/models/ax142100a.hpp +++ b/axiomatic_adapter/include/axiomatic_adapter/models/ax142100a.hpp @@ -40,7 +40,7 @@ enum class MessageId : uint16_t /// @class polymath::can::ax142100a::Ax142100a /// @brief Encodes a Forwarded Data message. -/// Decodes Forwarded Data messages; raw data payloads are skipped. +/// Decodes Forwarded Data messages; raw data (serial) payloads are returned in DecodeResult::raw_data, not as CAN frames. class Ax142100a : public AxiomaticModel { public: diff --git a/axiomatic_adapter/src/axiomatic_adapter.cpp b/axiomatic_adapter/src/axiomatic_adapter.cpp index 1a5e381..25fe67c 100644 --- a/axiomatic_adapter/src/axiomatic_adapter.cpp +++ b/axiomatic_adapter/src/axiomatic_adapter.cpp @@ -46,7 +46,8 @@ class AxiomaticAdapter::AxiomaticAdapterImpl const std::function && error_callback_function, const std::chrono::milliseconds & receive_timeout_ms, bool tcp_nodelay, - std::unique_ptr model) + std::unique_ptr model, + const std::function data)> && raw_data_callback_function) : tcp_io_context_() , tcp_socket_(tcp_io_context_) , ip_address_(ip_address) @@ -57,6 +58,7 @@ class AxiomaticAdapter::AxiomaticAdapterImpl , rx_buffer_(RECEIVE_BUFFER_SIZE, 0) , tcp_nodelay_(tcp_nodelay) , model_(std::move(model)) + , raw_data_callback_(raw_data_callback_function) { if (!model_) { throw std::invalid_argument("AxiomaticAdapter requires a model"); @@ -172,13 +174,19 @@ class AxiomaticAdapter::AxiomaticAdapterImpl tcp_receive_thread_ = std::thread([this]() { while (!stop_thread_requested_) { - polymath::socketcan::CanFrame frame = polymath::socketcan::CanFrame(); - std::optional error = receive(frame); + std::optional frame; + std::vector raw_data; + std::optional error = receive(frame, raw_data); - if (!error) { - receive_callback_(std::make_unique(frame)); - } else { + if (error) { error_callback_(*error); + continue; + } + if (frame) { + receive_callback_(std::make_unique(*frame)); + } + if (!raw_data.empty()) { + raw_data_callback_(std::move(raw_data)); } } @@ -205,6 +213,25 @@ class AxiomaticAdapter::AxiomaticAdapterImpl std::optional receive(polymath::socketcan::CanFrame & can_frame) { + std::optional frame; + std::vector raw_data; + auto error = receive(frame, raw_data); + if (error) { + return error; + } + if (!frame) { + return std::make_optional("No CAN frames in received protocol traffic."); + } + can_frame = *frame; + return std::nullopt; + } + + std::optional receive( + std::optional & can_frame, std::vector & raw_data) + { + can_frame.reset(); + raw_data.clear(); + // A previous TCP read may have decoded several CAN frames out of // packed protocol messages — deliver those one at a time before doing // another network read, so packed frames don't get silently dropped @@ -262,14 +289,20 @@ class AxiomaticAdapter::AxiomaticAdapterImpl for (const auto & diagnostic : decoded.diagnostics) { std::cerr << "[Axiomatic parser] " << diagnostic << std::endl; } - if (decoded.frames.empty()) { + // serial data is a byte stream, so one read's payloads are joined in wire order + for (const auto & payload : decoded.raw_data) { + raw_data.insert(raw_data.end(), payload.begin(), payload.end()); + } + if (decoded.frames.empty() && raw_data.empty()) { return std::make_optional( decoded.diagnostics.empty() ? "No CAN frames in received protocol traffic." : decoded.diagnostics.back()); } pending_frames_.insert(pending_frames_.end(), decoded.frames.begin(), decoded.frames.end()); - can_frame = pending_frames_.front(); - pending_frames_.pop_front(); + if (!pending_frames_.empty()) { + can_frame = pending_frames_.front(); + pending_frames_.pop_front(); + } return std::nullopt; } @@ -342,6 +375,8 @@ class AxiomaticAdapter::AxiomaticAdapterImpl bool tcp_nodelay_; std::unique_ptr model_; + + std::function data)> raw_data_callback_; }; AxiomaticAdapter::AxiomaticAdapter( @@ -351,7 +386,8 @@ AxiomaticAdapter::AxiomaticAdapter( const std::function && error_callback_function, const std::chrono::milliseconds & receive_timeout_ms, bool tcp_nodelay, - std::unique_ptr model) + std::unique_ptr model, + const std::function data)> && raw_data_callback_function) : pimpl_(std::make_unique( ip_address, port, @@ -359,7 +395,8 @@ AxiomaticAdapter::AxiomaticAdapter( std::move(error_callback_function), receive_timeout_ms, tcp_nodelay, - std::move(model))) + std::move(model), + std::move(raw_data_callback_function))) {} AxiomaticAdapter::~AxiomaticAdapter() @@ -393,6 +430,12 @@ std::optional AxiomaticAdapter::receive return pimpl_->receive(can_frame); } +std::optional AxiomaticAdapter::receive( + std::optional & can_frame, std::vector & raw_data) +{ + return pimpl_->receive(can_frame, raw_data); +} + std::optional AxiomaticAdapter::receive() { return pimpl_->receive(); diff --git a/axiomatic_adapter/src/axiomatic_model.cpp b/axiomatic_adapter/src/axiomatic_model.cpp index b36abfe..6fa598b 100644 --- a/axiomatic_adapter/src/axiomatic_model.cpp +++ b/axiomatic_adapter/src/axiomatic_model.cpp @@ -29,7 +29,7 @@ namespace polymath::can protocol::DecodeResult AxiomaticModel::decode(const uint8_t * data, size_t size) const { protocol::ParsedMessages parsed = protocol::parseMessages(protocolId(), data, size); - protocol::DecodeResult result{{}, std::move(parsed.diagnostics)}; + protocol::DecodeResult result{{}, std::move(parsed.diagnostics), {}}; for (const auto & message : parsed.messages) { if (!decodeMessage(message, result)) { result.diagnostics.push_back( diff --git a/axiomatic_adapter/src/axiomatic_socketcan_bridge.cpp b/axiomatic_adapter/src/axiomatic_socketcan_bridge.cpp index f7198ac..d0ce0a5 100644 --- a/axiomatic_adapter/src/axiomatic_socketcan_bridge.cpp +++ b/axiomatic_adapter/src/axiomatic_socketcan_bridge.cpp @@ -18,6 +18,7 @@ #include #include #include +#include namespace polymath::can { @@ -28,7 +29,8 @@ AxiomaticSocketcanBridge::AxiomaticSocketcanBridge( const std::string & port, bool verbose, bool tcp_nodelay, - std::unique_ptr model) + std::unique_ptr model, + const std::function data)> && raw_data_callback_function) : socketcan_adapter_(can_interface_name) , axiomatic_adapter_( ip, @@ -37,7 +39,8 @@ AxiomaticSocketcanBridge::AxiomaticSocketcanBridge( [](AxiomaticAdapter::socket_error_string_t /*error*/) { /* no-op */ }, AxiomaticAdapter::DEFAULT_SOCKET_RECEIVE_TIMEOUT_MS, tcp_nodelay, - std::move(model)) + std::move(model), + std::move(raw_data_callback_function)) , verbose_(verbose) { socketcan_adapter_.setOnReceiveCallback( diff --git a/axiomatic_adapter/src/models/ax142100a.cpp b/axiomatic_adapter/src/models/ax142100a.cpp index 7091934..9d2a493 100644 --- a/axiomatic_adapter/src/models/ax142100a.cpp +++ b/axiomatic_adapter/src/models/ax142100a.cpp @@ -39,16 +39,14 @@ constexpr uint8_t STATUS_BYTE_RAW_DATA_FLAG = 0x40; constexpr uint8_t STATUS_BYTE_EXTENDED_ID_FLAG = 0x10; constexpr uint8_t STATUS_BYTE_CAN_DATA_LENGTH_MASK = 0x0F; -/// Appends every CAN frame in one Forwarded Data message body to result +/// Appends every CAN frame in one Forwarded Data message body to result; a raw data payload goes to result.raw_data void decodeForwardedData(const uint8_t * body, size_t body_size, protocol::DecodeResult & result) { size_t walker = 0; while (walker + STATUS_BYTE_BYTES <= body_size) { const uint8_t status_byte = body[walker]; if (0 != (status_byte & STATUS_BYTE_RAW_DATA_FLAG)) { - result.diagnostics.push_back( - "SKIP: raw data payload of " + std::to_string(body_size - walker) + " bytes at body offset " + - std::to_string(walker)); + result.raw_data.emplace_back(body + walker + STATUS_BYTE_BYTES, body + body_size); return; } const bool extended_id = 0 != (status_byte & STATUS_BYTE_EXTENDED_ID_FLAG); diff --git a/axiomatic_adapter/test/axiomatic_model_test.cpp b/axiomatic_adapter/test/axiomatic_model_test.cpp index 7ad96c1..c27332e 100644 --- a/axiomatic_adapter/test/axiomatic_model_test.cpp +++ b/axiomatic_adapter/test/axiomatic_model_test.cpp @@ -12,10 +12,16 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include +#include +#include +#include + #include #include #include #include +#include #include #include #include @@ -91,18 +97,34 @@ TEST_CASE("AX142100A matches the manual example", "[model][ax142100a]") } } -TEST_CASE("AX142100A skips raw data payloads", "[model][ax142100a]") +TEST_CASE("AX142100A returns raw data payloads separately from CAN frames", "[model][ax142100a]") { const std::vector raw = { 0x41, 0x58, 0x49, 0x4F, 0x28, 0x4E, 0x01, 0x00, 0x00, 0x04, 0x00, 0x40, 'a', 'b', 'c'}; const auto stream = concat(raw, AX142100A_MANUAL_EXAMPLE); const auto result = polymath::can::ax142100a::Ax142100a().decode(stream.data(), stream.size()); - REQUIRE(1 == result.diagnostics.size()); + REQUIRE(result.diagnostics.empty()); + REQUIRE(1 == result.raw_data.size()); + REQUIRE(std::vector{'a', 'b', 'c'} == result.raw_data[0]); REQUIRE(1 == result.frames.size()); requireSameFrame(result.frames[0], AX142100A_MANUAL_FRAME); } +TEST_CASE("AX142100A decodes a captured scale print message", "[model][ax142100a]") +{ + // Captured from an AX142100A serial channel: form feed + two text lines, status 0x4F (raw, length nibble capped) + const std::string text = "\x0c\r\nLOAD 0 LB\r\nTOTAL -518882 LB\r\n"; + std::vector message = {0x41, 0x58, 0x49, 0x4F, 0x28, 0x4E, 0x01, 0x00, 0x00, 0x28, 0x00, 0x4F}; + message.insert(message.end(), text.begin(), text.end()); + + const auto result = polymath::can::ax142100a::Ax142100a().decode(message.data(), message.size()); + REQUIRE(result.diagnostics.empty()); + REQUIRE(result.frames.empty()); + REQUIRE(1 == result.raw_data.size()); + REQUIRE(text == std::string(result.raw_data[0].begin(), result.raw_data[0].end())); +} + TEST_CASE("AX140900 encodes a CAN Stream message with all CAN_MAX_DLC data bytes", "[model][ax140900]") { const std::vector expected = { @@ -196,3 +218,62 @@ TEST_CASE("AxiomaticAdapter rejects a null model", "[model]") nullptr), std::invalid_argument); } + +TEST_CASE("AxiomaticAdapter receives raw data alongside CAN frames", "[adapter][ax142100a]") +{ + const int server = socket(AF_INET, SOCK_STREAM, 0); + REQUIRE(0 <= server); + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + address.sin_port = 0; + REQUIRE(0 == bind(server, reinterpret_cast(&address), sizeof(address))); + REQUIRE(0 == listen(server, 1)); + socklen_t address_size = sizeof(address); + REQUIRE(0 == getsockname(server, reinterpret_cast(&address), &address_size)); + + polymath::can::AxiomaticAdapter adapter( + "127.0.0.1", + std::to_string(ntohs(address.sin_port)), + [](std::unique_ptr /*frame*/) {}, + [](polymath::can::AxiomaticAdapter::socket_error_string_t /*error*/) {}, + polymath::can::AxiomaticAdapter::DEFAULT_SOCKET_RECEIVE_TIMEOUT_MS, + true, + std::make_unique()); + REQUIRE(adapter.openSocket()); + const int client = accept(server, nullptr, nullptr); + REQUIRE(0 <= client); + + const std::vector raw_message = { + 0x41, 0x58, 0x49, 0x4F, 0x28, 0x4E, 0x01, 0x00, 0x00, 0x04, 0x00, 0x40, 'a', 'b', 'c'}; + std::optional frame; + std::vector raw_data; + + SECTION("raw data only is not an error") + { + REQUIRE(static_cast(raw_message.size()) == write(client, raw_message.data(), raw_message.size())); + REQUIRE(!adapter.receive(frame, raw_data).has_value()); + REQUIRE(!frame.has_value()); + REQUIRE(std::vector{'a', 'b', 'c'} == raw_data); + } + + SECTION("raw data and a CAN frame in one read") + { + const auto both = concat(raw_message, AX142100A_MANUAL_EXAMPLE); + REQUIRE(static_cast(both.size()) == write(client, both.data(), both.size())); + REQUIRE(!adapter.receive(frame, raw_data).has_value()); + REQUIRE(frame.has_value()); + requireSameFrame(*frame, AX142100A_MANUAL_FRAME); + REQUIRE(std::vector{'a', 'b', 'c'} == raw_data); + } + + SECTION("the CAN-only overload still reports raw-only traffic as an error") + { + REQUIRE(static_cast(raw_message.size()) == write(client, raw_message.data(), raw_message.size())); + CanFrame can_frame; + REQUIRE(adapter.receive(can_frame).has_value()); + } + + close(client); + close(server); +} From 9901ff8046b3c6170ba7fafc662fbcb610598c34 Mon Sep 17 00:00:00 2001 From: David Tarazi Date: Wed, 25 Nov 2026 12:30:35 -0800 Subject: [PATCH 2/3] fix export dependencies --- axiomatic_adapter/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/axiomatic_adapter/CMakeLists.txt b/axiomatic_adapter/CMakeLists.txt index e453477..e67e6d8 100644 --- a/axiomatic_adapter/CMakeLists.txt +++ b/axiomatic_adapter/CMakeLists.txt @@ -129,6 +129,7 @@ if(BUILD_TESTING) endif() endif() +ament_export_dependencies(socketcan_adapter) ament_export_targets(export_${PROJECT_NAME} HAS_LIBRARY_TARGET) ament_package() From d230bf8f12fa7ac5e429fd885794a3adb00a4cb6 Mon Sep 17 00:00:00 2001 From: David Tarazi Date: Wed, 25 Nov 2026 12:51:59 -0800 Subject: [PATCH 3/3] fix timed out error --- axiomatic_adapter/src/axiomatic_adapter.cpp | 6 +++-- .../test/axiomatic_model_test.cpp | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/axiomatic_adapter/src/axiomatic_adapter.cpp b/axiomatic_adapter/src/axiomatic_adapter.cpp index 25fe67c..03988c9 100644 --- a/axiomatic_adapter/src/axiomatic_adapter.cpp +++ b/axiomatic_adapter/src/axiomatic_adapter.cpp @@ -243,6 +243,7 @@ class AxiomaticAdapter::AxiomaticAdapterImpl size_t bytes_received = 0; std::atomic data_received(false); + bool timed_out = false; boost::system::error_code error_code; // set up the timer for timeout @@ -264,7 +265,8 @@ class AxiomaticAdapter::AxiomaticAdapterImpl // set up the timer to handle timeout cancellation timer.async_wait([&](const boost::system::error_code & error) { if (!error && !data_received.load()) { - error_code = boost::asio::error::timed_out; + // the cancelled receive completes after this handler with operation_aborted, so error_code cannot carry it + timed_out = true; // cancel the ongoing async receive operation on timeout (does not close socket) tcp_socket_.cancel(); } @@ -275,7 +277,7 @@ class AxiomaticAdapter::AxiomaticAdapterImpl tcp_io_context_.run(); // check for timeout or other errors - if (error_code == boost::asio::error::timed_out) { + if (timed_out) { return std::optional("Receive operation timed out"); } else if (error_code) { return std::optional( diff --git a/axiomatic_adapter/test/axiomatic_model_test.cpp b/axiomatic_adapter/test/axiomatic_model_test.cpp index c27332e..14d9da1 100644 --- a/axiomatic_adapter/test/axiomatic_model_test.cpp +++ b/axiomatic_adapter/test/axiomatic_model_test.cpp @@ -277,3 +277,30 @@ TEST_CASE("AxiomaticAdapter receives raw data alongside CAN frames", "[adapter][ close(client); close(server); } + +TEST_CASE("AxiomaticAdapter reports a receive timeout as a timeout", "[adapter]") +{ + const int server = socket(AF_INET, SOCK_STREAM, 0); + REQUIRE(0 <= server); + sockaddr_in address{}; + address.sin_family = AF_INET; + address.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + address.sin_port = 0; + REQUIRE(0 == bind(server, reinterpret_cast(&address), sizeof(address))); + REQUIRE(0 == listen(server, 1)); + socklen_t address_size = sizeof(address); + REQUIRE(0 == getsockname(server, reinterpret_cast(&address), &address_size)); + + polymath::can::AxiomaticAdapter adapter("127.0.0.1", std::to_string(ntohs(address.sin_port))); + REQUIRE(adapter.openSocket()); + const int client = accept(server, nullptr, nullptr); + REQUIRE(0 <= client); + + CanFrame frame; + const auto error = adapter.receive(frame); // the server sends nothing + REQUIRE(error.has_value()); + REQUIRE("Receive operation timed out" == *error); + + close(client); + close(server); +}