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