Files
2023-Webservices_And_Applic…/docs/udp_broadcast.md
2023-11-25 15:16:09 +01:00

116 lines
4.2 KiB
Markdown

# UDP broadcast
## Introduction
The UDP broadcast code is used to handle incoming UDP datagrams.
there are currently 2 types of datagrams it processes:
- broadcasts: "Where are you?v1.0", replies with current owner details.
- change of details: "func1:name: ..., surname: ...", replies with changed owner details.
It also writes the current owner's name on the screen and updates it everytime it's changed.
## Table of contents
- [Introduction](#introduction)
- [Table of contents](#table-of-contents)
- [Usage of UDP broadcast](#usage-of-udp-broadcast)
- [Initialization of UDP broadcast](#initialization-of-udp-broadcast)
- [Initialization of UDP connection](#initialization-of-udp-connection)
- [Owner details interface](#owner-details-interface)
- [Setting owner details](#setting-owner-details)
- [Getting owner details](#getting-owner-details)
## Usage of UDP broadcast
### Initialization of UDP broadcast
The 'udp_broadcast_init(uint16_t x_pos, uint16_t y_pos)' function does 4 things:
1. It initializes the coördinates of where the owner's name has to be written on the LCD.
2. [It initializes the UDP connection](#initialization-of-udp-connection)
3. It initializes the owner's details with a default name.
4. Returns the error value of [udp_broadcast_connection_init()](#initialization-of-udp-connection). This way the user can use some code to check whether the "connection" was initialized correctly.
```c
#include "UDP_broadcast.h'
...
void main(void){
...
if (udp_broadcast_init(270,255) != ERR_OK){
...
}
...
}
```
### Initialization of UDP connection
The 'udp_broadcast_connection_init()' funciton does 2 things:
1. Initializes the UDP "connection" so that incoming datagrams can be processed and replied to. It binds to port 64000 and listens to every IP-address in the local network.
2. returns the LWIP error code so that [err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos)](#initialization-of-udp-broadcast) knows the "connection" is initializes correctly
This function can be used seperately from [err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos)](#initialization-of-udp-broadcast), this gives the possibility to try "connecting" again after the first time failed.
```c
#include "UDP_broadcast.h'
...
void main(void){
...
if (udp_broadcast_init(10,255) == ERR_OK){
goto connected;
}
LOG_WARN(TAG,"error initializing udp connection, trying again in 500ms");
HAL_Delay(500);
if(udp_broadcast_connection_init() != ERR_OK){
LOG_WARN(TAG,"error initializing udp connection, check warnings from udp_broadcast_init() or udp_broadcast_connection_init()");
}
connected:
...
}
```
### Owner details interface
The interface to ask for the owner's details and change them is a modified version of the [Qt application](https://github.com/wimdams/Device_finder) Wim Dams build. His only has the functionality to ask for the owner's details.
Just because the owner's details might want to be used in other code, some functions have been written for obtaining these in the STM32 code aswell.
#### Setting owner details
THe 'udp_broadcast_set_owner_details(const char* , const char*)' function does 2 things:
1. Set the owner details, the order of the parameters is: name, surname
2. Return 1 if the owner's details have been set correctly and 0 if not.
```c
#include "UDP_broadcast.h'
...
void main(void){
...
if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven") != ERR_OK){
...
}
...
}
```
#### Getting owner details
There are 3 functions:
- udp_broadcast_get_owner_details_name(): returns the owner's name.
- udp_broadcast_get_owner_details_surname(): returns the owner's surname.
- udp_broadcast_get_owner_details_reply(): returns what would be replied to a UDP broadcast with datagram "Where are you?v1.0".
```c
#include <string.h>
#include "UDP_broadcast.h'
...
void main(void){
...
char name[20];
char surname[20];
char reply[120];
strncp(name, udp_broadcast_get_owner_details_name(), sizeof(name) - 1);
strncp(surname, udp_broadcast_get_owner_details_surname(), sizeof(surname) - 1);
strncp(reply, udp_broadcast_get_owner_details_reply(), sizeof(reply) - 1);
...
}
```