added some UDP server code

initialising UDP server succesfull, callback running
have not yet tried to receive a broadcast
This commit is contained in:
joran2738
2023-11-07 23:37:16 +01:00
parent 142aad264c
commit bdf445b708
3 changed files with 73 additions and 30 deletions

View File

@@ -40,5 +40,10 @@ char* get_owner_details_name(owner_details_t*);
char* get_owner_details_surname(owner_details_t*);
char* get_owner_details_reply(owner_details_t*);
err_t init_UDP_server();
#endif /* INC_UDP_BROADCAST_H_ */

View File

@@ -227,4 +227,68 @@ char* get_owner_details_reply(owner_details_t *owner){
return owner->reply;
}
/**
* @fn void udp_receive_callback(void*, struct udp_pcb*, struct pbuf*, const ip_addr_t*, u16_t)
* @brief
*
* @param arg
* @param upcb
* @param p
* @param addr
* @param port
*/
static void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port){
int i;
int len;
char *pc;
char data[1024];
char source_ip_str[16];
// Convert the source IP address to a string for printing.
ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str));
if (p != NULL) {
pc = (char*)p->payload;
len = p->tot_len;
for(i = 0; i < len; i++) {
data[i] = pc[i];
}
pbuf_free(p);
LOG_DEBUG(TAG,"received data from %s at port: %d: %s",source_ip_str,port,data);
}
}
/**
* @fn err_t init_UDP_server()
* @brief
*
* @return
*/
err_t init_UDP_server(){
struct udp_pcb *connection;
err_t err;
LOG_INFO(TAG,"initialising UDP server");
connection = udp_new();
if(connection != NULL){
err = udp_bind(connection, IP_ANY_TYPE, 64000);
if(err == ERR_OK){
udp_recv(connection, udp_receive_callback, NULL);
LOG_INFO(TAG,"initialising UDP server succesfull, callback running");
}
else{
udp_remove(connection);
LOG_WARN(TAG,"initialising UDP server failed, err not ok");
}
}
else{
LOG_WARN(TAG,"initialising UDP server failed, connection is null");
}
return err;
}

View File

@@ -25,6 +25,7 @@
#define LOGGER_LEVEL_ALL
#include "log.h"
#include "UDP_broadcast.h"
#include "udp.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -110,40 +111,13 @@ int main(void)
MX_LWIP_Init();
MX_QUADSPI_Init();
/* USER CODE BEGIN 2 */
init_UDP_server();
owner_details_t owner;
owner_details_t* owner_error = NULL;
char* test_error = NULL;
LOG_DEBUG(TAG,"\nhelloworld");
LOG_DEBUG(TAG,"owner1:%s",get_owner_details_reply(&owner));
if(!set_owner_details(&owner, "joran", "vn")){
LOG_DEBUG(TAG,"error");;
}
LOG_DEBUG(TAG,"owner2:%s",get_owner_details_reply(&owner));
if(!set_owner_details(&owner, "joran", "Van Nieuwenhoven")){
LOG_DEBUG(TAG,"error");
}
LOG_DEBUG(TAG,"owner3:%s",get_owner_details_reply(&owner));
if(!set_owner_details(&owner, "joran", "")){
LOG_DEBUG(TAG,"error");
}
LOG_DEBUG(TAG,"owner4:%s",get_owner_details_reply(&owner));
if(!set_owner_details(&owner, "joran", test_error)){
LOG_DEBUG(TAG,"error setting owner details");
}
LOG_DEBUG(TAG,"owner5:%s",get_owner_details_reply(&owner));
if(!set_owner_details(&owner, test_error, "Van Nieuwenhoven")){
LOG_DEBUG(TAG,"error setting owner details");
}
LOG_DEBUG(TAG,"owner6:%s",get_owner_details_reply(&owner));
if(!set_owner_details(owner_error, test_error, "Van Nieuwenhoven")){
LOG_DEBUG(TAG,"error setting owner details");
}
LOG_DEBUG(TAG,"owner7:%s",get_owner_details_reply(owner_error));
LOG_DEBUG(TAG,"%s",get_owner_details_reply(&owner));
/* USER CODE END 2 */
/* Infinite loop */