From 4641ee49b556619aa7a6618c42d4a3c126ad5f33 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 10 Mar 2019 19:15:22 +0100 Subject: [PATCH] Kernel: Add a simple MACAddress class. --- Kernel/E1000NetworkAdapter.cpp | 2 +- Kernel/MACAddress.h | 27 +++++++++++++++++++++++++++ Kernel/NetworkAdapter.cpp | 4 ---- Kernel/NetworkAdapter.h | 7 ++++--- 4 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 Kernel/MACAddress.h diff --git a/Kernel/E1000NetworkAdapter.cpp b/Kernel/E1000NetworkAdapter.cpp index 2fa645a05d..fe5e8736a6 100644 --- a/Kernel/E1000NetworkAdapter.cpp +++ b/Kernel/E1000NetworkAdapter.cpp @@ -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(); } diff --git a/Kernel/MACAddress.h b/Kernel/MACAddress.h new file mode 100644 index 0000000000..e9b8dd41a2 --- /dev/null +++ b/Kernel/MACAddress.h @@ -0,0 +1,27 @@ +#pragma once + +#include +#include +#include + +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 }; +}; diff --git a/Kernel/NetworkAdapter.cpp b/Kernel/NetworkAdapter.cpp index 242d31f3cf..ffbeabc0a8 100644 --- a/Kernel/NetworkAdapter.cpp +++ b/Kernel/NetworkAdapter.cpp @@ -10,7 +10,3 @@ NetworkAdapter::~NetworkAdapter() { } -void NetworkAdapter::set_mac_address(const byte* mac_address) -{ - memcpy(m_mac_address, mac_address, 6); -} diff --git a/Kernel/NetworkAdapter.h b/Kernel/NetworkAdapter.h index 61abda81de..7d0e11acc4 100644 --- a/Kernel/NetworkAdapter.h +++ b/Kernel/NetworkAdapter.h @@ -1,18 +1,19 @@ #pragma once #include +#include 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; };