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