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, struct ip_addr *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.
- 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, struct ip_addr *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
没有合适的资源?快使用搜索试试~ 我知道了~
GD32F103VBT6核心板代码示例
共766个文件
h:326个
c:310个
txt:91个
需积分: 5 1 下载量 113 浏览量
2024-04-11
09:31:38
上传
评论
收藏 4.06MB ZIP 举报
温馨提示
基于GD32F103VBT6做了一个核心板,代码里面包括有ADC功能、蜂鸣器功能、LCD功能、按键控制、IIC功能、GD-Link编程调试功能 、SPI通信功能、USART通信功能、USB功能、CAN通信功能的代码示例。
资源推荐
资源详情
资源评论
收起资源包目录
GD32F103VBT6核心板代码示例 (766个子文件)
picture.c 361KB
picture.c 140KB
mib2.c 105KB
gd32f10x_eth.c 105KB
sdcard.c 98KB
gd32f10x_timer.c 81KB
dhcp.c 60KB
lcp.c 57KB
sockets.c 57KB
ppp.c 56KB
tcp_in.c 53KB
etharp.c 44KB
tcp.c 44KB
gd32f10x_rcc.c 44KB
msg_in.c 43KB
ipcp.c 40KB
gd32f10x_can.c 39KB
ppp_oe.c 38KB
system_gd32f10x.c 38KB
gd32f10x_fmc.c 36KB
gd32f10x_adc.c 35KB
api_msg.c 34KB
gd32f10x_exmc.c 34KB
tcp_out.c 33KB
dns.c 31KB
mib_structs.c 30KB
pbuf.c 30KB
gd32f10x_i2c.c 30KB
udp.c 29KB
ip_frag.c 27KB
gd32f10x_usart.c 26KB
auth.c 26KB
igmp.c 26KB
ip.c 26KB
gd32f10x_dma.c 25KB
chap.c 25KB
fsm.c 24KB
usbd_enum.c 22KB
msg_out.c 22KB
gd32f10x_sdio.c 22KB
mem.c 22KB
usbd_dfu_core.c 21KB
exmc_nandflash.c 20KB
gd32f10x_spi.c 19KB
netif.c 19KB
vj.c 19KB
gd32f10x_gpio.c 18KB
usbd_msc_scsi.c 17KB
core_cm3.c 17KB
asn1_dec.c 17KB
api_lib.c 17KB
usbd_cdc_core.c 16KB
tcpip.c 16KB
gd32f107.c 16KB
pap.c 15KB
usbd_iap_core.c 15KB
autoip.c 15KB
usb_core.c 15KB
usbd_custom_hid_core.c 15KB
ethernetif.c 15KB
asn1_enc.c 14KB
inet_chksum.c 13KB
main.c 12KB
24Cxx.c 12KB
ili9320.c 12KB
exmc_norflash.c 12KB
main.c 12KB
icmp.c 12KB
GD25Qxx.c 12KB
GD25Qxx.c 12KB
md5.c 12KB
chpms.c 12KB
ip6.c 12KB
netdb.c 11KB
gd32f10x_pwr.c 11KB
usbd_it.c 11KB
memp.c 11KB
gd32f10x_dac.c 11KB
init.c 11KB
sys_arch.c 11KB
raw.c 11KB
main.c 11KB
main.c 11KB
main.c 10KB
main.c 10KB
sys.c 10KB
usbd_hid_core.c 10KB
ethernetif.c 10KB
usbd_msc_core.c 9KB
netconf.c 9KB
gd32103b_eval.c 9KB
main.c 9KB
EXMC_SRAM.c 9KB
main.c 9KB
main.c 9KB
main.c 9KB
main.c 8KB
main.c 8KB
usbd_msc_bot.c 8KB
main.c 8KB
共 766 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
qq_755682240
- 粉丝: 8837
- 资源: 79
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 算法竞赛中的离散化 概念总结和基本操作全解
- 算法竞赛位运算(简单易懂)
- 常用一维二维 前缀和与差分算法模板总结
- SAR成像算法+后向投影(BP)算法+星载平台实测数据
- 横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横向循环焦点轮播图横
- 基于Java和HTML的留言墙、验证码、计算器基础项目设计源码
- 基于JAVA C/C++的嵌入式设备组网平台物联网框架设计源码
- 基于Java开发的高性能全文检索工具包jsearch设计源码
- 基于多语言技术的pt遨游助手手机版设计源码
- 基于若依框架的染云盘V1.0.2设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功