List container improvements and testing

- support range based for loops
 - add remove() method to remove a node
 - add clear() to empty entire list and delete nodes
 - add empty() helper
This commit is contained in:
Daniel Agar
2019-02-22 13:34:23 -05:00
parent 6c3e79f361
commit e2bf4b1894
15 changed files with 335 additions and 88 deletions
+14 -49
View File
@@ -218,8 +218,6 @@ Mavlink::Mavlink() :
_wait_to_transmit(false),
_received_messages(false),
_main_loop_delay(1000),
_subscriptions(nullptr),
_streams(nullptr),
_mavlink_shell(nullptr),
_mavlink_ulog(nullptr),
_mavlink_ulog_stop_requested(false),
@@ -1349,9 +1347,7 @@ MavlinkOrbSubscription *Mavlink::add_orb_subscription(const orb_id_t topic, int
{
if (!disable_sharing) {
/* check if already subscribed to this topic */
MavlinkOrbSubscription *sub;
LL_FOREACH(_subscriptions, sub) {
for (MavlinkOrbSubscription *sub : _subscriptions) {
if (sub->get_topic() == topic && sub->get_instance() == instance) {
/* already subscribed */
return sub;
@@ -1362,7 +1358,7 @@ MavlinkOrbSubscription *Mavlink::add_orb_subscription(const orb_id_t topic, int
/* add new subscription */
MavlinkOrbSubscription *sub_new = new MavlinkOrbSubscription(topic, instance);
LL_APPEND(_subscriptions, sub_new);
_subscriptions.add(sub_new);
return sub_new;
}
@@ -1382,9 +1378,7 @@ Mavlink::configure_stream(const char *stream_name, const float rate)
interval = -1;
}
/* search if stream exists */
MavlinkStream *stream;
LL_FOREACH(_streams, stream) {
for (const auto &stream : _streams) {
if (strcmp(stream_name, stream->get_name()) == 0) {
if (interval != 0) {
/* set new interval */
@@ -1392,8 +1386,8 @@ Mavlink::configure_stream(const char *stream_name, const float rate)
} else {
/* delete stream */
LL_DELETE(_streams, stream);
delete stream;
_streams.deleteNode(stream);
return OK; // must finish with loop after node is deleted
}
return OK;
@@ -1407,11 +1401,11 @@ Mavlink::configure_stream(const char *stream_name, const float rate)
// search for stream with specified name in supported streams list
// create new instance if found
stream = create_mavlink_stream(stream_name, this);
MavlinkStream *stream = create_mavlink_stream(stream_name, this);
if (stream != nullptr) {
stream->set_interval(interval);
LL_APPEND(_streams, stream);
_streams.add(stream);
return OK;
}
@@ -1455,7 +1449,6 @@ Mavlink::configure_stream_threadsafe(const char *stream_name, const float rate)
int
Mavlink::message_buffer_init(int size)
{
_message_buffer.size = size;
_message_buffer.write_ptr = 0;
_message_buffer.read_ptr = 0;
@@ -1607,8 +1600,7 @@ Mavlink::update_rate_mult()
float rate = 0.0f;
/* scale down rates if their theoretical bandwidth is exceeding the link bandwidth */
MavlinkStream *stream;
LL_FOREACH(_streams, stream) {
for (const auto &stream : _streams) {
if (stream->const_rate()) {
const_rate += (stream->get_interval() > 0) ? stream->get_size_avg() * 1000000.0f / stream->get_interval() : 0;
@@ -2467,8 +2459,7 @@ Mavlink::task_main(int argc, char *argv[])
}
/* update streams */
MavlinkStream *stream;
LL_FOREACH(_streams, stream) {
for (const auto &stream : _streams) {
stream->update(t);
if (!_first_heartbeat_sent) {
@@ -2568,28 +2559,10 @@ Mavlink::task_main(int argc, char *argv[])
_subscribe_to_stream = nullptr;
/* delete streams */
MavlinkStream *stream_to_del = nullptr;
MavlinkStream *stream_next = _streams;
while (stream_next != nullptr) {
stream_to_del = stream_next;
stream_next = stream_to_del->next;
delete stream_to_del;
}
_streams = nullptr;
_streams.clear();
/* delete subscriptions */
MavlinkOrbSubscription *sub_to_del = nullptr;
MavlinkOrbSubscription *sub_next = _subscriptions;
while (sub_next != nullptr) {
sub_to_del = sub_next;
sub_next = sub_to_del->next;
delete sub_to_del;
}
_subscriptions = nullptr;
_subscriptions.clear();
if (_uart_fd >= 0 && !_is_usb_uart) {
/* close UART */
@@ -2628,15 +2601,7 @@ void Mavlink::publish_telemetry_status()
_tstatus.forwarding = get_forwarding_on();
_tstatus.mavlink_v2 = (_protocol_version == 2);
int num_streams = 0;
MavlinkStream *stream;
LL_FOREACH(_streams, stream) {
// count
num_streams++;
}
_tstatus.streams = num_streams;
_tstatus.streams = _streams.size();
_tstatus.timestamp = hrt_absolute_time();
int instance;
@@ -2873,8 +2838,8 @@ Mavlink::display_status_streams()
printf("\t%-20s%-16s %s\n", "Name", "Rate Config (current) [Hz]", "Message Size (if active) [B]");
const float rate_mult = _rate_mult;
MavlinkStream *stream;
LL_FOREACH(_streams, stream) {
for (const auto &stream : _streams) {
const int interval = stream->get_interval();
const unsigned size = stream->get_size();
char rate_str[20];
+5 -3
View File
@@ -59,6 +59,8 @@
#include <net/if.h>
#endif
#include <containers/List.hpp>
#include <systemlib/uthash/utlist.h>
#include <parameters/param.h>
#include <perf/perf_counter.h>
#include <pthread.h>
@@ -392,7 +394,7 @@ public:
*/
void send_protocol_version();
MavlinkStream *get_streams() const { return _streams; }
List<MavlinkStream *> &get_streams() { return _streams; }
float get_rate_mult() const { return _rate_mult; }
@@ -546,8 +548,8 @@ private:
unsigned _main_loop_delay; /**< mainloop delay, depends on data rate */
MavlinkOrbSubscription *_subscriptions;
MavlinkStream *_streams;
List<MavlinkOrbSubscription *> _subscriptions;
List<MavlinkStream *> _streams;
MavlinkShell *_mavlink_shell;
MavlinkULog *_mavlink_ulog;
@@ -41,14 +41,13 @@
#ifndef MAVLINK_ORB_SUBSCRIPTION_H_
#define MAVLINK_ORB_SUBSCRIPTION_H_
#include <systemlib/uthash/utlist.h>
#include <drivers/drv_hrt.h>
#include <containers/List.hpp>
#include "uORB/uORB.h" // orb_id_t
class MavlinkOrbSubscription
class MavlinkOrbSubscription : public ListNode<MavlinkOrbSubscription *>
{
public:
MavlinkOrbSubscription *next{nullptr}; ///< pointer to next subscription in list
MavlinkOrbSubscription(const orb_id_t topic, int instance);
~MavlinkOrbSubscription();
+1 -2
View File
@@ -1942,8 +1942,7 @@ MavlinkReceiver::get_message_interval(int msgId)
{
unsigned interval = 0;
MavlinkStream *stream = nullptr;
LL_FOREACH(_mavlink->get_streams(), stream) {
for (const auto &stream : _mavlink->get_streams()) {
if (stream->get_id() == msgId) {
interval = stream->get_interval();
break;
-1
View File
@@ -45,7 +45,6 @@
#include "mavlink_main.h"
MavlinkStream::MavlinkStream(Mavlink *mavlink) :
ModuleParams(nullptr),
_mavlink(mavlink)
{
_last_sent = hrt_absolute_time();
+2 -2
View File
@@ -43,14 +43,14 @@
#include <drivers/drv_hrt.h>
#include <px4_module_params.h>
#include <containers/List.hpp>
class Mavlink;
class MavlinkStream : public ModuleParams
class MavlinkStream : public ListNode<MavlinkStream *>
{
public:
MavlinkStream *next{nullptr};
MavlinkStream(Mavlink *mavlink);
virtual ~MavlinkStream() = default;
+5 -5
View File
@@ -163,7 +163,7 @@ void uORB::DeviceMaster::printStatistics(bool reset)
lock();
for (DeviceNode *node = _node_list.getHead(); node != nullptr; node = node->getSibling()) {
for (const auto &node : _node_list) {
if (node->print_statistics(reset)) {
had_print = true;
}
@@ -189,7 +189,7 @@ void uORB::DeviceMaster::addNewDeviceNodes(DeviceNodeStatisticsData **first_node
}
}
for (DeviceNode *node = _node_list.getHead(); node != nullptr; node = node->getSibling()) {
for (const auto &node : _node_list) {
++num_topics;
@@ -262,7 +262,7 @@ void uORB::DeviceMaster::showTop(char **topic_filter, int num_filters)
lock();
if (_node_list.getHead() == nullptr) {
if (_node_list.empty()) {
unlock();
PX4_INFO("no active topics");
return;
@@ -380,7 +380,7 @@ uORB::DeviceNode *uORB::DeviceMaster::getDeviceNode(const char *nodepath)
{
lock();
for (DeviceNode *node = _node_list.getHead(); node != nullptr; node = node->getSibling()) {
for (uORB::DeviceNode *node : _node_list) {
if (strcmp(node->get_devname(), nodepath) == 0) {
unlock();
return node;
@@ -405,7 +405,7 @@ uORB::DeviceNode *uORB::DeviceMaster::getDeviceNode(const struct orb_metadata *m
uORB::DeviceNode *uORB::DeviceMaster::getDeviceNodeLocked(const struct orb_metadata *meta, const uint8_t instance)
{
for (DeviceNode *node = _node_list.getHead(); node != nullptr; node = node->getSibling()) {
for (uORB::DeviceNode *node : _node_list) {
if ((strcmp(node->get_name(), meta->o_name) == 0) && (node->get_instance() == instance)) {
return node;
}