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