Commit 1ebee1a3 authored by Martin Vítek's avatar Martin Vítek

Add IP type, improvements in MAC type

parent df8898da
#pragma once
#include <cstdint>
#include <cstring>
class MAC
{
private:
uint8_t mac[6];
public:
MAC()
{
memset(mac, 0, sizeof(mac));
}
MAC(uint8_t (&mac)[6])
{
memcpy(this->mac, mac, sizeof(this->mac));
}
uint8_t (&operator=(const uint8_t (&mac)[6]))[6]
{
memcpy(this->mac, mac, sizeof(this->mac));
return this->mac;
}
uint8_t operator[](const uint8_t index)
{
if (index >= sizeof(mac)) return 0;
return mac[index];
}
bool operator==(const MAC& mac)
{
return !(memcmp(this->mac, mac.mac, sizeof(mac)));
}
bool operator!=(const MAC& mac)
{
return memcmp(this->mac, mac.mac, sizeof(mac));
}
void set(const uint8_t (&mac)[6])
{
memcpy(this->mac, mac, sizeof(mac));
}
void get(uint8_t (&mac)[6])
{
memcpy(mac, this->mac, sizeof(mac));
}
};
class IP
{
public:
private:
union
{
uint32_t value;
......@@ -25,7 +67,20 @@ class IP
public:
IP()
{
this->ip.value = 0;
}
IP(const uint32_t ip)
{
this->ip.value = ip;
}
IP(const uint8_t (&ip)[4])
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
this->ip.array[i] = ip[i];
}
}
uint32_t operator=(const uint32_t ip)
......@@ -35,14 +90,14 @@ class IP
return this->ip.value;
}
uint32_t operator=(const uint8_t (&ip)[4])
uint8_t (&operator=(const uint8_t (&ip)[4]))[4]
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
this->ip.array[i] = ip[i];
}
return this->ip.value;
return this->ip.array;
}
uint8_t operator[](const uint8_t index)
......
......@@ -89,11 +89,11 @@ int main(void)
ethernet_frame arp_frame;
ARP_class arp(my_mac);
IP ip;
IP ip, ip2;
ip = {192, 168, 1, 1};
uint32_t adfasdf;
adfasdf = ip;
ip2 = ip;
for (uint8_t i=0; i<4; i++)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment