/* ETH vrstva komunikacniho modulu pro Ethernet */

#include "stdint.h"
#include "eth.h"
#include "net.h"

extern unsigned char my_MAC[MAC_ADR_SIZE];					// MAC adresa HTTP serveru
/* --------------------------------------------------------------
 Nastaveni MAC adres prijemce a odesilatele paketu v ethernetove
 hlavicce a zvoli typ protokolu prenaseneho obsahu (IP nebo ARP)
 ------------------------------------------------------------- */
void ETH_Set_ETH_Header(uint8_t *buffer, uint8_t *DestMAC, uint8_t *SourceMAC, uint16_t type)
{
	unsigned char i;
	for(i=0;i<MAC_ADR_SIZE;i++)
	{
		buffer[OFFSET_ETH_DestMAC+i] = DestMAC[i];		// Nastaveni MAC adresy prijemce paketu
		buffer[OFFSET_ETH_SourceMAC+i] = SourceMAC[i];	// Nastaveni MAC adresy odesilatele paketu
	}
	buffer[OFFSET_ETH_TypeLen+0] = (0xFF00&type)>>8;	// Horni byte typu obsahu paketu (IPv4/ARP)
	buffer[OFFSET_ETH_TypeLen+1] = (0x00FF&type);		// Dolni byte typu obsahu paketu
}

/* --------------------------------------------------------------
 Zkopiruje ETH hlavicku prijateho paketu do odesilaneho
 a prohodi pritom zdrojovou a cilovou ETH adresu
 ------------------------------------------------------------- */
void ETH_CopyETH_Header(uint8_t *TX_buffer, uint8_t *RX_buffer, uint16_t type)
{
	uint8_t i;
	for(i=0;i<MAC_ADR_SIZE;i++)
	{
		TX_buffer[OFFSET_ETH_DestMAC+i] = RX_buffer[OFFSET_ETH_SourceMAC+i];	// Nastaveni MAC adresu prijemce paketu
		TX_buffer[OFFSET_ETH_SourceMAC+i] = my_MAC[i];							// Nastaveni MAC adresu odesilatele paketu
	}
	TX_buffer[OFFSET_ETH_TypeLen+0] = (0xFF00&type)>>8;							// Horni byte typu obsahu paketu (IPv4/ARP)
	TX_buffer[OFFSET_ETH_TypeLen+1] = (0x00FF&type);							// Dolni byte typu obsahu paketu	
}

/* ------------------------------------------------------------------
 Vrati ukazatel na ETH adresu zdroje z predaneho bufferu (RX) 
 ----------------------------------------------------------------- */
uint8_t *ETH_Get_SourceMAC(uint8_t *buffer)
{
	static uint8_t SourceMAC[MAC_ADR_SIZE];
	uint8_t i;
	
	for(i=0;i<MAC_ADR_SIZE;i++)
	{
		SourceMAC[i] = buffer[OFFSET_ETH_SourceMAC+i];
	}
	return(SourceMAC);
}

/* --------------------------------------------------------------
 Odesle paket do site
 ------------------------------------------------------------- */
//void ETH_SendPacket(uint16_t PaketLength, uint8_t *buffer)
//{
//	enc28j60PacketSend(PaketLength,buffer);										// Odeslani		
//}

/* Konec souboru */
