Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 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 | |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 23 | #include <linux/can.h> |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <iomanip> |
| 26 | #include <iostream> |
| 27 | #include <string> |
| 28 | #include <thread> |
| 29 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 30 | namespace android::hardware::automotive::can { |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 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 | |
| 40 | CanMessageListener(std::string name) : name(name) {} |
| 41 | |
| 42 | virtual Return<void> onReceive(const V1_0::CanMessage& message) { |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 43 | int msgIdWidth = 3; |
| 44 | if (message.isExtendedId) msgIdWidth = 8; |
| 45 | std::cout << " " << name << " " << std::hex << std::uppercase << std::setw(msgIdWidth) |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 46 | << std::setfill('0') << message.id << std::setw(0); |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 47 | std::cout << " [" << message.payload.size() << "] "; |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 48 | if (message.remoteTransmissionRequest) { |
chrisweir | bee3d2c | 2019-11-19 10:23:37 -0800 | [diff] [blame] | 49 | std::cout << "remote request"; |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 50 | } else { |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 51 | for (const auto byte : message.payload) { |
chrisweir | cf36cea | 2019-11-08 16:41:02 -0800 | [diff] [blame] | 52 | std::cout << " " << std::setfill('0') << std::setw(2) << unsigned(byte); |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 53 | } |
| 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 | |
| 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 |
| 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 | |
| 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 | |
| 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 | |
Tomasz Wasilczyk | 55f2193 | 2019-12-20 09:20:24 -0800 | [diff] [blame] | 128 | } // namespace android::hardware::automotive::can |
Tomasz Wasilczyk | 1b2c6ef | 2019-07-19 13:57:42 -0700 | [diff] [blame] | 129 | |
| 130 | int main(int argc, char* argv[]) { |
| 131 | if (argc < 1) return -1; |
| 132 | return ::android::hardware::automotive::can::main(--argc, ++argv); |
| 133 | } |