Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions axiomatic_adapter/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
5 changes: 3 additions & 2 deletions axiomatic_adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<polymath::can::ax142100a::Ax142100a>() // optional, defaults to ax140900::Ax140900
std::make_unique<polymath::can::ax142100a::Ax142100a>(), // optional, defaults to ax140900::Ax140900
[](std::vector<uint8_t> /*data*/) { /* serial data of one TCP read, ax142100a only */ } // optional
);

// open the socket
Expand Down Expand Up @@ -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<CanFrame> &, std::vector<uint8_t> &)`. 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.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "axiomatic_adapter/axiomatic_model.hpp"
#include "axiomatic_adapter/models/ax140900.hpp"
Expand Down Expand Up @@ -67,6 +68,8 @@ class AxiomaticAdapter : public std::enable_shared_from_this<AxiomaticAdapter>
/// 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<ax142100a::Ax142100a>(); 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,
Expand All @@ -76,7 +79,9 @@ class AxiomaticAdapter : public std::enable_shared_from_this<AxiomaticAdapter>
[](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<const AxiomaticModel> model = std::make_unique<ax140900::Ax140900>());
std::unique_ptr<const AxiomaticModel> model = std::make_unique<ax140900::Ax140900>(),
const std::function<void(std::vector<uint8_t> data)> && raw_data_callback_function =
[](std::vector<uint8_t> /*data*/) { /*do nothing*/ });

/// @brief Destructor for AxiomaticAdapter
virtual ~AxiomaticAdapter();
Expand All @@ -90,10 +95,22 @@ class AxiomaticAdapter : public std::enable_shared_from_this<AxiomaticAdapter>
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<socket_error_string_t> 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<socket_error_string_t> receive(
std::optional<polymath::socketcan::CanFrame> & can_frame, std::vector<uint8_t> & raw_data);

/// @brief Receive returns the received CanFrame
/// @return optional CanFrame
/// nullopt is returned if no frame received, acts like null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,14 @@ struct ParsedMessages
std::vector<std::string> 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<polymath::socketcan::CanFrame> frames;
std::vector<std::string> diagnostics;
/// @brief One entry per raw data message, in wire order; only models that carry serial data fill it
std::vector<std::vector<uint8_t>> raw_data;
};

/// @brief Read an unsigned little-endian integer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
#ifndef AXIOMATIC_ADAPTER__AXIOMATIC_SOCKETCAN_BRIDGE_NODE_HPP_
#define AXIOMATIC_ADAPTER__AXIOMATIC_SOCKETCAN_BRIDGE_NODE_HPP_

#include <functional>
#include <memory>
#include <string>
#include <vector>

#include "axiomatic_adapter/axiomatic_adapter.hpp"
#include "axiomatic_adapter/models/ax140900.hpp"
Expand All @@ -40,13 +42,17 @@ class AxiomaticSocketcanBridge
/// AxiomaticAdapter socket. See AxiomaticAdapter for the
/// full tradeoff discussion.
/// @param model converter model, e.g. std::make_unique<ax142100a::Ax142100a>(); 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<const AxiomaticModel> model = std::make_unique<ax140900::Ax140900>());
std::unique_ptr<const AxiomaticModel> model = std::make_unique<ax140900::Ax140900>(),
const std::function<void(std::vector<uint8_t> data)> && raw_data_callback_function =
[](std::vector<uint8_t> /*data*/) { /*do nothing*/ });

/// @brief Destruct axiomatic socketcan bridge
~AxiomaticSocketcanBridge();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
71 changes: 58 additions & 13 deletions axiomatic_adapter/src/axiomatic_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class AxiomaticAdapter::AxiomaticAdapterImpl
const std::function<void(AxiomaticAdapter::socket_error_string_t error)> && error_callback_function,
const std::chrono::milliseconds & receive_timeout_ms,
bool tcp_nodelay,
std::unique_ptr<const AxiomaticModel> model)
std::unique_ptr<const AxiomaticModel> model,
const std::function<void(std::vector<uint8_t> data)> && raw_data_callback_function)
: tcp_io_context_()
, tcp_socket_(tcp_io_context_)
, ip_address_(ip_address)
Expand All @@ -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");
Expand Down Expand Up @@ -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<AxiomaticAdapter::socket_error_string_t> error = receive(frame);
std::optional<polymath::socketcan::CanFrame> frame;
std::vector<uint8_t> raw_data;
std::optional<AxiomaticAdapter::socket_error_string_t> error = receive(frame, raw_data);

if (!error) {
receive_callback_(std::make_unique<polymath::socketcan::CanFrame>(frame));
} else {
if (error) {
error_callback_(*error);
continue;
}
if (frame) {
receive_callback_(std::make_unique<polymath::socketcan::CanFrame>(*frame));
}
if (!raw_data.empty()) {
raw_data_callback_(std::move(raw_data));
}
}

Expand All @@ -205,6 +213,25 @@ class AxiomaticAdapter::AxiomaticAdapterImpl

std::optional<AxiomaticAdapter::socket_error_string_t> receive(polymath::socketcan::CanFrame & can_frame)
{
std::optional<polymath::socketcan::CanFrame> frame;
std::vector<uint8_t> raw_data;
auto error = receive(frame, raw_data);
if (error) {
return error;
}
if (!frame) {
return std::make_optional<AxiomaticAdapter::socket_error_string_t>("No CAN frames in received protocol traffic.");
}
can_frame = *frame;
return std::nullopt;
}

std::optional<AxiomaticAdapter::socket_error_string_t> receive(
std::optional<polymath::socketcan::CanFrame> & can_frame, std::vector<uint8_t> & 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
Expand All @@ -216,6 +243,7 @@ class AxiomaticAdapter::AxiomaticAdapterImpl

size_t bytes_received = 0;
std::atomic<bool> data_received(false);
bool timed_out = false;
boost::system::error_code error_code;

// set up the timer for timeout
Expand All @@ -237,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();
}
Expand All @@ -248,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<AxiomaticAdapter::socket_error_string_t>("Receive operation timed out");
} else if (error_code) {
return std::optional<AxiomaticAdapter::socket_error_string_t>(
Expand All @@ -262,14 +291,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<AxiomaticAdapter::socket_error_string_t>(
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;
}

Expand Down Expand Up @@ -342,6 +377,8 @@ class AxiomaticAdapter::AxiomaticAdapterImpl
bool tcp_nodelay_;

std::unique_ptr<const AxiomaticModel> model_;

std::function<void(std::vector<uint8_t> data)> raw_data_callback_;
};

AxiomaticAdapter::AxiomaticAdapter(
Expand All @@ -351,15 +388,17 @@ AxiomaticAdapter::AxiomaticAdapter(
const std::function<void(AxiomaticAdapter::socket_error_string_t error)> && error_callback_function,
const std::chrono::milliseconds & receive_timeout_ms,
bool tcp_nodelay,
std::unique_ptr<const AxiomaticModel> model)
std::unique_ptr<const AxiomaticModel> model,
const std::function<void(std::vector<uint8_t> data)> && raw_data_callback_function)
: pimpl_(std::make_unique<AxiomaticAdapterImpl>(
ip_address,
port,
std::move(receive_callback_function),
std::move(error_callback_function),
receive_timeout_ms,
tcp_nodelay,
std::move(model)))
std::move(model),
std::move(raw_data_callback_function)))
{}

AxiomaticAdapter::~AxiomaticAdapter()
Expand Down Expand Up @@ -393,6 +432,12 @@ std::optional<AxiomaticAdapter::socket_error_string_t> AxiomaticAdapter::receive
return pimpl_->receive(can_frame);
}

std::optional<AxiomaticAdapter::socket_error_string_t> AxiomaticAdapter::receive(
std::optional<polymath::socketcan::CanFrame> & can_frame, std::vector<uint8_t> & raw_data)
{
return pimpl_->receive(can_frame, raw_data);
}

std::optional<const polymath::socketcan::CanFrame> AxiomaticAdapter::receive()
{
return pimpl_->receive();
Expand Down
2 changes: 1 addition & 1 deletion axiomatic_adapter/src/axiomatic_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions axiomatic_adapter/src/axiomatic_socketcan_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <memory>
#include <string>
#include <utility>
#include <vector>

namespace polymath::can
{
Expand All @@ -28,7 +29,8 @@ AxiomaticSocketcanBridge::AxiomaticSocketcanBridge(
const std::string & port,
bool verbose,
bool tcp_nodelay,
std::unique_ptr<const AxiomaticModel> model)
std::unique_ptr<const AxiomaticModel> model,
const std::function<void(std::vector<uint8_t> data)> && raw_data_callback_function)
: socketcan_adapter_(can_interface_name)
, axiomatic_adapter_(
ip,
Expand All @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions axiomatic_adapter/src/models/ax142100a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading
Loading