Apollo 10.0
自动驾驶开放平台
fake_can_client.cc
浏览该文件的文档.
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
18
19#include <cstring>
20#include <thread>
21
22namespace apollo {
23namespace drivers {
24namespace canbus {
25namespace can {
26
28
29bool FakeCanClient::Init(const CANCardParameter &param) { return true; }
30
31ErrorCode FakeCanClient::Start() { return ErrorCode::OK; }
32
34
35ErrorCode FakeCanClient::Send(const std::vector<CanFrame> &frames,
36 int32_t *const frame_num) {
37 if (frame_num == nullptr) {
38 AERROR << "frame_num pointer is null";
39 return ErrorCode::CAN_CLIENT_ERROR_BASE;
40 }
41
42 if (static_cast<size_t>(*frame_num) != frames.size()) {
43 AERROR << "frame num is incorrect.";
44 return ErrorCode::CAN_CLIENT_ERROR_FRAME_NUM;
45 }
46 for (size_t i = 0; i < frames.size(); ++i) {
47 ADEBUG << "send frame i:" << i;
48 ADEBUG << frames[i].CanFrameString();
49 frame_info_ << frames[i].CanFrameString();
50 }
51 ++send_counter_;
52 return ErrorCode::OK;
53}
54
55ErrorCode FakeCanClient::Receive(std::vector<CanFrame> *const frames,
56 int32_t *const frame_num) {
57 if (frame_num == nullptr || frames == nullptr) {
58 AERROR << "frames or frame_num pointer is null";
59 return ErrorCode::CAN_CLIENT_ERROR_BASE;
60 }
61 frames->resize(*frame_num);
62 const int MOCK_LEN = 8;
63 for (size_t i = 0; i < frames->size(); ++i) {
64 for (int j = 0; j < MOCK_LEN; ++j) {
65 (*frames)[i].data[j] = static_cast<uint8_t>(j);
66 }
67 (*frames)[i].id = static_cast<uint32_t>(i);
68 (*frames)[i].len = MOCK_LEN;
69 ADEBUG << (*frames)[i].CanFrameString() << "frame_num[" << i << "]";
70 }
71 std::this_thread::sleep_for(std::chrono::milliseconds(10));
72 ++recv_counter_;
73 return ErrorCode::OK;
74}
75
76std::string FakeCanClient::GetErrorString(const int32_t /*status*/) {
77 return "";
78}
79
80} // namespace can
81} // namespace canbus
82} // namespace drivers
83} // namespace apollo
apollo::common::ErrorCode Receive(std::vector< CanFrame > *frames, int32_t *const frame_num) override
Receive messages
apollo::common::ErrorCode Send(const std::vector< CanFrame > &frames, int32_t *const frame_num) override
Send messages
std::string GetErrorString(const int32_t status) override
Get the error string.
apollo::common::ErrorCode Start() override
Start the fake CAN client.
void Stop() override
Stop the fake CAN client.
bool Init(const CANCardParameter &param) override
Initialize the fake CAN client by specified CAN card parameters.
Defines the FakeCanClient class which inherites CanClient.
#define ADEBUG
Definition log.h:41
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37