Apollo  6.0
Open source self driving car software
can_client.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 
22 #pragma once
23 
24 #include <cstdint>
25 #include <cstring>
26 #include <sstream>
27 #include <string>
28 #include <vector>
29 
30 #include "modules/common/proto/error_code.pb.h"
31 #include "modules/drivers/canbus/proto/can_card_parameter.pb.h"
32 
33 #include "cyber/common/log.h"
35 
40 namespace apollo {
41 namespace drivers {
42 namespace canbus {
43 
48 struct CanFrame {
50  uint32_t id;
52  uint8_t len;
54  uint8_t data[8];
56  struct timeval timestamp;
57 
61  CanFrame() : id(0), len(0), timestamp{0} {
62  std::memset(data, 0, sizeof(data));
63  }
64 
69  std::string CanFrameString() const {
70  std::stringstream output_stream("");
71  output_stream << "id:0x" << Byte::byte_to_hex(id)
72  << ",len:" << static_cast<int>(len) << ",data:";
73  for (uint8_t i = 0; i < len; ++i) {
74  output_stream << Byte::byte_to_hex(data[i]);
75  }
76  output_stream << ",";
77  return output_stream.str();
78  }
79 };
80 
81 const int CAN_RESULT_SUCC = 0;
82 const int CAN_ERROR_BASE = 2000;
83 const int CAN_ERROR_OPEN_DEVICE_FAILED = CAN_ERROR_BASE + 1;
84 const int CAN_ERROR_FRAME_NUM = CAN_ERROR_BASE + 2;
85 const int CAN_ERROR_SEND_FAILED = CAN_ERROR_BASE + 3;
86 const int CAN_ERROR_RECV_FAILED = CAN_ERROR_BASE + 4;
87 
92 class CanClient {
93  public:
97  CanClient() = default;
98 
102  virtual ~CanClient() = default;
103 
109  virtual bool Init(const CANCardParameter &parameter) = 0;
110 
116  virtual apollo::common::ErrorCode Start() = 0;
117 
121  virtual void Stop() = 0;
122 
130  virtual apollo::common::ErrorCode Send(const std::vector<CanFrame> &frames,
131  int32_t *const frame_num) = 0;
132 
139  virtual apollo::common::ErrorCode SendSingleFrame(
140  const std::vector<CanFrame> &frames) {
141  CHECK_EQ(frames.size(), 1U)
142  << "frames size not equal to 1, actual frame size :" << frames.size();
143  int32_t n = 1;
144  return Send(frames, &n);
145  }
146 
154  virtual apollo::common::ErrorCode Receive(std::vector<CanFrame> *const frames,
155  int32_t *const frame_num) = 0;
156 
161  virtual std::string GetErrorString(const int32_t status) = 0;
162 
163  protected:
165  bool is_started_ = false;
166 };
167 
168 } // namespace canbus
169 } // namespace drivers
170 } // namespace apollo
const int CAN_ERROR_SEND_FAILED
Definition: can_client.h:85
struct timeval timestamp
Time stamp.
Definition: can_client.h:56
std::string CanFrameString() const
CanFrame string including essential information about the message.
Definition: can_client.h:69
static std::string byte_to_hex(const uint8_t value)
Transform an integer with the size of one byte to its hexadecimal represented by a string...
uint8_t data[8]
Message content.
Definition: can_client.h:54
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
uint32_t id
Message id.
Definition: can_client.h:50
const int CAN_ERROR_OPEN_DEVICE_FAILED
Definition: can_client.h:83
The class which defines the CAN client to send and receive message.
Definition: can_client.h:92
virtual apollo::common::ErrorCode SendSingleFrame(const std::vector< CanFrame > &frames)
Send a single message.
Definition: can_client.h:139
const int CAN_RESULT_SUCC
Definition: can_client.h:81
The class which defines the information to send and receive.
Definition: can_client.h:48
const int CAN_ERROR_FRAME_NUM
Definition: can_client.h:84
uint8_t len
Message length.
Definition: can_client.h:52
CanFrame()
Constructor.
Definition: can_client.h:61
const int CAN_ERROR_RECV_FAILED
Definition: can_client.h:86
Defines the Byte class.
const int CAN_ERROR_BASE
Definition: can_client.h:82
bool Init(const char *binary_name)