blob: fa1048d6ac5040398bd9dfb2e7e8e3ce31152cf4 [file] [log] [blame]
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -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 <android-base/logging.h>
18#include <android/hardware/automotive/can/1.0/ICanController.h>
19#include <android/hidl/manager/1.2/IServiceManager.h>
20#include <hidl-utils/hidl-utils.h>
21
22#include <iostream>
23#include <string>
24
25namespace android {
26namespace hardware {
27namespace automotive {
28namespace can {
29
30using ICanController = V1_0::ICanController;
31
32static void usage() {
33 std::cerr << "CAN bus HAL Control tool" << std::endl;
34 std::cerr << std::endl << "usage:" << std::endl << std::endl;
35 std::cerr << "canhalctrl up <bus name> <type> <interface> [baudrate]" << std::endl;
36 std::cerr << "where:" << std::endl;
37 std::cerr << " bus name - name under which ICanBus will be published" << std::endl;
38 std::cerr << " type - one of: virtual, socketcan, slcan, indexed" << std::endl;
39 std::cerr << " interface - hardware identifier (like can0, vcan0, /dev/ttyUSB0)" << std::endl;
40 std::cerr << " baudrate - such as 100000, 125000, 250000, 500000" << std::endl;
41 std::cerr << std::endl;
42 std::cerr << "canhalctrl down <bus name>" << std::endl;
43 std::cerr << "where:" << std::endl;
44 std::cerr << " bus name - name under which ICanBus will be published" << std::endl;
45}
46
47static hidl_vec<hidl_string> getControlServices() {
48 auto manager = hidl::manager::V1_2::IServiceManager::getService();
49 hidl_vec<hidl_string> services;
50 manager->listManifestByInterface(ICanController::descriptor, hidl_utils::fill(&services));
51 if (services.size() == 0) {
52 std::cerr << "No ICanController services registered (missing privileges?)" << std::endl;
53 exit(-1);
54 }
55 return services;
56}
57
58static bool isSupported(sp<ICanController> ctrl, ICanController::InterfaceType iftype) {
59 hidl_vec<ICanController::InterfaceType> supported;
60 if (!ctrl->getSupportedInterfaceTypes(hidl_utils::fill(&supported)).isOk()) return false;
61 return supported.contains(iftype);
62}
63
64static int up(const std::string& busName, ICanController::InterfaceType type,
65 const std::string& interface, uint32_t baudrate) {
66 bool anySupported = false;
67 for (auto&& service : getControlServices()) {
68 auto ctrl = ICanController::getService(service);
69 if (ctrl == nullptr) {
70 std::cerr << "Couldn't open ICanController/" << service;
71 continue;
72 }
73
74 if (!isSupported(ctrl, type)) continue;
75 anySupported = true;
76
77 ICanController::BusConfiguration config = {};
78 config.name = busName;
79 config.iftype = type;
80 config.baudrate = baudrate;
81
82 if (type == ICanController::InterfaceType::INDEXED) {
83 config.interfaceId.index(std::stol(interface));
84 } else {
85 config.interfaceId.address(interface);
86 }
87
88 const auto upresult = ctrl->upInterface(config);
89 if (upresult == ICanController::Result::OK) return 0;
90 std::cerr << "Failed to bring interface up: " << toString(upresult) << std::endl;
91 // Let's continue the loop to try other controllers.
92 }
93
94 if (!anySupported) {
95 std::cerr << "No controller supports " << toString(type) << std::endl;
96 }
97 return -1;
98}
99
100static int down(const std::string& busName) {
101 for (auto&& service : getControlServices()) {
102 auto ctrl = ICanController::getService(service);
103 if (ctrl == nullptr) continue;
104
105 if (ctrl->downInterface(busName)) return 0;
106 }
107
108 std::cerr << "Failed to bring interface " << busName << " down (maybe it's down already?)"
109 << std::endl;
110 return -1;
111}
112
113static std::optional<ICanController::InterfaceType> parseInterfaceType(const std::string& str) {
114 if (str == "virtual") return ICanController::InterfaceType::VIRTUAL;
115 if (str == "socketcan") return ICanController::InterfaceType::SOCKETCAN;
116 if (str == "slcan") return ICanController::InterfaceType::SLCAN;
117 if (str == "indexed") return ICanController::InterfaceType::INDEXED;
118 return std::nullopt;
119}
120
121static int main(int argc, char* argv[]) {
122 base::SetDefaultTag("CanHalControl");
123 base::SetMinimumLogSeverity(android::base::VERBOSE);
124
125 if (argc == 0) {
126 usage();
127 return 0;
128 }
129
130 std::string cmd(argv[0]);
131 argv++;
132 argc--;
133
134 if (cmd == "up") {
135 if (argc < 3 || argc > 4) {
136 std::cerr << "Invalid number of arguments to up command: " << argc << std::endl;
137 usage();
138 return -1;
139 }
140
141 const std::string busName(argv[0]);
142 const std::string typeStr(argv[1]);
143 const std::string interface(argv[2]);
144
145 const auto type = parseInterfaceType(typeStr);
146 if (!type) {
147 std::cerr << "Invalid interface type: " << typeStr << std::endl;
148 usage();
149 return -1;
150 }
151
152 long long baudrate = 0;
153 if (argc == 4) {
154 baudrate = std::stoll(argv[3]);
155 }
156
157 return up(busName, *type, interface, baudrate);
158 } else if (cmd == "down") {
159 if (argc != 1) {
160 std::cerr << "Invalid number of arguments to down command: " << argc << std::endl;
161 usage();
162 return -1;
163 }
164
165 return down(argv[0]);
166 } else {
167 std::cerr << "Invalid command: " << cmd << std::endl;
168 usage();
169 return -1;
170 }
171}
172
173} // namespace can
174} // namespace automotive
175} // namespace hardware
176} // namespace android
177
178int main(int argc, char* argv[]) {
179 if (argc < 1) return -1;
180 return ::android::hardware::automotive::can::main(--argc, ++argv);
181}