lwip 之三 操作系统隔离接口 sys-尊龙游戏旗舰厅官网
目前,网络上多数文章所使用的 lwip 版本为1.4.1。最新版本为 2.0.3。从 1.4.1 到 2.0.3(貌似从2.0.0开始), lwip 的源码有了一定的变化,甚至于源码的文件结构也不一样,内部的一些实现源文件也被更新和替换了。
lwip 为了适应不同的操作系统,在代码中没有使用和某一个操作系统相关的系统调用和数据结构。而是在 lwip 和操作系统之间增加了一个操作系统封装层。操作系统封装层为操作系统服务(定时,进程同步,消息传递)提供了一个统一的接口。
在 lwip 源码的doc/sys_arch.txt文件中,有详细的说明需要移植者实现那些文件及函数。以下主要是针对该文件的一些翻译。
the operating system emulation layer provides a common interface between the lwip code and the underlying operating system kernel. the general idea is that porting lwip to new architectures requires only small changes to a few header files and a new sys_arch implementation. it is also possible to do a sys_arch implementation that does not rely on any underlying operating system.
操作系统仿真层提供 lwip 代码和底层操作系统内核之间的通用接口。 总体思路是将 lwip 移植到新体系结构中只需对少量头文件和对新sys_arch实现进行小改动即可,且可以不依赖任何底层操作系统的sys_arch实现。
the sys_arch provides semaphores, mailboxes and mutexes to lwip . for the full lwip functionality, multiple threads support can be implemented in the sys_arch, but this is not required for the basic lwip functionality. timer scheduling is implemented in lwip , but can be implemented by the sys_arch port ( lwip _timers_custom==1).
sys_arch为 lwip 提供信号量,邮箱和互斥量。 对于完整的 lwip 功能,可以在sys_arch中实现多线程支持,但对于基本的 lwip 功能来说,这不是必需的。 定时器调度在 lwip 中实现,但可以通过sys_arch( lwip _timers_custom == 1)实现。
in addition to the source file providing the functionality of sys_arch,the os emulation layer must provide several header files defining macros used throughout lwip . the files required and the macros they must define are listed below the sys_arch description.
除了提供sys_arch功能的源文件之外,操作系统仿真层还必须提供几个用于定义整个 lwip 中使用的宏的头文件。 所需的文件和宏必须安装如下列出的描述进行定义。
semaphores can be either counting or binary - lwip works with both kinds. mailboxes should be implemented as a queue which allows multiple messages to be posted (implementing as a rendez-vous point where only one message can be posted at a time can have a highly negative impact on performance). a message in a mailbox is just a pointer, nothing more.
信号量可以是计数或二进制 —— lwip 可以同时使用这两种类型。 邮箱应该作为允许发布多个邮件的队列来实现(作为一次只能发布一条邮件的rendez-vous点,可能会对性能产生很大的负面影响)。 邮箱中的消息只是一个指针,仅此而已。
semaphores are represented by the type “sys_sem_t” which is typedef’d in the sys_arch.h file. mailboxes are equivalently represented by the type “sys_mbox_t”. mutexes are represented by the type “sys_mutex_t”. lwip does not place any restrictions on how these types are represented internally.
信号量由sys_arch.h文件中typedef的类型“sys_sem_t”表示。 邮箱等同于“sys_mbox_t”类型。 互斥体由“sys_mutex_t”类型表示。 lwip 并没有对这些类型如何在内部表现出任何限制。
例如,在使用freertos时移植时,sys_arch.h文件中有如下语句
since lwip 1.4.0, semaphore, mutexes and mailbox functions are prototyped in a way that allows both using pointers or actual os structures to be used. this way, memory required for such types can be either allocated in place (globally or on the stack) or on the heap (allocated internally in the "_new()" functions).
从 lwip 1.4.0版本开始,信号量,互斥和邮箱功能的原型方式允许使用指针或实际的os结构体。 这样,这些类型所需的内存可以在使用的地方分配(全局或堆栈)或在堆上(在“ _ new()”函数内部分配)。
该部分主要指需要实现的各函数,通常做法为建立一个名为sys_arch.c的文件,在其中实现以下函数:
the following functions must be implemented by the sys_arch: 以下函数必须由sys_arch实现:
- void sys_init(void):is called to initialize the sys_arch layer.被调用来初始化sys_arch层。
- err_t sys_sem_new(sys_sem_t *sem, u8_t count):creates a new semaphore. the semaphore is allocated to the memory that ‘sem’ points to (which can be both a pointer or the actual os structure).创建一个新的信号量。信号量分配给’sem’指向的内存(可以是指针或实际的os结构)。
the “count” argument specifies the initial state of the semaphore (which iseither 0 or 1).“count”参数指定信号的初始状态(可以是0或1)。
if the semaphore has been created, err_ok should be returned. returning any other error will provide a hint what went wrong, but except for assertions, no real error handling is implemented. 如果信号量已经创建,则应返回err_ok。 返回任何其他错误将提供一个出了什么问题的提示,但除了断言之外,没有实现真正的错误处理。 - void sys_sem_free(sys_sem_t *sem):deallocates a semaphore.释放一个信号量
- void sys_sem_signal(sys_sem_t *sem):signals a semaphore. 指示信号量
- u32_t sys_arch_sem_wait(sys_sem_t *sem, u32_t timeout):blocks the thread while waiting for the semaphore to be signaled. if the “timeout” argument is non-zero, the thread should only be blocked for the specified time (measured in milliseconds). if the “timeout” argument is zero, the thread should be blocked until the semaphore is signalled.在等待信号量发出信号时阻塞线程。 如果“超时”参数不为零,则线程只应在指定时间内被阻塞(以毫秒为单位)。 如果“超时”参数为零,则线程应被阻塞,直到信号被发信号。
if the timeout argument is non-zero, the return value is the number of milliseconds spent waiting for the semaphore to be signaled. if the semaphore wasn’t signaled within the specified time, the return value is sys_arch_timeout. if the thread didn’t have to wait for the semaphore(i.e., it was already signaled), the function may return zero.如果超时参数非零,则返回值是等待信号量发送信号所用的毫秒数。 如果在指定时间内没有发出信号量,则返回值为sys_arch_timeout。 如果线程不必等待信号量(即它已经被发信号),该函数可能返回零。
notice that lwip implements a function with a similar name, sys_sem_wait(), that uses the sys_arch_sem_wait() function.请注意, lwip 实现了一个具有类似名称的函数sys_sem_wait(),该函数使用了sys_arch_sem_wait() 函数。 - int sys_sem_valid(sys_sem_t *sem): returns 1 if the semaphore is valid, 0 if it is not valid.如果信号量有效返回1,如果无效则返回0。
when using pointers, a simple way is to check the pointer for != null.当使用指针时,一个简单的方法是检查指针 != null。
when directly using os structures, implementing this may be more complex.当直接使用os结构时,实现这可能会更复杂。
this may also be a define, in which case the function is not prototyped. 这也可能是一个定义,在这种情况下,函数不是原型的。 - void sys_sem_set_invalid(sys_sem_t *sem): invalidate a semaphore so that sys_sem_valid() returns 0. 使信号量失效,以便sys_sem_valid() 返回0。
attention: this does not mean that the semaphore shall be deallocated:sys_sem_free() is always called before calling this function! 注意:这并不意味着信号量应该被释放:在调用这个函数之前总是要调用sys_sem_free()!
this may also be a define, in which case the function is not prototyped. 这也可能是一个定义,在这种情况下,函数不是原型的。 - void sys_mutex_new(sys_mutex_t *mutex):creates a new mutex. the mutex is allocated to the memory that ‘mutex’ points to (which can be both a pointer or the actual os structure).创建一个新的互斥体。 互斥量被分配给’互斥’指向的内存(可以是指针或实际的操作系统结构)。
if the mutex has been created, err_ok should be returned. returning any other error will provide a hint what went wrong, but except for assertions, no real error handling is implemented.如果该互斥体已创建,则应返回err_ok。 返回任何其他错误将提供一个出了什么问题的提示,但除了断言之外,没有实现真正的错误处理。 - void sys_mutex_free(sys_mutex_t *mutex): deallocates a mutex.释放一个互斥信号量
- void sys_mutex_lock(sys_mutex_t *mutex):blocks the thread until the mutex can be grabbed.阻塞线程直到可以获取到互斥信号量。
- void sys_mutex_unlock(sys_mutex_t *mutex):releases the mutex previously locked through ‘sys_mutex_lock()’.释放先前通过sys_mutex_lock()锁定的互斥锁。
- void sys_mutex_valid(sys_mutex_t *mutex): returns 1 if the mutes is valid, 0 if it is not valid.如果互斥信号量有效则返回1,如果无效则返回0
when using pointers, a simple way is to check the pointer for != null.当使用指针时,一个简单的方法是检查指针 != null。
when directly using os structures, implementing this may be more complex.当直接使用os结构时,实现这可能会更复杂。
this may also be a define, in which case the function is not prototyped.这也可能是一个定义,在这种情况下,函数不是原型的。 - void sys_mutex_set_invalid(sys_mutex_t *mutex):invalidate a mutex so that sys_mutex_valid() returns 0. 使互斥体失效,以便sys_mutex_valid()返回0。
attention: this does not mean that the mutex shall be deallocated:sys_mutex_free() is always called before calling this function!这并不意味着该互斥体将被释放:在调用此函数之前总是调用sys_mutex_free()!
this may also be a define, in which case the function is not prototyped.这也可能是一个定义,在这种情况下,函数不是原型的。 - err_t sys_mbox_new(sys_mbox_t *mbox, int size):creates an empty mailbox for maximum “size” elements. elements stored in mailboxes are pointers. you have to define macros “_mbox_size” in your lwip opts.h, or ignore this parameter in your implementation and use a default size.创建最多包含size个元素的控邮箱。元素的指针存储在邮箱中。必须在lwip opts.h中定义宏值_mbox_size,或者忽略该参数,已使用默认大小。
if the mailbox has been created, err_ok should be returned. returning any other error will provide a hint what went wrong, but except for assertions,no real error handling is implemented.如果邮箱已被创建,则返回err_ok。返回任何其他错误将提供一个出了什么问题的提示,但除了断言之外,没有实现真正的错误处理。 - void sys_mbox_free(sys_mbox_t *mbox):deallocates a mailbox. if there are messages still present in the mailbox when the mailbox is deallocated, it is an indication of a programming error in lwip and the developer should be notified.
- void sys_mbox_post(sys_mbox_t *mbox, void *msg):posts the “msg” to the mailbox. this function have to block until the “msg” is really posted.
- err_t sys_mbox_trypost(sys_mbox_t *mbox, void *msg):try to post the “msg” to the mailbox. returns err_mem if this one is full, else, err_ok if the “msg” is posted.
- u32_t sys_arch_mbox_fetch(sys_mbox_t *mbox, void **msg, u32_t timeout):blocks the thread until a message arrives in the mailbox, but does not block the thread longer than “timeout” milliseconds (similar to the sys_arch_sem_wait() function). if “timeout” is 0, the thread should be blocked until a message arrives. the “msg” argument is a result parameter that is set by the function (i.e., by doing “*msg =ptr”). the “msg” parameter maybe null to indicate that the message should be dropped.
the return values are the same as for the sys_arch_sem_wait() function:number of milliseconds spent waiting or sys_arch_timeout if there was a timeout.
note that a function with a similar name, sys_mbox_fetch(), is implemented by lwip . - u32_t sys_arch_mbox_tryfetch(sys_mbox_t *mbox, void **msg):this is similar to sys_arch_mbox_fetch, however if a message is not present in the mailbox, it immediately returns with the code sys_mbox_empty. on success 0 is returned.
to allow for efficient implementations, this can be defined as a
function-like macro in sys_arch.h instead of a normal function. for example, a naive implementation could be:
#define sys_arch_mbox_tryfetch(mbox,msg)
sys_arch_mbox_fetch(mbox,msg,1)
although this would introduce unnecessary delays. - int sys_mbox_valid(sys_mbox_t *mbox):returns 1 if the mailbox is valid, 0 if it is not valid.
when using pointers, a simple way is to check the pointer for != null.
when directly using os structures, implementing this may be more complex.
this may also be a define, in which case the function is not prototyped. - void sys_mbox_set_invalid(sys_mbox_t *mbox):invalidate a mailbox so that sys_mbox_valid() returns 0.
attention: this does not mean that the mailbox shall be deallocated:sys_mbox_free() is always called before calling this function!
this may also be a define, in which case the function is not prototyped.
if threads are supported by the underlying operating system and if such functionality is needed in lwip , the following function will have to be implemented as well:
- sys_thread_t sys_thread_new(char *name, void (* thread)(void *arg), void *arg, int stacksize, int prio):starts a new thread named “name” with priority “prio” that will begin its execution in the function “thread()”. the “arg” argument will be passed as an argument to the thread() function. the stack size to used for this thread is the “stacksize” parameter. the id of the new thread is returned. both the id and the priority are system dependent.
when lwip is used from more than one context (e.g. from multiple threads or from main-loop and from interrupts), the sys_lightweight_prot protection should be enabled!
- sys_prot_t sys_arch_protect(void):this optional function does a “fast” critical region protection and returns the previous protection level. this function is only called during very short critical regions. an embedded system which supports isr-based drivers might want to implement this function by disabling interrupts. task-based systems might want to implement this by using a mutex or disabling tasking. this function should support recursive calls from the same task or interrupt. in other words, sys_arch_protect() could be called while already protected. in that case the return value indicates that it is already protected.
sys_arch_protect() is only required if your port is supporting an operating system. - void sys_arch_unprotect(sys_prot_t pval):this optional function does a “fast” set of critical region protection to the value specified by pval. see the documentation for sys_arch_protect() for more information. this function is only required if your port is supporting an operating system.
for some configurations, you also need:
- u32_t sys_now(void):this optional function returns the current time in milliseconds (don’t care for wraparound, this is only used for time diffs).
not implementing this function means you cannot use some modules (e.g. tcp timestamps, internal timeouts for no_sys==1).
be careful with using mem_malloc() in sys_arch. when malloc() refers to mem_malloc() you can run into a circular function call problem. in mem.c mem_init() tries to allcate a semaphore using mem_malloc, which of course can’t be performed when sys_arch uses mem_malloc.
通常做法为建立一个名为cc.h的文件,在其中定义以下类型及宏值:
architecture environment, some compiler specific, some environment specific (probably should move env stuff to sys_arch.h.)定义一些编译器特定的数据类型和对其方式。
(1)typedefs for the types used by lwip -
u8_t, s8_t, u16_t, s16_t, u32_t, s32_t, mem_ptr_t
(2)compiler hints for packing lwip 's structures
- pack_struct_field(x)
- pack_struct_struct
- pack_struct_begin
- pack_struct_end
(3)platform specific diagnostic output
- lwip _platform_diag(x) - non-fatal, print a message.
- lwip _platform_assert(x) - fatal, print message and abandon execution.
- portability defines for printf formatters:
u16_f, s16_f, x16_f, u32_f, s32_f, x32_f, szt_f
(4)“lightweight” synchronization mechanisms
- sys_arch_decl_protect(x) - declare a protection state variable.
- sys_arch_protect(x) - enter protection mode.
- sys_arch_unprotect(x) - leave protection mode.
(5)if the compiler does not provide memset() this file must include a definition of it, or include a file which defines it.如果编译系统中没有包含 memset(),则需要自己包含该函数
(6)this file must either include a system-local errno.h which defines the standard nix error codes, or it should #define lwip _provide_errno to make lwip /arch.h define the codes which are used throughout.该文件还必须包含本地系统的 errno.h 文件(该文件包含了标准nix错误码),或者可以定义宏值#define lwip _provide_errno(直接在 lwip opts.h中定义即可)来使用 lwip 自己的这个头文件。
示例文件如下(系统为freertos时):
architecture specific performance measurement.架构特定的性能测量。没搞明白这句啥意思!这是 lwip 的作者测试用的?
measurement calls made throughout lwip , these can be defined to nothing.
- perf_start - start measuring something.
- perf_stop(x) - stop measuring something, and record the result.
通常做法为建立一个名为perf.h的文件,在其中定义以上类型及宏值,但是这两个宏值具体干啥用还没搞清楚!示例文件如下:
/** 尊龙游戏旗舰厅官网 copyright (c) 2001-2003 swedish institute of computer science.* all rights reserved. * * redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met:** 1. redistributions of source code must retain the above 尊龙游戏旗舰厅官网 copyright notice,* this list of conditions and the following disclaimer.* 2. redistributions in binary form must reproduce the above 尊龙游戏旗舰厅官网 copyright notice,* this list of conditions and the following disclaimer in the documentation* and/or other materials provided with the distribution.* 3. the name of the author may not be used to endorse or promote products* derived from this software without specific prior written permission. ** this software is provided by the author ``as is'' and any express or implied * warranties, including, but not limited to, the implied warranties of * merchantability and fitness for a particular purpose are disclaimed. in no event * shall the author be liable for any direct, indirect, incidental, special, * exemplary, or consequential damages (including, but not limited to, procurement * of substitute goods or services; loss of use, data, or profits; or business * interruption) however caused and on any theory of liability, whether in * contract, strict liability, or tort (including negligence or otherwise) arising * in any way out of the use of this software, even if advised of the possibility * of such damage.** this file is part of the lwip tcp/ip stack.* * author: adam dunkels该文件主要是针对上面sys_arch.c的配套头文件,里面有各种函数的声明。
(1)arch dependent types for the following objects:sys_sem_t, sys_mbox_t, sys_thread_t, and, optionally:sys_prot_t需要定义以上这些类型(sys_prot_t是可选的)
(2)defines to set vars of sys_mbox_t and sys_sem_t to null.将以下两个宏值定位null
- sys_mbox_null null
- sys_sem_null null
示例文件如下(系统为freertos时):
/** 尊龙游戏旗舰厅官网 copyright (c) 2001-2003 swedish institute of computer science.* all rights reserved.** redistribution and use in source and binary forms, with or without modification,* are permitted provided that the following conditions are met:** 1. redistributions of source code must retain the above 尊龙游戏旗舰厅官网 copyright notice,* this list of conditions and the following disclaimer.* 2. redistributions in binary form must reproduce the above 尊龙游戏旗舰厅官网 copyright notice,* this list of conditions and the following disclaimer in the documentation* and/or other materials provided with the distribution.* 3. the name of the author may not be used to endorse or promote products* derived from this software without specific prior written permission.** this software is provided by the author ``as is'' and any express or implied* warranties, including, but not limited to, the implied warranties of* merchantability and fitness for a particular purpose are disclaimed. in no event* shall the author be liable for any direct, indirect, incidental, special,* exemplary, or consequential damages (including, but not limited to, procurement* of substitute goods or services; loss of use, data, or profits; or business* interruption) however caused and on any theory of liability, whether in* contract, strict liability, or tort (including negligence or otherwise) arising* in any way out of the use of this software, even if advised of the possibility* of such damage.** this file is part of the lwip tcp/ip stack.** author: adam dunkels例如cpu.hwindows下需要的bpstruct.h和epstruct.h
最终需要在sys_arch中的文件如下图:
总结
以上是尊龙游戏旗舰厅官网为你收集整理的lwip 之三 操作系统隔离接口 sys_arch的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: lwip 之二 网络接口 netif(e
- 下一篇: