• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "server-coap.h"
38 
39 #include <lwip/init.h>
40 #include <lwip/timeouts.h>
41 
42 #include <netif/etharp.h>
43 #include <netif/tapif.h>
44 
45 #include <signal.h>
46 
47 #if LWIP_IPV4
48 static ip4_addr_t ipaddr, netmask, gw;
49 #endif /* LWIP_IPV4 */
50 
51 void
handle_sigint(int signum)52 handle_sigint(int signum) {
53   (void)signum;
54 
55   server_coap_finished();
56   printf("Server Application finished.\n");
57   exit(0);
58 }
59 
60 /*
61  * This function is called internally by coap_io_process() to check
62  * for input.
63  */
64 static int
wait_for_input(void * arg,uint32_t milli_secs)65 wait_for_input(void *arg, uint32_t milli_secs) {
66   struct netif *netif = (struct netif *)arg;
67   int ret;
68 
69   (void)milli_secs;
70   ret = tapif_select(netif);
71 
72   sys_check_timeouts();
73   return ret;
74 }
75 
76 int
main(int argc,char ** argv)77 main(int argc, char **argv) {
78   struct netif netif;
79 #ifndef _WIN32
80   struct sigaction sa;
81 #endif
82 
83   /* startup defaults (may be overridden by one or more opts). this is
84    * hard-coded v4 even in presence of v6, which does auto-discovery and
85    * should thus wind up with an address of fe80::12:34ff:fe56:78ab%tap0
86    * */
87 #if LWIP_IPV4
88   IP4_ADDR(&gw, 192,168,113,1);
89   IP4_ADDR(&ipaddr, 192,168,113,2);
90   IP4_ADDR(&netmask, 255,255,255,0);
91 #endif /* LWIP_IPV4 */
92 
93   lwip_init();
94 
95   printf("TCP/IP initialized.\n");
96 
97 #if LWIP_IPV4
98   netif_add(&netif, &ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input);
99 #endif /* LWIP_IPV4 */
100   netif.flags |= NETIF_FLAG_ETHARP;
101   netif_set_default(&netif);
102   netif_set_up(&netif);
103 #if LWIP_IPV4
104   printf("IP4 %s\n", ip4addr_ntoa(ip_2_ip4(&netif.ip_addr)));
105 #endif /* LWIP_IPV4 */
106 #if LWIP_IPV6
107   netif_create_ip6_linklocal_address(&netif, 1);
108 #if LWIP_IPV4
109   printf("IP6 [%s]\n", ip6addr_ntoa(&netif.ip6_addr[0].u_addr.ip6));
110 #else /* ! LWIP_IPV4 */
111   printf("IP6 [%s]\n", ip6addr_ntoa(&netif.ip6_addr[0].addr));
112 #endif /* ! LWIP_IPV4 */
113 #endif /* LWIP_IPV6 */
114 
115   /* start applications here */
116 
117 #ifdef _WIN32
118   signal(SIGINT, handle_sigint);
119 #else
120   memset(&sa, 0, sizeof(sa));
121   sigemptyset(&sa.sa_mask);
122   sa.sa_handler = handle_sigint;
123   sa.sa_flags = 0;
124   sigaction(SIGINT, &sa, NULL);
125   sigaction(SIGTERM, &sa, NULL);
126   /* So we do not exit on a SIGPIPE */
127   sa.sa_handler = SIG_IGN;
128   sigaction(SIGPIPE, &sa, NULL);
129 #endif
130 
131   server_coap_init(wait_for_input, &netif, argc, argv);
132 
133   printf("Server Application started.\n");
134 
135   while (1) {
136     /*
137      * Poll netif, pass any read packet to lwIP
138      * Has internal timeout of 100 msec (sometimes less) based on
139      * sys_timeouts_sleeptime().
140      */
141     tapif_select(&netif);
142 
143     sys_check_timeouts();
144 
145     server_coap_poll();
146   }
147 
148   return 0;
149 }
150