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