/* * Copyright (c) 2001-2004 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 * */ #ifndef __LWIP_SYS_H__ #define __LWIP_SYS_H__ #include "arch/cc.h" #include "lwip/opt.h" #ifdef __cplusplus extern "C" { #endif #if NO_SYS /* For a totally minimal and standalone system, we provide null definitions of the sys_ functions. */ typedef u8_t sys_sem_t; typedef u8_t sys_mbox_t; struct sys_timer {u8_t dummy;}; #define sys_init() #define sys_timer_create(m,h,a) #define sys_timer_remove(m,a) #define sys_sem_new(c) c #define sys_sem_signal(s) #define sys_sem_wait(s) #define sys_sem_wait_timeout(s, t) 0 #define sys_sem_free(s) #define sys_mbox_new() 0 #define sys_mbox_fetch_timeout(m,d,t) #define sys_mbox_fetch(m,d) #define sys_mbox_tryfetch(m,d) #define sys_mbox_post(m,d) #define sys_mbox_free(m) #define sys_thread_new(t,a,p) #else /* NO_SYS */ /** Return code for timeouts from sys_arch_mbox_fetch and sys_arch_sem_wait */ #define SYS_ARCH_TIMEOUT ((lwip_timediff_t)-1) /* sys_mbox_tryfetch returns SYS_MBOX_EMPTY if appropriate. * For now we use the same magic value, but we allow this to change in future. */ #define SYS_MBOX_EMPTY SYS_ARCH_TIMEOUT /* portable way to calculate time */ #define TIMESTAMP_DIFF(t1, t2) ((lwip_timediff_t)((t1) - (t2))) #define TIMESTAMP_ADD(timestamp, timeout) ((lwip_timestamp_t)((timestamp)+(timeout))) struct lwip_thread_local_storage { struct sys_timer *first_timer; }; /* now we can include sys_arch.h and functions can be defined inline there. */ #include "arch/sys_arch.h" #ifdef LWIP_DEBUG #define SYS_CHECK_TIME_CONVERSION_CORRECT() \ do {\ if(SYS_ARCH_MS_TO_JIFFIES(SYS_ARCH_JIFFIES_TO_MS(SYS_ARCH_MAX_JIFFIES)) != SYS_ARCH_MAX_JIFFIES) { \ LWIP_ASSERT("please check your SYS_ARCH_*_TO_* or SYS_ARCH_MAX_JIFFIES macros!", 0); \ } \ }while(0) #else #define SYS_CHECK_TIME_CONVERSION_CORRECT() #endif typedef void (* sys_timer_handler)(void *arg); struct sys_timer { struct sys_timer *next; lwip_timestamp_t time_expire; sys_timer_handler h; void *arg; }; /* Get the struct lwip_thread_local_storage of the current thread */ struct lwip_thread_local_storage *sys_arch_get_threadlocalstorage(void); /* sys_init() must be called before anthing else. */ void sys_init(void); /* Time functions. */ #ifndef sys_msleep void sys_msleep(lwip_timediff_t ms); /* only has a (close to) 1 jiffy resolution. */ #endif /* sys_msleep */ #ifndef sys_jiffies lwip_timestamp_t sys_jiffies(void); /* system timer ticks since power up. */ #endif /* sys_jiffies */ /* Timer functions */ /* sys_timer_create(): * Schedule a timer a specified amount of jiffies or milliseconds in the * future. When the timeout occurs, the specified timer handler will * be called. The handler will be passed the "arg" argument when called. */ void sys_timer_create_jiffies(lwip_timediff_t msecs, sys_timer_handler h, void *arg); /* create timer using ms instead of jiffies (slow: requires calculation) */ #define sys_timer_create(msec, h, arg) \ sys_timer_create_jiffies(SYS_ARCH_MS_TO_JIFFIES((msec)), h, arg) void sys_timer_remove(sys_timer_handler h, void *arg); void sys_check_timers(void); /* Semaphore functions. */ sys_sem_t sys_sem_new(u8_t count); void sys_sem_free(sys_sem_t sem); #ifndef sys_sem_signal /* Allow port to override with a macro */ void sys_sem_signal(sys_sem_t sem); #endif /* sys_arch_sem_wait(): * @timeout_jiffies = 0 means wait forever * @return value = SYS_ARCH_TIMEOUT: timeout_jiffies passed without * aquiring semaphore * @return value = 0: aquired semaphore without waiting * @return value > 0: number of jiffies spent waiting for semaphore */ #ifndef sys_arch_sem_wait /* Allow port to override with a macro */ lwip_timediff_t sys_arch_sem_wait(sys_sem_t sem, lwip_timediff_t timeout_jiffies); #endif /* since no timeout processing is done in sys_sem_wait & sys_sem_wait_timeout, define them directly to sys_arch_sem_wait */ #define sys_sem_wait(sem) sys_arch_sem_wait(sem, 0) /* timeout = 0 means wait forever */ #define sys_sem_wait_timeout(sem, timeout) sys_arch_sem_wait(sem, timeout) /* Mailbox functions. */ sys_mbox_t sys_mbox_new(void); #ifndef sys_mbox_post /* Allow port to override with a macro */ void sys_mbox_post(sys_mbox_t mbox, void *msg); #endif /* timeout = 0 means wait forever */ #ifndef sys_arch_mbox_fetch /* Allow port to override with a macro */ lwip_timediff_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, lwip_timediff_t timeout); #endif #ifndef sys_arch_mbox_tryfetch /* Allow port to override with a macro */ lwip_timediff_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg); #endif /* For now, we map straight to sys_arch implementation. */ #define sys_mbox_tryfetch(mbox, msg) sys_arch_mbox_tryfetch(mbox, msg) void sys_mbox_free(sys_mbox_t mbox); #if LWIP_SO_RCVTIMEO /* timeout = 0 means wait forever */ void sys_mbox_fetch_timeout(sys_mbox_t mbox, void **msg, lwip_timediff_t timeout); #define sys_mbox_fetch(m,d) sys_mbox_fetch_timeout(m,d,0) #else void sys_mbox_fetch(sys_mbox_t mbox, void **msg); #endif /* LWIP_SO_RCVTIMEO */ /* Thread functions. */ sys_thread_t sys_thread_new(void (* thread)(void *arg), void *arg, int prio); #endif /* NO_SYS */ /* Critical Region Protection */ /* These functions must be implemented in the sys_arch.c file. In some implementations they can provide a more light-weight protection mechanism than using semaphores. Otherwise semaphores can be used for implementation */ #ifndef SYS_ARCH_PROTECT /** SYS_LIGHTWEIGHT_PROT * define SYS_LIGHTWEIGHT_PROT in lwipopts.h if you want inter-task protection * for certain critical regions during buffer allocation, deallocation and memory * allocation and deallocation. */ #if SYS_LIGHTWEIGHT_PROT /** SYS_ARCH_DECL_PROTECT * declare a protection variable. This macro will default to defining a variable of * type sys_prot_t. If a particular port needs a different implementation, then * this macro may be defined in sys_arch.h. */ #define SYS_ARCH_DECL_PROTECT(lev) sys_prot_t lev /** SYS_ARCH_PROTECT * Perform a "fast" protect. This could be implemented by * disabling interrupts for an embedded system or by using a semaphore or * mutex. The implementation should allow calling SYS_ARCH_PROTECT when * already protected. The old protection level is returned in the variable * "lev". This macro will default to calling the sys_arch_protect() function * which should be implemented in sys_arch.c. If a particular port needs a * different implementation, then this macro may be defined in sys_arch.h */ #define SYS_ARCH_PROTECT(lev) lev = sys_arch_protect() /** SYS_ARCH_UNPROTECT * Perform a "fast" set of the protection level to "lev". This could be * implemented by setting the interrupt level to "lev" within the MACRO or by * using a semaphore or mutex. This macro will default to calling the * sys_arch_unprotect() function which should be implemented in * sys_arch.c. If a particular port needs a different implementation, then * this macro may be defined in sys_arch.h */ #define SYS_ARCH_UNPROTECT(lev) sys_arch_unprotect(lev) sys_prot_t sys_arch_protect(void); void sys_arch_unprotect(sys_prot_t pval); #else #define SYS_ARCH_DECL_PROTECT(lev) #define SYS_ARCH_PROTECT(lev) #define SYS_ARCH_UNPROTECT(lev) #endif /* SYS_LIGHTWEIGHT_PROT */ #endif /* SYS_ARCH_PROTECT */ #ifdef __cplusplus } #endif #endif /* __LWIP_SYS_H__ */