diff --git a/docs/udp_broadcast.md b/docs/udp_broadcast.md new file mode 100644 index 0000000..d8aa96d --- /dev/null +++ b/docs/udp_broadcast.md @@ -0,0 +1,109 @@ +# 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(270,255) != ERR_OK || udp_broadcast_connection_init() != ERR_OK){ + ... + } + ... +} + +``` +### 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")){ + ... + } + ... +} +``` +#### 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 +#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)); + + strncp(surname,udp_broadcast_get_owner_details_surname(),sizeof(surname)); + + strncp(reply,udp_broadcast_get_owner_details_reply(),sizeof(reply)); + ... +} +``` \ No newline at end of file diff --git a/project/Core/Src/main.c b/project/Core/Src/main.c index 465131d..a5fcd16 100644 --- a/project/Core/Src/main.c +++ b/project/Core/Src/main.c @@ -127,7 +127,7 @@ int main(void) llfs_init(); // Initialize the UDP broadcast service - if (udp_broadcast_init(270,255) != ERR_OK && udp_broadcast_connection_init() != ERR_OK){ + if (udp_broadcast_init(270,255) != ERR_OK || udp_broadcast_connection_init() != ERR_OK){ LOG_WARN(TAG,"error initializing udp connection"); } if (!udp_broadcast_set_owner_details("0123456879012345678", "default")){