📄 rt_eth3.c
字号:
/* * 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 based in one file part of the lwIP TCP/IP stack. The file is: *//* ethernetif.c *//* which author is: Adam Dunkels <adam@sics.se> *//* *//* This file has been modified by Sergio Perez Alca駃z <serpeal@upvnet.upv.es> *//* Departamento de Inform醫ica de Sistemas y Computadores *//* Universidad Polit閏nica de Valencia *//* Valencia (Spain) *//* *//* The RTL-lwIP project has been supported by the Spanish Government Research *//* Office (CICYT) under grant TIC2002-04123-C03-03 *//* *//* Copyright (c) March, 2003 SISTEMAS DE TIEMPO REAL EMPOTRADOS, FIABLES Y *//* DISTRIBUIDOS BASADOS EN COMPONENTES *//* *//* This program is free software; you can redistribute it and/or modify *//* it under the terms of the GNU General Public License as published by *//* the Free Software Foundation; either version 2 of the License, or *//* (at your option) any later version. *//* *//* This program is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warrabnty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *//* GNU General Public License for more details. *//* *//* You should have received a copy of the GNU General Public License *//* along with this program; if not, write to the Free Software *//* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* *//* Linking RTL-lwIP statically or dynamically with other modules is making a *//* combined work based on RTL-lwIP. Thus, the terms and conditions of the GNU *//* General Public License cover the whole combination. *//* *//* As a special exception, the copyright holders of RTL-lwIP give you *//* permission to link RTL-lwIP with independent modules that communicate with *//* RTL-lwIP solely through the interfaces, regardless of the license terms of *//* these independent modules, and to copy and distribute the resulting combined *//* work under terms of your choice, provided that every copy of the combined *//* work is accompanied by a complete copy of the source code of RTL-lwIP (the *//* version of RTL-lwIP used to produce the combined work), being distributed *//* under the terms of the GNU General Public License plus this exception. An *//* independent module is a module which is not derived from or based on *//* RTL-lwIP. *//* *//* Note that people who make modified versions of RTL-lwIP are not obligated to *//* grant this special exception for their modified versions; it is their choice *//* whether to do so. The GNU General Public License gives permission to *//* release a modified version without this exception; this exception also makes *//* it possible to release a modified version which carries forward this *//* exception. *//*********************************************************************************/#include "lwip/opt.h"#include "lwip/def.h"#include "lwip/mem.h"#include "lwip/pbuf.h"#include "lwip/sys.h"#include "netif/etharp.h"#include "netif/rt_eth_exports.h"#include "netif/ethernetif.h"#include <unistd.h>#include "bcopy.h"#include <signal.h>#include <rtl_sema.h>#define IFNAME0 'e'#define IFNAME1 't'#define RTETH3_SIGNAL RTL_SIGUSR2ethernetif_thread_t rt_eth3_thread;struct rt_eth3 { struct eth_addr *ethaddr;};static const struct eth_addr rt_eth3_ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};static int rt_eth3_fd;static struct netif *rt_eth3_netif;/* Forward declarations. */static void rt_eth3_input(struct pbuf *p, struct netif *netif);static err_t rt_eth3_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr);static void rt_eth3_ethernetif_thread(void *arg);/*-----------------------------------------------------------------------------------*/static voidrt_eth3_low_level_init(struct netif *netif){ struct rt_eth3 *rt_eth3; rt_eth3 = netif->state; /* Do whatever else is needed to initialize interface. */ if((rt_eth3_fd=open(RTETH3_NAME,0)) == -1) rtl_printf("ERROR OPENING /dev/%s0\n",RTETH3_NAME); /* Obtain MAC address from network interface. */ ioctl(rt_eth3_fd, 2, (unsigned long) rt_eth3->ethaddr->addr); /* We set an IP filter to the ethernet card */ ioctl(rt_eth3_fd, 1, (unsigned long) netif->ip_addr.addr);// rtl_printf("ADRESS: %x:%x:%x:%x:%x:%x \n",rt_eth3->ethaddr->addr[0],rt_eth3->ethaddr->addr[1],rt_eth3->ethaddr->addr[2],rt_eth3->ethaddr->addr[3],rt_eth3->ethaddr->addr[4],rt_eth3->ethaddr->addr[5]); sys_thread_new(rt_eth3_ethernetif_thread, netif, 0);}/*-----------------------------------------------------------------------------------*/static void rt_eth3_ethernetif_thread(void *arg){ struct sched_param p; p . sched_priority = 100000; pthread_setschedparam (pthread_self(), SCHED_FIFO, &p); do{ rt_eth3_input(NULL, rt_eth3_netif); }while(1);}/*-----------------------------------------------------------------------------------*//* * rt_eth3_low_level_output(): * * Should do the actual transmission of the packet. The packet is * contained in the pbuf that is passed to the function. This pbuf * might be chained. * *//*-----------------------------------------------------------------------------------*/err_trt_eth3_low_level_output(struct netif *rt_eth3, struct pbuf *p){ struct pbuf *q; unsigned char buf[1536]; unsigned char *bufptr; //initiate transfer; bufptr = buf; for(q = p; q != NULL; q = q->next) { /* Send the data from the pbuf to the interface, one pbuf at a time. The size of the data in each pbuf is kept in the ->len variable. */ bcopy(q->payload, bufptr, q->len); bufptr += q->len; } //signal that packet should be sent; { int tmp; int counter=0; while((tmp = write(rt_eth3_fd,buf,p->tot_len)) == -1){ counter++; usleep(1); } } return ERR_OK;}/*-----------------------------------------------------------------------------------*//* * rt_eth3_low_level_input(): * * Should allocate a pbuf and transfer the bytes of the incoming * packet from the interface into the pbuf. * *//*-----------------------------------------------------------------------------------*/static struct pbuf *rt_eth3_low_level_input(struct rt_eth3 *rt_eth3){ struct pbuf *p, *q; struct memory receive_buffer; unsigned char *bufptr; u16_t len; /* Obtain the size of the packet and put it into the "len" variable. */ len = read(rt_eth3_fd,(void *) &receive_buffer,1536); /* We allocate a pbuf chain of pbufs from the pool. */ p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL); if(p != NULL) { /* We iterate over the pbuf chain until we have read the entire packet into the pbuf. */ bufptr = receive_buffer.mem; for(q = p; q != NULL; q = q->next) { /* Read enough bytes to fill this pbuf in the chain. The avaliable data in the pbuf is given by the q->len variable. */ bcopy(bufptr, q->payload, q->len); bufptr += q->len; } } else { rtl_printf("ERROR:Not enough memory!!!\n"); sys_arch_close(); } return p; }/*-----------------------------------------------------------------------------------*//* * rt_eth3_output(): * * This function is called by the TCP/IP stack when an IP packet * should be sent. It calls the function called rt_eth3_low_level_output() to * do the actuall transmission of the packet. * *//*-----------------------------------------------------------------------------------*/static err_trt_eth3_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr){ /* resolve hardware address, then send (or queue) packet */ return etharp_output(netif, ipaddr, p);}/*-----------------------------------------------------------------------------------*//* * rt_eth3_input(): * * This function should be called when a packet is ready to be read * from the interface. It uses the function rt_eth3_low_level_input() that * should handle the actual reception of bytes from the network * interface. * *//*-----------------------------------------------------------------------------------*/static voidrt_eth3_input(struct pbuf *o, struct netif *netif)//(struct netif *netif){ /* Ethernet protocol layer */ struct ethernetif *ethernetif; struct eth_hdr *ethhdr; struct pbuf *p; struct rt_eth3 *rt_3c905if = netif->state; ethernetif = netif->state; /* move received packet into a new pbuf */ p = rt_eth3_low_level_input(rt_3c905if); /* no packet could be read, silently ignore this */ if (p == NULL) return; /* points to packet payload, which starts with an Ethernet header */ ethhdr = p->payload; switch (htons(ethhdr->type)) { /* IP packet? */ case ETHTYPE_IP: /* update ARP table */ etharp_ip_input(netif, p); /* skip Ethernet header */ pbuf_header(p, -14); /* pass to network layer */ netif->input(p, netif); break; case ETHTYPE_ARP: /* pass p to ARP module */ etharp_arp_input(netif, rt_3c905if->ethaddr, p); break; default: pbuf_free(p); p = NULL; break; }}/*-----------------------------------------------------------------------------------*/void rt_eth3_etharp_timer(int signo){ etharp_tmr(); sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)rt_eth3_etharp_timer, NULL);}/*-----------------------------------------------------------------------------------*//* * rt_eth3_init(): * * Should be called at the beginning of the program to set up the * network interface. It calls the function rt_eth3_low_level_init() to do the * actual setup of the hardware. * *//*-----------------------------------------------------------------------------------*/err_trt_eth3_init(struct netif *netif){ struct rt_eth3 *rt_eth3; rt_eth3 = mem_malloc(sizeof(struct rt_eth3)); netif->state = rt_eth3; netif->name[0] = IFNAME0; netif->name[1] = IFNAME1; netif->output = rt_eth3_output; netif->linkoutput = rt_eth3_low_level_output; rt_eth3->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]); netif->hwaddr_len = 6; rt_eth3_netif = netif; rt_eth3_low_level_init(netif); etharp_init(); sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)rt_eth3_etharp_timer, NULL); return ERR_OK;}/*-----------------------------------------------------------------------------------*/void rt_eth3_close(void){ /* Closing the rt_eth3 card */ close(rt_eth3_fd);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -