cancel
Showing results for 
Search instead for 
Did you mean: 

How configure LwIP to receive UDP broadcast using STM32CubeMx ?

Peko
Associate III

Hello,

I need to receive UDP Broadcast, but The "callback" function is never called.

"No broadcast" UDP communication works fine.

Is IwIP at all able to receive UDP broadcast?

I use LwIP 2.0.3, STM32F407, PHY DP83848

/*----- Default Value for IP_SOF_BROADCAST: 0 ---*/
#define IP_SOF_BROADCAST 1
/*----- Default Value for IP_SOF_BROADCAST_RECV: 0 ---*/
#define IP_SOF_BROADCAST_RECV 1
   
void udp_server_init(void)
{
     struct udp_pcb *upcb;
     err_t err;
       
     upcb = udp_new();
     ip_set_option(upcb, SOF_BROADCAST);
    
     if (upcb)
     {
         err = udp_bind(upcb, IP_ADDR_BROADCAST, 8000);
        
         if(err == ERR_OK)
         {
            udp_recv(upcb, udp_receive_callback, NULL);
         }
     }
}
 
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
   HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_2);
    	
   pbuf_free(p);		// Free the p buffer
    
   //udp_transmit(upcb, addr, udp2_tx_data, udp2_tx_len);
}  

Thank you

Peter

5 REPLIES 5
Piranha
Chief II

DHCP client in dhcp.c uses IP4_ADDR_ANY. Maybe try binding to that for the sake of experiment.

Check BFD bit in ETH->MACFFR and maybe try RA bit.

Also make sure that NETIF_FLAG_BROADCAST flag is set for netif->flags.

Peko
Associate III

Hi Piranha,

NETIF_FLAG_BROADCAST is enabled in: static void low_level_init(struct netif *netif)

static void low_level_init(struct netif *netif)
{ 
 ....
   /* Accept broadcast address and ARP traffic */
  /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
  #if LWIP_ARP
    netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
  #else 
    netif->flags |= NETIF_FLAG_BROADCAST;
  #endif /* LWIP_ARP */
  ...
  }

I tried too: err = udp_bind(upcb, IP_ADDR_BROADCAST, 8000);

I use MX_LWIP_Init() which it generates by STM32CubeMx.

It still doesn't work.

Peter

Piranha
Chief II

https://www.nongnu.org/lwip/2_0_x/group__udp__raw.html#gac7fbda8b12b9b9360e92b51e805e799e

"ipaddr - local IP address to bind with. Use IP4_ADDR_ANY to bind to all local interfaces."

It's the local address of network interface to which binding must be done. It can't be IP_ADDR_BROADCAST as broadcast address is not a local address.

Piranha
Chief II

Some additional information. IP_SOF_BROADCAST and IP_SOF_BROADCAST_RECV enables broadcast filtering per connection. By default those are off and that means that all connections receive broadcasts irrespective of SOF_BROADCAST. When per connection filtering is enabled, no connection receives broadcasts unless SOF_BROADCAST option is set for particular connection.

eduardo_reis
Associate III

I am trying to receive a message using the many broadcast IPs (192.168.1.255, IP_ADDR_BROADCAST, and also (0, 0, 0, 0)). None seemed to work. My network mask is (255.255.255.0).

Initially, I installed a PCB that already works. I tried the attempt below, but it did not work.

 

int UdpCommunication::ResetUdpBroadcast() {
  ip_addr_t broadcast_ip_address_v4;
  IP4_ADDR(&broadcast_ip_address_v4, 0, 0, 0, 0);  // Bind to IP_ADDR_ANY

  udp_remove(udp_pcb_broadcast_);
  udp_pcb_broadcast_ = udp_new();

  ip_set_option(udp_pcb_broadcast_, SOF_BROADCAST);

  auto lwip_error = udp_bind(udp_pcb_broadcast_, &broadcast_ip_address_v4, 10999);
  if (lwip_error != ERR_OK) {
    udp_remove(udp_pcb_broadcast_);
    return 1;
  }

  udp_recv(udp_pcb_broadcast_, UdpCommunication::UdpReceivedCallback, this);

  return 0;
}

 

 

Is there any hint or extra documentation on this?