diff --git a/main/app_networking.cpp b/main/app_networking.cpp index 73bee9b..1b865d2 100644 --- a/main/app_networking.cpp +++ b/main/app_networking.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "esp_event.h" #include "esp_mac.h" @@ -26,7 +27,7 @@ std::variant s_peer; bool s_isBroadcastAddress(const std::uint8_t* mac) { - constexpr MacAddress broadcast_mac{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + constexpr MacAddress broadcast_mac{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; return std::memcmp(mac, broadcast_mac.begin(), ESP_NOW_ETH_ALEN) == 0; } @@ -35,6 +36,11 @@ bool s_isBroadcastAddress(const esp_now_recv_info_t* sender) return s_isBroadcastAddress(sender->src_addr); } +constexpr MacAddress s_getBroadcastAddress() +{ + return MacAddress{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +} + esp_err_t s_addPeer(const MacAddress& peer_address) { esp_now_peer_info_t broadcast_peer; @@ -96,7 +102,7 @@ QueueHandle_t setupEspNow() ESP_ERROR_CHECK(esp_now_register_send_cb(s_cbEspNowSendComplete)); ESP_ERROR_CHECK(esp_now_register_recv_cb(s_cbEspNowReceiveComplete)); - const auto broadcast_mac = MacAddress{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; + const auto broadcast_mac = s_getBroadcastAddress(); s_peer = broadcast_mac; ESP_ERROR_CHECK(s_addPeer(broadcast_mac)); @@ -104,6 +110,19 @@ QueueHandle_t setupEspNow() return s_esp_now_queue; } +void sendHello() +{ + MacAddress mac; + ESP_ERROR_CHECK(esp_read_mac(mac.data(), ESP_MAC_WIFI_STA)); + + char buffer[100]; + const std::size_t length = std::sprintf(buffer, "Hello from %02X:%02X:%02X:%02X:%02X:%02X.\n\r", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + const auto broadcast_mac = s_getBroadcastAddress(); + ESP_ERROR_CHECK(esp_now_send(broadcast_mac.data(), (uint8_t*)buffer, length)); +} + const std::array& getRxBuffer() { return s_rx_buffer; diff --git a/main/app_networking.hpp b/main/app_networking.hpp index 3c68824..1ad9c4e 100644 --- a/main/app_networking.hpp +++ b/main/app_networking.hpp @@ -26,6 +26,7 @@ using MacAddress = std::array; void setupWifi(); [[nodiscard]] QueueHandle_t setupEspNow(); +void sendHello(); void sendData(const std::array& buffer, const std::size_t length); const std::array& getRxBuffer(); diff --git a/main/esp_tunnel.cpp b/main/esp_tunnel.cpp index d0c75b2..8cc0175 100644 --- a/main/esp_tunnel.cpp +++ b/main/esp_tunnel.cpp @@ -151,6 +151,9 @@ extern "C" void app_main(void) xQueueAddToSet(s_esp_now_queue, s_queue_set); xQueueAddToSet(s_uart_queue, s_queue_set); - ESP_LOGI(LOG_TAG, "Setup completed."); + ESP_LOGI(LOG_TAG, "Setup completed. Sending hello."); + + Networking::sendHello(); + xTaskCreate(s_mainTask, "main_task", 2048, nullptr, 4, &s_main_task); }