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