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


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Fri May 14 2021 02:59:39