uORB: fix static orb_exists call for protected build

- unify ORBSet to maximum code coverage
This commit is contained in:
Daniel Agar 2022-09-24 13:28:39 -04:00
parent 2717137982
commit 462d0af384
3 changed files with 44 additions and 31 deletions

View File

@ -37,14 +37,12 @@ class ORBSet
{ {
public: public:
struct Node { struct Node {
struct Node *next; Node *next{nullptr};
const char *node_name; const char *node_name{nullptr};
}; };
ORBSet() : ORBSet() = default;
_top(nullptr),
_end(nullptr)
{ }
~ORBSet() ~ORBSet()
{ {
while (_top != nullptr) { while (_top != nullptr) {
@ -52,11 +50,14 @@ public:
if (_top->next == nullptr) { if (_top->next == nullptr) {
free((void *)_top->node_name); free((void *)_top->node_name);
free(_top); _top->node_name = nullptr;
delete _top;
_top = nullptr; _top = nullptr;
} }
} }
} }
void insert(const char *node_name) void insert(const char *node_name)
{ {
Node **p; Node **p;
@ -68,7 +69,7 @@ public:
p = &_end->next; p = &_end->next;
} }
*p = (Node *)malloc(sizeof(Node)); *p = new Node();
if (_end) { if (_end) {
_end = _end->next; _end = _end->next;
@ -102,8 +103,13 @@ public:
if (_top && (strcmp(_top->node_name, node_name) == 0)) { if (_top && (strcmp(_top->node_name, node_name) == 0)) {
p = _top->next; p = _top->next;
free((void *)_top->node_name);
free(_top); if (_top->node_name) {
free((void *)_top->node_name);
_top->node_name = nullptr;
}
delete _top;
_top = p; _top = p;
if (_top == nullptr) { if (_top == nullptr) {
@ -135,11 +141,17 @@ private:
} }
a->next = b->next; a->next = b->next;
free((void *)b->node_name);
free(b); if (b->node_name) {
free((void *)b->node_name);
b->node_name = nullptr;
}
delete b;
b = nullptr;
} }
} }
Node *_top; Node *_top{nullptr};
Node *_end; Node *_end{nullptr};
}; };

View File

@ -120,7 +120,12 @@ int uORB::Manager::orb_ioctl(unsigned int cmd, unsigned long arg)
orbiocdevexists_t *data = (orbiocdevexists_t *)arg; orbiocdevexists_t *data = (orbiocdevexists_t *)arg;
if (data->check_advertised) { if (data->check_advertised) {
data->ret = uORB::Manager::orb_exists(get_orb_meta(data->orb_id), data->instance); if (uORB::Manager::get_instance()) {
data->ret = uORB::Manager::get_instance()->orb_exists(get_orb_meta(data->orb_id), data->instance);
} else {
data->ret = PX4_ERROR;
}
} else { } else {
data->ret = uORB::Manager::orb_device_node_exists(data->orb_id, data->instance) ? PX4_OK : PX4_ERROR; data->ret = uORB::Manager::orb_device_node_exists(data->orb_id, data->instance) ? PX4_OK : PX4_ERROR;
@ -130,7 +135,7 @@ int uORB::Manager::orb_ioctl(unsigned int cmd, unsigned long arg)
case ORBIOCDEVADVERTISE: { case ORBIOCDEVADVERTISE: {
orbiocdevadvertise_t *data = (orbiocdevadvertise_t *)arg; orbiocdevadvertise_t *data = (orbiocdevadvertise_t *)arg;
uORB::DeviceMaster *dev = uORB::Manager::get_instance()->get_device_master(); uORB::DeviceMaster *dev = uORB::Manager::get_instance()->get_device_master();
if (dev) { if (dev) {
data->ret = dev->advertise(data->meta, data->is_advertiser, data->instance); data->ret = dev->advertise(data->meta, data->is_advertiser, data->instance);
@ -230,6 +235,10 @@ int uORB::Manager::orb_ioctl(unsigned int cmd, unsigned long arg)
int uORB::Manager::orb_exists(const struct orb_metadata *meta, int instance) int uORB::Manager::orb_exists(const struct orb_metadata *meta, int instance)
{ {
if (meta == nullptr) {
return PX4_ERROR;
}
int ret = PX4_ERROR; int ret = PX4_ERROR;
// instance valid range: [0, ORB_MULTI_MAX_INSTANCES) // instance valid range: [0, ORB_MULTI_MAX_INSTANCES)
@ -237,7 +246,7 @@ int uORB::Manager::orb_exists(const struct orb_metadata *meta, int instance)
return ret; return ret;
} }
uORB::DeviceMaster *dev = uORB::Manager::get_instance()->get_device_master(); uORB::DeviceMaster *dev = uORB::Manager::get_instance()->get_device_master();
if (dev) { if (dev) {
uORB::DeviceNode *node = dev->getDeviceNode(meta, instance); uORB::DeviceNode *node = dev->getDeviceNode(meta, instance);
@ -266,8 +275,10 @@ int uORB::Manager::orb_exists(const struct orb_metadata *meta, int instance)
ret = px4_access(path, F_OK); ret = px4_access(path, F_OK);
if (ret == -1 && meta != nullptr && !_remote_topics.empty()) { if (ret == -1) {
ret = (_remote_topics.find(meta->o_name) != _remote_topics.end()) ? OK : PX4_ERROR; if (_remote_topics.find(meta->o_name)) {
ret = 0;
}
} }
if (ret == 0) { if (ret == 0) {
@ -709,12 +720,9 @@ int16_t uORB::Manager::process_received_message(const char *messageName, int32_t
bool uORB::Manager::is_remote_subscriber_present(const char *messageName) bool uORB::Manager::is_remote_subscriber_present(const char *messageName)
{ {
#ifdef __PX4_NUTTX
return _remote_subscriber_topics.find(messageName); return _remote_subscriber_topics.find(messageName);
#else
return (_remote_subscriber_topics.find(messageName) != _remote_subscriber_topics.end());
#endif
} }
#endif /* ORB_COMMUNICATOR */ #endif /* ORB_COMMUNICATOR */
#ifdef ORB_USE_PUBLISHER_RULES #ifdef ORB_USE_PUBLISHER_RULES

View File

@ -41,15 +41,8 @@
#include <uORB/topics/uORBTopics.hpp> // For ORB_ID enum #include <uORB/topics/uORBTopics.hpp> // For ORB_ID enum
#include <stdint.h> #include <stdint.h>
#ifdef __PX4_NUTTX
#include "ORBSet.hpp"
#else
#include <string>
#include <set>
#define ORBSet std::set<std::string>
#endif
#ifdef ORB_COMMUNICATOR #ifdef ORB_COMMUNICATOR
#include "ORBSet.hpp"
#include "uORBCommunicator.hpp" #include "uORBCommunicator.hpp"
#endif /* ORB_COMMUNICATOR */ #endif /* ORB_COMMUNICATOR */