libpandac  0.0.0
A library designed for a comm.ai Panda.
can.h
1 /*
2  Author: Matt Bunting
3  Copyright (c) 2020 Arizona Board of Regents
4  All rights reserved.
5 
6  Permission is hereby granted, without written agreement and without
7  license or royalty fees, to use, copy, modify, and distribute this
8  software and its documentation for any purpose, provided that the
9  above copyright notice and the following two paragraphs appear in
10  all copies of this software.
11 
12  IN NO EVENT SHALL THE ARIZONA BOARD OF REGENTS BE LIABLE TO ANY PARTY
13  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
16  SUCH DAMAGE.
17 
18  THE ARIZONA BOARD OF REGENTS SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER
21  IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION
22  TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 
24  */
25 
26 #ifndef PANDA_CAN_H
27 #define PANDA_CAN_H
28 
29 #include <fstream>
30 #include <list>
31 #include <vector>
32 
33 #include "mogi/thread.h"
34 
35 #include "panda/usb.h"
36 #include "panda/candata.h"
37 
38 namespace Panda {
39 
46  CanFrame bufferToCanFrame( char* buffer, int bufferLength);
47 
53  void canFrameToBuffer( CanFrame& frame, unsigned char* buffer);
54 
55  class Can;
56 
63  class CanListener {
64  private:
65  friend class Can;
66 
67  std::vector<int> blacklistBus;
68  std::vector<int> blacklistId;
69  protected:
76  virtual void newDataNotification(CanFrame* canFrame) = 0;
77 
78  void newDataNotificationProxy(CanFrame* canFrame);
79 
80  public:
81  virtual ~CanListener() { };
82 
86  void addToBlacklistBus( const int& busToBlock );
87 
88 
92  void addToBlacklistMessageId( const int& idToBlock );
93 
94  };
95 
103  class Can : protected Mogi::Thread, public UsbListener {
104  public:
105  Can();
106  ~Can();
107 
108  void initialize(); // needs USB device
109 
114  void saveToFile(const char* filename);
115 
120  void saveToCsvFile(const char* filename);
121 
125  void setUsb( Panda::Usb* usbHandler );
126 
130  void addObserver( CanListener* listener );
131 
135  void removeObserver( CanListener* listener );
136 
140  void startParsing();
141 
145  void stopParsing();
146 
150  void sendMessage( CanFrame& frame );
151 
152  private:
153  bool currentlyReceiving = false;
154 
155  std::list<CanFrame> canFrames;
156 
157  std::vector<CanListener*> listeners;
158  Usb* usbHandler = NULL;
159 
160  FILE* canDump;
161  FILE* csvDump;
162 
163  void writeCsvToFile(CanFrame* frame, unsigned char* buffer, int bufLength);
164  void writeRawToFile(char* buffer, size_t length);
165 
166  // Overload frum UsbListener
167  void notificationCanRead(char* buffer, size_t bufferLength);
168 
169  // Overload from Mogi::Thread
170  void doAction();
171  };
172 
173 }
174 
175 #endif
Handles USB communication and parsing with a comma.ai Panda.
Definition: can.h:38
An abstract class for new data notifications for new CAN data.
Definition: can.h:63
void canFrameToBuffer(CanFrame &frame, unsigned char *buffer)
Converts a CanFrame into a buffer for sending to the Panda.
void addToBlacklistBus(const int &busToBlock)
Adds a BUS number to the blacklist.
void addToBlacklistMessageId(const int &idToBlock)
Adds a message ID number to the blacklist.
Abstract class, handles a single thread. Features mutual exclusion and pause/resume.
Definition: thread.h:27
Definition: candata.h:38
CanFrame bufferToCanFrame(char *buffer, int bufferLength)
Converts a buffer from Panda to CanFrame data.
Definition: usb.h:80
Definition: usb.h:60
A class that handles the CAN data.
Definition: can.h:103
virtual void newDataNotification(CanFrame *canFrame)=0
Called on a successful RMC message parse Overload this to get instant updates form freshly parsed CAN...