Apollo 10.0
自动驾驶开放平台
can_client.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
22#pragma once
23
24#include <cstdint>
25#include <cstring>
26#include <sstream>
27#include <string>
28#include <vector>
29
30#include "modules/common_msgs/basic_msgs/error_code.pb.h"
31#include "modules/common_msgs/drivers_msgs/can_card_parameter.pb.h"
32
33#include "cyber/common/log.h"
35
40namespace apollo {
41namespace drivers {
42namespace canbus {
43
48struct 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
81const int CAN_RESULT_SUCC = 0;
82const int CAN_ERROR_BASE = 2000;
87
92class CanClient {
93 public:
97 CanClient() = default;
98
102 virtual ~CanClient() = default;
103
109 virtual bool Init(const CANCardParameter &parameter) = 0;
110
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
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
Defines the Byte class.
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.
Definition byte.cc:42
The class which defines the CAN client to send and receive message.
Definition can_client.h:92
virtual apollo::common::ErrorCode Send(const std::vector< CanFrame > &frames, int32_t *const frame_num)=0
Send messages
virtual bool Init(const CANCardParameter &parameter)=0
Initialize the CAN client by specified CAN card parameters.
virtual std::string GetErrorString(const int32_t status)=0
Get the error string.
virtual void Stop()=0
Stop the CAN client.
virtual ~CanClient()=default
Destructor
CanClient()=default
Constructor
virtual apollo::common::ErrorCode Receive(std::vector< CanFrame > *const frames, int32_t *const frame_num)=0
Receive messages
virtual apollo::common::ErrorCode SendSingleFrame(const std::vector< CanFrame > &frames)
Send a single message.
Definition can_client.h:139
virtual apollo::common::ErrorCode Start()=0
Start the CAN client.
bool is_started_
The CAN client is started.
Definition can_client.h:165
const int CAN_ERROR_FRAME_NUM
Definition can_client.h:84
const int CAN_ERROR_SEND_FAILED
Definition can_client.h:85
const int CAN_ERROR_OPEN_DEVICE_FAILED
Definition can_client.h:83
const int CAN_ERROR_RECV_FAILED
Definition can_client.h:86
class register implement
Definition arena_queue.h:37
The class which defines the information to send and receive.
Definition can_client.h:48
std::string CanFrameString() const
CanFrame string including essential information about the message.
Definition can_client.h:69
uint8_t len
Message length
Definition can_client.h:52
struct timeval timestamp
Time stamp
Definition can_client.h:56
uint8_t data[8]
Message content
Definition can_client.h:54