]> git.proxmox.com Git - mirror_edk2.git/blame - Vlv2TbltDevicePkg/PlatformDxe/Observable/Observable.c
Vlv2TbltDevicePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / Vlv2TbltDevicePkg / PlatformDxe / Observable / Observable.c
CommitLineData
3cbfba02
DW
1/*++\r
2 This file contains 'Framework Code' and is licensed as such\r
3 under the terms of your license agreement with Intel or your\r
4 vendor. This file may not be modified, except as allowed by\r
5 additional terms of your license agreement.\r
6--*/\r
7/*++\r
8\r
9Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved\r
10 \r\r
9dc8036d
MK
11 SPDX-License-Identifier: BSD-2-Clause-Patent\r
12\r
3cbfba02
DW
13 \r\r
14\r
15\r
16Module Name:\r
17\r
18 Observable.c\r
19\r
20Abstract:\r
21\r
22 The following contains all of the implementation for the Observable protocol. The\r
23 protocol uses the observer design pattern to provide a way to publish events and\r
24 to subscribe to those events so that a callback will be performed at the time of\r
25 the event. The observables and subscribers are maintained by the static tree,\r
26 mObservableDb. The difference between this protocol and the existing event protocol\r
27 that exists within the EFI framework is that this protocol allows for parameters\r
28 to be passed to the subscribed callbacks that can contain up to date context.\r
29\r
30--*/\r
31\r
32#include "Observable.h"\r
33\r
34static OBS_TREE* mObservableDb = NULL;\r
35static EFI_HANDLE mObservableHandle = NULL;\r
36static OBS_OBSERVABLE_PROTOCOL mObservable = {\r
37 AddObservable,\r
38 RemoveObservable,\r
39 Subscribe,\r
40 Unsubscribe,\r
41 Publish,\r
42 RemoveAllObservables\r
43};\r
44\r
45/** Install observable protocol.\r
46 *\r
47 * Install interface and initialize the observable protocol.\r
48 *\r
49 * @param VOID No parameters.\r
50 *\r
51 * @return EFI_SUCCESS Successfully installed and initialized the protocol.\r
52 **/\r
53EFI_STATUS\r
54InitializeObservableProtocol(\r
55 VOID\r
56 )\r
57{\r
58 EFI_STATUS Status;\r
59\r
60 //\r
61 // Install protocol.\r
62 //\r
63 Status = gBS->InstallProtocolInterface (\r
64 &mObservableHandle,\r
65 &gObservableProtocolGuid,\r
66 EFI_NATIVE_INTERFACE,\r
67 &mObservable\r
68 );\r
69\r
70 return Status;\r
71}\r
72\r
73/** Deletes a subscriber\r
74 *\r
75 * This function removes the subscriber pointed to by Head.\r
76 *\r
77 * @param OBS_TREE* Head Points to the current subscriber.\r
78 *\r
79 * @return OBS_TREE* Returns the tree after successfully removing the subscriber.\r
80 **/\r
81OBS_LEAF*\r
82DeleteSubscriber(\r
83 OBS_LEAF* Head\r
84 )\r
85{\r
86 OBS_LEAF* Temp;\r
87\r
88 if (Head) {\r
89 Temp = Head;\r
90 Head = Head->Next;\r
91 gBS->FreePool(Temp);\r
92 }\r
93\r
94 return Head;\r
95}\r
96\r
97/** Finds and deletes all subscribers\r
98 *\r
99 * This function iterates recursively through the existing subscribers and delets them all.\r
100 *\r
101 * @param OBS_TREE* Head Points to the current subscriber.\r
102 *\r
103 * @return OBS_TREE* Returns the tree after successfully removing the subscribers.\r
104 **/\r
105OBS_LEAF*\r
106DeleteAllSubscribers(\r
107 OBS_LEAF* Head\r
108 )\r
109{\r
110 if (Head) {\r
111 if (Head->Next) {\r
112 //\r
113 // We aren't at the end of the list yet.\r
114 //\r
115 Head->Next = DeleteAllSubscribers(Head->Next);\r
116 }\r
117\r
118 //\r
119 // At the end, so delete the subscriber.\r
120 //\r
121 Head = DeleteSubscriber(Head);\r
122 }\r
123\r
124 return Head;\r
125}\r
126\r
127/** Deletes an observable\r
128 *\r
129 * This function removes the observable pointed to by Head.\r
130 *\r
131 * @param OBS_TREE* Head Points to the current observable.\r
132 *\r
133 * @return OBS_TREE* Returns the tree after successfully removing the observable.\r
134 **/\r
135OBS_TREE*\r
136DeleteObservable(\r
137 OBS_TREE* Head\r
138 )\r
139{\r
140 OBS_TREE* Temp;\r
141\r
142 if (Head) {\r
143 Temp = Head;\r
144 Head = Head->Next;\r
145 gBS->FreePool(Temp);\r
146 }\r
147\r
148 return Head;\r
149}\r
150\r
151/** Finds and deletes all observables\r
152 *\r
153 * This function iterates recursively through the existing observables database and, starting with\r
154 * the last most observable, deletes all of its subscribers, then deletes the observable itself.\r
155 *\r
156 * @param OBS_TREE* Head Points to the current observable.\r
157 *\r
158 * @return OBS_TREE* Returns the tree after successfully removing the observables.\r
159 **/\r
160OBS_TREE*\r
161DeleteAllObservables(\r
162 OBS_TREE* Head\r
163 )\r
164{\r
165 if (Head) {\r
166 if (Head->Next) {\r
167 //\r
168 // We aren't at the end of the list yet.\r
169 //\r
170 Head->Next = DeleteAllObservables(Head->Next);\r
171 }\r
172\r
173 //\r
174 // This is the end of the list of observables.\r
175 //\r
176 Head->Leaf = DeleteAllSubscribers(Head->Leaf);\r
177\r
178 //\r
179 // Subscribers are deleted, so now delete the observable.\r
180 //\r
181 Head = DeleteObservable(Head);\r
182 }\r
183\r
184 return Head;\r
185}\r
186\r
187/** Finds and deletes observable\r
188 *\r
189 * This function iterates recursively through the existing observable database in order to find the one\r
190 * specified by ReferenceGuid so that it can be deleted. If the requested observable is found, before it\r
191 * is deleted, all of the subscribers that are listening to this observable are deleted.\r
192 *\r
193 * @param OBS_TREE* Head Points to the current observable.\r
194 * EFI_GUID ReferenceGuid Corresponds to the observable that we're looking for.\r
195 *\r
196 * @return OBS_TREE* Returns the tree after successfully removing (or not finding) the observable.\r
197 **/\r
198OBS_TREE*\r
199FindAndDeleteObservable(\r
200 OBS_TREE* Head,\r
201 EFI_GUID ReferenceGuid\r
202 )\r
203{\r
204 if (Head) {\r
205 if (CompareMem(&(Head->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
206 //\r
207 // We found the observable. Delete all of it's subscribers, first.\r
208 //\r
209 Head->Leaf = DeleteAllSubscribers(Head->Leaf);\r
210 //\r
211 // Now we can safely remove the observable.\r
212 //\r
213 Head = DeleteObservable(Head);\r
214 } else {\r
215 //\r
216 // Not found. Keep searching.\r
217 //\r
218 Head->Next = FindAndDeleteObservable(Head->Next, ReferenceGuid);\r
219 }\r
220 }\r
221\r
222 return Head;\r
223}\r
224\r
225/** Finds and deletes subscriber\r
226 *\r
227 * This function iterates recursively through the existing subscribers that are listening to the\r
228 * observable that was found when this function was called.\r
229 *\r
230 * @param OBS_TREE* Head Points to the current subscriber.\r
231 * OBS_CALLBACK CallbackInterface This is the subscriber that is requested be removed.\r
232 *\r
233 * @return OBS_TREE* Returns the tree after successfully removing (or not finding) the subscriber.\r
234 **/\r
235OBS_LEAF*\r
236_FindAndDeleteSubscriber(\r
237 OBS_LEAF* Head,\r
238 OBS_CALLBACK CallbackInterface\r
239 )\r
240{\r
241 if (Head) {\r
242 if (Head->Observer == CallbackInterface) {\r
243 //\r
244 // Found it. Now let's delete it.\r
245 //\r
246 Head = DeleteSubscriber(Head);\r
247 } else {\r
248 //\r
249 // Not found. Keep searching.\r
250 //\r
251 Head->Next = _FindAndDeleteSubscriber(Head->Next, CallbackInterface);\r
252 }\r
253 }\r
254\r
255 return Head;\r
256}\r
257\r
258/** Finds and deletes subscriber\r
259 *\r
260 * This function iterates recursively through the existing observables database until it either finds\r
261 * a matching guid or reaches the end of the list. After finding a match, it calls a helper function,\r
262 * _FindAndDeleteSubscriber. At this point, all responsibility for finding and deleting the subscriber\r
263 * lies on the helper function.\r
264 *\r
265 * @param OBS_TREE* Head Points to the current observable.\r
266 * EFI_GUID ReferenceGuid Corresponds to the observable that we're looking for.\r
267 * OBS_CALLBACK CallbackInterface This is the subscriber that is requested be removed.\r
268 *\r
269 * @return OBS_TREE* Returns the tree after successfully removing (or not finding) the subscriber.\r
270 **/\r
271OBS_TREE*\r
272FindAndDeleteSubscriber(\r
273 IN OUT OBS_TREE* Head,\r
274 IN EFI_GUID ReferenceGuid,\r
275 IN OBS_CALLBACK CallbackInterface\r
276 )\r
277{\r
278 if (Head) {\r
279 if (CompareMem(&(Head->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
280 //\r
281 // We found the observer that matches ReferenceGuid. Find and delete the subscriber that is\r
282 // listening to it.\r
283 //\r
284 Head->Leaf = _FindAndDeleteSubscriber(Head->Leaf, CallbackInterface);\r
285 } else {\r
286 //\r
287 // Not found. Keep searching.\r
288 //\r
289 Head->Next = FindAndDeleteSubscriber(Head->Next, ReferenceGuid, CallbackInterface);\r
290 }\r
291 }\r
292\r
293 return Head;\r
294}\r
295\r
296/** Remove all observables.\r
297 *\r
298 * Remove all observable guids and all interfaces subscribed to them.\r
299 *\r
300 * @param VOID No parameters.\r
301 *\r
302 * @return EFI_SUCCESS Successfully removed all observables and subscribed interfaces.\r
303 **/\r
304EFI_STATUS\r
305EFIAPI\r
306RemoveAllObservables(\r
307 VOID\r
308 )\r
309{\r
310 mObservableDb = DeleteAllObservables(mObservableDb);\r
311\r
312 return EFI_SUCCESS;\r
313}\r
314\r
315/** Subscribe an interface with an observable guid.\r
316 *\r
317 * Use this to register a callback function with a guid. The function provided by CallbackInterface will be executed\r
318 * whenever the appropriate observable instance specified by ReferenceGuid calls Publish.\r
319 *\r
320 * @param EFI_GUID ReferenceGuid The observable guid that the callback interface will subscribe to.\r
321 * OBS_CASLLBACK CallbackInterface A pointer to the function that is subscribing to the observable.\r
322 *\r
323 * @return EFI_SUCCESS Successfully subscribed the interface to the observable guid.\r
324 * EFI_NOT_FOUND No match could be found between the provided guid and existing observables.\r
325 * EFI_OUT_OF_RESOURCES Could not subscribe to this observer due to resource limitations.\r
326 * EFI_INVALID_PARAMETER Interface is already subscribed to this observer.\r
327 **/\r
328EFI_STATUS\r
329EFIAPI\r
330Subscribe (\r
331 IN EFI_GUID ReferenceGuid,\r
332 IN OBS_CALLBACK CallbackInterface\r
333 )\r
334{\r
335 EFI_STATUS Status = EFI_SUCCESS;\r
336 OBS_TREE* TempTree = NULL;\r
337 OBS_LEAF* Last = NULL;\r
338 OBS_LEAF* TempLeaf = NULL;\r
339 OBS_LEAF* NewLeaf = NULL;\r
340 BOOLEAN Found = FALSE;\r
341\r
342 if (mObservableDb != NULL) {\r
343 //\r
344 // Find the observable guid that we're looking for.\r
345 //\r
346 for (TempTree = mObservableDb; TempTree != NULL; TempTree = TempTree->Next) {\r
347 if (CompareMem(&(TempTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
348 Found = TRUE;\r
349 break;\r
350 }\r
351 }\r
352 if (Found) {\r
353 //\r
354 // Prepare to add a new leaf.\r
355 //\r
356 NewLeaf = AllocateZeroPool(sizeof(OBS_LEAF));\r
357 if (!NewLeaf) {\r
358 Status = EFI_OUT_OF_RESOURCES;\r
359 } else {\r
360 NewLeaf->Next = NULL;\r
361 NewLeaf->Observer = CallbackInterface;\r
362 //\r
363 // Go to the end of the list of observers.\r
364 //\r
365 if (TempTree->Leaf != NULL) {\r
366 //\r
367 // First check to see if this is a duplicate observer.\r
368 //\r
369 Found = FALSE;\r
370 TempLeaf = TempTree->Leaf;\r
371 do {\r
372 Last = TempLeaf;\r
373 if (TempLeaf->Observer == CallbackInterface) {\r
374 //\r
375 // It is, so let's abort this process.\r
376 //\r
377 Found = TRUE;\r
378 break;\r
379 }\r
380 TempLeaf = TempLeaf->Next;\r
381 } while (TempLeaf != NULL);\r
382 TempLeaf = Last;\r
383\r
384 //\r
385 // Check for duplicates.\r
386 //\r
387 if (Found) {\r
388 gBS->FreePool(NewLeaf);\r
389 Status = EFI_INVALID_PARAMETER;\r
390 } else {\r
391 //\r
392 // At this point, TempLeaf->Next will be the end of the list.\r
393 //\r
394 TempLeaf->Next = NewLeaf;\r
395 }\r
396 } else {\r
397 //\r
398 // There are no observers listening to this guid. Start a new list.\r
399 //\r
400 TempTree->Leaf = NewLeaf;\r
401 }\r
402 }\r
403 } else {\r
404 Status = EFI_NOT_FOUND;\r
405 }\r
406 } else {\r
407 Status = EFI_NOT_FOUND;\r
408 }\r
409\r
410 return Status;\r
411}\r
412\r
413/** Unsubscribe an interface with an observable guid.\r
414 *\r
415 * Use this to remove an interface from the callback list associated with an observable guid.\r
416 *\r
417 * @param EFI_GUID ReferenceGuid The observable guid to unsubscribe the interface from.\r
418 * OBS_NOTIFY_INTERFACE NotifyCallback A pointer to the interface that is being unsubscribed.\r
419 *\r
420 * @return EFI_SUCCESS Successfully unsubscribed the interface from the observable guid.\r
421 **/\r
422EFI_STATUS\r
423EFIAPI\r
424Unsubscribe (\r
425 IN EFI_GUID ReferenceGuid,\r
426 IN OBS_CALLBACK CallbackInterface\r
427 )\r
428{\r
429 mObservableDb = FindAndDeleteSubscriber(mObservableDb, ReferenceGuid, CallbackInterface);\r
430\r
431 return EFI_SUCCESS;\r
432}\r
433\r
434/** Notify observing functions.\r
435 *\r
436 * Use this to notify all functions who are subscribed to the guid specified by ReferenceGuid.\r
437 *\r
438 * @param EFI_GUID ReferenceGuid The observable guid that contains the the list of interfaces to be notified.\r
439 * VOID* Data Parameter context to be passed to the notification function.\r
440 *\r
441 * @return EFI_SUCCESS Successfully notified all observers listening to this guid.\r
442 * EFI_NOT_FOUND No match could be found between the provided guid and existing observables.\r
443 **/\r
444EFI_STATUS\r
445EFIAPI\r
446Publish (\r
447 IN EFI_GUID ReferenceGuid,\r
448 IN OUT VOID* Data\r
449 )\r
450{\r
451 EFI_STATUS Status = EFI_SUCCESS;\r
452 OBS_TREE* TempTree = NULL;\r
453 OBS_LEAF* TempLeaf = NULL;\r
454 BOOLEAN Found = FALSE;\r
455\r
456 if (mObservableDb != NULL) {\r
457 //\r
458 // Find the observable guid that we're looking for.\r
459 //\r
460 for (TempTree = mObservableDb; TempTree != NULL; TempTree = TempTree->Next) {\r
461 if (CompareMem(&(TempTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
462 Found = TRUE;\r
463 break;\r
464 }\r
465 }\r
466 if (Found) {\r
467 //\r
468 // Notify every listener by performing each provided callback.\r
469 //\r
470 for (TempLeaf = TempTree->Leaf; TempLeaf != NULL; TempLeaf = TempLeaf->Next) {\r
471 if (TempLeaf->Observer != NULL) {\r
472 //\r
473 // Execute the callback.\r
474 //\r
475 TempLeaf->Observer(Data);\r
476 }\r
477 }\r
478 } else {\r
479 Status = EFI_NOT_FOUND;\r
480 }\r
481 } else {\r
482 Status = EFI_NOT_FOUND;\r
483 }\r
484\r
485 return Status;\r
486}\r
487\r
488/** Creates a new observable.\r
489 *\r
490 * Create a new observable that can be observed with the use of Subscribe function.\r
491 *\r
492 * @param EFI_GUID ReferenceGuid The observable guid to add.\r
493 *\r
494 * @return EFI_SUCCESS Successfully added observable.\r
495 * EFI_INVALID_PARAMETER Observable already exists.\r
496 **/\r
497EFI_STATUS\r
498EFIAPI\r
499AddObservable (\r
500 IN EFI_GUID ReferenceGuid\r
501 )\r
502{\r
503 EFI_STATUS Status = EFI_SUCCESS;\r
504 OBS_TREE* TempTree = NULL;\r
505 OBS_TREE* Last = NULL;\r
506 OBS_TREE* NewTree = NULL;\r
507 BOOLEAN Found = FALSE;\r
508\r
509 if (mObservableDb != NULL) {\r
510 if (mObservableDb->Next != NULL) {\r
511 //\r
512 // Iterate to the end of the observable list while checking to see if we aren't creating a duplicate.\r
513 //\r
514 TempTree = mObservableDb->Next;\r
515 do {\r
516 Last = TempTree;\r
517 if (CompareMem(&(TempTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
518 Found = TRUE;\r
519 break;\r
520 }\r
521 TempTree = TempTree->Next;\r
522 } while (TempTree != NULL);\r
523 TempTree = Last;\r
524 } else {\r
525 TempTree = mObservableDb;\r
526 }\r
527 if (Found) {\r
528 //\r
529 // Duplicate, so reject the parameter.\r
530 //\r
531 Status = EFI_INVALID_PARAMETER;\r
532 } else {\r
533 //\r
534 // TempTree->Next is our target. Prepare to add a new tree link.\r
535 //\r
536 NewTree = AllocateZeroPool(sizeof(OBS_TREE));\r
537 if (NewTree) {\r
538 NewTree->Next = NULL;\r
539 NewTree->Leaf = NULL;\r
540 CopyMem(&(NewTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid));\r
541 TempTree->Next = NewTree;\r
542 } else {\r
543 Status = EFI_OUT_OF_RESOURCES;\r
544 }\r
545 }\r
546 } else {\r
547 //\r
548 // mObservableDb has not been created yet. Let's do that.\r
549 //\r
550 NewTree = AllocateZeroPool(sizeof(OBS_TREE));\r
551 if (NewTree) {\r
552 NewTree->Next = NULL;\r
553 NewTree->Leaf = NULL;\r
554 CopyMem(&(NewTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid));\r
555 mObservableDb = NewTree;\r
556 } else {\r
557 Status = EFI_OUT_OF_RESOURCES;\r
558 }\r
559 }\r
560\r
561 return Status;\r
562}\r
563\r
564/** Remove an observable.\r
565 *\r
566 * Remove an observable so that it can no longer be subscribed to. In addition, unsubscribe any functions\r
567 * that are subscribed to this guid.\r
568 *\r
569 * @param EFI_GUID ReferenceGuid The observable guid to remove.\r
570 *\r
571 * @return EFI_SUCCESS Successfully removed observable.\r
572 **/\r
573EFI_STATUS\r
574EFIAPI\r
575RemoveObservable (\r
576 IN EFI_GUID ReferenceGuid\r
577 )\r
578{\r
579 mObservableDb = FindAndDeleteObservable(mObservableDb, ReferenceGuid);\r
580\r
581 return EFI_SUCCESS;\r
582}\r