Commit 115754ef authored by Martin Vítek's avatar Martin Vítek

Fixed NetworkTypes, clean-up in main.cpp

parent 6328bcdf
......@@ -63,7 +63,7 @@ void ARP_protocol::assembly_request(const IP& ip, ethernet_payload& payload)
arp->operation_code = __htons(ARP_REQUEST);
//My MAC
memcpy(arp->MAC_sender, local_mac.mac, 6);
memcpy(arp->MAC_sender, local_mac, 6);
//My IP (it can be 0.0.0.0 if IP address is not assigned yet)
memcpy(arp->IP_sender, local_ip, 4);
......@@ -80,7 +80,7 @@ void ARP_protocol::assembly_reply(const IP& ip, ethernet_payload& payload)
//TODO: implementation
}
bool ARP_protocol::decode(ethernet_payload& payload)
bool ARP_protocol::decode(const ethernet_payload& payload)
{
arp_packet* arp = (arp_packet*)payload.data;
......
......@@ -77,5 +77,5 @@ class ARP_protocol
void assembly_request(const IP& ip, ethernet_payload& payload);
void assembly_reply(const IP& ip, ethernet_payload& payload);
bool decode(ethernet_payload& payload);
bool decode(const ethernet_payload& payload);
};
......@@ -142,6 +142,11 @@ namespace ENC28J60_namespace
if (reenable) bit_field_set_in_register(ECON1, RXEN_bm);
}
bool ENC28J60::check_link()
{
return (read_PHY_register(PHSTAT2) & LSTAT_bm);
}
void ENC28J60::tx_frame_blocking(const ethernet_frame &frame)
{
const uint8_t control_byte = 0; //Use settings from MACON3
......
......@@ -91,6 +91,7 @@ namespace ENC28J60_namespace
//Initialization
void init();
void set_eth_rx_buffer_size(uint16_t size);
bool check_link();
//RX & TX
void tx_frame_blocking(const ethernet_frame &frame);
......
......@@ -7,156 +7,181 @@
class MAC
{
public:
uint8_t mac[6];
uint8_t mac[6];
public:
MAC()
{
memset(mac, 0, sizeof(mac));
}
MAC(const 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* mac)
{
memcpy(this->mac, mac, 6);
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)));
}
MAC()
{
memset(mac, 0, sizeof(mac));
}
MAC(const uint8_t (&mac)[6])
{
memcpy(this->mac, mac, sizeof(this->mac));
}
MAC(const uint8_t value)
{
memset(mac, value, sizeof(mac));
}
uint8_t (&operator=(const uint8_t (&mac)[6]))[6]
{
memcpy(this->mac, mac, sizeof(this->mac));
bool operator!=(const MAC& mac)
{
return memcmp(this->mac, mac.mac, sizeof(mac));
}
return this->mac;
}
uint8_t* operator=(const uint8_t* mac)
{
memcpy(this->mac, mac, 6);
void set(const uint8_t (&mac)[6])
{
memcpy(this->mac, mac, sizeof(mac));
}
return this->mac;
}
uint8_t operator[](const uint8_t index)
{
if (index >= sizeof(mac)) return 0;
void get(uint8_t (&mac)[6])
{
memcpy(mac, this->mac, sizeof(mac));
}
return mac[index];
}
operator uint8_t*()
{
return mac;
}
operator const uint8_t*() const
{
return mac;
}
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:
union
{
uint32_t value;
uint8_t array[4];
}ip;
union
{
uint32_t value;
uint8_t array[4];
}ip;
public:
IP()
{
this->ip.value = 0;
}
IP(const uint32_t ip)
{
this->ip.value = ip;
}
IP(const uint8_t (&ip)[4])
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++)
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
this->ip.array[i] = ip[i];
}
this->ip.array[i] = ip[i];
}
}
uint32_t operator=(const uint32_t ip)
{
this->ip.value = ip;
uint32_t operator=(const uint32_t ip)
return this->ip.value;
}
uint8_t (&operator=(const uint8_t (&ip)[4]))[4]
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
this->ip.value = ip;
return this->ip.value;
this->ip.array[i] = ip[i];
}
uint8_t (&operator=(const uint8_t (&ip)[4]))[4]
return this->ip.array;
}
const uint8_t* operator=(const uint8_t* ip)
{
for (uint8_t i=0; i<4; i++)
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
this->ip.array[i] = ip[i];
}
return this->ip.array;
this->ip.array[i] = ip[i];
}
const uint8_t* operator=(const uint8_t* ip)
{
for (uint8_t i=0; i<4; i++)
{
this->ip.array[i] = ip[i];
}
return this->ip.array;
}
return this->ip.array;
}
uint8_t operator[](const uint8_t index)
{
if (index >= sizeof(ip)) return 0;
return ip.array[index];
}
uint8_t operator[](const uint8_t index)
{
if (index >= sizeof(ip)) return 0;
bool operator==(const IP& ip)
{
return this->ip.value == ip.ip.value;
}
bool operator!=(const IP& ip)
{
return this->ip.value != ip.ip.value;
}
void set(const uint32_t ip)
{
this->ip.value = ip;
}
void set(const uint8_t (&ip)[4])
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
this->ip.array[i] = ip[i];
}
}
uint32_t get()
return ip.array[index];
}
bool operator==(const IP& ip)
{
return this->ip.value == ip.ip.value;
}
bool operator!=(const IP& ip)
{
return this->ip.value != ip.ip.value;
}
operator uint8_t*()
{
return ip.array;
}
operator const uint8_t*() const
{
return ip.array;
}
void set(const uint32_t ip)
{
this->ip.value = ip;
}
void set(const uint8_t (&ip)[4])
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
return ip.value;
this->ip.array[i] = ip[i];
}
void get(uint8_t (&ip)[4])
}
uint32_t get()
{
return ip.value;
}
void get(uint8_t (&ip)[4])
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
for (uint8_t i=0; i<sizeof(ip); i++)
{
ip[i] = this->ip.array[i];
}
ip[i] = this->ip.array[i];
}
}
};
#include "NixieStack.h"
NixieStack::NixieStack()
NixieStack::NixieStack(): mac(0),
ip(0),
arp(mac, ip)
{
}
void NixieStack::set_MAC(const uint8_t (&mac)[6])
{
this->mac.set(mac);
}
void NixieStack::set_IP(const uint8_t (&ip)[4])
{
this->ip.set(ip);
}
......@@ -75,55 +75,12 @@ int main(void)
uart.send("Martin Vitek - 2017\n\r\n\r");
eth.print_device_info();
//nixie.print_eeprom();
nixie.get_mac();
uint8_t i = 0;
time_t old = 0;
bool up = true;
MAC my_mac({0x54, 0x10, 0xEC, 0x20, 0xE5, 0xBD});
MAC pc_mac({0x00, 0x30, 0x4f, 0x08, 0x35, 0x9c});
IP my_ip({192, 168, 1, 1});
ethernet_frame arp_frame;
ARP_protocol arp(my_mac, my_ip);
IP ip, ip2;
ip = {192, 168, 1, 1};
uint32_t adfasdf;
ip2 = ip;
for (uint8_t i=0; i<4; i++)
{
uart.send_dec(ip.ip.array[i]);
uart.send('.');
}
uart.send_new_line();
arp.assembly_request(MAC({10, 20, 70, 113}), arp_frame.payload);
memcpy(arp_frame.dest_address, broadcast_mac, 6);
memcpy(arp_frame.src_address, my_mac, 6);
arp_frame.type_length = __htons(ARP);
//debug.frame(arp_frame);
sys.delay_ms(1000);
sys.delay_ms(1000);
sys.delay_ms(1000);
sys.delay_ms(1000);
sys.delay_ms(1000);
sys.delay_ms(1000);
eth.tx_frame_blocking(arp_frame);
//eth.tx_frame_blocking(packet);
while (true)
......@@ -183,12 +140,13 @@ int main(void)
//eth.print_connection_status();
//eth.tx_frame_blocking(packet);
/*
if (eth.rx_frame_blocking(arp_frame))
{
//debug.frame(arp_frame);
arp.decode(arp_frame.payload);
}
*/
/*
......@@ -226,11 +184,6 @@ int main(void)
uart.send_new_line();
*/
//clock.display.set(i++);
//if (i>99) i=0;
//nixie.display.set(static_cast<uint8_t>(adc.value[adc.TEMPERATURE_AMBIENT]/100));
}
}
}
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