Move part of Net::Update to HandlePacket

This commit is contained in:
Sijmen 2021-01-18 15:41:35 +01:00
parent 81e426fef4
commit 8275e5122d
Signed by: vijfhoek
GPG key ID: DAF7821E067D9C48
5 changed files with 30 additions and 24 deletions

View file

@ -132,8 +132,8 @@ namespace Net::Arp
const auto macAddress = Utils::GetMacAddress();
Packet arpPacket;
size_t arpSize = arpPacket.Deserialize(buffer, bufferSize);
if (arpSize == 0 || arpSize != arpPacket.SerializedLength())
const auto arpSize = arpPacket.Deserialize(buffer, bufferSize);
if (arpSize == 0)
{
DEBUG_LOG(
"Dropped ARP packet (invalid buffer size %u, expected %u)\r\n",

View file

@ -10,7 +10,6 @@
#include "debug.h"
#include "types.h"
#include <uspi.h>
#include <uspios.h>
namespace Net::Dhcp
{

View file

@ -34,21 +34,8 @@ namespace Net
postInitializeTime = read32(ARM_SYSTIMER_CLO) + 30000;
}
void Update()
void HandlePacket(const uint8_t* buffer, const size_t bufferSize)
{
if (postInitializeTime && read32(ARM_SYSTIMER_CLO) > postInitializeTime)
{
postInitialize();
postInitializeTime = 0;
}
unsigned int bufferSize = 0;
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
if (!USPiReceiveFrame(buffer, &bufferSize))
{
return;
}
Ethernet::Header ethernetHeader;
auto headerSize = Ethernet::Header::Deserialize(ethernetHeader, buffer, bufferSize);
if (headerSize == 0 || headerSize != Ethernet::Header::SerializedLength())
@ -71,6 +58,25 @@ namespace Net
}
}
void Update()
{
if (postInitializeTime && read32(ARM_SYSTIMER_CLO) > postInitializeTime)
{
postInitialize();
postInitializeTime = 0;
}
unsigned int bufferSize = 0;
uint8_t buffer[USPI_FRAME_BUFFER_SIZE];
if (!USPiReceiveFrame(buffer, &bufferSize))
{
return;
}
HandlePacket(buffer, sizeof(buffer));
}
static void postInitialize()
{
DEBUG_LOG("Running network post-init\r\n");

View file

@ -10,5 +10,6 @@
namespace Net
{
void Initialize(Options& options);
void HandlePacket(const uint8_t* buffer, const size_t bufferSize);
void Update();
} // namespace Net

View file

@ -9,19 +9,19 @@ void TestNetUtilsInternetChecksum()
{
{
const char buffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
const auto checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 61419);
}
{
const char buffer[] = {1, 2, 3, 4, 5, 6, 7};
uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
const auto checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 61427);
}
{
const char buffer[] = {};
uint16_t checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
const auto checksum = Utils::InternetChecksum(buffer, sizeof(buffer));
TEST_CHECK(checksum == 65535);
}
}
@ -30,20 +30,20 @@ void TestNetUtilsCrc32()
{
{
const uint8_t buffer[] = {1, 2, 3, 4, 5, 6, 7, 8};
uint32_t checksum = Utils::Crc32(buffer, sizeof(buffer));
const auto checksum = Utils::Crc32(buffer, sizeof(buffer));
TEST_CHECK(checksum == 1070237893);
}
{
const uint8_t buffer[] = {};
uint32_t checksum = Utils::Crc32(buffer, sizeof(buffer));
const auto checksum = Utils::Crc32(buffer, sizeof(buffer));
TEST_CHECK(checksum == 0);
}
}
void TestNetUtilsGetMacAddress()
{
auto mac = Utils::GetMacAddress();
auto expected = Utils::MacAddress{0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
const auto mac = Utils::GetMacAddress();
const auto expected = Utils::MacAddress{0x00, 0x10, 0x20, 0x30, 0x40, 0x50};
TEST_CHECK(mac == expected);
}