void client_process ( void )
{
uint16_t plen;
uint8_t i;
if (client_data_ready == 0) return; // nothing to send
if(client_state == IDLE){ // initialize ARP
es.ES_make_arp_request(buf, dest_ip);
client_state = ARP_SENT;
return;
}
if(client_state == ARP_SENT){
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
// destination ip address was found on network
if ( plen!=0 )
{
if ( es.ES_arp_packet_is_myreply_arp ( buf ) ){
client_state = ARP_REPLY;
syn_ack_timeout=0;
return;
}
}
delay(10);
syn_ack_timeout++;
if(syn_ack_timeout== 100) { //timeout, server ip not found
client_state = IDLE;
client_data_ready =0;
syn_ack_timeout=0;
return;
}
}
// send SYN packet to initial connection
if(client_state == ARP_REPLY){
// save dest mac
for(i=0; i<6; i++){
dest_mac[i] = buf[ETH_SRC_MAC+i];
}
es.ES_tcp_client_send_packet (
buf,
80,
1200,
TCP_FLAG_SYN_V, // flag
1, // (bool)maximum segment size
1, // (bool)clear sequence ack number
0, // 0=use old seq, seqack : 1=new seq,seqack no data : new seq,seqack with data
0, // tcp data length
dest_mac,
dest_ip
);
client_state = SYNC_SENT;
}
// get new packet
if(client_state == SYNC_SENT){
plen = es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf);
// no new packet incoming
if ( plen == 0 )
{
return;
}
// check ip packet send to avr or not?
// accept ip packet only
if ( es.ES_eth_type_is_ip_and_my_ip(buf,plen)==0){
return;
}
// check SYNACK flag, after AVR send SYN server response by send SYNACK to AVR
if ( buf [ TCP_FLAGS_P ] == ( TCP_FLAG_SYN_V | TCP_FLAG_ACK_V ) )
{
// send ACK to answer SYNACK
es.ES_tcp_client_send_packet (
buf,
80,
1200,
TCP_FLAG_ACK_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
1, // 0=use old seq, seqack : 1=new seq,seqack no data : new seq,seqack with data
0, // tcp data length
dest_mac,
dest_ip
);
// setup http request to server
plen = gen_client_request( buf );
// send http request packet
// send packet with PSHACK
es.ES_tcp_client_send_packet (
buf,
80, // destination port
1200, // source port
TCP_FLAG_ACK_V | TCP_FLAG_PUSH_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
0, // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
plen, // tcp data length
dest_mac,
dest_ip
);
return;
}
// after AVR send http request to server, server response by send data with PSHACK to AVR
// AVR answer by send ACK and FINACK to server
if ( buf [ TCP_FLAGS_P ] == (TCP_FLAG_ACK_V|TCP_FLAG_PUSH_V) )
{
plen = es.ES_tcp_get_dlength( (uint8_t*)&buf );
// send ACK to answer PSHACK from server
es.ES_tcp_client_send_packet (
buf,
80, // destination port
1200, // source port
TCP_FLAG_ACK_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
plen, // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
0, // tcp data length
dest_mac,
dest_ip
);;
// send finack to disconnect from web server
es.ES_tcp_client_send_packet (
buf,
80, // destination port
1200, // source port
TCP_FLAG_FIN_V|TCP_FLAG_ACK_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
0, // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
0,
dest_mac,
dest_ip
);
return;
}
// answer FINACK from web server by send ACK to web server
if ( buf [ TCP_FLAGS_P ] == (TCP_FLAG_ACK_V|TCP_FLAG_FIN_V) )
{
// send ACK with seqack = 1
es.ES_tcp_client_send_packet(
buf,
80, // destination port
1200, // source port
TCP_FLAG_ACK_V, // flag
0, // (bool)maximum segment size
0, // (bool)clear sequence ack number
1, // 0=use old seq, seqack : 1=new seq,seqack no data : >1 new seq,seqack with data
0,
dest_mac,
dest_ip
);
client_state = IDLE; // return to IDLE state
client_data_ready =0; // client data sent
}
}
}