xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision eddfc437edfc871b469199552bdeb0d65ee2dcf3)
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 
944851d45dSJason M. Bills static const Message* getMessage(const 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,
16039e77504SEd Tanous                                      const 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,
1827e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1837e860f15SJohn Edward Broadbent                                      const int& base, 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,
458711ac7a9SEd 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,
564fdd26906SClaire Weinan          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 =
645002d39b4SEd 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)},
7378e31778eSAsmitha Karunanithi          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](
7855b378546SPatrick Williams                 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(
9165b378546SPatrick Williams         [asyncResp, payload(task::Payload(req)), dumpPath](
9175b378546SPatrick Williams             const boost::system::error_code ec, const sdbusplus::message_t& msg,
9188e31778eSAsmitha Karunanithi             const sdbusplus::message::object_path& objPath) mutable {
919a43be80fSAsmitha Karunanithi         if (ec)
920a43be80fSAsmitha Karunanithi         {
921a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
9225907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
9235907571dSAsmitha Karunanithi             if (dbusError == nullptr)
9245907571dSAsmitha Karunanithi             {
9255907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
9265907571dSAsmitha Karunanithi                 return;
9275907571dSAsmitha Karunanithi             }
9285907571dSAsmitha Karunanithi 
9295907571dSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
9305907571dSAsmitha Karunanithi                              << " and error msg: " << dbusError->message;
9315907571dSAsmitha Karunanithi             if (std::string_view(
9325907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
9335907571dSAsmitha Karunanithi                 dbusError->name)
9345907571dSAsmitha Karunanithi             {
9355907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
9365907571dSAsmitha Karunanithi                 return;
9375907571dSAsmitha Karunanithi             }
9385907571dSAsmitha Karunanithi             if (std::string_view(
9395907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
9405907571dSAsmitha Karunanithi                 dbusError->name)
9415907571dSAsmitha Karunanithi             {
9425907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
9435907571dSAsmitha Karunanithi                 return;
9445907571dSAsmitha Karunanithi             }
9455907571dSAsmitha Karunanithi             if (std::string_view(
9465907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
9475907571dSAsmitha Karunanithi                 dbusError->name)
9485907571dSAsmitha Karunanithi             {
9495907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
9505907571dSAsmitha Karunanithi                 return;
9515907571dSAsmitha Karunanithi             }
9525907571dSAsmitha Karunanithi             // Other Dbus errors such as:
9535907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
9545907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
9555907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
9565907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
9575907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
9585907571dSAsmitha Karunanithi             // back to the client.
959a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
960a43be80fSAsmitha Karunanithi             return;
961a43be80fSAsmitha Karunanithi         }
9628e31778eSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
9638e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
964a43be80fSAsmitha Karunanithi         },
965b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
966b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
967b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
9688e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
969a43be80fSAsmitha Karunanithi }
970a43be80fSAsmitha Karunanithi 
9718d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9728d1b46d7Szhanghch05                       const std::string& dumpType)
97380319af1SAsmitha Karunanithi {
974b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
975b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
9768d1b46d7Szhanghch05 
9770d946211SClaire Weinan     crow::connections::systemBus->async_method_call(
9780d946211SClaire Weinan         [asyncResp](const boost::system::error_code& ec) {
97980319af1SAsmitha Karunanithi         if (ec)
98080319af1SAsmitha Karunanithi         {
9810d946211SClaire Weinan             BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
98280319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
98380319af1SAsmitha Karunanithi             return;
98480319af1SAsmitha Karunanithi         }
9850d946211SClaire Weinan         },
9860d946211SClaire Weinan         "xyz.openbmc_project.Dump.Manager",
9870d946211SClaire Weinan         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
9880d946211SClaire Weinan         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
98980319af1SAsmitha Karunanithi }
99080319af1SAsmitha Karunanithi 
991b9d36b47SEd Tanous inline static void
992b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
993b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
994b9d36b47SEd Tanous                              std::string& logfile)
995043a0536SJohnathan Mantey {
996d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
997d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
998d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
999d1bde9e5SKrzysztof Grobelny 
1000d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1001d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1002d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1003d1bde9e5SKrzysztof Grobelny 
1004d1bde9e5SKrzysztof Grobelny     if (!success)
1005043a0536SJohnathan Mantey     {
1006d1bde9e5SKrzysztof Grobelny         return;
1007043a0536SJohnathan Mantey     }
1008d1bde9e5SKrzysztof Grobelny 
1009d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1010043a0536SJohnathan Mantey     {
1011d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1012d1bde9e5SKrzysztof Grobelny     }
1013d1bde9e5SKrzysztof Grobelny 
1014d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1015043a0536SJohnathan Mantey     {
1016d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1017043a0536SJohnathan Mantey     }
1018d1bde9e5SKrzysztof Grobelny 
1019d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1020043a0536SJohnathan Mantey     {
1021d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1022043a0536SJohnathan Mantey     }
1023043a0536SJohnathan Mantey }
1024043a0536SJohnathan Mantey 
10257e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
10261da66f75SEd Tanous {
1027c4bf6374SJason M. Bills     /**
1028c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1029c4bf6374SJason M. Bills      */
103022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1031ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1032002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1033002d39b4SEd Tanous             [&app](const crow::Request& req,
103422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
103522d268cbSEd Tanous                    const std::string& systemName) {
10363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1037c4bf6374SJason M. Bills         {
103845ca1b86SEd Tanous             return;
103945ca1b86SEd Tanous         }
104022d268cbSEd Tanous         if (systemName != "system")
104122d268cbSEd Tanous         {
104222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
104322d268cbSEd Tanous                                        systemName);
104422d268cbSEd Tanous             return;
104522d268cbSEd Tanous         }
104622d268cbSEd Tanous 
10477e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
10487e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1049c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1050c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1051c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1052029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
105345ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1054c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1055c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1056002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1057c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
10581476687dSEd Tanous         nlohmann::json::object_t eventLog;
10591476687dSEd Tanous         eventLog["@odata.id"] =
10601476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
10611476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
10625cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
10631476687dSEd Tanous         nlohmann::json::object_t dumpLog;
1064002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
10651476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
1066c9bb6861Sraviteja-b #endif
1067c9bb6861Sraviteja-b 
1068d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
10691476687dSEd Tanous         nlohmann::json::object_t crashdump;
10701476687dSEd Tanous         crashdump["@odata.id"] =
10711476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
10721476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
1073d53dd41fSJason M. Bills #endif
1074b7028ebfSSpencer Ku 
1075b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
10761476687dSEd Tanous         nlohmann::json::object_t hostlogger;
10771476687dSEd Tanous         hostlogger["@odata.id"] =
10781476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
10791476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
1080b7028ebfSSpencer Ku #endif
1081c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1082c4bf6374SJason M. Bills             logServiceArray.size();
1083a3316fc6SZhikuiRen 
10847a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
10857a1dbc48SGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode"};
10867a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
10877a1dbc48SGeorge Liu             "/", 0, interfaces,
10887a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
1089b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1090b9d36b47SEd Tanous                             subtreePath) {
1091a3316fc6SZhikuiRen             if (ec)
1092a3316fc6SZhikuiRen             {
1093a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
1094a3316fc6SZhikuiRen                 return;
1095a3316fc6SZhikuiRen             }
1096a3316fc6SZhikuiRen 
109755f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1098a3316fc6SZhikuiRen             {
1099a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1100a3316fc6SZhikuiRen                 {
110123a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1102a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1103613dabeaSEd Tanous                     nlohmann::json::object_t member;
1104613dabeaSEd Tanous                     member["@odata.id"] =
1105613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
1106613dabeaSEd Tanous 
1107613dabeaSEd Tanous                     logServiceArrayLocal.push_back(std::move(member));
1108613dabeaSEd Tanous 
110945ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
111023a21a1cSEd Tanous                         logServiceArrayLocal.size();
1111a3316fc6SZhikuiRen                     return;
1112a3316fc6SZhikuiRen                 }
1113a3316fc6SZhikuiRen             }
11147a1dbc48SGeorge Liu             });
11157e860f15SJohn Edward Broadbent         });
1116c4bf6374SJason M. Bills }
1117c4bf6374SJason M. Bills 
11187e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1119c4bf6374SJason M. Bills {
112022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1121ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1122002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1123002d39b4SEd Tanous             [&app](const crow::Request& req,
112422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112522d268cbSEd Tanous                    const std::string& systemName) {
11263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
112745ca1b86SEd Tanous         {
112845ca1b86SEd Tanous             return;
112945ca1b86SEd Tanous         }
113022d268cbSEd Tanous         if (systemName != "system")
113122d268cbSEd Tanous         {
113222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
113322d268cbSEd Tanous                                        systemName);
113422d268cbSEd Tanous             return;
113522d268cbSEd Tanous         }
1136c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1137029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1138c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1139c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1140c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1141002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1142c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1143c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
11447c8c4058STejas Patil 
11457c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
11462b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
11477c8c4058STejas Patil 
11487c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
11497c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
11507c8c4058STejas Patil             redfishDateTimeOffset.second;
11517c8c4058STejas Patil 
11521476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
11531476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1154e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1155e7d6c8b2SGunnar Mills 
11560fda0f12SGeorge Liu             {"target",
11570fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
11587e860f15SJohn Edward Broadbent         });
1159489640c6SJason M. Bills }
1160489640c6SJason M. Bills 
11617e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1162489640c6SJason M. Bills {
11634978b63fSJason M. Bills     BMCWEB_ROUTE(
11644978b63fSJason M. Bills         app,
116522d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1166432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
11677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
116845ca1b86SEd Tanous             [&app](const crow::Request& req,
116922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117022d268cbSEd Tanous                    const std::string& systemName) {
11713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117245ca1b86SEd Tanous         {
117345ca1b86SEd Tanous             return;
117445ca1b86SEd Tanous         }
117522d268cbSEd Tanous         if (systemName != "system")
117622d268cbSEd Tanous         {
117722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
117822d268cbSEd Tanous                                        systemName);
117922d268cbSEd Tanous             return;
118022d268cbSEd Tanous         }
1181489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1182489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1183489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1184489640c6SJason M. Bills         {
1185489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1186489640c6SJason M. Bills             {
1187489640c6SJason M. Bills                 std::error_code ec;
1188489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1189489640c6SJason M. Bills             }
1190489640c6SJason M. Bills         }
1191489640c6SJason M. Bills 
1192489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1193489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1194489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1195489640c6SJason M. Bills             if (ec)
1196489640c6SJason M. Bills             {
1197002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1198489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1199489640c6SJason M. Bills                 return;
1200489640c6SJason M. Bills             }
1201489640c6SJason M. Bills 
1202489640c6SJason M. Bills             messages::success(asyncResp->res);
1203489640c6SJason M. Bills             },
1204489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1205002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1206002d39b4SEd Tanous             "replace");
12077e860f15SJohn Edward Broadbent         });
1208c4bf6374SJason M. Bills }
1209c4bf6374SJason M. Bills 
1210ac992cdeSJason M. Bills enum class LogParseError
1211ac992cdeSJason M. Bills {
1212ac992cdeSJason M. Bills     success,
1213ac992cdeSJason M. Bills     parseFailed,
1214ac992cdeSJason M. Bills     messageIdNotInRegistry,
1215ac992cdeSJason M. Bills };
1216ac992cdeSJason M. Bills 
1217ac992cdeSJason M. Bills static LogParseError
1218ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1219b5a76932SEd Tanous                           const std::string& logEntry,
1220de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1221c4bf6374SJason M. Bills {
122295820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1223cd225da8SJason M. Bills     // First get the Timestamp
1224f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1225cd225da8SJason M. Bills     if (space == std::string::npos)
122695820184SJason M. Bills     {
1227ac992cdeSJason M. Bills         return LogParseError::parseFailed;
122895820184SJason M. Bills     }
1229cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1230cd225da8SJason M. Bills     // Then get the log contents
1231f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1232cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1233cd225da8SJason M. Bills     {
1234ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1235cd225da8SJason M. Bills     }
1236cd225da8SJason M. Bills     std::string_view entry(logEntry);
1237cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1238cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1239cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
124050ebd4afSEd Tanous     bmcweb::split(logEntryFields, entry, ',');
1241cd225da8SJason M. Bills     // We need at least a MessageId to be valid
124226f6976fSEd Tanous     if (logEntryFields.empty())
1243cd225da8SJason M. Bills     {
1244ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1245cd225da8SJason M. Bills     }
1246cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
124795820184SJason M. Bills 
12484851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1249fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1250c4bf6374SJason M. Bills 
125154417b02SSui Chen     if (message == nullptr)
1252c4bf6374SJason M. Bills     {
125354417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1254ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1255c4bf6374SJason M. Bills     }
1256c4bf6374SJason M. Bills 
125754417b02SSui Chen     std::string msg = message->message;
125854417b02SSui Chen 
125915a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
126026702d01SEd Tanous     std::span<std::string> messageArgs;
126115a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
126215a86ff6SJason M. Bills     {
126315a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
126415a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
126515a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
126615a86ff6SJason M. Bills         if (!messageArgsStart.empty())
126715a86ff6SJason M. Bills         {
126815a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
126915a86ff6SJason M. Bills         }
127015a86ff6SJason M. Bills 
127123a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1272c4bf6374SJason M. Bills 
12734851d45dSJason M. Bills         // Fill the MessageArgs into the Message
127495820184SJason M. Bills         int i = 0;
127595820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
12764851d45dSJason M. Bills         {
127795820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
12784851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
12794851d45dSJason M. Bills             if (argPos != std::string::npos)
12804851d45dSJason M. Bills             {
128195820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
12824851d45dSJason M. Bills             }
12834851d45dSJason M. Bills         }
128415a86ff6SJason M. Bills     }
12854851d45dSJason M. Bills 
128695820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
128795820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
128895820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1289f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1290f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
129195820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1292c4bf6374SJason M. Bills     {
129395820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1294c4bf6374SJason M. Bills     }
1295c4bf6374SJason M. Bills 
1296c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
12979c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1298*eddfc437SWilly Tu     logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
1299*eddfc437SWilly Tu         "redfish", "v1", "Systems", "system", "LogServices", "EventLog",
1300*eddfc437SWilly Tu         "Entries", logEntryID);
130184afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
130284afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
130384afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
130484afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
130584afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
130684afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
130784afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
130884afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1309ac992cdeSJason M. Bills     return LogParseError::success;
1310c4bf6374SJason M. Bills }
1311c4bf6374SJason M. Bills 
13127e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1313c4bf6374SJason M. Bills {
131422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
13158b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1316002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1317002d39b4SEd Tanous             [&app](const crow::Request& req,
131822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
131922d268cbSEd Tanous                    const std::string& systemName) {
1320c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1321c937d2bfSEd Tanous             .canDelegateTop = true,
1322c937d2bfSEd Tanous             .canDelegateSkip = true,
1323c937d2bfSEd Tanous         };
1324c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1325c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
13263ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1327c4bf6374SJason M. Bills         {
1328c4bf6374SJason M. Bills             return;
1329c4bf6374SJason M. Bills         }
133022d268cbSEd Tanous         if (systemName != "system")
133122d268cbSEd Tanous         {
133222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
133322d268cbSEd Tanous                                        systemName);
133422d268cbSEd Tanous             return;
133522d268cbSEd Tanous         }
133622d268cbSEd Tanous 
13375143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
13383648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
13393648c8beSEd Tanous 
13407e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13417e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1342c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1343c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1344c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1345029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1346c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1347c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1348c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1349cb92c03bSAndrew Geissler 
13504978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1351c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
13527e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
13537e860f15SJohn Edward Broadbent         // entry
135495820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
135595820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1356b01bf299SEd Tanous         uint64_t entryCount = 0;
1357cd225da8SJason M. Bills         std::string logEntry;
135895820184SJason M. Bills 
13597e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
13607e860f15SJohn Edward Broadbent         // backwards
1361002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1362002d39b4SEd Tanous              it++)
1363c4bf6374SJason M. Bills         {
1364cd225da8SJason M. Bills             std::ifstream logStream(*it);
136595820184SJason M. Bills             if (!logStream.is_open())
1366c4bf6374SJason M. Bills             {
1367c4bf6374SJason M. Bills                 continue;
1368c4bf6374SJason M. Bills             }
1369c4bf6374SJason M. Bills 
1370e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1371e85d6b16SJason M. Bills             bool firstEntry = true;
137295820184SJason M. Bills             while (std::getline(logStream, logEntry))
137395820184SJason M. Bills             {
1374c4bf6374SJason M. Bills                 std::string idStr;
1375e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1376c4bf6374SJason M. Bills                 {
1377c4bf6374SJason M. Bills                     continue;
1378c4bf6374SJason M. Bills                 }
1379e85d6b16SJason M. Bills                 firstEntry = false;
1380e85d6b16SJason M. Bills 
1381de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1382ac992cdeSJason M. Bills                 LogParseError status =
1383ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1384ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1385ac992cdeSJason M. Bills                 {
1386ac992cdeSJason M. Bills                     continue;
1387ac992cdeSJason M. Bills                 }
1388ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1389c4bf6374SJason M. Bills                 {
1390c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1391c4bf6374SJason M. Bills                     return;
1392c4bf6374SJason M. Bills                 }
1393de703c5dSJason M. Bills 
1394de703c5dSJason M. Bills                 entryCount++;
1395de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1396de703c5dSJason M. Bills                 // start) and top (number of entries to display)
13973648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1398de703c5dSJason M. Bills                 {
1399de703c5dSJason M. Bills                     continue;
1400de703c5dSJason M. Bills                 }
1401de703c5dSJason M. Bills 
1402de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1403c4bf6374SJason M. Bills             }
140495820184SJason M. Bills         }
1405c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
14063648c8beSEd Tanous         if (skip + top < entryCount)
1407c4bf6374SJason M. Bills         {
1408c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
14094978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
14103648c8beSEd Tanous                 std::to_string(skip + top);
1411c4bf6374SJason M. Bills         }
14127e860f15SJohn Edward Broadbent         });
1413897967deSJason M. Bills }
1414897967deSJason M. Bills 
14157e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1416897967deSJason M. Bills {
14177e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
141822d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1419ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
14207e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
142145ca1b86SEd Tanous             [&app](const crow::Request& req,
14227e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
142322d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
14243ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
142545ca1b86SEd Tanous         {
142645ca1b86SEd Tanous             return;
142745ca1b86SEd Tanous         }
142822d268cbSEd Tanous 
142922d268cbSEd Tanous         if (systemName != "system")
143022d268cbSEd Tanous         {
143122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
143222d268cbSEd Tanous                                        systemName);
143322d268cbSEd Tanous             return;
143422d268cbSEd Tanous         }
143522d268cbSEd Tanous 
14367e860f15SJohn Edward Broadbent         const std::string& targetID = param;
14378d1b46d7Szhanghch05 
14387e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
14397e860f15SJohn Edward Broadbent         // entry to find the target entry
1440897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1441897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1442897967deSJason M. Bills         std::string logEntry;
1443897967deSJason M. Bills 
14447e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14457e860f15SJohn Edward Broadbent         // backwards
1446002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1447002d39b4SEd Tanous              it++)
1448897967deSJason M. Bills         {
1449897967deSJason M. Bills             std::ifstream logStream(*it);
1450897967deSJason M. Bills             if (!logStream.is_open())
1451897967deSJason M. Bills             {
1452897967deSJason M. Bills                 continue;
1453897967deSJason M. Bills             }
1454897967deSJason M. Bills 
1455897967deSJason M. Bills             // Reset the unique ID on the first entry
1456897967deSJason M. Bills             bool firstEntry = true;
1457897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1458897967deSJason M. Bills             {
1459897967deSJason M. Bills                 std::string idStr;
1460897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1461897967deSJason M. Bills                 {
1462897967deSJason M. Bills                     continue;
1463897967deSJason M. Bills                 }
1464897967deSJason M. Bills                 firstEntry = false;
1465897967deSJason M. Bills 
1466897967deSJason M. Bills                 if (idStr == targetID)
1467897967deSJason M. Bills                 {
1468de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1469ac992cdeSJason M. Bills                     LogParseError status =
1470ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1471ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1472897967deSJason M. Bills                     {
1473897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1474897967deSJason M. Bills                         return;
1475897967deSJason M. Bills                     }
1476d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1477897967deSJason M. Bills                     return;
1478897967deSJason M. Bills                 }
1479897967deSJason M. Bills             }
1480897967deSJason M. Bills         }
1481897967deSJason M. Bills         // Requested ID was not found
14829db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
14837e860f15SJohn Edward Broadbent         });
148408a4e4b5SAnthony Wilson }
148508a4e4b5SAnthony Wilson 
14867e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
148708a4e4b5SAnthony Wilson {
148822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1489ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1490002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1491002d39b4SEd Tanous             [&app](const crow::Request& req,
149222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
149322d268cbSEd Tanous                    const std::string& systemName) {
14943ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
149545ca1b86SEd Tanous         {
149645ca1b86SEd Tanous             return;
149745ca1b86SEd Tanous         }
149822d268cbSEd Tanous         if (systemName != "system")
149922d268cbSEd Tanous         {
150022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
150122d268cbSEd Tanous                                        systemName);
150222d268cbSEd Tanous             return;
150322d268cbSEd Tanous         }
150422d268cbSEd Tanous 
15057e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
15067e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
150708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
150808a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
150908a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
151008a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
151108a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
151208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
151308a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
151408a4e4b5SAnthony Wilson 
1515cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1516cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1517cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1518cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1519914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1520cb92c03bSAndrew Geissler             if (ec)
1521cb92c03bSAndrew Geissler             {
1522cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1523cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1524002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1525cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1526cb92c03bSAndrew Geissler                 return;
1527cb92c03bSAndrew Geissler             }
1528002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1529cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
15309eb808c1SEd Tanous             for (const auto& objectPath : resp)
1531cb92c03bSAndrew Geissler             {
1532914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1533c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1534c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1535914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1536914e2d5dSEd Tanous                 const std::string* message = nullptr;
1537914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
15389c11a172SVijay Lobo                 const std::string* resolution = nullptr;
153975710de2SXiaochao Ma                 bool resolved = false;
15409017faf2SAbhishek Patel                 const std::string* notify = nullptr;
15419017faf2SAbhishek Patel 
15429eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1543f86bb901SAdriana Kobylak                 {
1544f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1545f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1546f86bb901SAdriana Kobylak                     {
1547002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1548cb92c03bSAndrew Geissler                         {
1549cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1550cb92c03bSAndrew Geissler                             {
1551002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1552cb92c03bSAndrew Geissler                             }
1553cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1554cb92c03bSAndrew Geissler                             {
1555002d39b4SEd Tanous                                 timestamp =
1556002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15577e860f15SJohn Edward Broadbent                             }
1558002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
15597e860f15SJohn Edward Broadbent                             {
1560002d39b4SEd Tanous                                 updateTimestamp =
1561002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15627e860f15SJohn Edward Broadbent                             }
15637e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
15647e860f15SJohn Edward Broadbent                             {
15657e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
15667e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15677e860f15SJohn Edward Broadbent                             }
15689c11a172SVijay Lobo                             else if (propertyMap.first == "Resolution")
15699c11a172SVijay Lobo                             {
15709c11a172SVijay Lobo                                 resolution = std::get_if<std::string>(
15719c11a172SVijay Lobo                                     &propertyMap.second);
15729c11a172SVijay Lobo                             }
15737e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
15747e860f15SJohn Edward Broadbent                             {
15757e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
15767e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15777e860f15SJohn Edward Broadbent                             }
15787e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
15797e860f15SJohn Edward Broadbent                             {
1580914e2d5dSEd Tanous                                 const bool* resolveptr =
1581002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
15827e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
15837e860f15SJohn Edward Broadbent                                 {
1584002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
15857e860f15SJohn Edward Broadbent                                     return;
15867e860f15SJohn Edward Broadbent                                 }
15877e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
15887e860f15SJohn Edward Broadbent                             }
15899017faf2SAbhishek Patel                             else if (propertyMap.first ==
15909017faf2SAbhishek Patel                                      "ServiceProviderNotify")
15919017faf2SAbhishek Patel                             {
15929017faf2SAbhishek Patel                                 notify = std::get_if<std::string>(
15939017faf2SAbhishek Patel                                     &propertyMap.second);
15949017faf2SAbhishek Patel                                 if (notify == nullptr)
15959017faf2SAbhishek Patel                                 {
15969017faf2SAbhishek Patel                                     messages::internalError(asyncResp->res);
15979017faf2SAbhishek Patel                                     return;
15989017faf2SAbhishek Patel                                 }
15999017faf2SAbhishek Patel                             }
16007e860f15SJohn Edward Broadbent                         }
16017e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
16027e860f15SJohn Edward Broadbent                             severity == nullptr)
16037e860f15SJohn Edward Broadbent                         {
16047e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
16057e860f15SJohn Edward Broadbent                             return;
16067e860f15SJohn Edward Broadbent                         }
16077e860f15SJohn Edward Broadbent                     }
16087e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
16097e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
16107e860f15SJohn Edward Broadbent                     {
1611002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
16127e860f15SJohn Edward Broadbent                         {
16137e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
16147e860f15SJohn Edward Broadbent                             {
16157e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
16167e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16177e860f15SJohn Edward Broadbent                             }
16187e860f15SJohn Edward Broadbent                         }
16197e860f15SJohn Edward Broadbent                     }
16207e860f15SJohn Edward Broadbent                 }
16217e860f15SJohn Edward Broadbent                 // Object path without the
16227e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
16237e860f15SJohn Edward Broadbent                 // and continue.
16247e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1625c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1626c419c759SEd Tanous                     updateTimestamp == nullptr)
16277e860f15SJohn Edward Broadbent                 {
16287e860f15SJohn Edward Broadbent                     continue;
16297e860f15SJohn Edward Broadbent                 }
16307e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
16317e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
16329c11a172SVijay Lobo                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
1633*eddfc437SWilly Tu                 thisEntry["@odata.id"] = crow::utility::urlFromPieces(
1634*eddfc437SWilly Tu                     "redfish", "v1", "Systems", "system", "LogServices",
1635*eddfc437SWilly Tu                     "EventLog", "Entries", std::to_string(*id));
16367e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
16377e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
16387e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
16397e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
16409c11a172SVijay Lobo                 if ((resolution != nullptr) && (!(*resolution).empty()))
16419c11a172SVijay Lobo                 {
16429c11a172SVijay Lobo                     thisEntry["Resolution"] = *resolution;
16439c11a172SVijay Lobo                 }
16449017faf2SAbhishek Patel                 std::optional<bool> notifyAction =
16459017faf2SAbhishek Patel                     getProviderNotifyAction(*notify);
16469017faf2SAbhishek Patel                 if (notifyAction)
16479017faf2SAbhishek Patel                 {
16489017faf2SAbhishek Patel                     thisEntry["ServiceProviderNotified"] = *notifyAction;
16499017faf2SAbhishek Patel                 }
16507e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
16517e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
16527e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
16537e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
16542b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
16557e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
16562b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
16577e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
16587e860f15SJohn Edward Broadbent                 {
16597e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
16600fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
16617e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
16627e860f15SJohn Edward Broadbent                 }
16637e860f15SJohn Edward Broadbent             }
1664002d39b4SEd Tanous             std::sort(
1665002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1666002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
16677e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
16687e860f15SJohn Edward Broadbent                 });
16697e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
16707e860f15SJohn Edward Broadbent                 entriesArray.size();
16717e860f15SJohn Edward Broadbent             },
16727e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
16737e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16747e860f15SJohn Edward Broadbent         });
16757e860f15SJohn Edward Broadbent }
16767e860f15SJohn Edward Broadbent 
16777e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
16787e860f15SJohn Edward Broadbent {
16797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
168022d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1681ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1682002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1683002d39b4SEd Tanous             [&app](const crow::Request& req,
16847e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
168522d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
16863ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16877e860f15SJohn Edward Broadbent         {
168845ca1b86SEd Tanous             return;
168945ca1b86SEd Tanous         }
169022d268cbSEd Tanous         if (systemName != "system")
169122d268cbSEd Tanous         {
169222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
169322d268cbSEd Tanous                                        systemName);
169422d268cbSEd Tanous             return;
169522d268cbSEd Tanous         }
169622d268cbSEd Tanous 
16977e860f15SJohn Edward Broadbent         std::string entryID = param;
16987e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
16997e860f15SJohn Edward Broadbent 
17007e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
17017e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1702d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1703d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1704d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
1705002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1706b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
17077e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
17087e860f15SJohn Edward Broadbent             {
1709d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1710d1bde9e5SKrzysztof Grobelny                                            entryID);
17117e860f15SJohn Edward Broadbent                 return;
17127e860f15SJohn Edward Broadbent             }
17137e860f15SJohn Edward Broadbent             if (ec)
17147e860f15SJohn Edward Broadbent             {
17150fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1716002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
17177e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
17187e860f15SJohn Edward Broadbent                 return;
17197e860f15SJohn Edward Broadbent             }
1720914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1721c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1722c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1723914e2d5dSEd Tanous             const std::string* severity = nullptr;
1724914e2d5dSEd Tanous             const std::string* message = nullptr;
1725914e2d5dSEd Tanous             const std::string* filePath = nullptr;
17269c11a172SVijay Lobo             const std::string* resolution = nullptr;
17277e860f15SJohn Edward Broadbent             bool resolved = false;
17289017faf2SAbhishek Patel             const std::string* notify = nullptr;
17297e860f15SJohn Edward Broadbent 
1730d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1731d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1732d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
17339c11a172SVijay Lobo                 severity, "Message", message, "Resolved", resolved,
17349017faf2SAbhishek Patel                 "Resolution", resolution, "Path", filePath,
17359017faf2SAbhishek Patel                 "ServiceProviderNotify", notify);
1736d1bde9e5SKrzysztof Grobelny 
1737d1bde9e5SKrzysztof Grobelny             if (!success)
173875710de2SXiaochao Ma             {
173975710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
174075710de2SXiaochao Ma                 return;
174175710de2SXiaochao Ma             }
1742d1bde9e5SKrzysztof Grobelny 
1743002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
17449017faf2SAbhishek Patel                 timestamp == nullptr || updateTimestamp == nullptr ||
17459017faf2SAbhishek Patel                 notify == nullptr)
1746f86bb901SAdriana Kobylak             {
1747ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1748271584abSEd Tanous                 return;
1749271584abSEd Tanous             }
17509017faf2SAbhishek Patel 
1751f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
17529c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
1753f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
1754*eddfc437SWilly Tu                 crow::utility::urlFromPieces(
1755*eddfc437SWilly Tu                     "redfish", "v1", "Systems", "system", "LogServices",
1756*eddfc437SWilly Tu                     "EventLog", "Entries", std::to_string(*id));
175745ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1758f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1759f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1760f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
17619017faf2SAbhishek Patel             std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
17629017faf2SAbhishek Patel             if (notifyAction)
17639017faf2SAbhishek Patel             {
17649017faf2SAbhishek Patel                 asyncResp->res.jsonValue["ServiceProviderNotified"] =
17659017faf2SAbhishek Patel                     *notifyAction;
17669017faf2SAbhishek Patel             }
17679c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
17689c11a172SVijay Lobo             {
17699c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
17709c11a172SVijay Lobo             }
1771f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1772f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1773f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1774f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
17752b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1776f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
17772b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1778f86bb901SAdriana Kobylak             if (filePath != nullptr)
1779f86bb901SAdriana Kobylak             {
1780f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1781e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1782e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1783f86bb901SAdriana Kobylak             }
1784d1bde9e5SKrzysztof Grobelny             });
17857e860f15SJohn Edward Broadbent         });
1786336e96c6SChicago Duan 
17877e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
178822d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1789ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
17907e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
179145ca1b86SEd Tanous             [&app](const crow::Request& req,
17927e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
179322d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
17943ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179545ca1b86SEd Tanous         {
179645ca1b86SEd Tanous             return;
179745ca1b86SEd Tanous         }
179822d268cbSEd Tanous         if (systemName != "system")
179922d268cbSEd Tanous         {
180022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
180122d268cbSEd Tanous                                        systemName);
180222d268cbSEd Tanous             return;
180322d268cbSEd Tanous         }
180475710de2SXiaochao Ma         std::optional<bool> resolved;
180575710de2SXiaochao Ma 
180615ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
18077e860f15SJohn Edward Broadbent                                       resolved))
180875710de2SXiaochao Ma         {
180975710de2SXiaochao Ma             return;
181075710de2SXiaochao Ma         }
181175710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
181275710de2SXiaochao Ma 
181375710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
18144f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
181575710de2SXiaochao Ma             if (ec)
181675710de2SXiaochao Ma             {
181775710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
181875710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
181975710de2SXiaochao Ma                 return;
182075710de2SXiaochao Ma             }
182175710de2SXiaochao Ma             },
182275710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
182375710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
182475710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
182575710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1826168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
18277e860f15SJohn Edward Broadbent         });
182875710de2SXiaochao Ma 
18297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
183022d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1831ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1832ed398213SEd Tanous 
1833002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1834002d39b4SEd Tanous             [&app](const crow::Request& req,
1835002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
183622d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1838336e96c6SChicago Duan         {
183945ca1b86SEd Tanous             return;
184045ca1b86SEd Tanous         }
184122d268cbSEd Tanous         if (systemName != "system")
184222d268cbSEd Tanous         {
184322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
184422d268cbSEd Tanous                                        systemName);
184522d268cbSEd Tanous             return;
184622d268cbSEd Tanous         }
1847336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1848336e96c6SChicago Duan 
18497e860f15SJohn Edward Broadbent         std::string entryID = param;
1850336e96c6SChicago Duan 
1851336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1852336e96c6SChicago Duan 
1853336e96c6SChicago Duan         // Process response from Logging service.
1854002d39b4SEd Tanous         auto respHandler =
1855002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1856002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1857336e96c6SChicago Duan             if (ec)
1858336e96c6SChicago Duan             {
18593de8d8baSGeorge Liu                 if (ec.value() == EBADR)
18603de8d8baSGeorge Liu                 {
186145ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
186245ca1b86SEd Tanous                                                entryID);
18633de8d8baSGeorge Liu                     return;
18643de8d8baSGeorge Liu                 }
1865336e96c6SChicago Duan                 // TODO Handle for specific error code
18660fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
18670fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1868336e96c6SChicago Duan                     << ec;
1869336e96c6SChicago Duan                 asyncResp->res.result(
1870336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1871336e96c6SChicago Duan                 return;
1872336e96c6SChicago Duan             }
1873336e96c6SChicago Duan 
1874336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1875336e96c6SChicago Duan         };
1876336e96c6SChicago Duan 
1877336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1878336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1879336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1880336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1881336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
18827e860f15SJohn Edward Broadbent         });
1883400fd1fbSAdriana Kobylak }
1884400fd1fbSAdriana Kobylak 
18857e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1886400fd1fbSAdriana Kobylak {
18870fda0f12SGeorge Liu     BMCWEB_ROUTE(
18880fda0f12SGeorge Liu         app,
188922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1890ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
18917e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
189245ca1b86SEd Tanous             [&app](const crow::Request& req,
18937e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
189422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18953ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18967e860f15SJohn Edward Broadbent         {
189745ca1b86SEd Tanous             return;
189845ca1b86SEd Tanous         }
189999351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
190099351cd8SEd Tanous                 req.getHeaderValue("Accept"),
19014a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1902400fd1fbSAdriana Kobylak         {
1903002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1904400fd1fbSAdriana Kobylak             return;
1905400fd1fbSAdriana Kobylak         }
190622d268cbSEd Tanous         if (systemName != "system")
190722d268cbSEd Tanous         {
190822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
190922d268cbSEd Tanous                                        systemName);
191022d268cbSEd Tanous             return;
191122d268cbSEd Tanous         }
1912400fd1fbSAdriana Kobylak 
19137e860f15SJohn Edward Broadbent         std::string entryID = param;
1914400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1915400fd1fbSAdriana Kobylak 
1916400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1917002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1918400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1919400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1920400fd1fbSAdriana Kobylak             {
1921002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1922002d39b4SEd Tanous                                            entryID);
1923400fd1fbSAdriana Kobylak                 return;
1924400fd1fbSAdriana Kobylak             }
1925400fd1fbSAdriana Kobylak             if (ec)
1926400fd1fbSAdriana Kobylak             {
1927400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1928400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1929400fd1fbSAdriana Kobylak                 return;
1930400fd1fbSAdriana Kobylak             }
1931400fd1fbSAdriana Kobylak 
1932400fd1fbSAdriana Kobylak             int fd = -1;
1933400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1934400fd1fbSAdriana Kobylak             if (fd == -1)
1935400fd1fbSAdriana Kobylak             {
1936400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1937400fd1fbSAdriana Kobylak                 return;
1938400fd1fbSAdriana Kobylak             }
1939400fd1fbSAdriana Kobylak 
1940400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1941400fd1fbSAdriana Kobylak             if (size == -1)
1942400fd1fbSAdriana Kobylak             {
1943400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1944400fd1fbSAdriana Kobylak                 return;
1945400fd1fbSAdriana Kobylak             }
1946400fd1fbSAdriana Kobylak 
1947400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1948400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1949400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1950400fd1fbSAdriana Kobylak             {
1951002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1952400fd1fbSAdriana Kobylak                                  << maxFileSize;
1953400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1954400fd1fbSAdriana Kobylak                 return;
1955400fd1fbSAdriana Kobylak             }
1956400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1957400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1958400fd1fbSAdriana Kobylak             if (rc == -1)
1959400fd1fbSAdriana Kobylak             {
1960400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1961400fd1fbSAdriana Kobylak                 return;
1962400fd1fbSAdriana Kobylak             }
1963400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1964400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1965400fd1fbSAdriana Kobylak             {
1966400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1967400fd1fbSAdriana Kobylak                 return;
1968400fd1fbSAdriana Kobylak             }
1969400fd1fbSAdriana Kobylak             close(fd);
1970400fd1fbSAdriana Kobylak 
1971400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1972002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1973400fd1fbSAdriana Kobylak 
1974d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1975400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1976d9f6c621SEd Tanous             asyncResp->res.addHeader(
1977d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
1978400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1979400fd1fbSAdriana Kobylak             },
1980400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1981400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1982400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
19837e860f15SJohn Edward Broadbent         });
19841da66f75SEd Tanous }
19851da66f75SEd Tanous 
1986b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1987b7028ebfSSpencer Ku 
1988b7028ebfSSpencer Ku inline bool
1989b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1990b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1991b7028ebfSSpencer Ku {
1992b7028ebfSSpencer Ku     std::error_code ec;
1993b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1994b7028ebfSSpencer Ku     if (ec)
1995b7028ebfSSpencer Ku     {
1996b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1997b7028ebfSSpencer Ku         return false;
1998b7028ebfSSpencer Ku     }
1999b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
2000b7028ebfSSpencer Ku     {
2001b7028ebfSSpencer Ku         std::string filename = it.path().filename();
2002b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
2003b7028ebfSSpencer Ku         // path
200411ba3979SEd Tanous         if (filename.starts_with("log"))
2005b7028ebfSSpencer Ku         {
2006b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
2007b7028ebfSSpencer Ku         }
2008b7028ebfSSpencer Ku     }
2009b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
2010b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
2011b7028ebfSSpencer Ku     // descending order.
2012b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2013b7028ebfSSpencer Ku               AlphanumLess<std::string>());
2014b7028ebfSSpencer Ku 
2015b7028ebfSSpencer Ku     return true;
2016b7028ebfSSpencer Ku }
2017b7028ebfSSpencer Ku 
201802cad96eSEd Tanous inline bool getHostLoggerEntries(
201902cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
202002cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
2021b7028ebfSSpencer Ku {
2022b7028ebfSSpencer Ku     GzFileReader logFile;
2023b7028ebfSSpencer Ku 
2024b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
2025b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
2026b7028ebfSSpencer Ku     {
2027b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2028b7028ebfSSpencer Ku         {
2029b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
2030b7028ebfSSpencer Ku             return false;
2031b7028ebfSSpencer Ku         }
2032b7028ebfSSpencer Ku     }
2033b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2034b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2035b7028ebfSSpencer Ku     if (!lastMessage.empty())
2036b7028ebfSSpencer Ku     {
2037b7028ebfSSpencer Ku         logCount++;
2038b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2039b7028ebfSSpencer Ku         {
2040b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2041b7028ebfSSpencer Ku         }
2042b7028ebfSSpencer Ku     }
2043b7028ebfSSpencer Ku     return true;
2044b7028ebfSSpencer Ku }
2045b7028ebfSSpencer Ku 
2046b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2047b7028ebfSSpencer Ku                                     const std::string& msg,
20486d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2049b7028ebfSSpencer Ku {
2050b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
20519c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2052*eddfc437SWilly Tu     logEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2053*eddfc437SWilly Tu         "redfish", "v1", "Systems", "system", "LogServices", "HostLogger",
2054*eddfc437SWilly Tu         "Entries", logEntryID);
20556d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
20566d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
20576d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
20586d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
20596d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
20606d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2061b7028ebfSSpencer Ku }
2062b7028ebfSSpencer Ku 
2063b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2064b7028ebfSSpencer Ku {
206522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2066b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
20671476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
20681476687dSEd Tanous             [&app](const crow::Request& req,
206922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
207022d268cbSEd Tanous                    const std::string& systemName) {
20713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207245ca1b86SEd Tanous         {
207345ca1b86SEd Tanous             return;
207445ca1b86SEd Tanous         }
207522d268cbSEd Tanous         if (systemName != "system")
207622d268cbSEd Tanous         {
207722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
207822d268cbSEd Tanous                                        systemName);
207922d268cbSEd Tanous             return;
208022d268cbSEd Tanous         }
2081b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2082b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
2083b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2084b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
2085b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2086b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2087b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
20881476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
20891476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2090b7028ebfSSpencer Ku         });
2091b7028ebfSSpencer Ku }
2092b7028ebfSSpencer Ku 
2093b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2094b7028ebfSSpencer Ku {
2095b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
209622d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2097b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2098002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2099002d39b4SEd Tanous             [&app](const crow::Request& req,
210022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
210122d268cbSEd Tanous                    const std::string& systemName) {
2102c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2103c937d2bfSEd Tanous             .canDelegateTop = true,
2104c937d2bfSEd Tanous             .canDelegateSkip = true,
2105c937d2bfSEd Tanous         };
2106c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2107c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21083ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2109b7028ebfSSpencer Ku         {
2110b7028ebfSSpencer Ku             return;
2111b7028ebfSSpencer Ku         }
211222d268cbSEd Tanous         if (systemName != "system")
211322d268cbSEd Tanous         {
211422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
211522d268cbSEd Tanous                                        systemName);
211622d268cbSEd Tanous             return;
211722d268cbSEd Tanous         }
2118b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2119b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2120b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2121b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2122b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2123b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2124b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
21250fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2126b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2127b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2128b7028ebfSSpencer Ku 
2129b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2130b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2131b7028ebfSSpencer Ku         {
2132b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2133b7028ebfSSpencer Ku             return;
2134b7028ebfSSpencer Ku         }
21353648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
21363648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21375143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2138b7028ebfSSpencer Ku         size_t logCount = 0;
2139b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2140b7028ebfSSpencer Ku         // control by skip and top.
2141b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
21423648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
21433648c8beSEd Tanous                                   logCount))
2144b7028ebfSSpencer Ku         {
2145b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2146b7028ebfSSpencer Ku             return;
2147b7028ebfSSpencer Ku         }
2148b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2149b7028ebfSSpencer Ku         // log count
215026f6976fSEd Tanous         if (logEntries.empty())
2151b7028ebfSSpencer Ku         {
2152b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2153b7028ebfSSpencer Ku             return;
2154b7028ebfSSpencer Ku         }
215526f6976fSEd Tanous         if (!logEntries.empty())
2156b7028ebfSSpencer Ku         {
2157b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2158b7028ebfSSpencer Ku             {
21596d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
21603648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
21613648c8beSEd Tanous                                         hostLogEntry);
21626d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
2163b7028ebfSSpencer Ku             }
2164b7028ebfSSpencer Ku 
2165b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
21663648c8beSEd Tanous             if (skip + top < logCount)
2167b7028ebfSSpencer Ku             {
2168b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
21690fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
21703648c8beSEd Tanous                     std::to_string(skip + top);
2171b7028ebfSSpencer Ku             }
2172b7028ebfSSpencer Ku         }
2173b7028ebfSSpencer Ku         });
2174b7028ebfSSpencer Ku }
2175b7028ebfSSpencer Ku 
2176b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2177b7028ebfSSpencer Ku {
2178b7028ebfSSpencer Ku     BMCWEB_ROUTE(
217922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2180b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2181b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
218245ca1b86SEd Tanous             [&app](const crow::Request& req,
2183b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
21853ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
218645ca1b86SEd Tanous         {
218745ca1b86SEd Tanous             return;
218845ca1b86SEd Tanous         }
218922d268cbSEd Tanous         if (systemName != "system")
219022d268cbSEd Tanous         {
219122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
219222d268cbSEd Tanous                                        systemName);
219322d268cbSEd Tanous             return;
219422d268cbSEd Tanous         }
2195b7028ebfSSpencer Ku         const std::string& targetID = param;
2196b7028ebfSSpencer Ku 
2197b7028ebfSSpencer Ku         uint64_t idInt = 0;
2198ca45aa3cSEd Tanous 
2199ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2200ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
2201ca45aa3cSEd Tanous 
2202ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
22039db4ba25SJiaqing Zhao         if (ec == std::errc::invalid_argument ||
22049db4ba25SJiaqing Zhao             ec == std::errc::result_out_of_range)
2205b7028ebfSSpencer Ku         {
22069db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2207b7028ebfSSpencer Ku             return;
2208b7028ebfSSpencer Ku         }
2209b7028ebfSSpencer Ku 
2210b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2211b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2212b7028ebfSSpencer Ku         {
2213b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2214b7028ebfSSpencer Ku             return;
2215b7028ebfSSpencer Ku         }
2216b7028ebfSSpencer Ku 
2217b7028ebfSSpencer Ku         size_t logCount = 0;
22183648c8beSEd Tanous         size_t top = 1;
2219b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2220b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2221b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2222b7028ebfSSpencer Ku         // get that entry
2223002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2224002d39b4SEd Tanous                                   logCount))
2225b7028ebfSSpencer Ku         {
2226b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2227b7028ebfSSpencer Ku             return;
2228b7028ebfSSpencer Ku         }
2229b7028ebfSSpencer Ku 
2230b7028ebfSSpencer Ku         if (!logEntries.empty())
2231b7028ebfSSpencer Ku         {
22326d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
22336d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
22346d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2235b7028ebfSSpencer Ku             return;
2236b7028ebfSSpencer Ku         }
2237b7028ebfSSpencer Ku 
2238b7028ebfSSpencer Ku         // Requested ID was not found
22399db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2240b7028ebfSSpencer Ku         });
2241b7028ebfSSpencer Ku }
2242b7028ebfSSpencer Ku 
2243dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2244fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2245fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22461da66f75SEd Tanous {
22473ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
224845ca1b86SEd Tanous     {
224945ca1b86SEd Tanous         return;
225045ca1b86SEd Tanous     }
22517e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
22527e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2253e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
22541da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2255e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2256e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2257002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2258e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
22591da66f75SEd Tanous         "Collection of LogServices for this Manager";
2260002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2261c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2262fdd26906SClaire Weinan 
2263c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2264613dabeaSEd Tanous     nlohmann::json::object_t journal;
2265613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2266613dabeaSEd Tanous     logServiceArray.push_back(std::move(journal));
2267c4bf6374SJason M. Bills #endif
2268fdd26906SClaire Weinan 
2269fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2270fdd26906SClaire Weinan 
2271fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
227215912159SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
22737a1dbc48SGeorge Liu         "xyz.openbmc_project.Collection.DeleteAll"};
22747a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
22757a1dbc48SGeorge Liu         "/xyz/openbmc_project/dump", 0, interfaces,
2276fdd26906SClaire Weinan         [asyncResp](
22777a1dbc48SGeorge Liu             const boost::system::error_code& ec,
2278fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2279fdd26906SClaire Weinan         if (ec)
2280fdd26906SClaire Weinan         {
2281fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2282dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2283fdd26906SClaire Weinan                 << ec;
2284fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2285fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2286fdd26906SClaire Weinan             return;
2287fdd26906SClaire Weinan         }
2288fdd26906SClaire Weinan 
2289fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2290fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2291fdd26906SClaire Weinan 
2292fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2293fdd26906SClaire Weinan         {
2294fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2295fdd26906SClaire Weinan             {
2296613dabeaSEd Tanous                 nlohmann::json::object_t member;
2297613dabeaSEd Tanous                 member["@odata.id"] =
2298613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2299613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2300fdd26906SClaire Weinan             }
2301fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2302fdd26906SClaire Weinan             {
2303613dabeaSEd Tanous                 nlohmann::json::object_t member;
2304613dabeaSEd Tanous                 member["@odata.id"] =
2305613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2306613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2307fdd26906SClaire Weinan             }
2308fdd26906SClaire Weinan         }
2309fdd26906SClaire Weinan 
2310e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2311fdd26906SClaire Weinan             logServiceArrayLocal.size();
23127a1dbc48SGeorge Liu         });
2313fdd26906SClaire Weinan #endif
2314fdd26906SClaire Weinan }
2315fdd26906SClaire Weinan 
2316fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2317fdd26906SClaire Weinan {
2318fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2319fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2320fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2321dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2322e1f26343SJason M. Bills }
2323e1f26343SJason M. Bills 
23247e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2325e1f26343SJason M. Bills {
23267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2327ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23287e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
232945ca1b86SEd Tanous             [&app](const crow::Request& req,
233045ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23313ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
23327e860f15SJohn Edward Broadbent         {
233345ca1b86SEd Tanous             return;
233445ca1b86SEd Tanous         }
2335e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2336e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
23370f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
23380f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2339002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2340002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2341ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "Journal";
2342e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23437c8c4058STejas Patil 
23447c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
23452b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2346002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23477c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23487c8c4058STejas Patil             redfishDateTimeOffset.second;
23497c8c4058STejas Patil 
23501476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
23511476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
23527e860f15SJohn Edward Broadbent         });
2353e1f26343SJason M. Bills }
2354e1f26343SJason M. Bills 
23553a48b3a2SJason M. Bills static int
23563a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2357e1f26343SJason M. Bills                                sd_journal* journal,
23583a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2359e1f26343SJason M. Bills {
2360e1f26343SJason M. Bills     // Get the Log Entry contents
2361e1f26343SJason M. Bills     int ret = 0;
2362e1f26343SJason M. Bills 
2363a8fe54f0SJason M. Bills     std::string message;
2364a8fe54f0SJason M. Bills     std::string_view syslogID;
2365a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2366a8fe54f0SJason M. Bills     if (ret < 0)
2367a8fe54f0SJason M. Bills     {
2368a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2369a8fe54f0SJason M. Bills                          << strerror(-ret);
2370a8fe54f0SJason M. Bills     }
2371a8fe54f0SJason M. Bills     if (!syslogID.empty())
2372a8fe54f0SJason M. Bills     {
2373a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2374a8fe54f0SJason M. Bills     }
2375a8fe54f0SJason M. Bills 
237639e77504SEd Tanous     std::string_view msg;
237716428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2378e1f26343SJason M. Bills     if (ret < 0)
2379e1f26343SJason M. Bills     {
2380e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2381e1f26343SJason M. Bills         return 1;
2382e1f26343SJason M. Bills     }
2383a8fe54f0SJason M. Bills     message += std::string(msg);
2384e1f26343SJason M. Bills 
2385e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2386271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
238716428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2388e1f26343SJason M. Bills     if (ret < 0)
2389e1f26343SJason M. Bills     {
2390e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2391e1f26343SJason M. Bills     }
2392e1f26343SJason M. Bills 
2393e1f26343SJason M. Bills     // Get the Created time from the timestamp
239416428a1aSJason M. Bills     std::string entryTimeStr;
239516428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2396e1f26343SJason M. Bills     {
239716428a1aSJason M. Bills         return 1;
2398e1f26343SJason M. Bills     }
2399e1f26343SJason M. Bills 
2400e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
24019c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
2402*eddfc437SWilly Tu     bmcJournalLogEntryJson["@odata.id"] = crow::utility::urlFromPieces(
2403*eddfc437SWilly Tu         "redfish", "v1", "Managers", "bmc", "LogServices", "Journal", "Entries",
2404*eddfc437SWilly Tu         bmcJournalLogEntryID);
240584afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
240684afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
240784afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
240884afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
240984afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2410738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
241184afc48bSJason M. Bills                                                          : "OK";
241284afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
241384afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2414e1f26343SJason M. Bills     return 0;
2415e1f26343SJason M. Bills }
2416e1f26343SJason M. Bills 
24177e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2418e1f26343SJason M. Bills {
24197e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2420ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2421002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2422002d39b4SEd Tanous             [&app](const crow::Request& req,
2423002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2424c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2425c937d2bfSEd Tanous             .canDelegateTop = true,
2426c937d2bfSEd Tanous             .canDelegateSkip = true,
2427c937d2bfSEd Tanous         };
2428c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2429c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
24303ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2431193ad2faSJason M. Bills         {
2432193ad2faSJason M. Bills             return;
2433193ad2faSJason M. Bills         }
24343648c8beSEd Tanous 
24353648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
24365143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
24373648c8beSEd Tanous 
24387e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
24397e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2440e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2441e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
24420f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
24430f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2444e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2445e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2446e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
24470fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2448e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2449e1f26343SJason M. Bills 
24507e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
24517e860f15SJohn Edward Broadbent         // unique ID for each entry
2452e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2453e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2454e1f26343SJason M. Bills         if (ret < 0)
2455e1f26343SJason M. Bills         {
2456002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2457f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2458e1f26343SJason M. Bills             return;
2459e1f26343SJason M. Bills         }
24600fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
24610fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2462e1f26343SJason M. Bills         journalTmp = nullptr;
2463b01bf299SEd Tanous         uint64_t entryCount = 0;
2464e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2465e85d6b16SJason M. Bills         bool firstEntry = true;
2466e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2467e1f26343SJason M. Bills         {
2468193ad2faSJason M. Bills             entryCount++;
24697e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
24707e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
24713648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2472193ad2faSJason M. Bills             {
2473193ad2faSJason M. Bills                 continue;
2474193ad2faSJason M. Bills             }
2475193ad2faSJason M. Bills 
247616428a1aSJason M. Bills             std::string idStr;
2477e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2478e1f26343SJason M. Bills             {
2479e1f26343SJason M. Bills                 continue;
2480e1f26343SJason M. Bills             }
2481e85d6b16SJason M. Bills             firstEntry = false;
2482e85d6b16SJason M. Bills 
24833a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2484c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2485c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2486e1f26343SJason M. Bills             {
2487f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2488e1f26343SJason M. Bills                 return;
2489e1f26343SJason M. Bills             }
24903a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2491e1f26343SJason M. Bills         }
2492193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
24933648c8beSEd Tanous         if (skip + top < entryCount)
2494193ad2faSJason M. Bills         {
2495193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
24960fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
24973648c8beSEd Tanous                 std::to_string(skip + top);
2498193ad2faSJason M. Bills         }
24997e860f15SJohn Edward Broadbent         });
2500e1f26343SJason M. Bills }
2501e1f26343SJason M. Bills 
25027e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2503e1f26343SJason M. Bills {
25047e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
25057e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2506ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
25077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
250845ca1b86SEd Tanous             [&app](const crow::Request& req,
25097e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25107e860f15SJohn Edward Broadbent                    const std::string& entryID) {
25113ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
251245ca1b86SEd Tanous         {
251345ca1b86SEd Tanous             return;
251445ca1b86SEd Tanous         }
2515e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2516e1f26343SJason M. Bills         uint64_t ts = 0;
2517271584abSEd Tanous         uint64_t index = 0;
25188d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2519e1f26343SJason M. Bills         {
252016428a1aSJason M. Bills             return;
2521e1f26343SJason M. Bills         }
2522e1f26343SJason M. Bills 
2523e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2524e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2525e1f26343SJason M. Bills         if (ret < 0)
2526e1f26343SJason M. Bills         {
2527002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2528f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2529e1f26343SJason M. Bills             return;
2530e1f26343SJason M. Bills         }
2531002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2532002d39b4SEd Tanous             journalTmp, sd_journal_close);
2533e1f26343SJason M. Bills         journalTmp = nullptr;
25347e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
25357e860f15SJohn Edward Broadbent         // index tracking the unique ID
2536af07e3f5SJason M. Bills         std::string idStr;
2537af07e3f5SJason M. Bills         bool firstEntry = true;
2538e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
25392056b6d1SManojkiran Eda         if (ret < 0)
25402056b6d1SManojkiran Eda         {
25412056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
25422056b6d1SManojkiran Eda                              << strerror(-ret);
25432056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
25442056b6d1SManojkiran Eda             return;
25452056b6d1SManojkiran Eda         }
2546271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2547e1f26343SJason M. Bills         {
2548e1f26343SJason M. Bills             sd_journal_next(journal.get());
2549af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2550af07e3f5SJason M. Bills             {
2551af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2552af07e3f5SJason M. Bills                 return;
2553af07e3f5SJason M. Bills             }
2554af07e3f5SJason M. Bills             firstEntry = false;
2555af07e3f5SJason M. Bills         }
2556c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2557af07e3f5SJason M. Bills         if (idStr != entryID)
2558c4bf6374SJason M. Bills         {
25599db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
2560c4bf6374SJason M. Bills             return;
2561c4bf6374SJason M. Bills         }
2562c4bf6374SJason M. Bills 
25633a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2564c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
25653a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2566e1f26343SJason M. Bills         {
2567f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2568e1f26343SJason M. Bills             return;
2569e1f26343SJason M. Bills         }
2570d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
25717e860f15SJohn Edward Broadbent         });
2572c9bb6861Sraviteja-b }
2573c9bb6861Sraviteja-b 
2574fdd26906SClaire Weinan inline void
2575fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2576fdd26906SClaire Weinan                        const std::string& dumpType)
2577c9bb6861Sraviteja-b {
2578fdd26906SClaire Weinan     std::string dumpPath;
2579fdd26906SClaire Weinan     std::string overWritePolicy;
2580fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2581fdd26906SClaire Weinan 
2582fdd26906SClaire Weinan     if (dumpType == "BMC")
258345ca1b86SEd Tanous     {
2584fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2585fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2586fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2587fdd26906SClaire Weinan     }
2588fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2589fdd26906SClaire Weinan     {
2590fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2591fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2592fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2593fdd26906SClaire Weinan     }
2594fdd26906SClaire Weinan     else if (dumpType == "System")
2595fdd26906SClaire Weinan     {
2596fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2597fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2598fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2599fdd26906SClaire Weinan     }
2600fdd26906SClaire Weinan     else
2601fdd26906SClaire Weinan     {
2602fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2603fdd26906SClaire Weinan                          << dumpType;
2604fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
260545ca1b86SEd Tanous         return;
260645ca1b86SEd Tanous     }
2607fdd26906SClaire Weinan 
2608fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2609fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2610c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2611fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2612fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2613fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
26147c8c4058STejas Patil 
26157c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
26162b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
26170fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26187c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26197c8c4058STejas Patil         redfishDateTimeOffset.second;
26207c8c4058STejas Patil 
2621fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2622fdd26906SClaire Weinan 
2623fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2624fdd26906SClaire Weinan     {
2625002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26261476687dSEd Tanous                                 ["target"] =
2627fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2628fdd26906SClaire Weinan     }
26290d946211SClaire Weinan 
26300d946211SClaire Weinan     constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
26310d946211SClaire Weinan     dbus::utility::getSubTreePaths(
26320d946211SClaire Weinan         "/xyz/openbmc_project/dump", 0, interfaces,
26330d946211SClaire Weinan         [asyncResp, dumpType, dumpPath](
26340d946211SClaire Weinan             const boost::system::error_code& ec,
26350d946211SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
26360d946211SClaire Weinan         if (ec)
26370d946211SClaire Weinan         {
26380d946211SClaire Weinan             BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
26390d946211SClaire Weinan                              << ec;
26400d946211SClaire Weinan             // Assume that getting an error simply means there are no dump
26410d946211SClaire Weinan             // LogServices. Return without adding any error response.
26420d946211SClaire Weinan             return;
26430d946211SClaire Weinan         }
26440d946211SClaire Weinan 
26450d946211SClaire Weinan         const std::string dbusDumpPath =
26460d946211SClaire Weinan             "/xyz/openbmc_project/dump/" +
26470d946211SClaire Weinan             boost::algorithm::to_lower_copy(dumpType);
26480d946211SClaire Weinan 
26490d946211SClaire Weinan         for (const std::string& path : subTreePaths)
26500d946211SClaire Weinan         {
26510d946211SClaire Weinan             if (path == dbusDumpPath)
26520d946211SClaire Weinan             {
26530d946211SClaire Weinan                 asyncResp->res
26540d946211SClaire Weinan                     .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
26550d946211SClaire Weinan                     dumpPath + "/Actions/LogService.ClearLog";
26560d946211SClaire Weinan                 break;
26570d946211SClaire Weinan             }
26580d946211SClaire Weinan         }
26590d946211SClaire Weinan         });
2660c9bb6861Sraviteja-b }
2661c9bb6861Sraviteja-b 
2662fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2663fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2664fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26657e860f15SJohn Edward Broadbent {
26663ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266745ca1b86SEd Tanous     {
266845ca1b86SEd Tanous         return;
266945ca1b86SEd Tanous     }
2670fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2671fdd26906SClaire Weinan }
2672c9bb6861Sraviteja-b 
267322d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
267422d268cbSEd Tanous     crow::App& app, const crow::Request& req,
267522d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
267622d268cbSEd Tanous     const std::string& chassisId)
267722d268cbSEd Tanous {
267822d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
267922d268cbSEd Tanous     {
268022d268cbSEd Tanous         return;
268122d268cbSEd Tanous     }
268222d268cbSEd Tanous     if (chassisId != "system")
268322d268cbSEd Tanous     {
268422d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
268522d268cbSEd Tanous         return;
268622d268cbSEd Tanous     }
268722d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
268822d268cbSEd Tanous }
268922d268cbSEd Tanous 
2690fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2691fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2692fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2693fdd26906SClaire Weinan {
2694fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2695fdd26906SClaire Weinan     {
2696fdd26906SClaire Weinan         return;
2697fdd26906SClaire Weinan     }
2698fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2699fdd26906SClaire Weinan }
2700fdd26906SClaire Weinan 
270122d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
270222d268cbSEd Tanous     crow::App& app, const crow::Request& req,
270322d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
270422d268cbSEd Tanous     const std::string& chassisId)
270522d268cbSEd Tanous {
270622d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
270722d268cbSEd Tanous     {
270822d268cbSEd Tanous         return;
270922d268cbSEd Tanous     }
271022d268cbSEd Tanous     if (chassisId != "system")
271122d268cbSEd Tanous     {
271222d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
271322d268cbSEd Tanous         return;
271422d268cbSEd Tanous     }
271522d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
271622d268cbSEd Tanous }
271722d268cbSEd Tanous 
2718fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2719fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2720fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2721fdd26906SClaire Weinan     const std::string& dumpId)
2722fdd26906SClaire Weinan {
2723fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2724fdd26906SClaire Weinan     {
2725fdd26906SClaire Weinan         return;
2726fdd26906SClaire Weinan     }
2727fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2728fdd26906SClaire Weinan }
272922d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
273022d268cbSEd Tanous     crow::App& app, const crow::Request& req,
273122d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
273222d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
273322d268cbSEd Tanous {
273422d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
273522d268cbSEd Tanous     {
273622d268cbSEd Tanous         return;
273722d268cbSEd Tanous     }
273822d268cbSEd Tanous     if (chassisId != "system")
273922d268cbSEd Tanous     {
274022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
274122d268cbSEd Tanous         return;
274222d268cbSEd Tanous     }
274322d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
274422d268cbSEd Tanous }
2745fdd26906SClaire Weinan 
2746fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2747fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2748fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2749fdd26906SClaire Weinan     const std::string& dumpId)
2750fdd26906SClaire Weinan {
2751fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2752fdd26906SClaire Weinan     {
2753fdd26906SClaire Weinan         return;
2754fdd26906SClaire Weinan     }
2755fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2756fdd26906SClaire Weinan }
2757fdd26906SClaire Weinan 
275822d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
275922d268cbSEd Tanous     crow::App& app, const crow::Request& req,
276022d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
276122d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
276222d268cbSEd Tanous {
276322d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
276422d268cbSEd Tanous     {
276522d268cbSEd Tanous         return;
276622d268cbSEd Tanous     }
276722d268cbSEd Tanous     if (chassisId != "system")
276822d268cbSEd Tanous     {
276922d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
277022d268cbSEd Tanous         return;
277122d268cbSEd Tanous     }
277222d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
277322d268cbSEd Tanous }
277422d268cbSEd Tanous 
2775fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2776fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2777fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2778fdd26906SClaire Weinan {
2779fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2780fdd26906SClaire Weinan     {
2781fdd26906SClaire Weinan         return;
2782fdd26906SClaire Weinan     }
2783fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2784fdd26906SClaire Weinan }
2785fdd26906SClaire Weinan 
278622d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
278722d268cbSEd Tanous     crow::App& app, const crow::Request& req,
278822d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
278922d268cbSEd Tanous     const std::string& chassisId)
279022d268cbSEd Tanous {
279122d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
279222d268cbSEd Tanous     {
279322d268cbSEd Tanous         return;
279422d268cbSEd Tanous     }
279522d268cbSEd Tanous     if (chassisId != "system")
279622d268cbSEd Tanous     {
279722d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
279822d268cbSEd Tanous         return;
279922d268cbSEd Tanous     }
280022d268cbSEd Tanous     createDump(asyncResp, req, "System");
280122d268cbSEd Tanous }
280222d268cbSEd Tanous 
2803fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2804fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2805fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2806fdd26906SClaire Weinan {
2807fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2808fdd26906SClaire Weinan     {
2809fdd26906SClaire Weinan         return;
2810fdd26906SClaire Weinan     }
2811fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2812fdd26906SClaire Weinan }
2813fdd26906SClaire Weinan 
281422d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
281522d268cbSEd Tanous     crow::App& app, const crow::Request& req,
281622d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
281722d268cbSEd Tanous     const std::string& chassisId)
281822d268cbSEd Tanous {
281922d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
282022d268cbSEd Tanous     {
282122d268cbSEd Tanous         return;
282222d268cbSEd Tanous     }
282322d268cbSEd Tanous     if (chassisId != "system")
282422d268cbSEd Tanous     {
282522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
282622d268cbSEd Tanous         return;
282722d268cbSEd Tanous     }
282822d268cbSEd Tanous     clearDump(asyncResp, "System");
282922d268cbSEd Tanous }
283022d268cbSEd Tanous 
2831fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2832fdd26906SClaire Weinan {
2833fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2834fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2835fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2836fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2837fdd26906SClaire Weinan }
2838fdd26906SClaire Weinan 
2839fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2840fdd26906SClaire Weinan {
2841fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2842fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2843fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2844fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2845c9bb6861Sraviteja-b }
2846c9bb6861Sraviteja-b 
28477e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2848c9bb6861Sraviteja-b {
28497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28507e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2851ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2852fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2853fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2854fdd26906SClaire Weinan 
28557e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28567e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2857ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2858fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2859fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2860c9bb6861Sraviteja-b }
2861c9bb6861Sraviteja-b 
28627e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2863c9bb6861Sraviteja-b {
28640fda0f12SGeorge Liu     BMCWEB_ROUTE(
28650fda0f12SGeorge Liu         app,
28660fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2867ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
28687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2869fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2870fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2871a43be80fSAsmitha Karunanithi }
2872a43be80fSAsmitha Karunanithi 
28737e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
287480319af1SAsmitha Karunanithi {
28750fda0f12SGeorge Liu     BMCWEB_ROUTE(
28760fda0f12SGeorge Liu         app,
28770fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2878ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2879fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2880fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
288145ca1b86SEd Tanous }
2882fdd26906SClaire Weinan 
2883fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2884fdd26906SClaire Weinan {
2885fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2886fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2887fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2888fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2889fdd26906SClaire Weinan }
2890fdd26906SClaire Weinan 
2891fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2892fdd26906SClaire Weinan {
2893fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2894fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2895fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2896fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2897fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2898fdd26906SClaire Weinan }
2899fdd26906SClaire Weinan 
2900fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2901fdd26906SClaire Weinan {
2902fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2903fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2904fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2905fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2906fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2907fdd26906SClaire Weinan 
2908fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2909fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2910fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2911fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2912fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2913fdd26906SClaire Weinan }
2914fdd26906SClaire Weinan 
2915fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2916fdd26906SClaire Weinan {
2917fdd26906SClaire Weinan     BMCWEB_ROUTE(
2918fdd26906SClaire Weinan         app,
2919fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2920fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2921fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2922fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
29235cb1dd27SAsmitha Karunanithi }
29245cb1dd27SAsmitha Karunanithi 
29257e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
29265cb1dd27SAsmitha Karunanithi {
292722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2928ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
29296ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
293022d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
29315cb1dd27SAsmitha Karunanithi }
29325cb1dd27SAsmitha Karunanithi 
29337e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
29347e860f15SJohn Edward Broadbent {
293522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2936ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
293722d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
293822d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
293922d268cbSEd Tanous             std::ref(app)));
29405cb1dd27SAsmitha Karunanithi }
29415cb1dd27SAsmitha Karunanithi 
29427e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
29435cb1dd27SAsmitha Karunanithi {
29447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
294522d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2946ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29476ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
294822d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
29498d1b46d7Szhanghch05 
29507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
295122d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2952ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
29536ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
295422d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
29555cb1dd27SAsmitha Karunanithi }
2956c9bb6861Sraviteja-b 
29577e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2958c9bb6861Sraviteja-b {
29590fda0f12SGeorge Liu     BMCWEB_ROUTE(
29600fda0f12SGeorge Liu         app,
296122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2962ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
296322d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
296422d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
296522d268cbSEd Tanous             std::ref(app)));
2966a43be80fSAsmitha Karunanithi }
2967a43be80fSAsmitha Karunanithi 
29687e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2969a43be80fSAsmitha Karunanithi {
29700fda0f12SGeorge Liu     BMCWEB_ROUTE(
29710fda0f12SGeorge Liu         app,
297222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2973ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29746ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
297522d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2976013487e5Sraviteja-b }
2977013487e5Sraviteja-b 
29787e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
29791da66f75SEd Tanous {
29803946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29813946028dSAppaRao Puli     // method for security reasons.
29821da66f75SEd Tanous     /**
29831da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
29841da66f75SEd Tanous      */
298522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2986ed398213SEd Tanous         // This is incorrect, should be:
2987ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2988432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2989002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2990002d39b4SEd Tanous             [&app](const crow::Request& req,
299122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
299222d268cbSEd Tanous                    const std::string& systemName) {
29933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
299445ca1b86SEd Tanous         {
299545ca1b86SEd Tanous             return;
299645ca1b86SEd Tanous         }
299722d268cbSEd Tanous         if (systemName != "system")
299822d268cbSEd Tanous         {
299922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
300022d268cbSEd Tanous                                        systemName);
300122d268cbSEd Tanous             return;
300222d268cbSEd Tanous         }
300322d268cbSEd Tanous 
30047e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
30057e860f15SJohn Edward Broadbent         // SubRoute
30060f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
3007424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
3008e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
30098e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
30104f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
30114f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
30124f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3013e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3014e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
30157c8c4058STejas Patil 
30167c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
30172b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
30187c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30197c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30207c8c4058STejas Patil             redfishDateTimeOffset.second;
30217c8c4058STejas Patil 
30221476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
3023*eddfc437SWilly Tu             crow::utility::urlFromPieces("redfish", "v1", "Systems", "system",
3024*eddfc437SWilly Tu                                          "LogServices", "Crashdump", "Entries");
3025002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
30261476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3027002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
30281476687dSEd Tanous                                 ["target"] =
30291476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
30307e860f15SJohn Edward Broadbent         });
30311da66f75SEd Tanous }
30321da66f75SEd Tanous 
30337e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
30345b61b5e8SJason M. Bills {
30350fda0f12SGeorge Liu     BMCWEB_ROUTE(
30360fda0f12SGeorge Liu         app,
303722d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
3038ed398213SEd Tanous         // This is incorrect, should be:
3039ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3040432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
304245ca1b86SEd Tanous             [&app](const crow::Request& req,
304322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
304422d268cbSEd Tanous                    const std::string& systemName) {
30453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
304645ca1b86SEd Tanous         {
304745ca1b86SEd Tanous             return;
304845ca1b86SEd Tanous         }
304922d268cbSEd Tanous         if (systemName != "system")
305022d268cbSEd Tanous         {
305122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
305222d268cbSEd Tanous                                        systemName);
305322d268cbSEd Tanous             return;
305422d268cbSEd Tanous         }
30555b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
30565b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
3057cb13a392SEd Tanous                         const std::string&) {
30585b61b5e8SJason M. Bills             if (ec)
30595b61b5e8SJason M. Bills             {
30605b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
30615b61b5e8SJason M. Bills                 return;
30625b61b5e8SJason M. Bills             }
30635b61b5e8SJason M. Bills             messages::success(asyncResp->res);
30645b61b5e8SJason M. Bills             },
3065002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
30667e860f15SJohn Edward Broadbent         });
30675b61b5e8SJason M. Bills }
30685b61b5e8SJason M. Bills 
30698d1b46d7Szhanghch05 static void
30708d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30718d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3072e855dd28SJason M. Bills {
3073043a0536SJohnathan Mantey     auto getStoredLogCallback =
3074b9d36b47SEd Tanous         [asyncResp, logID,
3075b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
3076b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3077e855dd28SJason M. Bills         if (ec)
3078e855dd28SJason M. Bills         {
3079e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
30801ddcf01aSJason M. Bills             if (ec.value() ==
30811ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
30821ddcf01aSJason M. Bills             {
3083002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
30841ddcf01aSJason M. Bills             }
30851ddcf01aSJason M. Bills             else
30861ddcf01aSJason M. Bills             {
3087e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
30881ddcf01aSJason M. Bills             }
3089e855dd28SJason M. Bills             return;
3090e855dd28SJason M. Bills         }
3091043a0536SJohnathan Mantey 
3092043a0536SJohnathan Mantey         std::string timestamp{};
3093043a0536SJohnathan Mantey         std::string filename{};
3094043a0536SJohnathan Mantey         std::string logfile{};
30952c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3096043a0536SJohnathan Mantey 
3097043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3098e855dd28SJason M. Bills         {
30999db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3100e855dd28SJason M. Bills             return;
3101e855dd28SJason M. Bills         }
3102e855dd28SJason M. Bills 
3103043a0536SJohnathan Mantey         std::string crashdumpURI =
3104e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3105043a0536SJohnathan Mantey             logID + "/" + filename;
310684afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
31079c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3108*eddfc437SWilly Tu         logEntry["@odata.id"] = crow::utility::urlFromPieces(
3109*eddfc437SWilly Tu             "redfish", "v1", "Systems", "system", "LogServices", "Crashdump",
3110*eddfc437SWilly Tu             "Entries", logID);
311184afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
311284afc48bSJason M. Bills         logEntry["Id"] = logID;
311384afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
311484afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
311584afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
311684afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
311784afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
31182b20ef6eSJason M. Bills 
31192b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
31202b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
31212b20ef6eSJason M. Bills         // directly
31222b20ef6eSJason M. Bills         if (logEntryJson.is_array())
31232b20ef6eSJason M. Bills         {
31242b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
31252b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
31262b20ef6eSJason M. Bills                 logEntryJson.size();
31272b20ef6eSJason M. Bills         }
31282b20ef6eSJason M. Bills         else
31292b20ef6eSJason M. Bills         {
3130d405bb51SJason M. Bills             logEntryJson.update(logEntry);
31312b20ef6eSJason M. Bills         }
3132e855dd28SJason M. Bills     };
3133d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3134d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3135d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3136d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3137e855dd28SJason M. Bills }
3138e855dd28SJason M. Bills 
31397e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
31401da66f75SEd Tanous {
31413946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31423946028dSAppaRao Puli     // method for security reasons.
31431da66f75SEd Tanous     /**
31441da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
31451da66f75SEd Tanous      */
31467e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
314722d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3148ed398213SEd Tanous         // This is incorrect, should be.
3149ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3150432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3151002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3152002d39b4SEd Tanous             [&app](const crow::Request& req,
315322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
315422d268cbSEd Tanous                    const std::string& systemName) {
31553ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
315645ca1b86SEd Tanous         {
315745ca1b86SEd Tanous             return;
315845ca1b86SEd Tanous         }
315922d268cbSEd Tanous         if (systemName != "system")
316022d268cbSEd Tanous         {
316122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
316222d268cbSEd Tanous                                        systemName);
316322d268cbSEd Tanous             return;
316422d268cbSEd Tanous         }
316522d268cbSEd Tanous 
31667a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
31677a1dbc48SGeorge Liu             crashdumpInterface};
31687a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
31697a1dbc48SGeorge Liu             "/", 0, interfaces,
31707a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
31712b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
31721da66f75SEd Tanous             if (ec)
31731da66f75SEd Tanous             {
31741da66f75SEd Tanous                 if (ec.value() !=
31751da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
31761da66f75SEd Tanous                 {
31771da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
31781da66f75SEd Tanous                                      << ec.message();
3179f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
31801da66f75SEd Tanous                     return;
31811da66f75SEd Tanous                 }
31821da66f75SEd Tanous             }
3183e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
31841da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
31850f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3186424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3187002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3188e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3189424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3190002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3191a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
31922b20ef6eSJason M. Bills 
31932b20ef6eSJason M. Bills             for (const std::string& path : resp)
31941da66f75SEd Tanous             {
31952b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3196e855dd28SJason M. Bills                 // Get the log ID
31972b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
31982b20ef6eSJason M. Bills                 if (logID.empty())
31991da66f75SEd Tanous                 {
3200e855dd28SJason M. Bills                     continue;
32011da66f75SEd Tanous                 }
3202e855dd28SJason M. Bills                 // Add the log entry to the array
32032b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
32042b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
32051da66f75SEd Tanous             }
32067a1dbc48SGeorge Liu             });
32077e860f15SJohn Edward Broadbent         });
32081da66f75SEd Tanous }
32091da66f75SEd Tanous 
32107e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
32111da66f75SEd Tanous {
32123946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32133946028dSAppaRao Puli     // method for security reasons.
32141da66f75SEd Tanous 
32157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
321622d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3217ed398213SEd Tanous         // this is incorrect, should be
3218ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3219432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32207e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
322145ca1b86SEd Tanous             [&app](const crow::Request& req,
32227e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
322322d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
32243ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
322545ca1b86SEd Tanous         {
322645ca1b86SEd Tanous             return;
322745ca1b86SEd Tanous         }
322822d268cbSEd Tanous         if (systemName != "system")
322922d268cbSEd Tanous         {
323022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
323122d268cbSEd Tanous                                        systemName);
323222d268cbSEd Tanous             return;
323322d268cbSEd Tanous         }
32347e860f15SJohn Edward Broadbent         const std::string& logID = param;
3235e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
32367e860f15SJohn Edward Broadbent         });
3237e855dd28SJason M. Bills }
3238e855dd28SJason M. Bills 
32397e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3240e855dd28SJason M. Bills {
32413946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32423946028dSAppaRao Puli     // method for security reasons.
32437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
32447e860f15SJohn Edward Broadbent         app,
324522d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3246ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
32477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3248a4ce114aSNan Zhou             [](const crow::Request& req,
32497e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
325022d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
325122d268cbSEd Tanous                const std::string& fileName) {
32522a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
32532a9beeedSShounak Mitra         // Redfish resource.
325422d268cbSEd Tanous 
325522d268cbSEd Tanous         if (systemName != "system")
325622d268cbSEd Tanous         {
325722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
325822d268cbSEd Tanous                                        systemName);
325922d268cbSEd Tanous             return;
326022d268cbSEd Tanous         }
326122d268cbSEd Tanous 
3262043a0536SJohnathan Mantey         auto getStoredLogCallback =
3263002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3264abf2add6SEd Tanous                 const boost::system::error_code ec,
3265002d39b4SEd Tanous                 const std::vector<
3266002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
32677e860f15SJohn Edward Broadbent                     resp) {
32681da66f75SEd Tanous             if (ec)
32691da66f75SEd Tanous             {
3270002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3271f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
32721da66f75SEd Tanous                 return;
32731da66f75SEd Tanous             }
3274e855dd28SJason M. Bills 
3275043a0536SJohnathan Mantey             std::string dbusFilename{};
3276043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3277043a0536SJohnathan Mantey             std::string dbusFilepath{};
3278043a0536SJohnathan Mantey 
3279002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3280002d39b4SEd Tanous                                      dbusFilepath);
3281043a0536SJohnathan Mantey 
3282043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3283043a0536SJohnathan Mantey                 dbusFilepath.empty())
32841da66f75SEd Tanous             {
32859db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
32861da66f75SEd Tanous                 return;
32871da66f75SEd Tanous             }
3288e855dd28SJason M. Bills 
3289043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3290043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3291043a0536SJohnathan Mantey             {
32929db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3293043a0536SJohnathan Mantey                 return;
3294043a0536SJohnathan Mantey             }
3295043a0536SJohnathan Mantey 
3296043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3297043a0536SJohnathan Mantey             {
32989db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3299043a0536SJohnathan Mantey                 return;
3300043a0536SJohnathan Mantey             }
3301002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3302002d39b4SEd Tanous             asyncResp->res.body() =
3303002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3304043a0536SJohnathan Mantey 
33057e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
33067e860f15SJohn Edward Broadbent             // from a browser
3307d9f6c621SEd Tanous             asyncResp->res.addHeader(
3308d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
33091da66f75SEd Tanous         };
3310d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3311d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3312d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3313d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
33147e860f15SJohn Edward Broadbent         });
33151da66f75SEd Tanous }
33161da66f75SEd Tanous 
3317c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3318c5a4c82aSJason M. Bills {
3319c5a4c82aSJason M. Bills     onDemand,
3320c5a4c82aSJason M. Bills     telemetry,
3321c5a4c82aSJason M. Bills     invalid,
3322c5a4c82aSJason M. Bills };
3323c5a4c82aSJason M. Bills 
3324f7725d79SEd Tanous inline OEMDiagnosticType
3325f7725d79SEd Tanous     getOEMDiagnosticType(const 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)),
3420c5a4c82aSJason M. Bills              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(
344159d494eeSPatrick Williams                 [](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.
3496002d39b4SEd 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(
3589a3316fc6SZhikuiRen             [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";
3748*eddfc437SWilly Tu         bmcLogEntry["@odata.id"] = crow::utility::urlFromPieces(
3749*eddfc437SWilly Tu             "redfish", "v1", "Systems", "system", "LogServices", "PostCodes",
3750*eddfc437SWilly 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,
38036c9a279eSManojkiran Eda          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,
3840a3316fc6SZhikuiRen          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",
38991e1e598dSJonathan Doman         [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](
3991647b3cdcSGeorge Liu                 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