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:
Daniel Agar
2022-09-09 16:20:16 -04:00
parent 296b1704c5
commit 3f3a5f19f0
6 changed files with 69 additions and 28 deletions
@@ -7,7 +7,7 @@ param set-default IMU_INTEG_RATE 250
if [ "$PX4_SIMULATOR" = "sihsim" ] || [ "$(param show -q SYS_AUTOSTART)" -eq "0" ]; then if [ "$PX4_SIMULATOR" = "sihsim" ] || [ "$(param show -q SYS_AUTOSTART)" -eq "0" ]; then
if ! simulator_sih start; then if ! simulator_sih start; then
echo "ERROR [init] simulator_sih failed to start" echo "ERROR [init] simulator_sih failed to start"
exit 1 exit 1
fi fi
@@ -46,7 +46,7 @@ elif [ "$PX4_SIMULATOR" = "ignition" ]; then
sensor_gps_sim start sensor_gps_sim start
sensor_mag_sim start sensor_mag_sim start
else else
echo "ERROR [init] ign gazebo failed to start" echo "ERROR [init] ign gazebo failed to start"
exit 1 exit 1
fi fi
else else
@@ -61,7 +61,7 @@ elif [ "$PX4_SIMULATOR" = "ignition" ]; then
sensor_gps_sim start sensor_gps_sim start
sensor_mag_sim start sensor_mag_sim start
else else
echo "ERROR [init] ign gazebo failed to start" echo "ERROR [init] ign gazebo failed to start"
exit 1 exit 1
fi fi
+36 -8
View File
@@ -52,7 +52,7 @@ then
# shellcheck disable=SC2012 # shellcheck disable=SC2012
REQUESTED_AUTOSTART=$(ls "${R}etc/init.d-posix/airframes" | sed -n 's/^\([0-9][0-9]*\)_'${PX4_SIM_MODEL}'$/\1/p') REQUESTED_AUTOSTART=$(ls "${R}etc/init.d-posix/airframes" | sed -n 's/^\([0-9][0-9]*\)_'${PX4_SIM_MODEL}'$/\1/p')
if [ -z "$REQUESTED_AUTOSTART" ]; then if [ -z "$REQUESTED_AUTOSTART" ]; then
echo "ERROR [init] Unknown model $PX4_SIM_MODEL (not found by name on ${R}etc/init.d-posix/airframes)" echo "ERROR [init] Unknown model $PX4_SIM_MODEL (not found by name on ${R}etc/init.d-posix/airframes)"
exit 1 exit 1
else else
SYS_AUTOSTART=$REQUESTED_AUTOSTART SYS_AUTOSTART=$REQUESTED_AUTOSTART
@@ -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
param status
fi 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
+20 -12
View File
@@ -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
+1 -1
View File
@@ -78,7 +78,7 @@ class RCOutput():
result += "\t. /etc/init.d/airframes/${AIRFRAME}\n" result += "\t. /etc/init.d/airframes/${AIRFRAME}\n"
if not post_start: if not post_start:
result += "else\n" result += "else\n"
result += "\techo \"ERROR [init] No file matches SYS_AUTOSTART value found in : /etc/init.d/airframes\"\n" result += "\techo \"ERROR [init] No file matches SYS_AUTOSTART value found in : /etc/init.d/airframes\"\n"
# Reset the configuration # Reset the configuration
result += "\tparam set SYS_AUTOSTART 0\n" result += "\tparam set SYS_AUTOSTART 0\n"
result += "\ttone_alarm ${TUNE_ERR}\n" result += "\ttone_alarm ${TUNE_ERR}\n"
+7 -2
View File
@@ -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;
} }
+2 -2
View File
@@ -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;
} }