CARMA C++
JanzCanIo.h
Go to the documentation of this file.
1 
10 #ifndef CARMA_CANBUS_JANZCANIO_H
11 #define CARMA_CANBUS_JANZCANIO_H
12 
13 #include "carma/canbus/CanIo.h"
14 #include "carma/canbus/Message.h"
15 
16 #include <pthread.h>
17 #include <queue>
18 #include <string>
19 
20 namespace carma {
21 
22 namespace util {
23  template <class ElementType> class IPQreader;
24  template <class ElementType> class IPQwriter;
25 }
26 
27 namespace canbus {
28 
29  class JanzMessage;
30 
79  class JanzCanIo : public CanIo {
80  public:
81 
82  // Constructors...
83 
99  JanzCanIo ();
100 
117  JanzCanIo (const char * deviceName, bool terminate);
118 
137  JanzCanIo (const char* dev0, bool term0, const char* dev1, bool term1);
138 
139  typedef
140  ::std::vector< ::std::pair< ::std::string, bool > > NameTermPairVec;
141 
147  JanzCanIo ( const NameTermPairVec & nameTermPairs );
148 
152  virtual ~JanzCanIo ();
153 
154  // Input methods.
155 
171 
193  void postMessage(
194  const carma::canbus::Message &msg,
195  carma::canbus::txPriorityType prio = carma::canbus::NORMAL);
196 
207  void echoAll(bool enable);
208 
213  BusStatusMap getBusStatus() const;
214 
215  // Utilities...
216 
226  void clearReadQueue();
227 
236  void queueMessage(const Message &msg);
237 
244  void setTimestampEchoLatency(int tsLatency, busIdType busId);
245 
246  protected:
247 
248  private:
249 
250 
251  // Local structure typedefs for busses, devices and queue
252  // information...
253  // A bus is a single physical CAN bus and contains information
254  // pertinent to the bus and CAN messages. The device is mutex
255  // protected to synchronize access to it.
256 
261  typedef struct {
262  bool terminate;
263  int rxCount;
264  int txCount;
265  int oneMinRxCount;
266  int oneMinTxCount;
267  busIdType id;
268  timespec lastUpdateTime;
269  timespec lastOneMinUpdate;
271  } busType;
272 
279  typedef struct {
280  std::string name;
281  int fd;
282  bool initialized;
283  pthread_mutex_t mutex;
284  pthread_t threadId;
285  void * arg;
286  busType bus;
287  } deviceType;
288 
294  typedef struct queueAccessStruct {
295  pthread_mutex_t mutex;
296  pthread_cond_t cond;
297  std::queue<carma::canbus::Message> messageQueue;
298  } queueAccessType;
299 
300  // Private member data
301 
302  pthread_t updateThreadId_; // Thread Id for update thread
303  pthread_t writeThreadId_; // Thread Id for write thread
304  queueAccessType rxQueueAccessor_; // Received CAN message queue
305  // Direct CAN IPQs.
308 
309  typedef std::map<busIdType, deviceType*> DeviceMap;
310 
312  DeviceMap devices_;
313  const bool emulate_;
314  bool echo_; // Echo messages posted to canbus?
315 
316  // CanOutput routines
317 
328  void addDevice(busIdType busId);
329 
350  virtual void canFastSend(busIdType busId, const JanzMessage &jmsg,
351  txPriorityType prio);
352 
353  // Initialization and startup routines...
354 
362  void initialize();
363 
370  void initializeIpqs();
371 
375  void createThreads();
376 
377  // Utility routines...
378 
379  // The following two methods are protected to avoid cluttering the
380  // interface. They are implemented in the getMessage variants.
381 
387  bool waitForMessage(double maxWaitInSeconds = -1.0);
388 
392  static void waitCleanupHandler(void *arg);
393 
401  void processFastMessage(deviceType &dev);
402 
409  void processPlainMessage(deviceType &dev);
410 
416  JanzCanIo (const JanzCanIo &);
417 
423  JanzCanIo &operator=(const JanzCanIo &);
424 
425  // Utilities...
426 
434  void updateBusStatus();
435 
436  // Thread based utilities...
437 
446  static void *readThreadEntry(void *arg);
447 
455  void runReadThread(deviceType &dev);
456 
461  static void *writeThreadEntry(void *arg);
462 
470  void runWriteThread();
471 
475  static void *updateThreadEntry(void *arg);
476 
480  void runUpdateThread();
481 
482  };
483  } // namespace canbus
484 } // namespace carma
485 #endif // CARMA_CANBUS_JANZCANIO_H
void queueMessage(const Message &msg)
Place a carma::canbus::Message in the shared msg queue.
Declaration of carma::canbus::Message class.
Class to encapsulate a CAN message.
Definition: Message.h:21
JanzCanIo class.
Definition: JanzCanIo.h:79
void echoAll(bool enable)
Echo all messages posted to the canbus back through the read interface.
carma::canbus::Message getMessage()
Retrieve a CAN message.
void setTimestampEchoLatency(int tsLatency, busIdType busId)
Set the timestamp echo latency for the specified bus.
Declaration of carma::canbus::CanIo interface.
IPQ (InterProcessQueue) provides a generic way for information to be shared between processes or thre...
Definition: JanzCanIo.h:24
std::map< busIdType, busStatusType > BusStatusMap
Map to hold bus status for multiple busses.
Definition: Types.h:153
BusStatusMap getBusStatus() const
Get bus status for all busses.
IPQ (InterProcessQueue) provides a generic way for information to be shared between processes or thre...
Definition: JanzCanIo.h:23
void postMessage(const carma::canbus::Message &msg, carma::canbus::txPriorityType prio=carma::canbus::NORMAL)
Post a CAN message.
void clearReadQueue()
Clear the read message queue.
JanzCanIo()
Default constructor - for emulation only.
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
This class encapsulate a Janz CAN message.
Definition: JanzMessage.h:56
CanIo interface.
Definition: CanIo.h:22
virtual ~JanzCanIo()
Destructor for JanzCanIo object.