blob: 55b2a347c7b1d83172643054ffb6bf7fc4af31dc [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/hardware/automotive/can/1.0/ICanMessageListener.h>
20#include <android/hidl/manager/1.2/IServiceManager.h>
21#include <hidl-utils/hidl-utils.h>
22
chrisweirbee3d2c2019-11-19 10:23:37 -080023#include <linux/can.h>
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070024#include <chrono>
25#include <iomanip>
26#include <iostream>
27#include <string>
28#include <thread>
29
30namespace android {
31namespace hardware {
32namespace automotive {
33namespace can {
34
35using namespace std::chrono_literals;
36
37using ICanBus = V1_0::ICanBus;
38using Result = V1_0::Result;
39
40struct CanMessageListener : public V1_0::ICanMessageListener {
41 const std::string name;
42
43 CanMessageListener(std::string name) : name(name) {}
44
45 virtual Return<void> onReceive(const V1_0::CanMessage& message) {
chrisweirbee3d2c2019-11-19 10:23:37 -080046 int msgIdWidth = 3;
47 if (message.isExtendedId) msgIdWidth = 8;
48 std::cout << " " << name << " " << std::hex << std::uppercase << std::setw(msgIdWidth)
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070049 << std::setfill('0') << message.id << std::setw(0);
chrisweirbee3d2c2019-11-19 10:23:37 -080050 std::cout << " [" << message.payload.size() << "] ";
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070051 if (message.remoteTransmissionRequest) {
chrisweirbee3d2c2019-11-19 10:23:37 -080052 std::cout << "remote request";
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070053 } else {
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070054 for (const auto byte : message.payload) {
chrisweircf36cea2019-11-08 16:41:02 -080055 std::cout << " " << std::setfill('0') << std::setw(2) << unsigned(byte);
Tomasz Wasilczyk1b2c6ef2019-07-19 13:57:42 -070056 }
57 }
58 std::cout << std::nouppercase << std::dec << std::endl;
59 return {};
60 }
61
62 virtual Return<void> onError(V1_0::ErrorEvent error) {
63 std::cout << " " << name << " " << toString(error) << std::endl;
64 return {};
65 }
66};
67
68static void usage() {
69 std::cerr << "canhaldump - dump CAN bus traffic" << std::endl;
70 std::cerr << std::endl << "usage:" << std::endl << std::endl;
71 std::cerr << "canhaldump <bus name>" << std::endl;
72 std::cerr << "where:" << std::endl;
73 std::cerr << " bus name - name under which ICanBus is be published" << std::endl;
74}
75
76// TODO(b/135918744): extract to a new library
77static sp<ICanBus> tryOpen(const std::string& busname) {
78 auto bus = ICanBus::tryGetService(busname);
79 if (bus != nullptr) return bus;
80
81 /* Fallback for interfaces not registered in manifest. For testing purposes only,
82 * one should not depend on this in production deployment. */
83 auto manager = hidl::manager::V1_2::IServiceManager::getService();
84 auto ret = manager->get(ICanBus::descriptor, busname).withDefault(nullptr);
85 if (ret == nullptr) return nullptr;
86
87 std::cerr << "WARNING: bus " << busname << " is not registered in device manifest, "
88 << "trying to fetch it directly..." << std::endl;
89
90 return ICanBus::castFrom(ret);
91}
92
93static int candump(const std::string& busname) {
94 auto bus = tryOpen(busname);
95 if (bus == nullptr) {
96 std::cerr << "Bus " << busname << " is not available" << std::endl;
97 return -1;
98 }
99
100 Result result;
101 sp<V1_0::ICloseHandle> chnd;
102 // TODO(b/135918744): extract to library
103 bus->listen({}, new CanMessageListener(busname), hidl_utils::fill(&result, &chnd)).assertOk();
104
105 if (result != Result::OK) {
106 std::cerr << "Listen call failed: " << toString(result) << std::endl;
107 return -1;
108 }
109
110 while (true) std::this_thread::sleep_for(1h);
111}
112
113static int main(int argc, char* argv[]) {
114 base::SetDefaultTag("CanHalDump");
115 base::SetMinimumLogSeverity(android::base::VERBOSE);
116
117 if (argc == 0) {
118 usage();
119 return 0;
120 }
121
122 if (argc != 1) {
123 std::cerr << "Invalid number of arguments" << std::endl;
124 usage();
125 return -1;
126 }
127
128 return candump(argv[0]);
129}
130
131} // namespace can
132} // namespace automotive
133} // namespace hardware
134} // namespace android
135
136int main(int argc, char* argv[]) {
137 if (argc < 1) return -1;
138 return ::android::hardware::automotive::can::main(--argc, ++argv);
139}