μHAL (v2.7.9)
Part of the IPbus software repository
ProtocolIPbus.hpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30 ---------------------------------------------------------------------------
31 */
32 
39 #ifndef _uhal_ProtocolIPbus_hpp_
40 #define _uhal_ProtocolIPbus_hpp_
41 
42 
43 #include <deque>
44 #include <iosfwd>
45 #include <stdint.h>
46 #include <string>
47 #include <utility>
48 
49 #include <boost/function.hpp> // for function
50 #include <boost/thread/mutex.hpp> // for mutex
51 
52 #include "uhal/log/exception.hpp"
53 #include "uhal/ClientInterface.hpp" // for PacketLevelError
55 
56 
57 // Forward declarations
58 namespace boost { template <class Y> class shared_ptr; }
59 
60 
61 namespace uhal
62 {
63  class Buffers;
64  struct URI;
65 
66 
68  template< uint8_t IPbus_major , uint8_t IPbus_minor >
69  class IPbus;
70 
71 
73  template< uint8_t IPbus_minor >
74  class IPbus < 1 , IPbus_minor > : public IPbusCore
75  {
76 
77  public:
83  IPbus ( const std::string& aId, const URI& aUri );
84 
86  virtual ~IPbus();
87 
88  protected:
90  virtual void preamble ( boost::shared_ptr< Buffers > aBuffers );
91 
93  virtual uint32_t getPreambleSize();
94 
96  virtual void predispatch ( boost::shared_ptr< Buffers > aBuffers );
97 
98  public:
107  static uint32_t CalculateHeader ( const IPbusTransactionType& aType , const uint32_t& aWordCount , const uint32_t& aTransactionId , const uint8_t& aInfoCode = 0 );
108 
117  static uint32_t ExpectedHeader ( const IPbusTransactionType& aType , const uint32_t& aWordCount , const uint32_t& aTransactionId , const uint8_t& aInfoCode = 0 );
118 
128  static bool ExtractHeader ( const uint32_t& aHeader , IPbusTransactionType& aType , uint32_t& aWordCount , uint32_t& aTransactionId , uint8_t& aInfoCode );
129 
130 
131  protected:
132 
141  uint32_t implementCalculateHeader ( const IPbusTransactionType& aType , const uint32_t& aWordCount , const uint32_t& aTransactionId , const uint8_t& aInfoCode );
142 
152  bool implementExtractHeader ( const uint32_t& aHeader , IPbusTransactionType& aType , uint32_t& aWordCount , uint32_t& aTransactionId , uint8_t& aInfoCode );
153 
155  uint8_t requestTransactionInfoCode () const
156  {
157  return 0;
158  }
159 
161  uint32_t getMaxTransactionWordCount() const
162  {
163  return 0x1ff;
164  }
165 
166  virtual uint32_t getMaxNumberOfBuffers()
167  {
168  return 1;
169  }
170 
171  virtual void dispatchExceptionHandler();
172 
173  private:
174  boost::function<void (std::ostream&, const uint8_t&)> getInfoCodeTranslator() { return translateInfoCode; }
175 
176  static void translateInfoCode(std::ostream& aStream, const uint8_t& aErrorCode);
177  };
178 
179 
180 
181  namespace exception
182  {
184  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( IPbus2PacketHeaderMismatch, PacketLevelError, "Exception class to handle the case where the IPbus 2.0 packet header does not match that sent." )
185  }
186 
187 
189  template< uint8_t IPbus_minor >
190  class IPbus < 2 , IPbus_minor > : public IPbusCore
191  {
192 
193  public:
199  IPbus ( const std::string& aId, const URI& aUri );
200 
202  virtual ~IPbus();
203 
204  protected:
206  virtual void preamble ( boost::shared_ptr< Buffers > aBuffers );
207 
209  virtual uint32_t getPreambleSize();
210 
212  virtual void predispatch ( boost::shared_ptr< Buffers > aBuffers );
213 
214  public:
223  static uint32_t CalculateHeader ( const IPbusTransactionType& aType , const uint32_t& aWordCount , const uint32_t& aTransactionId , const uint8_t& aInfoCode = 0 );
224 
233  static uint32_t ExpectedHeader ( const IPbusTransactionType& aType , const uint32_t& aWordCount , const uint32_t& aTransactionId , const uint8_t& aInfoCode = 0 );
234 
244  static bool ExtractHeader ( const uint32_t& aHeader , IPbusTransactionType& aType , uint32_t& aWordCount , uint32_t& aTransactionId , uint8_t& aInfoCode );
245 
246 
247  protected:
248 
257  virtual exception::exception* validate ( uint8_t* aSendBufferStart ,
258  uint8_t* aSendBufferEnd ,
259  std::deque< std::pair< uint8_t* , uint32_t > >::iterator aReplyStartIt ,
260  std::deque< std::pair< uint8_t* , uint32_t > >::iterator aReplyEndIt );
261 
270  uint32_t implementCalculateHeader ( const IPbusTransactionType& aType , const uint32_t& aWordCount , const uint32_t& aTransactionId , const uint8_t& aInfoCode );
271 
281  bool implementExtractHeader ( const uint32_t& aHeader , IPbusTransactionType& aType , uint32_t& aWordCount , uint32_t& aTransactionId , uint8_t& aInfoCode );
282 
284  uint8_t requestTransactionInfoCode () const
285  {
286  return 0xF;
287  }
288 
290  uint32_t getMaxTransactionWordCount() const
291  {
292  return 0xff;
293  }
294 
295  virtual uint32_t getMaxNumberOfBuffers()
296  {
297  return 16;
298  }
299 
300  virtual void dispatchExceptionHandler();
301 
302  private:
303  boost::function<void (std::ostream&, const uint8_t&)> getInfoCodeTranslator() { return translateInfoCode; }
304 
305  static void translateInfoCode(std::ostream& aStream, const uint8_t& aErrorCode);
306 
308  uint16_t mPacketCounter;
309 
310  boost::mutex mReceivePacketMutex;
311  std::deque< uint32_t > mReceivePacketHeader;
312  };
313 
314 
315 }
316 
317 
318 #endif
uhal::IPbusCore
A class providing the core IPbus packing functionality.
Definition: ProtocolIPbusCore.hpp:117
boost::shared_ptr
Definition: DerivedNodeFactory.hpp:52
uhal::IPbus< 2, IPbus_minor >::getMaxNumberOfBuffers
virtual uint32_t getMaxNumberOfBuffers()
Return the maximum number of packets in flight.
Definition: ProtocolIPbus.hpp:295
uhal::IPbusTransactionType
IPbusTransactionType
Enumerated type to define the IPbus transaction type.
Definition: ProtocolIPbusCore.hpp:74
uhal::IPbus< 2, IPbus_minor >::getInfoCodeTranslator
boost::function< void(std::ostream &, const uint8_t &)> getInfoCodeTranslator()
Definition: ProtocolIPbus.hpp:303
uhal::IPbus
A class which provides the version-specific functionality for IPbus.
Definition: ProtocolIPbus.hpp:69
UHAL_DEFINE_DERIVED_EXCEPTION_CLASS
#define UHAL_DEFINE_DERIVED_EXCEPTION_CLASS(ClassName, BaseClassName, ClassDescription)
Macro for simplifying the declaration and definition of derived exception types.
Definition: exception.hpp:49
boost
Definition: log.hpp:13
uhal::IPbus< 2, IPbus_minor >::requestTransactionInfoCode
uint8_t requestTransactionInfoCode() const
Returns the InfoCode for request transactions in this IPbus version.
Definition: ProtocolIPbus.hpp:284
uhal::IPbus< 2, IPbus_minor >::mReceivePacketMutex
boost::mutex mReceivePacketMutex
Definition: ProtocolIPbus.hpp:310
uhal::exception::exception
An abstract base exception class, including an interface to throw as the derived type (for passing ex...
Definition: exception.hpp:71
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::IPbus< 1, IPbus_minor >::requestTransactionInfoCode
uint8_t requestTransactionInfoCode() const
Returns the InfoCode for request transactions in this IPbus version.
Definition: ProtocolIPbus.hpp:155
uhal::IPbus< 1, IPbus_minor >::getMaxTransactionWordCount
uint32_t getMaxTransactionWordCount() const
Returns the maximum value of the word count in the transaction header, for each IPbus version.
Definition: ProtocolIPbus.hpp:161
uhal::IPbus< 2, IPbus_minor >::mPacketCounter
uint16_t mPacketCounter
The transaction counter which will be incremented in the sent IPbus headers.
Definition: ProtocolIPbus.hpp:308
ProtocolIPbusCore.hpp
uhal::IPbus< 1, IPbus_minor >::getMaxNumberOfBuffers
virtual uint32_t getMaxNumberOfBuffers()
Return the maximum number of packets in flight.
Definition: ProtocolIPbus.hpp:166
uhal::IPbus< 2, IPbus_minor >::mReceivePacketHeader
std::deque< uint32_t > mReceivePacketHeader
Definition: ProtocolIPbus.hpp:311
uhal::URI
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50
exception.hpp
uhal::IPbus< 1, IPbus_minor >::getInfoCodeTranslator
boost::function< void(std::ostream &, const uint8_t &)> getInfoCodeTranslator()
Definition: ProtocolIPbus.hpp:174
ClientInterface.hpp
uhal::IPbus< 2, IPbus_minor >::getMaxTransactionWordCount
uint32_t getMaxTransactionWordCount() const
Returns the maximum value of the word count in the transaction header, for each IPbus version.
Definition: ProtocolIPbus.hpp:290