1c87c5fbaSopenharmony_ci/* 2c87c5fbaSopenharmony_ci * Demo for libcoap on lwIP 3c87c5fbaSopenharmony_ci * 4c87c5fbaSopenharmony_ci * partially copied from lwip-contrib/ports/unix/proj/minimal/main.c 5c87c5fbaSopenharmony_ci * 6c87c5fbaSopenharmony_ci * 7c87c5fbaSopenharmony_ci * Copyright (c) 2001-2003 Swedish Institute of Computer Science. 8c87c5fbaSopenharmony_ci * All rights reserved. 9c87c5fbaSopenharmony_ci * 10c87c5fbaSopenharmony_ci * Redistribution and use in source and binary forms, with or without modification, 11c87c5fbaSopenharmony_ci * are permitted provided that the following conditions are met: 12c87c5fbaSopenharmony_ci * 13c87c5fbaSopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, 14c87c5fbaSopenharmony_ci * this list of conditions and the following disclaimer. 15c87c5fbaSopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, 16c87c5fbaSopenharmony_ci * this list of conditions and the following disclaimer in the documentation 17c87c5fbaSopenharmony_ci * and/or other materials provided with the distribution. 18c87c5fbaSopenharmony_ci * 3. The name of the author may not be used to endorse or promote products 19c87c5fbaSopenharmony_ci * derived from this software without specific prior written permission. 20c87c5fbaSopenharmony_ci * 21c87c5fbaSopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 22c87c5fbaSopenharmony_ci * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23c87c5fbaSopenharmony_ci * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24c87c5fbaSopenharmony_ci * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25c87c5fbaSopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26c87c5fbaSopenharmony_ci * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27c87c5fbaSopenharmony_ci * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28c87c5fbaSopenharmony_ci * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 29c87c5fbaSopenharmony_ci * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 30c87c5fbaSopenharmony_ci * OF SUCH DAMAGE. 31c87c5fbaSopenharmony_ci * 32c87c5fbaSopenharmony_ci * Author: Adam Dunkels <adam@sics.se> 33c87c5fbaSopenharmony_ci * RT timer modifications by Christiaan Simons 34c87c5fbaSopenharmony_ci * lwip adaptions: chrysn <chrysn@fsfe.org> 35c87c5fbaSopenharmony_ci * also, https://savannah.nongnu.org/bugs/?40245 was applied */ 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ci#include "server-coap.h" 38c87c5fbaSopenharmony_ci 39c87c5fbaSopenharmony_ci#include <lwip/init.h> 40c87c5fbaSopenharmony_ci#include <lwip/timeouts.h> 41c87c5fbaSopenharmony_ci 42c87c5fbaSopenharmony_ci#include <netif/etharp.h> 43c87c5fbaSopenharmony_ci#include <netif/tapif.h> 44c87c5fbaSopenharmony_ci 45c87c5fbaSopenharmony_ci#include <signal.h> 46c87c5fbaSopenharmony_ci 47c87c5fbaSopenharmony_ci#if LWIP_IPV4 48c87c5fbaSopenharmony_cistatic ip4_addr_t ipaddr, netmask, gw; 49c87c5fbaSopenharmony_ci#endif /* LWIP_IPV4 */ 50c87c5fbaSopenharmony_ci 51c87c5fbaSopenharmony_civoid 52c87c5fbaSopenharmony_cihandle_sigint(int signum) { 53c87c5fbaSopenharmony_ci (void)signum; 54c87c5fbaSopenharmony_ci 55c87c5fbaSopenharmony_ci server_coap_finished(); 56c87c5fbaSopenharmony_ci printf("Server Application finished.\n"); 57c87c5fbaSopenharmony_ci exit(0); 58c87c5fbaSopenharmony_ci} 59c87c5fbaSopenharmony_ci 60c87c5fbaSopenharmony_ci/* 61c87c5fbaSopenharmony_ci * This function is called internally by coap_io_process() to check 62c87c5fbaSopenharmony_ci * for input. 63c87c5fbaSopenharmony_ci */ 64c87c5fbaSopenharmony_cistatic int 65c87c5fbaSopenharmony_ciwait_for_input(void *arg, uint32_t milli_secs) { 66c87c5fbaSopenharmony_ci struct netif *netif = (struct netif *)arg; 67c87c5fbaSopenharmony_ci int ret; 68c87c5fbaSopenharmony_ci 69c87c5fbaSopenharmony_ci (void)milli_secs; 70c87c5fbaSopenharmony_ci ret = tapif_select(netif); 71c87c5fbaSopenharmony_ci 72c87c5fbaSopenharmony_ci sys_check_timeouts(); 73c87c5fbaSopenharmony_ci return ret; 74c87c5fbaSopenharmony_ci} 75c87c5fbaSopenharmony_ci 76c87c5fbaSopenharmony_ciint 77c87c5fbaSopenharmony_cimain(int argc, char **argv) { 78c87c5fbaSopenharmony_ci struct netif netif; 79c87c5fbaSopenharmony_ci#ifndef _WIN32 80c87c5fbaSopenharmony_ci struct sigaction sa; 81c87c5fbaSopenharmony_ci#endif 82c87c5fbaSopenharmony_ci 83c87c5fbaSopenharmony_ci /* startup defaults (may be overridden by one or more opts). this is 84c87c5fbaSopenharmony_ci * hard-coded v4 even in presence of v6, which does auto-discovery and 85c87c5fbaSopenharmony_ci * should thus wind up with an address of fe80::12:34ff:fe56:78ab%tap0 86c87c5fbaSopenharmony_ci * */ 87c87c5fbaSopenharmony_ci#if LWIP_IPV4 88c87c5fbaSopenharmony_ci IP4_ADDR(&gw, 192,168,113,1); 89c87c5fbaSopenharmony_ci IP4_ADDR(&ipaddr, 192,168,113,2); 90c87c5fbaSopenharmony_ci IP4_ADDR(&netmask, 255,255,255,0); 91c87c5fbaSopenharmony_ci#endif /* LWIP_IPV4 */ 92c87c5fbaSopenharmony_ci 93c87c5fbaSopenharmony_ci lwip_init(); 94c87c5fbaSopenharmony_ci 95c87c5fbaSopenharmony_ci printf("TCP/IP initialized.\n"); 96c87c5fbaSopenharmony_ci 97c87c5fbaSopenharmony_ci#if LWIP_IPV4 98c87c5fbaSopenharmony_ci netif_add(&netif, &ipaddr, &netmask, &gw, NULL, tapif_init, ethernet_input); 99c87c5fbaSopenharmony_ci#endif /* LWIP_IPV4 */ 100c87c5fbaSopenharmony_ci netif.flags |= NETIF_FLAG_ETHARP; 101c87c5fbaSopenharmony_ci netif_set_default(&netif); 102c87c5fbaSopenharmony_ci netif_set_up(&netif); 103c87c5fbaSopenharmony_ci#if LWIP_IPV4 104c87c5fbaSopenharmony_ci printf("IP4 %s\n", ip4addr_ntoa(ip_2_ip4(&netif.ip_addr))); 105c87c5fbaSopenharmony_ci#endif /* LWIP_IPV4 */ 106c87c5fbaSopenharmony_ci#if LWIP_IPV6 107c87c5fbaSopenharmony_ci netif_create_ip6_linklocal_address(&netif, 1); 108c87c5fbaSopenharmony_ci#if LWIP_IPV4 109c87c5fbaSopenharmony_ci printf("IP6 [%s]\n", ip6addr_ntoa(&netif.ip6_addr[0].u_addr.ip6)); 110c87c5fbaSopenharmony_ci#else /* ! LWIP_IPV4 */ 111c87c5fbaSopenharmony_ci printf("IP6 [%s]\n", ip6addr_ntoa(&netif.ip6_addr[0].addr)); 112c87c5fbaSopenharmony_ci#endif /* ! LWIP_IPV4 */ 113c87c5fbaSopenharmony_ci#endif /* LWIP_IPV6 */ 114c87c5fbaSopenharmony_ci 115c87c5fbaSopenharmony_ci /* start applications here */ 116c87c5fbaSopenharmony_ci 117c87c5fbaSopenharmony_ci#ifdef _WIN32 118c87c5fbaSopenharmony_ci signal(SIGINT, handle_sigint); 119c87c5fbaSopenharmony_ci#else 120c87c5fbaSopenharmony_ci memset(&sa, 0, sizeof(sa)); 121c87c5fbaSopenharmony_ci sigemptyset(&sa.sa_mask); 122c87c5fbaSopenharmony_ci sa.sa_handler = handle_sigint; 123c87c5fbaSopenharmony_ci sa.sa_flags = 0; 124c87c5fbaSopenharmony_ci sigaction(SIGINT, &sa, NULL); 125c87c5fbaSopenharmony_ci sigaction(SIGTERM, &sa, NULL); 126c87c5fbaSopenharmony_ci /* So we do not exit on a SIGPIPE */ 127c87c5fbaSopenharmony_ci sa.sa_handler = SIG_IGN; 128c87c5fbaSopenharmony_ci sigaction(SIGPIPE, &sa, NULL); 129c87c5fbaSopenharmony_ci#endif 130c87c5fbaSopenharmony_ci 131c87c5fbaSopenharmony_ci server_coap_init(wait_for_input, &netif, argc, argv); 132c87c5fbaSopenharmony_ci 133c87c5fbaSopenharmony_ci printf("Server Application started.\n"); 134c87c5fbaSopenharmony_ci 135c87c5fbaSopenharmony_ci while (1) { 136c87c5fbaSopenharmony_ci /* 137c87c5fbaSopenharmony_ci * Poll netif, pass any read packet to lwIP 138c87c5fbaSopenharmony_ci * Has internal timeout of 100 msec (sometimes less) based on 139c87c5fbaSopenharmony_ci * sys_timeouts_sleeptime(). 140c87c5fbaSopenharmony_ci */ 141c87c5fbaSopenharmony_ci tapif_select(&netif); 142c87c5fbaSopenharmony_ci 143c87c5fbaSopenharmony_ci sys_check_timeouts(); 144c87c5fbaSopenharmony_ci 145c87c5fbaSopenharmony_ci server_coap_poll(); 146c87c5fbaSopenharmony_ci } 147c87c5fbaSopenharmony_ci 148c87c5fbaSopenharmony_ci return 0; 149c87c5fbaSopenharmony_ci} 150