xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision b729096d0a787abe2a7e8a9a2d1ea12b5e1540fb)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
183ccb3adbSEd Tanous #include "app.hpp"
197a1dbc48SGeorge Liu #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
2168dd075aSAsmitha Karunanithi #include "generated/enums/log_entry.hpp"
22539d8c6bSEd Tanous #include "generated/enums/log_service.hpp"
23b7028ebfSSpencer Ku #include "gzfile.hpp"
24647b3cdcSGeorge Liu #include "http_utility.hpp"
25b7028ebfSSpencer Ku #include "human_sort.hpp"
263ccb3adbSEd Tanous #include "query.hpp"
274851d45dSJason M. Bills #include "registries.hpp"
284851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
294851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
303ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
3146229577SJames Feist #include "task.hpp"
325b90429aSEd Tanous #include "task_messages.hpp"
333ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
345b90429aSEd Tanous #include "utils/json_utils.hpp"
353ccb3adbSEd Tanous #include "utils/time_utils.hpp"
361da66f75SEd Tanous 
3775e8e218SMyung Bae #include <systemd/sd-id128.h>
388e31778eSAsmitha Karunanithi #include <tinyxml2.h>
39400fd1fbSAdriana Kobylak #include <unistd.h>
40e1f26343SJason M. Bills 
4107c8c20dSEd Tanous #include <boost/beast/http/verb.hpp>
421da66f75SEd Tanous #include <boost/container/flat_map.hpp>
431ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
44ef4c65b7SEd Tanous #include <boost/url/format.hpp>
45d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
46d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
471214b7e7SGunnar Mills 
487a1dbc48SGeorge Liu #include <array>
49647b3cdcSGeorge Liu #include <charconv>
50b5f288d2SAbhilash Raju #include <cstddef>
514418c7f0SJames Feist #include <filesystem>
5218f8f608SEd Tanous #include <iterator>
5375710de2SXiaochao Ma #include <optional>
543544d2a7SEd Tanous #include <ranges>
5526702d01SEd Tanous #include <span>
5618f8f608SEd Tanous #include <string>
57cd225da8SJason M. Bills #include <string_view>
58abf2add6SEd Tanous #include <variant>
591da66f75SEd Tanous 
601da66f75SEd Tanous namespace redfish
611da66f75SEd Tanous {
621da66f75SEd Tanous 
6389492a15SPatrick Williams constexpr const char* crashdumpObject = "com.intel.crashdump";
6489492a15SPatrick Williams constexpr const char* crashdumpPath = "/com/intel/crashdump";
6589492a15SPatrick Williams constexpr const char* crashdumpInterface = "com.intel.crashdump";
6689492a15SPatrick Williams constexpr const char* deleteAllInterface =
675b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
6889492a15SPatrick Williams constexpr const char* crashdumpOnDemandInterface =
69424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
7089492a15SPatrick Williams constexpr const char* crashdumpTelemetryInterface =
716eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
721da66f75SEd Tanous 
738e31778eSAsmitha Karunanithi enum class DumpCreationProgress
748e31778eSAsmitha Karunanithi {
758e31778eSAsmitha Karunanithi     DUMP_CREATE_SUCCESS,
768e31778eSAsmitha Karunanithi     DUMP_CREATE_FAILED,
778e31778eSAsmitha Karunanithi     DUMP_CREATE_INPROGRESS
788e31778eSAsmitha Karunanithi };
798e31778eSAsmitha Karunanithi 
80f6150403SJames Feist namespace fs = std::filesystem;
811da66f75SEd Tanous 
82cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
83cb92c03bSAndrew Geissler {
84d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
85d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
86d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
87d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
88cb92c03bSAndrew Geissler     {
89cb92c03bSAndrew Geissler         return "Critical";
90cb92c03bSAndrew Geissler     }
913174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
92d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
93d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
94cb92c03bSAndrew Geissler     {
95cb92c03bSAndrew Geissler         return "OK";
96cb92c03bSAndrew Geissler     }
973174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
98cb92c03bSAndrew Geissler     {
99cb92c03bSAndrew Geissler         return "Warning";
100cb92c03bSAndrew Geissler     }
101cb92c03bSAndrew Geissler     return "";
102cb92c03bSAndrew Geissler }
103cb92c03bSAndrew Geissler 
1049017faf2SAbhishek Patel inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
1059017faf2SAbhishek Patel {
1069017faf2SAbhishek Patel     std::optional<bool> notifyAction;
1079017faf2SAbhishek Patel     if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
1089017faf2SAbhishek Patel     {
1099017faf2SAbhishek Patel         notifyAction = true;
1109017faf2SAbhishek Patel     }
1119017faf2SAbhishek Patel     else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
1129017faf2SAbhishek Patel     {
1139017faf2SAbhishek Patel         notifyAction = false;
1149017faf2SAbhishek Patel     }
1159017faf2SAbhishek Patel 
1169017faf2SAbhishek Patel     return notifyAction;
1179017faf2SAbhishek Patel }
1189017faf2SAbhishek Patel 
11918f8f608SEd Tanous inline std::string getDumpPath(std::string_view dumpType)
12018f8f608SEd Tanous {
12118f8f608SEd Tanous     std::string dbusDumpPath = "/xyz/openbmc_project/dump/";
12218f8f608SEd Tanous     std::ranges::transform(dumpType, std::back_inserter(dbusDumpPath),
12318f8f608SEd Tanous                            bmcweb::asciiToLower);
12418f8f608SEd Tanous 
12518f8f608SEd Tanous     return dbusDumpPath;
12618f8f608SEd Tanous }
12718f8f608SEd Tanous 
128055713e4SEd Tanous inline bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
129e85d6b16SJason M. Bills                              const bool firstEntry = true)
13095820184SJason M. Bills {
131271584abSEd Tanous     static time_t prevTs = 0;
13295820184SJason M. Bills     static int index = 0;
133e85d6b16SJason M. Bills     if (firstEntry)
134e85d6b16SJason M. Bills     {
135e85d6b16SJason M. Bills         prevTs = 0;
136e85d6b16SJason M. Bills     }
137e85d6b16SJason M. Bills 
13895820184SJason M. Bills     // Get the entry timestamp
139271584abSEd Tanous     std::time_t curTs = 0;
14095820184SJason M. Bills     std::tm timeStruct = {};
14195820184SJason M. Bills     std::istringstream entryStream(logEntry);
14295820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
14395820184SJason M. Bills     {
14495820184SJason M. Bills         curTs = std::mktime(&timeStruct);
14595820184SJason M. Bills     }
14695820184SJason M. Bills     // If the timestamp isn't unique, increment the index
14795820184SJason M. Bills     if (curTs == prevTs)
14895820184SJason M. Bills     {
14995820184SJason M. Bills         index++;
15095820184SJason M. Bills     }
15195820184SJason M. Bills     else
15295820184SJason M. Bills     {
15395820184SJason M. Bills         // Otherwise, reset it
15495820184SJason M. Bills         index = 0;
15595820184SJason M. Bills     }
15695820184SJason M. Bills     // Save the timestamp
15795820184SJason M. Bills     prevTs = curTs;
15895820184SJason M. Bills 
15995820184SJason M. Bills     entryID = std::to_string(curTs);
16095820184SJason M. Bills     if (index > 0)
16195820184SJason M. Bills     {
16295820184SJason M. Bills         entryID += "_" + std::to_string(index);
16395820184SJason M. Bills     }
16495820184SJason M. Bills     return true;
16595820184SJason M. Bills }
16695820184SJason M. Bills 
16795820184SJason M. Bills static bool
16895820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
16995820184SJason M. Bills {
17095820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
17195820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
17295820184SJason M. Bills 
17395820184SJason M. Bills     // Loop through the directory looking for redfish log files
17495820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
17595820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
17695820184SJason M. Bills     {
17795820184SJason M. Bills         // If we find a redfish log file, save the path
17895820184SJason M. Bills         std::string filename = dirEnt.path().filename();
17911ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
18095820184SJason M. Bills         {
18195820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
18295820184SJason M. Bills         }
18395820184SJason M. Bills     }
18495820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
18595820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
18695820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
1873544d2a7SEd Tanous     std::ranges::sort(redfishLogFiles);
18895820184SJason M. Bills 
18995820184SJason M. Bills     return !redfishLogFiles.empty();
19095820184SJason M. Bills }
19195820184SJason M. Bills 
19268dd075aSAsmitha Karunanithi inline log_entry::OriginatorTypes
19368dd075aSAsmitha Karunanithi     mapDbusOriginatorTypeToRedfish(const std::string& originatorType)
19468dd075aSAsmitha Karunanithi {
19568dd075aSAsmitha Karunanithi     if (originatorType ==
19668dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client")
19768dd075aSAsmitha Karunanithi     {
19868dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::Client;
19968dd075aSAsmitha Karunanithi     }
20068dd075aSAsmitha Karunanithi     if (originatorType ==
20168dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Internal")
20268dd075aSAsmitha Karunanithi     {
20368dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::Internal;
20468dd075aSAsmitha Karunanithi     }
20568dd075aSAsmitha Karunanithi     if (originatorType ==
20668dd075aSAsmitha Karunanithi         "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.SupportingService")
20768dd075aSAsmitha Karunanithi     {
20868dd075aSAsmitha Karunanithi         return log_entry::OriginatorTypes::SupportingService;
20968dd075aSAsmitha Karunanithi     }
21068dd075aSAsmitha Karunanithi     return log_entry::OriginatorTypes::Invalid;
21168dd075aSAsmitha Karunanithi }
21268dd075aSAsmitha Karunanithi 
213aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
2142d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
215c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
21668dd075aSAsmitha Karunanithi     std::string& originatorId, log_entry::OriginatorTypes& originatorType,
217aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
218aefe3786SClaire Weinan {
219aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
220aefe3786SClaire Weinan     {
221aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
222aefe3786SClaire Weinan         {
223aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
224aefe3786SClaire Weinan             {
225aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
226aefe3786SClaire Weinan                 {
227aefe3786SClaire Weinan                     const auto* status =
228aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
229aefe3786SClaire Weinan                     if (status == nullptr)
230aefe3786SClaire Weinan                     {
231aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
232aefe3786SClaire Weinan                         break;
233aefe3786SClaire Weinan                     }
234aefe3786SClaire Weinan                     dumpStatus = *status;
235aefe3786SClaire Weinan                 }
236aefe3786SClaire Weinan             }
237aefe3786SClaire Weinan         }
238aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
239aefe3786SClaire Weinan         {
240aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
241aefe3786SClaire Weinan             {
242aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
243aefe3786SClaire Weinan                 {
244aefe3786SClaire Weinan                     const auto* sizePtr =
245aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
246aefe3786SClaire Weinan                     if (sizePtr == nullptr)
247aefe3786SClaire Weinan                     {
248aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
249aefe3786SClaire Weinan                         break;
250aefe3786SClaire Weinan                     }
251aefe3786SClaire Weinan                     size = *sizePtr;
252aefe3786SClaire Weinan                     break;
253aefe3786SClaire Weinan                 }
254aefe3786SClaire Weinan             }
255aefe3786SClaire Weinan         }
256aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
257aefe3786SClaire Weinan         {
258aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
259aefe3786SClaire Weinan             {
260aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
261aefe3786SClaire Weinan                 {
262aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
263aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
264aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
265aefe3786SClaire Weinan                     {
266aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
267aefe3786SClaire Weinan                         break;
268aefe3786SClaire Weinan                     }
269c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
270aefe3786SClaire Weinan                     break;
271aefe3786SClaire Weinan                 }
272aefe3786SClaire Weinan             }
273aefe3786SClaire Weinan         }
27468dd075aSAsmitha Karunanithi         else if (interfaceMap.first ==
27568dd075aSAsmitha Karunanithi                  "xyz.openbmc_project.Common.OriginatedBy")
27668dd075aSAsmitha Karunanithi         {
27768dd075aSAsmitha Karunanithi             for (const auto& propertyMap : interfaceMap.second)
27868dd075aSAsmitha Karunanithi             {
27968dd075aSAsmitha Karunanithi                 if (propertyMap.first == "OriginatorId")
28068dd075aSAsmitha Karunanithi                 {
28168dd075aSAsmitha Karunanithi                     const std::string* id =
28268dd075aSAsmitha Karunanithi                         std::get_if<std::string>(&propertyMap.second);
28368dd075aSAsmitha Karunanithi                     if (id == nullptr)
28468dd075aSAsmitha Karunanithi                     {
28568dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
28668dd075aSAsmitha Karunanithi                         break;
28768dd075aSAsmitha Karunanithi                     }
28868dd075aSAsmitha Karunanithi                     originatorId = *id;
28968dd075aSAsmitha Karunanithi                 }
29068dd075aSAsmitha Karunanithi 
29168dd075aSAsmitha Karunanithi                 if (propertyMap.first == "OriginatorType")
29268dd075aSAsmitha Karunanithi                 {
29368dd075aSAsmitha Karunanithi                     const std::string* type =
29468dd075aSAsmitha Karunanithi                         std::get_if<std::string>(&propertyMap.second);
29568dd075aSAsmitha Karunanithi                     if (type == nullptr)
29668dd075aSAsmitha Karunanithi                     {
29768dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
29868dd075aSAsmitha Karunanithi                         break;
29968dd075aSAsmitha Karunanithi                     }
30068dd075aSAsmitha Karunanithi 
30168dd075aSAsmitha Karunanithi                     originatorType = mapDbusOriginatorTypeToRedfish(*type);
30268dd075aSAsmitha Karunanithi                     if (originatorType == log_entry::OriginatorTypes::Invalid)
30368dd075aSAsmitha Karunanithi                     {
30468dd075aSAsmitha Karunanithi                         messages::internalError(asyncResp->res);
30568dd075aSAsmitha Karunanithi                         break;
30668dd075aSAsmitha Karunanithi                     }
30768dd075aSAsmitha Karunanithi                 }
30868dd075aSAsmitha Karunanithi             }
30968dd075aSAsmitha Karunanithi         }
310aefe3786SClaire Weinan     }
311aefe3786SClaire Weinan }
312aefe3786SClaire Weinan 
31321ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
314fdd26906SClaire Weinan {
315fdd26906SClaire Weinan     std::string entriesPath;
316fdd26906SClaire Weinan 
317fdd26906SClaire Weinan     if (dumpType == "BMC")
318fdd26906SClaire Weinan     {
319253f11b8SEd Tanous         entriesPath =
320253f11b8SEd Tanous             std::format("/redfish/v1/Managers/{}/LogServices/Dump/Entries/",
321253f11b8SEd Tanous                         BMCWEB_REDFISH_MANAGER_URI_NAME);
322fdd26906SClaire Weinan     }
323fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
324fdd26906SClaire Weinan     {
325253f11b8SEd Tanous         entriesPath =
326253f11b8SEd Tanous             std::format("/redfish/v1/Managers/{}/LogServices/FaultLog/Entries/",
327253f11b8SEd Tanous                         BMCWEB_REDFISH_MANAGER_URI_NAME);
328fdd26906SClaire Weinan     }
329fdd26906SClaire Weinan     else if (dumpType == "System")
330fdd26906SClaire Weinan     {
331253f11b8SEd Tanous         entriesPath =
332253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/Dump/Entries/",
333253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
334fdd26906SClaire Weinan     }
335fdd26906SClaire Weinan     else
336fdd26906SClaire Weinan     {
33762598e31SEd Tanous         BMCWEB_LOG_ERROR("getDumpEntriesPath() invalid dump type: {}",
33862598e31SEd Tanous                          dumpType);
339fdd26906SClaire Weinan     }
340fdd26906SClaire Weinan 
341fdd26906SClaire Weinan     // Returns empty string on error
342fdd26906SClaire Weinan     return entriesPath;
343fdd26906SClaire Weinan }
344fdd26906SClaire Weinan 
3458d1b46d7Szhanghch05 inline void
3468d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3475cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3485cb1dd27SAsmitha Karunanithi {
349fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
350fdd26906SClaire Weinan     if (entriesPath.empty())
3515cb1dd27SAsmitha Karunanithi     {
3525cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3535cb1dd27SAsmitha Karunanithi         return;
3545cb1dd27SAsmitha Karunanithi     }
3555cb1dd27SAsmitha Karunanithi 
3565eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
3575eb468daSGeorge Liu     dbus::utility::getManagedObjects(
3585eb468daSGeorge Liu         "xyz.openbmc_project.Dump.Manager", path,
359fdd26906SClaire Weinan         [asyncResp, entriesPath,
3605e7e2dc5SEd Tanous          dumpType](const boost::system::error_code& ec,
3615eb468daSGeorge Liu                    const dbus::utility::ManagedObjectType& objects) {
3625cb1dd27SAsmitha Karunanithi         if (ec)
3635cb1dd27SAsmitha Karunanithi         {
36462598e31SEd Tanous             BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
3655cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
3665cb1dd27SAsmitha Karunanithi             return;
3675cb1dd27SAsmitha Karunanithi         }
3685cb1dd27SAsmitha Karunanithi 
369fdd26906SClaire Weinan         // Remove ending slash
370fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
371fdd26906SClaire Weinan         if (!odataIdStr.empty())
372fdd26906SClaire Weinan         {
373fdd26906SClaire Weinan             odataIdStr.pop_back();
374fdd26906SClaire Weinan         }
375fdd26906SClaire Weinan 
376fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
377fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
378fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
379fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
38089492a15SPatrick Williams         asyncResp->res.jsonValue["Description"] = "Collection of " + dumpType +
38189492a15SPatrick Williams                                                   " Dump Entries";
382fdd26906SClaire Weinan 
3833544d2a7SEd Tanous         nlohmann::json::array_t entriesArray;
38418f8f608SEd Tanous         std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
3855cb1dd27SAsmitha Karunanithi 
3865eb468daSGeorge Liu         dbus::utility::ManagedObjectType resp(objects);
3873544d2a7SEd Tanous         std::ranges::sort(resp, [](const auto& l, const auto& r) {
388002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
389002d39b4SEd Tanous                                                r.first.filename());
390565dfb6fSClaire Weinan         });
391565dfb6fSClaire Weinan 
3925cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
3935cb1dd27SAsmitha Karunanithi         {
394b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
3955cb1dd27SAsmitha Karunanithi             {
3965cb1dd27SAsmitha Karunanithi                 continue;
3975cb1dd27SAsmitha Karunanithi             }
398c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
3995cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
40035440d18SAsmitha Karunanithi             std::string dumpStatus;
40168dd075aSAsmitha Karunanithi             std::string originatorId;
40268dd075aSAsmitha Karunanithi             log_entry::OriginatorTypes originatorType =
40368dd075aSAsmitha Karunanithi                 log_entry::OriginatorTypes::Internal;
404433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
4052dfd18efSEd Tanous 
4062dfd18efSEd Tanous             std::string entryID = object.first.filename();
4072dfd18efSEd Tanous             if (entryID.empty())
4085cb1dd27SAsmitha Karunanithi             {
4095cb1dd27SAsmitha Karunanithi                 continue;
4105cb1dd27SAsmitha Karunanithi             }
4115cb1dd27SAsmitha Karunanithi 
412c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
41368dd075aSAsmitha Karunanithi                                          originatorId, originatorType,
414aefe3786SClaire Weinan                                          asyncResp);
4155cb1dd27SAsmitha Karunanithi 
4160fda0f12SGeorge Liu             if (dumpStatus !=
4170fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
41835440d18SAsmitha Karunanithi                 !dumpStatus.empty())
41935440d18SAsmitha Karunanithi             {
42035440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
42135440d18SAsmitha Karunanithi                 continue;
42235440d18SAsmitha Karunanithi             }
42335440d18SAsmitha Karunanithi 
42468dd075aSAsmitha Karunanithi             thisEntry["@odata.type"] = "#LogEntry.v1_11_0.LogEntry";
425fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
4265cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
4275cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
4285cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
429bbd80db8SClaire Weinan             thisEntry["Created"] =
430bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
4315cb1dd27SAsmitha Karunanithi 
43268dd075aSAsmitha Karunanithi             if (!originatorId.empty())
43368dd075aSAsmitha Karunanithi             {
43468dd075aSAsmitha Karunanithi                 thisEntry["Originator"] = originatorId;
43568dd075aSAsmitha Karunanithi                 thisEntry["OriginatorType"] = originatorType;
43668dd075aSAsmitha Karunanithi             }
43768dd075aSAsmitha Karunanithi 
4385cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
4395cb1dd27SAsmitha Karunanithi             {
440d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
44189492a15SPatrick Williams                 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
44289492a15SPatrick Williams                                                  "/attachment";
443fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
4445cb1dd27SAsmitha Karunanithi             }
4455cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
4465cb1dd27SAsmitha Karunanithi             {
447d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
448d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
44989492a15SPatrick Williams                 thisEntry["AdditionalDataURI"] = entriesPath + entryID +
45089492a15SPatrick Williams                                                  "/attachment";
451fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
4525cb1dd27SAsmitha Karunanithi             }
453b2ba3072SPatrick Williams             entriesArray.emplace_back(std::move(thisEntry));
4545cb1dd27SAsmitha Karunanithi         }
455002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
4563544d2a7SEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
4575eb468daSGeorge Liu     });
4585cb1dd27SAsmitha Karunanithi }
4595cb1dd27SAsmitha Karunanithi 
4608d1b46d7Szhanghch05 inline void
461c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4628d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
4635cb1dd27SAsmitha Karunanithi {
464fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
465fdd26906SClaire Weinan     if (entriesPath.empty())
4665cb1dd27SAsmitha Karunanithi     {
4675cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4685cb1dd27SAsmitha Karunanithi         return;
4695cb1dd27SAsmitha Karunanithi     }
4705cb1dd27SAsmitha Karunanithi 
4715eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/dump");
4725eb468daSGeorge Liu     dbus::utility::getManagedObjects(
4735eb468daSGeorge Liu         "xyz.openbmc_project.Dump.Manager", path,
474fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
4755e7e2dc5SEd Tanous          entriesPath](const boost::system::error_code& ec,
47602cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
4775cb1dd27SAsmitha Karunanithi         if (ec)
4785cb1dd27SAsmitha Karunanithi         {
47962598e31SEd Tanous             BMCWEB_LOG_ERROR("DumpEntry resp_handler got error {}", ec);
4805cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4815cb1dd27SAsmitha Karunanithi             return;
4825cb1dd27SAsmitha Karunanithi         }
4835cb1dd27SAsmitha Karunanithi 
484b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
48518f8f608SEd Tanous         std::string dumpEntryPath = getDumpPath(dumpType) + "/entry/";
486b47452b2SAsmitha Karunanithi 
4879eb808c1SEd Tanous         for (const auto& objectPath : resp)
4885cb1dd27SAsmitha Karunanithi         {
489b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
4905cb1dd27SAsmitha Karunanithi             {
4915cb1dd27SAsmitha Karunanithi                 continue;
4925cb1dd27SAsmitha Karunanithi             }
4935cb1dd27SAsmitha Karunanithi 
4945cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
495c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
4965cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
49735440d18SAsmitha Karunanithi             std::string dumpStatus;
49868dd075aSAsmitha Karunanithi             std::string originatorId;
49968dd075aSAsmitha Karunanithi             log_entry::OriginatorTypes originatorType =
50068dd075aSAsmitha Karunanithi                 log_entry::OriginatorTypes::Internal;
5015cb1dd27SAsmitha Karunanithi 
502aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
50368dd075aSAsmitha Karunanithi                                          timestampUs, originatorId,
50468dd075aSAsmitha Karunanithi                                          originatorType, asyncResp);
5055cb1dd27SAsmitha Karunanithi 
5060fda0f12SGeorge Liu             if (dumpStatus !=
5070fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
50835440d18SAsmitha Karunanithi                 !dumpStatus.empty())
50935440d18SAsmitha Karunanithi             {
51035440d18SAsmitha Karunanithi                 // Dump status is not Complete
51135440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
512d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
513d1bde9e5SKrzysztof Grobelny                                            entryID);
51435440d18SAsmitha Karunanithi                 return;
51535440d18SAsmitha Karunanithi             }
51635440d18SAsmitha Karunanithi 
5175cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
51868dd075aSAsmitha Karunanithi                 "#LogEntry.v1_11_0.LogEntry";
519fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
5205cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
5215cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
5225cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
523bbd80db8SClaire Weinan             asyncResp->res.jsonValue["Created"] =
524bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5255cb1dd27SAsmitha Karunanithi 
52668dd075aSAsmitha Karunanithi             if (!originatorId.empty())
52768dd075aSAsmitha Karunanithi             {
52868dd075aSAsmitha Karunanithi                 asyncResp->res.jsonValue["Originator"] = originatorId;
52968dd075aSAsmitha Karunanithi                 asyncResp->res.jsonValue["OriginatorType"] = originatorType;
53068dd075aSAsmitha Karunanithi             }
53168dd075aSAsmitha Karunanithi 
5325cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5335cb1dd27SAsmitha Karunanithi             {
534d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
535d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
536fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
537fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
5385cb1dd27SAsmitha Karunanithi             }
5395cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5405cb1dd27SAsmitha Karunanithi             {
541d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
542002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
543d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
544fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
545fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
5465cb1dd27SAsmitha Karunanithi             }
5475cb1dd27SAsmitha Karunanithi         }
548e05aec50SEd Tanous         if (!foundDumpEntry)
549b47452b2SAsmitha Karunanithi         {
55062598e31SEd Tanous             BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
551b90d14f2SMyung Bae             messages::resourceNotFound(asyncResp->res, dumpType + " dump",
552b90d14f2SMyung Bae                                        entryID);
553b47452b2SAsmitha Karunanithi             return;
554b47452b2SAsmitha Karunanithi         }
5555eb468daSGeorge Liu     });
5565cb1dd27SAsmitha Karunanithi }
5575cb1dd27SAsmitha Karunanithi 
5588d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5599878256fSStanley Chu                             const std::string& entryID,
560b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
5615cb1dd27SAsmitha Karunanithi {
5625a39f77aSPatrick Williams     auto respHandler = [asyncResp,
5635a39f77aSPatrick Williams                         entryID](const boost::system::error_code& ec) {
56462598e31SEd Tanous         BMCWEB_LOG_DEBUG("Dump Entry doDelete callback: Done");
5655cb1dd27SAsmitha Karunanithi         if (ec)
5665cb1dd27SAsmitha Karunanithi         {
5673de8d8baSGeorge Liu             if (ec.value() == EBADR)
5683de8d8baSGeorge Liu             {
5693de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
5703de8d8baSGeorge Liu                 return;
5713de8d8baSGeorge Liu             }
57262598e31SEd Tanous             BMCWEB_LOG_ERROR(
57362598e31SEd Tanous                 "Dump (DBus) doDelete respHandler got error {} entryID={}", ec,
57462598e31SEd Tanous                 entryID);
5755cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5765cb1dd27SAsmitha Karunanithi             return;
5775cb1dd27SAsmitha Karunanithi         }
5785cb1dd27SAsmitha Karunanithi     };
57918f8f608SEd Tanous 
5805cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
5815cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
58218f8f608SEd Tanous         std::format("{}/entry/{}", getDumpPath(dumpType), entryID),
5835cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
5845cb1dd27SAsmitha Karunanithi }
585b5f288d2SAbhilash Raju inline bool checkSizeLimit(int fd, crow::Response& res)
586b5f288d2SAbhilash Raju {
587b5f288d2SAbhilash Raju     long long int size = lseek(fd, 0, SEEK_END);
588b5f288d2SAbhilash Raju     if (size <= 0)
589b5f288d2SAbhilash Raju     {
590b5f288d2SAbhilash Raju         BMCWEB_LOG_ERROR("Failed to get size of file, lseek() returned {}",
591b5f288d2SAbhilash Raju                          size);
592b5f288d2SAbhilash Raju         messages::internalError(res);
593b5f288d2SAbhilash Raju         return false;
594b5f288d2SAbhilash Raju     }
5955cb1dd27SAsmitha Karunanithi 
596b5f288d2SAbhilash Raju     // Arbitrary max size of 20MB to accommodate BMC dumps
597b5f288d2SAbhilash Raju     constexpr long long int maxFileSize = 20LL * 1024LL * 1024LL;
598b5f288d2SAbhilash Raju     if (size > maxFileSize)
599b5f288d2SAbhilash Raju     {
600b5f288d2SAbhilash Raju         BMCWEB_LOG_ERROR("File size {} exceeds maximum allowed size of {}",
601b5f288d2SAbhilash Raju                          size, maxFileSize);
602b5f288d2SAbhilash Raju         messages::internalError(res);
603b5f288d2SAbhilash Raju         return false;
604b5f288d2SAbhilash Raju     }
605b5f288d2SAbhilash Raju     off_t rc = lseek(fd, 0, SEEK_SET);
606b5f288d2SAbhilash Raju     if (rc < 0)
607b5f288d2SAbhilash Raju     {
608b5f288d2SAbhilash Raju         BMCWEB_LOG_ERROR("Failed to reset file offset to 0");
609b5f288d2SAbhilash Raju         messages::internalError(res);
610b5f288d2SAbhilash Raju         return false;
611b5f288d2SAbhilash Raju     }
612b5f288d2SAbhilash Raju     return true;
613b5f288d2SAbhilash Raju }
614168d1b1aSCarson Labrado inline void
615168d1b1aSCarson Labrado     downloadEntryCallback(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
616168d1b1aSCarson Labrado                           const std::string& entryID,
617168d1b1aSCarson Labrado                           const std::string& downloadEntryType,
618168d1b1aSCarson Labrado                           const boost::system::error_code& ec,
619168d1b1aSCarson Labrado                           const sdbusplus::message::unix_fd& unixfd)
620168d1b1aSCarson Labrado {
621168d1b1aSCarson Labrado     if (ec.value() == EBADR)
622168d1b1aSCarson Labrado     {
623168d1b1aSCarson Labrado         messages::resourceNotFound(asyncResp->res, "EntryAttachment", entryID);
624168d1b1aSCarson Labrado         return;
625168d1b1aSCarson Labrado     }
626168d1b1aSCarson Labrado     if (ec)
627168d1b1aSCarson Labrado     {
628168d1b1aSCarson Labrado         BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
629168d1b1aSCarson Labrado         messages::internalError(asyncResp->res);
630168d1b1aSCarson Labrado         return;
631168d1b1aSCarson Labrado     }
632168d1b1aSCarson Labrado 
633168d1b1aSCarson Labrado     // Make sure we know how to process the retrieved entry attachment
634168d1b1aSCarson Labrado     if ((downloadEntryType != "BMC") && (downloadEntryType != "System"))
635168d1b1aSCarson Labrado     {
636168d1b1aSCarson Labrado         BMCWEB_LOG_ERROR("downloadEntryCallback() invalid entry type: {}",
637168d1b1aSCarson Labrado                          downloadEntryType);
638168d1b1aSCarson Labrado         messages::internalError(asyncResp->res);
639168d1b1aSCarson Labrado     }
640168d1b1aSCarson Labrado 
641168d1b1aSCarson Labrado     int fd = -1;
642168d1b1aSCarson Labrado     fd = dup(unixfd);
643168d1b1aSCarson Labrado     if (fd < 0)
644168d1b1aSCarson Labrado     {
645168d1b1aSCarson Labrado         BMCWEB_LOG_ERROR("Failed to open file");
646168d1b1aSCarson Labrado         messages::internalError(asyncResp->res);
647168d1b1aSCarson Labrado         return;
648168d1b1aSCarson Labrado     }
649b5f288d2SAbhilash Raju     if (!checkSizeLimit(fd, asyncResp->res))
650168d1b1aSCarson Labrado     {
651168d1b1aSCarson Labrado         close(fd);
652168d1b1aSCarson Labrado         return;
653168d1b1aSCarson Labrado     }
654168d1b1aSCarson Labrado     if (downloadEntryType == "System")
655168d1b1aSCarson Labrado     {
656b5f288d2SAbhilash Raju         if (!asyncResp->res.openFd(fd, bmcweb::EncodingType::Base64))
657b5f288d2SAbhilash Raju         {
658b5f288d2SAbhilash Raju             messages::internalError(asyncResp->res);
659b5f288d2SAbhilash Raju             close(fd);
660b5f288d2SAbhilash Raju             return;
661b5f288d2SAbhilash Raju         }
662168d1b1aSCarson Labrado         asyncResp->res.addHeader(
663168d1b1aSCarson Labrado             boost::beast::http::field::content_transfer_encoding, "Base64");
664b5f288d2SAbhilash Raju         return;
665168d1b1aSCarson Labrado     }
666b5f288d2SAbhilash Raju     if (!asyncResp->res.openFd(fd))
66727b0cf90SEd Tanous     {
668b5f288d2SAbhilash Raju         messages::internalError(asyncResp->res);
669b5f288d2SAbhilash Raju         close(fd);
670b5f288d2SAbhilash Raju         return;
67127b0cf90SEd Tanous     }
672168d1b1aSCarson Labrado     asyncResp->res.addHeader(boost::beast::http::field::content_type,
673168d1b1aSCarson Labrado                              "application/octet-stream");
674168d1b1aSCarson Labrado }
675168d1b1aSCarson Labrado 
676168d1b1aSCarson Labrado inline void
677168d1b1aSCarson Labrado     downloadDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
678168d1b1aSCarson Labrado                       const std::string& entryID, const std::string& dumpType)
679168d1b1aSCarson Labrado {
680168d1b1aSCarson Labrado     if (dumpType != "BMC")
681168d1b1aSCarson Labrado     {
682168d1b1aSCarson Labrado         BMCWEB_LOG_WARNING("Can't find Dump Entry {}", entryID);
683168d1b1aSCarson Labrado         messages::resourceNotFound(asyncResp->res, dumpType + " dump", entryID);
684168d1b1aSCarson Labrado         return;
685168d1b1aSCarson Labrado     }
686168d1b1aSCarson Labrado 
68718f8f608SEd Tanous     std::string dumpEntryPath = std::format("{}/entry/{}",
68818f8f608SEd Tanous                                             getDumpPath(dumpType), entryID);
689168d1b1aSCarson Labrado 
690168d1b1aSCarson Labrado     auto downloadDumpEntryHandler =
691168d1b1aSCarson Labrado         [asyncResp, entryID,
692168d1b1aSCarson Labrado          dumpType](const boost::system::error_code& ec,
693168d1b1aSCarson Labrado                    const sdbusplus::message::unix_fd& unixfd) {
694168d1b1aSCarson Labrado         downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
695168d1b1aSCarson Labrado     };
696168d1b1aSCarson Labrado 
697168d1b1aSCarson Labrado     crow::connections::systemBus->async_method_call(
698168d1b1aSCarson Labrado         std::move(downloadDumpEntryHandler), "xyz.openbmc_project.Dump.Manager",
699168d1b1aSCarson Labrado         dumpEntryPath, "xyz.openbmc_project.Dump.Entry", "GetFileHandle");
700168d1b1aSCarson Labrado }
701168d1b1aSCarson Labrado 
702168d1b1aSCarson Labrado inline void
703168d1b1aSCarson Labrado     downloadEventLogEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
704168d1b1aSCarson Labrado                           const std::string& systemName,
705168d1b1aSCarson Labrado                           const std::string& entryID,
706168d1b1aSCarson Labrado                           const std::string& dumpType)
707168d1b1aSCarson Labrado {
70825b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
709168d1b1aSCarson Labrado     {
710168d1b1aSCarson Labrado         // Option currently returns no systems.  TBD
711168d1b1aSCarson Labrado         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
712168d1b1aSCarson Labrado                                    systemName);
713168d1b1aSCarson Labrado         return;
714168d1b1aSCarson Labrado     }
715253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
716168d1b1aSCarson Labrado     {
717168d1b1aSCarson Labrado         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
718168d1b1aSCarson Labrado                                    systemName);
719168d1b1aSCarson Labrado         return;
720168d1b1aSCarson Labrado     }
721168d1b1aSCarson Labrado 
722168d1b1aSCarson Labrado     std::string entryPath =
723168d1b1aSCarson Labrado         sdbusplus::message::object_path("/xyz/openbmc_project/logging/entry") /
724168d1b1aSCarson Labrado         entryID;
725168d1b1aSCarson Labrado 
726168d1b1aSCarson Labrado     auto downloadEventLogEntryHandler =
727168d1b1aSCarson Labrado         [asyncResp, entryID,
728168d1b1aSCarson Labrado          dumpType](const boost::system::error_code& ec,
729168d1b1aSCarson Labrado                    const sdbusplus::message::unix_fd& unixfd) {
730168d1b1aSCarson Labrado         downloadEntryCallback(asyncResp, entryID, dumpType, ec, unixfd);
731168d1b1aSCarson Labrado     };
732168d1b1aSCarson Labrado 
733168d1b1aSCarson Labrado     crow::connections::systemBus->async_method_call(
734168d1b1aSCarson Labrado         std::move(downloadEventLogEntryHandler), "xyz.openbmc_project.Logging",
735168d1b1aSCarson Labrado         entryPath, "xyz.openbmc_project.Logging.Entry", "GetEntry");
736168d1b1aSCarson Labrado }
737168d1b1aSCarson Labrado 
7388e31778eSAsmitha Karunanithi inline DumpCreationProgress
7398e31778eSAsmitha Karunanithi     mapDbusStatusToDumpProgress(const std::string& status)
740a43be80fSAsmitha Karunanithi {
7418e31778eSAsmitha Karunanithi     if (status ==
7428e31778eSAsmitha Karunanithi             "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
7438e31778eSAsmitha Karunanithi         status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
7448e31778eSAsmitha Karunanithi     {
7458e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_FAILED;
7468e31778eSAsmitha Karunanithi     }
7478e31778eSAsmitha Karunanithi     if (status ==
7488e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
7498e31778eSAsmitha Karunanithi     {
7508e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_SUCCESS;
7518e31778eSAsmitha Karunanithi     }
7528e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7538e31778eSAsmitha Karunanithi }
7548e31778eSAsmitha Karunanithi 
7558e31778eSAsmitha Karunanithi inline DumpCreationProgress
7568e31778eSAsmitha Karunanithi     getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
7578e31778eSAsmitha Karunanithi {
7588e31778eSAsmitha Karunanithi     for (const auto& [key, val] : values)
7598e31778eSAsmitha Karunanithi     {
7608e31778eSAsmitha Karunanithi         if (key == "Status")
7618e31778eSAsmitha Karunanithi         {
7628e31778eSAsmitha Karunanithi             const std::string* value = std::get_if<std::string>(&val);
7638e31778eSAsmitha Karunanithi             if (value == nullptr)
7648e31778eSAsmitha Karunanithi             {
76562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Status property value is null");
7668e31778eSAsmitha Karunanithi                 return DumpCreationProgress::DUMP_CREATE_FAILED;
7678e31778eSAsmitha Karunanithi             }
7688e31778eSAsmitha Karunanithi             return mapDbusStatusToDumpProgress(*value);
7698e31778eSAsmitha Karunanithi         }
7708e31778eSAsmitha Karunanithi     }
7718e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7728e31778eSAsmitha Karunanithi }
7738e31778eSAsmitha Karunanithi 
7748e31778eSAsmitha Karunanithi inline std::string getDumpEntryPath(const std::string& dumpPath)
7758e31778eSAsmitha Karunanithi {
7768e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
7778e31778eSAsmitha Karunanithi     {
778253f11b8SEd Tanous         return std::format("/redfish/v1/Managers/{}/LogServices/Dump/Entries/",
7799f565090SEd Tanous                            BMCWEB_REDFISH_MANAGER_URI_NAME);
7808e31778eSAsmitha Karunanithi     }
7818e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
7828e31778eSAsmitha Karunanithi     {
783253f11b8SEd Tanous         return std::format("/redfish/v1/Systems/{}/LogServices/Dump/Entries/",
784253f11b8SEd Tanous                            BMCWEB_REDFISH_SYSTEM_URI_NAME);
7858e31778eSAsmitha Karunanithi     }
7868e31778eSAsmitha Karunanithi     return "";
7878e31778eSAsmitha Karunanithi }
7888e31778eSAsmitha Karunanithi 
7898e31778eSAsmitha Karunanithi inline void createDumpTaskCallback(
7908e31778eSAsmitha Karunanithi     task::Payload&& payload,
7918e31778eSAsmitha Karunanithi     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7928e31778eSAsmitha Karunanithi     const sdbusplus::message::object_path& createdObjPath)
7938e31778eSAsmitha Karunanithi {
7948e31778eSAsmitha Karunanithi     const std::string dumpPath = createdObjPath.parent_path().str;
7958e31778eSAsmitha Karunanithi     const std::string dumpId = createdObjPath.filename();
7968e31778eSAsmitha Karunanithi 
7978e31778eSAsmitha Karunanithi     std::string dumpEntryPath = getDumpEntryPath(dumpPath);
7988e31778eSAsmitha Karunanithi 
7998e31778eSAsmitha Karunanithi     if (dumpEntryPath.empty())
8008e31778eSAsmitha Karunanithi     {
80162598e31SEd Tanous         BMCWEB_LOG_ERROR("Invalid dump type received");
8028e31778eSAsmitha Karunanithi         messages::internalError(asyncResp->res);
8038e31778eSAsmitha Karunanithi         return;
8048e31778eSAsmitha Karunanithi     }
8058e31778eSAsmitha Karunanithi 
8068e31778eSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
8078cb2c024SEd Tanous         [asyncResp, payload = std::move(payload), createdObjPath,
8088e31778eSAsmitha Karunanithi          dumpEntryPath{std::move(dumpEntryPath)},
8095e7e2dc5SEd Tanous          dumpId](const boost::system::error_code& ec,
8108e31778eSAsmitha Karunanithi                  const std::string& introspectXml) {
8118e31778eSAsmitha Karunanithi         if (ec)
8128e31778eSAsmitha Karunanithi         {
81362598e31SEd Tanous             BMCWEB_LOG_ERROR("Introspect call failed with error: {}",
81462598e31SEd Tanous                              ec.message());
8158e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
8168e31778eSAsmitha Karunanithi             return;
8178e31778eSAsmitha Karunanithi         }
8188e31778eSAsmitha Karunanithi 
8198e31778eSAsmitha Karunanithi         // Check if the created dump object has implemented Progress
8208e31778eSAsmitha Karunanithi         // interface to track dump completion. If yes, fetch the "Status"
8218e31778eSAsmitha Karunanithi         // property of the interface, modify the task state accordingly.
8228e31778eSAsmitha Karunanithi         // Else, return task completed.
8238e31778eSAsmitha Karunanithi         tinyxml2::XMLDocument doc;
8248e31778eSAsmitha Karunanithi 
8258e31778eSAsmitha Karunanithi         doc.Parse(introspectXml.data(), introspectXml.size());
8268e31778eSAsmitha Karunanithi         tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
8278e31778eSAsmitha Karunanithi         if (pRoot == nullptr)
8288e31778eSAsmitha Karunanithi         {
82962598e31SEd Tanous             BMCWEB_LOG_ERROR("XML document failed to parse");
8308e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
8318e31778eSAsmitha Karunanithi             return;
8328e31778eSAsmitha Karunanithi         }
8338e31778eSAsmitha Karunanithi         tinyxml2::XMLElement* interfaceNode =
8348e31778eSAsmitha Karunanithi             pRoot->FirstChildElement("interface");
8358e31778eSAsmitha Karunanithi 
8368e31778eSAsmitha Karunanithi         bool isProgressIntfPresent = false;
8378e31778eSAsmitha Karunanithi         while (interfaceNode != nullptr)
8388e31778eSAsmitha Karunanithi         {
8398e31778eSAsmitha Karunanithi             const char* thisInterfaceName = interfaceNode->Attribute("name");
8408e31778eSAsmitha Karunanithi             if (thisInterfaceName != nullptr)
8418e31778eSAsmitha Karunanithi             {
8428e31778eSAsmitha Karunanithi                 if (thisInterfaceName ==
8438e31778eSAsmitha Karunanithi                     std::string_view("xyz.openbmc_project.Common.Progress"))
8448e31778eSAsmitha Karunanithi                 {
8458e31778eSAsmitha Karunanithi                     interfaceNode =
8468e31778eSAsmitha Karunanithi                         interfaceNode->NextSiblingElement("interface");
8478e31778eSAsmitha Karunanithi                     continue;
8488e31778eSAsmitha Karunanithi                 }
8498e31778eSAsmitha Karunanithi                 isProgressIntfPresent = true;
8508e31778eSAsmitha Karunanithi                 break;
8518e31778eSAsmitha Karunanithi             }
8528e31778eSAsmitha Karunanithi             interfaceNode = interfaceNode->NextSiblingElement("interface");
8538e31778eSAsmitha Karunanithi         }
8548e31778eSAsmitha Karunanithi 
855a43be80fSAsmitha Karunanithi         std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
8568e31778eSAsmitha Karunanithi             [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
8578b24275dSEd Tanous                 const boost::system::error_code& ec2, sdbusplus::message_t& msg,
858a43be80fSAsmitha Karunanithi                 const std::shared_ptr<task::TaskData>& taskData) {
8598b24275dSEd Tanous             if (ec2)
860cb13a392SEd Tanous             {
86162598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}: Error in creating dump",
86262598e31SEd Tanous                                  createdObjPath.str);
8638e31778eSAsmitha Karunanithi                 taskData->messages.emplace_back(messages::internalError());
8646145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
8656145ed6fSAsmitha Karunanithi                 return task::completed;
866cb13a392SEd Tanous             }
867b9d36b47SEd Tanous 
8688e31778eSAsmitha Karunanithi             if (isProgressIntfPresent)
869a43be80fSAsmitha Karunanithi             {
8708e31778eSAsmitha Karunanithi                 dbus::utility::DBusPropertiesMap values;
8718e31778eSAsmitha Karunanithi                 std::string prop;
8728e31778eSAsmitha Karunanithi                 msg.read(prop, values);
8738e31778eSAsmitha Karunanithi 
8748e31778eSAsmitha Karunanithi                 DumpCreationProgress dumpStatus =
8758e31778eSAsmitha Karunanithi                     getDumpCompletionStatus(values);
8768e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
8778e31778eSAsmitha Karunanithi                 {
87862598e31SEd Tanous                     BMCWEB_LOG_ERROR("{}: Error in creating dump",
87962598e31SEd Tanous                                      createdObjPath.str);
8808e31778eSAsmitha Karunanithi                     taskData->state = "Cancelled";
8818e31778eSAsmitha Karunanithi                     return task::completed;
8828e31778eSAsmitha Karunanithi                 }
8838e31778eSAsmitha Karunanithi 
8848e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
8858e31778eSAsmitha Karunanithi                 {
88662598e31SEd Tanous                     BMCWEB_LOG_DEBUG("{}: Dump creation task is in progress",
88762598e31SEd Tanous                                      createdObjPath.str);
8888e31778eSAsmitha Karunanithi                     return !task::completed;
8898e31778eSAsmitha Karunanithi                 }
8908e31778eSAsmitha Karunanithi             }
8918e31778eSAsmitha Karunanithi 
892a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
893a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
894a43be80fSAsmitha Karunanithi 
895c51a58eeSEd Tanous             boost::urls::url url = boost::urls::format(
896253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/LogServices/Dump/Entries/{}",
897253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME, dumpId);
898c51a58eeSEd Tanous 
899c51a58eeSEd Tanous             std::string headerLoc = "Location: ";
900c51a58eeSEd Tanous             headerLoc += url.buffer();
901c51a58eeSEd Tanous 
902002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
903a43be80fSAsmitha Karunanithi 
90462598e31SEd Tanous             BMCWEB_LOG_DEBUG("{}: Dump creation task completed",
90562598e31SEd Tanous                              createdObjPath.str);
906a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
907b47452b2SAsmitha Karunanithi             return task::completed;
908a43be80fSAsmitha Karunanithi         },
9098e31778eSAsmitha Karunanithi             "type='signal',interface='org.freedesktop.DBus.Properties',"
9108e31778eSAsmitha Karunanithi             "member='PropertiesChanged',path='" +
9118e31778eSAsmitha Karunanithi                 createdObjPath.str + "'");
912a43be80fSAsmitha Karunanithi 
9138e31778eSAsmitha Karunanithi         // The task timer is set to max time limit within which the
9148e31778eSAsmitha Karunanithi         // requested dump will be collected.
9158e31778eSAsmitha Karunanithi         task->startTimer(std::chrono::minutes(6));
916a43be80fSAsmitha Karunanithi         task->populateResp(asyncResp->res);
9178e31778eSAsmitha Karunanithi         task->payload.emplace(payload);
9188e31778eSAsmitha Karunanithi     },
9198e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", createdObjPath,
9208e31778eSAsmitha Karunanithi         "org.freedesktop.DBus.Introspectable", "Introspect");
921a43be80fSAsmitha Karunanithi }
922a43be80fSAsmitha Karunanithi 
9238d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9248d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
925a43be80fSAsmitha Karunanithi {
926fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
927fdd26906SClaire Weinan     if (dumpPath.empty())
928a43be80fSAsmitha Karunanithi     {
929a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
930a43be80fSAsmitha Karunanithi         return;
931a43be80fSAsmitha Karunanithi     }
932a43be80fSAsmitha Karunanithi 
933a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
934a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
935a43be80fSAsmitha Karunanithi 
93615ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
937a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
938a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
939a43be80fSAsmitha Karunanithi     {
940a43be80fSAsmitha Karunanithi         return;
941a43be80fSAsmitha Karunanithi     }
942a43be80fSAsmitha Karunanithi 
943a43be80fSAsmitha Karunanithi     if (dumpType == "System")
944a43be80fSAsmitha Karunanithi     {
945a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
946a43be80fSAsmitha Karunanithi         {
94762598e31SEd Tanous             BMCWEB_LOG_ERROR(
94862598e31SEd Tanous                 "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!");
949a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
950a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
951a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
952a43be80fSAsmitha Karunanithi             return;
953a43be80fSAsmitha Karunanithi         }
9543174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
955a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
956a43be80fSAsmitha Karunanithi         {
95762598e31SEd Tanous             BMCWEB_LOG_ERROR("Wrong parameter values passed");
958ace85d60SEd Tanous             messages::internalError(asyncResp->res);
959a43be80fSAsmitha Karunanithi             return;
960a43be80fSAsmitha Karunanithi         }
961253f11b8SEd Tanous         dumpPath = std::format("/redfish/v1/Systems/{}/LogServices/Dump/",
962253f11b8SEd Tanous                                BMCWEB_REDFISH_SYSTEM_URI_NAME);
963a43be80fSAsmitha Karunanithi     }
964a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
965a43be80fSAsmitha Karunanithi     {
966a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
967a43be80fSAsmitha Karunanithi         {
96862598e31SEd Tanous             BMCWEB_LOG_ERROR(
96962598e31SEd Tanous                 "CreateDump action parameter 'DiagnosticDataType' not found!");
970a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
971a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
972a43be80fSAsmitha Karunanithi             return;
973a43be80fSAsmitha Karunanithi         }
9743174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
975a43be80fSAsmitha Karunanithi         {
97662598e31SEd Tanous             BMCWEB_LOG_ERROR(
97762598e31SEd Tanous                 "Wrong parameter value passed for 'DiagnosticDataType'");
978ace85d60SEd Tanous             messages::internalError(asyncResp->res);
979a43be80fSAsmitha Karunanithi             return;
980a43be80fSAsmitha Karunanithi         }
981253f11b8SEd Tanous         dumpPath = std::format("/redfish/v1/Managers/{}/LogServices/Dump/",
982253f11b8SEd Tanous                                BMCWEB_REDFISH_MANAGER_URI_NAME);
9835907571dSAsmitha Karunanithi     }
9845907571dSAsmitha Karunanithi     else
9855907571dSAsmitha Karunanithi     {
98662598e31SEd Tanous         BMCWEB_LOG_ERROR("CreateDump failed. Unknown dump type");
9875907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
9885907571dSAsmitha Karunanithi         return;
989a43be80fSAsmitha Karunanithi     }
990a43be80fSAsmitha Karunanithi 
9918e31778eSAsmitha Karunanithi     std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
9928e31778eSAsmitha Karunanithi         createDumpParamVec;
9938e31778eSAsmitha Karunanithi 
994f574a8e1SCarson Labrado     if (req.session != nullptr)
995f574a8e1SCarson Labrado     {
99668dd075aSAsmitha Karunanithi         createDumpParamVec.emplace_back(
99768dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorId",
99868dd075aSAsmitha Karunanithi             req.session->clientIp);
99968dd075aSAsmitha Karunanithi         createDumpParamVec.emplace_back(
100068dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Dump.Create.CreateParameters.OriginatorType",
100168dd075aSAsmitha Karunanithi             "xyz.openbmc_project.Common.OriginatedBy.OriginatorTypes.Client");
1002f574a8e1SCarson Labrado     }
100368dd075aSAsmitha Karunanithi 
1004a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
10055e7e2dc5SEd Tanous         [asyncResp, payload(task::Payload(req)),
10065e7e2dc5SEd Tanous          dumpPath](const boost::system::error_code& ec,
10075e7e2dc5SEd Tanous                    const sdbusplus::message_t& msg,
10088e31778eSAsmitha Karunanithi                    const sdbusplus::message::object_path& objPath) mutable {
1009a43be80fSAsmitha Karunanithi         if (ec)
1010a43be80fSAsmitha Karunanithi         {
101162598e31SEd Tanous             BMCWEB_LOG_ERROR("CreateDump resp_handler got error {}", ec);
10125907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
10135907571dSAsmitha Karunanithi             if (dbusError == nullptr)
10145907571dSAsmitha Karunanithi             {
10155907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
10165907571dSAsmitha Karunanithi                 return;
10175907571dSAsmitha Karunanithi             }
10185907571dSAsmitha Karunanithi 
101962598e31SEd Tanous             BMCWEB_LOG_ERROR("CreateDump DBus error: {} and error msg: {}",
102062598e31SEd Tanous                              dbusError->name, dbusError->message);
10215907571dSAsmitha Karunanithi             if (std::string_view(
10225907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
10235907571dSAsmitha Karunanithi                 dbusError->name)
10245907571dSAsmitha Karunanithi             {
10255907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
10265907571dSAsmitha Karunanithi                 return;
10275907571dSAsmitha Karunanithi             }
10285907571dSAsmitha Karunanithi             if (std::string_view(
10295907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
10305907571dSAsmitha Karunanithi                 dbusError->name)
10315907571dSAsmitha Karunanithi             {
10325907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
10335907571dSAsmitha Karunanithi                 return;
10345907571dSAsmitha Karunanithi             }
10355907571dSAsmitha Karunanithi             if (std::string_view(
10365907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
10375907571dSAsmitha Karunanithi                 dbusError->name)
10385907571dSAsmitha Karunanithi             {
10395907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
10405907571dSAsmitha Karunanithi                 return;
10415907571dSAsmitha Karunanithi             }
10425907571dSAsmitha Karunanithi             // Other Dbus errors such as:
10435907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
10445907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
10455907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
10465907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
10475907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
10485907571dSAsmitha Karunanithi             // back to the client.
1049a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
1050a43be80fSAsmitha Karunanithi             return;
1051a43be80fSAsmitha Karunanithi         }
105262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Dump Created. Path: {}", objPath.str);
10538e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
1054a43be80fSAsmitha Karunanithi     },
105518f8f608SEd Tanous         "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
10568e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
1057a43be80fSAsmitha Karunanithi }
1058a43be80fSAsmitha Karunanithi 
10598d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
10608d1b46d7Szhanghch05                       const std::string& dumpType)
106180319af1SAsmitha Karunanithi {
10620d946211SClaire Weinan     crow::connections::systemBus->async_method_call(
10630d946211SClaire Weinan         [asyncResp](const boost::system::error_code& ec) {
106480319af1SAsmitha Karunanithi         if (ec)
106580319af1SAsmitha Karunanithi         {
106662598e31SEd Tanous             BMCWEB_LOG_ERROR("clearDump resp_handler got error {}", ec);
106780319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
106880319af1SAsmitha Karunanithi             return;
106980319af1SAsmitha Karunanithi         }
10700d946211SClaire Weinan     },
107118f8f608SEd Tanous         "xyz.openbmc_project.Dump.Manager", getDumpPath(dumpType),
10720d946211SClaire Weinan         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
107380319af1SAsmitha Karunanithi }
107480319af1SAsmitha Karunanithi 
1075df254f2cSEd Tanous inline void
1076b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
1077b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
1078b9d36b47SEd Tanous                              std::string& logfile)
1079043a0536SJohnathan Mantey {
1080d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
1081d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
1082d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
1083d1bde9e5SKrzysztof Grobelny 
1084d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1085d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1086d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1087d1bde9e5SKrzysztof Grobelny 
1088d1bde9e5SKrzysztof Grobelny     if (!success)
1089043a0536SJohnathan Mantey     {
1090d1bde9e5SKrzysztof Grobelny         return;
1091043a0536SJohnathan Mantey     }
1092d1bde9e5SKrzysztof Grobelny 
1093d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1094043a0536SJohnathan Mantey     {
1095d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1096d1bde9e5SKrzysztof Grobelny     }
1097d1bde9e5SKrzysztof Grobelny 
1098d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1099043a0536SJohnathan Mantey     {
1100d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1101043a0536SJohnathan Mantey     }
1102d1bde9e5SKrzysztof Grobelny 
1103d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1104043a0536SJohnathan Mantey     {
1105d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1106043a0536SJohnathan Mantey     }
1107043a0536SJohnathan Mantey }
1108043a0536SJohnathan Mantey 
11097e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
11101da66f75SEd Tanous {
1111c4bf6374SJason M. Bills     /**
1112c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1113c4bf6374SJason M. Bills      */
111422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1115ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1116002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1117002d39b4SEd Tanous             [&app](const crow::Request& req,
111822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
111922d268cbSEd Tanous                    const std::string& systemName) {
11203ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1121c4bf6374SJason M. Bills         {
112245ca1b86SEd Tanous             return;
112345ca1b86SEd Tanous         }
112425b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
11257f3e84a1SEd Tanous         {
11267f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
11277f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
11287f3e84a1SEd Tanous                                        systemName);
11297f3e84a1SEd Tanous             return;
11307f3e84a1SEd Tanous         }
1131253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
113222d268cbSEd Tanous         {
113322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
113422d268cbSEd Tanous                                        systemName);
113522d268cbSEd Tanous             return;
113622d268cbSEd Tanous         }
113722d268cbSEd Tanous 
11387e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
11397e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1140c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1141c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1142c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1143253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices",
1144253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
114545ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1146c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1147c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1148002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1149c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
11501476687dSEd Tanous         nlohmann::json::object_t eventLog;
11511476687dSEd Tanous         eventLog["@odata.id"] =
1152253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/EventLog",
1153253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
1154b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(eventLog));
115525b54dbaSEd Tanous         if constexpr (BMCWEB_REDFISH_DUMP_LOG)
115625b54dbaSEd Tanous         {
11571476687dSEd Tanous             nlohmann::json::object_t dumpLog;
115825b54dbaSEd Tanous             dumpLog["@odata.id"] =
1159253f11b8SEd Tanous                 std::format("/redfish/v1/Systems/{}/LogServices/Dump",
1160253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
1161b2ba3072SPatrick Williams             logServiceArray.emplace_back(std::move(dumpLog));
116225b54dbaSEd Tanous         }
1163c9bb6861Sraviteja-b 
11645ffd11f2SGunnar Mills         if constexpr (BMCWEB_REDFISH_CPU_LOG)
116525b54dbaSEd Tanous         {
11661476687dSEd Tanous             nlohmann::json::object_t crashdump;
11671476687dSEd Tanous             crashdump["@odata.id"] =
1168253f11b8SEd Tanous                 std::format("/redfish/v1/Systems/{}/LogServices/Crashdump",
1169253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
1170b2ba3072SPatrick Williams             logServiceArray.emplace_back(std::move(crashdump));
117125b54dbaSEd Tanous         }
1172b7028ebfSSpencer Ku 
117325b54dbaSEd Tanous         if constexpr (BMCWEB_REDFISH_HOST_LOGGER)
117425b54dbaSEd Tanous         {
11751476687dSEd Tanous             nlohmann::json::object_t hostlogger;
11761476687dSEd Tanous             hostlogger["@odata.id"] =
1177253f11b8SEd Tanous                 std::format("/redfish/v1/Systems/{}/LogServices/HostLogger",
1178253f11b8SEd Tanous                             BMCWEB_REDFISH_SYSTEM_URI_NAME);
1179b2ba3072SPatrick Williams             logServiceArray.emplace_back(std::move(hostlogger));
118025b54dbaSEd Tanous         }
1181c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1182c4bf6374SJason M. Bills             logServiceArray.size();
1183a3316fc6SZhikuiRen 
11847a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
11857a1dbc48SGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode"};
11867a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
11877a1dbc48SGeorge Liu             "/", 0, interfaces,
11887a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
1189b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1190b9d36b47SEd Tanous                             subtreePath) {
1191a3316fc6SZhikuiRen             if (ec)
1192a3316fc6SZhikuiRen             {
119362598e31SEd Tanous                 BMCWEB_LOG_ERROR("{}", ec);
1194a3316fc6SZhikuiRen                 return;
1195a3316fc6SZhikuiRen             }
1196a3316fc6SZhikuiRen 
119755f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1198a3316fc6SZhikuiRen             {
1199a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1200a3316fc6SZhikuiRen                 {
120123a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1202a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1203613dabeaSEd Tanous                     nlohmann::json::object_t member;
1204253f11b8SEd Tanous                     member["@odata.id"] = std::format(
1205253f11b8SEd Tanous                         "/redfish/v1/Systems/{}/LogServices/PostCodes",
1206253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
1207613dabeaSEd Tanous 
1208b2ba3072SPatrick Williams                     logServiceArrayLocal.emplace_back(std::move(member));
1209613dabeaSEd Tanous 
121045ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
121123a21a1cSEd Tanous                         logServiceArrayLocal.size();
1212a3316fc6SZhikuiRen                     return;
1213a3316fc6SZhikuiRen                 }
1214a3316fc6SZhikuiRen             }
12157a1dbc48SGeorge Liu         });
12167e860f15SJohn Edward Broadbent     });
1217c4bf6374SJason M. Bills }
1218c4bf6374SJason M. Bills 
12197e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1220c4bf6374SJason M. Bills {
122122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1222ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1223002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1224002d39b4SEd Tanous             [&app](const crow::Request& req,
122522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
122622d268cbSEd Tanous                    const std::string& systemName) {
12273ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
122845ca1b86SEd Tanous         {
122945ca1b86SEd Tanous             return;
123045ca1b86SEd Tanous         }
1231253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
123222d268cbSEd Tanous         {
123322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
123422d268cbSEd Tanous                                        systemName);
123522d268cbSEd Tanous             return;
123622d268cbSEd Tanous         }
1237c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1238253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/EventLog",
1239253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
1240c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1241b25644a1SJanet Adkins             "#LogService.v1_2_0.LogService";
1242c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1243002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1244c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1245539d8c6bSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] =
1246539d8c6bSEd Tanous             log_service::OverWritePolicy::WrapsWhenFull;
12477c8c4058STejas Patil 
12487c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
12492b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
12507c8c4058STejas Patil 
12517c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
12527c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
12537c8c4058STejas Patil             redfishDateTimeOffset.second;
12547c8c4058STejas Patil 
12551476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
1256253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/EventLog/Entries",
1257253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
125820fa6a2cSEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"]
1259e7d6c8b2SGunnar Mills 
126020fa6a2cSEd Tanous             = std::format(
1261253f11b8SEd Tanous                 "/redfish/v1/Systems/{}/LogServices/EventLog/Actions/LogService.ClearLog",
126220fa6a2cSEd Tanous                 BMCWEB_REDFISH_SYSTEM_URI_NAME);
12637e860f15SJohn Edward Broadbent     });
1264489640c6SJason M. Bills }
1265489640c6SJason M. Bills 
1266599b9af3SAlexander Hansen inline void handleSystemsLogServicesEventLogActionsClearPost(
1267599b9af3SAlexander Hansen     App& app, const crow::Request& req,
126822d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1269599b9af3SAlexander Hansen     const std::string& systemName)
1270599b9af3SAlexander Hansen {
12713ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
127245ca1b86SEd Tanous     {
127345ca1b86SEd Tanous         return;
127445ca1b86SEd Tanous     }
1275253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
127622d268cbSEd Tanous     {
127722d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
127822d268cbSEd Tanous                                    systemName);
127922d268cbSEd Tanous         return;
128022d268cbSEd Tanous     }
1281599b9af3SAlexander Hansen 
1282489640c6SJason M. Bills     // Clear the EventLog by deleting the log files
1283489640c6SJason M. Bills     std::vector<std::filesystem::path> redfishLogFiles;
1284489640c6SJason M. Bills     if (getRedfishLogFiles(redfishLogFiles))
1285489640c6SJason M. Bills     {
1286489640c6SJason M. Bills         for (const std::filesystem::path& file : redfishLogFiles)
1287489640c6SJason M. Bills         {
1288489640c6SJason M. Bills             std::error_code ec;
1289489640c6SJason M. Bills             std::filesystem::remove(file, ec);
1290489640c6SJason M. Bills         }
1291489640c6SJason M. Bills     }
1292489640c6SJason M. Bills 
1293489640c6SJason M. Bills     // Reload rsyslog so it knows to start new log files
1294489640c6SJason M. Bills     crow::connections::systemBus->async_method_call(
12955e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1296489640c6SJason M. Bills         if (ec)
1297489640c6SJason M. Bills         {
129862598e31SEd Tanous             BMCWEB_LOG_ERROR("Failed to reload rsyslog: {}", ec);
1299489640c6SJason M. Bills             messages::internalError(asyncResp->res);
1300489640c6SJason M. Bills             return;
1301489640c6SJason M. Bills         }
1302489640c6SJason M. Bills 
1303489640c6SJason M. Bills         messages::success(asyncResp->res);
1304489640c6SJason M. Bills     },
1305489640c6SJason M. Bills         "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1306002d39b4SEd Tanous         "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1307002d39b4SEd Tanous         "replace");
1308599b9af3SAlexander Hansen }
1309599b9af3SAlexander Hansen 
1310599b9af3SAlexander Hansen inline void requestRoutesJournalEventLogClear(App& app)
1311599b9af3SAlexander Hansen {
1312599b9af3SAlexander Hansen     BMCWEB_ROUTE(
1313599b9af3SAlexander Hansen         app,
1314599b9af3SAlexander Hansen         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1315599b9af3SAlexander Hansen         .privileges({{"ConfigureComponents"}})
1316599b9af3SAlexander Hansen         .methods(boost::beast::http::verb::post)(std::bind_front(
1317599b9af3SAlexander Hansen             handleSystemsLogServicesEventLogActionsClearPost, std::ref(app)));
1318c4bf6374SJason M. Bills }
1319c4bf6374SJason M. Bills 
1320ac992cdeSJason M. Bills enum class LogParseError
1321ac992cdeSJason M. Bills {
1322ac992cdeSJason M. Bills     success,
1323ac992cdeSJason M. Bills     parseFailed,
1324ac992cdeSJason M. Bills     messageIdNotInRegistry,
1325ac992cdeSJason M. Bills };
1326ac992cdeSJason M. Bills 
1327ac992cdeSJason M. Bills static LogParseError
1328ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1329b5a76932SEd Tanous                           const std::string& logEntry,
1330de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1331c4bf6374SJason M. Bills {
133295820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1333cd225da8SJason M. Bills     // First get the Timestamp
1334f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1335cd225da8SJason M. Bills     if (space == std::string::npos)
133695820184SJason M. Bills     {
1337ac992cdeSJason M. Bills         return LogParseError::parseFailed;
133895820184SJason M. Bills     }
1339cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1340cd225da8SJason M. Bills     // Then get the log contents
1341f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1342cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1343cd225da8SJason M. Bills     {
1344ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1345cd225da8SJason M. Bills     }
1346cd225da8SJason M. Bills     std::string_view entry(logEntry);
1347cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1348cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1349cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
135050ebd4afSEd Tanous     bmcweb::split(logEntryFields, entry, ',');
1351cd225da8SJason M. Bills     // We need at least a MessageId to be valid
13521e6deaf6SEd Tanous     auto logEntryIter = logEntryFields.begin();
13531e6deaf6SEd Tanous     if (logEntryIter == logEntryFields.end())
1354cd225da8SJason M. Bills     {
1355ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1356cd225da8SJason M. Bills     }
13571e6deaf6SEd Tanous     std::string& messageID = *logEntryIter;
13584851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1359fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1360c4bf6374SJason M. Bills 
13611e6deaf6SEd Tanous     logEntryIter++;
136254417b02SSui Chen     if (message == nullptr)
1363c4bf6374SJason M. Bills     {
136462598e31SEd Tanous         BMCWEB_LOG_WARNING("Log entry not found in registry: {}", logEntry);
1365ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1366c4bf6374SJason M. Bills     }
1367c4bf6374SJason M. Bills 
13681e6deaf6SEd Tanous     std::vector<std::string_view> messageArgs(logEntryIter,
13691e6deaf6SEd Tanous                                               logEntryFields.end());
1370c05bba45SEd Tanous     messageArgs.resize(message->numberOfArgs);
1371c05bba45SEd Tanous 
13721e6deaf6SEd Tanous     std::string msg = redfish::registries::fillMessageArgs(messageArgs,
13731e6deaf6SEd Tanous                                                            message->message);
13741e6deaf6SEd Tanous     if (msg.empty())
137515a86ff6SJason M. Bills     {
13761e6deaf6SEd Tanous         return LogParseError::parseFailed;
137715a86ff6SJason M. Bills     }
13784851d45dSJason M. Bills 
137995820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
138095820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
138195820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1382f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1383f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
138495820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1385c4bf6374SJason M. Bills     {
138695820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1387c4bf6374SJason M. Bills     }
1388c4bf6374SJason M. Bills 
1389c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
13909c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1391ef4c65b7SEd Tanous     logEntryJson["@odata.id"] = boost::urls::format(
1392253f11b8SEd Tanous         "/redfish/v1/Systems/{}/LogServices/EventLog/Entries/{}",
1393253f11b8SEd Tanous         BMCWEB_REDFISH_SYSTEM_URI_NAME, logEntryID);
139484afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
139584afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
139684afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
139784afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
139884afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
139984afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
140084afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
140184afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1402ac992cdeSJason M. Bills     return LogParseError::success;
1403c4bf6374SJason M. Bills }
1404c4bf6374SJason M. Bills 
1405*b729096dSEd Tanous inline void afterLogEntriesGetManagedObjects(
1406*b729096dSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1407*b729096dSEd Tanous     const boost::system::error_code& ec,
1408*b729096dSEd Tanous     const dbus::utility::ManagedObjectType& resp)
1409*b729096dSEd Tanous {
1410*b729096dSEd Tanous     if (ec)
1411*b729096dSEd Tanous     {
1412*b729096dSEd Tanous         // TODO Handle for specific error code
1413*b729096dSEd Tanous         BMCWEB_LOG_ERROR("getLogEntriesIfaceData resp_handler got error {}",
1414*b729096dSEd Tanous                          ec);
1415*b729096dSEd Tanous         messages::internalError(asyncResp->res);
1416*b729096dSEd Tanous         return;
1417*b729096dSEd Tanous     }
1418*b729096dSEd Tanous     nlohmann::json::array_t entriesArray;
1419*b729096dSEd Tanous     for (const auto& objectPath : resp)
1420*b729096dSEd Tanous     {
1421*b729096dSEd Tanous         const uint32_t* id = nullptr;
1422*b729096dSEd Tanous         const uint64_t* timestamp = nullptr;
1423*b729096dSEd Tanous         const uint64_t* updateTimestamp = nullptr;
1424*b729096dSEd Tanous         const std::string* severity = nullptr;
1425*b729096dSEd Tanous         const std::string* message = nullptr;
1426*b729096dSEd Tanous         const std::string* filePath = nullptr;
1427*b729096dSEd Tanous         const std::string* resolution = nullptr;
1428*b729096dSEd Tanous         bool resolved = false;
1429*b729096dSEd Tanous         const std::string* notify = nullptr;
1430*b729096dSEd Tanous 
1431*b729096dSEd Tanous         for (const auto& interfaceMap : objectPath.second)
1432*b729096dSEd Tanous         {
1433*b729096dSEd Tanous             if (interfaceMap.first == "xyz.openbmc_project.Logging.Entry")
1434*b729096dSEd Tanous             {
1435*b729096dSEd Tanous                 for (const auto& propertyMap : interfaceMap.second)
1436*b729096dSEd Tanous                 {
1437*b729096dSEd Tanous                     if (propertyMap.first == "Id")
1438*b729096dSEd Tanous                     {
1439*b729096dSEd Tanous                         id = std::get_if<uint32_t>(&propertyMap.second);
1440*b729096dSEd Tanous                     }
1441*b729096dSEd Tanous                     else if (propertyMap.first == "Timestamp")
1442*b729096dSEd Tanous                     {
1443*b729096dSEd Tanous                         timestamp = std::get_if<uint64_t>(&propertyMap.second);
1444*b729096dSEd Tanous                     }
1445*b729096dSEd Tanous                     else if (propertyMap.first == "UpdateTimestamp")
1446*b729096dSEd Tanous                     {
1447*b729096dSEd Tanous                         updateTimestamp =
1448*b729096dSEd Tanous                             std::get_if<uint64_t>(&propertyMap.second);
1449*b729096dSEd Tanous                     }
1450*b729096dSEd Tanous                     else if (propertyMap.first == "Severity")
1451*b729096dSEd Tanous                     {
1452*b729096dSEd Tanous                         severity =
1453*b729096dSEd Tanous                             std::get_if<std::string>(&propertyMap.second);
1454*b729096dSEd Tanous                     }
1455*b729096dSEd Tanous                     else if (propertyMap.first == "Resolution")
1456*b729096dSEd Tanous                     {
1457*b729096dSEd Tanous                         resolution =
1458*b729096dSEd Tanous                             std::get_if<std::string>(&propertyMap.second);
1459*b729096dSEd Tanous                     }
1460*b729096dSEd Tanous                     else if (propertyMap.first == "Message")
1461*b729096dSEd Tanous                     {
1462*b729096dSEd Tanous                         message = std::get_if<std::string>(&propertyMap.second);
1463*b729096dSEd Tanous                     }
1464*b729096dSEd Tanous                     else if (propertyMap.first == "Resolved")
1465*b729096dSEd Tanous                     {
1466*b729096dSEd Tanous                         const bool* resolveptr =
1467*b729096dSEd Tanous                             std::get_if<bool>(&propertyMap.second);
1468*b729096dSEd Tanous                         if (resolveptr == nullptr)
1469*b729096dSEd Tanous                         {
1470*b729096dSEd Tanous                             messages::internalError(asyncResp->res);
1471*b729096dSEd Tanous                             return;
1472*b729096dSEd Tanous                         }
1473*b729096dSEd Tanous                         resolved = *resolveptr;
1474*b729096dSEd Tanous                     }
1475*b729096dSEd Tanous                     else if (propertyMap.first == "ServiceProviderNotify")
1476*b729096dSEd Tanous                     {
1477*b729096dSEd Tanous                         notify = std::get_if<std::string>(&propertyMap.second);
1478*b729096dSEd Tanous                         if (notify == nullptr)
1479*b729096dSEd Tanous                         {
1480*b729096dSEd Tanous                             messages::internalError(asyncResp->res);
1481*b729096dSEd Tanous                             return;
1482*b729096dSEd Tanous                         }
1483*b729096dSEd Tanous                     }
1484*b729096dSEd Tanous                 }
1485*b729096dSEd Tanous                 if (id == nullptr || message == nullptr || severity == nullptr)
1486*b729096dSEd Tanous                 {
1487*b729096dSEd Tanous                     messages::internalError(asyncResp->res);
1488*b729096dSEd Tanous                     return;
1489*b729096dSEd Tanous                 }
1490*b729096dSEd Tanous             }
1491*b729096dSEd Tanous             else if (interfaceMap.first ==
1492*b729096dSEd Tanous                      "xyz.openbmc_project.Common.FilePath")
1493*b729096dSEd Tanous             {
1494*b729096dSEd Tanous                 for (const auto& propertyMap : interfaceMap.second)
1495*b729096dSEd Tanous                 {
1496*b729096dSEd Tanous                     if (propertyMap.first == "Path")
1497*b729096dSEd Tanous                     {
1498*b729096dSEd Tanous                         filePath =
1499*b729096dSEd Tanous                             std::get_if<std::string>(&propertyMap.second);
1500*b729096dSEd Tanous                     }
1501*b729096dSEd Tanous                 }
1502*b729096dSEd Tanous             }
1503*b729096dSEd Tanous         }
1504*b729096dSEd Tanous         // Object path without the
1505*b729096dSEd Tanous         // xyz.openbmc_project.Logging.Entry interface, ignore
1506*b729096dSEd Tanous         // and continue.
1507*b729096dSEd Tanous         if (id == nullptr || message == nullptr || severity == nullptr ||
1508*b729096dSEd Tanous             timestamp == nullptr || updateTimestamp == nullptr)
1509*b729096dSEd Tanous         {
1510*b729096dSEd Tanous             continue;
1511*b729096dSEd Tanous         }
1512*b729096dSEd Tanous         nlohmann::json& thisEntry = entriesArray.emplace_back();
1513*b729096dSEd Tanous         thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1514*b729096dSEd Tanous         thisEntry["@odata.id"] = boost::urls::format(
1515*b729096dSEd Tanous             "/redfish/v1/Systems/{}/LogServices/EventLog/Entries/{}",
1516*b729096dSEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME, std::to_string(*id));
1517*b729096dSEd Tanous         thisEntry["Name"] = "System Event Log Entry";
1518*b729096dSEd Tanous         thisEntry["Id"] = std::to_string(*id);
1519*b729096dSEd Tanous         thisEntry["Message"] = *message;
1520*b729096dSEd Tanous         thisEntry["Resolved"] = resolved;
1521*b729096dSEd Tanous         if ((resolution != nullptr) && (!(*resolution).empty()))
1522*b729096dSEd Tanous         {
1523*b729096dSEd Tanous             thisEntry["Resolution"] = *resolution;
1524*b729096dSEd Tanous         }
1525*b729096dSEd Tanous         if (notify != nullptr)
1526*b729096dSEd Tanous         {
1527*b729096dSEd Tanous             std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
1528*b729096dSEd Tanous             if (notifyAction)
1529*b729096dSEd Tanous             {
1530*b729096dSEd Tanous                 thisEntry["ServiceProviderNotified"] = *notifyAction;
1531*b729096dSEd Tanous             }
1532*b729096dSEd Tanous         }
1533*b729096dSEd Tanous         thisEntry["EntryType"] = "Event";
1534*b729096dSEd Tanous         thisEntry["Severity"] = translateSeverityDbusToRedfish(*severity);
1535*b729096dSEd Tanous         thisEntry["Created"] =
1536*b729096dSEd Tanous             redfish::time_utils::getDateTimeUintMs(*timestamp);
1537*b729096dSEd Tanous         thisEntry["Modified"] =
1538*b729096dSEd Tanous             redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1539*b729096dSEd Tanous         if (filePath != nullptr)
1540*b729096dSEd Tanous         {
1541*b729096dSEd Tanous             thisEntry["AdditionalDataURI"] =
1542*b729096dSEd Tanous                 std::format(
1543*b729096dSEd Tanous                     "/redfish/v1/Systems/{}/LogServices/EventLog/Entries/",
1544*b729096dSEd Tanous                     BMCWEB_REDFISH_SYSTEM_URI_NAME) +
1545*b729096dSEd Tanous                 std::to_string(*id) + "/attachment";
1546*b729096dSEd Tanous         }
1547*b729096dSEd Tanous     }
1548*b729096dSEd Tanous     std::ranges::sort(entriesArray, [](const nlohmann::json& left,
1549*b729096dSEd Tanous                                        const nlohmann::json& right) {
1550*b729096dSEd Tanous         return (left["Id"] <= right["Id"]);
1551*b729096dSEd Tanous     });
1552*b729096dSEd Tanous     asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
1553*b729096dSEd Tanous     asyncResp->res.jsonValue["Members"] = std::move(entriesArray);
1554*b729096dSEd Tanous }
1555*b729096dSEd Tanous 
1556599b9af3SAlexander Hansen inline void handleSystemsLogServiceEventLogLogEntryCollection(
1557599b9af3SAlexander Hansen     App& app, const crow::Request& req,
155822d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1559599b9af3SAlexander Hansen     const std::string& systemName)
1560599b9af3SAlexander Hansen {
1561c937d2bfSEd Tanous     query_param::QueryCapabilities capabilities = {
1562c937d2bfSEd Tanous         .canDelegateTop = true,
1563c937d2bfSEd Tanous         .canDelegateSkip = true,
1564c937d2bfSEd Tanous     };
1565c937d2bfSEd Tanous     query_param::Query delegatedQuery;
1566599b9af3SAlexander Hansen     if (!redfish::setUpRedfishRouteWithDelegation(app, req, asyncResp,
1567599b9af3SAlexander Hansen                                                   delegatedQuery, capabilities))
1568c4bf6374SJason M. Bills     {
1569c4bf6374SJason M. Bills         return;
1570c4bf6374SJason M. Bills     }
157125b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
15727f3e84a1SEd Tanous     {
15737f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
15747f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
15757f3e84a1SEd Tanous                                    systemName);
15767f3e84a1SEd Tanous         return;
15777f3e84a1SEd Tanous     }
1578253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
157922d268cbSEd Tanous     {
158022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
158122d268cbSEd Tanous                                    systemName);
158222d268cbSEd Tanous         return;
158322d268cbSEd Tanous     }
158422d268cbSEd Tanous 
15855143f7a5SJiaqing Zhao     size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
15863648c8beSEd Tanous     size_t skip = delegatedQuery.skip.value_or(0);
15873648c8beSEd Tanous 
15887e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
15897e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
1590c4bf6374SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
1591c4bf6374SJason M. Bills         "#LogEntryCollection.LogEntryCollection";
1592c4bf6374SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
1593253f11b8SEd Tanous         std::format("/redfish/v1/Systems/{}/LogServices/EventLog/Entries",
1594253f11b8SEd Tanous                     BMCWEB_REDFISH_SYSTEM_URI_NAME);
1595c4bf6374SJason M. Bills     asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1596c4bf6374SJason M. Bills     asyncResp->res.jsonValue["Description"] =
1597c4bf6374SJason M. Bills         "Collection of System Event Log Entries";
1598cb92c03bSAndrew Geissler 
15994978b63fSJason M. Bills     nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1600c4bf6374SJason M. Bills     logEntryArray = nlohmann::json::array();
16017e860f15SJohn Edward Broadbent     // Go through the log files and create a unique ID for each
16027e860f15SJohn Edward Broadbent     // entry
160395820184SJason M. Bills     std::vector<std::filesystem::path> redfishLogFiles;
160495820184SJason M. Bills     getRedfishLogFiles(redfishLogFiles);
1605b01bf299SEd Tanous     uint64_t entryCount = 0;
1606cd225da8SJason M. Bills     std::string logEntry;
160795820184SJason M. Bills 
16087e860f15SJohn Edward Broadbent     // Oldest logs are in the last file, so start there and loop
16097e860f15SJohn Edward Broadbent     // backwards
1610599b9af3SAlexander Hansen     for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend(); it++)
1611c4bf6374SJason M. Bills     {
1612cd225da8SJason M. Bills         std::ifstream logStream(*it);
161395820184SJason M. Bills         if (!logStream.is_open())
1614c4bf6374SJason M. Bills         {
1615c4bf6374SJason M. Bills             continue;
1616c4bf6374SJason M. Bills         }
1617c4bf6374SJason M. Bills 
1618e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
1619e85d6b16SJason M. Bills         bool firstEntry = true;
162095820184SJason M. Bills         while (std::getline(logStream, logEntry))
162195820184SJason M. Bills         {
1622c4bf6374SJason M. Bills             std::string idStr;
1623e85d6b16SJason M. Bills             if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1624c4bf6374SJason M. Bills             {
1625c4bf6374SJason M. Bills                 continue;
1626c4bf6374SJason M. Bills             }
1627e85d6b16SJason M. Bills             firstEntry = false;
1628e85d6b16SJason M. Bills 
1629de703c5dSJason M. Bills             nlohmann::json::object_t bmcLogEntry;
163089492a15SPatrick Williams             LogParseError status = fillEventLogEntryJson(idStr, logEntry,
163189492a15SPatrick Williams                                                          bmcLogEntry);
1632ac992cdeSJason M. Bills             if (status == LogParseError::messageIdNotInRegistry)
1633ac992cdeSJason M. Bills             {
1634ac992cdeSJason M. Bills                 continue;
1635ac992cdeSJason M. Bills             }
1636ac992cdeSJason M. Bills             if (status != LogParseError::success)
1637c4bf6374SJason M. Bills             {
1638c4bf6374SJason M. Bills                 messages::internalError(asyncResp->res);
1639c4bf6374SJason M. Bills                 return;
1640c4bf6374SJason M. Bills             }
1641de703c5dSJason M. Bills 
1642de703c5dSJason M. Bills             entryCount++;
1643de703c5dSJason M. Bills             // Handle paging using skip (number of entries to skip from the
1644de703c5dSJason M. Bills             // start) and top (number of entries to display)
16453648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
1646de703c5dSJason M. Bills             {
1647de703c5dSJason M. Bills                 continue;
1648de703c5dSJason M. Bills             }
1649de703c5dSJason M. Bills 
1650b2ba3072SPatrick Williams             logEntryArray.emplace_back(std::move(bmcLogEntry));
1651c4bf6374SJason M. Bills         }
165295820184SJason M. Bills     }
1653c4bf6374SJason M. Bills     asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
16543648c8beSEd Tanous     if (skip + top < entryCount)
1655c4bf6374SJason M. Bills     {
1656599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Members@odata.nextLink"] =
1657599b9af3SAlexander Hansen             boost::urls::format(
1658253f11b8SEd Tanous                 "/redfish/v1/Systems/{}/LogServices/EventLog/Entries?$skip={}",
1659253f11b8SEd Tanous                 BMCWEB_REDFISH_SYSTEM_URI_NAME, std::to_string(skip + top));
1660c4bf6374SJason M. Bills     }
1661897967deSJason M. Bills }
1662897967deSJason M. Bills 
1663599b9af3SAlexander Hansen inline void requestRoutesJournalEventLogEntryCollection(App& app)
1664897967deSJason M. Bills {
1665599b9af3SAlexander Hansen     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1666599b9af3SAlexander Hansen         .privileges(redfish::privileges::getLogEntryCollection)
1667599b9af3SAlexander Hansen         .methods(boost::beast::http::verb::get)(std::bind_front(
1668599b9af3SAlexander Hansen             handleSystemsLogServiceEventLogLogEntryCollection, std::ref(app)));
1669599b9af3SAlexander Hansen }
1670599b9af3SAlexander Hansen 
1671599b9af3SAlexander Hansen inline void handleSystemsLogServiceEventLogEntriesGet(
1672599b9af3SAlexander Hansen     App& app, const crow::Request& req,
16737e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1674599b9af3SAlexander Hansen     const std::string& systemName, const std::string& param)
1675599b9af3SAlexander Hansen {
16763ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
167745ca1b86SEd Tanous     {
167845ca1b86SEd Tanous         return;
167945ca1b86SEd Tanous     }
168025b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
16817f3e84a1SEd Tanous     {
16827f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
16837f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
16847f3e84a1SEd Tanous                                    systemName);
16857f3e84a1SEd Tanous         return;
16867f3e84a1SEd Tanous     }
168722d268cbSEd Tanous 
1688253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
168922d268cbSEd Tanous     {
169022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
169122d268cbSEd Tanous                                    systemName);
169222d268cbSEd Tanous         return;
169322d268cbSEd Tanous     }
169422d268cbSEd Tanous 
16957e860f15SJohn Edward Broadbent     const std::string& targetID = param;
16968d1b46d7Szhanghch05 
16977e860f15SJohn Edward Broadbent     // Go through the log files and check the unique ID for each
16987e860f15SJohn Edward Broadbent     // entry to find the target entry
1699897967deSJason M. Bills     std::vector<std::filesystem::path> redfishLogFiles;
1700897967deSJason M. Bills     getRedfishLogFiles(redfishLogFiles);
1701897967deSJason M. Bills     std::string logEntry;
1702897967deSJason M. Bills 
17037e860f15SJohn Edward Broadbent     // Oldest logs are in the last file, so start there and loop
17047e860f15SJohn Edward Broadbent     // backwards
1705599b9af3SAlexander Hansen     for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend(); it++)
1706897967deSJason M. Bills     {
1707897967deSJason M. Bills         std::ifstream logStream(*it);
1708897967deSJason M. Bills         if (!logStream.is_open())
1709897967deSJason M. Bills         {
1710897967deSJason M. Bills             continue;
1711897967deSJason M. Bills         }
1712897967deSJason M. Bills 
1713897967deSJason M. Bills         // Reset the unique ID on the first entry
1714897967deSJason M. Bills         bool firstEntry = true;
1715897967deSJason M. Bills         while (std::getline(logStream, logEntry))
1716897967deSJason M. Bills         {
1717897967deSJason M. Bills             std::string idStr;
1718897967deSJason M. Bills             if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1719897967deSJason M. Bills             {
1720897967deSJason M. Bills                 continue;
1721897967deSJason M. Bills             }
1722897967deSJason M. Bills             firstEntry = false;
1723897967deSJason M. Bills 
1724897967deSJason M. Bills             if (idStr == targetID)
1725897967deSJason M. Bills             {
1726de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1727599b9af3SAlexander Hansen                 LogParseError status = fillEventLogEntryJson(idStr, logEntry,
1728599b9af3SAlexander Hansen                                                              bmcLogEntry);
1729ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1730897967deSJason M. Bills                 {
1731897967deSJason M. Bills                     messages::internalError(asyncResp->res);
1732897967deSJason M. Bills                     return;
1733897967deSJason M. Bills                 }
1734d405bb51SJason M. Bills                 asyncResp->res.jsonValue.update(bmcLogEntry);
1735897967deSJason M. Bills                 return;
1736897967deSJason M. Bills             }
1737897967deSJason M. Bills         }
1738897967deSJason M. Bills     }
1739897967deSJason M. Bills     // Requested ID was not found
17409db4ba25SJiaqing Zhao     messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
1741599b9af3SAlexander Hansen }
1742599b9af3SAlexander Hansen 
1743599b9af3SAlexander Hansen inline void requestRoutesJournalEventLogEntry(App& app)
1744599b9af3SAlexander Hansen {
1745599b9af3SAlexander Hansen     BMCWEB_ROUTE(
1746599b9af3SAlexander Hansen         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1747599b9af3SAlexander Hansen         .privileges(redfish::privileges::getLogEntry)
1748599b9af3SAlexander Hansen         .methods(boost::beast::http::verb::get)(std::bind_front(
1749599b9af3SAlexander Hansen             handleSystemsLogServiceEventLogEntriesGet, std::ref(app)));
1750599b9af3SAlexander Hansen }
1751599b9af3SAlexander Hansen 
1752599b9af3SAlexander Hansen inline void dBusEventLogEntryCollection(
1753599b9af3SAlexander Hansen     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1754599b9af3SAlexander Hansen {
1755599b9af3SAlexander Hansen     // Collections don't include the static data added by SubRoute
1756599b9af3SAlexander Hansen     // because it has a duplicate entry for members
1757599b9af3SAlexander Hansen     asyncResp->res.jsonValue["@odata.type"] =
1758599b9af3SAlexander Hansen         "#LogEntryCollection.LogEntryCollection";
1759599b9af3SAlexander Hansen     asyncResp->res.jsonValue["@odata.id"] =
1760599b9af3SAlexander Hansen         std::format("/redfish/v1/Systems/{}/LogServices/EventLog/Entries",
1761599b9af3SAlexander Hansen                     BMCWEB_REDFISH_SYSTEM_URI_NAME);
1762599b9af3SAlexander Hansen     asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1763599b9af3SAlexander Hansen     asyncResp->res.jsonValue["Description"] =
1764599b9af3SAlexander Hansen         "Collection of System Event Log Entries";
1765599b9af3SAlexander Hansen 
1766599b9af3SAlexander Hansen     // DBus implementation of EventLog/Entries
1767599b9af3SAlexander Hansen     // Make call to Logging Service to find all log entry objects
1768599b9af3SAlexander Hansen     sdbusplus::message::object_path path("/xyz/openbmc_project/logging");
1769599b9af3SAlexander Hansen     dbus::utility::getManagedObjects(
1770599b9af3SAlexander Hansen         "xyz.openbmc_project.Logging", path,
1771599b9af3SAlexander Hansen         [asyncResp](const boost::system::error_code& ec,
1772599b9af3SAlexander Hansen                     const dbus::utility::ManagedObjectType& resp) {
1773*b729096dSEd Tanous         afterLogEntriesGetManagedObjects(asyncResp, ec, resp);
17747e860f15SJohn Edward Broadbent     });
177508a4e4b5SAnthony Wilson }
177608a4e4b5SAnthony Wilson 
17777e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
177808a4e4b5SAnthony Wilson {
177922d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1780ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1781002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1782002d39b4SEd Tanous             [&app](const crow::Request& req,
178322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
178422d268cbSEd Tanous                    const std::string& systemName) {
17853ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
178645ca1b86SEd Tanous         {
178745ca1b86SEd Tanous             return;
178845ca1b86SEd Tanous         }
178925b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
17907f3e84a1SEd Tanous         {
17917f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
17927f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
17937f3e84a1SEd Tanous                                        systemName);
17947f3e84a1SEd Tanous             return;
17957f3e84a1SEd Tanous         }
1796253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
179722d268cbSEd Tanous         {
179822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
179922d268cbSEd Tanous                                        systemName);
180022d268cbSEd Tanous             return;
180122d268cbSEd Tanous         }
1802599b9af3SAlexander Hansen         dBusEventLogEntryCollection(asyncResp);
1803599b9af3SAlexander Hansen     });
1804599b9af3SAlexander Hansen }
180522d268cbSEd Tanous 
1806599b9af3SAlexander Hansen inline void
1807599b9af3SAlexander Hansen     dBusEventLogEntryGet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1808599b9af3SAlexander Hansen                          std::string& entryID)
1809599b9af3SAlexander Hansen {
1810599b9af3SAlexander Hansen     dbus::utility::escapePathForDbus(entryID);
181108a4e4b5SAnthony Wilson 
1812cb92c03bSAndrew Geissler     // DBus implementation of EventLog/Entries
1813cb92c03bSAndrew Geissler     // Make call to Logging Service to find all log entry objects
1814599b9af3SAlexander Hansen     sdbusplus::asio::getAllProperties(
1815599b9af3SAlexander Hansen         *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1816599b9af3SAlexander Hansen         "/xyz/openbmc_project/logging/entry/" + entryID, "",
1817599b9af3SAlexander Hansen         [asyncResp, entryID](const boost::system::error_code& ec,
1818599b9af3SAlexander Hansen                              const dbus::utility::DBusPropertiesMap& resp) {
1819599b9af3SAlexander Hansen         if (ec.value() == EBADR)
1820599b9af3SAlexander Hansen         {
1821599b9af3SAlexander Hansen             messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1822599b9af3SAlexander Hansen                                        entryID);
1823599b9af3SAlexander Hansen             return;
1824599b9af3SAlexander Hansen         }
1825cb92c03bSAndrew Geissler         if (ec)
1826cb92c03bSAndrew Geissler         {
1827599b9af3SAlexander Hansen             BMCWEB_LOG_ERROR("EventLogEntry (DBus) resp_handler got error {}",
1828599b9af3SAlexander Hansen                              ec);
1829cb92c03bSAndrew Geissler             messages::internalError(asyncResp->res);
1830cb92c03bSAndrew Geissler             return;
1831cb92c03bSAndrew Geissler         }
1832914e2d5dSEd Tanous         const uint32_t* id = nullptr;
1833c419c759SEd Tanous         const uint64_t* timestamp = nullptr;
1834c419c759SEd Tanous         const uint64_t* updateTimestamp = nullptr;
1835914e2d5dSEd Tanous         const std::string* severity = nullptr;
1836914e2d5dSEd Tanous         const std::string* message = nullptr;
1837914e2d5dSEd Tanous         const std::string* filePath = nullptr;
18389c11a172SVijay Lobo         const std::string* resolution = nullptr;
183975710de2SXiaochao Ma         bool resolved = false;
18409017faf2SAbhishek Patel         const std::string* notify = nullptr;
18419017faf2SAbhishek Patel 
1842599b9af3SAlexander Hansen         const bool success = sdbusplus::unpackPropertiesNoThrow(
1843599b9af3SAlexander Hansen             dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1844599b9af3SAlexander Hansen             timestamp, "UpdateTimestamp", updateTimestamp, "Severity", severity,
1845599b9af3SAlexander Hansen             "Message", message, "Resolved", resolved, "Resolution", resolution,
1846599b9af3SAlexander Hansen             "Path", filePath, "ServiceProviderNotify", notify);
1847599b9af3SAlexander Hansen 
1848599b9af3SAlexander Hansen         if (!success)
18497e860f15SJohn Edward Broadbent         {
1850002d39b4SEd Tanous             messages::internalError(asyncResp->res);
18517e860f15SJohn Edward Broadbent             return;
18527e860f15SJohn Edward Broadbent         }
1853599b9af3SAlexander Hansen 
1854599b9af3SAlexander Hansen         if (id == nullptr || message == nullptr || severity == nullptr ||
1855599b9af3SAlexander Hansen             timestamp == nullptr || updateTimestamp == nullptr ||
1856599b9af3SAlexander Hansen             notify == nullptr)
18579017faf2SAbhishek Patel         {
18589017faf2SAbhishek Patel             messages::internalError(asyncResp->res);
18599017faf2SAbhishek Patel             return;
18609017faf2SAbhishek Patel         }
1861599b9af3SAlexander Hansen 
1862599b9af3SAlexander Hansen         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1863599b9af3SAlexander Hansen         asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1864253f11b8SEd Tanous             "/redfish/v1/Systems/{}/LogServices/EventLog/Entries/{}",
1865253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME, std::to_string(*id));
1866599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1867599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1868599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Message"] = *message;
1869599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Resolved"] = resolved;
1870599b9af3SAlexander Hansen         std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
18719017faf2SAbhishek Patel         if (notifyAction)
18729017faf2SAbhishek Patel         {
1873599b9af3SAlexander Hansen             asyncResp->res.jsonValue["ServiceProviderNotified"] = *notifyAction;
18749017faf2SAbhishek Patel         }
1875599b9af3SAlexander Hansen         if ((resolution != nullptr) && (!(*resolution).empty()))
1876599b9af3SAlexander Hansen         {
1877599b9af3SAlexander Hansen             asyncResp->res.jsonValue["Resolution"] = *resolution;
1878b0b6152cSEd Tanous         }
1879599b9af3SAlexander Hansen         asyncResp->res.jsonValue["EntryType"] = "Event";
1880599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Severity"] =
18817e860f15SJohn Edward Broadbent             translateSeverityDbusToRedfish(*severity);
1882599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Created"] =
18832b82937eSEd Tanous             redfish::time_utils::getDateTimeUintMs(*timestamp);
1884599b9af3SAlexander Hansen         asyncResp->res.jsonValue["Modified"] =
18852b82937eSEd Tanous             redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
18867e860f15SJohn Edward Broadbent         if (filePath != nullptr)
18877e860f15SJohn Edward Broadbent         {
1888599b9af3SAlexander Hansen             asyncResp->res.jsonValue["AdditionalDataURI"] =
1889253f11b8SEd Tanous                 std::format(
1890253f11b8SEd Tanous                     "/redfish/v1/Systems/{}/LogServices/EventLog/Entries/",
1891253f11b8SEd Tanous                     BMCWEB_REDFISH_SYSTEM_URI_NAME) +
18927e860f15SJohn Edward Broadbent                 std::to_string(*id) + "/attachment";
18937e860f15SJohn Edward Broadbent         }
1894599b9af3SAlexander Hansen     });
18957e860f15SJohn Edward Broadbent }
1896599b9af3SAlexander Hansen 
1897599b9af3SAlexander Hansen inline void
1898599b9af3SAlexander Hansen     dBusEventLogEntryPatch(const crow::Request& req,
1899599b9af3SAlexander Hansen                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1900599b9af3SAlexander Hansen                            const std::string& entryId)
1901599b9af3SAlexander Hansen {
1902599b9af3SAlexander Hansen     std::optional<bool> resolved;
1903599b9af3SAlexander Hansen 
1904599b9af3SAlexander Hansen     if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved", resolved))
1905599b9af3SAlexander Hansen     {
1906599b9af3SAlexander Hansen         return;
1907599b9af3SAlexander Hansen     }
1908599b9af3SAlexander Hansen     BMCWEB_LOG_DEBUG("Set Resolved");
1909599b9af3SAlexander Hansen 
1910599b9af3SAlexander Hansen     setDbusProperty(asyncResp, "Resolved", "xyz.openbmc_project.Logging",
1911599b9af3SAlexander Hansen                     "/xyz/openbmc_project/logging/entry/" + entryId,
1912599b9af3SAlexander Hansen                     "xyz.openbmc_project.Logging.Entry", "Resolved",
1913599b9af3SAlexander Hansen                     resolved.value_or(false));
1914599b9af3SAlexander Hansen }
1915599b9af3SAlexander Hansen 
1916599b9af3SAlexander Hansen inline void
1917599b9af3SAlexander Hansen     dBusEventLogEntryDelete(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1918599b9af3SAlexander Hansen                             std::string entryID)
1919599b9af3SAlexander Hansen {
1920599b9af3SAlexander Hansen     BMCWEB_LOG_DEBUG("Do delete single event entries.");
1921599b9af3SAlexander Hansen 
1922599b9af3SAlexander Hansen     dbus::utility::escapePathForDbus(entryID);
1923599b9af3SAlexander Hansen 
1924599b9af3SAlexander Hansen     // Process response from Logging service.
1925599b9af3SAlexander Hansen     auto respHandler = [asyncResp,
1926599b9af3SAlexander Hansen                         entryID](const boost::system::error_code& ec) {
1927599b9af3SAlexander Hansen         BMCWEB_LOG_DEBUG("EventLogEntry (DBus) doDelete callback: Done");
1928599b9af3SAlexander Hansen         if (ec)
1929599b9af3SAlexander Hansen         {
1930599b9af3SAlexander Hansen             if (ec.value() == EBADR)
1931599b9af3SAlexander Hansen             {
1932599b9af3SAlexander Hansen                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
1933599b9af3SAlexander Hansen                 return;
1934599b9af3SAlexander Hansen             }
1935599b9af3SAlexander Hansen             // TODO Handle for specific error code
1936599b9af3SAlexander Hansen             BMCWEB_LOG_ERROR(
1937599b9af3SAlexander Hansen                 "EventLogEntry (DBus) doDelete respHandler got error {}", ec);
1938599b9af3SAlexander Hansen             asyncResp->res.result(
1939599b9af3SAlexander Hansen                 boost::beast::http::status::internal_server_error);
1940599b9af3SAlexander Hansen             return;
1941599b9af3SAlexander Hansen         }
1942599b9af3SAlexander Hansen 
1943599b9af3SAlexander Hansen         asyncResp->res.result(boost::beast::http::status::ok);
1944599b9af3SAlexander Hansen     };
1945599b9af3SAlexander Hansen 
1946599b9af3SAlexander Hansen     // Make call to Logging service to request Delete Log
1947599b9af3SAlexander Hansen     crow::connections::systemBus->async_method_call(
1948599b9af3SAlexander Hansen         respHandler, "xyz.openbmc_project.Logging",
1949599b9af3SAlexander Hansen         "/xyz/openbmc_project/logging/entry/" + entryID,
1950599b9af3SAlexander Hansen         "xyz.openbmc_project.Object.Delete", "Delete");
19517e860f15SJohn Edward Broadbent }
19527e860f15SJohn Edward Broadbent 
19537e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
19547e860f15SJohn Edward Broadbent {
19557e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
195622d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1957ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1958002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1959002d39b4SEd Tanous             [&app](const crow::Request& req,
19607e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
196122d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
19623ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
19637e860f15SJohn Edward Broadbent         {
196445ca1b86SEd Tanous             return;
196545ca1b86SEd Tanous         }
196625b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
19677f3e84a1SEd Tanous         {
19687f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
19697f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
19707f3e84a1SEd Tanous                                        systemName);
19717f3e84a1SEd Tanous             return;
19727f3e84a1SEd Tanous         }
1973253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
197422d268cbSEd Tanous         {
197522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
197622d268cbSEd Tanous                                        systemName);
197722d268cbSEd Tanous             return;
197822d268cbSEd Tanous         }
197922d268cbSEd Tanous 
19807e860f15SJohn Edward Broadbent         std::string entryID = param;
19817e860f15SJohn Edward Broadbent 
1982599b9af3SAlexander Hansen         dBusEventLogEntryGet(asyncResp, entryID);
19837e860f15SJohn Edward Broadbent     });
1984336e96c6SChicago Duan 
19857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
198622d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1987ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
19887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
198945ca1b86SEd Tanous             [&app](const crow::Request& req,
19907e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
199122d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
19923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
199345ca1b86SEd Tanous         {
199445ca1b86SEd Tanous             return;
199545ca1b86SEd Tanous         }
199625b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
19977f3e84a1SEd Tanous         {
19987f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
19997f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
20007f3e84a1SEd Tanous                                        systemName);
20017f3e84a1SEd Tanous             return;
20027f3e84a1SEd Tanous         }
2003253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
200422d268cbSEd Tanous         {
200522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
200622d268cbSEd Tanous                                        systemName);
200722d268cbSEd Tanous             return;
200822d268cbSEd Tanous         }
200975710de2SXiaochao Ma 
2010599b9af3SAlexander Hansen         dBusEventLogEntryPatch(req, asyncResp, entryId);
20117e860f15SJohn Edward Broadbent     });
201275710de2SXiaochao Ma 
20137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
201422d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
2015ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2016ed398213SEd Tanous 
2017002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
2018002d39b4SEd Tanous             [&app](const crow::Request& req,
2019002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
202022d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
20213ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2022336e96c6SChicago Duan         {
202345ca1b86SEd Tanous             return;
202445ca1b86SEd Tanous         }
202525b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
20267f3e84a1SEd Tanous         {
20277f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
20287f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
20297f3e84a1SEd Tanous                                        systemName);
20307f3e84a1SEd Tanous             return;
20317f3e84a1SEd Tanous         }
2032253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
203322d268cbSEd Tanous         {
203422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
203522d268cbSEd Tanous                                        systemName);
203622d268cbSEd Tanous             return;
203722d268cbSEd Tanous         }
2038599b9af3SAlexander Hansen         dBusEventLogEntryDelete(asyncResp, param);
20397e860f15SJohn Edward Broadbent     });
2040400fd1fbSAdriana Kobylak }
2041400fd1fbSAdriana Kobylak 
2042b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
2043b7028ebfSSpencer Ku 
2044b7028ebfSSpencer Ku inline bool
2045b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
2046b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
2047b7028ebfSSpencer Ku {
2048b7028ebfSSpencer Ku     std::error_code ec;
2049b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
2050b7028ebfSSpencer Ku     if (ec)
2051b7028ebfSSpencer Ku     {
2052bf2ddedeSCarson Labrado         BMCWEB_LOG_WARNING("{}", ec.message());
2053b7028ebfSSpencer Ku         return false;
2054b7028ebfSSpencer Ku     }
2055b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
2056b7028ebfSSpencer Ku     {
2057b7028ebfSSpencer Ku         std::string filename = it.path().filename();
2058b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
2059b7028ebfSSpencer Ku         // path
206011ba3979SEd Tanous         if (filename.starts_with("log"))
2061b7028ebfSSpencer Ku         {
2062b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
2063b7028ebfSSpencer Ku         }
2064b7028ebfSSpencer Ku     }
2065b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
2066b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
2067b7028ebfSSpencer Ku     // descending order.
2068b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2069b7028ebfSSpencer Ku               AlphanumLess<std::string>());
2070b7028ebfSSpencer Ku 
2071b7028ebfSSpencer Ku     return true;
2072b7028ebfSSpencer Ku }
2073b7028ebfSSpencer Ku 
207402cad96eSEd Tanous inline bool getHostLoggerEntries(
207502cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
207602cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
2077b7028ebfSSpencer Ku {
2078b7028ebfSSpencer Ku     GzFileReader logFile;
2079b7028ebfSSpencer Ku 
2080b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
2081b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
2082b7028ebfSSpencer Ku     {
2083b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2084b7028ebfSSpencer Ku         {
208562598e31SEd Tanous             BMCWEB_LOG_ERROR("fail to expose host logs");
2086b7028ebfSSpencer Ku             return false;
2087b7028ebfSSpencer Ku         }
2088b7028ebfSSpencer Ku     }
2089b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2090b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2091b7028ebfSSpencer Ku     if (!lastMessage.empty())
2092b7028ebfSSpencer Ku     {
2093b7028ebfSSpencer Ku         logCount++;
2094b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2095b7028ebfSSpencer Ku         {
2096b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2097b7028ebfSSpencer Ku         }
2098b7028ebfSSpencer Ku     }
2099b7028ebfSSpencer Ku     return true;
2100b7028ebfSSpencer Ku }
2101b7028ebfSSpencer Ku 
21026f056f24SEd Tanous inline void fillHostLoggerEntryJson(std::string_view logEntryID,
21036f056f24SEd Tanous                                     std::string_view msg,
21046d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2105b7028ebfSSpencer Ku {
2106b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
21079c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2108ef4c65b7SEd Tanous     logEntryJson["@odata.id"] = boost::urls::format(
2109253f11b8SEd Tanous         "/redfish/v1/Systems/{}/LogServices/HostLogger/Entries/{}",
2110253f11b8SEd Tanous         BMCWEB_REDFISH_SYSTEM_URI_NAME, logEntryID);
21116d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
21126d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
21136d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
2114539d8c6bSEd Tanous     logEntryJson["EntryType"] = log_entry::LogEntryType::Oem;
2115539d8c6bSEd Tanous     logEntryJson["Severity"] = log_entry::EventSeverity::OK;
21166d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2117b7028ebfSSpencer Ku }
2118b7028ebfSSpencer Ku 
2119b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2120b7028ebfSSpencer Ku {
212122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2122b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
21231476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21241476687dSEd Tanous             [&app](const crow::Request& req,
212522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
212622d268cbSEd Tanous                    const std::string& systemName) {
21273ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
212845ca1b86SEd Tanous         {
212945ca1b86SEd Tanous             return;
213045ca1b86SEd Tanous         }
213125b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
21327f3e84a1SEd Tanous         {
21337f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
21347f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
21357f3e84a1SEd Tanous                                        systemName);
21367f3e84a1SEd Tanous             return;
21377f3e84a1SEd Tanous         }
2138253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
213922d268cbSEd Tanous         {
214022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
214122d268cbSEd Tanous                                        systemName);
214222d268cbSEd Tanous             return;
214322d268cbSEd Tanous         }
2144b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2145253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/HostLogger",
2146253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
2147b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2148b25644a1SJanet Adkins             "#LogService.v1_2_0.LogService";
2149b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2150b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2151b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
21521476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2153253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/HostLogger/Entries",
2154253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
2155b7028ebfSSpencer Ku     });
2156b7028ebfSSpencer Ku }
2157b7028ebfSSpencer Ku 
2158b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2159b7028ebfSSpencer Ku {
2160b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
216122d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2162b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2163002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2164002d39b4SEd Tanous             [&app](const crow::Request& req,
216522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
216622d268cbSEd Tanous                    const std::string& systemName) {
2167c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2168c937d2bfSEd Tanous             .canDelegateTop = true,
2169c937d2bfSEd Tanous             .canDelegateSkip = true,
2170c937d2bfSEd Tanous         };
2171c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2172c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21733ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2174b7028ebfSSpencer Ku         {
2175b7028ebfSSpencer Ku             return;
2176b7028ebfSSpencer Ku         }
217725b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
21787f3e84a1SEd Tanous         {
21797f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
21807f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
21817f3e84a1SEd Tanous                                        systemName);
21827f3e84a1SEd Tanous             return;
21837f3e84a1SEd Tanous         }
2184253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
218522d268cbSEd Tanous         {
218622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
218722d268cbSEd Tanous                                        systemName);
218822d268cbSEd Tanous             return;
218922d268cbSEd Tanous         }
2190b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2191253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/HostLogger/Entries",
2192253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
2193b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2194b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2195b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2196b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2197b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
21980fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2199b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2200b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2201b7028ebfSSpencer Ku 
2202b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2203b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2204b7028ebfSSpencer Ku         {
2205bf2ddedeSCarson Labrado             BMCWEB_LOG_DEBUG("Failed to get host log file path");
2206b7028ebfSSpencer Ku             return;
2207b7028ebfSSpencer Ku         }
22083648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
22093648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
22105143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2211b7028ebfSSpencer Ku         size_t logCount = 0;
2212b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2213b7028ebfSSpencer Ku         // control by skip and top.
2214b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
22153648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
22163648c8beSEd Tanous                                   logCount))
2217b7028ebfSSpencer Ku         {
2218b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2219b7028ebfSSpencer Ku             return;
2220b7028ebfSSpencer Ku         }
2221b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2222b7028ebfSSpencer Ku         // log count
222326f6976fSEd Tanous         if (logEntries.empty())
2224b7028ebfSSpencer Ku         {
2225b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2226b7028ebfSSpencer Ku             return;
2227b7028ebfSSpencer Ku         }
222826f6976fSEd Tanous         if (!logEntries.empty())
2229b7028ebfSSpencer Ku         {
2230b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2231b7028ebfSSpencer Ku             {
22326d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
22333648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
22343648c8beSEd Tanous                                         hostLogEntry);
2235b2ba3072SPatrick Williams                 logEntryArray.emplace_back(std::move(hostLogEntry));
2236b7028ebfSSpencer Ku             }
2237b7028ebfSSpencer Ku 
2238b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
22393648c8beSEd Tanous             if (skip + top < logCount)
2240b7028ebfSSpencer Ku             {
2241b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
2242253f11b8SEd Tanous                     std::format(
2243253f11b8SEd Tanous                         "/redfish/v1/Systems/{}/LogServices/HostLogger/Entries?$skip=",
2244253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME) +
22453648c8beSEd Tanous                     std::to_string(skip + top);
2246b7028ebfSSpencer Ku             }
2247b7028ebfSSpencer Ku         }
2248b7028ebfSSpencer Ku     });
2249b7028ebfSSpencer Ku }
2250b7028ebfSSpencer Ku 
2251b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2252b7028ebfSSpencer Ku {
2253b7028ebfSSpencer Ku     BMCWEB_ROUTE(
225422d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2255b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2256b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
225745ca1b86SEd Tanous             [&app](const crow::Request& req,
2258b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
225922d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
22603ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
226145ca1b86SEd Tanous         {
226245ca1b86SEd Tanous             return;
226345ca1b86SEd Tanous         }
226425b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
22657f3e84a1SEd Tanous         {
22667f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
22677f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
22687f3e84a1SEd Tanous                                        systemName);
22697f3e84a1SEd Tanous             return;
22707f3e84a1SEd Tanous         }
2271253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
227222d268cbSEd Tanous         {
227322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
227422d268cbSEd Tanous                                        systemName);
227522d268cbSEd Tanous             return;
227622d268cbSEd Tanous         }
22776f056f24SEd Tanous         std::string_view targetID = param;
2278b7028ebfSSpencer Ku 
2279b7028ebfSSpencer Ku         uint64_t idInt = 0;
2280ca45aa3cSEd Tanous 
22816f056f24SEd Tanous         auto [ptr, ec] = std::from_chars(targetID.begin(), targetID.end(),
228284396af9SPatrick Williams                                          idInt);
22836f056f24SEd Tanous         if (ec != std::errc{} || ptr != targetID.end())
2284b7028ebfSSpencer Ku         {
22859db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2286b7028ebfSSpencer Ku             return;
2287b7028ebfSSpencer Ku         }
2288b7028ebfSSpencer Ku 
2289b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2290b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2291b7028ebfSSpencer Ku         {
2292bf2ddedeSCarson Labrado             BMCWEB_LOG_DEBUG("Failed to get host log file path");
2293b7028ebfSSpencer Ku             return;
2294b7028ebfSSpencer Ku         }
2295b7028ebfSSpencer Ku 
2296b7028ebfSSpencer Ku         size_t logCount = 0;
22973648c8beSEd Tanous         size_t top = 1;
2298b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2299b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2300b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2301b7028ebfSSpencer Ku         // get that entry
2302002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2303002d39b4SEd Tanous                                   logCount))
2304b7028ebfSSpencer Ku         {
2305b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2306b7028ebfSSpencer Ku             return;
2307b7028ebfSSpencer Ku         }
2308b7028ebfSSpencer Ku 
2309b7028ebfSSpencer Ku         if (!logEntries.empty())
2310b7028ebfSSpencer Ku         {
23116d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
23126d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
23136d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2314b7028ebfSSpencer Ku             return;
2315b7028ebfSSpencer Ku         }
2316b7028ebfSSpencer Ku 
2317b7028ebfSSpencer Ku         // Requested ID was not found
23189db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2319b7028ebfSSpencer Ku     });
2320b7028ebfSSpencer Ku }
2321b7028ebfSSpencer Ku 
2322dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2323fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2324253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2325253f11b8SEd Tanous     const std::string& managerId)
23261da66f75SEd Tanous {
23273ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
232845ca1b86SEd Tanous     {
232945ca1b86SEd Tanous         return;
233045ca1b86SEd Tanous     }
2331253f11b8SEd Tanous 
2332253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2333253f11b8SEd Tanous     {
2334253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2335253f11b8SEd Tanous         return;
2336253f11b8SEd Tanous     }
2337253f11b8SEd Tanous 
23387e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
23397e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2340e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
23411da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2342253f11b8SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2343253f11b8SEd Tanous         "/redfish/v1/Managers/{}/LogServices", BMCWEB_REDFISH_MANAGER_URI_NAME);
2344002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2345e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
23461da66f75SEd Tanous         "Collection of LogServices for this Manager";
2347002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2348c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2349fdd26906SClaire Weinan 
235025b54dbaSEd Tanous     if constexpr (BMCWEB_REDFISH_BMC_JOURNAL)
235125b54dbaSEd Tanous     {
2352613dabeaSEd Tanous         nlohmann::json::object_t journal;
2353253f11b8SEd Tanous         journal["@odata.id"] =
2354253f11b8SEd Tanous             boost::urls::format("/redfish/v1/Managers/{}/LogServices/Journal",
2355253f11b8SEd Tanous                                 BMCWEB_REDFISH_MANAGER_URI_NAME);
2356b2ba3072SPatrick Williams         logServiceArray.emplace_back(std::move(journal));
235725b54dbaSEd Tanous     }
2358fdd26906SClaire Weinan 
2359fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2360fdd26906SClaire Weinan 
236125b54dbaSEd Tanous     if constexpr (BMCWEB_REDFISH_DUMP_LOG)
236225b54dbaSEd Tanous     {
236315912159SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
23647a1dbc48SGeorge Liu             "xyz.openbmc_project.Collection.DeleteAll"};
23657a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
23667a1dbc48SGeorge Liu             "/xyz/openbmc_project/dump", 0, interfaces,
236725b54dbaSEd Tanous             [asyncResp](const boost::system::error_code& ec,
236825b54dbaSEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
236925b54dbaSEd Tanous                             subTreePaths) {
2370fdd26906SClaire Weinan             if (ec)
2371fdd26906SClaire Weinan             {
237262598e31SEd Tanous                 BMCWEB_LOG_ERROR(
237362598e31SEd Tanous                     "handleBMCLogServicesCollectionGet respHandler got error {}",
237462598e31SEd Tanous                     ec);
2375fdd26906SClaire Weinan                 // Assume that getting an error simply means there are no dump
2376fdd26906SClaire Weinan                 // LogServices. Return without adding any error response.
2377fdd26906SClaire Weinan                 return;
2378fdd26906SClaire Weinan             }
2379fdd26906SClaire Weinan 
2380fdd26906SClaire Weinan             nlohmann::json& logServiceArrayLocal =
2381fdd26906SClaire Weinan                 asyncResp->res.jsonValue["Members"];
2382fdd26906SClaire Weinan 
2383fdd26906SClaire Weinan             for (const std::string& path : subTreePaths)
2384fdd26906SClaire Weinan             {
2385fdd26906SClaire Weinan                 if (path == "/xyz/openbmc_project/dump/bmc")
2386fdd26906SClaire Weinan                 {
2387613dabeaSEd Tanous                     nlohmann::json::object_t member;
2388253f11b8SEd Tanous                     member["@odata.id"] = boost::urls::format(
2389253f11b8SEd Tanous                         "/redfish/v1/Managers/{}/LogServices/Dump",
2390253f11b8SEd Tanous                         BMCWEB_REDFISH_MANAGER_URI_NAME);
2391b2ba3072SPatrick Williams                     logServiceArrayLocal.emplace_back(std::move(member));
2392fdd26906SClaire Weinan                 }
2393fdd26906SClaire Weinan                 else if (path == "/xyz/openbmc_project/dump/faultlog")
2394fdd26906SClaire Weinan                 {
2395613dabeaSEd Tanous                     nlohmann::json::object_t member;
2396253f11b8SEd Tanous                     member["@odata.id"] = boost::urls::format(
2397253f11b8SEd Tanous                         "/redfish/v1/Managers/{}/LogServices/FaultLog",
2398253f11b8SEd Tanous                         BMCWEB_REDFISH_MANAGER_URI_NAME);
2399b2ba3072SPatrick Williams                     logServiceArrayLocal.emplace_back(std::move(member));
2400fdd26906SClaire Weinan                 }
2401fdd26906SClaire Weinan             }
2402fdd26906SClaire Weinan 
2403e1f26343SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
2404fdd26906SClaire Weinan                 logServiceArrayLocal.size();
24057a1dbc48SGeorge Liu         });
240625b54dbaSEd Tanous     }
2407fdd26906SClaire Weinan }
2408fdd26906SClaire Weinan 
2409fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2410fdd26906SClaire Weinan {
2411253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/LogServices/")
2412fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2413fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2414dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2415e1f26343SJason M. Bills }
2416e1f26343SJason M. Bills 
2417fdd26906SClaire Weinan inline void
2418fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2419fdd26906SClaire Weinan                        const std::string& dumpType)
2420c9bb6861Sraviteja-b {
2421fdd26906SClaire Weinan     std::string dumpPath;
2422539d8c6bSEd Tanous     log_service::OverWritePolicy overWritePolicy =
2423539d8c6bSEd Tanous         log_service::OverWritePolicy::Invalid;
2424fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2425fdd26906SClaire Weinan 
2426fdd26906SClaire Weinan     if (dumpType == "BMC")
242745ca1b86SEd Tanous     {
2428253f11b8SEd Tanous         dumpPath = std::format("/redfish/v1/Managers/{}/LogServices/Dump",
2429253f11b8SEd Tanous                                BMCWEB_REDFISH_MANAGER_URI_NAME);
2430539d8c6bSEd Tanous         overWritePolicy = log_service::OverWritePolicy::WrapsWhenFull;
2431fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2432fdd26906SClaire Weinan     }
2433fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2434fdd26906SClaire Weinan     {
2435253f11b8SEd Tanous         dumpPath = std::format("/redfish/v1/Managers/{}/LogServices/FaultLog",
2436253f11b8SEd Tanous                                BMCWEB_REDFISH_MANAGER_URI_NAME);
2437539d8c6bSEd Tanous         overWritePolicy = log_service::OverWritePolicy::Unknown;
2438fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2439fdd26906SClaire Weinan     }
2440fdd26906SClaire Weinan     else if (dumpType == "System")
2441fdd26906SClaire Weinan     {
2442253f11b8SEd Tanous         dumpPath = std::format("/redfish/v1/Systems/{}/LogServices/Dump",
2443253f11b8SEd Tanous                                BMCWEB_REDFISH_SYSTEM_URI_NAME);
2444539d8c6bSEd Tanous         overWritePolicy = log_service::OverWritePolicy::WrapsWhenFull;
2445fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2446fdd26906SClaire Weinan     }
2447fdd26906SClaire Weinan     else
2448fdd26906SClaire Weinan     {
244962598e31SEd Tanous         BMCWEB_LOG_ERROR("getDumpServiceInfo() invalid dump type: {}",
245062598e31SEd Tanous                          dumpType);
2451fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
245245ca1b86SEd Tanous         return;
245345ca1b86SEd Tanous     }
2454fdd26906SClaire Weinan 
2455fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2456fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2457c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2458fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2459fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2460539d8c6bSEd Tanous     asyncResp->res.jsonValue["OverWritePolicy"] = overWritePolicy;
24617c8c4058STejas Patil 
24627c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
24632b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
24640fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
24657c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24667c8c4058STejas Patil         redfishDateTimeOffset.second;
24677c8c4058STejas Patil 
2468fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2469fdd26906SClaire Weinan 
2470fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2471fdd26906SClaire Weinan     {
2472002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
24731476687dSEd Tanous                                 ["target"] =
2474fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2475fdd26906SClaire Weinan     }
24760d946211SClaire Weinan 
24770d946211SClaire Weinan     constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
24780d946211SClaire Weinan     dbus::utility::getSubTreePaths(
24790d946211SClaire Weinan         "/xyz/openbmc_project/dump", 0, interfaces,
24800d946211SClaire Weinan         [asyncResp, dumpType, dumpPath](
24810d946211SClaire Weinan             const boost::system::error_code& ec,
24820d946211SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
24830d946211SClaire Weinan         if (ec)
24840d946211SClaire Weinan         {
248562598e31SEd Tanous             BMCWEB_LOG_ERROR("getDumpServiceInfo respHandler got error {}", ec);
24860d946211SClaire Weinan             // Assume that getting an error simply means there are no dump
24870d946211SClaire Weinan             // LogServices. Return without adding any error response.
24880d946211SClaire Weinan             return;
24890d946211SClaire Weinan         }
249018f8f608SEd Tanous         std::string dbusDumpPath = getDumpPath(dumpType);
24910d946211SClaire Weinan         for (const std::string& path : subTreePaths)
24920d946211SClaire Weinan         {
24930d946211SClaire Weinan             if (path == dbusDumpPath)
24940d946211SClaire Weinan             {
24950d946211SClaire Weinan                 asyncResp->res
24960d946211SClaire Weinan                     .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
24970d946211SClaire Weinan                     dumpPath + "/Actions/LogService.ClearLog";
24980d946211SClaire Weinan                 break;
24990d946211SClaire Weinan             }
25000d946211SClaire Weinan         }
25010d946211SClaire Weinan     });
2502c9bb6861Sraviteja-b }
2503c9bb6861Sraviteja-b 
2504fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2505fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2506253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2507253f11b8SEd Tanous     const std::string& managerId)
25087e860f15SJohn Edward Broadbent {
25093ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
251045ca1b86SEd Tanous     {
251145ca1b86SEd Tanous         return;
251245ca1b86SEd Tanous     }
2513253f11b8SEd Tanous 
2514253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2515253f11b8SEd Tanous     {
2516253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2517253f11b8SEd Tanous         return;
2518253f11b8SEd Tanous     }
2519253f11b8SEd Tanous 
2520fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2521fdd26906SClaire Weinan }
2522c9bb6861Sraviteja-b 
252322d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
252422d268cbSEd Tanous     crow::App& app, const crow::Request& req,
252522d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
252622d268cbSEd Tanous     const std::string& chassisId)
252722d268cbSEd Tanous {
252822d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
252922d268cbSEd Tanous     {
253022d268cbSEd Tanous         return;
253122d268cbSEd Tanous     }
2532253f11b8SEd Tanous     if (chassisId != BMCWEB_REDFISH_SYSTEM_URI_NAME)
253322d268cbSEd Tanous     {
253422d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
253522d268cbSEd Tanous         return;
253622d268cbSEd Tanous     }
253722d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
253822d268cbSEd Tanous }
253922d268cbSEd Tanous 
2540fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2541fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2542253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2543253f11b8SEd Tanous     const std::string& managerId)
2544fdd26906SClaire Weinan {
2545fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2546fdd26906SClaire Weinan     {
2547fdd26906SClaire Weinan         return;
2548fdd26906SClaire Weinan     }
2549253f11b8SEd Tanous 
2550253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2551253f11b8SEd Tanous     {
2552253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2553253f11b8SEd Tanous         return;
2554253f11b8SEd Tanous     }
2555fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2556fdd26906SClaire Weinan }
2557fdd26906SClaire Weinan 
255822d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
255922d268cbSEd Tanous     crow::App& app, const crow::Request& req,
256022d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
256122d268cbSEd Tanous     const std::string& chassisId)
256222d268cbSEd Tanous {
256322d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
256422d268cbSEd Tanous     {
256522d268cbSEd Tanous         return;
256622d268cbSEd Tanous     }
2567253f11b8SEd Tanous     if (chassisId != BMCWEB_REDFISH_SYSTEM_URI_NAME)
256822d268cbSEd Tanous     {
256922d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
257022d268cbSEd Tanous         return;
257122d268cbSEd Tanous     }
257222d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
257322d268cbSEd Tanous }
257422d268cbSEd Tanous 
2575fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2576fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2577fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2578253f11b8SEd Tanous     const std::string& managerId, const std::string& dumpId)
2579fdd26906SClaire Weinan {
2580fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2581fdd26906SClaire Weinan     {
2582fdd26906SClaire Weinan         return;
2583fdd26906SClaire Weinan     }
2584253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2585253f11b8SEd Tanous     {
2586253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2587253f11b8SEd Tanous         return;
2588253f11b8SEd Tanous     }
2589fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2590fdd26906SClaire Weinan }
2591168d1b1aSCarson Labrado 
259222d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
259322d268cbSEd Tanous     crow::App& app, const crow::Request& req,
259422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
259522d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
259622d268cbSEd Tanous {
259722d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
259822d268cbSEd Tanous     {
259922d268cbSEd Tanous         return;
260022d268cbSEd Tanous     }
2601253f11b8SEd Tanous     if (chassisId != BMCWEB_REDFISH_SYSTEM_URI_NAME)
260222d268cbSEd Tanous     {
260322d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
260422d268cbSEd Tanous         return;
260522d268cbSEd Tanous     }
260622d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
260722d268cbSEd Tanous }
2608fdd26906SClaire Weinan 
2609fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2610fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2611fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2612253f11b8SEd Tanous     const std::string& managerId, const std::string& dumpId)
2613fdd26906SClaire Weinan {
2614fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2615fdd26906SClaire Weinan     {
2616fdd26906SClaire Weinan         return;
2617fdd26906SClaire Weinan     }
2618253f11b8SEd Tanous 
2619253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2620253f11b8SEd Tanous     {
2621253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2622253f11b8SEd Tanous         return;
2623253f11b8SEd Tanous     }
2624fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2625fdd26906SClaire Weinan }
2626fdd26906SClaire Weinan 
262722d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
262822d268cbSEd Tanous     crow::App& app, const crow::Request& req,
262922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
263022d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
263122d268cbSEd Tanous {
263222d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
263322d268cbSEd Tanous     {
263422d268cbSEd Tanous         return;
263522d268cbSEd Tanous     }
2636253f11b8SEd Tanous     if (chassisId != BMCWEB_REDFISH_SYSTEM_URI_NAME)
263722d268cbSEd Tanous     {
263822d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
263922d268cbSEd Tanous         return;
264022d268cbSEd Tanous     }
264122d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
264222d268cbSEd Tanous }
264322d268cbSEd Tanous 
2644168d1b1aSCarson Labrado inline void handleLogServicesDumpEntryDownloadGet(
2645168d1b1aSCarson Labrado     crow::App& app, const std::string& dumpType, const crow::Request& req,
2646168d1b1aSCarson Labrado     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2647253f11b8SEd Tanous     const std::string& managerId, const std::string& dumpId)
2648168d1b1aSCarson Labrado {
2649168d1b1aSCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2650168d1b1aSCarson Labrado     {
2651168d1b1aSCarson Labrado         return;
2652168d1b1aSCarson Labrado     }
2653253f11b8SEd Tanous 
2654253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2655253f11b8SEd Tanous     {
2656253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2657253f11b8SEd Tanous         return;
2658253f11b8SEd Tanous     }
2659168d1b1aSCarson Labrado     downloadDumpEntry(asyncResp, dumpId, dumpType);
2660168d1b1aSCarson Labrado }
2661168d1b1aSCarson Labrado 
2662168d1b1aSCarson Labrado inline void handleDBusEventLogEntryDownloadGet(
2663168d1b1aSCarson Labrado     crow::App& app, const std::string& dumpType, const crow::Request& req,
2664168d1b1aSCarson Labrado     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2665168d1b1aSCarson Labrado     const std::string& systemName, const std::string& entryID)
2666168d1b1aSCarson Labrado {
2667168d1b1aSCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2668168d1b1aSCarson Labrado     {
2669168d1b1aSCarson Labrado         return;
2670168d1b1aSCarson Labrado     }
2671168d1b1aSCarson Labrado     if (!http_helpers::isContentTypeAllowed(
2672168d1b1aSCarson Labrado             req.getHeaderValue("Accept"),
2673168d1b1aSCarson Labrado             http_helpers::ContentType::OctetStream, true))
2674168d1b1aSCarson Labrado     {
2675168d1b1aSCarson Labrado         asyncResp->res.result(boost::beast::http::status::bad_request);
2676168d1b1aSCarson Labrado         return;
2677168d1b1aSCarson Labrado     }
2678168d1b1aSCarson Labrado     downloadEventLogEntry(asyncResp, systemName, entryID, dumpType);
2679168d1b1aSCarson Labrado }
2680168d1b1aSCarson Labrado 
2681fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2682fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2683253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2684253f11b8SEd Tanous     const std::string& managerId)
2685fdd26906SClaire Weinan {
2686fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2687fdd26906SClaire Weinan     {
2688fdd26906SClaire Weinan         return;
2689fdd26906SClaire Weinan     }
2690253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2691253f11b8SEd Tanous     {
2692253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2693253f11b8SEd Tanous         return;
2694253f11b8SEd Tanous     }
2695253f11b8SEd Tanous 
2696fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2697fdd26906SClaire Weinan }
2698fdd26906SClaire Weinan 
269922d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
270022d268cbSEd Tanous     crow::App& app, const crow::Request& req,
270122d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27027f3e84a1SEd Tanous     const std::string& systemName)
270322d268cbSEd Tanous {
270422d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
270522d268cbSEd Tanous     {
270622d268cbSEd Tanous         return;
270722d268cbSEd Tanous     }
27087f3e84a1SEd Tanous 
270925b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
271022d268cbSEd Tanous     {
27117f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
27127f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
27137f3e84a1SEd Tanous                                    systemName);
27147f3e84a1SEd Tanous         return;
27157f3e84a1SEd Tanous     }
2716253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
27177f3e84a1SEd Tanous     {
27187f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
27197f3e84a1SEd Tanous                                    systemName);
272022d268cbSEd Tanous         return;
272122d268cbSEd Tanous     }
272222d268cbSEd Tanous     createDump(asyncResp, req, "System");
272322d268cbSEd Tanous }
272422d268cbSEd Tanous 
2725fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2726fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2727253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2728253f11b8SEd Tanous     const std::string& managerId)
2729fdd26906SClaire Weinan {
2730fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2731fdd26906SClaire Weinan     {
2732fdd26906SClaire Weinan         return;
2733fdd26906SClaire Weinan     }
2734253f11b8SEd Tanous 
2735253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
2736253f11b8SEd Tanous     {
2737253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
2738253f11b8SEd Tanous         return;
2739253f11b8SEd Tanous     }
2740fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2741fdd26906SClaire Weinan }
2742fdd26906SClaire Weinan 
274322d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
274422d268cbSEd Tanous     crow::App& app, const crow::Request& req,
274522d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27467f3e84a1SEd Tanous     const std::string& systemName)
274722d268cbSEd Tanous {
274822d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
274922d268cbSEd Tanous     {
275022d268cbSEd Tanous         return;
275122d268cbSEd Tanous     }
275225b54dbaSEd Tanous     if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
275322d268cbSEd Tanous     {
27547f3e84a1SEd Tanous         // Option currently returns no systems.  TBD
27557f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
27567f3e84a1SEd Tanous                                    systemName);
27577f3e84a1SEd Tanous         return;
27587f3e84a1SEd Tanous     }
2759253f11b8SEd Tanous     if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
27607f3e84a1SEd Tanous     {
27617f3e84a1SEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem",
27627f3e84a1SEd Tanous                                    systemName);
276322d268cbSEd Tanous         return;
276422d268cbSEd Tanous     }
276522d268cbSEd Tanous     clearDump(asyncResp, "System");
276622d268cbSEd Tanous }
276722d268cbSEd Tanous 
2768fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2769fdd26906SClaire Weinan {
2770253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/LogServices/Dump/")
2771fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2772fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2773fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2774fdd26906SClaire Weinan }
2775fdd26906SClaire Weinan 
2776fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2777fdd26906SClaire Weinan {
2778253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/LogServices/Dump/Entries/")
2779fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2780fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2781fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2782c9bb6861Sraviteja-b }
2783c9bb6861Sraviteja-b 
27847e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2785c9bb6861Sraviteja-b {
27867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2787253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/LogServices/Dump/Entries/<str>/")
2788ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2789fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2790fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2791fdd26906SClaire Weinan 
27927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2793253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/LogServices/Dump/Entries/<str>/")
2794ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2795fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2796fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2797c9bb6861Sraviteja-b }
2798c9bb6861Sraviteja-b 
2799168d1b1aSCarson Labrado inline void requestRoutesBMCDumpEntryDownload(App& app)
2800168d1b1aSCarson Labrado {
2801168d1b1aSCarson Labrado     BMCWEB_ROUTE(
2802168d1b1aSCarson Labrado         app,
2803253f11b8SEd Tanous         "/redfish/v1/Managers/<str>/LogServices/Dump/Entries/<str>/attachment/")
2804168d1b1aSCarson Labrado         .privileges(redfish::privileges::getLogEntry)
2805168d1b1aSCarson Labrado         .methods(boost::beast::http::verb::get)(std::bind_front(
2806168d1b1aSCarson Labrado             handleLogServicesDumpEntryDownloadGet, std::ref(app), "BMC"));
2807168d1b1aSCarson Labrado }
2808168d1b1aSCarson Labrado 
28097e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2810c9bb6861Sraviteja-b {
28110fda0f12SGeorge Liu     BMCWEB_ROUTE(
28120fda0f12SGeorge Liu         app,
2813253f11b8SEd Tanous         "/redfish/v1/Managers/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2814ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
28157e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2816fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2817fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2818a43be80fSAsmitha Karunanithi }
2819a43be80fSAsmitha Karunanithi 
28207e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
282180319af1SAsmitha Karunanithi {
28220fda0f12SGeorge Liu     BMCWEB_ROUTE(
28230fda0f12SGeorge Liu         app,
2824253f11b8SEd Tanous         "/redfish/v1/Managers/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2825ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2826fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2827fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
282845ca1b86SEd Tanous }
2829fdd26906SClaire Weinan 
2830168d1b1aSCarson Labrado inline void requestRoutesDBusEventLogEntryDownload(App& app)
2831168d1b1aSCarson Labrado {
2832168d1b1aSCarson Labrado     BMCWEB_ROUTE(
2833168d1b1aSCarson Labrado         app,
28349e9d99daSRavi Teja         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment/")
2835168d1b1aSCarson Labrado         .privileges(redfish::privileges::getLogEntry)
2836168d1b1aSCarson Labrado         .methods(boost::beast::http::verb::get)(std::bind_front(
2837168d1b1aSCarson Labrado             handleDBusEventLogEntryDownloadGet, std::ref(app), "System"));
2838168d1b1aSCarson Labrado }
2839168d1b1aSCarson Labrado 
2840fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2841fdd26906SClaire Weinan {
2842253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/LogServices/FaultLog/")
2843fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2844fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2845fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2846fdd26906SClaire Weinan }
2847fdd26906SClaire Weinan 
2848fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2849fdd26906SClaire Weinan {
2850253f11b8SEd Tanous     BMCWEB_ROUTE(app,
2851253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/LogServices/FaultLog/Entries/")
2852fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2853fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2854fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2855fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2856fdd26906SClaire Weinan }
2857fdd26906SClaire Weinan 
2858fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2859fdd26906SClaire Weinan {
2860253f11b8SEd Tanous     BMCWEB_ROUTE(
2861253f11b8SEd Tanous         app, "/redfish/v1/Managers/<str>/LogServices/FaultLog/Entries/<str>/")
2862fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2863fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2864fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2865fdd26906SClaire Weinan 
2866253f11b8SEd Tanous     BMCWEB_ROUTE(
2867253f11b8SEd Tanous         app, "/redfish/v1/Managers/<str>/LogServices/FaultLog/Entries/<str>/")
2868fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2869fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2870fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2871fdd26906SClaire Weinan }
2872fdd26906SClaire Weinan 
2873fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2874fdd26906SClaire Weinan {
2875fdd26906SClaire Weinan     BMCWEB_ROUTE(
2876fdd26906SClaire Weinan         app,
2877253f11b8SEd Tanous         "/redfish/v1/Managers/<str>/LogServices/FaultLog/Actions/LogService.ClearLog/")
2878fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2879fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2880fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
28815cb1dd27SAsmitha Karunanithi }
28825cb1dd27SAsmitha Karunanithi 
28837e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
28845cb1dd27SAsmitha Karunanithi {
288522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2886ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
28876ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
288822d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
28895cb1dd27SAsmitha Karunanithi }
28905cb1dd27SAsmitha Karunanithi 
28917e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
28927e860f15SJohn Edward Broadbent {
289322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2894ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
289522d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
289622d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
289722d268cbSEd Tanous             std::ref(app)));
28985cb1dd27SAsmitha Karunanithi }
28995cb1dd27SAsmitha Karunanithi 
29007e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
29015cb1dd27SAsmitha Karunanithi {
29027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
290322d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2904ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29056ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
290622d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
29078d1b46d7Szhanghch05 
29087e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
290922d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2910ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
29116ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
291222d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
29135cb1dd27SAsmitha Karunanithi }
2914c9bb6861Sraviteja-b 
29157e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2916c9bb6861Sraviteja-b {
29170fda0f12SGeorge Liu     BMCWEB_ROUTE(
29180fda0f12SGeorge Liu         app,
291922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2920ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
292122d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
292222d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
292322d268cbSEd Tanous             std::ref(app)));
2924a43be80fSAsmitha Karunanithi }
2925a43be80fSAsmitha Karunanithi 
29267e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2927a43be80fSAsmitha Karunanithi {
29280fda0f12SGeorge Liu     BMCWEB_ROUTE(
29290fda0f12SGeorge Liu         app,
293022d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2931ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29326ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
293322d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2934013487e5Sraviteja-b }
2935013487e5Sraviteja-b 
29367e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
29371da66f75SEd Tanous {
29383946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29393946028dSAppaRao Puli     // method for security reasons.
29401da66f75SEd Tanous     /**
29411da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
29421da66f75SEd Tanous      */
294322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2944ed398213SEd Tanous         // This is incorrect, should be:
2945ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2946432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2947002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2948002d39b4SEd Tanous             [&app](const crow::Request& req,
294922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
295022d268cbSEd Tanous                    const std::string& systemName) {
29513ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
295245ca1b86SEd Tanous         {
295345ca1b86SEd Tanous             return;
295445ca1b86SEd Tanous         }
295525b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
29567f3e84a1SEd Tanous         {
29577f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
29587f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
29597f3e84a1SEd Tanous                                        systemName);
29607f3e84a1SEd Tanous             return;
29617f3e84a1SEd Tanous         }
2962253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
296322d268cbSEd Tanous         {
296422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
296522d268cbSEd Tanous                                        systemName);
296622d268cbSEd Tanous             return;
296722d268cbSEd Tanous         }
296822d268cbSEd Tanous 
29697e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
29707e860f15SJohn Edward Broadbent         // SubRoute
29710f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2972253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/Crashdump",
2973253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
2974e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
29758e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
29764f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
29774f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
297815b89725SV-Sanjana         asyncResp->res.jsonValue["Id"] = "Crashdump";
2979539d8c6bSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] =
2980539d8c6bSEd Tanous             log_service::OverWritePolicy::WrapsWhenFull;
2981e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
29827c8c4058STejas Patil 
29837c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
29842b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
29857c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
29867c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
29877c8c4058STejas Patil             redfishDateTimeOffset.second;
29887c8c4058STejas Patil 
29891476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
2990253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/Crashdump/Entries",
2991253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
2992253f11b8SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]
2993253f11b8SEd Tanous                                 ["target"] = std::format(
2994253f11b8SEd Tanous             "/redfish/v1/Systems/{}/LogServices/Crashdump/Actions/LogService.ClearLog",
2995253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME);
2996002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
2997253f11b8SEd Tanous                                 ["target"] = std::format(
2998253f11b8SEd Tanous             "/redfish/v1/Systems/{}/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData",
2999253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME);
30007e860f15SJohn Edward Broadbent     });
30011da66f75SEd Tanous }
30021da66f75SEd Tanous 
30037e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
30045b61b5e8SJason M. Bills {
30050fda0f12SGeorge Liu     BMCWEB_ROUTE(
30060fda0f12SGeorge Liu         app,
300722d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
3008ed398213SEd Tanous         // This is incorrect, should be:
3009ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3010432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30117e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
301245ca1b86SEd Tanous             [&app](const crow::Request& req,
301322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
301422d268cbSEd Tanous                    const std::string& systemName) {
30153ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
301645ca1b86SEd Tanous         {
301745ca1b86SEd Tanous             return;
301845ca1b86SEd Tanous         }
301925b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
30207f3e84a1SEd Tanous         {
30217f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
30227f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
30237f3e84a1SEd Tanous                                        systemName);
30247f3e84a1SEd Tanous             return;
30257f3e84a1SEd Tanous         }
3026253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
302722d268cbSEd Tanous         {
302822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
302922d268cbSEd Tanous                                        systemName);
303022d268cbSEd Tanous             return;
303122d268cbSEd Tanous         }
30325b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
30335e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec,
3034cb13a392SEd Tanous                         const std::string&) {
30355b61b5e8SJason M. Bills             if (ec)
30365b61b5e8SJason M. Bills             {
30375b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
30385b61b5e8SJason M. Bills                 return;
30395b61b5e8SJason M. Bills             }
30405b61b5e8SJason M. Bills             messages::success(asyncResp->res);
30415b61b5e8SJason M. Bills         },
3042002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
30437e860f15SJohn Edward Broadbent     });
30445b61b5e8SJason M. Bills }
30455b61b5e8SJason M. Bills 
30468d1b46d7Szhanghch05 static void
30478d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30488d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3049e855dd28SJason M. Bills {
3050043a0536SJohnathan Mantey     auto getStoredLogCallback =
3051b9d36b47SEd Tanous         [asyncResp, logID,
30525e7e2dc5SEd Tanous          &logEntryJson](const boost::system::error_code& ec,
3053b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3054e855dd28SJason M. Bills         if (ec)
3055e855dd28SJason M. Bills         {
305662598e31SEd Tanous             BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
30571ddcf01aSJason M. Bills             if (ec.value() ==
30581ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
30591ddcf01aSJason M. Bills             {
3060002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
30611ddcf01aSJason M. Bills             }
30621ddcf01aSJason M. Bills             else
30631ddcf01aSJason M. Bills             {
3064e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
30651ddcf01aSJason M. Bills             }
3066e855dd28SJason M. Bills             return;
3067e855dd28SJason M. Bills         }
3068043a0536SJohnathan Mantey 
3069043a0536SJohnathan Mantey         std::string timestamp{};
3070043a0536SJohnathan Mantey         std::string filename{};
3071043a0536SJohnathan Mantey         std::string logfile{};
30722c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3073043a0536SJohnathan Mantey 
3074043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3075e855dd28SJason M. Bills         {
30769db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3077e855dd28SJason M. Bills             return;
3078e855dd28SJason M. Bills         }
3079e855dd28SJason M. Bills 
3080043a0536SJohnathan Mantey         std::string crashdumpURI =
3081253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/Crashdump/Entries/",
3082253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME) +
3083043a0536SJohnathan Mantey             logID + "/" + filename;
308484afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
30859c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3086ef4c65b7SEd Tanous         logEntry["@odata.id"] = boost::urls::format(
3087253f11b8SEd Tanous             "/redfish/v1/Systems/{}/LogServices/Crashdump/Entries/{}",
3088253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME, logID);
308984afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
309084afc48bSJason M. Bills         logEntry["Id"] = logID;
3091539d8c6bSEd Tanous         logEntry["EntryType"] = log_entry::LogEntryType::Oem;
309284afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
309384afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
309484afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
309584afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
30962b20ef6eSJason M. Bills 
30972b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
30982b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
30992b20ef6eSJason M. Bills         // directly
31002b20ef6eSJason M. Bills         if (logEntryJson.is_array())
31012b20ef6eSJason M. Bills         {
31022b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
31032b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
31042b20ef6eSJason M. Bills                 logEntryJson.size();
31052b20ef6eSJason M. Bills         }
31062b20ef6eSJason M. Bills         else
31072b20ef6eSJason M. Bills         {
3108d405bb51SJason M. Bills             logEntryJson.update(logEntry);
31092b20ef6eSJason M. Bills         }
3110e855dd28SJason M. Bills     };
3111d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3112d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3113d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3114d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3115e855dd28SJason M. Bills }
3116e855dd28SJason M. Bills 
31177e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
31181da66f75SEd Tanous {
31193946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31203946028dSAppaRao Puli     // method for security reasons.
31211da66f75SEd Tanous     /**
31221da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
31231da66f75SEd Tanous      */
31247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
312522d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3126ed398213SEd Tanous         // This is incorrect, should be.
3127ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3128432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3129002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3130002d39b4SEd Tanous             [&app](const crow::Request& req,
313122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
313222d268cbSEd Tanous                    const std::string& systemName) {
31333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
313445ca1b86SEd Tanous         {
313545ca1b86SEd Tanous             return;
313645ca1b86SEd Tanous         }
313725b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
31387f3e84a1SEd Tanous         {
31397f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
31407f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
31417f3e84a1SEd Tanous                                        systemName);
31427f3e84a1SEd Tanous             return;
31437f3e84a1SEd Tanous         }
3144253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
314522d268cbSEd Tanous         {
314622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
314722d268cbSEd Tanous                                        systemName);
314822d268cbSEd Tanous             return;
314922d268cbSEd Tanous         }
315022d268cbSEd Tanous 
31517a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
31527a1dbc48SGeorge Liu             crashdumpInterface};
31537a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
31547a1dbc48SGeorge Liu             "/", 0, interfaces,
31557a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
31562b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
31571da66f75SEd Tanous             if (ec)
31581da66f75SEd Tanous             {
31591da66f75SEd Tanous                 if (ec.value() !=
31601da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
31611da66f75SEd Tanous                 {
316262598e31SEd Tanous                     BMCWEB_LOG_DEBUG("failed to get entries ec: {}",
316362598e31SEd Tanous                                      ec.message());
3164f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
31651da66f75SEd Tanous                     return;
31661da66f75SEd Tanous                 }
31671da66f75SEd Tanous             }
3168e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
31691da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
3170253f11b8SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = std::format(
3171253f11b8SEd Tanous                 "/redfish/v1/Systems/{}/LogServices/Crashdump/Entries",
3172253f11b8SEd Tanous                 BMCWEB_REDFISH_SYSTEM_URI_NAME);
3173002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3174e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3175424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3176002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3177a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
31782b20ef6eSJason M. Bills 
31792b20ef6eSJason M. Bills             for (const std::string& path : resp)
31801da66f75SEd Tanous             {
31812b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3182e855dd28SJason M. Bills                 // Get the log ID
31832b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
31842b20ef6eSJason M. Bills                 if (logID.empty())
31851da66f75SEd Tanous                 {
3186e855dd28SJason M. Bills                     continue;
31871da66f75SEd Tanous                 }
3188e855dd28SJason M. Bills                 // Add the log entry to the array
31892b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
31902b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
31911da66f75SEd Tanous             }
31927a1dbc48SGeorge Liu         });
31937e860f15SJohn Edward Broadbent     });
31941da66f75SEd Tanous }
31951da66f75SEd Tanous 
31967e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
31971da66f75SEd Tanous {
31983946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31993946028dSAppaRao Puli     // method for security reasons.
32001da66f75SEd Tanous 
32017e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
320222d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3203ed398213SEd Tanous         // this is incorrect, should be
3204ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3205432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32067e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
320745ca1b86SEd Tanous             [&app](const crow::Request& req,
32087e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
320922d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
32103ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
321145ca1b86SEd Tanous         {
321245ca1b86SEd Tanous             return;
321345ca1b86SEd Tanous         }
321425b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
32157f3e84a1SEd Tanous         {
32167f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
32177f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
32187f3e84a1SEd Tanous                                        systemName);
32197f3e84a1SEd Tanous             return;
32207f3e84a1SEd Tanous         }
3221253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
322222d268cbSEd Tanous         {
322322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
322422d268cbSEd Tanous                                        systemName);
322522d268cbSEd Tanous             return;
322622d268cbSEd Tanous         }
32277e860f15SJohn Edward Broadbent         const std::string& logID = param;
3228e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
32297e860f15SJohn Edward Broadbent     });
3230e855dd28SJason M. Bills }
3231e855dd28SJason M. Bills 
32327e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3233e855dd28SJason M. Bills {
32343946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32353946028dSAppaRao Puli     // method for security reasons.
32367e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
32377e860f15SJohn Edward Broadbent         app,
323822d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3239ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
32407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3241a4ce114aSNan Zhou             [](const crow::Request& req,
32427e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
324322d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
324422d268cbSEd Tanous                const std::string& fileName) {
32452a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
32462a9beeedSShounak Mitra         // Redfish resource.
324722d268cbSEd Tanous 
324825b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
32497f3e84a1SEd Tanous         {
32507f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
32517f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
32527f3e84a1SEd Tanous                                        systemName);
32537f3e84a1SEd Tanous             return;
32547f3e84a1SEd Tanous         }
3255253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
325622d268cbSEd Tanous         {
325722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
325822d268cbSEd Tanous                                        systemName);
325922d268cbSEd Tanous             return;
326022d268cbSEd Tanous         }
326122d268cbSEd Tanous 
3262043a0536SJohnathan Mantey         auto getStoredLogCallback =
326339662a3bSEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.url()))](
32645e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
3265002d39b4SEd Tanous                 const std::vector<
3266002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
32677e860f15SJohn Edward Broadbent                     resp) {
32681da66f75SEd Tanous             if (ec)
32691da66f75SEd Tanous             {
327062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("failed to get log ec: {}", ec.message());
3271f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
32721da66f75SEd Tanous                 return;
32731da66f75SEd Tanous             }
3274e855dd28SJason M. Bills 
3275043a0536SJohnathan Mantey             std::string dbusFilename{};
3276043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3277043a0536SJohnathan Mantey             std::string dbusFilepath{};
3278043a0536SJohnathan Mantey 
3279002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3280002d39b4SEd Tanous                                      dbusFilepath);
3281043a0536SJohnathan Mantey 
3282043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3283043a0536SJohnathan Mantey                 dbusFilepath.empty())
32841da66f75SEd Tanous             {
32859db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
32861da66f75SEd Tanous                 return;
32871da66f75SEd Tanous             }
3288e855dd28SJason M. Bills 
3289043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3290043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3291043a0536SJohnathan Mantey             {
32929db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3293043a0536SJohnathan Mantey                 return;
3294043a0536SJohnathan Mantey             }
3295043a0536SJohnathan Mantey 
329627b0cf90SEd Tanous             if (!asyncResp->res.openFile(dbusFilepath))
3297043a0536SJohnathan Mantey             {
32989db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3299043a0536SJohnathan Mantey                 return;
3300043a0536SJohnathan Mantey             }
3301043a0536SJohnathan Mantey 
33027e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
33037e860f15SJohn Edward Broadbent             // from a browser
3304d9f6c621SEd Tanous             asyncResp->res.addHeader(
3305d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
33061da66f75SEd Tanous         };
3307d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3308d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3309d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3310d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
33117e860f15SJohn Edward Broadbent     });
33121da66f75SEd Tanous }
33131da66f75SEd Tanous 
3314c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3315c5a4c82aSJason M. Bills {
3316c5a4c82aSJason M. Bills     onDemand,
3317c5a4c82aSJason M. Bills     telemetry,
3318c5a4c82aSJason M. Bills     invalid,
3319c5a4c82aSJason M. Bills };
3320c5a4c82aSJason M. Bills 
332126ccae32SEd Tanous inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
3322c5a4c82aSJason M. Bills {
3323c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3324c5a4c82aSJason M. Bills     {
3325c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3326c5a4c82aSJason M. Bills     }
3327c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3328c5a4c82aSJason M. Bills     {
3329c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3330c5a4c82aSJason M. Bills     }
3331c5a4c82aSJason M. Bills 
3332c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3333c5a4c82aSJason M. Bills }
3334c5a4c82aSJason M. Bills 
33357e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
33361da66f75SEd Tanous {
33373946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
33383946028dSAppaRao Puli     // method for security reasons.
33390fda0f12SGeorge Liu     BMCWEB_ROUTE(
33400fda0f12SGeorge Liu         app,
334122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3342ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3343ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3344432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3345002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3346002d39b4SEd Tanous             [&app](const crow::Request& req,
334722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
334822d268cbSEd Tanous                    const std::string& systemName) {
33493ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
335045ca1b86SEd Tanous         {
335145ca1b86SEd Tanous             return;
335245ca1b86SEd Tanous         }
335322d268cbSEd Tanous 
335425b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
33557f3e84a1SEd Tanous         {
33567f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
33577f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
33587f3e84a1SEd Tanous                                        systemName);
33597f3e84a1SEd Tanous             return;
33607f3e84a1SEd Tanous         }
3361253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
336222d268cbSEd Tanous         {
336322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
336422d268cbSEd Tanous                                        systemName);
336522d268cbSEd Tanous             return;
336622d268cbSEd Tanous         }
336722d268cbSEd Tanous 
33688e6c099aSJason M. Bills         std::string diagnosticDataType;
33698e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
337015ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3371002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3372002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
33738e6c099aSJason M. Bills         {
33748e6c099aSJason M. Bills             return;
33758e6c099aSJason M. Bills         }
33768e6c099aSJason M. Bills 
33778e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
33788e6c099aSJason M. Bills         {
337962598e31SEd Tanous             BMCWEB_LOG_ERROR(
338062598e31SEd Tanous                 "Only OEM DiagnosticDataType supported for Crashdump");
33818e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
33828e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
33838e6c099aSJason M. Bills                 "CollectDiagnosticData");
33848e6c099aSJason M. Bills             return;
33858e6c099aSJason M. Bills         }
33868e6c099aSJason M. Bills 
3387c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3388c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3389c5a4c82aSJason M. Bills 
3390c5a4c82aSJason M. Bills         std::string iface;
3391c5a4c82aSJason M. Bills         std::string method;
3392c5a4c82aSJason M. Bills         std::string taskMatchStr;
3393c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3394c5a4c82aSJason M. Bills         {
3395c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3396c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3397c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3398c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3399c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3400c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3401c5a4c82aSJason M. Bills         }
3402c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3403c5a4c82aSJason M. Bills         {
3404c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3405c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3406c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3407c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3408c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3409c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3410c5a4c82aSJason M. Bills         }
3411c5a4c82aSJason M. Bills         else
3412c5a4c82aSJason M. Bills         {
341362598e31SEd Tanous             BMCWEB_LOG_ERROR("Unsupported OEMDiagnosticDataType: {}",
341462598e31SEd Tanous                              oemDiagnosticDataType);
3415c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3416002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3417002d39b4SEd Tanous                 "CollectDiagnosticData");
3418c5a4c82aSJason M. Bills             return;
3419c5a4c82aSJason M. Bills         }
3420c5a4c82aSJason M. Bills 
3421c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3422c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
34235e7e2dc5SEd Tanous              taskMatchStr](const boost::system::error_code& ec,
342498be3e39SEd Tanous                            const std::string&) mutable {
34251da66f75SEd Tanous             if (ec)
34261da66f75SEd Tanous             {
3427002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
34281da66f75SEd Tanous                 {
3429f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
34301da66f75SEd Tanous                 }
34314363d3b2SJason M. Bills                 else if (ec.value() ==
34324363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
34334363d3b2SJason M. Bills                 {
3434002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3435002d39b4SEd Tanous                                                             "60");
34364363d3b2SJason M. Bills                 }
34371da66f75SEd Tanous                 else
34381da66f75SEd Tanous                 {
3439f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
34401da66f75SEd Tanous                 }
34411da66f75SEd Tanous                 return;
34421da66f75SEd Tanous             }
3443002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
34448b24275dSEd Tanous                 [](const boost::system::error_code& ec2, sdbusplus::message_t&,
3445002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
34468b24275dSEd Tanous                 if (!ec2)
344766afe4faSJames Feist                 {
3448002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3449e5d5006bSJames Feist                         std::to_string(taskData->index)));
3450831d6b09SJames Feist                     taskData->state = "Completed";
345166afe4faSJames Feist                 }
345232898ceaSJames Feist                 return task::completed;
345366afe4faSJames Feist             },
3454c5a4c82aSJason M. Bills                 taskMatchStr);
3455c5a4c82aSJason M. Bills 
345646229577SJames Feist             task->startTimer(std::chrono::minutes(5));
345746229577SJames Feist             task->populateResp(asyncResp->res);
345898be3e39SEd Tanous             task->payload.emplace(std::move(payload));
34591da66f75SEd Tanous         };
34608e6c099aSJason M. Bills 
34611da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3462002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3463002d39b4SEd Tanous             iface, method);
34647e860f15SJohn Edward Broadbent     });
34656eda7685SKenny L. Ku }
34666eda7685SKenny L. Ku 
3467599b9af3SAlexander Hansen inline void dBusLogServiceActionsClear(
3468599b9af3SAlexander Hansen     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
3469599b9af3SAlexander Hansen {
3470599b9af3SAlexander Hansen     BMCWEB_LOG_DEBUG("Do delete all entries.");
3471599b9af3SAlexander Hansen 
3472599b9af3SAlexander Hansen     // Process response from Logging service.
3473599b9af3SAlexander Hansen     auto respHandler = [asyncResp](const boost::system::error_code& ec) {
3474599b9af3SAlexander Hansen         BMCWEB_LOG_DEBUG("doClearLog resp_handler callback: Done");
3475599b9af3SAlexander Hansen         if (ec)
3476599b9af3SAlexander Hansen         {
3477599b9af3SAlexander Hansen             // TODO Handle for specific error code
3478599b9af3SAlexander Hansen             BMCWEB_LOG_ERROR("doClearLog resp_handler got error {}", ec);
3479599b9af3SAlexander Hansen             asyncResp->res.result(
3480599b9af3SAlexander Hansen                 boost::beast::http::status::internal_server_error);
3481599b9af3SAlexander Hansen             return;
3482599b9af3SAlexander Hansen         }
3483599b9af3SAlexander Hansen 
3484599b9af3SAlexander Hansen         asyncResp->res.result(boost::beast::http::status::no_content);
3485599b9af3SAlexander Hansen     };
3486599b9af3SAlexander Hansen 
3487599b9af3SAlexander Hansen     // Make call to Logging service to request Clear Log
3488599b9af3SAlexander Hansen     crow::connections::systemBus->async_method_call(
3489599b9af3SAlexander Hansen         respHandler, "xyz.openbmc_project.Logging",
3490599b9af3SAlexander Hansen         "/xyz/openbmc_project/logging",
3491599b9af3SAlexander Hansen         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
3492599b9af3SAlexander Hansen }
3493599b9af3SAlexander Hansen 
3494cb92c03bSAndrew Geissler /**
3495cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3496cb92c03bSAndrew Geissler  */
34977e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3498cb92c03bSAndrew Geissler {
3499cb92c03bSAndrew Geissler     /**
3500cb92c03bSAndrew Geissler      * Function handles POST method request.
3501cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3502cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3503cb92c03bSAndrew Geissler      */
35047e860f15SJohn Edward Broadbent 
35050fda0f12SGeorge Liu     BMCWEB_ROUTE(
35060fda0f12SGeorge Liu         app,
350722d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3508ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
35097e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
351045ca1b86SEd Tanous             [&app](const crow::Request& req,
351122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
351222d268cbSEd Tanous                    const std::string& systemName) {
35133ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
351445ca1b86SEd Tanous         {
351545ca1b86SEd Tanous             return;
351645ca1b86SEd Tanous         }
351725b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
35187f3e84a1SEd Tanous         {
35197f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
35207f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
35217f3e84a1SEd Tanous                                        systemName);
35227f3e84a1SEd Tanous             return;
35237f3e84a1SEd Tanous         }
3524253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
352522d268cbSEd Tanous         {
352622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
352722d268cbSEd Tanous                                        systemName);
352822d268cbSEd Tanous             return;
352922d268cbSEd Tanous         }
3530599b9af3SAlexander Hansen         dBusLogServiceActionsClear(asyncResp);
35317e860f15SJohn Edward Broadbent     });
3532cb92c03bSAndrew Geissler }
3533a3316fc6SZhikuiRen 
3534a3316fc6SZhikuiRen /****************************************************
3535a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3536a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3537a3316fc6SZhikuiRen  ******************************************************/
35387e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3539a3316fc6SZhikuiRen {
354022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3541ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3542002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3543002d39b4SEd Tanous             [&app](const crow::Request& req,
354422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
354522d268cbSEd Tanous                    const std::string& systemName) {
35463ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
354745ca1b86SEd Tanous         {
354845ca1b86SEd Tanous             return;
354945ca1b86SEd Tanous         }
355025b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
35517f3e84a1SEd Tanous         {
35527f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
35537f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
35547f3e84a1SEd Tanous                                        systemName);
35557f3e84a1SEd Tanous             return;
35567f3e84a1SEd Tanous         }
3557253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
355822d268cbSEd Tanous         {
355922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
356022d268cbSEd Tanous                                        systemName);
356122d268cbSEd Tanous             return;
356222d268cbSEd Tanous         }
35631476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
3564253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/PostCodes",
3565253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
35661476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
3567b25644a1SJanet Adkins             "#LogService.v1_2_0.LogService";
35681476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
35691476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3570ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "PostCodes";
3571539d8c6bSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] =
3572539d8c6bSEd Tanous             log_service::OverWritePolicy::WrapsWhenFull;
35731476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3574253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/PostCodes/Entries",
3575253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
35767c8c4058STejas Patil 
35777c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
35782b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
35790fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
35807c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
35817c8c4058STejas Patil             redfishDateTimeOffset.second;
35827c8c4058STejas Patil 
358320fa6a2cSEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]
358420fa6a2cSEd Tanous                                 ["target"] = std::format(
3585253f11b8SEd Tanous             "/redfish/v1/Systems/{}/LogServices/PostCodes/Actions/LogService.ClearLog",
358620fa6a2cSEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME);
35877e860f15SJohn Edward Broadbent     });
3588a3316fc6SZhikuiRen }
3589a3316fc6SZhikuiRen 
35907e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3591a3316fc6SZhikuiRen {
35920fda0f12SGeorge Liu     BMCWEB_ROUTE(
35930fda0f12SGeorge Liu         app,
359422d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3595ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3596ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3597432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
35987e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
359945ca1b86SEd Tanous             [&app](const crow::Request& req,
360022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
360122d268cbSEd Tanous                    const std::string& systemName) {
36023ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
360345ca1b86SEd Tanous         {
360445ca1b86SEd Tanous             return;
360545ca1b86SEd Tanous         }
360625b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
36077f3e84a1SEd Tanous         {
36087f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
36097f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
36107f3e84a1SEd Tanous                                        systemName);
36117f3e84a1SEd Tanous             return;
36127f3e84a1SEd Tanous         }
3613253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
361422d268cbSEd Tanous         {
361522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
361622d268cbSEd Tanous                                        systemName);
361722d268cbSEd Tanous             return;
361822d268cbSEd Tanous         }
361962598e31SEd Tanous         BMCWEB_LOG_DEBUG("Do delete all postcodes entries.");
3620a3316fc6SZhikuiRen 
3621a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3622a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
36235e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
3624a3316fc6SZhikuiRen             if (ec)
3625a3316fc6SZhikuiRen             {
3626a3316fc6SZhikuiRen                 // TODO Handle for specific error code
362762598e31SEd Tanous                 BMCWEB_LOG_ERROR("doClearPostCodes resp_handler got error {}",
362862598e31SEd Tanous                                  ec);
3629002d39b4SEd Tanous                 asyncResp->res.result(
3630002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3631a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3632a3316fc6SZhikuiRen                 return;
3633a3316fc6SZhikuiRen             }
363418fc70c0STony Lee             messages::success(asyncResp->res);
3635a3316fc6SZhikuiRen         },
363615124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
363715124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3638a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
36397e860f15SJohn Edward Broadbent     });
3640a3316fc6SZhikuiRen }
3641a3316fc6SZhikuiRen 
36426f284d24SJiaqing Zhao /**
36436f284d24SJiaqing Zhao  * @brief Parse post code ID and get the current value and index value
36446f284d24SJiaqing Zhao  *        eg: postCodeID=B1-2, currentValue=1, index=2
36456f284d24SJiaqing Zhao  *
36466f284d24SJiaqing Zhao  * @param[in]  postCodeID     Post Code ID
36476f284d24SJiaqing Zhao  * @param[out] currentValue   Current value
36486f284d24SJiaqing Zhao  * @param[out] index          Index value
36496f284d24SJiaqing Zhao  *
36506f284d24SJiaqing Zhao  * @return bool true if the parsing is successful, false the parsing fails
36516f284d24SJiaqing Zhao  */
36526f056f24SEd Tanous inline bool parsePostCode(std::string_view postCodeID, uint64_t& currentValue,
3653df254f2cSEd Tanous                           uint16_t& index)
36546f284d24SJiaqing Zhao {
36556f284d24SJiaqing Zhao     std::vector<std::string> split;
365650ebd4afSEd Tanous     bmcweb::split(split, postCodeID, '-');
36576f056f24SEd Tanous     if (split.size() != 2)
36586f056f24SEd Tanous     {
36596f056f24SEd Tanous         return false;
36606f056f24SEd Tanous     }
36616f056f24SEd Tanous     std::string_view postCodeNumber = split[0];
36626f056f24SEd Tanous     if (postCodeNumber.size() < 2)
36636f056f24SEd Tanous     {
36646f056f24SEd Tanous         return false;
36656f056f24SEd Tanous     }
36666f056f24SEd Tanous     if (postCodeNumber[0] != 'B')
36676f056f24SEd Tanous     {
36686f056f24SEd Tanous         return false;
36696f056f24SEd Tanous     }
36706f056f24SEd Tanous     postCodeNumber.remove_prefix(1);
36716f056f24SEd Tanous     auto [ptrIndex, ecIndex] = std::from_chars(postCodeNumber.begin(),
36726f056f24SEd Tanous                                                postCodeNumber.end(), index);
36736f056f24SEd Tanous     if (ptrIndex != postCodeNumber.end() || ecIndex != std::errc())
36746f284d24SJiaqing Zhao     {
36756f284d24SJiaqing Zhao         return false;
36766f284d24SJiaqing Zhao     }
36776f284d24SJiaqing Zhao 
36786f056f24SEd Tanous     std::string_view postCodeIndex = split[1];
36796f284d24SJiaqing Zhao 
36806f056f24SEd Tanous     auto [ptrValue, ecValue] = std::from_chars(
36816f056f24SEd Tanous         postCodeIndex.begin(), postCodeIndex.end(), currentValue);
36826f284d24SJiaqing Zhao 
36836f056f24SEd Tanous     return ptrValue == postCodeIndex.end() && ecValue == std::errc();
36846f284d24SJiaqing Zhao }
36856f284d24SJiaqing Zhao 
36866f284d24SJiaqing Zhao static bool fillPostCodeEntry(
3687ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36886c9a279eSManojkiran Eda     const boost::container::flat_map<
36896c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3690a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3691a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3692a3316fc6SZhikuiRen {
3693a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3694fffb8c1fSEd Tanous     const registries::Message* message =
3695fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3696dc8cfa66SEd Tanous     if (message == nullptr)
3697dc8cfa66SEd Tanous     {
3698dc8cfa66SEd Tanous         BMCWEB_LOG_ERROR("Couldn't find known message?");
3699dc8cfa66SEd Tanous         return false;
3700dc8cfa66SEd Tanous     }
3701a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3702a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
37036c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
37046c9a279eSManojkiran Eda              code : postcode)
3705a3316fc6SZhikuiRen     {
3706a3316fc6SZhikuiRen         currentCodeIndex++;
3707a3316fc6SZhikuiRen         std::string postcodeEntryID =
3708a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3709a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3710a3316fc6SZhikuiRen 
3711a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3712a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3713a3316fc6SZhikuiRen 
3714a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3715a3316fc6SZhikuiRen         { // already incremented
3716a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3717a3316fc6SZhikuiRen         }
3718a3316fc6SZhikuiRen         else
3719a3316fc6SZhikuiRen         {
3720a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3721a3316fc6SZhikuiRen         }
3722a3316fc6SZhikuiRen 
3723a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3724a3316fc6SZhikuiRen         // not fall between top and skip
3725a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3726a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3727a3316fc6SZhikuiRen         {
3728a3316fc6SZhikuiRen             continue;
3729a3316fc6SZhikuiRen         }
3730a3316fc6SZhikuiRen 
37314e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3732a3316fc6SZhikuiRen         // currentIndex
3733a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3734a3316fc6SZhikuiRen         {
3735a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3736a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3737a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3738a3316fc6SZhikuiRen             continue;
3739a3316fc6SZhikuiRen         }
3740a3316fc6SZhikuiRen 
3741a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3742a3316fc6SZhikuiRen         // index
3743a3316fc6SZhikuiRen 
3744a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3745a3316fc6SZhikuiRen         std::string entryTimeStr;
37462a025611SKonstantin Aladyshev         entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
3747a3316fc6SZhikuiRen 
3748a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3749a3316fc6SZhikuiRen         std::ostringstream hexCode;
3750a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
37516c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3752a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3753a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3754a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3755a3316fc6SZhikuiRen         // Set precision to 4 digits
3756a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3757a3316fc6SZhikuiRen         // Add double to stream
3758a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3759a3316fc6SZhikuiRen 
37601e6deaf6SEd Tanous         std::string bootIndexStr = std::to_string(bootIndex);
37611e6deaf6SEd Tanous         std::string timeOffsetString = timeOffsetStr.str();
37621e6deaf6SEd Tanous         std::string hexCodeStr = hexCode.str();
3763a3316fc6SZhikuiRen 
37641e6deaf6SEd Tanous         std::array<std::string_view, 3> messageArgs = {
37651e6deaf6SEd Tanous             bootIndexStr, timeOffsetString, hexCodeStr};
37661e6deaf6SEd Tanous 
37671e6deaf6SEd Tanous         std::string msg =
37681e6deaf6SEd Tanous             redfish::registries::fillMessageArgs(messageArgs, message->message);
37691e6deaf6SEd Tanous         if (msg.empty())
3770a3316fc6SZhikuiRen         {
37711e6deaf6SEd Tanous             messages::internalError(asyncResp->res);
37721e6deaf6SEd Tanous             return false;
3773a3316fc6SZhikuiRen         }
3774a3316fc6SZhikuiRen 
3775d4342a92STim Lee         // Get Severity template from message registry
3776d4342a92STim Lee         std::string severity;
3777d4342a92STim Lee         if (message != nullptr)
3778d4342a92STim Lee         {
37795f2b84eeSEd Tanous             severity = message->messageSeverity;
3780d4342a92STim Lee         }
3781d4342a92STim Lee 
37826f284d24SJiaqing Zhao         // Format entry
37836f284d24SJiaqing Zhao         nlohmann::json::object_t bmcLogEntry;
37849c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3785ef4c65b7SEd Tanous         bmcLogEntry["@odata.id"] = boost::urls::format(
3786253f11b8SEd Tanous             "/redfish/v1/Systems/{}/LogServices/PostCodes/Entries/{}",
3787253f11b8SEd Tanous             BMCWEB_REDFISH_SYSTEM_URI_NAME, postcodeEntryID);
378884afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
378984afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
379084afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
379184afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
37921e6deaf6SEd Tanous         bmcLogEntry["MessageArgs"] = messageArgs;
379384afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
379484afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
379584afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3796647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3797647b3cdcSGeorge Liu         {
3798647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3799253f11b8SEd Tanous                 std::format(
3800253f11b8SEd Tanous                     "/redfish/v1/Systems/{}/LogServices/PostCodes/Entries/",
3801253f11b8SEd Tanous                     BMCWEB_REDFISH_SYSTEM_URI_NAME) +
3802647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3803647b3cdcSGeorge Liu         }
38046f284d24SJiaqing Zhao 
38056f284d24SJiaqing Zhao         // codeIndex is only specified when querying single entry, return only
38066f284d24SJiaqing Zhao         // that entry in this case
38076f284d24SJiaqing Zhao         if (codeIndex != 0)
38086f284d24SJiaqing Zhao         {
3809ac106bf6SEd Tanous             asyncResp->res.jsonValue.update(bmcLogEntry);
38106f284d24SJiaqing Zhao             return true;
3811a3316fc6SZhikuiRen         }
38126f284d24SJiaqing Zhao 
3813ac106bf6SEd Tanous         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
3814b2ba3072SPatrick Williams         logEntryArray.emplace_back(std::move(bmcLogEntry));
38156f284d24SJiaqing Zhao     }
38166f284d24SJiaqing Zhao 
38176f284d24SJiaqing Zhao     // Return value is always false when querying multiple entries
38186f284d24SJiaqing Zhao     return false;
3819a3316fc6SZhikuiRen }
3820a3316fc6SZhikuiRen 
3821ac106bf6SEd Tanous static void
3822ac106bf6SEd Tanous     getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38236f284d24SJiaqing Zhao                         const std::string& entryId)
3824a3316fc6SZhikuiRen {
38256f284d24SJiaqing Zhao     uint16_t bootIndex = 0;
38266f284d24SJiaqing Zhao     uint64_t codeIndex = 0;
38276f284d24SJiaqing Zhao     if (!parsePostCode(entryId, codeIndex, bootIndex))
38286f284d24SJiaqing Zhao     {
38296f284d24SJiaqing Zhao         // Requested ID was not found
3830ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
38316f284d24SJiaqing Zhao         return;
38326f284d24SJiaqing Zhao     }
38336f284d24SJiaqing Zhao 
38346f284d24SJiaqing Zhao     if (bootIndex == 0 || codeIndex == 0)
38356f284d24SJiaqing Zhao     {
38366f284d24SJiaqing Zhao         // 0 is an invalid index
3837ac106bf6SEd Tanous         messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
38386f284d24SJiaqing Zhao         return;
38396f284d24SJiaqing Zhao     }
38406f284d24SJiaqing Zhao 
3841a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3842ac106bf6SEd Tanous         [asyncResp, entryId, bootIndex,
38435e7e2dc5SEd Tanous          codeIndex](const boost::system::error_code& ec,
38446c9a279eSManojkiran Eda                     const boost::container::flat_map<
38456c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38466c9a279eSManojkiran Eda                         postcode) {
3847a3316fc6SZhikuiRen         if (ec)
3848a3316fc6SZhikuiRen         {
384962598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
3850ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
3851a3316fc6SZhikuiRen             return;
3852a3316fc6SZhikuiRen         }
3853a3316fc6SZhikuiRen 
3854a3316fc6SZhikuiRen         if (postcode.empty())
3855a3316fc6SZhikuiRen         {
3856ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
3857a3316fc6SZhikuiRen             return;
3858a3316fc6SZhikuiRen         }
3859a3316fc6SZhikuiRen 
3860ac106bf6SEd Tanous         if (!fillPostCodeEntry(asyncResp, postcode, bootIndex, codeIndex))
38616f284d24SJiaqing Zhao         {
3862ac106bf6SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", entryId);
38636f284d24SJiaqing Zhao             return;
38646f284d24SJiaqing Zhao         }
3865a3316fc6SZhikuiRen     },
386615124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
386715124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3868a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3869a3316fc6SZhikuiRen         bootIndex);
3870a3316fc6SZhikuiRen }
3871a3316fc6SZhikuiRen 
3872ac106bf6SEd Tanous static void
3873ac106bf6SEd Tanous     getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3874ac106bf6SEd Tanous                        const uint16_t bootIndex, const uint16_t bootCount,
3875ac106bf6SEd Tanous                        const uint64_t entryCount, size_t skip, size_t top)
3876a3316fc6SZhikuiRen {
3877a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3878ac106bf6SEd Tanous         [asyncResp, bootIndex, bootCount, entryCount, skip,
38795e7e2dc5SEd Tanous          top](const boost::system::error_code& ec,
38806c9a279eSManojkiran Eda               const boost::container::flat_map<
38816c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38826c9a279eSManojkiran Eda                   postcode) {
3883a3316fc6SZhikuiRen         if (ec)
3884a3316fc6SZhikuiRen         {
388562598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS POST CODE PostCode response error");
3886ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
3887a3316fc6SZhikuiRen             return;
3888a3316fc6SZhikuiRen         }
3889a3316fc6SZhikuiRen 
3890a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3891a3316fc6SZhikuiRen         if (!postcode.empty())
3892a3316fc6SZhikuiRen         {
3893a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
38943648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3895a3316fc6SZhikuiRen             {
389689492a15SPatrick Williams                 uint64_t thisBootSkip = std::max(static_cast<uint64_t>(skip),
389789492a15SPatrick Williams                                                  entryCount) -
38983648c8beSEd Tanous                                         entryCount;
3899a3316fc6SZhikuiRen                 uint64_t thisBootTop =
39003648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
39013648c8beSEd Tanous                     entryCount;
3902a3316fc6SZhikuiRen 
3903ac106bf6SEd Tanous                 fillPostCodeEntry(asyncResp, postcode, bootIndex, 0,
3904ac106bf6SEd Tanous                                   thisBootSkip, thisBootTop);
3905a3316fc6SZhikuiRen             }
3906ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = endCount;
3907a3316fc6SZhikuiRen         }
3908a3316fc6SZhikuiRen 
3909a3316fc6SZhikuiRen         // continue to previous bootIndex
3910a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3911a3316fc6SZhikuiRen         {
3912ac106bf6SEd Tanous             getPostCodeForBoot(asyncResp, static_cast<uint16_t>(bootIndex + 1),
3913a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3914a3316fc6SZhikuiRen         }
391581584abeSJiaqing Zhao         else if (skip + top < endCount)
3916a3316fc6SZhikuiRen         {
3917ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.nextLink"] =
3918253f11b8SEd Tanous                 std::format(
3919253f11b8SEd Tanous                     "/redfish/v1/Systems/{}/LogServices/PostCodes/Entries?$skip=",
3920253f11b8SEd Tanous                     BMCWEB_REDFISH_SYSTEM_URI_NAME) +
3921a3316fc6SZhikuiRen                 std::to_string(skip + top);
3922a3316fc6SZhikuiRen         }
3923a3316fc6SZhikuiRen     },
392415124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
392515124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3926a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3927a3316fc6SZhikuiRen         bootIndex);
3928a3316fc6SZhikuiRen }
3929a3316fc6SZhikuiRen 
39308d1b46d7Szhanghch05 static void
3931ac106bf6SEd Tanous     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
39323648c8beSEd Tanous                          size_t skip, size_t top)
3933a3316fc6SZhikuiRen {
3934a3316fc6SZhikuiRen     uint64_t entryCount = 0;
39351e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
39361e1e598dSJonathan Doman         *crow::connections::systemBus,
39371e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
39381e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
39391e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3940ac106bf6SEd Tanous         [asyncResp, entryCount, skip, top](const boost::system::error_code& ec,
39411e1e598dSJonathan Doman                                            const uint16_t bootCount) {
3942a3316fc6SZhikuiRen         if (ec)
3943a3316fc6SZhikuiRen         {
394462598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
3945ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
3946a3316fc6SZhikuiRen             return;
3947a3316fc6SZhikuiRen         }
3948ac106bf6SEd Tanous         getPostCodeForBoot(asyncResp, 1, bootCount, entryCount, skip, top);
39491e1e598dSJonathan Doman     });
3950a3316fc6SZhikuiRen }
3951a3316fc6SZhikuiRen 
39527e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3953a3316fc6SZhikuiRen {
39547e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
395522d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3956ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
39577e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
395845ca1b86SEd Tanous             [&app](const crow::Request& req,
395922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
396022d268cbSEd Tanous                    const std::string& systemName) {
3961c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3962c937d2bfSEd Tanous             .canDelegateTop = true,
3963c937d2bfSEd Tanous             .canDelegateSkip = true,
3964c937d2bfSEd Tanous         };
3965c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3966c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
39673ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
396845ca1b86SEd Tanous         {
396945ca1b86SEd Tanous             return;
397045ca1b86SEd Tanous         }
397125b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
39727f3e84a1SEd Tanous         {
39737f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
39747f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
39757f3e84a1SEd Tanous                                        systemName);
39767f3e84a1SEd Tanous             return;
39777f3e84a1SEd Tanous         }
397822d268cbSEd Tanous 
3979253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
398022d268cbSEd Tanous         {
398122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
398222d268cbSEd Tanous                                        systemName);
398322d268cbSEd Tanous             return;
398422d268cbSEd Tanous         }
3985a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3986a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3987a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3988253f11b8SEd Tanous             std::format("/redfish/v1/Systems/{}/LogServices/PostCodes/Entries",
3989253f11b8SEd Tanous                         BMCWEB_REDFISH_SYSTEM_URI_NAME);
3990a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3991a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3992a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3993a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3994a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
39953648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
39965143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
39973648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
39987e860f15SJohn Edward Broadbent     });
3999a3316fc6SZhikuiRen }
4000a3316fc6SZhikuiRen 
4001647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
4002647b3cdcSGeorge Liu {
40030fda0f12SGeorge Liu     BMCWEB_ROUTE(
40040fda0f12SGeorge Liu         app,
400522d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
4006647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
4007647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
400845ca1b86SEd Tanous             [&app](const crow::Request& req,
4009647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
401022d268cbSEd Tanous                    const std::string& systemName,
4011647b3cdcSGeorge Liu                    const std::string& postCodeID) {
40123ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
401345ca1b86SEd Tanous         {
401445ca1b86SEd Tanous             return;
401545ca1b86SEd Tanous         }
401672e21377SMatt Spinler         if (!http_helpers::isContentTypeAllowed(
401799351cd8SEd Tanous                 req.getHeaderValue("Accept"),
40184a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
4019647b3cdcSGeorge Liu         {
4020002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
4021647b3cdcSGeorge Liu             return;
4022647b3cdcSGeorge Liu         }
402325b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
40247f3e84a1SEd Tanous         {
40257f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
40267f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
40277f3e84a1SEd Tanous                                        systemName);
40287f3e84a1SEd Tanous             return;
40297f3e84a1SEd Tanous         }
4030253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
403122d268cbSEd Tanous         {
403222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
403322d268cbSEd Tanous                                        systemName);
403422d268cbSEd Tanous             return;
403522d268cbSEd Tanous         }
4036647b3cdcSGeorge Liu 
4037647b3cdcSGeorge Liu         uint64_t currentValue = 0;
4038647b3cdcSGeorge Liu         uint16_t index = 0;
4039647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
4040647b3cdcSGeorge Liu         {
4041002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
4042647b3cdcSGeorge Liu             return;
4043647b3cdcSGeorge Liu         }
4044647b3cdcSGeorge Liu 
4045647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
4046647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
40475e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
4048002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
4049002d39b4SEd Tanous                     postcodes) {
4050647b3cdcSGeorge Liu             if (ec.value() == EBADR)
4051647b3cdcSGeorge Liu             {
4052002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4053002d39b4SEd Tanous                                            postCodeID);
4054647b3cdcSGeorge Liu                 return;
4055647b3cdcSGeorge Liu             }
4056647b3cdcSGeorge Liu             if (ec)
4057647b3cdcSGeorge Liu             {
405862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
4059647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
4060647b3cdcSGeorge Liu                 return;
4061647b3cdcSGeorge Liu             }
4062647b3cdcSGeorge Liu 
4063647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
4064002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
4065647b3cdcSGeorge Liu             {
406662598e31SEd Tanous                 BMCWEB_LOG_WARNING("Wrong currentValue value");
4067002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4068002d39b4SEd Tanous                                            postCodeID);
4069647b3cdcSGeorge Liu                 return;
4070647b3cdcSGeorge Liu             }
4071647b3cdcSGeorge Liu 
40729eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
407346ff87baSEd Tanous             if (c.empty())
4074647b3cdcSGeorge Liu             {
407562598e31SEd Tanous                 BMCWEB_LOG_WARNING("No found post code data");
4076002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4077002d39b4SEd Tanous                                            postCodeID);
4078647b3cdcSGeorge Liu                 return;
4079647b3cdcSGeorge Liu             }
408046ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
408146ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
408246ff87baSEd Tanous             std::string_view strData(d, c.size());
4083647b3cdcSGeorge Liu 
4084d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
4085647b3cdcSGeorge Liu                                      "application/octet-stream");
4086d9f6c621SEd Tanous             asyncResp->res.addHeader(
4087d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
408827b0cf90SEd Tanous             asyncResp->res.write(crow::utility::base64encode(strData));
4089647b3cdcSGeorge Liu         },
4090647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
4091647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
4092002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4093647b3cdcSGeorge Liu     });
4094647b3cdcSGeorge Liu }
4095647b3cdcSGeorge Liu 
40967e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
4097a3316fc6SZhikuiRen {
40987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
409922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
4100ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
41017e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
410245ca1b86SEd Tanous             [&app](const crow::Request& req,
41037e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
410422d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
41053ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
410645ca1b86SEd Tanous         {
410745ca1b86SEd Tanous             return;
410845ca1b86SEd Tanous         }
410925b54dbaSEd Tanous         if constexpr (BMCWEB_EXPERIMENTAL_REDFISH_MULTI_COMPUTER_SYSTEM)
41107f3e84a1SEd Tanous         {
41117f3e84a1SEd Tanous             // Option currently returns no systems.  TBD
41127f3e84a1SEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
41137f3e84a1SEd Tanous                                        systemName);
41147f3e84a1SEd Tanous             return;
41157f3e84a1SEd Tanous         }
4116253f11b8SEd Tanous         if (systemName != BMCWEB_REDFISH_SYSTEM_URI_NAME)
411722d268cbSEd Tanous         {
411822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
411922d268cbSEd Tanous                                        systemName);
412022d268cbSEd Tanous             return;
412122d268cbSEd Tanous         }
412222d268cbSEd Tanous 
41236f284d24SJiaqing Zhao         getPostCodeForEntry(asyncResp, targetID);
41247e860f15SJohn Edward Broadbent     });
4125a3316fc6SZhikuiRen }
4126a3316fc6SZhikuiRen 
41271da66f75SEd Tanous } // namespace redfish
4128