#include "net\stacktsk.h" #include "net\tick.h" #include "net\helpers.h" #include "net\tcp.h" #include "net\arp.h" #include "net\arptsk.h" //Create a TCP socket for receiving and sending data static TCP_SOCKET tcpSocketUser = INVALID_SOCKET; //TCP State machine #define SM_TCP_SEND_ARP 0 #define SM_TCP_WAIT_RESOLVE 1 #define SM_TCP_RESOLVED 2 #define SM_TCP_FINISHED 3 static BYTE smTcp = SM_TCP_SEND_ARP; //Timers TICK8 tsecWait = 0; //General purpose wait timer TICK16 tsecMsgSent = 0; //Time last message was sent void tcpExample(void) { BYTE c; NODE_INFO tcpServerNode; //Initialize remote IP and address with 10.1.0.101. The MAC address is //is not intialized yet, but after we receive an ARP responce. tcpServerNode.IPAddr.v[0] = 10; tcpServerNode.IPAddr.v[1] = 1; tcpServerNode.IPAddr.v[2] = 0; tcpServerNode.IPAddr.v[3] = 101; tcpSocketUser = INVALID_SOCKET; //Infinite loop. Check if anything is received on TCP port while(1) { switch (smTcp) { case SM_TCP_SEND_ARP: if (ARPIsTxReady()) { tsecWait = TickGet8bitSec(); //Remember when we sent last request //Send ARP request for given IP address ARPResolve(&tcpServerNode.IPAddr); smTcp = SM_TCP_WAIT_RESOLVE; } break; case SM_TCP_WAIT_RESOLVE: //The IP address has been resolved, we now have the MAC address of the //node at 10.1.0.101 if (ARPIsResolved(&tcpServerNode.IPAddr, &tcpServerNode.MACAddr)) { //After calling TCPConnect(), we have to wait for a connection //to be established. Stack will continuously try to connect! smTcp = SM_TCP_RESOLVED; //We can now connect, seeing that we have the remote server node //info (MAC and IP address) tcpSocketUser = TCPConnect(&tcpServerNode, 54124); //An error occurred during the TCPListen() function if (tcpSocketUser == INVALID_SOCKET) { //Add user code here to take action if required! } } //If not resolved after 2 seconds, send next request else { if (TickGetDiff8bitSec(tsecWait) >= (TICK8)2) { smTcp = SM_TCP_SEND_ARP; } } break; case SM_TCP_RESOLVED: //Connection has been established if (TCPIsConnected(tcpSocketUser)) { //Checks if there is a transmit buffer ready for accepting data if (TCPIsPutReady(tcpSocketUser)) { //Send a TCP Message with "Hello" TCPPut(tcpSocketUser, 'H'); TCPPut(tcpSocketUser, 'e'); TCPPut(tcpSocketUser, 'l'); TCPPut(tcpSocketUser, 'l'); TCPPut(tcpSocketUser, 'o'); //Send contents of transmit buffer, and free buffer TCPFlush(tcpSocketUser); smTcp = SM_TCP_FINISHED; //Set to "message sent" state //Close connection if not needed anymore! TCPDisconnect(tcpSocketUser) } } else { //The TCP stack will continuously try and connect. It is up to the user to //terminate the connection attempts if no connection is made after a //certian time. The TCPDisconnect() function can be used to stop the //stack from trying to connect. //TCPDisconnect(); //smTcp = SM_TCP_FINISHED; //Set to "message sent" state } break; case SM_TCP_FINISHED: //Message sent state break; } //This task performs normal stack task including checking for incoming //packet, type of packet and calling appropriate stack entity to //process it. StackTask(); } }