blob: 18e815dd6e0eaa82077885063117d9cbb07e55d8 [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>
chrisweir62bbf3d2020-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() {
chrisweir62bbf3d2020-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;
chrisweir62bbf3d2020-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
chrisweir62bbf3d2020-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
chrisweir62bbf3d2020-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
80 size_t idx = 0;
81 V1_0::CanMessageId msgid = std::stoi(msgidStr, &idx, 16);
82 if (msgidStr[idx] != '\0') return std::nullopt;
83
chrisweir62bbf3d2020-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);
114 payload.emplace_back(std::stoi(byteStr, &idx, 16));
115 if (byteStr[idx] != '\0') return std::nullopt;
116 }
chrisweir62bbf3d2020-02-25 15:15:40 -0800117 canmsg.payload = payload;
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700118
chrisweir62bbf3d2020-02-25 15:15:40 -0800119 return canmsg;
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700120}
121
122static int main(int argc, char* argv[]) {
123 base::SetDefaultTag("CanHalSend");
124 base::SetMinimumLogSeverity(android::base::VERBOSE);
125
126 if (argc == 0) {
127 usage();
128 return 0;
129 }
130
131 if (argc != 2) {
132 std::cerr << "Invalid number of arguments" << std::endl;
133 usage();
134 return -1;
135 }
136
137 std::string busname(argv[0]);
138 const auto canmsg = parseCanMessage(argv[1]);
139 if (!canmsg) {
140 std::cerr << "Failed to parse CAN message argument" << std::endl;
141 return -1;
142 }
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700143
chrisweir62bbf3d2020-02-25 15:15:40 -0800144 return cansend(busname, *canmsg);
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700145}
146
Tomasz Wasilczyk55f21932019-12-20 09:20:24 -0800147} // namespace android::hardware::automotive::can
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -0700148
149int main(int argc, char* argv[]) {
150 if (argc < 1) return -1;
151 return ::android::hardware::automotive::can::main(--argc, ++argv);
152}