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-base/strings.h>
20 #include <android/hardware/automotive/can/1.0/ICanBus.h>
21 #include <android/hidl/manager/1.2/IServiceManager.h>
22 
23 #include <iostream>
24 #include <string>
25 
26 namespace android::hardware::automotive::can {
27 
28 using ICanBus = V1_0::ICanBus;
29 using Result = V1_0::Result;
30 
usage()31 static void usage() {
32     std::cerr << "canhalsend - simple command line tool to send raw CAN frames" << std::endl;
33     std::cerr << std::endl << "usage:" << std::endl << std::endl;
34     std::cerr << "canhalsend <bus name> <can id>#<data>" << std::endl;
35     std::cerr << "where:" << std::endl;
36     std::cerr << " bus name - name under which ICanBus is published" << std::endl;
37     std::cerr << " can id - such as 1a5 or 1fab5982" << std::endl;
38     std::cerr << " data - such as deadbeef, 010203, or R for a remote frame" << std::endl;
39 }
40 
41 // TODO(b/135918744): extract to a new library
tryOpen(const std::string & busname)42 static sp<ICanBus> tryOpen(const std::string& busname) {
43     auto bus = ICanBus::tryGetService(busname);
44     if (bus != nullptr) return bus;
45 
46     /* Fallback for interfaces not registered in manifest. For testing purposes only,
47      * one should not depend on this in production deployment. */
48     auto manager = hidl::manager::V1_2::IServiceManager::getService();
49     auto ret = manager->get(ICanBus::descriptor, busname).withDefault(nullptr);
50     if (ret == nullptr) return nullptr;
51 
52     std::cerr << "WARNING: bus " << busname << " is not registered in device manifest, "
53               << "trying to fetch it directly..." << std::endl;
54 
55     return ICanBus::castFrom(ret);
56 }
57 
cansend(const std::string & busname,const V1_0::CanMessage & msg)58 static int cansend(const std::string& busname, const V1_0::CanMessage& msg) {
59     auto bus = tryOpen(busname);
60     if (bus == nullptr) {
61         std::cerr << "Bus " << busname << " is not available" << std::endl;
62         return -1;
63     }
64 
65     const auto result = bus->send(msg);
66     if (result != Result::OK) {
67         std::cerr << "Send call failed: " << toString(result) << std::endl;
68         return -1;
69     }
70     return 0;
71 }
72 
parseCanMessage(const std::string & msg)73 static std::optional<V1_0::CanMessage> parseCanMessage(const std::string& msg) {
74     const auto hashpos = msg.find("#");
75     if (hashpos == std::string::npos) return std::nullopt;
76 
77     const std::string msgidStr = msg.substr(0, hashpos);
78     const std::string payloadStr = msg.substr(hashpos + 1);
79 
80     V1_0::CanMessageId msgid;
81     // "0x" must be prepended to msgidStr, since ParseUint doesn't accept a base argument.
82     if (!android::base::ParseUint("0x" + msgidStr, &msgid)) return std::nullopt;
83 
84     V1_0::CanMessage canmsg = {};
85     canmsg.id = msgid;
86     if (msgid > 0x7FF) {
87         canmsg.isExtendedId = true;
88     }
89 
90     if (android::base::StartsWith(payloadStr, "R")) {
91         canmsg.remoteTransmissionRequest = true;
92 
93         /* The CAN bus HAL doesn't define a data length code (DLC) field, since it is inferrred
94          * from the payload size. RTR messages indicate to the receiver how many bytes they are
95          * expecting to receive back via the DLC sent with the RTR frame. */
96         if (payloadStr.size() <= 1) return canmsg;
97 
98         unsigned int dlc = 0;
99 
100         /* The maximum DLC for CAN-FD is 64 bytes and CAN 2.0 is 8 bytes. Limit the size of the DLC
101          * to something memory safe and let the HAL determine if the DLC is valid. */
102         if (!android::base::ParseUint(payloadStr.substr(1), &dlc, 10000u)) {
103             std::cerr << "Invalid DLC for RTR frame!" << std::endl;
104             return std::nullopt;
105         }
106         canmsg.payload.resize(dlc);
107         return canmsg;
108     }
109 
110     std::vector<uint8_t> payload;
111     if (payloadStr.size() % 2 != 0) return std::nullopt;
112     for (size_t i = 0; i < payloadStr.size(); i += 2) {
113         std::string byteStr(payloadStr, i, 2);
114         uint8_t byteBuf;
115         if (!android::base::ParseUint("0x" + byteStr, &byteBuf)) return std::nullopt;
116         payload.emplace_back(byteBuf);
117     }
118     canmsg.payload = payload;
119 
120     return canmsg;
121 }
122 
main(int argc,char * argv[])123 static int main(int argc, char* argv[]) {
124     base::SetDefaultTag("CanHalSend");
125     base::SetMinimumLogSeverity(android::base::VERBOSE);
126 
127     if (argc == 0) {
128         usage();
129         return 0;
130     }
131 
132     if (argc != 2) {
133         std::cerr << "Invalid number of arguments" << std::endl;
134         usage();
135         return -1;
136     }
137 
138     std::string busname(argv[0]);
139     const auto canmsg = parseCanMessage(argv[1]);
140     if (!canmsg) {
141         std::cerr << "Failed to parse CAN message argument" << std::endl;
142         return -1;
143     }
144 
145     return cansend(busname, *canmsg);
146 }
147 
148 }  // namespace android::hardware::automotive::can
149 
main(int argc,char * argv[])150 int main(int argc, char* argv[]) {
151     if (argc < 1) return -1;
152     return ::android::hardware::automotive::can::main(--argc, ++argv);
153 }
154