blob: 29d9d3c238f626764a405d8229e4c72e4b956349 [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
50ICanController::Result CanBusSlcan::preUp() {
51 // verify valid bitrate and translate to serial command format
52 const auto lookupIt = slcanprotocol::kBitrateCommands.find(kBitrate);
53 if (lookupIt == slcanprotocol::kBitrateCommands.end()) {
54 return ICanController::Result::BAD_BAUDRATE;
55 }
56 const auto canBitrateCommand = lookupIt->second;
57
58 /* Attempt to open the uart in r/w without blocking or becoming the
59 * controlling terminal */
60 mFd = base::unique_fd(open(mUartName.c_str(), O_RDWR | O_NONBLOCK | O_NOCTTY));
61 if (!mFd.ok()) {
62 LOG(ERROR) << "SLCAN Failed to open " << mUartName << ": " << strerror(errno);
63 return ICanController::Result::BAD_ADDRESS;
64 }
65
66 // blank terminal settings and pull them from the device
67 struct termios terminalSettings = {};
68 if (tcgetattr(mFd.get(), &terminalSettings) < 0) {
69 LOG(ERROR) << "Failed to read attrs of" << mUartName << ": " << strerror(errno);
70 return ICanController::Result::UNKNOWN_ERROR;
71 }
72
73 // change settings to raw mode
74 cfmakeraw(&terminalSettings);
75
76 // disable software flow control
77 terminalSettings.c_iflag &= ~IXOFF;
78 // enable hardware flow control
79 terminalSettings.c_cflag |= CRTSCTS;
80
81 struct serial_struct serialSettings;
82 // get serial settings
83 if (ioctl(mFd.get(), TIOCGSERIAL, &serialSettings) < 0) {
84 LOG(ERROR) << "Failed to read serial settings from " << mUartName << ": "
85 << strerror(errno);
86 return ICanController::Result::UNKNOWN_ERROR;
87 }
88 // set low latency mode
89 serialSettings.flags |= ASYNC_LOW_LATENCY;
90 // apply serial settings
91 if (ioctl(mFd.get(), TIOCSSERIAL, &serialSettings) < 0) {
92 LOG(ERROR) << "Failed to set low latency mode on " << mUartName << ": " << strerror(errno);
93 return ICanController::Result::UNKNOWN_ERROR;
94 }
95
96 /* TCSADRAIN applies settings after we finish writing the rest of our
97 * changes (as opposed to TCSANOW, which changes immediately) */
98 if (tcsetattr(mFd.get(), TCSADRAIN, &terminalSettings) < 0) {
99 LOG(ERROR) << "Failed to apply terminal settings to " << mUartName << ": "
100 << strerror(errno);
101 return ICanController::Result::UNKNOWN_ERROR;
102 }
103
104 // apply speed setting for CAN
105 if (write(mFd.get(), canBitrateCommand.c_str(), canBitrateCommand.length()) <= 0) {
106 LOG(ERROR) << "Failed to apply CAN bitrate: " << strerror(errno);
107 return ICanController::Result::UNKNOWN_ERROR;
108 }
109
110 // set open flag TODO: also support listen only
111 if (write(mFd.get(), slcanprotocol::kOpenCommand.c_str(),
112 slcanprotocol::kOpenCommand.length()) <= 0) {
113 LOG(ERROR) << "Failed to set open flag: " << strerror(errno);
114 return ICanController::Result::UNKNOWN_ERROR;
115 }
116
117 // set line discipline to slcan
118 if (ioctl(mFd.get(), TIOCSETD, &slcanprotocol::kSlcanDiscipline) < 0) {
119 LOG(ERROR) << "Failed to set line discipline to slcan: " << strerror(errno);
120 return ICanController::Result::UNKNOWN_ERROR;
121 }
122
123 // get the name of the device we created
124 struct ifreq ifrequest = {};
125 if (ioctl(mFd.get(), SIOCGIFNAME, ifrequest.ifr_name) < 0) {
126 LOG(ERROR) << "Failed to get the name of the created device: " << strerror(errno);
127 return ICanController::Result::UNKNOWN_ERROR;
128 }
129
130 // Update the CanBus object with name that was assigned to it
131 mIfname = ifrequest.ifr_name;
132
133 return ICanController::Result::OK;
134}
135
136bool CanBusSlcan::postDown() {
137 // reset the line discipline to TTY mode
138 if (ioctl(mFd.get(), TIOCSETD, &slcanprotocol::kDefaultDiscipline) < 0) {
139 LOG(ERROR) << "Failed to reset line discipline!";
140 return false;
141 }
142
143 // issue the close command
144 if (write(mFd.get(), slcanprotocol::kCloseCommand.c_str(),
145 slcanprotocol::kCloseCommand.length()) <= 0) {
146 LOG(ERROR) << "Failed to close tty!";
147 return false;
148 }
149
150 // close our unique_fd
151 mFd.reset();
152
153 return true;
154}
155
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -0800156} // namespace android::hardware::automotive::can::V1_0::implementation