8#include "helpers/_countof.h"
27 printf(
"\nAdapter_Exercise\n");
32 uint8_t writeData[16] = { 0 };
33 uint8_t readData[128] = { 0 };
34 int bufferOffset = 41;
35 uint32_t dataSize =
_countof(writeData);
38 int memoryBlockSize = 0;
39 const char* hexdump = NULL;
48 printf(
" Initial memory block contents:\n");
49 printf(
"%s\n", hexdump);
52 for (uint32_t index = 0; index < dataSize; index++)
54 writeData[index] = (uint8_t)(index + 1);
61 printf(
" Data to be written to memory block:\n");
62 printf(
"%s\n", hexdump);
64 printf(
" Writing data to byte offset %d\n", bufferOffset);
67 printf(
" Reading back the memory block...\n");
73 printf(
" Current memory block contents:\n");
74 printf(
"%s\n", hexdump);
void Adapter_Exercise(void)
Example of using the Adapter design pattern in C.
bool Adapter_CloseMemory(int dataHandle)
Closes a memory block from access.
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_OpenMemory(MemoryBlockNumber blockNumber, int *dataHandle)
Open a memory block for access.
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.
bool Adapter_GetMemorySize(int dataHandle, int *sizeInBytes)
Retrieve the number of bytes in the memory block associated with the specified data handle.
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 dum...
const char * Adapter_GetLastErrorMessage(void)
Retrieve a string describing the last error that occurred in the Adapter.
Declaration of the Adapter functions used in the Adapter Pattern.
@ Memory_Block_0
First block.
Declaration of the Adapter_Exercise() function as used in the Adapter Pattern.