From f7fe6a037d7cddcc1d956df63b9cce41c279d326 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Fri, 19 Jun 2015 11:39:08 -0700 Subject: [PATCH 1/2] Converted getopt use to px4_getopt In the posix and qurt builds, getopt is not thread safe so px4_getopt should be used instead. Signed-off-by: Mark Charlebois --- makefiles/posix/toolchain_native.mk | 2 +- src/modules/sdlog2/sdlog2.c | 9 +- .../posix/px4_layer/px4_posix_tasks.cpp | 1 + src/systemcmds/esc_calib/esc_calib.c | 120 +++++++++++------- src/systemcmds/reboot/reboot.c | 16 +-- 5 files changed, 86 insertions(+), 62 deletions(-) diff --git a/makefiles/posix/toolchain_native.mk b/makefiles/posix/toolchain_native.mk index ff4cdc4702..f58e55314b 100644 --- a/makefiles/posix/toolchain_native.mk +++ b/makefiles/posix/toolchain_native.mk @@ -120,7 +120,7 @@ ifeq ($(CONFIG_BOARD),) $(error Board config does not define CONFIG_BOARD) endif ARCHDEFINES += -DCONFIG_ARCH_BOARD_$(CONFIG_BOARD) \ - -Dnoreturn_function= \ + -Dnoreturn_function=__attribute__\(\(noreturn\)\) \ -I$(PX4_BASE)/src/modules/systemlib \ -I$(PX4_BASE)/src/lib/eigen \ -I$(PX4_BASE)/src/platforms/posix/include \ diff --git a/src/modules/sdlog2/sdlog2.c b/src/modules/sdlog2/sdlog2.c index 270804075e..693ff0d034 100644 --- a/src/modules/sdlog2/sdlog2.c +++ b/src/modules/sdlog2/sdlog2.c @@ -44,6 +44,7 @@ #include #include +#include #include #include #include @@ -895,10 +896,12 @@ int sdlog2_thread_main(int argc, char *argv[]) * set error flag instead */ bool err_flag = false; - while ((ch = getopt(argc, argv, "r:b:eatx")) != EOF) { + int myoptind = 1; + const char *myoptarg = NULL; + while ((ch = px4_getopt(argc, argv, "r:b:eatx", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'r': { - unsigned long r = strtoul(optarg, NULL, 10); + unsigned long r = strtoul(myoptarg, NULL, 10); if (r == 0) { r = 1; @@ -909,7 +912,7 @@ int sdlog2_thread_main(int argc, char *argv[]) break; case 'b': { - unsigned long s = strtoul(optarg, NULL, 10); + unsigned long s = strtoul(myoptarg, NULL, 10); if (s < 1) { s = 1; diff --git a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp index c65fadcc1a..ac87ddfd55 100644 --- a/src/platforms/posix/px4_layer/px4_posix_tasks.cpp +++ b/src/platforms/posix/px4_layer/px4_posix_tasks.cpp @@ -98,6 +98,7 @@ void px4_systemreset(bool to_bootloader) { PX4_WARN("Called px4_system_reset"); + exit(0); } px4_task_t px4_task_spawn_cmd(const char *name, int scheduler, int priority, int stack_size, px4_main_t entry, char * const argv[]) diff --git a/src/systemcmds/esc_calib/esc_calib.c b/src/systemcmds/esc_calib/esc_calib.c index f04ca29ff9..8b432f1638 100644 --- a/src/systemcmds/esc_calib/esc_calib.c +++ b/src/systemcmds/esc_calib/esc_calib.c @@ -39,7 +39,9 @@ */ #include -#include +#include +#include +#include #include #include @@ -52,15 +54,9 @@ #include #include -#include -#include -#include -#include - #include #include "systemlib/systemlib.h" -#include "systemlib/err.h" #include "drivers/drv_pwm_output.h" #include @@ -76,9 +72,9 @@ static void usage(const char *reason) { if (reason != NULL) - warnx("%s", reason); + PX4_ERR("%s", reason); - errx(1, + PX4_ERR( "usage:\n" "esc_calib\n" " [-d PWM output device (defaults to " PWM_OUTPUT0_DEVICE_PATH ")\n" @@ -93,7 +89,7 @@ usage(const char *reason) int esc_calib_main(int argc, char *argv[]) { - char *dev = PWM_OUTPUT0_DEVICE_PATH; + const char *dev = PWM_OUTPUT0_DEVICE_PATH; char *ep; int ch; int ret; @@ -114,21 +110,24 @@ esc_calib_main(int argc, char *argv[]) if (argc < 2) { usage("no channels provided"); + return 1; } int arg_consumed = 0; - while ((ch = getopt(argc, argv, "d:c:m:al:h:")) != EOF) { + int myoptind = 1; + const char *myoptarg = NULL; + while ((ch = px4_getopt(argc, argv, "d:c:m:al:h:", &myoptind, &myoptarg)) != EOF) { switch (ch) { case 'd': - dev = optarg; + dev = myoptarg; arg_consumed += 2; break; case 'c': /* Read in channels supplied as one int and convert to mask: 1234 -> 0xF */ - channels = strtoul(optarg, &ep, 0); + channels = strtoul(myoptarg, &ep, 0); while ((single_ch = channels % 10)) { @@ -139,9 +138,11 @@ esc_calib_main(int argc, char *argv[]) case 'm': /* Read in mask directly */ - set_mask = strtoul(optarg, &ep, 0); - if (*ep != '\0') + set_mask = strtoul(myoptarg, &ep, 0); + if (*ep != '\0') { usage("bad set_mask value"); + return 1; + } break; case 'a': @@ -153,27 +154,34 @@ esc_calib_main(int argc, char *argv[]) case 'l': /* Read in custom low value */ - pwm_low = strtoul(optarg, &ep, 0); - if (*ep != '\0' || pwm_low < PWM_LOWEST_MIN || pwm_low > PWM_HIGHEST_MIN) + pwm_low = strtoul(myoptarg, &ep, 0); + if (*ep != '\0' || pwm_low < PWM_LOWEST_MIN || pwm_low > PWM_HIGHEST_MIN) { usage("low PWM invalid"); + return 1; + } break; case 'h': /* Read in custom high value */ - pwm_high = strtoul(optarg, &ep, 0); - if (*ep != '\0' || pwm_high > PWM_HIGHEST_MAX || pwm_high < PWM_LOWEST_MAX) + pwm_high = strtoul(myoptarg, &ep, 0); + if (*ep != '\0' || pwm_high > PWM_HIGHEST_MAX || pwm_high < PWM_LOWEST_MAX) { usage("high PWM invalid"); + return 1; + } break; default: usage(NULL); + return 1; } } if (set_mask == 0) { usage("no channels chosen"); + return 1; } if (pwm_low > pwm_high) { usage("low pwm is higher than high pwm"); + return 1; } /* make sure no other source is publishing control values now */ @@ -191,9 +199,10 @@ esc_calib_main(int argc, char *argv[]) orb_check(act_sub, &orb_updated); if (orb_updated) { - errx(1, "ABORTING! Attitude control still active. Please ensure to shut down all controllers:\n" + PX4_ERR("ABORTING! Attitude control still active. Please ensure to shut down all controllers:\n" "\tmc_att_control stop\n" "\tfw_att_control stop\n"); + return 1; } printf("\nATTENTION, please remove or fix propellers before starting calibration!\n" @@ -211,24 +220,22 @@ esc_calib_main(int argc, char *argv[]) ret = poll(&fds, 1, 0); if (ret > 0) { - read(0, &c, 1); if (c == 'y' || c == 'Y') { - break; } else if (c == 0x03 || c == 0x63 || c == 'q') { printf("ESC calibration exited\n"); - exit(0); + return 0; } else if (c == 'n' || c == 'N') { printf("ESC calibration aborted\n"); - exit(0); + return 0; } else { printf("Unknown input, ESC calibration aborted\n"); - exit(0); + return 0; } } @@ -239,24 +246,32 @@ esc_calib_main(int argc, char *argv[]) /* open for ioctl only */ int fd = open(dev, 0); - if (fd < 0) - err(1, "can't open %s", dev); + if (fd < 0) { + PX4_ERR("can't open %s", dev); + return 1; + } /* get number of channels available on the device */ ret = ioctl(fd, PWM_SERVO_GET_COUNT, (unsigned long)&max_channels); - if (ret != OK) - err(1, "PWM_SERVO_GET_COUNT"); + if (ret != OK) { + PX4_ERR("PWM_SERVO_GET_COUNT"); + return 1; + } /* tell IO/FMU that its ok to disable its safety with the switch */ ret = ioctl(fd, PWM_SERVO_SET_ARM_OK, 0); - if (ret != OK) - err(1, "PWM_SERVO_SET_ARM_OK"); + if (ret != OK) { + PX4_ERR("PWM_SERVO_SET_ARM_OK"); + return 1; + } /* tell IO/FMU that the system is armed (it will output values if safety is off) */ ret = ioctl(fd, PWM_SERVO_ARM, 0); - if (ret != OK) - err(1, "PWM_SERVO_ARM"); + if (ret != OK) { + PX4_ERR("PWM_SERVO_ARM"); + return 1; + } - warnx("Outputs armed"); + printf("Outputs armed"); /* wait for user confirmation */ @@ -273,24 +288,24 @@ esc_calib_main(int argc, char *argv[]) if (set_mask & 1< 0) { - read(0, &c, 1); if (c == 13) { - break; } else if (c == 0x03 || c == 0x63 || c == 'q') { - warnx("ESC calibration exited"); - exit(0); + printf("ESC calibration exited"); + goto done; } } @@ -310,24 +325,24 @@ esc_calib_main(int argc, char *argv[]) if (set_mask & 1< 0) { - read(0, &c, 1); if (c == 13) { - break; } else if (c == 0x03 || c == 0x63 || c == 'q') { printf("ESC calibration exited\n"); - exit(0); + goto done; } } @@ -337,12 +352,19 @@ esc_calib_main(int argc, char *argv[]) /* disarm */ ret = ioctl(fd, PWM_SERVO_DISARM, 0); - if (ret != OK) - err(1, "PWM_SERVO_DISARM"); + if (ret != OK) { + PX4_ERR("PWM_SERVO_DISARM"); + goto cleanup; + } - warnx("Outputs disarmed"); + printf("Outputs disarmed"); printf("ESC calibration finished\n"); - exit(0); +done: + close(fd); + return 0; +cleanup: + close(fd); + return 1; } diff --git a/src/systemcmds/reboot/reboot.c b/src/systemcmds/reboot/reboot.c index de4d77d5fd..8de6b09985 100644 --- a/src/systemcmds/reboot/reboot.c +++ b/src/systemcmds/reboot/reboot.c @@ -38,12 +38,10 @@ */ #include -#include -#include -#include - +#include +#include +#include #include -#include __EXPORT int reboot_main(int argc, char *argv[]); @@ -52,14 +50,16 @@ int reboot_main(int argc, char *argv[]) int ch; bool to_bootloader = false; - while ((ch = getopt(argc, argv, "b")) != -1) { + int myoptind = 1; + const char *myoptarg = NULL; + while ((ch = px4_getopt(argc, argv, "b", &myoptind, &myoptarg)) != -1) { switch (ch) { case 'b': to_bootloader = true; break; default: - errx(1, "usage: reboot [-b]\n" + PX4_ERR("usage: reboot [-b]\n" " -b reboot into the bootloader"); } @@ -67,5 +67,3 @@ int reboot_main(int argc, char *argv[]) px4_systemreset(to_bootloader); } - - From 4a17411f8ffd8d7ee84c3e77d39097688587cab7 Mon Sep 17 00:00:00 2001 From: Mark Charlebois Date: Fri, 19 Jun 2015 13:26:58 -0700 Subject: [PATCH 2/2] POSIX: Added systemcmds esc_cal and reboot modules Signed-off-by: Mark Charlebois --- makefiles/posix/config_posix_sitl.mk | 2 ++ makefiles/posix/posix_elf.mk | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/makefiles/posix/config_posix_sitl.mk b/makefiles/posix/config_posix_sitl.mk index d0ec4086bb..10ae8ad3b3 100644 --- a/makefiles/posix/config_posix_sitl.mk +++ b/makefiles/posix/config_posix_sitl.mk @@ -20,6 +20,8 @@ MODULES += systemcmds/param MODULES += systemcmds/mixer MODULES += systemcmds/topic_listener MODULES += systemcmds/ver +MODULES += systemcmds/esc_calib +MODULES += systemcmds/reboot # # General system control diff --git a/makefiles/posix/posix_elf.mk b/makefiles/posix/posix_elf.mk index edb1e32d76..bbc7545a5e 100644 --- a/makefiles/posix/posix_elf.mk +++ b/makefiles/posix/posix_elf.mk @@ -56,9 +56,11 @@ $(PRODUCT_SHARED_PRELINK): $(OBJS) $(MODULE_OBJS) $(LIBRARY_LIBS) $(GLOBAL_DEPS) $(PRODUCT_SHARED_LIB): $(PRODUCT_SHARED_PRELINK) $(call LINK_A,$@,$(PRODUCT_SHARED_PRELINK)) +$(WORK_DIR)apps.h: $(WORK_DIR)builtin_commands + $(PX4_BASE)/Tools/posix_apps.py > $(WORK_DIR)apps.h + MAIN = $(PX4_BASE)/src/platforms/posix/main.cpp -$(WORK_DIR)mainapp: $(PRODUCT_SHARED_LIB) - $(PX4_BASE)/Tools/posix_apps.py > apps.h +$(WORK_DIR)mainapp: $(PRODUCT_SHARED_LIB) $(WORK_DIR)apps.h $(call LINK,$@, -I. $(MAIN) $(PRODUCT_SHARED_LIB)) #