Kernel: Add a simple MACAddress class.

This commit is contained in:
Andreas Kling 2019-03-10 19:15:22 +01:00
parent 405413c354
commit 4641ee49b5
4 changed files with 32 additions and 8 deletions

View file

@ -57,7 +57,7 @@ E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address pci_address, byte irq)
detect_eeprom();
kprintf("E1000: Has EEPROM? %u\n", m_has_eeprom);
read_mac_address();
auto* mac = mac_address();
const auto& mac = mac_address();
kprintf("E1000: MAC address: %b:%b:%b:%b:%b:%b\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
enable_irq();
}

27
Kernel/MACAddress.h Normal file
View file

@ -0,0 +1,27 @@
#pragma once
#include <AK/Assertions.h>
#include <AK/Types.h>
#include <Kernel/StdLib.h>
class MACAddress {
public:
MACAddress() { }
MACAddress(const byte data[6])
: m_valid(true)
{
memcpy(m_data, data, 6);
}
~MACAddress() { }
bool is_valid() const { return m_valid; }
byte operator[](int i) const
{
ASSERT(i >= 0 && i < 6);
return m_data[i];
}
private:
byte m_data[6];
bool m_valid { false };
};

View file

@ -10,7 +10,3 @@ NetworkAdapter::~NetworkAdapter()
{
}
void NetworkAdapter::set_mac_address(const byte* mac_address)
{
memcpy(m_mac_address, mac_address, 6);
}

View file

@ -1,18 +1,19 @@
#pragma once
#include <AK/Types.h>
#include <Kernel/MACAddress.h>
class NetworkAdapter {
public:
virtual ~NetworkAdapter();
virtual const char* class_name() const = 0;
const byte* mac_address() { return m_mac_address; }
MACAddress mac_address() { return m_mac_address; }
protected:
NetworkAdapter();
void set_mac_address(const byte*);
void set_mac_address(const MACAddress& mac_address) { m_mac_address = mac_address; }
private:
byte m_mac_address[6];
MACAddress m_mac_address;
};