Apollo 9.0
自动驾驶开放平台
parser.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2017 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 <cstdint>
20#include <string>
21#include <vector>
22
23#include "google/protobuf/message.h"
24
25#include "modules/drivers/gnss/proto/config.pb.h"
26
28
29namespace apollo {
30namespace drivers {
31namespace gnss {
32
33// A general pointer to a protobuf message.
34using MessagePtr = ::google::protobuf::Message *;
35enum class MessageType {
36 NONE,
37 GNSS,
39 IMU,
40 INS,
42 WHEEL,
45 GPGGA,
47 RAWIMU,
51 HEADING,
52};
57using MessageInfoVec = std::vector<MessageInfo>;
58
59// convert gps time (base on Jan 6 1980) to system time (base on Jan 1 1970)
60// notice: Jan 6 1980
61//
62// linux shell:
63// time1 = date +%s -d"Jan 6, 1980 00:00:01"
64// time2 = date +%s -d"Jan 1, 1970 00:00:01"
65// dif_tick = time1-time2
66// 315964800 = 315993601 - 28801
67
68#define EPOCH_AND_SYSTEM_DIFF_SECONDS 315964800
69
70// A helper function that returns a pointer to a protobuf message of type T.
71template <class T>
72inline T *As(::google::protobuf::Message *message_ptr) {
73 return dynamic_cast<T *>(message_ptr);
74}
75
76// An abstract class of Parser.
77// One should use the create_xxx() functions to create a Parser object.
78class Parser {
79 public:
80 // Return a pointer to a parser. The caller should take ownership.
81 static Parser *CreateNovatel(const config::Config &config);
82 static Parser *CreateHuaCeText(const config::Config &config);
83 static Parser *CreateAsensing(const config::Config &config);
84 static Parser *CreateBroadGnssText(const config::Config &config);
85
86 static Parser *CreateParser(const config::Config &config) {
87 switch (config.data().format()) {
89 return Parser::CreateNovatel(config);
91 return Parser::CreateHuaCeText(config);
93 return Parser::CreateAsensing(config);
95 return Parser::CreateBroadGnssText(config);
96 default:
97 return nullptr;
98 }
99 }
100
101 // Return a pointer to rtcm v3 parser. The caller should take ownership.
102 static Parser *CreateRtcmV3(bool is_base_station = false);
103
104 virtual ~Parser() {}
105
106 // Updates the parser with new data. The caller must keep the data valid until
107 // GetMessage()
108 // returns NONE.
109 void Update(const uint8_t *data, size_t length) {
110 data_ = data;
111 data_end_ = data + length;
112 }
113
114 void Update(const std::string &data) {
115 Update(reinterpret_cast<const uint8_t *>(data.data()), data.size());
116 }
117 // Gets a parsed protobuf message. The caller must consume the message before
118 // calling another
119 // GetMessage() or Update();
120 virtual MessageType GetMessage(MessagePtr *message_ptr) {
121 return MessageType::NONE;
122 }
123
124 virtual void GetMessages(MessageInfoVec *messages) {}
125
126 virtual bool GetInsStat(MessagePtr *message_ptr) { return false; }
127
128 protected:
130
131 // Point to the beginning and end of data. Do not take ownership.
132 const uint8_t *data_ = nullptr;
133 const uint8_t *data_end_ = nullptr;
134
135 private:
137};
138
139} // namespace gnss
140} // namespace drivers
141} // namespace apollo
virtual void GetMessages(MessageInfoVec *messages)
Definition parser.h:124
void Update(const std::string &data)
Definition parser.h:114
static Parser * CreateRtcmV3(bool is_base_station=false)
virtual bool GetInsStat(MessagePtr *message_ptr)
Definition parser.h:126
static Parser * CreateParser(const config::Config &config)
Definition parser.h:86
const uint8_t * data_
Definition parser.h:132
void Update(const uint8_t *data, size_t length)
Definition parser.h:109
static Parser * CreateHuaCeText(const config::Config &config)
static Parser * CreateNovatel(const config::Config &config)
const uint8_t * data_end_
Definition parser.h:133
static Parser * CreateBroadGnssText(const config::Config &config)
static Parser * CreateAsensing(const config::Config &config)
virtual MessageType GetMessage(MessagePtr *message_ptr)
Definition parser.h:120
#define DISABLE_COPY_AND_ASSIGN(TypeName)
Definition macros.h:32
::google::protobuf::Message * MessagePtr
Definition parser.h:34
T * As(::google::protobuf::Message *message_ptr)
Definition parser.h:72
std::vector< MessageInfo > MessageInfoVec
Definition parser.h:57
class register implement