New param interface for microSD and EEPROM

This commit is contained in:
Lorenz Meier 2012-10-31 16:31:21 +01:00
parent 8dcde7f8cd
commit 0ddfd7c75c
5 changed files with 141 additions and 108 deletions

View File

@ -268,33 +268,6 @@ void tune_confirm() {
ioctl(buzzer, TONE_SET_ALARM, 3);
}
static const char *parameter_file = "/eeprom/parameters";
static int pm_save_eeprom(bool only_unsaved)
{
/* delete the file in case it exists */
unlink(parameter_file);
/* create the file */
int fd = open(parameter_file, O_WRONLY | O_CREAT | O_EXCL);
if (fd < 0) {
warn("opening '%s' for writing failed", parameter_file);
return -1;
}
int result = param_export(fd, only_unsaved);
close(fd);
if (result != 0) {
unlink(parameter_file);
warn("error exporting parameters to '%s'", parameter_file);
return -2;
}
return 0;
}
void do_mag_calibration(int status_pub, struct vehicle_status_s *status)
{
/* set to mag calibration mode */
@ -496,9 +469,9 @@ void do_mag_calibration(int status_pub, struct vehicle_status_s *status)
}
/* auto-save to EEPROM */
int save_ret = pm_save_eeprom(false);
int save_ret = param_save_default();
if(save_ret != 0) {
warn("WARNING: auto-save of params to EEPROM failed");
warn("WARNING: auto-save of params to storage failed");
}
printf("[mag cal]\tscale: %.6f %.6f %.6f\n \toffset: %.6f %.6f %.6f\nradius: %.6f GA\n",
@ -616,9 +589,9 @@ void do_gyro_calibration(int status_pub, struct vehicle_status_s *status)
close(fd);
/* auto-save to EEPROM */
int save_ret = pm_save_eeprom(false);
int save_ret = param_save_default();
if(save_ret != 0) {
warn("WARNING: auto-save of params to EEPROM failed");
warn("WARNING: auto-save of params to storage failed");
}
// char buf[50];
@ -736,9 +709,9 @@ void do_accel_calibration(int status_pub, struct vehicle_status_s *status)
close(fd);
/* auto-save to EEPROM */
int save_ret = pm_save_eeprom(false);
int save_ret = param_save_default();
if(save_ret != 0) {
warn("WARNING: auto-save of params to EEPROM failed");
warn("WARNING: auto-save of params to storage failed");
}
//char buf[50];

View File

@ -172,64 +172,6 @@ int mavlink_pm_send_param(param_t param)
return ret;
}
static const char *mavlink_parameter_file = "/eeprom/parameters";
/**
* @return 0 on success, -1 if device open failed, -2 if writing parameters failed
*/
static int mavlink_pm_save_eeprom()
{
/* delete the file in case it exists */
unlink(mavlink_parameter_file);
/* create the file */
int fd = open(mavlink_parameter_file, O_WRONLY | O_CREAT | O_EXCL);
if (fd < 0) {
warn("opening '%s' for writing failed", mavlink_parameter_file);
return -1;
}
int result = param_export(fd, false);
close(fd);
if (result != 0) {
unlink(mavlink_parameter_file);
warn("error exporting parameters to '%s'", mavlink_parameter_file);
return -2;
}
return 0;
}
/**
* @return 0 on success, 1 if all params have not yet been stored, -1 if device open failed, -2 if writing parameters failed
*/
static int
mavlink_pm_load_eeprom()
{
int fd = open(mavlink_parameter_file, O_RDONLY);
if (fd < 0) {
/* no parameter file is OK, otherwise this is an error */
if (errno != ENOENT) {
warn("open '%s' for reading failed", mavlink_parameter_file);
return -1;
}
return 1;
}
int result = param_load(fd);
close(fd);
if (result != 0) {
warn("error reading parameters from '%s'", mavlink_parameter_file);
return -2;
}
return 0;
}
void mavlink_pm_message_handler(const mavlink_channel_t chan, const mavlink_message_t *msg)
{
switch (msg->msgid) {
@ -307,7 +249,7 @@ void mavlink_pm_message_handler(const mavlink_channel_t chan, const mavlink_mess
if (((int)(cmd_mavlink.param1)) == 0) {
/* read all parameters from EEPROM to RAM */
int read_ret = mavlink_pm_load_eeprom();
int read_ret = param_load_default();
if (read_ret == OK) {
//printf("[mavlink pm] Loaded EEPROM params in RAM\n");
mavlink_missionlib_send_gcs_string("[mavlink pm] OK loaded EEPROM params");
@ -327,7 +269,7 @@ void mavlink_pm_message_handler(const mavlink_channel_t chan, const mavlink_mess
} else if (((int)(cmd_mavlink.param1)) == 1) {
/* write all parameters from RAM to EEPROM */
int write_ret = mavlink_pm_save_eeprom();
int write_ret = param_save_default();
if (write_ret == OK) {
mavlink_missionlib_send_gcs_string("[mavlink pm] OK params written to EEPROM");
result = MAV_RESULT_ACCEPTED;

View File

@ -56,36 +56,53 @@
__EXPORT int param_main(int argc, char *argv[]);
static void do_save(void);
static void do_load(void);
static void do_import(void);
static void do_save(const char* param_file_name);
static void do_load(const char* param_file_name);
static void do_import(const char* param_file_name);
static void do_show(void);
static void do_show_print(void *arg, param_t param);
static const char *param_file_name = "/eeprom/parameters";
static const char *param_file_name_default = "/fs/microsd/parameters";
int
param_main(int argc, char *argv[])
{
if (argc >= 2) {
if (!strcmp(argv[1], "save"))
do_save();
if (!strcmp(argv[1], "save")) {
if (argc >= 3) {
do_save(argv[2]);
} else {
do_save(param_file_name_default);
}
}
if (!strcmp(argv[1], "load"))
do_load();
if (!strcmp(argv[1], "load")) {
if (argc >= 3) {
do_load(argv[2]);
} else {
do_load(param_file_name_default);
}
}
if (!strcmp(argv[1], "import"))
do_import();
if (!strcmp(argv[1], "import")) {
if (argc >= 3) {
do_import(argv[2]);
} else {
do_import(param_file_name_default);
}
}
if (!strcmp(argv[1], "show"))
if (!strcmp(argv[1], "show")) {
do_show();
}
}
errx(1, "expected a command, try 'load', 'import', 'show' or 'save'\n");
}
static void
do_save(void)
do_save(const char* param_file_name)
{
/* delete the parameter file in case it exists */
unlink(param_file_name);
@ -108,7 +125,7 @@ do_save(void)
}
static void
do_load(void)
do_load(const char* param_file_name)
{
int fd = open(param_file_name, O_RDONLY);
@ -118,14 +135,18 @@ do_load(void)
int result = param_load(fd);
close(fd);
if (result < 0)
if (result < 0) {
errx(1, "error importing from '%s'", param_file_name);
} else {
/* set default file name for next storage operation */
param_set_default_file(param_file_name);
}
exit(0);
}
static void
do_import(void)
do_import(const char* param_file_name)
{
int fd = open(param_file_name, O_RDONLY);

View File

@ -47,6 +47,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
#include <errno.h>
#include <sys/stat.h>
@ -480,6 +481,83 @@ param_reset_all(void)
param_notify_changes();
}
static char param_default_file_name[50] = "/eeprom/parameters";
int
param_set_default_file(const char* filename)
{
if (filename) {
if (strlen(filename) < sizeof(param_default_file_name))
{
strcpy(param_default_file_name, filename);
} else {
warnx("param file name too long");
return 1;
}
return 0;
} else {
warnx("no valid param file name");
return 1;
}
return 0;
}
int
param_save_default(void)
{
/* delete the file in case it exists */
unlink(param_default_file_name);
/* create the file */
int fd = open(param_default_file_name, O_WRONLY | O_CREAT | O_EXCL);
if (fd < 0) {
warn("opening '%s' for writing failed", param_default_file_name);
return -1;
}
int result = param_export(fd, false);
/* should not be necessary, over-careful here */
fsync(fd);
close(fd);
if (result != 0) {
unlink(param_default_file_name);
warn("error exporting parameters to '%s'", param_default_file_name);
return -2;
}
return 0;
}
/**
* @return 0 on success, 1 if all params have not yet been stored, -1 if device open failed, -2 if writing parameters failed
*/
int
param_load_default(void)
{
int fd = open(param_default_file_name, O_RDONLY);
if (fd < 0) {
/* no parameter file is OK, otherwise this is an error */
if (errno != ENOENT) {
warn("open '%s' for reading failed", param_default_file_name);
return -1;
}
return 1;
}
int result = param_load(fd);
close(fd);
if (result != 0) {
warn("error reading parameters from '%s'", param_default_file_name);
return -2;
}
return 0;
}
int
param_export(int fd, bool only_unsaved)
{

View File

@ -234,6 +234,25 @@ __EXPORT int param_load(int fd);
*/
__EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_changed);
/**
* Export parameters to the default file name.
*
*
* @param
*/
__EXPORT int param_save_default(void);
/**
* Set the default parameter file name.
*/
__EXPORT int param_set_default_file(const char* filename);
/**
* Import parameters from the default file name.
*/
__EXPORT int param_load_default(void);
/*
* Macros creating static parameter definitions.
*