minor changes

This commit is contained in:
joran2738
2023-11-19 23:29:54 +01:00
parent 47b1cd167b
commit 48acc38493
2 changed files with 23 additions and 40 deletions

View File

@@ -14,47 +14,39 @@
static const char *TAG = "UDP_broadcast"; // Tag used in logs
static owner_details_t udp_owner;
static uint16_t owner_name_x_pos = 10;
static uint16_t owner_name_y_pos = 10;
static uint16_t owner_name_x_pos;
static uint16_t owner_name_y_pos;
// Functions
static uint8_t udp_broadcast_set_owner_details_mac();
static void udp_broadcast_set_owner_details_mac();
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_reply(const char* reply);
static uint8_t 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);
/**
* @fn uint8_t udp_broadcast_set_owner_details_mac(owner_details_t*)
* @brief set_owner_details_mac() gets the MAC address from the default netif
* and sets it in the owner_details_t struct
*
* @param[out] owner owner_details_t structure, it contains information about the owner
* @return setting owner mac address error
* - 1: no error occured, mac address was set
* - 0: an error occured, owner pointer is NULL
*/
static uint8_t udp_broadcast_set_owner_details_mac(){
static void udp_broadcast_set_owner_details_mac(){
for (uint8_t i = 0; i < 6; i++){
udp_owner.mac_address[i] = netif_default->hwaddr[i]; // Access the MAC address
}
return 1;
}
/**
* @fn uint8_t udp_broadcast_set_owner_details_name(owner_details_t*, const char*)
* @brief set_owner_details_name() sets the owner's name in the owner_details_t struct
* if one of the pointers given is NULL it calls the error handler
* strncpy is used to copy the function paremeter safely to the owner_details_t's name
* it also uses the lcd api to display the latest owner's name
*
* @param[out] owner owner_details_t structure, it contains information about the owner
* @param[in] name string containing the owner's name
* @return setting owner name error
* - 1: no error occured, name was set
* - 0: an error occured, name pointer is NULL or owner pointer is NULL
* - 0: an error occured, name pointer is NULL
*/
static uint8_t udp_broadcast_set_owner_details_name(const char* name){
@@ -72,14 +64,12 @@ static uint8_t udp_broadcast_set_owner_details_name(const char* name){
/**
* @fn uint8_t udp_broadcast_set_owner_details_surname(owner_details_t*, const char*)
* @brief set_owner_details_surname() sets the owner's surname in the owner_details_t struct
* if one of the pointers given is NULL it calls the error handler
* strncpy is used to copy the function paremeter safely to the owner_details_t's surname
*
* @param[out] owner owner_details_t structure, it contains information about the owner
* @param[in] surname string containing the owner's surname
* @return setting owner surname error
* - 1: no error occured, surname was set
* - 0: an error occured, surname pointer is NULL or owner pointer is NULL
* - 0: an error occured, surname pointer is NULL
*/
static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){
if (surname == NULL){
@@ -94,13 +84,12 @@ static uint8_t udp_broadcast_set_owner_details_surname(const char* surname){
/**
* @fn uint8_t udp_broadcast_set_owner_details_reply(const char*)
* @brief set_owner_details_reply() sets the UDP reply in the owner_details_t struct
* if the pointer given is NULL it calls the error handler
* strncpy is used to copy the function paremeter safely to the owner_details_t's reply
*
* @param[in] reply string used to reply to the UDP broadcast
* @return setting owner reply error
* - 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
*/
static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){
@@ -116,15 +105,11 @@ static uint8_t udp_broadcast_set_owner_details_reply(const char* reply){
/**
* @fn uint8_t udp_broadcast_format_reply(const owner_details_t*)
* @brief format_reply() formats all the owner's details into a string
* if the pointer given is NULL it calls the error handler
* it formats a string using the owner's details using snprintf
* it sets this reply with @see udp_broadcast_set_owner_details_reply()
*
* @param[in] owner owner_details_t structure, it contains information about the owner
* @return 1 makes it easier to run @see udp_broadcast_set_owner_details()
*/
static uint8_t udp_broadcast_format_reply(){
static void udp_broadcast_format_reply(){
size_t reply_len = 0;
char mac_addr_str[18];
char reply_buf[120];
@@ -140,7 +125,6 @@ static uint8_t udp_broadcast_format_reply(){
LOG_DEBUG(TAG,"reply_buf: %s",reply_buf);
udp_broadcast_set_owner_details_reply(reply_buf);
return 1;
}
@@ -148,19 +132,18 @@ static uint8_t udp_broadcast_format_reply(){
* @fn uint8_t udp_broadcast_set_owner_details(owner_details_t*, const char*, const char*)
* @brief set_owner_details() is the interface that can be used in other files
* to set the owner's details
* if the pointer given is NULL it calls the error handler,
* the other pointers get checked by the functions that are called in this function
* the pointers get checked by the functions that are called in this function
*
* @param[out] owner owner_details_t structure, it contains information about the owner
* @param[in] name string containing the new owner's name
* @param[in] surname string containing the new owner's surname
* @return setting owner details error
* - 1: no error occured, details were set
* - 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
*/
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)
&& udp_broadcast_set_owner_details_mac() && udp_broadcast_format_reply()){
if (udp_broadcast_set_owner_details_name(name) && udp_broadcast_set_owner_details_surname(surname)){
udp_broadcast_set_owner_details_mac();
udp_broadcast_format_reply();
return 1;
}
return 0;
@@ -171,7 +154,6 @@ uint8_t udp_broadcast_set_owner_details(const char* name, const char* surname){
* @fn char udp_broadcast_get_owner_details_name*(owner_details_t)
* @brief get_owner_details_name() can be used to get the current owner's name
*
* @param[out] owner owner_details_t structure, it contains information about the owner
* @return name of owner
* this name is set by @see udp_broadcast_set_owner_details_name()
*/
@@ -184,7 +166,6 @@ char* udp_broadcast_get_owner_details_name(){
* @fn char udp_broadcast_get_owner_details_surname*(const owner_details_t)
* @brief get_owner_details_surname() can be used to get the current owner's surname
*
* @param[out] owner owner_details_t structure, it contains information about the owner
* @return surname of owner
* this name is set by @see udp_broadcast_set_owner_details_surname()
*/
@@ -207,9 +188,10 @@ char* udp_broadcast_get_owner_details_reply(){
/**
* @fn void udp_broadcast_check_function(const char[])
* @brief
* @brief checks what the UDP datagram asked to do if it was not @see UDP_QUESTION1
* and processes the datagram
*
* @param data
* @param[in] data the datagram received on port 64000
*/
static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){
@@ -264,6 +246,7 @@ static void udp_broadcast_check_function(const char data[MAX_DATA_SIZE]){
* @brief udp_receive_callback() callback function for when a UDP packet has been received.
* it compares the data to a set string @see UDP_QUESTION1, if it's the same it sends the reply string,
* @see reply_str, back to the client
* if it was not @see UDP_QUESTION1, it checks what function was called with @see udp_broadcast_check_function()
*
* @param[in] arg a pointer to some user-defined data or context
* @param[in] connection UDP PCB to be bound with a local address ipaddr and port.