mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-06 14:21:36 +08:00
Remove the arbitrary limit on the path to the default parameter file. Add a verb to the param command to set the default parameter file.
This commit is contained in:
@@ -62,8 +62,6 @@ static void do_import(const char* param_file_name);
|
|||||||
static void do_show(void);
|
static void do_show(void);
|
||||||
static void do_show_print(void *arg, param_t param);
|
static void do_show_print(void *arg, param_t param);
|
||||||
|
|
||||||
static const char *param_file_name_default = "/eeprom/parameters";
|
|
||||||
|
|
||||||
int
|
int
|
||||||
param_main(int argc, char *argv[])
|
param_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -72,7 +70,7 @@ param_main(int argc, char *argv[])
|
|||||||
if (argc >= 3) {
|
if (argc >= 3) {
|
||||||
do_save(argv[2]);
|
do_save(argv[2]);
|
||||||
} else {
|
} else {
|
||||||
do_save(param_file_name_default);
|
do_save(param_get_default_file());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +78,7 @@ param_main(int argc, char *argv[])
|
|||||||
if (argc >= 3) {
|
if (argc >= 3) {
|
||||||
do_load(argv[2]);
|
do_load(argv[2]);
|
||||||
} else {
|
} else {
|
||||||
do_load(param_file_name_default);
|
do_load(param_get_default_file());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,17 +86,26 @@ param_main(int argc, char *argv[])
|
|||||||
if (argc >= 3) {
|
if (argc >= 3) {
|
||||||
do_import(argv[2]);
|
do_import(argv[2]);
|
||||||
} else {
|
} else {
|
||||||
do_import(param_file_name_default);
|
do_import(param_get_default_file());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!strcmp(argv[1], "select")) {
|
||||||
|
if (argc >= 3) {
|
||||||
|
param_set_default_file(argv[2]);
|
||||||
|
} else {
|
||||||
|
param_set_default_file(NULL);
|
||||||
|
}
|
||||||
|
warnx("selected parameter file %s", param_get_default_file());
|
||||||
|
}
|
||||||
|
|
||||||
if (!strcmp(argv[1], "show")) {
|
if (!strcmp(argv[1], "show")) {
|
||||||
do_show();
|
do_show();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errx(1, "expected a command, try 'load', 'import', 'show' or 'save'\n");
|
errx(1, "expected a command, try 'load', 'import', 'show', 'select' or 'save'");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
|||||||
@@ -481,38 +481,38 @@ param_reset_all(void)
|
|||||||
param_notify_changes();
|
param_notify_changes();
|
||||||
}
|
}
|
||||||
|
|
||||||
static char param_default_file_name[50] = "/eeprom/parameters";
|
static const char *param_default_file = "/eeprom/parameters";
|
||||||
|
static char *param_user_file;
|
||||||
|
|
||||||
int
|
int
|
||||||
param_set_default_file(const char* filename)
|
param_set_default_file(const char* filename)
|
||||||
{
|
{
|
||||||
if (filename) {
|
if (param_user_file != NULL) {
|
||||||
if (strlen(filename) < sizeof(param_default_file_name))
|
free(param_user_file);
|
||||||
{
|
param_user_file = NULL;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
if (filename)
|
||||||
|
param_user_file = strdup(filename);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *
|
||||||
|
param_get_default_file(void)
|
||||||
|
{
|
||||||
|
return (param_user_file != NULL) ? param_user_file : param_default_file;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
param_save_default(void)
|
param_save_default(void)
|
||||||
{
|
{
|
||||||
/* delete the file in case it exists */
|
/* delete the file in case it exists */
|
||||||
unlink(param_default_file_name);
|
unlink(param_get_default_file());
|
||||||
|
|
||||||
/* create the file */
|
/* create the file */
|
||||||
int fd = open(param_default_file_name, O_WRONLY | O_CREAT | O_EXCL);
|
int fd = open(param_get_default_file(), O_WRONLY | O_CREAT | O_EXCL);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
warn("opening '%s' for writing failed", param_default_file_name);
|
warn("opening '%s' for writing failed", param_get_default_file());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -522,8 +522,8 @@ param_save_default(void)
|
|||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
unlink(param_default_file_name);
|
unlink(param_get_default_file());
|
||||||
warn("error exporting parameters to '%s'", param_default_file_name);
|
warn("error exporting parameters to '%s'", param_get_default_file());
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -536,12 +536,12 @@ param_save_default(void)
|
|||||||
int
|
int
|
||||||
param_load_default(void)
|
param_load_default(void)
|
||||||
{
|
{
|
||||||
int fd = open(param_default_file_name, O_RDONLY);
|
int fd = open(param_get_default_file(), O_RDONLY);
|
||||||
|
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
/* no parameter file is OK, otherwise this is an error */
|
/* no parameter file is OK, otherwise this is an error */
|
||||||
if (errno != ENOENT) {
|
if (errno != ENOENT) {
|
||||||
warn("open '%s' for reading failed", param_default_file_name);
|
warn("open '%s' for reading failed", param_get_default_file());
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
@@ -551,7 +551,7 @@ param_load_default(void)
|
|||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
if (result != 0) {
|
if (result != 0) {
|
||||||
warn("error reading parameters from '%s'", param_default_file_name);
|
warn("error reading parameters from '%s'", param_get_default_file());
|
||||||
return -2;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -234,25 +234,40 @@ __EXPORT int param_load(int fd);
|
|||||||
*/
|
*/
|
||||||
__EXPORT void param_foreach(void (*func)(void *arg, param_t param), void *arg, bool only_changed);
|
__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.
|
* Set the default parameter file name.
|
||||||
|
*
|
||||||
|
* @param filename Path to the default parameter file. The file is not require to
|
||||||
|
* exist.
|
||||||
|
* @return Zero on success.
|
||||||
*/
|
*/
|
||||||
__EXPORT int param_set_default_file(const char* filename);
|
__EXPORT int param_set_default_file(const char* filename);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Import parameters from the default file name.
|
* Get the default parameter file name.
|
||||||
|
*
|
||||||
|
* @return The path to the current default parameter file; either as
|
||||||
|
* a result of a call to param_set_default_file, or the
|
||||||
|
* built-in default.
|
||||||
|
*/
|
||||||
|
__EXPORT const char *param_get_default_file(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save parameters to the default file.
|
||||||
|
*
|
||||||
|
* This function saves all parameters with non-default values.
|
||||||
|
*
|
||||||
|
* @return Zero on success.
|
||||||
|
*/
|
||||||
|
__EXPORT int param_save_default(void);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load parameters from the default parameter file.
|
||||||
|
*
|
||||||
|
* @return Zero on success.
|
||||||
*/
|
*/
|
||||||
__EXPORT int param_load_default(void);
|
__EXPORT int param_load_default(void);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Macros creating static parameter definitions.
|
* Macros creating static parameter definitions.
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user