Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::net::EthernetFrameHandler Class Reference

Handles incoming Ethernet Frames and routes them to the appropriate payload handlers. More...

#include <ethernetframe.h>

Inheritance diagram for MaxOS::net::EthernetFrameHandler:
MaxOS::drivers::ethernet::EthernetDriverEventHandler MaxOS::common::EventHandler< EthernetDriverEvents >

Public Member Functions

 EthernetFrameHandler (drivers::ethernet::EthernetDriver *driver, common::OutputStream *error_messages)
 Construct a new Ether Frame Handler object.
 
drivers::ethernet::MediaAccessControlAddress get_mac ()
 Get the MAC address of this device.
 
bool data_received (uint8_t *data, uint32_t size) override
 Handle the received packet.
 
void connect_handler (EthernetFramePayloadHandler *handler)
 connect a handler to the frame handler
 
void send_ethernet_frame (uint64_t destination_mac, uint16_t frame_type, uint8_t *data, uint32_t size)
 send an packet via the backend driver
 
- Public Member Functions inherited from MaxOS::drivers::ethernet::EthernetDriverEventHandler
 EthernetDriverEventHandler ()
 __EVENT HANDLER___
 
common::Event< EthernetDriverEvents > * on_event (common::Event< EthernetDriverEvents > *event) override
 Handle an event.
 
virtual void before_send (uint8_t *buffer, uint32_t size)
 Handle before send event.
 
virtual void data_sent (uint8_t *buffer, uint32_t size)
 Handle data sent event.
 
- Public Member Functions inherited from MaxOS::common::EventHandler< EthernetDriverEvents >
virtual Event< EthernetDriverEvents > * on_event (Event< EthernetDriverEvents > *event)
 This function is called when an event is raised.
 

Protected Attributes

common::Map< uint16_t, EthernetFramePayloadHandler * > frame_handlers
 The map of frame handlers by type.
 
drivers::ethernet::EthernetDriverethernet_driver
 The driver this frame handler is using.
 
common::OutputStreamerror_messages
 The output stream for error messages.
 

Detailed Description

Handles incoming Ethernet Frames and routes them to the appropriate payload handlers.

Definition at line 76 of file ethernetframe.h.

Constructor & Destructor Documentation

◆ EthernetFrameHandler()

EthernetFrameHandler::EthernetFrameHandler ( drivers::ethernet::EthernetDriver driver,
common::OutputStream error_messages 
)

Construct a new Ether Frame Handler object.

Parameters
driverThe backend ethernet driver
error_messagesThe output stream for error messages

Definition at line 73 of file ethernetframe.cpp.

75
76 this->ethernet_driver = driver;
78
79 driver->connect_event_handler(this);
80
81}
void connect_event_handler(EventHandler< EventType > *handler)
connect an event handler to the event manager if it is not already connected
drivers::ethernet::EthernetDriver * ethernet_driver
The driver this frame handler is using.
common::OutputStream * error_messages
The output stream for error messages.

References error_messages, and ethernet_driver.

Member Function Documentation

◆ connect_handler()

void EthernetFrameHandler::connect_handler ( EthernetFramePayloadHandler handler)

connect a handler to the frame handler

Parameters
handlerThe handler to connect

Definition at line 164 of file ethernetframe.cpp.

164 {
165
166 // Convert the protocol type to big endian
167 uint16_t frame_type_be = ((handler->handled_type >> 8) & 0xFF) | ((handler->handled_type << 8) & 0xFF00);
168
169 // Add the handler to the list
171
172}
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
common::Map< uint16_t, EthernetFramePayloadHandler * > frame_handlers
The map of frame handlers by type.

References frame_handlers.

Referenced by MaxOS::net::EthernetFramePayloadHandler::EthernetFramePayloadHandler().

◆ data_received()

bool EthernetFrameHandler::data_received ( uint8_t buffer,
uint32_t  size 
)
overridevirtual

Handle the received packet.

Parameters
bufferthe buffer with the received data
sizethe size of the received data
Returns
True if the data is to be sent back, false otherwise
Todo:
Future debugging me: the override is not being called in derived classes

Reimplemented from MaxOS::drivers::ethernet::EthernetDriverEventHandler.

Definition at line 104 of file ethernetframe.cpp.

104 {
105
106 error_messages->write("EFH: Data received\n");
107
108
109 //Check if the size is big enough to contain an ethernet frame
110 if(size < sizeof(EthernetFrameHeader))
111 return false;
112
113 //Convert to struct for easier use
114 auto* frame = (EthernetFrameHeader*) buffer;
115 bool send_back = false;
116
117 //Only handle if it is for this device
118 if(frame->destination_mac == 0xFFFFFFFFFFFF //If it is a broadcast
119 || frame->destination_mac == ethernet_driver->get_media_access_control_address()) //If it is for this device
120 {
121
122 // Find the handler for the protocol
124
125 // If the handler is found
126 if(handler_iterator != frame_handlers.end()) {
127
128 //Handle the data
129 error_messages->write("EFH: Handling ethernet frame payload\n");
130 send_back = handler_iterator->second->handle_ethernetframe_payload(buffer + sizeof(EthernetFrameHeader),
131 size - sizeof(EthernetFrameHeader));
132 error_messages->write("..DONE\n");
133
134 } else {
135
136 //If the handler is not found, print an error message
137 error_messages->write("EFH: Unhandled ethernet frame type 0x");
139 error_messages->write("\n");
140
141 }
142 }
143
144 //If the data is to be sent back again
145 if(send_back) {
146
147 error_messages->write("EFH: Sending back\n");
148
149 frame->destination_mac = frame->source_mac; //Set the new destination to be the device the data was received from
150 frame->source_mac = ethernet_driver->get_media_access_control_address(); //Set the new source to be this device's MAC address
151
152 }
153
154 //Return if the data is to be sent back
155 return send_back;
156
157}
virtual void write_hex(uint64_t hex_to_write)
Writes a hex to the output stream.
void write(string string_to_write) override
Writes a string to the output stream.
virtual MediaAccessControlAddress get_media_access_control_address()
Get the MAC address.
Definition ethernet.cpp:89
Structure the raw data. This is the header of an Ethernet Frame.

References error_messages, ethernet_driver, frame_handlers, MaxOS::drivers::ethernet::EthernetDriver::get_media_access_control_address(), MaxOS::common::OutputStream::write(), and MaxOS::common::OutputStream::write_hex().

◆ get_mac()

◆ send_ethernet_frame()

void EthernetFrameHandler::send_ethernet_frame ( uint64_t  destination_mac,
uint16_t  frame_type,
uint8_t data,
uint32_t  size 
)

send an packet via the backend driver

Parameters
destination_macthe destination MAC address
frame_typethe type of the protocol
datathe data to send
sizethe size of the payload

Definition at line 182 of file ethernetframe.cpp.

182 {
183
184 error_messages->write("EFH: Sending frame...");
185
186 //Allocate memory for the buffer
187 auto* buffer = (uint8_t*) MemoryManager::kmalloc(size + sizeof(EthernetFrameHeader));
188 auto* frame = (EthernetFrameHeader*) buffer;
189
190 //Put data in the header
191 frame->destination_mac = destination_mac;
193 frame->type = (frame_type >> 8) | (frame_type << 8); //Convert to big endian
194
195 //Copy the data
196 for(uint8_t* src = data + size - 1, * dst = buffer + sizeof(EthernetFrameHeader) + size - 1; src >= data; --src, --dst)
197 *dst = *src;
198
199 //Send the data
200 ethernet_driver->send(buffer, size + sizeof(EthernetFrameHeader));
201
202 error_messages->write("Done\n");
203
204
205 //Free the buffer
206 MemoryManager::kfree(buffer);
207}
void send(uint8_t *buffer, uint32_t size)
send data to the network via the driver backend
Definition ethernet.cpp:99
static void * kmalloc(size_t size)
Allocates a block of memory in the KERNEL space.
static void kfree(void *pointer)
Frees a block of memory using the kernel memory manager.

References error_messages, ethernet_driver, MaxOS::drivers::ethernet::EthernetDriver::get_media_access_control_address(), MaxOS::memory::MemoryManager::kfree(), MaxOS::memory::MemoryManager::kmalloc(), MaxOS::drivers::ethernet::EthernetDriver::send(), and MaxOS::common::OutputStream::write().

Referenced by MaxOS::net::EthernetFramePayloadHandler::send(), and MaxOS::net::InternetProtocolHandler::send_internet_protocol_packet().

Member Data Documentation

◆ error_messages

common::OutputStream* MaxOS::net::EthernetFrameHandler::error_messages
protected

The output stream for error messages.

Definition at line 82 of file ethernetframe.h.

Referenced by data_received(), EthernetFrameHandler(), and send_ethernet_frame().

◆ ethernet_driver

drivers::ethernet::EthernetDriver* MaxOS::net::EthernetFrameHandler::ethernet_driver
protected

The driver this frame handler is using.

Definition at line 81 of file ethernetframe.h.

Referenced by data_received(), EthernetFrameHandler(), get_mac(), and send_ethernet_frame().

◆ frame_handlers

common::Map<uint16_t, EthernetFramePayloadHandler*> MaxOS::net::EthernetFrameHandler::frame_handlers
protected

The map of frame handlers by type.

Definition at line 79 of file ethernetframe.h.

Referenced by connect_handler(), and data_received().


The documentation for this class was generated from the following files: