Apollo 10.0
自动驾驶开放平台
stream.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// This defines an stream interface for communication via USB, Ethernet, etc.
18
19#pragma once
20
21#include <cstdint>
22#include <string>
23#include <vector>
24
25#include "modules/common_msgs/drivers_msgs/can_card_parameter.pb.h"
26
27#include "cyber/cyber.h"
29
30namespace apollo {
31namespace drivers {
32namespace gnss {
33
34// An abstract class of Stream.
35// One should use the create_xxx() functions to create a Stream object.
36class Stream {
37 public:
38 // Return a pointer to a Stream object. The caller should take ownership.
39 static Stream *create_tcp(const char *address, uint16_t port,
40 uint32_t timeout_usec = 1000000);
41
42 static Stream *create_udp(const char *address, uint16_t port,
43 uint32_t timeout_usec = 1000000);
44
45 // Currently the following baud rates are supported:
46 // 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600.
47 static Stream *create_serial(const char *device_name, uint32_t baud_rate,
48 uint32_t timeout_usec = 0);
49
50 static Stream *create_ntrip(const std::string &address, uint16_t port,
51 const std::string &mountpoint,
52 const std::string &user,
53 const std::string &passwd,
54 uint32_t timeout_s = 30);
55 static Stream *create_can(
57
58 virtual ~Stream() {}
59
60 // Stream status.
61 enum class Status {
64 ERROR,
65 };
66
67 static constexpr size_t NUM_STATUS =
68 static_cast<int>(Stream::Status::ERROR) + 1;
69 Status get_status() const { return status_; }
70
71 // Returns whether it was successful to connect.
72 virtual bool Connect() = 0;
73
74 // Returns whether it was successful to disconnect.
75 virtual bool Disconnect() = 0;
76
77 void RegisterLoginData(const std::vector<std::string> login_data) {
78 login_data_.assign(login_data.begin(), login_data.end());
79 }
80
81 void Login() {
82 for (size_t i = 0; i < login_data_.size(); ++i) {
83 write(login_data_[i]);
84 AINFO << "Login: " << login_data_[i];
85 // sleep a little to avoid overrun of the slow serial interface.
87 }
88 }
89
90 // Reads up to max_length bytes. Returns actually number of bytes read.
91 virtual size_t read(uint8_t *buffer, size_t max_length) = 0;
92
93 // Returns how many bytes it was successful to write.
94 virtual size_t write(const uint8_t *buffer, size_t length) = 0;
95
96 size_t write(const std::string &buffer) {
97 return write(reinterpret_cast<const uint8_t *>(buffer.data()),
98 buffer.size());
99 }
100
101 protected:
103
105
106 private:
107 std::vector<std::string> login_data_;
109};
110
111} // namespace gnss
112} // namespace drivers
113} // namespace apollo
virtual size_t write(const uint8_t *buffer, size_t length)=0
size_t write(const std::string &buffer)
Definition stream.h:96
virtual size_t read(uint8_t *buffer, size_t max_length)=0
Status get_status() const
Definition stream.h:69
static Stream * create_serial(const char *device_name, uint32_t baud_rate, uint32_t timeout_usec=0)
static constexpr size_t NUM_STATUS
Definition stream.h:67
static Stream * create_udp(const char *address, uint16_t port, uint32_t timeout_usec=1000000)
Definition udp_stream.cc:57
void RegisterLoginData(const std::vector< std::string > login_data)
Definition stream.h:77
static Stream * create_can(const apollo::drivers::canbus::CANCardParameter &parameter)
Definition can_stream.cc:74
static Stream * create_tcp(const char *address, uint16_t port, uint32_t timeout_usec=1000000)
static Stream * create_ntrip(const std::string &address, uint16_t port, const std::string &mountpoint, const std::string &user, const std::string &passwd, uint32_t timeout_s=30)
#define AINFO
Definition log.h:42
#define DISABLE_COPY_AND_ASSIGN(TypeName)
Definition macros.h:32
class register implement
Definition arena_queue.h:37