TCP Tunnel¶
The tcp_tunnel module implements a line protocol. When this protocol is chosen by a line, it sends data received via TCP out on the line. It sends data received on the line out via TCP when it receives Enter or fills its 80 character buffer.
As delivered in the SDK, the module listens for a TCP connection on both the AP0 and WLAN0 interface addressed to LOCAL_PORT 10001. When the connection is established, it sends Connected to the line. Subsequently, it listens to the line for data to send out via TCP. #define REMOTE_ADDRESS must be commented out to operate in this listening mode.
Alternately, you can change #define REMOTE_ADDRESS to either an IP address or a host name to operate in a connect mode. In this case you must also set #define REMOTE_PORT. When the connection is established, it sends Connected to the line.
Build it from project tcpTunnelDemo.
For connecting out, fill in REMOTE_ADDRESS with either an IP address or a host name. For listening, comment out the definition of REMOTE_ADDRESS. The REMOTE_PORT is only used if "connecting out" according to REMOTE_ADDRESS.
#define REMOTE_ADDRESS "remote_host_goes_here"
Function Details¶
The following is an overview of the functions used in the tcp_tunnel module.
Receive data from the socket using
ltrx_ip_socket_receive(). If there is data, useltrx_output_stream_write_binary()to write binary data to the buffer. If there is not, useltrx_tcp_socket_is_eof()to check if all receive data has been read and aFINhas been received. If so, useltrx_output_stream_write_line()to write "" ending with a new line and then "Disconnected" ending with a new line. Then useltrx_tcp_socket_close()to close the socket.
Use
ltrx_input_stream_peek()to perform non-blocking peek. When there are characters in the stream, useltrx_input_stream_read()to read the next character in the stream. If the next character is carriage return (\r), useltrx_output_stream_write_line()to write "", ending with a new line. Otherwise, useltrx_output_stream_write_binary()to write the next character.If
SendNowistrue, useltrx_ip_socket_send()to send the data and thenltrx_tcp_socket_close()to close the socket.
Use
ltrx_input_stream_init_from_uart()to set up an input stream from a serial line andltrx_output_stream_init_to_uart()to set up an output stream to a serial line. Then useltrx_output_stream_write_line()to write "" ending with a new line and then the protocol name and end with a new line.While the thread is running,
REMOTE_ADDRESSis defined, and there is no socket, useltrx_tcp_connect()to connect. If there is a socket, print "Connected".While the thread is running,
REMOTE_ADDRESSis not defined, and there is no socket and no listener, useltrx_tcp_listener_begin()to initialize a listener and start listening.If
REMOTE_ADDRESSis not defined and there is no socket open but a listener is in use, useltrx_tcp_accept()to accept a connection request and thenltrx_ip_socket_get_peer_information()to get peer information (IP address and port of the remote device). Then useltrx_ip_and_port_display()to print the IP address and port. Useltrx_output_stream_write_without_ending_line()to writes "Accepted from" (no new line) followed by ltrx_output_stream_write_line()to the remote address and port, with a new line. Finally, useltrx_tcp_listener_end()` to stop listening.If there is a socket open for the thread, use
ltrx_line_read_bytes_available()to find out how many bytes are available and signal the trigger when data is available.Wait for a trigger with
LTRX_TRIGGER_WAIT()and clear the trigger withltrx_trigger_clear().
Check to see if a thread is already running on the line and print the message to trouble log if so.
Use
ltrx_thread_create()to to create a thread and start it.
Use
ltrx_preemption_block()to block other threads from preempting this one. If the thread is running, then signal a trigger withltrx_trigger_signal(). Then useltrx_preemption_unblock()to allow other threads to preempt this one.If
wasRunningistrue, useltrx_timemark()to get time mark. While the thread exists and is not the current thread returned byltrx_thread_id(), and the time since time mark, returned by ltrx_elapsed_time_current_ms()is less that 2000 ms, but greater or equal to 500 ms, then purge the data from the serial line withltrx_line_purge(). Otherwise, pause the thread for 100ms withltrx_thread_sleep()`.