Apollo 10.0
自动驾驶开放平台
can_stream.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 <vector>
21
22#include "modules/common_msgs/basic_msgs/error_code.pb.h"
23
24#include "cyber/cyber.h"
27
28namespace apollo {
29namespace drivers {
30namespace gnss {
31
33 : parameter_(parameter) {}
34
36
38 // Init can client
39 auto can_factory = apollo::drivers::canbus::CanClientFactory::Instance();
40 can_factory->RegisterCanClients();
41 can_client_ = can_factory->CreateCANClient(parameter_);
42 if (!can_client_) {
43 AERROR << "Failed to create can client.";
44 return false;
45 }
46 AINFO << "Can client is successfully created.";
47 if (can_client_->Start() != apollo::common::ErrorCode::OK) {
48 AERROR << "Failed to start can client";
49 return false;
50 }
51 AINFO << "Can client is started.";
52 return true;
53}
54
55bool CanStream::Disconnect() { return true; }
56
57size_t CanStream::read(uint8_t* buffer, size_t max_length) {
58 std::vector<apollo::drivers::canbus::CanFrame> frames;
59 static int32_t recv_frame_num = 1;
60 if (can_client_->Receive(&frames, &recv_frame_num) !=
62 AERROR << "Failed to receive can frame.";
63 return 0;
64 }
65 std::memcpy(buffer, &frames[0], sizeof(frames[0]));
66 return sizeof(frames[0]);
67}
68
69size_t CanStream::write(const uint8_t* buffer, size_t length) {
70 // TODO(dev): implement
71 return 0;
72}
73
76 return new CanStream(parameter);
77}
78
79} // namespace gnss
80} // namespace drivers
81} // namespace apollo
Defines the CanClientFactory class.
virtual size_t read(uint8_t *buffer, size_t max_length)
Definition can_stream.cc:57
CanStream(const apollo::drivers::canbus::CANCardParameter &parameter)
Definition can_stream.cc:32
virtual size_t write(const uint8_t *data, size_t length)
Definition can_stream.cc:69
static Stream * create_can(const apollo::drivers::canbus::CANCardParameter &parameter)
Definition can_stream.cc:74
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
class register implement
Definition arena_queue.h:37