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 00072 #include <inet/debug.h> 00073 #include <inet/datatypes.h> 00074 #include <inet/globalvariables.h> 00075 #include <inet/system.h> 00076 #include <inet/tcp_ip.h> 00077 00078 00079 00080 /* The applications that use TCP must implement following function stubs */ 00081 /* void application_name_init (void) - call once when processor starts */ 00082 /* void application_name_run (void) - call periodically on main loop */ 00083 /* INT32 application_name_eventlistener (INT8, UINT8, UINT32, UINT32) */ 00084 /* - called by TCP input process to inform arriving data, errors etc */ 00085 00086 /* These will probably go to some include file */ 00087 void tcps_demo_init(void); 00088 void tcps_demo_run(void); 00089 INT32 tcps_demo_eventlistener(INT8 , UINT8 , UINT32 , UINT32 ); 00090 00098 INT8 tcps_demo_soch; 00099 00100 UINT8 tcps_demo_senddata; 00102 #define TCPS_DEMO_PORT 5001 00104 /* Internal function used for sending data to a predefined host */ 00105 INT16 tcps_demo_send(void); 00106 00107 /* Initialize resources needed for the TCP server application */ 00108 void tcps_demo_init(void) 00109 { 00110 00111 DEBUGOUT("Initializing TCP server application. \r\n"); 00112 00113 /* Get socket: 00114 * TCP_TYPE_SERVER - type of TCP socket is server 00115 * TCP_TOS_NORMAL - no other type of service implemented so far 00116 * TCP_DEF_TOUT - timeout value in seconds. If for this many seconds 00117 * no data is exchanged over the TCP connection the socket will be 00118 * closed. 00119 * tcps_demo_eventlistener - pointer to event listener function for 00120 * this socket. 00121 */ 00122 00123 tcps_demo_soch = tcp_getsocket(TCP_TYPE_SERVER, TCP_TOS_NORMAL, TCP_DEF_TOUT, tcps_demo_eventlistener); 00124 00125 if( tcps_demo_soch < 0 ) 00126 { 00127 DEBUGOUT("TCP server unable to get socket. Resetting!!!\r\n"); 00128 RESET_SYSTEM(); 00129 } 00130 00131 /* Put it to listen on some port */ 00132 tcp_listen(tcps_demo_soch,TCPS_DEMO_PORT); 00133 00134 /* for now no data sending */ 00135 tcps_demo_senddata=0; 00136 } 00137 00138 void tcps_demo_run(void) 00139 { 00140 00141 UINT8 i; 00142 00143 /* do maybe some other TCP server app stuff 00144 * ..... 00145 */ 00146 00147 if(tcps_demo_senddata){ 00148 if(tcps_demo_send()!=-1) 00149 tcps_demo_senddata=0; 00150 } 00151 } 00152 00153 /* 00154 * Event listener invoked when TCP/IP stack receives TCP data for 00155 * a given socket. Parameters: 00156 * - cbhandle - handle of the socket this packet is intended for. Check it 00157 * just to be sure, but in general case not needed 00158 * - event - event that is notified. For TCP there are quite a few possible 00159 * events, check switch structure below for more information 00160 * - par1, par2 - parameters who's use depends on the event that is notified 00161 */ 00162 INT32 tcps_demo_eventlistener(INT8 cbhandle, UINT8 event, UINT32 par1, UINT32 par2) 00163 { 00164 /* This function is called by TCP stack to inform about events */ 00165 UINT16 i; 00166 00167 if( cbhandle != tcps_demo_soch) /* Not our handle */ 00168 return(-1); 00169 00170 switch( event ){ 00171 00172 /* Connection request event. Used by TCP/IP stack to inform 00173 * the application someone is trying to establish a connection. 00174 * Server can decide, based on provided IP address and port number, 00175 * whether to allow or not connection establishment. 00176 * Parameters: 00177 * - par1 - remote hosts IP address 00178 * - par2 - remote hosts port number 00179 * 00180 * Return values from event listener: 00181 * -1 - do not allow connection to be established (send reset) 00182 * -2 - do not send any response for now to the SYN packet (let's 00183 * think a little before answering) 00184 * 1 - allow connection to be established 00185 */ 00186 case TCP_EVENT_CONREQ: 00187 DEBUGOUT("Connection request arrived!\r\n"); 00188 00189 /* Enable all connections */ 00190 return(1); 00191 00192 break; 00193 00194 /* Connection abort event. Connection on a given socket is beeing 00195 * aborted for somereason (usually retransmissions are used up or 00196 * some abnormal situation in communication happened). 00197 * Parameters: 00198 * - par1 - remote hosts IP address 00199 * - par2 - remote hosts port number 00200 */ 00201 case TCP_EVENT_ABORT: 00202 DEBUGOUT("Connection aborting!\r\n"); 00203 break; 00204 00205 /* Connection established event - three-way handshaking performed 00206 * OK and connection is established. 00207 * Parameters: 00208 * - par1 - remote hosts IP address 00209 * - par2 - remote hosts port number 00210 */ 00211 case TCP_EVENT_CONNECTED: 00212 DEBUGOUT("TCP connection established!\r\n"); 00213 00214 break; 00215 00216 /* Connection closing event. Happens when TCP connection is 00217 * intentionally close by some side calling close function and 00218 * initializing proper TCP connection close procedure. 00219 * Parameters: 00220 * - par1 - remote hosts IP address 00221 * - par2 - remote hosts port number 00222 */ 00223 case TCP_EVENT_CLOSE: 00224 DEBUGOUT("TCP Connection closing...!\r\n"); 00225 break; 00226 00227 /* Data acknowledgment event. Happens when data that was 00228 * previously sent gets acknowledged. This means we can now 00229 * send some more data! :-) 00230 * Parameters: 00231 * - par1 - remote hosts IP address 00232 * - par2 - remote hosts port number 00233 */ 00234 case TCP_EVENT_ACK: 00235 DEBUGOUT("Data acknowledged!\r\n"); 00236 /* if more data should be sent, adjust variables and 00237 set tcps_demo_senddata variable */ 00238 00239 break; 00240 00241 /* Data received event. Happens when we receive some data over the 00242 * TCP connection. 00243 * Parameters: 00244 * - par1 - number of data bytes received 00245 * - par2 = 0 00246 */ 00247 case TCP_EVENT_DATA: 00248 DEBUGOUT("Data arrived!\r\n"); 00249 /* read data that was received (and 00250 * probably do something with it :-) 00251 */ 00252 for(i=0;i<par1;i++) 00253 RECEIVE_NETWORK_B(); 00254 00255 /* If needed initialize data sending 00256 * by setting tcps_demo_senddata variable 00257 */ 00258 break; 00259 00260 /* Regenerate data event. Happens when data needs to be 00261 * retransmitted because of possible loss on the network. 00262 * Note that THE SAME DATA must be sent over and over again 00263 * until TCP_EVENT_ACK is generated (for that data)! 00264 * Parameters: 00265 * - par1 - amount of data to regenerate (usually all) 00266 * - par2 = 0 00267 */ 00268 case TCP_EVENT_REGENERATE: 00269 tcps_demo_send(); 00270 break; 00271 00272 00273 default: 00274 return(-1); 00275 } 00276 } 00277 00278 INT16 tcps_demo_send(void){ 00279 UINT16 i; 00280 /* first check if data sending is possible (it may be that 00281 * previously sent data is not yet acknowledged) 00282 */ 00283 if(tcp_checksend(tcps_demo_soch) < 0 ){ 00284 /* Not yet */ 00285 return -1; 00286 } 00287 00288 /* put message in buffer. Message needs to start from TCP_APP_OFFSET 00289 * because TCP/IP stack will put headers in front of the message to 00290 * avoid data copying 00291 */ 00292 for(i=0;i<32;i++) 00293 net_buf[TCP_APP_OFFSET+i]='A'+(i%25); 00294 00295 /* send data */ 00296 return tcp_send(tcps_demo_soch, &net_buf[TCP_APP_OFFSET], NETWORK_TX_BUFFER_SIZE - TCP_APP_OFFSET, 32); 00297 00298 }