mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-06-01 02:55:07 +08:00
Merge pull request #2785 from mcharleb/posix_daemon_mode
Add daemon mode to posix build
This commit is contained in:
@@ -47,7 +47,7 @@ endif
|
|||||||
#
|
#
|
||||||
HEXAGON_TOOLS_ROOT ?= /opt/6.4.03
|
HEXAGON_TOOLS_ROOT ?= /opt/6.4.03
|
||||||
#HEXAGON_TOOLS_ROOT = /opt/6.4.05
|
#HEXAGON_TOOLS_ROOT = /opt/6.4.05
|
||||||
HEXAGON_SDK_ROOT = /opt/Hexagon_SDK/2.0
|
HEXAGON_SDK_ROOT ?= /opt/Hexagon_SDK/2.0
|
||||||
V_ARCH = v5
|
V_ARCH = v5
|
||||||
CROSSDEV = hexagon-
|
CROSSDEV = hexagon-
|
||||||
HEXAGON_BIN = $(addsuffix /gnu/bin,$(HEXAGON_TOOLS_ROOT))
|
HEXAGON_BIN = $(addsuffix /gnu/bin,$(HEXAGON_TOOLS_ROOT))
|
||||||
|
|||||||
@@ -42,8 +42,10 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
namespace px4 {
|
namespace px4
|
||||||
|
{
|
||||||
void init_once(void);
|
void init_once(void);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,18 +56,31 @@ typedef int (*px4_main_t)(int argc, char *argv[]);
|
|||||||
#include "apps.h"
|
#include "apps.h"
|
||||||
#include "px4_middleware.h"
|
#include "px4_middleware.h"
|
||||||
|
|
||||||
static void run_cmd(const vector<string> &appargs) {
|
static bool _ExitFlag = false;
|
||||||
|
extern "C" {
|
||||||
|
void _SigIntHandler(int sig_num);
|
||||||
|
void _SigIntHandler(int sig_num)
|
||||||
|
{
|
||||||
|
_ExitFlag = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void run_cmd(const vector<string> &appargs)
|
||||||
|
{
|
||||||
// command is appargs[0]
|
// command is appargs[0]
|
||||||
string command = appargs[0];
|
string command = appargs[0];
|
||||||
cout << "----------------------------------\n";
|
cout << "----------------------------------\n";
|
||||||
|
|
||||||
if (apps.find(command) != apps.end()) {
|
if (apps.find(command) != apps.end()) {
|
||||||
const char *arg[appargs.size() + 2];
|
const char *arg[appargs.size() + 2];
|
||||||
|
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
|
|
||||||
while (i < appargs.size() && appargs[i] != "") {
|
while (i < appargs.size() && appargs[i] != "") {
|
||||||
arg[i] = (char *)appargs[i].c_str();
|
arg[i] = (char *)appargs[i].c_str();
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
arg[i] = (char *)0;
|
arg[i] = (char *)0;
|
||||||
cout << "Running: " << command << "\n";
|
cout << "Running: " << command << "\n";
|
||||||
apps[command](i, (char **)arg);
|
apps[command](i, (char **)arg);
|
||||||
@@ -78,40 +93,110 @@ static void run_cmd(const vector<string> &appargs) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void usage()
|
||||||
|
{
|
||||||
|
|
||||||
|
cout << "./mainapp [-d] [startup_config] -h" << std::endl;
|
||||||
|
cout << " -d - Optional flag to run the app in daemon mode and does not take listen for user input." <<
|
||||||
|
std::endl;
|
||||||
|
cout << " This is needed if mainapp is intended to be run as a upstart job on linux" << std::endl;
|
||||||
|
cout << "<startup_config> - config file for starting/stopping px4 modules" << std::endl;
|
||||||
|
cout << " -h - help/usage information" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
static void process_line(string &line)
|
static void process_line(string &line)
|
||||||
{
|
{
|
||||||
vector<string> appargs(8);
|
vector<string> appargs(8);
|
||||||
|
|
||||||
stringstream(line) >> appargs[0] >> appargs[1] >> appargs[2] >> appargs[3] >> appargs[4] >> appargs[5] >> appargs[6] >> appargs[7];
|
stringstream(line) >> appargs[0] >> appargs[1] >> appargs[2] >> appargs[3] >> appargs[4] >> appargs[5] >> appargs[6] >>
|
||||||
|
appargs[7];
|
||||||
run_cmd(appargs);
|
run_cmd(appargs);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
|
bool daemon_mode = false;
|
||||||
|
signal(SIGINT, _SigIntHandler);
|
||||||
|
|
||||||
|
int index = 1;
|
||||||
|
bool error_detected = false;
|
||||||
|
char *commands_file = nullptr;
|
||||||
|
|
||||||
|
while (index < argc) {
|
||||||
|
if (argv[index][0] == '-') {
|
||||||
|
// the arg starts with -
|
||||||
|
if (strcmp(argv[index], "-d") == 0) {
|
||||||
|
daemon_mode = true;
|
||||||
|
|
||||||
|
} else if (strcmp(argv[index], "-h") == 0) {
|
||||||
|
usage();
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
} else {
|
||||||
|
PX4_WARN("Unknown/unhandled parameter: %s", argv[index]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// this is an argument that does not have '-' prefix; treat it like a file name
|
||||||
|
ifstream infile(argv[index]);
|
||||||
|
|
||||||
|
if (infile.good()) {
|
||||||
|
infile.close();
|
||||||
|
commands_file = argv[index];
|
||||||
|
|
||||||
|
} else {
|
||||||
|
PX4_WARN("Error opening file: %s", argv[index]);
|
||||||
|
error_detected = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
++index;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!error_detected) {
|
||||||
px4::init_once();
|
px4::init_once();
|
||||||
|
|
||||||
px4::init(argc, argv, "mainapp");
|
px4::init(argc, argv, "mainapp");
|
||||||
|
|
||||||
// Execute a command list of provided
|
//if commandfile is present, process the commands from the file
|
||||||
if (argc == 2) {
|
if (commands_file != nullptr) {
|
||||||
ifstream infile(argv[1]);
|
ifstream infile(commands_file);
|
||||||
|
|
||||||
if (!infile) {
|
|
||||||
cout << "failed opening script" << argv[1] << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (infile.is_open()) {
|
||||||
for (string line; getline(infile, line, '\n');) {
|
for (string line; getline(infile, line, '\n');) {
|
||||||
process_line(line);
|
process_line(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
PX4_WARN("Error opening file: %s", commands_file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!daemon_mode) {
|
||||||
string mystr;
|
string mystr;
|
||||||
|
|
||||||
while(1) {
|
while (!_ExitFlag) {
|
||||||
cout << "Enter a command and its args:" << endl;
|
cout << "Enter a command and its args:" << endl;
|
||||||
getline(cin, mystr);
|
getline(cin, mystr);
|
||||||
process_line(mystr);
|
process_line(mystr);
|
||||||
mystr = "";
|
mystr = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
while (!_ExitFlag) {
|
||||||
|
sleep(1000000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (px4_task_is_running("muorb")) {
|
||||||
|
// sending muorb stop is needed if it is running to exit cleanly
|
||||||
|
vector<string> muorb_stop_cmd = { "muorb", "stop" };
|
||||||
|
run_cmd(muorb_stop_cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
vector<string> shutdown_cmd = { "shutdown" };
|
||||||
|
run_cmd(shutdown_cmd);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -280,6 +280,17 @@ void px4_show_tasks()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool px4_task_is_running(const char *taskname)
|
||||||
|
{
|
||||||
|
int idx;
|
||||||
|
for (idx=0; idx < PX4_MAX_TASKS; idx++)
|
||||||
|
{
|
||||||
|
if (taskmap[idx].isused && (strcmp(taskmap[idx].name.c_str(), taskname) == 0)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
__BEGIN_DECLS
|
__BEGIN_DECLS
|
||||||
|
|
||||||
unsigned long px4_getpid()
|
unsigned long px4_getpid()
|
||||||
|
|||||||
@@ -117,5 +117,8 @@ __EXPORT void px4_task_exit(int ret);
|
|||||||
/** Show a list of running tasks **/
|
/** Show a list of running tasks **/
|
||||||
__EXPORT void px4_show_tasks(void);
|
__EXPORT void px4_show_tasks(void);
|
||||||
|
|
||||||
|
/** See if a task is running **/
|
||||||
|
__EXPORT bool px4_task_is_running(const char *taskname);
|
||||||
|
|
||||||
__END_DECLS
|
__END_DECLS
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user