f
Fork of lwip by
trunk/Core/device.cpp@1:803fdc96fbd7, 2017-08-03 (annotated)
- Committer:
- idijoeteque
- Date:
- Thu Aug 03 10:24:16 2017 +0000
- Revision:
- 1:803fdc96fbd7
- Parent:
- 0:5e1631496985
hello
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
root@mbed.org | 0:5e1631496985 | 1 | #include "mbed.h" |
root@mbed.org | 0:5e1631496985 | 2 | |
root@mbed.org | 0:5e1631496985 | 3 | using namespace mbed; |
root@mbed.org | 0:5e1631496985 | 4 | |
root@mbed.org | 0:5e1631496985 | 5 | Ethernet *eth; |
root@mbed.org | 0:5e1631496985 | 6 | #ifdef __cplusplus |
root@mbed.org | 0:5e1631496985 | 7 | extern "C" { |
root@mbed.org | 0:5e1631496985 | 8 | #endif |
root@mbed.org | 0:5e1631496985 | 9 | |
root@mbed.org | 0:5e1631496985 | 10 | #include "lwip/opt.h" |
root@mbed.org | 0:5e1631496985 | 11 | |
root@mbed.org | 0:5e1631496985 | 12 | #include "lwip/def.h" |
root@mbed.org | 0:5e1631496985 | 13 | #include "lwip/pbuf.h" |
root@mbed.org | 0:5e1631496985 | 14 | #include "lwip/sys.h" |
root@mbed.org | 0:5e1631496985 | 15 | #include "lwip/stats.h" |
root@mbed.org | 0:5e1631496985 | 16 | #include "netif/etharp.h" |
root@mbed.org | 0:5e1631496985 | 17 | #include "string.h" |
root@mbed.org | 0:5e1631496985 | 18 | |
root@mbed.org | 0:5e1631496985 | 19 | #define IFNAME0 'E' |
root@mbed.org | 0:5e1631496985 | 20 | #define IFNAME1 'X' |
root@mbed.org | 0:5e1631496985 | 21 | |
root@mbed.org | 0:5e1631496985 | 22 | #define min(x,y) (((x)<(y))?(x):(y)) |
root@mbed.org | 0:5e1631496985 | 23 | |
root@mbed.org | 0:5e1631496985 | 24 | struct netif *gnetif; |
root@mbed.org | 0:5e1631496985 | 25 | |
root@mbed.org | 0:5e1631496985 | 26 | static err_t device_output(struct netif *netif, struct pbuf *p) { |
root@mbed.org | 0:5e1631496985 | 27 | #if ETH_PAD_SIZE |
root@mbed.org | 0:5e1631496985 | 28 | pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */ |
root@mbed.org | 0:5e1631496985 | 29 | #endif |
root@mbed.org | 0:5e1631496985 | 30 | |
root@mbed.org | 0:5e1631496985 | 31 | do { |
root@mbed.org | 0:5e1631496985 | 32 | eth->write((const char *)p->payload, p->len); |
root@mbed.org | 0:5e1631496985 | 33 | } while((p = p->next)!=NULL); |
root@mbed.org | 0:5e1631496985 | 34 | |
root@mbed.org | 0:5e1631496985 | 35 | eth->send(); |
root@mbed.org | 0:5e1631496985 | 36 | |
root@mbed.org | 0:5e1631496985 | 37 | #if ETH_PAD_SIZE |
root@mbed.org | 0:5e1631496985 | 38 | pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */ |
root@mbed.org | 0:5e1631496985 | 39 | #endif |
root@mbed.org | 0:5e1631496985 | 40 | |
root@mbed.org | 0:5e1631496985 | 41 | LINK_STATS_INC(link.xmit); |
root@mbed.org | 0:5e1631496985 | 42 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 43 | } |
root@mbed.org | 0:5e1631496985 | 44 | |
root@mbed.org | 0:5e1631496985 | 45 | void device_poll() { |
root@mbed.org | 0:5e1631496985 | 46 | struct eth_hdr *ethhdr; |
root@mbed.org | 0:5e1631496985 | 47 | struct pbuf *frame, *p; |
root@mbed.org | 0:5e1631496985 | 48 | int len, read; |
root@mbed.org | 0:5e1631496985 | 49 | |
root@mbed.org | 0:5e1631496985 | 50 | while((len = eth->receive()) != 0) { |
root@mbed.org | 0:5e1631496985 | 51 | frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); |
root@mbed.org | 0:5e1631496985 | 52 | if(frame == NULL) { |
root@mbed.org | 0:5e1631496985 | 53 | return; |
root@mbed.org | 0:5e1631496985 | 54 | } |
root@mbed.org | 0:5e1631496985 | 55 | p = frame; |
root@mbed.org | 0:5e1631496985 | 56 | do { |
root@mbed.org | 0:5e1631496985 | 57 | read = eth->read((char *)p->payload, p->len); |
root@mbed.org | 0:5e1631496985 | 58 | p = p->next; |
root@mbed.org | 0:5e1631496985 | 59 | } while(p != NULL && read != 0); |
root@mbed.org | 0:5e1631496985 | 60 | |
root@mbed.org | 0:5e1631496985 | 61 | #if ETH_PAD_SIZE |
root@mbed.org | 0:5e1631496985 | 62 | pbuf_header(p, ETH_PAD_SIZE); |
root@mbed.org | 0:5e1631496985 | 63 | #endif |
root@mbed.org | 0:5e1631496985 | 64 | |
root@mbed.org | 0:5e1631496985 | 65 | ethhdr = (struct eth_hdr *)(frame->payload); |
root@mbed.org | 0:5e1631496985 | 66 | |
root@mbed.org | 0:5e1631496985 | 67 | switch(htons(ethhdr->type)) { |
root@mbed.org | 0:5e1631496985 | 68 | |
root@mbed.org | 0:5e1631496985 | 69 | case ETHTYPE_IP: |
root@mbed.org | 0:5e1631496985 | 70 | etharp_ip_input(gnetif, frame); |
root@mbed.org | 0:5e1631496985 | 71 | pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr))); |
root@mbed.org | 0:5e1631496985 | 72 | gnetif->input(frame, gnetif); |
root@mbed.org | 0:5e1631496985 | 73 | break; |
root@mbed.org | 0:5e1631496985 | 74 | |
root@mbed.org | 0:5e1631496985 | 75 | case ETHTYPE_ARP: |
root@mbed.org | 0:5e1631496985 | 76 | etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame); |
root@mbed.org | 0:5e1631496985 | 77 | break; |
root@mbed.org | 0:5e1631496985 | 78 | |
root@mbed.org | 0:5e1631496985 | 79 | default: |
root@mbed.org | 0:5e1631496985 | 80 | break; |
root@mbed.org | 0:5e1631496985 | 81 | } |
root@mbed.org | 0:5e1631496985 | 82 | pbuf_free(frame); |
root@mbed.org | 0:5e1631496985 | 83 | } |
root@mbed.org | 0:5e1631496985 | 84 | } |
root@mbed.org | 0:5e1631496985 | 85 | |
root@mbed.org | 0:5e1631496985 | 86 | err_t device_init(struct netif *netif) { |
root@mbed.org | 0:5e1631496985 | 87 | LWIP_ASSERT("netif != NULL", (netif != NULL)); |
root@mbed.org | 0:5e1631496985 | 88 | |
root@mbed.org | 0:5e1631496985 | 89 | NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA); |
root@mbed.org | 0:5e1631496985 | 90 | |
root@mbed.org | 0:5e1631496985 | 91 | /* maximum transfer unit */ |
root@mbed.org | 0:5e1631496985 | 92 | netif->mtu = 0x2EA; |
root@mbed.org | 0:5e1631496985 | 93 | |
root@mbed.org | 0:5e1631496985 | 94 | /* device capabilities */ |
root@mbed.org | 0:5e1631496985 | 95 | /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */ |
root@mbed.org | 0:5e1631496985 | 96 | netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP; |
root@mbed.org | 0:5e1631496985 | 97 | |
root@mbed.org | 0:5e1631496985 | 98 | netif->state = NULL; |
root@mbed.org | 0:5e1631496985 | 99 | gnetif = netif; |
root@mbed.org | 0:5e1631496985 | 100 | |
root@mbed.org | 0:5e1631496985 | 101 | netif->name[0] = IFNAME0; |
root@mbed.org | 0:5e1631496985 | 102 | netif->name[1] = IFNAME1; |
root@mbed.org | 0:5e1631496985 | 103 | |
root@mbed.org | 0:5e1631496985 | 104 | /* We directly use etharp_output() here to save a function call. |
root@mbed.org | 0:5e1631496985 | 105 | * You can instead declare your own function an call etharp_output() |
root@mbed.org | 0:5e1631496985 | 106 | * from it if you have to do some checks before sending (e.g. if link |
root@mbed.org | 0:5e1631496985 | 107 | * is available...) */ |
root@mbed.org | 0:5e1631496985 | 108 | netif->output = etharp_output; |
root@mbed.org | 0:5e1631496985 | 109 | netif->linkoutput = device_output; |
root@mbed.org | 0:5e1631496985 | 110 | |
root@mbed.org | 0:5e1631496985 | 111 | eth = new Ethernet(); |
root@mbed.org | 0:5e1631496985 | 112 | |
root@mbed.org | 0:5e1631496985 | 113 | return ERR_OK; |
root@mbed.org | 0:5e1631496985 | 114 | } |
root@mbed.org | 0:5e1631496985 | 115 | |
root@mbed.org | 0:5e1631496985 | 116 | void device_address(char *mac) { |
root@mbed.org | 0:5e1631496985 | 117 | eth->address(mac); |
root@mbed.org | 0:5e1631496985 | 118 | } |
root@mbed.org | 0:5e1631496985 | 119 | |
root@mbed.org | 0:5e1631496985 | 120 | #ifdef __cplusplus |
root@mbed.org | 0:5e1631496985 | 121 | }; |
root@mbed.org | 0:5e1631496985 | 122 | #endif |