esp_alox/main/message_handler.c

72 lines
2.3 KiB
C

#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";
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;
uint8_t *send_payload_buffer = task_params->payload_buffer;
size_t send_payload_buffer_size = task_params->payload_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_payload_buffer, send_payload_buffer_size,
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);