00001 00009 #include "net\stacktsk.h" 00010 #include "net\tick.h" 00011 #include "net\helpers.h" 00012 #include "net\tcp.h" 00013 #include "net\arp.h" 00014 #include "net\arptsk.h" 00015 00016 //Create a TCP socket for receiving and sending data 00017 static TCP_SOCKET tcpSocketUser = INVALID_SOCKET; 00018 00019 //TCP State machine 00020 #define SM_TCP_SEND_ARP 0 00021 #define SM_TCP_WAIT_RESOLVE 1 00022 #define SM_TCP_RESOLVED 2 00023 #define SM_TCP_FINISHED 3 00024 static BYTE smTcp = SM_TCP_SEND_ARP; 00025 00026 //Timers 00027 TICK8 tsecWait = 0; //General purpose wait timer 00028 TICK16 tsecMsgSent = 0; //Time last message was sent 00029 00030 void tcpExample(void) 00031 { 00032 BYTE c; 00033 NODE_INFO tcpServerNode; 00034 00035 //Initialize remote IP and address with 10.1.0.101. The MAC address is 00036 //is not intialized yet, but after we receive an ARP responce. 00037 tcpServerNode.IPAddr.v[0] = 10; 00038 tcpServerNode.IPAddr.v[1] = 1; 00039 tcpServerNode.IPAddr.v[2] = 0; 00040 tcpServerNode.IPAddr.v[3] = 101; 00041 00042 tcpSocketUser = INVALID_SOCKET; 00043 00044 //Infinite loop. Check if anything is received on TCP port 00045 while(1) 00046 { 00047 switch (smTcp) { 00048 case SM_TCP_SEND_ARP: 00049 if (ARPIsTxReady()) { 00050 tsecWait = TickGet8bitSec(); //Remember when we sent last request 00051 00052 //Send ARP request for given IP address 00053 ARPResolve(&tcpServerNode.IPAddr); 00054 00055 smTcp = SM_TCP_WAIT_RESOLVE; 00056 } 00057 break; 00058 case SM_TCP_WAIT_RESOLVE: 00059 //The IP address has been resolved, we now have the MAC address of the 00060 //node at 10.1.0.101 00061 if (ARPIsResolved(&tcpServerNode.IPAddr, &tcpServerNode.MACAddr)) { 00062 //After calling TCPConnect(), we have to wait for a connection 00063 //to be established. Stack will continuously try to connect! 00064 smTcp = SM_TCP_RESOLVED; 00065 00066 //We can now connect, seeing that we have the remote server node 00067 //info (MAC and IP address) 00068 tcpSocketUser = TCPConnect(&tcpServerNode, 54124); 00069 00070 //An error occurred during the TCPListen() function 00071 if (tcpSocketUser == INVALID_SOCKET) { 00072 //Add user code here to take action if required! 00073 } 00074 } 00075 //If not resolved after 2 seconds, send next request 00076 else { 00077 if (TickGetDiff8bitSec(tsecWait) >= (TICK8)2) { 00078 smTcp = SM_TCP_SEND_ARP; 00079 } 00080 } 00081 break; 00082 case SM_TCP_RESOLVED: 00083 //Connection has been established 00084 if (TCPIsConnected(tcpSocketUser)) { 00085 //Checks if there is a transmit buffer ready for accepting data 00086 if (TCPIsPutReady(tcpSocketUser)) { 00087 //Send a TCP Message with "Hello" 00088 TCPPut(tcpSocketUser, 'H'); 00089 TCPPut(tcpSocketUser, 'e'); 00090 TCPPut(tcpSocketUser, 'l'); 00091 TCPPut(tcpSocketUser, 'l'); 00092 TCPPut(tcpSocketUser, 'o'); 00093 00094 //Send contents of transmit buffer, and free buffer 00095 TCPFlush(tcpSocketUser); 00096 smTcp = SM_TCP_FINISHED; //Set to "message sent" state 00097 00098 //Close connection if not needed anymore! 00099 TCPDisconnect(tcpSocketUser) 00100 } 00101 } 00102 else { 00103 //The TCP stack will continuously try and connect. It is up to the user to 00104 //terminate the connection attempts if no connection is made after a 00105 //certian time. The TCPDisconnect() function can be used to stop the 00106 //stack from trying to connect. 00107 00108 //TCPDisconnect(); 00109 //smTcp = SM_TCP_FINISHED; //Set to "message sent" state 00110 } 00111 break; 00112 case SM_TCP_FINISHED: 00113 //Message sent state 00114 break; 00115 } 00116 00117 //This task performs normal stack task including checking for incoming 00118 //packet, type of packet and calling appropriate stack entity to 00119 //process it. 00120 StackTask(); 00121 } 00122 }
1.5.2