⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rtk8019as_if.c

📁 lwIP-Softools-11Jul2002-alpha
💻 C
字号:
/* * Contains the network interface code and driver calls for the RTK8019as * 10megabit ethernet controller chip. * * (C) 2002 Softools, Inc. * (C) 2002 Compendium Technologies, Inc. * All rights reserved. * *//* * Copyright (c) 2001, Swedish Institute of Computer Science. * All rights reserved.  * * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met:  * 1. Redistributions of source code must retain the above copyright  *    notice, this list of conditions and the following disclaimer.  * 2. Redistributions in binary form must reproduce the above copyright  *    notice, this list of conditions and the following disclaimer in the  *    documentation and/or other materials provided with the distribution.  * 3. Neither the name of the Institute nor the names of its contributors  *    may be used to endorse or promote products derived from this software  *    without specific prior written permission.  * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF  * SUCH DAMAGE.  * * This file is part of the lwIP TCP/IP stack. *  * Author: Adam Dunkels <adam@sics.se> * * $Id: RTK8019AS_if.c,v 1.6 2002/06/19 06:57:35 jctoman Exp $ */#include "lwip/debug.h"#include "lwip/opt.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/pbuf.h"#include "lwip/sys.h"#include "lwip/stats.h"#include "netif/arp.h"#include "RTK8019AS.h"/* Name of the ethernet interface */#define IFNAME0 'e'#define IFNAME1 't'#define RX_DEBUGstatic const struct eth_addr ethbroadcast = {0xffff,0xffff,0xffff};/** * Send a packet on this ethernet interface. The sending work is done by * RealTekSendPacket(). */static err_t _nearcallRTK8019AS_if_output(struct netif *netif, struct pbuf *p,	 	    struct ip_addr *ipaddr){  struct ethernetif *ethernetif;  struct pbuf *q;  struct eth_hdr *ethhdr;  struct eth_addr *dest, mcastaddr;  struct ip_addr *queryaddr;  err_t err;    ethernetif = netif->state;#ifdef TX_DEBUG  printf("packet length: %d\n", p->tot_len);#endif  /* Make room for Ethernet header. */  if(pbuf_header(p, 14) != 0) {#ifdef TX_DEBUG    puts("allocating extra space for header\n");#endif    /* The pbuf_header() call shouldn't fail, but we allocate an extra       pbuf just in case. */    q = pbuf_alloc(PBUF_LINK, 14, PBUF_RAM);    if(q == NULL) {#ifdef LINK_STATS      stats.link.drop++;      stats.link.memerr++;#endif /* LINK_STATS */            return ERR_MEM;    }    pbuf_chain(q, p);    p = q;  }  /* Construct Ethernet header. Start with looking up deciding which     MAC address to use as a destination address. Broadcasts and     multicasts are special, all other addresses are looked up in the     ARP table. */  queryaddr = ipaddr;  if(ip_addr_isany(ipaddr) ||     ip_addr_isbroadcast(ipaddr, &(netif->netmask))) {    dest = (struct eth_addr *)&ethbroadcast;  } else if(ip_addr_ismulticast(ipaddr)) {    /* Hash IP multicast address to MAC address. */#ifdef NOT_USED    mcastaddr.addr[0] = HTONS(0x01 << 8);    mcastaddr.addr[1] = HTONS((0x5e << 8) |			      (ip4_addr2(ipaddr) & 0x7f));    mcastaddr.addr[2] = HTONS((ip4_addr3(ipaddr) << 8) |			      ip4_addr4(ipaddr));#endif    dest = &mcastaddr;  } else {    if(ip_addr_maskcmp(ipaddr, &(netif->ip_addr), &(netif->netmask))) {      /* Use destination IP address if the destination is on the same         subnet as we are. */      queryaddr = ipaddr;    } else {      /* Otherwise we use the default router as the address to send         the Ethernet frame to. */      queryaddr = &(netif->gw);    }    dest = arp_lookup(queryaddr);  }  /* If the arp_lookup() didn't find an address, we send out an ARP     query for the IP address. */  if(dest == NULL) {#ifdef TX_DEBUG    puts("no address, doing query\n");#endif    q = arp_query(netif, ethernetif->ethaddr, queryaddr);    if(q != NULL) {      err = RealTekSendPacket(q);      pbuf_free(q);      return err;    }#ifdef LINK_STATS    stats.link.drop++;    stats.link.memerr++;#endif /* LINK_STATS */              return ERR_MEM;  }  ethhdr = p->payload;  ethhdr->dest = *dest;  ethhdr->src = *(ethernetif->ethaddr);  //ethhdr->type = htons(ETHTYPE_IP);  ethhdr->type = ETHTYPE_IP;#ifdef TX_DEBUG  {      char address[20];      printf("source addr: %s\n", etherAddressToString(address, &ethhdr->src));      printf("dest addr  : %s\n", etherAddressToString(address, &ethhdr->dest));      printf("type       : %d\n", ethhdr->type);  }#endif    return RealTekSendPacket(p);}/** * Get a packet from the RTK8019as interface and route it, if there is a  * packet available. The problem with this implementation is that error * conditions are silent and unreported. *  * @return 1 if a packet is detected, 0 otherwise */u16_tRTK8019AS_if_input(struct netif *netif){    struct eth_hdr *ethhdr;    struct pbuf *p;    struct ethernetif *interface;    p = RealTekRecvPacket();    if(p != NULL) {        ethhdr = p->payload;            //switch (htons(ethhdr->type)) {        switch (ethhdr->type) {        case ETHTYPE_IP:#ifdef RX_DEBUG            //printf("got IP packet\n");#endif            arp_ip_input(netif, p);            pbuf_header(p, -14);            netif->input(p, netif);            break;        case ETHTYPE_ARP:#ifdef RX_DEBUG            puts("got ARP\n");#endif            interface = netif->state;            p = arp_arp_input(netif, interface->ethaddr, p);            if (p != NULL)            {#ifdef RX_DEBUG                puts("send ARP\n");#endif                RealTekSendPacket(p);                pbuf_free(p);            }            break;        default:#ifdef RX_DEBUG            puts("unknown packet\n");#endif            pbuf_free(p);            break;        }        return 1;    }    else    {        return 0;    }}/** * Called somehow at the beginning of the program to set up the network  * interface. */void _nearcallRTK8019AS_if_init(struct netif *netif){    struct ethernetif *ethernetif;    // Allocate ethernet address space    ethernetif = mem_malloc(sizeof(struct ethernetif));    netif->state = ethernetif;    netif->name[0] = IFNAME0;    netif->name[1] = IFNAME1;    netif->output = RTK8019AS_if_output;        ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);      RealTekInit(netif);    arp_init();   // This wipes out the entire arp cache, even if there is more                  // than one interface.}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -