A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers device.cpp Source File

device.cpp

00001 #include "mbed.h"
00002 
00003 using namespace mbed;
00004 
00005 Ethernet *eth;
00006 #ifdef __cplusplus
00007 extern "C" {
00008 #endif
00009 
00010 #include "lwip/opt.h"
00011 
00012 #include "lwip/def.h"
00013 #include "lwip/pbuf.h"
00014 #include "lwip/sys.h"
00015 #include "lwip/stats.h"
00016 #include "netif/etharp.h"
00017 #include "string.h"
00018 
00019 #define IFNAME0 'E'
00020 #define IFNAME1 'X'
00021 
00022 #define min(x,y) (((x)<(y))?(x):(y))
00023 
00024 struct netif *gnetif;
00025 
00026 static err_t device_output(struct netif *netif, struct pbuf *p) {
00027   #if ETH_PAD_SIZE
00028     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
00029   #endif
00030 
00031   do {
00032     eth->write((const char *)p->payload, p->len);
00033   } while((p = p->next)!=NULL);
00034 
00035   eth->send();
00036 
00037   #if ETH_PAD_SIZE
00038     pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
00039   #endif
00040   
00041   LINK_STATS_INC(link.xmit);
00042   return ERR_OK;
00043 }
00044 
00045 void device_poll() {
00046   struct eth_hdr *ethhdr;
00047   struct pbuf *frame, *p;
00048   int len, read;
00049 
00050   while((len = eth->receive()) != 0) {
00051       frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
00052       if(frame == NULL) {
00053           return;
00054       }
00055       p = frame;
00056       do {
00057          read = eth->read((char *)p->payload, p->len);
00058          p = p->next;
00059       } while(p != NULL && read != 0);
00060       
00061       #if ETH_PAD_SIZE
00062           pbuf_header(p, ETH_PAD_SIZE);
00063       #endif
00064 
00065       ethhdr = (struct eth_hdr *)(frame->payload);
00066 
00067       switch(htons(ethhdr->type)) {
00068           
00069           case ETHTYPE_IP:
00070               etharp_ip_input(gnetif, frame);
00071               pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr)));
00072               gnetif->input(frame, gnetif);
00073               break;
00074           
00075           case ETHTYPE_ARP:
00076               etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame);
00077               break;
00078           
00079           default:
00080               break;
00081       }
00082       pbuf_free(frame);
00083   }
00084 }
00085 
00086 err_t device_init(struct netif *netif) {
00087   LWIP_ASSERT("netif != NULL", (netif != NULL));
00088   
00089   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA);
00090   
00091   /* maximum transfer unit */
00092   netif->mtu = 0x2EA;
00093   
00094   /* device capabilities */
00095   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
00096   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
00097 
00098   netif->state = NULL;
00099   gnetif = netif;
00100 
00101   netif->name[0] = IFNAME0;
00102   netif->name[1] = IFNAME1;
00103 
00104   /* We directly use etharp_output() here to save a function call.
00105    * You can instead declare your own function an call etharp_output()
00106    * from it if you have to do some checks before sending (e.g. if link
00107    * is available...) */
00108   netif->output          = etharp_output;
00109   netif->linkoutput      = device_output;
00110 
00111   eth = new Ethernet();
00112 
00113   return ERR_OK;
00114 }
00115 
00116 void device_address(char *mac) {
00117     eth->address(mac);
00118 }
00119 
00120 #ifdef __cplusplus
00121 };
00122 #endif