μHAL (v2.8.17)
Part of the IPbus software repository
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <memory>
46#include <mutex>
47#include <stdint.h>
48#include <string>
49#include <utility>
50#include <vector>
51
52#include <boost/date_time/posix_time/posix_time_types.hpp>
53
54#include "uhal/grammars/URI.hpp"
56#include "uhal/definitions.hpp"
57#include "uhal/ValMem.hpp"
58
59
60namespace uhal
61{
62 // Forward declaration
63 class Buffers;
64 class ClientInterface;
65 class HwInterface;
66 class IPbusCore;
67 class Node;
68
69 namespace detail
70 {
71 std::string getAddressDescription(const ClientInterface&, const uint32_t, const size_t&);
72 }
73
74 namespace exception
75 {
77 UHAL_DEFINE_EXCEPTION_CLASS ( PingFailed , "Exception class to handle the case where pinging of a client failed." )
78
79
80 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." )
81
83 UHAL_DEFINE_EXCEPTION_CLASS ( ValidationError , "Exception class to handle the case where we were unable to validate the packet." )
84
86 UHAL_DEFINE_EXCEPTION_CLASS ( NullBufferException , "Exception class to handle a NULL buffer being passed to the transport class." )
87
88 UHAL_DEFINE_EXCEPTION_CLASS ( PacketLevelError, "Base exception class covering situations in which a packet-level error occurs.")
89
90 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).")
91
92 UHAL_DEFINE_EXCEPTION_CLASS ( ClientTimeout, "Base exception class covering timeouts when waiting for reply from device")
93
94 UHAL_DEFINE_EXCEPTION_CLASS ( TransportLayerError, "Base exception class covering non-timeout transport-layer-specific errors.")
95
96 UHAL_DEFINE_EXCEPTION_CLASS ( InvalidURI, "Exception class for invalid URIs." )
97 }
98
101 {
102 protected:
109 ClientInterface ( const std::string& aId, const URI& aUri , const boost::posix_time::time_duration& aTimeoutPeriod );
110
111 private:
114
119 ClientInterface ( const ClientInterface& aClientInterface );
120
126 virtual ClientInterface& operator= ( const ClientInterface& aClientInterface );
127
128 public:
130 virtual ~ClientInterface();
131
136 const std::string& id() const;
137
138 // /**
139 // Ping the target for this client
140 // */
141 // void ping();
142
147 const std::string& uri() const;
148
150 void dispatch ();
151
157 void setTimeoutPeriod ( const uint32_t& aTimeoutPeriod = 0 );
158
164 uint64_t getTimeoutPeriod();
165
166 protected:
172 const boost::posix_time::time_duration& getBoostTimeoutPeriod();
173
174 public:
175
181 ValHeader write ( const uint32_t& aAddr, const uint32_t& aValue );
182
189 ValHeader write ( const uint32_t& aAddr, const uint32_t& aValue, const uint32_t& aMask );
190
197 ValHeader writeBlock ( const uint32_t& aAddr, const std::vector< uint32_t >& aValues, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL );
198
204 ValWord< uint32_t > read ( const uint32_t& aAddr );
205
212 ValWord< uint32_t > read ( const uint32_t& aAddr, const uint32_t& aMask );
213
221 ValVector< uint32_t > readBlock ( const uint32_t& aAddr, const uint32_t& aSize, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL );
222
230 ValWord< uint32_t > rmw_bits ( const uint32_t& aAddr , const uint32_t& aANDterm , const uint32_t& aORterm );
231
238 ValWord< uint32_t > rmw_sum ( const uint32_t& aAddr , const int32_t& aAddend );
239
240 protected:
245 virtual void implementDispatch ( std::shared_ptr< Buffers > aBuffers ) = 0;
246
248 virtual void Flush( );
249
250
252 virtual ValHeader implementBOT( ) = 0;
253
259 virtual ValHeader implementWrite ( const uint32_t& aAddr, const uint32_t& aValue ) = 0;
260
267 virtual ValHeader implementWriteBlock ( const uint32_t& aAddr, const std::vector< uint32_t >& aValues, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL ) = 0;
268
275 virtual ValWord< uint32_t > implementRead ( const uint32_t& aAddr, const uint32_t& aMask = defs::NOMASK ) = 0;
276
284 virtual ValVector< uint32_t > implementReadBlock ( const uint32_t& aAddr, const uint32_t& aSize, const defs::BlockReadWriteMode& aMode=defs::INCREMENTAL ) = 0;
285
286
294 virtual ValWord< uint32_t > implementRMWbits ( const uint32_t& aAddr , const uint32_t& aANDterm , const uint32_t& aORterm ) = 0;
295
302 virtual ValWord< uint32_t > implementRMWsum ( const uint32_t& aAddr , const int32_t& aAddend ) = 0;
303
305 virtual void preamble ( std::shared_ptr< Buffers > aBuffers );
306
308 virtual uint32_t getPreambleSize();
309
314 virtual void predispatch ( std::shared_ptr< Buffers > aBuffers );
315
320 std::pair < ValHeader , _ValHeader_* > CreateValHeader();
321
328 std::pair < ValWord<uint32_t> , _ValWord_<uint32_t>* > CreateValWord ( const uint32_t& aValue , const uint32_t& aMask = defs::NOMASK );
329
335 std::pair < ValVector<uint32_t> , _ValVector_<uint32_t>* > CreateValVector ( const uint32_t& aSize );
336
342 virtual exception::exception* validate ( std::shared_ptr< Buffers > aBuffers );
343
344
345 protected:
354 virtual exception::exception* validate ( uint8_t* aSendBufferStart ,
355 uint8_t* aSendBufferEnd ,
356 std::deque< std::pair< uint8_t* , uint32_t > >::iterator aReplyStartIt ,
357 std::deque< std::pair< uint8_t* , uint32_t > >::iterator aReplyEndIt ) = 0;
358
360 virtual void dispatchExceptionHandler();
361
366 void returnBufferToPool ( std::shared_ptr< Buffers >& aBuffers );
371 void returnBufferToPool ( std::deque< std::shared_ptr< Buffers > >& aBuffers );
376 void returnBufferToPool ( std::vector< std::shared_ptr< Buffers > >& aBuffers );
381 void returnBufferToPool ( std::deque< std::vector< std::shared_ptr< Buffers > > >& aBuffers );
382
383 private:
388 void updateCurrentBuffers();
389 void deleteBuffers();
390
391
392 private:
394 std::mutex mUserSideMutex;
395
397 std::mutex mBufferMutex;
398
400 std::deque < std::shared_ptr< Buffers > > mBuffers;
401
402#ifdef NO_PREEMPTIVE_DISPATCH
404 std::deque < std::shared_ptr< Buffers > > mNoPreemptiveDispatchBuffers;
405#endif
406
408 std::shared_ptr< Buffers > mCurrentBuffers;
409
411 std::string mId;
412
414 boost::posix_time::time_duration mTimeoutPeriod;
415
416 std::weak_ptr<Node> mNode;
417
418 friend class IPbusCore;
419 friend class HwInterface;
420 friend std::string detail::getAddressDescription(const ClientInterface&, const uint32_t, const size_t&);
421
422 protected:
423
426
427 std::string mUriString;
428
436 virtual std::shared_ptr< Buffers > checkBufferSpace ( const uint32_t& aSendSize , const uint32_t& aReplySize , uint32_t& aAvailableSendSize , uint32_t& aAvailableReplySize );
437
442 virtual uint32_t getMaxNumberOfBuffers() = 0;
447 virtual uint32_t getMaxSendSize() = 0;
452 virtual uint32_t getMaxReplySize() = 0;
453 };
454
455}
456
457#endif
458
Wrapper to generate a new Python exception type.
Definition: pybind11.h:2517
\rst Holds a reference to a Python object (no reference counting)
Definition: pytypes.h:194
\rst Wraps a Python iterator so that it can also be used as a C++ input iterator
Definition: pytypes.h:1102
An abstract base class for defining the interface to the various IPbus clients as well as providing t...
virtual ValHeader implementBOT()=0
Send a byte order transaction.
virtual uint32_t getMaxReplySize()=0
Return the maximum size of reply packet based on the buffer size in the target.
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...
virtual void implementDispatch(std::shared_ptr< Buffers > aBuffers)=0
Pure virtual function which actually performs the dispatch operation.
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.
std::mutex mUserSideMutex
A MutEx lock used to make sure the access functions are thread safe.
std::shared_ptr< Buffers > mCurrentBuffers
A pointer to a buffer-wrapper object.
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...
boost::posix_time::time_duration mTimeoutPeriod
Timeout period for transactions.
virtual uint32_t getMaxSendSize()=0
Return the maximum size to be sent based on the buffer size in the target.
virtual ValWord< uint32_t > implementRead(const uint32_t &aAddr, const uint32_t &aMask=defs::NOMASK)=0
Read a single, masked, unsigned word.
virtual uint32_t getMaxNumberOfBuffers()=0
Return the maximum number of packets in flight.
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.
std::deque< std::shared_ptr< Buffers > > mBuffers
A memory pool of buffers which will be dispatched.
URI mUri
a struct containing the full URI of the target for this client
std::string mId
the identifier of the target for this client
std::mutex mBufferMutex
A MutEx lock used to make sure the access to the buffers is thread safe.
std::weak_ptr< Node > mNode
virtual ValHeader implementWrite(const uint32_t &aAddr, const uint32_t &aValue)=0
Write a single, unmasked word to a register.
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...
A class which bundles a node tree and an IPbus client interface together providing everything you nee...
Definition: HwInterface.hpp:56
A class providing the core IPbus packing functionality.
A class which wraps a single word of data and marks whether or not it is valid.
Definition: ValMem.hpp:147
An abstract base exception class, including an interface to throw as the derived type (for passing ex...
Definition: exception.hpp:71
#define UHAL_DEFINE_EXCEPTION_CLASS(ClassName, ClassDescription)
Definition: exception.hpp:59
BlockReadWriteMode
define whether transactions target a single register, a block of registers, a block-read/write port o...
Definition: definitions.hpp:53
const uint32_t NOMASK
define what it means to have no mask
Definition: definitions.hpp:56
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:105
arr data(const arr &a, Ix... index)
A Template helper struct wrapping a block of IPbus header, a register for storing a block of data and...
Definition: ValMem.hpp:126
A Template helper struct wrapping an IPbus header, a register for storing a single word of data,...
Definition: ValMem.hpp:102
Struct to store a URI when parsed by boost spirit.
Definition: URI.hpp:50