Apollo  6.0
Open source self driving car software
stream.h
Go to the documentation of this file.
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 "cyber/cyber.h"
26 
28 
29 namespace apollo {
30 namespace drivers {
31 namespace gnss {
32 
33 // An abstract class of Stream.
34 // One should use the create_xxx() functions to create a Stream object.
35 class Stream {
36  public:
37  // Return a pointer to a Stream object. The caller should take ownership.
38  static Stream *create_tcp(const char *address, uint16_t port,
39  uint32_t timeout_usec = 1000000);
40 
41  static Stream *create_udp(const char *address, uint16_t port,
42  uint32_t timeout_usec = 1000000);
43 
44  // Currently the following baud rates are supported:
45  // 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600.
46  static Stream *create_serial(const char *device_name, uint32_t baud_rate,
47  uint32_t timeout_usec = 0);
48 
49  static Stream *create_ntrip(const std::string &address, uint16_t port,
50  const std::string &mountpoint,
51  const std::string &user,
52  const std::string &passwd,
53  uint32_t timeout_s = 30);
54 
55  virtual ~Stream() {}
56 
57  // Stream status.
58  enum class Status {
60  CONNECTED,
61  ERROR,
62  };
63 
64  static constexpr size_t NUM_STATUS =
65  static_cast<int>(Stream::Status::ERROR) + 1;
66  Status get_status() const { return status_; }
67 
68  // Returns whether it was successful to connect.
69  virtual bool Connect() = 0;
70 
71  // Returns whether it was successful to disconnect.
72  virtual bool Disconnect() = 0;
73 
74  void RegisterLoginData(const std::vector<std::string> login_data) {
75  login_data_.assign(login_data.begin(), login_data.end());
76  }
77 
78  void Login() {
79  for (size_t i = 0; i < login_data_.size(); ++i) {
80  write(login_data_[i]);
81  AINFO << "Login: " << login_data_[i];
82  // sleep a little to avoid overrun of the slow serial interface.
83  cyber::Duration(0.5).Sleep();
84  }
85  }
86 
87  // Reads up to max_length bytes. Returns actually number of bytes read.
88  virtual size_t read(uint8_t *buffer, size_t max_length) = 0;
89 
90  // Returns how many bytes it was successful to write.
91  virtual size_t write(const uint8_t *buffer, size_t length) = 0;
92 
93  size_t write(const std::string &buffer) {
94  return write(reinterpret_cast<const uint8_t *>(buffer.data()),
95  buffer.size());
96  }
97 
98  protected:
99  Stream() {}
100 
102 
103  private:
104  std::vector<std::string> login_data_;
106 };
107 
108 } // namespace gnss
109 } // namespace drivers
110 } // namespace apollo
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual size_t write(const uint8_t *buffer, size_t length)=0
Status status_
Definition: stream.h:101
static Stream * create_udp(const char *address, uint16_t port, uint32_t timeout_usec=1000000)
void Login()
Definition: stream.h:78
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:64
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)
virtual size_t read(uint8_t *buffer, size_t max_length)=0
std::chrono::microseconds Duration
Definition: croutine.h:36
void RegisterLoginData(const std::vector< std::string > login_data)
Definition: stream.h:74
#define DISABLE_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:32
Status
Definition: stream.h:58
Stream()
Definition: stream.h:99
Definition: stream.h:35
size_t write(const std::string &buffer)
Definition: stream.h:93
virtual ~Stream()
Definition: stream.h:55
static Stream * create_tcp(const char *address, uint16_t port, uint32_t timeout_usec=1000000)
#define AINFO
Definition: log.h:42
Status get_status() const
Definition: stream.h:66