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