 | RL-ARM User's Guide |  |
|
|
| Example for Sending DataIn the following example, the basic concept is to send large amounts of data using a TCP socket. This example sends 64 Kbytes to the remote IP address 192.168.0.100, which is listening on port 1000. The TCP socket is permanently allocated and is not released when the data is sent or when the connection is closed. - Initialize the RL-TCPnet system and allocate a free TCP socket:
#include <rtl.h>
U8 tcp_soc;
U8 soc_state;
BOOL wait_ack;
void main (void) {
init_ ();
tcp_soc = tcp_get_socket (TCP_TYPE_CLIENT, 0, 120, tcp_callback);
soc_state = 0;
- Run the main thread of the RL-TCPnet system and call the send_data() function from an endless loop:
while (1) {
timer_poll ();
main_TcpNet ();
send_data ();
}
}
- The send_data() function must be implemented as a state machine. It opens an Active TCP connection, sends data, and closes the TCP connection in the end. When the soc_state is 0, the connection is initiated:
void send_data (void) {
static const U8 rem_IP[4] = {192,168,0,100};
static int bcount;
U32 max;
U8 *sendbuf;
switch (soc_state) {
case 0:
tcp_connect (tcp_soc, rem_IP, 1000, 0);
bcount = 0;
wait_ack = __FALSE;
soc_state = 1;
return;
- Next, state 1 is waiting for the TCP_EVT_CONNECT event. This event is received in the tcp_callback() event callback function, which places the send_data process into state 2 (sending data state).
- In state 2, allocate the maximum possible size of transmit buffer, fill it with some data, and send it. The maximum possible transmit buffer is allocated to reduce the number of packets and improve the transfer speed.
After the packet is sent, wait for the remote acknowledge before proceeding with the next data packet.
case 2:
if (wait_ack == __TRUE) {
return;
}
max = tcp_max_dsize (tcp_soc);
sendbuf = tcp_get_buf (max);
for (i = 0; i < max; i += 2) {
sendbuf[i] = bcount >> 8;
sendbuf[i+1] = bcount & 0xFF;
if (bcount >= 32768) {
soc_state = 3;
break;
}
}
tcp_send (tcp_soc, sendbuf, i);
wait_ack = __TRUE;
return;
- State 3 is achieved when the data transfer is finished. Wait for the last packet to be acknowledged and then close the TCP connection.
case 3:
if (wait_ack == __TRUE) {
return;
}
tcp_close (tcp_soc);
soc_state = 4;
return;
}
}
- The embedded application waits for the TCP socket to connect before starting to send data. When the data packet is sent, the application waits for the acknowledge before creating and sending the next data packet. Use the callback listener function to wait for the remote acknowledge.
U16 tcp_callback (U8 soc, U8 event, U8 *ptr, U16 par) {
/* This function is called on TCP event */
switch (event) {
..
case TCP_EVT_CONNECT:
/* Socket is now connected and ready to send data. */
soc_state = 2;
break;
case TCP_EVT_ACK:
/* Our sent data has been acknowledged by remote peer */
wait_ack = __FALSE;
break;
..
}
return (0);
}
Note - This assumes that the Network Interface Adapter is selected, enabled, and properly configured in the Net_Config.c configuration file.
- If the system runs out of TCP sockets, the application hangs in an endless loop in the system error function with the error code ERR_TCP_ALLOC.
|
|