野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 18321|回复: 1

请教LWIP DHCP使用

[复制链接]
发表于 2013-7-25 10:18:19 | 显示全部楼层 |阅读模式
本帖最后由 ¤疯]愚[者¤ 于 2013-7-25 11:28 编辑

我把STM32做成了CLIENT,使用的预先指定静态IP,这样做有些不方便。所以想用自动分配IP,想知道除了在lwipopts.h中#define LWIP_DHCP 1
#define LWIP_ARP  1
和将dhcp.h包含进相关的源文件里,还需要做其它的什么吗。DHCP是UDP协议工作的,是不是要用UDP才行。
void LwIP_Init( void )
{
  struct ip_addr ipaddr;
  struct ip_addr netmask;
  struct ip_addr gw;

   /*调用LWIP初始化函数,
   初始化网络接口结构体链表、内存池、pbuf结构体,启动协议栈管理进程*/
   lwip_init();                  
#if LWIP_DHCP                                                           
  ipaddr.addr = 0;
  netmask.addr = 0;
  gw.addr = 0;
#else               
  IP4_ADDR(&ipaddr, 192, 168, 0, 16);                  //设置网络接口的ip地址
  IP4_ADDR(&netmask, 255, 255, 255, 0);                //子网掩码
  IP4_ADDR(&gw, 192, 168, 0, 1);  //网关
#endif

  netif_add(&enc28j60, &ipaddr, &netmask, &gw, NULL, eernetif_init, eernet_input);
  netif_set_default(&enc28j60);

#if LWIP_DHCP                                   //若使用了DHCP
  dhcp_start(&enc28j60);           //启动DHCP
#endif
  netif_set_up(&enc28j60); //使能enc28j60接口
}

void tcp_client_init(void)
{
        struct tcp_pcb* client_pcb;
        struct tcp_client_app_arg* app_arg;

        printf("tcp client inti\n");

        destip.addr = (uint32_t)192+(168<<8)+(0<<16)+(105<<24);

        client_pcb = tcp_new();
        if(client_pcb != NULL)
        {
                tcp_arg(client_pcb, mem_calloc(sizeof(struct tcp_client_app_arg), 1));   
                app_arg = client_pcb->callback_arg;
                app_arg->app_state = CLIENT_WAITING_FOR_CONNECTION;
                tcp_connect(client_pcb, &destip, 2200, tcp_client_connected);
        }
        else
        {

                printf("tcp alloc failed\n");
        }
}

void LwIP_Periodic_Handle(__IO uint32_t localtime)
{        
        if(ETH_INT == 1)
        {
                 ETH_INT = 0;
                    /* Read a received packet from the Ethernet buffers and send it to the lwIP for handling */
                IN:        ethernetif_input(&enc28j60);                         //轮询是否接收到数据
        }
        if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2) == RESET) goto IN;

        /* TCP periodic process every 250 ms */
        if (localtime - TCPTimer >= TCP_TMR_INTERVAL)
        {
           TCPTimer =  localtime;
           tcp_tmr();                 //每250ms调用一次
        }
        /* ARP periodic process every 5s */
        if (localtime - ARPTimer >= ARP_TMR_INTERVAL)
        {
           ARPTimer =  localtime;
           etharp_tmr();          //每5s调用一次
        }

#if LWIP_DHCP
          /* Fine DHCP periodic process every 500ms */
          if (localtime - DHCPfineTimer >= DHCP_FINE_TIMER_MSECS)
          {
            DHCPfineTimer =  localtime;
            dhcp_fine_tmr();
          }
          /* DHCP Coarse periodic process every 60s */
          if (localtime - DHCPcoarseTimer >= DHCP_COARSE_TIMER_MSECS)
          {
            DHCPcoarseTimer =  localtime;
            dhcp_coarse_tmr();
          }
#endif






回复

使用道具 举报

发表于 2013-7-25 18:53:02 | 显示全部楼层
没做过 自动分配IP ,所以我们也不了解
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-8 04:42 , Processed in 0.051274 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表