libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
packet.hpp
1#pragma once
2
3#include "../topic.hpp"
4
5namespace LibXR
6{
7#ifndef __DOXYGEN__
8LIBXR_PACKED_BEGIN
20struct Topic::PackedDataHeader
21{
22 uint8_t prefix;
23 uint8_t
24 data_len_raw[3];
25 uint32_t topic_name_crc32;
27 uint8_t timestamp_us_raw[6];
29 uint8_t version;
30 uint8_t pack_header_crc8;
31
36 void SetDataLen(uint32_t len);
37
42 uint32_t GetDataLen() const;
43
48 void SetTimestamp(MicrosecondTimestamp timestamp);
49
54 MicrosecondTimestamp GetTimestamp() const;
55};
56
57static_assert(sizeof(Topic::PackedDataHeader) == 16);
58static_assert(offsetof(Topic::PackedDataHeader, prefix) == 0);
59static_assert(offsetof(Topic::PackedDataHeader, data_len_raw) == 1);
60static_assert(offsetof(Topic::PackedDataHeader, topic_name_crc32) == 4);
61static_assert(offsetof(Topic::PackedDataHeader, timestamp_us_raw) == 8);
62static_assert(offsetof(Topic::PackedDataHeader, version) == 14);
63static_assert(offsetof(Topic::PackedDataHeader, pack_header_crc8) == 15);
64
74template <typename Data>
75class Topic::PackedData
76{
77 static_assert(TopicPayload<Data>);
78
79 public:
84 struct
85 {
86 PackedDataHeader header_;
87 uint8_t data_[sizeof(Data)];
88 } raw;
90
91 uint8_t crc8_;
92
97 MicrosecondTimestamp GetTimestamp() const { return raw.header_.GetTimestamp(); }
98};
99LIBXR_PACKED_END
100#endif
101
111template <typename Data>
112ErrorCode Topic::PackData(const Data& data, PackedData<Data>& packet,
113 MicrosecondTimestamp timestamp)
114{
116 ASSERT(block_ != nullptr);
117 ASSERT(block_->data_.payload_type_id == TypeID::GetID<Data>());
118 ASSERT(block_->data_.payload_size == sizeof(Data));
119
120 PackBytes(block_->data_.crc32, RawData(packet), timestamp, ConstRawData(data));
121 return ErrorCode::OK;
122}
123} // namespace LibXR
只读原始数据视图 / Immutable raw data view
微秒时间戳 / Microsecond timestamp
Data data_
存储的数据 (Stored data).
Definition rbt.hpp:98
可写原始数据视图 / Mutable raw data view
static void PackBytes(uint32_t topic_name_crc32, RawData buffer, MicrosecondTimestamp timestamp, ConstRawData data)
将一段 payload 字节和 topic 元数据拼成 packet / Pack one payload byte range together with topic metadata into on...
Definition packet.cpp:42
TopicHandle block_
Definition topic.hpp:610
ErrorCode PackData(const Data &data, PackedData< Data > &packet)
将一个精确类型消息打包成 packet / Pack one exact-typed message into one packet using the topic's runtime contract...
Definition topic.hpp:525
static ID GetID()
获取类型的唯一标识符 / Get a unique identifier for type T
LibXR 命名空间
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举
@ OK
操作成功 | Operation successful
constexpr void CheckTopicPayload()
在模板上下文里断言 payload 类型满足 topic 契约 / Assert in template context that one payload type satisfies the topi...
Definition topic.hpp:38