xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 5e7e2dc585dc7a7e62d2b648b541e7f50c39ea5d)
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"
21b7028ebfSSpencer Ku #include "gzfile.hpp"
22647b3cdcSGeorge Liu #include "http_utility.hpp"
23b7028ebfSSpencer Ku #include "human_sort.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
254851d45dSJason M. Bills #include "registries.hpp"
264851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
274851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
283ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
2946229577SJames Feist #include "task.hpp"
303ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
313ccb3adbSEd Tanous #include "utils/time_utils.hpp"
321da66f75SEd Tanous 
33e1f26343SJason M. Bills #include <systemd/sd-journal.h>
348e31778eSAsmitha Karunanithi #include <tinyxml2.h>
35400fd1fbSAdriana Kobylak #include <unistd.h>
36e1f26343SJason M. Bills 
379896eaedSEd Tanous #include <boost/algorithm/string/case_conv.hpp>
3811ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
39400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
404851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
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>
44d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
45d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
461214b7e7SGunnar Mills 
477a1dbc48SGeorge Liu #include <array>
48647b3cdcSGeorge Liu #include <charconv>
494418c7f0SJames Feist #include <filesystem>
5075710de2SXiaochao Ma #include <optional>
5126702d01SEd Tanous #include <span>
52cd225da8SJason M. Bills #include <string_view>
53abf2add6SEd Tanous #include <variant>
541da66f75SEd Tanous 
551da66f75SEd Tanous namespace redfish
561da66f75SEd Tanous {
571da66f75SEd Tanous 
585b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
595b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
605b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
615b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
625b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
635b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
64424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
656eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
666eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
671da66f75SEd Tanous 
688e31778eSAsmitha Karunanithi enum class DumpCreationProgress
698e31778eSAsmitha Karunanithi {
708e31778eSAsmitha Karunanithi     DUMP_CREATE_SUCCESS,
718e31778eSAsmitha Karunanithi     DUMP_CREATE_FAILED,
728e31778eSAsmitha Karunanithi     DUMP_CREATE_INPROGRESS
738e31778eSAsmitha Karunanithi };
748e31778eSAsmitha Karunanithi 
75fffb8c1fSEd Tanous namespace registries
764851d45dSJason M. Bills {
7726702d01SEd Tanous static const Message*
7826702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
7926702d01SEd Tanous                            const std::span<const MessageEntry> registry)
804851d45dSJason M. Bills {
81002d39b4SEd Tanous     std::span<const MessageEntry>::iterator messageIt =
82002d39b4SEd Tanous         std::find_if(registry.begin(), registry.end(),
834851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
84e662eae8SEd Tanous         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
854851d45dSJason M. Bills         });
8626702d01SEd Tanous     if (messageIt != registry.end())
874851d45dSJason M. Bills     {
884851d45dSJason M. Bills         return &messageIt->second;
894851d45dSJason M. Bills     }
904851d45dSJason M. Bills 
914851d45dSJason M. Bills     return nullptr;
924851d45dSJason M. Bills }
934851d45dSJason M. Bills 
9426ccae32SEd Tanous static const Message* getMessage(std::string_view messageID)
954851d45dSJason M. Bills {
964851d45dSJason M. Bills     // Redfish MessageIds are in the form
974851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
984851d45dSJason M. Bills     // the right Message
994851d45dSJason M. Bills     std::vector<std::string> fields;
1004851d45dSJason M. Bills     fields.reserve(4);
10150ebd4afSEd Tanous     bmcweb::split(fields, messageID, '.');
10202cad96eSEd Tanous     const std::string& registryName = fields[0];
10302cad96eSEd Tanous     const std::string& messageKey = fields[3];
1044851d45dSJason M. Bills 
1054851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
1064851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
1074851d45dSJason M. Bills     {
1084851d45dSJason M. Bills         return getMessageFromRegistry(
10926702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
1104851d45dSJason M. Bills     }
1114851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
1124851d45dSJason M. Bills     {
1134851d45dSJason M. Bills         return getMessageFromRegistry(
11426702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1154851d45dSJason M. Bills     }
1164851d45dSJason M. Bills     return nullptr;
1174851d45dSJason M. Bills }
118fffb8c1fSEd Tanous } // namespace registries
1194851d45dSJason M. Bills 
120f6150403SJames Feist namespace fs = std::filesystem;
1211da66f75SEd Tanous 
122cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
123cb92c03bSAndrew Geissler {
124d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
125d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
126d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
127d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
128cb92c03bSAndrew Geissler     {
129cb92c03bSAndrew Geissler         return "Critical";
130cb92c03bSAndrew Geissler     }
1313174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
132d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
133d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
134cb92c03bSAndrew Geissler     {
135cb92c03bSAndrew Geissler         return "OK";
136cb92c03bSAndrew Geissler     }
1373174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
138cb92c03bSAndrew Geissler     {
139cb92c03bSAndrew Geissler         return "Warning";
140cb92c03bSAndrew Geissler     }
141cb92c03bSAndrew Geissler     return "";
142cb92c03bSAndrew Geissler }
143cb92c03bSAndrew Geissler 
1449017faf2SAbhishek Patel inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
1459017faf2SAbhishek Patel {
1469017faf2SAbhishek Patel     std::optional<bool> notifyAction;
1479017faf2SAbhishek Patel     if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
1489017faf2SAbhishek Patel     {
1499017faf2SAbhishek Patel         notifyAction = true;
1509017faf2SAbhishek Patel     }
1519017faf2SAbhishek Patel     else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
1529017faf2SAbhishek Patel     {
1539017faf2SAbhishek Patel         notifyAction = false;
1549017faf2SAbhishek Patel     }
1559017faf2SAbhishek Patel 
1569017faf2SAbhishek Patel     return notifyAction;
1579017faf2SAbhishek Patel }
1589017faf2SAbhishek Patel 
1597e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
16026ccae32SEd Tanous                                      std::string_view field,
16139e77504SEd Tanous                                      std::string_view& contents)
16216428a1aSJason M. Bills {
16316428a1aSJason M. Bills     const char* data = nullptr;
16416428a1aSJason M. Bills     size_t length = 0;
16516428a1aSJason M. Bills     int ret = 0;
16616428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16746ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
16846ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
16946ff87baSEd Tanous 
17046ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
17116428a1aSJason M. Bills     if (ret < 0)
17216428a1aSJason M. Bills     {
17316428a1aSJason M. Bills         return ret;
17416428a1aSJason M. Bills     }
17539e77504SEd Tanous     contents = std::string_view(data, length);
17616428a1aSJason M. Bills     // Only use the content after the "=" character.
17781ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
17816428a1aSJason M. Bills     return ret;
17916428a1aSJason M. Bills }
18016428a1aSJason M. Bills 
1817e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
18226ccae32SEd Tanous                                      std::string_view field, const int& base,
18326ccae32SEd Tanous                                      long int& contents)
18416428a1aSJason M. Bills {
18516428a1aSJason M. Bills     int ret = 0;
18639e77504SEd Tanous     std::string_view metadata;
18716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
18816428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
18916428a1aSJason M. Bills     if (ret < 0)
19016428a1aSJason M. Bills     {
19116428a1aSJason M. Bills         return ret;
19216428a1aSJason M. Bills     }
193b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
19416428a1aSJason M. Bills     return ret;
19516428a1aSJason M. Bills }
19616428a1aSJason M. Bills 
1977e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1987e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
199a3316fc6SZhikuiRen {
200a3316fc6SZhikuiRen     int ret = 0;
201a3316fc6SZhikuiRen     uint64_t timestamp = 0;
202a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
203a3316fc6SZhikuiRen     if (ret < 0)
204a3316fc6SZhikuiRen     {
205a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
206a3316fc6SZhikuiRen                          << strerror(-ret);
207a3316fc6SZhikuiRen         return false;
208a3316fc6SZhikuiRen     }
209e645c5e6SKonstantin Aladyshev     entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
2109c620e21SAsmitha Karunanithi     return true;
211a3316fc6SZhikuiRen }
21250b8a43aSEd Tanous 
2137e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
214e85d6b16SJason M. Bills                                     const bool firstEntry = true)
21516428a1aSJason M. Bills {
21616428a1aSJason M. Bills     int ret = 0;
21716428a1aSJason M. Bills     static uint64_t prevTs = 0;
21816428a1aSJason M. Bills     static int index = 0;
219e85d6b16SJason M. Bills     if (firstEntry)
220e85d6b16SJason M. Bills     {
221e85d6b16SJason M. Bills         prevTs = 0;
222e85d6b16SJason M. Bills     }
223e85d6b16SJason M. Bills 
22416428a1aSJason M. Bills     // Get the entry timestamp
22516428a1aSJason M. Bills     uint64_t curTs = 0;
22616428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
22716428a1aSJason M. Bills     if (ret < 0)
22816428a1aSJason M. Bills     {
22916428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
23016428a1aSJason M. Bills                          << strerror(-ret);
23116428a1aSJason M. Bills         return false;
23216428a1aSJason M. Bills     }
23316428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
23416428a1aSJason M. Bills     if (curTs == prevTs)
23516428a1aSJason M. Bills     {
23616428a1aSJason M. Bills         index++;
23716428a1aSJason M. Bills     }
23816428a1aSJason M. Bills     else
23916428a1aSJason M. Bills     {
24016428a1aSJason M. Bills         // Otherwise, reset it
24116428a1aSJason M. Bills         index = 0;
24216428a1aSJason M. Bills     }
24316428a1aSJason M. Bills     // Save the timestamp
24416428a1aSJason M. Bills     prevTs = curTs;
24516428a1aSJason M. Bills 
24616428a1aSJason M. Bills     entryID = std::to_string(curTs);
24716428a1aSJason M. Bills     if (index > 0)
24816428a1aSJason M. Bills     {
24916428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
25016428a1aSJason M. Bills     }
25116428a1aSJason M. Bills     return true;
25216428a1aSJason M. Bills }
25316428a1aSJason M. Bills 
254e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
255e85d6b16SJason M. Bills                              const bool firstEntry = true)
25695820184SJason M. Bills {
257271584abSEd Tanous     static time_t prevTs = 0;
25895820184SJason M. Bills     static int index = 0;
259e85d6b16SJason M. Bills     if (firstEntry)
260e85d6b16SJason M. Bills     {
261e85d6b16SJason M. Bills         prevTs = 0;
262e85d6b16SJason M. Bills     }
263e85d6b16SJason M. Bills 
26495820184SJason M. Bills     // Get the entry timestamp
265271584abSEd Tanous     std::time_t curTs = 0;
26695820184SJason M. Bills     std::tm timeStruct = {};
26795820184SJason M. Bills     std::istringstream entryStream(logEntry);
26895820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
26995820184SJason M. Bills     {
27095820184SJason M. Bills         curTs = std::mktime(&timeStruct);
27195820184SJason M. Bills     }
27295820184SJason M. Bills     // If the timestamp isn't unique, increment the index
27395820184SJason M. Bills     if (curTs == prevTs)
27495820184SJason M. Bills     {
27595820184SJason M. Bills         index++;
27695820184SJason M. Bills     }
27795820184SJason M. Bills     else
27895820184SJason M. Bills     {
27995820184SJason M. Bills         // Otherwise, reset it
28095820184SJason M. Bills         index = 0;
28195820184SJason M. Bills     }
28295820184SJason M. Bills     // Save the timestamp
28395820184SJason M. Bills     prevTs = curTs;
28495820184SJason M. Bills 
28595820184SJason M. Bills     entryID = std::to_string(curTs);
28695820184SJason M. Bills     if (index > 0)
28795820184SJason M. Bills     {
28895820184SJason M. Bills         entryID += "_" + std::to_string(index);
28995820184SJason M. Bills     }
29095820184SJason M. Bills     return true;
29195820184SJason M. Bills }
29295820184SJason M. Bills 
2937e860f15SJohn Edward Broadbent inline static bool
2948d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2958d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2968d1b46d7Szhanghch05                        uint64_t& index)
29716428a1aSJason M. Bills {
29816428a1aSJason M. Bills     if (entryID.empty())
29916428a1aSJason M. Bills     {
30016428a1aSJason M. Bills         return false;
30116428a1aSJason M. Bills     }
30216428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
30339e77504SEd Tanous     std::string_view tsStr(entryID);
30416428a1aSJason M. Bills 
30581ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
30671d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
30716428a1aSJason M. Bills     {
30816428a1aSJason M. Bills         // Timestamp has an index
30916428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
31039e77504SEd Tanous         std::string_view indexStr(entryID);
31116428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
312c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
313c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
314c0bd5e4bSEd Tanous         if (ec != std::errc())
31516428a1aSJason M. Bills         {
3169db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
31716428a1aSJason M. Bills             return false;
31816428a1aSJason M. Bills         }
31916428a1aSJason M. Bills     }
32016428a1aSJason M. Bills     // Timestamp has no index
321c0bd5e4bSEd Tanous     auto [ptr, ec] =
322c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
323c0bd5e4bSEd Tanous     if (ec != std::errc())
32416428a1aSJason M. Bills     {
3259db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
32616428a1aSJason M. Bills         return false;
32716428a1aSJason M. Bills     }
32816428a1aSJason M. Bills     return true;
32916428a1aSJason M. Bills }
33016428a1aSJason M. Bills 
33195820184SJason M. Bills static bool
33295820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
33395820184SJason M. Bills {
33495820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
33595820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
33695820184SJason M. Bills 
33795820184SJason M. Bills     // Loop through the directory looking for redfish log files
33895820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
33995820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
34095820184SJason M. Bills     {
34195820184SJason M. Bills         // If we find a redfish log file, save the path
34295820184SJason M. Bills         std::string filename = dirEnt.path().filename();
34311ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
34495820184SJason M. Bills         {
34595820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
34695820184SJason M. Bills         }
34795820184SJason M. Bills     }
34895820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
34995820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
35095820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
35195820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
35295820184SJason M. Bills 
35395820184SJason M. Bills     return !redfishLogFiles.empty();
35495820184SJason M. Bills }
35595820184SJason M. Bills 
356aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
3572d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
358c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
359aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
360aefe3786SClaire Weinan {
361aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
362aefe3786SClaire Weinan     {
363aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
364aefe3786SClaire Weinan         {
365aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
366aefe3786SClaire Weinan             {
367aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
368aefe3786SClaire Weinan                 {
369aefe3786SClaire Weinan                     const auto* status =
370aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
371aefe3786SClaire Weinan                     if (status == nullptr)
372aefe3786SClaire Weinan                     {
373aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
374aefe3786SClaire Weinan                         break;
375aefe3786SClaire Weinan                     }
376aefe3786SClaire Weinan                     dumpStatus = *status;
377aefe3786SClaire Weinan                 }
378aefe3786SClaire Weinan             }
379aefe3786SClaire Weinan         }
380aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
381aefe3786SClaire Weinan         {
382aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
383aefe3786SClaire Weinan             {
384aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
385aefe3786SClaire Weinan                 {
386aefe3786SClaire Weinan                     const auto* sizePtr =
387aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
388aefe3786SClaire Weinan                     if (sizePtr == nullptr)
389aefe3786SClaire Weinan                     {
390aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
391aefe3786SClaire Weinan                         break;
392aefe3786SClaire Weinan                     }
393aefe3786SClaire Weinan                     size = *sizePtr;
394aefe3786SClaire Weinan                     break;
395aefe3786SClaire Weinan                 }
396aefe3786SClaire Weinan             }
397aefe3786SClaire Weinan         }
398aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
399aefe3786SClaire Weinan         {
400aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
401aefe3786SClaire Weinan             {
402aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
403aefe3786SClaire Weinan                 {
404aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
405aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
406aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
407aefe3786SClaire Weinan                     {
408aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
409aefe3786SClaire Weinan                         break;
410aefe3786SClaire Weinan                     }
411c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
412aefe3786SClaire Weinan                     break;
413aefe3786SClaire Weinan                 }
414aefe3786SClaire Weinan             }
415aefe3786SClaire Weinan         }
416aefe3786SClaire Weinan     }
417aefe3786SClaire Weinan }
418aefe3786SClaire Weinan 
41921ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
420fdd26906SClaire Weinan {
421fdd26906SClaire Weinan     std::string entriesPath;
422fdd26906SClaire Weinan 
423fdd26906SClaire Weinan     if (dumpType == "BMC")
424fdd26906SClaire Weinan     {
425fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
426fdd26906SClaire Weinan     }
427fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
428fdd26906SClaire Weinan     {
429fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
430fdd26906SClaire Weinan     }
431fdd26906SClaire Weinan     else if (dumpType == "System")
432fdd26906SClaire Weinan     {
433fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
434fdd26906SClaire Weinan     }
435fdd26906SClaire Weinan     else
436fdd26906SClaire Weinan     {
437fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
438fdd26906SClaire Weinan                          << dumpType;
439fdd26906SClaire Weinan     }
440fdd26906SClaire Weinan 
441fdd26906SClaire Weinan     // Returns empty string on error
442fdd26906SClaire Weinan     return entriesPath;
443fdd26906SClaire Weinan }
444fdd26906SClaire Weinan 
4458d1b46d7Szhanghch05 inline void
4468d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4475cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4485cb1dd27SAsmitha Karunanithi {
449fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
450fdd26906SClaire Weinan     if (entriesPath.empty())
4515cb1dd27SAsmitha Karunanithi     {
4525cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4535cb1dd27SAsmitha Karunanithi         return;
4545cb1dd27SAsmitha Karunanithi     }
4555cb1dd27SAsmitha Karunanithi 
4565cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
457fdd26906SClaire Weinan         [asyncResp, entriesPath,
458*5e7e2dc5SEd Tanous          dumpType](const boost::system::error_code& ec,
459711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4605cb1dd27SAsmitha Karunanithi         if (ec)
4615cb1dd27SAsmitha Karunanithi         {
4625cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4635cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4645cb1dd27SAsmitha Karunanithi             return;
4655cb1dd27SAsmitha Karunanithi         }
4665cb1dd27SAsmitha Karunanithi 
467fdd26906SClaire Weinan         // Remove ending slash
468fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
469fdd26906SClaire Weinan         if (!odataIdStr.empty())
470fdd26906SClaire Weinan         {
471fdd26906SClaire Weinan             odataIdStr.pop_back();
472fdd26906SClaire Weinan         }
473fdd26906SClaire Weinan 
474fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
475fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
476fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
477fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
478fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
479fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
480fdd26906SClaire Weinan 
4815cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4825cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
483b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
484b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
485002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
4865cb1dd27SAsmitha Karunanithi 
487002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
488002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
489002d39b4SEd Tanous                                                r.first.filename());
490565dfb6fSClaire Weinan         });
491565dfb6fSClaire Weinan 
4925cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4935cb1dd27SAsmitha Karunanithi         {
494b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4955cb1dd27SAsmitha Karunanithi             {
4965cb1dd27SAsmitha Karunanithi                 continue;
4975cb1dd27SAsmitha Karunanithi             }
498c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
4995cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
50035440d18SAsmitha Karunanithi             std::string dumpStatus;
501433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
5022dfd18efSEd Tanous 
5032dfd18efSEd Tanous             std::string entryID = object.first.filename();
5042dfd18efSEd Tanous             if (entryID.empty())
5055cb1dd27SAsmitha Karunanithi             {
5065cb1dd27SAsmitha Karunanithi                 continue;
5075cb1dd27SAsmitha Karunanithi             }
5085cb1dd27SAsmitha Karunanithi 
509c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
510aefe3786SClaire Weinan                                          asyncResp);
5115cb1dd27SAsmitha Karunanithi 
5120fda0f12SGeorge Liu             if (dumpStatus !=
5130fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
51435440d18SAsmitha Karunanithi                 !dumpStatus.empty())
51535440d18SAsmitha Karunanithi             {
51635440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
51735440d18SAsmitha Karunanithi                 continue;
51835440d18SAsmitha Karunanithi             }
51935440d18SAsmitha Karunanithi 
5209c11a172SVijay Lobo             thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
521fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
5225cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
5235cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
5245cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
525bbd80db8SClaire Weinan             thisEntry["Created"] =
526bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5275cb1dd27SAsmitha Karunanithi 
5285cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5295cb1dd27SAsmitha Karunanithi             {
530d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
531d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
532fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
533fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5345cb1dd27SAsmitha Karunanithi             }
5355cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5365cb1dd27SAsmitha Karunanithi             {
537d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
538d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
539d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
540fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
541fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5425cb1dd27SAsmitha Karunanithi             }
54335440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5445cb1dd27SAsmitha Karunanithi         }
545002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5465cb1dd27SAsmitha Karunanithi         },
5475cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5485cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5495cb1dd27SAsmitha Karunanithi }
5505cb1dd27SAsmitha Karunanithi 
5518d1b46d7Szhanghch05 inline void
552c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5538d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5545cb1dd27SAsmitha Karunanithi {
555fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
556fdd26906SClaire Weinan     if (entriesPath.empty())
5575cb1dd27SAsmitha Karunanithi     {
5585cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5595cb1dd27SAsmitha Karunanithi         return;
5605cb1dd27SAsmitha Karunanithi     }
5615cb1dd27SAsmitha Karunanithi 
5625cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
563fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
564*5e7e2dc5SEd Tanous          entriesPath](const boost::system::error_code& ec,
56502cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5665cb1dd27SAsmitha Karunanithi         if (ec)
5675cb1dd27SAsmitha Karunanithi         {
5685cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5695cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5705cb1dd27SAsmitha Karunanithi             return;
5715cb1dd27SAsmitha Karunanithi         }
5725cb1dd27SAsmitha Karunanithi 
573b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
574b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
575b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
576002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
577b47452b2SAsmitha Karunanithi 
5789eb808c1SEd Tanous         for (const auto& objectPath : resp)
5795cb1dd27SAsmitha Karunanithi         {
580b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5815cb1dd27SAsmitha Karunanithi             {
5825cb1dd27SAsmitha Karunanithi                 continue;
5835cb1dd27SAsmitha Karunanithi             }
5845cb1dd27SAsmitha Karunanithi 
5855cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
586c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5875cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
58835440d18SAsmitha Karunanithi             std::string dumpStatus;
5895cb1dd27SAsmitha Karunanithi 
590aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
591c6fecdabSClaire Weinan                                          timestampUs, asyncResp);
5925cb1dd27SAsmitha Karunanithi 
5930fda0f12SGeorge Liu             if (dumpStatus !=
5940fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
59535440d18SAsmitha Karunanithi                 !dumpStatus.empty())
59635440d18SAsmitha Karunanithi             {
59735440d18SAsmitha Karunanithi                 // Dump status is not Complete
59835440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
599d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
600d1bde9e5SKrzysztof Grobelny                                            entryID);
60135440d18SAsmitha Karunanithi                 return;
60235440d18SAsmitha Karunanithi             }
60335440d18SAsmitha Karunanithi 
6045cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
6059c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
606fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
6075cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
6085cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
6095cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
610bbd80db8SClaire Weinan             asyncResp->res.jsonValue["Created"] =
611bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
6125cb1dd27SAsmitha Karunanithi 
6135cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
6145cb1dd27SAsmitha Karunanithi             {
615d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
616d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
617fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
618fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6195cb1dd27SAsmitha Karunanithi             }
6205cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6215cb1dd27SAsmitha Karunanithi             {
622d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
623002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
624d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
625fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
626fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6275cb1dd27SAsmitha Karunanithi             }
6285cb1dd27SAsmitha Karunanithi         }
629e05aec50SEd Tanous         if (!foundDumpEntry)
630b47452b2SAsmitha Karunanithi         {
631b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
632b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
633b47452b2SAsmitha Karunanithi             return;
634b47452b2SAsmitha Karunanithi         }
6355cb1dd27SAsmitha Karunanithi         },
6365cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6375cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6385cb1dd27SAsmitha Karunanithi }
6395cb1dd27SAsmitha Karunanithi 
6408d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6419878256fSStanley Chu                             const std::string& entryID,
642b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6435cb1dd27SAsmitha Karunanithi {
644002d39b4SEd Tanous     auto respHandler =
645*5e7e2dc5SEd Tanous         [asyncResp, entryID](const boost::system::error_code& ec) {
6465cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6475cb1dd27SAsmitha Karunanithi         if (ec)
6485cb1dd27SAsmitha Karunanithi         {
6493de8d8baSGeorge Liu             if (ec.value() == EBADR)
6503de8d8baSGeorge Liu             {
6513de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6523de8d8baSGeorge Liu                 return;
6533de8d8baSGeorge Liu             }
6545cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
655fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6565cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6575cb1dd27SAsmitha Karunanithi             return;
6585cb1dd27SAsmitha Karunanithi         }
6595cb1dd27SAsmitha Karunanithi     };
6605cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6615cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
662b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
663b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
664b47452b2SAsmitha Karunanithi             entryID,
6655cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6665cb1dd27SAsmitha Karunanithi }
6675cb1dd27SAsmitha Karunanithi 
6688e31778eSAsmitha Karunanithi inline DumpCreationProgress
6698e31778eSAsmitha Karunanithi     mapDbusStatusToDumpProgress(const std::string& status)
670a43be80fSAsmitha Karunanithi {
6718e31778eSAsmitha Karunanithi     if (status ==
6728e31778eSAsmitha Karunanithi             "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
6738e31778eSAsmitha Karunanithi         status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
6748e31778eSAsmitha Karunanithi     {
6758e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_FAILED;
6768e31778eSAsmitha Karunanithi     }
6778e31778eSAsmitha Karunanithi     if (status ==
6788e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
6798e31778eSAsmitha Karunanithi     {
6808e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_SUCCESS;
6818e31778eSAsmitha Karunanithi     }
6828e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
6838e31778eSAsmitha Karunanithi }
6848e31778eSAsmitha Karunanithi 
6858e31778eSAsmitha Karunanithi inline DumpCreationProgress
6868e31778eSAsmitha Karunanithi     getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
6878e31778eSAsmitha Karunanithi {
6888e31778eSAsmitha Karunanithi     for (const auto& [key, val] : values)
6898e31778eSAsmitha Karunanithi     {
6908e31778eSAsmitha Karunanithi         if (key == "Status")
6918e31778eSAsmitha Karunanithi         {
6928e31778eSAsmitha Karunanithi             const std::string* value = std::get_if<std::string>(&val);
6938e31778eSAsmitha Karunanithi             if (value == nullptr)
6948e31778eSAsmitha Karunanithi             {
6958e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Status property value is null";
6968e31778eSAsmitha Karunanithi                 return DumpCreationProgress::DUMP_CREATE_FAILED;
6978e31778eSAsmitha Karunanithi             }
6988e31778eSAsmitha Karunanithi             return mapDbusStatusToDumpProgress(*value);
6998e31778eSAsmitha Karunanithi         }
7008e31778eSAsmitha Karunanithi     }
7018e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7028e31778eSAsmitha Karunanithi }
7038e31778eSAsmitha Karunanithi 
7048e31778eSAsmitha Karunanithi inline std::string getDumpEntryPath(const std::string& dumpPath)
7058e31778eSAsmitha Karunanithi {
7068e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
7078e31778eSAsmitha Karunanithi     {
7088e31778eSAsmitha Karunanithi         return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
7098e31778eSAsmitha Karunanithi     }
7108e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
7118e31778eSAsmitha Karunanithi     {
7128e31778eSAsmitha Karunanithi         return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
7138e31778eSAsmitha Karunanithi     }
7148e31778eSAsmitha Karunanithi     return "";
7158e31778eSAsmitha Karunanithi }
7168e31778eSAsmitha Karunanithi 
7178e31778eSAsmitha Karunanithi inline void createDumpTaskCallback(
7188e31778eSAsmitha Karunanithi     task::Payload&& payload,
7198e31778eSAsmitha Karunanithi     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7208e31778eSAsmitha Karunanithi     const sdbusplus::message::object_path& createdObjPath)
7218e31778eSAsmitha Karunanithi {
7228e31778eSAsmitha Karunanithi     const std::string dumpPath = createdObjPath.parent_path().str;
7238e31778eSAsmitha Karunanithi     const std::string dumpId = createdObjPath.filename();
7248e31778eSAsmitha Karunanithi 
7258e31778eSAsmitha Karunanithi     std::string dumpEntryPath = getDumpEntryPath(dumpPath);
7268e31778eSAsmitha Karunanithi 
7278e31778eSAsmitha Karunanithi     if (dumpEntryPath.empty())
7288e31778eSAsmitha Karunanithi     {
7298e31778eSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type received";
7308e31778eSAsmitha Karunanithi         messages::internalError(asyncResp->res);
7318e31778eSAsmitha Karunanithi         return;
7328e31778eSAsmitha Karunanithi     }
7338e31778eSAsmitha Karunanithi 
7348e31778eSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7358e31778eSAsmitha Karunanithi         [asyncResp, payload, createdObjPath,
7368e31778eSAsmitha Karunanithi          dumpEntryPath{std::move(dumpEntryPath)},
737*5e7e2dc5SEd Tanous          dumpId](const boost::system::error_code& ec,
7388e31778eSAsmitha Karunanithi                  const std::string& introspectXml) {
7398e31778eSAsmitha Karunanithi         if (ec)
7408e31778eSAsmitha Karunanithi         {
7418e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Introspect call failed with error: "
7428e31778eSAsmitha Karunanithi                              << ec.message();
7438e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7448e31778eSAsmitha Karunanithi             return;
7458e31778eSAsmitha Karunanithi         }
7468e31778eSAsmitha Karunanithi 
7478e31778eSAsmitha Karunanithi         // Check if the created dump object has implemented Progress
7488e31778eSAsmitha Karunanithi         // interface to track dump completion. If yes, fetch the "Status"
7498e31778eSAsmitha Karunanithi         // property of the interface, modify the task state accordingly.
7508e31778eSAsmitha Karunanithi         // Else, return task completed.
7518e31778eSAsmitha Karunanithi         tinyxml2::XMLDocument doc;
7528e31778eSAsmitha Karunanithi 
7538e31778eSAsmitha Karunanithi         doc.Parse(introspectXml.data(), introspectXml.size());
7548e31778eSAsmitha Karunanithi         tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
7558e31778eSAsmitha Karunanithi         if (pRoot == nullptr)
7568e31778eSAsmitha Karunanithi         {
7578e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "XML document failed to parse";
7588e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7598e31778eSAsmitha Karunanithi             return;
7608e31778eSAsmitha Karunanithi         }
7618e31778eSAsmitha Karunanithi         tinyxml2::XMLElement* interfaceNode =
7628e31778eSAsmitha Karunanithi             pRoot->FirstChildElement("interface");
7638e31778eSAsmitha Karunanithi 
7648e31778eSAsmitha Karunanithi         bool isProgressIntfPresent = false;
7658e31778eSAsmitha Karunanithi         while (interfaceNode != nullptr)
7668e31778eSAsmitha Karunanithi         {
7678e31778eSAsmitha Karunanithi             const char* thisInterfaceName = interfaceNode->Attribute("name");
7688e31778eSAsmitha Karunanithi             if (thisInterfaceName != nullptr)
7698e31778eSAsmitha Karunanithi             {
7708e31778eSAsmitha Karunanithi                 if (thisInterfaceName ==
7718e31778eSAsmitha Karunanithi                     std::string_view("xyz.openbmc_project.Common.Progress"))
7728e31778eSAsmitha Karunanithi                 {
7738e31778eSAsmitha Karunanithi                     interfaceNode =
7748e31778eSAsmitha Karunanithi                         interfaceNode->NextSiblingElement("interface");
7758e31778eSAsmitha Karunanithi                     continue;
7768e31778eSAsmitha Karunanithi                 }
7778e31778eSAsmitha Karunanithi                 isProgressIntfPresent = true;
7788e31778eSAsmitha Karunanithi                 break;
7798e31778eSAsmitha Karunanithi             }
7808e31778eSAsmitha Karunanithi             interfaceNode = interfaceNode->NextSiblingElement("interface");
7818e31778eSAsmitha Karunanithi         }
7828e31778eSAsmitha Karunanithi 
783a43be80fSAsmitha Karunanithi         std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7848e31778eSAsmitha Karunanithi             [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
785*5e7e2dc5SEd Tanous                 const boost::system::error_code& err, sdbusplus::message_t& msg,
786a43be80fSAsmitha Karunanithi                 const std::shared_ptr<task::TaskData>& taskData) {
787cb13a392SEd Tanous             if (err)
788cb13a392SEd Tanous             {
7898e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << createdObjPath.str
7908e31778eSAsmitha Karunanithi                                  << ": Error in creating dump";
7918e31778eSAsmitha Karunanithi                 taskData->messages.emplace_back(messages::internalError());
7926145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7936145ed6fSAsmitha Karunanithi                 return task::completed;
794cb13a392SEd Tanous             }
795b9d36b47SEd Tanous 
7968e31778eSAsmitha Karunanithi             if (isProgressIntfPresent)
797a43be80fSAsmitha Karunanithi             {
7988e31778eSAsmitha Karunanithi                 dbus::utility::DBusPropertiesMap values;
7998e31778eSAsmitha Karunanithi                 std::string prop;
8008e31778eSAsmitha Karunanithi                 msg.read(prop, values);
8018e31778eSAsmitha Karunanithi 
8028e31778eSAsmitha Karunanithi                 DumpCreationProgress dumpStatus =
8038e31778eSAsmitha Karunanithi                     getDumpCompletionStatus(values);
8048e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
8058e31778eSAsmitha Karunanithi                 {
8068e31778eSAsmitha Karunanithi                     BMCWEB_LOG_ERROR << createdObjPath.str
8078e31778eSAsmitha Karunanithi                                      << ": Error in creating dump";
8088e31778eSAsmitha Karunanithi                     taskData->state = "Cancelled";
8098e31778eSAsmitha Karunanithi                     return task::completed;
8108e31778eSAsmitha Karunanithi                 }
8118e31778eSAsmitha Karunanithi 
8128e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
8138e31778eSAsmitha Karunanithi                 {
8148e31778eSAsmitha Karunanithi                     BMCWEB_LOG_DEBUG << createdObjPath.str
8158e31778eSAsmitha Karunanithi                                      << ": Dump creation task is in progress";
8168e31778eSAsmitha Karunanithi                     return !task::completed;
8178e31778eSAsmitha Karunanithi                 }
8188e31778eSAsmitha Karunanithi             }
8198e31778eSAsmitha Karunanithi 
820a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
821a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
822a43be80fSAsmitha Karunanithi 
823a43be80fSAsmitha Karunanithi             std::string headerLoc =
8248e31778eSAsmitha Karunanithi                 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
825002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
826a43be80fSAsmitha Karunanithi 
8278e31778eSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << createdObjPath.str
8288e31778eSAsmitha Karunanithi                              << ": Dump creation task completed";
829a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
830b47452b2SAsmitha Karunanithi             return task::completed;
831a43be80fSAsmitha Karunanithi             },
8328e31778eSAsmitha Karunanithi             "type='signal',interface='org.freedesktop.DBus.Properties',"
8338e31778eSAsmitha Karunanithi             "member='PropertiesChanged',path='" +
8348e31778eSAsmitha Karunanithi                 createdObjPath.str + "'");
835a43be80fSAsmitha Karunanithi 
8368e31778eSAsmitha Karunanithi         // The task timer is set to max time limit within which the
8378e31778eSAsmitha Karunanithi         // requested dump will be collected.
8388e31778eSAsmitha Karunanithi         task->startTimer(std::chrono::minutes(6));
839a43be80fSAsmitha Karunanithi         task->populateResp(asyncResp->res);
8408e31778eSAsmitha Karunanithi         task->payload.emplace(payload);
8418e31778eSAsmitha Karunanithi         },
8428e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", createdObjPath,
8438e31778eSAsmitha Karunanithi         "org.freedesktop.DBus.Introspectable", "Introspect");
844a43be80fSAsmitha Karunanithi }
845a43be80fSAsmitha Karunanithi 
8468d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8478d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
848a43be80fSAsmitha Karunanithi {
849fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
850fdd26906SClaire Weinan     if (dumpPath.empty())
851a43be80fSAsmitha Karunanithi     {
852a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
853a43be80fSAsmitha Karunanithi         return;
854a43be80fSAsmitha Karunanithi     }
855a43be80fSAsmitha Karunanithi 
856a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
857a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
858a43be80fSAsmitha Karunanithi 
85915ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
860a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
861a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
862a43be80fSAsmitha Karunanithi     {
863a43be80fSAsmitha Karunanithi         return;
864a43be80fSAsmitha Karunanithi     }
865a43be80fSAsmitha Karunanithi 
866a43be80fSAsmitha Karunanithi     if (dumpType == "System")
867a43be80fSAsmitha Karunanithi     {
868a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
869a43be80fSAsmitha Karunanithi         {
8704978b63fSJason M. Bills             BMCWEB_LOG_ERROR
8714978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
872a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
873a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
874a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
875a43be80fSAsmitha Karunanithi             return;
876a43be80fSAsmitha Karunanithi         }
8773174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
878a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
879a43be80fSAsmitha Karunanithi         {
880a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
881ace85d60SEd Tanous             messages::internalError(asyncResp->res);
882a43be80fSAsmitha Karunanithi             return;
883a43be80fSAsmitha Karunanithi         }
8845907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
885a43be80fSAsmitha Karunanithi     }
886a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
887a43be80fSAsmitha Karunanithi     {
888a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
889a43be80fSAsmitha Karunanithi         {
8900fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8910fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
892a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
893a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
894a43be80fSAsmitha Karunanithi             return;
895a43be80fSAsmitha Karunanithi         }
8963174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
897a43be80fSAsmitha Karunanithi         {
898a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
899a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
900ace85d60SEd Tanous             messages::internalError(asyncResp->res);
901a43be80fSAsmitha Karunanithi             return;
902a43be80fSAsmitha Karunanithi         }
9035907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
9045907571dSAsmitha Karunanithi     }
9055907571dSAsmitha Karunanithi     else
9065907571dSAsmitha Karunanithi     {
9075907571dSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
9085907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
9095907571dSAsmitha Karunanithi         return;
910a43be80fSAsmitha Karunanithi     }
911a43be80fSAsmitha Karunanithi 
9128e31778eSAsmitha Karunanithi     std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
9138e31778eSAsmitha Karunanithi         createDumpParamVec;
9148e31778eSAsmitha Karunanithi 
915a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
916*5e7e2dc5SEd Tanous         [asyncResp, payload(task::Payload(req)),
917*5e7e2dc5SEd Tanous          dumpPath](const boost::system::error_code& ec,
918*5e7e2dc5SEd Tanous                    const sdbusplus::message_t& msg,
9198e31778eSAsmitha Karunanithi                    const sdbusplus::message::object_path& objPath) mutable {
920a43be80fSAsmitha Karunanithi         if (ec)
921a43be80fSAsmitha Karunanithi         {
922a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
9235907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
9245907571dSAsmitha Karunanithi             if (dbusError == nullptr)
9255907571dSAsmitha Karunanithi             {
9265907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
9275907571dSAsmitha Karunanithi                 return;
9285907571dSAsmitha Karunanithi             }
9295907571dSAsmitha Karunanithi 
9305907571dSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
9315907571dSAsmitha Karunanithi                              << " and error msg: " << dbusError->message;
9325907571dSAsmitha Karunanithi             if (std::string_view(
9335907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
9345907571dSAsmitha Karunanithi                 dbusError->name)
9355907571dSAsmitha Karunanithi             {
9365907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
9375907571dSAsmitha Karunanithi                 return;
9385907571dSAsmitha Karunanithi             }
9395907571dSAsmitha Karunanithi             if (std::string_view(
9405907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
9415907571dSAsmitha Karunanithi                 dbusError->name)
9425907571dSAsmitha Karunanithi             {
9435907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
9445907571dSAsmitha Karunanithi                 return;
9455907571dSAsmitha Karunanithi             }
9465907571dSAsmitha Karunanithi             if (std::string_view(
9475907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
9485907571dSAsmitha Karunanithi                 dbusError->name)
9495907571dSAsmitha Karunanithi             {
9505907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
9515907571dSAsmitha Karunanithi                 return;
9525907571dSAsmitha Karunanithi             }
9535907571dSAsmitha Karunanithi             // Other Dbus errors such as:
9545907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
9555907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
9565907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
9575907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
9585907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
9595907571dSAsmitha Karunanithi             // back to the client.
960a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
961a43be80fSAsmitha Karunanithi             return;
962a43be80fSAsmitha Karunanithi         }
9638e31778eSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
9648e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
965a43be80fSAsmitha Karunanithi         },
966b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
967b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
968b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
9698e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
970a43be80fSAsmitha Karunanithi }
971a43be80fSAsmitha Karunanithi 
9728d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9738d1b46d7Szhanghch05                       const std::string& dumpType)
97480319af1SAsmitha Karunanithi {
975b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
976b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
9778d1b46d7Szhanghch05 
9780d946211SClaire Weinan     crow::connections::systemBus->async_method_call(
9790d946211SClaire Weinan         [asyncResp](const boost::system::error_code& ec) {
98080319af1SAsmitha Karunanithi         if (ec)
98180319af1SAsmitha Karunanithi         {
9820d946211SClaire Weinan             BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
98380319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
98480319af1SAsmitha Karunanithi             return;
98580319af1SAsmitha Karunanithi         }
9860d946211SClaire Weinan         },
9870d946211SClaire Weinan         "xyz.openbmc_project.Dump.Manager",
9880d946211SClaire Weinan         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
9890d946211SClaire Weinan         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
99080319af1SAsmitha Karunanithi }
99180319af1SAsmitha Karunanithi 
992b9d36b47SEd Tanous inline static void
993b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
994b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
995b9d36b47SEd Tanous                              std::string& logfile)
996043a0536SJohnathan Mantey {
997d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
998d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
999d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
1000d1bde9e5SKrzysztof Grobelny 
1001d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1002d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1003d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1004d1bde9e5SKrzysztof Grobelny 
1005d1bde9e5SKrzysztof Grobelny     if (!success)
1006043a0536SJohnathan Mantey     {
1007d1bde9e5SKrzysztof Grobelny         return;
1008043a0536SJohnathan Mantey     }
1009d1bde9e5SKrzysztof Grobelny 
1010d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1011043a0536SJohnathan Mantey     {
1012d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1013d1bde9e5SKrzysztof Grobelny     }
1014d1bde9e5SKrzysztof Grobelny 
1015d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1016043a0536SJohnathan Mantey     {
1017d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1018043a0536SJohnathan Mantey     }
1019d1bde9e5SKrzysztof Grobelny 
1020d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1021043a0536SJohnathan Mantey     {
1022d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1023043a0536SJohnathan Mantey     }
1024043a0536SJohnathan Mantey }
1025043a0536SJohnathan Mantey 
10267e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
10271da66f75SEd Tanous {
1028c4bf6374SJason M. Bills     /**
1029c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1030c4bf6374SJason M. Bills      */
103122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1032ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1033002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1034002d39b4SEd Tanous             [&app](const crow::Request& req,
103522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
103622d268cbSEd Tanous                    const std::string& systemName) {
10373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1038c4bf6374SJason M. Bills         {
103945ca1b86SEd Tanous             return;
104045ca1b86SEd Tanous         }
104122d268cbSEd Tanous         if (systemName != "system")
104222d268cbSEd Tanous         {
104322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
104422d268cbSEd Tanous                                        systemName);
104522d268cbSEd Tanous             return;
104622d268cbSEd Tanous         }
104722d268cbSEd Tanous 
10487e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
10497e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1050c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1051c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1052c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1053029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
105445ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1055c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1056c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1057002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1058c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
10591476687dSEd Tanous         nlohmann::json::object_t eventLog;
10601476687dSEd Tanous         eventLog["@odata.id"] =
10611476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
10621476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
10635cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
10641476687dSEd Tanous         nlohmann::json::object_t dumpLog;
1065002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
10661476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
1067c9bb6861Sraviteja-b #endif
1068c9bb6861Sraviteja-b 
1069d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
10701476687dSEd Tanous         nlohmann::json::object_t crashdump;
10711476687dSEd Tanous         crashdump["@odata.id"] =
10721476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
10731476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
1074d53dd41fSJason M. Bills #endif
1075b7028ebfSSpencer Ku 
1076b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
10771476687dSEd Tanous         nlohmann::json::object_t hostlogger;
10781476687dSEd Tanous         hostlogger["@odata.id"] =
10791476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
10801476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
1081b7028ebfSSpencer Ku #endif
1082c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1083c4bf6374SJason M. Bills             logServiceArray.size();
1084a3316fc6SZhikuiRen 
10857a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
10867a1dbc48SGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode"};
10877a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
10887a1dbc48SGeorge Liu             "/", 0, interfaces,
10897a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
1090b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1091b9d36b47SEd Tanous                             subtreePath) {
1092a3316fc6SZhikuiRen             if (ec)
1093a3316fc6SZhikuiRen             {
1094a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
1095a3316fc6SZhikuiRen                 return;
1096a3316fc6SZhikuiRen             }
1097a3316fc6SZhikuiRen 
109855f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1099a3316fc6SZhikuiRen             {
1100a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1101a3316fc6SZhikuiRen                 {
110223a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1103a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1104613dabeaSEd Tanous                     nlohmann::json::object_t member;
1105613dabeaSEd Tanous                     member["@odata.id"] =
1106613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
1107613dabeaSEd Tanous 
1108613dabeaSEd Tanous                     logServiceArrayLocal.push_back(std::move(member));
1109613dabeaSEd Tanous 
111045ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
111123a21a1cSEd Tanous                         logServiceArrayLocal.size();
1112a3316fc6SZhikuiRen                     return;
1113a3316fc6SZhikuiRen                 }
1114a3316fc6SZhikuiRen             }
11157a1dbc48SGeorge Liu             });
11167e860f15SJohn Edward Broadbent         });
1117c4bf6374SJason M. Bills }
1118c4bf6374SJason M. Bills 
11197e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1120c4bf6374SJason M. Bills {
112122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1122ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1123002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1124002d39b4SEd Tanous             [&app](const crow::Request& req,
112522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112622d268cbSEd Tanous                    const std::string& systemName) {
11273ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
112845ca1b86SEd Tanous         {
112945ca1b86SEd Tanous             return;
113045ca1b86SEd Tanous         }
113122d268cbSEd Tanous         if (systemName != "system")
113222d268cbSEd Tanous         {
113322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
113422d268cbSEd Tanous                                        systemName);
113522d268cbSEd Tanous             return;
113622d268cbSEd Tanous         }
1137c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1138029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1139c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1140c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1141c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1142002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1143c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1144c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
11457c8c4058STejas Patil 
11467c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
11472b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
11487c8c4058STejas Patil 
11497c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
11507c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
11517c8c4058STejas Patil             redfishDateTimeOffset.second;
11527c8c4058STejas Patil 
11531476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
11541476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1155e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1156e7d6c8b2SGunnar Mills 
11570fda0f12SGeorge Liu             {"target",
11580fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
11597e860f15SJohn Edward Broadbent         });
1160489640c6SJason M. Bills }
1161489640c6SJason M. Bills 
11627e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1163489640c6SJason M. Bills {
11644978b63fSJason M. Bills     BMCWEB_ROUTE(
11654978b63fSJason M. Bills         app,
116622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1167432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
11687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
116945ca1b86SEd Tanous             [&app](const crow::Request& req,
117022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117122d268cbSEd Tanous                    const std::string& systemName) {
11723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117345ca1b86SEd Tanous         {
117445ca1b86SEd Tanous             return;
117545ca1b86SEd Tanous         }
117622d268cbSEd Tanous         if (systemName != "system")
117722d268cbSEd Tanous         {
117822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
117922d268cbSEd Tanous                                        systemName);
118022d268cbSEd Tanous             return;
118122d268cbSEd Tanous         }
1182489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1183489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1184489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1185489640c6SJason M. Bills         {
1186489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1187489640c6SJason M. Bills             {
1188489640c6SJason M. Bills                 std::error_code ec;
1189489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1190489640c6SJason M. Bills             }
1191489640c6SJason M. Bills         }
1192489640c6SJason M. Bills 
1193489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1194489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1195*5e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
1196489640c6SJason M. Bills             if (ec)
1197489640c6SJason M. Bills             {
1198002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1199489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1200489640c6SJason M. Bills                 return;
1201489640c6SJason M. Bills             }
1202489640c6SJason M. Bills 
1203489640c6SJason M. Bills             messages::success(asyncResp->res);
1204489640c6SJason M. Bills             },
1205489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1206002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1207002d39b4SEd Tanous             "replace");
12087e860f15SJohn Edward Broadbent         });
1209c4bf6374SJason M. Bills }
1210c4bf6374SJason M. Bills 
1211ac992cdeSJason M. Bills enum class LogParseError
1212ac992cdeSJason M. Bills {
1213ac992cdeSJason M. Bills     success,
1214ac992cdeSJason M. Bills     parseFailed,
1215ac992cdeSJason M. Bills     messageIdNotInRegistry,
1216ac992cdeSJason M. Bills };
1217ac992cdeSJason M. Bills 
1218ac992cdeSJason M. Bills static LogParseError
1219ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1220b5a76932SEd Tanous                           const std::string& logEntry,
1221de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1222c4bf6374SJason M. Bills {
122395820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1224cd225da8SJason M. Bills     // First get the Timestamp
1225f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1226cd225da8SJason M. Bills     if (space == std::string::npos)
122795820184SJason M. Bills     {
1228ac992cdeSJason M. Bills         return LogParseError::parseFailed;
122995820184SJason M. Bills     }
1230cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1231cd225da8SJason M. Bills     // Then get the log contents
1232f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1233cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1234cd225da8SJason M. Bills     {
1235ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1236cd225da8SJason M. Bills     }
1237cd225da8SJason M. Bills     std::string_view entry(logEntry);
1238cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1239cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1240cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
124150ebd4afSEd Tanous     bmcweb::split(logEntryFields, entry, ',');
1242cd225da8SJason M. Bills     // We need at least a MessageId to be valid
124326f6976fSEd Tanous     if (logEntryFields.empty())
1244cd225da8SJason M. Bills     {
1245ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1246cd225da8SJason M. Bills     }
1247cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
124895820184SJason M. Bills 
12494851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1250fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1251c4bf6374SJason M. Bills 
125254417b02SSui Chen     if (message == nullptr)
1253c4bf6374SJason M. Bills     {
125454417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1255ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1256c4bf6374SJason M. Bills     }
1257c4bf6374SJason M. Bills 
125854417b02SSui Chen     std::string msg = message->message;
125954417b02SSui Chen 
126015a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
126126702d01SEd Tanous     std::span<std::string> messageArgs;
126215a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
126315a86ff6SJason M. Bills     {
126415a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
126515a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
126615a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
126715a86ff6SJason M. Bills         if (!messageArgsStart.empty())
126815a86ff6SJason M. Bills         {
126915a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
127015a86ff6SJason M. Bills         }
127115a86ff6SJason M. Bills 
127223a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1273c4bf6374SJason M. Bills 
12744851d45dSJason M. Bills         // Fill the MessageArgs into the Message
127595820184SJason M. Bills         int i = 0;
127695820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
12774851d45dSJason M. Bills         {
127895820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
12794851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
12804851d45dSJason M. Bills             if (argPos != std::string::npos)
12814851d45dSJason M. Bills             {
128295820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
12834851d45dSJason M. Bills             }
12844851d45dSJason M. Bills         }
128515a86ff6SJason M. Bills     }
12864851d45dSJason M. Bills 
128795820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
128895820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
128995820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1290f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1291f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
129295820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1293c4bf6374SJason M. Bills     {
129495820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1295c4bf6374SJason M. Bills     }
1296c4bf6374SJason M. Bills 
1297c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
12989c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1299eddfc437SWilly Tu     logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
1300eddfc437SWilly Tu         "redfish", "v1", "Systems", "system", "LogServices", "EventLog",
1301eddfc437SWilly Tu         "Entries", logEntryID);
130284afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
130384afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
130484afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
130584afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
130684afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
130784afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
130884afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
130984afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1310ac992cdeSJason M. Bills     return LogParseError::success;
1311c4bf6374SJason M. Bills }
1312c4bf6374SJason M. Bills 
13137e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1314c4bf6374SJason M. Bills {
131522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
13168b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1317002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1318002d39b4SEd Tanous             [&app](const crow::Request& req,
131922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
132022d268cbSEd Tanous                    const std::string& systemName) {
1321c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1322c937d2bfSEd Tanous             .canDelegateTop = true,
1323c937d2bfSEd Tanous             .canDelegateSkip = true,
1324c937d2bfSEd Tanous         };
1325c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1326c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
13273ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1328c4bf6374SJason M. Bills         {
1329c4bf6374SJason M. Bills             return;
1330c4bf6374SJason M. Bills         }
133122d268cbSEd Tanous         if (systemName != "system")
133222d268cbSEd Tanous         {
133322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
133422d268cbSEd Tanous                                        systemName);
133522d268cbSEd Tanous             return;
133622d268cbSEd Tanous         }
133722d268cbSEd Tanous 
13385143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
13393648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
13403648c8beSEd Tanous 
13417e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13427e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1343c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1344c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1345c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1346029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1347c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1348c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1349c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1350cb92c03bSAndrew Geissler 
13514978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1352c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
13537e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
13547e860f15SJohn Edward Broadbent         // entry
135595820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
135695820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1357b01bf299SEd Tanous         uint64_t entryCount = 0;
1358cd225da8SJason M. Bills         std::string logEntry;
135995820184SJason M. Bills 
13607e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
13617e860f15SJohn Edward Broadbent         // backwards
1362002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1363002d39b4SEd Tanous              it++)
1364c4bf6374SJason M. Bills         {
1365cd225da8SJason M. Bills             std::ifstream logStream(*it);
136695820184SJason M. Bills             if (!logStream.is_open())
1367c4bf6374SJason M. Bills             {
1368c4bf6374SJason M. Bills                 continue;
1369c4bf6374SJason M. Bills             }
1370c4bf6374SJason M. Bills 
1371e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1372e85d6b16SJason M. Bills             bool firstEntry = true;
137395820184SJason M. Bills             while (std::getline(logStream, logEntry))
137495820184SJason M. Bills             {
1375c4bf6374SJason M. Bills                 std::string idStr;
1376e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1377c4bf6374SJason M. Bills                 {
1378c4bf6374SJason M. Bills                     continue;
1379c4bf6374SJason M. Bills                 }
1380e85d6b16SJason M. Bills                 firstEntry = false;
1381e85d6b16SJason M. Bills 
1382de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1383ac992cdeSJason M. Bills                 LogParseError status =
1384ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1385ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1386ac992cdeSJason M. Bills                 {
1387ac992cdeSJason M. Bills                     continue;
1388ac992cdeSJason M. Bills                 }
1389ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1390c4bf6374SJason M. Bills                 {
1391c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1392c4bf6374SJason M. Bills                     return;
1393c4bf6374SJason M. Bills                 }
1394de703c5dSJason M. Bills 
1395de703c5dSJason M. Bills                 entryCount++;
1396de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1397de703c5dSJason M. Bills                 // start) and top (number of entries to display)
13983648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1399de703c5dSJason M. Bills                 {
1400de703c5dSJason M. Bills                     continue;
1401de703c5dSJason M. Bills                 }
1402de703c5dSJason M. Bills 
1403de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1404c4bf6374SJason M. Bills             }
140595820184SJason M. Bills         }
1406c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
14073648c8beSEd Tanous         if (skip + top < entryCount)
1408c4bf6374SJason M. Bills         {
1409c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
14104978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
14113648c8beSEd Tanous                 std::to_string(skip + top);
1412c4bf6374SJason M. Bills         }
14137e860f15SJohn Edward Broadbent         });
1414897967deSJason M. Bills }
1415897967deSJason M. Bills 
14167e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1417897967deSJason M. Bills {
14187e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
141922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1420ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
14217e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
142245ca1b86SEd Tanous             [&app](const crow::Request& req,
14237e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
142422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
14253ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
142645ca1b86SEd Tanous         {
142745ca1b86SEd Tanous             return;
142845ca1b86SEd Tanous         }
142922d268cbSEd Tanous 
143022d268cbSEd Tanous         if (systemName != "system")
143122d268cbSEd Tanous         {
143222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
143322d268cbSEd Tanous                                        systemName);
143422d268cbSEd Tanous             return;
143522d268cbSEd Tanous         }
143622d268cbSEd Tanous 
14377e860f15SJohn Edward Broadbent         const std::string& targetID = param;
14388d1b46d7Szhanghch05 
14397e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
14407e860f15SJohn Edward Broadbent         // entry to find the target entry
1441897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1442897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1443897967deSJason M. Bills         std::string logEntry;
1444897967deSJason M. Bills 
14457e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14467e860f15SJohn Edward Broadbent         // backwards
1447002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1448002d39b4SEd Tanous              it++)
1449897967deSJason M. Bills         {
1450897967deSJason M. Bills             std::ifstream logStream(*it);
1451897967deSJason M. Bills             if (!logStream.is_open())
1452897967deSJason M. Bills             {
1453897967deSJason M. Bills                 continue;
1454897967deSJason M. Bills             }
1455897967deSJason M. Bills 
1456897967deSJason M. Bills             // Reset the unique ID on the first entry
1457897967deSJason M. Bills             bool firstEntry = true;
1458897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1459897967deSJason M. Bills             {
1460897967deSJason M. Bills                 std::string idStr;
1461897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1462897967deSJason M. Bills                 {
1463897967deSJason M. Bills                     continue;
1464897967deSJason M. Bills                 }
1465897967deSJason M. Bills                 firstEntry = false;
1466897967deSJason M. Bills 
1467897967deSJason M. Bills                 if (idStr == targetID)
1468897967deSJason M. Bills                 {
1469de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1470ac992cdeSJason M. Bills                     LogParseError status =
1471ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1472ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1473897967deSJason M. Bills                     {
1474897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1475897967deSJason M. Bills                         return;
1476897967deSJason M. Bills                     }
1477d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1478897967deSJason M. Bills                     return;
1479897967deSJason M. Bills                 }
1480897967deSJason M. Bills             }
1481897967deSJason M. Bills         }
1482897967deSJason M. Bills         // Requested ID was not found
14839db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
14847e860f15SJohn Edward Broadbent         });
148508a4e4b5SAnthony Wilson }
148608a4e4b5SAnthony Wilson 
14877e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
148808a4e4b5SAnthony Wilson {
148922d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1490ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1491002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1492002d39b4SEd Tanous             [&app](const crow::Request& req,
149322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
149422d268cbSEd Tanous                    const std::string& systemName) {
14953ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
149645ca1b86SEd Tanous         {
149745ca1b86SEd Tanous             return;
149845ca1b86SEd Tanous         }
149922d268cbSEd Tanous         if (systemName != "system")
150022d268cbSEd Tanous         {
150122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
150222d268cbSEd Tanous                                        systemName);
150322d268cbSEd Tanous             return;
150422d268cbSEd Tanous         }
150522d268cbSEd Tanous 
15067e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
15077e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
150808a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
150908a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
151008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
151108a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
151208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
151308a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
151408a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
151508a4e4b5SAnthony Wilson 
1516cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1517cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1518cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1519*5e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec,
1520914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1521cb92c03bSAndrew Geissler             if (ec)
1522cb92c03bSAndrew Geissler             {
1523cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1524cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1525002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1526cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1527cb92c03bSAndrew Geissler                 return;
1528cb92c03bSAndrew Geissler             }
1529002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1530cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
15319eb808c1SEd Tanous             for (const auto& objectPath : resp)
1532cb92c03bSAndrew Geissler             {
1533914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1534c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1535c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1536914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1537914e2d5dSEd Tanous                 const std::string* message = nullptr;
1538914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
15399c11a172SVijay Lobo                 const std::string* resolution = nullptr;
154075710de2SXiaochao Ma                 bool resolved = false;
15419017faf2SAbhishek Patel                 const std::string* notify = nullptr;
15429017faf2SAbhishek Patel 
15439eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1544f86bb901SAdriana Kobylak                 {
1545f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1546f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1547f86bb901SAdriana Kobylak                     {
1548002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1549cb92c03bSAndrew Geissler                         {
1550cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1551cb92c03bSAndrew Geissler                             {
1552002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1553cb92c03bSAndrew Geissler                             }
1554cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1555cb92c03bSAndrew Geissler                             {
1556002d39b4SEd Tanous                                 timestamp =
1557002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15587e860f15SJohn Edward Broadbent                             }
1559002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
15607e860f15SJohn Edward Broadbent                             {
1561002d39b4SEd Tanous                                 updateTimestamp =
1562002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15637e860f15SJohn Edward Broadbent                             }
15647e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
15657e860f15SJohn Edward Broadbent                             {
15667e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
15677e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15687e860f15SJohn Edward Broadbent                             }
15699c11a172SVijay Lobo                             else if (propertyMap.first == "Resolution")
15709c11a172SVijay Lobo                             {
15719c11a172SVijay Lobo                                 resolution = std::get_if<std::string>(
15729c11a172SVijay Lobo                                     &propertyMap.second);
15739c11a172SVijay Lobo                             }
15747e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
15757e860f15SJohn Edward Broadbent                             {
15767e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
15777e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15787e860f15SJohn Edward Broadbent                             }
15797e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
15807e860f15SJohn Edward Broadbent                             {
1581914e2d5dSEd Tanous                                 const bool* resolveptr =
1582002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
15837e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
15847e860f15SJohn Edward Broadbent                                 {
1585002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
15867e860f15SJohn Edward Broadbent                                     return;
15877e860f15SJohn Edward Broadbent                                 }
15887e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
15897e860f15SJohn Edward Broadbent                             }
15909017faf2SAbhishek Patel                             else if (propertyMap.first ==
15919017faf2SAbhishek Patel                                      "ServiceProviderNotify")
15929017faf2SAbhishek Patel                             {
15939017faf2SAbhishek Patel                                 notify = std::get_if<std::string>(
15949017faf2SAbhishek Patel                                     &propertyMap.second);
15959017faf2SAbhishek Patel                                 if (notify == nullptr)
15969017faf2SAbhishek Patel                                 {
15979017faf2SAbhishek Patel                                     messages::internalError(asyncResp->res);
15989017faf2SAbhishek Patel                                     return;
15999017faf2SAbhishek Patel                                 }
16009017faf2SAbhishek Patel                             }
16017e860f15SJohn Edward Broadbent                         }
16027e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
16037e860f15SJohn Edward Broadbent                             severity == nullptr)
16047e860f15SJohn Edward Broadbent                         {
16057e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
16067e860f15SJohn Edward Broadbent                             return;
16077e860f15SJohn Edward Broadbent                         }
16087e860f15SJohn Edward Broadbent                     }
16097e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
16107e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
16117e860f15SJohn Edward Broadbent                     {
1612002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
16137e860f15SJohn Edward Broadbent                         {
16147e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
16157e860f15SJohn Edward Broadbent                             {
16167e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
16177e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16187e860f15SJohn Edward Broadbent                             }
16197e860f15SJohn Edward Broadbent                         }
16207e860f15SJohn Edward Broadbent                     }
16217e860f15SJohn Edward Broadbent                 }
16227e860f15SJohn Edward Broadbent                 // Object path without the
16237e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
16247e860f15SJohn Edward Broadbent                 // and continue.
16257e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1626c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1627c419c759SEd Tanous                     updateTimestamp == nullptr)
16287e860f15SJohn Edward Broadbent                 {
16297e860f15SJohn Edward Broadbent                     continue;
16307e860f15SJohn Edward Broadbent                 }
16317e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
16327e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
16339c11a172SVijay Lobo                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1634eddfc437SWilly Tu                 thisEntry["@odata.id"] = crow::utility::urlFromPieces(
1635eddfc437SWilly Tu                     "redfish", "v1", "Systems", "system", "LogServices",
1636eddfc437SWilly Tu                     "EventLog", "Entries", std::to_string(*id));
16377e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
16387e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
16397e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
16407e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
16419c11a172SVijay Lobo                 if ((resolution != nullptr) && (!(*resolution).empty()))
16429c11a172SVijay Lobo                 {
16439c11a172SVijay Lobo                     thisEntry["Resolution"] = *resolution;
16449c11a172SVijay Lobo                 }
16459017faf2SAbhishek Patel                 std::optional<bool> notifyAction =
16469017faf2SAbhishek Patel                     getProviderNotifyAction(*notify);
16479017faf2SAbhishek Patel                 if (notifyAction)
16489017faf2SAbhishek Patel                 {
16499017faf2SAbhishek Patel                     thisEntry["ServiceProviderNotified"] = *notifyAction;
16509017faf2SAbhishek Patel                 }
16517e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
16527e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
16537e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
16547e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
16552b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
16567e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
16572b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
16587e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
16597e860f15SJohn Edward Broadbent                 {
16607e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
16610fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
16627e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
16637e860f15SJohn Edward Broadbent                 }
16647e860f15SJohn Edward Broadbent             }
1665002d39b4SEd Tanous             std::sort(
1666002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1667002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
16687e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
16697e860f15SJohn Edward Broadbent                 });
16707e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
16717e860f15SJohn Edward Broadbent                 entriesArray.size();
16727e860f15SJohn Edward Broadbent             },
16737e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
16747e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16757e860f15SJohn Edward Broadbent         });
16767e860f15SJohn Edward Broadbent }
16777e860f15SJohn Edward Broadbent 
16787e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
16797e860f15SJohn Edward Broadbent {
16807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
168122d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1682ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1683002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1684002d39b4SEd Tanous             [&app](const crow::Request& req,
16857e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
168622d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
16873ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16887e860f15SJohn Edward Broadbent         {
168945ca1b86SEd Tanous             return;
169045ca1b86SEd Tanous         }
169122d268cbSEd Tanous         if (systemName != "system")
169222d268cbSEd Tanous         {
169322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
169422d268cbSEd Tanous                                        systemName);
169522d268cbSEd Tanous             return;
169622d268cbSEd Tanous         }
169722d268cbSEd Tanous 
16987e860f15SJohn Edward Broadbent         std::string entryID = param;
16997e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
17007e860f15SJohn Edward Broadbent 
17017e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
17027e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1703d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1704d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1705d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
1706*5e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec,
1707b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
17087e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
17097e860f15SJohn Edward Broadbent             {
1710d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1711d1bde9e5SKrzysztof Grobelny                                            entryID);
17127e860f15SJohn Edward Broadbent                 return;
17137e860f15SJohn Edward Broadbent             }
17147e860f15SJohn Edward Broadbent             if (ec)
17157e860f15SJohn Edward Broadbent             {
17160fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1717002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
17187e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
17197e860f15SJohn Edward Broadbent                 return;
17207e860f15SJohn Edward Broadbent             }
1721914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1722c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1723c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1724914e2d5dSEd Tanous             const std::string* severity = nullptr;
1725914e2d5dSEd Tanous             const std::string* message = nullptr;
1726914e2d5dSEd Tanous             const std::string* filePath = nullptr;
17279c11a172SVijay Lobo             const std::string* resolution = nullptr;
17287e860f15SJohn Edward Broadbent             bool resolved = false;
17299017faf2SAbhishek Patel             const std::string* notify = nullptr;
17307e860f15SJohn Edward Broadbent 
1731d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1732d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1733d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
17349c11a172SVijay Lobo                 severity, "Message", message, "Resolved", resolved,
17359017faf2SAbhishek Patel                 "Resolution", resolution, "Path", filePath,
17369017faf2SAbhishek Patel                 "ServiceProviderNotify", notify);
1737d1bde9e5SKrzysztof Grobelny 
1738d1bde9e5SKrzysztof Grobelny             if (!success)
173975710de2SXiaochao Ma             {
174075710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
174175710de2SXiaochao Ma                 return;
174275710de2SXiaochao Ma             }
1743d1bde9e5SKrzysztof Grobelny 
1744002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
17459017faf2SAbhishek Patel                 timestamp == nullptr || updateTimestamp == nullptr ||
17469017faf2SAbhishek Patel                 notify == nullptr)
1747f86bb901SAdriana Kobylak             {
1748ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1749271584abSEd Tanous                 return;
1750271584abSEd Tanous             }
17519017faf2SAbhishek Patel 
1752f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
17539c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
1754f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
1755eddfc437SWilly Tu                 crow::utility::urlFromPieces(
1756eddfc437SWilly Tu                     "redfish", "v1", "Systems", "system", "LogServices",
1757eddfc437SWilly Tu                     "EventLog", "Entries", std::to_string(*id));
175845ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1759f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1760f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1761f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
17629017faf2SAbhishek Patel             std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
17639017faf2SAbhishek Patel             if (notifyAction)
17649017faf2SAbhishek Patel             {
17659017faf2SAbhishek Patel                 asyncResp->res.jsonValue["ServiceProviderNotified"] =
17669017faf2SAbhishek Patel                     *notifyAction;
17679017faf2SAbhishek Patel             }
17689c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
17699c11a172SVijay Lobo             {
17709c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
17719c11a172SVijay Lobo             }
1772f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1773f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1774f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1775f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
17762b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1777f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
17782b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1779f86bb901SAdriana Kobylak             if (filePath != nullptr)
1780f86bb901SAdriana Kobylak             {
1781f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1782e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1783e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1784f86bb901SAdriana Kobylak             }
1785d1bde9e5SKrzysztof Grobelny             });
17867e860f15SJohn Edward Broadbent         });
1787336e96c6SChicago Duan 
17887e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
178922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1790ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
17917e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
179245ca1b86SEd Tanous             [&app](const crow::Request& req,
17937e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
179422d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
17953ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179645ca1b86SEd Tanous         {
179745ca1b86SEd Tanous             return;
179845ca1b86SEd Tanous         }
179922d268cbSEd Tanous         if (systemName != "system")
180022d268cbSEd Tanous         {
180122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
180222d268cbSEd Tanous                                        systemName);
180322d268cbSEd Tanous             return;
180422d268cbSEd Tanous         }
180575710de2SXiaochao Ma         std::optional<bool> resolved;
180675710de2SXiaochao Ma 
180715ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
18087e860f15SJohn Edward Broadbent                                       resolved))
180975710de2SXiaochao Ma         {
181075710de2SXiaochao Ma             return;
181175710de2SXiaochao Ma         }
181275710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
181375710de2SXiaochao Ma 
181475710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
1815*5e7e2dc5SEd Tanous             [asyncResp, entryId](const boost::system::error_code& ec) {
181675710de2SXiaochao Ma             if (ec)
181775710de2SXiaochao Ma             {
181875710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
181975710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
182075710de2SXiaochao Ma                 return;
182175710de2SXiaochao Ma             }
182275710de2SXiaochao Ma             },
182375710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
182475710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
182575710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
182675710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1827168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
18287e860f15SJohn Edward Broadbent         });
182975710de2SXiaochao Ma 
18307e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
183122d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1832ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1833ed398213SEd Tanous 
1834002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1835002d39b4SEd Tanous             [&app](const crow::Request& req,
1836002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
183722d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18383ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1839336e96c6SChicago Duan         {
184045ca1b86SEd Tanous             return;
184145ca1b86SEd Tanous         }
184222d268cbSEd Tanous         if (systemName != "system")
184322d268cbSEd Tanous         {
184422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
184522d268cbSEd Tanous                                        systemName);
184622d268cbSEd Tanous             return;
184722d268cbSEd Tanous         }
1848336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1849336e96c6SChicago Duan 
18507e860f15SJohn Edward Broadbent         std::string entryID = param;
1851336e96c6SChicago Duan 
1852336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1853336e96c6SChicago Duan 
1854336e96c6SChicago Duan         // Process response from Logging service.
1855002d39b4SEd Tanous         auto respHandler =
1856*5e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec) {
1857002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1858336e96c6SChicago Duan             if (ec)
1859336e96c6SChicago Duan             {
18603de8d8baSGeorge Liu                 if (ec.value() == EBADR)
18613de8d8baSGeorge Liu                 {
186245ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
186345ca1b86SEd Tanous                                                entryID);
18643de8d8baSGeorge Liu                     return;
18653de8d8baSGeorge Liu                 }
1866336e96c6SChicago Duan                 // TODO Handle for specific error code
18670fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
18680fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1869336e96c6SChicago Duan                     << ec;
1870336e96c6SChicago Duan                 asyncResp->res.result(
1871336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1872336e96c6SChicago Duan                 return;
1873336e96c6SChicago Duan             }
1874336e96c6SChicago Duan 
1875336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1876336e96c6SChicago Duan         };
1877336e96c6SChicago Duan 
1878336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1879336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1880336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1881336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1882336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
18837e860f15SJohn Edward Broadbent         });
1884400fd1fbSAdriana Kobylak }
1885400fd1fbSAdriana Kobylak 
18867e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1887400fd1fbSAdriana Kobylak {
18880fda0f12SGeorge Liu     BMCWEB_ROUTE(
18890fda0f12SGeorge Liu         app,
189022d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1891ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
18927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
189345ca1b86SEd Tanous             [&app](const crow::Request& req,
18947e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
189522d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18977e860f15SJohn Edward Broadbent         {
189845ca1b86SEd Tanous             return;
189945ca1b86SEd Tanous         }
190099351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
190199351cd8SEd Tanous                 req.getHeaderValue("Accept"),
19024a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1903400fd1fbSAdriana Kobylak         {
1904002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1905400fd1fbSAdriana Kobylak             return;
1906400fd1fbSAdriana Kobylak         }
190722d268cbSEd Tanous         if (systemName != "system")
190822d268cbSEd Tanous         {
190922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
191022d268cbSEd Tanous                                        systemName);
191122d268cbSEd Tanous             return;
191222d268cbSEd Tanous         }
1913400fd1fbSAdriana Kobylak 
19147e860f15SJohn Edward Broadbent         std::string entryID = param;
1915400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1916400fd1fbSAdriana Kobylak 
1917400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1918*5e7e2dc5SEd Tanous             [asyncResp, entryID](const boost::system::error_code& ec,
1919400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1920400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1921400fd1fbSAdriana Kobylak             {
1922002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1923002d39b4SEd Tanous                                            entryID);
1924400fd1fbSAdriana Kobylak                 return;
1925400fd1fbSAdriana Kobylak             }
1926400fd1fbSAdriana Kobylak             if (ec)
1927400fd1fbSAdriana Kobylak             {
1928400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1929400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1930400fd1fbSAdriana Kobylak                 return;
1931400fd1fbSAdriana Kobylak             }
1932400fd1fbSAdriana Kobylak 
1933400fd1fbSAdriana Kobylak             int fd = -1;
1934400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1935400fd1fbSAdriana Kobylak             if (fd == -1)
1936400fd1fbSAdriana Kobylak             {
1937400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1938400fd1fbSAdriana Kobylak                 return;
1939400fd1fbSAdriana Kobylak             }
1940400fd1fbSAdriana Kobylak 
1941400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1942400fd1fbSAdriana Kobylak             if (size == -1)
1943400fd1fbSAdriana Kobylak             {
1944400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1945400fd1fbSAdriana Kobylak                 return;
1946400fd1fbSAdriana Kobylak             }
1947400fd1fbSAdriana Kobylak 
1948400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1949400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1950400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1951400fd1fbSAdriana Kobylak             {
1952002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1953400fd1fbSAdriana Kobylak                                  << maxFileSize;
1954400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1955400fd1fbSAdriana Kobylak                 return;
1956400fd1fbSAdriana Kobylak             }
1957400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1958400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1959400fd1fbSAdriana Kobylak             if (rc == -1)
1960400fd1fbSAdriana Kobylak             {
1961400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1962400fd1fbSAdriana Kobylak                 return;
1963400fd1fbSAdriana Kobylak             }
1964400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1965400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1966400fd1fbSAdriana Kobylak             {
1967400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1968400fd1fbSAdriana Kobylak                 return;
1969400fd1fbSAdriana Kobylak             }
1970400fd1fbSAdriana Kobylak             close(fd);
1971400fd1fbSAdriana Kobylak 
1972400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1973002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1974400fd1fbSAdriana Kobylak 
1975d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1976400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1977d9f6c621SEd Tanous             asyncResp->res.addHeader(
1978d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
1979400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1980400fd1fbSAdriana Kobylak             },
1981400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1982400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1983400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
19847e860f15SJohn Edward Broadbent         });
19851da66f75SEd Tanous }
19861da66f75SEd Tanous 
1987b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1988b7028ebfSSpencer Ku 
1989b7028ebfSSpencer Ku inline bool
1990b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1991b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1992b7028ebfSSpencer Ku {
1993b7028ebfSSpencer Ku     std::error_code ec;
1994b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1995b7028ebfSSpencer Ku     if (ec)
1996b7028ebfSSpencer Ku     {
1997b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1998b7028ebfSSpencer Ku         return false;
1999b7028ebfSSpencer Ku     }
2000b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
2001b7028ebfSSpencer Ku     {
2002b7028ebfSSpencer Ku         std::string filename = it.path().filename();
2003b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
2004b7028ebfSSpencer Ku         // path
200511ba3979SEd Tanous         if (filename.starts_with("log"))
2006b7028ebfSSpencer Ku         {
2007b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
2008b7028ebfSSpencer Ku         }
2009b7028ebfSSpencer Ku     }
2010b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
2011b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
2012b7028ebfSSpencer Ku     // descending order.
2013b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2014b7028ebfSSpencer Ku               AlphanumLess<std::string>());
2015b7028ebfSSpencer Ku 
2016b7028ebfSSpencer Ku     return true;
2017b7028ebfSSpencer Ku }
2018b7028ebfSSpencer Ku 
201902cad96eSEd Tanous inline bool getHostLoggerEntries(
202002cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
202102cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
2022b7028ebfSSpencer Ku {
2023b7028ebfSSpencer Ku     GzFileReader logFile;
2024b7028ebfSSpencer Ku 
2025b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
2026b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
2027b7028ebfSSpencer Ku     {
2028b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2029b7028ebfSSpencer Ku         {
2030b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
2031b7028ebfSSpencer Ku             return false;
2032b7028ebfSSpencer Ku         }
2033b7028ebfSSpencer Ku     }
2034b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2035b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2036b7028ebfSSpencer Ku     if (!lastMessage.empty())
2037b7028ebfSSpencer Ku     {
2038b7028ebfSSpencer Ku         logCount++;
2039b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2040b7028ebfSSpencer Ku         {
2041b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2042b7028ebfSSpencer Ku         }
2043b7028ebfSSpencer Ku     }
2044b7028ebfSSpencer Ku     return true;
2045b7028ebfSSpencer Ku }
2046b7028ebfSSpencer Ku 
2047b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2048b7028ebfSSpencer Ku                                     const std::string& msg,
20496d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2050b7028ebfSSpencer Ku {
2051b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
20529c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2053eddfc437SWilly Tu     logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2054eddfc437SWilly Tu         "redfish", "v1", "Systems", "system", "LogServices", "HostLogger",
2055eddfc437SWilly Tu         "Entries", logEntryID);
20566d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
20576d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
20586d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
20596d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
20606d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
20616d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2062b7028ebfSSpencer Ku }
2063b7028ebfSSpencer Ku 
2064b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2065b7028ebfSSpencer Ku {
206622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2067b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
20681476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
20691476687dSEd Tanous             [&app](const crow::Request& req,
207022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
207122d268cbSEd Tanous                    const std::string& systemName) {
20723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207345ca1b86SEd Tanous         {
207445ca1b86SEd Tanous             return;
207545ca1b86SEd Tanous         }
207622d268cbSEd Tanous         if (systemName != "system")
207722d268cbSEd Tanous         {
207822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
207922d268cbSEd Tanous                                        systemName);
208022d268cbSEd Tanous             return;
208122d268cbSEd Tanous         }
2082b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2083b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
2084b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2085b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
2086b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2087b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2088b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
20891476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
20901476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2091b7028ebfSSpencer Ku         });
2092b7028ebfSSpencer Ku }
2093b7028ebfSSpencer Ku 
2094b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2095b7028ebfSSpencer Ku {
2096b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
209722d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2098b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2099002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2100002d39b4SEd Tanous             [&app](const crow::Request& req,
210122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
210222d268cbSEd Tanous                    const std::string& systemName) {
2103c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2104c937d2bfSEd Tanous             .canDelegateTop = true,
2105c937d2bfSEd Tanous             .canDelegateSkip = true,
2106c937d2bfSEd Tanous         };
2107c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2108c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21093ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2110b7028ebfSSpencer Ku         {
2111b7028ebfSSpencer Ku             return;
2112b7028ebfSSpencer Ku         }
211322d268cbSEd Tanous         if (systemName != "system")
211422d268cbSEd Tanous         {
211522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
211622d268cbSEd Tanous                                        systemName);
211722d268cbSEd Tanous             return;
211822d268cbSEd Tanous         }
2119b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2120b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2121b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2122b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2123b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2124b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2125b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
21260fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2127b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2128b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2129b7028ebfSSpencer Ku 
2130b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2131b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2132b7028ebfSSpencer Ku         {
2133b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2134b7028ebfSSpencer Ku             return;
2135b7028ebfSSpencer Ku         }
21363648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
21373648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21385143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2139b7028ebfSSpencer Ku         size_t logCount = 0;
2140b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2141b7028ebfSSpencer Ku         // control by skip and top.
2142b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
21433648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
21443648c8beSEd Tanous                                   logCount))
2145b7028ebfSSpencer Ku         {
2146b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2147b7028ebfSSpencer Ku             return;
2148b7028ebfSSpencer Ku         }
2149b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2150b7028ebfSSpencer Ku         // log count
215126f6976fSEd Tanous         if (logEntries.empty())
2152b7028ebfSSpencer Ku         {
2153b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2154b7028ebfSSpencer Ku             return;
2155b7028ebfSSpencer Ku         }
215626f6976fSEd Tanous         if (!logEntries.empty())
2157b7028ebfSSpencer Ku         {
2158b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2159b7028ebfSSpencer Ku             {
21606d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
21613648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
21623648c8beSEd Tanous                                         hostLogEntry);
21636d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
2164b7028ebfSSpencer Ku             }
2165b7028ebfSSpencer Ku 
2166b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
21673648c8beSEd Tanous             if (skip + top < logCount)
2168b7028ebfSSpencer Ku             {
2169b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
21700fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
21713648c8beSEd Tanous                     std::to_string(skip + top);
2172b7028ebfSSpencer Ku             }
2173b7028ebfSSpencer Ku         }
2174b7028ebfSSpencer Ku         });
2175b7028ebfSSpencer Ku }
2176b7028ebfSSpencer Ku 
2177b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2178b7028ebfSSpencer Ku {
2179b7028ebfSSpencer Ku     BMCWEB_ROUTE(
218022d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2181b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2182b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
218345ca1b86SEd Tanous             [&app](const crow::Request& req,
2184b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218522d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
21863ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
218745ca1b86SEd Tanous         {
218845ca1b86SEd Tanous             return;
218945ca1b86SEd Tanous         }
219022d268cbSEd Tanous         if (systemName != "system")
219122d268cbSEd Tanous         {
219222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
219322d268cbSEd Tanous                                        systemName);
219422d268cbSEd Tanous             return;
219522d268cbSEd Tanous         }
2196b7028ebfSSpencer Ku         const std::string& targetID = param;
2197b7028ebfSSpencer Ku 
2198b7028ebfSSpencer Ku         uint64_t idInt = 0;
2199ca45aa3cSEd Tanous 
2200ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2201ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
2202ca45aa3cSEd Tanous 
2203ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
22049db4ba25SJiaqing Zhao         if (ec == std::errc::invalid_argument ||
22059db4ba25SJiaqing Zhao             ec == std::errc::result_out_of_range)
2206b7028ebfSSpencer Ku         {
22079db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2208b7028ebfSSpencer Ku             return;
2209b7028ebfSSpencer Ku         }
2210b7028ebfSSpencer Ku 
2211b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2212b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2213b7028ebfSSpencer Ku         {
2214b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2215b7028ebfSSpencer Ku             return;
2216b7028ebfSSpencer Ku         }
2217b7028ebfSSpencer Ku 
2218b7028ebfSSpencer Ku         size_t logCount = 0;
22193648c8beSEd Tanous         size_t top = 1;
2220b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2221b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2222b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2223b7028ebfSSpencer Ku         // get that entry
2224002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2225002d39b4SEd Tanous                                   logCount))
2226b7028ebfSSpencer Ku         {
2227b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2228b7028ebfSSpencer Ku             return;
2229b7028ebfSSpencer Ku         }
2230b7028ebfSSpencer Ku 
2231b7028ebfSSpencer Ku         if (!logEntries.empty())
2232b7028ebfSSpencer Ku         {
22336d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
22346d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
22356d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2236b7028ebfSSpencer Ku             return;
2237b7028ebfSSpencer Ku         }
2238b7028ebfSSpencer Ku 
2239b7028ebfSSpencer Ku         // Requested ID was not found
22409db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2241b7028ebfSSpencer Ku         });
2242b7028ebfSSpencer Ku }
2243b7028ebfSSpencer Ku 
2244dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2245fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2246fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22471da66f75SEd Tanous {
22483ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
224945ca1b86SEd Tanous     {
225045ca1b86SEd Tanous         return;
225145ca1b86SEd Tanous     }
22527e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
22537e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2254e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
22551da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2256e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2257e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2258002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2259e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
22601da66f75SEd Tanous         "Collection of LogServices for this Manager";
2261002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2262c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2263fdd26906SClaire Weinan 
2264c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2265613dabeaSEd Tanous     nlohmann::json::object_t journal;
2266613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2267613dabeaSEd Tanous     logServiceArray.push_back(std::move(journal));
2268c4bf6374SJason M. Bills #endif
2269fdd26906SClaire Weinan 
2270fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2271fdd26906SClaire Weinan 
2272fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
227315912159SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
22747a1dbc48SGeorge Liu         "xyz.openbmc_project.Collection.DeleteAll"};
22757a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
22767a1dbc48SGeorge Liu         "/xyz/openbmc_project/dump", 0, interfaces,
2277fdd26906SClaire Weinan         [asyncResp](
22787a1dbc48SGeorge Liu             const boost::system::error_code& ec,
2279fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2280fdd26906SClaire Weinan         if (ec)
2281fdd26906SClaire Weinan         {
2282fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2283dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2284fdd26906SClaire Weinan                 << ec;
2285fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2286fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2287fdd26906SClaire Weinan             return;
2288fdd26906SClaire Weinan         }
2289fdd26906SClaire Weinan 
2290fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2291fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2292fdd26906SClaire Weinan 
2293fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2294fdd26906SClaire Weinan         {
2295fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2296fdd26906SClaire Weinan             {
2297613dabeaSEd Tanous                 nlohmann::json::object_t member;
2298613dabeaSEd Tanous                 member["@odata.id"] =
2299613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2300613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2301fdd26906SClaire Weinan             }
2302fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2303fdd26906SClaire Weinan             {
2304613dabeaSEd Tanous                 nlohmann::json::object_t member;
2305613dabeaSEd Tanous                 member["@odata.id"] =
2306613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2307613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2308fdd26906SClaire Weinan             }
2309fdd26906SClaire Weinan         }
2310fdd26906SClaire Weinan 
2311e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2312fdd26906SClaire Weinan             logServiceArrayLocal.size();
23137a1dbc48SGeorge Liu         });
2314fdd26906SClaire Weinan #endif
2315fdd26906SClaire Weinan }
2316fdd26906SClaire Weinan 
2317fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2318fdd26906SClaire Weinan {
2319fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2320fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2321fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2322dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2323e1f26343SJason M. Bills }
2324e1f26343SJason M. Bills 
23257e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2326e1f26343SJason M. Bills {
23277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2328ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
233045ca1b86SEd Tanous             [&app](const crow::Request& req,
233145ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23323ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
23337e860f15SJohn Edward Broadbent         {
233445ca1b86SEd Tanous             return;
233545ca1b86SEd Tanous         }
2336e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2337e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
23380f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
23390f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2340002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2341002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2342ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "Journal";
2343e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23447c8c4058STejas Patil 
23457c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
23462b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2347002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23487c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23497c8c4058STejas Patil             redfishDateTimeOffset.second;
23507c8c4058STejas Patil 
23511476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
23521476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
23537e860f15SJohn Edward Broadbent         });
2354e1f26343SJason M. Bills }
2355e1f26343SJason M. Bills 
23563a48b3a2SJason M. Bills static int
23573a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2358e1f26343SJason M. Bills                                sd_journal* journal,
23593a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2360e1f26343SJason M. Bills {
2361e1f26343SJason M. Bills     // Get the Log Entry contents
2362e1f26343SJason M. Bills     int ret = 0;
2363e1f26343SJason M. Bills 
2364a8fe54f0SJason M. Bills     std::string message;
2365a8fe54f0SJason M. Bills     std::string_view syslogID;
2366a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2367a8fe54f0SJason M. Bills     if (ret < 0)
2368a8fe54f0SJason M. Bills     {
2369a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2370a8fe54f0SJason M. Bills                          << strerror(-ret);
2371a8fe54f0SJason M. Bills     }
2372a8fe54f0SJason M. Bills     if (!syslogID.empty())
2373a8fe54f0SJason M. Bills     {
2374a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2375a8fe54f0SJason M. Bills     }
2376a8fe54f0SJason M. Bills 
237739e77504SEd Tanous     std::string_view msg;
237816428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2379e1f26343SJason M. Bills     if (ret < 0)
2380e1f26343SJason M. Bills     {
2381e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2382e1f26343SJason M. Bills         return 1;
2383e1f26343SJason M. Bills     }
2384a8fe54f0SJason M. Bills     message += std::string(msg);
2385e1f26343SJason M. Bills 
2386e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2387271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
238816428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2389e1f26343SJason M. Bills     if (ret < 0)
2390e1f26343SJason M. Bills     {
2391e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2392e1f26343SJason M. Bills     }
2393e1f26343SJason M. Bills 
2394e1f26343SJason M. Bills     // Get the Created time from the timestamp
239516428a1aSJason M. Bills     std::string entryTimeStr;
239616428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2397e1f26343SJason M. Bills     {
239816428a1aSJason M. Bills         return 1;
2399e1f26343SJason M. Bills     }
2400e1f26343SJason M. Bills 
2401e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
24029c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2403eddfc437SWilly Tu     bmcJournalLogEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2404eddfc437SWilly Tu         "redfish", "v1", "Managers", "bmc", "LogServices", "Journal", "Entries",
2405eddfc437SWilly Tu         bmcJournalLogEntryID);
240684afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
240784afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
240884afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
240984afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
241084afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2411738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
241284afc48bSJason M. Bills                                                          : "OK";
241384afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
241484afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2415e1f26343SJason M. Bills     return 0;
2416e1f26343SJason M. Bills }
2417e1f26343SJason M. Bills 
24187e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2419e1f26343SJason M. Bills {
24207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2421ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2422002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2423002d39b4SEd Tanous             [&app](const crow::Request& req,
2424002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2425c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2426c937d2bfSEd Tanous             .canDelegateTop = true,
2427c937d2bfSEd Tanous             .canDelegateSkip = true,
2428c937d2bfSEd Tanous         };
2429c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2430c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
24313ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2432193ad2faSJason M. Bills         {
2433193ad2faSJason M. Bills             return;
2434193ad2faSJason M. Bills         }
24353648c8beSEd Tanous 
24363648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
24375143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
24383648c8beSEd Tanous 
24397e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
24407e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2441e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2442e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
24430f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
24440f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2445e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2446e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2447e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
24480fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2449e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2450e1f26343SJason M. Bills 
24517e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
24527e860f15SJohn Edward Broadbent         // unique ID for each entry
2453e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2454e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2455e1f26343SJason M. Bills         if (ret < 0)
2456e1f26343SJason M. Bills         {
2457002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2458f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2459e1f26343SJason M. Bills             return;
2460e1f26343SJason M. Bills         }
24610fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
24620fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2463e1f26343SJason M. Bills         journalTmp = nullptr;
2464b01bf299SEd Tanous         uint64_t entryCount = 0;
2465e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2466e85d6b16SJason M. Bills         bool firstEntry = true;
2467e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2468e1f26343SJason M. Bills         {
2469193ad2faSJason M. Bills             entryCount++;
24707e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
24717e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
24723648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2473193ad2faSJason M. Bills             {
2474193ad2faSJason M. Bills                 continue;
2475193ad2faSJason M. Bills             }
2476193ad2faSJason M. Bills 
247716428a1aSJason M. Bills             std::string idStr;
2478e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2479e1f26343SJason M. Bills             {
2480e1f26343SJason M. Bills                 continue;
2481e1f26343SJason M. Bills             }
2482e85d6b16SJason M. Bills             firstEntry = false;
2483e85d6b16SJason M. Bills 
24843a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2485c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2486c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2487e1f26343SJason M. Bills             {
2488f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2489e1f26343SJason M. Bills                 return;
2490e1f26343SJason M. Bills             }
24913a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2492e1f26343SJason M. Bills         }
2493193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
24943648c8beSEd Tanous         if (skip + top < entryCount)
2495193ad2faSJason M. Bills         {
2496193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
24970fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
24983648c8beSEd Tanous                 std::to_string(skip + top);
2499193ad2faSJason M. Bills         }
25007e860f15SJohn Edward Broadbent         });
2501e1f26343SJason M. Bills }
2502e1f26343SJason M. Bills 
25037e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2504e1f26343SJason M. Bills {
25057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
25067e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2507ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
25087e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
250945ca1b86SEd Tanous             [&app](const crow::Request& req,
25107e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25117e860f15SJohn Edward Broadbent                    const std::string& entryID) {
25123ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
251345ca1b86SEd Tanous         {
251445ca1b86SEd Tanous             return;
251545ca1b86SEd Tanous         }
2516e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2517e1f26343SJason M. Bills         uint64_t ts = 0;
2518271584abSEd Tanous         uint64_t index = 0;
25198d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2520e1f26343SJason M. Bills         {
252116428a1aSJason M. Bills             return;
2522e1f26343SJason M. Bills         }
2523e1f26343SJason M. Bills 
2524e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2525e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2526e1f26343SJason M. Bills         if (ret < 0)
2527e1f26343SJason M. Bills         {
2528002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2529f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2530e1f26343SJason M. Bills             return;
2531e1f26343SJason M. Bills         }
2532002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2533002d39b4SEd Tanous             journalTmp, sd_journal_close);
2534e1f26343SJason M. Bills         journalTmp = nullptr;
25357e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
25367e860f15SJohn Edward Broadbent         // index tracking the unique ID
2537af07e3f5SJason M. Bills         std::string idStr;
2538af07e3f5SJason M. Bills         bool firstEntry = true;
2539e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
25402056b6d1SManojkiran Eda         if (ret < 0)
25412056b6d1SManojkiran Eda         {
25422056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
25432056b6d1SManojkiran Eda                              << strerror(-ret);
25442056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
25452056b6d1SManojkiran Eda             return;
25462056b6d1SManojkiran Eda         }
2547271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2548e1f26343SJason M. Bills         {
2549e1f26343SJason M. Bills             sd_journal_next(journal.get());
2550af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2551af07e3f5SJason M. Bills             {
2552af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2553af07e3f5SJason M. Bills                 return;
2554af07e3f5SJason M. Bills             }
2555af07e3f5SJason M. Bills             firstEntry = false;
2556af07e3f5SJason M. Bills         }
2557c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2558af07e3f5SJason M. Bills         if (idStr != entryID)
2559c4bf6374SJason M. Bills         {
25609db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
2561c4bf6374SJason M. Bills             return;
2562c4bf6374SJason M. Bills         }
2563c4bf6374SJason M. Bills 
25643a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2565c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
25663a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2567e1f26343SJason M. Bills         {
2568f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2569e1f26343SJason M. Bills             return;
2570e1f26343SJason M. Bills         }
2571d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
25727e860f15SJohn Edward Broadbent         });
2573c9bb6861Sraviteja-b }
2574c9bb6861Sraviteja-b 
2575fdd26906SClaire Weinan inline void
2576fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2577fdd26906SClaire Weinan                        const std::string& dumpType)
2578c9bb6861Sraviteja-b {
2579fdd26906SClaire Weinan     std::string dumpPath;
2580fdd26906SClaire Weinan     std::string overWritePolicy;
2581fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2582fdd26906SClaire Weinan 
2583fdd26906SClaire Weinan     if (dumpType == "BMC")
258445ca1b86SEd Tanous     {
2585fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2586fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2587fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2588fdd26906SClaire Weinan     }
2589fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2590fdd26906SClaire Weinan     {
2591fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2592fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2593fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2594fdd26906SClaire Weinan     }
2595fdd26906SClaire Weinan     else if (dumpType == "System")
2596fdd26906SClaire Weinan     {
2597fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2598fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2599fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2600fdd26906SClaire Weinan     }
2601fdd26906SClaire Weinan     else
2602fdd26906SClaire Weinan     {
2603fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2604fdd26906SClaire Weinan                          << dumpType;
2605fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
260645ca1b86SEd Tanous         return;
260745ca1b86SEd Tanous     }
2608fdd26906SClaire Weinan 
2609fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2610fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2611c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2612fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2613fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2614fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
26157c8c4058STejas Patil 
26167c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
26172b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
26180fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26197c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26207c8c4058STejas Patil         redfishDateTimeOffset.second;
26217c8c4058STejas Patil 
2622fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2623fdd26906SClaire Weinan 
2624fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2625fdd26906SClaire Weinan     {
2626002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26271476687dSEd Tanous                                 ["target"] =
2628fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2629fdd26906SClaire Weinan     }
26300d946211SClaire Weinan 
26310d946211SClaire Weinan     constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
26320d946211SClaire Weinan     dbus::utility::getSubTreePaths(
26330d946211SClaire Weinan         "/xyz/openbmc_project/dump", 0, interfaces,
26340d946211SClaire Weinan         [asyncResp, dumpType, dumpPath](
26350d946211SClaire Weinan             const boost::system::error_code& ec,
26360d946211SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
26370d946211SClaire Weinan         if (ec)
26380d946211SClaire Weinan         {
26390d946211SClaire Weinan             BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
26400d946211SClaire Weinan                              << ec;
26410d946211SClaire Weinan             // Assume that getting an error simply means there are no dump
26420d946211SClaire Weinan             // LogServices. Return without adding any error response.
26430d946211SClaire Weinan             return;
26440d946211SClaire Weinan         }
26450d946211SClaire Weinan 
26460d946211SClaire Weinan         const std::string dbusDumpPath =
26470d946211SClaire Weinan             "/xyz/openbmc_project/dump/" +
26480d946211SClaire Weinan             boost::algorithm::to_lower_copy(dumpType);
26490d946211SClaire Weinan 
26500d946211SClaire Weinan         for (const std::string& path : subTreePaths)
26510d946211SClaire Weinan         {
26520d946211SClaire Weinan             if (path == dbusDumpPath)
26530d946211SClaire Weinan             {
26540d946211SClaire Weinan                 asyncResp->res
26550d946211SClaire Weinan                     .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
26560d946211SClaire Weinan                     dumpPath + "/Actions/LogService.ClearLog";
26570d946211SClaire Weinan                 break;
26580d946211SClaire Weinan             }
26590d946211SClaire Weinan         }
26600d946211SClaire Weinan         });
2661c9bb6861Sraviteja-b }
2662c9bb6861Sraviteja-b 
2663fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2664fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2665fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26667e860f15SJohn Edward Broadbent {
26673ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266845ca1b86SEd Tanous     {
266945ca1b86SEd Tanous         return;
267045ca1b86SEd Tanous     }
2671fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2672fdd26906SClaire Weinan }
2673c9bb6861Sraviteja-b 
267422d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
267522d268cbSEd Tanous     crow::App& app, const crow::Request& req,
267622d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
267722d268cbSEd Tanous     const std::string& chassisId)
267822d268cbSEd Tanous {
267922d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
268022d268cbSEd Tanous     {
268122d268cbSEd Tanous         return;
268222d268cbSEd Tanous     }
268322d268cbSEd Tanous     if (chassisId != "system")
268422d268cbSEd Tanous     {
268522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
268622d268cbSEd Tanous         return;
268722d268cbSEd Tanous     }
268822d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
268922d268cbSEd Tanous }
269022d268cbSEd Tanous 
2691fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2692fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2693fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2694fdd26906SClaire Weinan {
2695fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2696fdd26906SClaire Weinan     {
2697fdd26906SClaire Weinan         return;
2698fdd26906SClaire Weinan     }
2699fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2700fdd26906SClaire Weinan }
2701fdd26906SClaire Weinan 
270222d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
270322d268cbSEd Tanous     crow::App& app, const crow::Request& req,
270422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
270522d268cbSEd Tanous     const std::string& chassisId)
270622d268cbSEd Tanous {
270722d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
270822d268cbSEd Tanous     {
270922d268cbSEd Tanous         return;
271022d268cbSEd Tanous     }
271122d268cbSEd Tanous     if (chassisId != "system")
271222d268cbSEd Tanous     {
271322d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
271422d268cbSEd Tanous         return;
271522d268cbSEd Tanous     }
271622d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
271722d268cbSEd Tanous }
271822d268cbSEd Tanous 
2719fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2720fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2721fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2722fdd26906SClaire Weinan     const std::string& dumpId)
2723fdd26906SClaire Weinan {
2724fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2725fdd26906SClaire Weinan     {
2726fdd26906SClaire Weinan         return;
2727fdd26906SClaire Weinan     }
2728fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2729fdd26906SClaire Weinan }
273022d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
273122d268cbSEd Tanous     crow::App& app, const crow::Request& req,
273222d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
273322d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
273422d268cbSEd Tanous {
273522d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
273622d268cbSEd Tanous     {
273722d268cbSEd Tanous         return;
273822d268cbSEd Tanous     }
273922d268cbSEd Tanous     if (chassisId != "system")
274022d268cbSEd Tanous     {
274122d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
274222d268cbSEd Tanous         return;
274322d268cbSEd Tanous     }
274422d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
274522d268cbSEd Tanous }
2746fdd26906SClaire Weinan 
2747fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2748fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2749fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2750fdd26906SClaire Weinan     const std::string& dumpId)
2751fdd26906SClaire Weinan {
2752fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2753fdd26906SClaire Weinan     {
2754fdd26906SClaire Weinan         return;
2755fdd26906SClaire Weinan     }
2756fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2757fdd26906SClaire Weinan }
2758fdd26906SClaire Weinan 
275922d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
276022d268cbSEd Tanous     crow::App& app, const crow::Request& req,
276122d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
276222d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
276322d268cbSEd Tanous {
276422d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
276522d268cbSEd Tanous     {
276622d268cbSEd Tanous         return;
276722d268cbSEd Tanous     }
276822d268cbSEd Tanous     if (chassisId != "system")
276922d268cbSEd Tanous     {
277022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
277122d268cbSEd Tanous         return;
277222d268cbSEd Tanous     }
277322d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
277422d268cbSEd Tanous }
277522d268cbSEd Tanous 
2776fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2777fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2778fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2779fdd26906SClaire Weinan {
2780fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2781fdd26906SClaire Weinan     {
2782fdd26906SClaire Weinan         return;
2783fdd26906SClaire Weinan     }
2784fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2785fdd26906SClaire Weinan }
2786fdd26906SClaire Weinan 
278722d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
278822d268cbSEd Tanous     crow::App& app, const crow::Request& req,
278922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
279022d268cbSEd Tanous     const std::string& chassisId)
279122d268cbSEd Tanous {
279222d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
279322d268cbSEd Tanous     {
279422d268cbSEd Tanous         return;
279522d268cbSEd Tanous     }
279622d268cbSEd Tanous     if (chassisId != "system")
279722d268cbSEd Tanous     {
279822d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
279922d268cbSEd Tanous         return;
280022d268cbSEd Tanous     }
280122d268cbSEd Tanous     createDump(asyncResp, req, "System");
280222d268cbSEd Tanous }
280322d268cbSEd Tanous 
2804fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2805fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2806fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2807fdd26906SClaire Weinan {
2808fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2809fdd26906SClaire Weinan     {
2810fdd26906SClaire Weinan         return;
2811fdd26906SClaire Weinan     }
2812fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2813fdd26906SClaire Weinan }
2814fdd26906SClaire Weinan 
281522d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
281622d268cbSEd Tanous     crow::App& app, const crow::Request& req,
281722d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
281822d268cbSEd Tanous     const std::string& chassisId)
281922d268cbSEd Tanous {
282022d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
282122d268cbSEd Tanous     {
282222d268cbSEd Tanous         return;
282322d268cbSEd Tanous     }
282422d268cbSEd Tanous     if (chassisId != "system")
282522d268cbSEd Tanous     {
282622d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
282722d268cbSEd Tanous         return;
282822d268cbSEd Tanous     }
282922d268cbSEd Tanous     clearDump(asyncResp, "System");
283022d268cbSEd Tanous }
283122d268cbSEd Tanous 
2832fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2833fdd26906SClaire Weinan {
2834fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2835fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2836fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2837fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2838fdd26906SClaire Weinan }
2839fdd26906SClaire Weinan 
2840fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2841fdd26906SClaire Weinan {
2842fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2843fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2844fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2845fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2846c9bb6861Sraviteja-b }
2847c9bb6861Sraviteja-b 
28487e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2849c9bb6861Sraviteja-b {
28507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28517e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2852ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2853fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2854fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2855fdd26906SClaire Weinan 
28567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28577e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2858ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2859fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2860fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2861c9bb6861Sraviteja-b }
2862c9bb6861Sraviteja-b 
28637e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2864c9bb6861Sraviteja-b {
28650fda0f12SGeorge Liu     BMCWEB_ROUTE(
28660fda0f12SGeorge Liu         app,
28670fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2868ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
28697e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2870fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2871fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2872a43be80fSAsmitha Karunanithi }
2873a43be80fSAsmitha Karunanithi 
28747e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
287580319af1SAsmitha Karunanithi {
28760fda0f12SGeorge Liu     BMCWEB_ROUTE(
28770fda0f12SGeorge Liu         app,
28780fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2879ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2880fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2881fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
288245ca1b86SEd Tanous }
2883fdd26906SClaire Weinan 
2884fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2885fdd26906SClaire Weinan {
2886fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2887fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2888fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2889fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2890fdd26906SClaire Weinan }
2891fdd26906SClaire Weinan 
2892fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2893fdd26906SClaire Weinan {
2894fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2895fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2896fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2897fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2898fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2899fdd26906SClaire Weinan }
2900fdd26906SClaire Weinan 
2901fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2902fdd26906SClaire Weinan {
2903fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2904fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2905fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2906fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2907fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2908fdd26906SClaire Weinan 
2909fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2910fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2911fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2912fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2913fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2914fdd26906SClaire Weinan }
2915fdd26906SClaire Weinan 
2916fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2917fdd26906SClaire Weinan {
2918fdd26906SClaire Weinan     BMCWEB_ROUTE(
2919fdd26906SClaire Weinan         app,
2920fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2921fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2922fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2923fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
29245cb1dd27SAsmitha Karunanithi }
29255cb1dd27SAsmitha Karunanithi 
29267e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
29275cb1dd27SAsmitha Karunanithi {
292822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2929ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
29306ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
293122d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
29325cb1dd27SAsmitha Karunanithi }
29335cb1dd27SAsmitha Karunanithi 
29347e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
29357e860f15SJohn Edward Broadbent {
293622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2937ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
293822d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
293922d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
294022d268cbSEd Tanous             std::ref(app)));
29415cb1dd27SAsmitha Karunanithi }
29425cb1dd27SAsmitha Karunanithi 
29437e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
29445cb1dd27SAsmitha Karunanithi {
29457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
294622d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2947ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29486ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
294922d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
29508d1b46d7Szhanghch05 
29517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
295222d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2953ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
29546ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
295522d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
29565cb1dd27SAsmitha Karunanithi }
2957c9bb6861Sraviteja-b 
29587e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2959c9bb6861Sraviteja-b {
29600fda0f12SGeorge Liu     BMCWEB_ROUTE(
29610fda0f12SGeorge Liu         app,
296222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2963ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
296422d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
296522d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
296622d268cbSEd Tanous             std::ref(app)));
2967a43be80fSAsmitha Karunanithi }
2968a43be80fSAsmitha Karunanithi 
29697e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2970a43be80fSAsmitha Karunanithi {
29710fda0f12SGeorge Liu     BMCWEB_ROUTE(
29720fda0f12SGeorge Liu         app,
297322d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2974ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29756ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
297622d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2977013487e5Sraviteja-b }
2978013487e5Sraviteja-b 
29797e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
29801da66f75SEd Tanous {
29813946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29823946028dSAppaRao Puli     // method for security reasons.
29831da66f75SEd Tanous     /**
29841da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
29851da66f75SEd Tanous      */
298622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2987ed398213SEd Tanous         // This is incorrect, should be:
2988ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2989432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2990002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2991002d39b4SEd Tanous             [&app](const crow::Request& req,
299222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
299322d268cbSEd Tanous                    const std::string& systemName) {
29943ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
299545ca1b86SEd Tanous         {
299645ca1b86SEd Tanous             return;
299745ca1b86SEd Tanous         }
299822d268cbSEd Tanous         if (systemName != "system")
299922d268cbSEd Tanous         {
300022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
300122d268cbSEd Tanous                                        systemName);
300222d268cbSEd Tanous             return;
300322d268cbSEd Tanous         }
300422d268cbSEd Tanous 
30057e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
30067e860f15SJohn Edward Broadbent         // SubRoute
30070f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
3008424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
3009e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
30108e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
30114f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
30124f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
30134f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3014e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3015e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
30167c8c4058STejas Patil 
30177c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
30182b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
30197c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30207c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30217c8c4058STejas Patil             redfishDateTimeOffset.second;
30227c8c4058STejas Patil 
30231476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3024eddfc437SWilly Tu             crow::utility::urlFromPieces("redfish", "v1", "Systems", "system",
3025eddfc437SWilly Tu                                          "LogServices", "Crashdump", "Entries");
3026002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
30271476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3028002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
30291476687dSEd Tanous                                 ["target"] =
30301476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
30317e860f15SJohn Edward Broadbent         });
30321da66f75SEd Tanous }
30331da66f75SEd Tanous 
30347e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
30355b61b5e8SJason M. Bills {
30360fda0f12SGeorge Liu     BMCWEB_ROUTE(
30370fda0f12SGeorge Liu         app,
303822d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
3039ed398213SEd Tanous         // This is incorrect, should be:
3040ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3041432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
304345ca1b86SEd Tanous             [&app](const crow::Request& req,
304422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
304522d268cbSEd Tanous                    const std::string& systemName) {
30463ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
304745ca1b86SEd Tanous         {
304845ca1b86SEd Tanous             return;
304945ca1b86SEd Tanous         }
305022d268cbSEd Tanous         if (systemName != "system")
305122d268cbSEd Tanous         {
305222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
305322d268cbSEd Tanous                                        systemName);
305422d268cbSEd Tanous             return;
305522d268cbSEd Tanous         }
30565b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
3057*5e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec,
3058cb13a392SEd Tanous                         const std::string&) {
30595b61b5e8SJason M. Bills             if (ec)
30605b61b5e8SJason M. Bills             {
30615b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
30625b61b5e8SJason M. Bills                 return;
30635b61b5e8SJason M. Bills             }
30645b61b5e8SJason M. Bills             messages::success(asyncResp->res);
30655b61b5e8SJason M. Bills             },
3066002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
30677e860f15SJohn Edward Broadbent         });
30685b61b5e8SJason M. Bills }
30695b61b5e8SJason M. Bills 
30708d1b46d7Szhanghch05 static void
30718d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30728d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3073e855dd28SJason M. Bills {
3074043a0536SJohnathan Mantey     auto getStoredLogCallback =
3075b9d36b47SEd Tanous         [asyncResp, logID,
3076*5e7e2dc5SEd Tanous          &logEntryJson](const boost::system::error_code& ec,
3077b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3078e855dd28SJason M. Bills         if (ec)
3079e855dd28SJason M. Bills         {
3080e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
30811ddcf01aSJason M. Bills             if (ec.value() ==
30821ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
30831ddcf01aSJason M. Bills             {
3084002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
30851ddcf01aSJason M. Bills             }
30861ddcf01aSJason M. Bills             else
30871ddcf01aSJason M. Bills             {
3088e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
30891ddcf01aSJason M. Bills             }
3090e855dd28SJason M. Bills             return;
3091e855dd28SJason M. Bills         }
3092043a0536SJohnathan Mantey 
3093043a0536SJohnathan Mantey         std::string timestamp{};
3094043a0536SJohnathan Mantey         std::string filename{};
3095043a0536SJohnathan Mantey         std::string logfile{};
30962c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3097043a0536SJohnathan Mantey 
3098043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3099e855dd28SJason M. Bills         {
31009db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3101e855dd28SJason M. Bills             return;
3102e855dd28SJason M. Bills         }
3103e855dd28SJason M. Bills 
3104043a0536SJohnathan Mantey         std::string crashdumpURI =
3105e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3106043a0536SJohnathan Mantey             logID + "/" + filename;
310784afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
31089c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3109eddfc437SWilly Tu         logEntry["@odata.id"] = crow::utility::urlFromPieces(
3110eddfc437SWilly Tu             "redfish", "v1", "Systems", "system", "LogServices", "Crashdump",
3111eddfc437SWilly Tu             "Entries", logID);
311284afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
311384afc48bSJason M. Bills         logEntry["Id"] = logID;
311484afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
311584afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
311684afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
311784afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
311884afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
31192b20ef6eSJason M. Bills 
31202b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
31212b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
31222b20ef6eSJason M. Bills         // directly
31232b20ef6eSJason M. Bills         if (logEntryJson.is_array())
31242b20ef6eSJason M. Bills         {
31252b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
31262b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
31272b20ef6eSJason M. Bills                 logEntryJson.size();
31282b20ef6eSJason M. Bills         }
31292b20ef6eSJason M. Bills         else
31302b20ef6eSJason M. Bills         {
3131d405bb51SJason M. Bills             logEntryJson.update(logEntry);
31322b20ef6eSJason M. Bills         }
3133e855dd28SJason M. Bills     };
3134d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3135d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3136d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3137d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3138e855dd28SJason M. Bills }
3139e855dd28SJason M. Bills 
31407e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
31411da66f75SEd Tanous {
31423946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31433946028dSAppaRao Puli     // method for security reasons.
31441da66f75SEd Tanous     /**
31451da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
31461da66f75SEd Tanous      */
31477e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
314822d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3149ed398213SEd Tanous         // This is incorrect, should be.
3150ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3151432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3152002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3153002d39b4SEd Tanous             [&app](const crow::Request& req,
315422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
315522d268cbSEd Tanous                    const std::string& systemName) {
31563ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
315745ca1b86SEd Tanous         {
315845ca1b86SEd Tanous             return;
315945ca1b86SEd Tanous         }
316022d268cbSEd Tanous         if (systemName != "system")
316122d268cbSEd Tanous         {
316222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
316322d268cbSEd Tanous                                        systemName);
316422d268cbSEd Tanous             return;
316522d268cbSEd Tanous         }
316622d268cbSEd Tanous 
31677a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
31687a1dbc48SGeorge Liu             crashdumpInterface};
31697a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
31707a1dbc48SGeorge Liu             "/", 0, interfaces,
31717a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
31722b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
31731da66f75SEd Tanous             if (ec)
31741da66f75SEd Tanous             {
31751da66f75SEd Tanous                 if (ec.value() !=
31761da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
31771da66f75SEd Tanous                 {
31781da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
31791da66f75SEd Tanous                                      << ec.message();
3180f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
31811da66f75SEd Tanous                     return;
31821da66f75SEd Tanous                 }
31831da66f75SEd Tanous             }
3184e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
31851da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
31860f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3187424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3188002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3189e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3190424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3191002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3192a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
31932b20ef6eSJason M. Bills 
31942b20ef6eSJason M. Bills             for (const std::string& path : resp)
31951da66f75SEd Tanous             {
31962b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3197e855dd28SJason M. Bills                 // Get the log ID
31982b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
31992b20ef6eSJason M. Bills                 if (logID.empty())
32001da66f75SEd Tanous                 {
3201e855dd28SJason M. Bills                     continue;
32021da66f75SEd Tanous                 }
3203e855dd28SJason M. Bills                 // Add the log entry to the array
32042b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
32052b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
32061da66f75SEd Tanous             }
32077a1dbc48SGeorge Liu             });
32087e860f15SJohn Edward Broadbent         });
32091da66f75SEd Tanous }
32101da66f75SEd Tanous 
32117e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
32121da66f75SEd Tanous {
32133946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32143946028dSAppaRao Puli     // method for security reasons.
32151da66f75SEd Tanous 
32167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
321722d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3218ed398213SEd Tanous         // this is incorrect, should be
3219ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3220432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32217e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
322245ca1b86SEd Tanous             [&app](const crow::Request& req,
32237e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
322422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
32253ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
322645ca1b86SEd Tanous         {
322745ca1b86SEd Tanous             return;
322845ca1b86SEd Tanous         }
322922d268cbSEd Tanous         if (systemName != "system")
323022d268cbSEd Tanous         {
323122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
323222d268cbSEd Tanous                                        systemName);
323322d268cbSEd Tanous             return;
323422d268cbSEd Tanous         }
32357e860f15SJohn Edward Broadbent         const std::string& logID = param;
3236e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
32377e860f15SJohn Edward Broadbent         });
3238e855dd28SJason M. Bills }
3239e855dd28SJason M. Bills 
32407e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3241e855dd28SJason M. Bills {
32423946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32433946028dSAppaRao Puli     // method for security reasons.
32447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
32457e860f15SJohn Edward Broadbent         app,
324622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3247ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
32487e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3249a4ce114aSNan Zhou             [](const crow::Request& req,
32507e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
325122d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
325222d268cbSEd Tanous                const std::string& fileName) {
32532a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
32542a9beeedSShounak Mitra         // Redfish resource.
325522d268cbSEd Tanous 
325622d268cbSEd Tanous         if (systemName != "system")
325722d268cbSEd Tanous         {
325822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
325922d268cbSEd Tanous                                        systemName);
326022d268cbSEd Tanous             return;
326122d268cbSEd Tanous         }
326222d268cbSEd Tanous 
3263043a0536SJohnathan Mantey         auto getStoredLogCallback =
3264002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3265*5e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
3266002d39b4SEd Tanous                 const std::vector<
3267002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
32687e860f15SJohn Edward Broadbent                     resp) {
32691da66f75SEd Tanous             if (ec)
32701da66f75SEd Tanous             {
3271002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3272f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
32731da66f75SEd Tanous                 return;
32741da66f75SEd Tanous             }
3275e855dd28SJason M. Bills 
3276043a0536SJohnathan Mantey             std::string dbusFilename{};
3277043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3278043a0536SJohnathan Mantey             std::string dbusFilepath{};
3279043a0536SJohnathan Mantey 
3280002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3281002d39b4SEd Tanous                                      dbusFilepath);
3282043a0536SJohnathan Mantey 
3283043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3284043a0536SJohnathan Mantey                 dbusFilepath.empty())
32851da66f75SEd Tanous             {
32869db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
32871da66f75SEd Tanous                 return;
32881da66f75SEd Tanous             }
3289e855dd28SJason M. Bills 
3290043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3291043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3292043a0536SJohnathan Mantey             {
32939db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3294043a0536SJohnathan Mantey                 return;
3295043a0536SJohnathan Mantey             }
3296043a0536SJohnathan Mantey 
3297043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3298043a0536SJohnathan Mantey             {
32999db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3300043a0536SJohnathan Mantey                 return;
3301043a0536SJohnathan Mantey             }
3302002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3303002d39b4SEd Tanous             asyncResp->res.body() =
3304002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3305043a0536SJohnathan Mantey 
33067e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
33077e860f15SJohn Edward Broadbent             // from a browser
3308d9f6c621SEd Tanous             asyncResp->res.addHeader(
3309d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
33101da66f75SEd Tanous         };
3311d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3312d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3313d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3314d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
33157e860f15SJohn Edward Broadbent         });
33161da66f75SEd Tanous }
33171da66f75SEd Tanous 
3318c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3319c5a4c82aSJason M. Bills {
3320c5a4c82aSJason M. Bills     onDemand,
3321c5a4c82aSJason M. Bills     telemetry,
3322c5a4c82aSJason M. Bills     invalid,
3323c5a4c82aSJason M. Bills };
3324c5a4c82aSJason M. Bills 
332526ccae32SEd Tanous inline OEMDiagnosticType getOEMDiagnosticType(std::string_view oemDiagStr)
3326c5a4c82aSJason M. Bills {
3327c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3328c5a4c82aSJason M. Bills     {
3329c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3330c5a4c82aSJason M. Bills     }
3331c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3332c5a4c82aSJason M. Bills     {
3333c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3334c5a4c82aSJason M. Bills     }
3335c5a4c82aSJason M. Bills 
3336c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3337c5a4c82aSJason M. Bills }
3338c5a4c82aSJason M. Bills 
33397e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
33401da66f75SEd Tanous {
33413946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
33423946028dSAppaRao Puli     // method for security reasons.
33430fda0f12SGeorge Liu     BMCWEB_ROUTE(
33440fda0f12SGeorge Liu         app,
334522d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3346ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3347ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3348432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3349002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3350002d39b4SEd Tanous             [&app](const crow::Request& req,
335122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
335222d268cbSEd Tanous                    const std::string& systemName) {
33533ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
335445ca1b86SEd Tanous         {
335545ca1b86SEd Tanous             return;
335645ca1b86SEd Tanous         }
335722d268cbSEd Tanous 
335822d268cbSEd Tanous         if (systemName != "system")
335922d268cbSEd Tanous         {
336022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
336122d268cbSEd Tanous                                        systemName);
336222d268cbSEd Tanous             return;
336322d268cbSEd Tanous         }
336422d268cbSEd Tanous 
33658e6c099aSJason M. Bills         std::string diagnosticDataType;
33668e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
336715ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3368002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3369002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
33708e6c099aSJason M. Bills         {
33718e6c099aSJason M. Bills             return;
33728e6c099aSJason M. Bills         }
33738e6c099aSJason M. Bills 
33748e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
33758e6c099aSJason M. Bills         {
33768e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
33778e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
33788e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
33798e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
33808e6c099aSJason M. Bills                 "CollectDiagnosticData");
33818e6c099aSJason M. Bills             return;
33828e6c099aSJason M. Bills         }
33838e6c099aSJason M. Bills 
3384c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3385c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3386c5a4c82aSJason M. Bills 
3387c5a4c82aSJason M. Bills         std::string iface;
3388c5a4c82aSJason M. Bills         std::string method;
3389c5a4c82aSJason M. Bills         std::string taskMatchStr;
3390c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3391c5a4c82aSJason M. Bills         {
3392c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3393c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3394c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3395c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3396c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3397c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3398c5a4c82aSJason M. Bills         }
3399c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3400c5a4c82aSJason M. Bills         {
3401c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3402c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3403c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3404c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3405c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3406c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3407c5a4c82aSJason M. Bills         }
3408c5a4c82aSJason M. Bills         else
3409c5a4c82aSJason M. Bills         {
3410c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3411c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3412c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3413002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3414002d39b4SEd Tanous                 "CollectDiagnosticData");
3415c5a4c82aSJason M. Bills             return;
3416c5a4c82aSJason M. Bills         }
3417c5a4c82aSJason M. Bills 
3418c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3419c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3420*5e7e2dc5SEd Tanous              taskMatchStr](const boost::system::error_code& ec,
342198be3e39SEd Tanous                            const std::string&) mutable {
34221da66f75SEd Tanous             if (ec)
34231da66f75SEd Tanous             {
3424002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
34251da66f75SEd Tanous                 {
3426f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
34271da66f75SEd Tanous                 }
34284363d3b2SJason M. Bills                 else if (ec.value() ==
34294363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
34304363d3b2SJason M. Bills                 {
3431002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3432002d39b4SEd Tanous                                                             "60");
34334363d3b2SJason M. Bills                 }
34341da66f75SEd Tanous                 else
34351da66f75SEd Tanous                 {
3436f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
34371da66f75SEd Tanous                 }
34381da66f75SEd Tanous                 return;
34391da66f75SEd Tanous             }
3440002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
3441*5e7e2dc5SEd Tanous                 [](const boost::system::error_code& err, sdbusplus::message_t&,
3442002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
344366afe4faSJames Feist                 if (!err)
344466afe4faSJames Feist                 {
3445002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3446e5d5006bSJames Feist                         std::to_string(taskData->index)));
3447831d6b09SJames Feist                     taskData->state = "Completed";
344866afe4faSJames Feist                 }
344932898ceaSJames Feist                 return task::completed;
345066afe4faSJames Feist                 },
3451c5a4c82aSJason M. Bills                 taskMatchStr);
3452c5a4c82aSJason M. Bills 
345346229577SJames Feist             task->startTimer(std::chrono::minutes(5));
345446229577SJames Feist             task->populateResp(asyncResp->res);
345598be3e39SEd Tanous             task->payload.emplace(std::move(payload));
34561da66f75SEd Tanous         };
34578e6c099aSJason M. Bills 
34581da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3459002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3460002d39b4SEd Tanous             iface, method);
34617e860f15SJohn Edward Broadbent         });
34626eda7685SKenny L. Ku }
34636eda7685SKenny L. Ku 
3464cb92c03bSAndrew Geissler /**
3465cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3466cb92c03bSAndrew Geissler  */
34677e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3468cb92c03bSAndrew Geissler {
3469cb92c03bSAndrew Geissler     /**
3470cb92c03bSAndrew Geissler      * Function handles POST method request.
3471cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3472cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3473cb92c03bSAndrew Geissler      */
34747e860f15SJohn Edward Broadbent 
34750fda0f12SGeorge Liu     BMCWEB_ROUTE(
34760fda0f12SGeorge Liu         app,
347722d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3478ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
34797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
348045ca1b86SEd Tanous             [&app](const crow::Request& req,
348122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
348222d268cbSEd Tanous                    const std::string& systemName) {
34833ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
348445ca1b86SEd Tanous         {
348545ca1b86SEd Tanous             return;
348645ca1b86SEd Tanous         }
348722d268cbSEd Tanous         if (systemName != "system")
348822d268cbSEd Tanous         {
348922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
349022d268cbSEd Tanous                                        systemName);
349122d268cbSEd Tanous             return;
349222d268cbSEd Tanous         }
3493cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3494cb92c03bSAndrew Geissler 
3495cb92c03bSAndrew Geissler         // Process response from Logging service.
3496*5e7e2dc5SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code& ec) {
3497002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3498cb92c03bSAndrew Geissler             if (ec)
3499cb92c03bSAndrew Geissler             {
3500cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3501002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3502cb92c03bSAndrew Geissler                 asyncResp->res.result(
3503cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3504cb92c03bSAndrew Geissler                 return;
3505cb92c03bSAndrew Geissler             }
3506cb92c03bSAndrew Geissler 
3507002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3508cb92c03bSAndrew Geissler         };
3509cb92c03bSAndrew Geissler 
3510cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3511cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
35122c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3513cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3514cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
35157e860f15SJohn Edward Broadbent         });
3516cb92c03bSAndrew Geissler }
3517a3316fc6SZhikuiRen 
3518a3316fc6SZhikuiRen /****************************************************
3519a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3520a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3521a3316fc6SZhikuiRen  ******************************************************/
35227e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3523a3316fc6SZhikuiRen {
352422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3525ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3526002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3527002d39b4SEd Tanous             [&app](const crow::Request& req,
352822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
352922d268cbSEd Tanous                    const std::string& systemName) {
35303ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
353145ca1b86SEd Tanous         {
353245ca1b86SEd Tanous             return;
353345ca1b86SEd Tanous         }
353422d268cbSEd Tanous         if (systemName != "system")
353522d268cbSEd Tanous         {
353622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
353722d268cbSEd Tanous                                        systemName);
353822d268cbSEd Tanous             return;
353922d268cbSEd Tanous         }
35401476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
35411476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
35421476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
35431476687dSEd Tanous             "#LogService.v1_1_0.LogService";
35441476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
35451476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3546ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "PostCodes";
35471476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
35481476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
35491476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
35507c8c4058STejas Patil 
35517c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
35522b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
35530fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
35547c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
35557c8c4058STejas Patil             redfishDateTimeOffset.second;
35567c8c4058STejas Patil 
3557a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
35587e860f15SJohn Edward Broadbent             {"target",
35590fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
35607e860f15SJohn Edward Broadbent         });
3561a3316fc6SZhikuiRen }
3562a3316fc6SZhikuiRen 
35637e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3564a3316fc6SZhikuiRen {
35650fda0f12SGeorge Liu     BMCWEB_ROUTE(
35660fda0f12SGeorge Liu         app,
356722d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3568ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3569ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3570432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
35717e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
357245ca1b86SEd Tanous             [&app](const crow::Request& req,
357322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357422d268cbSEd Tanous                    const std::string& systemName) {
35753ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
357645ca1b86SEd Tanous         {
357745ca1b86SEd Tanous             return;
357845ca1b86SEd Tanous         }
357922d268cbSEd Tanous         if (systemName != "system")
358022d268cbSEd Tanous         {
358122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
358222d268cbSEd Tanous                                        systemName);
358322d268cbSEd Tanous             return;
358422d268cbSEd Tanous         }
3585a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3586a3316fc6SZhikuiRen 
3587a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3588a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3589*5e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
3590a3316fc6SZhikuiRen             if (ec)
3591a3316fc6SZhikuiRen             {
3592a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3593002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
35947e860f15SJohn Edward Broadbent                                  << ec;
3595002d39b4SEd Tanous                 asyncResp->res.result(
3596002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3597a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3598a3316fc6SZhikuiRen                 return;
3599a3316fc6SZhikuiRen             }
3600a3316fc6SZhikuiRen             },
360115124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
360215124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3603a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
36047e860f15SJohn Edward Broadbent         });
3605a3316fc6SZhikuiRen }
3606a3316fc6SZhikuiRen 
36076f284d24SJiaqing Zhao /**
36086f284d24SJiaqing Zhao  * @brief Parse post code ID and get the current value and index value
36096f284d24SJiaqing Zhao  *        eg: postCodeID=B1-2, currentValue=1, index=2
36106f284d24SJiaqing Zhao  *
36116f284d24SJiaqing Zhao  * @param[in]  postCodeID     Post Code ID
36126f284d24SJiaqing Zhao  * @param[out] currentValue   Current value
36136f284d24SJiaqing Zhao  * @param[out] index          Index value
36146f284d24SJiaqing Zhao  *
36156f284d24SJiaqing Zhao  * @return bool true if the parsing is successful, false the parsing fails
36166f284d24SJiaqing Zhao  */
36176f284d24SJiaqing Zhao inline static bool parsePostCode(const std::string& postCodeID,
36186f284d24SJiaqing Zhao                                  uint64_t& currentValue, uint16_t& index)
36196f284d24SJiaqing Zhao {
36206f284d24SJiaqing Zhao     std::vector<std::string> split;
362150ebd4afSEd Tanous     bmcweb::split(split, postCodeID, '-');
36226f284d24SJiaqing Zhao     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
36236f284d24SJiaqing Zhao     {
36246f284d24SJiaqing Zhao         return false;
36256f284d24SJiaqing Zhao     }
36266f284d24SJiaqing Zhao 
36276f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36286f284d24SJiaqing Zhao     const char* start = split[0].data() + 1;
36296f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36306f284d24SJiaqing Zhao     const char* end = split[0].data() + split[0].size();
36316f284d24SJiaqing Zhao     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
36326f284d24SJiaqing Zhao 
36336f284d24SJiaqing Zhao     if (ptrIndex != end || ecIndex != std::errc())
36346f284d24SJiaqing Zhao     {
36356f284d24SJiaqing Zhao         return false;
36366f284d24SJiaqing Zhao     }
36376f284d24SJiaqing Zhao 
36386f284d24SJiaqing Zhao     start = split[1].data();
36396f284d24SJiaqing Zhao 
36406f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36416f284d24SJiaqing Zhao     end = split[1].data() + split[1].size();
36426f284d24SJiaqing Zhao     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
36436f284d24SJiaqing Zhao 
36446f284d24SJiaqing Zhao     return ptrValue == end && ecValue == std::errc();
36456f284d24SJiaqing Zhao }
36466f284d24SJiaqing Zhao 
36476f284d24SJiaqing Zhao static bool fillPostCodeEntry(
36488d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
36496c9a279eSManojkiran Eda     const boost::container::flat_map<
36506c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3651a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3652a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3653a3316fc6SZhikuiRen {
3654a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3655fffb8c1fSEd Tanous     const registries::Message* message =
3656fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3657a3316fc6SZhikuiRen 
3658a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3659a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
36606c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
36616c9a279eSManojkiran Eda              code : postcode)
3662a3316fc6SZhikuiRen     {
3663a3316fc6SZhikuiRen         currentCodeIndex++;
3664a3316fc6SZhikuiRen         std::string postcodeEntryID =
3665a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3666a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3667a3316fc6SZhikuiRen 
3668a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3669a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3670a3316fc6SZhikuiRen 
3671a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3672a3316fc6SZhikuiRen         { // already incremented
3673a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3674a3316fc6SZhikuiRen         }
3675a3316fc6SZhikuiRen         else
3676a3316fc6SZhikuiRen         {
3677a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3678a3316fc6SZhikuiRen         }
3679a3316fc6SZhikuiRen 
3680a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3681a3316fc6SZhikuiRen         // not fall between top and skip
3682a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3683a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3684a3316fc6SZhikuiRen         {
3685a3316fc6SZhikuiRen             continue;
3686a3316fc6SZhikuiRen         }
3687a3316fc6SZhikuiRen 
36884e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3689a3316fc6SZhikuiRen         // currentIndex
3690a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3691a3316fc6SZhikuiRen         {
3692a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3693a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3694a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3695a3316fc6SZhikuiRen             continue;
3696a3316fc6SZhikuiRen         }
3697a3316fc6SZhikuiRen 
3698a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3699a3316fc6SZhikuiRen         // index
3700a3316fc6SZhikuiRen 
3701a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3702a3316fc6SZhikuiRen         std::string entryTimeStr;
37032a025611SKonstantin Aladyshev         entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
3704a3316fc6SZhikuiRen 
3705a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3706a3316fc6SZhikuiRen         std::ostringstream hexCode;
3707a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
37086c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3709a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3710a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3711a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3712a3316fc6SZhikuiRen         // Set precision to 4 digits
3713a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3714a3316fc6SZhikuiRen         // Add double to stream
3715a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3716a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3717a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3718a3316fc6SZhikuiRen 
3719a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3720a3316fc6SZhikuiRen         std::string msg;
3721a3316fc6SZhikuiRen         if (message != nullptr)
3722a3316fc6SZhikuiRen         {
3723a3316fc6SZhikuiRen             msg = message->message;
3724a3316fc6SZhikuiRen 
3725a3316fc6SZhikuiRen             // fill in this post code value
3726a3316fc6SZhikuiRen             int i = 0;
3727a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3728a3316fc6SZhikuiRen             {
3729a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3730a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3731a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3732a3316fc6SZhikuiRen                 {
3733a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3734a3316fc6SZhikuiRen                 }
3735a3316fc6SZhikuiRen             }
3736a3316fc6SZhikuiRen         }
3737a3316fc6SZhikuiRen 
3738d4342a92STim Lee         // Get Severity template from message registry
3739d4342a92STim Lee         std::string severity;
3740d4342a92STim Lee         if (message != nullptr)
3741d4342a92STim Lee         {
37425f2b84eeSEd Tanous             severity = message->messageSeverity;
3743d4342a92STim Lee         }
3744d4342a92STim Lee 
37456f284d24SJiaqing Zhao         // Format entry
37466f284d24SJiaqing Zhao         nlohmann::json::object_t bmcLogEntry;
37479c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3748eddfc437SWilly Tu         bmcLogEntry["@odata.id"] = crow::utility::urlFromPieces(
3749eddfc437SWilly Tu             "redfish", "v1", "Systems", "system", "LogServices", "PostCodes",
3750eddfc437SWilly Tu             "Entries", postcodeEntryID);
375184afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
375284afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
375384afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
375484afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
375584afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
375684afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
375784afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
375884afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3759647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3760647b3cdcSGeorge Liu         {
3761647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3762647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3763647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3764647b3cdcSGeorge Liu         }
37656f284d24SJiaqing Zhao 
37666f284d24SJiaqing Zhao         // codeIndex is only specified when querying single entry, return only
37676f284d24SJiaqing Zhao         // that entry in this case
37686f284d24SJiaqing Zhao         if (codeIndex != 0)
37696f284d24SJiaqing Zhao         {
37706f284d24SJiaqing Zhao             aResp->res.jsonValue.update(bmcLogEntry);
37716f284d24SJiaqing Zhao             return true;
3772a3316fc6SZhikuiRen         }
37736f284d24SJiaqing Zhao 
37746f284d24SJiaqing Zhao         nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
37756f284d24SJiaqing Zhao         logEntryArray.push_back(std::move(bmcLogEntry));
37766f284d24SJiaqing Zhao     }
37776f284d24SJiaqing Zhao 
37786f284d24SJiaqing Zhao     // Return value is always false when querying multiple entries
37796f284d24SJiaqing Zhao     return false;
3780a3316fc6SZhikuiRen }
3781a3316fc6SZhikuiRen 
37828d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
37836f284d24SJiaqing Zhao                                 const std::string& entryId)
3784a3316fc6SZhikuiRen {
37856f284d24SJiaqing Zhao     uint16_t bootIndex = 0;
37866f284d24SJiaqing Zhao     uint64_t codeIndex = 0;
37876f284d24SJiaqing Zhao     if (!parsePostCode(entryId, codeIndex, bootIndex))
37886f284d24SJiaqing Zhao     {
37896f284d24SJiaqing Zhao         // Requested ID was not found
37906f284d24SJiaqing Zhao         messages::resourceNotFound(aResp->res, "LogEntry", entryId);
37916f284d24SJiaqing Zhao         return;
37926f284d24SJiaqing Zhao     }
37936f284d24SJiaqing Zhao 
37946f284d24SJiaqing Zhao     if (bootIndex == 0 || codeIndex == 0)
37956f284d24SJiaqing Zhao     {
37966f284d24SJiaqing Zhao         // 0 is an invalid index
37976f284d24SJiaqing Zhao         messages::resourceNotFound(aResp->res, "LogEntry", entryId);
37986f284d24SJiaqing Zhao         return;
37996f284d24SJiaqing Zhao     }
38006f284d24SJiaqing Zhao 
3801a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
38026f284d24SJiaqing Zhao         [aResp, entryId, bootIndex,
3803*5e7e2dc5SEd Tanous          codeIndex](const boost::system::error_code& ec,
38046c9a279eSManojkiran Eda                     const boost::container::flat_map<
38056c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38066c9a279eSManojkiran Eda                         postcode) {
3807a3316fc6SZhikuiRen         if (ec)
3808a3316fc6SZhikuiRen         {
3809a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3810a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3811a3316fc6SZhikuiRen             return;
3812a3316fc6SZhikuiRen         }
3813a3316fc6SZhikuiRen 
3814a3316fc6SZhikuiRen         if (postcode.empty())
3815a3316fc6SZhikuiRen         {
38166f284d24SJiaqing Zhao             messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3817a3316fc6SZhikuiRen             return;
3818a3316fc6SZhikuiRen         }
3819a3316fc6SZhikuiRen 
38206f284d24SJiaqing Zhao         if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
38216f284d24SJiaqing Zhao         {
38226f284d24SJiaqing Zhao             messages::resourceNotFound(aResp->res, "LogEntry", entryId);
38236f284d24SJiaqing Zhao             return;
38246f284d24SJiaqing Zhao         }
3825a3316fc6SZhikuiRen         },
382615124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
382715124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3828a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3829a3316fc6SZhikuiRen         bootIndex);
3830a3316fc6SZhikuiRen }
3831a3316fc6SZhikuiRen 
38328d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3833a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3834a3316fc6SZhikuiRen                                const uint16_t bootCount,
38353648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
38363648c8beSEd Tanous                                size_t top)
3837a3316fc6SZhikuiRen {
3838a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3839a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3840*5e7e2dc5SEd Tanous          top](const boost::system::error_code& ec,
38416c9a279eSManojkiran Eda               const boost::container::flat_map<
38426c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38436c9a279eSManojkiran Eda                   postcode) {
3844a3316fc6SZhikuiRen         if (ec)
3845a3316fc6SZhikuiRen         {
3846a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3847a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3848a3316fc6SZhikuiRen             return;
3849a3316fc6SZhikuiRen         }
3850a3316fc6SZhikuiRen 
3851a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3852a3316fc6SZhikuiRen         if (!postcode.empty())
3853a3316fc6SZhikuiRen         {
3854a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
38553648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3856a3316fc6SZhikuiRen             {
38573648c8beSEd Tanous                 uint64_t thisBootSkip =
38583648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
38593648c8beSEd Tanous                     entryCount;
3860a3316fc6SZhikuiRen                 uint64_t thisBootTop =
38613648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
38623648c8beSEd Tanous                     entryCount;
3863a3316fc6SZhikuiRen 
3864002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3865002d39b4SEd Tanous                                   thisBootTop);
3866a3316fc6SZhikuiRen             }
3867a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3868a3316fc6SZhikuiRen         }
3869a3316fc6SZhikuiRen 
3870a3316fc6SZhikuiRen         // continue to previous bootIndex
3871a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3872a3316fc6SZhikuiRen         {
3873a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3874a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3875a3316fc6SZhikuiRen         }
387681584abeSJiaqing Zhao         else if (skip + top < endCount)
3877a3316fc6SZhikuiRen         {
3878a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
38790fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3880a3316fc6SZhikuiRen                 std::to_string(skip + top);
3881a3316fc6SZhikuiRen         }
3882a3316fc6SZhikuiRen         },
388315124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
388415124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3885a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3886a3316fc6SZhikuiRen         bootIndex);
3887a3316fc6SZhikuiRen }
3888a3316fc6SZhikuiRen 
38898d1b46d7Szhanghch05 static void
38908d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
38913648c8beSEd Tanous                          size_t skip, size_t top)
3892a3316fc6SZhikuiRen {
3893a3316fc6SZhikuiRen     uint64_t entryCount = 0;
38941e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
38951e1e598dSJonathan Doman         *crow::connections::systemBus,
38961e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
38971e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
38981e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
3899*5e7e2dc5SEd Tanous         [aResp, entryCount, skip, top](const boost::system::error_code& ec,
39001e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3901a3316fc6SZhikuiRen         if (ec)
3902a3316fc6SZhikuiRen         {
3903a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3904a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3905a3316fc6SZhikuiRen             return;
3906a3316fc6SZhikuiRen         }
39071e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
39081e1e598dSJonathan Doman         });
3909a3316fc6SZhikuiRen }
3910a3316fc6SZhikuiRen 
39117e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3912a3316fc6SZhikuiRen {
39137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
391422d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3915ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
39167e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
391745ca1b86SEd Tanous             [&app](const crow::Request& req,
391822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
391922d268cbSEd Tanous                    const std::string& systemName) {
3920c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3921c937d2bfSEd Tanous             .canDelegateTop = true,
3922c937d2bfSEd Tanous             .canDelegateSkip = true,
3923c937d2bfSEd Tanous         };
3924c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3925c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
39263ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
392745ca1b86SEd Tanous         {
392845ca1b86SEd Tanous             return;
392945ca1b86SEd Tanous         }
393022d268cbSEd Tanous 
393122d268cbSEd Tanous         if (systemName != "system")
393222d268cbSEd Tanous         {
393322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
393422d268cbSEd Tanous                                        systemName);
393522d268cbSEd Tanous             return;
393622d268cbSEd Tanous         }
3937a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3938a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3939a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3940a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3941a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3942a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3943a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3944a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3945a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
39463648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
39475143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
39483648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
39497e860f15SJohn Edward Broadbent         });
3950a3316fc6SZhikuiRen }
3951a3316fc6SZhikuiRen 
3952647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3953647b3cdcSGeorge Liu {
39540fda0f12SGeorge Liu     BMCWEB_ROUTE(
39550fda0f12SGeorge Liu         app,
395622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
3957647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3958647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
395945ca1b86SEd Tanous             [&app](const crow::Request& req,
3960647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
396122d268cbSEd Tanous                    const std::string& systemName,
3962647b3cdcSGeorge Liu                    const std::string& postCodeID) {
39633ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
396445ca1b86SEd Tanous         {
396545ca1b86SEd Tanous             return;
396645ca1b86SEd Tanous         }
396799351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
396899351cd8SEd Tanous                 req.getHeaderValue("Accept"),
39694a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
3970647b3cdcSGeorge Liu         {
3971002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3972647b3cdcSGeorge Liu             return;
3973647b3cdcSGeorge Liu         }
397422d268cbSEd Tanous         if (systemName != "system")
397522d268cbSEd Tanous         {
397622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
397722d268cbSEd Tanous                                        systemName);
397822d268cbSEd Tanous             return;
397922d268cbSEd Tanous         }
3980647b3cdcSGeorge Liu 
3981647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3982647b3cdcSGeorge Liu         uint16_t index = 0;
3983647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3984647b3cdcSGeorge Liu         {
3985002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3986647b3cdcSGeorge Liu             return;
3987647b3cdcSGeorge Liu         }
3988647b3cdcSGeorge Liu 
3989647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3990647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3991*5e7e2dc5SEd Tanous                 const boost::system::error_code& ec,
3992002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3993002d39b4SEd Tanous                     postcodes) {
3994647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3995647b3cdcSGeorge Liu             {
3996002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3997002d39b4SEd Tanous                                            postCodeID);
3998647b3cdcSGeorge Liu                 return;
3999647b3cdcSGeorge Liu             }
4000647b3cdcSGeorge Liu             if (ec)
4001647b3cdcSGeorge Liu             {
4002647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4003647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
4004647b3cdcSGeorge Liu                 return;
4005647b3cdcSGeorge Liu             }
4006647b3cdcSGeorge Liu 
4007647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
4008002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
4009647b3cdcSGeorge Liu             {
4010647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
4011002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4012002d39b4SEd Tanous                                            postCodeID);
4013647b3cdcSGeorge Liu                 return;
4014647b3cdcSGeorge Liu             }
4015647b3cdcSGeorge Liu 
40169eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
401746ff87baSEd Tanous             if (c.empty())
4018647b3cdcSGeorge Liu             {
4019647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
4020002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4021002d39b4SEd Tanous                                            postCodeID);
4022647b3cdcSGeorge Liu                 return;
4023647b3cdcSGeorge Liu             }
402446ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
402546ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
402646ff87baSEd Tanous             std::string_view strData(d, c.size());
4027647b3cdcSGeorge Liu 
4028d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
4029647b3cdcSGeorge Liu                                      "application/octet-stream");
4030d9f6c621SEd Tanous             asyncResp->res.addHeader(
4031d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
4032002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
4033647b3cdcSGeorge Liu             },
4034647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
4035647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
4036002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4037647b3cdcSGeorge Liu         });
4038647b3cdcSGeorge Liu }
4039647b3cdcSGeorge Liu 
40407e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
4041a3316fc6SZhikuiRen {
40427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
404322d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
4044ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
40457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
404645ca1b86SEd Tanous             [&app](const crow::Request& req,
40477e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
404822d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
40493ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
405045ca1b86SEd Tanous         {
405145ca1b86SEd Tanous             return;
405245ca1b86SEd Tanous         }
405322d268cbSEd Tanous         if (systemName != "system")
405422d268cbSEd Tanous         {
405522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
405622d268cbSEd Tanous                                        systemName);
405722d268cbSEd Tanous             return;
405822d268cbSEd Tanous         }
405922d268cbSEd Tanous 
40606f284d24SJiaqing Zhao         getPostCodeForEntry(asyncResp, targetID);
40617e860f15SJohn Edward Broadbent         });
4062a3316fc6SZhikuiRen }
4063a3316fc6SZhikuiRen 
40641da66f75SEd Tanous } // namespace redfish
4065