added to the udp server code
request is received well, no reply seen in the Qt application, could be because i had to correct code to this version of Qt creator. Waiting on validation of correct changes from Wim Dams
This commit is contained in:
@@ -8,6 +8,7 @@
|
|||||||
#include "UDP_broadcast.h"
|
#include "UDP_broadcast.h"
|
||||||
|
|
||||||
static const char *TAG = "UDP_broadcast";
|
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*)
|
* @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){
|
if(reply != NULL && owner != NULL){
|
||||||
LOG_DEBUG(TAG,"set: %s",reply);
|
LOG_DEBUG(TAG,"set: %s",reply);
|
||||||
strncpy(owner->reply,reply,sizeof(owner->reply));
|
strncpy(owner->reply,reply,sizeof(owner->reply));
|
||||||
|
strncpy(reply_str,reply,sizeof(reply_str));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
@@ -238,12 +240,21 @@ char* get_owner_details_reply(owner_details_t *owner){
|
|||||||
* @param port
|
* @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 i;
|
||||||
int len;
|
int len;
|
||||||
char *pc;
|
char *pc;
|
||||||
char data[1024];
|
char data[MAX_DATA_SIZE];
|
||||||
char source_ip_str[16];
|
char source_ip_str[16];
|
||||||
|
struct pbuf *p_data;
|
||||||
|
|
||||||
|
memset(data, 0, sizeof(data));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Convert the source IP address to a string for printing.
|
// Convert the source IP address to a string for printing.
|
||||||
ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str));
|
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) {
|
if (p != NULL) {
|
||||||
pc = (char*)p->payload;
|
pc = (char*)p->payload;
|
||||||
len = p->tot_len;
|
len = p->tot_len;
|
||||||
|
p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(reply_str), PBUF_RAM);
|
||||||
for(i = 0; i < len; i++) {
|
if (p_data == NULL){
|
||||||
data[i] = pc[i];
|
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){
|
if(connection != NULL){
|
||||||
err = udp_bind(connection, IP_ANY_TYPE, 64000);
|
err = udp_bind(connection, IP_ANY_TYPE, 64000);
|
||||||
if(err == ERR_OK){
|
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");
|
LOG_INFO(TAG,"initialising UDP server succesfull, callback running");
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|||||||
@@ -111,8 +111,8 @@ int main(void)
|
|||||||
MX_LWIP_Init();
|
MX_LWIP_Init();
|
||||||
MX_QUADSPI_Init();
|
MX_QUADSPI_Init();
|
||||||
/* USER CODE BEGIN 2 */
|
/* USER CODE BEGIN 2 */
|
||||||
init_UDP_server();
|
|
||||||
owner_details_t owner;
|
owner_details_t owner;
|
||||||
|
init_UDP_server();
|
||||||
|
|
||||||
if(!set_owner_details(&owner, "joran", "vn")){
|
if(!set_owner_details(&owner, "joran", "vn")){
|
||||||
LOG_DEBUG(TAG,"error");;
|
LOG_DEBUG(TAG,"error");;
|
||||||
|
|||||||
Reference in New Issue
Block a user