Communication Library for Autonomous Systems v1.0
Reliable and secure communication library for autonomous vehicle systems
Loading...
Searching...
No Matches
ethernet.h
Go to the documentation of this file.
1#ifndef ETHERNET_H
2#define ETHERNET_H
4#include <cstdint>
5#include <cstring>
6#include <sstream>
7#include <iomanip>
8
9class Ethernet {
10
11 public:
12 static constexpr unsigned int MTU = 1500; // Ethernet MTU = 1500 Bytes
13 static constexpr unsigned int MAC_SIZE = 6;
14
15 // Defining MAC address
16 struct Address {
17 std::uint8_t bytes[MAC_SIZE];
18 };
19
21 static const Ethernet::Address BROADCAST;
22
23 // Protocol Type
24 typedef std::uint16_t Protocol;
25
26 static constexpr unsigned int HEADER_SIZE = sizeof(Address)*2 + sizeof(Protocol);
27
28 // Defining Ethernet Frame
35
36 static std::string mac_to_string(Address addr) {
37 std::ostringstream oss;
38 for (unsigned int i = 0; i < MAC_SIZE; ++i) {
39 if (i != 0) oss << ":";
41 }
42 return oss.str();
43 }
44
45 // Constructor / Destructor
46 Ethernet() = default;
47 ~Ethernet() = default;
48
49}; // all necessary definitions and formats
50
51constexpr unsigned int Ethernet::MTU;
52
54const Ethernet::Address Ethernet::BROADCAST = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
55
56
57inline bool operator==(const Ethernet::Address& a, const Ethernet::Address& b) {
58 return std::memcmp(a.bytes, b.bytes, Ethernet::MAC_SIZE) == 0;
59}
60
61inline bool operator!=(const Ethernet::Address& a, const Ethernet::Address& b) {
62 return !(a == b);
63}
64
65#endif // ETHERNET_H
Definition ethernet.h:9
struct Ethernet::Frame __attribute__((packed))
static std::string mac_to_string(Address addr)
Definition ethernet.h:36
Ethernet()=default
static constexpr unsigned int MAC_SIZE
Definition ethernet.h:13
static const Ethernet::Address BROADCAST
Definition ethernet.h:54
std::uint16_t Protocol
Definition ethernet.h:24
static constexpr unsigned int HEADER_SIZE
Definition ethernet.h:26
static constexpr unsigned int MTU
Definition ethernet.h:12
static const Ethernet::Address NULL_ADDRESS
Definition ethernet.h:53
~Ethernet()=default
Definition protocol.h:29
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
bool operator==(const Ethernet::Address &a, const Ethernet::Address &b)
Definition ethernet.h:57
bool operator!=(const Ethernet::Address &a, const Ethernet::Address &b)
Definition ethernet.h:61
Definition ethernet.h:16
std::uint8_t bytes[MAC_SIZE]
Definition ethernet.h:17
Definition ethernet.h:29
Address dst
Definition ethernet.h:30
Address src
Definition ethernet.h:31
std::uint8_t payload[MTU]
Definition ethernet.h:33
Protocol prot
Definition ethernet.h:32