mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-18 11:40:34 +08:00
topic_listener: avoid code generation, use existing metadata at runtime
This reduces flash size for v5 by ~110KB, the topic listener now only adds about 1.2KB.
This commit is contained in:
@@ -31,31 +31,11 @@
|
||||
#
|
||||
############################################################################
|
||||
|
||||
add_custom_command(OUTPUT listener_generated.cpp
|
||||
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/generate_listener.py ${PX4_SOURCE_DIR} ${msg_files} > listener_generated.cpp
|
||||
DEPENDS generate_listener.py ${msg_files}
|
||||
)
|
||||
|
||||
add_custom_target(generate_topic_listener
|
||||
DEPENDS
|
||||
listener_generated.cpp
|
||||
generate_listener.py
|
||||
)
|
||||
add_dependencies(prebuild_targets generate_topic_listener)
|
||||
|
||||
px4_add_module(
|
||||
MODULE systemcmds__topic_listener
|
||||
MAIN listener
|
||||
COMPILE_FLAGS
|
||||
STACK_MAIN 4096
|
||||
INCLUDES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
SRCS
|
||||
listener_main.cpp
|
||||
${CMAKE_CURRENT_BINARY_DIR}/listener_generated.cpp
|
||||
DEPENDS
|
||||
generate_topic_listener
|
||||
uorb_msgs
|
||||
)
|
||||
|
||||
|
||||
@@ -1,135 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import glob
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
|
||||
# This script is run from Build/<target>_default.build/$(PX4_BASE)/Firmware/src/systemcmds/topic_listener
|
||||
|
||||
# argv[1] must be the full path of the top Firmware dir
|
||||
# argv[2] - argv[n] is the full list of msg files
|
||||
|
||||
raw_messages = sys.argv[2:]
|
||||
|
||||
messages = []
|
||||
topics = []
|
||||
message_elements = []
|
||||
|
||||
for index,m in enumerate(raw_messages):
|
||||
topic_list = []
|
||||
|
||||
msg_path = sys.argv[1]+ '/msg/' + m
|
||||
|
||||
if os.path.isfile(msg_path):
|
||||
# first try opening file in msg/ directory
|
||||
f = open(msg_path,'r')
|
||||
else:
|
||||
# otherwise try opening directly (could be an external module msg)
|
||||
f = open(m,'r')
|
||||
|
||||
for line in f.readlines():
|
||||
items = re.split('\s+', line.strip())
|
||||
|
||||
if '# TOPICS' == ' '.join(items[:2]):
|
||||
for topic in items[2:]:
|
||||
topic_list.append(topic)
|
||||
|
||||
f.close()
|
||||
|
||||
(m_head, m_tail) = os.path.split(m)
|
||||
message = m_tail.split('.')[0]
|
||||
|
||||
if len(topic_list) == 0:
|
||||
topic_list.append(message)
|
||||
|
||||
for topic in topic_list:
|
||||
messages.append(message)
|
||||
topics.append(topic)
|
||||
|
||||
num_messages = len(messages);
|
||||
|
||||
print("""
|
||||
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2015 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
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
/**
|
||||
* @file topic_listener.cpp
|
||||
*
|
||||
* Autogenerated by Tools/generate_listener.py
|
||||
*
|
||||
* Tool for listening to topics when running flight stack on linux.
|
||||
*/
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <px4_platform_common/app.h>
|
||||
#include <px4_platform_common/px4_config.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <uORB/uORB.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
#include <topic_listener.hpp>
|
||||
#include <topic_listener_generated.hpp>
|
||||
|
||||
""")
|
||||
for m in set(messages):
|
||||
print("#include <uORB/topics/%s.h>" % m)
|
||||
|
||||
print ("""
|
||||
\nvoid listener_generated(char * topic_name, int topic_instance, int topic_rate, int num_msgs) {
|
||||
""")
|
||||
|
||||
print("""
|
||||
unsigned topic_interval = 0;
|
||||
if (topic_rate != 0) {
|
||||
topic_interval = 1000 / topic_rate;
|
||||
}
|
||||
""")
|
||||
|
||||
for index, (m, t) in enumerate(zip(messages, topics)):
|
||||
if index == 0:
|
||||
print("\tif (strcmp(topic_name,\"%s\") == 0) {" % (t))
|
||||
else:
|
||||
print("\t} else if (strcmp(topic_name,\"%s\") == 0) {" % (t))
|
||||
print("\t\tlistener(listener_print_topic<%s_s>, ORB_ID(%s), num_msgs, topic_instance, topic_interval);" % (m, t))
|
||||
|
||||
print("\t} else {")
|
||||
print("\t\t PX4_INFO_RAW(\" Topic did not match any known topics\\n\");")
|
||||
print("\t}")
|
||||
|
||||
print("}\n")
|
||||
@@ -42,8 +42,8 @@
|
||||
|
||||
#include <poll.h>
|
||||
|
||||
#include <uORB/topics/uORBTopics.hpp>
|
||||
#include "topic_listener.hpp"
|
||||
#include "topic_listener_generated.hpp"
|
||||
|
||||
// Amount of time to wait when listening for a message, before giving up.
|
||||
static constexpr float MESSAGE_TIMEOUT_S = 2.0f;
|
||||
@@ -52,7 +52,7 @@ extern "C" __EXPORT int listener_main(int argc, char *argv[]);
|
||||
|
||||
static void usage();
|
||||
|
||||
void listener(listener_print_topic_cb cb, const orb_id_t &id, unsigned num_msgs, int topic_instance,
|
||||
void listener(const orb_id_t &id, unsigned num_msgs, int topic_instance,
|
||||
unsigned topic_interval)
|
||||
{
|
||||
|
||||
@@ -69,7 +69,7 @@ void listener(listener_print_topic_cb cb, const orb_id_t &id, unsigned num_msgs,
|
||||
if (instances == 1) {
|
||||
PX4_INFO_RAW("\nTOPIC: %s\n", id->o_name);
|
||||
int sub = orb_subscribe(id);
|
||||
cb(id, sub);
|
||||
listener_print_topic(id, sub);
|
||||
orb_unsubscribe(sub);
|
||||
|
||||
} else if (instances > 1) {
|
||||
@@ -79,7 +79,7 @@ void listener(listener_print_topic_cb cb, const orb_id_t &id, unsigned num_msgs,
|
||||
if (orb_exists(id, i) == PX4_OK) {
|
||||
PX4_INFO_RAW("\nInstance %d:\n", i);
|
||||
int sub = orb_subscribe_multi(id, i);
|
||||
cb(id, sub);
|
||||
listener_print_topic(id, sub);
|
||||
orb_unsubscribe(sub);
|
||||
}
|
||||
}
|
||||
@@ -141,7 +141,7 @@ void listener(listener_print_topic_cb cb, const orb_id_t &id, unsigned num_msgs,
|
||||
|
||||
PX4_INFO_RAW("\nTOPIC: %s instance %d #%d\n", id->o_name, topic_instance, msgs_received);
|
||||
|
||||
int ret = cb(id, sub);
|
||||
int ret = listener_print_topic(id, sub);
|
||||
|
||||
if (ret != PX4_OK) {
|
||||
PX4_ERR("listener callback failed (%i)", ret);
|
||||
@@ -208,7 +208,29 @@ int listener_main(int argc, char *argv[])
|
||||
}
|
||||
}
|
||||
|
||||
listener_generated(topic_name, topic_instance, topic_rate, num_msgs);
|
||||
|
||||
unsigned topic_interval = 0;
|
||||
|
||||
if (topic_rate != 0) {
|
||||
topic_interval = 1000 / topic_rate;
|
||||
}
|
||||
|
||||
const orb_metadata *const *topics = orb_get_topics();
|
||||
const orb_metadata *found_topic = nullptr;
|
||||
|
||||
for (size_t i = 0; i < orb_topics_count(); i++) {
|
||||
if (strcmp(topics[i]->o_name, topic_name) == 0) {
|
||||
found_topic = topics[i];
|
||||
}
|
||||
}
|
||||
|
||||
if (found_topic) {
|
||||
listener(found_topic, num_msgs, topic_instance, topic_interval);
|
||||
|
||||
} else {
|
||||
PX4_INFO_RAW("Topic %s did not match any known topics\n", topic_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -50,21 +50,24 @@
|
||||
#include <stdlib.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
typedef int(*listener_print_topic_cb)(const orb_id_t &orb_id, int subscription);
|
||||
|
||||
template <typename T>
|
||||
int listener_print_topic(const orb_id_t &orb_id, int subscription)
|
||||
inline int listener_print_topic(const orb_id_t &orb_id, int subscription)
|
||||
{
|
||||
T container;
|
||||
static constexpr int max_size = 512;
|
||||
alignas(8) char container[max_size];
|
||||
|
||||
if (orb_id->o_size > max_size) {
|
||||
PX4_ERR("topic %s too large (%i > %i)", orb_id->o_name, orb_id->o_size, max_size);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ret = orb_copy(orb_id, subscription, &container);
|
||||
|
||||
if (ret == PX4_OK) {
|
||||
print_message(container);
|
||||
orb_print_message_internal(orb_id, &container, true);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void listener(listener_print_topic_cb cb, const orb_id_t &id, unsigned num_msgs, int topic_instance,
|
||||
void listener(const orb_id_t &id, unsigned num_msgs, int topic_instance,
|
||||
unsigned topic_interval);
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
/****************************************************************************
|
||||
*
|
||||
* Copyright (c) 2018 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
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
* 3. Neither the name PX4 nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
||||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
||||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
||||
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
||||
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||||
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
void listener_generated(char *topic_name, int topic_instance, int topic_rate, int num_msgs);
|
||||
Reference in New Issue
Block a user