cleaning
This commit is contained in:
@@ -30,7 +30,7 @@
|
|||||||
#define F_REPLY "format_reply"
|
#define F_REPLY "format_reply"
|
||||||
|
|
||||||
// Defines used by UDP callback
|
// Defines used by UDP callback
|
||||||
#define MAX_DATA_SIZE 50 // Define the maximum expected data size
|
#define MAX_DATA_SIZE 30 // Define the maximum expected data size
|
||||||
#define UDP_QUESTION1 "Where are you?v1.0"
|
#define UDP_QUESTION1 "Where are you?v1.0"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -43,7 +43,6 @@ typedef struct {
|
|||||||
char name[20];
|
char name[20];
|
||||||
char surname[20];
|
char surname[20];
|
||||||
uint8_t mac_address[6];
|
uint8_t mac_address[6];
|
||||||
char reply[100];
|
|
||||||
}owner_details_t;
|
}owner_details_t;
|
||||||
|
|
||||||
// The following functions are used for owner details (those that must be available in main)
|
// The following functions are used for owner details (those that must be available in main)
|
||||||
@@ -51,7 +50,7 @@ uint8_t set_owner_details(owner_details_t*, char* , char*);
|
|||||||
|
|
||||||
char* get_owner_details_name(owner_details_t*);
|
char* get_owner_details_name(owner_details_t*);
|
||||||
char* get_owner_details_surname(owner_details_t*);
|
char* get_owner_details_surname(owner_details_t*);
|
||||||
char* get_owner_details_reply(owner_details_t*);
|
char* get_owner_details_reply();
|
||||||
|
|
||||||
// The following functions are used for UDP (those that must be available in main)
|
// The following functions are used for UDP (those that must be available in main)
|
||||||
err_t init_UDP_server();
|
err_t init_UDP_server();
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
// Includes
|
// Includes
|
||||||
#include "UDP_broadcast.h"
|
#include "UDP_broadcast.h"
|
||||||
|
|
||||||
|
// Global variables
|
||||||
|
|
||||||
static const char *TAG = "UDP_broadcast"; // Tag used in logs
|
static const char *TAG = "UDP_broadcast"; // Tag used in logs
|
||||||
static char reply_str[100] = "|no reply formatted yet|"; // Global reply string for UDP broadcast
|
static char reply_str[100] = "|no reply formatted yet|"; // Global reply string for UDP broadcast
|
||||||
|
|
||||||
@@ -48,19 +50,14 @@ static void owner_details_error_handler(owner_details_t* owner, char* funct,char
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static uint8_t set_owner_details_mac(owner_details_t* owner){
|
static uint8_t set_owner_details_mac(owner_details_t* owner){
|
||||||
|
if(owner == NULL){
|
||||||
if(owner != NULL){
|
|
||||||
for(int i = 0; i < 6; i++){
|
|
||||||
owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
owner_details_error_handler(owner, SOD_MAC,"");
|
owner_details_error_handler(owner, SOD_MAC,"");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
for(uint8_t i = 0; i < 6; i++){
|
||||||
|
owner->mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -75,16 +72,13 @@ static uint8_t set_owner_details_mac(owner_details_t* owner){
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static uint8_t set_owner_details_name(owner_details_t *owner, char *name){
|
static uint8_t set_owner_details_name(owner_details_t *owner, char *name){
|
||||||
if(name != NULL && owner != NULL){
|
if(name == NULL || owner == NULL){
|
||||||
LOG_DEBUG(TAG,"set: %s",name);
|
|
||||||
strncpy(owner->name,name,sizeof(owner->name));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
owner_details_error_handler(owner, SOD_NAME, name);
|
owner_details_error_handler(owner, SOD_NAME, name);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
LOG_DEBUG(TAG,"set: %s",name);
|
||||||
|
strncpy(owner->name,name,sizeof(owner->name));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -98,39 +92,33 @@ static uint8_t set_owner_details_name(owner_details_t *owner, char *name){
|
|||||||
* - 0: an error occured, surname pointer is NULL or owner pointer is NULL
|
* - 0: an error occured, surname pointer is NULL or owner pointer is NULL
|
||||||
*/
|
*/
|
||||||
static uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){
|
static uint8_t set_owner_details_surname(owner_details_t* owner, char* surname){
|
||||||
if(surname != NULL && owner != NULL){
|
if(surname == NULL || owner == NULL){
|
||||||
|
owner_details_error_handler(owner, SOD_SURNAME, surname);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
LOG_DEBUG(TAG,"set: %s",surname);
|
LOG_DEBUG(TAG,"set: %s",surname);
|
||||||
strncpy(owner->surname,surname,sizeof(owner->surname));
|
strncpy(owner->surname,surname,sizeof(owner->surname));
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
else{
|
|
||||||
owner_details_error_handler(owner, SOD_SURNAME, surname);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn uint8_t set_owner_details_reply(owner_details_t, char*)
|
* @fn uint8_t set_owner_details_reply(char*)
|
||||||
* @brief set_owner_details_reply() sets the UDP reply in the owner_details_t struct
|
* @brief set_owner_details_reply() sets the UDP reply in the owner_details_t struct
|
||||||
*
|
*
|
||||||
* @param owner owner_details_t structure, it contains information about the owner
|
|
||||||
* @param reply string used to reply to the UDP broadcast
|
* @param reply string used to reply to the UDP broadcast
|
||||||
* @return setting owner reply error
|
* @return setting owner reply error
|
||||||
* - 1: no error occured, reply was set
|
* - 1: no error occured, reply was set
|
||||||
* - 0: an error occured, reply pointer is null or owner pointer is NULL
|
* - 0: an error occured, reply pointer is null or owner pointer is NULL
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static uint8_t set_owner_details_reply(owner_details_t *owner, char *reply){
|
static uint8_t set_owner_details_reply(char *reply){
|
||||||
if(reply != NULL && owner != NULL){
|
if(reply == NULL){
|
||||||
LOG_DEBUG(TAG,"set: %s",reply);
|
LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_REPLY);
|
||||||
strncpy(owner->reply,reply,sizeof(owner->reply));
|
|
||||||
strncpy(reply_str,reply,sizeof(reply_str));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
owner_details_error_handler(owner, SOD_REPLY, reply);
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
LOG_DEBUG(TAG,"set: %s",reply);
|
||||||
|
strncpy(reply_str,reply,sizeof(reply_str));
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -147,10 +135,12 @@ static uint8_t format_reply(owner_details_t *owner){
|
|||||||
size_t reply_len = 0;
|
size_t reply_len = 0;
|
||||||
char mac_addr_str[18];
|
char mac_addr_str[18];
|
||||||
char reply_buf[100];
|
char reply_buf[100];
|
||||||
if (owner != NULL) {
|
if (owner == NULL) {
|
||||||
|
owner_details_error_handler(owner, F_REPLY,"");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name);
|
reply_len = 20 + sizeof(mac_addr_str) + sizeof(owner->surname) + sizeof(owner->name);
|
||||||
|
|
||||||
|
|
||||||
snprintf(mac_addr_str, sizeof(mac_addr_str), "%02X:%02X:%02X:%02X:%02X:%02X",
|
snprintf(mac_addr_str, sizeof(mac_addr_str), "%02X:%02X:%02X:%02X:%02X:%02X",
|
||||||
owner->mac_address[0], owner->mac_address[1], owner->mac_address[2],
|
owner->mac_address[0], owner->mac_address[1], owner->mac_address[2],
|
||||||
owner->mac_address[3], owner->mac_address[4], owner->mac_address[5]);
|
owner->mac_address[3], owner->mac_address[4], owner->mac_address[5]);
|
||||||
@@ -158,13 +148,9 @@ static uint8_t format_reply(owner_details_t *owner){
|
|||||||
snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s",
|
snprintf(reply_buf, reply_len, "%s is present and my owner is %s %s",
|
||||||
mac_addr_str, owner->surname, owner->name);
|
mac_addr_str, owner->surname, owner->name);
|
||||||
|
|
||||||
set_owner_details_reply(owner, reply_buf);
|
set_owner_details_reply(reply_buf);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
|
||||||
else{
|
|
||||||
owner_details_error_handler(owner, F_REPLY,"");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -180,23 +166,14 @@ static uint8_t format_reply(owner_details_t *owner){
|
|||||||
* - 0: an error occured, all or some details weren't set or owner pointer is NULL
|
* - 0: an error occured, all or some details weren't set or owner pointer is NULL
|
||||||
*/
|
*/
|
||||||
uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){
|
uint8_t set_owner_details(owner_details_t* owner, char* name, char* surname){
|
||||||
if(owner != NULL){
|
if(owner == NULL){
|
||||||
if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname) && set_owner_details_mac(owner)){
|
|
||||||
if(format_reply(owner)){
|
|
||||||
return 1;;
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
owner_details_error_handler(owner, SOD,"");
|
owner_details_error_handler(owner, SOD,"");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
else if(set_owner_details_name(owner, name) && set_owner_details_surname(owner, surname) && set_owner_details_mac(owner) && format_reply(owner)){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -235,20 +212,15 @@ char* get_owner_details_surname(owner_details_t* owner){
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @fn char get_owner_details_reply*(owner_details_t)
|
* @fn char get_owner_details_reply*()
|
||||||
* @brief get_owner_details_reply() can be used to get the current UDP reply
|
* @brief get_owner_details_reply() can be used to get the current UDP reply
|
||||||
*
|
*
|
||||||
* @param owner owner_details_t structure, it contains information about the owner
|
|
||||||
* @return reply for UDP broadcast
|
* @return reply for UDP broadcast
|
||||||
* this reply is formatted by @see format_reply()
|
* this reply is formatted by @see format_reply()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char* get_owner_details_reply(owner_details_t *owner){
|
char* get_owner_details_reply(){
|
||||||
if(owner == NULL || owner->reply == NULL){
|
return reply_str;
|
||||||
owner_details_error_handler(owner, GOD_REPLY,"");
|
|
||||||
return "|no reply formatted yet|";
|
|
||||||
}
|
|
||||||
return owner->reply;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -266,7 +238,6 @@ char* get_owner_details_reply(owner_details_t *owner){
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct pbuf *p, const ip_addr_t *addr, u16_t port){
|
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;
|
int len;
|
||||||
char *pc;
|
char *pc;
|
||||||
char data[MAX_DATA_SIZE];
|
char data[MAX_DATA_SIZE];
|
||||||
@@ -285,15 +256,15 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p
|
|||||||
LOG_WARN(TAG,"udp_receive_callback: unable to allocate data buffer for reply");
|
LOG_WARN(TAG,"udp_receive_callback: unable to allocate data buffer for reply");
|
||||||
}
|
}
|
||||||
else if(len <= MAX_DATA_SIZE){
|
else if(len <= MAX_DATA_SIZE){
|
||||||
for(i = 0; i < len; i++) {
|
for(uint8_t i = 0; i < len; i++) {
|
||||||
data[i] = pc[i];
|
data[i] = pc[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
LOG_INFO(TAG,"udp_receive_callback: received data from %s at port: %d: %s",source_ip_str,port,data);
|
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){
|
if(strcmp(data,UDP_QUESTION1) == 0){
|
||||||
p_data->payload = reply_str;
|
p_data->payload = reply_str;
|
||||||
p_data->len = sizeof(reply_str);
|
p_data->len = strlen(reply_str);
|
||||||
p_data->tot_len = sizeof(reply_str);
|
p_data->tot_len = strlen(reply_str);
|
||||||
udp_sendto(connection, p_data, addr, 64000); /*was using the sending port of the pc,
|
udp_sendto(connection, p_data, addr, 64000); /*was using the sending port of the pc,
|
||||||
this is not the port that Qt is listening to*/
|
this is not the port that Qt is listening to*/
|
||||||
LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str);
|
LOG_INFO(TAG,"tried to reply to %s at port: %d: %s",source_ip_str,port,reply_str);
|
||||||
@@ -319,8 +290,7 @@ static void udp_receive_callback(void *arg, struct udp_pcb *connection, struct p
|
|||||||
*
|
*
|
||||||
* @return lwIP error code.
|
* @return lwIP error code.
|
||||||
* - ERR_OK. Successful. No error occurred.
|
* - ERR_OK. Successful. No error occurred.
|
||||||
* - ERR_USE. The specified ipaddr and port are already bound to by
|
* - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB.
|
||||||
* another UDP PCB.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
err_t init_UDP_server(){
|
err_t init_UDP_server(){
|
||||||
|
|||||||
Reference in New Issue
Block a user