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 "CanController.h"
18 
19 #include "CanBusNative.h"
20 #include "CanBusSlcan.h"
21 #include "CanBusVirtual.h"
22 
23 #include <android-base/logging.h>
24 #include <android/hidl/manager/1.2/IServiceManager.h>
25 
26 #include <automotive/filesystem>
27 #include <fstream>
28 #include <regex>
29 
30 namespace android::hardware::automotive::can::V1_0::implementation {
31 
32 using IfId = ICanController::BusConfig::InterfaceId;
33 using IfIdDisc = ICanController::BusConfig::InterfaceId::hidl_discriminator;
34 namespace fs = android::hardware::automotive::filesystem;
35 
36 namespace fsErrors {
37 static const std::error_code ok;
38 static const std::error_code eperm(EPERM, std::generic_category());
39 static const std::error_code enoent(ENOENT, std::generic_category());
40 static const std::error_code eacces(EACCES, std::generic_category());
41 }  // namespace fsErrors
42 
43 /* In the /sys/devices tree, there are files called "serial", which contain the serial numbers
44  * for various devices. The exact location inside of this directory is dependent upon the
45  * hardware we are running on, so we have to start from /sys/devices and work our way down. */
46 static const fs::path kDevPath("/sys/devices/");
47 static const std::regex kTtyRe("^tty[A-Z]+[0-9]+$");
48 static constexpr auto kOpts = ~(fs::directory_options::follow_directory_symlink |
49                                 fs::directory_options::skip_permission_denied);
50 
51 /**
52  * A helper object to associate the interface name and type of a USB to CAN adapter.
53  */
54 struct UsbCanIface {
55     ICanController::InterfaceType iftype;
56     std::string ifaceName;
57 };
58 
getSupportedInterfaceTypes(getSupportedInterfaceTypes_cb _hidl_cb)59 Return<void> CanController::getSupportedInterfaceTypes(getSupportedInterfaceTypes_cb _hidl_cb) {
60     _hidl_cb({ICanController::InterfaceType::VIRTUAL, ICanController::InterfaceType::SOCKETCAN,
61               ICanController::InterfaceType::SLCAN});
62     return {};
63 }
64 
isValidName(const std::string & name)65 static bool isValidName(const std::string& name) {
66     static const std::regex nameRE("^[a-zA-Z0-9_]{1,32}$");
67     return std::regex_match(name, nameRE);
68 }
69 
70 /**
71  * Given a UsbCanIface object, get the ifaceName given the serialPath.
72  *
73  * \param serialPath - Absolute path to a "serial" file for a given device in /sys.
74  * \return A populated UsbCanIface. On failure, nullopt is returned.
75  */
getIfaceName(fs::path serialPath)76 static std::optional<UsbCanIface> getIfaceName(fs::path serialPath) {
77     std::error_code fsStatus;
78     // Since the path is to a file called "serial", we need to search its parent directory.
79     fs::recursive_directory_iterator fsItr(serialPath.parent_path(), kOpts, fsStatus);
80     if (fsStatus != fsErrors::ok) {
81         LOG(ERROR) << "Failed to open " << serialPath.parent_path();
82         return std::nullopt;
83     }
84 
85     for (; fsStatus == fsErrors::ok && fsItr != fs::recursive_directory_iterator();
86          fsItr.increment(fsStatus)) {
87         /* We want either a directory called "net" or a directory that looks like tty<something>, so
88          * skip files. */
89         bool isDir = fsItr->is_directory(fsStatus);
90         if (fsStatus != fsErrors::ok || !isDir) continue;
91 
92         /* path() returns an iterator that steps through directories from / to the leaf.
93          * end() returns one past the leaf of the path, but we want the leaf. Decrementing the
94          * path gives us a pointer to the leaf, which we then dereference.*/
95         std::string currentDir = *(--(fsItr->path().end()));
96         if (currentDir == "net") {
97             /* This device is a SocketCAN device. The iface name is the only directory under
98              * net/. Multiple directories under net/ is an error.*/
99             fs::directory_iterator netItr(fsItr->path(), kOpts, fsStatus);
100             if (fsStatus != fsErrors::ok) {
101                 LOG(ERROR) << "Failed to open " << fsItr->path() << " to get net name!";
102                 return std::nullopt;
103             }
104 
105             // Get the leaf of the path. This is the interface name, assuming it's the only leaf.
106             std::string netName = *(--(netItr->path().end()));
107 
108             // Check if there is more than one item in net/
109             netItr.increment(fsStatus);
110             if (fsStatus != fsErrors::ok) {
111                 // It's possible we have a valid net name, but this is most likely an error.
112                 LOG(ERROR) << "Failed to verify " << fsItr->path() << " has valid net name!";
113                 return std::nullopt;
114             }
115             if (netItr != fs::directory_iterator()) {
116                 // There should never be more than one name under net/
117                 LOG(ERROR) << "Found more than one net name in " << fsItr->path() << "!";
118                 return std::nullopt;
119             }
120             return {{ICanController::InterfaceType::SOCKETCAN, netName}};
121         } else if (std::regex_match(currentDir, kTtyRe)) {
122             // This device is a USB serial device, and currentDir is the tty name.
123             return {{ICanController::InterfaceType::SLCAN, "/dev/" + currentDir}};
124         }
125     }
126 
127     // check if the loop above exited due to a c++fs error.
128     if (fsStatus != fsErrors::ok) {
129         LOG(ERROR) << "Failed search filesystem: " << fsStatus;
130     }
131     return std::nullopt;
132 }
133 
134 /**
135  * A helper function to read the serial number from a "serial" file in /sys/devices/
136  *
137  * \param serialnoPath - path to the file to read.
138  * \return the serial number, or nullopt on failure.
139  */
readSerialNo(const std::string & serialnoPath)140 static std::optional<std::string> readSerialNo(const std::string& serialnoPath) {
141     std::ifstream serialnoStream(serialnoPath);
142     std::string serialno;
143     if (!serialnoStream.good()) {
144         LOG(ERROR) << "Failed to read serial number from " << serialnoPath;
145         return std::nullopt;
146     }
147     std::getline(serialnoStream, serialno);
148     return serialno;
149 }
150 
151 /**
152  * Searches for USB devices found in /sys/devices/, and attempts to find a device matching the
153  * provided list of serial numbers.
154  *
155  * \param configSerialnos - a list of serial number (suffixes) from the HAL config.
156  * \param iftype - the type of the interface to be located.
157  * \return a matching USB device. On failure, std::nullopt is returned.
158  */
findUsbDevice(const hidl_vec<hidl_string> & configSerialnos)159 static std::optional<UsbCanIface> findUsbDevice(const hidl_vec<hidl_string>& configSerialnos) {
160     std::error_code fsStatus;
161     fs::recursive_directory_iterator fsItr(kDevPath, kOpts, fsStatus);
162     if (fsStatus != fsErrors::ok) {
163         LOG(ERROR) << "Failed to open " << kDevPath;
164         return std::nullopt;
165     }
166 
167     for (; fsStatus == fsErrors::ok && fsItr != fs::recursive_directory_iterator();
168          fsItr.increment(fsStatus)) {
169         // We want to find a file called "serial", which is in a directory somewhere. Skip files.
170         bool isDir = fsItr->is_directory(fsStatus);
171         if (fsStatus != fsErrors::ok) {
172             LOG(ERROR) << "Failed check if " << fsStatus;
173             return std::nullopt;
174         }
175         if (!isDir) continue;
176 
177         auto serialnoPath = fsItr->path() / "serial";
178         bool isReg = fs::is_regular_file(serialnoPath, fsStatus);
179 
180         /* Make sure we have permissions to this directory, ignore enoent, since the file
181          * "serial" may not exist, which is ok. */
182         if (fsStatus == fsErrors::eperm || fsStatus == fsErrors::eacces) {
183             /* This means we  don't have access to this directory. If we recurse into it, this
184              * will cause the iterator to loose its state and we'll crash. */
185             fsItr.disable_recursion_pending();
186             continue;
187         }
188         if (fsStatus == fsErrors::enoent) continue;
189         if (fsStatus != fsErrors::ok) {
190             LOG(WARNING) << "An unexpected error occurred while checking for serialno: "
191                          << fsStatus;
192             continue;
193         }
194         if (!isReg) continue;
195 
196         // we found a serial number
197         auto serialno = readSerialNo(serialnoPath);
198         if (!serialno.has_value()) continue;
199 
200         // see if the serial number exists in the config
201         for (auto&& cfgSn : configSerialnos) {
202             if (serialno->ends_with(std::string(cfgSn))) {
203                 auto ifaceInfo = getIfaceName(serialnoPath);
204                 if (!ifaceInfo.has_value()) break;
205                 return ifaceInfo;
206             }
207         }
208     }
209     if (fsStatus != fsErrors::ok) {
210         LOG(ERROR) << "Error searching filesystem: " << fsStatus;
211         return std::nullopt;
212     }
213     return std::nullopt;
214 }
215 
upInterface(const ICanController::BusConfig & config)216 Return<ICanController::Result> CanController::upInterface(const ICanController::BusConfig& config) {
217     LOG(VERBOSE) << "Attempting to bring interface up: " << toString(config);
218 
219     std::lock_guard<std::mutex> lck(mCanBusesGuard);
220 
221     if (!isValidName(config.name)) {
222         LOG(ERROR) << "Bus name " << config.name << " is invalid";
223         return ICanController::Result::BAD_SERVICE_NAME;
224     }
225 
226     if (mCanBuses.find(config.name) != mCanBuses.end()) {
227         LOG(ERROR) << "Bus " << config.name << " is already up";
228         return ICanController::Result::INVALID_STATE;
229     }
230 
231     sp<CanBus> busService;
232 
233     // SocketCAN native type interface.
234     if (config.interfaceId.getDiscriminator() == IfIdDisc::socketcan) {
235         auto& socketcan = config.interfaceId.socketcan();
236         std::string ifaceName;
237         if (socketcan.getDiscriminator() == IfId::Socketcan::hidl_discriminator::serialno) {
238             // Configure by serial number.
239             auto selectedDevice = findUsbDevice(socketcan.serialno());
240             // verify the returned device is the correct one
241             if (!selectedDevice.has_value() ||
242                 selectedDevice->iftype != ICanController::InterfaceType::SOCKETCAN) {
243                 return ICanController::Result::BAD_INTERFACE_ID;
244             }
245             ifaceName = selectedDevice->ifaceName;
246         } else {
247             // configure by iface name.
248             ifaceName = socketcan.ifname();
249         }
250         busService = new CanBusNative(ifaceName, config.bitrate);
251     }
252     // Virtual interface.
253     else if (config.interfaceId.getDiscriminator() == IfIdDisc::virtualif) {
254         busService = new CanBusVirtual(config.interfaceId.virtualif().ifname);
255     }
256     // SLCAN interface.
257     else if (config.interfaceId.getDiscriminator() == IfIdDisc::slcan) {
258         auto& slcan = config.interfaceId.slcan();
259         std::string ttyName;
260         if (slcan.getDiscriminator() == IfId::Slcan::hidl_discriminator::serialno) {
261             // Configure by serial number.
262             auto selectedDevice = findUsbDevice(slcan.serialno());
263             if (!selectedDevice.has_value() ||
264                 selectedDevice->iftype != ICanController::InterfaceType::SLCAN) {
265                 return ICanController::Result::BAD_INTERFACE_ID;
266             }
267             ttyName = selectedDevice->ifaceName;
268         } else {
269             // Configure by tty name.
270             ttyName = slcan.ttyname();
271         }
272         busService = new CanBusSlcan(ttyName, config.bitrate);
273     } else {
274         return ICanController::Result::NOT_SUPPORTED;
275     }
276 
277     busService->setErrorCallback([this, name = config.name]() { downInterface(name); });
278 
279     const auto result = busService->up();
280     if (result != ICanController::Result::OK) return result;
281 
282     if (busService->registerAsService(config.name) != OK) {
283         LOG(ERROR) << "Failed to register ICanBus/" << config.name;
284         if (!busService->down()) {
285             LOG(WARNING) << "Failed to bring down CAN bus that failed to register";
286         }
287         return ICanController::Result::BAD_SERVICE_NAME;
288     }
289 
290     mCanBuses[config.name] = busService;
291 
292     return ICanController::Result::OK;
293 }
294 
unregisterCanBusService(const hidl_string & name,sp<CanBus> busService)295 static bool unregisterCanBusService(const hidl_string& name, sp<CanBus> busService) {
296     auto manager = hidl::manager::V1_2::IServiceManager::getService();
297     if (!manager) return false;
298     const auto res = manager->tryUnregister(ICanBus::descriptor, name, busService);
299     if (!res.isOk()) return false;
300     return res;
301 }
302 
downInterface(const hidl_string & name)303 Return<bool> CanController::downInterface(const hidl_string& name) {
304     LOG(VERBOSE) << "Attempting to bring interface down: " << name;
305 
306     std::lock_guard<std::mutex> lck(mCanBusesGuard);
307 
308     auto busEntry = mCanBuses.extract(name);
309     if (!busEntry) {
310         LOG(WARNING) << "Interface " << name << " is not up";
311         return false;
312     }
313 
314     auto success = true;
315 
316     if (!unregisterCanBusService(name, busEntry.mapped())) {
317         LOG(ERROR) << "Couldn't unregister " << name;
318         // don't return yet, let's try to do best-effort cleanup
319         success = false;
320     }
321 
322     if (!busEntry.mapped()->down()) {
323         LOG(ERROR) << "Couldn't bring " << name << " down";
324         success = false;
325     }
326 
327     return success;
328 }
329 
330 }  // namespace android::hardware::automotive::can::V1_0::implementation
331