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(); const auto macAddress = Utils::GetMacAddress();
Packet arpPacket; Packet arpPacket;
size_t arpSize = arpPacket.Deserialize(buffer, bufferSize); const auto arpSize = arpPacket.Deserialize(buffer, bufferSize);
if (arpSize == 0 || arpSize != arpPacket.SerializedLength()) if (arpSize == 0)
{ {
DEBUG_LOG( DEBUG_LOG(
"Dropped ARP packet (invalid buffer size %u, expected %u)\r\n", "Dropped ARP packet (invalid buffer size %u, expected %u)\r\n",

View file

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

View file

@ -34,21 +34,8 @@ namespace Net
postInitializeTime = read32(ARM_SYSTIMER_CLO) + 30000; 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; Ethernet::Header ethernetHeader;
auto headerSize = Ethernet::Header::Deserialize(ethernetHeader, buffer, bufferSize); auto headerSize = Ethernet::Header::Deserialize(ethernetHeader, buffer, bufferSize);
if (headerSize == 0 || headerSize != Ethernet::Header::SerializedLength()) 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() static void postInitialize()
{ {
DEBUG_LOG("Running network post-init\r\n"); DEBUG_LOG("Running network post-init\r\n");

View file

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

View file

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