Apollo 10.0
自动驾驶开放平台
bridge_header.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#pragma once
18
19#include <cstring>
20#include <string>
21
23
24namespace apollo {
25namespace bridge {
26
27typedef uint32_t hsize;
28
29constexpr char BRIDGE_HEADER_FLAG[] = "ApolloBridgeHeader";
30constexpr size_t HEADER_FLAG_SIZE = sizeof(BRIDGE_HEADER_FLAG);
31constexpr size_t Item_Header_Size = sizeof(HType) + sizeof(bsize) + 2;
32
34 public:
35 BridgeHeader() = default;
36 ~BridgeHeader() = default;
37
38 public:
39 bool Serialize(char *buf, size_t size);
40 bool Diserialize(const char *buf, size_t buf_size);
41 bool IsAvailable(const char *buf);
42
43 uint32_t GetHeaderVer() const { return header_ver_.value_; }
45 return static_cast<hsize>(header_body_size_ + HEADER_FLAG_SIZE +
46 sizeof(hsize) + 2);
47 }
48 bsize GetHeaderBodySize() const { return header_body_size_; }
49 std::string GetMsgName() const { return msg_name_.value_; }
50 uint32_t GetMsgID() const { return msg_id_.value_; }
51 uint32_t GetTotalFrames() const { return total_frames_.value_; }
52 uint32_t GetIndex() const { return index_.value_; }
53 double GetTimeStamp() const { return time_stamp_.value_; }
54 bsize GetMsgSize() const { return msg_size_.value_; }
55 bsize GetFrameSize() const { return frame_size_.value_; }
56 bsize GetFramePos() const { return frame_pos_.value_; }
57
58 void SetHeaderVer(uint32_t header_ver) {
59 header_ver_ = header_ver;
60 header_body_size_ +=
61 static_cast<hsize>(Item_Header_Size + 1 + sizeof(uint32_t));
62 }
63 void SetMsgName(const std::string &msg_name) {
64 msg_name_ = msg_name;
65 header_body_size_ +=
66 static_cast<hsize>(Item_Header_Size + 1 + msg_name.length() + 1);
67 }
68 void SetMsgID(uint32_t msg_id) {
69 msg_id_ = msg_id;
70 header_body_size_ +=
71 static_cast<hsize>(Item_Header_Size + 1 + sizeof(uint32_t));
72 }
73 void SetTotalFrames(uint32_t total_frames) {
74 total_frames_ = total_frames;
75 header_body_size_ +=
76 static_cast<hsize>(Item_Header_Size + 1 + sizeof(uint32_t));
77 }
78 void SetFrameSize(bsize frame_size) {
79 frame_size_ = frame_size;
80 header_body_size_ +=
81 static_cast<hsize>(Item_Header_Size + 1 + sizeof(bsize));
82 }
83 void SetFramePos(bsize frame_pos) {
84 frame_pos_ = frame_pos;
85 header_body_size_ +=
86 static_cast<hsize>(Item_Header_Size + 1 + sizeof(bsize));
87 }
88 void SetIndex(uint32_t index) {
89 index_ = index;
90 header_body_size_ +=
91 static_cast<hsize>(Item_Header_Size + 1 + sizeof(uint32_t));
92 }
93 void SetTimeStamp(double time_stamp) {
94 time_stamp_ = time_stamp;
95 header_body_size_ +=
96 static_cast<hsize>(Item_Header_Size + 1 + sizeof(double));
97 }
98 void SetMsgSize(bsize msg_size) {
99 msg_size_ = msg_size;
100 header_body_size_ +=
101 static_cast<hsize>(Item_Header_Size + 1 + sizeof(bsize));
102 }
103
104 private:
105 template <typename T, size_t S>
106 char *SerializeBasicType(const T *value, char *buf, size_t size) {
107 if (!buf || size < S) {
108 return nullptr;
109 }
110 char *res = buf;
111 memcpy(res, value, S);
112 res[S] = '\n';
113 res += S + 1;
114 return res;
115 }
116
117 template <typename T, size_t S>
118 bool DiserializeBasicType(T *value, const char *buf) {
119 if (!buf) {
120 return false;
121 }
122 char temp[S] = {0};
123 memcpy(temp, buf, S);
124 *value = *(reinterpret_cast<T *>(temp));
125 return true;
126 }
127
128 char *SerializeHeaderFlag(char *buf, size_t size);
129 char *SerializeHeaderSize(char *buf, size_t size);
130
131 private:
132 HeaderItem<Header_Ver, uint32_t> header_ver_;
133 HeaderItem<Msg_Name, std::string> msg_name_;
134 HeaderItem<Msg_ID, uint32_t> msg_id_;
135 HeaderItem<Msg_Size, bsize> msg_size_;
136 HeaderItem<Msg_Frames, uint32_t> total_frames_;
137 HeaderItem<Frame_Size, bsize> frame_size_;
138 HeaderItem<Frame_Pos, bsize> frame_pos_;
139 HeaderItem<Frame_Index, uint32_t> index_;
140 HeaderItem<Time_Stamp, double> time_stamp_;
141 hsize header_body_size_ = 0;
142 HeaderItemBase *header_item[Header_Tail] = {
143 &header_ver_, &msg_name_, &msg_id_, &msg_size_, &total_frames_,
144 &frame_size_, &frame_pos_, &index_, &time_stamp_,
145 };
146};
147
148} // namespace bridge
149} // namespace apollo
std::string GetMsgName() const
void SetMsgSize(bsize msg_size)
uint32_t GetHeaderVer() const
void SetTotalFrames(uint32_t total_frames)
void SetHeaderVer(uint32_t header_ver)
void SetMsgID(uint32_t msg_id)
void SetFrameSize(bsize frame_size)
bool Serialize(char *buf, size_t size)
void SetFramePos(bsize frame_pos)
bool Diserialize(const char *buf, size_t buf_size)
void SetMsgName(const std::string &msg_name)
void SetTimeStamp(double time_stamp)
bool IsAvailable(const char *buf)
uint32_t GetTotalFrames() const
void SetIndex(uint32_t index)
constexpr char BRIDGE_HEADER_FLAG[]
constexpr size_t HEADER_FLAG_SIZE
constexpr size_t Item_Header_Size
class register implement
Definition arena_queue.h:37