Exemplo n.º 1
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)
{
	warnx("param_load_default\n");
	int fd_load = PARAM_OPEN(param_get_default_file(), O_RDONLY);

	if (fd_load < 0) {
		/* no parameter file is OK, otherwise this is an error */
		if (errno != ENOENT) {
			warn("open '%s' for reading failed", param_get_default_file());
			return -1;
		}

		return 1;
	}

	int result = param_load(fd_load);
	PARAM_CLOSE(fd_load);

	if (result != 0) {
		warn("error reading parameters from '%s'", param_get_default_file());
		return -2;
	}

	return 0;
}
Exemplo n.º 2
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 res = 0;
#if !defined(FLASH_BASED_PARAMS)
	int fd_load = PARAM_OPEN(param_get_default_file(), O_RDONLY);

	if (fd_load < 0) {
		/* no parameter file is OK, otherwise this is an error */
		if (errno != ENOENT) {
			warn("open '%s' for reading failed", param_get_default_file());
			return -1;
		}

		return 1;
	}

	int result = param_load(fd_load);
	PARAM_CLOSE(fd_load);

	if (result != 0) {
		warn("error reading parameters from '%s'", param_get_default_file());
		return -2;
	}

#else
	// no need for locking
	res = flash_param_load();
#endif
	return res;
}
Exemplo n.º 3
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
param_load_default_no_notify(void)
{
	int fd_load = open(param_get_default_file(), O_RDONLY);

	if (fd_load < 0) {
		release_shmem_lock();

		/* no parameter file is OK, otherwise this is an error */
		if (errno != ENOENT) {
			debug("open '%s' for reading failed", param_get_default_file());
			return -1;
		}

		return 1;
	}

	int result = param_import(fd_load);

	close(fd_load);

	PX4_INFO("param loading done\n");

	if (result != 0) {
		warn("error reading parameters from '%s'", param_get_default_file());
		return -2;
	}

	return 0;
}
Exemplo n.º 4
0
int
param_save_default(void)
{
	/* delete the file in case it exists */
	unlink(param_get_default_file());

	/* create the file */
	int fd = open(param_get_default_file(), O_WRONLY | O_CREAT | O_EXCL);

	if (fd < 0) {
		warn("opening '%s' for writing failed", param_get_default_file());
		return -1;
	}

	int result = param_export(fd, false);
	close(fd);

	if (result != 0) {
		warn("error exporting parameters to '%s'", param_get_default_file());
		unlink(param_get_default_file());
		return -2;
	}

	return 0;
}
Exemplo n.º 5
0
int
param_main(int argc, char *argv[])
{
	if (argc >= 2) {
		if (!strcmp(argv[1], "save")) {
			if (argc >= 3) {
				do_save(argv[2]);
			} else {
				do_save(param_get_default_file());
			}
		}

		if (!strcmp(argv[1], "load")) {
			if (argc >= 3) {
				do_load(argv[2]);
			} else {
				do_load(param_get_default_file());
			}
		}

		if (!strcmp(argv[1], "import")) {
			if (argc >= 3) {
				do_import(argv[2]);
			} else {
				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 default file %s", param_get_default_file());
			exit(0);
		}

		if (!strcmp(argv[1], "show"))
			if (argc >= 3) {
				do_show(argv[2]);
			} else {
				do_show(NULL);
			}
	}
	
	errx(1, "expected a command, try 'load', 'import', 'show', 'select' or 'save'");
}
Exemplo n.º 6
0
int
param_save_default(void)
{
	int res;
	int fd;

	const char *filename = param_get_default_file();

	/* write parameters to temp file */
	fd = PARAM_OPEN(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);

	if (fd < 0) {
		warn("failed to open param file: %s", filename);
		return ERROR;
	}

	res = param_export(fd, false);

	if (res != OK) {
		warnx("failed to write parameters to file: %s", filename);
	}

	PARAM_CLOSE(fd);

	return res;
}
Exemplo n.º 7
0
int
param_save_default(void)
{
	int result;

	/* delete the file in case it exists */
	struct stat buffer;
	if (stat(param_get_default_file(), &buffer) == 0) {
		result = unlink(param_get_default_file());
		if (result != OK)
			warnx("unlinking file %s failed.", param_get_default_file());
	}

	/* create the file */
	int fd = open(param_get_default_file(), O_WRONLY | O_CREAT | O_EXCL);

	if (fd < 0) {
		warn("opening '%s' for writing failed", param_get_default_file());
		return fd;
	}

	result = param_export(fd, false);
	close(fd);

	if (result != 0) {
		warn("error exporting parameters to '%s'", param_get_default_file());
		unlink(param_get_default_file());
		return result;
	}

	return 0;
}
Exemplo n.º 8
0
int
param_save_default(void)
{
	int res = OK;
	int fd = -1;
	bool is_locked = false;

	const char *filename = param_get_default_file();

	if (get_shmem_lock(__FILE__, __LINE__) != 0) {
		PX4_ERR("Could not get shmem lock\n");
		res = ERROR;
		goto exit;
	}

	is_locked = true;

	fd = PARAM_OPEN(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);

	if (fd < 0) {
		PX4_ERR("failed to open param file: %s", filename);
		goto exit;
	}

	res = param_export(fd, false);

	if (res != OK) {
		PX4_ERR("failed to write parameters to file: %s", filename);
		goto exit;
	}

	PARAM_CLOSE(fd);
	fd = -1;

exit:

	if (is_locked) {
		release_shmem_lock();
	}

	if (fd >= 0) {
		close(fd);
	}

	if (res == OK) {
		PX4_INFO("saving params completed successfully\n");
	}

	return res;
}
Exemplo n.º 9
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_get_default_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", param_get_default_file());
			return -1;
		}
		return 1;
	}

	int result = param_load(fd);
	close(fd);

	if (result != 0) {
		warn("error reading parameters from '%s'", param_get_default_file());
		return -2;
	}

	return 0;
}
Exemplo n.º 10
0
int
param_save_default(void)
{
	int res = PX4_ERROR;
#if !defined(FLASH_BASED_PARAMS)

	const char *filename = param_get_default_file();

	/* write parameters to temp file */
	int fd = PARAM_OPEN(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);

	if (fd < 0) {
		PX4_ERR("failed to open param file: %s", filename);
		return ERROR;
	}

	int attempts = 5;

	while (res != OK && attempts > 0) {
		res = param_export(fd, false);
		attempts--;

		if (res != PX4_OK) {
			PX4_ERR("param_export failed, retrying %d", attempts);
			lseek(fd, 0, SEEK_SET); // jump back to the beginning of the file
		}
	}

	if (res != OK) {
		PX4_ERR("failed to write parameters to file: %s", filename);
	}

	PARAM_CLOSE(fd);
#else
	param_lock_writer();
	res = flash_param_save();
	param_unlock_writer();
#endif

	return res;
}
Exemplo n.º 11
0
int
param_save_default(void)
{
	int res;
#if !defined(FLASH_BASED_PARAMS)
	int fd;

	const char *filename = param_get_default_file();

	/* write parameters to temp file */
	fd = PARAM_OPEN(filename, O_WRONLY | O_CREAT, PX4_O_MODE_666);

	if (fd < 0) {
		warn("failed to open param file: %s", filename);
		return ERROR;
	}

	res = 1;
	int attempts = 5;

	while (res != OK && attempts > 0) {
		res = param_export(fd, false);
		attempts--;
	}

	if (res != OK) {
		warnx("failed to write parameters to file: %s", filename);
	}

	PARAM_CLOSE(fd);
#else
	res = flash_param_save();
#endif

	return res;
}
Exemplo n.º 12
0
int
param_main(int argc, char *argv[])
{
	if (argc >= 2) {
		if (!strcmp(argv[1], "save")) {
			if (argc >= 3) {
				do_save(argv[2]);
			} else {
				if (param_save_default()) {
					warnx("Param export failed.");
					exit(1);
				} else {
					exit(0);
				}
			}
		}

		if (!strcmp(argv[1], "load")) {
			if (argc >= 3) {
				do_load(argv[2]);
			} else {
				do_load(param_get_default_file());
			}
		}

		if (!strcmp(argv[1], "import")) {
			if (argc >= 3) {
				do_import(argv[2]);
			} else {
				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 default file %s", param_get_default_file());
			exit(0);
		}

		if (!strcmp(argv[1], "show")) {
			if (argc >= 3) {
				do_show(argv[2]);
			} else {
				do_show(NULL);
			}
		}

		if (!strcmp(argv[1], "set")) {
			if (argc >= 4) {
				do_set(argv[2], argv[3]);
			} else {
				errx(1, "not enough arguments.\nTry 'param set PARAM_NAME 3'");
			}
		}

		if (!strcmp(argv[1], "compare")) {
			if (argc >= 4) {
				do_compare(argv[2], &argv[3], argc - 3);
			} else {
				errx(1, "not enough arguments.\nTry 'param compare PARAM_NAME 3'");
			}
		}
	}
	
	errx(1, "expected a command, try 'load', 'import', 'show', 'set', 'compare', 'select' or 'save'");
}
Exemplo n.º 13
0
int
param_main(int argc, char *argv[])
{
	if (argc >= 2) {
		if (!strcmp(argv[1], "save")) {
			if (argc >= 3) {
				return do_save(argv[2]);

			} else {
				int ret = do_save_default();

				if (ret) {
					PX4_ERR("Param save failed (%i)", ret);
					return 1;

				} else {
					return 0;
				}
			}
		}

		if (!strcmp(argv[1], "load")) {
			if (argc >= 3) {
				return do_load(argv[2]);

			} else {
				return do_load(param_get_default_file());
			}
		}

		if (!strcmp(argv[1], "import")) {
			if (argc >= 3) {
				return do_import(argv[2]);

			} else {
				return 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);
			}

			PX4_INFO("selected parameter default file %s", param_get_default_file());
			return 0;
		}

		if (!strcmp(argv[1], "show")) {
			if (argc >= 3) {
				// optional argument -c to show only non-default params
				if (!strcmp(argv[2], "-c")) {
					if (argc >= 4) {
						return do_show(argv[3], true);

					} else {
						return do_show(NULL, true);
					}

				} else {
					return do_show(argv[2], false);
				}

			} else {
				return do_show(NULL, false);
			}
		}

		if (!strcmp(argv[1], "set")) {
			if (argc >= 5) {

				/* if the fail switch is provided, fails the command if not found */
				bool fail = !strcmp(argv[4], "fail");

				return do_set(argv[2], argv[3], fail);

			} else if (argc >= 4) {
				return do_set(argv[2], argv[3], false);

			} else {
				PX4_ERR("not enough arguments.\nTry 'param set PARAM_NAME 3 [fail]'");
				return 1;
			}
		}

		if (!strcmp(argv[1], "compare")) {
			if (argc >= 4) {
				return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR_EQUAL);

			} else {
				PX4_ERR("not enough arguments.\nTry 'param compare PARAM_NAME 3'");
				return 1;
			}
		}

		if (!strcmp(argv[1], "greater")) {
			if (argc >= 4) {
				return do_compare(argv[2], &argv[3], argc - 3, COMPARE_OPERATOR_GREATER);

			} else {
				PX4_ERR("not enough arguments.\nTry 'param greater PARAM_NAME 3'");
				return 1;
			}
		}

		if (!strcmp(argv[1], "reset")) {
			if (argc >= 3) {
				return do_reset((const char **) &argv[2], argc - 2);

			} else {
				return do_reset(NULL, 0);
			}
		}

		if (!strcmp(argv[1], "reset_nostart")) {
			if (argc >= 3) {
				return do_reset_nostart((const char **) &argv[2], argc - 2);

			} else {
				return do_reset_nostart(NULL, 0);
			}
		}

		if (!strcmp(argv[1], "index_used")) {
			if (argc >= 3) {
				return do_show_index(argv[2], true);

			} else {
				PX4_ERR("no index provided");
				return 1;
			}
		}

		if (!strcmp(argv[1], "index")) {
			if (argc >= 3) {
				return do_show_index(argv[2], false);

			} else {
				PX4_ERR("no index provided");
				return 1;
			}
		}

		if (!strcmp(argv[1], "find")) {
			if (argc >= 3) {
				return do_find(argv[2]);

			} else {
				PX4_ERR("not enough arguments.\nTry 'param find PARAM_NAME'");
				return 1;
			}
		}
	}

	PX4_INFO("expected a command, try 'load', 'import', 'show [-c] [<filter>]', 'set <param> <value>', 'compare',\n'index', 'index_used', 'find', 'greater', 'select', 'save', or 'reset' ");
	return 1;
}
Exemplo n.º 14
0
int
param_main(int argc, char *argv[])
{
	if (argc >= 2) {
		if (!strcmp(argv[1], "save")) {
			if (argc >= 3) {
				return do_save(argv[2]);

			} else {
				if (param_save_default()) {
					warnx("Param export failed.");
					return 1;

				} else {
					return 0;
				}
			}
		}

		if (!strcmp(argv[1], "load")) {
			if (argc >= 3) {
				return do_load(argv[2]);

			} else {
				return do_load(param_get_default_file());
			}
		}

		if (!strcmp(argv[1], "import")) {
			if (argc >= 3) {
				return do_import(argv[2]);

			} else {
				return 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 default file %s", param_get_default_file());
			return 0;
		}

		if (!strcmp(argv[1], "show")) {
			if (argc >= 3) {
				do_show(argv[2]);
				return 0;

			} else {
				do_show(NULL);
				return 0;
			}
		}

		if (!strcmp(argv[1], "set")) {
			if (argc >= 5) {

				/* if the fail switch is provided, fails the command if not found */
				bool fail = !strcmp(argv[4], "fail");

				return do_set(argv[2], argv[3], fail);

			} else if (argc >= 4) {
				return do_set(argv[2], argv[3], false);

			} else {
				warnx("not enough arguments.\nTry 'param set PARAM_NAME 3 [fail]'");
				return 1;
			}
		}

		if (!strcmp(argv[1], "compare")) {
			if (argc >= 4) {
				return do_compare(argv[2], &argv[3], argc - 3);

			} else {
				warnx("not enough arguments.\nTry 'param compare PARAM_NAME 3'");
				return 1;
			}
		}

		if (!strcmp(argv[1], "reset")) {
			if (argc >= 3) {
				return do_reset((const char **) &argv[2], argc - 2);

			} else {
				return do_reset(NULL, 0);
			}
		}

		if (!strcmp(argv[1], "reset_nostart")) {
			if (argc >= 3) {
				return do_reset_nostart((const char **) &argv[2], argc - 2);

			} else {
				return do_reset_nostart(NULL, 0);
			}
		}

		if (!strcmp(argv[1], "index_used")) {
			if (argc >= 3) {
				do_show_index(argv[2], true);
			} else {
				warnx("no index provided");
				return 1;
			}
		}

		if (!strcmp(argv[1], "index")) {
			if (argc >= 3) {
				do_show_index(argv[2], false);
			} else {
				warnx("no index provided");
				return 1;
			}
		}
	}

	warnx("expected a command, try 'load', 'import', 'show', 'set', 'compare', 'select' or 'save'");
	return 1;
}