Apollo 10.0
自动驾驶开放平台
message_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#ifndef CYBER_MESSAGE_MESSAGE_HEADER_H_
18#define CYBER_MESSAGE_MESSAGE_HEADER_H_
19
20#include <arpa/inet.h>
21
22#include <algorithm>
23#include <cstdint>
24#include <cstring>
25#include <string>
26
27namespace apollo {
28namespace cyber {
29namespace message {
30
32 public:
43
44 bool is_magic_num_match(const char* other, size_t other_len) const {
45 if (other == nullptr || other_len != sizeof(magic_num_)) {
46 return false;
47 }
48 return memcmp(magic_num_, other, sizeof(magic_num_)) == 0;
49 }
50 void reset_magic_num() { memcpy(magic_num_, "BDACBDAC", sizeof(magic_num_)); }
51
52 uint64_t seq() const { return ConvertArrayTo64(seq_); }
53 void set_seq(uint64_t seq) { Convert64ToArray(seq, const_cast<char*>(seq_)); }
54 void reset_seq() { memset(seq_, 0, sizeof(seq_)); }
55
56 uint64_t timestamp_ns() const { return ConvertArrayTo64(timestamp_ns_); }
58 Convert64ToArray(timestamp_ns, const_cast<char*>(timestamp_ns_));
59 }
60 void reset_timestamp_ns() { memset(timestamp_ns_, 0, sizeof(timestamp_ns_)); }
61
62 uint64_t src_id() const { return ConvertArrayTo64(src_id_); }
63 void set_src_id(uint64_t src_id) {
64 Convert64ToArray(src_id, const_cast<char*>(src_id_));
65 }
66 void reset_src_id() { memset(src_id_, 0, sizeof(src_id_)); }
67
68 uint64_t dst_id() const { return ConvertArrayTo64(dst_id_); }
69 void set_dst_id(uint64_t dst_id) {
70 Convert64ToArray(dst_id, const_cast<char*>(dst_id_));
71 }
72 void reset_dst_id() { memset(dst_id_, 0, sizeof(dst_id_)); }
73
74 const char* msg_type() const { return msg_type_; }
75 void set_msg_type(const char* msg_type, size_t msg_type_len) {
76 if (msg_type == nullptr || msg_type_len == 0) {
77 return;
78 }
79 size_t real_len = msg_type_len;
80 if (msg_type_len >= sizeof(msg_type_)) {
81 real_len = sizeof(msg_type_) - 1;
82 }
84 memcpy(msg_type_, msg_type, real_len);
85 }
86 void reset_msg_type() { memset(msg_type_, 0, sizeof(msg_type_)); }
87
88 void reset_res() { memset(res_, 0, sizeof(res_)); }
89
90 uint32_t content_size() const { return ConvertArrayTo32(content_size_); }
92 Convert32ToArray(content_size, const_cast<char*>(content_size_));
93 }
94 void reset_content_size() { memset(content_size_, 0, sizeof(content_size_)); }
95
96 private:
97 void Convert32ToArray(uint32_t input, char* output) {
98 uint32_t n = htonl(input);
99 memcpy(static_cast<void*>(output), static_cast<const void*>(&n), sizeof(n));
100 }
101
102 void Convert64ToArray(uint64_t input, char* output) {
103 uint32_t h_high =
104 static_cast<uint32_t>((input & 0xffffffff00000000UL) >> 32);
105 uint32_t h_low = static_cast<uint32_t>(input & 0x00000000ffffffffUL);
106 Convert32ToArray(h_high, output);
107 Convert32ToArray(h_low, output + 4);
108 }
109
110 uint32_t ConvertArrayTo32(const char* input) const {
111 uint32_t n = 0;
112 memcpy(static_cast<void*>(&n), static_cast<const void*>(input), sizeof(n));
113 return ntohl(n);
114 }
115
116 uint64_t ConvertArrayTo64(const char* input) const {
117 uint64_t high = ConvertArrayTo32(input);
118 uint64_t low = ConvertArrayTo32(input + 4);
119 return (high << 32) | low;
120 }
121
122 char magic_num_[8];
123 char seq_[8];
124 char timestamp_ns_[8];
125 char src_id_[8];
126 char dst_id_[8];
127 char msg_type_[129];
128 char res_[19];
129 char content_size_[4];
130};
131
132} // namespace message
133} // namespace cyber
134} // namespace apollo
135
136#endif // CYBER_MESSAGE_MESSAGE_HEADER_H_
void set_timestamp_ns(uint64_t timestamp_ns)
void set_msg_type(const char *msg_type, size_t msg_type_len)
void set_content_size(uint32_t content_size)
bool is_magic_num_match(const char *other, size_t other_len) const
class register implement
Definition arena_queue.h:37