public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-staging][PATCH 2/3] edk2-staging/RedfishClientPkg: Add Redfish Resource Addendum Library
@ 2022-12-07 14:41 Nickle Wang
  2022-12-12  3:15 ` Chang, Abner
  0 siblings, 1 reply; 2+ messages in thread
From: Nickle Wang @ 2022-12-07 14:41 UTC (permalink / raw)
  To: devel; +Cc: Abner Chang, Igor Kulchytskyy, Nick Ramirez

Implement RedfishAddendumLib to support Redfish Resource Addendum Protocol.
Feature driver calls this library to get addendum data before providing
data to Redfish service.

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>
---
 .../Include/Library/RedfishAddendumLib.h      |  63 +++++
 .../RedfishAddendumLib/RedfishAddendumLib.c   | 258 ++++++++++++++++++
 .../RedfishAddendumLib/RedfishAddendumLib.inf |  40 +++
 RedfishClientPkg/RedfishClientLibs.dsc.inc    |   2 +
 4 files changed, 363 insertions(+)
 create mode 100644 RedfishClientPkg/Include/Library/RedfishAddendumLib.h
 create mode 100644 RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.c
 create mode 100644 RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.inf

diff --git a/RedfishClientPkg/Include/Library/RedfishAddendumLib.h b/RedfishClientPkg/Include/Library/RedfishAddendumLib.h
new file mode 100644
index 0000000000..4215257820
--- /dev/null
+++ b/RedfishClientPkg/Include/Library/RedfishAddendumLib.h
@@ -0,0 +1,63 @@
+/** @file
+  This file defines the Redfish event library interface.
+
+  (C) Copyright 2022 Hewlett Packard Enterprise Development LP<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#ifndef REDFISH_ADDENDUM_LIB_H_
+#define REDFISH_ADDENDUM_LIB_H_
+
+#include <Uefi.h>
+#include <Library/JsonLib.h>
+#include <Protocol/EdkIIRedfishResourceAddendumProtocol.h>
+
+/**
+  Provising redfish resource with with addendum data in given schema.
+
+  @param[in]   Uri              Uri of input resource.
+  @param[in]   Schema           Redfish schema string.
+  @param[in]   Version          Schema version string.
+  @param[in]   JsonText         Input resource in JSON format string.
+  @param[out]  JsonWithAddendum The input resource with addendum value attached.
+
+  @retval EFI_SUCCESS              Addendum data is attached.
+  @retval EFI_UNSUPPORTED          No addendum data is required in given schema.
+  @retval Others                   Some error happened.
+
+**/
+EFI_STATUS
+RedfishGetAddendumData (
+  IN     EFI_STRING  Uri,
+  IN     CHAR8       *Schema,
+  IN     CHAR8       *Version,
+  IN     CHAR8       *JsonText,
+  OUT    CHAR8       **JsonWithAddendum
+  );
+
+/**
+  Provising redfish OEM resource with given schema information.
+
+  @param[in]   Uri             Uri of input resource.
+  @param[in]   Schema          Redfish schema string.
+  @param[in]   Version         Schema version string.
+  @param[in]   JsonText        Input resource in JSON format string.
+  @param[out]  JsonWithOem     The input resource with OEM value attached.
+
+  @retval EFI_SUCCESS              OEM data is attached.
+  @retval EFI_UNSUPPORTED          No OEM data is required in given schema.
+  @retval Others                   Some error happened.
+
+**/
+EFI_STATUS
+RedfishGetOemData (
+  IN  EFI_STRING  Uri,
+  IN  CHAR8       *Schema,
+  IN  CHAR8       *Version,
+  IN  CHAR8       *JsonText,
+  OUT CHAR8       **JsonWithOem
+  );
+
+#endif
diff --git a/RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.c b/RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.c
new file mode 100644
index 0000000000..4154d01e5a
--- /dev/null
+++ b/RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.c
@@ -0,0 +1,258 @@
+/** @file
+  Redfish event library to deliver Redfish specific event.
+
+  (C) Copyright 2022 Hewlett Packard Enterprise Development LP<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <RedfishBase.h>
+
+#include <Library/UefiLib.h>
+#include <Library/BaseLib.h>
+#include <Library/DebugLib.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/RedfishAddendumLib.h>
+
+/**
+
+  Convert Unicode string to ASCII string. It's call responsibility to release returned buffer.
+
+  @param[in]  UnicodeStr      Unicode string to convert.
+
+  @retval     CHAR8 *         ASCII string returned.
+  @retval     NULL            Errors occur.
+
+**/
+CHAR8 *
+UnicodeToAscii (
+  IN EFI_STRING  UnicodeStr
+  )
+{
+  CHAR8       *AsciiStr;
+  UINTN       AsciiStrSize;
+  EFI_STATUS  Status;
+
+  if (IS_EMPTY_STRING (UnicodeStr)) {
+    return NULL;
+  }
+
+  AsciiStrSize = StrLen (UnicodeStr) + 1;
+  AsciiStr     = AllocatePool (AsciiStrSize);
+  if (AsciiStr == NULL) {
+    return NULL;
+  }
+
+  Status = UnicodeStrToAsciiStrS (UnicodeStr, AsciiStr, AsciiStrSize);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "UnicodeStrToAsciiStrS failed: %r\n", Status));
+    FreePool (AsciiStr);
+    return NULL;
+  }
+
+  return AsciiStr;
+}
+
+/**
+  Provising redfish resource with with addendum data in given schema.
+
+  @param[in]   Uri              Uri of input resource.
+  @param[in]   Schema           Redfish schema string.
+  @param[in]   Version          Schema version string.
+  @param[in]   JsonText         Input resource in JSON format string.
+  @param[out]  JsonWithAddendum The input resource with addendum value attached.
+
+  @retval EFI_SUCCESS              Addendum data is attached.
+  @retval EFI_UNSUPPORTED          No addendum data is required in given schema.
+  @retval Others                   Some error happened.
+
+**/
+EFI_STATUS
+RedfishGetAddendumData (
+  IN     EFI_STRING  Uri,
+  IN     CHAR8       *Schema,
+  IN     CHAR8       *Version,
+  IN     CHAR8       *JsonText,
+  OUT    CHAR8       **JsonWithAddendum
+  )
+{
+  REDFISH_RESOURCE_SCHEMA_INFO              SchemaInfo;
+  EDKII_JSON_VALUE                          JsonValue;
+  EFI_STATUS                                Status;
+  EFI_HANDLE                                *HandleBuffer;
+  UINTN                                     NumberOfHandles;
+  UINTN                                     Index;
+  EDKII_REDFISH_RESOURCE_ADDENDUM_PROTOCOL  *Protocol;
+
+  if (IS_EMPTY_STRING (Uri) || IS_EMPTY_STRING (Schema) || IS_EMPTY_STRING (Version) || IS_EMPTY_STRING (JsonText) || (JsonWithAddendum == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *JsonWithAddendum  = NULL;
+  SchemaInfo.Uri     = UnicodeToAscii (Uri);
+  SchemaInfo.Schema  = Schema;
+  SchemaInfo.Version = Version;
+  NumberOfHandles    = 0;
+  HandleBuffer       = NULL;
+
+  JsonValue = JsonLoadString (JsonText, 0, NULL);
+  if ((JsonValue == NULL) || !JsonValueIsObject (JsonValue)) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  Status = gBS->LocateHandleBuffer (
+                  ByProtocol,
+                  &gEdkIIRedfishResourceAddendumProtocolGuid,
+                  NULL,
+                  &NumberOfHandles,
+                  &HandleBuffer
+                  );
+  if (EFI_ERROR (Status)) {
+    goto ON_RELEASE;
+  }
+
+  for (Index = 0; Index < NumberOfHandles; Index++) {
+    Status = gBS->HandleProtocol (
+                    HandleBuffer[Index],
+                    &gEdkIIRedfishResourceAddendumProtocolGuid,
+                    (VOID **)&Protocol
+                    );
+    if (EFI_ERROR (Status)) {
+      continue;
+    }
+
+    Status = Protocol->ProvisioningCallback (Protocol, &SchemaInfo, JsonValue);
+    if (!EFI_ERROR (Status)) {
+      *JsonWithAddendum = JsonDumpString (JsonValue, EDKII_JSON_COMPACT);
+      break;
+    }
+  }
+
+ON_RELEASE:
+
+  if (HandleBuffer != NULL) {
+    FreePool (HandleBuffer);
+  }
+
+  if (JsonValue != NULL) {
+    JsonValueFree (JsonValue);
+  }
+
+  if (SchemaInfo.Uri != NULL) {
+    FreePool (SchemaInfo.Uri);
+  }
+
+  return Status;
+}
+
+/**
+  Provising redfish OEM resource with given schema information.
+
+  @param[in]   Uri             Uri of input resource.
+  @param[in]   Schema          Redfish schema string.
+  @param[in]   Version         Schema version string.
+  @param[in]   JsonText        Input resource in JSON format string.
+  @param[out]  JsonWithOem     The input resource with OEM value attached.
+
+  @retval EFI_SUCCESS              OEM data is attached.
+  @retval EFI_UNSUPPORTED          No OEM data is required in given schema.
+  @retval Others                   Some error happened.
+
+**/
+EFI_STATUS
+RedfishGetOemData (
+  IN  EFI_STRING  Uri,
+  IN  CHAR8       *Schema,
+  IN  CHAR8       *Version,
+  IN  CHAR8       *JsonText,
+  OUT CHAR8       **JsonWithOem
+  )
+{
+  REDFISH_RESOURCE_SCHEMA_INFO              SchemaInfo;
+  EDKII_JSON_VALUE                          JsonValue;
+  EDKII_JSON_VALUE                          JsonValueWithOem;
+  EFI_STATUS                                Status;
+  EFI_HANDLE                                *HandleBuffer;
+  UINTN                                     NumberOfHandles;
+  UINTN                                     Index;
+  EDKII_REDFISH_RESOURCE_ADDENDUM_PROTOCOL  *Protocol;
+
+  if (IS_EMPTY_STRING (Uri) || IS_EMPTY_STRING (Schema) || IS_EMPTY_STRING (Version) || IS_EMPTY_STRING (JsonText) || (JsonWithOem == NULL)) {
+    return EFI_INVALID_PARAMETER;
+  }
+
+  *JsonWithOem       = NULL;
+  SchemaInfo.Uri     = UnicodeToAscii (Uri);
+  SchemaInfo.Schema  = Schema;
+  SchemaInfo.Version = Version;
+  JsonValue          = NULL;
+  JsonValueWithOem   = NULL;
+  NumberOfHandles    = 0;
+  HandleBuffer       = NULL;
+
+  JsonValue = JsonLoadString (JsonText, 0, NULL);
+  if ((JsonValue == NULL) || !JsonValueIsObject (JsonValue)) {
+    return EFI_OUT_OF_RESOURCES;
+  }
+
+  JsonValueWithOem = JsonValueInitObject ();
+  if ((JsonValueWithOem == NULL) || !JsonValueIsObject (JsonValueWithOem)) {
+    Status = EFI_OUT_OF_RESOURCES;
+    goto ON_RELEASE;
+  }
+
+  Status = gBS->LocateHandleBuffer (
+                  ByProtocol,
+                  &gEdkIIRedfishResourceAddendumProtocolGuid,
+                  NULL,
+                  &NumberOfHandles,
+                  &HandleBuffer
+                  );
+  if (EFI_ERROR (Status)) {
+    goto ON_RELEASE;
+  }
+
+  for (Index = 0; Index < NumberOfHandles; Index++) {
+    Status = gBS->HandleProtocol (
+                    HandleBuffer[Index],
+                    &gEdkIIRedfishResourceAddendumProtocolGuid,
+                    (VOID **)&Protocol
+                    );
+    if (EFI_ERROR (Status)) {
+      continue;
+    }
+
+    Status = Protocol->OemCallback (Protocol, &SchemaInfo, JsonValueWithOem);
+    if (!EFI_ERROR (Status)) {
+      Status = JsonObjectSetValue (JsonValue, "Oem", JsonValueWithOem);
+      if (!EFI_ERROR (Status)) {
+        *JsonWithOem = JsonDumpString (JsonValue, EDKII_JSON_COMPACT);
+      }
+
+      break;
+    }
+  }
+
+ON_RELEASE:
+
+  if (HandleBuffer != NULL) {
+    FreePool (HandleBuffer);
+  }
+
+  if (JsonValue != NULL) {
+    JsonValueFree (JsonValue);
+  }
+
+  if (JsonValueWithOem != NULL) {
+    JsonValueFree (JsonValueWithOem);
+  }
+
+  if (SchemaInfo.Uri != NULL) {
+    FreePool (SchemaInfo.Uri);
+  }
+
+  return Status;
+}
diff --git a/RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.inf b/RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.inf
new file mode 100644
index 0000000000..7b9ef6114f
--- /dev/null
+++ b/RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.inf
@@ -0,0 +1,40 @@
+## @file
+#
+#  Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010006
+  BASE_NAME                      = RedfishAddendumLib
+  FILE_GUID                      = 7B227D39-746D-4247-8291-27B0FA79A7AF
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = RedfishAddendumLib| DXE_DRIVER UEFI_DRIVER
+
+#
+#  VALID_ARCHITECTURES           = IA32 X64 EBC
+#
+
+[Sources]
+  RedfishAddendumLib.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  RedfishPkg/RedfishPkg.dec
+  RedfishClientPkg/RedfishClientPkg.dec
+
+[LibraryClasses]
+  BaseLib
+  DebugLib
+  UefiBootServicesTableLib
+  MemoryAllocationLib
+  UefiLib
+  JsonLib
+
+[Protocols]
+  gEdkIIRedfishResourceAddendumProtocolGuid   ## CONSUMED ##
+
diff --git a/RedfishClientPkg/RedfishClientLibs.dsc.inc b/RedfishClientPkg/RedfishClientLibs.dsc.inc
index fe0c4b0885..6ce50e1980 100644
--- a/RedfishClientPkg/RedfishClientLibs.dsc.inc
+++ b/RedfishClientPkg/RedfishClientLibs.dsc.inc
@@ -6,6 +6,7 @@
 # of EDKII network library classes.
 #
 # (C) Copyright 2021-2022 Hewlett Packard Enterprise Development LP<BR>
+# Copyright (c) 2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
 #
 #    SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -37,3 +38,4 @@
   EdkIIRedfishResourceConfigLib|RedfishClientPkg/Library/EdkIIRedfishResourceConfigLib/EdkIIRedfishResourceConfigLib.inf
   RedfishEventLib|RedfishClientPkg/Library/RedfishEventLib/RedfishEventLib.inf
   RedfishVersionLib|RedfishClientPkg/Library/RedfishVersionLib/RedfishVersionLib.inf
+  RedfishAddendumLib|RedfishClientPkg/Library/RedfishAddendumLib/RedfishAddendumLib.inf
-- 
2.38.1.windows.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-12-12  3:15 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-12-07 14:41 [edk2-staging][PATCH 2/3] edk2-staging/RedfishClientPkg: Add Redfish Resource Addendum Library Nickle Wang
2022-12-12  3:15 ` Chang, Abner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox