
Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.
Dependencies: C12832 MQTT LM75B MMA7660
Dependents: MFT_IoT_demo_USB400 IBM_RFID
EthernetInterface/lwip/netif/ethernetif.c@6:37b6d0d56190, 2014-08-20 (annotated)
- Committer:
- samdanbury
- Date:
- Wed Aug 20 12:45:14 2014 +0000
- Revision:
- 6:37b6d0d56190
Code completely changed to improve the structure, flow and memory usage of the application
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 6:37b6d0d56190 | 1 | /** |
samdanbury | 6:37b6d0d56190 | 2 | * @file |
samdanbury | 6:37b6d0d56190 | 3 | * Ethernet Interface Skeleton |
samdanbury | 6:37b6d0d56190 | 4 | * |
samdanbury | 6:37b6d0d56190 | 5 | */ |
samdanbury | 6:37b6d0d56190 | 6 | |
samdanbury | 6:37b6d0d56190 | 7 | /* |
samdanbury | 6:37b6d0d56190 | 8 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science. |
samdanbury | 6:37b6d0d56190 | 9 | * All rights reserved. |
samdanbury | 6:37b6d0d56190 | 10 | * |
samdanbury | 6:37b6d0d56190 | 11 | * Redistribution and use in source and binary forms, with or without modification, |
samdanbury | 6:37b6d0d56190 | 12 | * are permitted provided that the following conditions are met: |
samdanbury | 6:37b6d0d56190 | 13 | * |
samdanbury | 6:37b6d0d56190 | 14 | * 1. Redistributions of source code must retain the above copyright notice, |
samdanbury | 6:37b6d0d56190 | 15 | * this list of conditions and the following disclaimer. |
samdanbury | 6:37b6d0d56190 | 16 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
samdanbury | 6:37b6d0d56190 | 17 | * this list of conditions and the following disclaimer in the documentation |
samdanbury | 6:37b6d0d56190 | 18 | * and/or other materials provided with the distribution. |
samdanbury | 6:37b6d0d56190 | 19 | * 3. The name of the author may not be used to endorse or promote products |
samdanbury | 6:37b6d0d56190 | 20 | * derived from this software without specific prior written permission. |
samdanbury | 6:37b6d0d56190 | 21 | * |
samdanbury | 6:37b6d0d56190 | 22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
samdanbury | 6:37b6d0d56190 | 23 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
samdanbury | 6:37b6d0d56190 | 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
samdanbury | 6:37b6d0d56190 | 25 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
samdanbury | 6:37b6d0d56190 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
samdanbury | 6:37b6d0d56190 | 27 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
samdanbury | 6:37b6d0d56190 | 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
samdanbury | 6:37b6d0d56190 | 29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
samdanbury | 6:37b6d0d56190 | 30 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
samdanbury | 6:37b6d0d56190 | 31 | * OF SUCH DAMAGE. |
samdanbury | 6:37b6d0d56190 | 32 | * |
samdanbury | 6:37b6d0d56190 | 33 | * This file is part of the lwIP TCP/IP stack. |
samdanbury | 6:37b6d0d56190 | 34 | * |
samdanbury | 6:37b6d0d56190 | 35 | * Author: Adam Dunkels <adam@sics.se> |
samdanbury | 6:37b6d0d56190 | 36 | * |
samdanbury | 6:37b6d0d56190 | 37 | */ |
samdanbury | 6:37b6d0d56190 | 38 | |
samdanbury | 6:37b6d0d56190 | 39 | /* |
samdanbury | 6:37b6d0d56190 | 40 | * This file is a skeleton for developing Ethernet network interface |
samdanbury | 6:37b6d0d56190 | 41 | * drivers for lwIP. Add code to the low_level functions and do a |
samdanbury | 6:37b6d0d56190 | 42 | * search-and-replace for the word "ethernetif" to replace it with |
samdanbury | 6:37b6d0d56190 | 43 | * something that better describes your network interface. |
samdanbury | 6:37b6d0d56190 | 44 | */ |
samdanbury | 6:37b6d0d56190 | 45 | |
samdanbury | 6:37b6d0d56190 | 46 | #include "lwip/opt.h" |
samdanbury | 6:37b6d0d56190 | 47 | |
samdanbury | 6:37b6d0d56190 | 48 | #if 0 /* don't build, this is only a skeleton, see previous comment */ |
samdanbury | 6:37b6d0d56190 | 49 | |
samdanbury | 6:37b6d0d56190 | 50 | #include "lwip/def.h" |
samdanbury | 6:37b6d0d56190 | 51 | #include "lwip/mem.h" |
samdanbury | 6:37b6d0d56190 | 52 | #include "lwip/pbuf.h" |
samdanbury | 6:37b6d0d56190 | 53 | #include "lwip/sys.h" |
samdanbury | 6:37b6d0d56190 | 54 | #include <lwip/stats.h> |
samdanbury | 6:37b6d0d56190 | 55 | #include <lwip/snmp.h> |
samdanbury | 6:37b6d0d56190 | 56 | #include "netif/etharp.h" |
samdanbury | 6:37b6d0d56190 | 57 | #include "netif/ppp_oe.h" |
samdanbury | 6:37b6d0d56190 | 58 | |
samdanbury | 6:37b6d0d56190 | 59 | /* Define those to better describe your network interface. */ |
samdanbury | 6:37b6d0d56190 | 60 | #define IFNAME0 'e' |
samdanbury | 6:37b6d0d56190 | 61 | #define IFNAME1 'n' |
samdanbury | 6:37b6d0d56190 | 62 | |
samdanbury | 6:37b6d0d56190 | 63 | /** |
samdanbury | 6:37b6d0d56190 | 64 | * Helper struct to hold private data used to operate your ethernet interface. |
samdanbury | 6:37b6d0d56190 | 65 | * Keeping the ethernet address of the MAC in this struct is not necessary |
samdanbury | 6:37b6d0d56190 | 66 | * as it is already kept in the struct netif. |
samdanbury | 6:37b6d0d56190 | 67 | * But this is only an example, anyway... |
samdanbury | 6:37b6d0d56190 | 68 | */ |
samdanbury | 6:37b6d0d56190 | 69 | struct ethernetif { |
samdanbury | 6:37b6d0d56190 | 70 | struct eth_addr *ethaddr; |
samdanbury | 6:37b6d0d56190 | 71 | /* Add whatever per-interface state that is needed here. */ |
samdanbury | 6:37b6d0d56190 | 72 | }; |
samdanbury | 6:37b6d0d56190 | 73 | |
samdanbury | 6:37b6d0d56190 | 74 | /* Forward declarations. */ |
samdanbury | 6:37b6d0d56190 | 75 | static void ethernetif_input(struct netif *netif); |
samdanbury | 6:37b6d0d56190 | 76 | |
samdanbury | 6:37b6d0d56190 | 77 | /** |
samdanbury | 6:37b6d0d56190 | 78 | * In this function, the hardware should be initialized. |
samdanbury | 6:37b6d0d56190 | 79 | * Called from ethernetif_init(). |
samdanbury | 6:37b6d0d56190 | 80 | * |
samdanbury | 6:37b6d0d56190 | 81 | * @param netif the already initialized lwip network interface structure |
samdanbury | 6:37b6d0d56190 | 82 | * for this ethernetif |
samdanbury | 6:37b6d0d56190 | 83 | */ |
samdanbury | 6:37b6d0d56190 | 84 | static void |
samdanbury | 6:37b6d0d56190 | 85 | low_level_init(struct netif *netif) |
samdanbury | 6:37b6d0d56190 | 86 | { |
samdanbury | 6:37b6d0d56190 | 87 | struct ethernetif *ethernetif = netif->state; |
samdanbury | 6:37b6d0d56190 | 88 | |
samdanbury | 6:37b6d0d56190 | 89 | /* set MAC hardware address length */ |
samdanbury | 6:37b6d0d56190 | 90 | netif->hwaddr_len = ETHARP_HWADDR_LEN; |
samdanbury | 6:37b6d0d56190 | 91 | |
samdanbury | 6:37b6d0d56190 | 92 | /* set MAC hardware address */ |
samdanbury | 6:37b6d0d56190 | 93 | netif->hwaddr[0] = ; |
samdanbury | 6:37b6d0d56190 | 94 | ... |
samdanbury | 6:37b6d0d56190 | 95 | netif->hwaddr[5] = ; |
samdanbury | 6:37b6d0d56190 | 96 | |
samdanbury | 6:37b6d0d56190 | 97 | /* maximum transfer unit */ |
samdanbury | 6:37b6d0d56190 | 98 | netif->mtu = 1500; |
samdanbury | 6:37b6d0d56190 | 99 | |
samdanbury | 6:37b6d0d56190 | 100 | /* device capabilities */ |
samdanbury | 6:37b6d0d56190 | 101 | /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ |
samdanbury | 6:37b6d0d56190 | 102 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; |
samdanbury | 6:37b6d0d56190 | 103 | |
samdanbury | 6:37b6d0d56190 | 104 | /* Do whatever else is needed to initialize interface. */ |
samdanbury | 6:37b6d0d56190 | 105 | } |
samdanbury | 6:37b6d0d56190 | 106 | |
samdanbury | 6:37b6d0d56190 | 107 | /** |
samdanbury | 6:37b6d0d56190 | 108 | * This function should do the actual transmission of the packet. The packet is |
samdanbury | 6:37b6d0d56190 | 109 | * contained in the pbuf that is passed to the function. This pbuf |
samdanbury | 6:37b6d0d56190 | 110 | * might be chained. |
samdanbury | 6:37b6d0d56190 | 111 | * |
samdanbury | 6:37b6d0d56190 | 112 | * @param netif the lwip network interface structure for this ethernetif |
samdanbury | 6:37b6d0d56190 | 113 | * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type) |
samdanbury | 6:37b6d0d56190 | 114 | * @return ERR_OK if the packet could be sent |
samdanbury | 6:37b6d0d56190 | 115 | * an err_t value if the packet couldn't be sent |
samdanbury | 6:37b6d0d56190 | 116 | * |
samdanbury | 6:37b6d0d56190 | 117 | * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to |
samdanbury | 6:37b6d0d56190 | 118 | * strange results. You might consider waiting for space in the DMA queue |
samdanbury | 6:37b6d0d56190 | 119 | * to become availale since the stack doesn't retry to send a packet |
samdanbury | 6:37b6d0d56190 | 120 | * dropped because of memory failure (except for the TCP timers). |
samdanbury | 6:37b6d0d56190 | 121 | */ |
samdanbury | 6:37b6d0d56190 | 122 | |
samdanbury | 6:37b6d0d56190 | 123 | static err_t |
samdanbury | 6:37b6d0d56190 | 124 | low_level_output(struct netif *netif, struct pbuf *p) |
samdanbury | 6:37b6d0d56190 | 125 | { |
samdanbury | 6:37b6d0d56190 | 126 | struct ethernetif *ethernetif = netif->state; |
samdanbury | 6:37b6d0d56190 | 127 | struct pbuf *q; |
samdanbury | 6:37b6d0d56190 | 128 | |
samdanbury | 6:37b6d0d56190 | 129 | initiate transfer(); |
samdanbury | 6:37b6d0d56190 | 130 | |
samdanbury | 6:37b6d0d56190 | 131 | #if ETH_PAD_SIZE |
samdanbury | 6:37b6d0d56190 | 132 | pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ |
samdanbury | 6:37b6d0d56190 | 133 | #endif |
samdanbury | 6:37b6d0d56190 | 134 | |
samdanbury | 6:37b6d0d56190 | 135 | for(q = p; q != NULL; q = q->next) { |
samdanbury | 6:37b6d0d56190 | 136 | /* Send the data from the pbuf to the interface, one pbuf at a |
samdanbury | 6:37b6d0d56190 | 137 | time. The size of the data in each pbuf is kept in the ->len |
samdanbury | 6:37b6d0d56190 | 138 | variable. */ |
samdanbury | 6:37b6d0d56190 | 139 | send data from(q->payload, q->len); |
samdanbury | 6:37b6d0d56190 | 140 | } |
samdanbury | 6:37b6d0d56190 | 141 | |
samdanbury | 6:37b6d0d56190 | 142 | signal that packet should be sent(); |
samdanbury | 6:37b6d0d56190 | 143 | |
samdanbury | 6:37b6d0d56190 | 144 | #if ETH_PAD_SIZE |
samdanbury | 6:37b6d0d56190 | 145 | pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ |
samdanbury | 6:37b6d0d56190 | 146 | #endif |
samdanbury | 6:37b6d0d56190 | 147 | |
samdanbury | 6:37b6d0d56190 | 148 | LINK_STATS_INC(link.xmit); |
samdanbury | 6:37b6d0d56190 | 149 | |
samdanbury | 6:37b6d0d56190 | 150 | return ERR_OK; |
samdanbury | 6:37b6d0d56190 | 151 | } |
samdanbury | 6:37b6d0d56190 | 152 | |
samdanbury | 6:37b6d0d56190 | 153 | /** |
samdanbury | 6:37b6d0d56190 | 154 | * Should allocate a pbuf and transfer the bytes of the incoming |
samdanbury | 6:37b6d0d56190 | 155 | * packet from the interface into the pbuf. |
samdanbury | 6:37b6d0d56190 | 156 | * |
samdanbury | 6:37b6d0d56190 | 157 | * @param netif the lwip network interface structure for this ethernetif |
samdanbury | 6:37b6d0d56190 | 158 | * @return a pbuf filled with the received packet (including MAC header) |
samdanbury | 6:37b6d0d56190 | 159 | * NULL on memory error |
samdanbury | 6:37b6d0d56190 | 160 | */ |
samdanbury | 6:37b6d0d56190 | 161 | static struct pbuf * |
samdanbury | 6:37b6d0d56190 | 162 | low_level_input(struct netif *netif) |
samdanbury | 6:37b6d0d56190 | 163 | { |
samdanbury | 6:37b6d0d56190 | 164 | struct ethernetif *ethernetif = netif->state; |
samdanbury | 6:37b6d0d56190 | 165 | struct pbuf *p, *q; |
samdanbury | 6:37b6d0d56190 | 166 | u16_t len; |
samdanbury | 6:37b6d0d56190 | 167 | |
samdanbury | 6:37b6d0d56190 | 168 | /* Obtain the size of the packet and put it into the "len" |
samdanbury | 6:37b6d0d56190 | 169 | variable. */ |
samdanbury | 6:37b6d0d56190 | 170 | len = ; |
samdanbury | 6:37b6d0d56190 | 171 | |
samdanbury | 6:37b6d0d56190 | 172 | #if ETH_PAD_SIZE |
samdanbury | 6:37b6d0d56190 | 173 | len += ETH_PAD_SIZE; /* allow room for Ethernet padding */ |
samdanbury | 6:37b6d0d56190 | 174 | #endif |
samdanbury | 6:37b6d0d56190 | 175 | |
samdanbury | 6:37b6d0d56190 | 176 | /* We allocate a pbuf chain of pbufs from the pool. */ |
samdanbury | 6:37b6d0d56190 | 177 | p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); |
samdanbury | 6:37b6d0d56190 | 178 | |
samdanbury | 6:37b6d0d56190 | 179 | if (p != NULL) { |
samdanbury | 6:37b6d0d56190 | 180 | |
samdanbury | 6:37b6d0d56190 | 181 | #if ETH_PAD_SIZE |
samdanbury | 6:37b6d0d56190 | 182 | pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ |
samdanbury | 6:37b6d0d56190 | 183 | #endif |
samdanbury | 6:37b6d0d56190 | 184 | |
samdanbury | 6:37b6d0d56190 | 185 | /* We iterate over the pbuf chain until we have read the entire |
samdanbury | 6:37b6d0d56190 | 186 | * packet into the pbuf. */ |
samdanbury | 6:37b6d0d56190 | 187 | for(q = p; q != NULL; q = q->next) { |
samdanbury | 6:37b6d0d56190 | 188 | /* Read enough bytes to fill this pbuf in the chain. The |
samdanbury | 6:37b6d0d56190 | 189 | * available data in the pbuf is given by the q->len |
samdanbury | 6:37b6d0d56190 | 190 | * variable. |
samdanbury | 6:37b6d0d56190 | 191 | * This does not necessarily have to be a memcpy, you can also preallocate |
samdanbury | 6:37b6d0d56190 | 192 | * pbufs for a DMA-enabled MAC and after receiving truncate it to the |
samdanbury | 6:37b6d0d56190 | 193 | * actually received size. In this case, ensure the tot_len member of the |
samdanbury | 6:37b6d0d56190 | 194 | * pbuf is the sum of the chained pbuf len members. |
samdanbury | 6:37b6d0d56190 | 195 | */ |
samdanbury | 6:37b6d0d56190 | 196 | read data into(q->payload, q->len); |
samdanbury | 6:37b6d0d56190 | 197 | } |
samdanbury | 6:37b6d0d56190 | 198 | acknowledge that packet has been read(); |
samdanbury | 6:37b6d0d56190 | 199 | |
samdanbury | 6:37b6d0d56190 | 200 | #if ETH_PAD_SIZE |
samdanbury | 6:37b6d0d56190 | 201 | pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ |
samdanbury | 6:37b6d0d56190 | 202 | #endif |
samdanbury | 6:37b6d0d56190 | 203 | |
samdanbury | 6:37b6d0d56190 | 204 | LINK_STATS_INC(link.recv); |
samdanbury | 6:37b6d0d56190 | 205 | } else { |
samdanbury | 6:37b6d0d56190 | 206 | drop packet(); |
samdanbury | 6:37b6d0d56190 | 207 | LINK_STATS_INC(link.memerr); |
samdanbury | 6:37b6d0d56190 | 208 | LINK_STATS_INC(link.drop); |
samdanbury | 6:37b6d0d56190 | 209 | } |
samdanbury | 6:37b6d0d56190 | 210 | |
samdanbury | 6:37b6d0d56190 | 211 | return p; |
samdanbury | 6:37b6d0d56190 | 212 | } |
samdanbury | 6:37b6d0d56190 | 213 | |
samdanbury | 6:37b6d0d56190 | 214 | /** |
samdanbury | 6:37b6d0d56190 | 215 | * This function should be called when a packet is ready to be read |
samdanbury | 6:37b6d0d56190 | 216 | * from the interface. It uses the function low_level_input() that |
samdanbury | 6:37b6d0d56190 | 217 | * should handle the actual reception of bytes from the network |
samdanbury | 6:37b6d0d56190 | 218 | * interface. Then the type of the received packet is determined and |
samdanbury | 6:37b6d0d56190 | 219 | * the appropriate input function is called. |
samdanbury | 6:37b6d0d56190 | 220 | * |
samdanbury | 6:37b6d0d56190 | 221 | * @param netif the lwip network interface structure for this ethernetif |
samdanbury | 6:37b6d0d56190 | 222 | */ |
samdanbury | 6:37b6d0d56190 | 223 | static void |
samdanbury | 6:37b6d0d56190 | 224 | ethernetif_input(struct netif *netif) |
samdanbury | 6:37b6d0d56190 | 225 | { |
samdanbury | 6:37b6d0d56190 | 226 | struct ethernetif *ethernetif; |
samdanbury | 6:37b6d0d56190 | 227 | struct eth_hdr *ethhdr; |
samdanbury | 6:37b6d0d56190 | 228 | struct pbuf *p; |
samdanbury | 6:37b6d0d56190 | 229 | |
samdanbury | 6:37b6d0d56190 | 230 | ethernetif = netif->state; |
samdanbury | 6:37b6d0d56190 | 231 | |
samdanbury | 6:37b6d0d56190 | 232 | /* move received packet into a new pbuf */ |
samdanbury | 6:37b6d0d56190 | 233 | p = low_level_input(netif); |
samdanbury | 6:37b6d0d56190 | 234 | /* no packet could be read, silently ignore this */ |
samdanbury | 6:37b6d0d56190 | 235 | if (p == NULL) return; |
samdanbury | 6:37b6d0d56190 | 236 | /* points to packet payload, which starts with an Ethernet header */ |
samdanbury | 6:37b6d0d56190 | 237 | ethhdr = p->payload; |
samdanbury | 6:37b6d0d56190 | 238 | |
samdanbury | 6:37b6d0d56190 | 239 | switch (htons(ethhdr->type)) { |
samdanbury | 6:37b6d0d56190 | 240 | /* IP or ARP packet? */ |
samdanbury | 6:37b6d0d56190 | 241 | case ETHTYPE_IP: |
samdanbury | 6:37b6d0d56190 | 242 | case ETHTYPE_ARP: |
samdanbury | 6:37b6d0d56190 | 243 | #if PPPOE_SUPPORT |
samdanbury | 6:37b6d0d56190 | 244 | /* PPPoE packet? */ |
samdanbury | 6:37b6d0d56190 | 245 | case ETHTYPE_PPPOEDISC: |
samdanbury | 6:37b6d0d56190 | 246 | case ETHTYPE_PPPOE: |
samdanbury | 6:37b6d0d56190 | 247 | #endif /* PPPOE_SUPPORT */ |
samdanbury | 6:37b6d0d56190 | 248 | /* full packet send to tcpip_thread to process */ |
samdanbury | 6:37b6d0d56190 | 249 | if (netif->input(p, netif)!=ERR_OK) |
samdanbury | 6:37b6d0d56190 | 250 | { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); |
samdanbury | 6:37b6d0d56190 | 251 | pbuf_free(p); |
samdanbury | 6:37b6d0d56190 | 252 | p = NULL; |
samdanbury | 6:37b6d0d56190 | 253 | } |
samdanbury | 6:37b6d0d56190 | 254 | break; |
samdanbury | 6:37b6d0d56190 | 255 | |
samdanbury | 6:37b6d0d56190 | 256 | default: |
samdanbury | 6:37b6d0d56190 | 257 | pbuf_free(p); |
samdanbury | 6:37b6d0d56190 | 258 | p = NULL; |
samdanbury | 6:37b6d0d56190 | 259 | break; |
samdanbury | 6:37b6d0d56190 | 260 | } |
samdanbury | 6:37b6d0d56190 | 261 | } |
samdanbury | 6:37b6d0d56190 | 262 | |
samdanbury | 6:37b6d0d56190 | 263 | /** |
samdanbury | 6:37b6d0d56190 | 264 | * Should be called at the beginning of the program to set up the |
samdanbury | 6:37b6d0d56190 | 265 | * network interface. It calls the function low_level_init() to do the |
samdanbury | 6:37b6d0d56190 | 266 | * actual setup of the hardware. |
samdanbury | 6:37b6d0d56190 | 267 | * |
samdanbury | 6:37b6d0d56190 | 268 | * This function should be passed as a parameter to netif_add(). |
samdanbury | 6:37b6d0d56190 | 269 | * |
samdanbury | 6:37b6d0d56190 | 270 | * @param netif the lwip network interface structure for this ethernetif |
samdanbury | 6:37b6d0d56190 | 271 | * @return ERR_OK if the loopif is initialized |
samdanbury | 6:37b6d0d56190 | 272 | * ERR_MEM if private data couldn't be allocated |
samdanbury | 6:37b6d0d56190 | 273 | * any other err_t on error |
samdanbury | 6:37b6d0d56190 | 274 | */ |
samdanbury | 6:37b6d0d56190 | 275 | err_t |
samdanbury | 6:37b6d0d56190 | 276 | ethernetif_init(struct netif *netif) |
samdanbury | 6:37b6d0d56190 | 277 | { |
samdanbury | 6:37b6d0d56190 | 278 | struct ethernetif *ethernetif; |
samdanbury | 6:37b6d0d56190 | 279 | |
samdanbury | 6:37b6d0d56190 | 280 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
samdanbury | 6:37b6d0d56190 | 281 | |
samdanbury | 6:37b6d0d56190 | 282 | ethernetif = mem_malloc(sizeof(struct ethernetif)); |
samdanbury | 6:37b6d0d56190 | 283 | if (ethernetif == NULL) { |
samdanbury | 6:37b6d0d56190 | 284 | LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n")); |
samdanbury | 6:37b6d0d56190 | 285 | return ERR_MEM; |
samdanbury | 6:37b6d0d56190 | 286 | } |
samdanbury | 6:37b6d0d56190 | 287 | |
samdanbury | 6:37b6d0d56190 | 288 | #if LWIP_NETIF_HOSTNAME |
samdanbury | 6:37b6d0d56190 | 289 | /* Initialize interface hostname */ |
samdanbury | 6:37b6d0d56190 | 290 | netif->hostname = "lwip"; |
samdanbury | 6:37b6d0d56190 | 291 | #endif /* LWIP_NETIF_HOSTNAME */ |
samdanbury | 6:37b6d0d56190 | 292 | |
samdanbury | 6:37b6d0d56190 | 293 | /* |
samdanbury | 6:37b6d0d56190 | 294 | * Initialize the snmp variables and counters inside the struct netif. |
samdanbury | 6:37b6d0d56190 | 295 | * The last argument should be replaced with your link speed, in units |
samdanbury | 6:37b6d0d56190 | 296 | * of bits per second. |
samdanbury | 6:37b6d0d56190 | 297 | */ |
samdanbury | 6:37b6d0d56190 | 298 | NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS); |
samdanbury | 6:37b6d0d56190 | 299 | |
samdanbury | 6:37b6d0d56190 | 300 | netif->state = ethernetif; |
samdanbury | 6:37b6d0d56190 | 301 | netif->name[0] = IFNAME0; |
samdanbury | 6:37b6d0d56190 | 302 | netif->name[1] = IFNAME1; |
samdanbury | 6:37b6d0d56190 | 303 | /* We directly use etharp_output() here to save a function call. |
samdanbury | 6:37b6d0d56190 | 304 | * You can instead declare your own function an call etharp_output() |
samdanbury | 6:37b6d0d56190 | 305 | * from it if you have to do some checks before sending (e.g. if link |
samdanbury | 6:37b6d0d56190 | 306 | * is available...) */ |
samdanbury | 6:37b6d0d56190 | 307 | netif->output = etharp_output; |
samdanbury | 6:37b6d0d56190 | 308 | netif->linkoutput = low_level_output; |
samdanbury | 6:37b6d0d56190 | 309 | |
samdanbury | 6:37b6d0d56190 | 310 | ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); |
samdanbury | 6:37b6d0d56190 | 311 | |
samdanbury | 6:37b6d0d56190 | 312 | /* initialize the hardware */ |
samdanbury | 6:37b6d0d56190 | 313 | low_level_init(netif); |
samdanbury | 6:37b6d0d56190 | 314 | |
samdanbury | 6:37b6d0d56190 | 315 | return ERR_OK; |
samdanbury | 6:37b6d0d56190 | 316 | } |
samdanbury | 6:37b6d0d56190 | 317 | |
samdanbury | 6:37b6d0d56190 | 318 | #endif /* 0 */ |