solving reviews pt5
This commit is contained in:
@@ -78,7 +78,7 @@ static void udp_broadcast_name_to_lcd(void){
|
|||||||
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, "set_owner_details_name: string given is a NULL pointer");
|
LOG_WARN(TAG, "%s: string given is a NULL pointer", __func__);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
LOG_DEBUG(TAG, "set: %s", name);
|
LOG_DEBUG(TAG, "set: %s", name);
|
||||||
@@ -100,7 +100,7 @@ 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) {
|
||||||
if (surname == NULL) {
|
if (surname == NULL) {
|
||||||
LOG_WARN(TAG, "set_owner_details_surname: string given is a NULL pointer");
|
LOG_WARN(TAG, "%s: string given is a NULL pointer", __func__);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
LOG_DEBUG(TAG, "set: %s", surname);
|
LOG_DEBUG(TAG, "set: %s", surname);
|
||||||
@@ -121,7 +121,7 @@ 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) {
|
||||||
if (reply == NULL) {
|
if (reply == NULL) {
|
||||||
LOG_WARN(TAG, "set_owner_details_reply: string given is a NULL pointer");
|
LOG_WARN(TAG, "%s: string given is a NULL pointer", __func__);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
LOG_DEBUG(TAG, "set: %s", reply);
|
LOG_DEBUG(TAG, "set: %s", reply);
|
||||||
@@ -236,7 +236,7 @@ static uint8_t udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DA
|
|||||||
|
|
||||||
memcpy(func,data,UDP_BROADCAST_MAX_FUNC_LEN - 1);
|
memcpy(func,data,UDP_BROADCAST_MAX_FUNC_LEN - 1);
|
||||||
if (strcmp(func, "func1:") != 0) {
|
if (strcmp(func, "func1:") != 0) {
|
||||||
LOG_WARN(TAG, "udp_broadcast_check_function: datagram does not contain function that's currently available");
|
LOG_WARN(TAG, "%s: datagram does not contain function that's currently available", __func__);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
for (uint8_t i = 0; i < data_len && counter < UDP_BROADCAST_MAX_COLON_COMMA_COUNT; i++) {
|
for (uint8_t i = 0; i < data_len && counter < UDP_BROADCAST_MAX_COLON_COMMA_COUNT; i++) {
|
||||||
@@ -271,10 +271,9 @@ static uint8_t udp_broadcast_check_function(const char data[UDP_BROADCAST_MAX_DA
|
|||||||
udp_broadcast_set_owner_details_surname(buffer);
|
udp_broadcast_set_owner_details_surname(buffer);
|
||||||
udp_broadcast_format_reply();
|
udp_broadcast_format_reply();
|
||||||
return 0;
|
return 0;
|
||||||
}else{
|
|
||||||
LOG_WARN(TAG,"udp_broadcast_check_function: function didn't receive the right formatting");
|
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
LOG_WARN(TAG,"%s: function didn't receive the right formatting", __func__);
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -307,34 +306,34 @@ static void udp_receive_callback(void* arg,
|
|||||||
ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); // Convert the source IP address to a string
|
ipaddr_ntoa_r(addr, source_ip_str, sizeof(source_ip_str)); // Convert the source IP address to a string
|
||||||
|
|
||||||
if (p == NULL) {
|
if (p == NULL) {
|
||||||
LOG_WARN(TAG, "udp_receive_callback: input buffer was a NULL pointer");
|
LOG_WARN(TAG, "%s: input buffer was a NULL pointer", __func__);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pc = (char*)p->payload;
|
pc = (char*)p->payload;
|
||||||
len = p->tot_len;
|
len = p->tot_len;
|
||||||
if (len >= UDP_BROADCAST_MAX_DATA_SIZE) { // >= : only if it's smaller to compensate for '\0'
|
if (len >= UDP_BROADCAST_MAX_DATA_SIZE) { // >= : only if it's smaller to compensate for '\0'
|
||||||
LOG_WARN(TAG, "udp_receive_callback: input buffer was bigger than or was max size %d", UDP_BROADCAST_MAX_DATA_SIZE);
|
LOG_WARN(TAG, "%s: input buffer was bigger than or was max size %d", __func__, UDP_BROADCAST_MAX_DATA_SIZE);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
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, "%s: unable to allocate data buffer for reply", __func__);
|
||||||
goto defer;
|
goto defer;
|
||||||
}
|
}
|
||||||
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, "%s: received data from %s at port: %d: %s", __func__, source_ip_str, port, data);
|
||||||
LOG_INFO(TAG, "udp_receive_callback: checking which function was called");
|
LOG_INFO(TAG, "%s: checking which function was called", __func__);
|
||||||
|
|
||||||
if(!udp_broadcast_check_function(data)){ // Should return 0 to reply
|
if(!udp_broadcast_check_function(data)){ // Should return 0 to reply
|
||||||
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); // QT app listens on port 64000
|
udp_sendto(connection, p_data, addr, 64000); // QT app listens on port 64000
|
||||||
LOG_INFO(TAG, "udp_receive_callback: tried to reply to %s at port: %d: %s", source_ip_str, 64000, udp_owner.reply);
|
LOG_INFO(TAG, "%s: tried to reply to %s at port: %d: %s", __func__, source_ip_str, 64000, udp_owner.reply);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -359,20 +358,20 @@ err_t udp_broadcast_connection_init(void) {
|
|||||||
struct udp_pcb* connection;
|
struct udp_pcb* connection;
|
||||||
err_t err;
|
err_t err;
|
||||||
|
|
||||||
LOG_INFO(TAG, "udp_broadcast_connection_init: initializing UDP server");
|
LOG_INFO(TAG, "%s: initializing UDP server", __func__);
|
||||||
connection = udp_new();
|
connection = udp_new();
|
||||||
if (connection == NULL) {
|
if (connection == NULL) {
|
||||||
LOG_WARN(TAG, "udp_broadcast_connection_init: Initializing UDP server failed, connection is null");
|
LOG_WARN(TAG, "%s: Initializing UDP server failed, connection is null", __func__);
|
||||||
return ERR_MEM;
|
return ERR_MEM;
|
||||||
}
|
}
|
||||||
err = udp_bind(connection, IP_ANY_TYPE, 64000);
|
err = udp_bind(connection, IP_ANY_TYPE, 64000);
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
LOG_WARN(TAG, "udp_broadcast_connection_init: Initializing UDP server failed, err not ok");
|
LOG_WARN(TAG, "%s: Initializing UDP server failed, err not ok", __func__);
|
||||||
udp_remove(connection);
|
udp_remove(connection);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
udp_recv(connection, udp_receive_callback, NULL);
|
udp_recv(connection, udp_receive_callback, NULL);
|
||||||
LOG_INFO(TAG, "udp_broadcast_connection_init: Initializing UDP server successful, callback running");
|
LOG_INFO(TAG, "%s: Initializing UDP server successful, callback running", __func__);
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,7 +392,7 @@ err_t udp_broadcast_init(uint16_t x_pos, uint16_t y_pos) {
|
|||||||
owner_name_x_pos = x_pos;
|
owner_name_x_pos = x_pos;
|
||||||
owner_name_y_pos = y_pos;
|
owner_name_y_pos = y_pos;
|
||||||
if(udp_broadcast_set_owner_details("name", "default") != ERR_OK){
|
if(udp_broadcast_set_owner_details("name", "default") != ERR_OK){
|
||||||
LOG_WARN(TAG, "udp_broadcast_init: don't give NULL pointers as arguments for the owner's details");
|
LOG_WARN(TAG, "%s: don't give NULL pointers as arguments for the owner's details", __func__);
|
||||||
return ERR_ARG;
|
return ERR_ARG;
|
||||||
}
|
}
|
||||||
return udp_broadcast_connection_init();
|
return udp_broadcast_connection_init();
|
||||||
|
|||||||
@@ -136,20 +136,12 @@ int main(void)
|
|||||||
|
|
||||||
// Initialize the UDP broadcast service
|
// Initialize the UDP broadcast service
|
||||||
|
|
||||||
if (udp_broadcast_init(10,255) == ERR_OK){
|
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()");
|
LOG_WARN(TAG,"error initializing udp connection, check warnings from udp_broadcast_init() or udp_broadcast_connection_init()");
|
||||||
}
|
}
|
||||||
|
|
||||||
connected:
|
|
||||||
if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven") != ERR_OK){
|
if (udp_broadcast_set_owner_details("Joran", "Van Nieuwenhoven") != ERR_OK){
|
||||||
LOG_WARN(TAG,"error setting owner's details");
|
LOG_WARN(TAG,"error setting owner's details");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* USER CODE END 2 */
|
/* USER CODE END 2 */
|
||||||
|
|
||||||
/* Infinite loop */
|
/* Infinite loop */
|
||||||
|
|||||||
Reference in New Issue
Block a user