Design Pattern Examples
Overview of object-oriented design patterns
Adapter_Exercise.c
Go to the documentation of this file.
1
5
6#include <stdio.h>
7
8#include "helpers/_countof.h"
9
10#include "Adapter_Exercise.h"
11
12#include "Adapter_Functions.h"
13
24// ! [Using Adapter in C]
26{
27 printf("\nAdapter_Exercise\n");
28
29 int dataHandle = -1;
30 if (Adapter_OpenMemory(Memory_Block_0, &dataHandle))
31 {
32 uint8_t writeData[16] = { 0 };
33 uint8_t readData[128] = { 0 };
34 int bufferOffset = 41;
35 uint32_t dataSize = _countof(writeData);
36 int bytesWritten = 0;
37 int bytesRead = 0;
38 int memoryBlockSize = 0;
39 const char* hexdump = NULL;
40
41 if (Adapter_GetMemorySize(dataHandle, &memoryBlockSize))
42 {
43 if (Adapter_ReadMemory(dataHandle, 0, readData, memoryBlockSize, &bytesRead))
44 {
45 hexdump = Adapter_BufferToString(readData, bytesRead, 2);
46 if (hexdump != NULL)
47 {
48 printf(" Initial memory block contents:\n");
49 printf("%s\n", hexdump);
50
51 // Create the data to be written
52 for (uint32_t index = 0; index < dataSize; index++)
53 {
54 writeData[index] = (uint8_t)(index + 1);
55 }
56
57 // Display the data to be written
58 hexdump = Adapter_BufferToString(writeData, dataSize, 2);
59 if (hexdump != NULL)
60 {
61 printf(" Data to be written to memory block:\n");
62 printf("%s\n", hexdump);
63
64 printf(" Writing data to byte offset %d\n", bufferOffset);
65 if (Adapter_WriteMemory(dataHandle, bufferOffset, writeData, dataSize, &bytesWritten))
66 {
67 printf(" Reading back the memory block...\n");
68 if (Adapter_ReadMemory(dataHandle, 0, readData, memoryBlockSize, &bytesRead))
69 {
70 hexdump = Adapter_BufferToString(readData, bytesRead, 2);
71 if (hexdump != NULL)
72 {
73 printf(" Current memory block contents:\n");
74 printf("%s\n", hexdump);
75 }
76 else
77 {
78 printf(" %s\n", Adapter_GetLastErrorMessage());
79 }
80 }
81 else
82 {
83 printf(" %s\n", Adapter_GetLastErrorMessage());
84 }
85 }
86 else
87 {
88 printf(" %s\n", Adapter_GetLastErrorMessage());
89 }
90 }
91 else
92 {
93 printf(" %s\n", Adapter_GetLastErrorMessage());
94 }
95 }
96 else
97 {
98 printf(" %s\n", Adapter_GetLastErrorMessage());
99}
100 }
101 else
102 {
103 printf(" %s\n", Adapter_GetLastErrorMessage());
104 }
105 }
106 else
107 {
108 printf(" %s\n", Adapter_GetLastErrorMessage());
109 }
110
111 if (!Adapter_CloseMemory(dataHandle))
112 {
113 printf(" %s\n", Adapter_GetLastErrorMessage());
114 }
115 }
116 else
117 {
118 printf(" %s\n", Adapter_GetLastErrorMessage());
119 }
120
121 printf(" Done.\n");
122}
123// ! [Using Adapter in C]
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.
#define _countof(w)