blob: 29330c9c756d793d47e2360e0c5863acf781d663 [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/ICanBus.h>
19#include <android/hidl/manager/1.2/IServiceManager.h>
20
21#include <iostream>
22#include <string>
23
24namespace android {
25namespace hardware {
26namespace automotive {
27namespace can {
28
29using ICanBus = V1_0::ICanBus;
30using Result = V1_0::Result;
31
32static void usage() {
33 std::cerr << "cansend - simple command line tool to send raw CAN frames" << std::endl;
34 std::cerr << std::endl << "usage:" << std::endl << std::endl;
35 std::cerr << "canhalsend <bus name> <can id>#<data>" << std::endl;
36 std::cerr << "where:" << std::endl;
37 std::cerr << " bus name - name under which ICanBus is be published" << std::endl;
38 std::cerr << " can id - such as 1a5" << std::endl;
39 std::cerr << " data - such as deadbeef or 010203" << std::endl;
40}
41
42// TODO(b/135918744): extract to a new library
43static sp<ICanBus> tryOpen(const std::string& busname) {
44 auto bus = ICanBus::tryGetService(busname);
45 if (bus != nullptr) return bus;
46
47 /* Fallback for interfaces not registered in manifest. For testing purposes only,
48 * one should not depend on this in production deployment. */
49 auto manager = hidl::manager::V1_2::IServiceManager::getService();
50 auto ret = manager->get(ICanBus::descriptor, busname).withDefault(nullptr);
51 if (ret == nullptr) return nullptr;
52
53 std::cerr << "WARNING: bus " << busname << " is not registered in device manifest, "
54 << "trying to fetch it directly..." << std::endl;
55
56 return ICanBus::castFrom(ret);
57}
58
59static int cansend(const std::string& busname, V1_0::CanMessageId msgid,
60 std::vector<uint8_t> payload) {
61 auto bus = tryOpen(busname);
62 if (bus == nullptr) {
63 std::cerr << "Bus " << busname << " is not available" << std::endl;
64 return -1;
65 }
66
67 V1_0::CanMessage msg = {};
68 msg.id = msgid;
69 msg.payload = payload;
70
71 const auto result = bus->send(msg);
72 if (result != Result::OK) {
73 std::cerr << "Send call failed: " << toString(result) << std::endl;
74 return -1;
75 }
76 return 0;
77}
78
79static std::optional<std::tuple<V1_0::CanMessageId, std::vector<uint8_t>>> parseCanMessage(
80 const std::string& msg) {
81 const auto hashpos = msg.find("#");
82 if (hashpos == std::string::npos) return std::nullopt;
83
84 const std::string msgidStr = msg.substr(0, hashpos);
85 const std::string payloadStr = msg.substr(hashpos + 1);
86
87 size_t idx = 0;
88 V1_0::CanMessageId msgid = std::stoi(msgidStr, &idx, 16);
89 if (msgidStr[idx] != '\0') return std::nullopt;
90
91 std::vector<uint8_t> payload;
92 if (payloadStr.size() % 2 != 0) return std::nullopt;
93 for (size_t i = 0; i < payloadStr.size(); i += 2) {
94 std::string byteStr(payloadStr, i, 2);
95 payload.emplace_back(std::stoi(byteStr, &idx, 16));
96 if (byteStr[idx] != '\0') return std::nullopt;
97 }
98
99 return {{msgid, payload}};
100}
101
102static int main(int argc, char* argv[]) {
103 base::SetDefaultTag("CanHalSend");
104 base::SetMinimumLogSeverity(android::base::VERBOSE);
105
106 if (argc == 0) {
107 usage();
108 return 0;
109 }
110
111 if (argc != 2) {
112 std::cerr << "Invalid number of arguments" << std::endl;
113 usage();
114 return -1;
115 }
116
117 std::string busname(argv[0]);
118 const auto canmsg = parseCanMessage(argv[1]);
119 if (!canmsg) {
120 std::cerr << "Failed to parse CAN message argument" << std::endl;
121 return -1;
122 }
123 const auto [msgid, payload] = *canmsg;
124
125 return cansend(busname, msgid, payload);
126}
127
128} // namespace can
129} // namespace automotive
130} // namespace hardware
131} // namespace android
132
133int main(int argc, char* argv[]) {
134 if (argc < 1) return -1;
135 return ::android::hardware::automotive::can::main(--argc, ++argv);
136}