mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-07-01 03:10:34 +08:00
orb structs: add padding bytes to align the structs where necessary
This is required for the logger, we just manually add the padding bytes what would otherwise be done by the compiler. Additionally we reorder the fields by type, so that padding is only necessary for nested types.
This commit is contained in:
@@ -494,43 +494,21 @@ void Logger::run()
|
||||
|
||||
for (LoggerSubscription &sub : _subscriptions) {
|
||||
/* each message consists of a header followed by an orb data object
|
||||
* The size of the data object is given by orb_metadata.o_size
|
||||
*/
|
||||
size_t padded_msg_size = sub.metadata->o_size;
|
||||
uint8_t orb_msg_padded[padded_msg_size];
|
||||
size_t msg_size = sizeof(message_data_header_s) + sub.metadata->o_size;
|
||||
//TODO: use sub.metadata->o_size_no_padding
|
||||
uint8_t buffer[msg_size];
|
||||
|
||||
/* if this topic has been updated, copy the new data into the message buffer
|
||||
* and write a message to the log
|
||||
*/
|
||||
//orb_check(sub.fd, &updated); // check whether a non-multi topic has been updated
|
||||
/* this works for both single and multi-instances */
|
||||
for (uint8_t instance = 0; instance < ORB_MULTI_MAX_INSTANCES; instance++) {
|
||||
if (copy_if_updated_multi(sub.metadata, instance, &sub.fd[instance], &orb_msg_padded, &sub.time_tried_subscribe)) {
|
||||
if (copy_if_updated_multi(sub.metadata, instance, &sub.fd[instance], buffer + sizeof(message_data_header_s),
|
||||
&sub.time_tried_subscribe)) {
|
||||
|
||||
struct orb_output_buffer output_buffer = {};
|
||||
uint8_t buffer[sizeof(message_data_header_s) + padded_msg_size];
|
||||
output_buffer.data = &buffer;
|
||||
output_buffer.next = sizeof(message_data_header_s);
|
||||
|
||||
sub.metadata->serialize(&orb_msg_padded, &output_buffer);
|
||||
size_t msg_size = output_buffer.next;
|
||||
|
||||
//uint64_t timestamp;
|
||||
//memcpy(×tamp, buffer + sizeof(message_data_header_s), sizeof(timestamp));
|
||||
//warnx("topic: %s, instance: %d, timestamp: %llu",
|
||||
// sub.metadata->o_name, instance, timestamp);
|
||||
|
||||
/* copy the current topic data into the buffer after the header */
|
||||
//orb_copy(sub.metadata, sub.fd, buffer + sizeof(message_data_header_s));
|
||||
|
||||
/* fill the message header struct in-place at the front of the buffer,
|
||||
* accessing the unaligned (packed) structure properly
|
||||
*/
|
||||
message_data_header_s *header = reinterpret_cast<message_data_header_s *>(buffer);
|
||||
header->msg_type = static_cast<uint8_t>(MessageType::DATA);
|
||||
/* the ORB topic data object has 2 unused trailing bytes? */
|
||||
//header->msg_size = static_cast<uint16_t>(msg_size - 3);
|
||||
header->msg_size = msg_size;
|
||||
header->msg_size = static_cast<uint16_t>(msg_size - 3);
|
||||
header->msg_id = msg_id;
|
||||
header->multi_id = 0x80 + instance; // Non multi, active
|
||||
|
||||
|
||||
+5
-12
@@ -46,21 +46,14 @@
|
||||
// Hack until everything is using this header
|
||||
#include <systemlib/visibility.h>
|
||||
|
||||
struct orb_output_buffer {
|
||||
void *data;
|
||||
size_t next;
|
||||
};
|
||||
|
||||
typedef void (*func_ptr)(void *in, struct orb_output_buffer *out);
|
||||
|
||||
/**
|
||||
* Object metadata.
|
||||
*/
|
||||
struct orb_metadata {
|
||||
const char *o_name; /**< unique object name */
|
||||
const size_t o_size; /**< object size */
|
||||
func_ptr serialize; /**< serialization function for this orb topic */
|
||||
const char *o_fields; /**< semicolon separated list of fields */
|
||||
const size_t o_size_no_padding; /**< object size w/o padding at the end (for logger) */
|
||||
const char *o_fields; /**< semicolon separated list of fields (with type) */
|
||||
};
|
||||
|
||||
typedef const struct orb_metadata *orb_id_t;
|
||||
@@ -119,14 +112,14 @@ enum ORB_PRIO {
|
||||
*
|
||||
* @param _name The name of the topic.
|
||||
* @param _struct The structure the topic provides.
|
||||
* @param _func The pointer to a function that serializes this topic
|
||||
* @param _size_no_padding Struct size w/o padding at the end
|
||||
* @param _fields All fields in a semicolon separated list e.g: "float[3] position;bool armed"
|
||||
*/
|
||||
#define ORB_DEFINE(_name, _struct, _func, _fields) \
|
||||
#define ORB_DEFINE(_name, _struct, _size_no_padding, _fields) \
|
||||
const struct orb_metadata __orb_##_name = { \
|
||||
#_name, \
|
||||
sizeof(_struct), \
|
||||
_func, \
|
||||
_size_no_padding, \
|
||||
_fields \
|
||||
}; struct hack
|
||||
|
||||
|
||||
@@ -41,8 +41,8 @@ struct orb_test {
|
||||
int val;
|
||||
hrt_abstime time;
|
||||
};
|
||||
ORB_DEFINE(orb_test, struct orb_test, nullptr, "ORB_TEST:int val;hrt_abstime time;");
|
||||
ORB_DEFINE(orb_multitest, struct orb_test, nullptr, "ORB_MULTITEST:int val;hrt_abstime time;");
|
||||
ORB_DEFINE(orb_test, struct orb_test, sizeof(orb_test), "ORB_TEST:int val;hrt_abstime time;");
|
||||
ORB_DEFINE(orb_multitest, struct orb_test, sizeof(orb_test), "ORB_MULTITEST:int val;hrt_abstime time;");
|
||||
|
||||
|
||||
struct orb_test_medium {
|
||||
@@ -50,9 +50,10 @@ struct orb_test_medium {
|
||||
hrt_abstime time;
|
||||
char junk[64];
|
||||
};
|
||||
ORB_DEFINE(orb_test_medium, struct orb_test_medium, nullptr, "ORB_TEST_MEDIUM:int val;hrt_abstime time;char[64] junk;");
|
||||
ORB_DEFINE(orb_test_medium_multi, struct orb_test_medium, nullptr,
|
||||
"ORB_TEST_MEDIUM_MULTI:int val;hrt_abstime time;char[64] junk;");
|
||||
ORB_DEFINE(orb_test_medium, struct orb_test_medium, sizeof(orb_test_medium),
|
||||
"ORB_TEST_MEDIUM:int val;hrt_abstime time;char[64] junk;");
|
||||
ORB_DEFINE(orb_test_medium_multi, struct orb_test_medium, sizeof(orb_test_medium),
|
||||
"ORB_TEST_MEDIUM_MULTI:int val;hrt_abstime time;char[64] junk;");
|
||||
|
||||
|
||||
struct orb_test_large {
|
||||
@@ -60,7 +61,8 @@ struct orb_test_large {
|
||||
hrt_abstime time;
|
||||
char junk[512];
|
||||
};
|
||||
ORB_DEFINE(orb_test_large, struct orb_test_large, nullptr, "ORB_TEST_LARGE:int val;hrt_abstime time;char[512] junk;");
|
||||
ORB_DEFINE(orb_test_large, struct orb_test_large, sizeof(orb_test_large),
|
||||
"ORB_TEST_LARGE:int val;hrt_abstime time;char[512] junk;");
|
||||
|
||||
|
||||
namespace uORBTest
|
||||
|
||||
Reference in New Issue
Block a user