mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-01 19:07:45 +08:00
parameters: open export files O_TRUNC to discard previous data
- rcS parameter backup try to directly restore param (FRAM) from backup (in case SD card is removed before successful export) - rcS parameter backup logging rearrange to capture more logging output (param_import_fail.txt) - posix rcS try to keep param backup and restore roughly in sync with NuttX rcS - tinybson fix debug printf format - param_export_internal ensure file descriptor positioned at 0 (precaution)
This commit is contained in:
@@ -62,18 +62,46 @@ fi
|
|||||||
|
|
||||||
# Load parameters
|
# Load parameters
|
||||||
set PARAM_FILE parameters.bson
|
set PARAM_FILE parameters.bson
|
||||||
param select $PARAM_FILE
|
set PARAM_BACKUP_FILE parameters_backup.bson
|
||||||
|
|
||||||
if [ -f $PARAM_FILE ]
|
param select $PARAM_FILE
|
||||||
then
|
if [ -f $PARAM_FILE ]; then
|
||||||
if param load
|
set +e # disable exit on error control for param import
|
||||||
|
if ! param import
|
||||||
then
|
then
|
||||||
echo "[param] Loaded: $PARAM_FILE"
|
echo "ERROR [init] param import failed"
|
||||||
else
|
|
||||||
echo "[param] FAILED loading $PARAM_FILE"
|
param dump $PARAM_FILE
|
||||||
|
|
||||||
|
# try to make a backup copy
|
||||||
|
cp $PARAM_FILE param_import_fail.bson
|
||||||
|
|
||||||
|
# try importing from backup file
|
||||||
|
if [ -f $PARAM_BACKUP_FILE ]
|
||||||
|
then
|
||||||
|
echo "[init] importing from parameter backup"
|
||||||
|
|
||||||
|
# dump current backup file contents for comparison
|
||||||
|
param dump $PARAM_BACKUP_FILE
|
||||||
|
|
||||||
|
param import $PARAM_BACKUP_FILE
|
||||||
|
|
||||||
|
# overwrite invalid $PARAM_FILE with backup
|
||||||
|
cp $PARAM_BACKUP_FILE $PARAM_FILE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
param status
|
||||||
|
fi
|
||||||
|
set -e # restore exit on error control
|
||||||
|
|
||||||
|
elif [ -f $PARAM_BACKUP_FILE ]; then
|
||||||
|
echo "ERROR [init] primary param file $PARAM_FILE unavailable, using backup $PARAM_BACKUP_FILE"
|
||||||
|
param import $PARAM_BACKUP_FILE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
param select-backup $PARAM_BACKUP_FILE
|
||||||
|
|
||||||
|
|
||||||
# exit early when the minimal shell is requested
|
# exit early when the minimal shell is requested
|
||||||
[ $RUN_MINIMAL_SHELL = yes ] && exit 0
|
[ $RUN_MINIMAL_SHELL = yes ] && exit 0
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ set IOFW "/etc/extras/px4_io-v2_default.bin"
|
|||||||
set LOGGER_ARGS ""
|
set LOGGER_ARGS ""
|
||||||
set LOGGER_BUF 8
|
set LOGGER_BUF 8
|
||||||
set PARAM_FILE ""
|
set PARAM_FILE ""
|
||||||
|
set PARAM_BACKUP_FILE ""
|
||||||
set RC_INPUT_ARGS ""
|
set RC_INPUT_ARGS ""
|
||||||
set SDCARD_AVAILABLE no
|
set SDCARD_AVAILABLE no
|
||||||
set SDCARD_EXT_PATH /fs/microsd/ext_autostart
|
set SDCARD_EXT_PATH /fs/microsd/ext_autostart
|
||||||
@@ -93,6 +94,7 @@ then
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
set PARAM_FILE /fs/microsd/params
|
set PARAM_FILE /fs/microsd/params
|
||||||
|
set PARAM_BACKUP_FILE "/fs/microsd/parameters_backup.bson"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -132,27 +134,32 @@ else
|
|||||||
|
|
||||||
if [ -d "/fs/microsd" ]
|
if [ -d "/fs/microsd" ]
|
||||||
then
|
then
|
||||||
dmesg >> /fs/microsd/param_import_fail.txt &
|
|
||||||
|
|
||||||
# try to make a backup copy
|
# try to make a backup copy
|
||||||
cp $PARAM_FILE /fs/microsd/param_import_fail.bson &
|
cp $PARAM_FILE /fs/microsd/param_import_fail.bson
|
||||||
fi
|
|
||||||
|
|
||||||
# try importing from backup file
|
# try importing from backup file
|
||||||
if [ -f "/fs/microsd/parameters_backup.bson" ]
|
if [ -f $PARAM_BACKUP_FILE ]
|
||||||
then
|
then
|
||||||
echo "[init] importing from parameter backup"
|
echo "[init] importing from parameter backup"
|
||||||
|
|
||||||
# dump current backup file contents for comparison
|
# dump current backup file contents for comparison
|
||||||
param dump /fs/microsd/parameters_backup.bson
|
param dump $PARAM_BACKUP_FILE
|
||||||
|
|
||||||
param import /fs/microsd/parameters_backup.bson
|
param import $PARAM_BACKUP_FILE
|
||||||
|
|
||||||
|
# overwrite invalid $PARAM_FILE with backup
|
||||||
|
cp $PARAM_BACKUP_FILE $PARAM_FILE
|
||||||
|
fi
|
||||||
|
|
||||||
|
param status
|
||||||
|
|
||||||
|
dmesg >> /fs/microsd/param_import_fail.txt &
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ $SDCARD_AVAILABLE = yes ]
|
if [ $SDCARD_AVAILABLE = yes ]
|
||||||
then
|
then
|
||||||
param select-backup /fs/microsd/parameters_backup.bson
|
param select-backup $PARAM_BACKUP_FILE
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if ver hwcmp PX4_FMU_V5X PX4_FMU_V6X
|
if ver hwcmp PX4_FMU_V5X PX4_FMU_V6X
|
||||||
@@ -534,6 +541,7 @@ unset IOFW
|
|||||||
unset LOGGER_ARGS
|
unset LOGGER_ARGS
|
||||||
unset LOGGER_BUF
|
unset LOGGER_BUF
|
||||||
unset PARAM_FILE
|
unset PARAM_FILE
|
||||||
|
unset PARAM_BACKUP_FILE
|
||||||
unset RC_INPUT_ARGS
|
unset RC_INPUT_ARGS
|
||||||
unset SDCARD_AVAILABLE
|
unset SDCARD_AVAILABLE
|
||||||
unset SDCARD_EXT_PATH
|
unset SDCARD_EXT_PATH
|
||||||
|
|||||||
@@ -1116,7 +1116,7 @@ int param_save_default()
|
|||||||
|
|
||||||
for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
for (int attempt = 1; attempt <= MAX_ATTEMPTS; attempt++) {
|
||||||
// write parameters to file
|
// write parameters to file
|
||||||
int fd = ::open(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);
|
int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, PX4_O_MODE_666);
|
||||||
|
|
||||||
if (fd > -1) {
|
if (fd > -1) {
|
||||||
perf_begin(param_export_perf);
|
perf_begin(param_export_perf);
|
||||||
@@ -1155,7 +1155,7 @@ int param_save_default()
|
|||||||
|
|
||||||
// backup file
|
// backup file
|
||||||
if (param_backup_file) {
|
if (param_backup_file) {
|
||||||
int fd_backup_file = ::open(param_backup_file, O_WRONLY | O_CREAT, PX4_O_MODE_666);
|
int fd_backup_file = ::open(param_backup_file, O_WRONLY | O_CREAT | O_TRUNC, PX4_O_MODE_666);
|
||||||
|
|
||||||
if (fd_backup_file > -1) {
|
if (fd_backup_file > -1) {
|
||||||
int backup_export_ret = param_export_internal(fd_backup_file, nullptr);
|
int backup_export_ret = param_export_internal(fd_backup_file, nullptr);
|
||||||
@@ -1374,6 +1374,11 @@ static int param_export_internal(int fd, param_filter_func filter)
|
|||||||
bson_encoder_s encoder{};
|
bson_encoder_s encoder{};
|
||||||
uint8_t bson_buffer[256];
|
uint8_t bson_buffer[256];
|
||||||
|
|
||||||
|
if (lseek(fd, 0, SEEK_SET) != 0) {
|
||||||
|
PX4_ERR("export seek failed %d", errno);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
if (bson_encoder_init_buf_file(&encoder, fd, &bson_buffer, sizeof(bson_buffer)) != 0) {
|
if (bson_encoder_init_buf_file(&encoder, fd, &bson_buffer, sizeof(bson_buffer)) != 0) {
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -147,7 +147,7 @@ bson_decoder_init_buf(bson_decoder_t decoder, void *buf, unsigned bufsize, bson_
|
|||||||
|
|
||||||
if (bufsize == 0) {
|
if (bufsize == 0) {
|
||||||
decoder->bufsize = *(uint32_t *)buf;
|
decoder->bufsize = *(uint32_t *)buf;
|
||||||
debug("auto-detected %u byte object", decoder->bufsize);
|
debug("auto-detected %zu byte object", decoder->bufsize);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
decoder->bufsize = bufsize;
|
decoder->bufsize = bufsize;
|
||||||
@@ -403,7 +403,7 @@ write_x(bson_encoder_t encoder, const void *p, size_t s)
|
|||||||
|
|
||||||
memcpy(encoder->buf + encoder->bufpos, p, s);
|
memcpy(encoder->buf + encoder->bufpos, p, s);
|
||||||
encoder->bufpos += s;
|
encoder->bufpos += s;
|
||||||
debug("appended %d bytes", s);
|
debug("appended %zu bytes", s);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user