3 The implementation of EFI REST Resource JSON to C structure convertor
6 (C) Copyright 2020 Hewlett Packard Enterprise Development LP<BR>
8 SPDX-License-Identifier: BSD-2-Clause-Patent
13 #include <Protocol/RestJsonStructure.h>
14 #include "RestJsonStructureInternal.h"
16 LIST_ENTRY mRestJsonStructureList
;
17 EFI_HANDLE mProtocolHandle
;
20 This function registers Restful resource interpreter for the
23 @param[in] This This is the EFI_REST_JSON_STRUCTURE_PROTOCOL instance.
24 @param[in] JsonStructureSupported The type and version of REST JSON resource which this converter
26 @param[in] ToStructure The function to convert REST JSON resource to structure.
27 @param[in] ToJson The function to convert REST JSON structure to JSON in text format.
28 @param[in] DestroyStructure Destroy REST JSON structure returned in ToStructure() function.
30 @retval EFI_SUCCESS Register successfully.
31 @retval Others Fail to register.
36 RestJsonStructureRegister (
37 IN EFI_REST_JSON_STRUCTURE_PROTOCOL
*This
,
38 IN EFI_REST_JSON_STRUCTURE_SUPPORTED
*JsonStructureSupported
,
39 IN EFI_REST_JSON_STRUCTURE_TO_STRUCTURE ToStructure
,
40 IN EFI_REST_JSON_STRUCTURE_TO_JSON ToJson
,
41 IN EFI_REST_JSON_STRUCTURE_DESTORY_STRUCTURE DestroyStructure
47 REST_JSON_STRUCTURE_INSTANCE
*Instance
;
48 EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*CloneSupportedInterpId
;
49 EFI_REST_JSON_STRUCTURE_SUPPORTED
*ThisSupportedInterp
;
52 ToStructure
== NULL
||
54 DestroyStructure
== NULL
||
55 JsonStructureSupported
== NULL
57 return EFI_INVALID_PARAMETER
;
61 // Check how many name space interpreter can interpret.
63 ThisList
= &JsonStructureSupported
->NextSupportedRsrcInterp
;
66 if (ThisList
->ForwardLink
== &JsonStructureSupported
->NextSupportedRsrcInterp
) {
69 ThisList
= ThisList
->ForwardLink
;
75 (REST_JSON_STRUCTURE_INSTANCE
*)AllocateZeroPool (sizeof (REST_JSON_STRUCTURE_INSTANCE
) + NumberOfNS
* sizeof (EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
));
76 if (Instance
== NULL
) {
77 return EFI_OUT_OF_RESOURCES
;
79 InitializeListHead (&Instance
->NextRestJsonStructureInstance
);
80 Instance
->NumberOfNameSpaceToConvert
= NumberOfNS
;
81 Instance
->SupportedRsrcIndentifier
= (EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*)((REST_JSON_STRUCTURE_INSTANCE
*)Instance
+ 1);
83 // Copy supported resource identifer interpreter.
85 CloneSupportedInterpId
= Instance
->SupportedRsrcIndentifier
;
86 ThisSupportedInterp
= JsonStructureSupported
;
87 for (Index
= 0; Index
< NumberOfNS
; Index
++) {
88 CopyMem ((VOID
*)CloneSupportedInterpId
, (VOID
*)&ThisSupportedInterp
->RestResourceInterp
, sizeof (EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
));
89 ThisSupportedInterp
= (EFI_REST_JSON_STRUCTURE_SUPPORTED
*)ThisSupportedInterp
->NextSupportedRsrcInterp
.ForwardLink
;
90 CloneSupportedInterpId
++;
92 Instance
->JsonToStructure
= ToStructure
;
93 Instance
->StructureToJson
= ToJson
;
94 Instance
->DestroyStructure
= DestroyStructure
;
95 InsertTailList (&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
);
100 This function check if this interpreter instance support the given namesapce.
102 @param[in] This EFI_REST_JSON_STRUCTURE_PROTOCOL instance.
103 @param[in] InterpreterInstance REST_JSON_STRUCTURE_INSTANCE
104 @param[in] RsrcTypeIdentifier Resource type identifier.
105 @param[in] ResourceRaw Given Restful resource.
106 @param[out] RestJSonHeader Property interpreted from given ResourceRaw.
113 InterpreterInstanceToStruct (
114 IN EFI_REST_JSON_STRUCTURE_PROTOCOL
*This
,
115 IN REST_JSON_STRUCTURE_INSTANCE
*InterpreterInstance
,
116 IN EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*RsrcTypeIdentifier OPTIONAL
,
117 IN CHAR8
*ResourceRaw
,
118 OUT EFI_REST_JSON_STRUCTURE_HEADER
**RestJSonHeader
123 EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*ThisSupportedRsrcTypeId
;
126 InterpreterInstance
== NULL
||
127 ResourceRaw
== NULL
||
128 RestJSonHeader
== NULL
130 return EFI_INVALID_PARAMETER
;
133 Status
= EFI_UNSUPPORTED
;
134 if (RsrcTypeIdentifier
== NULL
) {
136 // No resource type identifier, send to intepreter anyway.
137 // Interpreter may recognize this resource.
139 Status
= InterpreterInstance
->JsonToStructure (
147 // Check if the namesapce and version is supported by this interpreter.
149 ThisSupportedRsrcTypeId
= InterpreterInstance
->SupportedRsrcIndentifier
;
150 for (Index
= 0; Index
< InterpreterInstance
->NumberOfNameSpaceToConvert
; Index
++){
152 RsrcTypeIdentifier
->NameSpace
.ResourceTypeName
,
153 ThisSupportedRsrcTypeId
->NameSpace
.ResourceTypeName
) == 0){
154 if ((RsrcTypeIdentifier
->NameSpace
.MajorVersion
== NULL
) &&
155 (RsrcTypeIdentifier
->NameSpace
.MinorVersion
== NULL
) &&
156 (RsrcTypeIdentifier
->NameSpace
.ErrataVersion
== NULL
)
159 // Don't check version of this resource type identifier.
161 Status
= InterpreterInstance
->JsonToStructure (
173 RsrcTypeIdentifier
->NameSpace
.MajorVersion
,
174 ThisSupportedRsrcTypeId
->NameSpace
.MajorVersion
) == 0) &&
176 RsrcTypeIdentifier
->NameSpace
.MinorVersion
,
177 ThisSupportedRsrcTypeId
->NameSpace
.MinorVersion
) == 0) &&
179 RsrcTypeIdentifier
->NameSpace
.ErrataVersion
,
180 ThisSupportedRsrcTypeId
->NameSpace
.ErrataVersion
) == 0)) {
181 Status
= InterpreterInstance
->JsonToStructure (
191 ThisSupportedRsrcTypeId
++;
197 This function converts JSON C structure to JSON property.
199 @param[in] This EFI_REST_JSON_STRUCTURE_PROTOCOL instance.
200 @param[in] InterpreterInstance REST_JSON_STRUCTURE_INSTANCE
201 @param[in] RestJSonHeader Resource type identifier.
202 @param[out] ResourceRaw Output in JSON text format.
209 InterpreterEfiStructToInstance (
210 IN EFI_REST_JSON_STRUCTURE_PROTOCOL
*This
,
211 IN REST_JSON_STRUCTURE_INSTANCE
*InterpreterInstance
,
212 IN EFI_REST_JSON_STRUCTURE_HEADER
*RestJSonHeader
,
213 OUT CHAR8
**ResourceRaw
218 EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*ThisSupportedRsrcTypeId
;
219 EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*RsrcTypeIdentifier
;
222 InterpreterInstance
== NULL
||
223 RestJSonHeader
== NULL
||
226 return EFI_INVALID_PARAMETER
;
228 RsrcTypeIdentifier
= &RestJSonHeader
->JsonRsrcIdentifier
;
229 if (RsrcTypeIdentifier
== NULL
||
230 RsrcTypeIdentifier
->NameSpace
.ResourceTypeName
== NULL
||
231 RsrcTypeIdentifier
->NameSpace
.MajorVersion
== NULL
||
232 RsrcTypeIdentifier
->NameSpace
.MinorVersion
== NULL
||
233 RsrcTypeIdentifier
->NameSpace
.ErrataVersion
== NULL
235 return EFI_INVALID_PARAMETER
;
239 // Check if the namesapce and version is supported by this interpreter.
241 Status
= EFI_UNSUPPORTED
;
242 ThisSupportedRsrcTypeId
= InterpreterInstance
->SupportedRsrcIndentifier
;
243 for (Index
= 0; Index
< InterpreterInstance
->NumberOfNameSpaceToConvert
; Index
++){
245 RsrcTypeIdentifier
->NameSpace
.ResourceTypeName
,
246 ThisSupportedRsrcTypeId
->NameSpace
.ResourceTypeName
) == 0){
251 RsrcTypeIdentifier
->NameSpace
.MajorVersion
,
252 ThisSupportedRsrcTypeId
->NameSpace
.MajorVersion
) == 0) &&
254 RsrcTypeIdentifier
->NameSpace
.MinorVersion
,
255 ThisSupportedRsrcTypeId
->NameSpace
.MinorVersion
) == 0) &&
257 RsrcTypeIdentifier
->NameSpace
.ErrataVersion
,
258 ThisSupportedRsrcTypeId
->NameSpace
.ErrataVersion
) == 0)) {
259 Status
= InterpreterInstance
->StructureToJson (
267 ThisSupportedRsrcTypeId
++;
273 This function destory REST property structure.
275 @param[in] This EFI_REST_JSON_STRUCTURE_PROTOCOL instance.
276 @param[in] InterpreterInstance REST_JSON_STRUCTURE_INSTANCE
277 @param[in] RestJSonHeader Property interpreted from given ResourceRaw.
284 InterpreterInstanceDestoryJsonStruct (
285 IN EFI_REST_JSON_STRUCTURE_PROTOCOL
*This
,
286 IN REST_JSON_STRUCTURE_INSTANCE
*InterpreterInstance
,
287 IN EFI_REST_JSON_STRUCTURE_HEADER
*RestJSonHeader
292 EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*ThisSupportedRsrcTypeId
;
295 InterpreterInstance
== NULL
||
296 RestJSonHeader
== NULL
298 return EFI_INVALID_PARAMETER
;
301 Status
= EFI_UNSUPPORTED
;
303 // Check if the namesapce and version is supported by this interpreter.
305 ThisSupportedRsrcTypeId
= InterpreterInstance
->SupportedRsrcIndentifier
;
306 for (Index
= 0; Index
< InterpreterInstance
->NumberOfNameSpaceToConvert
; Index
++){
308 RestJSonHeader
->JsonRsrcIdentifier
.NameSpace
.ResourceTypeName
,
309 ThisSupportedRsrcTypeId
->NameSpace
.ResourceTypeName
) == 0) {
310 if ((RestJSonHeader
->JsonRsrcIdentifier
.NameSpace
.MajorVersion
== NULL
) &&
311 (RestJSonHeader
->JsonRsrcIdentifier
.NameSpace
.MinorVersion
== NULL
) &&
312 (RestJSonHeader
->JsonRsrcIdentifier
.NameSpace
.ErrataVersion
== NULL
)
315 // Don't check version of this resource type identifier.
317 Status
= InterpreterInstance
->DestroyStructure (
327 RestJSonHeader
->JsonRsrcIdentifier
.NameSpace
.MajorVersion
,
328 ThisSupportedRsrcTypeId
->NameSpace
.MajorVersion
) == 0) &&
330 RestJSonHeader
->JsonRsrcIdentifier
.NameSpace
.MinorVersion
,
331 ThisSupportedRsrcTypeId
->NameSpace
.MinorVersion
) == 0) &&
333 RestJSonHeader
->JsonRsrcIdentifier
.NameSpace
.ErrataVersion
,
334 ThisSupportedRsrcTypeId
->NameSpace
.ErrataVersion
) == 0)) {
335 Status
= InterpreterInstance
->DestroyStructure (
343 ThisSupportedRsrcTypeId
++;
349 This function translates the given JSON text to JSON C Structure.
351 @param[in] This EFI_REST_JSON_STRUCTURE_PROTOCOL instance.
352 @param[in] RsrcTypeIdentifier Resource type identifier.
353 @param[in] ResourceJsonText Given Restful resource.
354 @param[out] JsonStructure Property interpreted from given ResourceRaw.
362 RestJsonStructureToStruct (
363 IN EFI_REST_JSON_STRUCTURE_PROTOCOL
*This
,
364 IN EFI_REST_JSON_RESOURCE_TYPE_IDENTIFIER
*RsrcTypeIdentifier OPTIONAL
,
365 IN CHAR8
*ResourceJsonText
,
366 OUT EFI_REST_JSON_STRUCTURE_HEADER
**JsonStructure
370 REST_JSON_STRUCTURE_INSTANCE
*Instance
;
373 ResourceJsonText
== NULL
||
374 JsonStructure
== NULL
376 return EFI_INVALID_PARAMETER
;
379 if (IsListEmpty (&mRestJsonStructureList
)) {
380 return EFI_UNSUPPORTED
;
382 Status
= EFI_SUCCESS
;
383 Instance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetFirstNode (&mRestJsonStructureList
);
385 Status
= InterpreterInstanceToStruct (
392 if (!EFI_ERROR (Status
)) {
395 if (IsNodeAtEnd(&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
)) {
396 Status
= EFI_UNSUPPORTED
;
399 Instance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetNextNode (&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
);
405 This function destory REST property EFI structure which returned in
408 @param[in] This EFI_REST_JSON_STRUCTURE_PROTOCOL instance.
409 @param[in] RestJSonHeader Property to destory.
417 RestJsonStructureDestroyStruct (
418 IN EFI_REST_JSON_STRUCTURE_PROTOCOL
*This
,
419 IN EFI_REST_JSON_STRUCTURE_HEADER
*RestJSonHeader
423 REST_JSON_STRUCTURE_INSTANCE
*Instance
;
425 if (This
== NULL
|| RestJSonHeader
== NULL
) {
426 return EFI_INVALID_PARAMETER
;
429 if (IsListEmpty (&mRestJsonStructureList
)) {
430 return EFI_UNSUPPORTED
;
432 Status
= EFI_SUCCESS
;
433 Instance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetFirstNode (&mRestJsonStructureList
);
435 Status
= InterpreterInstanceDestoryJsonStruct (
440 if (!EFI_ERROR (Status
)) {
443 if (IsNodeAtEnd(&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
)) {
444 Status
= EFI_UNSUPPORTED
;
447 Instance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetNextNode (&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
);
453 This function translates the given JSON C Structure to JSON text.
455 @param[in] This EFI_REST_JSON_STRUCTURE_PROTOCOL instance.
456 @param[in] RestJSonHeader Given Restful resource.
457 @param[out] ResourceRaw Resource in RESTfuls service oriented.
460 @retval Others Fail to remove the entry
465 RestJsonStructureToJson (
466 IN EFI_REST_JSON_STRUCTURE_PROTOCOL
*This
,
467 IN EFI_REST_JSON_STRUCTURE_HEADER
*RestJSonHeader
,
468 OUT CHAR8
**ResourceRaw
472 REST_JSON_STRUCTURE_INSTANCE
*Instance
;
474 if (This
== NULL
|| RestJSonHeader
== NULL
|| ResourceRaw
== NULL
) {
475 return EFI_INVALID_PARAMETER
;
478 if (IsListEmpty (&mRestJsonStructureList
)) {
479 return EFI_UNSUPPORTED
;
481 Status
= EFI_SUCCESS
;
482 Instance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetFirstNode (&mRestJsonStructureList
);
484 Status
= InterpreterEfiStructToInstance (
490 if (!EFI_ERROR (Status
)) {
493 if (IsNodeAtEnd(&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
)) {
494 Status
= EFI_UNSUPPORTED
;
497 Instance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetNextNode (&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
);
502 EFI_REST_JSON_STRUCTURE_PROTOCOL mRestJsonStructureProtocol
= {
503 RestJsonStructureRegister
,
504 RestJsonStructureToStruct
,
505 RestJsonStructureToJson
,
506 RestJsonStructureDestroyStruct
510 This is the declaration of an EFI image entry point.
512 @param ImageHandle The firmware allocated handle for the UEFI image.
513 @param SystemTable A pointer to the EFI System Table.
515 @retval EFI_SUCCESS The operation completed successfully.
516 @retval Others An unexpected error occurred.
520 RestJsonStructureEntryPoint (
521 IN EFI_HANDLE ImageHandle
,
522 IN EFI_SYSTEM_TABLE
*SystemTable
527 InitializeListHead (&mRestJsonStructureList
);
529 // Install the Restful Resource Interpreter Protocol.
531 mProtocolHandle
= NULL
;
532 Status
= gBS
->InstallProtocolInterface (
534 &gEfiRestJsonStructureProtocolGuid
,
535 EFI_NATIVE_INTERFACE
,
536 (VOID
*)&mRestJsonStructureProtocol
542 This is the unload handle for Redfish discover module.
544 Disconnect the driver specified by ImageHandle from all the devices in the handle database.
545 Uninstall all the protocols installed in the driver entry point.
547 @param[in] ImageHandle The drivers' driver image.
549 @retval EFI_SUCCESS The image is unloaded.
550 @retval Others Failed to unload the image.
555 RestJsonStructureUnload (
556 IN EFI_HANDLE ImageHandle
560 REST_JSON_STRUCTURE_INSTANCE
*Instance
;
561 REST_JSON_STRUCTURE_INSTANCE
*NextInstance
;
563 Status
= gBS
->UninstallProtocolInterface (
565 &gEfiRestJsonStructureProtocolGuid
,
566 (VOID
*)&mRestJsonStructureProtocol
569 if (IsListEmpty (&mRestJsonStructureList
)) {
573 // Free memory of REST_JSON_STRUCTURE_INSTANCE instance.
575 Instance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetFirstNode (&mRestJsonStructureList
);
578 if (!IsNodeAtEnd(&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
)) {
579 NextInstance
= (REST_JSON_STRUCTURE_INSTANCE
*)GetNextNode (&mRestJsonStructureList
, &Instance
->NextRestJsonStructureInstance
);
581 FreePool ((VOID
*)Instance
);
582 Instance
= NextInstance
;
583 } while (Instance
!= NULL
);