1 /*
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
23 #include <linux/can.h>
24 #include <chrono>
25 #include <iomanip>
26 #include <iostream>
27 #include <string>
28 #include <thread>
29
30 namespace android::hardware::automotive::can {
31
32 using namespace std::chrono_literals;
33
34 using ICanBus = V1_0::ICanBus;
35 using Result = V1_0::Result;
36
37 struct CanMessageListener : public V1_0::ICanMessageListener {
38 const std::string name;
39
CanMessageListenerandroid::hardware::automotive::can::CanMessageListener40 CanMessageListener(std::string name) : name(name) {}
41
onReceiveandroid::hardware::automotive::can::CanMessageListener42 virtual Return<void> onReceive(const V1_0::CanMessage& message) {
43 int msgIdWidth = 3;
44 if (message.isExtendedId) msgIdWidth = 8;
45 std::cout << " " << name << " " << std::hex << std::uppercase << std::setw(msgIdWidth)
46 << std::setfill('0') << message.id << std::setw(0);
47 std::cout << " [" << message.payload.size() << "] ";
48 if (message.remoteTransmissionRequest) {
49 std::cout << "remote request";
50 } else {
51 for (const auto byte : message.payload) {
52 std::cout << " " << std::setfill('0') << std::setw(2) << unsigned(byte);
53 }
54 }
55 std::cout << std::nouppercase << std::dec << std::endl;
56 return {};
57 }
58
onErrorandroid::hardware::automotive::can::CanMessageListener59 virtual Return<void> onError(V1_0::ErrorEvent error) {
60 std::cout << " " << name << " " << toString(error) << std::endl;
61 return {};
62 }
63 };
64
usage()65 static 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
tryOpen(const std::string & busname)74 static 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
candump(const std::string & busname)90 static 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
main(int argc,char * argv[])110 static 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
128 } // namespace android::hardware::automotive::can
129
main(int argc,char * argv[])130 int main(int argc, char* argv[]) {
131 if (argc < 1) return -1;
132 return ::android::hardware::automotive::can::main(--argc, ++argv);
133 }
134