mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-01 06:51:37 +08:00
format src/systemcmds/nshterm
This commit is contained in:
@@ -59,87 +59,92 @@ __EXPORT int nshterm_main(int argc, char *argv[]);
|
|||||||
int
|
int
|
||||||
nshterm_main(int argc, char *argv[])
|
nshterm_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
printf("Usage: nshterm <device>\n");
|
printf("Usage: nshterm <device>\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
unsigned retries = 0;
|
|
||||||
int fd = -1;
|
|
||||||
int armed_fd = orb_subscribe(ORB_ID(actuator_armed));
|
|
||||||
struct actuator_armed_s armed;
|
|
||||||
|
|
||||||
/* back off 1800 ms to avoid running into the USB setup timing */
|
unsigned retries = 0;
|
||||||
while (hrt_absolute_time() < 1800U * 1000U) {
|
int fd = -1;
|
||||||
usleep(50000);
|
int armed_fd = orb_subscribe(ORB_ID(actuator_armed));
|
||||||
}
|
struct actuator_armed_s armed;
|
||||||
|
|
||||||
/* try to bring up the console - stop doing so if the system gets armed */
|
/* back off 1800 ms to avoid running into the USB setup timing */
|
||||||
while (true) {
|
while (hrt_absolute_time() < 1800U * 1000U) {
|
||||||
|
usleep(50000);
|
||||||
|
}
|
||||||
|
|
||||||
/* abort if an arming topic is published and system is armed */
|
/* try to bring up the console - stop doing so if the system gets armed */
|
||||||
bool updated = false;
|
while (true) {
|
||||||
orb_check(armed_fd, &updated);
|
|
||||||
if (updated) {
|
|
||||||
/* the system is now providing arming status feedback.
|
|
||||||
* instead of timing out, we resort to abort bringing
|
|
||||||
* up the terminal.
|
|
||||||
*/
|
|
||||||
orb_copy(ORB_ID(actuator_armed), armed_fd, &armed);
|
|
||||||
|
|
||||||
if (armed.armed) {
|
/* abort if an arming topic is published and system is armed */
|
||||||
/* this is not an error, but we are done */
|
bool updated = false;
|
||||||
exit(0);
|
orb_check(armed_fd, &updated);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* the retries are to cope with the behaviour of /dev/ttyACM0 */
|
if (updated) {
|
||||||
/* which may not be ready immediately. */
|
/* the system is now providing arming status feedback.
|
||||||
fd = open(argv[1], O_RDWR);
|
* instead of timing out, we resort to abort bringing
|
||||||
if (fd != -1) {
|
* up the terminal.
|
||||||
close(armed_fd);
|
*/
|
||||||
break;
|
orb_copy(ORB_ID(actuator_armed), armed_fd, &armed);
|
||||||
}
|
|
||||||
usleep(100000);
|
|
||||||
retries++;
|
|
||||||
}
|
|
||||||
if (fd == -1) {
|
|
||||||
perror(argv[1]);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* set up the serial port with output processing */
|
if (armed.armed) {
|
||||||
|
/* this is not an error, but we are done */
|
||||||
/* Try to set baud rate */
|
exit(0);
|
||||||
struct termios uart_config;
|
}
|
||||||
int termios_state;
|
}
|
||||||
|
|
||||||
/* Back up the original uart configuration to restore it after exit */
|
/* the retries are to cope with the behaviour of /dev/ttyACM0 */
|
||||||
if ((termios_state = tcgetattr(fd, &uart_config)) < 0) {
|
/* which may not be ready immediately. */
|
||||||
warnx("ERR get config %s: %d\n", argv[1], termios_state);
|
fd = open(argv[1], O_RDWR);
|
||||||
close(fd);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set ONLCR flag (which appends a CR for every LF) */
|
if (fd != -1) {
|
||||||
uart_config.c_oflag |= (ONLCR | OPOST);
|
close(armed_fd);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if ((termios_state = tcsetattr(fd, TCSANOW, &uart_config)) < 0) {
|
usleep(100000);
|
||||||
warnx("ERR set config %s\n", argv[1]);
|
retries++;
|
||||||
close(fd);
|
}
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* setup standard file descriptors */
|
if (fd == -1) {
|
||||||
close(0);
|
perror(argv[1]);
|
||||||
close(1);
|
exit(1);
|
||||||
close(2);
|
}
|
||||||
dup2(fd, 0);
|
|
||||||
dup2(fd, 1);
|
|
||||||
dup2(fd, 2);
|
|
||||||
|
|
||||||
nsh_consolemain(0, NULL);
|
/* set up the serial port with output processing */
|
||||||
|
|
||||||
close(fd);
|
/* Try to set baud rate */
|
||||||
|
struct termios uart_config;
|
||||||
|
int termios_state;
|
||||||
|
|
||||||
return OK;
|
/* Back up the original uart configuration to restore it after exit */
|
||||||
|
if ((termios_state = tcgetattr(fd, &uart_config)) < 0) {
|
||||||
|
warnx("ERR get config %s: %d\n", argv[1], termios_state);
|
||||||
|
close(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Set ONLCR flag (which appends a CR for every LF) */
|
||||||
|
uart_config.c_oflag |= (ONLCR | OPOST);
|
||||||
|
|
||||||
|
if ((termios_state = tcsetattr(fd, TCSANOW, &uart_config)) < 0) {
|
||||||
|
warnx("ERR set config %s\n", argv[1]);
|
||||||
|
close(fd);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* setup standard file descriptors */
|
||||||
|
close(0);
|
||||||
|
close(1);
|
||||||
|
close(2);
|
||||||
|
dup2(fd, 0);
|
||||||
|
dup2(fd, 1);
|
||||||
|
dup2(fd, 2);
|
||||||
|
|
||||||
|
nsh_consolemain(0, NULL);
|
||||||
|
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
return OK;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user