From 1d453bbb4355237a5f74c934f2a08f656432dbe5 Mon Sep 17 00:00:00 2001
From: chrysn <chrysn@fsfe.org>
Date: Fri, 21 Feb 2014 00:19:19 +0100
Subject: [PATCH 2/2] further simplification on the minimal unix example

the old scheme of using SIGALRM was created after the previous
select-based version was deemed insufficient; however, that version was
based on the ticks of particular (tcp) timers back then.

this requires the newly introduced sys_timeouts_sleeptime function to be
present in lwip.
---
 ports/unix/proj/minimal/Makefile   |  2 +-
 ports/unix/proj/minimal/lwipopts.h | 10 ++++++
 ports/unix/proj/minimal/main.c     | 37 +++++--------------
 ports/unix/proj/minimal/mintapif.c | 45 +++--------------------
 ports/unix/proj/minimal/timer.c    | 74 --------------------------------------
 ports/unix/proj/minimal/timer.h    |  6 ----
 6 files changed, 24 insertions(+), 150 deletions(-)
 delete mode 100644 ports/unix/proj/minimal/timer.c
 delete mode 100644 ports/unix/proj/minimal/timer.h

diff --git a/ports/unix/proj/minimal/Makefile b/ports/unix/proj/minimal/Makefile
index 0f6f8d9..2db23a6 100644
--- a/ports/unix/proj/minimal/Makefile
+++ b/ports/unix/proj/minimal/Makefile
@@ -82,7 +82,7 @@ LWIPFILESW=$(wildcard $(LWIPFILES))
 LWIPOBJS=$(notdir $(LWIPFILESW:.c=.o))
 
 # APPFILES
-APPFILES=echo.c timer.c
+APPFILES=echo.c
 
 LWIPLIB=liblwip4.a
 APPLIB=liblwipapps.a
diff --git a/ports/unix/proj/minimal/lwipopts.h b/ports/unix/proj/minimal/lwipopts.h
index 859346c..7251824 100644
--- a/ports/unix/proj/minimal/lwipopts.h
+++ b/ports/unix/proj/minimal/lwipopts.h
@@ -122,6 +122,16 @@
 #define MEMP_NUM_ARP_QUEUE              2
 
 /**
+ * MEMP_NUM_SYS_TIMEOUT: the number of simulateously active timeouts.
+ * (requires NO_SYS==0)
+ * The default number of timeouts is calculated here for all enabled modules.
+ * The formula expects settings to be either '0' or '1'.
+ *
+ * To this default value, 1 was added for the snmp_increment timer.
+ */
+#define MEMP_NUM_SYS_TIMEOUT            (LWIP_TCP + IP_REASSEMBLY + LWIP_ARP + (2*LWIP_DHCP) + LWIP_AUTOIP + LWIP_IGMP + LWIP_DNS + PPP_SUPPORT + (LWIP_IPV6 ? (1 + LWIP_IPV6_REASS + LWIP_IPV6_MLD) : 0)) + 1
+
+/**
  * MEMP_NUM_NETBUF: the number of struct netbufs.
  * (only needed if you use the sequential API, like api_lib.c)
  */
diff --git a/ports/unix/proj/minimal/main.c b/ports/unix/proj/minimal/main.c
index 9b08ce5..e8acf92 100644
--- a/ports/unix/proj/minimal/main.c
+++ b/ports/unix/proj/minimal/main.c
@@ -40,6 +40,7 @@
 #include "lwip/mem.h"
 #include "lwip/memp.h"
 #include "lwip/sys.h"
+#include "lwip/timers.h"
 
 #include "lwip/stats.h"
 
@@ -51,9 +52,6 @@
 #include "mintapif.h"
 #include "netif/etharp.h"
 
-#include "timer.h"
-#include <signal.h>
-
 #include "echo.h"
 #include "private_mib.h"
 
@@ -104,11 +102,16 @@ void usage(void)
   }
 }
 
+void snmp_increment(void *arg)
+{
+  snmp_inc_sysuptime();
+  sys_timeout(10, snmp_increment, NULL);
+}
+
 int
 main(int argc, char **argv)
 {
   struct netif netif;
-  sigset_t mask, oldmask, empty;
   int ch;
   char ip_str[16] = {0}, nm_str[16] = {0}, gw_str[16] = {0};
 
@@ -191,35 +194,13 @@ main(int argc, char **argv)
 
   echo_init();
 
-  timer_init();
+  sys_timeout(10, snmp_increment, NULL);
   
   printf("Applications started.\n");
     
 
   while (1) {
-    
-      /* poll for input packet and ensure
-         select() or read() arn't interrupted */
-      sigemptyset(&mask);
-      sigaddset(&mask, SIGALRM);
-      sigprocmask(SIG_BLOCK, &mask, &oldmask);
-
-      /* 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);
-      }
-      else
-      {
-        /* no work, wait a little (10 msec) for SIGALRM */
-          sigemptyset(&empty);
-          sigsuspend(&empty);
-        /* ... end critical section */
-          sigprocmask(SIG_SETMASK, &oldmask, NULL);
-      }
+      mintapif_select(&netif);
 
       sys_check_timeouts();
   }
diff --git a/ports/unix/proj/minimal/mintapif.c b/ports/unix/proj/minimal/mintapif.c
index 9f18899..6a88b74 100644
--- a/ports/unix/proj/minimal/mintapif.c
+++ b/ports/unix/proj/minimal/mintapif.c
@@ -62,6 +62,7 @@
 #include "lwip/stats.h"
 #include "lwip/snmp.h"
 #include "lwip/mem.h"
+#include "lwip/timers.h"
 #include "netif/etharp.h"
 
 #include "mintapif.h"
@@ -310,45 +311,6 @@ mintapif_init(struct netif *netif)
   return ERR_OK;
 }
 /*-----------------------------------------------------------------------------------*/
-enum mintapif_signal
-mintapif_wait(struct netif *netif, u16_t time)
-{
-  fd_set fdset;
-  struct timeval tv, now;
-  struct timezone tz;
-  int ret;
-  struct mintapif *mintapif;
-
-  mintapif = netif->state;
-
-  while (1) {
-  
-    if (mintapif->lasttime >= (u32_t)time * 1000) {
-      mintapif->lasttime = 0;
-      return MINTAPIF_TIMEOUT;
-    }
-    
-    tv.tv_sec = 0;
-    tv.tv_usec = (u32_t)time * 1000 - mintapif->lasttime;
-    
-    
-    FD_ZERO(&fdset);
-    FD_SET(mintapif->fd, &fdset);
-    
-    gettimeofday(&now, &tz);
-    ret = select(mintapif->fd + 1, &fdset, NULL, NULL, &tv);
-    if (ret == 0) {
-      mintapif->lasttime = 0;    
-      return MINTAPIF_TIMEOUT;
-    } 
-    gettimeofday(&tv, &tz);
-    mintapif->lasttime += (tv.tv_sec - now.tv_sec) * 1000000 + (tv.tv_usec - now.tv_usec);
-
-    mintapif_input(netif);
-  }
-  
-  return MINTAPIF_PACKET;
-}
 
 int
 mintapif_select(struct netif *netif)
@@ -357,11 +319,12 @@ mintapif_select(struct netif *netif)
   int ret;
   struct timeval tv;
   struct mintapif *mintapif;
+  u32_t msecs = sys_timeouts_sleeptime();
 
   mintapif = netif->state;
 
-  tv.tv_sec = 0;
-  tv.tv_usec = 0; /* usec_to; */
+  tv.tv_sec = msecs / 1000;
+  tv.tv_usec = (msecs % 1000) * 1000;
   
   FD_ZERO(&fdset);
   FD_SET(mintapif->fd, &fdset);
diff --git a/ports/unix/proj/minimal/timer.c b/ports/unix/proj/minimal/timer.c
deleted file mode 100644
index 6a2e65f..0000000
--- a/ports/unix/proj/minimal/timer.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/*
- * 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@axon.tv>
- */
- 
-/** 
- * @file
- * Simple pre-allocated interval (reload) timers.
- * @see timer.h
- */
-
-#include <stddef.h>
-#include <signal.h>
-#include <sys/time.h>
-#include "timer.h"
-#include "lwip/snmp.h"
-
-static struct itimerval tmr;
-
-void sigalarm_handler(int sig);
-
-/**
- * Initializes interval timers.
- */
-void
-timer_init(void)
-{
-  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);
-}
-
-/**
- * interrupting (!) sigalarm handler
- */
-void
-sigalarm_handler(int sig)
-{
-  snmp_inc_sysuptime();
-}
-
diff --git a/ports/unix/proj/minimal/timer.h b/ports/unix/proj/minimal/timer.h
deleted file mode 100644
index 5e83147..0000000
--- a/ports/unix/proj/minimal/timer.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef _TIMER_H_
-#define _TIMER_H_
-
-void timer_init(void);
-
-#endif
-- 
1.9.0

