Fork of NetServicesMin with some warnings removed
Fork of NetServicesMin by
drv/eth/eth_drv.cpp@4:c8b2f969c8dd, 2012-11-24 (annotated)
- Committer:
- uci1
- Date:
- Sat Nov 24 20:20:45 2012 +0000
- Revision:
- 4:c8b2f969c8dd
- Parent:
- 0:8b387bed54c2
Remove debugging printouts
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:8b387bed54c2 | 1 | |
hlipka | 0:8b387bed54c2 | 2 | /* |
hlipka | 0:8b387bed54c2 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
hlipka | 0:8b387bed54c2 | 4 | |
hlipka | 0:8b387bed54c2 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
hlipka | 0:8b387bed54c2 | 6 | of this software and associated documentation files (the "Software"), to deal |
hlipka | 0:8b387bed54c2 | 7 | in the Software without restriction, including without limitation the rights |
hlipka | 0:8b387bed54c2 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hlipka | 0:8b387bed54c2 | 9 | copies of the Software, and to permit persons to whom the Software is |
hlipka | 0:8b387bed54c2 | 10 | furnished to do so, subject to the following conditions: |
hlipka | 0:8b387bed54c2 | 11 | |
hlipka | 0:8b387bed54c2 | 12 | The above copyright notice and this permission notice shall be included in |
hlipka | 0:8b387bed54c2 | 13 | all copies or substantial portions of the Software. |
hlipka | 0:8b387bed54c2 | 14 | |
hlipka | 0:8b387bed54c2 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hlipka | 0:8b387bed54c2 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hlipka | 0:8b387bed54c2 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hlipka | 0:8b387bed54c2 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hlipka | 0:8b387bed54c2 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hlipka | 0:8b387bed54c2 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hlipka | 0:8b387bed54c2 | 21 | THE SOFTWARE. |
hlipka | 0:8b387bed54c2 | 22 | */ |
hlipka | 0:8b387bed54c2 | 23 | |
hlipka | 0:8b387bed54c2 | 24 | #include "netCfg.h" |
hlipka | 0:8b387bed54c2 | 25 | #if NET_ETH |
hlipka | 0:8b387bed54c2 | 26 | |
hlipka | 0:8b387bed54c2 | 27 | #include "mbed.h" |
hlipka | 0:8b387bed54c2 | 28 | |
hlipka | 0:8b387bed54c2 | 29 | Ethernet *pEth = NULL; |
hlipka | 0:8b387bed54c2 | 30 | #ifdef __cplusplus |
hlipka | 0:8b387bed54c2 | 31 | extern "C" { |
hlipka | 0:8b387bed54c2 | 32 | #endif |
hlipka | 0:8b387bed54c2 | 33 | |
hlipka | 0:8b387bed54c2 | 34 | #include "lwip/opt.h" |
hlipka | 0:8b387bed54c2 | 35 | |
hlipka | 0:8b387bed54c2 | 36 | #include "lwip/def.h" |
hlipka | 0:8b387bed54c2 | 37 | #include "lwip/pbuf.h" |
hlipka | 0:8b387bed54c2 | 38 | #include "lwip/sys.h" |
hlipka | 0:8b387bed54c2 | 39 | #include "lwip/stats.h" |
hlipka | 0:8b387bed54c2 | 40 | #include "netif/etharp.h" |
hlipka | 0:8b387bed54c2 | 41 | #include "string.h" |
hlipka | 0:8b387bed54c2 | 42 | |
hlipka | 0:8b387bed54c2 | 43 | //#include "eth_drv.h" |
hlipka | 0:8b387bed54c2 | 44 | |
hlipka | 0:8b387bed54c2 | 45 | #define IFNAME0 'E' |
hlipka | 0:8b387bed54c2 | 46 | #define IFNAME1 'X' |
hlipka | 0:8b387bed54c2 | 47 | |
hlipka | 0:8b387bed54c2 | 48 | #define min(x,y) (((x)<(y))?(x):(y)) |
hlipka | 0:8b387bed54c2 | 49 | |
hlipka | 0:8b387bed54c2 | 50 | struct netif* eth_netif; |
hlipka | 0:8b387bed54c2 | 51 | |
hlipka | 0:8b387bed54c2 | 52 | static err_t eth_output(struct netif *netif, struct pbuf *p) { |
hlipka | 0:8b387bed54c2 | 53 | #if ETH_PAD_SIZE |
hlipka | 0:8b387bed54c2 | 54 | pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ |
hlipka | 0:8b387bed54c2 | 55 | #endif |
hlipka | 0:8b387bed54c2 | 56 | |
hlipka | 0:8b387bed54c2 | 57 | do { |
hlipka | 0:8b387bed54c2 | 58 | pEth->write((const char *)p->payload, p->len); |
hlipka | 0:8b387bed54c2 | 59 | } while((p = p->next)!=NULL); |
hlipka | 0:8b387bed54c2 | 60 | |
hlipka | 0:8b387bed54c2 | 61 | pEth->send(); |
hlipka | 0:8b387bed54c2 | 62 | |
hlipka | 0:8b387bed54c2 | 63 | #if ETH_PAD_SIZE |
hlipka | 0:8b387bed54c2 | 64 | pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ |
hlipka | 0:8b387bed54c2 | 65 | #endif |
hlipka | 0:8b387bed54c2 | 66 | |
hlipka | 0:8b387bed54c2 | 67 | LINK_STATS_INC(link.xmit); |
hlipka | 0:8b387bed54c2 | 68 | return ERR_OK; |
hlipka | 0:8b387bed54c2 | 69 | } |
hlipka | 0:8b387bed54c2 | 70 | |
hlipka | 0:8b387bed54c2 | 71 | /* |
hlipka | 0:8b387bed54c2 | 72 | void show(char *buf, int size) { |
hlipka | 0:8b387bed54c2 | 73 | printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n", |
hlipka | 0:8b387bed54c2 | 74 | buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]); |
hlipka | 0:8b387bed54c2 | 75 | printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n", |
hlipka | 0:8b387bed54c2 | 76 | buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]); |
hlipka | 0:8b387bed54c2 | 77 | |
hlipka | 0:8b387bed54c2 | 78 | printf("Type %hd\n", htons((short)buf[12])); |
hlipka | 0:8b387bed54c2 | 79 | |
hlipka | 0:8b387bed54c2 | 80 | // hexview(buf, size); |
hlipka | 0:8b387bed54c2 | 81 | } |
hlipka | 0:8b387bed54c2 | 82 | */ |
hlipka | 0:8b387bed54c2 | 83 | |
hlipka | 0:8b387bed54c2 | 84 | void eth_poll() { |
hlipka | 0:8b387bed54c2 | 85 | struct eth_hdr *ethhdr; |
hlipka | 0:8b387bed54c2 | 86 | struct pbuf *frame, *p; |
hlipka | 0:8b387bed54c2 | 87 | int len, read; |
hlipka | 0:8b387bed54c2 | 88 | |
hlipka | 0:8b387bed54c2 | 89 | while((len = pEth->receive()) != 0) { |
hlipka | 0:8b387bed54c2 | 90 | frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); |
hlipka | 0:8b387bed54c2 | 91 | if(frame == NULL) { |
hlipka | 0:8b387bed54c2 | 92 | return; |
hlipka | 0:8b387bed54c2 | 93 | } |
hlipka | 0:8b387bed54c2 | 94 | p = frame; |
hlipka | 0:8b387bed54c2 | 95 | /* no packet could be read, silently ignore this */ |
hlipka | 0:8b387bed54c2 | 96 | if (p == NULL) return; |
hlipka | 0:8b387bed54c2 | 97 | do { |
hlipka | 0:8b387bed54c2 | 98 | read = pEth->read((char *)p->payload, p->len); |
hlipka | 0:8b387bed54c2 | 99 | p = p->next; |
hlipka | 0:8b387bed54c2 | 100 | } while(p != NULL && read != 0); |
hlipka | 0:8b387bed54c2 | 101 | |
hlipka | 0:8b387bed54c2 | 102 | #if ETH_PAD_SIZE |
hlipka | 0:8b387bed54c2 | 103 | pbuf_header(p, ETH_PAD_SIZE); |
hlipka | 0:8b387bed54c2 | 104 | #endif |
hlipka | 0:8b387bed54c2 | 105 | |
hlipka | 0:8b387bed54c2 | 106 | ethhdr = (struct eth_hdr *)(frame->payload); |
hlipka | 0:8b387bed54c2 | 107 | |
hlipka | 0:8b387bed54c2 | 108 | // show((char*)ethhdr, 13); |
hlipka | 0:8b387bed54c2 | 109 | |
hlipka | 0:8b387bed54c2 | 110 | /* |
hlipka | 0:8b387bed54c2 | 111 | switch(htons(ethhdr->type)) { |
hlipka | 0:8b387bed54c2 | 112 | |
hlipka | 0:8b387bed54c2 | 113 | case ETHTYPE_IP: |
hlipka | 0:8b387bed54c2 | 114 | etharp_ip_input(gnetif, frame); |
hlipka | 0:8b387bed54c2 | 115 | pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr))); |
hlipka | 0:8b387bed54c2 | 116 | gnetif->input(frame, gnetif); |
hlipka | 0:8b387bed54c2 | 117 | break; |
hlipka | 0:8b387bed54c2 | 118 | |
hlipka | 0:8b387bed54c2 | 119 | case ETHTYPE_ARP: |
hlipka | 0:8b387bed54c2 | 120 | etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame); |
hlipka | 0:8b387bed54c2 | 121 | break; |
hlipka | 0:8b387bed54c2 | 122 | |
hlipka | 0:8b387bed54c2 | 123 | default: |
hlipka | 0:8b387bed54c2 | 124 | break; |
hlipka | 0:8b387bed54c2 | 125 | }*/ |
hlipka | 0:8b387bed54c2 | 126 | |
hlipka | 0:8b387bed54c2 | 127 | |
hlipka | 0:8b387bed54c2 | 128 | |
hlipka | 0:8b387bed54c2 | 129 | //ethernet_input(frame, gnetif); |
hlipka | 0:8b387bed54c2 | 130 | |
hlipka | 0:8b387bed54c2 | 131 | switch (htons(ethhdr->type)) { |
hlipka | 0:8b387bed54c2 | 132 | /* IP or ARP packet? */ |
hlipka | 0:8b387bed54c2 | 133 | case ETHTYPE_IP: |
hlipka | 0:8b387bed54c2 | 134 | case ETHTYPE_ARP: |
hlipka | 0:8b387bed54c2 | 135 | #if PPPOE_SUPPORT |
hlipka | 0:8b387bed54c2 | 136 | /* PPPoE packet? */ |
hlipka | 0:8b387bed54c2 | 137 | case ETHTYPE_PPPOEDISC: |
hlipka | 0:8b387bed54c2 | 138 | case ETHTYPE_PPPOE: |
hlipka | 0:8b387bed54c2 | 139 | #endif /* PPPOE_SUPPORT */ |
hlipka | 0:8b387bed54c2 | 140 | /* full packet send to tcpip_thread to process */ |
hlipka | 0:8b387bed54c2 | 141 | //if (netif->input(p, gnetif)!=ERR_OK) |
hlipka | 0:8b387bed54c2 | 142 | if (ethernet_input(frame, eth_netif)!=ERR_OK) |
hlipka | 0:8b387bed54c2 | 143 | { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n")); |
hlipka | 0:8b387bed54c2 | 144 | pbuf_free(frame); |
hlipka | 0:8b387bed54c2 | 145 | frame = NULL; |
hlipka | 0:8b387bed54c2 | 146 | } |
hlipka | 0:8b387bed54c2 | 147 | break; |
hlipka | 0:8b387bed54c2 | 148 | |
hlipka | 0:8b387bed54c2 | 149 | default: |
hlipka | 0:8b387bed54c2 | 150 | pbuf_free(frame); |
hlipka | 0:8b387bed54c2 | 151 | frame = NULL; |
hlipka | 0:8b387bed54c2 | 152 | break; |
hlipka | 0:8b387bed54c2 | 153 | } |
hlipka | 0:8b387bed54c2 | 154 | |
hlipka | 0:8b387bed54c2 | 155 | /* pbuf_free(frame); */ |
hlipka | 0:8b387bed54c2 | 156 | } |
hlipka | 0:8b387bed54c2 | 157 | |
hlipka | 0:8b387bed54c2 | 158 | |
hlipka | 0:8b387bed54c2 | 159 | |
hlipka | 0:8b387bed54c2 | 160 | |
hlipka | 0:8b387bed54c2 | 161 | } |
hlipka | 0:8b387bed54c2 | 162 | |
hlipka | 0:8b387bed54c2 | 163 | err_t eth_init(struct netif *netif) { |
hlipka | 0:8b387bed54c2 | 164 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
hlipka | 0:8b387bed54c2 | 165 | |
hlipka | 0:8b387bed54c2 | 166 | NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA); |
hlipka | 0:8b387bed54c2 | 167 | |
hlipka | 0:8b387bed54c2 | 168 | /* maximum transfer unit */ |
hlipka | 0:8b387bed54c2 | 169 | netif->mtu = 0x2EA; |
hlipka | 0:8b387bed54c2 | 170 | |
hlipka | 0:8b387bed54c2 | 171 | /* device capabilities */ |
hlipka | 0:8b387bed54c2 | 172 | /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ |
hlipka | 0:8b387bed54c2 | 173 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_IGMP; |
hlipka | 0:8b387bed54c2 | 174 | |
hlipka | 0:8b387bed54c2 | 175 | netif->state = NULL; |
hlipka | 0:8b387bed54c2 | 176 | eth_netif = netif; |
hlipka | 0:8b387bed54c2 | 177 | |
hlipka | 0:8b387bed54c2 | 178 | netif->name[0] = IFNAME0; |
hlipka | 0:8b387bed54c2 | 179 | netif->name[1] = IFNAME1; |
hlipka | 0:8b387bed54c2 | 180 | |
hlipka | 0:8b387bed54c2 | 181 | /* We directly use etharp_output() here to save a function call. |
hlipka | 0:8b387bed54c2 | 182 | * You can instead declare your own function an call etharp_output() |
hlipka | 0:8b387bed54c2 | 183 | * from it if you have to do some checks before sending (e.g. if link |
hlipka | 0:8b387bed54c2 | 184 | * is available...) */ |
hlipka | 0:8b387bed54c2 | 185 | netif->output = etharp_output; |
hlipka | 0:8b387bed54c2 | 186 | netif->linkoutput = eth_output; |
hlipka | 0:8b387bed54c2 | 187 | |
hlipka | 0:8b387bed54c2 | 188 | if (!pEth) pEth = new Ethernet(); // only create Ethernet object if required |
hlipka | 0:8b387bed54c2 | 189 | |
hlipka | 0:8b387bed54c2 | 190 | return ERR_OK; |
hlipka | 0:8b387bed54c2 | 191 | } |
hlipka | 0:8b387bed54c2 | 192 | |
hlipka | 0:8b387bed54c2 | 193 | void eth_free() |
hlipka | 0:8b387bed54c2 | 194 | { |
hlipka | 0:8b387bed54c2 | 195 | if(pEth) |
hlipka | 0:8b387bed54c2 | 196 | delete pEth; |
hlipka | 0:8b387bed54c2 | 197 | pEth = NULL; |
hlipka | 0:8b387bed54c2 | 198 | } |
hlipka | 0:8b387bed54c2 | 199 | |
hlipka | 0:8b387bed54c2 | 200 | void eth_address(char* mac) { |
hlipka | 0:8b387bed54c2 | 201 | pEth->address(mac); |
hlipka | 0:8b387bed54c2 | 202 | } |
hlipka | 0:8b387bed54c2 | 203 | |
hlipka | 0:8b387bed54c2 | 204 | Ethernet* eth_interface() { |
hlipka | 0:8b387bed54c2 | 205 | return pEth; |
hlipka | 0:8b387bed54c2 | 206 | } |
hlipka | 0:8b387bed54c2 | 207 | |
hlipka | 0:8b387bed54c2 | 208 | #ifdef __cplusplus |
hlipka | 0:8b387bed54c2 | 209 | }; |
hlipka | 0:8b387bed54c2 | 210 | #endif |
hlipka | 0:8b387bed54c2 | 211 | |
hlipka | 0:8b387bed54c2 | 212 | #endif |