Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed
netif.h
00001 /* 00002 * Copyright (c) 2001-2004 Swedish Institute of Computer Science. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without modification, 00006 * are permitted provided that the following conditions are met: 00007 * 00008 * 1. Redistributions of source code must retain the above copyright notice, 00009 * this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright notice, 00011 * this list of conditions and the following disclaimer in the documentation 00012 * and/or other materials provided with the distribution. 00013 * 3. The name of the author may not be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 00017 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00018 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 00019 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00020 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 00021 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00023 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 00024 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 00025 * OF SUCH DAMAGE. 00026 * 00027 * This file is part of the lwIP TCP/IP stack. 00028 * 00029 * Author: Adam Dunkels <adam@sics.se> 00030 * 00031 */ 00032 #ifndef __LWIP_NETIF_H__ 00033 #define __LWIP_NETIF_H__ 00034 00035 #include "lwip/opt.h" 00036 00037 #define ENABLE_LOOPBACK (LWIP_NETIF_LOOPBACK || LWIP_HAVE_LOOPIF) 00038 00039 #include "lwip/err.h" 00040 00041 #include "lwip/ip_addr.h" 00042 00043 #include "lwip/inet.h" 00044 #include "lwip/pbuf.h" 00045 #if LWIP_DHCP 00046 struct dhcp; 00047 #endif 00048 #if LWIP_AUTOIP 00049 struct autoip; 00050 #endif 00051 00052 #ifdef __cplusplus 00053 extern "C" { 00054 #endif 00055 00056 /* Throughout this file, IP addresses are expected to be in 00057 * the same byte order as in IP_PCB. */ 00058 00059 /** must be the maximum of all used hardware address lengths 00060 across all types of interfaces in use */ 00061 #define NETIF_MAX_HWADDR_LEN 6U 00062 00063 /** TODO: define the use (where, when, whom) of netif flags */ 00064 00065 /** whether the network interface is 'up'. this is 00066 * a software flag used to control whether this network 00067 * interface is enabled and processes traffic. 00068 */ 00069 #define NETIF_FLAG_UP 0x01U 00070 /** if set, the netif has broadcast capability */ 00071 #define NETIF_FLAG_BROADCAST 0x02U 00072 /** if set, the netif is one end of a point-to-point connection */ 00073 #define NETIF_FLAG_POINTTOPOINT 0x04U 00074 /** if set, the interface is configured using DHCP */ 00075 #define NETIF_FLAG_DHCP 0x08U 00076 /** if set, the interface has an active link 00077 * (set by the network interface driver) */ 00078 #define NETIF_FLAG_LINK_UP 0x10U 00079 /** if set, the netif is an device using ARP */ 00080 #define NETIF_FLAG_ETHARP 0x20U 00081 /** if set, the netif has IGMP capability */ 00082 #define NETIF_FLAG_IGMP 0x40U 00083 00084 /** Generic data structure used for all lwIP network interfaces. 00085 * The following fields should be filled in by the initialization 00086 * function for the device driver: hwaddr_len, hwaddr[], mtu, flags */ 00087 00088 struct netif { 00089 /** pointer to next in linked list */ 00090 struct netif *next; 00091 00092 /** IP address configuration in network byte order */ 00093 struct ip_addr ip_addr; 00094 struct ip_addr netmask; 00095 struct ip_addr gw; 00096 00097 /** This function is called by the network device driver 00098 * to pass a packet up the TCP/IP stack. */ 00099 err_t (* input)(struct pbuf *p, struct netif *inp); 00100 /** This function is called by the IP module when it wants 00101 * to send a packet on the interface. This function typically 00102 * first resolves the hardware address, then sends the packet. */ 00103 err_t (* output)(struct netif *netif, struct pbuf *p, 00104 struct ip_addr *ipaddr); 00105 /** This function is called by the ARP module when it wants 00106 * to send a packet on the interface. This function outputs 00107 * the pbuf as-is on the link medium. */ 00108 err_t (* linkoutput)(struct netif *netif, struct pbuf *p); 00109 #if LWIP_NETIF_STATUS_CALLBACK 00110 /** This function is called when the netif state is set to up or down 00111 */ 00112 void (* status_callback)(struct netif *netif); 00113 #endif /* LWIP_NETIF_STATUS_CALLBACK */ 00114 #if LWIP_NETIF_LINK_CALLBACK 00115 /** This function is called when the netif link is set to up or down 00116 */ 00117 void (* link_callback)(struct netif *netif); 00118 #endif /* LWIP_NETIF_LINK_CALLBACK */ 00119 /** This field can be set by the device driver and could point 00120 * to state information for the device. */ 00121 void *state; 00122 #if LWIP_DHCP 00123 /** the DHCP client state information for this netif */ 00124 struct dhcp *dhcp; 00125 #endif /* LWIP_DHCP */ 00126 #if LWIP_AUTOIP 00127 /** the AutoIP client state information for this netif */ 00128 struct autoip *autoip; 00129 #endif 00130 #if LWIP_NETIF_HOSTNAME 00131 /* the hostname for this netif, NULL is a valid value */ 00132 char* hostname; 00133 #endif /* LWIP_NETIF_HOSTNAME */ 00134 /** number of bytes used in hwaddr */ 00135 u8_t hwaddr_len; 00136 /** link level hardware address of this interface */ 00137 u8_t hwaddr[NETIF_MAX_HWADDR_LEN]; 00138 /** maximum transfer unit (in bytes) */ 00139 u16_t mtu; 00140 /** flags (see NETIF_FLAG_ above) */ 00141 u8_t flags; 00142 /** descriptive abbreviation */ 00143 char name[2]; 00144 /** number of this interface */ 00145 u8_t num; 00146 #if LWIP_SNMP 00147 /** link type (from "snmp_ifType" enum from snmp.h) */ 00148 u8_t link_type; 00149 /** (estimate) link speed */ 00150 u32_t link_speed; 00151 /** timestamp at last change made (up/down) */ 00152 u32_t ts; 00153 /** counters */ 00154 u32_t ifinoctets; 00155 u32_t ifinucastpkts; 00156 u32_t ifinnucastpkts; 00157 u32_t ifindiscards; 00158 u32_t ifoutoctets; 00159 u32_t ifoutucastpkts; 00160 u32_t ifoutnucastpkts; 00161 u32_t ifoutdiscards; 00162 #endif /* LWIP_SNMP */ 00163 #if LWIP_IGMP 00164 /* This function could be called to add or delete a entry in the multicast filter table of the ethernet MAC.*/ 00165 err_t (*igmp_mac_filter)( struct netif *netif, struct ip_addr *group, u8_t action); 00166 #endif /* LWIP_IGMP */ 00167 #if LWIP_NETIF_HWADDRHINT 00168 u8_t *addr_hint; 00169 #endif /* LWIP_NETIF_HWADDRHINT */ 00170 #if ENABLE_LOOPBACK 00171 /* List of packets to be queued for ourselves. */ 00172 struct pbuf *loop_first; 00173 struct pbuf *loop_last; 00174 #if LWIP_LOOPBACK_MAX_PBUFS 00175 u16_t loop_cnt_current; 00176 #endif /* LWIP_LOOPBACK_MAX_PBUFS */ 00177 #endif /* ENABLE_LOOPBACK */ 00178 }; 00179 00180 #if LWIP_SNMP 00181 #define NETIF_INIT_SNMP(netif, type, speed) \ 00182 /* use "snmp_ifType" enum from snmp.h for "type", snmp_ifType_ethernet_csmacd by example */ \ 00183 netif->link_type = type; \ 00184 /* your link speed here (units: bits per second) */ \ 00185 netif->link_speed = speed; \ 00186 netif->ts = 0; \ 00187 netif->ifinoctets = 0; \ 00188 netif->ifinucastpkts = 0; \ 00189 netif->ifinnucastpkts = 0; \ 00190 netif->ifindiscards = 0; \ 00191 netif->ifoutoctets = 0; \ 00192 netif->ifoutucastpkts = 0; \ 00193 netif->ifoutnucastpkts = 0; \ 00194 netif->ifoutdiscards = 0 00195 #else /* LWIP_SNMP */ 00196 #define NETIF_INIT_SNMP(netif, type, speed) 00197 #endif /* LWIP_SNMP */ 00198 00199 00200 /** The list of network interfaces. */ 00201 extern struct netif *netif_list; 00202 /** The default network interface. */ 00203 extern struct netif *netif_default; 00204 00205 #define netif_init() /* Compatibility define, not init needed. */ 00206 00207 struct netif *netif_add(struct netif *netif, struct ip_addr *ipaddr, struct ip_addr *netmask, 00208 struct ip_addr *gw, 00209 void *state, 00210 err_t (* init)(struct netif *netif), 00211 err_t (* input)(struct pbuf *p, struct netif *netif)); 00212 00213 void 00214 netif_set_addr(struct netif *netif,struct ip_addr *ipaddr, struct ip_addr *netmask, 00215 struct ip_addr *gw); 00216 void netif_remove(struct netif * netif); 00217 00218 /* Returns a network interface given its name. The name is of the form 00219 "et0", where the first two letters are the "name" field in the 00220 netif structure, and the digit is in the num field in the same 00221 structure. */ 00222 struct netif *netif_find(char *name); 00223 00224 void netif_set_default(struct netif *netif); 00225 00226 void netif_set_ipaddr(struct netif *netif, struct ip_addr *ipaddr); 00227 void netif_set_netmask(struct netif *netif, struct ip_addr *netmask); 00228 void netif_set_gw(struct netif *netif, struct ip_addr *gw); 00229 00230 void netif_set_up(struct netif *netif); 00231 void netif_set_down(struct netif *netif); 00232 u8_t netif_is_up(struct netif *netif); 00233 00234 #if LWIP_NETIF_STATUS_CALLBACK 00235 /* 00236 * Set callback to be called when interface is brought up/down 00237 */ 00238 void netif_set_status_callback(struct netif *netif, void (* status_callback)(struct netif *netif)); 00239 #endif /* LWIP_NETIF_STATUS_CALLBACK */ 00240 00241 #if LWIP_NETIF_LINK_CALLBACK 00242 void netif_set_link_up(struct netif *netif); 00243 void netif_set_link_down(struct netif *netif); 00244 u8_t netif_is_link_up(struct netif *netif); 00245 /* 00246 * Set callback to be called when link is brought up/down 00247 */ 00248 void netif_set_link_callback(struct netif *netif, void (* link_callback)(struct netif *netif)); 00249 #endif /* LWIP_NETIF_LINK_CALLBACK */ 00250 00251 #ifdef __cplusplus 00252 } 00253 #endif 00254 00255 #if ENABLE_LOOPBACK 00256 err_t netif_loop_output(struct netif *netif, struct pbuf *p, struct ip_addr *dest_ip); 00257 void netif_poll(struct netif *netif); 00258 #if !LWIP_NETIF_LOOPBACK_MULTITHREADING 00259 void netif_poll_all(void); 00260 #endif /* !LWIP_NETIF_LOOPBACK_MULTITHREADING */ 00261 #endif /* ENABLE_LOOPBACK */ 00262 00263 #endif /* __LWIP_NETIF_H__ */
Generated on Tue Jul 12 2022 19:17:23 by 1.7.2