Implementation of the Adapter functions used in the Adapter Pattern. More...
#include <stdio.h>
#include <string.h>
#include <Adapter_BackEnd.h>
#include "helpers/_countof.h"
#include "Adapter_Functions.h"
Go to the source code of this file.
Functions | |
static const char * | _GetBlockNameForBlockNumber (MemoryBlockNumber blockNumber) |
Given a block number, retrieve the corresponding block name. | |
static void | _ResetLastError (void) |
Set the last error message to an empty string. Call this before doing any operation. | |
static const char * | _GetErrorMessage (DDR_ErrorCode errorCode) |
Convert the given error code to a string message. | |
static void | _ReportErrorMessage (const char *message, const char *prompt) |
Format the given message and prompt into a single error message string. | |
static void | _ReportDDRError (DDR_ErrorCode errorCode, const char *prompt) |
Create a human-readable error message for the given error code, adding a prompt to provide some context to the error. Use the GetLastErrorMessage() function to retrieve the string. | |
bool | Adapter_OpenMemory (MemoryBlockNumber blockNumber, int *dataHandle) |
Open a memory block for access. | |
bool | Adapter_CloseMemory (int dataHandle) |
Closes a memory block from access. | |
bool | Adapter_GetMemorySize (int dataHandle, int *sizeInBytes) |
Retrieve the number of bytes in the memory block associated with the specified data handle. | |
bool | Adapter_ReadMemory (int dataHandle, int byteOffset, uint8_t *buffer, int maxBytes, int *bytesRead) |
Read a requested number of bytes from the memory block associated with the given handle. | |
bool | Adapter_WriteMemory (int dataHandle, int byteOffset, const uint8_t *buffer, int maxBytes, int *bytesWritten) |
Write a requested number of bytes to the memory block associated with the given handle. | |
const char * | Adapter_GetLastErrorMessage (void) |
Retrieve a string describing the last error that occurred in the Adapter. | |
const char * | Adapter_BufferToString (const uint8_t *data, uint32_t maxBytes, int indent) |
Convert the specified data up to the specified number of bytes into a string by performing a "hex dump" on the data. | |
Variables | |
static char | _lastError [128] = { 0 } |
Holds last error message. | |
static char | _hexdump [512] = { 0 } |
Collects hex dump as a single string. We do not expect more than about 424 bytes per hex dump. | |
Implementation of the Adapter functions used in the Adapter Pattern.
Definition in file Adapter_Functions.c.
|
static |
Given a block number, retrieve the corresponding block name.
blockNumber | A value from the MemoryBlockNumber enumeration indicating the block of memory to open. |
Definition at line 25 of file Adapter_Functions.c.
References BLOCK_NAME_0, BLOCK_NAME_1, BLOCK_NAME_2, Memory_Block_0, Memory_Block_1, and Memory_Block_2.
Referenced by Adapter_OpenMemory().
|
static |
Convert the given error code to a string message.
errorCode | A value from the DDR_ErrorCode enumeration. |
Definition at line 64 of file Adapter_Functions.c.
References DDR_ErrorCode_Block_Already_Opened, DDR_ErrorCode_Block_Not_Opened, DDR_ErrorCode_Invalid_Block_Name, DDR_ErrorCode_Invalid_Handle, DDR_ErrorCode_Invalid_Offset, DDR_ErrorCode_Null_Argument, and DDR_ErrorCode_Success.
Referenced by _ReportDDRError().
|
static |
Create a human-readable error message for the given error code, adding a prompt to provide some context to the error. Use the GetLastErrorMessage() function to retrieve the string.
errorCode | A value from the DDR_ErrorCode enumeration indicating the error that occurred. |
prompt | A prompt that indicates the context in which the error occurred. |
Definition at line 127 of file Adapter_Functions.c.
References _GetErrorMessage(), and _ReportErrorMessage().
Referenced by Adapter_CloseMemory(), Adapter_GetMemorySize(), Adapter_OpenMemory(), Adapter_ReadMemory(), and Adapter_WriteMemory().
|
static |
Format the given message and prompt into a single error message string.
message | The error message to report. Cannot be NULL. |
prompt | A prompt that indicates the context in which the error occurred. Cannot be NULL. |
Definition at line 112 of file Adapter_Functions.c.
References _countof, and _lastError.
Referenced by _ReportDDRError(), Adapter_BufferToString(), Adapter_GetMemorySize(), Adapter_OpenMemory(), Adapter_ReadMemory(), and Adapter_WriteMemory().
|
static |
Set the last error message to an empty string. Call this before doing any operation.
Definition at line 54 of file Adapter_Functions.c.
References _lastError.
Referenced by Adapter_CloseMemory(), Adapter_GetMemorySize(), Adapter_OpenMemory(), Adapter_ReadMemory(), and Adapter_WriteMemory().
const char * Adapter_BufferToString | ( | const uint8_t * | data, |
uint32_t | maxBytes, | ||
int | indent | ||
) |
Convert the specified data up to the specified number of bytes into a string by performing a "hex dump" on the data.
data | The data to process. |
maxBytes | The number of bytes from the data to process. |
indent | Number of spaces to indent each line. |
Definition at line 400 of file Adapter_Functions.c.
References _countof, _hexdump, and _ReportErrorMessage().
Referenced by Adapter_Exercise().
bool Adapter_CloseMemory | ( | int | dataHandle | ) |
Closes a memory block from access.
dataHandle | The handle returned from Adapter_OpenMemory() to be closed. |
Definition at line 173 of file Adapter_Functions.c.
References _ReportDDRError(), _ResetLastError(), DDR_CloseMemoryBlock(), and DDR_ErrorCode_Success.
Referenced by Adapter_Exercise().
const char * Adapter_GetLastErrorMessage | ( | void | ) |
Retrieve a string describing the last error that occurred in the Adapter.
Definition at line 391 of file Adapter_Functions.c.
References _lastError.
Referenced by Adapter_Exercise().
bool Adapter_GetMemorySize | ( | int | dataHandle, |
int * | sizeInBytes | ||
) |
Retrieve the number of bytes in the memory block associated with the specified data handle.
dataHandle | The handle returned from Adapter_OpenMemory() to access. |
sizeInBytes | Returns the number of bytes available to access. |
Definition at line 191 of file Adapter_Functions.c.
References _ReportDDRError(), _ReportErrorMessage(), _ResetLastError(), DDR_ErrorCode_Success, and DDR_GetMemorySize().
Referenced by Adapter_Exercise().
bool Adapter_OpenMemory | ( | MemoryBlockNumber | blockNumber, |
int * | dataHandle | ||
) |
Open a memory block for access.
blockNumber | A value from the MemoryBlockNumber enumeration indicating which memory block to access. |
dataHandle | Returns the handle to the memory block to access. |
Definition at line 142 of file Adapter_Functions.c.
References _GetBlockNameForBlockNumber(), _ReportDDRError(), _ReportErrorMessage(), _ResetLastError(), DDR_ErrorCode_Success, and DDR_OpenMemoryBlock().
Referenced by Adapter_Exercise().
bool Adapter_ReadMemory | ( | int | dataHandle, |
int | byteOffset, | ||
uint8_t * | buffer, | ||
int | maxBytes, | ||
int * | bytesRead | ||
) |
Read a requested number of bytes from the memory block associated with the given handle.
dataHandle | The handle returned from Adapter_OpenMemory() to access. |
byteOffset | Byte offset into the memory block from which to start reading. |
buffer | Buffer to fill with bytes from the memory block. |
maxBytes | The number of bytes to read. |
bytesRead | if not NULL, returns the number of bytes actually read. |
Definition at line 223 of file Adapter_Functions.c.
References _ReportDDRError(), _ReportErrorMessage(), _ResetLastError(), DDR_ErrorCode_Success, DDR_GetDataChunk(), and DDR_MAX_OFFSET.
Referenced by Adapter_Exercise().
bool Adapter_WriteMemory | ( | int | dataHandle, |
int | byteOffset, | ||
const uint8_t * | buffer, | ||
int | maxBytes, | ||
int * | bytesWritten | ||
) |
Write a requested number of bytes to the memory block associated with the given handle.
dataHandle | The handle returned from Adapter_OpenMemory() to access. |
byteOffset | Byte offset into the memory block to which to start writing. |
buffer | Buffer of bytes to write to the memory block. |
maxBytes | The number of bytes to write. |
bytesWritten | If not NULL, returns the number of bytes actually written. |
Definition at line 294 of file Adapter_Functions.c.
References _ReportDDRError(), _ReportErrorMessage(), _ResetLastError(), DDR_ErrorCode_Success, DDR_GetDataChunk(), DDR_MAX_OFFSET, and DDR_SetDataChunk().
Referenced by Adapter_Exercise().
|
static |
Collects hex dump as a single string. We do not expect more than about 424 bytes per hex dump.
Definition at line 16 of file Adapter_Functions.c.
Referenced by Adapter_BufferToString().
|
static |
Holds last error message.
Definition at line 15 of file Adapter_Functions.c.
Referenced by _ReportErrorMessage(), _ResetLastError(), and Adapter_GetLastErrorMessage().