00001 00008 #include "net\stacktsk.h" 00009 #include "net\tick.h" 00010 #include "net\helpers.h" 00011 #include "net\udp.h" 00012 #include "net\arp.h" 00013 #include "net\arptsk.h" 00014 00015 //Create a UDP socket for receiving and sending data 00016 static UDP_SOCKET udpSocketUser; 00017 00018 //UDP State machine 00019 #define SM_UDP_SEND_ARP 0 00020 #define SM_UDP_WAIT_RESOLVE 1 00021 #define SM_UDP_RESOLVED 2 00022 static BYTE smUdp = SM_UDP_SEND_ARP; 00023 00024 //Timers 00025 TICK16 tsecMsgSent = 0; //Time last message was sent 00026 TICK8 tsecArpRequested = 0; //Time last ARP request was sent 00027 00028 void udpExample(void) 00029 { 00030 BYTE c; 00031 NODE_INFO udpServerNode; 00032 00033 //Initialize remote IP and address with 10.1.0.101. The MAC address is 00034 //is not intialized yet, but after we receive an ARP responce. 00035 //Configure for local port 54123 and remote port 54124. 00036 udpServerNode.IPAddr.v[0] = 10; 00037 udpServerNode.IPAddr.v[1] = 1; 00038 udpServerNode.IPAddr.v[2] = 0; 00039 udpServerNode.IPAddr.v[3] = 101; 00040 udpSocketUser = UDPOpen(54123, &udpServerNode, 54124); 00041 00042 //An error occurred during the UDPOpen() function 00043 if (udpSocketUser == INVALID_UDP_SOCKET) { 00044 //Add user code here to take action if required! 00045 } 00046 00047 //Infinite loop. Check if anything is received on UDP port 00048 while(1) 00049 { 00050 switch (smUdp) { 00051 case SM_UDP_SEND_ARP: 00052 if (ARPIsTxReady()) { 00053 //Remember when we sent last request 00054 tsecArpRequested = TickGet8bitSec(); 00055 00056 //Send ARP request for given IP address 00057 ARPResolve(&udpServerNode.IPAddr); 00058 00059 smUdp = SM_UDP_WAIT_RESOLVE; 00060 } 00061 break; 00062 case SM_UDP_WAIT_RESOLVE: 00063 //The IP address has been resolved, we now have the MAC address of the 00064 //node at 10.1.0.101 00065 if (ARPIsResolved(&udpServerNode.IPAddr, &udpServerNode.MACAddr)) { 00066 smUdp = SM_UDP_RESOLVED; 00067 } 00068 //If not resolved after 2 seconds, send next request 00069 else { 00070 if (TickGetDiff8bitSec(tsecArpRequested) >= 2) { 00071 smUdp = SM_UDP_SEND_ARP; 00072 } 00073 } 00074 break; 00075 case SM_UDP_RESOLVED: 00076 if ( !PORTB_RB0) { 00077 //Send a message every second for as long as PIC port pin B0 is = 0 00078 if (TickGetSecDiff(tsecMsgSent) >= 1) { 00079 //Checks if there is a transmit buffer ready for accepting 00080 //data, and that the given socket is valid (not equal to 00081 //INVALID_UDP_SOCKET for example) 00082 if (UDPIsPutReady(udpSocketUser)) { 00083 tsecMsgSent = TickGetSec(); //Update with current time 00084 00085 //Send a UDP Datagram with one byte only indicating the 00086 //status We are only interrested in the first byte of the message. 00087 UDPPut(1); 00088 00089 //Send contents of transmit buffer, and free buffer 00090 UDPFlush(); 00091 00092 } 00093 00094 //Toggle system LED each time a message is sent 00095 TRISB_RB6 = 0; 00096 LATB6 ^= 1; 00097 } 00098 } 00099 break; 00100 } 00101 00102 00103 //This task performs normal stack task including checking for incoming 00104 //packet, type of packet and calling appropriate stack entity to 00105 //process it. 00106 StackTask(); 00107 } 00108 }
1.4.5