candump.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 #include <unordered_set>
4 
5 #include <boost/exception/diagnostic_information.hpp>
8 
9 using namespace can;
10 
11 #include <iostream>
12 
13 void print_frame(const Frame &f){
14 
15  if(f.is_error){
16  std::cout << "E " << std::hex << f.id << std::dec;
17  }else if(f.is_extended){
18  std::cout << "e " << std::hex << f.id << std::dec;
19  }else{
20  std::cout << "s " << std::hex << f.id << std::dec;
21  }
22 
23  std::cout << "\t";
24 
25  if(f.is_rtr){
26  std::cout << "r";
27  }else{
28  std::cout << (int) f.dlc << std::hex;
29 
30  for(int i=0; i < f.dlc; ++i){
31  std::cout << std::hex << " " << (int) f.data[i];
32  }
33  }
34 
35  std::cout << std::dec << std::endl;
36 }
37 
38 
39 std::shared_ptr<class_loader::ClassLoader> g_loader;
41 
42 void print_state(const State & s){
43  std::string err;
44  g_driver->translateError(s.internal_error,err);
45  std::cout << "STATE: driver_state=" << s.driver_state << " internal_error=" << s.internal_error << "('" << err << "') asio: " << s.error_code << std::endl;
46 }
47 
48 
49 int main(int argc, char *argv[]){
50 
51  if(argc != 2 && argc != 4){
52  std::cout << "usage: "<< argv[0] << " DEVICE [PLUGIN_PATH PLUGIN_NAME]" << std::endl;
53  return 1;
54  }
55 
56  if(argc == 4 ){
57  try
58  {
59  g_loader = std::make_shared<class_loader::ClassLoader>(argv[2]);
60  g_driver = g_loader->createUniqueInstance<DriverInterface>(argv[3]);
61  }
62 
63  catch(std::exception& ex)
64  {
65  std::cerr << boost::diagnostic_information(ex) << std::endl;;
66  return 1;
67  }
68  }else{
69  g_driver = std::make_shared<SocketCANInterface>();
70  }
71 
72 
73 
74  FrameListenerConstSharedPtr frame_printer = g_driver->createMsgListener(print_frame);
75  StateListenerConstSharedPtr error_printer = g_driver->createStateListener(print_state);
76 
77  if(!g_driver->init(argv[1], false, can::NoSettings::create())){
78  print_state(g_driver->getState());
79  return 1;
80  }
81 
82  g_driver->run();
83 
84  g_driver->shutdown();
85  g_driver.reset();
86  g_loader.reset();
87 
88  return 0;
89 
90 }
can::NoSettings::create
static SettingsConstSharedPtr create()
Definition: settings.h:36
can::StateListenerConstSharedPtr
StateInterface::StateListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:133
can::Frame
Definition: interface.h:62
g_loader
std::shared_ptr< class_loader::ClassLoader > g_loader
Definition: candump.cpp:39
can::Header::is_extended
unsigned int is_extended
frame uses 29 bit CAN identifier
Definition: interface.h:26
can::Frame::dlc
unsigned char dlc
len of data
Definition: interface.h:65
print_frame
void print_frame(const Frame &f)
Definition: candump.cpp:13
can::DriverInterfaceSharedPtr
std::shared_ptr< DriverInterface > DriverInterfaceSharedPtr
Definition: interface.h:225
can::Header::is_rtr
unsigned int is_rtr
frame is a remote transfer request
Definition: interface.h:25
main
int main(int argc, char *argv[])
Definition: candump.cpp:49
can::Header::id
unsigned int id
CAN ID (11 or 29 bits valid, depending on is_extended member.
Definition: interface.h:23
can::State::error_code
boost::system::error_code error_code
device access error
Definition: interface.h:91
can::Frame::data
std::array< value_type, 8 > data
array for 8 data bytes with bounds checking
Definition: interface.h:64
class_loader.hpp
can::DriverInterface
Definition: interface.h:178
can
Definition: asio_base.h:11
can::FrameListenerConstSharedPtr
CommInterface::FrameListenerConstSharedPtr FrameListenerConstSharedPtr
Definition: interface.h:176
can::Header::is_error
unsigned int is_error
marks an error frame (only used internally)
Definition: interface.h:24
can::State
Definition: interface.h:86
print_state
void print_state(const State &s)
Definition: candump.cpp:42
g_driver
DriverInterfaceSharedPtr g_driver
Definition: candump.cpp:40
can::State::driver_state
enum can::State::DriverState driver_state
can::State::internal_error
unsigned int internal_error
driver specific error
Definition: interface.h:92
socketcan.h


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Wed Mar 2 2022 00:52:25