From a9779cbadeb717164f3edcfd31fc3f3aaa3c05a5 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 26 Jul 2025 10:37:57 +0200 Subject: [PATCH] Added Version to Client Infos --- main/client_handler.h | 1 + main/communication_handler.c | 42 +++++++++++++++++++++++++++++++++--- main/communication_handler.h | 8 +++++-- main/main.c | 4 +++- 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/main/client_handler.h b/main/client_handler.h index f033677..5761379 100644 --- a/main/client_handler.h +++ b/main/client_handler.h @@ -28,6 +28,7 @@ typedef struct { uint8_t macAddr[MAC_LENGTH]; TickType_t lastSuccessfullPing; TickType_t lastPing; + uint16_t clientVersion; } ClientInfo; typedef struct { diff --git a/main/communication_handler.c b/main/communication_handler.c index fad3a66..5742097 100644 --- a/main/communication_handler.c +++ b/main/communication_handler.c @@ -18,19 +18,21 @@ static uint8_t channelNumber = 0; #define MAC_STRING_BUFFER_SIZE 18 -void init_com(ClientList *clients, uint8_t wifi_channel) { +int init_com(ClientList *clients, uint8_t wifi_channel) { // Initialisiere die Kommunikations-Warteschlange messageQueue = xQueueCreate(MESSAGE_QUEUE_SIZE, sizeof(BaseMessage)); if (messageQueue == NULL) { ESP_LOGE(TAG, "Message queue creation failed"); + return -1; } esp_client_list = clients; hasMaster = false; channelNumber = wifi_channel; + return 0; } -void add_peer(uint8_t *macAddr) { +int add_peer(uint8_t *macAddr) { esp_now_peer_info_t peerInfo = { .channel = channelNumber, .ifidx = ESP_IF_WIFI_STA, @@ -48,6 +50,7 @@ void add_peer(uint8_t *macAddr) { ESP_LOGE(TAG, "Client could not be added to client handler, removing " "it from esp now client list!"); esp_now_del_peer(peerInfo.peer_addr); + return -1; } ESP_LOGI(TAG, "New client added."); } @@ -60,7 +63,9 @@ void add_peer(uint8_t *macAddr) { } } else { ESP_LOGE(TAG, "Failed to add peer: %s", esp_err_to_name(result)); + return -1; } + return 0; } BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload, @@ -129,10 +134,18 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len) { ESP_LOGI(TAG, "MASTER GOT MESSAGE"); + BaseMessage replyMessage = {}; const BaseMessage *message = (const BaseMessage *)data; + int id; switch (message->commandPage) { case StatusPage: ESP_LOGI(TAG, "GOT STATUS MESSAGE"); + id = get_client_id(esp_client_list, esp_now_info->src_addr); + if (id >= 0) { + esp_client_list->Clients[id].clientVersion = + message->payload.status_payload.version; + } + break; case PingPage: ESP_LOGI(TAG, "GOT PING MESSAGE"); @@ -143,7 +156,7 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info, message->payload.ping_payload.timestamp, currentTime, diff, diff / 1000); // ping in ms - int id = get_client_id(esp_client_list, esp_now_info->src_addr); + id = get_client_id(esp_client_list, esp_now_info->src_addr); if (id >= 0) { esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount(); esp_client_list->Clients[id].lastPing = (diff / 1000); @@ -179,6 +192,15 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info, case (ESP_ERR_ESPNOW_NOT_FOUND): ESP_LOGI(TAG, "CLIENT WIRD IN DIE LISTE AUFGENOMMEN"); add_peer(esp_now_info->src_addr); + ESP_LOGI(TAG, "FRAGE CLIENT STATUS AN"); + + GetStatusPayload payload = {}; + replyMessage = MessageBuilder(GetStatusPage, *(PayloadUnion *)&payload, + sizeof(payload)); + ESP_ERROR_CHECK(esp_now_send(esp_now_info->src_addr, + (uint8_t *)&replyMessage, + sizeof(BaseMessage))); + break; default: ESP_LOGI(TAG, "Unknown Message %i", checkPeer); @@ -204,6 +226,20 @@ void client_receive_callback(const esp_now_recv_info_t *esp_now_info, case StatusPage: ESP_LOGI(TAG, "GOT STATUS MESSAGE"); break; + case GetStatusPage: { + StatusPayload payload = { + .status = 1, + .runningPartition = 1, + .uptime = 100, + .version = 0x0002, + + }; + replyMessage = + MessageBuilder(StatusPage, *(PayloadUnion *)&payload, sizeof(payload)); + ESP_ERROR_CHECK(esp_now_send( + esp_now_info->src_addr, (uint8_t *)&replyMessage, sizeof(BaseMessage))); + + } break; case PingPage: ESP_LOGI(TAG, "GOT PING MESSAGE"); replyMessage = MessageBuilder(PingPage, *(PayloadUnion *)&message->payload, diff --git a/main/communication_handler.h b/main/communication_handler.h index edf5bcf..b49d472 100644 --- a/main/communication_handler.h +++ b/main/communication_handler.h @@ -29,6 +29,7 @@ static uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF, typedef enum { StatusPage, + GetStatusPage, ConfigPage, PingPage, BroadCastPage, @@ -44,6 +45,9 @@ typedef struct __attribute__((packed)) { uint32_t uptime; } StatusPayload; +typedef struct __attribute__((packed)) { +} GetStatusPayload; + typedef struct __attribute__((packed)) { uint8_t timeslot; } ConfigPayload; @@ -93,9 +97,9 @@ typedef struct __attribute__((packed)) { static_assert(sizeof(BaseMessage) <= 255, "BaseMessage darf nicht größer als 255 sein"); -void init_com(ClientList *clients, uint8_t wifi_channel); +int init_com(ClientList *clients, uint8_t wifi_channel); int getNextFreeClientId(); -void add_peer(uint8_t *macAddr); +int add_peer(uint8_t *macAddr); BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload, size_t payload_size); diff --git a/main/main.c b/main/main.c index 7784794..6d86f5a 100644 --- a/main/main.c +++ b/main/main.c @@ -89,7 +89,7 @@ void clientInfoCallback(uint8_t msgid, const uint8_t *payload, size_t send_payload_buffer_size, uint8_t *send_buffer, size_t send_buffer_size) { ESP_LOGI(TAG, "Client Info Command 0x03..."); - static uint8_t entryLength = 17; + static uint8_t entryLength = 19; uint8_t needed_buffer_size = 1 + entryLength * clientList.ClientCount; if (send_payload_buffer_size < needed_buffer_size) { @@ -131,6 +131,8 @@ void clientInfoCallback(uint8_t msgid, const uint8_t *payload, 4); memcpy(&send_payload_buffer[offset + 13], &clientList.Clients[i].lastSuccessfullPing, 4); +memcpy(&send_payload_buffer[offset + 17], + &clientList.Clients[i].clientVersion, 2); } }