VirtualBox

source: vbox/trunk/src/VBox/Devices/EFI/FirmwareNew/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.c@ 101291

Last change on this file since 101291 was 101291, checked in by vboxsync, 17 months ago

EFI/FirmwareNew: Make edk2-stable202308 build on all supported platforms (using gcc at least, msvc not tested yet), bugref:4643

  • Property svn:eol-style set to native
File size: 9.5 KB
Line 
1/** @file
2 Redfish debug library to debug Redfish application.
3
4 Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
5
6 SPDX-License-Identifier: BSD-2-Clause-Patent
7
8**/
9
10#include <Uefi.h>
11
12#include <Library/BaseLib.h>
13#include <Library/DebugLib.h>
14#include <Library/MemoryAllocationLib.h>
15#include <Library/RedfishDebugLib.h>
16#include <Library/UefiLib.h>
17
18#ifndef IS_EMPTY_STRING
19#define IS_EMPTY_STRING(a) ((a) == NULL || (a)[0] == '\0')
20#endif
21
22#define REDFISH_JSON_STRING_LENGTH 200
23#define REDFISH_JSON_OUTPUT_FORMAT (EDKII_JSON_COMPACT | EDKII_JSON_INDENT(2))
24
25/**
26 Debug print the value of StatementValue.
27
28 @param[in] ErrorLevel DEBUG macro error level.
29 @param[in] StatementValue The statement value to print.
30
31 @retval EFI_SUCCESS StatementValue is printed.
32 @retval EFI_INVALID_PARAMETER StatementValue is NULL.
33**/
34EFI_STATUS
35DumpHiiStatementValue (
36 IN UINTN ErrorLevel,
37 IN HII_STATEMENT_VALUE *StatementValue
38 )
39{
40 if (StatementValue == NULL) {
41 return EFI_INVALID_PARAMETER;
42 }
43
44 DEBUG ((ErrorLevel, "BufferValueType: 0x%x\n", StatementValue->BufferValueType));
45 DEBUG ((ErrorLevel, "BufferLen: 0x%x\n", StatementValue->BufferLen));
46 DEBUG ((ErrorLevel, "Buffer: 0x%p\n", StatementValue->Buffer));
47 DEBUG ((ErrorLevel, "Type: 0x%p\n", StatementValue->Type));
48
49 switch (StatementValue->Type) {
50 case EFI_IFR_TYPE_NUM_SIZE_8:
51 DEBUG ((ErrorLevel, "Value: 0x%x\n", StatementValue->Value.u8));
52 break;
53 case EFI_IFR_TYPE_NUM_SIZE_16:
54 DEBUG ((ErrorLevel, "Value: 0x%x\n", StatementValue->Value.u16));
55 break;
56 case EFI_IFR_TYPE_NUM_SIZE_32:
57 DEBUG ((ErrorLevel, "Value: 0x%x\n", StatementValue->Value.u32));
58 break;
59 case EFI_IFR_TYPE_NUM_SIZE_64:
60 DEBUG ((ErrorLevel, "Value: 0x%lx\n", StatementValue->Value.u64));
61 break;
62 case EFI_IFR_TYPE_BOOLEAN:
63 DEBUG ((ErrorLevel, "Value: %a\n", (StatementValue->Value.b ? "true" : "false")));
64 break;
65 case EFI_IFR_TYPE_STRING:
66 DEBUG ((ErrorLevel, "Value: 0x%x\n", StatementValue->Value.string));
67 break;
68 case EFI_IFR_TYPE_TIME:
69 case EFI_IFR_TYPE_DATE:
70 default:
71 break;
72 }
73
74 return EFI_SUCCESS;
75}
76
77/**
78 Debug print the value of RedfishValue.
79
80 @param[in] ErrorLevel DEBUG macro error level.
81 @param[in] RedfishValue The statement value to print.
82
83 @retval EFI_SUCCESS RedfishValue is printed.
84 @retval EFI_INVALID_PARAMETER RedfishValue is NULL.
85**/
86EFI_STATUS
87DumpRedfishValue (
88 IN UINTN ErrorLevel,
89 IN EDKII_REDFISH_VALUE *RedfishValue
90 )
91{
92 UINTN Index;
93
94 if (RedfishValue == NULL) {
95 return EFI_INVALID_PARAMETER;
96 }
97
98 DEBUG ((ErrorLevel, "Type: 0x%x\n", RedfishValue->Type));
99 DEBUG ((ErrorLevel, "ArrayCount: 0x%x\n", RedfishValue->ArrayCount));
100
101 switch (RedfishValue->Type) {
102 case RedfishValueTypeInteger:
103 DEBUG ((ErrorLevel, "Value: 0x%x\n", RedfishValue->Value.Integer));
104 break;
105 case RedfishValueTypeBoolean:
106 DEBUG ((ErrorLevel, "Value: %a\n", (RedfishValue->Value.Boolean ? "true" : "false")));
107 break;
108 case RedfishValueTypeString:
109 DEBUG ((ErrorLevel, "Value: %a\n", RedfishValue->Value.Buffer));
110 break;
111 case RedfishValueTypeStringArray:
112 for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
113 DEBUG ((ErrorLevel, "Value[%d]: %a\n", Index, RedfishValue->Value.StringArray[Index]));
114 }
115
116 break;
117 case RedfishValueTypeIntegerArray:
118 for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
119 DEBUG ((ErrorLevel, "Value[%d]: 0x%x\n", Index, RedfishValue->Value.IntegerArray[Index]));
120 }
121
122 break;
123 case RedfishValueTypeBooleanArray:
124 for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
125 DEBUG ((ErrorLevel, "Value[%d]: %a\n", Index, (RedfishValue->Value.BooleanArray[Index] ? "true" : "false")));
126 }
127
128 break;
129 case RedfishValueTypeUnknown:
130 case RedfishValueTypeMax:
131 default:
132 break;
133 }
134
135 return EFI_SUCCESS;
136}
137
138/**
139
140 This function dump the Json string in given error level.
141
142 @param[in] ErrorLevel DEBUG macro error level
143 @param[in] JsonValue Json value to dump.
144
145 @retval EFI_SUCCESS Json string is printed.
146 @retval Others Errors occur.
147
148**/
149EFI_STATUS
150DumpJsonValue (
151 IN UINTN ErrorLevel,
152 IN EDKII_JSON_VALUE JsonValue
153 )
154{
155 CHAR8 *String;
156 CHAR8 *Runner;
157 CHAR8 Buffer[REDFISH_JSON_STRING_LENGTH + 1];
158 UINTN StrLen;
159 UINTN Count;
160 UINTN Index;
161
162 if (JsonValue == NULL) {
163 return EFI_INVALID_PARAMETER;
164 }
165
166 String = JsonDumpString (JsonValue, REDFISH_JSON_OUTPUT_FORMAT);
167 if (String == NULL) {
168 return EFI_UNSUPPORTED;
169 }
170
171 StrLen = AsciiStrLen (String);
172 if (StrLen == 0) {
173 return EFI_UNSUPPORTED;
174 }
175
176 Count = StrLen / REDFISH_JSON_STRING_LENGTH;
177 Runner = String;
178 for (Index = 0; Index < Count; Index++) {
179 AsciiStrnCpyS (Buffer, (REDFISH_JSON_STRING_LENGTH + 1), Runner, REDFISH_JSON_STRING_LENGTH);
180 Buffer[REDFISH_JSON_STRING_LENGTH] = '\0';
181 DEBUG ((ErrorLevel, "%a", Buffer));
182 Runner += REDFISH_JSON_STRING_LENGTH;
183 }
184
185 Count = StrLen % REDFISH_JSON_STRING_LENGTH;
186 if (Count > 0) {
187 DEBUG ((ErrorLevel, "%a", Runner));
188 }
189
190 DEBUG ((ErrorLevel, "\n"));
191
192 FreePool (String);
193 return EFI_SUCCESS;
194}
195
196/**
197
198 This function dump the status code, header and body in given
199 Redfish payload.
200
201 @param[in] ErrorLevel DEBUG macro error level
202 @param[in] Payload Redfish payload to dump
203
204 @retval EFI_SUCCESS Redfish payload is printed.
205 @retval Others Errors occur.
206
207**/
208EFI_STATUS
209DumpRedfishPayload (
210 IN UINTN ErrorLevel,
211 IN REDFISH_PAYLOAD Payload
212 )
213{
214 EDKII_JSON_VALUE JsonValue;
215
216 if (Payload == NULL) {
217 return EFI_INVALID_PARAMETER;
218 }
219
220 JsonValue = RedfishJsonInPayload (Payload);
221 if (JsonValue != NULL) {
222 DEBUG ((ErrorLevel, "Payload:\n"));
223 DumpJsonValue (ErrorLevel, JsonValue);
224 }
225
226 return EFI_SUCCESS;
227}
228
229/**
230
231 This function dump the HTTP status code.
232
233 @param[in] ErrorLevel DEBUG macro error level
234 @param[in] HttpStatusCode HTTP status code
235
236 @retval EFI_SUCCESS HTTP status code is printed
237
238**/
239EFI_STATUS
240DumpHttpStatusCode (
241 IN UINTN ErrorLevel,
242 IN EFI_HTTP_STATUS_CODE HttpStatusCode
243 )
244{
245 switch (HttpStatusCode) {
246 case HTTP_STATUS_100_CONTINUE:
247 DEBUG ((ErrorLevel, "Status code: 100 CONTINUE\n"));
248 break;
249 case HTTP_STATUS_200_OK:
250 DEBUG ((ErrorLevel, "Status code: 200 OK\n"));
251 break;
252 case HTTP_STATUS_201_CREATED:
253 DEBUG ((ErrorLevel, "Status code: 201 CREATED\n"));
254 break;
255 case HTTP_STATUS_202_ACCEPTED:
256 DEBUG ((ErrorLevel, "Status code: 202 ACCEPTED\n"));
257 break;
258 case HTTP_STATUS_304_NOT_MODIFIED:
259 DEBUG ((ErrorLevel, "Status code: 304 NOT MODIFIED\n"));
260 break;
261 case HTTP_STATUS_400_BAD_REQUEST:
262 DEBUG ((ErrorLevel, "Status code: 400 BAD REQUEST\n"));
263 break;
264 case HTTP_STATUS_401_UNAUTHORIZED:
265 DEBUG ((ErrorLevel, "Status code: 401 UNAUTHORIZED\n"));
266 break;
267 case HTTP_STATUS_403_FORBIDDEN:
268 DEBUG ((ErrorLevel, "Status code: 403 FORBIDDEN\n"));
269 break;
270 case HTTP_STATUS_404_NOT_FOUND:
271 DEBUG ((ErrorLevel, "Status code: 404 NOT FOUND\n"));
272 break;
273 case HTTP_STATUS_405_METHOD_NOT_ALLOWED:
274 DEBUG ((ErrorLevel, "Status code: 405 METHOD NOT ALLOWED\n"));
275 break;
276 case HTTP_STATUS_500_INTERNAL_SERVER_ERROR:
277 DEBUG ((ErrorLevel, "Status code: 500 INTERNAL SERVER ERROR\n"));
278 break;
279 default:
280 DEBUG ((ErrorLevel, "Status code: 0x%x\n", HttpStatusCode));
281 break;
282 }
283
284 return EFI_SUCCESS;
285}
286
287/**
288
289 This function dump the status code, header and body in given
290 Redfish response.
291
292 @param[in] Message Message string
293 @param[in] ErrorLevel DEBUG macro error level
294 @param[in] Response Redfish response to dump
295
296 @retval EFI_SUCCESS Redfish response is printed.
297 @retval Others Errors occur.
298
299**/
300EFI_STATUS
301DumpRedfishResponse (
302 IN CONST CHAR8 *Message,
303 IN UINTN ErrorLevel,
304 IN REDFISH_RESPONSE *Response
305 )
306{
307 UINTN Index;
308
309 if (Response == NULL) {
310 return EFI_INVALID_PARAMETER;
311 }
312
313 if (!IS_EMPTY_STRING (Message)) {
314 DEBUG ((ErrorLevel, "%a\n", Message));
315 }
316
317 //
318 // status code
319 //
320 if (Response->StatusCode != NULL) {
321 DumpHttpStatusCode (ErrorLevel, *(Response->StatusCode));
322 }
323
324 //
325 // header
326 //
327 if (Response->HeaderCount > 0) {
328 DEBUG ((ErrorLevel, "Header: %d\n", Response->HeaderCount));
329 for (Index = 0; Index < Response->HeaderCount; Index++) {
330 DEBUG ((ErrorLevel, " %a: %a\n", Response->Headers[Index].FieldName, Response->Headers[Index].FieldValue));
331 }
332 }
333
334 //
335 // Body
336 //
337 if (Response->Payload != NULL) {
338 DumpRedfishPayload (ErrorLevel, Response->Payload);
339 }
340
341 return EFI_SUCCESS;
342}
343
344/**
345
346 This function dump the IPv4 address in given error level.
347
348 @param[in] ErrorLevel DEBUG macro error level
349 @param[in] Ipv4Address IPv4 address to dump
350
351 @retval EFI_SUCCESS IPv4 address string is printed.
352 @retval Others Errors occur.
353
354**/
355EFI_STATUS
356DumpIpv4Address (
357 IN UINTN ErrorLevel,
358 IN EFI_IPv4_ADDRESS *Ipv4Address
359 )
360{
361 if (Ipv4Address == NULL) {
362 return EFI_INVALID_PARAMETER;
363 }
364
365 DEBUG ((ErrorLevel, "%d.%d.%d.%d\n", Ipv4Address->Addr[0], Ipv4Address->Addr[1], Ipv4Address->Addr[2], Ipv4Address->Addr[3]));
366
367 return EFI_SUCCESS;
368}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette