【原创】LWIP-1.2.0+RTL8019+uC/OS-II--代码篇2

本文详细解析了LWIP中的ethernetif.c文件,介绍了其核心函数ethernetif_input的功能实现过程,包括如何接收、处理以太网数据包,并根据不同类型的数据包进行相应的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在LWIP中需要添加的驱动的位置:ethernetif.c文件

void ethernetif_input(struct netif *netif)
{
  struct ethernetif *ethernetif;
  struct eth_hdr *ethhdr;
  struct pbuf *p;
 
  while(1)
  {
   ethernetif = netif->state;
   do{
    p = low_level_input(netif);
    if (p == NULL)
     OSTimeDlyHMSM(0,0,0,100);
   }while(p == NULL);

 #if LINK_STATS
   lwip_stats.link.recv++;
 #endif

   ethhdr = p->payload;
    
   switch (htons(ethhdr->type)) {

    case ETHTYPE_IP:
 #if 1 
      etharp_ip_input(netif, p); //更新ARP表
 #endif
   //跳过以太网头部字段
      pbuf_header(p, -sizeof(struct eth_hdr));
      //传递到网络层
      netif->input(p, netif);
      break;
      
     case ETHTYPE_ARP:
        //将netif传递到ARP模块
        p = etharp_arp_input(netif, ethernetif->ethaddr, p);
        if(p != NULL) {
         low_level_output(netif, p);
         pbuf_free(p);
        p = NULL;
       }        
        break;
     default:
        pbuf_free(p);
       p = NULL;
        break;
   }       
  }
}

 

static struct pbuf * low_level_input(struct netif *netif)
{
 struct ethernetif *ethernetif;
   struct pbuf *p = NULL, *q;
   u16_t len;
   
   int flag;
   
   unsigned int i;
 unsigned char buffer[4096];
 unsigned char *des;
 unsigned char *data;
 
 data =  buffer;
    
 ethernetif = netif->state;
   
 flag = board_eth_rcv(data,&len);
 
#if 0 
 if(flag == 1) {
  des = data;
  LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));   
  for(i=0; i<len; i++) {
   LWIP_DEBUGF(RTL8019_DEBUG, ("0x%02X ",*des++));
  }
  LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));
  LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));
  LWIP_DEBUGF(RTL8019_DEBUG, ("the receive data length is %d\r\n",len));
  LWIP_DEBUGF(RTL8019_DEBUG, ("\r\n"));
  
 }
#endif 

 if(flag == 1) { 
  #if ETH_PAD_SIZE
   len += ETH_PAD_SIZE;
  #endif

    //p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
    //Modify by Small.Box at 06.10.16
    if(p == NULL) {
     p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
    }
  
    if (p != NULL) {

   #if ETH_PAD_SIZE
       pbuf_header(p, -ETH_PAD_SIZE);
   #endif

      for(q = p; q != NULL; q = q->next) {
    memcpy(q->payload,(unsigned char *)data, q->len);
         data += q->len;
      }

   #if ETH_PAD_SIZE
       pbuf_header(p, ETH_PAD_SIZE);
   #endif

   #if LINK_STATS
       lwip_stats.link.recv++;
   #endif    
    } else {
   #if LINK_STATS
       lwip_stats.link.memerr++;
       lwip_stats.link.drop++;
   #endif   
    }
 }

  return p;   
}

 

static err_t low_level_output(struct netif *netif, struct pbuf *p)
{
  struct ethernetif *ethernetif;
  struct pbuf *q;
 
  ethernetif= netif->state;
 
#if ETH_PAD_SIZE
  pbuf_header(p, -ETH_PAD_SIZE);
#endif

  for(q = p; q != NULL; q = q->next) {
     board_eth_send(q->payload,q->len);
  }

#if ETH_PAD_SIZE
  pbuf_header(p, ETH_PAD_SIZE);
#endif
 
#if LINK_STATS
  lwip_stats.link.xmit++;
#endif /* LINK_STATS */     

  return ERR_OK;
}

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值