-- (c) William Welch 2004 -- -- This software is provided 'as-is', without any express or implied -- warranty. In no event will the authors be held liable for any damages -- arising from the use of this software. -- -- Permission is granted to anyone to use this software for any purpose, -- including commercial applications, and to alter it and redistribute it -- freely, subject to the following restrictions: -- -- 1. The origin of this software must not be misrepresented; -- you must not claim that you wrote the original software. -- If you use this software in a product, an acknowledgment in -- the product documentation would be appreciated but is not required. -- -- 2. Altered source versions must be plainly marked as such, and must -- not be misrepresented as being the original software. -- -- 3. This notice may not be removed or altered from any source distribution. -- -- This license is commonly known as the zlib/libpng License. -- -- -- tcpapi.jal 5 April 2005 -- See RFC 793 for details -- Applications should always call this function before -- calling tcp_send or tcp_recv. return codes: -- 0xff == tcp connection is not established -- 0xfe == someone else owns the PKT at this time. -- 0 == can't read or write at this time. -- 1 == the current PKT has valid rx data now. -- 2 == the current PKT is available for writing. function tcp_status ( byte in conn_off ) return byte is if MemRdEx ( conn_off , conn_state ) != tcp_estab then return 0xff end if if ( pkt_owner != 0 ) & ( pkt_owner != conn_off ) then return 0xfe end if MemCpyEx( L16, 0, conn_off , conn_rxlen, 2) if I16_CEQ ( L16 , ZERO16 ) == false then return 1 end if if MemRdEx ( conn_off , conn_txok ) == 1 then SetPointerRel ( TCP_APP_OFF, TCP_OFF , 20 ) return 2 end if -- probably waiting for ACK for previous TX return 0 end function -- returns the number of bytes available in the buffer pointed to by TCP_APP_OFF function tcp_recv(byte in conn_off, byte out rxlen_hi, byte out rxlen_lo) return bit is rxlen_hi = 0 rxlen_lo = 0 if tcp_status ( conn_off ) != 1 then return false end if MemCpyEx( L16, 0, conn_off , conn_rxlen, 2) I16_SVL ( L16 , rxlen_hi , rxlen_lo) MemCpyEx( conn_off , conn_rxlen , ZERO16 , 0 , 2) pkt_owner = 0 return I16_CEQ ( L16 , ZERO16 ) end function -- sends the data, which has been already written to by the application. function tcp_send ( byte in conn_off, byte in txlen_hi , byte in txlen_lo ) return bit is if tcp_status ( conn_off ) != 2 then return false end if MemWrEx( conn_off , conn_txok, 0 ) if debug_mode then putc = "x" end if -- set ACK bit. tcp_txpkt(conn_off, 20, 0x10, txlen_hi , txlen_lo) return true end function function tcp_send_close ( byte in conn_off ) return bit is if ( pkt_owner != 0 ) & ( pkt_owner != conn_off ) then return false end if -- send our reply if debug_mode then putc = "z" end if -- set ACK and FIN tcp_txpkt(conn_off, 20, 0x11, 0 , 0) return true end function