Fix style guide issues by auto formatting

This commit is contained in:
2023-11-20 00:46:32 +01:00
parent 48acc38493
commit c2dc36c765
2 changed files with 161 additions and 153 deletions

View File

@@ -10,11 +10,11 @@
#define INC_UDP_BROADCAST_H_ #define INC_UDP_BROADCAST_H_
// includes // includes
#include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include "lwip/netif.h" #include <string.h>
#include "lwip.h" #include "lwip.h"
#include "lwip/netif.h"
#define LOGGER_LEVEL_INFO #define LOGGER_LEVEL_INFO
#include "log.h" #include "log.h"
#include "udp.h" #include "udp.h"
@@ -47,7 +47,7 @@ typedef struct {
char surname[20]; char surname[20];
uint8_t mac_address[6]; uint8_t mac_address[6];
char reply[120]; char reply[120];
}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)
@@ -62,7 +62,7 @@ typedef struct {
* - 1: no error occured, details were set * - 1: no error occured, details were set
* - 0: an error occured, all or some details weren't set * - 0: an error occured, all or some details weren't set
*/ */
uint8_t udp_broadcast_set_owner_details(const char* , const char*); uint8_t udp_broadcast_set_owner_details(const char*, const char*);
/** /**
* @fn char udp_broadcast_get_owner_details_name*(owner_details_t) * @fn char udp_broadcast_get_owner_details_name*(owner_details_t)

View File

@@ -5,14 +5,14 @@
* Created on: Nov 6, 2023 * Created on: Nov 6, 2023
* Author: joran * Author: joran
*/ */
//| //|
// Includes // Includes
#include "UDP_broadcast.h" #include "UDP_broadcast.h"
// Global variables // Global variables
static const char *TAG = "UDP_broadcast"; // Tag used in logs static const char* TAG = "UDP_broadcast"; // Tag used in logs
static owner_details_t udp_owner; static owner_details_t udp_owner;
static uint16_t owner_name_x_pos; static uint16_t owner_name_x_pos;
static uint16_t owner_name_y_pos; static uint16_t owner_name_y_pos;
@@ -23,7 +23,11 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* name);
static uint8_t udp_broadcast_set_owner_details_surname(const char* surname); static uint8_t udp_broadcast_set_owner_details_surname(const char* surname);
static uint8_t udp_broadcast_set_owner_details_reply(const char* reply); static uint8_t udp_broadcast_set_owner_details_reply(const char* reply);
static void udp_broadcast_format_reply(); static void udp_broadcast_format_reply();
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);
/** /**
* @fn uint8_t udp_broadcast_set_owner_details_mac(owner_details_t*) * @fn uint8_t udp_broadcast_set_owner_details_mac(owner_details_t*)
@@ -31,8 +35,8 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
* and sets it in the owner_details_t struct * and sets it in the owner_details_t struct
*/ */
static void udp_broadcast_set_owner_details_mac(){ static void udp_broadcast_set_owner_details_mac() {
for (uint8_t i = 0; i < 6; i++){ for (uint8_t i = 0; i < 6; i++) {
udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address
} }
} }
@@ -49,14 +53,14 @@ static void udp_broadcast_set_owner_details_mac(){
* - 0: an error occured, name pointer is NULL * - 0: an error occured, name pointer is NULL
*/ */
static uint8_t udp_broadcast_set_owner_details_name(const char* name){ static uint8_t udp_broadcast_set_owner_details_name(const char* name) {
if (name == NULL){ if (name == NULL) {
LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_NAME); LOG_WARN(TAG, "%s: string given is a NULL pointer", SOD_NAME);
return 0; return 0;
} }
LOG_DEBUG(TAG,"set: %s",name); LOG_DEBUG(TAG, "set: %s", name);
lcd_display_text(" ", owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16); lcd_display_text(" ", owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16);
strncpy(udp_owner.name,name,sizeof(udp_owner.name)); strncpy(udp_owner.name, name, sizeof(udp_owner.name));
lcd_display_text(name, owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16); lcd_display_text(name, owner_name_x_pos, owner_name_y_pos, LCD_GREEN, LCD_BLACK, LCD_FONT16);
return 1; return 1;
} }
@@ -71,13 +75,13 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* name){
* - 1: no error occured, surname was set * - 1: no error occured, surname was set
* - 0: an error occured, surname pointer is NULL * - 0: an error occured, surname pointer is NULL
*/ */
static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname) {
if (surname == NULL){ if (surname == NULL) {
LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_SURNAME); LOG_WARN(TAG, "%s: string given is a NULL pointer", SOD_SURNAME);
return 0; return 0;
} }
LOG_DEBUG(TAG,"set: %s",surname); LOG_DEBUG(TAG, "set: %s", surname);
strncpy(udp_owner.surname,surname,sizeof(udp_owner.surname)); strncpy(udp_owner.surname, surname, sizeof(udp_owner.surname));
return 1; return 1;
} }
@@ -92,13 +96,13 @@ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){
* - 0: an error occured, reply pointer is null * - 0: an error occured, reply pointer is null
*/ */
static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply) {
if (reply == NULL){ if (reply == NULL) {
LOG_WARN(TAG,"%s: string given is a NULL pointer",SOD_REPLY); LOG_WARN(TAG, "%s: string given is a NULL pointer", SOD_REPLY);
return 0; return 0;
} }
LOG_DEBUG(TAG,"set: %s",reply); LOG_DEBUG(TAG, "set: %s", reply);
strncpy(udp_owner.reply,reply,sizeof(udp_owner.reply)); strncpy(udp_owner.reply, reply, sizeof(udp_owner.reply));
return 1; return 1;
} }
@@ -109,23 +113,22 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){
* it sets this reply with @see udp_broadcast_set_owner_details_reply() * it sets this reply with @see udp_broadcast_set_owner_details_reply()
*/ */
static void udp_broadcast_format_reply(){ static void udp_broadcast_format_reply() {
size_t reply_len = 0; size_t reply_len = 0;
char mac_addr_str[18]; char mac_addr_str[18];
char reply_buf[120]; char reply_buf[120];
reply_len = 27 + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_owner.name); reply_len = 27 + sizeof(mac_addr_str) + sizeof(udp_owner.surname) + sizeof(udp_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", udp_owner.mac_address[0],
udp_owner.mac_address[0], udp_owner.mac_address[1], udp_owner.mac_address[2], udp_owner.mac_address[1], udp_owner.mac_address[2], udp_owner.mac_address[3], udp_owner.mac_address[4],
udp_owner.mac_address[3], udp_owner.mac_address[4], udp_owner.mac_address[5]); udp_owner.mac_address[5]);
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, udp_owner.surname,
mac_addr_str, udp_owner.surname, udp_owner.name); udp_owner.name);
LOG_DEBUG(TAG,"reply_buf: %s",reply_buf); LOG_DEBUG(TAG, "reply_buf: %s", reply_buf);
udp_broadcast_set_owner_details_reply(reply_buf); udp_broadcast_set_owner_details_reply(reply_buf);
} }
/** /**
@@ -140,14 +143,13 @@ static void udp_broadcast_format_reply(){
* - 1: no error occured, details were set * - 1: no error occured, details were set
* - 0: an error occured, all or some details weren't set * - 0: an error occured, all or some details weren't set
*/ */
uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname){ uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname) {
if (udp_broadcast_set_owner_details_name(name) && udp_broadcast_set_owner_details_surname(surname)){ if (udp_broadcast_set_owner_details_name(name) && udp_broadcast_set_owner_details_surname(surname)) {
udp_broadcast_set_owner_details_mac(); udp_broadcast_set_owner_details_mac();
udp_broadcast_format_reply(); udp_broadcast_format_reply();
return 1; return 1;
} }
return 0; return 0;
} }
/** /**
@@ -158,7 +160,7 @@ uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname){
* this name is set by @see udp_broadcast_set_owner_details_name() * this name is set by @see udp_broadcast_set_owner_details_name()
*/ */
char* udp_broadcast_get_owner_details_name(){ char* udp_broadcast_get_owner_details_name() {
return udp_owner.name; return udp_owner.name;
} }
@@ -170,7 +172,7 @@ char* udp_broadcast_get_owner_details_name(){
* this name is set by @see udp_broadcast_set_owner_details_surname() * this name is set by @see udp_broadcast_set_owner_details_surname()
*/ */
char* udp_broadcast_get_owner_details_surname(){ char* udp_broadcast_get_owner_details_surname() {
return udp_owner.surname; return udp_owner.surname;
} }
@@ -182,7 +184,7 @@ char* udp_broadcast_get_owner_details_surname(){
* this reply is formatted by @see format_reply() * this reply is formatted by @see format_reply()
*/ */
char* udp_broadcast_get_owner_details_reply(){ char* udp_broadcast_get_owner_details_reply() {
return udp_owner.reply; return udp_owner.reply;
} }
@@ -194,7 +196,7 @@ char* udp_broadcast_get_owner_details_reply(){
* @param[in] data the datagram received on port 64000 * @param[in] data the datagram received on port 64000
*/ */
static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]) {
char func[7]; char func[7];
char buffer[20]; char buffer[20];
uint8_t enders[4]; uint8_t enders[4];
@@ -202,39 +204,41 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){
memset(func, 0, sizeof(func)); memset(func, 0, sizeof(func));
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
for (uint8_t i = 0; i<6;i++){ for (uint8_t i = 0; i < 6; i++) {
func[i] = data[i]; func[i] = data[i];
} }
if (strcmp(func,"func1:")==0){ if (strcmp(func, "func1:") == 0) {
for (uint8_t i = 0; i < strlen(data); i++){ for (uint8_t i = 0; i < strlen(data); i++) {
if (data[i] == ',' || data[i] == ':'){ if (data[i] == ',' || data[i] == ':') {
enders[counter] = i; enders[counter] = i;
counter++; counter++;
} }
} }
LOG_DEBUG(TAG,"%d-%d=%d, %d-%d=%d",enders[2],enders[1],enders[2] - enders[1],strlen(data),enders[3],strlen(data) - enders[3]); LOG_DEBUG(TAG, "%d-%d=%d, %d-%d=%d", enders[2], enders[1], enders[2] - enders[1], strlen(data), enders[3],
if(enders[2] - enders[1] < 22 && strlen(data) - enders[3] < 22 && strncmp(data+enders[0],":name",5) == 0 && strncmp(data+enders[2],", surname",9) == 0 ){ strlen(data) - enders[3]);
if (enders[2] - enders[1] < 22 && strlen(data) - enders[3] < 22 && strncmp(data + enders[0], ":name", 5) == 0
&& strncmp(data + enders[2], ", surname", 9) == 0) {
counter = 0; counter = 0;
for (uint8_t i = enders[1]+2; i< enders[2] && data[i] != '\0';i++){ for (uint8_t i = enders[1] + 2; i < enders[2] && data[i] != '\0'; i++) {
buffer[counter] = data[i]; buffer[counter] = data[i];
LOG_DEBUG(TAG,"%d",counter); LOG_DEBUG(TAG, "%d", counter);
counter++; counter++;
} }
if (strcmp(buffer,"") == 0){ if (strcmp(buffer, "") == 0) {
strncpy(buffer,"name",sizeof(buffer)); strncpy(buffer, "name", sizeof(buffer));
} }
LOG_INFO(TAG,"new owner name:%s",buffer); LOG_INFO(TAG, "new owner name:%s", buffer);
udp_broadcast_set_owner_details_name(buffer); udp_broadcast_set_owner_details_name(buffer);
memset(buffer, 0, sizeof(buffer)); memset(buffer, 0, sizeof(buffer));
counter = 0; counter = 0;
for (uint8_t i = enders[3]+2; i< strlen(data) && data[i] != '\0';i++){ for (uint8_t i = enders[3] + 2; i < strlen(data) && data[i] != '\0'; i++) {
buffer[counter] = data[i]; buffer[counter] = data[i];
counter++; counter++;
} }
if (strcmp(buffer,"") == 0){ if (strcmp(buffer, "") == 0) {
strncpy(buffer,"default",sizeof(buffer)); strncpy(buffer, "default", sizeof(buffer));
} }
LOG_INFO(TAG,"new owner surname:%s",buffer); LOG_INFO(TAG, "new owner surname:%s", buffer);
udp_broadcast_set_owner_details_surname(buffer); udp_broadcast_set_owner_details_surname(buffer);
udp_broadcast_format_reply(); udp_broadcast_format_reply();
} }
@@ -255,10 +259,14 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){
* @param[in] port the source port number of the sender's UDP packet * @param[in] port the source port number of the sender's UDP packet
*/ */
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 pbuf *p_data; struct udp_pcb* connection,
struct pbuf* p,
const ip_addr_t* addr,
u16_t port) {
struct pbuf* p_data;
size_t len; size_t len;
char *pc; char* pc;
char data[MAX_DATA_SIZE]; char data[MAX_DATA_SIZE];
char source_ip_str[16]; char source_ip_str[16];
@@ -270,24 +278,24 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
pc = (char*)p->payload; pc = (char*)p->payload;
len = p->tot_len; len = p->tot_len;
p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(udp_owner.reply), PBUF_RAM); p_data = pbuf_alloc(PBUF_TRANSPORT, sizeof(udp_owner.reply), PBUF_RAM);
if (p_data == NULL){ if (p_data == NULL) {
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 (size_t i = 0; i < len; i++) { for (size_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 = udp_owner.reply; p_data->payload = udp_owner.reply;
p_data->len = strlen(udp_owner.reply); p_data->len = strlen(udp_owner.reply);
p_data->tot_len = strlen(udp_owner.reply); p_data->tot_len = strlen(udp_owner.reply);
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,64000,udp_owner.reply); LOG_INFO(TAG, "tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply);
}else{ } else {
LOG_INFO(TAG,"other function called"); LOG_INFO(TAG, "other function called");
udp_broadcast_check_function(data); udp_broadcast_check_function(data);
p_data->payload = udp_owner.reply; p_data->payload = udp_owner.reply;
p_data->len = strlen(udp_owner.reply); p_data->len = strlen(udp_owner.reply);
@@ -295,15 +303,15 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
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,64000,udp_owner.reply); LOG_INFO(TAG, "tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply);
} }
}else{ } else {
LOG_WARN(TAG,"udp_receive_callback: input buffer was bigger than max size %d",MAX_DATA_SIZE); LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than max size %d", MAX_DATA_SIZE);
} }
}else{ } else {
LOG_WARN(TAG,"udp_receive_callback: input buffer was a NULL pointer"); LOG_WARN(TAG, "udp_receive_callback: input buffer was a NULL pointer");
} }
pbuf_free(p); pbuf_free(p);
pbuf_free(p_data); pbuf_free(p_data);
@@ -320,23 +328,23 @@ static void udp_receive_callback(void* arg, struct udp_pcb* connection, struct p
* - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB. * - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB.
*/ */
err_t udp_broadcast_connection_init(){ err_t udp_broadcast_connection_init() {
struct udp_pcb* connection; struct udp_pcb* connection;
err_t err; err_t err;
LOG_INFO(TAG,"initialising UDP server"); LOG_INFO(TAG, "initialising UDP server");
connection = udp_new(); connection = udp_new();
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 {
udp_remove(connection); udp_remove(connection);
LOG_WARN(TAG,"initialising UDP server failed, err not ok"); LOG_WARN(TAG, "initialising UDP server failed, err not ok");
} }
}else{ } else {
LOG_WARN(TAG,"initialising UDP server failed, connection is null"); LOG_WARN(TAG, "initialising UDP server failed, connection is null");
} }
return err; return err;
} }
@@ -349,7 +357,7 @@ err_t udp_broadcast_connection_init(){
* - ERR_OK. Successful. No error occurred. * - ERR_OK. Successful. No error occurred.
* - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB. * - ERR_USE. The specified ipaddr and port are already bound to by another UDP PCB.
*/ */
err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos){ err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos) {
err_t err; err_t err;
owner_name_x_pos = x_pos; owner_name_x_pos = x_pos;
owner_name_y_pos = y_pos; owner_name_y_pos = y_pos;