summaryrefslogtreecommitdiffstats
path: root/RedfishPkg/Library
diff options
context:
space:
mode:
authorNickle Wang <nicklew@nvidia.com>2023-03-01 16:44:57 +0800
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2023-03-06 03:20:58 +0000
commit5a0932b7d480c05761a3ba9b5cf1d2fbb470c615 (patch)
tree0bca3c852d1c60b555cf4978517a1a1d8c484054 /RedfishPkg/Library
parentfc14c809cb982f3c8cb3429604262cde0cb264a9 (diff)
downloadedk2-5a0932b7d480c05761a3ba9b5cf1d2fbb470c615.tar.gz
edk2-5a0932b7d480c05761a3ba9b5cf1d2fbb470c615.tar.bz2
edk2-5a0932b7d480c05761a3ba9b5cf1d2fbb470c615.zip
RedfishPkg/RedfishDebugLib: provide Redfish debug
Introduce RedfishDebugLib to RedfishPkg. This library provides several debugging functions for Redfish application. Redfish drivers rely on Rest Ex protocol to communicate with BMC and the communication data may be big and complicated. Use RedfishDebugLib in RedfishRestExDxe to simplify debugging process. Signed-off-by: Nickle Wang <nicklew@nvidia.com> Cc: Abner Chang <abner.chang@amd.com> Cc: Igor Kulchytskyy <igork@ami.com> Cc: Nick Ramirez <nramirez@nvidia.com> Reviewed-by: Abner Chang <abner.chang@amd.com>
Diffstat (limited to 'RedfishPkg/Library')
-rw-r--r--RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.c229
-rw-r--r--RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.inf39
2 files changed, 268 insertions, 0 deletions
diff --git a/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.c b/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.c
new file mode 100644
index 0000000000..6f1d9de25e
--- /dev/null
+++ b/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.c
@@ -0,0 +1,229 @@
+/** @file
+ Redfish debug library to debug Redfish application.
+
+ Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+
+ SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/RedfishDebugLib.h>
+#include <Library/UefiLib.h>
+
+#ifndef IS_EMPTY_STRING
+#define IS_EMPTY_STRING(a) ((a) == NULL || (a)[0] == '\0')
+#endif
+
+#define REDFISH_JSON_STRING_LENGTH 200
+#define REDFISH_JSON_OUTPUT_FORMAT (EDKII_JSON_COMPACT | EDKII_JSON_INDENT(2))
+
+/**
+
+ This function dump the Json string in given error level.
+
+ @param[in] ErrorLevel DEBUG macro error level
+ @param[in] JsonValue Json value to dump.
+
+ @retval EFI_SUCCESS Json string is printed.
+ @retval Others Errors occur.
+
+**/
+EFI_STATUS
+DumpJsonValue (
+ IN UINTN ErrorLevel,
+ IN EDKII_JSON_VALUE JsonValue
+ )
+{
+ CHAR8 *String;
+ CHAR8 *Runner;
+ CHAR8 Buffer[REDFISH_JSON_STRING_LENGTH + 1];
+ UINTN StrLen;
+ UINTN Count;
+ UINTN Index;
+
+ if (JsonValue == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ String = JsonDumpString (JsonValue, REDFISH_JSON_OUTPUT_FORMAT);
+ if (String == NULL) {
+ return EFI_UNSUPPORTED;
+ }
+
+ StrLen = AsciiStrLen (String);
+ if (StrLen == 0) {
+ return EFI_UNSUPPORTED;
+ }
+
+ Count = StrLen / REDFISH_JSON_STRING_LENGTH;
+ Runner = String;
+ for (Index = 0; Index < Count; Index++) {
+ AsciiStrnCpyS (Buffer, (REDFISH_JSON_STRING_LENGTH + 1), Runner, REDFISH_JSON_STRING_LENGTH);
+ Buffer[REDFISH_JSON_STRING_LENGTH] = '\0';
+ DEBUG ((ErrorLevel, "%a", Buffer));
+ Runner += REDFISH_JSON_STRING_LENGTH;
+ }
+
+ Count = StrLen % REDFISH_JSON_STRING_LENGTH;
+ if (Count > 0) {
+ DEBUG ((ErrorLevel, "%a", Runner));
+ }
+
+ DEBUG ((ErrorLevel, "\n"));
+
+ FreePool (String);
+ return EFI_SUCCESS;
+}
+
+/**
+
+ This function dump the status code, header and body in given
+ Redfish payload.
+
+ @param[in] ErrorLevel DEBUG macro error level
+ @param[in] Payload Redfish payload to dump
+
+ @retval EFI_SUCCESS Redfish payload is printed.
+ @retval Others Errors occur.
+
+**/
+EFI_STATUS
+DumpRedfishPayload (
+ IN UINTN ErrorLevel,
+ IN REDFISH_PAYLOAD Payload
+ )
+{
+ EDKII_JSON_VALUE JsonValue;
+
+ if (Payload == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ JsonValue = RedfishJsonInPayload (Payload);
+ if (JsonValue != NULL) {
+ DEBUG ((ErrorLevel, "Payload:\n"));
+ DumpJsonValue (ErrorLevel, JsonValue);
+ }
+
+ return EFI_SUCCESS;
+}
+
+/**
+
+ This function dump the HTTP status code.
+
+ @param[in] ErrorLevel DEBUG macro error level
+ @param[in] HttpStatusCode HTTP status code
+
+ @retval EFI_SUCCESS HTTP status code is printed
+
+**/
+EFI_STATUS
+DumpHttpStatusCode (
+ IN UINTN ErrorLevel,
+ IN EFI_HTTP_STATUS_CODE HttpStatusCode
+ )
+{
+ switch (HttpStatusCode) {
+ case HTTP_STATUS_100_CONTINUE:
+ DEBUG ((ErrorLevel, "Status code: 100 CONTINUE\n"));
+ break;
+ case HTTP_STATUS_200_OK:
+ DEBUG ((ErrorLevel, "Status code: 200 OK\n"));
+ break;
+ case HTTP_STATUS_201_CREATED:
+ DEBUG ((ErrorLevel, "Status code: 201 CREATED\n"));
+ break;
+ case HTTP_STATUS_202_ACCEPTED:
+ DEBUG ((ErrorLevel, "Status code: 202 ACCEPTED\n"));
+ break;
+ case HTTP_STATUS_304_NOT_MODIFIED:
+ DEBUG ((ErrorLevel, "Status code: 304 NOT MODIFIED\n"));
+ break;
+ case HTTP_STATUS_400_BAD_REQUEST:
+ DEBUG ((ErrorLevel, "Status code: 400 BAD REQUEST\n"));
+ break;
+ case HTTP_STATUS_401_UNAUTHORIZED:
+ DEBUG ((ErrorLevel, "Status code: 401 UNAUTHORIZED\n"));
+ break;
+ case HTTP_STATUS_403_FORBIDDEN:
+ DEBUG ((ErrorLevel, "Status code: 403 FORBIDDEN\n"));
+ break;
+ case HTTP_STATUS_404_NOT_FOUND:
+ DEBUG ((ErrorLevel, "Status code: 404 NOT FOUND\n"));
+ break;
+ case HTTP_STATUS_405_METHOD_NOT_ALLOWED:
+ DEBUG ((ErrorLevel, "Status code: 405 METHOD NOT ALLOWED\n"));
+ break;
+ case HTTP_STATUS_500_INTERNAL_SERVER_ERROR:
+ DEBUG ((ErrorLevel, "Status code: 500 INTERNAL SERVER ERROR\n"));
+ break;
+ default:
+ DEBUG ((ErrorLevel, "Status code: 0x%x\n", HttpStatusCode));
+ break;
+ }
+
+ return EFI_SUCCESS;
+}
+
+/**
+
+ This function dump the status code, header and body in given
+ Redfish response.
+
+ @param[in] Message Message string
+ @param[in] ErrorLevel DEBUG macro error level
+ @param[in] Response Redfish response to dump
+
+ @retval EFI_SUCCESS Redfish response is printed.
+ @retval Others Errors occur.
+
+**/
+EFI_STATUS
+DumpRedfishResponse (
+ IN CONST CHAR8 *Message,
+ IN UINTN ErrorLevel,
+ IN REDFISH_RESPONSE *Response
+ )
+{
+ UINTN Index;
+
+ if (Response == NULL) {
+ return EFI_INVALID_PARAMETER;
+ }
+
+ if (!IS_EMPTY_STRING (Message)) {
+ DEBUG ((ErrorLevel, "%a\n", Message));
+ }
+
+ //
+ // status code
+ //
+ if (Response->StatusCode != NULL) {
+ DumpHttpStatusCode (ErrorLevel, *(Response->StatusCode));
+ }
+
+ //
+ // header
+ //
+ if (Response->HeaderCount > 0) {
+ DEBUG ((ErrorLevel, "Header: %d\n", Response->HeaderCount));
+ for (Index = 0; Index < Response->HeaderCount; Index++) {
+ DEBUG ((ErrorLevel, " %a: %a\n", Response->Headers[Index].FieldName, Response->Headers[Index].FieldValue));
+ }
+ }
+
+ //
+ // Body
+ //
+ if (Response->Payload != NULL) {
+ DumpRedfishPayload (ErrorLevel, Response->Payload);
+ }
+
+ return EFI_SUCCESS;
+}
diff --git a/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.inf b/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.inf
new file mode 100644
index 0000000000..048d27c6dc
--- /dev/null
+++ b/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.inf
@@ -0,0 +1,39 @@
+## @file
+# INF file for Redfish debug library.
+#
+# Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+ INF_VERSION = 0x00010006
+ BASE_NAME = RedfishDebugLib
+ FILE_GUID = 7F64C79F-ABD0-4446-86B5-2C1AE36168AD
+ MODULE_TYPE = DXE_DRIVER
+ VERSION_STRING = 1.0
+ LIBRARY_CLASS = RedfishDebugLib| DXE_DRIVER UEFI_DRIVER
+
+#
+# VALID_ARCHITECTURES = IA32 X64 EBC
+#
+
+[Sources]
+ RedfishDebugLib.c
+
+[Packages]
+ MdePkg/MdePkg.dec
+ MdeModulePkg/MdeModulePkg.dec
+ RedfishPkg/RedfishPkg.dec
+
+[LibraryClasses]
+ BaseLib
+ DebugLib
+ JsonLib
+ MemoryAllocationLib
+ RedfishLib
+ UefiLib
+
+[Depex]
+ TRUE