diff --git a/project/Core/Src/UDP_broadcast.c b/project/Core/Src/UDP_broadcast.c index 3f75cbe..5c99a12 100644 --- a/project/Core/Src/UDP_broadcast.c +++ b/project/Core/Src/UDP_broadcast.c @@ -8,6 +8,7 @@ #include "UDP_broadcast.h" static const char *TAG = "UDP_broadcast"; +static char reply_str[100] = "|no reply formatted yet|"; /** * @fn void owner_details_error_handler(owner_details_t*, char*, char*) @@ -109,6 +110,7 @@ static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){ if(reply != NULL && owner != NULL){ LOG_DEBUG(TAG,"set: %s",reply); strncpy(owner->reply,reply,sizeof(owner->reply)); + strncpy(reply_str,reply,sizeof(reply_str)); return 1; } else{ @@ -238,12 +240,21 @@ char* get_owner_details_reply(owner_details_t *owner){ * @param port */ -static void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){ +#define MAX_DATA_SIZE 50 // Define the maximum expected data size +#define UDP_QUESTION1 "Where are you?v1.0" + +static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct pbuf *p, const ip_addr_t *addr, u16_t port){ int i; int len; char *pc; - char data[1024]; + char data[MAX_DATA_SIZE]; char source_ip_str[16]; + struct pbuf *p_data; + + memset(data, 0, sizeof(data)); + + + // Convert the source IP address to a string for printing. ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); @@ -251,14 +262,34 @@ static void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p if (p != NULL) { pc = (char*)p->payload; len = p->tot_len; - - for(i = 0; i < len; i++) { - data[i] = pc[i]; + p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), PBUF_RAM); + if (p_data == NULL){ + LOG_WARN(TAG,"udp_receive_callback: unable to allocated data buffer for reply"); + } + else if(len <= MAX_DATA_SIZE){ + for(i = 0; i < len; i++) { + data[i] = pc[i]; + } + + LOG_INFO(TAG,"udp_receive_callback: received data from %s at port: %d: %s",source_ip_str,port,data); + if(strcmp(data,UDP_QUESTION1) == 0){ + *((uint32_t*)p_data->payload) = reply_str; + p_data->len = sizeof(reply_str); + p_data->tot_len = sizeof(reply_str); + udp_sendto(connection, p_data, addr, port); + LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str); + } + + } + else{ + LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE); } - pbuf_free(p); - LOG_DEBUG(TAG,"received data from %s at port: %d: %s",source_ip_str,port,data); } + else{ + LOG_WARN(TAG,"udp_receive_callback: input buffer was a NULL pointer"); + } + pbuf_free(p); } /** @@ -276,7 +307,7 @@ err_t init_UDP_server(){ if(connection != NULL){ err = udp_bind(connection, IP_ANY_TYPE, 64000); if(err == ERR_OK){ - udp_recv(connection, udp_receive_callback, NULL); + udp_recv(connection, udp_receive_callback,NULL); LOG_INFO(TAG,"initialising UDP server succesfull, callback running"); } else{ diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 99f6437..48d20c3 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -111,8 +111,8 @@ int main(void) MX_LWIP_Init(); MX_QUADSPI_Init(); /* USER CODE BEGIN 2 */ - init_UDP_server(); owner_details_t owner; + init_UDP_server(); if(!set_owner_details(&owner, "joran", "vn")){ LOG_DEBUG(TAG,"error");;