aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Meier <lm@inf.ethz.ch>2013-10-23 02:59:43 -0700
committerLorenz Meier <lm@inf.ethz.ch>2013-10-23 02:59:43 -0700
commit030164d6b0b13d7e032e50bbe1eb4252b6be88a3 (patch)
tree44d808ffd9e58124334aff992cac85abbe23eabd
parentfa43eee47e07aee3517dae31b45ed3b05678ff94 (diff)
parent2f66a8894f1f8035bfc076306aa0d83197be108a (diff)
downloadpx4-firmware-030164d6b0b13d7e032e50bbe1eb4252b6be88a3.tar.gz
px4-firmware-030164d6b0b13d7e032e50bbe1eb4252b6be88a3.tar.bz2
px4-firmware-030164d6b0b13d7e032e50bbe1eb4252b6be88a3.zip
Merge pull request #486 from PX4/param_save_fix
Parameter saving fixed
-rw-r--r--src/modules/commander/commander.cpp4
-rw-r--r--src/modules/systemlib/param/param.c85
-rw-r--r--src/modules/systemlib/rc_check.c4
-rw-r--r--src/modules/systemlib/rc_check.h2
-rw-r--r--src/systemcmds/preflight_check/preflight_check.c4
5 files changed, 48 insertions, 51 deletions
diff --git a/src/modules/commander/commander.cpp b/src/modules/commander/commander.cpp
index 2ef509980..db758c386 100644
--- a/src/modules/commander/commander.cpp
+++ b/src/modules/commander/commander.cpp
@@ -687,7 +687,7 @@ int commander_thread_main(int argc, char *argv[])
bool updated = false;
- bool rc_calibration_ok = (OK == rc_calibration_check());
+ bool rc_calibration_ok = (OK == rc_calibration_check(mavlink_fd));
/* Subscribe to safety topic */
int safety_sub = orb_subscribe(ORB_ID(safety));
@@ -802,7 +802,7 @@ int commander_thread_main(int argc, char *argv[])
status_changed = true;
/* re-check RC calibration */
- rc_calibration_ok = (OK == rc_calibration_check());
+ rc_calibration_ok = (OK == rc_calibration_check(mavlink_fd));
/* navigation parameters */
param_get(_param_takeoff_alt, &takeoff_alt);
diff --git a/src/modules/systemlib/param/param.c b/src/modules/systemlib/param/param.c
index ccdb2ea38..398657dd7 100644
--- a/src/modules/systemlib/param/param.c
+++ b/src/modules/systemlib/param/param.c
@@ -508,64 +508,63 @@ param_get_default_file(void)
int
param_save_default(void)
{
- int result;
- unsigned retries = 0;
-
- /* delete the file in case it exists */
- struct stat buffer;
- if (stat(param_get_default_file(), &buffer) == 0) {
-
- do {
- result = unlink(param_get_default_file());
- if (result != 0) {
- retries++;
- usleep(1000 * retries);
- }
- } while (result != OK && retries < 10);
+ int res;
+ int fd;
- if (result != OK)
- warnx("unlinking file %s failed.", param_get_default_file());
- }
+ const char *filename = param_get_default_file();
+ const char *filename_tmp = malloc(strlen(filename) + 5);
+ sprintf(filename_tmp, "%s.tmp", filename);
- /* create the file */
- int fd;
+ /* delete temp file if exist */
+ res = unlink(filename_tmp);
+
+ if (res != OK && errno == ENOENT)
+ res = OK;
+
+ if (res != OK)
+ warn("failed to delete temp file: %s", filename_tmp);
+
+ if (res == OK) {
+ /* write parameters to temp file */
+ fd = open(filename_tmp, O_WRONLY | O_CREAT | O_EXCL);
- do {
- /* do another attempt in case the unlink call is not synced yet */
- fd = open(param_get_default_file(), O_WRONLY | O_CREAT | O_EXCL);
if (fd < 0) {
- retries++;
- usleep(1000 * retries);
+ warn("failed to open temp file: %s", filename_tmp);
+ res = ERROR;
}
- } while (fd < 0 && retries < 10);
+ if (res == OK) {
+ res = param_export(fd, false);
- if (fd < 0) {
-
- warn("opening '%s' for writing failed", param_get_default_file());
- return fd;
- }
+ if (res != OK)
+ warnx("failed to write parameters to file: %s", filename_tmp);
+ }
- do {
- result = param_export(fd, false);
+ close(fd);
+ }
- if (result != OK) {
- retries++;
- usleep(1000 * retries);
- }
+ if (res == OK) {
+ /* delete parameters file */
+ res = unlink(filename);
- } while (result != 0 && retries < 10);
+ if (res != OK && errno == ENOENT)
+ res = OK;
+ if (res != OK)
+ warn("failed to delete parameters file: %s", filename);
+ }
- close(fd);
+ if (res == OK) {
+ /* rename temp file to parameters */
+ res = rename(filename_tmp, filename);
- if (result != OK) {
- warn("error exporting parameters to '%s'", param_get_default_file());
- (void)unlink(param_get_default_file());
- return result;
+ if (res != OK)
+ warn("failed to rename %s to %s", filename_tmp, filename);
}
- return 0;
+ free(filename_tmp);
+
+ return res;
}
/**
diff --git a/src/modules/systemlib/rc_check.c b/src/modules/systemlib/rc_check.c
index 60d6473b8..b4350cc24 100644
--- a/src/modules/systemlib/rc_check.c
+++ b/src/modules/systemlib/rc_check.c
@@ -47,14 +47,12 @@
#include <mavlink/mavlink_log.h>
#include <uORB/topics/rc_channels.h>
-int rc_calibration_check(void) {
+int rc_calibration_check(int mavlink_fd) {
char nbuf[20];
param_t _parameter_handles_min, _parameter_handles_trim, _parameter_handles_max,
_parameter_handles_rev, _parameter_handles_dz;
- int mavlink_fd = open(MAVLINK_LOG_DEVICE, 0);
-
float param_min, param_max, param_trim, param_rev, param_dz;
/* first check channel mappings */
diff --git a/src/modules/systemlib/rc_check.h b/src/modules/systemlib/rc_check.h
index e2238d151..e70b83cce 100644
--- a/src/modules/systemlib/rc_check.h
+++ b/src/modules/systemlib/rc_check.h
@@ -47,6 +47,6 @@
* @return 0 / OK if RC calibration is ok, index + 1 of the first
* channel that failed else (so 1 == first channel failed)
*/
-__EXPORT int rc_calibration_check(void);
+__EXPORT int rc_calibration_check(int mavlink_fd);
__END_DECLS
diff --git a/src/systemcmds/preflight_check/preflight_check.c b/src/systemcmds/preflight_check/preflight_check.c
index e9c5f1a2c..1c58a2db6 100644
--- a/src/systemcmds/preflight_check/preflight_check.c
+++ b/src/systemcmds/preflight_check/preflight_check.c
@@ -140,7 +140,7 @@ int preflight_check_main(int argc, char *argv[])
/* ---- RC CALIBRATION ---- */
- bool rc_ok = (OK == rc_calibration_check());
+ bool rc_ok = (OK == rc_calibration_check(mavlink_fd));
/* warn */
if (!rc_ok)
@@ -227,4 +227,4 @@ static int led_off(int leds, int led)
static int led_on(int leds, int led)
{
return ioctl(leds, LED_ON, led);
-} \ No newline at end of file
+}