diff --git a/main/communication_handler.c b/main/communication_handler.c index 83beda3..802135b 100644 --- a/main/communication_handler.c +++ b/main/communication_handler.c @@ -63,6 +63,7 @@ void add_peer(uint8_t *macAddr) { 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."); } @@ -133,13 +134,9 @@ void master_broadcast_ping(void *param) { } } - - void master_ping_task(void *param) { while (1) { for (size_t i = 0; i < MAX_CLIENTS; i++) { - // ESP_LOGI(TAG, "WANT TO SEND PING TO CLIENT %zu isAvailiable %d", i, - // clients[i].isAvailable); if (clients[i].isAvailable) { ESP_LOGI(TAG, "SEND PING TO %zu: %s", i, MACtoString(clients[i].macAddr)); @@ -150,7 +147,6 @@ void master_ping_task(void *param) { esp_now_send(clients[i].macAddr, (uint8_t *)&message, sizeof(BaseMessage)); ESP_LOGI(TAG, "SENDING PING!!!!"); - vTaskDelay(pdMS_TO_TICKS(5)); } } vTaskDelay(pdMS_TO_TICKS(1000)); @@ -181,6 +177,7 @@ void master_receive_callback(const esp_now_recv_info_t *esp_now_info, memcmp(clients[i].macAddr, esp_now_info->src_addr, ESP_NOW_ETH_ALEN) == 0) { clients[i].lastSuccessfullPing = xTaskGetTickCount(); + clients[i].lastPing = (diff/1000); ESP_LOGI(TAG, "Updated client %d: %s last ping time to %lu", i, MACtoString(clients[i].macAddr), clients[i].lastSuccessfullPing); @@ -321,3 +318,7 @@ void client_monitor_task(void *pvParameters) { vTaskDelay(interval_ticks); } } + +ClientInfo *get_client_info(int entry) { + return &clients[entry]; +} diff --git a/main/communication_handler.h b/main/communication_handler.h index d9eac0a..1b7bc45 100644 --- a/main/communication_handler.h +++ b/main/communication_handler.h @@ -68,6 +68,7 @@ typedef struct { int rssi; bool isAvailable; TickType_t lastSuccessfullPing; + TickType_t lastPing; } ClientInfo; void init_com(); @@ -88,4 +89,6 @@ void client_receive_callback(const esp_now_recv_info_t *esp_now_info, void client_data_sending_task(void *param); void client_monitor_task(void *pvParameters); +ClientInfo *get_client_info(int entry); + #endif diff --git a/main/main.c b/main/main.c index 897dd28..7b1682f 100644 --- a/main/main.c +++ b/main/main.c @@ -11,11 +11,23 @@ #include "communication_handler.h" #include "main.h" +#include "portmacro.h" #include "uart_handler.h" #include static const char *TAG = "ALOX - MAIN"; +void SendClientInfoTask() { + int clientId = 0; + while (1) { + ClientInfo info = *get_client_info(clientId); + send_client_info(clientId, info.isAvailable, info.lastPing); + clientId += 1; + clientId = clientId%20; + vTaskDelay(100 / portTICK_PERIOD_MS); + } +} + void app_main(void) { esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || @@ -65,7 +77,8 @@ void app_main(void) { NULL); xTaskCreate(client_monitor_task, "MonitorClientTask", 4096, NULL, 1, NULL); init_uart(); - xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1, NULL); + //xTaskCreate(uart_status_task, "MasterUartStatusTask", 4096, NULL, 1, NULL); + xTaskCreate(SendClientInfoTask, "SendCientInfo", 4096, NULL, 1, NULL); } else { ESP_LOGI(TAG, "Started in Slavemode"); xTaskCreate(client_data_sending_task, "ClientDataSending", 4096, NULL, 1, diff --git a/main/uart_handler.c b/main/uart_handler.c index 2859911..67bcc69 100644 --- a/main/uart_handler.c +++ b/main/uart_handler.c @@ -5,6 +5,8 @@ #include "hal/uart_types.h" #include "nvs_flash.h" #include "portmacro.h" +#include +#include #include "uart_handler.h" @@ -45,3 +47,10 @@ void uart_status_task(void *param) { vTaskDelay(1000 / portTICK_PERIOD_MS); } } + +void send_client_info(int clientid, bool isAvailable, TickType_t lastPing) { + char buf[128]; + snprintf(buf, sizeof(buf), "c%d,status,2,%d,%u\r\n", clientid, + isAvailable ? 1 : 0, (unsigned int)lastPing); + uart_write_bytes(MASTER_UART, buf, strlen(buf)); +} diff --git a/main/uart_handler.h b/main/uart_handler.h index bd2e403..8e01578 100644 --- a/main/uart_handler.h +++ b/main/uart_handler.h @@ -11,4 +11,6 @@ void init_uart(); void uart_read_task(void *param); void uart_status_task(void *param); +void send_client_info(int clientid, bool isAvailable, TickType_t lastPing); + #endif