Loading automotive/can/1.0/tools/canhalsend.cpp +37 −15 Original line number Diff line number Diff line Loading @@ -15,6 +15,8 @@ */ #include <android-base/logging.h> #include <android-base/parseint.h> #include <android-base/strings.h> #include <android/hardware/automotive/can/1.0/ICanBus.h> #include <android/hidl/manager/1.2/IServiceManager.h> Loading @@ -27,13 +29,13 @@ using ICanBus = V1_0::ICanBus; using Result = V1_0::Result; static void usage() { std::cerr << "cansend - simple command line tool to send raw CAN frames" << std::endl; std::cerr << "canhalsend - simple command line tool to send raw CAN frames" << std::endl; std::cerr << std::endl << "usage:" << std::endl << std::endl; std::cerr << "canhalsend <bus name> <can id>#<data>" << std::endl; std::cerr << "where:" << std::endl; std::cerr << " bus name - name under which ICanBus is be published" << std::endl; std::cerr << " can id - such as 1a5" << std::endl; std::cerr << " data - such as deadbeef or 010203" << std::endl; std::cerr << " bus name - name under which ICanBus is published" << std::endl; std::cerr << " can id - such as 1a5 or 1fab5982" << std::endl; std::cerr << " data - such as deadbeef, 010203, or R for a remote frame" << std::endl; } // TODO(b/135918744): extract to a new library Loading @@ -53,18 +55,13 @@ static sp<ICanBus> tryOpen(const std::string& busname) { return ICanBus::castFrom(ret); } static int cansend(const std::string& busname, V1_0::CanMessageId msgid, std::vector<uint8_t> payload) { static int cansend(const std::string& busname, const V1_0::CanMessage& msg) { auto bus = tryOpen(busname); if (bus == nullptr) { std::cerr << "Bus " << busname << " is not available" << std::endl; return -1; } V1_0::CanMessage msg = {}; msg.id = msgid; msg.payload = payload; const auto result = bus->send(msg); if (result != Result::OK) { std::cerr << "Send call failed: " << toString(result) << std::endl; Loading @@ -73,8 +70,7 @@ static int cansend(const std::string& busname, V1_0::CanMessageId msgid, return 0; } static std::optional<std::tuple<V1_0::CanMessageId, std::vector<uint8_t>>> parseCanMessage( const std::string& msg) { static std::optional<V1_0::CanMessage> parseCanMessage(const std::string& msg) { const auto hashpos = msg.find("#"); if (hashpos == std::string::npos) return std::nullopt; Loading @@ -85,6 +81,32 @@ static std::optional<std::tuple<V1_0::CanMessageId, std::vector<uint8_t>>> parse V1_0::CanMessageId msgid = std::stoi(msgidStr, &idx, 16); if (msgidStr[idx] != '\0') return std::nullopt; V1_0::CanMessage canmsg = {}; canmsg.id = msgid; if (msgid > 0x7FF) { canmsg.isExtendedId = true; } if (android::base::StartsWith(payloadStr, "R")) { canmsg.remoteTransmissionRequest = true; /* The CAN bus HAL doesn't define a data length code (DLC) field, since it is inferrred * from the payload size. RTR messages indicate to the receiver how many bytes they are * expecting to receive back via the DLC sent with the RTR frame. */ if (payloadStr.size() <= 1) return canmsg; unsigned int dlc = 0; /* The maximum DLC for CAN-FD is 64 bytes and CAN 2.0 is 8 bytes. Limit the size of the DLC * to something memory safe and let the HAL determine if the DLC is valid. */ if (!android::base::ParseUint(payloadStr.substr(1), &dlc, 10000u)) { std::cerr << "Invalid DLC for RTR frame!" << std::endl; return std::nullopt; } canmsg.payload.resize(dlc); return canmsg; } std::vector<uint8_t> payload; if (payloadStr.size() % 2 != 0) return std::nullopt; for (size_t i = 0; i < payloadStr.size(); i += 2) { Loading @@ -92,8 +114,9 @@ static std::optional<std::tuple<V1_0::CanMessageId, std::vector<uint8_t>>> parse payload.emplace_back(std::stoi(byteStr, &idx, 16)); if (byteStr[idx] != '\0') return std::nullopt; } canmsg.payload = payload; return {{msgid, payload}}; return canmsg; } static int main(int argc, char* argv[]) { Loading @@ -117,9 +140,8 @@ static int main(int argc, char* argv[]) { std::cerr << "Failed to parse CAN message argument" << std::endl; return -1; } const auto [msgid, payload] = *canmsg; return cansend(busname, msgid, payload); return cansend(busname, *canmsg); } } // namespace android::hardware::automotive::can Loading Loading
automotive/can/1.0/tools/canhalsend.cpp +37 −15 Original line number Diff line number Diff line Loading @@ -15,6 +15,8 @@ */ #include <android-base/logging.h> #include <android-base/parseint.h> #include <android-base/strings.h> #include <android/hardware/automotive/can/1.0/ICanBus.h> #include <android/hidl/manager/1.2/IServiceManager.h> Loading @@ -27,13 +29,13 @@ using ICanBus = V1_0::ICanBus; using Result = V1_0::Result; static void usage() { std::cerr << "cansend - simple command line tool to send raw CAN frames" << std::endl; std::cerr << "canhalsend - simple command line tool to send raw CAN frames" << std::endl; std::cerr << std::endl << "usage:" << std::endl << std::endl; std::cerr << "canhalsend <bus name> <can id>#<data>" << std::endl; std::cerr << "where:" << std::endl; std::cerr << " bus name - name under which ICanBus is be published" << std::endl; std::cerr << " can id - such as 1a5" << std::endl; std::cerr << " data - such as deadbeef or 010203" << std::endl; std::cerr << " bus name - name under which ICanBus is published" << std::endl; std::cerr << " can id - such as 1a5 or 1fab5982" << std::endl; std::cerr << " data - such as deadbeef, 010203, or R for a remote frame" << std::endl; } // TODO(b/135918744): extract to a new library Loading @@ -53,18 +55,13 @@ static sp<ICanBus> tryOpen(const std::string& busname) { return ICanBus::castFrom(ret); } static int cansend(const std::string& busname, V1_0::CanMessageId msgid, std::vector<uint8_t> payload) { static int cansend(const std::string& busname, const V1_0::CanMessage& msg) { auto bus = tryOpen(busname); if (bus == nullptr) { std::cerr << "Bus " << busname << " is not available" << std::endl; return -1; } V1_0::CanMessage msg = {}; msg.id = msgid; msg.payload = payload; const auto result = bus->send(msg); if (result != Result::OK) { std::cerr << "Send call failed: " << toString(result) << std::endl; Loading @@ -73,8 +70,7 @@ static int cansend(const std::string& busname, V1_0::CanMessageId msgid, return 0; } static std::optional<std::tuple<V1_0::CanMessageId, std::vector<uint8_t>>> parseCanMessage( const std::string& msg) { static std::optional<V1_0::CanMessage> parseCanMessage(const std::string& msg) { const auto hashpos = msg.find("#"); if (hashpos == std::string::npos) return std::nullopt; Loading @@ -85,6 +81,32 @@ static std::optional<std::tuple<V1_0::CanMessageId, std::vector<uint8_t>>> parse V1_0::CanMessageId msgid = std::stoi(msgidStr, &idx, 16); if (msgidStr[idx] != '\0') return std::nullopt; V1_0::CanMessage canmsg = {}; canmsg.id = msgid; if (msgid > 0x7FF) { canmsg.isExtendedId = true; } if (android::base::StartsWith(payloadStr, "R")) { canmsg.remoteTransmissionRequest = true; /* The CAN bus HAL doesn't define a data length code (DLC) field, since it is inferrred * from the payload size. RTR messages indicate to the receiver how many bytes they are * expecting to receive back via the DLC sent with the RTR frame. */ if (payloadStr.size() <= 1) return canmsg; unsigned int dlc = 0; /* The maximum DLC for CAN-FD is 64 bytes and CAN 2.0 is 8 bytes. Limit the size of the DLC * to something memory safe and let the HAL determine if the DLC is valid. */ if (!android::base::ParseUint(payloadStr.substr(1), &dlc, 10000u)) { std::cerr << "Invalid DLC for RTR frame!" << std::endl; return std::nullopt; } canmsg.payload.resize(dlc); return canmsg; } std::vector<uint8_t> payload; if (payloadStr.size() % 2 != 0) return std::nullopt; for (size_t i = 0; i < payloadStr.size(); i += 2) { Loading @@ -92,8 +114,9 @@ static std::optional<std::tuple<V1_0::CanMessageId, std::vector<uint8_t>>> parse payload.emplace_back(std::stoi(byteStr, &idx, 16)); if (byteStr[idx] != '\0') return std::nullopt; } canmsg.payload = payload; return {{msgid, payload}}; return canmsg; } static int main(int argc, char* argv[]) { Loading @@ -117,9 +140,8 @@ static int main(int argc, char* argv[]) { std::cerr << "Failed to parse CAN message argument" << std::endl; return -1; } const auto [msgid, payload] = *canmsg; return cansend(busname, msgid, payload); return cansend(busname, *canmsg); } } // namespace android::hardware::automotive::can Loading