μHAL (v2.7.9)
Part of the IPbus software repository
ProtocolMmap.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  Tom Williams, Rutherford Appleton Laboratory, Oxfordshire
31  email: tom.williams <AT> cern.ch
32 
33 ---------------------------------------------------------------------------
34 */
35 
42 #ifndef _uhal_ProtocolMmap_hpp_
43 #define _uhal_ProtocolMmap_hpp_
44 
45 
46 #include <deque> // for deque
47 #include <istream> // for istream
48 #include <stddef.h> // for size_t
49 #include <stdint.h> // for uint32_t, uint8_t
50 #include <string> // for string
51 #include <utility> // for pair
52 #include <vector> // for vector
53 
54 #include <boost/chrono/system_clocks.hpp> // for steady_clock
55 #include <boost/function.hpp> // for function
56 
57 #include "uhal/ClientInterface.hpp"
58 #include "uhal/log/exception.hpp"
59 #include "uhal/ProtocolIPbus.hpp"
60 
61 
62 namespace boost
63 {
64  template <class Y> class shared_ptr;
65 }
66 
67 namespace uhal
68 {
69  class Buffers;
70  struct URI;
71 
72  namespace exception
73  {
75  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( MmapTimeout , ClientTimeout , "Exception class to handle the case in which the PCIe connection timed out." )
77  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( MmapInitialisationError , TransportLayerError , "Exception class to handle a failure to read from the specified device files during initialisation." )
79  UHAL_DEFINE_DERIVED_EXCEPTION_CLASS ( MmapCommunicationError , TransportLayerError , "Exception class to handle a low-level seek/read/write error after initialisation." )
80  }
81 
83  class Mmap : public IPbus< 2 , 0 >
84  {
85  public:
86  class PacketFmt {
87  public:
88  PacketFmt(const uint8_t* const, const size_t);
89  PacketFmt(const std::vector< std::pair<const uint8_t*, size_t> >& aData);
90  ~PacketFmt();
91 
92  const std::vector< std::pair<const uint8_t*, size_t> > mData;
93  };
94 
95  private:
96  class File {
97  public:
98  File(const std::string& aPath, int aFlags);
99  ~File();
100 
101  const std::string& getPath() const;
102  void setPath(const std::string& aPath);
103 
104  void setOffset(size_t aOffset);
105 
106  void open();
107  void close();
108 
109  void read(const uint32_t aAddr, const uint32_t aNrWords, std::vector<uint32_t>& aValues);
110 
111  void write(const uint32_t aAddr, const std::vector<std::pair<const uint8_t*, size_t> >& aData);
112 
113  private:
114  std::string mPath;
115  int mFd;
116  int mFlags;
117  off_t mOffset;
118  void* mMmapPtr;
119  void* mMmapIOPtr;
120  };
121 
122  template <typename T>
123  struct HexTo {
124  T value;
125  operator T() const {return value;}
126  friend std::istream& operator>>(std::istream& in, HexTo& out)
127  {
128  in >> std::hex >> out.value;
129  return in;
130  }
131  };
132 
133  Mmap ( const Mmap& aMmap );
134 
135  Mmap& operator= ( const Mmap& aMmap );
136 
137  public:
143  Mmap ( const std::string& aId, const URI& aUri );
144 
146  virtual ~Mmap();
147 
148  private:
149 
155  void implementDispatch ( boost::shared_ptr< Buffers > aBuffers );
156 
158  virtual void Flush( );
159 
160 
162  virtual void dispatchExceptionHandler();
163 
164 
166 
167  typedef boost::chrono::steady_clock SteadyClock_t;
168 
173  uint32_t getMaxSendSize();
174 
179  uint32_t getMaxReplySize();
180 
182  void connect();
183 
185  void disconnect();
186 
188  void write(const boost::shared_ptr<Buffers>& aBuffers);
189 
191  void read();
192 
194 
196 
197  boost::chrono::microseconds mSleepDuration;
198 
199  uint32_t mNumberOfPages, mPageSize, mIndexNextPage, mPublishedReplyPageCount, mReadReplyPageCount;
200 
202  std::deque < boost::shared_ptr< Buffers > > mReplyQueue;
203 
209  };
210 
211 
212 }
213 
214 
215 #endif
uhal::Mmap::InnerProtocol
IPbus< 2, 0 > InnerProtocol
Definition: ProtocolMmap.hpp:165
uhal::Mmap::mReplyQueue
std::deque< boost::shared_ptr< Buffers > > mReplyQueue
The list of buffers still awaiting a reply.
Definition: ProtocolMmap.hpp:202
boost::shared_ptr
Definition: DerivedNodeFactory.hpp:52
uhal::Mmap::File::mFd
int mFd
Definition: ProtocolMmap.hpp:115
uhal::Mmap::File::mPath
std::string mPath
Definition: ProtocolMmap.hpp:114
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::Mmap::mDeviceFile
File mDeviceFile
Definition: ProtocolMmap.hpp:195
uhal::Mmap::Mmap
Mmap(const Mmap &aMmap)
uhal::Mmap::File::mMmapIOPtr
void * mMmapIOPtr
Definition: ProtocolMmap.hpp:119
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::Mmap::mAsynchronousException
uhal::exception::exception * mAsynchronousException
A pointer to an exception object for passing exceptions from the worker thread to the main thread.
Definition: ProtocolMmap.hpp:208
uhal::Mmap::File::mFlags
int mFlags
Definition: ProtocolMmap.hpp:116
uhal::Mmap::HexTo
Definition: ProtocolMmap.hpp:123
uhal::hex
@ hex
Hexadecimal.
Definition: log_inserters.integer.hpp:51
uhal::Mmap::PacketFmt::mData
const std::vector< std::pair< const uint8_t *, size_t > > mData
Definition: ProtocolMmap.hpp:92
uhal::Mmap::HexTo::value
T value
Definition: ProtocolMmap.hpp:124
ProtocolIPbus.hpp
uhal::Mmap::SteadyClock_t
boost::chrono::steady_clock SteadyClock_t
Definition: ProtocolMmap.hpp:167
uhal::Mmap::mSleepDuration
boost::chrono::microseconds mSleepDuration
Definition: ProtocolMmap.hpp:197
uhal::Mmap::mConnected
bool mConnected
Definition: ProtocolMmap.hpp:193
uhal::Mmap::File::mOffset
off_t mOffset
Definition: ProtocolMmap.hpp:117
uhal::Mmap::File::mMmapPtr
void * mMmapPtr
Definition: ProtocolMmap.hpp:118
uhal::Mmap::PacketFmt
Definition: ProtocolMmap.hpp:86
uhal::Mmap::mReadReplyPageCount
uint32_t mReadReplyPageCount
Definition: ProtocolMmap.hpp:199
uhal::Mmap::HexTo::operator>>
friend std::istream & operator>>(std::istream &in, HexTo &out)
Definition: ProtocolMmap.hpp:126
uhal::Mmap
Transport protocol to transfer an IPbus buffer via device file, using mmap.
Definition: ProtocolMmap.hpp:84
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::Mmap::File
Definition: ProtocolMmap.hpp:96
ClientInterface.hpp