Design Pattern Examples
Overview of object-oriented design patterns
Adapter_FrontEndClass.cs
Go to the documentation of this file.
1
5
6using System;
7using System.Collections;
8using System.Text;
10
12{
17 internal class DataReaderWriterInitException : Exception
18 {
20 : base(msg)
21 {
22 }
23 }
24
25
30 internal class DataReaderWriterException : Exception
31 {
32 public DataReaderWriterException(string msg)
33 : base(msg)
34 {
35 }
36 }
37
38
43 internal class DataReaderWriter : IDisposable
44 {
50 {
51 Memory_Block_0 = 0,
52 Memory_Block_1 = 1,
53 Memory_Block_2 = 2,
54 };
55
56 private bool _disposed;
57 private bool _initialized;
58 private int _dataHandle;
60
65 {
66 get { return _memoryBlockByteSize; }
67 }
68
78 {
79 string blockName = "";
80
81 switch (blockNumber)
82 {
83 case MemoryBlockNumber.Memory_Block_0:
84 blockName = BLOCK_NAME_0;
85 break;
86
87 case MemoryBlockNumber.Memory_Block_1:
88 blockName = BLOCK_NAME_1;
89 break;
90
91 case MemoryBlockNumber.Memory_Block_2:
92 blockName = BLOCK_NAME_2;
93 break;
94
95 default:
96 break;
97 }
98
99 return blockName;
100 }
101
108 {
109 string message = "";
110
111 switch (errorCode)
112 {
113 case DataReadWriteFunctions.DDR_ErrorCode.DDR_ErrorCode_Success:
114 message = "Operation succeeded";
115 break;
116
117 case DataReadWriteFunctions.DDR_ErrorCode.DDR_ErrorCode_Block_Already_Opened:
118 message = "Memory block is already open and cannot be opened again";
119 break;
120
121 case DataReadWriteFunctions.DDR_ErrorCode.DDR_ErrorCode_Block_Not_Opened:
122 message = "Memory block is closed and cannot be accessed";
123 break;
124
125 case DataReadWriteFunctions.DDR_ErrorCode.DDR_ErrorCode_Invalid_Block_Name:
126 message = "The given name is not a recognized memory block name";
127 break;
128
129 case DataReadWriteFunctions.DDR_ErrorCode.DDR_ErrorCode_Invalid_Handle:
130 message = "The handle argument does not correspond to a valid open memory block";
131 break;
132
133 case DataReadWriteFunctions.DDR_ErrorCode.DDR_ErrorCode_Invalid_Offset:
134 message = "The given offset is out of bounds";
135 break;
136
137 case DataReadWriteFunctions.DDR_ErrorCode.DDR_ErrorCode_Null_Argument:
138 message = "The block name pointer or return handle pointer argument is NULL";
139 break;
140
141 default:
142 message = "Unrecognized error code.";
143 break;
144 }
145
146 return message;
147 }
148
158 string _ConstructErrorMessage(DDR_ErrorCode errorCode, string operation)
159 {
160 string msg = _GetErrorMessage(errorCode);
161 return String.Format("{0}: {1}", operation, msg);
162 }
163
164
172 {
173 _initialized = false;
174 _dataHandle = -1;
175 string blockName = _GetBlockNameForBlockNumber(blockNumber);
176 if (!String.IsNullOrEmpty(blockName))
177 {
180 if (errorCode == DDR_ErrorCode.DDR_ErrorCode_Success)
181 {
182 int memorySizeInChunks = 0;
183 errorCode = DataReadWriteFunctions.DDR_GetMemorySize(_dataHandle, out memorySizeInChunks);
184 if (errorCode != DDR_ErrorCode.DDR_ErrorCode_Success)
185 {
186 string msg = _ConstructErrorMessage(errorCode,
187 "Memory block not opened so cannot retrieve memory block size");
188 throw new DataReaderWriterInitException(msg);
189 }
190 _memoryBlockByteSize = (uint)memorySizeInChunks * sizeof(UInt32);
191 _initialized = true;
192 }
193 else
194 {
195 string msg = _ConstructErrorMessage(errorCode, "Initializing data reader/writer");
196 throw new DataReaderWriterInitException(msg);
197 }
198 }
199 }
200
201
211 public byte[] Read(int byteOffset, uint maxBytes)
212 {
213 if (!_initialized)
214 {
215 throw new DataReaderWriterInitException("Data reader/writer is not initialized.");
216 }
217
218 byte[] data = new byte[maxBytes];
219
220 if (maxBytes > 0)
221 {
222 int chunkOffset = byteOffset / (sizeof(UInt32));
223 UInt32 value = 0;
224 UInt32 bufferIndex = 0;
225 DDR_ErrorCode errorCode = DDR_ErrorCode.DDR_ErrorCode_Success;
226 errorCode = DDR_GetDataChunk(_dataHandle, chunkOffset, out value);
227 if (errorCode == DDR_ErrorCode.DDR_ErrorCode_Success)
228 {
229 int byteOffsetInChunk = byteOffset % (sizeof(UInt32));
230 while (bufferIndex < maxBytes)
231 {
232 data[bufferIndex] = (byte) (value & 0xff);
233 bufferIndex++;
234 value >>= 8;
235 byteOffsetInChunk++;
236 if (byteOffsetInChunk == sizeof(UInt32))
237 {
238 chunkOffset++;
239 if (chunkOffset >= DataReadWriteFunctions.DDR_MAX_OFFSET)
240 {
241 break;
242 }
243 byteOffsetInChunk = 0;
244 errorCode = DDR_GetDataChunk(_dataHandle, chunkOffset, out value);
245 if (errorCode != DDR_ErrorCode.DDR_ErrorCode_Success)
246 {
247 string msg = _ConstructErrorMessage(errorCode, "Reading memory");
248 throw new DataReaderWriterException(msg);
249 }
250 }
251 }
252 }
253 else
254 {
255 string msg = _ConstructErrorMessage(errorCode, "Reading memory");
256 throw new DataReaderWriterException(msg);
257 }
258 }
259
260 return data;
261 }
262
272 public void Write(int byteOffset, byte[] data, uint maxBytes)
273 {
274 if (!_initialized)
275 {
276 throw new DataReaderWriterInitException("Data reader/writer is not initialized.");
277 }
278 DDR_ErrorCode errorCode = DDR_ErrorCode.DDR_ErrorCode_Success;
279 int chunkOffset = byteOffset / sizeof(UInt32);
280 UInt32 value = 0;
281 int byteOffsetInChunk = byteOffset % sizeof(UInt32);
282 UInt32 bufferIndex = 0;
283 UInt32 byteMask = (UInt32)0xff << (byteOffsetInChunk * 8);
284 if (byteOffsetInChunk != 0)
285 {
286 errorCode = DDR_GetDataChunk(_dataHandle, chunkOffset, out value);
287 }
288 if (errorCode == DDR_ErrorCode.DDR_ErrorCode_Success)
289 {
290 while (bufferIndex < maxBytes)
291 {
292 value &= ~byteMask;
293 value |= ((UInt32)data[bufferIndex]) << (byteOffsetInChunk * 8);
294 bufferIndex++;
295 byteMask <<= 8;
296 byteOffsetInChunk++;
297 if (byteOffsetInChunk == sizeof(UInt32))
298 {
299 errorCode = DDR_SetDataChunk(_dataHandle, chunkOffset, value);
300 if (errorCode == DDR_ErrorCode.DDR_ErrorCode_Success)
301 {
302 byteMask = 0xff;
303 byteOffsetInChunk = 0;
304 chunkOffset++;
305 if (chunkOffset >= DDR_MAX_OFFSET)
306 {
307 break;
308 }
309 errorCode = DDR_GetDataChunk(_dataHandle, chunkOffset, out value);
310 if (errorCode != DDR_ErrorCode.DDR_ErrorCode_Success)
311 {
312 break;
313 }
314 }
315 else
316 {
317 string msg = _ConstructErrorMessage(errorCode, "Writing memory");
318 throw new DataReaderWriterException(msg);
319 }
320 }
321 }
322 if (errorCode == DDR_ErrorCode.DDR_ErrorCode_Success)
323 {
324 if (byteOffsetInChunk != 0)
325 {
326 errorCode = DDR_SetDataChunk(_dataHandle, chunkOffset, value);
327 }
328 }
329 if (errorCode != DDR_ErrorCode.DDR_ErrorCode_Success)
330 {
331 string msg = _ConstructErrorMessage(errorCode, "Writing memory");
332 throw new DataReaderWriterException(msg);
333 }
334 }
335 else
336 {
337 string msg = _ConstructErrorMessage(errorCode, "Reading memory in preparation to writing memory");
338 throw new DataReaderWriterException(msg);
339 }
340 }
341
342
352 public string BufferToString(byte[] data, uint maxBytes, int indent)
353 {
354 StringBuilder output = new StringBuilder();
355 string indentSpaces = new string(' ', indent);
356
357 if (data != null && maxBytes != 0)
358 {
359 int byteCount = (int)maxBytes;
360 if (byteCount > data.Length)
361 {
362 byteCount = data.Length;
363 }
364 int bytesPerRow = 32;
365 for (int row = 0; row < maxBytes; row += bytesPerRow)
366 {
367 output.AppendFormat("{0}{1:x4} --", indentSpaces, row);
368 for (int col = 0; col < bytesPerRow && (row + col) < maxBytes; ++col)
369 {
370 if (col > 0)
371 {
372 output.Append(' ');
373 }
374 output.AppendFormat("{0:x2}", data[row + col]);
375 }
376 output.Append(Environment.NewLine);
377 }
378 }
379 return output.ToString();
380 }
381
382
383#region IDisposable Members
384
389 public void Dispose()
390 {
391 if (!_disposed)
392 {
393 if (_initialized)
394 {
395 _initialized = false;
397 _dataHandle = -1;
398 if (errorCode != 0)
399 {
400 string msg = _ConstructErrorMessage(errorCode, "Shutting down data reader/writer");
401 throw new DataReaderWriterInitException(msg);
402 }
403 }
404 _disposed = true;
405 }
406 }
407
408#endregion
409 }
410}
const char * BLOCK_NAME_0
Name of the first block.
DDR_ErrorCode DDR_SetDataChunk(int dataHandle, int chunkOffset, uint32_t value)
Writes a single 32-bit value to the given offset in the memory block indicated by the specified handl...
const char * BLOCK_NAME_2
Name of the third block.
DDR_ErrorCode DDR_GetDataChunk(int dataHandle, int chunkOffset, uint32_t *value)
Read a single 32-bit value at the given offset in the memory block indicated by the specified handle.
const char * BLOCK_NAME_1
Name of the second block.
DDR_ErrorCode
Represents the possible errors that can be returned from the memory block access functions.
@ DDR_MAX_OFFSET
All offsets must from 0 to 1 less than this value.
Represents some P/Invoke functions for accessing a named blocks of memory to read/write data in the A...
DDR_ErrorCode
Represents the possible errors that can be returned from the memory block access functions....
static DDR_ErrorCode DDR_GetMemorySize(Int32 dataHandle, out Int32 memorySizeInChunks)
P/Invoke wrapper that retrieves the number of chunks in the memory block indicated by the handle to t...
static DDR_ErrorCode DDR_OpenMemoryBlock(string blockName, out Int32 dataHandle)
P/Invoke wrapper that opens access to a memory block for exclusive use, given the name of the memory ...
const int DDR_MAX_OFFSET
All offsets must from 0 to 1 less than this value.
static DDR_ErrorCode DDR_CloseMemoryBlock(Int32 dataHandle)
P/Invoke wrapper that closes access to a previously opened memory block, thus releasing it for others...
Represents an error that occurred when reading or writing data in the Data reader/writer.
Represents a data reader/writer to a caller.
string _ConstructErrorMessage(DDR_ErrorCode errorCode, string operation)
Creates a formatted error message from the given operation, using the error code from the Adapter_Bac...
string BufferToString(byte[] data, uint maxBytes, int indent)
Convert the specified data up to the specified number of bytes into a string by performing a "hex dum...
void Dispose()
Shut down the data reader/writer and dispose of resources.
void Write(int byteOffset, byte[] data, uint maxBytes)
Write a specified number of bytes.
string _GetErrorMessage(DDR_ErrorCode errorCode)
Convert the given error code to a string message.
uint MemoryBlockByteSize
Retrieve the size of the memory block in bytes.
byte[] Read(int byteOffset, uint maxBytes)
Read a specified number of bytes.
MemoryBlockNumber
Represents the memory blocks that can be accessed. Hides how memory blocks are actually identified.
DataReaderWriter(MemoryBlockNumber blockNumber)
Constructor for a data reader/writer.
string _GetBlockNameForBlockNumber(MemoryBlockNumber blockNumber)
Helper function to convert a value from the MemoryBlockNumber enumeration, which specifies the number...
Represents an error that occurred during initialization or shut down of the Data reader/writer.
The namespace containing all Design Pattern Examples implemented in C#.