00001 /* 00002 *Copyright (c) 2000-2002 Viola Systems Ltd. 00003 *All rights reserved. 00004 * 00005 *Redistribution and use in source and binary forms, with or without 00006 *modification, are permitted provided that the following conditions 00007 *are met: 00008 * 00009 *1. Redistributions of source code must retain the above copyright 00010 *notice, this list of conditions and the following disclaimer. 00011 * 00012 *2. Redistributions in binary form must reproduce the above copyright 00013 *notice, this list of conditions and the following disclaimer in the 00014 *documentation and/or other materials provided with the distribution. 00015 * 00016 *3. The end-user documentation included with the redistribution, if 00017 *any, must include the following acknowledgment: 00018 * "This product includes software developed by Viola 00019 * Systems (http://www.violasystems.com/)." 00020 * 00021 *Alternately, this acknowledgment may appear in the software itself, 00022 *if and wherever such third-party acknowledgments normally appear. 00023 * 00024 *4. The names "OpenTCP" and "Viola Systems" must not be used to 00025 *endorse or promote products derived from this software without prior 00026 *written permission. For written permission, please contact 00027 *opentcp@opentcp.org. 00028 * 00029 *5. Products derived from this software may not be called "OpenTCP", 00030 *nor may "OpenTCP" appear in their name, without prior written 00031 *permission of the Viola Systems Ltd. 00032 * 00033 *THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED 00034 *WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00035 *MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 00036 *IN NO EVENT SHALL VIOLA SYSTEMS LTD. OR ITS CONTRIBUTORS BE LIABLE 00037 *FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00038 *CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00039 *SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 00040 *BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 00041 *WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 00042 *OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00043 *EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00044 *==================================================================== 00045 * 00046 *OpenTCP is the unified open source TCP/IP stack available on a series 00047 *of 8/16-bit microcontrollers, please see <http://www.opentcp.org>. 00048 * 00049 *For more information on how to network-enable your devices, or how to 00050 *obtain commercial technical support for OpenTCP, please see 00051 *<http://www.violasystems.com/>. 00052 */ 00053 00071 #include <inet/debug.h> 00072 #include <inet/datatypes.h> 00073 #include <inet/globalvariables.h> 00074 #include <inet/system.h> 00075 #include <inet/tcp_ip.h> 00076 00077 /* The applications that use UDP must implement following function stubs */ 00078 /* void application_name_init (void) - call once when processor starts */ 00079 /* void application_name_run (void) - call periodically on main loop */ 00080 /* INT32 application_name_eventlistener (INT8, UINT8, UINT32, UINT16, UINT16, UINT16) */ 00081 /* - called by TCP input process to inform arriving data, errors etc */ 00082 00083 /* These will probably go to some include file */ 00084 void udp_demo_init(void); 00085 void udp_demo_run(void); 00086 INT32 udp_demo_eventlistener(INT8 , UINT8 , UINT32 , UINT16 , UINT16 , UINT16 ); 00087 00095 UINT8 udp_demo_soch; 00096 00097 UINT8 udp_demo_senddata; 00099 #define UDP_DEMO_PORT 5000 00101 #define UDP_DEMO_RMTHOST_IP 0xAC100101 00103 #define UDP_DEMO_RMTHOST_PRT 5001 00105 /* Internal function used for sending data to a predefined host */ 00106 INT16 udp_demo_send(void); 00107 00108 /* Initialize resources needed for the UDP socket application */ 00109 void udp_demo_init(void){ 00110 00111 DEBUGOUT("Initializing UDP demo client\r\n"); 00112 00113 /* Get socket: 00114 * 0 - for now not type of service implemented in UDP 00115 * udp_echo_eventlistener - pointer to listener function 00116 * UDP_OPT_SEND_CS|UDP_OPT_CHECK_CS - checksum options. Calculate 00117 * checksum for outgoing packets and check checksum for 00118 * received packets. 00119 */ 00120 udp_demo_soch=udp_getsocket(0 , udp_demo_eventlistener , UDP_OPT_SEND_CS | UDP_OPT_CHECK_CS); 00121 00122 if(udp_demo_soch == -1){ 00123 DEBUGOUT("No free UDP sockets!! \r\n"); 00124 RESET_SYSTEM(); 00125 } 00126 00127 /* open socket for receiving/sending of the data on defined port*/ 00128 udp_open(udp_demo_soch,UDP_DEMO_PORT); 00129 00130 /* for now no data sending */ 00131 udp_demo_senddata=0; 00132 } 00133 00134 /* UDP Demo app main loop that is periodically invoked from the 00135 * main loop (see main_demo.c) 00136 */ 00137 void udp_demo_run(void){ 00138 /* do maybe some other UDP demo app stuff 00139 * ..... 00140 */ 00141 if(udp_demo_senddata) { 00142 switch(udp_demo_send()){ 00143 case -1: 00144 DEBUGOUT("Error (General error, e.g. parameters)\r\n"); 00145 break; 00146 case -2: 00147 DEBUGOUT("ARP or lower layer not ready, try again\r\n"); 00148 break; 00149 case -3: 00150 DEBUGOUT("Socket closed or socket handle not valid!\r\n"); 00151 break; 00152 default: 00153 /* data sent (could check how many bytes too) 00154 * if no more data to send, reset flag 00155 */ 00156 udp_demo_senddata=0; 00157 break; 00158 00159 } 00160 } 00161 } 00162 00163 /* 00164 * Event listener invoked when TCP/IP stack receives UDP datagram for 00165 * a given socket. Parameters: 00166 * - cbhandle - handle of the socket this packet is intended for. Check it 00167 * just to be sure, but in general case not needed 00168 * - event - event that is notified. For UDP, only UDP_EVENT_DATA 00169 * - ipaddr - IP address of remote host who sent the UDP datagram 00170 * - port - port number of remote host who sent the UDP datagram 00171 * - buffindex - buffer index in RTL8019AS allowing you to read 00172 * received data more than once from Ethernet controller by 00173 * invoking NETWORK_RECEIVE_INITIALIZE(buffindex) and then start 00174 * reading the bytes all over again 00175 */ 00176 INT32 udp_demo_eventlistener(INT8 cbhandle, UINT8 event, UINT32 ipaddr, UINT16 port, UINT16 buffindex, UINT16 datalen){ 00177 UINT16 i; 00178 if(cbhandle!=udp_demo_soch){ 00179 DEBUGOUT("Not my handle!!!!"); 00180 return (-1); 00181 } 00182 00183 switch(event){ 00184 case UDP_EVENT_DATA: 00185 00186 /* read data that was received (and 00187 * probably do something with it :-) 00188 */ 00189 for(i=0;i<datalen;i++) 00190 RECEIVE_NETWORK_B(); 00191 00192 /* If needed initialize data sending 00193 * by setting udp_demo_senddata variable or 00194 * send data directly from event listener ( 00195 * only possible for UDP applications!!!! 00196 */ 00197 00198 break; 00199 00200 default: 00201 /* should never get here */ 00202 DEBUGOUT("Unknown UDP event :-("); 00203 break; 00204 } 00205 return 0; 00206 } 00207 00208 /* internal function invoked to send UDP message to, in this case, 00209 * some predefined host. 00210 */ 00211 #define MSG_SIZE 20 00212 INT16 udp_demo_send(void){ 00213 UINT8 i; 00214 00215 /* put message in buffer. Message needs to start from UDP_APP_OFFSET 00216 * because TCP/IP stack will put headers in front of the message to 00217 * avoid data copying 00218 */ 00219 for(i=0;i<MSG_SIZE;i++) 00220 net_buf[UDP_APP_OFFSET+i]=i; 00221 00222 /* send message */ 00223 return udp_send(udp_demo_soch,UDP_DEMO_RMTHOST_IP,UDP_DEMO_RMTHOST_PRT,net_buf+UDP_APP_OFFSET,NETWORK_TX_BUFFER_SIZE-UDP_APP_OFFSET,MSG_SIZE); 00224 00225 }