summaryrefslogtreecommitdiffstats
path: root/RedfishPkg/Library/RedfishDebugLib/RedfishDebugLib.c
blob: 3728f512139511384e9722678c9613c6b471a6d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/** @file
  Redfish debug library to debug Redfish application.

  Copyright (c) 2023-2024, 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))
#define REDFISH_PRINT_BUFFER_BYTES_PER_ROW  16

/**
  Debug print the value of StatementValue.

  @param[in]  ErrorLevel     DEBUG macro error level.
  @param[in]  StatementValue The statement value to print.

  @retval     EFI_SUCCESS            StatementValue is printed.
  @retval     EFI_INVALID_PARAMETER  StatementValue is NULL.
**/
EFI_STATUS
DumpHiiStatementValue (
  IN UINTN                ErrorLevel,
  IN HII_STATEMENT_VALUE  *StatementValue
  )
{
  if (StatementValue == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  DEBUG ((ErrorLevel, "BufferValueType: 0x%x\n", StatementValue->BufferValueType));
  DEBUG ((ErrorLevel, "BufferLen: 0x%x\n", StatementValue->BufferLen));
  DEBUG ((ErrorLevel, "Buffer:    0x%p\n", StatementValue->Buffer));
  DEBUG ((ErrorLevel, "Type:      0x%p\n", StatementValue->Type));

  switch (StatementValue->Type) {
    case EFI_IFR_TYPE_NUM_SIZE_8:
      DEBUG ((ErrorLevel, "Value:     0x%x\n", StatementValue->Value.u8));
      break;
    case EFI_IFR_TYPE_NUM_SIZE_16:
      DEBUG ((ErrorLevel, "Value:     0x%x\n", StatementValue->Value.u16));
      break;
    case EFI_IFR_TYPE_NUM_SIZE_32:
      DEBUG ((ErrorLevel, "Value:     0x%x\n", StatementValue->Value.u32));
      break;
    case EFI_IFR_TYPE_NUM_SIZE_64:
      DEBUG ((ErrorLevel, "Value:     0x%lx\n", StatementValue->Value.u64));
      break;
    case EFI_IFR_TYPE_BOOLEAN:
      DEBUG ((ErrorLevel, "Value:     %a\n", (StatementValue->Value.b ? "true" : "false")));
      break;
    case EFI_IFR_TYPE_STRING:
      DEBUG ((ErrorLevel, "Value:     0x%x\n", StatementValue->Value.string));
      break;
    case EFI_IFR_TYPE_TIME:
    case EFI_IFR_TYPE_DATE:
    default:
      break;
  }

  return EFI_SUCCESS;
}

/**
  Debug print the value of RedfishValue.

  @param[in]  ErrorLevel     DEBUG macro error level.
  @param[in]  RedfishValue   The statement value to print.

  @retval     EFI_SUCCESS            RedfishValue is printed.
  @retval     EFI_INVALID_PARAMETER  RedfishValue is NULL.
**/
EFI_STATUS
DumpRedfishValue (
  IN UINTN                ErrorLevel,
  IN EDKII_REDFISH_VALUE  *RedfishValue
  )
{
  UINTN  Index;

  if (RedfishValue == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  DEBUG ((ErrorLevel, "Type:       0x%x\n", RedfishValue->Type));
  DEBUG ((ErrorLevel, "ArrayCount: 0x%x\n", RedfishValue->ArrayCount));

  switch (RedfishValue->Type) {
    case RedfishValueTypeInteger:
      DEBUG ((ErrorLevel, "Value:      0x%x\n", RedfishValue->Value.Integer));
      break;
    case RedfishValueTypeBoolean:
      DEBUG ((ErrorLevel, "Value:      %a\n", (RedfishValue->Value.Boolean ? "true" : "false")));
      break;
    case RedfishValueTypeString:
      DEBUG ((ErrorLevel, "Value:      %a\n", RedfishValue->Value.Buffer));
      break;
    case RedfishValueTypeStringArray:
      for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
        DEBUG ((ErrorLevel, "Value[%d]:      %a\n", Index, RedfishValue->Value.StringArray[Index]));
      }

      break;
    case RedfishValueTypeIntegerArray:
      for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
        DEBUG ((ErrorLevel, "Value[%d]:      0x%x\n", Index, RedfishValue->Value.IntegerArray[Index]));
      }

      break;
    case RedfishValueTypeBooleanArray:
      for (Index = 0; Index < RedfishValue->ArrayCount; Index++) {
        DEBUG ((ErrorLevel, "Value[%d]:      %a\n", Index, (RedfishValue->Value.BooleanArray[Index] ? "true" : "false")));
      }

      break;
    case RedfishValueTypeUnknown:
    case RedfishValueTypeMax:
    default:
      break;
  }

  return EFI_SUCCESS;
}

/**

  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;
}

/**

  This function dump the IPv4 address in given error level.

  @param[in]  ErrorLevel  DEBUG macro error level
  @param[in]  Ipv4Address IPv4 address to dump

  @retval     EFI_SUCCESS         IPv4 address string is printed.
  @retval     Others              Errors occur.

**/
EFI_STATUS
DumpIpv4Address (
  IN UINTN             ErrorLevel,
  IN EFI_IPv4_ADDRESS  *Ipv4Address
  )
{
  if (Ipv4Address == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  DEBUG ((ErrorLevel, "%d.%d.%d.%d\n", Ipv4Address->Addr[0], Ipv4Address->Addr[1], Ipv4Address->Addr[2], Ipv4Address->Addr[3]));

  return EFI_SUCCESS;
}

/**
  Debug output raw data buffer.

  @param[in]    ErrorLevel  DEBUG macro error level
  @param[in]    Buffer      Debug output data buffer.
  @param[in]    BufferSize  The size of Buffer in byte.

  @retval EFI_SUCCESS             Debug dump finished.
  @retval EFI_INVALID_PARAMETER   Buffer is NULL.

**/
EFI_STATUS
DumpBuffer (
  IN  UINTN  ErrorLevel,
  IN  UINT8  *Buffer,
  IN  UINTN  BufferSize
  )
{
  UINTN  Index;

  if (Buffer == NULL) {
    return EFI_INVALID_PARAMETER;
  }

  DEBUG ((ErrorLevel, "Address: 0x%p size: %d\n", Buffer, BufferSize));
  for (Index = 0; Index < BufferSize; Index++) {
    if (Index % REDFISH_PRINT_BUFFER_BYTES_PER_ROW == 0) {
      DEBUG ((ErrorLevel, "\n%04X: ", Index));
    }

    DEBUG ((ErrorLevel, "%02X ", Buffer[Index]));
  }

  DEBUG ((ErrorLevel, "\n"));

  return EFI_SUCCESS;
}