blob: f0f9d108756c3f20db3a085a226676bef3d14222 [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>
chrisweir30bd3dc2020-02-25 15:15:40 -080018#include <android-base/parseint.h>
19#include <android-base/strings.h>
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070020#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
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -080026namespace android::hardware::automotive::can {
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070027
28using ICanBus = V1_0::ICanBus;
29using Result = V1_0::Result;
30
31static void usage() {
chrisweir30bd3dc2020-02-25 15:15:40 -080032 std::cerr << "canhalsend - simple command line tool to send raw CAN frames" << std::endl;
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070033 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;
chrisweir30bd3dc2020-02-25 15:15:40 -080036 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;
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070039}
40
41// TODO(b/135918744): extract to a new library
42static 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
chrisweir30bd3dc2020-02-25 15:15:40 -080058static int cansend(const std::string& busname, const V1_0::CanMessage& msg) {
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070059 auto bus = tryOpen(busname);
60 if (bus == nullptr) {
61 std::cerr << "Bus " << busname << " is not available" << std::endl;
62 return -1;
63 }
64
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070065 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
chrisweir30bd3dc2020-02-25 15:15:40 -080073static std::optional<V1_0::CanMessage> parseCanMessage(const std::string& msg) {
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070074 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
chrisweir1173a722020-02-26 14:39:56 -080080 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;
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070083
chrisweir30bd3dc2020-02-25 15:15:40 -080084 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
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700110 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);
chrisweir1173a722020-02-26 14:39:56 -0800114 uint8_t byteBuf;
115 if (!android::base::ParseUint("0x" + byteStr, &byteBuf)) return std::nullopt;
116 payload.emplace_back(byteBuf);
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700117 }
chrisweir30bd3dc2020-02-25 15:15:40 -0800118 canmsg.payload = payload;
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700119
chrisweir30bd3dc2020-02-25 15:15:40 -0800120 return canmsg;
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700121}
122
123static 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 }
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700144
chrisweir30bd3dc2020-02-25 15:15:40 -0800145 return cansend(busname, *canmsg);
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700146}
147
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -0800148} // namespace android::hardware::automotive::can
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700149
150int main(int argc, char* argv[]) {
151 if (argc < 1) return -1;
152 return ::android::hardware::automotive::can::main(--argc, ++argv);
153}