Clean up of app.h

app.h, generated from app.h_in, has unnecessary code duplication
and isn't a header file (it defines globals, static functions
and doesn't have a header guard, moreover, it has a 'using namespace
std;'). Because of this, a real headerfile that declares the stuff
defined in apps.h was missing leading to even more code duplication:
scattered forward declarations in .cpp files and an often repeated
type of std::map<std::string, px4_main_t>.

This patch moves cmake/qurt/apps.h_in to src/platforms/apps.cpp.in
(with some changes) and removes cmake/posix/apps.h_in.
Then src/platforms/apps.cpp.in is split into src/platforms/apps.cpp.in
and src/platforms/apps.h.in, splitting declarations from definitions.

A typedef is defined for the map (apps_map_type).

The main difference between cmake/posix/apps.h_in and
cmake/qurt/apps.h_in was that the first defined a global 'apps',
while qurt stores the apps in QShell. I opted to get rid of
the global variable (which are in general evil for various reasons)
and used the API of cmake/qurt/apps.h_in where a provided 'apps'
map is initialized with a call to init_app_map. Thus removing
the existing code duplication.
This commit is contained in:
Carlo Wood
2016-10-22 08:31:14 +02:00
committed by Julian Oes
parent c200ef88ed
commit 6fc30c76a6
12 changed files with 143 additions and 209 deletions
+74
View File
@@ -0,0 +1,74 @@
/* definitions of builtin command list - automatically generated, do not edit */
#include "px4_posix.h"
#include "px4_log.h"
#include "apps.h"
#include <iostream>
#include <map>
#include <string>
#include <cstdlib>
extern void px4_show_devices(void);
void init_app_map(apps_map_type &apps)
{
${builtin_apps_string}
apps["shutdown"] = shutdown_main;
apps["list_tasks"] = list_tasks_main;
apps["list_files"] = list_files_main;
apps["list_devices"] = list_devices_main;
apps["list_topics"] = list_topics_main;
apps["sleep"] = sleep_main;
}
void list_builtins(apps_map_type &apps)
{
PX4_INFO("Builtin Commands:\n");
for (apps_map_type::iterator it = apps.begin(); it != apps.end(); ++it)
PX4_INFO("%s : 0x%x\n", it->first.c_str(), it->second);
}
int shutdown_main(int argc, char *argv[])
{
std::cout << "Shutting down" << std::endl;
exit(0);
}
int list_tasks_main(int argc, char *argv[])
{
px4_show_tasks();
return 0;
}
int list_devices_main(int argc, char *argv[])
{
px4_show_devices();
return 0;
}
int list_topics_main(int argc, char *argv[])
{
px4_show_topics();
return 0;
}
int list_files_main(int argc, char *argv[])
{
px4_show_files();
return 0;
}
int sleep_main(int argc, char *argv[])
{
if (argc != 2) {
PX4_WARN( "Usage: sleep <seconds>" );
return 1;
}
unsigned long usecs = 1000000UL * atol(argv[1]);
std::cout << "Sleeping for " << argv[1] << " s; (" << usecs << " us)." << std::endl;
usleep(usecs);
return 0;
}
+27
View File
@@ -0,0 +1,27 @@
/* declarations of builtin command list - automatically generated, do not edit */
#pragma once
#include "px4_tasks.h" // px4_main_t
#include <map>
extern "C" {
${builtin_apps_decl_string}
int shutdown_main(int argc, char *argv[]);
int list_tasks_main(int argc, char *argv[]);
int list_files_main(int argc, char *argv[]);
int list_devices_main(int argc, char *argv[]);
int list_topics_main(int argc, char *argv[]);
int sleep_main(int argc, char *argv[]);
}
// Maps an app name to it's function.
typedef std::map<std::string, px4_main_t> apps_map_type;
// Initialize an apps map.
void init_app_map(apps_map_type &apps);
// List an apps map.
void list_builtins(apps_map_type &apps);
+11 -1
View File
@@ -48,6 +48,8 @@
#include <stdio.h>
#include "apps.h"
#include "px4_middleware.h"
#include "px4_posix.h"
#include "px4_log.h"
#include "DriverFramework.hpp"
#include <termios.h>
#include <sys/stat.h>
@@ -191,6 +193,14 @@ static void print_prompt()
static void run_cmd(const vector<string> &appargs, bool exit_on_fail, bool silently_fail = false)
{
static apps_map_type apps;
static bool initialized = false;
if (!initialized) {
init_app_map(apps);
initialized = true;
}
// command is appargs[0]
string command = appargs[0];
@@ -217,7 +227,7 @@ static void run_cmd(const vector<string> &appargs, bool exit_on_fail, bool silen
}
} else if (command.compare("help") == 0) {
list_builtins();
list_builtins(apps);
} else if (command.length() == 0 || command[0] == '#') {
// Do nothing
+4 -6
View File
@@ -55,8 +55,6 @@
using namespace std;
extern void init_app_map(map<string, px4_main_t> &apps);
extern void list_builtins(map<string, px4_main_t> &apps);
static px4_task_t g_dspal_task = -1;
__BEGIN_DECLS
@@ -70,13 +68,13 @@ void qurt_external_hook(void)
{
}
static void run_cmd(map<string, px4_main_t> &apps, const vector<string> &appargs)
static void run_cmd(apps_map_type &apps, const vector<string> &appargs)
{
// command is appargs[0]
string command = appargs[0];
//replaces app.find with iterator code to avoid null pointer exception
for (map<string, px4_main_t>::iterator it = apps.begin(); it != apps.end(); ++it)
for (apps_map_type::iterator it = apps.begin(); it != apps.end(); ++it)
if (it->first == command) {
// one for command name, one for null terminator
const char *arg[MAX_ARGS + 2];
@@ -117,7 +115,7 @@ void eat_whitespace(const char *&b, int &i)
i = 0;
}
static void process_commands(map<string, px4_main_t> &apps, const char *cmds)
static void process_commands(apps_map_type &apps, const char *cmds)
{
vector<string> appargs;
int i = 0;
@@ -220,7 +218,7 @@ const char *get_commands()
int dspal_entry(int argc, char *argv[])
{
PX4_INFO("In dspal_entry");
map<string, px4_main_t> apps;
apps_map_type apps;
init_app_map(apps);
DriverFramework::Framework::initialize();
px4::init_once();