mirror of
https://github.com/ep00ch/lwip-contrib-mac.git
synced 2024-06-12 01:29:28 +00:00
Changed broken select() timeout based timers into setitimer() timers.
This commit is contained in:
parent
67ff807e63
commit
3ee29c973e
|
@ -68,7 +68,7 @@ LWIPFILESW=$(wildcard $(LWIPFILES))
|
|||
LWIPOBJS=$(notdir $(LWIPFILESW:.c=.o))
|
||||
|
||||
# APPFILES
|
||||
APPFILES=echo.c
|
||||
APPFILES=echo.c timer.c
|
||||
|
||||
LWIPLIB=liblwip4.a
|
||||
APPLIB=liblwipapps.a
|
||||
|
|
|
@ -27,7 +27,7 @@
|
|||
* This file is part of the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Adam Dunkels <adam@sics.se>
|
||||
*
|
||||
* RT timer modifications by Christiaan Simons
|
||||
*/
|
||||
|
||||
#include "lwip/debug.h"
|
||||
|
@ -46,6 +46,9 @@
|
|||
#include "mintapif.h"
|
||||
#include "netif/etharp.h"
|
||||
|
||||
#include "timer.h"
|
||||
#include <signal.h>
|
||||
|
||||
#include "echo.h"
|
||||
|
||||
int
|
||||
|
@ -53,8 +56,7 @@ main(int argc, char **argv)
|
|||
{
|
||||
struct ip_addr ipaddr, netmask, gw;
|
||||
struct netif netif;
|
||||
static u8_t tcp_slow_timer = 0;
|
||||
static u8_t arp_timer = 0;
|
||||
sigset_t mask, oldmask, empty;
|
||||
|
||||
#ifdef PERF
|
||||
perf_init("/tmp/minimal.perf");
|
||||
|
@ -84,36 +86,61 @@ main(int argc, char **argv)
|
|||
|
||||
echo_init();
|
||||
|
||||
timer_init();
|
||||
timer_set_interval(TIMER_EVT_ETHARPTMR,2000);
|
||||
timer_set_interval(TIMER_EVT_TCPFASTTMR, TCP_FAST_INTERVAL / 10);
|
||||
timer_set_interval(TIMER_EVT_TCPSLOWTMR, TCP_SLOW_INTERVAL / 10);
|
||||
#if IP_REASSEMBLY
|
||||
timer_set_interval(TIMER_EVT_IPREASSTMR,100);
|
||||
#endif
|
||||
|
||||
printf("Applications started.\n");
|
||||
|
||||
|
||||
while (1) {
|
||||
|
||||
if (mintapif_wait(&netif, TCP_FAST_INTERVAL) == MINTAPIF_TIMEOUT) {
|
||||
/* poll for input packet and ensure
|
||||
select() or read() arn't interrupted */
|
||||
sigemptyset(&mask);
|
||||
sigaddset(&mask, SIGALRM);
|
||||
sigprocmask(SIG_BLOCK, &mask, &oldmask);
|
||||
|
||||
tcp_fasttmr();
|
||||
|
||||
if (tcp_slow_timer == 4) {
|
||||
tcp_slow_timer = 0;
|
||||
tcp_slowtmr();
|
||||
/* CSi these timers are unrelated, only happen to have a similar period.
|
||||
TCP will avoid fragmentation. IP reassembly only makes sense for UDP/ICMP. */
|
||||
#if IP_REASSEMBLY
|
||||
ip_reass_tmr();
|
||||
#endif
|
||||
} else {
|
||||
tcp_slow_timer++;
|
||||
/* start of critical section,
|
||||
poll netif, pass packet to lwIP */
|
||||
if (mintapif_select(&netif) > 0)
|
||||
{
|
||||
/* work, immediatly end critical section
|
||||
hoping lwIP ended quickly ... */
|
||||
sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
||||
}
|
||||
|
||||
if (arp_timer == 20) {
|
||||
arp_timer = 0;
|
||||
etharp_tmr();
|
||||
} else {
|
||||
arp_timer++;
|
||||
else
|
||||
{
|
||||
/* no work, wait a little (10 msec) for SIGALRM */
|
||||
sigemptyset(&empty);
|
||||
sigsuspend(&empty);
|
||||
/* ... end critical section */
|
||||
sigprocmask(SIG_SETMASK, &oldmask, NULL);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(timer_testclr_evt(TIMER_EVT_TCPFASTTMR))
|
||||
{
|
||||
tcp_fasttmr();
|
||||
}
|
||||
if(timer_testclr_evt(TIMER_EVT_TCPSLOWTMR))
|
||||
{
|
||||
tcp_slowtmr();
|
||||
}
|
||||
#if IP_REASSEMBLY
|
||||
if(timer_testclr_evt(TIMER_EVT_IPREASSTMR))
|
||||
{
|
||||
ip_reass_tmr();
|
||||
}
|
||||
#endif
|
||||
if(timer_testclr_evt(TIMER_EVT_ETHARPTMR))
|
||||
{
|
||||
etharp_tmr();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -358,3 +358,26 @@ mintapif_wait(struct netif *netif, u16_t time)
|
|||
|
||||
return MINTAPIF_PACKET;
|
||||
}
|
||||
|
||||
int
|
||||
mintapif_select(struct netif *netif)
|
||||
{
|
||||
fd_set fdset;
|
||||
int ret;
|
||||
struct timeval tv;
|
||||
struct mintapif *mintapif;
|
||||
|
||||
mintapif = netif->state;
|
||||
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 0; /* usec_to; */
|
||||
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(mintapif->fd, &fdset);
|
||||
|
||||
ret = select(mintapif->fd + 1, &fdset, NULL, NULL, &tv);
|
||||
if (ret > 0) {
|
||||
mintapif_input(netif);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -40,6 +40,6 @@ enum mintapif_signal {
|
|||
};
|
||||
|
||||
err_t mintapif_init(struct netif *netif);
|
||||
enum mintapif_signal mintapif_wait(struct netif *netif, u16_t time);
|
||||
int mintapif_select(struct netif *netif);
|
||||
|
||||
#endif /* __MINTAPIF_H__ */
|
||||
|
|
158
ports/unix/proj/minimal/timer.c
Normal file
158
ports/unix/proj/minimal/timer.c
Normal file
|
@ -0,0 +1,158 @@
|
|||
/*
|
||||
* Copyright (c) 2006 Christiaan Simons.
|
||||
* 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 and a contribution to the lwIP TCP/IP stack.
|
||||
*
|
||||
* Author: Christiaan Simons <christiaan.simons@gmail.com>
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Simple pre-allocated interval (reload) timers.
|
||||
* @see timer.h
|
||||
*/
|
||||
|
||||
#include <signal.h>
|
||||
#include <sys/time.h>
|
||||
#include "timer.h"
|
||||
|
||||
static struct itimerval tmr;
|
||||
|
||||
struct itmr
|
||||
{
|
||||
volatile unsigned int interval;
|
||||
volatile unsigned int cnt;
|
||||
volatile unsigned char event;
|
||||
};
|
||||
|
||||
static struct itmr timers[TIMER_NUM];
|
||||
|
||||
|
||||
void sigalarm_handler(int sig);
|
||||
|
||||
/**
|
||||
* Initializes interval timers.
|
||||
*/
|
||||
void
|
||||
timer_init(void)
|
||||
{
|
||||
unsigned char i;
|
||||
struct itmr *tp;
|
||||
|
||||
tp = &timers[TIMER_NUM-1];
|
||||
for(i = TIMER_NUM; i > 0; i--)
|
||||
{
|
||||
tp->event = 0;
|
||||
tp->interval = 0;
|
||||
tp->cnt = 0;
|
||||
tp--;
|
||||
}
|
||||
|
||||
signal(SIGALRM,sigalarm_handler);
|
||||
|
||||
/* timer reload is in 10msec steps */
|
||||
tmr.it_interval.tv_sec = 0;
|
||||
tmr.it_interval.tv_usec = 10000;
|
||||
/* set to half period (enables timer) */
|
||||
tmr.it_value.tv_sec = 0;
|
||||
tmr.it_value.tv_usec = 5000;
|
||||
|
||||
setitimer(ITIMER_REAL,&tmr,NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configures timer.
|
||||
*
|
||||
* @param tmr the timer number from timer.h
|
||||
* @param interval when > 0 enables this timer, 0 disables.
|
||||
*/
|
||||
void
|
||||
timer_set_interval(unsigned char tmr, unsigned int interval)
|
||||
{
|
||||
if (tmr < TIMER_NUM)
|
||||
{
|
||||
timers[tmr].interval = interval;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns timer event and restarts timer.
|
||||
*/
|
||||
unsigned char
|
||||
timer_testclr_evt(unsigned char tmr)
|
||||
{
|
||||
if (tmr < TIMER_NUM)
|
||||
{
|
||||
unsigned char evt;
|
||||
struct itmr *tp;
|
||||
|
||||
tp = &timers[tmr];
|
||||
|
||||
evt = tp->event;
|
||||
if (tp->event != 0)
|
||||
{
|
||||
tp->event = 0;
|
||||
tp->cnt = tp->interval;
|
||||
}
|
||||
return evt;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* interrupting (!) sigalarm handler
|
||||
*/
|
||||
void
|
||||
sigalarm_handler(int sig)
|
||||
{
|
||||
unsigned char i;
|
||||
struct itmr *tp;
|
||||
|
||||
tp = &timers[TIMER_NUM-1];
|
||||
for(i = TIMER_NUM; i > 0; i--)
|
||||
{
|
||||
if (tp->interval != 0)
|
||||
{
|
||||
/* timer is running */
|
||||
if (tp->cnt == 0)
|
||||
{
|
||||
/* timer expired */
|
||||
tp->event |= 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* timer ticking */
|
||||
tp->cnt--;
|
||||
}
|
||||
}
|
||||
tp--;
|
||||
}
|
||||
}
|
||||
|
14
ports/unix/proj/minimal/timer.h
Normal file
14
ports/unix/proj/minimal/timer.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef _TIMER_H_
|
||||
#define _TIMER_H_
|
||||
|
||||
#define TIMER_EVT_ETHARPTMR 0
|
||||
#define TIMER_EVT_TCPFASTTMR 1
|
||||
#define TIMER_EVT_TCPSLOWTMR 2
|
||||
#define TIMER_EVT_IPREASSTMR 3
|
||||
#define TIMER_NUM 4
|
||||
|
||||
void timer_init(void);
|
||||
void timer_set_interval(unsigned char tmr, unsigned int interval);
|
||||
unsigned char timer_testclr_evt(unsigned char tmr);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user