Daniel Peter / Mbed 2 deprecated cig_httpclient2

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eth_drv.cpp Source File

eth_drv.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "netCfg.h"
00025 #if NET_ETH
00026 
00027 #include "mbed.h"
00028 
00029 Ethernet *pEth = NULL;
00030 #ifdef __cplusplus
00031 extern "C" {
00032 #endif
00033 
00034 #include "lwip/opt.h"
00035 
00036 #include "lwip/def.h"
00037 #include "lwip/pbuf.h"
00038 #include "lwip/sys.h"
00039 #include "lwip/stats.h"
00040 #include "netif/etharp.h"
00041 #include "string.h"
00042 
00043 //#include "eth_drv.h"
00044 
00045 #define IFNAME0 'E'
00046 #define IFNAME1 'X'
00047 
00048 #define min(x,y) (((x)<(y))?(x):(y))
00049 
00050 struct netif* eth_netif;
00051 
00052 static err_t eth_output(struct netif *netif, struct pbuf *p) {
00053   #if ETH_PAD_SIZE
00054     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
00055   #endif
00056 
00057   do {
00058     pEth->write((const char *)p->payload, p->len);
00059   } while((p = p->next)!=NULL);
00060 
00061   pEth->send();
00062 
00063   #if ETH_PAD_SIZE
00064     pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
00065   #endif
00066   
00067   LINK_STATS_INC(link.xmit);
00068   return ERR_OK;
00069 }
00070 
00071 bool broadcast(char *buf) {
00072     return ((buf[0] == 0xff) &&
00073         (buf[1] == 0xff) &&
00074         (buf[2] == 0xff) &&
00075         (buf[3] == 0xff) &&
00076         (buf[4] == 0xff) &&
00077         (buf[5] == 0xff));
00078 }
00079 
00080 void show(char *buf, int size) {
00081 
00082 if (htons((short)buf[12]) == 0x0800 && !broadcast(buf)) {
00083     printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx | ",
00084             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
00085     printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx | ",
00086             buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
00087   
00088     printf("Type %hx\n", htons((short)buf[12]));
00089     }
00090    // hexview(buf, size);
00091 }
00092 
00093 void eth_poll() {
00094   struct eth_hdr *ethhdr;
00095   struct pbuf *frame, *p;
00096   int len, read;
00097 
00098   while((len = pEth->receive()) != 0) {
00099       frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
00100       if(frame == NULL) {
00101           return;
00102       }
00103       p = frame;
00104       /* no packet could be read, silently ignore this */
00105       if (p == NULL) return;
00106       do {
00107          read = pEth->read((char *)p->payload, p->len);
00108          p = p->next;
00109       } while(p != NULL && read != 0);
00110            
00111       #if ETH_PAD_SIZE
00112           pbuf_header(p, ETH_PAD_SIZE);
00113       #endif
00114 
00115       ethhdr = (struct eth_hdr *)(frame->payload);
00116       
00117      show((char*)ethhdr, 13);
00118       
00119       /*
00120       switch(htons(ethhdr->type)) {
00121           
00122           case ETHTYPE_IP:
00123               etharp_ip_input(gnetif, frame);
00124               pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr)));
00125               gnetif->input(frame, gnetif);
00126               break;
00127           
00128           case ETHTYPE_ARP:
00129               etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame);
00130               break;
00131           
00132           default:
00133               break;
00134       }*/
00135       
00136       
00137       
00138       //ethernet_input(frame, gnetif);
00139       
00140       switch (htons(ethhdr->type)) {
00141       /* IP or ARP packet? */
00142       case ETHTYPE_IP:
00143       case ETHTYPE_ARP:
00144       #if PPPOE_SUPPORT
00145       /* PPPoE packet? */
00146       case ETHTYPE_PPPOEDISC:
00147       case ETHTYPE_PPPOE:
00148       #endif /* PPPOE_SUPPORT */
00149       /* full packet send to tcpip_thread to process */
00150         //if (netif->input(p, gnetif)!=ERR_OK)
00151         if (ethernet_input(frame, eth_netif)!=ERR_OK)
00152         { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
00153           pbuf_free(frame);
00154           frame = NULL;
00155         }
00156         break;
00157 
00158       default:
00159         pbuf_free(frame);
00160         frame = NULL;
00161         break;
00162       }
00163       
00164       /* pbuf_free(frame); */
00165   }
00166 
00167   
00168  
00169   
00170 }
00171 
00172 err_t eth_init(struct netif *netif) {
00173   LWIP_ASSERT("netif != NULL", (netif != NULL));
00174   
00175   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA);
00176   
00177   /* maximum transfer unit */
00178   netif->mtu = 0x2EA;
00179   
00180   /* device capabilities */
00181   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
00182   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_IGMP;
00183 
00184   netif->state = NULL;
00185   eth_netif = netif;
00186 
00187   netif->name[0] = IFNAME0;
00188   netif->name[1] = IFNAME1;
00189 
00190   /* We directly use etharp_output() here to save a function call.
00191    * You can instead declare your own function an call etharp_output()
00192    * from it if you have to do some checks before sending (e.g. if link
00193    * is available...) */
00194   netif->output          = etharp_output;
00195   netif->linkoutput      = eth_output;
00196 
00197   if (!pEth) pEth = new Ethernet(); // only create Ethernet object if required
00198 
00199   return ERR_OK;
00200 }
00201 
00202 void eth_free()
00203 {
00204   if(pEth)
00205     delete pEth;
00206   pEth = NULL;
00207 }
00208 
00209 void eth_address(char* mac) {
00210     pEth->address(mac);
00211 }
00212 
00213 Ethernet* eth_interface() {
00214     return pEth;
00215 }    
00216 
00217 #ifdef __cplusplus
00218 };
00219 #endif
00220 
00221 #endif