1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CanBus.h"
#include "CloseHandle.h"
#include <android-base/logging.h>
#include <libnetdevice/can.h>
#include <libnetdevice/libnetdevice.h>
#include <linux/can.h>
#include <linux/can/error.h>
#include <linux/can/raw.h>
namespace android::hardware::automotive::can::V1_0::implementation {
/** Whether to log sent/received packets. */
static constexpr bool kSuperVerbose = false;
Return<Result> CanBus::send(const CanMessage& message) {
std::lock_guard<std::mutex> lck(mIsUpGuard);
if (!mIsUp) return Result::INTERFACE_DOWN;
if (UNLIKELY(kSuperVerbose)) {
LOG(VERBOSE) << "Sending " << toString(message);
}
if (message.payload.size() > CAN_MAX_DLEN) return Result::PAYLOAD_TOO_LONG;
struct canfd_frame frame = {};
frame.can_id = message.id;
if (message.isExtendedId) frame.can_id |= CAN_EFF_FLAG;
if (message.remoteTransmissionRequest) frame.can_id |= CAN_RTR_FLAG;
frame.len = message.payload.size();
memcpy(frame.data, message.payload.data(), message.payload.size());
if (!mSocket->send(frame)) return Result::TRANSMISSION_FAILURE;
return Result::OK;
}
Return<void> CanBus::listen(const hidl_vec<CanMessageFilter>& filter,
const sp<ICanMessageListener>& listenerCb, listen_cb _hidl_cb) {
std::lock_guard<std::mutex> lck(mIsUpGuard);
if (listenerCb == nullptr) {
_hidl_cb(Result::INVALID_ARGUMENTS, nullptr);
return {};
}
if (!mIsUp) {
_hidl_cb(Result::INTERFACE_DOWN, nullptr);
return {};
}
std::lock_guard<std::mutex> lckListeners(mMsgListenersGuard);
sp<CloseHandle> closeHandle = new CloseHandle([this, listenerCb]() {
std::lock_guard<std::mutex> lck(mMsgListenersGuard);
std::erase_if(mMsgListeners, [&](const auto& e) { return e.callback == listenerCb; });
});
mMsgListeners.emplace_back(CanMessageListener{listenerCb, filter, closeHandle});
auto& listener = mMsgListeners.back();
// fix message IDs to have all zeros on bits not covered by mask
std::for_each(listener.filter.begin(), listener.filter.end(),
[](auto& rule) { rule.id &= rule.mask; });
_hidl_cb(Result::OK, closeHandle);
return {};
}
CanBus::CanBus() {}
CanBus::CanBus(const std::string& ifname) : mIfname(ifname) {}
CanBus::~CanBus() {
std::lock_guard<std::mutex> lck(mIsUpGuard);
CHECK(!mIsUp) << "Interface is still up while being destroyed";
std::lock_guard<std::mutex> lckListeners(mMsgListenersGuard);
CHECK(mMsgListeners.empty()) << "Listener list is not empty while interface is being destroyed";
}
void CanBus::setErrorCallback(ErrorCallback errcb) {
CHECK(!mIsUp) << "Can't set error callback while interface is up";
CHECK(mErrCb == nullptr) << "Error callback is already set";
mErrCb = errcb;
CHECK(!mIsUp) << "Can't set error callback while interface is up";
}
ICanController::Result CanBus::preUp() {
return ICanController::Result::OK;
}
bool CanBus::postDown() {
return true;
}
ICanController::Result CanBus::up() {
std::lock_guard<std::mutex> lck(mIsUpGuard);
if (mIsUp) {
LOG(WARNING) << "Interface is already up";
return ICanController::Result::INVALID_STATE;
}
const auto preResult = preUp();
if (preResult != ICanController::Result::OK) return preResult;
const auto isUp = netdevice::isUp(mIfname);
if (!isUp.has_value()) {
// preUp() should prepare the interface (either create or make sure it's there)
LOG(ERROR) << "Interface " << mIfname << " didn't get prepared";
return ICanController::Result::BAD_INTERFACE_ID;
}
if (!*isUp && !netdevice::up(mIfname)) {
LOG(ERROR) << "Can't bring " << mIfname << " up";
return ICanController::Result::UNKNOWN_ERROR;
}
mDownAfterUse = !*isUp;
using namespace std::placeholders;
CanSocket::ReadCallback rdcb = std::bind(&CanBus::onRead, this, _1, _2);
CanSocket::ErrorCallback errcb = std::bind(&CanBus::onError, this, _1);
mSocket = CanSocket::open(mIfname, rdcb, errcb);
if (!mSocket) {
if (mDownAfterUse) netdevice::down(mIfname);
return ICanController::Result::UNKNOWN_ERROR;
}
mIsUp = true;
return ICanController::Result::OK;
}
void CanBus::clearMsgListeners() {
std::vector<wp<ICloseHandle>> listenersToClose;
{
std::lock_guard<std::mutex> lck(mMsgListenersGuard);
std::transform(mMsgListeners.begin(), mMsgListeners.end(),
std::back_inserter(listenersToClose),
[](const auto& e) { return e.closeHandle; });
}
for (auto& weakListener : listenersToClose) {
/* Between populating listenersToClose and calling close method here, some listeners might
* have been already removed from the original mMsgListeners list (resulting in a dangling
* weak pointer here). It's fine - we just want to clean them up. */
auto listener = weakListener.promote();
if (listener != nullptr) listener->close();
}
std::lock_guard<std::mutex> lck(mMsgListenersGuard);
CHECK(mMsgListeners.empty()) << "Listeners list wasn't emptied";
}
void CanBus::clearErrListeners() {
std::lock_guard<std::mutex> lck(mErrListenersGuard);
mErrListeners.clear();
}
Return<sp<ICloseHandle>> CanBus::listenForErrors(const sp<ICanErrorListener>& listener) {
if (listener == nullptr) {
return new CloseHandle();
}
std::lock_guard<std::mutex> upLck(mIsUpGuard);
if (!mIsUp) {
listener->onError(ErrorEvent::INTERFACE_DOWN, true);
return new CloseHandle();
}
std::lock_guard<std::mutex> errLck(mErrListenersGuard);
mErrListeners.emplace_back(listener);
return new CloseHandle([this, listener]() {
std::lock_guard<std::mutex> lck(mErrListenersGuard);
std::erase(mErrListeners, listener);
});
}
bool CanBus::down() {
std::lock_guard<std::mutex> lck(mIsUpGuard);
if (!mIsUp) {
LOG(WARNING) << "Interface is already down";
return false;
}
mIsUp = false;
clearMsgListeners();
clearErrListeners();
mSocket.reset();
bool success = true;
if (mDownAfterUse && !netdevice::down(mIfname)) {
LOG(ERROR) << "Can't bring " << mIfname << " down";
// don't return yet, let's try to do best-effort cleanup
success = false;
}
if (!postDown()) success = false;
return success;
}
/**
* Helper function to determine if a flag meets the requirements of a
* FilterFlag. See definition of FilterFlag in types.hal
*
* \param filterFlag FilterFlag object to match flag against
* \param flag bool object from CanMessage object
*/
static bool satisfiesFilterFlag(FilterFlag filterFlag, bool flag) {
if (filterFlag == FilterFlag::DONT_CARE) return true;
if (filterFlag == FilterFlag::SET) return flag;
if (filterFlag == FilterFlag::NOT_SET) return !flag;
return false;
}
/**
* Match the filter set against message id.
*
* For details on the filters syntax, please see CanMessageFilter at
* the HAL definition (types.hal).
*
* \param filter Filter to match against
* \param id Message id to filter
* \return true if the message id matches the filter, false otherwise
*/
static bool match(const hidl_vec<CanMessageFilter>& filter, CanMessageId id, bool isRtr,
bool isExtendedId) {
if (filter.size() == 0) return true;
bool anyNonExcludeRulePresent = false;
bool anyNonExcludeRuleSatisfied = false;
for (auto& rule : filter) {
const bool satisfied = ((id & rule.mask) == rule.id) &&
satisfiesFilterFlag(rule.rtr, isRtr) &&
satisfiesFilterFlag(rule.extendedFormat, isExtendedId);
if (rule.exclude) {
// Any exclude rule being satisfied invalidates the whole filter set.
if (satisfied) return false;
} else {
anyNonExcludeRulePresent = true;
if (satisfied) anyNonExcludeRuleSatisfied = true;
}
}
return !anyNonExcludeRulePresent || anyNonExcludeRuleSatisfied;
}
void CanBus::notifyErrorListeners(ErrorEvent err, bool isFatal) {
std::lock_guard<std::mutex> lck(mErrListenersGuard);
for (auto& listener : mErrListeners) {
if (!listener->onError(err, isFatal).isOk()) {
LOG(WARNING) << "Failed to notify listener about error";
}
}
}
static ErrorEvent parseErrorFrame(const struct canfd_frame& frame) {
// decode error frame (to a degree)
if ((frame.can_id & (CAN_ERR_BUSERROR | CAN_ERR_BUSOFF)) != 0) {
return ErrorEvent::BUS_ERROR;
}
if ((frame.data[1] & CAN_ERR_CRTL_TX_OVERFLOW) != 0) {
return ErrorEvent::TX_OVERFLOW;
}
if ((frame.data[1] & CAN_ERR_CRTL_RX_OVERFLOW) != 0) {
return ErrorEvent::RX_OVERFLOW;
}
if ((frame.data[2] & CAN_ERR_PROT_OVERLOAD) != 0) {
return ErrorEvent::BUS_OVERLOAD;
}
if ((frame.can_id & CAN_ERR_PROT) != 0) {
return ErrorEvent::MALFORMED_INPUT;
}
if ((frame.can_id & (CAN_ERR_CRTL | CAN_ERR_TRX | CAN_ERR_RESTARTED)) != 0) {
// "controller restarted" constitutes a HARDWARE_ERROR imo
return ErrorEvent::HARDWARE_ERROR;
}
return ErrorEvent::UNKNOWN_ERROR;
}
void CanBus::onRead(const struct canfd_frame& frame, std::chrono::nanoseconds timestamp) {
if ((frame.can_id & CAN_ERR_FLAG) != 0) {
// error bit is set
LOG(WARNING) << "CAN Error frame received";
notifyErrorListeners(parseErrorFrame(frame), false);
return;
}
CanMessage message = {};
message.id = frame.can_id & CAN_EFF_MASK; // mask out eff/rtr/err flags
message.payload = hidl_vec<uint8_t>(frame.data, frame.data + frame.len);
message.timestamp = timestamp.count();
message.isExtendedId = (frame.can_id & CAN_EFF_FLAG) != 0;
message.remoteTransmissionRequest = (frame.can_id & CAN_RTR_FLAG) != 0;
if (UNLIKELY(kSuperVerbose)) {
LOG(VERBOSE) << "Got message " << toString(message);
}
std::lock_guard<std::mutex> lck(mMsgListenersGuard);
for (auto& listener : mMsgListeners) {
if (!match(listener.filter, message.id, message.remoteTransmissionRequest,
message.isExtendedId))
continue;
if (!listener.callback->onReceive(message).isOk() && !listener.failedOnce) {
listener.failedOnce = true;
LOG(WARNING) << "Failed to notify listener about message";
}
}
}
void CanBus::onError(int errnoVal) {
auto eventType = ErrorEvent::HARDWARE_ERROR;
if (errnoVal == ENODEV || errnoVal == ENETDOWN) {
mDownAfterUse = false;
eventType = ErrorEvent::INTERFACE_DOWN;
}
notifyErrorListeners(eventType, true);
const auto errcb = mErrCb;
if (errcb != nullptr) errcb();
}
} // namespace android::hardware::automotive::can::V1_0::implementation
|