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 "CanController.h" |
| 18 | |
| 19 | #include "CanBusNative.h" |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 20 | #include "CanBusSlcan.h" |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 21 | #include "CanBusVirtual.h" |
| 22 | |
| 23 | #include <android-base/logging.h> |
| 24 | #include <android/hidl/manager/1.2/IServiceManager.h> |
| 25 | |
| 26 | #include <regex> |
| 27 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 28 | namespace android::hardware::automotive::can::V1_0::implementation { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 29 | |
Tomasz Wasilczyk | f3da9b6 | 2020-02-14 10:54:28 -0800 | [diff] [blame^] | 30 | using IfId = ICanController::BusConfig::InterfaceId; |
| 31 | using IfIdDisc = ICanController::BusConfig::InterfaceId::hidl_discriminator; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 32 | |
| 33 | Return<void> CanController::getSupportedInterfaceTypes(getSupportedInterfaceTypes_cb _hidl_cb) { |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 34 | _hidl_cb({ICanController::InterfaceType::VIRTUAL, ICanController::InterfaceType::SOCKETCAN, |
| 35 | ICanController::InterfaceType::SLCAN}); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 36 | return {}; |
| 37 | } |
| 38 | |
| 39 | static bool isValidName(const std::string& name) { |
| 40 | static const std::regex nameRE("^[a-zA-Z0-9_]{1,32}$"); |
| 41 | return std::regex_match(name, nameRE); |
| 42 | } |
| 43 | |
Tomasz Wasilczyk | f3da9b6 | 2020-02-14 10:54:28 -0800 | [diff] [blame^] | 44 | Return<ICanController::Result> CanController::upInterface(const ICanController::BusConfig& config) { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 45 | LOG(VERBOSE) << "Attempting to bring interface up: " << toString(config); |
| 46 | |
| 47 | std::lock_guard<std::mutex> lck(mCanBusesGuard); |
| 48 | |
| 49 | if (!isValidName(config.name)) { |
| 50 | LOG(ERROR) << "Bus name " << config.name << " is invalid"; |
| 51 | return ICanController::Result::UNKNOWN_ERROR; |
| 52 | } |
| 53 | |
| 54 | if (mCanBuses.find(config.name) != mCanBuses.end()) { |
| 55 | LOG(ERROR) << "Bus " << config.name << " is already up"; |
| 56 | return ICanController::Result::INVALID_STATE; |
| 57 | } |
| 58 | |
| 59 | sp<CanBus> busService; |
| 60 | |
Tomasz Wasilczyk | f3da9b6 | 2020-02-14 10:54:28 -0800 | [diff] [blame^] | 61 | if (config.interfaceId.getDiscriminator() == IfIdDisc::socketcan) { |
| 62 | // TODO(b/142654031): support serialno |
| 63 | auto& socketcan = config.interfaceId.socketcan(); |
| 64 | if (socketcan.getDiscriminator() == IfId::Socketcan::hidl_discriminator::ifname) { |
| 65 | busService = new CanBusNative(socketcan.ifname(), config.bitrate); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 66 | } else { |
Tomasz Wasilczyk | f3da9b6 | 2020-02-14 10:54:28 -0800 | [diff] [blame^] | 67 | return ICanController::Result::BAD_INTERFACE_ID; |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 68 | } |
Tomasz Wasilczyk | f3da9b6 | 2020-02-14 10:54:28 -0800 | [diff] [blame^] | 69 | } else if (config.interfaceId.getDiscriminator() == IfIdDisc::virtualif) { |
| 70 | busService = new CanBusVirtual(config.interfaceId.virtualif().ifname); |
| 71 | } else if (config.interfaceId.getDiscriminator() == IfIdDisc::slcan) { |
| 72 | // TODO(b/142654031): support serialno |
| 73 | auto& slcan = config.interfaceId.slcan(); |
| 74 | if (slcan.getDiscriminator() == IfId::Slcan::hidl_discriminator::ttyname) { |
| 75 | busService = new CanBusSlcan(slcan.ttyname(), config.bitrate); |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 76 | } else { |
Tomasz Wasilczyk | f3da9b6 | 2020-02-14 10:54:28 -0800 | [diff] [blame^] | 77 | return ICanController::Result::BAD_INTERFACE_ID; |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 78 | } |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 79 | } else { |
| 80 | return ICanController::Result::NOT_SUPPORTED; |
| 81 | } |
| 82 | |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 83 | busService->setErrorCallback([this, name = config.name]() { downInterface(name); }); |
| 84 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 85 | const auto result = busService->up(); |
| 86 | if (result != ICanController::Result::OK) return result; |
| 87 | |
| 88 | if (busService->registerAsService(config.name) != OK) { |
| 89 | LOG(ERROR) << "Failed to register ICanBus/" << config.name; |
| 90 | if (!busService->down()) { |
| 91 | LOG(WARNING) << "Failed to bring down CAN bus that failed to register"; |
| 92 | } |
| 93 | return ICanController::Result::UNKNOWN_ERROR; |
| 94 | } |
| 95 | |
| 96 | mCanBuses[config.name] = busService; |
| 97 | |
| 98 | return ICanController::Result::OK; |
| 99 | } |
| 100 | |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 101 | static bool unregisterCanBusService(const hidl_string& name, sp<CanBus> busService) { |
| 102 | auto manager = hidl::manager::V1_2::IServiceManager::getService(); |
| 103 | if (!manager) return false; |
| 104 | const auto res = manager->tryUnregister(ICanBus::descriptor, name, busService); |
| 105 | if (!res.isOk()) return false; |
| 106 | return res; |
| 107 | } |
| 108 | |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 109 | Return<bool> CanController::downInterface(const hidl_string& name) { |
| 110 | LOG(VERBOSE) << "Attempting to bring interface down: " << name; |
| 111 | |
| 112 | std::lock_guard<std::mutex> lck(mCanBusesGuard); |
| 113 | |
| 114 | auto busEntry = mCanBuses.extract(name); |
| 115 | if (!busEntry) { |
| 116 | LOG(WARNING) << "Interface " << name << " is not up"; |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | auto success = true; |
| 121 | |
Tomasz Wasilczyk | a906196 | 2019-11-04 12:53:09 -0800 | [diff] [blame] | 122 | if (!unregisterCanBusService(name, busEntry.mapped())) { |
Tomasz Wasilczyk | 8732967 | 2019-07-12 11:43:00 -0700 | [diff] [blame] | 123 | LOG(ERROR) << "Couldn't unregister " << name; |
| 124 | // don't return yet, let's try to do best-effort cleanup |
| 125 | success = false; |
| 126 | } |
| 127 | |
| 128 | if (!busEntry.mapped()->down()) { |
| 129 | LOG(ERROR) << "Couldn't bring " << name << " down"; |
| 130 | success = false; |
| 131 | } |
| 132 | |
| 133 | return success; |
| 134 | } |
| 135 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 136 | } // namespace android::hardware::automotive::can::V1_0::implementation |