Added Version to Client Infos

This commit is contained in:
simon 2025-07-26 10:37:57 +02:00
parent 704d1c9c0b
commit a9779cbade
4 changed files with 49 additions and 6 deletions

View File

@ -28,6 +28,7 @@ typedef struct {
uint8_t macAddr[MAC_LENGTH]; uint8_t macAddr[MAC_LENGTH];
TickType_t lastSuccessfullPing; TickType_t lastSuccessfullPing;
TickType_t lastPing; TickType_t lastPing;
uint16_t clientVersion;
} ClientInfo; } ClientInfo;
typedef struct { typedef struct {

View File

@ -18,19 +18,21 @@ static uint8_t channelNumber = 0;
#define MAC_STRING_BUFFER_SIZE 18 #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 // Initialisiere die Kommunikations-Warteschlange
messageQueue = xQueueCreate(MESSAGE_QUEUE_SIZE, sizeof(BaseMessage)); messageQueue = xQueueCreate(MESSAGE_QUEUE_SIZE, sizeof(BaseMessage));
if (messageQueue == NULL) { if (messageQueue == NULL) {
ESP_LOGE(TAG, "Message queue creation failed"); ESP_LOGE(TAG, "Message queue creation failed");
return -1;
} }
esp_client_list = clients; esp_client_list = clients;
hasMaster = false; hasMaster = false;
channelNumber = wifi_channel; channelNumber = wifi_channel;
return 0;
} }
void add_peer(uint8_t *macAddr) { int add_peer(uint8_t *macAddr) {
esp_now_peer_info_t peerInfo = { esp_now_peer_info_t peerInfo = {
.channel = channelNumber, .channel = channelNumber,
.ifidx = ESP_IF_WIFI_STA, .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 " ESP_LOGE(TAG, "Client could not be added to client handler, removing "
"it from esp now client list!"); "it from esp now client list!");
esp_now_del_peer(peerInfo.peer_addr); esp_now_del_peer(peerInfo.peer_addr);
return -1;
} }
ESP_LOGI(TAG, "New client added."); ESP_LOGI(TAG, "New client added.");
} }
@ -60,7 +63,9 @@ void add_peer(uint8_t *macAddr) {
} }
} else { } else {
ESP_LOGE(TAG, "Failed to add peer: %s", esp_err_to_name(result)); ESP_LOGE(TAG, "Failed to add peer: %s", esp_err_to_name(result));
return -1;
} }
return 0;
} }
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload, 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) { const uint8_t *data, int data_len) {
ESP_LOGI(TAG, "MASTER GOT MESSAGE"); ESP_LOGI(TAG, "MASTER GOT MESSAGE");
BaseMessage replyMessage = {};
const BaseMessage *message = (const BaseMessage *)data; const BaseMessage *message = (const BaseMessage *)data;
int id;
switch (message->commandPage) { switch (message->commandPage) {
case StatusPage: case StatusPage:
ESP_LOGI(TAG, "GOT STATUS MESSAGE"); 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; break;
case PingPage: case PingPage:
ESP_LOGI(TAG, "GOT PING MESSAGE"); 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, message->payload.ping_payload.timestamp, currentTime, diff,
diff / 1000); // ping in ms 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) { if (id >= 0) {
esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount(); esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount();
esp_client_list->Clients[id].lastPing = (diff / 1000); 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): case (ESP_ERR_ESPNOW_NOT_FOUND):
ESP_LOGI(TAG, "CLIENT WIRD IN DIE LISTE AUFGENOMMEN"); ESP_LOGI(TAG, "CLIENT WIRD IN DIE LISTE AUFGENOMMEN");
add_peer(esp_now_info->src_addr); 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; break;
default: default:
ESP_LOGI(TAG, "Unknown Message %i", checkPeer); 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: case StatusPage:
ESP_LOGI(TAG, "GOT STATUS MESSAGE"); ESP_LOGI(TAG, "GOT STATUS MESSAGE");
break; 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: case PingPage:
ESP_LOGI(TAG, "GOT PING MESSAGE"); ESP_LOGI(TAG, "GOT PING MESSAGE");
replyMessage = MessageBuilder(PingPage, *(PayloadUnion *)&message->payload, replyMessage = MessageBuilder(PingPage, *(PayloadUnion *)&message->payload,

View File

@ -29,6 +29,7 @@ static uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
typedef enum { typedef enum {
StatusPage, StatusPage,
GetStatusPage,
ConfigPage, ConfigPage,
PingPage, PingPage,
BroadCastPage, BroadCastPage,
@ -44,6 +45,9 @@ typedef struct __attribute__((packed)) {
uint32_t uptime; uint32_t uptime;
} StatusPayload; } StatusPayload;
typedef struct __attribute__((packed)) {
} GetStatusPayload;
typedef struct __attribute__((packed)) { typedef struct __attribute__((packed)) {
uint8_t timeslot; uint8_t timeslot;
} ConfigPayload; } ConfigPayload;
@ -93,9 +97,9 @@ typedef struct __attribute__((packed)) {
static_assert(sizeof(BaseMessage) <= 255, static_assert(sizeof(BaseMessage) <= 255,
"BaseMessage darf nicht größer als 255 sein"); "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(); int getNextFreeClientId();
void add_peer(uint8_t *macAddr); int add_peer(uint8_t *macAddr);
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload, BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
size_t payload_size); size_t payload_size);

View File

@ -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_payload_buffer_size, uint8_t *send_buffer,
size_t send_buffer_size) { size_t send_buffer_size) {
ESP_LOGI(TAG, "Client Info Command 0x03..."); 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; uint8_t needed_buffer_size = 1 + entryLength * clientList.ClientCount;
if (send_payload_buffer_size < needed_buffer_size) { if (send_payload_buffer_size < needed_buffer_size) {
@ -131,6 +131,8 @@ void clientInfoCallback(uint8_t msgid, const uint8_t *payload,
4); 4);
memcpy(&send_payload_buffer[offset + 13], memcpy(&send_payload_buffer[offset + 13],
&clientList.Clients[i].lastSuccessfullPing, 4); &clientList.Clients[i].lastSuccessfullPing, 4);
memcpy(&send_payload_buffer[offset + 17],
&clientList.Clients[i].clientVersion, 2);
} }
} }