MIRA
CANInterface.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 MetraLabs GmbH (MLAB), GERMANY.
3  * All rights reserved.
4  * Contact: info@MetraLabs.com
5  *
6  * Commercial Usage:
7  * Licensees holding valid commercial licenses may use this file in
8  * accordance with the commercial license agreement provided with the
9  * software or, alternatively, in accordance with the terms contained in
10  * a written agreement between you and MetraLabs.
11  *
12  * GNU General Public License Usage:
13  * Alternatively, this file may be used under the terms of the GNU
14  * General Public License version 3.0 as published by the Free Software
15  * Foundation and appearing in the file LICENSE.GPL3 included in the
16  * packaging of this file. Please review the following information to
17  * ensure the GNU General Public License version 3.0 requirements will be
18  * met: http://www.gnu.org/copyleft/gpl.html.
19  * Alternatively you may (at your option) use any later version of the GNU
20  * General Public License if such license has been publicly approved by
21  * MetraLabs (or its successors, if any).
22  *
23  * IN NO EVENT SHALL "MLAB" BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
24  * SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE
25  * OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLABS" HAS BEEN
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * "MLAB" SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
31  * "AS IS" BASIS, AND "MLAB" HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
32  * SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
33  */
34 
43 #ifndef _MLAB_CANINTERFACE_H_
44 #define _MLAB_CANINTERFACE_H_
45 
46 #include <queue>
47 #include <vector>
48 
49 #ifndef Q_MOC_RUN
50 #include <boost/shared_ptr.hpp>
51 #include <boost/thread.hpp>
52 #endif
53 
54 #include <platform/Types.h>
55 #include <utils/Time.h>
56 
57 #include <can/CANDefs.h>
58 #include <can/CANDriver.h>
59 
60 namespace mira { namespace can {
61 
63 
64 // Forward declaration
65 class CANOpenPDOListener;
66 
68 
70 typedef boost::function<void (const CANMessage&, const Time&)> CANMessageCallback;
71 
73 typedef boost::function<void (const CANMessage&, const Time&)> PDOCallback;
74 
76 
84 {
85 public:
88 
90  CANInterface();
91 
93  virtual ~CANInterface() {};
94 
96 
97 public:
100 
106  virtual void sendMessage(const CANMessage& msg) = 0;
107 
120  uint32 addMessageSentListener(CANMessageCallback pFunc,
121  uint32 pMask = 0x00000000,
122  uint32 pFilter = 0xFFFFFFFF);
123 
128  void removeMessageSentListener(uint32 pFuncID);
129 
131 
132 public:
135 
148  uint32 addMessageRecvListener(CANMessageCallback pFunc,
149  uint32 pMask = 0x00000000,
150  uint32 pFilter = 0xFFFFFFFF);
151 
156  void removeMessageRecvListener(uint32 pFuncID);
157 
159 
160 public:
163 
170  uint32 addPDOListener(PDOCallback pFunc, uint32 pPDO);
171 
176  void removePDOListener(uint32 pFuncID);
177 
179 
180 protected:
181  void onMessageSend(const CANMessage& message, const Time& time);
182  void onMessageReceived(const CANMessage& message, const Time& time);
183 
184 private:
185 
186  struct MsgListener {
187  uint32 id;
188  CANMessageCallback func;
189  uint32 mask;
190  uint32 filter;
191  };
192 
193  struct PDOListener {
194  uint32 id;
195  uint32 pdo;
196  PDOCallback func;
197 
198  };
199 
200 private:
201 
202  // outgoing message listeners
203  boost::mutex mOutgoingListenersMutex;
204  uint32 mOutgoingListenersNextID;
205  std::vector<MsgListener> mOutgoingListeners;
206 
207  // incoming message listeners
208  boost::mutex mIncomingListenersMutex;
209  uint32 mIncomingListenersNextID;
210  std::vector<MsgListener> mIncomingListeners;
211 
212  // PDO callbacks
213  boost::mutex mPDOCallbacksMutex;
214  uint32 mPDOCallbacksNextID;
215  std::vector<PDOListener> mPDOCallbacks;
216 
217  // A CANopen PDO listener
218  CANOpenPDOListener* mPDOListener;
219 };
220 
222 
224 typedef boost::shared_ptr<CANInterface> CANInterfacePtr;
225 
227 
228 }} // namespaces
229 
230 #endif
virtual ~CANInterface()
The destructor.
Definition: CANInterface.h:93
boost::shared_ptr< CANInterface > CANInterfacePtr
A shared pointer of the CANInterface.
Definition: CANInterface.h:224
boost::function< void(const CANMessage &, const Time &)> PDOCallback
A PDO callback.
Definition: CANInterface.h:73
#define MLAB_CAN_EXPORT
Definition: CANExports.h:57
A general interface for a CAN driver.
Definition: CANInterface.h:83
boost::function< void(const CANMessage &, const Time &)> CANMessageCallback
A callback for CAN message.
Definition: CANInterface.h:65
Base class for all CAN-Bus drivers.
A definition of a CAN message.
Definition: CANDefs.h:105
Common CANbus definitions.