cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to receive UDP broadcast packets

rrooyen
Associate III

I am able to receive UDP unicast packets on the Nucleo-h563ZI using the netconn API, however I cannot receive broadcast UDP packets (global or subnet). I have attached my lwiopts.h for reference along with the relevant discovery service code below. Note that netif->flags has NETIF_FLAG_BROADCAST set in addition to NETIF_FLAG_ETHARP. I was unable to find any broadcast related settings in the IOC file for the ETH peripheral nor related HAL_ETH_xyz calls. I did try enabling broadcast in the MACFilter, but this had no effect.

#define NET_TASK_DISCO_PORT			(5555)
#define NET_TASK_DISCO_REQUEST			"SCPI_DISCOVERY"
#define NET_TASK_DISCO_RESPONSE			"SCPI_DEVICE"
#define NET_TASK_DISCO_RESP_LENGTH		(64)

//
// @brief Discovery application
// @return none
//
static void disco(void *pArgument)
{
	char		   response[NET_TASK_DISCO_RESP_LENGTH];
	uint16_t	   length;
	void		  *pBuffer;
	void		  *pSend;
	struct netbuf *prBuffer;
	struct netbuf *prSend;
	trNet		  *prNet = (trNet*)pArgument;

	// Validate argument[s]
	ASSERT(NULL != prNet);

	// Create connection and bind to port
	if (NULL == (prNet->rDisco.prConnection = netconn_new(NETCONN_UDP)))
	{
		LOGERROR(eErrorOutOfResources);

		return;
	}
	netconn_bind(prNet->rDisco.prConnection, IP_ADDR_ANY, NET_TASK_DISCO_PORT);

	// Forever loop
	for(;;)
	{
		// Receive request
		if (ERR_OK == netconn_recv(prNet->rDisco.prConnection, &prBuffer))
		{
			// Get receive buffer
			netbuf_data(prBuffer, &pBuffer, &length);
			LOG(eLogLevelDebug, "%s", pBuffer);

			// Check request
			if (0 == strncmp(pBuffer, NET_TASK_DISCO_REQUEST, length))
			{
				// Get send buffer
				if (NULL != (prSend = netbuf_new()))
				{
					// Build response
					snprintf(response, sizeof(response) - 1,
						"%s;IDN=%s;IP=%s;PORT=%u", NET_TASK_DISCO_RESPONSE,
						"SPPTS-101", "192.168.0.10", NET_TASK_DISCO_PORT);
					LOG(eLogLevelDebug, "%s", response);

					// Allocate and copy buffer
					pSend = netbuf_alloc(prSend, strlen(response));
					memcpy(pSend, response, strlen(response));

					// Send response
					netconn_sendto(prNet->rDisco.prConnection, prSend,
						netbuf_fromaddr(prBuffer), netbuf_fromport(prBuffer));

					// Return send buffer
					netbuf_delete(prSend);
				}
			}

			// Return buffer
			netbuf_delete(prBuffer);
		}
	}
}

Here is the successful response when sending a unicast packet:

bob@xyz:~$ echo -n "SCPI_DISCOVERY" | nc -u 192.168.50.123 5555
SCPI_DEVICE;IDN=SPPTS-101;IP=192.168.0.10;PORT=5555

Relevent settings in lwipopts.h

#define IP_SOF_BROADCAST 1
#define IP_SOF_BROADCAST_RECV 1
#define LWIP_BROADCAST_PING 1

6 REPLIES 6
mbarg.1
Lead

Your ethernet driver is not boradcast enabled.

Check eth setting or share your code and we can show how to enable.

Thank you for the quick response and I have attached the ethernetif code for review.

Just checking in as I have not received a reply after uploading the ethernetif code.

waclawek.jan
Super User

What's the content of ETH_MACPFR register?

JW

Hi JW,

Please see the attached screenshot of the ETH_MACPFR register contents. I do see that the PR bit is reset, which could be the reason for not seeing broadcast packets. Is there a function in LwIP or the driver that typically handles this or should I set the bit directly?

waclawek.jan
Super User

I don't think you'd need the promiscuous mode to be enabled.

What I was thinking was, that the broadcasts are disabled by ETH_MACPFR.DBF being set - but as you've shown that's not the case here.

At this point you should have the 'H5 board isolated from any network traffic, so that you can transmit to it in a controlled way, and observe (e.g. in debugger) at the ETH level, what packets are received and which are rejected/filtered.

JW