
// 通过 SPI 连接W5500来实现以太网连接功能

#include "config.h"

#ifndef __ETHERNET_H__
#define	__ETHERNET_H__


#include "socket.h"

//-------------------------------------------------------------------

typedef struct
{
	int type;
	int status;
	u8 socket;
	u32 localIp;
	u16 localPort;

	u32 remoteIp;
	u16 remotePort;

	int newconnect;

}SocketCtrl;


#define STA_INIT		0
#define STA_CLOSED		1
#define STA_CONNECT		2
#define STA_READY		3

#define	FLAG_ENABLE			0x55AA

#define	MAX_TRANS_DATA		1500

//-------------------------------------------------------------------

// 默认IP地址
#ifndef DEF_LOCAL_IP1
#define	DEF_LOCAL_IP1		192
#endif
#ifndef DEF_LOCAL_IP2
#define	DEF_LOCAL_IP2		168
#endif
#ifndef DEF_LOCAL_IP3
#define	DEF_LOCAL_IP3		16
#endif
#ifndef DEF_LOCAL_IP4
#define	DEF_LOCAL_IP4		253
#endif

#ifndef DEF_GATEWAY_IP1
#define	DEF_GATEWAY_IP1		192
#endif
#ifndef DEF_GATEWAY_IP2
#define	DEF_GATEWAY_IP2		168
#endif
#ifndef DEF_GATEWAY_IP3
#define	DEF_GATEWAY_IP3		16
#endif
#ifndef DEF_GATEWAY_IP4
#define	DEF_GATEWAY_IP4		1
#endif

#ifndef DEF_DNS_IP1
#define	DEF_DNS_IP1			192
#endif
#ifndef DEF_DNS_IP2
#define	DEF_DNS_IP2			168
#endif
#ifndef DEF_DNS_IP3
#define	DEF_DNS_IP3			16
#endif
#ifndef DEF_DNS_IP4
#define	DEF_DNS_IP4			1
#endif

#ifndef DEF_REMOTE_IP1
#define	DEF_REMOTE_IP1		192
#endif
#ifndef DEF_REMOTE_IP2
#define	DEF_REMOTE_IP2		168
#endif
#ifndef DEF_REMOTE_IP3
#define	DEF_REMOTE_IP3		16
#endif
#ifndef DEF_REMOTE_IP4
#define	DEF_REMOTE_IP4		41
#endif

//-------------------------------------------------------------------

// UDP 通讯
#ifndef UDP_NUM
#define UDP_NUM				0
#endif

#define	UDP_SN_BEG			0
#define	UDP_LOCAL_PORT_BEG	5100
#define	UDP_LOCAL_IP		MAKEIP(DEF_LOCAL_IP1,DEF_LOCAL_IP2,DEF_LOCAL_IP3,DEF_LOCAL_IP4)

#define	UDP_REMOTE_PORT		5101
#define	UDP_REMOTE_IP		MAKEIP(DEF_REMOTE_IP1,DEF_REMOTE_IP2,DEF_REMOTE_IP3,DEF_REMOTE_IP4)

//-------------------------------------------------------------------

#define UDP_COMM	0
#define	TCP_SERVER	1
#define	TCP_CLIENT	2

//-------------------------------------------------------------------

#define	LOCAL_IP	MAKEIP(DEF_LOCAL_IP1,DEF_LOCAL_IP2,DEF_LOCAL_IP3,DEF_LOCAL_IP4)
#define	NET_MASK	MAKEIP(255,255,255,0)
#define	GATE_WAY	MAKEIP(DEF_GATEWAY_IP1,DEF_GATEWAY_IP2,DEF_GATEWAY_IP3,DEF_GATEWAY_IP4)
#define	DNS_IP		MAKEIP(DEF_DNS_IP1,DEF_DNS_IP2,DEF_DNS_IP3,DEF_DNS_IP4)

#define	TCP_REMOTE_IP	MAKEIP(DEF_REMOTE_IP1,DEF_REMOTE_IP2,DEF_REMOTE_IP3,DEF_REMOTE_IP4)

//-------------------------------------------------------------------

// TCP server
#ifndef SERVER_NUM
#define	SERVER_NUM					0			// 服务器个数
#endif

#define	TCP_SERVER_SN_BEG		(UDP_SN_BEG+UDP_NUM)			// TCP 服务器起始 Socket 号, 支持 1--7; 并且 SERVER_NUM+TCP_SERVER_SN_BEG 小于 7
#define	TCP_SERVER_PORT_BEG		5000		// 服务器端口号
#define	TCP_SERVER_LOCAL_IP_BEG	LOCAL_IP
#define	TCP_SERVER_LOCAL_IP_ADD	((u32)1<<24)

//-------------------------------------------------------------------

// TCP client
#ifndef CLIENT_NUM
#define	CLIENT_NUM					0		// 客户端个数
#endif

#define	TCP_CLIENT_SN_BEG			(TCP_SERVER_SN_BEG+SERVER_NUM)	// TCP 客户端起始 Socket 号, 支持 1--7; 并且 SERVER_NUM+TCP_SERVER_SN_BEG 小于 7
#define	TCP_CLIENT_LOCAL_PORT_BEG	(TCP_SERVER_PORT_BEG+8)			// 本地 port
#define	TCP_CLIENT_LOCAL_IP			LOCAL_IP

#define	TCP_CLIENT_REMOTE_IP_BEG	(TCP_REMOTE_IP+(TCP_CLIENT_REMOTE_IP_ADD*SERVER_NUM))
#define	TCP_CLIENT_REMOTE_PORT_BEG	502

#define	TCP_CLIENT_REMOTE_IP_ADD	((u32)1<<24)

//-------------------------------------------------------------------


//---------------------------------------------------------------------------
// 网络数据

#ifdef _IN_ETHERNET_C

wiz_NetInfo		g_netInfo;
wiz_PhyConf		g_phyConfig;
wiz_NetTimeout	g_netTimeout;

u32 g_localIP;
u32 g_netMask;
u32 g_gateWay;
u32 g_dnsIP;


#else

extern wiz_NetInfo		g_netInfo;
extern wiz_PhyConf		g_phyConfig;
extern wiz_NetTimeout	g_netTimeout;


extern u32 g_localIP;
extern u32 g_netMask;
extern u32 g_gateWay;
extern u32 g_dnsIP;

#endif

#define	LINK_INIT		0
#define	LINK_UP			1
#define	LINK_WAIT		2
#define	LINK_DOWN		3

//-------------------------------------------------------------------

// 初始化
void InitEthernet(void);

void InitNetConfig(void);

// 中断处理
void EthernetIntProc(void);

// 以太网服务运行
void EthernetServiceRun(void);

void GetChipConfig(void);

// Ethernet 发送数据
int EthernetSendData(SocketCtrl * pCtrl, u8 * pDatBuf, int len);

// Ethernet 接收数据
int EthernetGetData(SocketCtrl * pCtrl, u8 * pDat, int expectLen);

// Ethernet 已接收数据长度
int EthernetGetRsLen(SocketCtrl * pCtrl);

// 得到发送缓冲区空闲长度
int EthernetGetSdFreeLen(SocketCtrl * pCtrl);

// 设置网络地址
void SetNetAddr(u32 ip, u32 mask, u32 gateway, u32 dns);

SocketCtrl * GetCtrlFromSocketIdx(int sIdx);

//-------------------------------------------------------------------

#define	MAKEIP(ip1,ip2,ip3,ip4)		((u32)( (((u32)(ip4&0xff))<<24) + (((u32)(ip3&0xff))<<16) + (((u32)(ip2&0xff))<<8) + (((u32)(ip1&0xff))<<0)))

#define	GETIP1(ip)	((u8)(((u32)(ip)) >> 0) & 0xFF)
#define	GETIP2(ip)	((u8)(((u32)(ip)) >> 8) & 0xFF)
#define	GETIP3(ip)	((u8)(((u32)(ip)) >> 16) & 0xFF)
#define	GETIP4(ip)	((u8)(((u32)(ip)) >> 24) & 0xFF)

//-------------------------------------------------------------------

#endif

