diff --git a/esp/speaker-netsender/main/CMakeLists.txt b/esp/speaker-netsender/main/CMakeLists.txt index 728062e..c82750f 100755 --- a/esp/speaker-netsender/main/CMakeLists.txt +++ b/esp/speaker-netsender/main/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "main.cpp" "audio.cpp" "ethernet.cpp" "sd.cpp" "self_check.cpp" +idf_component_register(SRCS "main.cpp" "audio.cpp" "ethernet.cpp" "sd.cpp" "self_check.cpp" "time.cpp" INCLUDE_DIRS "." "include" REQUIRES esp_driver_sdspi esp_eth esp_netif fatfs sdmmc driver tas5805 netsender ota_tcp pipi) diff --git a/esp/speaker-netsender/main/include/time.hpp b/esp/speaker-netsender/main/include/time.hpp new file mode 100644 index 0000000..4581e53 --- /dev/null +++ b/esp/speaker-netsender/main/include/time.hpp @@ -0,0 +1,29 @@ +/* + Name: + time.hpp - Time management utilities and helpers. + + Authors: + David Sutton + + License: + Copyright (C) 2026 The Australian Ocean Lab (AusOcean). + + This file is part of NetSender. NetSender is free software: you can + redistribute it and/or modify it under the terms of the GNU + General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) + any later version. + + NetSender is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NetSender in gpl.txt. If not, see + . +*/ + +// Initialises network time syncing. +// NOTE: All times are handled in UTC. +void initialise_sntp(); diff --git a/esp/speaker-netsender/main/main.cpp b/esp/speaker-netsender/main/main.cpp index aa02be2..6d92bc1 100644 --- a/esp/speaker-netsender/main/main.cpp +++ b/esp/speaker-netsender/main/main.cpp @@ -40,10 +40,10 @@ #include "include/audio.hpp" #include "include/ethernet.hpp" #include "include/globals.h" -#include "include/log.hpp" #include "include/netsender_vars.hpp" #include "include/sd.hpp" #include "include/self_check.hpp" +#include "include/time.hpp" #include "netsender.hpp" #include "ota_tcp.hpp" #include "tas5805.hpp" @@ -126,10 +126,6 @@ void app_main(void) init_ethernet(); ESP_LOGI(TAG, "Ethernet initialised"); - ESP_LOGI(TAG, "Initialising UDP Logging"); - init_udp_logging(); - ESP_LOGI(TAG, "Logging Initialised"); - ESP_LOGI(TAG, "Initialising SD card"); init_sd(); ESP_LOGI(TAG, "SD initialised"); @@ -142,6 +138,10 @@ void app_main(void) const auto valid_partition = self_check_ok(&); ota_tcp_validate_partition(valid_partition); + ESP_LOGI(TAG, "Initialising SNTP"); + initialise_sntp(); + ESP_LOGI(TAG, "SNTP Initialised"); + ESP_LOGI(TAG, "Initialising OTA listener"); init_ota_tcp(); ESP_LOGI(TAG, "OTA Initialised"); diff --git a/esp/speaker-netsender/main/time.cpp b/esp/speaker-netsender/main/time.cpp new file mode 100644 index 0000000..14fcfbc --- /dev/null +++ b/esp/speaker-netsender/main/time.cpp @@ -0,0 +1,59 @@ +/* + Name: + time.cpp - Time management utilities and helpers. + + Authors: + David Sutton + + License: + Copyright (C) 2026 The Australian Ocean Lab (AusOcean). + + This file is part of NetSender. NetSender is free software: you can + redistribute it and/or modify it under the terms of the GNU + General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) + any later version. + + NetSender is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NetSender in gpl.txt. If not, see + . +*/ + +// Logging TAG. +#include "include/time.hpp" + +#include + +#include "esp_err.h" +#include "esp_log.h" +#include "esp_netif_sntp.h" + +// Logging TAG. +static constexpr auto TAG = "time"; + +void initialise_sntp() +{ + esp_sntp_config_t config = ESP_NETIF_SNTP_DEFAULT_CONFIG("pool.ntp.org"); + auto err = esp_netif_sntp_init(&config); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize SNTP (%s)", esp_err_to_name(err)); + return; + } + + auto retry = 0; + const auto max_retries = 10; + while ((err = esp_netif_sntp_sync_wait(pdMS_TO_TICKS(2000))) != ESP_OK && ++retry < max_retries) { + ESP_LOGI(TAG, "Attempt to synchronise time (%d/%d) failed: %s", retry, max_retries, esp_err_to_name(err)); + } + + if (retry >= max_retries) { + ESP_LOGE(TAG, "Failed to synchronize time within timeout!"); + } else { + ESP_LOGI(TAG, "System time set successfully"); + } +} diff --git a/esp/speaker-netsender/sdkconfig b/esp/speaker-netsender/sdkconfig index f26f51a..630b1ef 100644 --- a/esp/speaker-netsender/sdkconfig +++ b/esp/speaker-netsender/sdkconfig @@ -1898,12 +1898,9 @@ CONFIG_LOG_TAG_LEVEL_IMPL_CACHE_SIZE=31 # # Format # -# default: # CONFIG_LOG_COLORS is not set -# default: -CONFIG_LOG_TIMESTAMP_SOURCE_RTOS=y -# default: -# CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM is not set +# CONFIG_LOG_TIMESTAMP_SOURCE_RTOS is not set +CONFIG_LOG_TIMESTAMP_SOURCE_SYSTEM=y # end of Format #