posix: add mavlink shell for posix targets (#19800)

This commit is contained in:
Bruce Meagher
2022-06-20 02:51:47 -07:00
committed by GitHub
parent 4cc3e78558
commit 714234ca90
4 changed files with 328 additions and 24 deletions
+43 -7
View File
@@ -1,6 +1,6 @@
/****************************************************************************
*
* Copyright (c) 2016 PX4 Development Team. All rights reserved.
* Copyright (c) 2016-2022 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -45,11 +45,14 @@
#include <errno.h>
#include <sys/ioctl.h>
#ifdef __PX4_NUTTX
#include <nshlib/nshlib.h>
#endif /* __PX4_NUTTX */
#ifdef __PX4_POSIX
#include "../../../platforms/posix/src/px4/common/px4_daemon/pxh.h"
#endif /* __PX4_POSIX */
#ifdef __PX4_CYGWIN
#include <asm/socket.h>
#endif
@@ -69,11 +72,10 @@ MavlinkShell::~MavlinkShell()
int MavlinkShell::start()
{
//this currently only works for NuttX
#ifndef __PX4_NUTTX
//this currently only works for NuttX & POSIX
#if !defined(__PX4_NUTTX) && !defined(__PX4_POSIX)
return -1;
#endif /* __PX4_NUTTX */
#endif
PX4_INFO("Starting mavlink shell");
@@ -113,6 +115,17 @@ int MavlinkShell::start()
fflush(stdout);
fflush(stderr);
#ifdef __PX4_POSIX
int remote_in_fd = dup(_shell_fds[0]); // Input file descriptor for the remote shell
int remote_out_fd = dup(_shell_fds[1]); // Output file descriptor for the remote shell
char r_in[32];
char r_out[32];
sprintf(r_in, "%d", remote_in_fd);
sprintf(r_out, "%d", remote_out_fd);
char *const argv[3] = {r_in, r_out, nullptr};
#else
int fd_backups[2]; //we don't touch stderr, we will redirect it to stdout in the startup of the shell task
for (int i = 0; i < 2; ++i) {
@@ -125,6 +138,7 @@ int MavlinkShell::start()
dup2(_shell_fds[0], 0);
dup2(_shell_fds[1], 1);
#endif
if (ret == 0) {
_task = px4_task_spawn_cmd("mavlink_shell",
@@ -132,13 +146,19 @@ int MavlinkShell::start()
SCHED_PRIORITY_DEFAULT,
2048,
&MavlinkShell::shell_start_thread,
#ifdef __PX4_POSIX
argv);
#else
nullptr);
#endif
if (_task < 0) {
ret = -1;
}
}
#if !defined(__PX4_POSIX)
//restore fd's
for (int i = 0; i < 2; ++i) {
if (dup2(fd_backups[i], i) == -1) {
@@ -148,6 +168,8 @@ int MavlinkShell::start()
close(fd_backups[i]);
}
#endif
//close unused pipe fd's
close(_shell_fds[0]);
close(_shell_fds[1]);
@@ -161,12 +183,26 @@ int MavlinkShell::start()
int MavlinkShell::shell_start_thread(int argc, char *argv[])
{
#ifdef __PX4_NUTTX
dup2(1, 2); //redirect stderror to stdout
#ifdef __PX4_NUTTX
nsh_consolemain(0, NULL);
#endif /* __PX4_NUTTX */
#ifdef __PX4_POSIX
if (argc != 3) {
PX4_ERR("Mavlink shell bug");
return -1;
}
int remote_in_fd = atoi(argv[1]);
int remote_out_fd = atoi(argv[2]);
px4_daemon::Pxh pxh;
pxh.run_remote_pxh(remote_in_fd, remote_out_fd);
#endif
return 0;
}