mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-17 05:23:34 +08:00
uORB: Update code style
This commit is contained in:
@@ -65,9 +65,10 @@ public:
|
|||||||
* between updates
|
* between updates
|
||||||
*/
|
*/
|
||||||
SubscriptionBase(const struct orb_metadata *meta,
|
SubscriptionBase(const struct orb_metadata *meta,
|
||||||
unsigned interval=0) :
|
unsigned interval = 0) :
|
||||||
_meta(meta),
|
_meta(meta),
|
||||||
_handle() {
|
_handle()
|
||||||
|
{
|
||||||
setHandle(orb_subscribe(getMeta()));
|
setHandle(orb_subscribe(getMeta()));
|
||||||
orb_set_interval(getHandle(), interval);
|
orb_set_interval(getHandle(), interval);
|
||||||
}
|
}
|
||||||
@@ -75,7 +76,8 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Check if there is a new update.
|
* Check if there is a new update.
|
||||||
* */
|
* */
|
||||||
bool updated() {
|
bool updated()
|
||||||
|
{
|
||||||
bool isUpdated = false;
|
bool isUpdated = false;
|
||||||
orb_check(_handle, &isUpdated);
|
orb_check(_handle, &isUpdated);
|
||||||
return isUpdated;
|
return isUpdated;
|
||||||
@@ -85,7 +87,8 @@ public:
|
|||||||
* Update the struct
|
* Update the struct
|
||||||
* @param data The uORB message struct we are updating.
|
* @param data The uORB message struct we are updating.
|
||||||
*/
|
*/
|
||||||
void update(void * data) {
|
void update(void *data)
|
||||||
|
{
|
||||||
if (updated()) {
|
if (updated()) {
|
||||||
orb_copy(_meta, _handle, data);
|
orb_copy(_meta, _handle, data);
|
||||||
}
|
}
|
||||||
@@ -94,7 +97,8 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Deconstructor
|
* Deconstructor
|
||||||
*/
|
*/
|
||||||
virtual ~SubscriptionBase() {
|
virtual ~SubscriptionBase()
|
||||||
|
{
|
||||||
orb_unsubscribe(_handle);
|
orb_unsubscribe(_handle);
|
||||||
}
|
}
|
||||||
// accessors
|
// accessors
|
||||||
@@ -108,9 +112,9 @@ protected:
|
|||||||
int _handle;
|
int _handle;
|
||||||
private:
|
private:
|
||||||
// forbid copy
|
// forbid copy
|
||||||
SubscriptionBase(const SubscriptionBase& other);
|
SubscriptionBase(const SubscriptionBase &other);
|
||||||
// forbid assignment
|
// forbid assignment
|
||||||
SubscriptionBase& operator = (const SubscriptionBase &);
|
SubscriptionBase &operator = (const SubscriptionBase &);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -139,11 +143,12 @@ public:
|
|||||||
* that this should be appended to.
|
* that this should be appended to.
|
||||||
*/
|
*/
|
||||||
SubscriptionNode(const struct orb_metadata *meta,
|
SubscriptionNode(const struct orb_metadata *meta,
|
||||||
unsigned interval=0,
|
unsigned interval = 0,
|
||||||
List<SubscriptionNode *> * list=nullptr) :
|
List<SubscriptionNode *> *list = nullptr) :
|
||||||
SubscriptionBase(meta, interval),
|
SubscriptionBase(meta, interval),
|
||||||
_interval(interval) {
|
_interval(interval)
|
||||||
if (list != nullptr) list->add(this);
|
{
|
||||||
|
if (list != nullptr) { list->add(this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -179,8 +184,8 @@ public:
|
|||||||
* list during construction
|
* list during construction
|
||||||
*/
|
*/
|
||||||
Subscription(const struct orb_metadata *meta,
|
Subscription(const struct orb_metadata *meta,
|
||||||
unsigned interval=0,
|
unsigned interval = 0,
|
||||||
List<SubscriptionNode *> * list=nullptr);
|
List<SubscriptionNode *> *list = nullptr);
|
||||||
/**
|
/**
|
||||||
* Deconstructor
|
* Deconstructor
|
||||||
*/
|
*/
|
||||||
@@ -190,7 +195,8 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Create an update function that uses the embedded struct.
|
* Create an update function that uses the embedded struct.
|
||||||
*/
|
*/
|
||||||
void update() {
|
void update()
|
||||||
|
{
|
||||||
SubscriptionBase::update(getDataVoidPtr());
|
SubscriptionBase::update(getDataVoidPtr());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -269,6 +269,7 @@ int orb_exists(const struct orb_metadata *meta, int instance)
|
|||||||
int orb_group_count(const struct orb_metadata *meta)
|
int orb_group_count(const struct orb_metadata *meta)
|
||||||
{
|
{
|
||||||
unsigned group_count = 0;
|
unsigned group_count = 0;
|
||||||
|
|
||||||
while (!uORB::Manager::get_instance()->orb_exists(meta, group_count++)) {};
|
while (!uORB::Manager::get_instance()->orb_exists(meta, group_count++)) {};
|
||||||
|
|
||||||
return group_count;
|
return group_count;
|
||||||
|
|||||||
@@ -45,83 +45,86 @@ extern "C" { __EXPORT int uorb_main(int argc, char *argv[]); }
|
|||||||
static uORB::DeviceMaster *g_dev = nullptr;
|
static uORB::DeviceMaster *g_dev = nullptr;
|
||||||
static void usage()
|
static void usage()
|
||||||
{
|
{
|
||||||
warnx("Usage: uorb 'start', 'test', 'latency_test' or 'status'");
|
warnx("Usage: uorb 'start', 'test', 'latency_test' or 'status'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
uorb_main(int argc, char *argv[])
|
uorb_main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
usage();
|
usage();
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Start/load the driver.
|
* Start/load the driver.
|
||||||
*
|
*
|
||||||
* XXX it would be nice to have a wrapper for this...
|
* XXX it would be nice to have a wrapper for this...
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[1], "start")) {
|
if (!strcmp(argv[1], "start")) {
|
||||||
|
|
||||||
if (g_dev != nullptr) {
|
if (g_dev != nullptr) {
|
||||||
warnx("already loaded");
|
warnx("already loaded");
|
||||||
/* user wanted to start uorb, its already running, no error */
|
/* user wanted to start uorb, its already running, no error */
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create the driver */
|
/* create the driver */
|
||||||
g_dev = new uORB::DeviceMaster(uORB::PUBSUB);
|
g_dev = new uORB::DeviceMaster(uORB::PUBSUB);
|
||||||
|
|
||||||
if (g_dev == nullptr) {
|
if (g_dev == nullptr) {
|
||||||
warnx("driver alloc failed");
|
warnx("driver alloc failed");
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (OK != g_dev->init()) {
|
if (OK != g_dev->init()) {
|
||||||
warnx("driver init failed");
|
warnx("driver init failed");
|
||||||
delete g_dev;
|
delete g_dev;
|
||||||
g_dev = nullptr;
|
g_dev = nullptr;
|
||||||
return -EIO;
|
return -EIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef __PX4_QURT
|
#ifndef __PX4_QURT
|
||||||
/*
|
|
||||||
* Test the driver/device.
|
|
||||||
*/
|
|
||||||
if (!strcmp(argv[1], "test"))
|
|
||||||
{
|
|
||||||
uORBTest::UnitTest &t = uORBTest::UnitTest::instance();
|
|
||||||
return t.test();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Test the latency.
|
* Test the driver/device.
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[1], "latency_test")) {
|
if (!strcmp(argv[1], "test")) {
|
||||||
|
uORBTest::UnitTest &t = uORBTest::UnitTest::instance();
|
||||||
|
return t.test();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Test the latency.
|
||||||
|
*/
|
||||||
|
if (!strcmp(argv[1], "latency_test")) {
|
||||||
|
|
||||||
|
uORBTest::UnitTest &t = uORBTest::UnitTest::instance();
|
||||||
|
|
||||||
|
if (argc > 2 && !strcmp(argv[2], "medium")) {
|
||||||
|
return t.latency_test<struct orb_test_medium>(ORB_ID(orb_test_medium), true);
|
||||||
|
|
||||||
|
} else if (argc > 2 && !strcmp(argv[2], "large")) {
|
||||||
|
return t.latency_test<struct orb_test_large>(ORB_ID(orb_test_large), true);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
return t.latency_test<struct orb_test>(ORB_ID(orb_test), true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
uORBTest::UnitTest &t = uORBTest::UnitTest::instance();
|
|
||||||
if (argc > 2 && !strcmp(argv[2], "medium")) {
|
|
||||||
return t.latency_test<struct orb_test_medium>(ORB_ID(orb_test_medium), true);
|
|
||||||
} else if (argc > 2 && !strcmp(argv[2], "large")) {
|
|
||||||
return t.latency_test<struct orb_test_large>(ORB_ID(orb_test_large), true);
|
|
||||||
} else {
|
|
||||||
return t.latency_test<struct orb_test>(ORB_ID(orb_test), true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Print driver information.
|
* Print driver information.
|
||||||
*/
|
*/
|
||||||
if (!strcmp(argv[1], "status"))
|
if (!strcmp(argv[1], "status")) {
|
||||||
{
|
return OK;
|
||||||
return OK;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
usage();
|
usage();
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,45 +37,46 @@
|
|||||||
|
|
||||||
int uORB::Utils::node_mkpath
|
int uORB::Utils::node_mkpath
|
||||||
(
|
(
|
||||||
char *buf,
|
char *buf,
|
||||||
Flavor f,
|
Flavor f,
|
||||||
const struct orb_metadata *meta,
|
const struct orb_metadata *meta,
|
||||||
int *instance
|
int *instance
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
unsigned len;
|
unsigned len;
|
||||||
|
|
||||||
unsigned index = 0;
|
unsigned index = 0;
|
||||||
|
|
||||||
if (instance != nullptr) {
|
if (instance != nullptr) {
|
||||||
index = *instance;
|
index = *instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = snprintf(buf, orb_maxpath, "/%s/%s%d",
|
len = snprintf(buf, orb_maxpath, "/%s/%s%d",
|
||||||
(f == PUBSUB) ? "obj" : "param",
|
(f == PUBSUB) ? "obj" : "param",
|
||||||
meta->o_name, index);
|
meta->o_name, index);
|
||||||
|
|
||||||
if (len >= orb_maxpath) {
|
if (len >= orb_maxpath) {
|
||||||
return -ENAMETOOLONG;
|
return -ENAMETOOLONG;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
int uORB::Utils::node_mkpath(char *buf, Flavor f,
|
int uORB::Utils::node_mkpath(char *buf, Flavor f,
|
||||||
const char *orbMsgName )
|
const char *orbMsgName)
|
||||||
{
|
{
|
||||||
unsigned len;
|
unsigned len;
|
||||||
|
|
||||||
unsigned index = 0;
|
unsigned index = 0;
|
||||||
|
|
||||||
len = snprintf(buf, orb_maxpath, "/%s/%s%d", (f == PUBSUB) ? "obj" : "param",
|
len = snprintf(buf, orb_maxpath, "/%s/%s%d", (f == PUBSUB) ? "obj" : "param",
|
||||||
orbMsgName, index );
|
orbMsgName, index);
|
||||||
|
|
||||||
if (len >= orb_maxpath)
|
if (len >= orb_maxpath) {
|
||||||
return -ENAMETOOLONG;
|
return -ENAMETOOLONG;
|
||||||
|
}
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,24 +38,24 @@
|
|||||||
|
|
||||||
namespace uORB
|
namespace uORB
|
||||||
{
|
{
|
||||||
class Utils;
|
class Utils;
|
||||||
}
|
}
|
||||||
|
|
||||||
class uORB::Utils
|
class uORB::Utils
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static int node_mkpath
|
static int node_mkpath
|
||||||
(
|
(
|
||||||
char *buf,
|
char *buf,
|
||||||
Flavor f,
|
Flavor f,
|
||||||
const struct orb_metadata *meta,
|
const struct orb_metadata *meta,
|
||||||
int *instance = nullptr
|
int *instance = nullptr
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* same as above except this generators the path based on the string.
|
* same as above except this generators the path based on the string.
|
||||||
*/
|
*/
|
||||||
static int node_mkpath(char *buf, Flavor f, const char *orbMsgName);
|
static int node_mkpath(char *buf, Flavor f, const char *orbMsgName);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user