μHAL (v2.7.9)
Part of the IPbus software repository
ClientInterface.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 
40 #ifndef _uhal_ClientInterface_hpp_
41 #define _uhal_ClientInterface_hpp_
42 
43 
44 #include <deque>
45 #include <stdint.h>
46 #include <string>
47 #include <utility>
48 #include <vector>
49 
50 #include <boost/date_time/posix_time/posix_time_types.hpp>
51 #include <boost/shared_ptr.hpp>
52 #include <boost/weak_ptr.hpp>
53 #include <boost/thread/mutex.hpp>
54 
55 #include "uhal/grammars/URI.hpp"
56 #include "uhal/log/exception.hpp"
57 #include "uhal/definitions.hpp"
58 #include "uhal/ValMem.hpp"
59 
60 
61 namespace uhal
62 {
63  // Forward declaration
64  class Buffers;
65  class ClientInterface;
66  class HwInterface;
67  class IPbusCore;
68  class Node;
69 
70  namespace detail
71  {
72  std::string getAddressDescription(const ClientInterface&, const uint32_t, const size_t&);
73  }
74 
75  namespace exception
76  {
78  UHAL_DEFINE_EXCEPTION_CLASS ( PingFailed , "Exception class to handle the case where pinging of a client failed." )
79 
80 
81  UHAL_DEFINE_EXCEPTION_CLASS ( BitsSetWhichAreForbiddenByBitMask , "Exception class to handle the case where a masked write was attempted with a data source which has non-zero bits outside the bit-mask's bounds." )
82 
84  UHAL_DEFINE_EXCEPTION_CLASS ( ValidationError , "Exception class to handle the case where we were unable to validate the packet." )
85 
87  UHAL_DEFINE_EXCEPTION_CLASS ( NullBufferException , "Exception class to handle a NULL buffer being passed to the transport class." )
88 
89  UHAL_DEFINE_EXCEPTION_CLASS ( PacketLevelError, "Base exception class covering situations in which a packet-level error occurs.")
90 
91  UHAL_DEFINE_EXCEPTION_CLASS ( TransactionLevelError, "Base exception class covering situations in which a transaction-level error occurs (i.e. error occurs in individual read/write).")
92 
93  UHAL_DEFINE_EXCEPTION_CLASS ( ClientTimeout, "Base exception class covering timeouts when waiting for reply from device")
94 
95  UHAL_DEFINE_EXCEPTION_CLASS ( TransportLayerError, "Base exception class covering non-timeout transport-layer-specific errors.")
96  }
97 
100  {
101  protected:
108  ClientInterface ( const std::string& aId, const URI& aUri , const boost::posix_time::time_duration& aTimeoutPeriod );
109 
110  private:
112  ClientInterface ();
113 
118  ClientInterface ( const ClientInterface& aClientInterface );
119 
125  virtual ClientInterface& operator= ( const ClientInterface& aClientInterface );
126 
127  public:
129  virtual ~ClientInterface();
130 
135  const std::string& id() const;
136 
137  // /**
138  // Ping the target for this client
139  // */
140  // void ping();
141 
146  const std::string& uri() const;
147 
149  void dispatch ();
150 
156  void setTimeoutPeriod ( const uint32_t& aTimeoutPeriod = 0 );
157 
163  uint64_t getTimeoutPeriod();
164 
165  protected:
171  const boost::posix_time::time_duration& getBoostTimeoutPeriod();
172 
173  public:
174 
180  ValHeader write ( const uint32_t& aAddr, const uint32_t& aValue );
181 
188  ValHeader write ( const uint32_t& aAddr, const uint32_t& aValue, const uint32_t& aMask );
189 
196  ValHeader writeBlock ( const uint32_t& aAddr, const std::vector< uint32_t >& aValues, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL );
197 
203  ValWord< uint32_t > read ( const uint32_t& aAddr );
204 
211  ValWord< uint32_t > read ( const uint32_t& aAddr, const uint32_t& aMask );
212 
220  ValVector< uint32_t > readBlock ( const uint32_t& aAddr, const uint32_t& aSize, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL );
221 
229  ValWord< uint32_t > rmw_bits ( const uint32_t& aAddr , const uint32_t& aANDterm , const uint32_t& aORterm );
230 
237  ValWord< uint32_t > rmw_sum ( const uint32_t& aAddr , const int32_t& aAddend );
238 
239  protected:
244  virtual void implementDispatch ( boost::shared_ptr< Buffers > aBuffers ) = 0;
245 
247  virtual void Flush( );
248 
249 
251  virtual ValHeader implementBOT( ) = 0;
252 
258  virtual ValHeader implementWrite ( const uint32_t& aAddr, const uint32_t& aValue ) = 0;
259 
266  virtual ValHeader implementWriteBlock ( const uint32_t& aAddr, const std::vector< uint32_t >& aValues, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL ) = 0;
267 
274  virtual ValWord< uint32_t > implementRead ( const uint32_t& aAddr, const uint32_t& aMask = defs::NOMASK ) = 0;
275 
283  virtual ValVector< uint32_t > implementReadBlock ( const uint32_t& aAddr, const uint32_t& aSize, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL ) = 0;
284 
285 
293  virtual ValWord< uint32_t > implementRMWbits ( const uint32_t& aAddr , const uint32_t& aANDterm , const uint32_t& aORterm ) = 0;
294 
301  virtual ValWord< uint32_t > implementRMWsum ( const uint32_t& aAddr , const int32_t& aAddend ) = 0;
302 
304  virtual void preamble ( boost::shared_ptr< Buffers > aBuffers );
305 
307  virtual uint32_t getPreambleSize();
308 
313  virtual void predispatch ( boost::shared_ptr< Buffers > aBuffers );
314 
319  std::pair < ValHeader , _ValHeader_* > CreateValHeader();
320 
327  std::pair < ValWord<uint32_t> , _ValWord_<uint32_t>* > CreateValWord ( const uint32_t& aValue , const uint32_t& aMask = defs::NOMASK );
328 
334  std::pair < ValVector<uint32_t> , _ValVector_<uint32_t>* > CreateValVector ( const uint32_t& aSize );
335 
341  virtual exception::exception* validate ( boost::shared_ptr< Buffers > aBuffers );
342 
343 
344  protected:
353  virtual exception::exception* validate ( uint8_t* aSendBufferStart ,
354  uint8_t* aSendBufferEnd ,
355  std::deque< std::pair< uint8_t* , uint32_t > >::iterator aReplyStartIt ,
356  std::deque< std::pair< uint8_t* , uint32_t > >::iterator aReplyEndIt ) = 0;
357 
359  virtual void dispatchExceptionHandler();
360 
365  void returnBufferToPool ( boost::shared_ptr< Buffers >& aBuffers );
370  void returnBufferToPool ( std::deque< boost::shared_ptr< Buffers > >& aBuffers );
375  void returnBufferToPool ( std::vector< boost::shared_ptr< Buffers > >& aBuffers );
380  void returnBufferToPool ( std::deque< std::vector< boost::shared_ptr< Buffers > > >& aBuffers );
381 
382  private:
387  void updateCurrentBuffers();
388  void deleteBuffers();
389 
390 
391  private:
393  boost::mutex mUserSideMutex;
394 
396  boost::mutex mBufferMutex;
397 
399  std::deque < boost::shared_ptr< Buffers > > mBuffers;
400 
401 #ifdef NO_PREEMPTIVE_DISPATCH
402  std::deque < boost::shared_ptr< Buffers > > mNoPreemptiveDispatchBuffers;
404 #endif
405 
408 
410  std::string mId;
411 
413  boost::posix_time::time_duration mTimeoutPeriod;
414 
415  boost::weak_ptr<Node> mNode;
416 
417  friend class IPbusCore;
418  friend class HwInterface;
419  friend std::string detail::getAddressDescription(const ClientInterface&, const uint32_t, const size_t&);
420 
421  protected:
422 
425 
426  std::string mUriString;
427 
435  virtual boost::shared_ptr< Buffers > checkBufferSpace ( const uint32_t& aSendSize , const uint32_t& aReplySize , uint32_t& aAvailableSendSize , uint32_t& aAvailableReplySize );
436 
441  virtual uint32_t getMaxNumberOfBuffers() = 0;
446  virtual uint32_t getMaxSendSize() = 0;
451  virtual uint32_t getMaxReplySize() = 0;
452  };
453 
454 }
455 
456 #endif
457 
uhal::HwInterface
A class which bundles a node tree and an IPbus client interface together providing everything you nee...
Definition: HwInterface.hpp:61
uhal::ValVector< uint32_t >
UHAL_DEFINE_EXCEPTION_CLASS
#define UHAL_DEFINE_EXCEPTION_CLASS(ClassName, ClassDescription)
Definition: exception.hpp:59
uhal::IPbusCore
A class providing the core IPbus packing functionality.
Definition: ProtocolIPbusCore.hpp:117
boost::shared_ptr
Definition: DerivedNodeFactory.hpp:52
definitions.hpp
uhal::ClientInterface::mCurrentBuffers
boost::shared_ptr< Buffers > mCurrentBuffers
A pointer to a buffer-wrapper object.
Definition: ClientInterface.hpp:407
uhal::ClientInterface::implementWriteBlock
virtual ValHeader implementWriteBlock(const uint32_t &aAddr, const std::vector< uint32_t > &aValues, const defs::BlockReadWriteMode &aMode=defs::INCREMENTAL)=0
Write a block of data to a block of registers or a block-write port.
uhal::tests::writeBlock
c writeBlock(addr, xx)
uhal::ClientInterface::implementRMWbits
virtual ValWord< uint32_t > implementRMWbits(const uint32_t &aAddr, const uint32_t &aANDterm, const uint32_t &aORterm)=0
Read the value of a register, apply the AND-term, apply the OR-term, set the register to this new val...
uhal::ClientInterface
An abstract base class for defining the interface to the various IPbus clients as well as providing t...
Definition: ClientInterface.hpp:100
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::ClientInterface::implementDispatch
virtual void implementDispatch(boost::shared_ptr< Buffers > aBuffers)=0
Pure virtual function which actually performs the dispatch operation.
uhal::ValHeader
A class which wraps a single word of data and marks whether or not it is valid.
Definition: ValMem.hpp:148
uhal
Definition: HttpResponseGrammar.hpp:49
uhal::ClientInterface::getMaxSendSize
virtual uint32_t getMaxSendSize()=0
Return the maximum size to be sent based on the buffer size in the target.
uhal::tests::setTimeoutPeriod
hw setTimeoutPeriod(timeout)
uhal::ClientInterface::mId
std::string mId
the identifier of the target for this client
Definition: ClientInterface.hpp:410
uhal::defs::BlockReadWriteMode
BlockReadWriteMode
define whether transactions target a single register, a block of registers, a block-read/write port o...
Definition: definitions.hpp:53
uhal::tests::for
for(size_t i=0;i< lDepths.size();i++)
Definition: test_block.cpp:135
uhal::ClientInterface::getMaxNumberOfBuffers
virtual uint32_t getMaxNumberOfBuffers()=0
Return the maximum number of packets in flight.
uhal::ClientInterface::mUriString
std::string mUriString
Definition: ClientInterface.hpp:426
uhal::ClientInterface::mUserSideMutex
boost::mutex mUserSideMutex
A MutEx lock used to make sure the access functions are thread safe.
Definition: ClientInterface.hpp:393
uhal::ClientInterface::implementRead
virtual ValWord< uint32_t > implementRead(const uint32_t &aAddr, const uint32_t &aMask=defs::NOMASK)=0
Read a single, masked, unsigned word.
uhal::ClientInterface::implementReadBlock
virtual ValVector< uint32_t > implementReadBlock(const uint32_t &aAddr, const uint32_t &aSize, const defs::BlockReadWriteMode &aMode=defs::INCREMENTAL)=0
Read a block of unsigned data from a block of registers or a block-read port.
uhal::defs::INCREMENTAL
@ INCREMENTAL
Definition: definitions.hpp:53
uhal::tests::uri
std::string uri
Definition: test_single.cpp:89
uhal::ClientInterface::mTimeoutPeriod
boost::posix_time::time_duration mTimeoutPeriod
Timeout period for transactions.
Definition: ClientInterface.hpp:413
uhal::ClientInterface::mUri
URI mUri
a struct containing the full URI of the target for this client
Definition: ClientInterface.hpp:424
uhal::ValWord< uint32_t >
ValMem.hpp
uhal::_ValVector_
A Template helper struct wrapping a block of IPbus header, a register for storing a block of data and...
Definition: ValMem.hpp:127
URI.hpp
uhal::ClientInterface::implementBOT
virtual ValHeader implementBOT()=0
Send a byte order transaction.
uhal::_ValWord_
A Template helper struct wrapping an IPbus header, a register for storing a single word of data,...
Definition: ValMem.hpp:103
uhal::tests::write
c write(addr, xx[0])
uhal::URI
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50
exception.hpp
uhal::ClientInterface::implementWrite
virtual ValHeader implementWrite(const uint32_t &aAddr, const uint32_t &aValue)=0
Write a single, unmasked word to a register.
uhal::detail::getAddressDescription
std::string getAddressDescription(const ClientInterface &, const uint32_t, const size_t &)
Generates a short string summarising which nodes match the specified address.
Definition: utilities.cpp:106
uhal::defs::NOMASK
const uint32_t NOMASK
define what it means to have no mask
Definition: definitions.hpp:56
uhal::tests::dispatch
c dispatch()
uhal::ClientInterface::mBufferMutex
boost::mutex mBufferMutex
A MutEx lock used to make sure the access to the buffers is thread safe.
Definition: ClientInterface.hpp:396
uhal::ClientInterface::implementRMWsum
virtual ValWord< uint32_t > implementRMWsum(const uint32_t &aAddr, const int32_t &aAddend)=0
Read the value of a register, add the addend, set the register to this new value and return a copy of...
uhal::ClientInterface::validate
virtual exception::exception * validate(uint8_t *aSendBufferStart, uint8_t *aSendBufferEnd, std::deque< std::pair< uint8_t *, uint32_t > >::iterator aReplyStartIt, std::deque< std::pair< uint8_t *, uint32_t > >::iterator aReplyEndIt)=0
Function which the dispatch calls when the reply is received to check that the headers are as expecte...
uhal::ClientInterface::getMaxReplySize
virtual uint32_t getMaxReplySize()=0
Return the maximum size of reply packet based on the buffer size in the target.
uhal::ClientInterface::mBuffers
std::deque< boost::shared_ptr< Buffers > > mBuffers
A memory pool of buffers which will be dispatched.
Definition: ClientInterface.hpp:399
uhal::ClientInterface::mNode
boost::weak_ptr< Node > mNode
Definition: ClientInterface.hpp:415