C2: Add Ethernet message support

This commit is contained in:
David Rebbe
2026-04-30 15:28:40 -04:00
committed by Kyle Schwarz
parent 87f45e060e
commit 9ae3e115fc
27 changed files with 2295 additions and 152 deletions
@@ -4,63 +4,89 @@
#ifdef __cplusplus
#include "icsneo/communication/message/message.h"
#include <array>
#include <string>
#include <vector>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <optional>
namespace icsneo {
struct MACAddress {
uint8_t data[6];
std::string toString() const {
std::stringstream ss;
for(size_t i = 0; i < 6; i++) {
ss << std::hex << std::setw(2) << std::setfill('0') << (int)data[i];
if(i != 5)
ss << ':';
}
return ss.str();
}
friend std::ostream& operator<<(std::ostream& os, const MACAddress& mac) {
os << mac.toString();
return os;
}
};
using MACAddress = std::array<uint8_t, 6>;
class EthernetMessage : public Frame {
public:
// Standard Ethernet fields
bool preemptionEnabled = false;
uint8_t preemptionFlags = 0;
// Frame Check Sequence
std::optional<uint32_t> fcs;
bool frameTooShort = false;
bool noPadding = false;
bool fcsVerified = false;
bool txAborted = false;
bool crcError = false;
bool isT1S = false;
bool isT1SSymbol = false;
bool isT1SBurst = false;
bool txCollision = false;
bool isT1SWake = false;
uint8_t t1sNodeId = 0;
uint8_t t1sBurstCount = 0;
uint8_t t1sSymbolType = 0;
// T1S-specific fields
struct T1S {
T1S() {}
// Accessors
const MACAddress& getDestinationMAC() const { return *(const MACAddress*)(data.data() + 0); }
const MACAddress& getSourceMAC() const { return *(const MACAddress*)(data.data() + 6); }
uint16_t getEtherType() const { return (data[12] << 8) | data[13]; }
bool isSymbol = false;
bool isBurst = false;
bool txCollision = false;
bool isWake = false;
uint8_t nodeId = 0;
uint8_t burstCount = 0;
uint8_t symbolType = 0;
};
std::optional<T1S> t1s;
// TSN-specific fields
// If we expand TSN we should probably do something similar to what we did above with T1S.
// IEEE 802.1Qbu frame preemption
std::optional<uint8_t> preemptionFlags;
// Helper functions to extract Destination MAC from the data payload
// returns std::nullopt if the data payload is not large enough
std::optional<MACAddress> getDestinationMAC() const {
if(data.size() < 6) {
return std::nullopt;
}
MACAddress mac;
std::copy(data.begin(), data.begin() + 6, mac.begin());
return mac;
}
// Helper functions to extract Source MAC from the data payload
// returns std::nullopt if the data payload is not large enough
std::optional<MACAddress> getSourceMAC() const {
if(data.size() < 12) {
return std::nullopt;
}
MACAddress mac;
std::copy(data.begin() + 6, data.begin() + 12, mac.begin());
return mac;
}
// Helper function to extract EtherType from the data payload
//
// EtherType is a two-octet field in an Ethernet frame (big-endian).
// It is used to indicate which protocol is encapsulated in the payload of the frame
// and is used at the receiving end by the data link layer to determine how the payload is processed.
// For example, an EtherType of 0x0800 indicates that the payload is an IPv4 packet, while 0x86DD indicates an IPv6 packet.
//
// returns std::nullopt if the data payload is not large enough
std::optional<uint16_t> getEtherType() const {
if(data.size() < 14) {
return std::nullopt;
}
// EtherType is stored in a 2-byte network byte order (big-endian)
return static_cast<uint16_t>((uint16_t(data[12]) << 8) | uint16_t(data[13]));
}
};
}
#endif // __cplusplus
#endif
#endif // __ETHERNETMESSAGE_H_
@@ -10,6 +10,7 @@
#include <iostream>
#include <iomanip>
#include <sstream>
#include <optional>
namespace icsneo {
+1
View File
@@ -44,6 +44,7 @@ typedef enum _icsneoc2_error_t {
icsneoc2_error_script_load_prepare_failed, // Failed to prepare script load
icsneoc2_error_close_failed, // Failed to close device
icsneoc2_error_reconnect_failed, // Failed to reconnect to device
icsneoc2_error_invalid_data, // Failed to get/set data due to invalid data pointer or size
// NOTE: Any new values added here should be updated in icsneoc2_error_code_get
icsneoc2_error_maxsize
} _icsneoc2_error_t;
+120
View File
@@ -142,6 +142,126 @@ icsneoc2_error_t icsneoc2_message_can_props_set(icsneoc2_message_t* message, con
*/
icsneoc2_error_t icsneoc2_message_can_props_get(icsneoc2_message_t* message, uint64_t* arb_id, icsneoc2_message_can_flags_t* flags);
/**
* Create Ethernet message
*
* @param[out] message Pointer to icsneoc2_message_t to copy the message into.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_message_eth_create(icsneoc2_message_t** message);
// Standard Ethernet frame flags
#define ICSNEOC2_MESSAGE_ETH_FLAGS_FRAME_TOO_SHORT 0x001
#define ICSNEOC2_MESSAGE_ETH_FLAGS_NO_PADDING 0x002
#define ICSNEOC2_MESSAGE_ETH_FLAGS_FCS_VERIFIED 0x004
#define ICSNEOC2_MESSAGE_ETH_FLAGS_TX_ABORTED 0x008
#define ICSNEOC2_MESSAGE_ETH_FLAGS_CRC_ERROR 0x010
#define ICSNEOC2_MESSAGE_ETH_FLAGS_IS_T1S 0x020
#define ICSNEOC2_MESSAGE_ETH_FLAGS_PREEMPTION_ENABLED 0x040
typedef uint64_t icsneoc2_message_eth_flags_t;
// T1S-specific Ethernet frame flags
#define ICSNEOC2_MESSAGE_ETH_T1S_FLAGS_IS_T1S_SYMBOL 0x002
#define ICSNEOC2_MESSAGE_ETH_T1S_FLAGS_IS_T1S_BURST 0x004
#define ICSNEOC2_MESSAGE_ETH_T1S_FLAGS_TX_COLLISION 0x008
#define ICSNEOC2_MESSAGE_ETH_T1S_FLAGS_IS_T1S_WAKE 0x010
typedef uint64_t icsneoc2_message_eth_t1s_flags_t;
/**
* Set the Ethernet specific properties of a message
*
* @param[in] message The message to modify.
* @param[in] flags Pointer to a icsneoc2_message_eth_flags_t containing the flags to set. If NULL, flags are not modified.
* @param[in] has_fcs Pointer to a bool indicating whether the FCS is present. If NULL, it's ignored.
* @param[in] fcs Pointer to a uint32_t containing the FCS value. If NULL, the FCS is not modified.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters or icsneoc2_error_invalid_type otherwise.
*/
icsneoc2_error_t icsneoc2_message_eth_props_set(icsneoc2_message_t* message, const icsneoc2_message_eth_flags_t* flags, const bool* has_fcs, const uint32_t* fcs);
/**
* Get the Ethernet specific properties of a message
*
* @param[in] message The message to check.
* @param[out] flags Pointer to a icsneoc2_message_eth_flags_t to copy the flags into. If NULL, it's ignored.
* @param[out] has_fcs Pointer to a bool indicating whether the FCS is present. If NULL, it's ignored.
* @param[out] fcs Pointer to a uint32_t to copy the FCS value into. Only valid if has_fcs is true, set to 0 if FCS is not present. If NULL, it's ignored.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters or icsneoc2_error_invalid_type otherwise.
*/
icsneoc2_error_t icsneoc2_message_eth_props_get(icsneoc2_message_t* message, icsneoc2_message_eth_flags_t* flags, bool* has_fcs, uint32_t* fcs);
/**
* Get the destination and/or source MAC address from an Ethernet message.
* The MAC addresses are extracted from the message data bytes.
*
* @param[in] message The message to check.
* @param[out] dst_mac Pointer to a 6-byte buffer to copy the destination MAC into. If NULL, it's ignored.
* @param[out] src_mac Pointer to a 6-byte buffer to copy the source MAC into. If NULL, it's ignored.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters or icsneoc2_error_invalid_type otherwise.
*/
icsneoc2_error_t icsneoc2_message_eth_mac_get(icsneoc2_message_t* message, uint8_t* dst_mac, uint8_t* src_mac);
/**
* Helper function to get the EtherType field from an Ethernet message payload.
*
* EtherType is a two-octet field in an Ethernet frame (big-endian).
* It is used to indicate which protocol is encapsulated in the payload of the frame
* and is used at the receiving end by the data link layer to determine how the payload is processed.
* For example, an EtherType of 0x0800 indicates that the payload is an IPv4 packet, while 0x86DD indicates an IPv6 packet.
*
* @param[in] message The message to check.
* @param[out] ether_type Pointer to a uint16_t to copy the EtherType into.
*
* @note The EtherType is extracted from the message data bytes, so the message must have the data field and it must be
* large enough to contain the EtherType (at least 14 bytes). Returned value is host byte order.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters, icsneoc2_error_invalid_type, icsneoc2_error_invalid_data otherwise.
*/
icsneoc2_error_t icsneoc2_message_eth_ether_type_get(icsneoc2_message_t* message, uint16_t* ether_type);
/**
* Set the T1S specific properties of an Ethernet message
*
* @param[in] message The message to modify.
* @param[in] flags Pointer to a icsneoc2_message_eth_t1s_flags_t containing the T1S flags to set. If NULL, flags are not modified.
* @param[in] node_id Pointer to a uint8_t containing the T1S node ID. If NULL, it's ignored.
* @param[in] burst_count Pointer to a uint8_t containing the T1S burst count. If NULL, it's ignored.
* @param[in] symbol_type Pointer to a uint8_t containing the T1S symbol type. If NULL, it's ignored.
*
* @note If all four optional parameters are NULL, the T1S-specific state is cleared.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters or icsneoc2_error_invalid_type otherwise.
*/
icsneoc2_error_t icsneoc2_message_eth_t1s_props_set(icsneoc2_message_t* message, const icsneoc2_message_eth_t1s_flags_t* flags, const uint8_t* node_id, const uint8_t* burst_count, const uint8_t* symbol_type);
/**
* Get the T1S specific properties of an Ethernet message
*
* @param[in] message The message to check.
* @param[out] flags Pointer to a icsneoc2_message_eth_t1s_flags_t to copy the T1S flags into. If NULL, it's ignored.
* @param[out] node_id Pointer to a uint8_t to copy the T1S node ID into. If NULL, it's ignored.
* @param[out] burst_count Pointer to a uint8_t to copy the T1S burst count into. If NULL, it's ignored.
* @param[out] symbol_type Pointer to a uint8_t to copy the T1S symbol type into. If NULL, it's ignored.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters or icsneoc2_error_invalid_type otherwise.
*/
icsneoc2_error_t icsneoc2_message_eth_t1s_props_get(icsneoc2_message_t* message, icsneoc2_message_eth_t1s_flags_t* flags, uint8_t* node_id, uint8_t* burst_count, uint8_t* symbol_type);
/**
* Check if a message is an Ethernet message
*
* @param[in] message The message to check.
* @param[out] is_ethernet Pointer to a bool to copy the Ethernet status of the message into.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_message_is_ethernet(icsneoc2_message_t* message, bool* is_ethernet);
/**
* Check if a message is valid
*
+156
View File
@@ -456,6 +456,28 @@ icsneoc2_error_t icsneoc2_settings_t1s_tx_opp_timer_get(icsneoc2_device_t* devic
*/
icsneoc2_error_t icsneoc2_settings_t1s_tx_opp_timer_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t value);
/**
* Get the burst timer for a network that supports 10BASE-T1S.
*
* @param[in] device The device to check.
* @param[in] netid The network ID to check.
* @param[out] value Pointer to store the burst timer value.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_burst_timer_get(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t* value);
/**
* Set the burst timer for a network that supports 10BASE-T1S.
*
* @param[in] device The device to configure.
* @param[in] netid The network ID to configure.
* @param[in] value The burst timer value to set.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_burst_timer_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t value);
/**
* Get the Max burst timer for a network that supports 10BASE-T1S.
*
@@ -478,6 +500,140 @@ icsneoc2_error_t icsneoc2_settings_t1s_max_burst_timer_for_get(icsneoc2_device_t
*/
icsneoc2_error_t icsneoc2_settings_t1s_max_burst_timer_for_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t value);
/**
* Get the alternate local ID for a network that supports 10BASE-T1S.
*
* @param[in] device The device to check.
* @param[in] netid The network ID to check.
* @param[out] value Pointer to store the alternate local ID.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_local_id_alternate_get(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t* value);
/**
* Set the alternate local ID for a network that supports 10BASE-T1S.
*
* @param[in] device The device to configure.
* @param[in] netid The network ID to configure.
* @param[in] value The alternate local ID to set.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_local_id_alternate_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t value);
/**
* Check if T1S termination is enabled for a network that supports 10BASE-T1S.
*
* @param[in] device The device to check.
* @param[in] netid The network ID to check.
* @param[out] value Pointer to store the termination enable state.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_is_termination_enabled_for(icsneoc2_device_t* device, icsneoc2_netid_t netid, bool* value);
/**
* Enable or disable T1S termination for a network that supports 10BASE-T1S.
*
* @param[in] device The device to configure.
* @param[in] netid The network ID to configure.
* @param[in] value True to enable termination, false to disable.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_termination_for_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, bool value);
/**
* Check if T1S bus decoding beacons are enabled for a network that supports 10BASE-T1S.
*
* @param[in] device The device to check.
* @param[in] netid The network ID to check.
* @param[out] value Pointer to store the bus decoding beacons enable state.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_is_bus_decoding_beacons_enabled_for(icsneoc2_device_t* device, icsneoc2_netid_t netid, bool* value);
/**
* Enable or disable T1S bus decoding beacons for a network that supports 10BASE-T1S.
*
* @param[in] device The device to configure.
* @param[in] netid The network ID to configure.
* @param[in] value True to enable bus decoding beacons, false to disable.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_bus_decoding_beacons_for_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, bool value);
/**
* Check if T1S bus decoding all is enabled for a network that supports 10BASE-T1S.
*
* @param[in] device The device to check.
* @param[in] netid The network ID to check.
* @param[out] value Pointer to store the bus decoding all enable state.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_is_bus_decoding_all_enabled_for(icsneoc2_device_t* device, icsneoc2_netid_t netid, bool* value);
/**
* Enable or disable T1S bus decoding all for a network that supports 10BASE-T1S.
*
* @param[in] device The device to configure.
* @param[in] netid The network ID to configure.
* @param[in] value True to enable bus decoding all, false to disable.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_bus_decoding_all_for_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, bool value);
/**
* Get the multi-ID enable mask for a network that supports 10BASE-T1S.
*
* @param[in] device The device to check.
* @param[in] netid The network ID to check.
* @param[out] value Pointer to store the multi-ID enable mask.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_multi_id_enable_mask_get(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t* value);
/**
* Set the multi-ID enable mask for a network that supports 10BASE-T1S.
*
* @param[in] device The device to configure.
* @param[in] netid The network ID to configure.
* @param[in] value The multi-ID enable mask to set.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_multi_id_enable_mask_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t value);
/**
* Get a multi-ID entry for a network that supports 10BASE-T1S.
*
* @param[in] device The device to check.
* @param[in] netid The network ID to check.
* @param[in] index The multi-ID index to get (0-6).
* @param[out] value Pointer to store the multi-ID value.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_multi_id_get(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t index, uint8_t* value);
/**
* Set a multi-ID entry for a network that supports 10BASE-T1S.
*
* @param[in] device The device to configure.
* @param[in] netid The network ID to configure.
* @param[in] index The multi-ID index to set (0-6).
* @param[in] value The multi-ID value to set.
*
* @return icsneoc2_error_t icsneoc2_error_success if successful, icsneoc2_error_invalid_parameters otherwise.
*/
icsneoc2_error_t icsneoc2_settings_t1s_multi_id_set(icsneoc2_device_t* device, icsneoc2_netid_t netid, uint8_t index, uint8_t value);
/**
* Set the analog output enabled.
*