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