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