]> git.proxmox.com Git - mirror_edk2.git/blame - RedfishPkg/PrivateLibrary/RedfishLib/RedfishLib.c
RedfishPkg: Redfish functions for REST requests are not fully spec complied
[mirror_edk2.git] / RedfishPkg / PrivateLibrary / RedfishLib / RedfishLib.c
CommitLineData
4751a48a
AC
1/** @file\r
2 Provides a set of utility APIs that allow to create/read/update/delete\r
3 (CRUD) Redfish resources and provide basic query.\r
4\r
5 Copyright (c) 2019, Intel Corporation. All rights reserved.<BR>\r
6 (C) Copyright 2021 Hewlett Packard Enterprise Development LP<BR>\r
7\r
8 SPDX-License-Identifier: BSD-2-Clause-Patent\r
9\r
10**/\r
11\r
12#include "RedfishMisc.h"\r
13\r
14/**\r
15 This function uses REST EX protocol provided in RedfishConfigServiceInfo.\r
16 The service enumerator will also handle the authentication flow automatically\r
17 if HTTP basic auth or Redfish session login is configured to use.\r
18\r
19 Callers are responsible for freeing the returned service by RedfishCleanupService().\r
20\r
21 @param[in] RedfishConfigServiceInfo Redfish service information the EFI Redfish\r
22 feature driver communicates with.\r
23\r
24 @return New created Redfish Service, or NULL if error happens.\r
25\r
26**/\r
27REDFISH_SERVICE\r
28EFIAPI\r
29RedfishCreateService (\r
39de741e 30 IN REDFISH_CONFIG_SERVICE_INFORMATION *RedfishConfigServiceInfo\r
4751a48a
AC
31 )\r
32{\r
33 REDFISH_SERVICE RedfishService;\r
34 EDKII_REDFISH_AUTH_METHOD AuthMethod;\r
35 CHAR8 *UserId;\r
36 CHAR8 *Password;\r
37 EFI_STATUS Status;\r
38\r
39 RedfishService = NULL;\r
40 UserId = NULL;\r
41 Password = NULL;\r
42\r
43 //\r
44 // Check Input Parameters.\r
45 //\r
46 if (RedfishConfigServiceInfo == NULL) {\r
47 return NULL;\r
48 }\r
49\r
50 //\r
51 // Get Authentication Configuration.\r
52 //\r
53 Status = RedfishGetAuthInfo (&AuthMethod, &UserId, &Password);\r
54 if (EFI_ERROR (Status)) {\r
55 goto ON_EXIT;\r
56 }\r
57\r
58 //\r
59 // Create a redfish service node based on Redfish network host interface.\r
60 //\r
61 RedfishService = RedfishCreateLibredfishService (\r
62 RedfishConfigServiceInfo,\r
63 AuthMethod,\r
64 UserId,\r
65 Password\r
66 );\r
67\r
68ON_EXIT:\r
69 if (UserId != NULL) {\r
70 FreePool (UserId);\r
71 }\r
39de741e
MK
72\r
73 if (Password != NULL) {\r
4751a48a
AC
74 FreePool (Password);\r
75 }\r
76\r
77 return RedfishService;\r
78}\r
79\r
80/**\r
81 Free the Service and all its related resources.\r
82\r
83 @param[in] RedfishService The Service to access the Redfish resources.\r
84\r
85**/\r
86VOID\r
87EFIAPI\r
88RedfishCleanupService (\r
39de741e 89 IN REDFISH_SERVICE RedfishService\r
4751a48a
AC
90 )\r
91{\r
92 if (RedfishService == NULL) {\r
93 return;\r
94 }\r
95\r
96 cleanupServiceEnumerator (RedfishService);\r
97}\r
39de741e 98\r
4751a48a
AC
99/**\r
100 Create REDFISH_PAYLOAD instance in local with JSON represented resource value and\r
101 the Redfish Service.\r
102\r
103 The returned REDFISH_PAYLOAD can be used to create or update Redfish resource in\r
104 server side.\r
105\r
106 Callers are responsible for freeing the returned payload by RedfishCleanupPayload().\r
107\r
108 @param[in] Value JSON Value of the redfish resource.\r
109 @param[in] RedfishService The Service to access the Redfish resources.\r
110\r
111 @return REDFISH_PAYLOAD instance of the resource, or NULL if error happens.\r
112\r
113**/\r
114REDFISH_PAYLOAD\r
115EFIAPI\r
116RedfishCreatePayload (\r
39de741e
MK
117 IN EDKII_JSON_VALUE Value,\r
118 IN REDFISH_SERVICE RedfishService\r
4751a48a
AC
119 )\r
120{\r
39de741e 121 EDKII_JSON_VALUE CopyValue;\r
4751a48a
AC
122\r
123 CopyValue = JsonValueClone (Value);\r
124 return createRedfishPayload (CopyValue, RedfishService);\r
125}\r
126\r
127/**\r
128 Free the RedfishPayload and all its related resources.\r
129\r
130 @param[in] Payload Payload to be freed.\r
131\r
132**/\r
133VOID\r
134EFIAPI\r
135RedfishCleanupPayload (\r
39de741e 136 IN REDFISH_PAYLOAD Payload\r
4751a48a
AC
137 )\r
138{\r
139 if (Payload == NULL) {\r
140 return;\r
141 }\r
142\r
39de741e 143 cleanupPayload ((redfishPayload *)Payload);\r
4751a48a
AC
144}\r
145\r
146/**\r
147 This function returns the decoded JSON value of a REDFISH_PAYLOAD.\r
148\r
149 Caller doesn't need to free the returned JSON value because it will be released\r
150 in corresponding RedfishCleanupPayload() function.\r
151\r
152 @param[in] Payload A REDFISH_PAYLOAD instance.\r
153\r
154 @return Decoded JSON value of the payload.\r
155\r
156**/\r
157EDKII_JSON_VALUE\r
158EFIAPI\r
159RedfishJsonInPayload (\r
39de741e 160 IN REDFISH_PAYLOAD Payload\r
4751a48a
AC
161 )\r
162{\r
163 if (Payload == NULL) {\r
164 return NULL;\r
165 }\r
166\r
39de741e 167 return ((redfishPayload *)Payload)->json;\r
4751a48a
AC
168}\r
169\r
170/**\r
171 Fill the input RedPath string with system UUID from SMBIOS table or use the customized\r
172 ID if FromSmbios == FALSE.\r
173\r
174 This is a helper function to build a RedPath string which can be used to address\r
175 a Redfish resource for this computer system. The input PathString must have a Systems\r
176 note in format of "Systems[UUID=%g]" or "Systems[UUID~%g]" to fill the UUID value.\r
177\r
178 Example:\r
179 Use "/v1/Systems[UUID=%g]/Bios" to build a RedPath to address the "Bios" resource\r
180 for this computer system.\r
181\r
182 @param[in] RedPath RedPath format to be build.\r
183 @param[in] FromSmbios Get system UUID from SMBIOS as computer system instance ID.\r
184 @param[in] IdString The computer system instance ID.\r
185\r
186 @return Full RedPath with system UUID inside, or NULL if error happens.\r
187\r
188**/\r
189CHAR8 *\r
190EFIAPI\r
191RedfishBuildPathWithSystemUuid (\r
39de741e
MK
192 IN CONST CHAR8 *RedPath,\r
193 IN BOOLEAN FromSmbios,\r
194 IN CHAR8 *IdString OPTIONAL\r
4751a48a
AC
195 )\r
196{\r
39de741e
MK
197 UINTN BufSize;\r
198 CHAR8 *RetRedPath;\r
199 EFI_GUID SystemUuid;\r
200 EFI_STATUS Status;\r
4751a48a
AC
201\r
202 if (RedPath == NULL) {\r
203 return NULL;\r
204 }\r
205\r
206 //\r
207 // Find system UUID from SMBIOS table.\r
208 //\r
209 if (FromSmbios) {\r
39de741e 210 Status = NetLibGetSystemGuid (&SystemUuid);\r
4751a48a
AC
211 if (EFI_ERROR (Status)) {\r
212 return NULL;\r
213 }\r
39de741e 214\r
4751a48a
AC
215 // AsciiStrLen ("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") = 36\r
216 BufSize = AsciiStrSize (RedPath) + AsciiStrLen ("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX");\r
217 } else {\r
218 BufSize = AsciiStrSize (RedPath) + AsciiStrLen (IdString);\r
219 }\r
220\r
221 RetRedPath = AllocateZeroPool (BufSize);\r
222 if (RetRedPath == NULL) {\r
223 return NULL;\r
224 }\r
39de741e 225\r
4751a48a
AC
226 if (FromSmbios) {\r
227 AsciiSPrint (RetRedPath, BufSize, RedPath, &SystemUuid);\r
228 } else {\r
229 AsciiSPrint (RetRedPath, BufSize, RedPath, IdString);\r
230 }\r
39de741e 231\r
4751a48a
AC
232 return RetRedPath;\r
233}\r
39de741e 234\r
4751a48a
AC
235/**\r
236 Get a redfish response addressed by a RedPath string, including HTTP StatusCode, Headers\r
237 and Payload which record any HTTP response messages.\r
238\r
239 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
240 redfish response data.\r
241\r
242 @param[in] RedfishService The Service to access the Redfish resources.\r
243 @param[in] RedPath RedPath string to address a resource, must start\r
244 from the root node.\r
245 @param[out] RedResponse Pointer to the Redfish response data.\r
246\r
247 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
248 NULL and the value is 2XX. The corresponding redfish resource has\r
249 been returned in Payload within RedResponse.\r
250 @retval EFI_INVALID_PARAMETER RedfishService, RedPath, or RedResponse is NULL.\r
251 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
252 more error info from returned HTTP StatusCode, Headers and Payload\r
253 within RedResponse:\r
254 1. If the returned Payload is NULL, indicates any error happen.\r
255 2. If the returned StatusCode is NULL, indicates any error happen.\r
256 3. If the returned StatusCode is not 2XX, indicates any error happen.\r
257**/\r
258EFI_STATUS\r
259EFIAPI\r
260RedfishGetByService (\r
39de741e
MK
261 IN REDFISH_SERVICE RedfishService,\r
262 IN CONST CHAR8 *RedPath,\r
263 OUT REDFISH_RESPONSE *RedResponse\r
4751a48a
AC
264 )\r
265{\r
39de741e 266 if ((RedfishService == NULL) || (RedPath == NULL) || (RedResponse == NULL)) {\r
4751a48a
AC
267 return EFI_INVALID_PARAMETER;\r
268 }\r
269\r
270 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
271\r
39de741e 272 RedResponse->Payload = (REDFISH_PAYLOAD)getPayloadByPath (RedfishService, RedPath, &(RedResponse->StatusCode));\r
4751a48a
AC
273\r
274 //\r
275 // 1. If the returned Payload is NULL, indicates any error happen.\r
276 // 2. If the returned StatusCode is NULL, indicates any error happen.\r
277 //\r
39de741e 278 if ((RedResponse->Payload == NULL) || (RedResponse->StatusCode == NULL)) {\r
4751a48a
AC
279 return EFI_DEVICE_ERROR;\r
280 }\r
281\r
282 //\r
283 // 3. If the returned StatusCode is not 2XX, indicates any error happen.\r
284 // NOTE: If there is any error message returned from server, it will be returned in\r
285 // Payload within RedResponse.\r
286 //\r
39de741e
MK
287 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
288 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
289 {\r
4751a48a
AC
290 return EFI_DEVICE_ERROR;\r
291 }\r
292\r
293 return EFI_SUCCESS;\r
294}\r
39de741e 295\r
4751a48a
AC
296/**\r
297 Get a redfish response addressed by URI, including HTTP StatusCode, Headers\r
298 and Payload which record any HTTP response messages.\r
299\r
300 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
301 redfish response data.\r
302\r
303 @param[in] RedfishService The Service to access the URI resources.\r
304 @param[in] Uri String to address a resource.\r
305 @param[out] RedResponse Pointer to the Redfish response data.\r
306\r
307 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
308 NULL and the value is 2XX. The corresponding redfish resource has\r
309 been returned in Payload within RedResponse.\r
310 @retval EFI_INVALID_PARAMETER RedfishService, RedPath, or RedResponse is NULL.\r
311 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
312 more error info from returned HTTP StatusCode, Headers and Payload\r
313 within RedResponse:\r
314 1. If the returned Payload is NULL, indicates any error happen.\r
315 2. If the returned StatusCode is NULL, indicates any error happen.\r
316 3. If the returned StatusCode is not 2XX, indicates any error happen.\r
317**/\r
318EFI_STATUS\r
319EFIAPI\r
320RedfishGetByUri (\r
39de741e
MK
321 IN REDFISH_SERVICE RedfishService,\r
322 IN CONST CHAR8 *Uri,\r
323 OUT REDFISH_RESPONSE *RedResponse\r
4751a48a
AC
324 )\r
325{\r
39de741e 326 EDKII_JSON_VALUE JsonValue;\r
4751a48a 327\r
39de741e 328 if ((RedfishService == NULL) || (Uri == NULL) || (RedResponse == NULL)) {\r
4751a48a
AC
329 return EFI_INVALID_PARAMETER;\r
330 }\r
331\r
332 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
333\r
39de741e
MK
334 JsonValue = getUriFromService (RedfishService, Uri, &RedResponse->StatusCode);\r
335 RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);\r
4751a48a
AC
336\r
337 //\r
338 // 1. If the returned Payload is NULL, indicates any error happen.\r
339 // 2. If the returned StatusCode is NULL, indicates any error happen.\r
340 //\r
39de741e 341 if ((RedResponse->Payload == NULL) || (RedResponse->StatusCode == NULL)) {\r
4751a48a
AC
342 return EFI_DEVICE_ERROR;\r
343 }\r
344\r
345 //\r
346 // 3. If the returned StatusCode is not 2XX, indicates any error happen.\r
347 // NOTE: If there is any error message returned from server, it will be returned in\r
348 // Payload within RedResponse.\r
349 //\r
39de741e
MK
350 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
351 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
352 {\r
4751a48a
AC
353 return EFI_DEVICE_ERROR;\r
354 }\r
39de741e 355\r
4751a48a
AC
356 return EFI_SUCCESS;\r
357}\r
39de741e 358\r
4751a48a
AC
359/**\r
360 Get a redfish response addressed by the input Payload and relative RedPath string,\r
361 including HTTP StatusCode, Headers and Payload which record any HTTP response messages.\r
362\r
363 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
364 redfish response data.\r
365\r
366 @param[in] Payload A existing REDFISH_PAYLOAD instance.\r
367 @param[in] RedPath Relative RedPath string to address a resource inside Payload.\r
368 @param[out] RedResponse Pointer to the Redfish response data.\r
369\r
370 @retval EFI_SUCCESS The opeartion is successful:\r
371 1. The HTTP StatusCode is NULL and the returned Payload in\r
372 RedResponse is not NULL, indicates the Redfish resource has\r
373 been parsed from the input payload directly.\r
374 2. The HTTP StatusCode is not NULL and the value is 2XX,\r
375 indicates the corresponding redfish resource has been returned\r
376 in Payload within RedResponse.\r
377 @retval EFI_INVALID_PARAMETER Payload, RedPath, or RedResponse is NULL.\r
378 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
379 more error info from returned HTTP StatusCode, Headers and Payload\r
380 within RedResponse:\r
381 1. If the returned Payload is NULL, indicates any error happen.\r
382 2. If StatusCode is not NULL and the returned value of StatusCode\r
383 is not 2XX, indicates any error happen.\r
384**/\r
385EFI_STATUS\r
386EFIAPI\r
387RedfishGetByPayload (\r
39de741e
MK
388 IN REDFISH_PAYLOAD Payload,\r
389 IN CONST CHAR8 *RedPath,\r
390 OUT REDFISH_RESPONSE *RedResponse\r
4751a48a
AC
391 )\r
392{\r
39de741e 393 if ((Payload == NULL) || (RedPath == NULL) || (RedResponse == NULL)) {\r
4751a48a
AC
394 return EFI_INVALID_PARAMETER;\r
395 }\r
396\r
397 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
398\r
39de741e 399 RedResponse->Payload = (REDFISH_PAYLOAD)getPayloadForPathString (Payload, RedPath, &(RedResponse->StatusCode));\r
4751a48a
AC
400\r
401 //\r
402 // 1. If the returned Payload is NULL, indicates any error happen.\r
403 //\r
404 if (RedResponse->Payload == NULL) {\r
405 return EFI_DEVICE_ERROR;\r
406 }\r
407\r
408 //\r
409 // 2. If StatusCode is not NULL and the returned value of StatusCode is not 2XX, indicates any\r
410 // error happen.\r
411 // NOTE: If there is any error message returned from server, it will be returned in\r
412 // Payload within RedResponse.\r
413 //\r
39de741e
MK
414 if ((RedResponse->StatusCode != NULL) && \\r
415 ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
416 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT)\r
417 ))\r
418 {\r
4751a48a
AC
419 return EFI_DEVICE_ERROR;\r
420 }\r
421\r
422 return EFI_SUCCESS;\r
423}\r
39de741e 424\r
4751a48a
AC
425/**\r
426 Use HTTP PATCH to perform updates on pre-existing Redfish resource.\r
427\r
428 This function uses the RedfishService to patch a Redfish resource addressed by\r
429 Uri (only the relative path is required). Changes to one or more properties within\r
430 the target resource are represented in the input Content, properties not specified\r
431 in Content won't be changed by this request. The corresponding redfish response will\r
432 returned, including HTTP StatusCode, Headers and Payload which record any HTTP response\r
433 messages.\r
434\r
435 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
436 redfish response data.\r
437\r
438 @param[in] RedfishService The Service to access the Redfish resources.\r
439 @param[in] Uri Relative path to address the resource.\r
440 @param[in] Content JSON represented properties to be update.\r
441 @param[out] RedResponse Pointer to the Redfish response data.\r
442\r
443 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
444 NULL and the value is 2XX. The Redfish resource will be returned\r
445 in Payload within RedResponse if server send it back in the HTTP\r
446 response message body.\r
447 @retval EFI_INVALID_PARAMETER RedfishService, Uri, Content, or RedResponse is NULL.\r
448 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
449 more error info from returned HTTP StatusCode, Headers and Payload\r
450 within RedResponse:\r
451 1. If the returned StatusCode is NULL, indicates any error happen.\r
452 2. If the returned StatusCode is not NULL and the value is not 2XX,\r
453 indicates any error happen.\r
454**/\r
455EFI_STATUS\r
456EFIAPI\r
457RedfishPatchToUri (\r
39de741e
MK
458 IN REDFISH_SERVICE RedfishService,\r
459 IN CONST CHAR8 *Uri,\r
460 IN CONST CHAR8 *Content,\r
461 OUT REDFISH_RESPONSE *RedResponse\r
4751a48a
AC
462 )\r
463{\r
39de741e
MK
464 EFI_STATUS Status;\r
465 EDKII_JSON_VALUE JsonValue;\r
4751a48a
AC
466\r
467 Status = EFI_SUCCESS;\r
468 JsonValue = NULL;\r
469\r
39de741e 470 if ((RedfishService == NULL) || (Uri == NULL) || (Content == NULL) || (RedResponse == NULL)) {\r
4751a48a
AC
471 return EFI_INVALID_PARAMETER;\r
472 }\r
473\r
474 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
475\r
39de741e
MK
476 JsonValue = (EDKII_JSON_VALUE)patchUriFromService (\r
477 RedfishService,\r
478 Uri,\r
479 Content,\r
480 &(RedResponse->StatusCode)\r
481 );\r
4751a48a
AC
482\r
483 //\r
484 // 1. If the returned StatusCode is NULL, indicates any error happen.\r
485 //\r
486 if (RedResponse->StatusCode == NULL) {\r
487 Status = EFI_DEVICE_ERROR;\r
488 goto ON_EXIT;\r
489 }\r
490\r
491 //\r
492 // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
493 // NOTE: If there is any error message returned from server, it will be returned in\r
494 // Payload within RedResponse.\r
495 //\r
39de741e
MK
496 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
497 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
498 {\r
4751a48a
AC
499 Status = EFI_DEVICE_ERROR;\r
500 }\r
501\r
502ON_EXIT:\r
503 if (JsonValue != NULL) {\r
504 RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);\r
505 if (RedResponse->Payload == NULL) {\r
506 //\r
507 // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what\r
508 // we care about if the returned StatusCode is 2XX.\r
509 //\r
510 JsonValueFree (JsonValue);\r
511 }\r
512 }\r
513\r
514 return Status;\r
515}\r
39de741e 516\r
4751a48a
AC
517/**\r
518 Use HTTP PATCH to perform updates on target payload. Patch to odata.id in Payload directly.\r
519\r
520 This function uses the Payload to patch the Target. Changes to one or more properties\r
521 within the target resource are represented in the input Payload, properties not specified\r
522 in Payload won't be changed by this request. The corresponding redfish response will\r
523 returned, including HTTP StatusCode, Headers and Payload which record any HTTP response\r
524 messages.\r
525\r
526 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
527 redfish response data.\r
528\r
529 @param[in] Target The target payload to be updated.\r
530 @param[in] Payload Palyoad with properties to be changed.\r
531 @param[out] RedResponse Pointer to the Redfish response data.\r
532\r
533 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
534 NULL and the value is 2XX. The Redfish resource will be returned\r
535 in Payload within RedResponse if server send it back in the HTTP\r
536 response message body.\r
537 @retval EFI_INVALID_PARAMETER Target, Payload, or RedResponse is NULL.\r
538 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
539 more error info from returned HTTP StatusCode, Headers and Payload\r
540 within RedResponse:\r
541 1. If the returned StatusCode is NULL, indicates any error happen.\r
542 2. If the returned StatusCode is not NULL and the value is not 2XX,\r
543 indicates any error happen.\r
544**/\r
545EFI_STATUS\r
546EFIAPI\r
547RedfishPatchToPayload (\r
39de741e
MK
548 IN REDFISH_PAYLOAD Target,\r
549 IN REDFISH_PAYLOAD Payload,\r
550 OUT REDFISH_RESPONSE *RedResponse\r
4751a48a
AC
551 )\r
552{\r
39de741e 553 if ((Target == NULL) || (Payload == NULL) || (RedResponse == NULL)) {\r
4751a48a
AC
554 return EFI_INVALID_PARAMETER;\r
555 }\r
556\r
557 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
558\r
39de741e
MK
559 RedResponse->Payload = (REDFISH_PAYLOAD)patchPayload (\r
560 Target,\r
561 Payload,\r
562 &(RedResponse->StatusCode)\r
563 );\r
4751a48a
AC
564\r
565 //\r
566 // 1. If the returned StatusCode is NULL, indicates any error happen.\r
567 //\r
568 if (RedResponse->StatusCode == NULL) {\r
569 return EFI_DEVICE_ERROR;\r
570 }\r
571\r
572 //\r
573 // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
574 // NOTE: If there is any error message returned from server, it will be returned in\r
575 // Payload within RedResponse.\r
576 //\r
39de741e
MK
577 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
578 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
579 {\r
4751a48a
AC
580 return EFI_DEVICE_ERROR;\r
581 }\r
582\r
583 return EFI_SUCCESS;\r
584}\r
39de741e 585\r
f2bf043a
AC
586/**\r
587 Use HTTP POST to create new Redfish resource in the Resource Collection.\r
588\r
589 The POST request should be submitted to the Resource Collection in which the new resource\r
590 is to belong. The Resource Collection is addressed by URI. The Redfish may\r
591 ignore any service controlled properties. The corresponding redfish response will returned,\r
592 including HTTP StatusCode, Headers and Payload which record any HTTP response messages.\r
593\r
594 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
595 redfish response data.\r
596\r
597 @param[in] RedfishService The Service to access the Redfish resources.\r
598 @param[in] Uri Relative path to address the resource.\r
599 @param[in] Content JSON represented properties to be update.\r
600 @param[in] ContentSize Size of the Content to be send to Redfish service\r
601 @param[in] ContentType Type of the Content to be send to Redfish service\r
602 @param[out] RedResponse Pointer to the Redfish response data.\r
603\r
604 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
605 NULL and the value is 2XX. The Redfish resource will be returned\r
606 in Payload within RedResponse if server send it back in the HTTP\r
607 response message body.\r
608 @retval EFI_INVALID_PARAMETER RedfishService, Uri, Content, or RedResponse is NULL.\r
609 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
610 more error info from returned HTTP StatusCode, Headers and Payload\r
611 within RedResponse:\r
612 1. If the returned StatusCode is NULL, indicates any error happen.\r
613 2. If the returned StatusCode is not NULL and the value is not 2XX,\r
614 indicates any error happen.\r
615**/\r
616EFI_STATUS\r
617EFIAPI\r
618RedfishPostToUri (\r
619 IN REDFISH_SERVICE RedfishService,\r
620 IN CONST CHAR8 *Uri,\r
621 IN CONST CHAR8 *Content,\r
622 IN UINTN ContentSize,\r
623 IN CONST CHAR8 *ContentType,\r
624 OUT REDFISH_RESPONSE *RedResponse\r
625 )\r
626{\r
627 EFI_STATUS Status;\r
628 EDKII_JSON_VALUE JsonValue;\r
629\r
630 Status = EFI_SUCCESS;\r
631 JsonValue = NULL;\r
632\r
633 if ((RedfishService == NULL) || (Uri == NULL) || (Content == NULL) || (RedResponse == NULL)) {\r
634 return EFI_INVALID_PARAMETER;\r
635 }\r
636\r
637 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
638\r
639 JsonValue = (EDKII_JSON_VALUE)postUriFromService (\r
640 RedfishService,\r
641 Uri,\r
642 Content,\r
643 ContentSize,\r
644 ContentType,\r
645 &(RedResponse->StatusCode)\r
646 );\r
647\r
648 //\r
649 // 1. If the returned StatusCode is NULL, indicates any error happen.\r
650 //\r
651 if (RedResponse->StatusCode == NULL) {\r
652 Status = EFI_DEVICE_ERROR;\r
653 goto ON_EXIT;\r
654 }\r
655\r
656 //\r
657 // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
658 // NOTE: If there is any error message returned from server, it will be returned in\r
659 // Payload within RedResponse.\r
660 //\r
661 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
662 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
663 {\r
664 Status = EFI_DEVICE_ERROR;\r
665 }\r
666\r
667ON_EXIT:\r
668 if (JsonValue != NULL) {\r
669 RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);\r
670 if (RedResponse->Payload == NULL) {\r
671 //\r
672 // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what\r
673 // we care about if the returned StatusCode is 2XX.\r
674 //\r
675 JsonValueFree (JsonValue);\r
676 }\r
677 }\r
678\r
679 return Status;\r
680}\r
681\r
4751a48a
AC
682/**\r
683 Use HTTP POST to create a new resource in target payload.\r
684\r
685 The POST request should be submitted to the Resource Collection in which the new resource\r
686 is to belong. The Resource Collection is addressed by Target payload. The Redfish may\r
687 ignore any service controlled properties. The corresponding redfish response will returned,\r
688 including HTTP StatusCode, Headers and Payload which record any HTTP response messages.\r
689\r
690 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
691 redfish response data.\r
692\r
693 @param[in] Target Target payload of the Resource Collection.\r
694 @param[in] Payload The new resource to be created.\r
695 @param[out] RedResponse Pointer to the Redfish response data.\r
696\r
697 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
698 NULL and the value is 2XX. The Redfish resource will be returned\r
699 in Payload within RedResponse if server send it back in the HTTP\r
700 response message body.\r
701 @retval EFI_INVALID_PARAMETER Target, Payload, or RedResponse is NULL.\r
702 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
703 more error info from returned HTTP StatusCode, Headers and Payload\r
704 within RedResponse:\r
705 1. If the returned StatusCode is NULL, indicates any error happen.\r
706 2. If the returned StatusCode is not NULL and the value is not 2XX,\r
707 indicates any error happen.\r
708**/\r
709EFI_STATUS\r
710EFIAPI\r
711RedfishPostToPayload (\r
39de741e
MK
712 IN REDFISH_PAYLOAD Target,\r
713 IN REDFISH_PAYLOAD Payload,\r
714 OUT REDFISH_RESPONSE *RedResponse\r
4751a48a
AC
715 )\r
716{\r
39de741e 717 if ((Target == NULL) || (Payload == NULL) || (RedResponse == NULL)) {\r
4751a48a
AC
718 return EFI_INVALID_PARAMETER;\r
719 }\r
720\r
721 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
722\r
39de741e
MK
723 RedResponse->Payload = (REDFISH_PAYLOAD)postPayload (\r
724 Target,\r
725 Payload,\r
726 &(RedResponse->StatusCode)\r
727 );\r
4751a48a
AC
728\r
729 //\r
730 // 1. If the returned StatusCode is NULL, indicates any error happen.\r
731 //\r
732 if (RedResponse->StatusCode == NULL) {\r
733 return EFI_DEVICE_ERROR;\r
734 }\r
735\r
736 //\r
737 // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
738 // NOTE: If there is any error message returned from server, it will be returned in\r
739 // Payload within RedResponse.\r
740 //\r
39de741e
MK
741 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
742 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
743 {\r
4751a48a
AC
744 return EFI_DEVICE_ERROR;\r
745 }\r
746\r
747 return EFI_SUCCESS;\r
748}\r
39de741e 749\r
4751a48a
AC
750/**\r
751 Use HTTP DELETE to remove a resource.\r
752\r
753 This function uses the RedfishService to remove a Redfish resource which is addressed\r
754 by input Uri (only the relative path is required). The corresponding redfish response will\r
755 returned, including HTTP StatusCode, Headers and Payload which record any HTTP response\r
756 messages.\r
757\r
758 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
759 redfish response data.\r
760\r
761 @param[in] RedfishService The Service to access the Redfish resources.\r
762 @param[in] Uri Relative path to address the resource.\r
763 @param[out] RedResponse Pointer to the Redfish response data.\r
764\r
765 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
766 NULL and the value is 2XX, the Redfish resource has been removed.\r
767 If there is any message returned from server, it will be returned\r
768 in Payload within RedResponse.\r
769 @retval EFI_INVALID_PARAMETER RedfishService, Uri, or RedResponse is NULL.\r
770 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
771 more error info from returned HTTP StatusCode, Headers and Payload\r
772 within RedResponse:\r
773 1. If the returned StatusCode is NULL, indicates any error happen.\r
774 2. If the returned StatusCode is not NULL and the value is not 2XX,\r
775 indicates any error happen.\r
776**/\r
777EFI_STATUS\r
778EFIAPI\r
779RedfishDeleteByUri (\r
39de741e
MK
780 IN REDFISH_SERVICE RedfishService,\r
781 IN CONST CHAR8 *Uri,\r
782 OUT REDFISH_RESPONSE *RedResponse\r
4751a48a
AC
783 )\r
784{\r
39de741e
MK
785 EFI_STATUS Status;\r
786 EDKII_JSON_VALUE JsonValue;\r
4751a48a
AC
787\r
788 Status = EFI_SUCCESS;\r
789 JsonValue = NULL;\r
790\r
39de741e 791 if ((RedfishService == NULL) || (Uri == NULL) || (RedResponse == NULL)) {\r
4751a48a
AC
792 return EFI_INVALID_PARAMETER;\r
793 }\r
794\r
795 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
796\r
39de741e
MK
797 JsonValue = (EDKII_JSON_VALUE)deleteUriFromService (\r
798 RedfishService,\r
799 Uri,\r
800 &(RedResponse->StatusCode)\r
801 );\r
4751a48a
AC
802\r
803 //\r
804 // 1. If the returned StatusCode is NULL, indicates any error happen.\r
f2bf043a
AC
805 //\r
806 if (RedResponse->StatusCode == NULL) {\r
807 Status = EFI_DEVICE_ERROR;\r
808 goto ON_EXIT;\r
809 }\r
810\r
811 //\r
812 // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
813 // NOTE: If there is any error message returned from server, it will be returned in\r
814 // Payload within RedResponse.\r
815 //\r
816 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
817 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
818 {\r
819 Status = EFI_DEVICE_ERROR;\r
820 }\r
821\r
822ON_EXIT:\r
823 if (JsonValue != NULL) {\r
824 RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);\r
825 if (RedResponse->Payload == NULL) {\r
826 //\r
827 // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what\r
828 // we care about if the returned StatusCode is 2XX.\r
829 //\r
830 JsonValueFree (JsonValue);\r
831 }\r
832 }\r
833\r
834 return Status;\r
835}\r
836\r
837/**\r
838 Use HTTP DELETE to remove a resource.\r
839\r
840 This function uses the RedfishService to remove a Redfish resource which is addressed\r
841 by input Uri (only the relative path is required). The corresponding redfish response will\r
842 returned, including HTTP StatusCode, Headers and Payload which record any HTTP response\r
843 messages.\r
844\r
845 Callers are responsible for freeing the HTTP StatusCode, Headers and Payload returned in\r
846 redfish response data.\r
847\r
848 @param[in] RedfishService The Service to access the Redfish resources.\r
849 @param[in] Uri Relative path to address the resource.\r
850 @param[in] Content JSON represented properties to be deleted.\r
851 @param[out] RedResponse Pointer to the Redfish response data.\r
852\r
853 @retval EFI_SUCCESS The opeartion is successful, indicates the HTTP StatusCode is not\r
854 NULL and the value is 2XX, the Redfish resource has been removed.\r
855 If there is any message returned from server, it will be returned\r
856 in Payload within RedResponse.\r
857 @retval EFI_INVALID_PARAMETER RedfishService, Uri, or RedResponse is NULL.\r
858 @retval EFI_DEVICE_ERROR An unexpected system or network error occurred. Callers can get\r
859 more error info from returned HTTP StatusCode, Headers and Payload\r
860 within RedResponse:\r
861 1. If the returned StatusCode is NULL, indicates any error happen.\r
862 2. If the returned StatusCode is not NULL and the value is not 2XX,\r
863 indicates any error happen.\r
864**/\r
865EFI_STATUS\r
866EFIAPI\r
867RedfishDeleteByUriEx (\r
868 IN REDFISH_SERVICE RedfishService,\r
869 IN CONST CHAR8 *Uri,\r
870 IN CONST CHAR8 *Content,\r
871 OUT REDFISH_RESPONSE *RedResponse\r
872 )\r
873{\r
874 EFI_STATUS Status;\r
875 EDKII_JSON_VALUE JsonValue;\r
876\r
877 Status = EFI_SUCCESS;\r
878 JsonValue = NULL;\r
879\r
880 if ((RedfishService == NULL) || (Content == NULL) || (Uri == NULL) || (RedResponse == NULL)) {\r
881 return EFI_INVALID_PARAMETER;\r
882 }\r
883\r
884 ZeroMem (RedResponse, sizeof (REDFISH_RESPONSE));\r
885\r
886 JsonValue = (EDKII_JSON_VALUE)deleteUriFromServiceEx (\r
887 RedfishService,\r
888 Uri,\r
889 Content,\r
890 &(RedResponse->StatusCode)\r
891 );\r
892\r
893 //\r
894 // 1. If the returned StatusCode is NULL, indicates any error happen.\r
4751a48a
AC
895 //\r
896 if (RedResponse->StatusCode == NULL) {\r
897 Status = EFI_DEVICE_ERROR;\r
898 goto ON_EXIT;\r
899 }\r
900\r
901 //\r
902 // 2. If the returned StatusCode is not NULL and the value is not 2XX, indicates any error happen.\r
903 // NOTE: If there is any error message returned from server, it will be returned in\r
904 // Payload within RedResponse.\r
905 //\r
39de741e
MK
906 if ((*(RedResponse->StatusCode) < HTTP_STATUS_200_OK) || \\r
907 (*(RedResponse->StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT))\r
908 {\r
4751a48a
AC
909 Status = EFI_DEVICE_ERROR;\r
910 }\r
911\r
912ON_EXIT:\r
913 if (JsonValue != NULL) {\r
914 RedResponse->Payload = createRedfishPayload (JsonValue, RedfishService);\r
915 if (RedResponse->Payload == NULL) {\r
916 //\r
917 // Ignore the error when create RedfishPayload, just free the JsonValue since it's not what\r
918 // we care about if the returned StatusCode is 2XX.\r
919 //\r
920 JsonValueFree (JsonValue);\r
921 }\r
922 }\r
923\r
924 return Status;\r
925}\r
39de741e 926\r
4751a48a
AC
927/**\r
928 Dump text in fractions.\r
929\r
930 @param[in] String ASCII string to dump.\r
931\r
932**/\r
933VOID\r
934RedfishDumpJsonStringFractions (\r
39de741e 935 IN CHAR8 *String\r
4751a48a
AC
936 )\r
937{\r
39de741e
MK
938 CHAR8 *NextFraction;\r
939 UINTN StringFractionSize;\r
940 UINTN StrLen;\r
941 UINTN Count;\r
942 CHAR8 BackupChar;\r
4751a48a
AC
943\r
944 StringFractionSize = 200;\r
945 if (String == NULL) {\r
39de741e 946 return;\r
4751a48a
AC
947 }\r
948\r
39de741e 949 DEBUG ((DEBUG_INFO, "JSON text:\n"));\r
4751a48a 950 NextFraction = String;\r
39de741e 951 StrLen = AsciiStrLen (String);\r
4751a48a
AC
952 if (StrLen == 0) {\r
953 return;\r
954 }\r
39de741e 955\r
4751a48a 956 for (Count = 0; Count < (StrLen / StringFractionSize); Count++) {\r
39de741e 957 BackupChar = *(NextFraction + StringFractionSize);\r
4751a48a 958 *(NextFraction + StringFractionSize) = 0;\r
39de741e 959 DEBUG ((DEBUG_INFO, "%a", NextFraction));\r
4751a48a 960 *(NextFraction + StringFractionSize) = BackupChar;\r
39de741e 961 NextFraction += StringFractionSize;\r
4751a48a 962 }\r
39de741e 963\r
4751a48a 964 if ((StrLen % StringFractionSize) != 0) {\r
39de741e 965 DEBUG ((DEBUG_INFO, "%a\n\n", NextFraction));\r
4751a48a
AC
966 }\r
967}\r
39de741e 968\r
4751a48a
AC
969/**\r
970 Dump text in JSON value.\r
971\r
972 @param[in] JsonValue The Redfish JSON value to dump.\r
973\r
974**/\r
975VOID\r
976RedfishDumpJson (\r
977 IN EDKII_JSON_VALUE JsonValue\r
978 )\r
979{\r
39de741e 980 CHAR8 *String;\r
4751a48a
AC
981\r
982 String = JsonDumpString (JsonValue, 0);\r
983 if (String == NULL) {\r
984 return;\r
985 }\r
39de741e 986\r
4751a48a 987 RedfishDumpJsonStringFractions (String);\r
39de741e 988 FreePool (String);\r
4751a48a 989}\r
39de741e 990\r
4751a48a
AC
991/**\r
992 Extract the JSON text content from REDFISH_PAYLOAD and dump to debug console.\r
993\r
994 @param[in] Payload The Redfish payload to dump.\r
995\r
996**/\r
997VOID\r
998RedfishDumpPayload (\r
39de741e 999 IN REDFISH_PAYLOAD Payload\r
4751a48a
AC
1000 )\r
1001{\r
39de741e
MK
1002 EDKII_JSON_VALUE JsonValue;\r
1003 CHAR8 *String;\r
4751a48a
AC
1004\r
1005 JsonValue = NULL;\r
39de741e 1006 String = NULL;\r
4751a48a
AC
1007\r
1008 if (Payload == NULL) {\r
1009 return;\r
1010 }\r
1011\r
1012 JsonValue = RedfishJsonInPayload (Payload);\r
1013 if (JsonValue == NULL) {\r
1014 return;\r
1015 }\r
1016\r
1017 String = JsonDumpString (JsonValue, 0);\r
1018 if (String == NULL) {\r
1019 return;\r
1020 }\r
1021\r
1022 RedfishDumpJsonStringFractions (String);\r
39de741e 1023 FreePool (String);\r
4751a48a 1024}\r
39de741e 1025\r
4751a48a
AC
1026/**\r
1027 This function will cleanup the HTTP header and Redfish payload resources.\r
1028\r
1029 @param[in] StatusCode The status code in HTTP response message.\r
1030 @param[in] HeaderCount Number of HTTP header structures in Headers list.\r
1031 @param[in] Headers Array containing list of HTTP headers.\r
1032 @param[in] Payload The Redfish payload to dump.\r
1033\r
1034**/\r
1035VOID\r
1036RedfishFreeResponse (\r
1037 IN EFI_HTTP_STATUS_CODE *StatusCode,\r
1038 IN UINTN HeaderCount,\r
1039 IN EFI_HTTP_HEADER *Headers,\r
1040 IN REDFISH_PAYLOAD Payload\r
1041 )\r
1042{\r
1043 if (StatusCode != NULL) {\r
1044 FreePool (StatusCode);\r
1045 StatusCode = NULL;\r
1046 }\r
1047\r
39de741e
MK
1048 if ((HeaderCount != 0) && (Headers != NULL)) {\r
1049 HttpFreeHeaderFields (Headers, HeaderCount);\r
4751a48a
AC
1050 Headers = NULL;\r
1051 }\r
1052\r
1053 if (Payload != NULL) {\r
1054 RedfishCleanupPayload (Payload);\r
1055 Payload = NULL;\r
1056 }\r
1057}\r
39de741e 1058\r
4751a48a
AC
1059/**\r
1060 Check if the "@odata.type" in Payload is valid or not.\r
1061\r
1062 @param[in] Payload The Redfish payload to be checked.\r
1063 @param[in] OdataTypeName OdataType will be retrived from mapping list.\r
1064 @param[in] OdataTypeMappingList The list of OdataType.\r
1065 @param[in] OdataTypeMappingListSize The number of mapping list\r
1066\r
1067 @return TRUE if the "@odata.type" in Payload is valid, otherwise FALSE.\r
1068\r
1069**/\r
1070BOOLEAN\r
1071RedfishIsValidOdataType (\r
39de741e
MK
1072 IN REDFISH_PAYLOAD Payload,\r
1073 IN CONST CHAR8 *OdataTypeName,\r
1074 IN REDFISH_ODATA_TYPE_MAPPING *OdataTypeMappingList,\r
1075 IN UINTN OdataTypeMappingListSize\r
4751a48a
AC
1076 )\r
1077{\r
39de741e
MK
1078 UINTN Index;\r
1079 EDKII_JSON_VALUE OdataType;\r
1080 EDKII_JSON_VALUE JsonValue;\r
4751a48a 1081\r
39de741e 1082 if ((Payload == NULL) || (OdataTypeName == NULL)) {\r
4751a48a
AC
1083 return FALSE;\r
1084 }\r
1085\r
1086 JsonValue = RedfishJsonInPayload (Payload);\r
1087 if (!JsonValueIsObject (JsonValue)) {\r
1088 return FALSE;\r
1089 }\r
1090\r
1091 OdataType = JsonObjectGetValue (JsonValueGetObject (JsonValue), "@odata.type");\r
39de741e 1092 if (!JsonValueIsString (OdataType) || (JsonValueGetAsciiString (OdataType) == NULL)) {\r
4751a48a
AC
1093 return FALSE;\r
1094 }\r
1095\r
39de741e
MK
1096 for (Index = 0; Index < OdataTypeMappingListSize; Index++) {\r
1097 if ((AsciiStrCmp (OdataTypeMappingList[Index].OdataTypeName, OdataTypeName) == 0) &&\r
1098 (AsciiStrCmp (OdataTypeMappingList[Index].OdataType, JsonValueGetAsciiString (OdataType)) == 0))\r
1099 {\r
4751a48a
AC
1100 return TRUE;\r
1101 }\r
1102 }\r
39de741e 1103\r
4751a48a
AC
1104 DEBUG ((DEBUG_INFO, "%a: This Odata type is not in the list.\n", __FUNCTION__));\r
1105 return FALSE;\r
1106}\r
39de741e 1107\r
4751a48a
AC
1108/**\r
1109 Check if the payload is collection\r
1110\r
1111 @param[in] Payload The Redfish payload to be checked.\r
1112\r
1113 @return TRUE if the payload is collection.\r
1114\r
1115**/\r
1116BOOLEAN\r
1117RedfishIsPayloadCollection (\r
39de741e
MK
1118 IN REDFISH_PAYLOAD Payload\r
1119 )\r
4751a48a
AC
1120{\r
1121 return isPayloadCollection (Payload);\r
1122}\r
39de741e 1123\r
4751a48a
AC
1124/**\r
1125 Get collection size.\r
1126\r
1127 @param[in] Payload The Redfish collection payload\r
1128 @param[in] CollectionSize Size of this collection\r
1129\r
1130 @return EFI_SUCCESS Coolection size is returned in CollectionSize\r
1131 @return EFI_INVALID_PARAMETER The payload is not a collection.\r
1132**/\r
1133EFI_STATUS\r
39de741e
MK
1134RedfishGetCollectionSize (\r
1135 IN REDFISH_PAYLOAD Payload,\r
1136 IN UINTN *CollectionSize\r
4751a48a
AC
1137 )\r
1138{\r
39de741e 1139 if ((Payload == NULL) || (CollectionSize == NULL)) {\r
4751a48a
AC
1140 return EFI_INVALID_PARAMETER;\r
1141 }\r
39de741e
MK
1142\r
1143 if (!RedfishIsPayloadCollection (Payload)) {\r
4751a48a
AC
1144 return EFI_INVALID_PARAMETER;\r
1145 }\r
1146\r
39de741e 1147 *CollectionSize = (UINTN)getCollectionSize (Payload);\r
4751a48a
AC
1148 return EFI_SUCCESS;\r
1149}\r
39de741e 1150\r
4751a48a
AC
1151/**\r
1152 Get Redfish payload of collection member\r
1153\r
1154 @param[in] Payload The Redfish collection payload\r
1155 @param[in] Index Index of collection member\r
1156\r
1157 @return NULL Fail to get collection member.\r
1158 @return Non NULL Payload is returned.\r
1159**/\r
1160REDFISH_PAYLOAD\r
1161RedfishGetPayloadByIndex (\r
39de741e
MK
1162 IN REDFISH_PAYLOAD Payload,\r
1163 IN UINTN Index\r
1164 )\r
4751a48a 1165{\r
39de741e
MK
1166 REDFISH_RESPONSE RedfishResponse;\r
1167 REDFISH_PAYLOAD PayloadReturn;\r
4751a48a
AC
1168\r
1169 PayloadReturn = (VOID *)getPayloadByIndex (Payload, Index, &RedfishResponse.StatusCode);\r
39de741e
MK
1170 if ((PayloadReturn == NULL) ||\r
1171 ((*(RedfishResponse.StatusCode) < HTTP_STATUS_200_OK) && (*(RedfishResponse.StatusCode) > HTTP_STATUS_206_PARTIAL_CONTENT)))\r
1172 {\r
4751a48a
AC
1173 return NULL;\r
1174 }\r
39de741e 1175\r
4751a48a
AC
1176 return PayloadReturn;\r
1177}\r
39de741e 1178\r
4751a48a
AC
1179/**\r
1180 Check and return Redfish resource of the given Redpath.\r
1181\r
1182 @param[in] RedfishService Pointer to REDFISH_SERVICE\r
1183 @param[in] Redpath Redpath of the resource.\r
1184 @param[in] Response Optional return the resource.\r
1185\r
1186 @return EFI_STATUS\r
1187**/\r
1188EFI_STATUS\r
1189RedfishCheckIfRedpathExist (\r
39de741e
MK
1190 IN REDFISH_SERVICE RedfishService,\r
1191 IN CHAR8 *Redpath,\r
1192 IN REDFISH_RESPONSE *Response OPTIONAL\r
4751a48a
AC
1193 )\r
1194{\r
39de741e
MK
1195 EFI_STATUS Status;\r
1196 REDFISH_RESPONSE TempResponse;\r
4751a48a
AC
1197\r
1198 if (Redpath == NULL) {\r
1199 return EFI_INVALID_PARAMETER;\r
1200 }\r
39de741e 1201\r
4751a48a
AC
1202 Status = RedfishGetByService (RedfishService, Redpath, &TempResponse);\r
1203 if (EFI_ERROR (Status)) {\r
1204 return Status;\r
1205 }\r
39de741e 1206\r
4751a48a 1207 if (Response == NULL) {\r
39de741e 1208 RedfishFreeResponse (\r
4751a48a
AC
1209 TempResponse.StatusCode,\r
1210 TempResponse.HeaderCount,\r
1211 TempResponse.Headers,\r
1212 TempResponse.Payload\r
39de741e 1213 );\r
4751a48a
AC
1214 } else {\r
1215 CopyMem ((VOID *)Response, (VOID *)&TempResponse, sizeof (REDFISH_RESPONSE));\r
1216 }\r
39de741e 1217\r
4751a48a
AC
1218 return EFI_SUCCESS;\r
1219}\r