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 <android-base/logging.h>
18 #include <android-base/parseint.h>
19 #include <android/hardware/automotive/can/1.0/ICanController.h>
20 #include <android/hidl/manager/1.2/IServiceManager.h>
21 #include <hidl-utils/hidl-utils.h>
22 #include <libcanhaltools/libcanhaltools.h>
23 
24 #include <iostream>
25 #include <string>
26 
27 namespace android::hardware::automotive::can {
28 
29 using ICanController = V1_0::ICanController;
30 
usage()31 static void usage() {
32     std::cerr << "CAN bus HAL Control tool" << std::endl;
33     std::cerr << std::endl << "usage:" << std::endl << std::endl;
34     std::cerr << "canhalctrl up <bus name> <type> <interface> [bitrate]" << std::endl;
35     std::cerr << "where:" << std::endl;
36     std::cerr << " bus name - name under which ICanBus will be published" << std::endl;
37     std::cerr << " type - one of: virtual, socketcan, slcan, indexed" << std::endl;
38     std::cerr << " interface - hardware identifier (like can0, vcan0, /dev/ttyUSB0)" << std::endl;
39     std::cerr << " bitrate - such as 100000, 125000, 250000, 500000" << std::endl;
40     std::cerr << std::endl;
41     std::cerr << "canhalctrl down <bus name>" << std::endl;
42     std::cerr << "where:" << std::endl;
43     std::cerr << " bus name - name under which ICanBus will be published" << std::endl;
44 }
45 
up(const std::string & busName,ICanController::InterfaceType type,const std::string & interface,uint32_t bitrate)46 static int up(const std::string& busName, ICanController::InterfaceType type,
47               const std::string& interface, uint32_t bitrate) {
48     bool anySupported = false;
49     for (auto&& service : libcanhaltools::getControlServices()) {
50         auto ctrl = ICanController::getService(service);
51         if (ctrl == nullptr) {
52             std::cerr << "Couldn't open ICanController/" << service;
53             continue;
54         }
55 
56         if (!libcanhaltools::isSupported(ctrl, type)) continue;
57         anySupported = true;
58 
59         ICanController::BusConfig config = {};
60         config.name = busName;
61         config.bitrate = bitrate;
62 
63         // TODO(b/146214370): move interfaceId constructors to a library
64         using IfCfg = ICanController::BusConfig::InterfaceId;
65         if (type == ICanController::InterfaceType::VIRTUAL) {
66             config.interfaceId.virtualif({interface});
67         } else if (type == ICanController::InterfaceType::SOCKETCAN) {
68             IfCfg::Socketcan socketcan = {};
69             socketcan.ifname(interface);
70             config.interfaceId.socketcan(socketcan);
71         } else if (type == ICanController::InterfaceType::SLCAN) {
72             IfCfg::Slcan slcan = {};
73             slcan.ttyname(interface);
74             config.interfaceId.slcan(slcan);
75         } else if (type == ICanController::InterfaceType::INDEXED) {
76             unsigned idx;
77             if (!android::base::ParseUint(interface, &idx, unsigned(UINT8_MAX))) {
78                 std::cerr << "Interface index out of range: " << idx;
79                 return -1;
80             }
81             config.interfaceId.indexed({uint8_t(idx)});
82         } else {
83             CHECK(false) << "Unexpected interface type: " << toString(type);
84         }
85 
86         const auto upresult = ctrl->upInterface(config);
87         if (upresult == ICanController::Result::OK) return 0;
88         std::cerr << "Failed to bring interface up: " << toString(upresult) << std::endl;
89         // Let's continue the loop to try other controllers.
90     }
91 
92     if (!anySupported) {
93         std::cerr << "No controller supports " << toString(type) << std::endl;
94     }
95     return -1;
96 }
97 
down(const std::string & busName)98 static int down(const std::string& busName) {
99     for (auto&& service : libcanhaltools::getControlServices()) {
100         auto ctrl = ICanController::getService(service);
101         if (ctrl == nullptr) continue;
102 
103         if (ctrl->downInterface(busName)) return 0;
104     }
105 
106     std::cerr << "Failed to bring interface " << busName << " down (maybe it's down already?)"
107               << std::endl;
108     return -1;
109 }
110 
parseInterfaceType(const std::string & str)111 static std::optional<ICanController::InterfaceType> parseInterfaceType(const std::string& str) {
112     if (str == "virtual") return ICanController::InterfaceType::VIRTUAL;
113     if (str == "socketcan") return ICanController::InterfaceType::SOCKETCAN;
114     if (str == "slcan") return ICanController::InterfaceType::SLCAN;
115     if (str == "indexed") return ICanController::InterfaceType::INDEXED;
116     return std::nullopt;
117 }
118 
main(int argc,char * argv[])119 static int main(int argc, char* argv[]) {
120     base::SetDefaultTag("CanHalControl");
121     base::SetMinimumLogSeverity(android::base::VERBOSE);
122 
123     if (argc == 0) {
124         usage();
125         return 0;
126     }
127 
128     std::string cmd(argv[0]);
129     argv++;
130     argc--;
131 
132     if (cmd == "up") {
133         if (argc < 3 || argc > 4) {
134             std::cerr << "Invalid number of arguments to up command: " << argc << std::endl;
135             usage();
136             return -1;
137         }
138 
139         const std::string busName(argv[0]);
140         const std::string typeStr(argv[1]);
141         const std::string interface(argv[2]);
142 
143         const auto type = parseInterfaceType(typeStr);
144         if (!type) {
145             std::cerr << "Invalid interface type: " << typeStr << std::endl;
146             usage();
147             return -1;
148         }
149 
150         uint32_t bitrate = 0;
151         if (argc == 4 && !android::base::ParseUint(argv[3], &bitrate)) {
152             std::cerr << "Invalid bitrate!" << std::endl;
153             usage();
154             return -1;
155         }
156 
157         return up(busName, *type, interface, bitrate);
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 android::hardware::automotive::can
174 
main(int argc,char * argv[])175 int main(int argc, char* argv[]) {
176     if (argc < 1) return -1;
177     return ::android::hardware::automotive::can::main(--argc, ++argv);
178 }
179