打印

请教LWIP DHCP使用

[复制链接]
560|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
哈金|  楼主 | 2018-7-4 16:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我把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
");

        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
");
        }
}

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

使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

432

主题

433

帖子

1

粉丝