From 4215e20120706221ce6f4cd0fab332a55f3bbeac Mon Sep 17 00:00:00 2001 From: Alexander Lerach Date: Fri, 4 Oct 2024 21:44:49 +0200 Subject: [PATCH] modules/time_persistor: new time persistor module to save time to persistent storage (#23760) --- src/modules/time_persistor/CMakeLists.txt | 43 +++++ src/modules/time_persistor/Kconfig | 5 + src/modules/time_persistor/TimePersistor.cpp | 174 +++++++++++++++++++ src/modules/time_persistor/TimePersistor.hpp | 71 ++++++++ 4 files changed, 293 insertions(+) create mode 100644 src/modules/time_persistor/CMakeLists.txt create mode 100644 src/modules/time_persistor/Kconfig create mode 100644 src/modules/time_persistor/TimePersistor.cpp create mode 100644 src/modules/time_persistor/TimePersistor.hpp diff --git a/src/modules/time_persistor/CMakeLists.txt b/src/modules/time_persistor/CMakeLists.txt new file mode 100644 index 000000000000..add51551b403 --- /dev/null +++ b/src/modules/time_persistor/CMakeLists.txt @@ -0,0 +1,43 @@ +############################################################################ +# +# Copyright (c) 2024 PX4 Development Team. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name PX4 nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +px4_add_module( + MODULE modules__time_persistor + MAIN time_persistor + COMPILE_FLAGS + SRCS + TimePersistor.cpp + TimePersistor.hpp + DEPENDS + px4_work_queue + ) diff --git a/src/modules/time_persistor/Kconfig b/src/modules/time_persistor/Kconfig new file mode 100644 index 000000000000..da75222f49d3 --- /dev/null +++ b/src/modules/time_persistor/Kconfig @@ -0,0 +1,5 @@ +menuconfig MODULES_TIME_PERSISTOR + bool "time_persistor" + default n + ---help--- + Enable support for time_persistor diff --git a/src/modules/time_persistor/TimePersistor.cpp b/src/modules/time_persistor/TimePersistor.cpp new file mode 100644 index 000000000000..92d310c14c0b --- /dev/null +++ b/src/modules/time_persistor/TimePersistor.cpp @@ -0,0 +1,174 @@ +/**************************************************************************** + * + * Copyright (c) 2024 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#include "TimePersistor.hpp" + +#include +#include + +TimePersistor::TimePersistor() : + ModuleParams(nullptr), + ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::lp_default) +{ +} + +TimePersistor::~TimePersistor() +{ + fclose(_file); +} + +void TimePersistor::start() +{ + ScheduleOnInterval(1_s); +} + +void TimePersistor::Run() +{ + if (should_exit()) { + ScheduleClear(); + exit_and_cleanup(); + return; + } + + if (!_file) { + if (init() != PX4_OK) { + ScheduleClear(); + exit_and_cleanup(); + return; + } + } + + struct timespec ts; + + px4_clock_gettime(CLOCK_REALTIME, &ts); + + if (write_time(ts.tv_sec) != PX4_OK) { + PX4_ERR("error writing RTC to time file"); + exit_and_cleanup(); + return; + } +} + +int TimePersistor::task_spawn(int argc, char *argv[]) +{ + TimePersistor *instance = new TimePersistor(); + + if (!instance) { + PX4_ERR("alloc failed"); + return PX4_ERROR; + } + + _object.store(instance); + _task_id = task_id_is_work_queue; + + instance->start(); + return PX4_OK; +} + +int TimePersistor::custom_command(int argc, char *argv[]) +{ + return print_usage("unknown command"); +} + +int TimePersistor::print_usage(const char *reason) +{ + if (reason) { + PX4_WARN("%s\n", reason); + } + + PRINT_MODULE_DESCRIPTION( + R"DESCR_STR( +### Description +Writes the RTC time cyclically to a file and reloads this value on startup. +This allows monotonic time on systems that only have a software RTC (that is not battery powered). +Explicitly setting the time backwards (e.g. via system_time) is still possible. + +)DESCR_STR"); + + PRINT_MODULE_USAGE_NAME("time_persistor", "system"); + PRINT_MODULE_USAGE_COMMAND("start"); + PRINT_MODULE_USAGE_DEFAULT_COMMANDS(); + + return 0; +} + +extern "C" __EXPORT int time_persistor_main(int argc, char *argv[]) +{ + return TimePersistor::main(argc, argv); +} + +int TimePersistor::init() +{ + if ((_file = fopen(TIME_FILE_PATH, "r+")) == nullptr) { + _file = fopen(TIME_FILE_PATH, "w+"); + } + + if (!_file) { + PX4_ERR("error opening the time file"); + return PX4_ERROR; + } + + time_t t; + + if (read_time(&t) == PX4_OK) { + struct timespec ts; + ts.tv_sec = t; + ts.tv_nsec = 0; + + px4_clock_settime(CLOCK_REALTIME, &ts); + } + + return PX4_OK; +} + +int TimePersistor::read_time(time_t *time) +{ + int status = PX4_OK; + + status |= fseek(_file, 0, SEEK_SET); + status |= fread(time, sizeof(time_t), 1, _file) != 1; + + return status; +} + +int TimePersistor::write_time(const time_t time) +{ + int status = PX4_OK; + + status |= fseek(_file, 0, SEEK_SET); + status |= fwrite(&time, sizeof(time_t), 1, _file) != 1; + status |= fflush(_file); + status |= fsync(fileno(_file)); + + return status; +} diff --git a/src/modules/time_persistor/TimePersistor.hpp b/src/modules/time_persistor/TimePersistor.hpp new file mode 100644 index 000000000000..8aeef085b51e --- /dev/null +++ b/src/modules/time_persistor/TimePersistor.hpp @@ -0,0 +1,71 @@ +/**************************************************************************** + * + * Copyright (c) 2024 PX4 Development Team. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#pragma once + +#include +#include +#include +#include + +static constexpr const auto TIME_FILE_PATH = PX4_STORAGEDIR "/time_save.bin"; + +using namespace time_literals; + +class TimePersistor : public ModuleBase, public ModuleParams, public px4::ScheduledWorkItem +{ +public: + TimePersistor(); + ~TimePersistor() override; + + /** @see ModuleBase */ + static int task_spawn(int argc, char *argv[]); + + /** @see ModuleBase */ + static int custom_command(int argc, char *argv[]); + + /** @see ModuleBase */ + static int print_usage(const char *reason = nullptr); + + /** @see ModuleBase::run() */ + void Run() override; + + void start(); + +private: + int init(); + int read_time(time_t *time); + int write_time(const time_t time); + + FILE *_file = 0; +};