CARMA C++
SocketCan.h
Go to the documentation of this file.
1 
9 #ifndef CARMA_CANBUS_SOCKETCAN_H
10 #define CARMA_CANBUS_SOCKETCAN_H
11 
12 #include "carma/canbus/CanIo.h"
13 #include "carma/canbus/Message.h"
14 #include "carma/canbus/Types.h"
15 
16 #include <boost/thread.hpp>
17 #include <boost/thread/mutex.hpp>
18 #include <vector>
19 
20 namespace carma {
21 namespace canbus {
22 
27 public:
28 
32  SocketCan( );
33 
38  explicit SocketCan( const ::std::vector< ::std::string > & canInterfaces );
39 
43  virtual ~SocketCan( );
44 
50 
54  void postMessage( const carma::canbus::Message & msg,
55  carma::canbus::txPriorityType p = carma::canbus::NORMAL );
56 
60  BusStatusMap getBusStatus() const;
61 
67  void echoAll( bool enable );
68 
72  static busIdType parseBusId( const std::string & canInterfaceName );
73 
74 protected:
75 
76  // No protected methods.
77 
78 private:
79 
80  void
81  commonConstruction( const ::std::vector< ::std::string > & canInterfaces );
82 
83  void updateThread( ); // Update bus statistics.
84 
85  struct BusInfo {
86  const ::std::string name;
87  const busIdType id;
88  const int wsfd;
89  int rxCount;
90  int txCount;
91  int oneMinRxCount;
92  int oneMinTxCount;
93  double lastUpdateMJD;
94  double lastOneMinUpdateMJD;
97  BusInfo( const ::std::string & name,
98  const busIdType id,
99  const int wsfd );
100 
101  void updateBusStatus( );
102 
103  };
104 
105  ::std::vector< int > fileDescriptors_;
106  int maxFileDescriptor_;
107 
108  typedef ::std::map< int, BusInfo > BusInfoMap; // Indexed by interface index
109  typedef ::std::map< int, int > BusIdToInterfaceMap;
110 
111  BusInfoMap busses_;
112  BusIdToInterfaceMap busIdToIf_;
113 
114  mutable boost::mutex mutex_;
115  boost::thread updateThread_;
116 
117 }; // class SocketCan
118 
119 } } // namespace carma::canbus
120 #endif
BusStatusMap getBusStatus() const
Retrieve bus statistics indexed by bus id.
Declaration of carma::canbus::Message class.
static busIdType parseBusId(const std::string &canInterfaceName)
Parse bus id given an interface name (e.g.
Declarations of carma::canbus types.
Class to encapsulate a CAN message.
Definition: Message.h:21
Declaration of carma::canbus::CanIo interface.
virtual ~SocketCan()
Destructor.
std::map< busIdType, busStatusType > BusStatusMap
Map to hold bus status for multiple busses.
Definition: Types.h:153
Implements the CanIo interface using the socketcan framework.
Definition: SocketCan.h:26
txPriorityType
CAN Tx priority type.
Definition: Types.h:194
unsigned short busIdType
Carma Bus Id type.
Definition: Types.h:70
Bus Status type.
Definition: Types.h:124
SocketCan()
Constructor for use with all busses on the system.
carma::canbus::Message getMessage()
Wait and retrieve a CAN message.
void postMessage(const carma::canbus::Message &msg, carma::canbus::txPriorityType p=carma::canbus::NORMAL)
Post a message to a single or multiple CAN busses.
CanIo interface.
Definition: CanIo.h:22
void echoAll(bool enable)
Echo all CAN messages.