Question
Problem With UDP Broadcast receive
I am using the STM32F107VCT6 controller. use STMcubeIDE.
I write the code of the UDP Server. but when I change the IP, subnet, and Gateway using TCP and reset the device. I do not get Broadcast info or the udp_recv function not working.
void udpServer_init(void)
{
// UDP Control Block structure
struct udp_pcb *upcb;
// struct udp_pcb *upcb;
err_t err;
/* 1. Create a new UDP control block */
upcb = udp_new();
if(upcb == NULL){
HAL_UART_Transmit(&huart1, "fail\n", 5, 5);
}
/* 2. Bind the upcb to the local port */
err = udp_bind(upcb, IP_ADDR_ANY , 1225); // 7 is the server UDP port
/* 3. Set a receive callback for the upcb */
if(err == ERR_OK)
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP bind\n", 20,20);
udp_recv(upcb, udp_receive_callback, NULL);
}
else
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP remo\n", 20,20);
udp_remove(upcb);
}
}
// udp_receive_callback will be called, when the client sends some data to the server
/* 4. Process the datagram packet and send a reply to client. */
void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP rec\n", 20,20);
struct pbuf *txBuf;
/* Get the IP of the Client */
char *remoteIP = ipaddr_ntoa(addr);
char buf[100];
memset (buf, '\0', 100);
char load[100];
memset (load, '\0', 100);
strcpy(load,p->payload);
uint8_t len = 0;
if(load[0]=='I'&& load[1]=='N'&&load[2]=='F'&& load[3]=='O')
{
HAL_UART_Transmit(&huart1, "ENTER IN UDP rec1\n", 20,20);
len = sprintf(buf,"IP = %u.%u.%u.%u \n SUBNET = %u.%u.%u.%u \n GETWAY = %u.%u.%u.%u \n",(char*)IP_ADDRESS[0],(char*)IP_ADDRESS[1],(char*)IP_ADDRESS[2],(char*)IP_ADDRESS[3]
,(char*)NETMASK_ADDRESS[0],(char*)NETMASK_ADDRESS[1],(char*)NETMASK_ADDRESS[2],(char*)NETMASK_ADDRESS[3]
,(char*)GATEWAY_ADDRESS[0],(char*)GATEWAY_ADDRESS[1],(char*)GATEWAY_ADDRESS[2],(char*)GATEWAY_ADDRESS[3]);
}
else
{
len = sprintf(buf,"Invalid Command");
}
/* allocate pbuf from RAM*/
txBuf = pbuf_alloc(PBUF_TRANSPORT,len, PBUF_RAM);
/* copy the data into the buffer */
pbuf_take(txBuf, buf, len);
/* Connect to the remote client */
udp_connect(upcb, addr, port);
/* Send a Reply to the Client */
udp_send(upcb, txBuf);
// udp_sendto(upcb, txBuf, addr, port);
/* free the UDP connection, so we can accept new clients */
udp_disconnect(upcb);
/* Free the p_tx buffer */
pbuf_free(txBuf);
/* Free the p buffer */
pbuf_free(p);
}
