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