Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 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 | |
| 17 | #include "CanSocket.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <libnetdevice/can.h> |
| 21 | #include <libnetdevice/libnetdevice.h> |
| 22 | #include <linux/can.h> |
| 23 | #include <utils/SystemClock.h> |
| 24 | |
| 25 | #include <chrono> |
| 26 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 27 | namespace android::hardware::automotive::can::V1_0::implementation { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 28 | |
| 29 | using namespace std::chrono_literals; |
| 30 | |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 31 | /* How frequently the read thread checks whether the interface was asked to be down. |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 32 | * |
| 33 | * Note: This does *not* affect read timing or bandwidth, just CPU load vs time to |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 34 | * down the interface. */ |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 35 | static constexpr auto kReadPooling = 100ms; |
| 36 | |
| 37 | std::unique_ptr<CanSocket> CanSocket::open(const std::string& ifname, ReadCallback rdcb, |
| 38 | ErrorCallback errcb) { |
| 39 | auto sock = netdevice::can::socket(ifname); |
| 40 | if (!sock.ok()) { |
| 41 | LOG(ERROR) << "Can't open CAN socket on " << ifname; |
| 42 | return nullptr; |
| 43 | } |
| 44 | |
| 45 | // Can't use std::make_unique due to private CanSocket constructor. |
| 46 | return std::unique_ptr<CanSocket>(new CanSocket(std::move(sock), rdcb, errcb)); |
| 47 | } |
| 48 | |
| 49 | CanSocket::CanSocket(base::unique_fd socket, ReadCallback rdcb, ErrorCallback errcb) |
| 50 | : mReadCallback(rdcb), |
| 51 | mErrorCallback(errcb), |
| 52 | mSocket(std::move(socket)), |
| 53 | mReaderThread(&CanSocket::readerThread, this) {} |
| 54 | |
| 55 | CanSocket::~CanSocket() { |
| 56 | mStopReaderThread = true; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 57 | |
| 58 | /* CanSocket can be brought down as a result of read failure, from the same thread, |
| 59 | * so let's just detach and let it finish on its own. */ |
| 60 | if (mReaderThreadFinished) { |
| 61 | mReaderThread.detach(); |
| 62 | } else { |
| 63 | mReaderThread.join(); |
| 64 | } |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool CanSocket::send(const struct canfd_frame& frame) { |
| 68 | const auto res = write(mSocket.get(), &frame, CAN_MTU); |
| 69 | if (res < 0) { |
chrisweir | 75a80f6 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 70 | PLOG(DEBUG) << "CanSocket send failed"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 71 | return false; |
| 72 | } |
| 73 | if (res != CAN_MTU) { |
| 74 | LOG(DEBUG) << "CanSocket sent wrong number of bytes: " << res; |
| 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | static struct timeval toTimeval(std::chrono::microseconds t) { |
| 81 | struct timeval tv; |
| 82 | tv.tv_sec = t / 1s; |
| 83 | tv.tv_usec = (t % 1s) / 1us; |
| 84 | return tv; |
| 85 | } |
| 86 | |
| 87 | static int selectRead(const base::unique_fd& fd, std::chrono::microseconds timeout) { |
| 88 | auto timeouttv = toTimeval(timeout); |
| 89 | fd_set readfds; |
| 90 | FD_ZERO(&readfds); |
| 91 | FD_SET(fd.get(), &readfds); |
| 92 | return select(fd.get() + 1, &readfds, nullptr, nullptr, &timeouttv); |
| 93 | } |
| 94 | |
| 95 | void CanSocket::readerThread() { |
| 96 | LOG(VERBOSE) << "Reader thread started"; |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 97 | int errnoCopy = 0; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 98 | |
| 99 | while (!mStopReaderThread) { |
| 100 | /* The ideal would be to have a blocking read(3) call and interrupt it with shutdown(3). |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 101 | * This is unfortunately not supported for SocketCAN, so we need to rely on select(3). */ |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 102 | const auto sel = selectRead(mSocket, kReadPooling); |
| 103 | if (sel == 0) continue; // timeout |
| 104 | if (sel == -1) { |
chrisweir | 75a80f6 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 105 | PLOG(ERROR) << "Select failed"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 106 | break; |
| 107 | } |
| 108 | |
| 109 | struct canfd_frame frame; |
| 110 | const auto nbytes = read(mSocket.get(), &frame, CAN_MTU); |
| 111 | |
| 112 | /* We could use SIOCGSTAMP to get a precise UNIX timestamp for a given packet, but what |
| 113 | * we really need is a time since boot. There is no direct way to convert between these |
| 114 | * clocks. We could implement a class to calculate the difference between the clocks |
| 115 | * (querying both several times and picking the smallest difference); apply the difference |
| 116 | * to a SIOCGSTAMP returned value; re-synchronize if the elapsed time is too much in the |
| 117 | * past (indicating the UNIX timestamp might have been adjusted). |
| 118 | * |
| 119 | * Apart from the added complexity, it's possible the added calculations and system calls |
| 120 | * would add so much time to the processing pipeline so the precision of the reported time |
| 121 | * was buried under the subsystem latency. Let's just use a local time since boot here and |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 122 | * leave precise hardware timestamps for custom proprietary implementations (if needed). */ |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 123 | const std::chrono::nanoseconds ts(elapsedRealtimeNano()); |
| 124 | |
| 125 | if (nbytes != CAN_MTU) { |
| 126 | if (nbytes >= 0) { |
| 127 | LOG(ERROR) << "Failed to read CAN packet, got " << nbytes << " bytes"; |
| 128 | break; |
| 129 | } |
| 130 | if (errno == EAGAIN) continue; |
| 131 | |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 132 | errnoCopy = errno; |
chrisweir | 75a80f6 | 2020-02-26 14:39:56 -0800 | [diff] [blame] | 133 | PLOG(ERROR) << "Failed to read CAN packet"; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | |
| 137 | mReadCallback(frame, ts); |
| 138 | } |
| 139 | |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 140 | bool failed = !mStopReaderThread; |
| 141 | auto errCb = mErrorCallback; |
| 142 | mReaderThreadFinished = true; |
| 143 | |
| 144 | // Don't access any fields from here, see CanSocket::~CanSocket comment about detached thread |
| 145 | if (failed) errCb(errnoCopy); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 146 | |
| 147 | LOG(VERBOSE) << "Reader thread stopped"; |
| 148 | } |
| 149 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 150 | } // namespace android::hardware::automotive::can::V1_0::implementation |