MediaTek LinkIt™ Connect 7681 Developer's Guide
© 2015, 2016 MediaTek Inc.
Page 48 of 65
This document contains information that is proprietary to MediaTek Inc.
Unauthorized reproduction or disclosure of this information in whole or in part is strictly prohibited.
2)
Exchange data with TCP Client
The logic and source code in
iot_tcp_appcall
is the same as used in section5.3.1. It prints
out whenever data are received and sends back “hello” once every 5 seconds:
File: iot_tcp_app.c
void
app_handle_connection
(
void
)
{
static
struct
timer user_timer
;
//create a timer;
static
bool app_init
=
FALSE
;
if
(
uip_newdata
())
// there are data to be received and stored in
uip_appdata
{
printf_high
(
"TCP Server RX [%d] bytes\n"
,
uip_datalen
());
iot_uart_output
(
uip_appdata
,
uip_datalen
());
}
if
(
uip_poll
())
{
if
((app_init == FALSE) ||
timer_expired
(&
user_timer
))
{
printf_high
(
"TCP Server timer is expired, TX\n"
);
uip_send
(
"hello\n"
,
6
);
timer_set
(&
user_timer
,
5
*
CLOCK_SECOND
);
app_init = TRUE;
}
}
}
iot_tcp_appcall
(
void
)
{
u16_t lport
=
HTONS
(
uip_conn
->
lport
);
if
(
lport
==
9999
)
// if local port is 9999, it is our event
{
app_handle_connection
();
}
...
}
To test the code, first make sure that MT7681 has network access. Second, have the remote client
connect to the IP of MT7681 (the IP address is output to UART during the acquisition process). You
can use nc (netcat), which is built-in to Linux and
available for Windows
, to do this:
> nc [IP of MT7681] 9999 // Ex. nc 192.168.1.1 9999
The shell window at the remote client will start displaying “hello” periodically. You can also type
characters back, these should be output on UART of MT7681.
Placing the client and server (MT7681) under the same network will avoid potential issues with
local IP and port forwarding.