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