Compare commits
5 Commits
94b5fd47a4
...
fad6a0aee2
| Author | SHA1 | Date | |
|---|---|---|---|
| fad6a0aee2 | |||
| 50ee8009fc | |||
| beef75f31c | |||
| c564fedf65 | |||
| b4d9f24f0e |
@ -1,3 +1,24 @@
|
|||||||
idf_component_register(SRCS "main.c" "uart_handler.c" "communication_handler.c" "uart_prot.c"
|
idf_component_register(SRCS "main.c" "uart_handler.c" "communication_handler.c" "client_handler.c" "message_parser.c" "message_builder.c" "message_handler.c"
|
||||||
INCLUDE_DIRS ".")
|
INCLUDE_DIRS ".")
|
||||||
|
|
||||||
|
# Get the short Git commit hash of the current HEAD.
|
||||||
|
# If not in a Git repository or git command fails, it will default to "N/A".
|
||||||
|
execute_process(
|
||||||
|
COMMAND git rev-parse --short HEAD
|
||||||
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||||
|
OUTPUT_VARIABLE GIT_COMMIT_HASH_SHORT
|
||||||
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
||||||
|
RESULT_VARIABLE GIT_HASH_RESULT
|
||||||
|
)
|
||||||
|
# Fallback if git is not available or not in a git repo
|
||||||
|
if(GIT_HASH_RESULT_CODE)
|
||||||
|
set(GIT_COMMIT_HASH_SHORT "N/A")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Add the Git hash as a preprocessor definition to your component.
|
||||||
|
# This makes BUILD_GIT_HASH available in your C/C++ source files.
|
||||||
|
target_compile_definitions(${COMPONENT_LIB} PRIVATE
|
||||||
|
BUILD_GIT_HASH="${GIT_COMMIT_HASH_SHORT}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
54
main/client_handler.c
Normal file
54
main/client_handler.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#include "client_handler.h"
|
||||||
|
#include "communication_handler.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int get_client_id(ClientList *list, const uint8_t *client_mac) {
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
if (memcmp(client_mac, list->Clients[i].macAddr, MAC_LENGTH) == 0) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return CLIENT_DOES_NOT_EXISTS;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Sanity check when list full then list->count should be MAX_CLIENTS
|
||||||
|
int get_next_free_slot(ClientList *list) {
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
// if slot is not used return index
|
||||||
|
if (!list->Clients[i].slotIsUsed) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// list is full
|
||||||
|
return CLIENT_LIST_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int add_client(ClientList *list, const uint8_t *client_mac) {
|
||||||
|
if (get_client_id(list, client_mac) >= 0) {
|
||||||
|
// Client already exists dont add to list
|
||||||
|
return CLIENT_EXISTS;
|
||||||
|
}
|
||||||
|
|
||||||
|
int slot = get_next_free_slot(list);
|
||||||
|
if (slot < 0) {
|
||||||
|
// Client list full
|
||||||
|
return CLIENT_LIST_FULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
list->Clients[slot].slotIsUsed = true;
|
||||||
|
list->Clients[slot].isAvailable = true;
|
||||||
|
memcpy(list->Clients[slot].macAddr, client_mac, MAC_LENGTH);
|
||||||
|
list->ClientCount++;
|
||||||
|
return CLIENT_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
int remove_client(ClientList *list, const uint8_t client_id) {
|
||||||
|
if (client_id >= MAX_CLIENTS)
|
||||||
|
return CLIENT_INVALID_ID; // invalid index
|
||||||
|
list->Clients[client_id].slotIsUsed = false;
|
||||||
|
list->ClientCount--;
|
||||||
|
return CLIENT_OK;
|
||||||
|
}
|
||||||
43
main/client_handler.h
Normal file
43
main/client_handler.h
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
#ifndef CLIENT_HANDLER_H
|
||||||
|
#define CLIENT_HANDLER_H
|
||||||
|
|
||||||
|
#include "portmacro.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/_intsup.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#define MAX_CLIENTS 16
|
||||||
|
#define MAC_LENGTH 6
|
||||||
|
|
||||||
|
enum ClientErrors {
|
||||||
|
CLIENT_OK = 0,
|
||||||
|
CLIENT_EXISTS = -1,
|
||||||
|
CLIENT_DOES_NOT_EXISTS = -2,
|
||||||
|
CLIENT_LIST_FULL = -3,
|
||||||
|
CLIENT_INVALID_ID = -4,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool slotIsUsed;
|
||||||
|
bool isAvailable;
|
||||||
|
uint8_t clientID;
|
||||||
|
uint8_t macAddr[MAC_LENGTH];
|
||||||
|
TickType_t lastSuccessfullPing;
|
||||||
|
TickType_t lastPing;
|
||||||
|
} ClientInfo;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ClientInfo Clients[MAX_CLIENTS];
|
||||||
|
uint8_t ClientCount;
|
||||||
|
} ClientList;
|
||||||
|
|
||||||
|
int get_client_id(ClientList *list, const uint8_t *client_mac);
|
||||||
|
int add_client(ClientList *list, const uint8_t *client_mac);
|
||||||
|
int remove_client(ClientList *list, const uint8_t clientid);
|
||||||
|
int get_next_free_slot(ClientList *list);
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1,41 +1,33 @@
|
|||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_now.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
#include "freertos/idf_additions.h"
|
#include "freertos/idf_additions.h"
|
||||||
|
|
||||||
|
#include "client_handler.h"
|
||||||
#include "communication_handler.h"
|
#include "communication_handler.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
static const char *TAG = "ALOX - COM";
|
static const char *TAG = "ALOX - COM";
|
||||||
|
|
||||||
QueueHandle_t messageQueue = NULL; // Warteschlange für empfangene Nachrichten
|
QueueHandle_t messageQueue = NULL; // Warteschlange für empfangene Nachrichten
|
||||||
ClientInfo clients[MAX_CLIENTS] = {
|
|
||||||
0}; // Clients statisch initialisieren, alle auf 0 gesetzt
|
|
||||||
size_t numClients = 0;
|
|
||||||
size_t activeClients = 0;
|
|
||||||
bool hasMaster = false;
|
bool hasMaster = false;
|
||||||
|
static ClientList *esp_client_list;
|
||||||
|
|
||||||
void init_com() {
|
#define MAC_STRING_BUFFER_SIZE 18
|
||||||
|
|
||||||
|
void init_com(ClientList *clients) {
|
||||||
// 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");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Weitere Initialisierungen, falls nötig
|
esp_client_list = clients;
|
||||||
numClients = 0;
|
|
||||||
activeClients = 0;
|
|
||||||
hasMaster = false;
|
hasMaster = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return any inactive client field for new usage
|
|
||||||
int getNextFreeClientId() {
|
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
||||||
if (!clients[i].isAvailable) {
|
|
||||||
return i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_peer(uint8_t *macAddr) {
|
void add_peer(uint8_t *macAddr) {
|
||||||
esp_now_peer_info_t peerInfo = {
|
esp_now_peer_info_t peerInfo = {
|
||||||
.channel =
|
.channel =
|
||||||
@ -53,41 +45,29 @@ void add_peer(uint8_t *macAddr) {
|
|||||||
peerInfo.peer_addr[4], peerInfo.peer_addr[5]);
|
peerInfo.peer_addr[4], peerInfo.peer_addr[5]);
|
||||||
|
|
||||||
if (!IS_BROADCAST_ADDR(macAddr)) {
|
if (!IS_BROADCAST_ADDR(macAddr)) {
|
||||||
|
int ret = add_client(esp_client_list, peerInfo.peer_addr);
|
||||||
if (numClients >= MAX_CLIENTS) {
|
if (ret < 0) {
|
||||||
ESP_LOGW(TAG, "Cannot add more clients, maximum reached.");
|
ESP_LOGE(TAG, "Client could not be added to client handler, removing "
|
||||||
return;
|
"it from esp now client list!");
|
||||||
|
esp_now_del_peer(peerInfo.peer_addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientInfo newClient = {};
|
|
||||||
memcpy(newClient.macAddr, macAddr, ESP_NOW_ETH_ALEN);
|
|
||||||
newClient.isAvailable = true;
|
|
||||||
newClient.lastSuccessfullPing = xTaskGetTickCount();
|
|
||||||
newClient.lastPing = 0;
|
|
||||||
clients[getNextFreeClientId()] = newClient;
|
|
||||||
ESP_LOGI(TAG, "New client added.");
|
ESP_LOGI(TAG, "New client added.");
|
||||||
}
|
}
|
||||||
} else if (result == ESP_ERR_ESPNOW_EXIST) {
|
} else if (result == ESP_ERR_ESPNOW_EXIST) {
|
||||||
ESP_LOGW(TAG, "Peer already exists.");
|
ESP_LOGW(TAG, "Peer already exists.");
|
||||||
// Überprüfen, ob der Client bereits existiert
|
int id = get_client_id(esp_client_list, peerInfo.peer_addr);
|
||||||
for (int i = 0; i < numClients; i++) {
|
if (id >= 0) {
|
||||||
if (memcmp(clients[i].macAddr, macAddr, ESP_NOW_ETH_ALEN) == 0) {
|
|
||||||
ESP_LOGI(TAG, "Client found again, welcome back!");
|
ESP_LOGI(TAG, "Client found again, welcome back!");
|
||||||
clients[i].isAvailable = true; // Reaktiviere den Client
|
esp_client_list->Clients[id].isAvailable = true;
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} 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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UNSAFE ACCROSS THREADS BUT EZ TO USE
|
void MACtoString(const uint8_t *macAddr, char *outputBuffer) {
|
||||||
const char *MACtoString(uint8_t *macAddr) {
|
sprintf(outputBuffer, "%02X:%02X:%02X:%02X:%02X:%02X", macAddr[0], macAddr[1],
|
||||||
static char output[18]; // 17 Zeichen + 1 für Nullterminierung
|
|
||||||
sprintf(output, "%02X:%02X:%02X:%02X:%02X:%02X", macAddr[0], macAddr[1],
|
|
||||||
macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
macAddr[2], macAddr[3], macAddr[4], macAddr[5]);
|
||||||
return output;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
||||||
@ -135,16 +115,17 @@ void master_broadcast_ping(void *param) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void master_ping_task(void *param) {
|
void master_ping_task(void *param) {
|
||||||
|
char macBuffer[MAC_STRING_BUFFER_SIZE];
|
||||||
while (1) {
|
while (1) {
|
||||||
for (size_t i = 0; i < MAX_CLIENTS; i++) {
|
for (size_t i = 0; i < MAX_CLIENTS; i++) {
|
||||||
if (clients[i].isAvailable) {
|
if (esp_client_list->Clients[i].isAvailable) {
|
||||||
ESP_LOGI(TAG, "SEND PING TO %zu: %s", i,
|
MACtoString(esp_client_list->Clients[i].macAddr, macBuffer);
|
||||||
MACtoString(clients[i].macAddr));
|
ESP_LOGI(TAG, "SEND PING TO %zu: %s", i, macBuffer);
|
||||||
PingPayload payload = {};
|
PingPayload payload = {};
|
||||||
payload.timestamp = esp_timer_get_time();
|
payload.timestamp = esp_timer_get_time();
|
||||||
BaseMessage message = MessageBuilder(
|
BaseMessage message = MessageBuilder(
|
||||||
PingPage, *(PayloadUnion *)&payload, sizeof(payload));
|
PingPage, *(PayloadUnion *)&payload, sizeof(payload));
|
||||||
esp_now_send(clients[i].macAddr, (uint8_t *)&message,
|
esp_now_send(esp_client_list->Clients[i].macAddr, (uint8_t *)&message,
|
||||||
sizeof(BaseMessage));
|
sizeof(BaseMessage));
|
||||||
ESP_LOGI(TAG, "SENDING PING!!!!");
|
ESP_LOGI(TAG, "SENDING PING!!!!");
|
||||||
}
|
}
|
||||||
@ -155,6 +136,7 @@ void master_ping_task(void *param) {
|
|||||||
|
|
||||||
void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
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) {
|
||||||
|
char macBuffer[MAC_STRING_BUFFER_SIZE];
|
||||||
ESP_LOGI(TAG, "MASTER GOT MESSAGE");
|
ESP_LOGI(TAG, "MASTER GOT MESSAGE");
|
||||||
|
|
||||||
const BaseMessage *message = (const BaseMessage *)data;
|
const BaseMessage *message = (const BaseMessage *)data;
|
||||||
@ -171,18 +153,13 @@ 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
|
||||||
|
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
int id = get_client_id(esp_client_list, esp_now_info->src_addr);
|
||||||
// Überprüfen, ob der Client existiert und die MAC-Adresse übereinstimmt
|
if (id >= 0) {
|
||||||
if (clients[i].isAvailable &&
|
esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount();
|
||||||
memcmp(clients[i].macAddr, esp_now_info->src_addr,
|
esp_client_list->Clients[id].lastPing = (diff / 1000);
|
||||||
ESP_NOW_ETH_ALEN) == 0) {
|
MACtoString(esp_now_info->src_addr, macBuffer);
|
||||||
clients[i].lastSuccessfullPing = xTaskGetTickCount();
|
ESP_LOGI(TAG, "Updated client %d: %s last ping time to %lu", id,
|
||||||
clients[i].lastPing = (diff/1000);
|
macBuffer, esp_client_list->Clients[id].lastSuccessfullPing);
|
||||||
ESP_LOGI(TAG, "Updated client %d: %s last ping time to %lu", i,
|
|
||||||
MACtoString(clients[i].macAddr),
|
|
||||||
clients[i].lastSuccessfullPing);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case BroadCastPage:
|
case BroadCastPage:
|
||||||
@ -197,18 +174,11 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|||||||
switch (checkPeer) {
|
switch (checkPeer) {
|
||||||
case (ESP_OK):
|
case (ESP_OK):
|
||||||
ESP_LOGI(TAG, "CLIENT BEKANNT");
|
ESP_LOGI(TAG, "CLIENT BEKANNT");
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
int id = get_client_id(esp_client_list, esp_now_info->src_addr);
|
||||||
// client in liste wiederfinden
|
esp_client_list->Clients[id].isAvailable = true;
|
||||||
if (!clients[i].isAvailable &&
|
esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount();
|
||||||
memcmp(clients[i].macAddr, esp_now_info->src_addr,
|
ESP_LOGI(TAG, "Updated client %d last ping time to %lu", id,
|
||||||
ESP_NOW_ETH_ALEN) == 0) {
|
esp_client_list->Clients[id].lastSuccessfullPing);
|
||||||
clients[i].isAvailable = true;
|
|
||||||
clients[i].lastSuccessfullPing = xTaskGetTickCount();
|
|
||||||
ESP_LOGI(TAG, "Updated client %d last ping time to %lu", i,
|
|
||||||
clients[i].lastSuccessfullPing);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case (ESP_ERR_ESPNOW_NOT_INIT):
|
case (ESP_ERR_ESPNOW_NOT_INIT):
|
||||||
ESP_LOGI(TAG, "Not initalised");
|
ESP_LOGI(TAG, "Not initalised");
|
||||||
@ -229,14 +199,12 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void client_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) {
|
const uint8_t *data, int data_len) {
|
||||||
|
char macBuffer[MAC_STRING_BUFFER_SIZE];
|
||||||
ESP_LOGI(TAG, "SLAVE GOT MESSAGE");
|
ESP_LOGI(TAG, "SLAVE GOT MESSAGE");
|
||||||
ESP_LOGI(TAG, "Received message from: %02X:%02X:%02X:%02X:%02X:%02X",
|
MACtoString(esp_now_info->src_addr, macBuffer);
|
||||||
esp_now_info->src_addr[0], esp_now_info->src_addr[1],
|
ESP_LOGI(TAG, "Received message from: %s", macBuffer);
|
||||||
esp_now_info->src_addr[2], esp_now_info->src_addr[3],
|
|
||||||
esp_now_info->src_addr[4], esp_now_info->src_addr[5]);
|
|
||||||
ESP_LOGI(TAG, "Message: %.*s", data_len, data);
|
ESP_LOGI(TAG, "Message: %.*s", data_len, data);
|
||||||
|
|
||||||
BaseMessage replyMessage = {};
|
BaseMessage replyMessage = {};
|
||||||
@ -289,6 +257,7 @@ void client_data_sending_task(void *param) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void client_monitor_task(void *pvParameters) {
|
void client_monitor_task(void *pvParameters) {
|
||||||
|
char macBuffer[MAC_STRING_BUFFER_SIZE];
|
||||||
TickType_t timeout_ticks =
|
TickType_t timeout_ticks =
|
||||||
pdMS_TO_TICKS(CLIENT_TIMEOUT_MS); // Timeout in Ticks
|
pdMS_TO_TICKS(CLIENT_TIMEOUT_MS); // Timeout in Ticks
|
||||||
TickType_t interval_ticks =
|
TickType_t interval_ticks =
|
||||||
@ -298,18 +267,15 @@ void client_monitor_task(void *pvParameters) {
|
|||||||
TickType_t now = xTaskGetTickCount(); // Aktuelle Zeit in Ticks
|
TickType_t now = xTaskGetTickCount(); // Aktuelle Zeit in Ticks
|
||||||
|
|
||||||
for (int i = 0; i < MAX_CLIENTS; i++) {
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
if (clients[i].isAvailable) {
|
if (esp_client_list->Clients[i].isAvailable) {
|
||||||
TickType_t time_diff = now - clients[i].lastSuccessfullPing;
|
TickType_t time_diff =
|
||||||
|
now - esp_client_list->Clients[i].lastSuccessfullPing;
|
||||||
|
|
||||||
// Prüfen, ob der Client als "nicht verfügbar" markiert werden soll
|
// Prüfen, ob der Client als "nicht verfügbar" markiert werden soll
|
||||||
if (time_diff > timeout_ticks) {
|
if (time_diff > timeout_ticks) {
|
||||||
clients[i].isAvailable = false;
|
esp_client_list->Clients[i].isAvailable = false;
|
||||||
ESP_LOGW(
|
MACtoString(esp_client_list->Clients[i].macAddr, macBuffer);
|
||||||
TAG,
|
ESP_LOGW(TAG, "Client %d (MAC: %s) is unavailable", macBuffer);
|
||||||
"Client %d (MAC: %02X:%02X:%02X:%02X:%02X:%02X) is unavailable",
|
|
||||||
i, clients[i].macAddr[0], clients[i].macAddr[1],
|
|
||||||
clients[i].macAddr[2], clients[i].macAddr[3],
|
|
||||||
clients[i].macAddr[4], clients[i].macAddr[5]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -318,7 +284,3 @@ void client_monitor_task(void *pvParameters) {
|
|||||||
vTaskDelay(interval_ticks);
|
vTaskDelay(interval_ticks);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientInfo *get_client_info(int entry) {
|
|
||||||
return &clients[entry];
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
#ifndef COMMUNICATION_HANDLER_H
|
#ifndef COMMUNICATION_HANDLER_H
|
||||||
#define COMMUNICATION_HANDLER_H
|
#define COMMUNICATION_HANDLER_H
|
||||||
|
|
||||||
|
#include "client_handler.h"
|
||||||
#include <esp_now.h>
|
#include <esp_now.h>
|
||||||
#include <esp_wifi.h>
|
#include <esp_wifi.h>
|
||||||
#include <freertos/FreeRTOS.h>
|
#include <freertos/FreeRTOS.h>
|
||||||
@ -20,7 +21,6 @@ static uint8_t broadcast_address[ESP_NOW_ETH_ALEN] = {0xFF, 0xFF, 0xFF,
|
|||||||
#define IS_BROADCAST_ADDR(addr) \
|
#define IS_BROADCAST_ADDR(addr) \
|
||||||
(memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0)
|
(memcmp(addr, broadcast_address, ESP_NOW_ETH_ALEN) == 0)
|
||||||
|
|
||||||
#define MAX_CLIENTS 19
|
|
||||||
#define MESSAGE_QUEUE_SIZE 10
|
#define MESSAGE_QUEUE_SIZE 10
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@ -63,18 +63,10 @@ typedef struct {
|
|||||||
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");
|
||||||
|
|
||||||
typedef struct {
|
void init_com(ClientList *clients);
|
||||||
uint8_t macAddr[ESP_NOW_ETH_ALEN];
|
|
||||||
int rssi;
|
|
||||||
bool isAvailable;
|
|
||||||
TickType_t lastSuccessfullPing;
|
|
||||||
TickType_t lastPing;
|
|
||||||
} ClientInfo;
|
|
||||||
|
|
||||||
void init_com();
|
|
||||||
int getNextFreeClientId();
|
int getNextFreeClientId();
|
||||||
void add_peer(uint8_t *macAddr);
|
void add_peer(uint8_t *macAddr);
|
||||||
const char *MACtoString(uint8_t *macAddr);
|
void MACtoString(const uint8_t *macAddr, char *outputBuffer);
|
||||||
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
BaseMessage MessageBuilder(CommandPages commandPage, PayloadUnion payload,
|
||||||
size_t payload_size);
|
size_t payload_size);
|
||||||
|
|
||||||
@ -89,6 +81,4 @@ void client_receive_callback(const esp_now_recv_info_t *esp_now_info,
|
|||||||
void client_data_sending_task(void *param);
|
void client_data_sending_task(void *param);
|
||||||
void client_monitor_task(void *pvParameters);
|
void client_monitor_task(void *pvParameters);
|
||||||
|
|
||||||
ClientInfo *get_client_info(int entry);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
163
main/main.c
163
main/main.c
@ -1,3 +1,4 @@
|
|||||||
|
#include "client_handler.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
@ -7,28 +8,143 @@
|
|||||||
#include "esp_wifi.h"
|
#include "esp_wifi.h"
|
||||||
#include "freertos/idf_additions.h"
|
#include "freertos/idf_additions.h"
|
||||||
#include "hal/uart_types.h"
|
#include "hal/uart_types.h"
|
||||||
|
#include "message_handler.h"
|
||||||
|
#include "message_parser.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
|
|
||||||
|
#include "glob.h"
|
||||||
#include "communication_handler.h"
|
#include "communication_handler.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
#include "portmacro.h"
|
#include "portmacro.h"
|
||||||
#include "uart_handler.h"
|
#include "uart_handler.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
#include "message_builder.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
QueueHandle_t message_queue;
|
||||||
|
uint8_t *send_buffer;
|
||||||
|
size_t send_buffer_size;
|
||||||
|
} MessageBrokerTaskParams_t;
|
||||||
|
|
||||||
static const char *TAG = "ALOX - MAIN";
|
static const char *TAG = "ALOX - MAIN";
|
||||||
|
static const uint16_t version = 0x0001;
|
||||||
|
static uint8_t send_message_buffer[1024];
|
||||||
|
static MessageBrokerTaskParams_t broker_task_params;
|
||||||
|
|
||||||
void SendClientInfoTask() {
|
ClientList clientList = {.Clients = {{0}}, .ClientCount = 0};
|
||||||
int clientId = 0;
|
|
||||||
while (1) {
|
typedef void (*RegisterTaskCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
ClientInfo info = *get_client_info(clientId);
|
size_t payload_len, uint8_t *send_buffer,
|
||||||
send_client_info(clientId, info.isAvailable, info.lastPing);
|
size_t send_buffer_size);
|
||||||
clientId += 1;
|
|
||||||
clientId = clientId%20;
|
void echoCallback(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
uint8_t *send_buffer, size_t send_buffer_size) {
|
||||||
|
// Code für den Button-Press
|
||||||
|
ESP_LOGI(TAG, "Echo command 0x01...");
|
||||||
|
int len =
|
||||||
|
build_message(0x01, payload, payload_len, send_buffer, send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void versionCallback(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
|
uint8_t *send_buffer, size_t send_buffer_size) {
|
||||||
|
// Code für den Button-Press
|
||||||
|
ESP_LOGI(TAG, "Version command 0x02...");
|
||||||
|
size_t git_build_hash_len = strlen(BUILD_GIT_HASH);
|
||||||
|
|
||||||
|
uint8_t send_payload[2 + git_build_hash_len];
|
||||||
|
send_payload[0] = (uint8_t)(version & 0xFF);
|
||||||
|
send_payload[1] = (uint8_t)((version >> 8) & 0xFF);
|
||||||
|
memcpy(&send_payload[2], &BUILD_GIT_HASH, git_build_hash_len);
|
||||||
|
|
||||||
|
int len = build_message(0x02, send_payload, sizeof(send_payload), send_buffer,
|
||||||
|
send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
void clientInfoCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size) {
|
||||||
|
ESP_LOGI(TAG, "Client Info Command 0x03...");
|
||||||
|
static uint8_t entryLength = 17;
|
||||||
|
|
||||||
|
ESP_LOGE(TAG, "clientList.ClientCount = %d", clientList.ClientCount);
|
||||||
|
|
||||||
|
uint8_t payload_size = 1 + entryLength * clientList.ClientCount;
|
||||||
|
|
||||||
|
ESP_LOGE(TAG, "payload_size = %d", payload_size);
|
||||||
|
|
||||||
|
uint8_t send_payload[payload_size];
|
||||||
|
send_payload[0] = clientList.ClientCount;
|
||||||
|
uint8_t offsetMult = 0;
|
||||||
|
|
||||||
|
uint8_t used_slots = 0;
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
if (clientList.Clients[i].slotIsUsed) {
|
||||||
|
used_slots++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ESP_LOGE("SPECIAL", "USED SLOTS: %d", used_slots);
|
||||||
|
|
||||||
|
uint8_t loop_sanity_counter = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
||||||
|
if (clientList.Clients[i].slotIsUsed) {
|
||||||
|
loop_sanity_counter++;
|
||||||
|
if (loop_sanity_counter > clientList.ClientCount) {
|
||||||
|
ESP_LOGE("SPECIAL", "DAS KANN NICHT SEIN");
|
||||||
|
ESP_LOGE("SPECIAL", "loop_santy_count: %d, client_count: %d", loop_sanity_counter, clientList.ClientCount);
|
||||||
|
}
|
||||||
|
size_t offset = 1 + (entryLength * offsetMult++);
|
||||||
|
|
||||||
|
ESP_LOGE("SPECIAL", "OFFSET %d", offset);
|
||||||
|
send_payload[offset] = i;
|
||||||
|
send_payload[offset + 1] = clientList.Clients[i].isAvailable;
|
||||||
|
send_payload[offset + 2] = clientList.Clients[i].slotIsUsed;
|
||||||
|
memcpy(&send_payload[offset + 3], clientList.Clients[i].macAddr,
|
||||||
|
MAC_LENGTH);
|
||||||
|
memcpy(&send_payload[offset + 9], &clientList.Clients[i].lastPing, 4);
|
||||||
|
memcpy(&send_payload[offset + 13],
|
||||||
|
&clientList.Clients[i].lastSuccessfullPing, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int len = build_message(0x04, send_payload, sizeof(send_payload), send_buffer,
|
||||||
|
send_buffer_size);
|
||||||
|
if (len < 0) {
|
||||||
|
ESP_LOGE(TAG,
|
||||||
|
"Error Building UART Message: payload_len, %d, sendbuffer_size: "
|
||||||
|
"%d, mes_len(error): %d",
|
||||||
|
payload_len, send_buffer_size, len);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uart_write_bytes(MASTER_UART, send_buffer, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
void app_main(void) {
|
void app_main(void) {
|
||||||
|
ESP_LOGI(TAG, "Starting Alox Powerpod Version %d Build: %s", version,
|
||||||
|
BUILD_GIT_HASH);
|
||||||
|
|
||||||
esp_err_t ret = nvs_flash_init();
|
esp_err_t ret = nvs_flash_init();
|
||||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES ||
|
||||||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||||
@ -54,8 +170,8 @@ void app_main(void) {
|
|||||||
// denselben Kanal verwenden
|
// denselben Kanal verwenden
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
||||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||||
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||||
ESP_ERROR_CHECK(esp_wifi_start());
|
ESP_ERROR_CHECK(esp_wifi_start());
|
||||||
ESP_ERROR_CHECK(esp_now_init());
|
ESP_ERROR_CHECK(esp_now_init());
|
||||||
|
|
||||||
@ -65,7 +181,7 @@ void app_main(void) {
|
|||||||
ESP_ERROR_CHECK(esp_now_register_recv_cb(client_receive_callback));
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(client_receive_callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
init_com();
|
init_com(&clientList);
|
||||||
|
|
||||||
// Tasks starten basierend auf Master/Client
|
// Tasks starten basierend auf Master/Client
|
||||||
if (isMaster) {
|
if (isMaster) {
|
||||||
@ -75,10 +191,29 @@ void app_main(void) {
|
|||||||
// xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL);
|
// xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL);
|
||||||
xTaskCreate(master_broadcast_ping, "MasterBroadcastPing", 4096, NULL, 1,
|
xTaskCreate(master_broadcast_ping, "MasterBroadcastPing", 4096, NULL, 1,
|
||||||
NULL);
|
NULL);
|
||||||
xTaskCreate(client_monitor_task, "MonitorClientTask", 4096, NULL, 1, NULL);
|
// xTaskCreate(client_monitor_task, "MonitorClientTask", 4096, NULL, 1,
|
||||||
init_uart();
|
// NULL);
|
||||||
//xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1, NULL);
|
|
||||||
xTaskCreate(SendClientInfoTask, "SendCientInfo", 4096, NULL, 1, NULL);
|
QueueHandle_t parsed_message_queue =
|
||||||
|
xQueueCreate(10, sizeof(ParsedMessage_t));
|
||||||
|
init_uart(parsed_message_queue);
|
||||||
|
InitMessageBroker();
|
||||||
|
|
||||||
|
// Initialisiere die Parameterstruktur
|
||||||
|
broker_task_params.message_queue = parsed_message_queue;
|
||||||
|
broker_task_params.send_buffer = send_message_buffer;
|
||||||
|
broker_task_params.send_buffer_size = sizeof(send_message_buffer);
|
||||||
|
|
||||||
|
xTaskCreate(MessageBrokerTask, "message_handler_task", 4096,
|
||||||
|
(void *)&broker_task_params, 5, NULL);
|
||||||
|
|
||||||
|
RegisterCallback(0x01, echoCallback);
|
||||||
|
RegisterCallback(0x02, versionCallback);
|
||||||
|
RegisterCallback(0x03, clientInfoCallback);
|
||||||
|
|
||||||
|
// xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1,
|
||||||
|
// NULL); xTaskCreate(SendClientInfoTask, "SendCientInfo", 4096, NULL, 1,
|
||||||
|
// NULL);
|
||||||
} else {
|
} else {
|
||||||
ESP_LOGI(TAG, "Started in Slavemode");
|
ESP_LOGI(TAG, "Started in Slavemode");
|
||||||
xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1,
|
||||||
|
|||||||
80
main/message_builder.c
Normal file
80
main/message_builder.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include "message_builder.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "message_parser.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
bool needs_stuffing_byte(uint8_t byte) {
|
||||||
|
return (byte == StartByte || byte == EscapeByte || byte == EndByte);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool add_byte_with_length_check(uint8_t byte, size_t write_index, uint8_t *data,
|
||||||
|
size_t max_length) {
|
||||||
|
if (write_index >= max_length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
data[write_index] = byte;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int build_message(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
|
uint8_t *msg_buffer, size_t msg_buffer_size) {
|
||||||
|
|
||||||
|
ESP_LOGE("BM", "payload_len %d, msg_buffer_size %d", payload_len + 4,
|
||||||
|
msg_buffer_size);
|
||||||
|
if (payload_len + 4 > msg_buffer_size) {
|
||||||
|
return PayloadBiggerThenBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t checksum = 0;
|
||||||
|
size_t write_index = 0;
|
||||||
|
msg_buffer[write_index++] = StartByte;
|
||||||
|
|
||||||
|
if (needs_stuffing_byte(msgid)) {
|
||||||
|
if (!add_byte_with_length_check(EscapeByte, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
}
|
||||||
|
if (!add_byte_with_length_check(msgid, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
checksum ^= msgid;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < payload_len; i++) {
|
||||||
|
if (needs_stuffing_byte(payload[i])) {
|
||||||
|
if (!add_byte_with_length_check(EscapeByte, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
}
|
||||||
|
if (!add_byte_with_length_check(payload[i], write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
|
||||||
|
write_index++;
|
||||||
|
checksum ^= payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needs_stuffing_byte(checksum)) {
|
||||||
|
if (!add_byte_with_length_check(EscapeByte, write_index, msg_buffer,
|
||||||
|
msg_buffer_size)) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
write_index++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (write_index + 2 > msg_buffer_size) {
|
||||||
|
return BufferOverFlow;
|
||||||
|
}
|
||||||
|
msg_buffer[write_index++] = checksum;
|
||||||
|
msg_buffer[write_index++] = EndByte;
|
||||||
|
|
||||||
|
ESP_LOGE("BM", "MESSAGE FERTIG GEBAUT");
|
||||||
|
return write_index;
|
||||||
|
}
|
||||||
18
main/message_builder.h
Normal file
18
main/message_builder.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef _MESSAGE_BUILDER_HEADER
|
||||||
|
#define _MESSAGE_BUILDER_HEADER
|
||||||
|
|
||||||
|
#include "message_parser.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
enum BuildMessageErrors {
|
||||||
|
NoBuildError = 0,
|
||||||
|
PayloadBiggerThenBuffer = -1,
|
||||||
|
BufferOverFlow = -2,
|
||||||
|
};
|
||||||
|
|
||||||
|
// returns the length of msg_buffer
|
||||||
|
int build_message(uint8_t msgid, const uint8_t *payload, size_t payload_len,
|
||||||
|
uint8_t *msg_buffer, size_t msg_buffer_length);
|
||||||
|
|
||||||
|
#endif
|
||||||
74
main/message_handler.c
Normal file
74
main/message_handler.c
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
#include "message_handler.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "freertos/idf_additions.h"
|
||||||
|
#include "uart_handler.h"
|
||||||
|
|
||||||
|
static struct MessageBroker mr;
|
||||||
|
static char *TAG = "ALOX - Message Handler";
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
QueueHandle_t message_queue;
|
||||||
|
uint8_t *send_buffer;
|
||||||
|
size_t send_buffer_size;
|
||||||
|
} MessageBrokerTaskParams_t;
|
||||||
|
|
||||||
|
void InitMessageBroker() {
|
||||||
|
mr.num_direct_callbacks = 0;
|
||||||
|
mr.num_task_callbacks = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterCallback(uint8_t msgid, RegisterFunctionCallback callback) {
|
||||||
|
mr.FunctionList[mr.num_direct_callbacks].MSGID = msgid;
|
||||||
|
mr.FunctionList[mr.num_direct_callbacks].callback = callback;
|
||||||
|
mr.num_direct_callbacks++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RegisterTask(uint8_t msgid, RegisterTaskCallback callback) {
|
||||||
|
mr.TaskList[mr.num_task_callbacks].MSGID = msgid;
|
||||||
|
mr.TaskList[mr.num_task_callbacks].task = callback;
|
||||||
|
mr.num_task_callbacks++;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MessageBrokerTask(void *param) {
|
||||||
|
ParsedMessage_t received_msg;
|
||||||
|
MessageBrokerTaskParams_t *task_params = (MessageBrokerTaskParams_t *)param;
|
||||||
|
|
||||||
|
// Extrahiere die einzelnen Parameter
|
||||||
|
QueueHandle_t msg_queue = task_params->message_queue;
|
||||||
|
uint8_t *send_message_buffer = task_params->send_buffer;
|
||||||
|
size_t send_message_buffer_size = task_params->send_buffer_size;
|
||||||
|
|
||||||
|
if (msg_queue == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Message queue not initialized. Terminating task.");
|
||||||
|
vTaskDelete(NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Message broker task started.");
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
if (xQueueReceive(msg_queue, &received_msg, portMAX_DELAY)) {
|
||||||
|
ESP_LOGI(TAG, "Received message from queue: MSGID=0x%02X, Length=%u",
|
||||||
|
received_msg.msgid, received_msg.payload_len);
|
||||||
|
|
||||||
|
for (int i = 0; i < mr.num_direct_callbacks; i++) {
|
||||||
|
if (mr.FunctionList[i].MSGID == received_msg.msgid) {
|
||||||
|
mr.FunctionList[i].callback(
|
||||||
|
received_msg.msgid, received_msg.data, received_msg.payload_len,
|
||||||
|
send_message_buffer, send_message_buffer_size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i = 0; i < mr.num_direct_callbacks; i++) {
|
||||||
|
if (mr.FunctionList[i].MSGID == received_msg.msgid) {
|
||||||
|
// TODO: Not yet implemented
|
||||||
|
// Only send data to task, task should be created beforhead and wait
|
||||||
|
// for new data in the queue.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void SendMessage(const uint8_t *buffer, size_t length);
|
||||||
41
main/message_handler.h
Normal file
41
main/message_handler.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#ifndef _MESSAGE_HANDLER_HEADER
|
||||||
|
#define _MESSAGE_HANDLER_HEADER
|
||||||
|
|
||||||
|
#include "freertos/idf_additions.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef void (*RegisterFunctionCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len,
|
||||||
|
uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size);
|
||||||
|
typedef void (*RegisterTaskCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, uint8_t *send_buffer,
|
||||||
|
size_t send_buffer_size);
|
||||||
|
|
||||||
|
struct RegisterdFunction {
|
||||||
|
uint8_t MSGID;
|
||||||
|
RegisterFunctionCallback callback;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct RegisterdTask {
|
||||||
|
uint8_t MSGID;
|
||||||
|
RegisterTaskCallback task;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct MessageBroker {
|
||||||
|
struct RegisterdFunction FunctionList[64];
|
||||||
|
uint8_t num_direct_callbacks;
|
||||||
|
struct RegisterdTask TaskList[64];
|
||||||
|
uint8_t num_task_callbacks;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*SendMessageHookCallback)(const uint8_t *buffer, size_t length);
|
||||||
|
|
||||||
|
void InitMessageBroker();
|
||||||
|
void RegisterCallback(uint8_t msgid, RegisterFunctionCallback callback);
|
||||||
|
void RegisterTask(uint8_t msgid, RegisterTaskCallback callback);
|
||||||
|
void SendMessage(const uint8_t *buffer, size_t length);
|
||||||
|
void MessageBrokerTask(void *param);
|
||||||
|
|
||||||
|
#endif
|
||||||
112
main/message_parser.c
Normal file
112
main/message_parser.c
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
#include "message_parser.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
MessageReceivedCallback on_message_received = NULL;
|
||||||
|
MessageFailCallback on_message_fail = NULL;
|
||||||
|
|
||||||
|
struct MessageReceive InitMessageReceive() {
|
||||||
|
struct MessageReceive mr = {
|
||||||
|
.state = WaitingForStartByte, // Startzustand des Parsers
|
||||||
|
.error = NoError, // Kein Fehler zu Beginn
|
||||||
|
.messageid = 0, // MSGID auf Standardwert setzen
|
||||||
|
// .message Array muss nicht explizit initialisiert werden, da es bei
|
||||||
|
// jedem Start geleert wird
|
||||||
|
.index = 0, // Index für das Nachrichten-Array initialisieren
|
||||||
|
.checksum = 0 // Checksumme initialisieren
|
||||||
|
};
|
||||||
|
return mr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Registrierungsfunktionen für die Callbacks
|
||||||
|
void register_message_callback(MessageReceivedCallback callback) {
|
||||||
|
on_message_received = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void register_message_fail_callback(MessageFailCallback callback) {
|
||||||
|
on_message_fail = callback;
|
||||||
|
}
|
||||||
|
|
||||||
|
void parse_byte(struct MessageReceive *mr, uint8_t pbyte) {
|
||||||
|
switch (mr->state) {
|
||||||
|
case WaitingForStartByte:
|
||||||
|
if (pbyte == StartByte) {
|
||||||
|
mr->index = 0;
|
||||||
|
mr->checksum = 0;
|
||||||
|
mr->state = GetMessageType;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case EscapedMessageType:
|
||||||
|
mr->messageid = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
mr->state = InPayload;
|
||||||
|
break;
|
||||||
|
case GetMessageType:
|
||||||
|
if (pbyte == EscapeByte) {
|
||||||
|
mr->state = EscapedMessageType;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pbyte == StartByte || pbyte == EndByte) {
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = UnexpectedCommandByte;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mr->messageid = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
mr->state = InPayload;
|
||||||
|
break;
|
||||||
|
case EscapePayloadByte:
|
||||||
|
mr->message[mr->index++] = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
mr->state = InPayload;
|
||||||
|
break;
|
||||||
|
case InPayload:
|
||||||
|
if (pbyte == EscapeByte) {
|
||||||
|
mr->state = EscapePayloadByte;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pbyte == StartByte) {
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = UnexpectedCommandByte;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (pbyte == EndByte) {
|
||||||
|
if (mr->checksum != 0x00) {
|
||||||
|
// Checksum failure
|
||||||
|
// The Checksum gets treated like a normal byte until the end byte
|
||||||
|
// accours. Therefore the last byte xor'ed to the checksum ist the
|
||||||
|
// checksum so the checksum must be Zero.
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = WrongCheckSum;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_received(mr->messageid, mr->message,
|
||||||
|
mr->index - 1); // remove checksum byte by just
|
||||||
|
// setting the length of the message
|
||||||
|
}
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
}
|
||||||
|
if (mr->index < MAX_TOTAL_CONTENT_LENGTH) {
|
||||||
|
mr->message[mr->index++] = pbyte;
|
||||||
|
mr->checksum ^= pbyte;
|
||||||
|
} else {
|
||||||
|
mr->state = WaitingForStartByte;
|
||||||
|
mr->error = MessageToLong;
|
||||||
|
if (on_message_received) {
|
||||||
|
on_message_fail(mr->messageid, mr->message, mr->index, mr->error);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
52
main/message_parser.h
Normal file
52
main/message_parser.h
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
#ifndef _MESSAGE_PARSER_HEADER
|
||||||
|
#define _MESSAGE_PARSER_HEADER
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define MAX_MESSAGE_PAYLOAD_LENGTH 128
|
||||||
|
#define MAX_TOTAL_CONTENT_LENGTH (MAX_MESSAGE_PAYLOAD_LENGTH + 1)
|
||||||
|
|
||||||
|
enum ParserState {
|
||||||
|
WaitingForStartByte,
|
||||||
|
GetMessageType,
|
||||||
|
EscapedMessageType,
|
||||||
|
EscapePayloadByte,
|
||||||
|
InPayload,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum ParserError {
|
||||||
|
NoError,
|
||||||
|
WrongCheckSum,
|
||||||
|
MessageToLong,
|
||||||
|
UnexpectedCommandByte,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
StartByte = 0xAA,
|
||||||
|
EscapeByte = 0xBB,
|
||||||
|
EndByte = 0xCC,
|
||||||
|
} MessageBytes;
|
||||||
|
|
||||||
|
struct MessageReceive {
|
||||||
|
enum ParserState state;
|
||||||
|
enum ParserError error;
|
||||||
|
uint8_t messageid;
|
||||||
|
uint8_t message[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
uint8_t index;
|
||||||
|
uint8_t checksum;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef void (*MessageReceivedCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len);
|
||||||
|
typedef void (*MessageFailCallback)(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, enum ParserError error);
|
||||||
|
|
||||||
|
struct MessageReceive InitMessageReceive();
|
||||||
|
|
||||||
|
void register_message_callback(MessageReceivedCallback callback);
|
||||||
|
void register_message_fail_callback(MessageFailCallback callback);
|
||||||
|
|
||||||
|
void parse_byte(struct MessageReceive *mr, uint8_t pbyte);
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -1,19 +1,23 @@
|
|||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
#include "driver/uart.h"
|
#include "driver/uart.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "esp_log_buffer.h"
|
||||||
#include "freertos/idf_additions.h"
|
#include "freertos/idf_additions.h"
|
||||||
#include "hal/uart_types.h"
|
#include "hal/uart_types.h"
|
||||||
|
#include "message_handler.h"
|
||||||
|
#include "message_parser.h"
|
||||||
#include "nvs_flash.h"
|
#include "nvs_flash.h"
|
||||||
#include "portmacro.h"
|
#include "portmacro.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "message_parser.h"
|
||||||
#include "uart_handler.h"
|
#include "uart_handler.h"
|
||||||
#include "uart_prot.h"
|
|
||||||
|
|
||||||
static const char *TAG = "ALOX - UART";
|
static const char *TAG = "ALOX - UART";
|
||||||
|
static QueueHandle_t parsed_message_queue;
|
||||||
|
|
||||||
void init_uart() {
|
void init_uart(QueueHandle_t msg_queue_handle) {
|
||||||
uart_config_t uart_config = {.baud_rate = 115200,
|
uart_config_t uart_config = {.baud_rate = 115200,
|
||||||
.data_bits = UART_DATA_8_BITS,
|
.data_bits = UART_DATA_8_BITS,
|
||||||
.parity = UART_PARITY_DISABLE,
|
.parity = UART_PARITY_DISABLE,
|
||||||
@ -25,12 +29,16 @@ void init_uart() {
|
|||||||
uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
|
uart_set_pin(MASTER_UART, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
|
||||||
UART_PIN_NO_CHANGE);
|
UART_PIN_NO_CHANGE);
|
||||||
|
|
||||||
message_handler_init();
|
parsed_message_queue = msg_queue_handle;
|
||||||
|
register_message_callback(HandleMessageReceivedCallback);
|
||||||
|
register_message_fail_callback(HandleMessageFailCallback);
|
||||||
|
|
||||||
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
|
xTaskCreate(uart_read_task, "Read Uart", 4096, NULL, 1, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_read_task(void *param) {
|
void uart_read_task(void *param) {
|
||||||
QueueHandle_t inputQueue = message_handler_get_input_queue();
|
// Send all Input from Uart to the Message Handler for Parsing
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
|
uint8_t *data = (uint8_t *)malloc(BUF_SIZE);
|
||||||
int len = 0;
|
int len = 0;
|
||||||
while (1) {
|
while (1) {
|
||||||
@ -39,32 +47,44 @@ void uart_read_task(void *param) {
|
|||||||
uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS));
|
uart_read_bytes(MASTER_UART, data, BUF_SIZE, (20 / portTICK_PERIOD_MS));
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
for (int i = 0; i < len; ++i) {
|
for (int i = 0; i < len; ++i) {
|
||||||
BaseType_t res = xQueueSend(inputQueue, &data[i], 0);
|
parse_byte(&mr, data[i]);
|
||||||
if (res == errQUEUE_FULL) {
|
|
||||||
ESP_LOGW(TAG, "inputQueue full");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void uart_status_task(void *param) {
|
// TODO: Remove this? or handle message sending in any other way reduce
|
||||||
while (1) {
|
// abstraction hell
|
||||||
uart_write_bytes(MASTER_UART, "c1,status,0\n\r", sizeof("c1,status,0\n\r"));
|
void send_message_hook(const uint8_t *buffer, size_t length) {
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
uart_write_bytes(MASTER_UART, buffer, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
void HandleMessageReceivedCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
ESP_LOGI(TAG, "GOT UART MESSAGE MSGID: %02X, Len: %u bytes \nMSG: ", msgid,
|
||||||
|
payload_len, payload);
|
||||||
|
ESP_LOG_BUFFER_HEX(TAG, payload, payload_len);
|
||||||
|
|
||||||
|
ParsedMessage_t msg_to_send;
|
||||||
|
msg_to_send.msgid = msgid;
|
||||||
|
msg_to_send.payload_len = payload_len;
|
||||||
|
memcpy(msg_to_send.data, payload, payload_len);
|
||||||
|
|
||||||
|
if (xQueueSend(parsed_message_queue, &msg_to_send, portMAX_DELAY) != pdPASS) {
|
||||||
|
// Fehlerbehandlung: Queue voll oder Senden fehlgeschlagen
|
||||||
|
ESP_LOGE(TAG, "Failed to send parsed message to queue.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void send_client_info(int clientid, bool isAvailable, TickType_t lastPing) {
|
void HandleMessageFailCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
char buf[128];
|
size_t payload_len, enum ParserError error) {
|
||||||
snprintf(buf, sizeof(buf), "c%d,status,2,%d,%u\r\n", clientid,
|
ESP_LOGE(
|
||||||
isAvailable ? 1 : 0, (unsigned int)lastPing);
|
TAG,
|
||||||
uart_write_bytes(MASTER_UART, buf, strlen(buf));
|
"UART MESSAGE Parsing Failed MSGID: %02X, Len: %u, ERROR: %X, \nMSG: ",
|
||||||
}
|
msgid, payload_len, error);
|
||||||
|
ESP_LOG_BUFFER_HEX(TAG, payload, payload_len);
|
||||||
|
|
||||||
void esp_send_message_hook(ESPTOPCBaseMessage *msg) {
|
return;
|
||||||
// serialize + send via UART
|
|
||||||
uint8_t buffer[128];
|
|
||||||
uart_write_bytes(UART_NUM_1, (const char *)buffer,
|
|
||||||
sizeof(ESPTOPCBaseMessage));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,16 +1,30 @@
|
|||||||
#ifndef UART_HANDLER_H
|
#ifndef UART_HANDLER_H
|
||||||
#define UART_HANDLER_H
|
#define UART_HANDLER_H
|
||||||
|
|
||||||
|
#include "freertos/idf_additions.h"
|
||||||
|
#include "message_parser.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#define MASTER_UART UART_NUM_1
|
#define MASTER_UART UART_NUM_1
|
||||||
#define TXD_PIN (GPIO_NUM_1)
|
#define TXD_PIN (GPIO_NUM_1)
|
||||||
#define RXD_PIN (GPIO_NUM_2)
|
#define RXD_PIN (GPIO_NUM_2)
|
||||||
|
|
||||||
#define BUF_SIZE (1024)
|
#define BUF_SIZE (256)
|
||||||
|
|
||||||
void init_uart();
|
typedef struct {
|
||||||
|
uint8_t msgid;
|
||||||
|
size_t payload_len;
|
||||||
|
uint8_t data[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
} ParsedMessage_t;
|
||||||
|
|
||||||
|
void init_uart(QueueHandle_t msg_queue_handle);
|
||||||
void uart_read_task(void *param);
|
void uart_read_task(void *param);
|
||||||
void uart_status_task(void *param);
|
void uart_send_task(void *param);
|
||||||
|
|
||||||
void send_client_info(int clientid, bool isAvailable, TickType_t lastPing);
|
void HandleMessageReceivedCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len);
|
||||||
|
void HandleMessageFailCallback(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, enum ParserError error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,97 +0,0 @@
|
|||||||
#include "uart_prot.h"
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define MSG_QUEUE_LEN 64
|
|
||||||
static QueueHandle_t input_queue;
|
|
||||||
static QueueHandle_t output_queue;
|
|
||||||
|
|
||||||
QueueHandle_t message_handler_get_input_queue(void) {
|
|
||||||
return input_queue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QueueHandle_t message_handler_get_output_queue(void) {
|
|
||||||
return output_queue;
|
|
||||||
}
|
|
||||||
|
|
||||||
void message_handler_init(void) {
|
|
||||||
input_queue = xQueueCreate(MSG_QUEUE_LEN, sizeof(uint8_t));
|
|
||||||
output_queue = xQueueCreate(MSG_QUEUE_LEN, sizeof(uint8_t));
|
|
||||||
}
|
|
||||||
|
|
||||||
void message_handler_task(void *param) {
|
|
||||||
uint8_t byte;
|
|
||||||
while (1) {
|
|
||||||
if (xQueueReceive(input_queue, &byte, portMAX_DELAY)) {
|
|
||||||
// handle byte, check message recieve with start and stop byte length and crc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Message Dispatcher
|
|
||||||
void dispatch_message(uint8_t msg_id, void *payload) {
|
|
||||||
switch (msg_id) {
|
|
||||||
case RequestPing:
|
|
||||||
if (on_request_ping)
|
|
||||||
on_request_ping((RequestPingPayload *)payload);
|
|
||||||
break;
|
|
||||||
case RequestStatus:
|
|
||||||
if (on_request_status)
|
|
||||||
on_request_status((RequestStatusPayload *)payload);
|
|
||||||
break;
|
|
||||||
case PrepareFirmwareUpdate:
|
|
||||||
if (on_prepare_firmware_update)
|
|
||||||
on_prepare_firmware_update((PrepareFirmwareUpdatePayload *)payload);
|
|
||||||
break;
|
|
||||||
case FirmwareUpdateLine:
|
|
||||||
if (on_firmware_update_line)
|
|
||||||
on_firmware_update_line((FirmwareUpdateLinePayload *)payload);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// Unknown message
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generic Send Function
|
|
||||||
void send_message(ESP_TO_PC_MESSAGE_IDS msgid, PayloadUnion *payload) {
|
|
||||||
ESPTOPCBaseMessage mes;
|
|
||||||
mes.Version = 1;
|
|
||||||
mes.MessageID = msgid;
|
|
||||||
mes.Payload = *payload;
|
|
||||||
|
|
||||||
esp_send_message_hook(&mes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sepzific Send Functions
|
|
||||||
void send_clients(uint8_t clientCount, uint32_t clientAvaiableBitMask) {
|
|
||||||
ClientsPayload payload;
|
|
||||||
|
|
||||||
// Payload-Daten zuweisen
|
|
||||||
payload.clientCount = clientCount;
|
|
||||||
payload.clientAvaiableBitMask = clientAvaiableBitMask;
|
|
||||||
|
|
||||||
// Nachricht senden
|
|
||||||
send_message(Clients, (PayloadUnion *)&payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_status(uint8_t clientId, uint8_t *mac) {
|
|
||||||
StatusPayload payload;
|
|
||||||
|
|
||||||
// Payload-Daten zuweisen
|
|
||||||
payload.clientId = clientId;
|
|
||||||
memcpy(payload.mac, mac, 6);
|
|
||||||
|
|
||||||
// Nachricht senden
|
|
||||||
send_message(Status, (PayloadUnion *)&payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
void send_pong(uint8_t clientId, uint32_t ping) {
|
|
||||||
PongPayload payload;
|
|
||||||
|
|
||||||
// Payload-Daten zuweisen
|
|
||||||
payload.clientId = clientId;
|
|
||||||
payload.ping = ping;
|
|
||||||
|
|
||||||
// Nachricht senden
|
|
||||||
send_message(Pong, (PayloadUnion *)&payload);
|
|
||||||
}
|
|
||||||
100
main/uart_prot.h
100
main/uart_prot.h
@ -1,100 +0,0 @@
|
|||||||
#ifndef _PROTO_HEADER
|
|
||||||
#define _PROTO_HEADER
|
|
||||||
|
|
||||||
#include "freertos/idf_additions.h"
|
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
void message_handler_init(void);
|
|
||||||
QueueHandle_t message_handler_get_input_queue(void);
|
|
||||||
QueueHandle_t message_handler_get_output_queue(void);
|
|
||||||
|
|
||||||
// MessageIDs
|
|
||||||
typedef enum {
|
|
||||||
RequestPing = 0xE1,
|
|
||||||
RequestStatus = 0xE2,
|
|
||||||
PrepareFirmwareUpdate = 0xF1,
|
|
||||||
FirmwareUpdateLine = 0xF2,
|
|
||||||
} PC_TO_ESP_MESSAGE_IDS;
|
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
Clients = 0xE1,
|
|
||||||
Status = 0xE2,
|
|
||||||
Pong = 0xD1,
|
|
||||||
} ESP_TO_PC_MESSAGE_IDS;
|
|
||||||
|
|
||||||
// Payloads for single Messages
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
} RequestPingPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
} RequestStatusPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
// empty payload
|
|
||||||
} PrepareFirmwareUpdatePayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t data[240];
|
|
||||||
} FirmwareUpdateLinePayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientCount;
|
|
||||||
uint32_t clientAvaiableBitMask;
|
|
||||||
} ClientsPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
uint8_t mac[6];
|
|
||||||
} StatusPayload;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t clientId;
|
|
||||||
uint32_t ping;
|
|
||||||
} PongPayload;
|
|
||||||
|
|
||||||
// Union for all the Payloads
|
|
||||||
typedef union {
|
|
||||||
RequestPingPayload request_ping;
|
|
||||||
RequestStatusPayload request_status;
|
|
||||||
PrepareFirmwareUpdatePayload prepare_firmware_update;
|
|
||||||
FirmwareUpdateLinePayload firmware_update_line;
|
|
||||||
ClientsPayload clients;
|
|
||||||
StatusPayload status;
|
|
||||||
PongPayload pong;
|
|
||||||
} PayloadUnion;
|
|
||||||
|
|
||||||
// Base Message that can hold all Payloads
|
|
||||||
typedef struct {
|
|
||||||
uint8_t Version;
|
|
||||||
PC_TO_ESP_MESSAGE_IDS MessageID;
|
|
||||||
uint8_t Length;
|
|
||||||
PayloadUnion Payload;
|
|
||||||
} PCTOESPBaseMessage;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
uint8_t Version;
|
|
||||||
ESP_TO_PC_MESSAGE_IDS MessageID;
|
|
||||||
uint8_t Length;
|
|
||||||
PayloadUnion Payload;
|
|
||||||
} ESPTOPCBaseMessage;
|
|
||||||
|
|
||||||
// deklarierte Hook-Signatur
|
|
||||||
void esp_send_message_hook(ESPTOPCBaseMessage *msg);
|
|
||||||
|
|
||||||
// Generic Send Function Prototype
|
|
||||||
void send_message(ESP_TO_PC_MESSAGE_IDS msgid, PayloadUnion *payload);
|
|
||||||
|
|
||||||
// Spezific Send Functions Prototype
|
|
||||||
void send_clients(uint8_t clientCount, uint32_t clientAvaiableBitMask);
|
|
||||||
void send_status(uint8_t clientId, uint8_t *mac);
|
|
||||||
void send_pong(uint8_t clientId, uint32_t ping);
|
|
||||||
|
|
||||||
// Prototypes for Message Recieve Handler to be set in user code
|
|
||||||
void (*on_request_ping)(RequestPingPayload *);
|
|
||||||
void (*on_request_status)(RequestStatusPayload *);
|
|
||||||
void (*on_prepare_firmware_update)(PrepareFirmwareUpdatePayload *);
|
|
||||||
void (*on_firmware_update_line)(FirmwareUpdateLinePayload *);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@ -1283,9 +1283,9 @@ CONFIG_HAL_DEFAULT_ASSERTION_LEVEL=2
|
|||||||
#
|
#
|
||||||
# Heap memory debugging
|
# Heap memory debugging
|
||||||
#
|
#
|
||||||
CONFIG_HEAP_POISONING_DISABLED=y
|
# CONFIG_HEAP_POISONING_DISABLED is not set
|
||||||
# CONFIG_HEAP_POISONING_LIGHT is not set
|
# CONFIG_HEAP_POISONING_LIGHT is not set
|
||||||
# CONFIG_HEAP_POISONING_COMPREHENSIVE is not set
|
CONFIG_HEAP_POISONING_COMPREHENSIVE=y
|
||||||
CONFIG_HEAP_TRACING_OFF=y
|
CONFIG_HEAP_TRACING_OFF=y
|
||||||
# CONFIG_HEAP_TRACING_STANDALONE is not set
|
# CONFIG_HEAP_TRACING_STANDALONE is not set
|
||||||
# CONFIG_HEAP_TRACING_TOHOST is not set
|
# CONFIG_HEAP_TRACING_TOHOST is not set
|
||||||
|
|||||||
1908
sdkconfig.old
Normal file
1908
sdkconfig.old
Normal file
File diff suppressed because it is too large
Load Diff
3
tests/.gitignore
vendored
Normal file
3
tests/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Unity/*
|
||||||
|
test_runner
|
||||||
|
test_builder
|
||||||
21
tests/Makefile
Normal file
21
tests/Makefile
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
CC=gcc
|
||||||
|
CFLAGS=-Wall -Wextra -I../main
|
||||||
|
SRC = Unity/src/unity.c test_parser.c \
|
||||||
|
../main/message_parser.c
|
||||||
|
TARGET = test_runner
|
||||||
|
|
||||||
|
.PHONY: test_builder
|
||||||
|
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
$(TARGET): $(SRC)
|
||||||
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
./$(TARGET)
|
||||||
|
|
||||||
|
test_builder: Unity/src/unity.c test_message_builder.c ../main/message_parser.c ../main/message_builder.c
|
||||||
|
$(CC) $(CFLAGS) -o $@ $^
|
||||||
|
@echo "--- Running Builder Tests ---"
|
||||||
|
./$(BUILDER_TEST_TARGET)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(TARGET)
|
||||||
9
tests/main_tests.c
Normal file
9
tests/main_tests.c
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include "Unity/src/unity.h"
|
||||||
|
|
||||||
|
void setUp(void) {} // optional
|
||||||
|
void tearDown(void) {} // optional
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
433
tests/test_message_builder.c
Normal file
433
tests/test_message_builder.c
Normal file
@ -0,0 +1,433 @@
|
|||||||
|
#include "Unity/src/unity.h"
|
||||||
|
#include "message_builder.h" // Stellt sicher, dass deine Header-Datei message_parser.h die Definitionen für build_message, StartByte, EscapeByte, EndByte, MAX_MESSAGE_PAYLOAD_LENGTH und Fehlercodes enthält
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h> // Für memcpy, memset
|
||||||
|
|
||||||
|
// --- Globale Konstanten (Annahmen aus vorheriger Konversation) ---
|
||||||
|
// Wenn diese in message_parser.h nicht definiert sind, musst du sie hier
|
||||||
|
// definieren: #define StartByte 0xAA #define EscapeByte 0xBB #define EndByte
|
||||||
|
// 0xCC
|
||||||
|
|
||||||
|
// #define MAX_MESSAGE_PAYLOAD_LENGTH 250 // Beispiel: Max. Payload-Länge
|
||||||
|
|
||||||
|
// Fehlercodes für den Builder (sollten mit deiner build_message Implementierung
|
||||||
|
// übereinstimmen) #define BUILD_ERROR_BUFFER_TOO_SMALL_INITIAL_CHECK -1 #define
|
||||||
|
// BUILD_ERROR_BUFFER_OVERFLOW -2 #define PayloadBiggerThenBuffer -3
|
||||||
|
|
||||||
|
// --- UNITY SETUP/TEARDOWN ---
|
||||||
|
void setUp(void) {
|
||||||
|
// Nichts Besonderes für den Builder-Test zu resetten
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {} // optional
|
||||||
|
|
||||||
|
static bool needs_stuffing_byte(uint8_t byte) {
|
||||||
|
return (byte == StartByte || byte == EscapeByte || byte == EndByte);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Hilfsfunktion zur Berechnung des *zu sendenden* Checksummen-Bytes ---
|
||||||
|
// Dies ist der Wert, der im Frame an der Checksummen-Position steht,
|
||||||
|
// sodass die finale XOR-Summe der Nutzdaten (MSGID + Payload + dieses Byte)
|
||||||
|
// 0x00 ergibt.
|
||||||
|
uint8_t calculate_payload_checksum_byte(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
uint8_t cs = msgid;
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
cs ^= payload[i];
|
||||||
|
}
|
||||||
|
return cs; // Dies ist der Wert, der gesendet werden muss
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Hilfsfunktion zum Vergleichen von Hex-Arrays und Debug-Ausgabe ---
|
||||||
|
void TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(const uint8_t *expected,
|
||||||
|
const uint8_t *actual, size_t len) {
|
||||||
|
char expected_str[len * 3 + 1];
|
||||||
|
char actual_str[len * 3 + 1];
|
||||||
|
int offset_exp = 0;
|
||||||
|
int offset_act = 0;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < len; ++i) {
|
||||||
|
offset_exp +=
|
||||||
|
snprintf(expected_str + offset_exp, sizeof(expected_str) - offset_exp,
|
||||||
|
"%02X ", expected[i]);
|
||||||
|
offset_act += snprintf(actual_str + offset_act,
|
||||||
|
sizeof(actual_str) - offset_act, "%02X ", actual[i]);
|
||||||
|
}
|
||||||
|
// Stelle sicher, dass die Strings nullterminiert sind, falls der Puffer genau
|
||||||
|
// gefüllt wurde
|
||||||
|
if (offset_exp > 0)
|
||||||
|
expected_str[offset_exp - 1] = '\0';
|
||||||
|
else
|
||||||
|
expected_str[0] = '\0'; // Remove last space, null-terminate
|
||||||
|
if (offset_act > 0)
|
||||||
|
actual_str[offset_act - 1] = '\0';
|
||||||
|
else
|
||||||
|
actual_str[0] = '\0';
|
||||||
|
|
||||||
|
printf("\n"); // Neue Zeile für bessere Lesbarkeit
|
||||||
|
printf(" Expected: %s\n", expected_str);
|
||||||
|
printf(" Actual: %s\n", actual_str);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- TESTFÄLLE FÜR build_message ---
|
||||||
|
|
||||||
|
// Test 1: Gültige Nachricht ohne Escaping
|
||||||
|
void test_builder_1_basic_message_no_escaping(void) {
|
||||||
|
uint8_t msgid = 0x01;
|
||||||
|
uint8_t payload[] = {0x10, 0x20, 0x30, 0x40};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64]; // Ausreichend großer Puffer
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
// Erwarteter Checksummen-Wert
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
// Erwartete fertige Nachricht
|
||||||
|
uint8_t expected_message[] = {
|
||||||
|
StartByte, msgid, 0x10, 0x20, 0x30, 0x40, // Payload
|
||||||
|
expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: Gültige Nachricht mit leerem Payload
|
||||||
|
void test_builder_2_empty_payload(void) {
|
||||||
|
uint8_t msgid = 0x02;
|
||||||
|
uint8_t payload[] = {};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte, msgid, expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 3: MSGID muss escapet werden (StartByte als MSGID)
|
||||||
|
void test_builder_3_escaped_msgid(void) {
|
||||||
|
uint8_t msgid = StartByte; // MSGID = 0xAA
|
||||||
|
uint8_t payload[] = {0x11, 0x22};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte,
|
||||||
|
EscapeByte,
|
||||||
|
StartByte, // Escaped MSGID
|
||||||
|
0x11,
|
||||||
|
0x22, // Payload
|
||||||
|
expected_checksum,
|
||||||
|
EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 4: Payload-Byte muss escapet werden (EndByte im Payload)
|
||||||
|
void test_builder_4_escaped_payload_byte(void) {
|
||||||
|
uint8_t msgid = 0x04;
|
||||||
|
uint8_t payload[] = {0x01, EndByte, 0x03}; // EndByte = 0xCC im Payload
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte, msgid,
|
||||||
|
0x01, EscapeByte,
|
||||||
|
EndByte, // Escaped payload byte
|
||||||
|
0x03, expected_checksum,
|
||||||
|
EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 5: Checksummen-Byte muss escapet werden (EscapeByte als Checksumme)
|
||||||
|
void test_builder_5_escaped_checksum_byte(void) {
|
||||||
|
uint8_t msgid = 0x05;
|
||||||
|
// Payload so wählen, dass msgid ^ payload[0] = EscapeByte (0xBB)
|
||||||
|
uint8_t payload[] = {msgid ^ EscapeByte}; // Payload ist 0x05 ^ 0xBB = 0xBE
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(EscapeByte, expected_checksum); // Sanity check
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {StartByte,
|
||||||
|
msgid,
|
||||||
|
payload[0],
|
||||||
|
EscapeByte,
|
||||||
|
expected_checksum, // Escaped checksum byte
|
||||||
|
EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 6: Mehrere Escapings in einer Nachricht
|
||||||
|
void test_builder_6_multiple_escapings(void) {
|
||||||
|
uint8_t msgid = StartByte; // 0xAA
|
||||||
|
uint8_t payload[] = {EscapeByte, 0x01, EndByte, StartByte, 0x02};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t output_buffer[64];
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
// Angenommen, die berechnete Checksumme selbst ist KEIN Steuerzeichen,
|
||||||
|
// sonst müsste sie auch escapet werden.
|
||||||
|
// Wenn doch, würde expected_message noch ein EscapeByte vor der Checksumme
|
||||||
|
// bekommen.
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {
|
||||||
|
StartByte, EscapeByte, StartByte, // Escaped MSGID
|
||||||
|
EscapeByte, EscapeByte, // Escaped payload[0]
|
||||||
|
0x01, EscapeByte, EndByte, // Escaped payload[2]
|
||||||
|
EscapeByte, StartByte, // Escaped payload[3]
|
||||||
|
0x02, expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 7: Puffer zu klein - initiale Prüfung (Payload ist zu lang für den
|
||||||
|
// Puffer)
|
||||||
|
void test_builder_7_buffer_too_small_initial(void) {
|
||||||
|
uint8_t msgid = 0x07;
|
||||||
|
// Payload, das auch im besten Fall (ohne Stuffing) nicht in einen
|
||||||
|
// 10-Byte-Puffer passt. Minimale Länge wäre 1+1+5+1+1 = 9 Bytes (Start, ID,
|
||||||
|
// Payload, CRC, End)
|
||||||
|
uint8_t payload[8] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
|
||||||
|
size_t payload_len = sizeof(payload); // 8 Bytes
|
||||||
|
|
||||||
|
uint8_t output_buffer[10]; // Ein Puffer, der zu klein ist für 8 Payload-Bytes
|
||||||
|
// + Rahmung + CRC
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer); // 10 Bytes
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
// Die Berechnung für PayloadBiggerThenBuffer sollte hier greifen.
|
||||||
|
// payload_len * 2 + 5 (worst case) = 8*2+5 = 21. 21 ist > 10.
|
||||||
|
TEST_ASSERT_EQUAL_INT(PayloadBiggerThenBuffer, actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 8: Puffer zu klein - Überlauf beim Schreiben (knapper Puffer, z.B. bei
|
||||||
|
// Stuffing)
|
||||||
|
void test_builder_8_buffer_overflow_during_build(void) {
|
||||||
|
uint8_t msgid = 0x08;
|
||||||
|
// Payload so wählen, dass Stuffing stattfindet
|
||||||
|
uint8_t payload[] = {0x01, StartByte, 0x02}; // StartByte im Payload
|
||||||
|
size_t payload_len = sizeof(payload); // 3 Bytes
|
||||||
|
|
||||||
|
// Minimaler Puffer für diese Nachricht OHNE Stuffing:
|
||||||
|
// Start (1) + MSGID (1) + Payload (3) + CRC (1) + End (1) = 7 Bytes
|
||||||
|
// Mit Stuffing für payload[1] (StartByte) wird es:
|
||||||
|
// 1 (Start) + 1 (MSGID) + 1 (0x01) + 2 (Escape+StartByte) + 1 (0x02) + 1
|
||||||
|
// (CRC) + 1 (End) = 8 Bytes
|
||||||
|
uint8_t output_buffer[7]; // Puffer ist 1 Byte zu klein für die gestuffte
|
||||||
|
// Nachricht
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(BufferOverFlow, actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 9: Puffer genau groß genug (Randfall)
|
||||||
|
void test_builder_9_buffer_just_enough(void) {
|
||||||
|
uint8_t msgid = 0x09;
|
||||||
|
uint8_t payload[] = {0x01, 0x02, 0x03, 0x04}; // 4 Bytes
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t expected_message[] = {
|
||||||
|
StartByte, msgid, 0x01, 0x02, 0x03, 0x04, expected_checksum, EndByte};
|
||||||
|
size_t expected_len = sizeof(expected_message); // 1 + 1 + 4 + 1 + 1 = 8 Bytes
|
||||||
|
|
||||||
|
uint8_t output_buffer[expected_len]; // Puffer GENAU der erwarteten Größe
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 10: Maximale Payload-Länge ohne Stuffing
|
||||||
|
void test_builder_10_max_payload_no_stuffing(void) {
|
||||||
|
uint8_t msgid = 0x10;
|
||||||
|
uint8_t payload[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
for (size_t i = 0; i < MAX_MESSAGE_PAYLOAD_LENGTH; ++i) {
|
||||||
|
// Sicherstellen, dass keine Steuerzeichen dabei sind
|
||||||
|
payload[i] = (uint8_t)(i % 0xF0 + 0x01); // Vermeidet 0xAA, 0xBB, 0xCC
|
||||||
|
}
|
||||||
|
size_t payload_len = MAX_MESSAGE_PAYLOAD_LENGTH;
|
||||||
|
|
||||||
|
// Geschätzte maximale Puffergröße für worst-case (alle Bytes gestuffed)
|
||||||
|
// 1 (Start) + 1 (MSGID) + MAX_PAYLOAD_LEN*2 (Payload worst-case) + 1 (CRC) +
|
||||||
|
// 1 (End)
|
||||||
|
// + 2 (potential stuffing for MSGID/CRC) = 2*MAX_MESSAGE_PAYLOAD_LENGTH + 5
|
||||||
|
// Aber da wir hier KEIN Stuffing haben, ist es einfacher: 1 + 1 +
|
||||||
|
// MAX_PAYLOAD_LEN + 1 + 1
|
||||||
|
size_t max_buffer_needed = 1 + 1 + MAX_MESSAGE_PAYLOAD_LENGTH + 1 + 1;
|
||||||
|
|
||||||
|
uint8_t output_buffer[max_buffer_needed + 10]; // Etwas Puffer extra
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
// Erstelle erwartete Nachricht manuell oder dynamisch
|
||||||
|
uint8_t expected_message[max_buffer_needed];
|
||||||
|
size_t exp_idx = 0;
|
||||||
|
expected_message[exp_idx++] = StartByte;
|
||||||
|
expected_message[exp_idx++] = msgid;
|
||||||
|
memcpy(&expected_message[exp_idx], payload, payload_len);
|
||||||
|
exp_idx += payload_len;
|
||||||
|
expected_message[exp_idx++] = expected_checksum;
|
||||||
|
expected_message[exp_idx++] = EndByte;
|
||||||
|
size_t expected_len = exp_idx;
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 11: Maximale Payload-Länge mit Stuffing (Worst-Case Szenario)
|
||||||
|
void test_builder_11_max_payload_with_stuffing(void) {
|
||||||
|
uint8_t msgid = StartByte; // MSGID muss gestuffed werden
|
||||||
|
uint8_t payload[MAX_MESSAGE_PAYLOAD_LENGTH];
|
||||||
|
for (size_t i = 0; i < MAX_MESSAGE_PAYLOAD_LENGTH; ++i) {
|
||||||
|
// Alle Bytes sind Steuerzeichen, müssen gestuffed werden
|
||||||
|
payload[i] =
|
||||||
|
(i % 3 == 0) ? StartByte : ((i % 3 == 1) ? EscapeByte : EndByte);
|
||||||
|
}
|
||||||
|
size_t payload_len = MAX_MESSAGE_PAYLOAD_LENGTH;
|
||||||
|
|
||||||
|
// Worst-Case Puffergröße:
|
||||||
|
// 1 (Start) + 2 (Escaped MSGID) + MAX_PAYLOAD_LEN * 2 (Escaped Payload) + 2
|
||||||
|
// (Escaped CRC) + 1 (End)
|
||||||
|
size_t max_buffer_needed_worst_case =
|
||||||
|
1 + 2 + (MAX_MESSAGE_PAYLOAD_LENGTH * 2) + 2 + 1;
|
||||||
|
|
||||||
|
uint8_t
|
||||||
|
output_buffer[max_buffer_needed_worst_case + 10]; // Etwas Puffer extra
|
||||||
|
size_t output_buffer_size = sizeof(output_buffer);
|
||||||
|
|
||||||
|
// Build the expected message manually to account for all stuffing
|
||||||
|
uint8_t expected_message[max_buffer_needed_worst_case];
|
||||||
|
size_t exp_idx = 0;
|
||||||
|
|
||||||
|
// StartByte
|
||||||
|
expected_message[exp_idx++] = StartByte;
|
||||||
|
|
||||||
|
// MSGID (escaped)
|
||||||
|
expected_message[exp_idx++] = EscapeByte;
|
||||||
|
expected_message[exp_idx++] = msgid;
|
||||||
|
|
||||||
|
// Payload (all bytes escaped)
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
expected_message[exp_idx++] = EscapeByte;
|
||||||
|
expected_message[exp_idx++] = payload[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checksumme (kann auch escapet sein)
|
||||||
|
uint8_t expected_checksum =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
if (needs_stuffing_byte(expected_checksum)) {
|
||||||
|
expected_message[exp_idx++] = EscapeByte;
|
||||||
|
}
|
||||||
|
expected_message[exp_idx++] = expected_checksum;
|
||||||
|
|
||||||
|
// EndByte
|
||||||
|
expected_message[exp_idx++] = EndByte;
|
||||||
|
size_t expected_len = exp_idx;
|
||||||
|
|
||||||
|
int actual_len = build_message(msgid, payload, payload_len, output_buffer,
|
||||||
|
output_buffer_size);
|
||||||
|
|
||||||
|
TEST_ASSERT_EQUAL_INT(expected_len, actual_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY_HEX(expected_message, output_buffer,
|
||||||
|
actual_len);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- MAIN TEST RUNNER ---
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
|
||||||
|
// Run Builder Tests
|
||||||
|
RUN_TEST(test_builder_1_basic_message_no_escaping);
|
||||||
|
RUN_TEST(test_builder_2_empty_payload);
|
||||||
|
RUN_TEST(test_builder_3_escaped_msgid);
|
||||||
|
RUN_TEST(test_builder_4_escaped_payload_byte);
|
||||||
|
RUN_TEST(test_builder_5_escaped_checksum_byte);
|
||||||
|
RUN_TEST(test_builder_6_multiple_escapings);
|
||||||
|
RUN_TEST(test_builder_7_buffer_too_small_initial);
|
||||||
|
RUN_TEST(test_builder_8_buffer_overflow_during_build);
|
||||||
|
RUN_TEST(test_builder_9_buffer_just_enough);
|
||||||
|
RUN_TEST(test_builder_10_max_payload_no_stuffing);
|
||||||
|
RUN_TEST(test_builder_11_max_payload_with_stuffing);
|
||||||
|
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
452
tests/test_parser.c
Normal file
452
tests/test_parser.c
Normal file
@ -0,0 +1,452 @@
|
|||||||
|
#include "Unity/src/unity.h"
|
||||||
|
#include "message_parser.h" // Stellt sicher, dass deine Header-Datei message_parser.h korrekt ist
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h> // Für memcpy
|
||||||
|
|
||||||
|
// Globale Variablen für Callback-Überprüfung
|
||||||
|
static uint8_t received_msgid = 0xFF;
|
||||||
|
static uint8_t
|
||||||
|
received_payload[MAX_TOTAL_CONTENT_LENGTH]; // Muss groß genug sein
|
||||||
|
static size_t received_payload_len = 0;
|
||||||
|
static enum ParserError received_error = NoError;
|
||||||
|
static bool message_received_flag = false;
|
||||||
|
static bool message_fail_flag = false;
|
||||||
|
|
||||||
|
// Mock-Implementierungen für die Callbacks
|
||||||
|
void mock_on_message_received(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
received_msgid = msgid;
|
||||||
|
received_payload_len = payload_len;
|
||||||
|
// Sicherstellen, dass der Puffer nicht überläuft
|
||||||
|
memcpy(received_payload, payload,
|
||||||
|
(payload_len < MAX_TOTAL_CONTENT_LENGTH) ? payload_len
|
||||||
|
: MAX_TOTAL_CONTENT_LENGTH);
|
||||||
|
message_received_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mock_on_message_fail(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len, enum ParserError error) {
|
||||||
|
received_msgid = msgid; // Auch bei Fehlern kann die ID relevant sein
|
||||||
|
received_payload_len = payload_len;
|
||||||
|
// Auch hier, um sicherzustellen, dass wir den Zustand des Puffers beim Fehler
|
||||||
|
// sehen können
|
||||||
|
memcpy(received_payload, payload,
|
||||||
|
(payload_len < MAX_TOTAL_CONTENT_LENGTH) ? payload_len
|
||||||
|
: MAX_TOTAL_CONTENT_LENGTH);
|
||||||
|
received_error = error;
|
||||||
|
message_fail_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- UNITY SETUP/TEARDOWN ---
|
||||||
|
void setUp(void) {
|
||||||
|
// Reset der globalen Variablen vor jedem Test
|
||||||
|
received_msgid = 0xFF;
|
||||||
|
received_payload_len = 0;
|
||||||
|
received_error = NoError;
|
||||||
|
message_received_flag = false;
|
||||||
|
message_fail_flag = false;
|
||||||
|
memset(received_payload, 0, MAX_TOTAL_CONTENT_LENGTH);
|
||||||
|
|
||||||
|
// Registrierung der Mock-Callbacks (muss vor den Tests erfolgen)
|
||||||
|
register_message_callback(mock_on_message_received);
|
||||||
|
register_message_fail_callback(mock_on_message_fail);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tearDown(void) {} // optional
|
||||||
|
|
||||||
|
// --- Hilfsfunktion zur Checksummenberechnung (für Tests) ---
|
||||||
|
// Berechnet die Checksumme für MSGID + Payload + Checksummen-Byte
|
||||||
|
// Ergibt 0x00, wenn die gesamte Kette XORiert wird
|
||||||
|
uint8_t calculate_test_checksum_final(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len,
|
||||||
|
uint8_t actual_checksum_byte) {
|
||||||
|
uint8_t cs = msgid;
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
cs ^= payload[i];
|
||||||
|
}
|
||||||
|
cs ^=
|
||||||
|
actual_checksum_byte; // Das gesendete Checksummen-Byte wird auch XORiert
|
||||||
|
return cs;
|
||||||
|
}
|
||||||
|
// Hilfsfunktion zur Berechnung des *zu sendenden* Checksummen-Bytes
|
||||||
|
// Dies ist der Wert, der im Frame an der Checksummen-Position steht,
|
||||||
|
// sodass die finale XOR-Summe der Nutzdaten (MSGID + Payload + dieses Byte)
|
||||||
|
// 0x00 ergibt.
|
||||||
|
uint8_t calculate_payload_checksum_byte(uint8_t msgid, const uint8_t *payload,
|
||||||
|
size_t payload_len) {
|
||||||
|
uint8_t cs = msgid;
|
||||||
|
for (size_t i = 0; i < payload_len; ++i) {
|
||||||
|
cs ^= payload[i];
|
||||||
|
}
|
||||||
|
return cs; // Dies ist der Wert, der gesendet werden muss, damit die finale
|
||||||
|
// XOR-Summe 0x00 wird
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 1: Gültige Nachricht mit Payload
|
||||||
|
void test_1_valid_message_parses_correctly(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x01;
|
||||||
|
uint8_t payload[] = {0x01, 0x02, 0x03};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
// Berechne das Checksummen-Byte, das gesendet werden muss
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, // 0xAA
|
||||||
|
msgid, // 0x01
|
||||||
|
0x01,
|
||||||
|
0x02,
|
||||||
|
0x03, // Payload
|
||||||
|
checksum_byte_to_send, // Das Checksummen-Byte
|
||||||
|
EndByte // 0xCC
|
||||||
|
};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len); // Payload-Länge
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 2: Ungültige Checksumme – Fehler gemeldet und Zustand zurückgesetzt
|
||||||
|
void test_2_invalid_checksum_resets_state(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x02;
|
||||||
|
uint8_t payload[] = {0x10, 0x20};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
uint8_t wrong_checksum_byte = 0x01; // Absichtlich falsche Checksumme
|
||||||
|
|
||||||
|
uint8_t full_message[] = {StartByte,
|
||||||
|
msgid,
|
||||||
|
0x10,
|
||||||
|
0x20,
|
||||||
|
wrong_checksum_byte, // Falsches Checksummen-Byte
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
// received_payload_len sollte die Länge der Daten sein, die bis zum Fehler
|
||||||
|
// empfangen wurden, d.h., Payload-Länge + das falsche Checksummen-Byte.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len + 1, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 3: Gültige Nachricht ohne Payload (Länge 0)
|
||||||
|
void test_3_zero_length_message(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x03;
|
||||||
|
uint8_t payload[] = {}; // Leerer Payload
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, msgid,
|
||||||
|
checksum_byte_to_send, // Checksummen-Byte (hier gleich MSGID, da Payload
|
||||||
|
// leer)
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, received_payload_len); // Payload-Länge ist 0
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 4: Escapete MSGID (0xAA im MSGID-Feld)
|
||||||
|
void test_4_escaped_message_id(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0xAA; // MSGID ist ein Steuerzeichen, muss escapet werden
|
||||||
|
uint8_t payload[] = {0x42};
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {StartByte,
|
||||||
|
EscapeByte,
|
||||||
|
msgid, // Escapete MSGID (0xBB 0xAA)
|
||||||
|
0x42, // Payload
|
||||||
|
checksum_byte_to_send,
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 5: Escapetes Payload-Byte (z.B. ein StartByte im Payload)
|
||||||
|
void test_5_escaped_payload_byte(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x05;
|
||||||
|
uint8_t payload[] = {
|
||||||
|
0x11, StartByte}; // StartByte (0xAA) im Payload, muss escapet werden
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send =
|
||||||
|
calculate_payload_checksum_byte(msgid, payload, payload_len);
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte,
|
||||||
|
msgid,
|
||||||
|
0x11,
|
||||||
|
EscapeByte,
|
||||||
|
StartByte, // Escaptes StartByte (0xBB 0xAA) im Payload
|
||||||
|
checksum_byte_to_send,
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 6: Escapetes Checksummen-Byte (z.B. ein EndByte als Checksumme)
|
||||||
|
void test_6_escaped_checksum_byte(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x01;
|
||||||
|
uint8_t payload[] = {
|
||||||
|
0xCD}; // payload[0] ^ msgid = 0xCD ^ 0x01 = 0xCC (EndByte)
|
||||||
|
size_t payload_len = sizeof(payload);
|
||||||
|
|
||||||
|
uint8_t checksum_byte_to_send = calculate_payload_checksum_byte(
|
||||||
|
msgid, payload, payload_len); // Dies ist 0xCC
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte,
|
||||||
|
msgid,
|
||||||
|
payload[0], // Payload
|
||||||
|
EscapeByte,
|
||||||
|
checksum_byte_to_send, // Escaptes 0xCC als Checksummen-Byte (0xBB 0xCC)
|
||||||
|
EndByte};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(payload_len, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(payload, received_payload, payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 7: Nachricht zu lang (Pufferüberlauf)
|
||||||
|
void test_7_message_too_long(void) {
|
||||||
|
struct MessageReceive mr =
|
||||||
|
InitMessageReceive(); // mr.max_total_content_length wird auf
|
||||||
|
// MAX_TOTAL_CONTENT_LENGTH gesetzt
|
||||||
|
|
||||||
|
uint8_t msgid = 0x07;
|
||||||
|
// Dieser Payload ist absichtlich 1 Byte zu lang für
|
||||||
|
// MAX_MESSAGE_PAYLOAD_LENGTH. D.h., der gesamte Inhalt für mr.message[]
|
||||||
|
// (Payload + Checksumme) ist MAX_TOTAL_CONTENT_LENGTH + 1 Bytes lang. Dadurch
|
||||||
|
// wird der Puffer definitiv überlaufen.
|
||||||
|
uint8_t oversized_data_for_buffer[MAX_TOTAL_CONTENT_LENGTH +
|
||||||
|
1]; // Ein Byte zu viel für mr.message[]
|
||||||
|
for (size_t i = 0; i < sizeof(oversized_data_for_buffer); ++i) {
|
||||||
|
oversized_data_for_buffer[i] = (uint8_t)(i + 1); // Beliebige Daten
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wir brauchen eine korrekte Checksumme, auch wenn die Nachricht zu lang ist,
|
||||||
|
// da der Sender diese theoretisch senden würde.
|
||||||
|
// Die Berechnung erfolgt über die tatsächlich gesendeten Payload-Daten (die
|
||||||
|
// zu lang sind).
|
||||||
|
uint8_t checksum_byte_for_oversized_msg = calculate_payload_checksum_byte(
|
||||||
|
msgid, oversized_data_for_buffer, MAX_MESSAGE_PAYLOAD_LENGTH + 1);
|
||||||
|
|
||||||
|
// Simuliere den Versand der Nachricht Byte für Byte
|
||||||
|
parse_byte(&mr, StartByte); // mr.state = GetMessageType
|
||||||
|
parse_byte(&mr, msgid); // mr.state = InPayload, mr.messageid = 0x07
|
||||||
|
|
||||||
|
// Sende MAX_TOTAL_CONTENT_LENGTH Bytes, die den Puffer `mr.message`
|
||||||
|
// vollständig füllen. Index läuft von 0 bis MAX_TOTAL_CONTENT_LENGTH-1.
|
||||||
|
for (size_t i = 0; i < MAX_TOTAL_CONTENT_LENGTH; ++i) {
|
||||||
|
// Hier schicken wir die ersten MAX_TOTAL_CONTENT_LENGTH Bytes des
|
||||||
|
// übergroßen Payloads (inklusive dem eigentlichen Checksummen-Byte an
|
||||||
|
// Position MAX_MESSAGE_PAYLOAD_LENGTH). Das wird den Puffer anfüllen, aber
|
||||||
|
// noch keinen Overflow melden.
|
||||||
|
parse_byte(&mr, oversized_data_for_buffer[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// An diesem Punkt sollte mr.index = MAX_TOTAL_CONTENT_LENGTH sein.
|
||||||
|
// Der Puffer `mr.message` ist jetzt voll.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MAX_TOTAL_CONTENT_LENGTH,
|
||||||
|
mr.index); // Der Puffer ist genau gefüllt.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(InPayload, mr.state); // Noch im Payload-Zustand.
|
||||||
|
|
||||||
|
// Das nächste Byte (das letzte Byte von oversized_data_for_buffer)
|
||||||
|
// wird den Overflow auslösen, da mr->index dann mr->max_total_content_length
|
||||||
|
// überschreitet.
|
||||||
|
parse_byte(&mr, oversized_data_for_buffer[MAX_TOTAL_CONTENT_LENGTH]);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid); // MSGID ist korrekt gesetzt
|
||||||
|
// received_payload_len sollte die max. Puffergröße sein, bis der Fehler
|
||||||
|
// auftrat
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MAX_TOTAL_CONTENT_LENGTH, received_payload_len);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MessageToLong, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(MessageToLong, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 8: Unerwartetes StartByte mitten im Frame
|
||||||
|
void test_8_unexpected_start_byte_in_payload(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x08;
|
||||||
|
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, msgid,
|
||||||
|
0x10, // Teil des Payloads
|
||||||
|
StartByte, // Unerwartetes StartByte mitten im Payload
|
||||||
|
0x20, // Dies würde danach kommen
|
||||||
|
0x00, // Dummy-Checksumme
|
||||||
|
EndByte // Dummy-EndByte
|
||||||
|
};
|
||||||
|
|
||||||
|
parse_byte(&mr, full_message[0]); // StartByte
|
||||||
|
parse_byte(&mr, full_message[1]); // MSGID
|
||||||
|
parse_byte(&mr, full_message[2]); // Payload 0x10
|
||||||
|
|
||||||
|
// Hier kommt das unerwartete StartByte, sollte den Fehler auslösen und
|
||||||
|
// resetten
|
||||||
|
parse_byte(&mr, full_message[3]);
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid);
|
||||||
|
// received_payload_len sollte die Länge der Daten sein, die vor dem Fehler
|
||||||
|
// empfangen wurden.
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(1, received_payload_len); // Nur 0x10 empfangen
|
||||||
|
TEST_ASSERT_EQUAL_UINT8_ARRAY(((uint8_t[]){0x10}), received_payload, 1);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 9: Unerwartetes EndByte an der Position der MSGID
|
||||||
|
void test_9_unexpected_end_byte_at_msgid(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t full_message[] = {StartByte, EndByte, // EndByte anstelle von MSGID
|
||||||
|
0x01, 0x02, 0x03, 0x04, EndByte};
|
||||||
|
|
||||||
|
parse_byte(&mr, full_message[0]); // StartByte
|
||||||
|
parse_byte(&mr, full_message[1]); // EndByte anstelle MSGID
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(
|
||||||
|
0x00, received_msgid); // MSGID ist noch 0, da keine empfangen
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, received_payload_len); // Noch kein Payload
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, received_error);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(UnexpectedCommandByte, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 10: Kein StartByte zu Beginn der Sequenz (Parser sollte ignorieren)
|
||||||
|
void test_10_no_startbyte_at_beginning_ignored(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msg[] = {0x01, 0x02, 0x03, 0x04}; // Beginnt nicht mit StartByte
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(msg); ++i) {
|
||||||
|
parse_byte(&mr, msg[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_FALSE(
|
||||||
|
message_fail_flag); // Sollte keinen Fehler melden, nur ignorieren
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte,
|
||||||
|
mr.state); // Sollte im Wartezustand bleiben
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, mr.index); // Index sollte 0 bleiben
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(NoError, mr.error); // Kein Fehler gemeldet
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test 11: Ungültige Länge (z.B. EndByte kommt zu früh, ohne Checksumme)
|
||||||
|
// Angenommen, das Protokoll erwartet immer mindestens MSGID + Checksumme.
|
||||||
|
// Ein leeres Payload ist OK (MSGID + Checksumme + EndByte), aber nur MSGID +
|
||||||
|
// EndByte ist Fehler.
|
||||||
|
void test_11_frame_too_short_no_checksum(void) {
|
||||||
|
struct MessageReceive mr = InitMessageReceive();
|
||||||
|
|
||||||
|
uint8_t msgid = 0x09;
|
||||||
|
uint8_t full_message[] = {
|
||||||
|
StartByte, msgid, EndByte // Kein Payload, keine Checksumme
|
||||||
|
};
|
||||||
|
|
||||||
|
for (uint8_t i = 0; i < sizeof(full_message); ++i) {
|
||||||
|
parse_byte(&mr, full_message[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_ASSERT_FALSE(message_received_flag);
|
||||||
|
TEST_ASSERT_TRUE(message_fail_flag);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(msgid, received_msgid); // MSGID ist bekannt
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(0, received_payload_len); // Payload-Puffer ist leer
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum,
|
||||||
|
received_error); // Checksumme kann nicht 0x00 sein
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WaitingForStartByte, mr.state);
|
||||||
|
TEST_ASSERT_EQUAL_UINT8(WrongCheckSum, mr.error);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
UNITY_BEGIN();
|
||||||
|
RUN_TEST(test_1_valid_message_parses_correctly);
|
||||||
|
RUN_TEST(test_2_invalid_checksum_resets_state);
|
||||||
|
RUN_TEST(test_3_zero_length_message);
|
||||||
|
RUN_TEST(test_4_escaped_message_id);
|
||||||
|
RUN_TEST(test_5_escaped_payload_byte);
|
||||||
|
RUN_TEST(test_6_escaped_checksum_byte);
|
||||||
|
RUN_TEST(test_7_message_too_long);
|
||||||
|
RUN_TEST(test_8_unexpected_start_byte_in_payload);
|
||||||
|
RUN_TEST(test_9_unexpected_end_byte_at_msgid);
|
||||||
|
RUN_TEST(test_10_no_startbyte_at_beginning_ignored);
|
||||||
|
RUN_TEST(test_11_frame_too_short_no_checksum);
|
||||||
|
return UNITY_END();
|
||||||
|
}
|
||||||
111
tools/main.py
Normal file
111
tools/main.py
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
import serial
|
||||||
|
import time
|
||||||
|
from parser import UartMessageParser, ParserError
|
||||||
|
from message_builder import MessageBuilder, MessageBuilderError, PayloadTooLargeError, BufferOverflowError
|
||||||
|
import payload_parser
|
||||||
|
|
||||||
|
SERIAL_PORT = "/dev/ttyUSB0"
|
||||||
|
BAUDRATE = 115200
|
||||||
|
WRITE_TIMEOUT = 0.5
|
||||||
|
READ_TIMEOUT = 1.0
|
||||||
|
|
||||||
|
payload_parser = payload_parser.PayloadParser()
|
||||||
|
|
||||||
|
|
||||||
|
def on_message_received_from_uart(message_id: int, payload: bytes, payload_length: int):
|
||||||
|
"""
|
||||||
|
Callback-Funktion, die aufgerufen wird, wenn der Parser eine vollständige,
|
||||||
|
gültige Nachricht empfangen hat.
|
||||||
|
"""
|
||||||
|
print(f"\n[MAIN] Nachricht erfolgreich empfangen! ID: 0x{
|
||||||
|
message_id:02X}")
|
||||||
|
print(f"[MAIN] Payload ({payload_length} Bytes): {
|
||||||
|
payload[:payload_length].hex().upper()}")
|
||||||
|
|
||||||
|
parsed_object = payload_parser.parse_payload(
|
||||||
|
message_id, payload[:payload_length])
|
||||||
|
|
||||||
|
print(parsed_object)
|
||||||
|
|
||||||
|
|
||||||
|
def on_message_fail_from_uart(message_id: int, current_message_buffer: bytes,
|
||||||
|
current_index: int, error_type: ParserError):
|
||||||
|
"""
|
||||||
|
Callback-Funktion, die aufgerufen wird, wenn der Parser einen Fehler
|
||||||
|
beim Empfang einer Nachricht feststellt.
|
||||||
|
"""
|
||||||
|
print(f"\n[MAIN] Fehler beim Parsen der Nachricht! ID: 0x{
|
||||||
|
message_id:02X}")
|
||||||
|
print(f"[MAIN] Fehler: {error_type.name}")
|
||||||
|
print(f"[MAIN] Bisheriger Puffer ({current_index} Bytes): {
|
||||||
|
current_message_buffer[:current_index].hex().upper()}")
|
||||||
|
|
||||||
|
|
||||||
|
def run_uart_test():
|
||||||
|
"""
|
||||||
|
Führt den UART-Test durch: Sendet eine Nachricht und liest alle Antworten.
|
||||||
|
"""
|
||||||
|
ser = None
|
||||||
|
|
||||||
|
parser = UartMessageParser(
|
||||||
|
on_message_received_callback=on_message_received_from_uart,
|
||||||
|
on_message_fail_callback=on_message_fail_from_uart
|
||||||
|
)
|
||||||
|
message_builder = MessageBuilder()
|
||||||
|
|
||||||
|
try:
|
||||||
|
ser = serial.Serial(
|
||||||
|
port=SERIAL_PORT,
|
||||||
|
baudrate=BAUDRATE,
|
||||||
|
timeout=READ_TIMEOUT,
|
||||||
|
write_timeout=WRITE_TIMEOUT
|
||||||
|
)
|
||||||
|
print(f"Serielle Schnittstelle {
|
||||||
|
SERIAL_PORT} mit Baudrate {BAUDRATE} geöffnet.")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Baue die Nachricht
|
||||||
|
message_to_send = message_builder.build_message(
|
||||||
|
0x3,
|
||||||
|
b'',
|
||||||
|
255
|
||||||
|
)
|
||||||
|
print(f"\n[MAIN] Gebaute Nachricht zum Senden: {
|
||||||
|
message_to_send.hex().upper()}")
|
||||||
|
bytes_written = ser.write(message_to_send)
|
||||||
|
print(f"[MAIN] {bytes_written} Bytes gesendet.")
|
||||||
|
except (PayloadTooLargeError, BufferOverflowError) as e:
|
||||||
|
print(f"[MAIN] Fehler beim Bauen der Nachricht: {e}")
|
||||||
|
return # Beende die Funktion, wenn die Nachricht nicht gebaut werden kann
|
||||||
|
except Exception as e:
|
||||||
|
print(
|
||||||
|
f"[MAIN] Ein unerwarteter Fehler beim Bauen der Nachricht ist aufgetreten: {e}")
|
||||||
|
return
|
||||||
|
|
||||||
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
received_data = ser.read_all()
|
||||||
|
|
||||||
|
if received_data:
|
||||||
|
print(f"Empfangene Daten ({len(received_data)} Bytes): {
|
||||||
|
received_data.hex().upper()}")
|
||||||
|
for byte_val in received_data:
|
||||||
|
parser.parse_byte(byte_val)
|
||||||
|
else:
|
||||||
|
print("Keine Daten empfangen.")
|
||||||
|
|
||||||
|
except serial.SerialException as e:
|
||||||
|
print(f"Fehler beim Zugriff auf die serielle Schnittstelle: {e}")
|
||||||
|
print(f"Stelle sicher, dass '{
|
||||||
|
SERIAL_PORT}' der korrekte Port ist und nicht von einer anderen Anwendung verwendet wird.")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Ein unerwarteter Fehler ist aufgetreten: {e}")
|
||||||
|
finally:
|
||||||
|
if ser and ser.is_open:
|
||||||
|
ser.close()
|
||||||
|
print("Serielle Schnittstelle geschlossen.")
|
||||||
|
|
||||||
|
|
||||||
|
# Führe den Test aus
|
||||||
|
if __name__ == "__main__":
|
||||||
|
run_uart_test()
|
||||||
115
tools/message_builder.py
Normal file
115
tools/message_builder.py
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
import enum
|
||||||
|
|
||||||
|
START_BYTE = 0xAA
|
||||||
|
ESCAPE_BYTE = 0xBB
|
||||||
|
END_BYTE = 0xCC
|
||||||
|
|
||||||
|
|
||||||
|
class MessageBuilderError(Exception):
|
||||||
|
"""Basisklasse für Fehler des Message Builders."""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PayloadTooLargeError(MessageBuilderError):
|
||||||
|
"""Ausnahme, wenn der Payload zu groß für den Puffer ist."""
|
||||||
|
|
||||||
|
def __init__(self, required_size, buffer_size):
|
||||||
|
super().__init__(f"Payload ({
|
||||||
|
required_size} bytes) ist größer als der verfügbare Puffer ({buffer_size} bytes).")
|
||||||
|
self.required_size = required_size
|
||||||
|
self.buffer_size = buffer_size
|
||||||
|
|
||||||
|
|
||||||
|
class BufferOverflowError(MessageBuilderError):
|
||||||
|
"""Ausnahme, wenn der Puffer während des Bauens überläuft."""
|
||||||
|
|
||||||
|
def __init__(self, current_size, max_size, byte_to_add=None):
|
||||||
|
msg = f"Pufferüberlauf: Aktuelle Größe {
|
||||||
|
current_size}, Max. Größe {max_size}."
|
||||||
|
if byte_to_add is not None:
|
||||||
|
msg += f" Versuch, Byte 0x{byte_to_add:02X} hinzuzufügen."
|
||||||
|
super().__init__(msg)
|
||||||
|
self.current_size = current_size
|
||||||
|
self.max_size = max_size
|
||||||
|
self.byte_to_add = byte_to_add
|
||||||
|
|
||||||
|
|
||||||
|
class MessageBuilder:
|
||||||
|
"""
|
||||||
|
Klasse zum Aufbau von UART-Nachrichten gemäß dem definierten Protokoll,
|
||||||
|
inklusive Stuffing und Checksummenberechnung.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def _needs_stuffing_byte(self, byte: int) -> bool:
|
||||||
|
"""
|
||||||
|
Prüft, ob ein Byte ein Stuffing-Byte benötigt (d.h. ob es ein Steuerbyte ist).
|
||||||
|
"""
|
||||||
|
return (byte == START_BYTE or byte == ESCAPE_BYTE or byte == END_BYTE)
|
||||||
|
|
||||||
|
def _add_byte_with_length_check(self, byte: int, buffer: bytearray, max_length: int):
|
||||||
|
"""
|
||||||
|
Fügt ein Byte zum Puffer hinzu und prüft auf Pufferüberlauf.
|
||||||
|
Löst BufferOverflowError aus, wenn der Puffer voll ist.
|
||||||
|
"""
|
||||||
|
if len(buffer) >= max_length:
|
||||||
|
raise BufferOverflowError(len(buffer), max_length, byte)
|
||||||
|
buffer.append(byte)
|
||||||
|
|
||||||
|
def build_message(self, msgid: int, payload: bytes, msg_buffer_size: int) -> bytes:
|
||||||
|
"""
|
||||||
|
Baut eine vollständige UART-Nachricht.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
msgid (int): Die Message ID (0-255).
|
||||||
|
payload (bytes): Die Nutzdaten der Nachricht als Byte-Objekt.
|
||||||
|
msg_buffer_size (int): Die maximale Größe des Ausgabepuffers.
|
||||||
|
Dies ist die maximale Länge der *fertigen* Nachricht.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bytes: Die fertig aufgebaute Nachricht als Byte-Objekt.
|
||||||
|
|
||||||
|
Raises:
|
||||||
|
PayloadTooLargeError: Wenn der Payload (mit Overhead) den Puffer überschreiten würde.
|
||||||
|
BufferOverflowError: Wenn während des Bauens ein Pufferüberlauf auftritt.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if len(payload) + 4 > msg_buffer_size:
|
||||||
|
raise PayloadTooLargeError(len(payload) + 4, msg_buffer_size)
|
||||||
|
|
||||||
|
checksum = 0
|
||||||
|
msg_buffer = bytearray()
|
||||||
|
|
||||||
|
# 1. StartByte hinzufügen
|
||||||
|
self._add_byte_with_length_check(
|
||||||
|
START_BYTE, msg_buffer, msg_buffer_size)
|
||||||
|
|
||||||
|
# 2. Message ID hinzufügen (mit Stuffing)
|
||||||
|
if self._needs_stuffing_byte(msgid):
|
||||||
|
self._add_byte_with_length_check(
|
||||||
|
ESCAPE_BYTE, msg_buffer, msg_buffer_size)
|
||||||
|
self._add_byte_with_length_check(msgid, msg_buffer, msg_buffer_size)
|
||||||
|
checksum ^= msgid
|
||||||
|
|
||||||
|
# 3. Payload-Bytes hinzufügen (mit Stuffing)
|
||||||
|
for byte_val in payload:
|
||||||
|
if self._needs_stuffing_byte(byte_val):
|
||||||
|
self._add_byte_with_length_check(
|
||||||
|
ESCAPE_BYTE, msg_buffer, msg_buffer_size)
|
||||||
|
self._add_byte_with_length_check(
|
||||||
|
byte_val, msg_buffer, msg_buffer_size)
|
||||||
|
checksum ^= byte_val
|
||||||
|
|
||||||
|
# 4. Checksumme hinzufügen (mit Stuffing)
|
||||||
|
if self._needs_stuffing_byte(checksum):
|
||||||
|
self._add_byte_with_length_check(
|
||||||
|
ESCAPE_BYTE, msg_buffer, msg_buffer_size)
|
||||||
|
self._add_byte_with_length_check(checksum, msg_buffer, msg_buffer_size)
|
||||||
|
|
||||||
|
# 5. EndByte hinzufügen
|
||||||
|
self._add_byte_with_length_check(END_BYTE, msg_buffer, msg_buffer_size)
|
||||||
|
|
||||||
|
# Konvertiere bytearray zu unveränderlichem bytes-Objekt
|
||||||
|
return bytes(msg_buffer)
|
||||||
170
tools/parser.py
Normal file
170
tools/parser.py
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
import enum
|
||||||
|
|
||||||
|
# --- Konstanten für das UART-Protokoll ---
|
||||||
|
# Diese Werte müssen mit denen auf deinem Embedded-System übereinstimmen
|
||||||
|
START_BYTE = 0xAA
|
||||||
|
END_BYTE = 0xCC
|
||||||
|
ESCAPE_BYTE = 0x7D # Beispielwert, bitte an dein Protokoll anpassen
|
||||||
|
|
||||||
|
MAX_PAYLOAD_LENGTH = 255 # Maximale Länge des Nachrichten-Payloads (ohne Message ID und Checksumme)
|
||||||
|
# MAX_TOTAL_CONTENT_LENGTH in C beinhaltet Message ID, Payload und Checksumme.
|
||||||
|
# Hier definieren wir MAX_PAYLOAD_LENGTH, da der Parser den Payload sammelt.
|
||||||
|
# Die Gesamtgröße des empfangenen Puffers (message + checksum) darf MAX_PAYLOAD_LENGTH + 1 nicht überschreiten,
|
||||||
|
# da die Checksumme als letztes Byte des Payloads behandelt wird.
|
||||||
|
|
||||||
|
# --- Enumerationen für Parser-Zustände und Fehler ---
|
||||||
|
class ParserState(enum.Enum):
|
||||||
|
WAITING_FOR_START_BYTE = 0
|
||||||
|
GET_MESSAGE_TYPE = 1
|
||||||
|
ESCAPED_MESSAGE_TYPE = 2
|
||||||
|
IN_PAYLOAD = 3
|
||||||
|
ESCAPE_PAYLOAD_BYTE = 4
|
||||||
|
|
||||||
|
class ParserError(enum.Enum):
|
||||||
|
NO_ERROR = 0
|
||||||
|
UNEXPECTED_COMMAND_BYTE = 1
|
||||||
|
WRONG_CHECKSUM = 2
|
||||||
|
MESSAGE_TOO_LONG = 3
|
||||||
|
|
||||||
|
class UartMessageParser:
|
||||||
|
"""
|
||||||
|
Ein State-Machine-Parser für UART-Nachrichten basierend auf der bereitgestellten C-Logik.
|
||||||
|
|
||||||
|
Nachrichtenformat (angenommen):
|
||||||
|
[START_BYTE] [MESSAGE_ID] [PAYLOAD_BYTES...] [CHECKSUM_BYTE] [END_BYTE]
|
||||||
|
|
||||||
|
Escape-Sequenzen:
|
||||||
|
Wenn START_BYTE, END_BYTE oder ESCAPE_BYTE im MESSAGE_ID oder PAYLOAD vorkommen,
|
||||||
|
werden sie durch ESCAPE_BYTE gefolgt vom ursprünglichen Byte (nicht XORed) ersetzt.
|
||||||
|
Die Checksumme wird über die unescaped Bytes berechnet.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, on_message_received_callback=None, on_message_fail_callback=None):
|
||||||
|
"""
|
||||||
|
Initialisiert den UART-Nachrichten-Parser.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
on_message_received_callback (callable, optional): Eine Funktion, die aufgerufen wird,
|
||||||
|
wenn eine gültige Nachricht empfangen wurde.
|
||||||
|
Signatur: on_message_received(message_id: int, payload: bytes, payload_length: int)
|
||||||
|
on_message_fail_callback (callable, optional): Eine Funktion, die aufgerufen wird,
|
||||||
|
wenn ein Nachrichtenfehler auftritt.
|
||||||
|
Signatur: on_message_fail(message_id: int, current_message_buffer: bytes,
|
||||||
|
current_index: int, error_type: ParserError)
|
||||||
|
"""
|
||||||
|
self.state = ParserState.WAITING_FOR_START_BYTE
|
||||||
|
self.index = 0
|
||||||
|
self.checksum = 0
|
||||||
|
self.message_id = 0
|
||||||
|
self.message_buffer = bytearray(MAX_PAYLOAD_LENGTH + 1) # +1 für Checksummen-Byte
|
||||||
|
self.error = ParserError.NO_ERROR
|
||||||
|
|
||||||
|
# Callbacks für die Anwendung. Standardmäßig None oder einfache Print-Funktionen.
|
||||||
|
self.on_message_received = on_message_received_callback if on_message_received_callback else self._default_on_message_received
|
||||||
|
self.on_message_fail = on_message_fail_callback if on_message_fail_callback else self._default_on_message_fail
|
||||||
|
|
||||||
|
def _default_on_message_received(self, message_id, payload, payload_length):
|
||||||
|
"""Standard-Callback für empfangene Nachrichten, falls keiner angegeben ist."""
|
||||||
|
print(f"Parser: Nachricht empfangen! ID: 0x{message_id:02X}, "
|
||||||
|
f"Payload ({payload_length} Bytes): {payload[:payload_length].hex().upper()}")
|
||||||
|
|
||||||
|
def _default_on_message_fail(self, message_id, current_message_buffer, current_index, error_type):
|
||||||
|
"""Standard-Callback für Nachrichtenfehler, falls keiner angegeben ist."""
|
||||||
|
print(f"Parser: Fehler bei Nachricht! ID: 0x{message_id:02X}, "
|
||||||
|
f"Fehler: {error_type.name}, "
|
||||||
|
f"Bisheriger Puffer ({current_index} Bytes): {current_message_buffer[:current_index].hex().upper()}")
|
||||||
|
|
||||||
|
def parse_byte(self, pbyte: int):
|
||||||
|
"""
|
||||||
|
Verarbeitet ein einzelnes empfangenes Byte.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
pbyte (int): Das empfangene Byte (0-255).
|
||||||
|
"""
|
||||||
|
# Sicherstellen, dass pbyte ein Integer im Bereich 0-255 ist
|
||||||
|
if not isinstance(pbyte, int) or not (0 <= pbyte <= 255):
|
||||||
|
print(f"Parser: Ungültiges Byte empfangen: {pbyte}. Muss ein Integer von 0-255 sein.")
|
||||||
|
return
|
||||||
|
|
||||||
|
current_state = self.state # Für bessere Lesbarkeit
|
||||||
|
|
||||||
|
if current_state == ParserState.WAITING_FOR_START_BYTE:
|
||||||
|
if pbyte == START_BYTE:
|
||||||
|
self.index = 0
|
||||||
|
self.checksum = 0
|
||||||
|
self.message_id = 0 # Reset message_id
|
||||||
|
self.error = ParserError.NO_ERROR # Reset error
|
||||||
|
self.state = ParserState.GET_MESSAGE_TYPE
|
||||||
|
# Andernfalls ignorieren wir Bytes, bis ein Start-Byte gefunden wird
|
||||||
|
|
||||||
|
elif current_state == ParserState.ESCAPED_MESSAGE_TYPE:
|
||||||
|
self.message_id = pbyte
|
||||||
|
self.checksum ^= pbyte
|
||||||
|
self.state = ParserState.IN_PAYLOAD
|
||||||
|
|
||||||
|
elif current_state == ParserState.GET_MESSAGE_TYPE:
|
||||||
|
if pbyte == ESCAPE_BYTE:
|
||||||
|
self.state = ParserState.ESCAPED_MESSAGE_TYPE
|
||||||
|
return # Dieses Byte wurde als Escape-Sequenz verarbeitet, nicht zum Payload hinzufügen
|
||||||
|
if pbyte == START_BYTE or pbyte == END_BYTE:
|
||||||
|
self.state = ParserState.WAITING_FOR_START_BYTE
|
||||||
|
self.error = ParserError.UNEXPECTED_COMMAND_BYTE
|
||||||
|
self.on_message_fail(self.message_id, self.message_buffer, self.index, self.error)
|
||||||
|
return
|
||||||
|
self.message_id = pbyte
|
||||||
|
self.checksum ^= pbyte
|
||||||
|
self.state = ParserState.IN_PAYLOAD
|
||||||
|
|
||||||
|
elif current_state == ParserState.ESCAPE_PAYLOAD_BYTE:
|
||||||
|
# Das escapte Byte ist Teil des Payloads
|
||||||
|
if self.index < MAX_PAYLOAD_LENGTH + 1: # +1 für Checksummen-Byte
|
||||||
|
self.message_buffer[self.index] = pbyte
|
||||||
|
self.index += 1
|
||||||
|
self.checksum ^= pbyte
|
||||||
|
self.state = ParserState.IN_PAYLOAD
|
||||||
|
else:
|
||||||
|
self.state = ParserState.WAITING_FOR_START_BYTE
|
||||||
|
self.error = ParserError.MESSAGE_TOO_LONG
|
||||||
|
self.on_message_fail(self.message_id, self.message_buffer, self.index, self.error)
|
||||||
|
return
|
||||||
|
|
||||||
|
elif current_state == ParserState.IN_PAYLOAD:
|
||||||
|
if pbyte == ESCAPE_BYTE:
|
||||||
|
self.state = ParserState.ESCAPE_PAYLOAD_BYTE
|
||||||
|
return # Dieses Byte wurde als Escape-Sequenz verarbeitet
|
||||||
|
if pbyte == START_BYTE:
|
||||||
|
self.state = ParserState.WAITING_FOR_START_BYTE
|
||||||
|
self.error = ParserError.UNEXPECTED_COMMAND_BYTE
|
||||||
|
self.on_message_fail(self.message_id, self.message_buffer, self.index, self.error)
|
||||||
|
return
|
||||||
|
if pbyte == END_BYTE:
|
||||||
|
if self.checksum != 0x00:
|
||||||
|
# Checksummenfehler: Die Checksumme wurde bis zum End-Byte XORed.
|
||||||
|
# Wenn die empfangene Checksumme korrekt war, sollte das Ergebnis 0 sein.
|
||||||
|
self.state = ParserState.WAITING_FOR_START_BYTE
|
||||||
|
self.error = ParserError.WRONG_CHECKSUM
|
||||||
|
self.on_message_fail(self.message_id, self.message_buffer, self.index, self.error)
|
||||||
|
return
|
||||||
|
|
||||||
|
# Erfolgreich empfangen! Die Checksumme ist das letzte Byte im Puffer.
|
||||||
|
# Die Länge des Payloads ist index - 1 (da das letzte Byte die Checksumme war).
|
||||||
|
payload_length = self.index - 1
|
||||||
|
if payload_length < 0: # Falls nur Message ID und Checksumme, aber kein Payload
|
||||||
|
payload_length = 0
|
||||||
|
|
||||||
|
self.on_message_received(self.message_id, self.message_buffer, payload_length)
|
||||||
|
self.state = ParserState.WAITING_FOR_START_BYTE
|
||||||
|
return # EndByte wurde verarbeitet, nicht zum Payload hinzufügen
|
||||||
|
|
||||||
|
# Normales Payload-Byte
|
||||||
|
if self.index < MAX_PAYLOAD_LENGTH + 1: # +1 für Checksummen-Byte
|
||||||
|
self.message_buffer[self.index] = pbyte
|
||||||
|
self.index += 1
|
||||||
|
self.checksum ^= pbyte
|
||||||
|
else:
|
||||||
|
# Nachricht zu lang
|
||||||
|
self.state = ParserState.WAITING_FOR_START_BYTE
|
||||||
|
self.error = ParserError.MESSAGE_TOO_LONG
|
||||||
|
self.on_message_fail(self.message_id, self.message_buffer, self.index, self.error)
|
||||||
|
return
|
||||||
|
|
||||||
192
tools/payload_parser.py
Normal file
192
tools/payload_parser.py
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
import dataclasses
|
||||||
|
import struct
|
||||||
|
from typing import Optional, Union, List
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class StatusMessage:
|
||||||
|
"""
|
||||||
|
Repräsentiert eine Status-Nachricht (z.B. Message ID 0x01).
|
||||||
|
Payload-Format: [status_code: uint8], [battery_level: uint8], [uptime_seconds: uint16]
|
||||||
|
"""
|
||||||
|
status_code: int
|
||||||
|
battery_level: int # 0-100%
|
||||||
|
uptime_seconds: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class SensorDataMessage:
|
||||||
|
"""
|
||||||
|
Repräsentiert eine Sensor-Daten-Nachricht (z.B. Message ID 0x02).
|
||||||
|
Payload-Format: [temperature_celsius: int16], [humidity_percent: uint16]
|
||||||
|
"""
|
||||||
|
temperature_celsius: int # signed short
|
||||||
|
humidity_percent: int # unsigned short
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class ClientEntry:
|
||||||
|
"""
|
||||||
|
Repräsentiert die Informationen für einen einzelnen Client innerhalb der ClientInfoMessage.
|
||||||
|
Payload-Format:
|
||||||
|
[client_id: uint8]
|
||||||
|
[is_available: uint8] (0=false, >0=true)
|
||||||
|
[is_slot_used: uint8] (0=false, >0=true)
|
||||||
|
[mac_address: bytes (6)]
|
||||||
|
[unoccupied_value_1: uint32]
|
||||||
|
[unoccupied_value_2: uint32]
|
||||||
|
Gesamt: 1 + 1 + 1 + 6 + 4 + 4 = 17 Bytes pro Eintrag.
|
||||||
|
"""
|
||||||
|
client_id: int
|
||||||
|
is_available: bool
|
||||||
|
is_slot_used: bool
|
||||||
|
mac_address: bytes # 6 Bytes MAC-Adresse
|
||||||
|
last_ping: int # 4 Bytes, unbelegt
|
||||||
|
last_successfull_ping: int # 4 Bytes, unbelegt
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class ClientInfoMessage:
|
||||||
|
"""
|
||||||
|
Repräsentiert eine Nachricht mit Client-Informationen (Message ID 0x03).
|
||||||
|
Payload-Format:
|
||||||
|
[num_clients: uint8]
|
||||||
|
[client_entry_1: ClientEntry]
|
||||||
|
[client_entry_2: ClientEntry]
|
||||||
|
...
|
||||||
|
"""
|
||||||
|
num_clients: int
|
||||||
|
clients: List[ClientEntry]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclasses.dataclass
|
||||||
|
class UnknownMessage:
|
||||||
|
"""
|
||||||
|
Repräsentiert eine Nachricht mit unbekannter ID oder fehlerhaftem Payload.
|
||||||
|
"""
|
||||||
|
message_id: int
|
||||||
|
raw_payload: bytes
|
||||||
|
error_message: str
|
||||||
|
|
||||||
|
# --- Payload Parser Klasse ---
|
||||||
|
|
||||||
|
|
||||||
|
class PayloadParser:
|
||||||
|
"""
|
||||||
|
Interpretiert den Payload einer UART-Nachricht basierend auf ihrer Message ID
|
||||||
|
und wandelt ihn in ein strukturiertes Python-Objekt (dataclass) um.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
# Ein Dictionary, das Message IDs auf ihre entsprechenden Parsing-Funktionen abbildet.
|
||||||
|
self._parser_map = {
|
||||||
|
0x01: self._parse_status_message,
|
||||||
|
0x02: self._parse_sensor_data_message,
|
||||||
|
# Aktualisiert für die neue 0x03 Struktur
|
||||||
|
0x03: self._parse_client_info_message,
|
||||||
|
0x04: self._parse_client_info_message,
|
||||||
|
# Füge hier weitere Message IDs und ihre Parsing-Funktionen hinzu
|
||||||
|
}
|
||||||
|
|
||||||
|
def _parse_status_message(self, payload: bytes) -> Union[StatusMessage, UnknownMessage]:
|
||||||
|
"""Parsen des Payloads für Message ID 0x01 (StatusMessage)."""
|
||||||
|
# Erwartetes Format: 1 Byte Status, 1 Byte Battery, 2 Bytes Uptime (Little-Endian)
|
||||||
|
if len(payload) != 4:
|
||||||
|
return UnknownMessage(0x01, payload, f"Falsche Payload-Länge für StatusMessage: Erwartet 4, Got {len(payload)}")
|
||||||
|
try:
|
||||||
|
# '<BBH' bedeutet: Little-Endian, Byte (unsigned char), Byte (unsigned char), Half-word (unsigned short)
|
||||||
|
status_code, battery_level, uptime_seconds = struct.unpack(
|
||||||
|
'<BBH', payload)
|
||||||
|
return StatusMessage(status_code, battery_level, uptime_seconds)
|
||||||
|
except struct.error as e:
|
||||||
|
return UnknownMessage(0x01, payload, f"Fehler beim Entpacken der StatusMessage: {e}")
|
||||||
|
|
||||||
|
def _parse_sensor_data_message(self, payload: bytes) -> Union[SensorDataMessage, UnknownMessage]:
|
||||||
|
"""Parsen des Payloads für Message ID 0x02 (SensorDataMessage)."""
|
||||||
|
# Erwartetes Format: 2 Bytes Temperatur (signed short), 2 Bytes Feuchtigkeit (unsigned short) (Little-Endian)
|
||||||
|
if len(payload) != 4:
|
||||||
|
return UnknownMessage(0x02, payload, f"Falsche Payload-Länge für SensorDataMessage: Erwartet 4, Got {len(payload)}")
|
||||||
|
try:
|
||||||
|
# '<hH' bedeutet: Little-Endian, short (signed), unsigned short
|
||||||
|
temperature_celsius, humidity_percent = struct.unpack(
|
||||||
|
'<hH', payload)
|
||||||
|
return SensorDataMessage(temperature_celsius, humidity_percent)
|
||||||
|
except struct.error as e:
|
||||||
|
return UnknownMessage(0x02, payload, f"Fehler beim Entpacken der SensorDataMessage: {e}")
|
||||||
|
|
||||||
|
def _parse_client_info_message(self, payload: bytes) -> Union[ClientInfoMessage, UnknownMessage]:
|
||||||
|
"""Parsen des Payloads für Message ID 0x03 (ClientInfoMessage)."""
|
||||||
|
if not payload:
|
||||||
|
# Wenn der Payload leer ist, aber num_clients erwartet wird, ist das ein Fehler
|
||||||
|
return UnknownMessage(0x03, payload, "Payload für ClientInfoMessage ist leer, aber num_clients erwartet.")
|
||||||
|
|
||||||
|
try:
|
||||||
|
# Das erste Byte ist die Anzahl der Clients
|
||||||
|
num_clients = payload[0]
|
||||||
|
# Die restlichen Bytes sind die Client-Einträge
|
||||||
|
client_data_bytes = payload[1:]
|
||||||
|
|
||||||
|
# 1 (ID) + 1 (Avail) + 1 (Used) + 6 (MAC) + 4 (Val1) + 4 (Val2)
|
||||||
|
EXPECTED_CLIENT_ENTRY_SIZE = 17
|
||||||
|
|
||||||
|
if len(client_data_bytes) != num_clients * EXPECTED_CLIENT_ENTRY_SIZE:
|
||||||
|
return UnknownMessage(0x03, payload,
|
||||||
|
f"Falsche Payload-Länge für Client-Einträge: Erwartet {
|
||||||
|
num_clients * EXPECTED_CLIENT_ENTRY_SIZE}, "
|
||||||
|
f"Got {len(client_data_bytes)} nach num_clients.")
|
||||||
|
|
||||||
|
clients_list: List[ClientEntry] = []
|
||||||
|
# Formatstring für einen Client-Eintrag:
|
||||||
|
# < : Little-Endian
|
||||||
|
# B : uint8 (client_id, is_available, is_slot_used)
|
||||||
|
# 6s: 6 Bytes (mac_address)
|
||||||
|
# I : uint32 (unoccupied_value_1, unoccupied_value_2)
|
||||||
|
CLIENT_ENTRY_FORMAT = '<BBB6sII'
|
||||||
|
|
||||||
|
for i in range(num_clients):
|
||||||
|
start_index = i * EXPECTED_CLIENT_ENTRY_SIZE
|
||||||
|
end_index = start_index + EXPECTED_CLIENT_ENTRY_SIZE
|
||||||
|
entry_bytes = client_data_bytes[start_index:end_index]
|
||||||
|
|
||||||
|
# Entpacke die Daten für einen Client-Eintrag
|
||||||
|
client_id, is_available_byte, is_slot_used_byte, mac_address, val1, val2 = \
|
||||||
|
struct.unpack(CLIENT_ENTRY_FORMAT, entry_bytes)
|
||||||
|
|
||||||
|
# Konvertiere 0/1 Bytes zu boolschen Werten
|
||||||
|
is_available = bool(is_available_byte)
|
||||||
|
is_slot_used = bool(is_slot_used_byte)
|
||||||
|
|
||||||
|
clients_list.append(ClientEntry(
|
||||||
|
client_id=client_id,
|
||||||
|
is_available=is_available,
|
||||||
|
is_slot_used=is_slot_used,
|
||||||
|
mac_address=mac_address,
|
||||||
|
last_ping=val1,
|
||||||
|
last_successfull_ping=val2
|
||||||
|
))
|
||||||
|
|
||||||
|
return ClientInfoMessage(num_clients=num_clients, clients=clients_list)
|
||||||
|
|
||||||
|
except struct.error as e:
|
||||||
|
return UnknownMessage(0x03, payload, f"Fehler beim Entpacken der ClientInfoMessage-Einträge: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
return UnknownMessage(0x03, payload, f"Unerwarteter Fehler beim Parsen der ClientInfoMessage: {e}")
|
||||||
|
|
||||||
|
def parse_payload(self, message_id: int, payload: bytes) -> Union[StatusMessage, SensorDataMessage, ClientInfoMessage, UnknownMessage]:
|
||||||
|
"""
|
||||||
|
Interpretiert den gegebenen Payload basierend auf der Message ID.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
message_id (int): Die ID der Nachricht.
|
||||||
|
payload (bytes): Die rohen Nutzdaten der Nachricht.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Union[StatusMessage, SensorDataMessage, ClientInfoMessage, UnknownMessage]:
|
||||||
|
Ein dataclass-Objekt, das die dekodierten Daten repräsentiert,
|
||||||
|
oder ein UnknownMessage-Objekt bei unbekannter ID oder Parsing-Fehler.
|
||||||
|
"""
|
||||||
|
parser_func = self._parser_map.get(message_id)
|
||||||
|
if parser_func:
|
||||||
|
return parser_func(payload)
|
||||||
|
else:
|
||||||
|
return UnknownMessage(message_id, payload, "Unbekannte Message ID.")
|
||||||
Loading…
x
Reference in New Issue
Block a user