From 0934105952e63a5f34ccc3a03c7becfd090e6707 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 24 Jul 2025 14:32:25 +0200 Subject: [PATCH] Reworked MAC to String logic to many little buffers --- main/communication_handler.c | 43 ++++++++++++------------------------ main/communication_handler.h | 4 +++- 2 files changed, 17 insertions(+), 30 deletions(-) diff --git a/main/communication_handler.c b/main/communication_handler.c index a4c01bc..a9f836a 100644 --- a/main/communication_handler.c +++ b/main/communication_handler.c @@ -39,10 +39,7 @@ void add_peer(uint8_t *macAddr) { esp_err_t result = esp_now_add_peer(&peerInfo); if (result == ESP_OK) { - ESP_LOGI(TAG, "Peer added: %02X:%02X:%02X:%02X:%02X:%02X", - peerInfo.peer_addr[0], peerInfo.peer_addr[1], - peerInfo.peer_addr[2], peerInfo.peer_addr[3], - peerInfo.peer_addr[4], peerInfo.peer_addr[5]); + ESP_LOGI(TAG, "Peer added: " MACSTR, MAC2STR(peerInfo.peer_addr)); if (!IS_BROADCAST_ADDR(macAddr)) { int ret = add_client(esp_client_list, peerInfo.peer_addr); @@ -65,11 +62,6 @@ void add_peer(uint8_t *macAddr) { } } -void MACtoString(const uint8_t *macAddr, char *outputBuffer) { - sprintf(outputBuffer, "%02X:%02X:%02X:%02X:%02X:%02X", macAddr[0], macAddr[1], - macAddr[2], macAddr[3], macAddr[4], macAddr[5]); -} - BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload, size_t payload_size) { BaseMessage message; @@ -115,12 +107,11 @@ void master_broadcast_ping(void *param) { } void master_ping_task(void *param) { - char macBuffer[MAC_STRING_BUFFER_SIZE]; while (1) { for (size_t i = 0; i < MAX_CLIENTS; i++) { if (esp_client_list->Clients[i].isAvailable) { - MACtoString(esp_client_list->Clients[i].macAddr, macBuffer); - ESP_LOGI(TAG, "SEND PING TO %zu: %s", i, macBuffer); + ESP_LOGI(TAG, "SEND PING TO %zu: " MACSTR, i, + MAC2STR(esp_client_list->Clients[i].macAddr)); PingPayload payload = {}; payload.timestamp = esp_timer_get_time(); BaseMessage message = MessageBuilder( @@ -136,7 +127,6 @@ void master_ping_task(void *param) { void master_receive_callback(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len) { - char macBuffer[MAC_STRING_BUFFER_SIZE]; ESP_LOGI(TAG, "MASTER GOT MESSAGE"); const BaseMessage *message = (const BaseMessage *)data; @@ -157,9 +147,9 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info, if (id >= 0) { esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount(); esp_client_list->Clients[id].lastPing = (diff / 1000); - MACtoString(esp_now_info->src_addr, macBuffer); - ESP_LOGI(TAG, "Updated client %d: %s last ping time to %lu", id, - macBuffer, esp_client_list->Clients[id].lastSuccessfullPing); + ESP_LOGI(TAG, "Updated client %d: " MACSTR " last ping time to %lu", id, + MAC2STR(esp_now_info->src_addr), + esp_client_list->Clients[id].lastSuccessfullPing); } break; case BroadCastPage: @@ -201,10 +191,9 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info, } void client_receive_callback(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len) { - char macBuffer[MAC_STRING_BUFFER_SIZE]; ESP_LOGI(TAG, "SLAVE GOT MESSAGE"); - MACtoString(esp_now_info->src_addr, macBuffer); - ESP_LOGI(TAG, "Received message from: %s", macBuffer); + ESP_LOGI(TAG, "Received message from: " MACSTR, + MAC2STR(esp_now_info->src_addr)); ESP_LOGI(TAG, "Message: %.*s", data_len, data); BaseMessage replyMessage = {}; @@ -251,36 +240,32 @@ void client_data_sending_task(void *param) { const char *dataToSend = "DATA:42"; ESP_LOGI(TAG, "SEND DATA"); esp_now_send(NULL, (uint8_t *)dataToSend, - strlen(dataToSend)); // Sende Daten an Master + strlen(dataToSend)); vTaskDelay(pdMS_TO_TICKS(5000)); } } void client_monitor_task(void *pvParameters) { - char macBuffer[MAC_STRING_BUFFER_SIZE]; TickType_t timeout_ticks = - pdMS_TO_TICKS(CLIENT_TIMEOUT_MS); // Timeout in Ticks + pdMS_TO_TICKS(CLIENT_TIMEOUT_MS); TickType_t interval_ticks = - pdMS_TO_TICKS(CHECK_INTERVAL_MS); // Prüfintervall in Ticks + pdMS_TO_TICKS(CHECK_INTERVAL_MS); while (1) { - TickType_t now = xTaskGetTickCount(); // Aktuelle Zeit in Ticks + TickType_t now = xTaskGetTickCount(); for (int i = 0; i < MAX_CLIENTS; i++) { if (esp_client_list->Clients[i].isAvailable) { TickType_t time_diff = now - esp_client_list->Clients[i].lastSuccessfullPing; - // Prüfen, ob der Client als "nicht verfügbar" markiert werden soll if (time_diff > timeout_ticks) { esp_client_list->Clients[i].isAvailable = false; - MACtoString(esp_client_list->Clients[i].macAddr, macBuffer); - ESP_LOGW(TAG, "Client %d (MAC: %s) is unavailable", macBuffer); + ESP_LOGW(TAG, "Client %d (MAC: " MACSTR ") is unavailable", + MAC2STR(esp_client_list->Clients[i].macAddr)); } } } - - // Task für das Prüfintervall anhalten vTaskDelay(interval_ticks); } } diff --git a/main/communication_handler.h b/main/communication_handler.h index d550208..be3916e 100644 --- a/main/communication_handler.h +++ b/main/communication_handler.h @@ -21,6 +21,9 @@ static uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, #define IS_BROADCAST_ADDR(addr) \ (memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0) +#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] +#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X" + #define MESSAGE_QUEUE_SIZE 10 typedef enum { @@ -66,7 +69,6 @@ static_assert(sizeof(BaseMessage) <= 255, void init_com(ClientList *clients); int getNextFreeClientId(); void add_peer(uint8_t *macAddr); -void MACtoString(const uint8_t *macAddr, char *outputBuffer); BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload, size_t payload_size);