MIRA
CANChannelInterface.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_CANCHANNELINTERFACE_H_
44 #define _MLAB_CANCHANNELINTERFACE_H_
45 
46 #include <utils/Foreach.h>
47 
48 #include <fw/Framework.h>
49 
50 #include <can/CANInterface.h>
51 
52 namespace mira { namespace can {
53 
55 
60 {
61 public:
64 
67  mAuthority(authority.getNamespace(),
68  authority.getName()+"_CANInterface", Authority::ANONYMOUS | Authority::HIDDEN),
69  mLastTime(Time::now())
70  {
71  mOutChannel = mAuthority.publish<CANMessage>("OutgoingCAN");
72  mInChannel = mAuthority.subscribe<CANMessage>("IncomingCAN",
73  &CANChannelInterface::onIncoming,
74  this,
75  Duration::seconds(10));
76  }
77 
79 
80 public:
83 
84  virtual void sendMessage(const CANMessage& msg)
85  {
86  // We must use a mutex here, since this function might be called
87  // from multiple threads.
88  boost::mutex::scoped_lock lock(mOutgoingChannelMutex);
89 
90  auto w = mOutChannel.write();
91  w->value() = msg;
92  Time ts = w->timestamp;
93  w.finish();
94  onMessageSend(msg, ts);
95  }
96 
98 
99 private:
100 
101  void onIncoming(ChannelRead<CANMessage> message)
102  {
104  mInChannel.readInterval(mLastTime, message->timestamp);
105  mLastTime = message->timestamp;
106  foreach(const Stamped<CANMessage>& m, interval)
108  }
109 
110 private:
111  Authority mAuthority;
112  Time mLastTime;
113  Channel<CANMessage> mOutChannel;
114  Channel<CANMessage> mInChannel;
115 
116  boost::mutex mOutgoingChannelMutex;
117 };
118 
120 typedef boost::shared_ptr<CANChannelInterface> CANChannelInterfacePtr;
121 
123 
124 }} // namespaces
125 
126 #endif
Abstract class for a hardware specific CAN driver.
An interface for providing CAN messages with the MIRA framework.
Definition: CANChannelInterface.h:59
Channel< T > publish(const std::string &channelID)
virtual void sendMessage(const CANMessage &msg)
Send a new message over the CAN bus.
Definition: CANChannelInterface.h:84
sec_type seconds() const
void onMessageReceived(const CANMessage &message, const Time &time)
A general interface for a CAN driver.
Definition: CANInterface.h:83
boost::shared_ptr< CANChannelInterface > CANChannelInterfacePtr
A shared pointer for CANChannelInterface.
Definition: CANChannelInterface.h:120
void onMessageSend(const CANMessage &message, const Time &time)
CANChannelInterface(Authority &authority)
The constructor.
Definition: CANChannelInterface.h:66
A definition of a CAN message.
Definition: CANDefs.h:105
Channel< T > subscribe(const std::string &channelID, const Duration &storageDuration=Duration::seconds(0))