Design Pattern Examples
Overview of object-oriented design patterns
Facade_ComplexSystem.c
Go to the documentation of this file.
1
5
6#include <stdio.h>
7#include "Facade_Interface.h"
9
14typedef struct
15{
19 const char* Name;
20
24 uint32_t Idcode;
25
31
35 bool Visible;
37
38
39//#############################################################################
40//#############################################################################
41
45typedef struct
46{
47 const char* Name;
48 bool IsLocked;
50 size_t numNodes;
52
53
54//-----------------------------------------------------------------------------
55//-----------------------------------------------------------------------------
56
57
58
59//#############################################################################
60//#############################################################################
61
62
68{
69 { "DDD_DEVCTRL0", 0x10101010, DEVICECONTROLLER, true },
70 { "DDD_CORE0" , 0x20202020, CORE , false },
71 { "DDD_GTE0" , 0x30303030, GTE , false },
72};
73
79{
80 { "DDD_DEVCTRL1", 0x10101011, DEVICECONTROLLER, true },
81 { "DDD_PCH0" , 0x40404040, PCH , false },
82 { "DDD_PMC0" , 0x50505050, PMC , false },
83};
84
90 { "CHAIN0", false, deviceChain0, sizeof(deviceChain0) / sizeof(deviceChain0[0]) },
91 { "CHAIN1", false, deviceChain1, sizeof(deviceChain1) / sizeof(deviceChain0[1]) }
92};
93
97static int deviceChainCount = (int)(sizeof(deviceChains) / sizeof(deviceChains[0]));
98
99
100//#############################################################################
101//#############################################################################
102
112static void DeviceChain_ShowHideNodes(int chainIndex, uint32_t nodeSelectMask, bool makeVisible)
113{
114 if (chainIndex >= 0 && chainIndex < deviceChainCount)
115 {
116 // bit 0 is always the DEVICECONTROLLER and is always selected so skip
117 // it in the mask
118 uint32_t bitMask = 0x2;
119 size_t numNodes = deviceChains[chainIndex].numNodes;
120 DeviceNode* nodes = deviceChains[chainIndex].nodes;
121
122 // Start at the device after the DEVICECONTROLLER
123 for (size_t index = 1; index < numNodes; ++index)
124 {
125 if ((bitMask & nodeSelectMask) != 0)
126 {
127 nodes[index].Visible = makeVisible;
128 }
129 bitMask <<= 1;
130 if (bitMask == 0)
131 {
132 // We don't allow more than 32 devices
133 break;
134 }
135 }
136 }
137}
138
144static void DeviceChain_ResetVisibility(int chainIndex)
145{
146 if (chainIndex >= 0 && chainIndex < deviceChainCount)
147 {
148 size_t numNodes = deviceChains[chainIndex].numNodes;
149 DeviceNode* nodes = deviceChains[chainIndex].nodes;
150 for (size_t index = 0; index < numNodes; index++)
151 {
152 if (nodes[index].DeviceType != DEVICECONTROLLER)
153 {
154 nodes[index].Visible = false;
155 }
156 }
157 }
158}
159
167static void DeviceChain_SelectNodes(int chainIndex, uint32_t nodeSelectMask)
168{
169 DeviceChain_ShowHideNodes(chainIndex, nodeSelectMask, true);
170}
171
179static void DeviceChain_DeselectNodes(int chainIndex, uint32_t nodeSelectMask)
180{
181 DeviceChain_ShowHideNodes(chainIndex, nodeSelectMask, false);
182}
183
184
194static void DeviceChain_GetIdCodesForVisibleNodes(int chainIndex, UIntArray* idcodes)
195{
196 if (chainIndex >= 0 && chainIndex < deviceChainCount)
197 {
198 if (idcodes != NULL)
199 {
200 size_t numNodes = deviceChains[chainIndex].numNodes;
201 DeviceNode* nodes = deviceChains[chainIndex].nodes;
202 for (size_t index = 0; index < numNodes; index++)
203 {
204 if (nodes[index].Visible)
205 {
206 if (!UIntArray_AddInt(idcodes, nodes[index].Idcode))
207 {
208 printf(" Error! Out of memory condition adding idcode to list of idcodes in DeviceChain_GetIdCodesForVisibleNodes()!\n");
209 break;
210 }
211 }
212 }
213 }
214 }
215}
216
217//-----------------------------------------------------------------------------
218//-----------------------------------------------------------------------------
219
220
221//====================================================================
222// IDeviceNetworkLowLevel methods
223//====================================================================
224
229static int GetNumChains(void)
230{
231 return (int)deviceChainCount;
232}
233
241static bool LockDeviceChain(int chainIndex)
242{
243 bool IsLocked = false;
244
245 if (chainIndex >= 0 && chainIndex < deviceChainCount)
246 {
247 if (!deviceChains[chainIndex].IsLocked)
248 {
249 deviceChains[chainIndex].IsLocked = true;
250 IsLocked = true;
251 }
252 }
253
254 return IsLocked;
255}
256
264static bool UnlockDeviceChain(int chainIndex)
265{
266 bool unlocked = false;
267
268 if (chainIndex >= 0 && chainIndex < deviceChainCount)
269 {
270 if (deviceChains[chainIndex].IsLocked)
271 {
272 deviceChains[chainIndex].IsLocked = false;
273 unlocked = true;
274 }
275 }
276
277 return unlocked;
278}
279
285static void ResetDeviceChain(int chainIndex)
286{
287 if (chainIndex >= 0 && chainIndex < deviceChainCount)
288 {
289 DeviceChain_ResetVisibility(chainIndex);
290 }
291}
292
302static void EnableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask)
303{
304 if (chainIndex >= 0 && chainIndex < deviceChainCount)
305 {
306 DeviceChain_SelectNodes(chainIndex, deviceselectMask);
307 }
308}
309
319static void DisableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask)
320{
321 if (chainIndex >= 0 && chainIndex < deviceChainCount)
322 {
323 DeviceChain_DeselectNodes(chainIndex, deviceselectMask);
324 }
325}
326
335static void GetIdcodes(int chainIndex, UIntArray* idcodes)
336{
337 if (chainIndex >= 0 && chainIndex < deviceChainCount)
338 {
339 DeviceChain_GetIdCodesForVisibleNodes(chainIndex, idcodes);
340 }
341}
342
343
344//#############################################################################
345//#############################################################################
346
356{
364};
365
366//-----------------------------------------------------------------------------
367
369{
370 return &lowlevelService;
371}
static void DeviceChain_GetIdCodesForVisibleNodes(int chainIndex, UIntArray *idcodes)
Retrieve a list of idcodes for all devices that are visible in the device chain.
static void DisableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask)
Deselect one or more devices in the given device chain so those devices are no longer visible.
static bool LockDeviceChain(int chainIndex)
Lock the specified device chain to indicate exclusive access is desired.
static DeviceNode deviceChain0[]
Device Chain 0, with 3 devices. The first device is always the device controller, which is always vis...
static void ResetDeviceChain(int chainIndex)
Reset the visibility of all devices on the given device chain so that all devices except the first ar...
static void DeviceChain_ResetVisibility(int chainIndex)
Resets the specified device chain so that all devices that are not device controllers are no longer v...
static void GetIdcodes(int chainIndex, UIntArray *idcodes)
Retrieve a list of idcodes of all visible devices in the given device chain.
static bool UnlockDeviceChain(int chainIndex)
Unlock the specified device chain to indicate exclusive access is no longer desired.
static int deviceChainCount
Number of device chains that have been pre-defined.
static DeviceNode deviceChain1[]
Device Chain 1, with 3 devices. The first device is always the device controller, which is always vis...
static DeviceChain deviceChains[]
Device chains. There are two device chains, which are accessed separately. Each chain has its own loc...
IDeviceNetworkLowLevel * Facade_GetLowLevelDeviceService(void)
Retrieve a set of function pointers to the low-level device service used in the Facade Pattern exampl...
IDeviceNetworkLowLevel lowlevelService
Definition of the IDeviceNetworkLowLevel interface, using function pointers to each function.
static int GetNumChains(void)
Retrieve the number of device chains.
static void DeviceChain_DeselectNodes(int chainIndex, uint32_t nodeSelectMask)
Make invisible one or more devices in the device chain.
static void EnableDevicesInDeviceChain(int chainIndex, uint32_t deviceselectMask)
Select one or more devices in the given device chain so those devices are visible.
static void DeviceChain_SelectNodes(int chainIndex, uint32_t nodeSelectMask)
Make visible one or more devices in the device chain.
static void DeviceChain_ShowHideNodes(int chainIndex, uint32_t nodeSelectMask, bool makeVisible)
Helper function to show or hide devices on the device chain.
Declaration of the IDeviceNetworkLowLevel interface representing the complex system for the Facade Pa...
DeviceTypes
Identifies the type of devices that can appear in a device chain. Part of the Facade Pattern example.
@ DEVICECONTROLLER
device controller. This is always visible.
@ CORE
Core device.
@ GTE
GTE device.
@ PMC
PMC device.
@ PCH
PCH device.
Declaration of the IDeviceNetworkHighLevel interface representing the high-level system used in the F...
Represents a single device chain, a collection of DeviceNode objects.
bool IsLocked
true if device chain is locked; otherwise, false
const char * Name
Name of device chain.
size_t numNodes
Number of nodes in the nodes field.
DeviceNode * nodes
List of nodes in this device chain.
Represents a single device. Part of the Facade Pattern example.
DeviceTypes DeviceType
A value from the DeviceTypes enumeration identifying the type of the device.
const char * Name
Name of this device.
bool Visible
Whether the device is visible in the device chain.
uint32_t Idcode
The idcode for this device.
Represents a network of device chains and the low level access to that network. In general,...
Represents an array of 32-bit unsigned integers. The data field points to a block of memory allocated...
Definition: uintarray.h:24
bool UIntArray_AddInt(UIntArray *array, uint32_t value)
Add an unsigned 32-bit integer to the given UIntArray object.
Definition: uintarray.c:38