Apollo 10.0
自动驾驶开放平台
esdcan_monitor.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 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 <string>
20
21#if USE_ESD_CAN
22#include "esd_can/include/ntcan.h"
23#endif
24
25#include "cyber/common/file.h"
26#include "cyber/common/log.h"
30
31DEFINE_int32(esdcan_id, 0, "ESD CAN id.");
32DEFINE_string(esdcan_monitor_name, "EsdCanMonitor", "Name of the CAN monitor.");
33DEFINE_double(esdcan_monitor_interval, 3, "CAN status checking interval (s).");
34DEFINE_string(esdcan_component_name, "ESD-CAN",
35 "Name of the ESD CAN component in SystemStatus.");
36
37namespace apollo {
38namespace monitor {
39namespace {
40
41#if USE_ESD_CAN
42std::string StatusString(const NTCAN_RESULT ntstatus) {
43 switch (ntstatus) {
44 case NTCAN_SUCCESS:
45 return "NTCAN_SUCCESS";
46 case NTCAN_RX_TIMEOUT:
47 return "NTCAN_RX_TIMEOUT";
48 case NTCAN_TX_TIMEOUT:
49 return "NTCAN_TX_TIMEOUT";
50 case NTCAN_TX_ERROR:
51 return "NTCAN_TX_ERROR";
52 case NTCAN_CONTR_OFF_BUS:
53 return "NTCAN_CONTR_OFF_BUS";
54 case NTCAN_CONTR_BUSY:
55 return "NTCAN_CONTR_BUSY";
56 case NTCAN_CONTR_WARN:
57 return "NTCAN_CONTR_WARN";
58 case NTCAN_NO_ID_ENABLED:
59 return "NTCAN_NO_ID_ENABLED";
60 case NTCAN_ID_ALREADY_ENABLED:
61 return "NTCAN_ID_ALREADY_ENABLED";
62 case NTCAN_ID_NOT_ENABLED:
63 return "NTCAN_ID_NOT_ENABLED";
64 case NTCAN_INVALID_FIRMWARE:
65 return "NTCAN_INVALID_FIRMWARE";
66 case NTCAN_MESSAGE_LOST:
67 return "NTCAN_MESSAGE_LOST";
68 case NTCAN_INVALID_PARAMETER:
69 return "NTCAN_INVALID_PARAMETER";
70 case NTCAN_INVALID_HANDLE:
71 return "NTCAN_INVALID_HANDLE";
72 case NTCAN_NET_NOT_FOUND:
73 return "NTCAN_NET_NOT_FOUND";
74 case NTCAN_INSUFFICIENT_RESOURCES:
75 return "NTCAN_INSUFFICIENT_RESOURCES";
76#ifdef NTCAN_IO_INCOMPLETE
77 case NTCAN_IO_INCOMPLETE:
78 return "NTCAN_IO_INCOMPLETE";
79#endif
80#ifdef NTCAN_IO_PENDING
81 case NTCAN_IO_PENDING:
82 return "NTCAN_IO_PENDING";
83#endif
84#ifdef NTCAN_INVALID_HARDWARE
85 case NTCAN_INVALID_HARDWARE:
86 return "NTCAN_INVALID_HARDWARE";
87#endif
88#ifdef NTCAN_PENDING_WRITE
89 case NTCAN_PENDING_WRITE:
90 return "NTCAN_PENDING_WRITE";
91#endif
92#ifdef NTCAN_PENDING_READ
93 case NTCAN_PENDING_READ:
94 return "NTCAN_PENDING_READ";
95#endif
96#ifdef NTCAN_INVALID_DRIVER
97 case NTCAN_INVALID_DRIVER:
98 return "NTCAN_INVALID_DRIVER";
99#endif
100#ifdef NTCAN_OPERATION_ABORTED
101 case NTCAN_OPERATION_ABORTED:
102 return "NTCAN_OPERATION_ABORTED";
103#endif
104#ifdef NTCAN_WRONG_DEVICE_STATE
105 case NTCAN_WRONG_DEVICE_STATE:
106 return "NTCAN_WRONG_DEVICE_STATE";
107#endif
108#ifdef NTCAN_HANDLE_FORCED_CLOSE
109 case NTCAN_HANDLE_FORCED_CLOSE:
110 return "NTCAN_HANDLE_FORCED_CLOSE";
111#endif
112#ifdef NTCAN_NOT_IMPLEMENTED
113 case NTCAN_NOT_IMPLEMENTED:
114 return "NTCAN_NOT_IMPLEMENTED";
115#endif
116#ifdef NTCAN_NOT_SUPPORTED
117 case NTCAN_NOT_SUPPORTED:
118 return "NTCAN_NOT_SUPPORTED";
119#endif
120#ifdef NTCAN_SOCK_CONN_TIMEOUT
121 case NTCAN_SOCK_CONN_TIMEOUT:
122 return "NTCAN_SOCK_CONN_TIMEOUT";
123#endif
124#ifdef NTCAN_SOCK_CMD_TIMEOUT
125 case NTCAN_SOCK_CMD_TIMEOUT:
126 return "NTCAN_SOCK_CMD_TIMEOUT";
127#endif
128#ifdef NTCAN_SOCK_HOST_NOT_FOUND
129 case NTCAN_SOCK_HOST_NOT_FOUND:
130 return "NTCAN_SOCK_HOST_NOT_FOUND";
131#endif
132#ifdef NTCAN_CONTR_ERR_PASSIVE
133 case NTCAN_CONTR_ERR_PASSIVE:
134 return "NTCAN_CONTR_ERR_PASSIVE";
135#endif
136#ifdef NTCAN_ERROR_NO_BAUDRATE
137 case NTCAN_ERROR_NO_BAUDRATE:
138 return "NTCAN_ERROR_NO_BAUDRATE";
139#endif
140#ifdef NTCAN_ERROR_LOM
141 case NTCAN_ERROR_LOM:
142 return "NTCAN_ERROR_LOM";
143#endif
144 default:
145 break;
146 }
147 return "NTCAN_UNKNOWN";
148}
149
150NTCAN_RESULT EsdCanTest(const int can_id, NTCAN_HANDLE* handle) {
151 NTCAN_RESULT ret = canOpen(can_id, 0, 1, 1, 0, 0, handle);
152 if (ret == NTCAN_SUCCESS) {
153 AINFO << "Successfully opened ESD-CAN device " << can_id;
154 } else {
155 AERROR << "Failed to open ESD-CAN device " << can_id << ", error: " << ret
156 << " (" << StatusString(ret) << ")";
157 return ret;
158 }
159
160 CAN_IF_STATUS if_status;
161 ret = canStatus(*handle, &if_status);
162 if (ret != NTCAN_SUCCESS) {
163 AERROR << "Cannot get status of ESD-CAN, ret=" << ret << " ("
164 << StatusString(ret) << ")";
165 return ret;
166 }
167
168 NTCAN_BUS_STATISTIC stats;
169 ret = canIoctl(*handle, NTCAN_IOCTL_GET_BUS_STATISTIC, &stats);
170 if (ret != NTCAN_SUCCESS) {
171 AERROR << "NTCAN_IOCTL_GET_BUS_STATISTIC failed for device with error: "
172 << ret << " (" << StatusString(ret) << ")";
173 return ret;
174 }
175
176 NTCAN_CTRL_STATE ctrl_state;
177 ret = canIoctl(*handle, NTCAN_IOCTL_GET_CTRL_STATUS, &ctrl_state);
178 if (ret != NTCAN_SUCCESS) {
179 AERROR << "NTCAN_IOCTL_GET_CTRL_STATUS failed for device with error: "
180 << ret << " (" << StatusString(ret) << ")";
181 return ret;
182 }
183
184 NTCAN_BITRATE bitrate;
185 ret = canIoctl(*handle, NTCAN_IOCTL_GET_BITRATE_DETAILS, &bitrate);
186 if (ret != NTCAN_SUCCESS) {
187 AERROR << "NTCAN_IOCTL_GET_BITRATE_ for device with error: " << ret << " ("
188 << StatusString(ret) << ")";
189 return ret;
190 }
191 return ret;
192}
193
194void EsdCanTest(const int can_id, ComponentStatus* status) {
195 NTCAN_HANDLE handle;
196 const NTCAN_RESULT ret = EsdCanTest(can_id, &handle);
197 canClose(handle);
198
200 ret == NTCAN_SUCCESS ? ComponentStatus::OK : ComponentStatus::ERROR,
201 StatusString(ret), status);
202}
203#else
204// USE_ESD_CAN is not set, do dummy check.
205void EsdCanTest(const int can_id, ComponentStatus* status) {
206 SummaryMonitor::EscalateStatus(ComponentStatus::ERROR,
207 "USE_ESD_CAN is not defined during compiling",
208 status);
209}
210#endif
211
212} // namespace
213
215 : RecurrentRunner(FLAGS_esdcan_monitor_name,
216 FLAGS_esdcan_monitor_interval) {}
217
218void EsdCanMonitor::RunOnce(const double current_time) {
219 Component* component = apollo::common::util::FindOrNull(
220 *MonitorManager::Instance()->GetStatus()->mutable_components(),
221 FLAGS_esdcan_component_name);
222 if (component == nullptr) {
223 // Canbus is not monitored in current mode, skip.
224 return;
225 }
226
227 auto* status = component->mutable_other_status();
228 status->clear_status();
229 EsdCanTest(FLAGS_esdcan_id, status);
230}
231
232} // namespace monitor
233} // namespace apollo
void RunOnce(const double current_time) override
static void EscalateStatus(const ComponentStatus::Status new_status, const std::string &message, ComponentStatus *current_status)
DEFINE_int32(esdcan_id, 0, "ESD CAN id.")
DEFINE_double(esdcan_monitor_interval, 3, "CAN status checking interval (s).")
DEFINE_string(esdcan_monitor_name, "EsdCanMonitor", "Name of the CAN monitor.")
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
Some map util functions.
class register implement
Definition arena_queue.h:37