diff --git a/main/communication_handler.c b/main/communication_handler.c index c72166b..d6566e1 100644 --- a/main/communication_handler.c +++ b/main/communication_handler.c @@ -8,15 +8,18 @@ #include #include #include +#include #include static const char *TAG = "ALOX - COM"; static struct ESP_MessageBroker mr; +static QueueHandle_t ESP_recieved_message_queue; -void ESP_InitMessageBroker() { +void ESP_InitMessageBroker(QueueHandle_t msg_queue_handle) { mr.num_direct_callbacks = 0; mr.num_task_callbacks = 0; + ESP_recieved_message_queue = msg_queue_handle; return; } @@ -52,12 +55,16 @@ void ESP_MessageBrokerTask(void *param) { while (1) { if (xQueueReceive(msg_queue, &received_msg, portMAX_DELAY)) { + ESP_LOGI(TAG, "Broker got message trying to relay it now"); const BaseMessage *message = (const BaseMessage *)received_msg.data; - + ESP_LOGI(TAG, "Broker searching for command page %d", + message->commandPage); for (int i = 0; i < mr.num_direct_callbacks; i++) { if (mr.FunctionList[i].MSGID == message->commandPage) { - mr.FunctionList[i].callback(received_msg.esp_now_info, + mr.FunctionList[i].callback(&received_msg.esp_now_info, received_msg.data, received_msg.data_len); + ESP_LOGI(TAG, "Broker found matching msgid %d", + mr.FunctionList[i].MSGID); } } for (int i = 0; i < mr.num_direct_callbacks; i++) { @@ -190,10 +197,93 @@ void master_ping_task(void *param) { } } +void master_StatusCallback(const esp_now_recv_info_t *esp_now_info, + const uint8_t *data, int data_len) { + const BaseMessage *message = (const BaseMessage *)data; + + ESP_LOGI(TAG, "SRC " MACSTR, MAC2STR(esp_now_info->src_addr)); + ESP_LOGI(TAG, + "Status Message Received: status: %d, runningPartition: %d, uptime: " + "%d, version: %d", + message->payload.status_payload.status, + message->payload.status_payload.runningPartition, + message->payload.status_payload.uptime, + message->payload.status_payload.version); +} + +void master_RegisterCallback(const esp_now_recv_info_t *esp_now_info, + const uint8_t *data, int data_len) { + BaseMessage replyMessage = {}; + const BaseMessage *message = (const BaseMessage *)data; + + ESP_LOGI(TAG, "WILL REGISTER DEVICE"); + esp_now_peer_info_t checkPeerInfo; + esp_err_t checkPeer = + esp_now_get_peer(esp_now_info->src_addr, &checkPeerInfo); + switch (checkPeer) { + case (ESP_OK): + ESP_LOGI(TAG, "CLIENT BEKANNT"); + int id = get_client_id(esp_client_list, esp_now_info->src_addr); + esp_client_list->Clients[id].isAvailable = true; + esp_client_list->Clients[id].lastSuccessfullPing = xTaskGetTickCount(); + ESP_LOGI(TAG, "Updated client %d last ping time to %lu", id, + esp_client_list->Clients[id].lastSuccessfullPing); + break; + case (ESP_ERR_ESPNOW_NOT_INIT): + ESP_LOGI(TAG, "Not initalised"); + break; + case (ESP_ERR_ESPNOW_ARG): + ESP_LOGI(TAG, "ESP ERR ESPNOW_ARG"); + break; + case (ESP_ERR_ESPNOW_NOT_FOUND): + ESP_LOGI(TAG, "CLIENT WIRD IN DIE LISTE AUFGENOMMEN"); + add_peer(esp_now_info->src_addr); + ESP_LOGI(TAG, "FRAGE CLIENT STATUS AN"); + + GetStatusPayload payload = {}; + replyMessage = MessageBuilder(GetStatusPage, *(PayloadUnion *)&payload, + sizeof(payload)); + ESP_ERROR_CHECK(esp_now_send( + esp_now_info->src_addr, (uint8_t *)&replyMessage, sizeof(BaseMessage))); + + break; + default: + ESP_LOGI(TAG, "Unknown Message %i", checkPeer); + } +} + +void ESPNOW_RegisterMasterCallbacks() { + ESP_RegisterFunction(StatusPage, master_StatusCallback); + ESP_RegisterFunction(RegisterPage, master_RegisterCallback); +} + void master_receive_callback(const esp_now_recv_info_t *esp_now_info, const uint8_t *data, int data_len) { ESP_LOGI(TAG, "MASTER GOT MESSAGE"); + // Allokiere Speicher für die Daten und kopiere sie + uint8_t *copied_data = (uint8_t *)malloc(data_len); + if (copied_data == NULL) { + ESP_LOGE(TAG, "Failed to allocate memory for message data."); + return; + } + memcpy(copied_data, data, data_len); + + // Fülle die neue Struktur mit kopierten Daten + ESPNOW_MessageInfo msg_info; + memcpy(&msg_info.esp_now_info, esp_now_info, sizeof(esp_now_recv_info_t)); + msg_info.data = copied_data; + msg_info.data_len = data_len; + + if (xQueueSend(ESP_recieved_message_queue, &msg_info, portMAX_DELAY) != + pdPASS) { + // Fehlerbehandlung: Queue voll oder Senden fehlgeschlagen + ESP_LOGE(TAG, "Failed to send parsed message to queue."); + } + + return; + // TODO: just testing if callback queue logic works here + BaseMessage replyMessage = {}; const BaseMessage *message = (const BaseMessage *)data; int id; diff --git a/main/communication_handler.h b/main/communication_handler.h index 644eec4..d94aa15 100644 --- a/main/communication_handler.h +++ b/main/communication_handler.h @@ -143,15 +143,19 @@ typedef struct { } ESP_MessageBrokerTaskParams_t; typedef struct { - const esp_now_recv_info_t *esp_now_info; + esp_now_recv_info_t esp_now_info; const uint8_t *data; int data_len; } ESPNOW_MessageInfo; -void ESP_InitMessageBroker(); +void ESP_InitMessageBroker(QueueHandle_t msg_queue_handle); void ESP_RegisterFunction(CommandPages command, ESP_RegisterFunctionCallback callback); void ESP_RegisterTask(CommandPages command, ESP_RegisterTaskCallback callback); +void ESP_MessageBrokerTask(void *param); + +void ESPNOW_RegisterMasterCallbacks(); + int init_com(ClientList *clients, uint8_t wifi_channel); int getNextFreeClientId(); diff --git a/main/main.c b/main/main.c index e7df4f6..e21e2c7 100644 --- a/main/main.c +++ b/main/main.c @@ -36,6 +36,7 @@ static uint8_t send_message_buffer[1024]; static uint8_t send_message_payload_buffer[512]; static MessageBrokerTaskParams_t broker_task_params; +static ESP_MessageBrokerTaskParams_t esp_broker_task_params; ClientList clientList = {.Clients = {{0}}, .ClientCount = 0}; @@ -141,7 +142,7 @@ void clientInfoCallback(uint8_t msgid, const uint8_t *payload, int len = build_message(UART_CLIENT_INFO, send_payload_buffer, needed_buffer_size, send_buffer, send_buffer_size); - //ESP_LOG_BUFFER_HEX("SEND BUFFER: ", send_buffer, send_buffer_size); + // ESP_LOG_BUFFER_HEX("SEND BUFFER: ", send_buffer, send_buffer_size); if (len < 0) { ESP_LOGE(TAG, @@ -212,7 +213,7 @@ void app_main(void) { uint8_t ota_part_count = esp_ota_get_app_partition_count(); ESP_LOGI(TAG, "OTA: Got %d OTA Partitions", ota_part_count); - esp_ota_img_states_t ota_state; + esp_ota_img_states_t ota_state; if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) { ESP_LOGI(TAG, "OTA: Partition State : %d", ota_state); if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) { @@ -257,9 +258,19 @@ void app_main(void) { } nvs_close(nt); + QueueHandle_t espnow_message_queue = + xQueueCreate(10, sizeof(ESPNOW_MessageInfo)); + ESP_InitMessageBroker(espnow_message_queue); + + esp_broker_task_params.message_queue = espnow_message_queue; + xTaskCreate(ESP_MessageBrokerTask, "espnow_message_broker_task", 4096, + (void *)&esp_broker_task_params, 4, NULL); + // Tasks starten basierend auf Master/Client if (isMaster) { ESP_LOGI(TAG, "Started in Mastermode"); + ESPNOW_RegisterMasterCallbacks(); + add_peer(broadcast_address); xTaskCreate(master_broadcast_task, "MasterBroadcast", 4096, NULL, 1, NULL); // xTaskCreate(master_ping_task, "MasterPing", 4096, NULL, 1, NULL); @@ -288,7 +299,7 @@ void app_main(void) { RegisterCallback(0x02, versionCallback); RegisterCallback(0x03, clientInfoCallback); - init_ota(); + init_ota(); // xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1, // NULL); xTaskCreate(SendClientInfoTask, "SendCientInfo", 4096, NULL, 1,