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