1 /*
2  * Demo for libcoap on lwIP
3  *
4  * partially copied from lwip-contrib/ports/unix/proj/minimal/main.c
5  *
6  *
7  * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without modification,
11  * are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
24  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  *
32  * Author: Adam Dunkels <adam@sics.se>
33  * RT timer modifications by Christiaan Simons
34  * lwip adaptions: chrysn <chrysn@fsfe.org>
35  * also, https://savannah.nongnu.org/bugs/?40245 was applied */
36 
37 #include <lwip/init.h>
38 #include <lwip/timers.h>
39 
40 #include <mintapif.h>
41 #include <netif/etharp.h>
42 
43 #include "timer.h"
44 #include <signal.h>
45 
46 #include "server-coap.h"
47 
48 static ip_addr_t ipaddr, netmask, gw;
49 
50 int
main(int argc,char ** argv)51 main(int argc, char **argv)
52 {
53 	struct netif netif;
54 	sigset_t mask, oldmask, empty;
55 
56 	/* startup defaults (may be overridden by one or more opts) */
57 	IP4_ADDR(&gw, 192,168,0,1);
58 	IP4_ADDR(&ipaddr, 192,168,0,2);
59 	IP4_ADDR(&netmask, 255,255,255,0);
60 
61 	lwip_init();
62 
63 	printf("TCP/IP initialized.\n");
64 
65 	netif_add(&netif, &ipaddr, &netmask, &gw, NULL, mintapif_init, ethernet_input);
66 	netif.flags |= NETIF_FLAG_ETHARP;
67 	netif_set_default(&netif);
68 	netif_set_up(&netif);
69 #if LWIP_IPV6
70 	netif_create_ip6_linklocal_address(&netif, 1);
71 #endif
72 
73 	timer_init();
74 
75 	sys_timeouts_init();
76 
77 	/* start applications here */
78 
79 	server_coap_init();
80 
81 	printf("Applications started.\n");
82 
83 
84 	while (1) {
85 
86 		/* poll for input packet and ensure
87 		 select() or read() arn't interrupted */
88 		sigemptyset(&mask);
89 		sigaddset(&mask, SIGALRM);
90 		sigprocmask(SIG_BLOCK, &mask, &oldmask);
91 
92 		/* start of critical section,
93 		 poll netif, pass packet to lwIP */
94 		if (mintapif_select(&netif) > 0)
95 		{
96 			/* work, immediatly end critical section
97 			   hoping lwIP ended quickly ... */
98 			sigprocmask(SIG_SETMASK, &oldmask, NULL);
99 		}
100 		else
101 		{
102 			/* no work, wait a little (10 msec) for SIGALRM */
103 			  sigemptyset(&empty);
104 			  sigsuspend(&empty);
105 			/* ... end critical section */
106 			  sigprocmask(SIG_SETMASK, &oldmask, NULL);
107 		}
108 
109 		sys_check_timeouts();
110 
111 		server_coap_poll();
112 	}
113 
114 	return 0;
115 }
116