modules/time_persistor: new time persistor module to save time to persistent storage (#23760)

This commit is contained in:
Alexander Lerach 2024-10-04 21:44:49 +02:00 committed by GitHub
parent 652bb82603
commit 4215e20120
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 293 additions and 0 deletions

View File

@ -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
)

View File

@ -0,0 +1,5 @@
menuconfig MODULES_TIME_PERSISTOR
bool "time_persistor"
default n
---help---
Enable support for time_persistor

View File

@ -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 <errno.h>
#include <px4_platform_common/log.h>
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;
}

View File

@ -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 <px4_platform_common/module.h>
#include <px4_platform_common/module_params.h>
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
#include <px4_platform_common/time.h>
static constexpr const auto TIME_FILE_PATH = PX4_STORAGEDIR "/time_save.bin";
using namespace time_literals;
class TimePersistor : public ModuleBase<TimePersistor>, 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;
};