blob: 0feee8f51a61994716b6cc1b95cba02541b140e3 [file] [log] [blame]
chrisweircf36cea2019-11-08 16:41:02 -08001/*
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 "CanBusSlcan.h"
18
19#include <android-base/logging.h>
20#include <libnetdevice/can.h>
21#include <libnetdevice/libnetdevice.h>
22
23#include <net/if.h>
24#include <sys/ioctl.h>
25#include <sys/stat.h>
26#include <termios.h>
27
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080028namespace android::hardware::automotive::can::V1_0::implementation {
chrisweircf36cea2019-11-08 16:41:02 -080029
30namespace slcanprotocol {
31static const std::string kOpenCommand = "O\r";
32static const std::string kCloseCommand = "C\r";
33static constexpr int kSlcanDiscipline = N_SLCAN;
34static constexpr int kDefaultDiscipline = N_TTY;
35
36static const std::map<uint32_t, std::string> kBitrateCommands = {
37 {10000, "C\rS0\r"}, {20000, "C\rS1\r"}, {50000, "C\rS2\r"},
38 {100000, "C\rS3\r"}, {125000, "C\rS4\r"}, {250000, "C\rS5\r"},
39 {500000, "C\rS6\r"}, {800000, "C\rS7\r"}, {1000000, "C\rS8\r"}};
40} // namespace slcanprotocol
41
42/**
43 * Serial Line CAN constructor
44 * \param string uartName - name of slcan device (e.x. /dev/ttyUSB0)
45 * \param uint32_t bitrate - speed of the CAN bus (125k = MSCAN, 500k = HSCAN)
46 */
47CanBusSlcan::CanBusSlcan(const std::string& uartName, uint32_t bitrate)
48 : CanBus(), mUartName(uartName), kBitrate(bitrate) {}
49
chrisweir39187db2020-01-06 15:21:07 -080050/** helper function to update CanBusSlcan object's iface name */
51ICanController::Result CanBusSlcan::updateIfaceName(base::unique_fd& uartFd) {
52 struct ifreq ifrequest = {};
53 /*
54 * Fetching the iface name with an ioctl won't interfere with an open socketCAN iface attached
55 * to this tty. This is important in the event we are trying to register a SLCAN based iface
56 * that has already been configured and brought up.
57 */
58 if (ioctl(uartFd.get(), SIOCGIFNAME, ifrequest.ifr_name) < 0) {
59 LOG(ERROR) << "Failed to get the name of the created device: " << strerror(errno);
60 return ICanController::Result::UNKNOWN_ERROR;
61 }
62
63 // Update the CanBus object with name that was assigned to it
64 mIfname = ifrequest.ifr_name;
65 return ICanController::Result::OK;
66}
67
chrisweircf36cea2019-11-08 16:41:02 -080068ICanController::Result CanBusSlcan::preUp() {
69 // verify valid bitrate and translate to serial command format
chrisweir39187db2020-01-06 15:21:07 -080070 std::optional<std::string> canBitrateCommand = std::nullopt;
71 if (kBitrate != 0) {
72 const auto lookupIt = slcanprotocol::kBitrateCommands.find(kBitrate);
73 if (lookupIt == slcanprotocol::kBitrateCommands.end()) {
chrisweir204b8f92020-01-09 15:25:45 -080074 return ICanController::Result::BAD_BITRATE;
chrisweir39187db2020-01-06 15:21:07 -080075 }
76 canBitrateCommand = lookupIt->second;
chrisweircf36cea2019-11-08 16:41:02 -080077 }
chrisweircf36cea2019-11-08 16:41:02 -080078
79 /* Attempt to open the uart in r/w without blocking or becoming the
80 * controlling terminal */
81 mFd = base::unique_fd(open(mUartName.c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY));
82 if (!mFd.ok()) {
83 LOG(ERROR) << "SLCAN Failed to open " << mUartName << ": " << strerror(errno);
84 return ICanController::Result::BAD_ADDRESS;
85 }
86
chrisweir39187db2020-01-06 15:21:07 -080087 // If the device is already up, update the iface name in our CanBusSlcan object
88 if (kBitrate == 0) {
89 return updateIfaceName(mFd);
90 }
91
chrisweircf36cea2019-11-08 16:41:02 -080092 // blank terminal settings and pull them from the device
93 struct termios terminalSettings = {};
94 if (tcgetattr(mFd.get(), &terminalSettings) < 0) {
95 LOG(ERROR) << "Failed to read attrs of" << mUartName << ": " << strerror(errno);
96 return ICanController::Result::UNKNOWN_ERROR;
97 }
98
99 // change settings to raw mode
100 cfmakeraw(&terminalSettings);
101
102 // disable software flow control
103 terminalSettings.c_iflag &= ~IXOFF;
104 // enable hardware flow control
105 terminalSettings.c_cflag |= CRTSCTS;
106
107 struct serial_struct serialSettings;
108 // get serial settings
109 if (ioctl(mFd.get(), TIOCGSERIAL, &serialSettings) < 0) {
110 LOG(ERROR) << "Failed to read serial settings from " << mUartName << ": "
111 << strerror(errno);
112 return ICanController::Result::UNKNOWN_ERROR;
113 }
114 // set low latency mode
115 serialSettings.flags |= ASYNC_LOW_LATENCY;
116 // apply serial settings
117 if (ioctl(mFd.get(), TIOCSSERIAL, &serialSettings) < 0) {
118 LOG(ERROR) << "Failed to set low latency mode on " << mUartName << ": " << strerror(errno);
119 return ICanController::Result::UNKNOWN_ERROR;
120 }
121
122 /* TCSADRAIN applies settings after we finish writing the rest of our
123 * changes (as opposed to TCSANOW, which changes immediately) */
124 if (tcsetattr(mFd.get(), TCSADRAIN, &terminalSettings) < 0) {
125 LOG(ERROR) << "Failed to apply terminal settings to " << mUartName << ": "
126 << strerror(errno);
127 return ICanController::Result::UNKNOWN_ERROR;
128 }
129
130 // apply speed setting for CAN
chrisweir39187db2020-01-06 15:21:07 -0800131 if (write(mFd.get(), canBitrateCommand->c_str(), canBitrateCommand->length()) <= 0) {
chrisweircf36cea2019-11-08 16:41:02 -0800132 LOG(ERROR) << "Failed to apply CAN bitrate: " << strerror(errno);
133 return ICanController::Result::UNKNOWN_ERROR;
134 }
135
136 // set open flag TODO: also support listen only
137 if (write(mFd.get(), slcanprotocol::kOpenCommand.c_str(),
138 slcanprotocol::kOpenCommand.length()) <= 0) {
139 LOG(ERROR) << "Failed to set open flag: " << strerror(errno);
140 return ICanController::Result::UNKNOWN_ERROR;
141 }
142
143 // set line discipline to slcan
144 if (ioctl(mFd.get(), TIOCSETD, &slcanprotocol::kSlcanDiscipline) < 0) {
145 LOG(ERROR) << "Failed to set line discipline to slcan: " << strerror(errno);
146 return ICanController::Result::UNKNOWN_ERROR;
147 }
148
chrisweircf36cea2019-11-08 16:41:02 -0800149 // Update the CanBus object with name that was assigned to it
chrisweir39187db2020-01-06 15:21:07 -0800150 return updateIfaceName(mFd);
chrisweircf36cea2019-11-08 16:41:02 -0800151}
152
153bool CanBusSlcan::postDown() {
154 // reset the line discipline to TTY mode
155 if (ioctl(mFd.get(), TIOCSETD, &slcanprotocol::kDefaultDiscipline) < 0) {
156 LOG(ERROR) << "Failed to reset line discipline!";
157 return false;
158 }
159
160 // issue the close command
161 if (write(mFd.get(), slcanprotocol::kCloseCommand.c_str(),
162 slcanprotocol::kCloseCommand.length()) <= 0) {
163 LOG(ERROR) << "Failed to close tty!";
164 return false;
165 }
166
167 // close our unique_fd
168 mFd.reset();
169
170 return true;
171}
172
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -0800173} // namespace android::hardware::automotive::can::V1_0::implementation