Raw TCP/IP interface for lwIP
Authors: Adam Dunkels, Leon Woestenberg, Christiaan Simons
lwIP provides three Application Program's Interfaces (APIs) for programs
to use for communication with the TCP/IP code:
* low-level "core" / "callback" or "raw" API.
* higher-level "sequential" API.
* BSD-style socket API.
The sequential API provides a way for ordinary, sequential, programs
to use the lwIP stack. It is quite similar to the BSD socket API. The
model of execution is based on the blocking open-read-write-close
paradigm. Since the TCP/IP stack is event based by nature, the TCP/IP
code and the application program must reside in different execution
contexts (threads).
The socket API is a compatibility API for existing applications,
currently it is built on top of the sequential API. It is meant to
provide all functions needed to run socket API applications running
on other platforms (e.g. unix / windows etc.). However, due to limitations
in the specification of this API, there might be incompatibilities
that require small modifications of existing programs.
** Threading
lwIP started targeting single-threaded environments. When adding multi-
threading support, instead of making the core thread-safe, another
approach was chosen: there is one main thread running the lwIP core
(also known as the "tcpip_thread"). The raw API may only be used from
this thread! Application threads using the sequential- or socket API
communicate with this main thread through message passing.
As such, the list of functions that may be called from
other threads or an ISR is very limited! Only functions
from these API header files are thread-safe:
- api.h
- netbuf.h
- netdb.h
- netifapi.h
- sockets.h
- sys.h
Additionaly, memory (de-)allocation functions may be
called from multiple threads (not ISR!) with NO_SYS=0
since they are protected by SYS_LIGHTWEIGHT_PROT and/or
semaphores.
Only since 1.3.0, if SYS_LIGHTWEIGHT_PROT is set to 1
and LWIP_ALLOW_MEM_FREE_FROM_OTHER_CONTEXT is set to 1,
pbuf_free() may also be called from another thread or
an ISR (since only then, mem_free - for PBUF_RAM - may
be called from an ISR: otherwise, the HEAP is only
protected by semaphores).
** The remainder of this document discusses the "raw" API. **
The raw TCP/IP interface allows the application program to integrate
better with the TCP/IP code. Program execution is event based by
having callback functions being called from within the TCP/IP
code. The TCP/IP code and the application program both run in the same
thread. The sequential API has a much higher overhead and is not very
well suited for small systems since it forces a multithreaded paradigm
on the application.
The raw TCP/IP interface is not only faster in terms of code execution
time but is also less memory intensive. The drawback is that program
development is somewhat harder and application programs written for
the raw TCP/IP interface are more difficult to understand. Still, this
is the preferred way of writing applications that should be small in
code size and memory usage.
Both APIs can be used simultaneously by different application
programs. In fact, the sequential API is implemented as an application
program using the raw TCP/IP interface.
--- Callbacks
Program execution is driven by callbacks. Each callback is an ordinary
C function that is called from within the TCP/IP code. Every callback
function is passed the current TCP or UDP connection state as an
argument. Also, in order to be able to keep program specific state,
the callback functions are called with a program specified argument
that is independent of the TCP/IP state.
The function for setting the application connection state is:
- void tcp_arg(struct tcp_pcb *pcb, void *arg)
Specifies the program specific state that should be passed to all
other callback functions. The "pcb" argument is the current TCP
connection control block, and the "arg" argument is the argument
that will be passed to the callbacks.
--- TCP connection setup
The functions used for setting up connections is similar to that of
the sequential API and of the BSD socket API. A new TCP connection
identifier (i.e., a protocol control block - PCB) is created with the
tcp_new() function. This PCB can then be either set to listen for new
incoming connections or be explicitly connected to another host.
- struct tcp_pcb *tcp_new(void)
Creates a new connection identifier (PCB). If memory is not
available for creating the new pcb, NULL is returned.
- err_t tcp_bind(struct tcp_pcb *pcb, ip_addr_t *ipaddr,
u16_t port)
Binds the pcb to a local IP address and port number. The IP address
can be specified as IP_ADDR_ANY in order to bind the connection to
all local IP addresses.
If another connection is bound to the same port, the function will
return ERR_USE, otherwise ERR_OK is returned.
- struct tcp_pcb *tcp_listen(struct tcp_pcb *pcb)
Commands a pcb to start listening for incoming connections. When an
incoming connection is accepted, the function specified with the
tcp_accept() function will be called. The pcb will have to be bound
to a local port with the tcp_bind() function.
The tcp_listen() function returns a new connection identifier, and
the one passed as an argument to the function will be
deallocated. The reason for this behavior is that less memory is
needed for a connection that is listening, so tcp_listen() will
reclaim the memory needed for the original connection and allocate a
new smaller memory block for the listening connection.
tcp_listen() may return NULL if no memory was available for the
listening connection. If so, the memory associated with the pcb
passed as an argument to tcp_listen() will not be deallocated.
- struct tcp_pcb *tcp_listen_with_backlog(struct tcp_pcb *pcb, u8_t backlog)
Same as tcp_listen, but limits the number of outstanding connections
in the listen queue to the value specified by the backlog argument.
To use it, your need to set TCP_LISTEN_BACKLOG=1 in your lwipopts.h.
- void tcp_accepted(struct tcp_pcb *pcb)
Inform lwIP that an incoming connection has been accepted. This would
usually be called from the accept callback. This allows lwIP to perform
housekeeping tasks, such as allowing further incoming connections to be
queued in the listen backlog.
ATTENTION: the PCB passed in must be the listening pcb, not the pcb passed
into the accept callback!
- void tcp_accept(struct tcp_pcb *pcb,
err_t (* accept)(void *arg, struct tcp_pcb *newpcb,
err_t err))
Specified the callback function that should be called when a new
connection arrives on a listening connection.
- err_t tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr,
u16_t port, err_t (* connected)(void *arg,
struct tcp_pcb *tpcb,
err_t err));
Sets up the pcb to connect to the remote host and sends the
initial SYN segment which opens the connection.
The tcp_connect() function returns immediately; it does not wait for
the connection to be properly setup. Instead, it will call the
function specified as the fourth argument (the "connected" argument)
when the connection is established. If the connection could not be
properly established, either because the other host refused the
connection or because the other host didn't answer, the "err"
callback function of this pcb (registered with tcp_err, see below)
will be called.
The tcp_connect() function can return ERR_MEM if no memory is
available for enqueueing the SYN segment. If the SYN indeed was
enqueued successfully, the tcp_connect() function returns ERR_OK.
--- Sending TCP data
TCP data is sent by enqueueing the data with a call to
没有合适的资源?快使用搜索试试~ 我知道了~
STM32F767+lwip+freertos 通过网页API获取天气城市的数据(整个工程文件,有源代码,有注释)
共446个文件
h:205个
c:189个
png:17个
5星 · 超过95%的资源 需积分: 37 27 下载量 57 浏览量
2019-01-05
11:07:24
上传
评论 1
收藏 3.94MB ZIP 举报
温馨提示
STM32F767+lwip+freertos 通过网页API获取天气城市的数据(整个工程文件,有源代码,有注释) 通过 2017.ip138.com/ic.asp 获得IP和地区 ip.ws.126.net/ipquery?ip=255.255.255.255 通过IP获得城市信息 www.weather.com.cn/data/cityinfo/111111111.html 通过城市代码信息获得天气信息。 这些网址都可以通过windows的ping它们的网址 得到对应的IP地址, 然后通过LWIP的设置板子为TCP客户端 绑定对应的网站IP进行通信。
资源推荐
资源详情
资源评论
收起资源包目录
STM32F767+lwip+freertos 通过网页API获取天气城市的数据(整个工程文件,有源代码,有注释) (446个子文件)
FreeRTOS-LwIP.uvguix.Administrator 137KB
keilkilll.bat 399B
cc936.c 708KB
cc949.c 555KB
cc950.c 441KB
cc932.c 244KB
ff.c 189KB
stm32f7xx_hal_tim.c 179KB
stm32f7xx_hal_i2c.c 156KB
tasks.c 154KB
stm32f7xx_hal_cryp.c 121KB
stm32f7xx_hal_jpeg.c 120KB
stm32f7xx_hal_dfsdm.c 108KB
stm32f7xx_hal_sd.c 107KB
stm32f7xx_hal_cryp_ex.c 104KB
mib2.c 103KB
stm32f7xx_hal_spi.c 90KB
stm32f7xx_hal_tim_ex.c 88KB
queue.c 82KB
stm32f7xx_hal_qspi.c 78KB
stm32f7xx_hal_eth.c 73KB
stm32f7xx_hal_dsi.c 72KB
stm32f7xx_hal_uart.c 71KB
sockets.c 71KB
stm32f7xx_hal_sai.c 69KB
lcd.c 68KB
stm32f7xx_hal_usart.c 65KB
dhcp.c 63KB
stm32f7xx_hal_ltdc.c 63KB
stm32f7xx_hal_dma2d.c 63KB
stm32f7xx_hal_adc.c 62KB
stm32f7xx_hal_hash.c 60KB
stm32f7xx_hal_rtc_ex.c 59KB
tcp_in.c 59KB
ppp.c 57KB
lcp.c 56KB
stm32f7xx_hal_rcc_ex.c 56KB
stm32f7xx_hal_irda.c 56KB
stm32f7xx_hal_rtc.c 55KB
stm32f7xx_hal_lptim.c 53KB
stm32f7xx_hal_hash_ex.c 53KB
tcp.c 52KB
stm32f7xx_hal_i2s.c 52KB
etharp.c 52KB
tcp_out.c 51KB
stm32f7xx_ll_usb.c 50KB
stm32f7xx_hal_smartcard.c 47KB
api_msg.c 47KB
stm32f7xx_hal_nand.c 47KB
stm32f7xx_hal_can.c 45KB
msg_in.c 43KB
stm32f7xx_hal_rcc.c 42KB
stm32f7xx_hal_spdifrx.c 42KB
stm32f7xx_hal_dma.c 42KB
stm32f7xx_hal_adc_ex.c 41KB
stm32f7xx_ll_fmc.c 41KB
timers.c 40KB
stm32f7xx_hal_flash_ex.c 40KB
pbuf.c 39KB
ipcp.c 38KB
mpu_wrappers.c 38KB
stm32f7xx_hal_pcd.c 37KB
stm32f7xx_hal_hcd.c 36KB
auth.c 35KB
stm32f7xx_hal_nor.c 33KB
udp.c 33KB
ppp_oe.c 33KB
stm32f7xx_hal_dac.c 32KB
ip.c 32KB
test_tcp_oos.c 31KB
dns.c 30KB
stm32f7xx_hal_dcmi.c 30KB
mib_structs.c 29KB
ip_frag.c 28KB
port.c 28KB
stm32f7xx_hal_sdram.c 27KB
igmp.c 26KB
stm32f7xx_hal_flash.c 26KB
event_groups.c 26KB
ccsbcs.c 25KB
chap.c 24KB
api_lib.c 24KB
mem.c 24KB
fsm.c 23KB
stm32f7xx_hal_cec.c 23KB
stm32f7xx_hal_sram.c 23KB
stm32f7xx_hal_pwr.c 22KB
netif.c 22KB
msg_out.c 21KB
lwip_client_app.c 21KB
test_tcp.c 21KB
stm32f7xx_hal_pwr_ex.c 20KB
stm32f7xx_hal_gpio.c 20KB
stm32f7xx_hal_mdios.c 20KB
stm32f7xx_hal_cortex.c 19KB
stm32f7xx_ll_sdmmc.c 19KB
heap_5.c 18KB
vj.c 18KB
stm32f7xx_hal.c 18KB
stm32f7xx_hal_crc.c 18KB
共 446 条
- 1
- 2
- 3
- 4
- 5
资源评论
- w6z6c2019-06-04想法挺好,谢谢分享
c_a_i_n_i_a_o
- 粉丝: 3
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功