Design Pattern Examples
Overview of object-oriented design patterns
Adapter_Functions.c File Reference

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"
Include dependency graph for Adapter_Functions.c:

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.
 

Detailed Description

Implementation of the Adapter functions used in the Adapter Pattern.

Definition in file Adapter_Functions.c.

Function Documentation

◆ _GetBlockNameForBlockNumber()

static const char * _GetBlockNameForBlockNumber ( MemoryBlockNumber  blockNumber)
static

Given a block number, retrieve the corresponding block name.

Parameters
blockNumberA value from the MemoryBlockNumber enumeration indicating the block of memory to open.
Returns
Returns a pointer to the block name if the block number is recognized; otherwise returns NULL.

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().

◆ _GetErrorMessage()

static const char * _GetErrorMessage ( DDR_ErrorCode  errorCode)
static

Convert the given error code to a string message.

Parameters
errorCodeA value from the DDR_ErrorCode enumeration.
Returns
A constant string describing the error.

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().

◆ _ReportDDRError()

static void _ReportDDRError ( DDR_ErrorCode  errorCode,
const char *  prompt 
)
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.

Parameters
errorCodeA value from the DDR_ErrorCode enumeration indicating the error that occurred.
promptA 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().

◆ _ReportErrorMessage()

static void _ReportErrorMessage ( const char *  message,
const char *  prompt 
)
static

Format the given message and prompt into a single error message string.

Parameters
messageThe error message to report. Cannot be NULL.
promptA 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().

◆ _ResetLastError()

static void _ResetLastError ( void  )
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().

◆ Adapter_BufferToString()

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.

Parameters
dataThe data to process.
maxBytesThe number of bytes from the data to process.
indentNumber of spaces to indent each line.
Returns
A string containing the data in the form of a hex dump, possibly multiple lines.

Definition at line 400 of file Adapter_Functions.c.

References _countof, _hexdump, and _ReportErrorMessage().

Referenced by Adapter_Exercise().

◆ Adapter_CloseMemory()

bool Adapter_CloseMemory ( int  dataHandle)

Closes a memory block from access.

Parameters
dataHandleThe handle returned from Adapter_OpenMemory() to be closed.
Returns
Returns true if all went well; otherwise, returns false, use AdapterGetLastErrorMessage() to get the reason for the failure.

Definition at line 173 of file Adapter_Functions.c.

References _ReportDDRError(), _ResetLastError(), DDR_CloseMemoryBlock(), and DDR_ErrorCode_Success.

Referenced by Adapter_Exercise().

◆ Adapter_GetLastErrorMessage()

const char * Adapter_GetLastErrorMessage ( void  )

Retrieve a string describing the last error that occurred in the Adapter.

Returns
A string containing an error message. Remains valid until the next Adapter operation that is called.

Definition at line 391 of file Adapter_Functions.c.

References _lastError.

Referenced by Adapter_Exercise().

◆ Adapter_GetMemorySize()

bool Adapter_GetMemorySize ( int  dataHandle,
int *  sizeInBytes 
)

Retrieve the number of bytes in the memory block associated with the specified data handle.

Parameters
dataHandleThe handle returned from Adapter_OpenMemory() to access.
sizeInBytesReturns the number of bytes available to access.
Returns
Returns true if all went well; otherwise, returns false, use AdapterGetLastErrorMessage() to get the reason for the failure.

Definition at line 191 of file Adapter_Functions.c.

References _ReportDDRError(), _ReportErrorMessage(), _ResetLastError(), DDR_ErrorCode_Success, and DDR_GetMemorySize().

Referenced by Adapter_Exercise().

◆ Adapter_OpenMemory()

bool Adapter_OpenMemory ( MemoryBlockNumber  blockNumber,
int *  dataHandle 
)

Open a memory block for access.

Parameters
blockNumberA value from the MemoryBlockNumber enumeration indicating which memory block to access.
dataHandleReturns the handle to the memory block to access.
Returns
Returns true if all went well; otherwise, returns false, use AdapterGetLastErrorMessage() to get the reason for the failure.

Definition at line 142 of file Adapter_Functions.c.

References _GetBlockNameForBlockNumber(), _ReportDDRError(), _ReportErrorMessage(), _ResetLastError(), DDR_ErrorCode_Success, and DDR_OpenMemoryBlock().

Referenced by Adapter_Exercise().

◆ Adapter_ReadMemory()

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.

Parameters
dataHandleThe handle returned from Adapter_OpenMemory() to access.
byteOffsetByte offset into the memory block from which to start reading.
bufferBuffer to fill with bytes from the memory block.
maxBytesThe number of bytes to read.
bytesReadif not NULL, returns the number of bytes actually read.
Returns
Returns true if all went well; otherwise, returns false, use AdapterGetLastErrorMessage() to get the reason for the failure.

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().

◆ Adapter_WriteMemory()

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.

Parameters
dataHandleThe handle returned from Adapter_OpenMemory() to access.
byteOffsetByte offset into the memory block to which to start writing.
bufferBuffer of bytes to write to the memory block.
maxBytesThe number of bytes to write.
bytesWrittenIf not NULL, returns the number of bytes actually written.
Returns
Returns true if all went well; otherwise, returns false, use AdapterGetLastErrorMessage() to get the reason for the failure.

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().

Variable Documentation

◆ _hexdump

char _hexdump[512] = { 0 }
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().

◆ _lastError

char _lastError[128] = { 0 }
static

Holds last error message.

Definition at line 15 of file Adapter_Functions.c.

Referenced by _ReportErrorMessage(), _ResetLastError(), and Adapter_GetLastErrorMessage().