| 1 | /* |
| 2 | RailControl - Model Railway Control Software |
| 3 | |
| 4 | Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org |
| 5 | |
| 6 | RailControl is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 3, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | RailControl is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | See the GNU General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with RailControl; see the file LICENCE. If not see |
| 18 | <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <cstdio> //printf |
| 22 | #include <cstdlib> //exit(0); |
| 23 | #include <cstring> //memset |
| 24 | #include <iostream> |
| 25 | #include <signal.h> |
| 26 | #include <sstream> |
| 27 | #include <unistd.h> //close; |
| 28 | #include <vector> |
| 29 | |
| 30 | #include "Logger/Logger.h" |
| 31 | #include "Network/Serial.h" |
| 32 | |
| 33 | static volatile unsigned int runSniffer; |
| 34 | |
| 35 | void stopSignal(int signo) |
| 36 | { |
| 37 | runSniffer = false; |
| 38 | } |
| 39 | |
| 40 | int main (int argc, char* argv[]) |
| 41 | { |
| 42 | signal(SIGINT, stopSignal); |
| 43 | signal(SIGTERM, stopSignal); |
| 44 | |
| 45 | runSniffer = true; |
| 46 | Logger::Logger* logger = Logger::Logger::GetLogger("CAN bus sniffer"); |
| 47 | logger->AddConsoleLogger(); |
| 48 | logger->SetLogLevel(Logger::Logger::LevelDebug); |
| 49 | |
| 50 | logger->Debug("Starting CAN bus sniffer"); |
| 51 | |
| 52 | Network::Serial serial(logger, "/dev/ttyUSB0", B500000, 8, 'N', 1); |
| 53 | |
| 54 | do |
| 55 | { |
| 56 | unsigned char buffer[13]; |
| 57 | int ret = serial.ReceiveExact(buffer, sizeof(buffer)); |
| 58 | if (ret == 0) |
| 59 | { |
| 60 | // do nothing |
| 61 | } |
| 62 | else if (ret != sizeof(buffer)) |
| 63 | { |
| 64 | logger->Debug("Received != 13 bytes"); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | logger->HexIn(buffer, sizeof(buffer)); |
| 69 | } |
| 70 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 71 | } while (runSniffer); |
| 72 | |
| 73 | logger->Debug("Terminating CAN bus sniffer"); |
| 74 | return 0; |
| 75 | } |
| 76 |