📄 rtl8019as_tcp_m128.c
字号:
#include "Rtl8019_M16_TCP.h"
//******************************************************************
//* Application Code
//* Your application code goes here.
//* This particular code toggles the LED on PORT A bit 4 using
//* Telnet.
//******************************************************************
void application_code() //--zzz
{
int i,j;
++cntr;
if(aux_data[0] != 0x0A)
tcpdatalen_out = tcpdatalen_in;
if(aux_data[0] == 0x0A)
{
tcpdatalen_out = 0x00;
clr_hex;
}
if(hexflag)
{
if(aux_data[0] >= '0' && aux_data[0] <= '9')
aux_data[0] -= 0x30;
else if(aux_data[0] >= 'A' && aux_data[0] <= 'F')
aux_data[0] -= 0x37;
else if(aux_data[0] >= 'a' && aux_data[0] <= 'f')
aux_data[0] -= 0x67;
else
{
cntr = 0x00;
clr_hex;
}
if(cntr == 1)
byteout = aux_data[0] << 4;
if(cntr == 2)
{
byteout |= aux_data[0] & 0x0F;
DDRA = 0xFF; //tocreg;
PORTA = byteout; //cregdata = byteout;
latchdata;
clr_hex;
printf("Byte Latched = %x\r\n",byteout); //--zzz
}
}
if(aux_data[0] == '*')
{
set_hex;
cntr=0;
}
if (aux_data[0] == 0x0D)
{
j = sizeof(telnet_banner);
for(i=0;i<j;++i)
packet[TCP_data+i] = telnet_banner[i];
tcpdatalen_out = j;
}
}
//******************************************************************
//* TCP Function
//* This function uses TCP protocol to act as a Telnet server on
//* port 8088 decimal. The application function is called with
//* every incoming character.
//******************************************************************
void tcp()
{
int i,j;
//assemble the destination port address from the incoming packet
portaddr = make16(packet[TCP_destport],packet[TCP_destport+1]);
//calculate the length of the data coming in with the packet
//tcpdatalen_in = incoming packet length - incoming ip header length -
//incoming tcp header length
tcpdatalen_in = (make16(packet[ip_pktlen],packet[ip_pktlen+1]))- \
((packet[ip_vers_len] & 0x0F)* 4)-(((packet[TCP_hdrflags] & 0xF0) >> 4) * 4);
//If an ACK is received and the destination port address is valid
//and no data is in the packet
if(ACK_IN && portaddr == MY_PORT_ADDRESS && tcpdatalen_in == 0x00)
{
//assemble the acknowledgment number from the incoming packet
incoming_ack =make32(packet[TCP_acknum],packet[TCP_acknum+1], \
packet[TCP_acknum+2],packet[TCP_acknum+3]);
//if the incoming packet is a result of session establishment
if(synflag_bit)
{
//clear the SYN flag
clr_synflag;
//the incoming acknowledgment is my new sequence number
my_seqnum = incoming_ack;
//send the Telnet server banner
//limit the character count to 40 decimal
j = sizeof(telnet_banner);
for(i=0;i<j;++i)
packet[TCP_data+i] = telnet_banner[i];
//length of the banner message
tcpdatalen_out = j;
//send the Telnet server banner
//limit the character count to 40 decimal
//strcpy(&packet[TCP_data],"EDTP AVR Telnet SERVER>");
//length of the banner message
//tcpdatalen_out = 23;
//expect to get an acknowledgment of the banner message
expected_ack = my_seqnum +tcpdatalen_out;
//send the TCP/IP packet
send_tcp_packet();
}
}
//if an ack is received and the port address is valid and there is data
//in the incoming packet
if((ACK_IN) && portaddr == MY_PORT_ADDRESS && tcpdatalen_in)
{
for(i=0;i<tcpdatalen_in;++i)
//receive the data and put it into the incoming data buffer
aux_data[i] = packet[TCP_data+i];
application_code();
//assemble the acknowledgment number from the incoming packet
incoming_ack =make32(packet[TCP_acknum],packet[TCP_acknum+1], \
packet[TCP_acknum+2],packet[TCP_acknum+3]);
//check for the number of bytes acknowledged
//determine how many bytes are outstanding and adjust the outgoing
//sequence number accordingly
if(incoming_ack <= expected_ack)
my_seqnum = expected_ack - (expected_ack - incoming_ack);
//my expected acknowledgement number
expected_ack = my_seqnum +tcpdatalen_out;
send_tcp_packet();
}
//this code segment processes the incoming SYN from the Telnet client
//and sends back the initial sequence number (ISN) and acknowledges
//the incoming SYN packet
if(SYN_IN && portaddr == MY_PORT_ADDRESS)
{
tcpdatalen_in = 0x01;
set_synflag;
setipaddrs();
data_L = packet[TCP_srcport];
packet[TCP_srcport] = packet[TCP_destport];
packet[TCP_destport] = data_L;
data_L = packet[TCP_srcport+1];
packet[TCP_srcport+1] = packet[TCP_destport+1];
packet[TCP_destport+1] = data_L;
assemble_ack();
if(++ISN == 0x0000 || ++ISN == 0xFFFF)
my_seqnum = 0x1234FFFF;
set_packet32(TCP_seqnum,my_seqnum);
packet[TCP_hdrflags+1] = 0x00;
SYN_OUT;
ACK_OUT;
packet[TCP_cksum] = 0x00;
packet[TCP_cksum+1] = 0x00;
hdr_chksum =0;
hdrlen = 0x08;
addr = &packet[ip_srcaddr];
cksum();
hdr_chksum = hdr_chksum + packet[ip_proto];
tcplen = make16(packet[ip_pktlen],packet[ip_pktlen+1]) - \
((packet[ip_vers_len] & 0x0F) * 4);
hdr_chksum = hdr_chksum + tcplen;
hdrlen = tcplen;
addr = &packet[TCP_srcport];
cksum();
chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
packet[TCP_cksum] = make8(chksum16,1);
packet[TCP_cksum+1] = make8(chksum16,0);
echo_packet();
}
//this code segment processes a FIN from the Telnet client
//and acknowledges the FIN and any incoming data.
if(FIN_IN && portaddr == MY_PORT_ADDRESS)
{
if(tcpdatalen_in)
{
for(i=0;i<tcpdatalen_in;++i)
{
aux_data[i] = packet[TCP_data+i];
application_code();
}
}
set_finflag;
++tcpdatalen_in;
incoming_ack =make32(packet[TCP_acknum],packet[TCP_acknum+1], \
packet[TCP_acknum+2],packet[TCP_acknum+3]);
if(incoming_ack <= expected_ack)
my_seqnum = expected_ack - (expected_ack - incoming_ack);
expected_ack = my_seqnum +tcpdatalen_out;
send_tcp_packet();
}
}
//******************************************************************
//* Assemble the Acknowledgment
//* This function assembles the acknowledgment to send to
//* to the client by adding the received data count to the
//* client's incoming sequence number.
//******************************************************************
void assemble_ack()
{
client_seqnum=make32(packet[TCP_seqnum],packet[TCP_seqnum+1], \
packet[TCP_seqnum+2],packet[TCP_seqnum+3]);
client_seqnum = client_seqnum + tcpdatalen_in;
set_packet32(TCP_acknum,client_seqnum);
}
//******************************************************************
//* Send TCP Packet
//* This routine assembles and sends a complete TCP/IP packet.
//* 40 bytes of IP and TCP header data is assumed.
//******************************************************************
void send_tcp_packet()
{
//count IP and TCP header bytes.. Total = 40 bytes
ip_packet_len = 40 + tcpdatalen_out;
packet[ip_pktlen] = make8(ip_packet_len,1);
packet[ip_pktlen+1] = make8(ip_packet_len,0);
setipaddrs();
data_L = packet[TCP_srcport];
packet[TCP_srcport] = packet[TCP_destport];
packet[TCP_destport] = data_L;
data_L = packet[TCP_srcport+1];
packet[TCP_srcport+1] = packet[TCP_destport+1];
packet[TCP_destport+1] = data_L;
assemble_ack();
set_packet32(TCP_seqnum,my_seqnum);
packet[TCP_hdrflags+1] = 0x00;
ACK_OUT;
if(flags & finflag)
{
FIN_OUT;
clr_finflag;
}
packet[TCP_cksum] = 0x00;
packet[TCP_cksum+1] = 0x00;
hdr_chksum =0;
hdrlen = 0x08;
addr = &packet[ip_srcaddr];
cksum();
hdr_chksum = hdr_chksum + packet[ip_proto];
tcplen = ip_packet_len - ((packet[ip_vers_len] & 0x0F) * 4);
hdr_chksum = hdr_chksum + tcplen;
hdrlen = tcplen;
addr = &packet[TCP_srcport];
cksum();
chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
packet[TCP_cksum] = make8(chksum16,1);
packet[TCP_cksum+1] = make8(chksum16,0);
txlen = ip_packet_len + 14;
if(txlen < 60)
txlen = 60;
data_L = make8(txlen,0);
data_H = make8(txlen,1);
write_rtl(CR,0x22);
write_rtl(TPSR,txstart);
write_rtl(RSAR0,0x00);
write_rtl(RSAR1,0x40);
write_rtl(ISR,0xFF);
write_rtl(RBCR0,data_L);
write_rtl(RBCR1,data_H);
write_rtl(CR,0x12);
for(i=0;i<txlen;++i)
write_rtl(RDMAPORT,packet[enetpacketDest0+i]);
byte_read = 0;
while(!(byte_read & RDC))
read_rtl(ISR);
write_rtl(TBCR0,data_L);
write_rtl(TBCR1,data_H);
write_rtl(CR,0x24);
}
//******************************************************************
//* Read/Write for show_regs
//* This routine reads a NIC register and dumps it out to the
//* serial port as ASCII.
//******************************************************************
void readwrite()
{
read_rtl(i);
bin2hex(byte_read);
printf("\t%c%c",high_char,low_char);
}
//******************************************************************
//* Displays Control Registers in Pages 1, 2 and 3
//* This routine dumps all of the NIC internal registers
//* to the serial port as ASCII characters.
//******************************************************************
void show_regs()
{
write_rtl(CR,0x21);
cls();
printf("\r\n");
printf(" Realtek 8019AS Register Dump\n\n\r");
printf("REG\tPage0\tPage1\tPage2\tPage3\n\r");
for(i=0;i<16;++i)
{
bin2hex((unsigned char) i);
printf("%c%c",high_char,low_char);
write_rtl(CR,0x21);
readwrite();
write_rtl(CR,0x61);
readwrite();
write_rtl(CR,0xA1);
readwrite();
write_rtl(CR,0xE1);
readwrite();
printf("\r\n");
}
}
//******************************************************************
//* Dump Receive Ring Buffer Header
//* This routine dumps the 4-byte receive buffer ring header
//* to the serial port as ASCII characters.
//******************************************************************
void dump_header()
{
for(i=0;i<4;++i)
{
bin2hex(pageheader[i]);
printf("\r\n%c%c",high_char,low_char);
}
}
//******************************************************************
//* Used with Tera Term to clear the screen (VT-100 command)
//******************************************************************
void cls(void)
{
printf("%c[2J",esc);
}
//******************************************************************
//* show_packet
//* This routine is for diagnostic purposes and displays
//* the Packet Buffer memory in the AVR.
//******************************************************************
void show_packet()
{
cls();
printf("\r\n");
data_L = 0x00;
for(i=0;i<96;++i)
{
bin2hex(packet[i]);
printf(" %c%c",high_char,low_char);
if(++data_L == 0x10)
{
data_L = 0x00;
printf("\r\n");
}
}
}
//******************************************************************
//* show_aux_packet
//* This routine is a diagnostic that displays Auxillary
//* Packet Buffer buffer memory in the AVR.
//******************************************************************
void show_aux_packet()
{
cls();
printf("\r\n");
data_L = 0x00;
for(i=0;i<80;++i)
{
bin2hex(aux_data[i]);
printf(" %c%c",high_char,low_char);
if(++data_L == 0x10)
{
data_L = 0x00;
printf("\r\n");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -