xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 50ebd4af91ece2e7b5e75b600f83a2a74b315068)
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);
101*50ebd4afSEd 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     }
2092b82937eSEd Tanous     entryTimestamp =
2102b82937eSEd Tanous         redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000);
2119c620e21SAsmitha Karunanithi     return true;
212a3316fc6SZhikuiRen }
21350b8a43aSEd Tanous 
2147e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
215e85d6b16SJason M. Bills                                     const bool firstEntry = true)
21616428a1aSJason M. Bills {
21716428a1aSJason M. Bills     int ret = 0;
21816428a1aSJason M. Bills     static uint64_t prevTs = 0;
21916428a1aSJason M. Bills     static int index = 0;
220e85d6b16SJason M. Bills     if (firstEntry)
221e85d6b16SJason M. Bills     {
222e85d6b16SJason M. Bills         prevTs = 0;
223e85d6b16SJason M. Bills     }
224e85d6b16SJason M. Bills 
22516428a1aSJason M. Bills     // Get the entry timestamp
22616428a1aSJason M. Bills     uint64_t curTs = 0;
22716428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
22816428a1aSJason M. Bills     if (ret < 0)
22916428a1aSJason M. Bills     {
23016428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
23116428a1aSJason M. Bills                          << strerror(-ret);
23216428a1aSJason M. Bills         return false;
23316428a1aSJason M. Bills     }
23416428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
23516428a1aSJason M. Bills     if (curTs == prevTs)
23616428a1aSJason M. Bills     {
23716428a1aSJason M. Bills         index++;
23816428a1aSJason M. Bills     }
23916428a1aSJason M. Bills     else
24016428a1aSJason M. Bills     {
24116428a1aSJason M. Bills         // Otherwise, reset it
24216428a1aSJason M. Bills         index = 0;
24316428a1aSJason M. Bills     }
24416428a1aSJason M. Bills     // Save the timestamp
24516428a1aSJason M. Bills     prevTs = curTs;
24616428a1aSJason M. Bills 
24716428a1aSJason M. Bills     entryID = std::to_string(curTs);
24816428a1aSJason M. Bills     if (index > 0)
24916428a1aSJason M. Bills     {
25016428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
25116428a1aSJason M. Bills     }
25216428a1aSJason M. Bills     return true;
25316428a1aSJason M. Bills }
25416428a1aSJason M. Bills 
255e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
256e85d6b16SJason M. Bills                              const bool firstEntry = true)
25795820184SJason M. Bills {
258271584abSEd Tanous     static time_t prevTs = 0;
25995820184SJason M. Bills     static int index = 0;
260e85d6b16SJason M. Bills     if (firstEntry)
261e85d6b16SJason M. Bills     {
262e85d6b16SJason M. Bills         prevTs = 0;
263e85d6b16SJason M. Bills     }
264e85d6b16SJason M. Bills 
26595820184SJason M. Bills     // Get the entry timestamp
266271584abSEd Tanous     std::time_t curTs = 0;
26795820184SJason M. Bills     std::tm timeStruct = {};
26895820184SJason M. Bills     std::istringstream entryStream(logEntry);
26995820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
27095820184SJason M. Bills     {
27195820184SJason M. Bills         curTs = std::mktime(&timeStruct);
27295820184SJason M. Bills     }
27395820184SJason M. Bills     // If the timestamp isn't unique, increment the index
27495820184SJason M. Bills     if (curTs == prevTs)
27595820184SJason M. Bills     {
27695820184SJason M. Bills         index++;
27795820184SJason M. Bills     }
27895820184SJason M. Bills     else
27995820184SJason M. Bills     {
28095820184SJason M. Bills         // Otherwise, reset it
28195820184SJason M. Bills         index = 0;
28295820184SJason M. Bills     }
28395820184SJason M. Bills     // Save the timestamp
28495820184SJason M. Bills     prevTs = curTs;
28595820184SJason M. Bills 
28695820184SJason M. Bills     entryID = std::to_string(curTs);
28795820184SJason M. Bills     if (index > 0)
28895820184SJason M. Bills     {
28995820184SJason M. Bills         entryID += "_" + std::to_string(index);
29095820184SJason M. Bills     }
29195820184SJason M. Bills     return true;
29295820184SJason M. Bills }
29395820184SJason M. Bills 
2947e860f15SJohn Edward Broadbent inline static bool
2958d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2968d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2978d1b46d7Szhanghch05                        uint64_t& index)
29816428a1aSJason M. Bills {
29916428a1aSJason M. Bills     if (entryID.empty())
30016428a1aSJason M. Bills     {
30116428a1aSJason M. Bills         return false;
30216428a1aSJason M. Bills     }
30316428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
30439e77504SEd Tanous     std::string_view tsStr(entryID);
30516428a1aSJason M. Bills 
30681ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
30771d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
30816428a1aSJason M. Bills     {
30916428a1aSJason M. Bills         // Timestamp has an index
31016428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
31139e77504SEd Tanous         std::string_view indexStr(entryID);
31216428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
313c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
314c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
315c0bd5e4bSEd Tanous         if (ec != std::errc())
31616428a1aSJason M. Bills         {
3179db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
31816428a1aSJason M. Bills             return false;
31916428a1aSJason M. Bills         }
32016428a1aSJason M. Bills     }
32116428a1aSJason M. Bills     // Timestamp has no index
322c0bd5e4bSEd Tanous     auto [ptr, ec] =
323c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
324c0bd5e4bSEd Tanous     if (ec != std::errc())
32516428a1aSJason M. Bills     {
3269db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
32716428a1aSJason M. Bills         return false;
32816428a1aSJason M. Bills     }
32916428a1aSJason M. Bills     return true;
33016428a1aSJason M. Bills }
33116428a1aSJason M. Bills 
33295820184SJason M. Bills static bool
33395820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
33495820184SJason M. Bills {
33595820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
33695820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
33795820184SJason M. Bills 
33895820184SJason M. Bills     // Loop through the directory looking for redfish log files
33995820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
34095820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
34195820184SJason M. Bills     {
34295820184SJason M. Bills         // If we find a redfish log file, save the path
34395820184SJason M. Bills         std::string filename = dirEnt.path().filename();
34411ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
34595820184SJason M. Bills         {
34695820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
34795820184SJason M. Bills         }
34895820184SJason M. Bills     }
34995820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
35095820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
35195820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
35295820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
35395820184SJason M. Bills 
35495820184SJason M. Bills     return !redfishLogFiles.empty();
35595820184SJason M. Bills }
35695820184SJason M. Bills 
357aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
3582d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
359c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
360aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
361aefe3786SClaire Weinan {
362aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
363aefe3786SClaire Weinan     {
364aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
365aefe3786SClaire Weinan         {
366aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
367aefe3786SClaire Weinan             {
368aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
369aefe3786SClaire Weinan                 {
370aefe3786SClaire Weinan                     const auto* status =
371aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
372aefe3786SClaire Weinan                     if (status == nullptr)
373aefe3786SClaire Weinan                     {
374aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
375aefe3786SClaire Weinan                         break;
376aefe3786SClaire Weinan                     }
377aefe3786SClaire Weinan                     dumpStatus = *status;
378aefe3786SClaire Weinan                 }
379aefe3786SClaire Weinan             }
380aefe3786SClaire Weinan         }
381aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
382aefe3786SClaire Weinan         {
383aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
384aefe3786SClaire Weinan             {
385aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
386aefe3786SClaire Weinan                 {
387aefe3786SClaire Weinan                     const auto* sizePtr =
388aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
389aefe3786SClaire Weinan                     if (sizePtr == nullptr)
390aefe3786SClaire Weinan                     {
391aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
392aefe3786SClaire Weinan                         break;
393aefe3786SClaire Weinan                     }
394aefe3786SClaire Weinan                     size = *sizePtr;
395aefe3786SClaire Weinan                     break;
396aefe3786SClaire Weinan                 }
397aefe3786SClaire Weinan             }
398aefe3786SClaire Weinan         }
399aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
400aefe3786SClaire Weinan         {
401aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
402aefe3786SClaire Weinan             {
403aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
404aefe3786SClaire Weinan                 {
405aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
406aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
407aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
408aefe3786SClaire Weinan                     {
409aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
410aefe3786SClaire Weinan                         break;
411aefe3786SClaire Weinan                     }
412c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
413aefe3786SClaire Weinan                     break;
414aefe3786SClaire Weinan                 }
415aefe3786SClaire Weinan             }
416aefe3786SClaire Weinan         }
417aefe3786SClaire Weinan     }
418aefe3786SClaire Weinan }
419aefe3786SClaire Weinan 
42021ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
421fdd26906SClaire Weinan {
422fdd26906SClaire Weinan     std::string entriesPath;
423fdd26906SClaire Weinan 
424fdd26906SClaire Weinan     if (dumpType == "BMC")
425fdd26906SClaire Weinan     {
426fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
427fdd26906SClaire Weinan     }
428fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
429fdd26906SClaire Weinan     {
430fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
431fdd26906SClaire Weinan     }
432fdd26906SClaire Weinan     else if (dumpType == "System")
433fdd26906SClaire Weinan     {
434fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
435fdd26906SClaire Weinan     }
436fdd26906SClaire Weinan     else
437fdd26906SClaire Weinan     {
438fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
439fdd26906SClaire Weinan                          << dumpType;
440fdd26906SClaire Weinan     }
441fdd26906SClaire Weinan 
442fdd26906SClaire Weinan     // Returns empty string on error
443fdd26906SClaire Weinan     return entriesPath;
444fdd26906SClaire Weinan }
445fdd26906SClaire Weinan 
4468d1b46d7Szhanghch05 inline void
4478d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4485cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4495cb1dd27SAsmitha Karunanithi {
450fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
451fdd26906SClaire Weinan     if (entriesPath.empty())
4525cb1dd27SAsmitha Karunanithi     {
4535cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4545cb1dd27SAsmitha Karunanithi         return;
4555cb1dd27SAsmitha Karunanithi     }
4565cb1dd27SAsmitha Karunanithi 
4575cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
458fdd26906SClaire Weinan         [asyncResp, entriesPath,
459711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
460711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4615cb1dd27SAsmitha Karunanithi         if (ec)
4625cb1dd27SAsmitha Karunanithi         {
4635cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4645cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4655cb1dd27SAsmitha Karunanithi             return;
4665cb1dd27SAsmitha Karunanithi         }
4675cb1dd27SAsmitha Karunanithi 
468fdd26906SClaire Weinan         // Remove ending slash
469fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
470fdd26906SClaire Weinan         if (!odataIdStr.empty())
471fdd26906SClaire Weinan         {
472fdd26906SClaire Weinan             odataIdStr.pop_back();
473fdd26906SClaire Weinan         }
474fdd26906SClaire Weinan 
475fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
476fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
477fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
478fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
479fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
480fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
481fdd26906SClaire Weinan 
4825cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4835cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
484b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
485b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
486002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
4875cb1dd27SAsmitha Karunanithi 
488002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
489002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
490002d39b4SEd Tanous                                                r.first.filename());
491565dfb6fSClaire Weinan         });
492565dfb6fSClaire Weinan 
4935cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4945cb1dd27SAsmitha Karunanithi         {
495b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4965cb1dd27SAsmitha Karunanithi             {
4975cb1dd27SAsmitha Karunanithi                 continue;
4985cb1dd27SAsmitha Karunanithi             }
499c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5005cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
50135440d18SAsmitha Karunanithi             std::string dumpStatus;
502433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
5032dfd18efSEd Tanous 
5042dfd18efSEd Tanous             std::string entryID = object.first.filename();
5052dfd18efSEd Tanous             if (entryID.empty())
5065cb1dd27SAsmitha Karunanithi             {
5075cb1dd27SAsmitha Karunanithi                 continue;
5085cb1dd27SAsmitha Karunanithi             }
5095cb1dd27SAsmitha Karunanithi 
510c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
511aefe3786SClaire Weinan                                          asyncResp);
5125cb1dd27SAsmitha Karunanithi 
5130fda0f12SGeorge Liu             if (dumpStatus !=
5140fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
51535440d18SAsmitha Karunanithi                 !dumpStatus.empty())
51635440d18SAsmitha Karunanithi             {
51735440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
51835440d18SAsmitha Karunanithi                 continue;
51935440d18SAsmitha Karunanithi             }
52035440d18SAsmitha Karunanithi 
5219c11a172SVijay Lobo             thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
522fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
5235cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
5245cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
5255cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
526bbd80db8SClaire Weinan             thisEntry["Created"] =
527bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5285cb1dd27SAsmitha Karunanithi 
5295cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5305cb1dd27SAsmitha Karunanithi             {
531d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
532d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
533fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
534fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5355cb1dd27SAsmitha Karunanithi             }
5365cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5375cb1dd27SAsmitha Karunanithi             {
538d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
539d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
540d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
541fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
542fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5435cb1dd27SAsmitha Karunanithi             }
54435440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5455cb1dd27SAsmitha Karunanithi         }
546002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5475cb1dd27SAsmitha Karunanithi         },
5485cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5495cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5505cb1dd27SAsmitha Karunanithi }
5515cb1dd27SAsmitha Karunanithi 
5528d1b46d7Szhanghch05 inline void
553c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5548d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5555cb1dd27SAsmitha Karunanithi {
556fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
557fdd26906SClaire Weinan     if (entriesPath.empty())
5585cb1dd27SAsmitha Karunanithi     {
5595cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5605cb1dd27SAsmitha Karunanithi         return;
5615cb1dd27SAsmitha Karunanithi     }
5625cb1dd27SAsmitha Karunanithi 
5635cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
564fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
565fdd26906SClaire Weinan          entriesPath](const boost::system::error_code ec,
56602cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5675cb1dd27SAsmitha Karunanithi         if (ec)
5685cb1dd27SAsmitha Karunanithi         {
5695cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5705cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5715cb1dd27SAsmitha Karunanithi             return;
5725cb1dd27SAsmitha Karunanithi         }
5735cb1dd27SAsmitha Karunanithi 
574b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
575b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
576b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
577002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
578b47452b2SAsmitha Karunanithi 
5799eb808c1SEd Tanous         for (const auto& objectPath : resp)
5805cb1dd27SAsmitha Karunanithi         {
581b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5825cb1dd27SAsmitha Karunanithi             {
5835cb1dd27SAsmitha Karunanithi                 continue;
5845cb1dd27SAsmitha Karunanithi             }
5855cb1dd27SAsmitha Karunanithi 
5865cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
587c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5885cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
58935440d18SAsmitha Karunanithi             std::string dumpStatus;
5905cb1dd27SAsmitha Karunanithi 
591aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
592c6fecdabSClaire Weinan                                          timestampUs, asyncResp);
5935cb1dd27SAsmitha Karunanithi 
5940fda0f12SGeorge Liu             if (dumpStatus !=
5950fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
59635440d18SAsmitha Karunanithi                 !dumpStatus.empty())
59735440d18SAsmitha Karunanithi             {
59835440d18SAsmitha Karunanithi                 // Dump status is not Complete
59935440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
600d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
601d1bde9e5SKrzysztof Grobelny                                            entryID);
60235440d18SAsmitha Karunanithi                 return;
60335440d18SAsmitha Karunanithi             }
60435440d18SAsmitha Karunanithi 
6055cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
6069c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
607fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
6085cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
6095cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
6105cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
611bbd80db8SClaire Weinan             asyncResp->res.jsonValue["Created"] =
612bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
6135cb1dd27SAsmitha Karunanithi 
6145cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
6155cb1dd27SAsmitha Karunanithi             {
616d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
617d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
618fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
619fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6205cb1dd27SAsmitha Karunanithi             }
6215cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6225cb1dd27SAsmitha Karunanithi             {
623d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
624002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
625d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
626fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
627fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6285cb1dd27SAsmitha Karunanithi             }
6295cb1dd27SAsmitha Karunanithi         }
630e05aec50SEd Tanous         if (!foundDumpEntry)
631b47452b2SAsmitha Karunanithi         {
632b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
633b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
634b47452b2SAsmitha Karunanithi             return;
635b47452b2SAsmitha Karunanithi         }
6365cb1dd27SAsmitha Karunanithi         },
6375cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6385cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6395cb1dd27SAsmitha Karunanithi }
6405cb1dd27SAsmitha Karunanithi 
6418d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6429878256fSStanley Chu                             const std::string& entryID,
643b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6445cb1dd27SAsmitha Karunanithi {
645002d39b4SEd Tanous     auto respHandler =
646002d39b4SEd Tanous         [asyncResp, entryID](const boost::system::error_code ec) {
6475cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6485cb1dd27SAsmitha Karunanithi         if (ec)
6495cb1dd27SAsmitha Karunanithi         {
6503de8d8baSGeorge Liu             if (ec.value() == EBADR)
6513de8d8baSGeorge Liu             {
6523de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6533de8d8baSGeorge Liu                 return;
6543de8d8baSGeorge Liu             }
6555cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
656fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6575cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6585cb1dd27SAsmitha Karunanithi             return;
6595cb1dd27SAsmitha Karunanithi         }
6605cb1dd27SAsmitha Karunanithi     };
6615cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6625cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
663b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
664b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
665b47452b2SAsmitha Karunanithi             entryID,
6665cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6675cb1dd27SAsmitha Karunanithi }
6685cb1dd27SAsmitha Karunanithi 
6698e31778eSAsmitha Karunanithi inline DumpCreationProgress
6708e31778eSAsmitha Karunanithi     mapDbusStatusToDumpProgress(const std::string& status)
671a43be80fSAsmitha Karunanithi {
6728e31778eSAsmitha Karunanithi     if (status ==
6738e31778eSAsmitha Karunanithi             "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
6748e31778eSAsmitha Karunanithi         status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
6758e31778eSAsmitha Karunanithi     {
6768e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_FAILED;
6778e31778eSAsmitha Karunanithi     }
6788e31778eSAsmitha Karunanithi     if (status ==
6798e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
6808e31778eSAsmitha Karunanithi     {
6818e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_SUCCESS;
6828e31778eSAsmitha Karunanithi     }
6838e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
6848e31778eSAsmitha Karunanithi }
6858e31778eSAsmitha Karunanithi 
6868e31778eSAsmitha Karunanithi inline DumpCreationProgress
6878e31778eSAsmitha Karunanithi     getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
6888e31778eSAsmitha Karunanithi {
6898e31778eSAsmitha Karunanithi     for (const auto& [key, val] : values)
6908e31778eSAsmitha Karunanithi     {
6918e31778eSAsmitha Karunanithi         if (key == "Status")
6928e31778eSAsmitha Karunanithi         {
6938e31778eSAsmitha Karunanithi             const std::string* value = std::get_if<std::string>(&val);
6948e31778eSAsmitha Karunanithi             if (value == nullptr)
6958e31778eSAsmitha Karunanithi             {
6968e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Status property value is null";
6978e31778eSAsmitha Karunanithi                 return DumpCreationProgress::DUMP_CREATE_FAILED;
6988e31778eSAsmitha Karunanithi             }
6998e31778eSAsmitha Karunanithi             return mapDbusStatusToDumpProgress(*value);
7008e31778eSAsmitha Karunanithi         }
7018e31778eSAsmitha Karunanithi     }
7028e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7038e31778eSAsmitha Karunanithi }
7048e31778eSAsmitha Karunanithi 
7058e31778eSAsmitha Karunanithi inline std::string getDumpEntryPath(const std::string& dumpPath)
7068e31778eSAsmitha Karunanithi {
7078e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
7088e31778eSAsmitha Karunanithi     {
7098e31778eSAsmitha Karunanithi         return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
7108e31778eSAsmitha Karunanithi     }
7118e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
7128e31778eSAsmitha Karunanithi     {
7138e31778eSAsmitha Karunanithi         return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
7148e31778eSAsmitha Karunanithi     }
7158e31778eSAsmitha Karunanithi     return "";
7168e31778eSAsmitha Karunanithi }
7178e31778eSAsmitha Karunanithi 
7188e31778eSAsmitha Karunanithi inline void createDumpTaskCallback(
7198e31778eSAsmitha Karunanithi     task::Payload&& payload,
7208e31778eSAsmitha Karunanithi     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7218e31778eSAsmitha Karunanithi     const sdbusplus::message::object_path& createdObjPath)
7228e31778eSAsmitha Karunanithi {
7238e31778eSAsmitha Karunanithi     const std::string dumpPath = createdObjPath.parent_path().str;
7248e31778eSAsmitha Karunanithi     const std::string dumpId = createdObjPath.filename();
7258e31778eSAsmitha Karunanithi 
7268e31778eSAsmitha Karunanithi     std::string dumpEntryPath = getDumpEntryPath(dumpPath);
7278e31778eSAsmitha Karunanithi 
7288e31778eSAsmitha Karunanithi     if (dumpEntryPath.empty())
7298e31778eSAsmitha Karunanithi     {
7308e31778eSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type received";
7318e31778eSAsmitha Karunanithi         messages::internalError(asyncResp->res);
7328e31778eSAsmitha Karunanithi         return;
7338e31778eSAsmitha Karunanithi     }
7348e31778eSAsmitha Karunanithi 
7358e31778eSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7368e31778eSAsmitha Karunanithi         [asyncResp, payload, createdObjPath,
7378e31778eSAsmitha Karunanithi          dumpEntryPath{std::move(dumpEntryPath)},
7388e31778eSAsmitha Karunanithi          dumpId](const boost::system::error_code ec,
7398e31778eSAsmitha Karunanithi                  const std::string& introspectXml) {
7408e31778eSAsmitha Karunanithi         if (ec)
7418e31778eSAsmitha Karunanithi         {
7428e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Introspect call failed with error: "
7438e31778eSAsmitha Karunanithi                              << ec.message();
7448e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7458e31778eSAsmitha Karunanithi             return;
7468e31778eSAsmitha Karunanithi         }
7478e31778eSAsmitha Karunanithi 
7488e31778eSAsmitha Karunanithi         // Check if the created dump object has implemented Progress
7498e31778eSAsmitha Karunanithi         // interface to track dump completion. If yes, fetch the "Status"
7508e31778eSAsmitha Karunanithi         // property of the interface, modify the task state accordingly.
7518e31778eSAsmitha Karunanithi         // Else, return task completed.
7528e31778eSAsmitha Karunanithi         tinyxml2::XMLDocument doc;
7538e31778eSAsmitha Karunanithi 
7548e31778eSAsmitha Karunanithi         doc.Parse(introspectXml.data(), introspectXml.size());
7558e31778eSAsmitha Karunanithi         tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
7568e31778eSAsmitha Karunanithi         if (pRoot == nullptr)
7578e31778eSAsmitha Karunanithi         {
7588e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "XML document failed to parse";
7598e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7608e31778eSAsmitha Karunanithi             return;
7618e31778eSAsmitha Karunanithi         }
7628e31778eSAsmitha Karunanithi         tinyxml2::XMLElement* interfaceNode =
7638e31778eSAsmitha Karunanithi             pRoot->FirstChildElement("interface");
7648e31778eSAsmitha Karunanithi 
7658e31778eSAsmitha Karunanithi         bool isProgressIntfPresent = false;
7668e31778eSAsmitha Karunanithi         while (interfaceNode != nullptr)
7678e31778eSAsmitha Karunanithi         {
7688e31778eSAsmitha Karunanithi             const char* thisInterfaceName = interfaceNode->Attribute("name");
7698e31778eSAsmitha Karunanithi             if (thisInterfaceName != nullptr)
7708e31778eSAsmitha Karunanithi             {
7718e31778eSAsmitha Karunanithi                 if (thisInterfaceName ==
7728e31778eSAsmitha Karunanithi                     std::string_view("xyz.openbmc_project.Common.Progress"))
7738e31778eSAsmitha Karunanithi                 {
7748e31778eSAsmitha Karunanithi                     interfaceNode =
7758e31778eSAsmitha Karunanithi                         interfaceNode->NextSiblingElement("interface");
7768e31778eSAsmitha Karunanithi                     continue;
7778e31778eSAsmitha Karunanithi                 }
7788e31778eSAsmitha Karunanithi                 isProgressIntfPresent = true;
7798e31778eSAsmitha Karunanithi                 break;
7808e31778eSAsmitha Karunanithi             }
7818e31778eSAsmitha Karunanithi             interfaceNode = interfaceNode->NextSiblingElement("interface");
7828e31778eSAsmitha Karunanithi         }
7838e31778eSAsmitha Karunanithi 
784a43be80fSAsmitha Karunanithi         std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7858e31778eSAsmitha Karunanithi             [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
7865b378546SPatrick Williams                 boost::system::error_code err, sdbusplus::message_t& msg,
787a43be80fSAsmitha Karunanithi                 const std::shared_ptr<task::TaskData>& taskData) {
788cb13a392SEd Tanous             if (err)
789cb13a392SEd Tanous             {
7908e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << createdObjPath.str
7918e31778eSAsmitha Karunanithi                                  << ": Error in creating dump";
7928e31778eSAsmitha Karunanithi                 taskData->messages.emplace_back(messages::internalError());
7936145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7946145ed6fSAsmitha Karunanithi                 return task::completed;
795cb13a392SEd Tanous             }
796b9d36b47SEd Tanous 
7978e31778eSAsmitha Karunanithi             if (isProgressIntfPresent)
798a43be80fSAsmitha Karunanithi             {
7998e31778eSAsmitha Karunanithi                 dbus::utility::DBusPropertiesMap values;
8008e31778eSAsmitha Karunanithi                 std::string prop;
8018e31778eSAsmitha Karunanithi                 msg.read(prop, values);
8028e31778eSAsmitha Karunanithi 
8038e31778eSAsmitha Karunanithi                 DumpCreationProgress dumpStatus =
8048e31778eSAsmitha Karunanithi                     getDumpCompletionStatus(values);
8058e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
8068e31778eSAsmitha Karunanithi                 {
8078e31778eSAsmitha Karunanithi                     BMCWEB_LOG_ERROR << createdObjPath.str
8088e31778eSAsmitha Karunanithi                                      << ": Error in creating dump";
8098e31778eSAsmitha Karunanithi                     taskData->state = "Cancelled";
8108e31778eSAsmitha Karunanithi                     return task::completed;
8118e31778eSAsmitha Karunanithi                 }
8128e31778eSAsmitha Karunanithi 
8138e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
8148e31778eSAsmitha Karunanithi                 {
8158e31778eSAsmitha Karunanithi                     BMCWEB_LOG_DEBUG << createdObjPath.str
8168e31778eSAsmitha Karunanithi                                      << ": Dump creation task is in progress";
8178e31778eSAsmitha Karunanithi                     return !task::completed;
8188e31778eSAsmitha Karunanithi                 }
8198e31778eSAsmitha Karunanithi             }
8208e31778eSAsmitha Karunanithi 
821a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
822a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
823a43be80fSAsmitha Karunanithi 
824a43be80fSAsmitha Karunanithi             std::string headerLoc =
8258e31778eSAsmitha Karunanithi                 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
826002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
827a43be80fSAsmitha Karunanithi 
8288e31778eSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << createdObjPath.str
8298e31778eSAsmitha Karunanithi                              << ": Dump creation task completed";
830a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
831b47452b2SAsmitha Karunanithi             return task::completed;
832a43be80fSAsmitha Karunanithi             },
8338e31778eSAsmitha Karunanithi             "type='signal',interface='org.freedesktop.DBus.Properties',"
8348e31778eSAsmitha Karunanithi             "member='PropertiesChanged',path='" +
8358e31778eSAsmitha Karunanithi                 createdObjPath.str + "'");
836a43be80fSAsmitha Karunanithi 
8378e31778eSAsmitha Karunanithi         // The task timer is set to max time limit within which the
8388e31778eSAsmitha Karunanithi         // requested dump will be collected.
8398e31778eSAsmitha Karunanithi         task->startTimer(std::chrono::minutes(6));
840a43be80fSAsmitha Karunanithi         task->populateResp(asyncResp->res);
8418e31778eSAsmitha Karunanithi         task->payload.emplace(payload);
8428e31778eSAsmitha Karunanithi         },
8438e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", createdObjPath,
8448e31778eSAsmitha Karunanithi         "org.freedesktop.DBus.Introspectable", "Introspect");
845a43be80fSAsmitha Karunanithi }
846a43be80fSAsmitha Karunanithi 
8478d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8488d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
849a43be80fSAsmitha Karunanithi {
850fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
851fdd26906SClaire Weinan     if (dumpPath.empty())
852a43be80fSAsmitha Karunanithi     {
853a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
854a43be80fSAsmitha Karunanithi         return;
855a43be80fSAsmitha Karunanithi     }
856a43be80fSAsmitha Karunanithi 
857a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
858a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
859a43be80fSAsmitha Karunanithi 
86015ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
861a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
862a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
863a43be80fSAsmitha Karunanithi     {
864a43be80fSAsmitha Karunanithi         return;
865a43be80fSAsmitha Karunanithi     }
866a43be80fSAsmitha Karunanithi 
867a43be80fSAsmitha Karunanithi     if (dumpType == "System")
868a43be80fSAsmitha Karunanithi     {
869a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
870a43be80fSAsmitha Karunanithi         {
8714978b63fSJason M. Bills             BMCWEB_LOG_ERROR
8724978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
873a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
874a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
875a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
876a43be80fSAsmitha Karunanithi             return;
877a43be80fSAsmitha Karunanithi         }
8783174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
879a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
880a43be80fSAsmitha Karunanithi         {
881a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
882ace85d60SEd Tanous             messages::internalError(asyncResp->res);
883a43be80fSAsmitha Karunanithi             return;
884a43be80fSAsmitha Karunanithi         }
8855907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
886a43be80fSAsmitha Karunanithi     }
887a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
888a43be80fSAsmitha Karunanithi     {
889a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
890a43be80fSAsmitha Karunanithi         {
8910fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8920fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
893a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
894a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
895a43be80fSAsmitha Karunanithi             return;
896a43be80fSAsmitha Karunanithi         }
8973174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
898a43be80fSAsmitha Karunanithi         {
899a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
900a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
901ace85d60SEd Tanous             messages::internalError(asyncResp->res);
902a43be80fSAsmitha Karunanithi             return;
903a43be80fSAsmitha Karunanithi         }
9045907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
9055907571dSAsmitha Karunanithi     }
9065907571dSAsmitha Karunanithi     else
9075907571dSAsmitha Karunanithi     {
9085907571dSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
9095907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
9105907571dSAsmitha Karunanithi         return;
911a43be80fSAsmitha Karunanithi     }
912a43be80fSAsmitha Karunanithi 
9138e31778eSAsmitha Karunanithi     std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
9148e31778eSAsmitha Karunanithi         createDumpParamVec;
9158e31778eSAsmitha Karunanithi 
916a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
9175b378546SPatrick Williams         [asyncResp, payload(task::Payload(req)), dumpPath](
9185b378546SPatrick Williams             const boost::system::error_code ec, const sdbusplus::message_t& msg,
9198e31778eSAsmitha Karunanithi             const sdbusplus::message::object_path& objPath) mutable {
920a43be80fSAsmitha Karunanithi         if (ec)
921a43be80fSAsmitha Karunanithi         {
922a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
9235907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
9245907571dSAsmitha Karunanithi             if (dbusError == nullptr)
9255907571dSAsmitha Karunanithi             {
9265907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
9275907571dSAsmitha Karunanithi                 return;
9285907571dSAsmitha Karunanithi             }
9295907571dSAsmitha Karunanithi 
9305907571dSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
9315907571dSAsmitha Karunanithi                              << " and error msg: " << dbusError->message;
9325907571dSAsmitha Karunanithi             if (std::string_view(
9335907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
9345907571dSAsmitha Karunanithi                 dbusError->name)
9355907571dSAsmitha Karunanithi             {
9365907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
9375907571dSAsmitha Karunanithi                 return;
9385907571dSAsmitha Karunanithi             }
9395907571dSAsmitha Karunanithi             if (std::string_view(
9405907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
9415907571dSAsmitha Karunanithi                 dbusError->name)
9425907571dSAsmitha Karunanithi             {
9435907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
9445907571dSAsmitha Karunanithi                 return;
9455907571dSAsmitha Karunanithi             }
9465907571dSAsmitha Karunanithi             if (std::string_view(
9475907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
9485907571dSAsmitha Karunanithi                 dbusError->name)
9495907571dSAsmitha Karunanithi             {
9505907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
9515907571dSAsmitha Karunanithi                 return;
9525907571dSAsmitha Karunanithi             }
9535907571dSAsmitha Karunanithi             // Other Dbus errors such as:
9545907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
9555907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
9565907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
9575907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
9585907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
9595907571dSAsmitha Karunanithi             // back to the client.
960a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
961a43be80fSAsmitha Karunanithi             return;
962a43be80fSAsmitha Karunanithi         }
9638e31778eSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
9648e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
965a43be80fSAsmitha Karunanithi         },
966b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
967b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
968b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
9698e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
970a43be80fSAsmitha Karunanithi }
971a43be80fSAsmitha Karunanithi 
9728d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9738d1b46d7Szhanghch05                       const std::string& dumpType)
97480319af1SAsmitha Karunanithi {
975b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
976b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
9778d1b46d7Szhanghch05 
9780d946211SClaire Weinan     crow::connections::systemBus->async_method_call(
9790d946211SClaire Weinan         [asyncResp](const boost::system::error_code& ec) {
98080319af1SAsmitha Karunanithi         if (ec)
98180319af1SAsmitha Karunanithi         {
9820d946211SClaire Weinan             BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
98380319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
98480319af1SAsmitha Karunanithi             return;
98580319af1SAsmitha Karunanithi         }
9860d946211SClaire Weinan         },
9870d946211SClaire Weinan         "xyz.openbmc_project.Dump.Manager",
9880d946211SClaire Weinan         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
9890d946211SClaire Weinan         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
99080319af1SAsmitha Karunanithi }
99180319af1SAsmitha Karunanithi 
992b9d36b47SEd Tanous inline static void
993b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
994b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
995b9d36b47SEd Tanous                              std::string& logfile)
996043a0536SJohnathan Mantey {
997d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
998d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
999d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
1000d1bde9e5SKrzysztof Grobelny 
1001d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1002d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1003d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1004d1bde9e5SKrzysztof Grobelny 
1005d1bde9e5SKrzysztof Grobelny     if (!success)
1006043a0536SJohnathan Mantey     {
1007d1bde9e5SKrzysztof Grobelny         return;
1008043a0536SJohnathan Mantey     }
1009d1bde9e5SKrzysztof Grobelny 
1010d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1011043a0536SJohnathan Mantey     {
1012d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1013d1bde9e5SKrzysztof Grobelny     }
1014d1bde9e5SKrzysztof Grobelny 
1015d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1016043a0536SJohnathan Mantey     {
1017d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1018043a0536SJohnathan Mantey     }
1019d1bde9e5SKrzysztof Grobelny 
1020d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1021043a0536SJohnathan Mantey     {
1022d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1023043a0536SJohnathan Mantey     }
1024043a0536SJohnathan Mantey }
1025043a0536SJohnathan Mantey 
10267e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
10271da66f75SEd Tanous {
1028c4bf6374SJason M. Bills     /**
1029c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1030c4bf6374SJason M. Bills      */
103122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1032ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1033002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1034002d39b4SEd Tanous             [&app](const crow::Request& req,
103522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
103622d268cbSEd Tanous                    const std::string& systemName) {
10373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1038c4bf6374SJason M. Bills         {
103945ca1b86SEd Tanous             return;
104045ca1b86SEd Tanous         }
104122d268cbSEd Tanous         if (systemName != "system")
104222d268cbSEd Tanous         {
104322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
104422d268cbSEd Tanous                                        systemName);
104522d268cbSEd Tanous             return;
104622d268cbSEd Tanous         }
104722d268cbSEd Tanous 
10487e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
10497e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1050c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1051c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1052c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1053029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
105445ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1055c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1056c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1057002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1058c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
10591476687dSEd Tanous         nlohmann::json::object_t eventLog;
10601476687dSEd Tanous         eventLog["@odata.id"] =
10611476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
10621476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
10635cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
10641476687dSEd Tanous         nlohmann::json::object_t dumpLog;
1065002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
10661476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
1067c9bb6861Sraviteja-b #endif
1068c9bb6861Sraviteja-b 
1069d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
10701476687dSEd Tanous         nlohmann::json::object_t crashdump;
10711476687dSEd Tanous         crashdump["@odata.id"] =
10721476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
10731476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
1074d53dd41fSJason M. Bills #endif
1075b7028ebfSSpencer Ku 
1076b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
10771476687dSEd Tanous         nlohmann::json::object_t hostlogger;
10781476687dSEd Tanous         hostlogger["@odata.id"] =
10791476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
10801476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
1081b7028ebfSSpencer Ku #endif
1082c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1083c4bf6374SJason M. Bills             logServiceArray.size();
1084a3316fc6SZhikuiRen 
10857a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
10867a1dbc48SGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode"};
10877a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
10887a1dbc48SGeorge Liu             "/", 0, interfaces,
10897a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
1090b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1091b9d36b47SEd Tanous                             subtreePath) {
1092a3316fc6SZhikuiRen             if (ec)
1093a3316fc6SZhikuiRen             {
1094a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
1095a3316fc6SZhikuiRen                 return;
1096a3316fc6SZhikuiRen             }
1097a3316fc6SZhikuiRen 
109855f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1099a3316fc6SZhikuiRen             {
1100a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1101a3316fc6SZhikuiRen                 {
110223a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1103a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1104613dabeaSEd Tanous                     nlohmann::json::object_t member;
1105613dabeaSEd Tanous                     member["@odata.id"] =
1106613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
1107613dabeaSEd Tanous 
1108613dabeaSEd Tanous                     logServiceArrayLocal.push_back(std::move(member));
1109613dabeaSEd Tanous 
111045ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
111123a21a1cSEd Tanous                         logServiceArrayLocal.size();
1112a3316fc6SZhikuiRen                     return;
1113a3316fc6SZhikuiRen                 }
1114a3316fc6SZhikuiRen             }
11157a1dbc48SGeorge Liu             });
11167e860f15SJohn Edward Broadbent         });
1117c4bf6374SJason M. Bills }
1118c4bf6374SJason M. Bills 
11197e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1120c4bf6374SJason M. Bills {
112122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1122ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1123002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1124002d39b4SEd Tanous             [&app](const crow::Request& req,
112522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112622d268cbSEd Tanous                    const std::string& systemName) {
11273ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
112845ca1b86SEd Tanous         {
112945ca1b86SEd Tanous             return;
113045ca1b86SEd Tanous         }
113122d268cbSEd Tanous         if (systemName != "system")
113222d268cbSEd Tanous         {
113322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
113422d268cbSEd Tanous                                        systemName);
113522d268cbSEd Tanous             return;
113622d268cbSEd Tanous         }
1137c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1138029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1139c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1140c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1141c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1142002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1143c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1144c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
11457c8c4058STejas Patil 
11467c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
11472b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
11487c8c4058STejas Patil 
11497c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
11507c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
11517c8c4058STejas Patil             redfishDateTimeOffset.second;
11527c8c4058STejas Patil 
11531476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
11541476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1155e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1156e7d6c8b2SGunnar Mills 
11570fda0f12SGeorge Liu             {"target",
11580fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
11597e860f15SJohn Edward Broadbent         });
1160489640c6SJason M. Bills }
1161489640c6SJason M. Bills 
11627e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1163489640c6SJason M. Bills {
11644978b63fSJason M. Bills     BMCWEB_ROUTE(
11654978b63fSJason M. Bills         app,
116622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1167432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
11687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
116945ca1b86SEd Tanous             [&app](const crow::Request& req,
117022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117122d268cbSEd Tanous                    const std::string& systemName) {
11723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117345ca1b86SEd Tanous         {
117445ca1b86SEd Tanous             return;
117545ca1b86SEd Tanous         }
117622d268cbSEd Tanous         if (systemName != "system")
117722d268cbSEd Tanous         {
117822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
117922d268cbSEd Tanous                                        systemName);
118022d268cbSEd Tanous             return;
118122d268cbSEd Tanous         }
1182489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1183489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1184489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1185489640c6SJason M. Bills         {
1186489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1187489640c6SJason M. Bills             {
1188489640c6SJason M. Bills                 std::error_code ec;
1189489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1190489640c6SJason M. Bills             }
1191489640c6SJason M. Bills         }
1192489640c6SJason M. Bills 
1193489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1194489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1195489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1196489640c6SJason M. Bills             if (ec)
1197489640c6SJason M. Bills             {
1198002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1199489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1200489640c6SJason M. Bills                 return;
1201489640c6SJason M. Bills             }
1202489640c6SJason M. Bills 
1203489640c6SJason M. Bills             messages::success(asyncResp->res);
1204489640c6SJason M. Bills             },
1205489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1206002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1207002d39b4SEd Tanous             "replace");
12087e860f15SJohn Edward Broadbent         });
1209c4bf6374SJason M. Bills }
1210c4bf6374SJason M. Bills 
1211ac992cdeSJason M. Bills enum class LogParseError
1212ac992cdeSJason M. Bills {
1213ac992cdeSJason M. Bills     success,
1214ac992cdeSJason M. Bills     parseFailed,
1215ac992cdeSJason M. Bills     messageIdNotInRegistry,
1216ac992cdeSJason M. Bills };
1217ac992cdeSJason M. Bills 
1218ac992cdeSJason M. Bills static LogParseError
1219ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1220b5a76932SEd Tanous                           const std::string& logEntry,
1221de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1222c4bf6374SJason M. Bills {
122395820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1224cd225da8SJason M. Bills     // First get the Timestamp
1225f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1226cd225da8SJason M. Bills     if (space == std::string::npos)
122795820184SJason M. Bills     {
1228ac992cdeSJason M. Bills         return LogParseError::parseFailed;
122995820184SJason M. Bills     }
1230cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1231cd225da8SJason M. Bills     // Then get the log contents
1232f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1233cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1234cd225da8SJason M. Bills     {
1235ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1236cd225da8SJason M. Bills     }
1237cd225da8SJason M. Bills     std::string_view entry(logEntry);
1238cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1239cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1240cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1241*50ebd4afSEd Tanous     bmcweb::split(logEntryFields, entry, ',');
1242cd225da8SJason M. Bills     // We need at least a MessageId to be valid
124326f6976fSEd Tanous     if (logEntryFields.empty())
1244cd225da8SJason M. Bills     {
1245ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1246cd225da8SJason M. Bills     }
1247cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
124895820184SJason M. Bills 
12494851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1250fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1251c4bf6374SJason M. Bills 
125254417b02SSui Chen     if (message == nullptr)
1253c4bf6374SJason M. Bills     {
125454417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1255ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1256c4bf6374SJason M. Bills     }
1257c4bf6374SJason M. Bills 
125854417b02SSui Chen     std::string msg = message->message;
125954417b02SSui Chen 
126015a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
126126702d01SEd Tanous     std::span<std::string> messageArgs;
126215a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
126315a86ff6SJason M. Bills     {
126415a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
126515a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
126615a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
126715a86ff6SJason M. Bills         if (!messageArgsStart.empty())
126815a86ff6SJason M. Bills         {
126915a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
127015a86ff6SJason M. Bills         }
127115a86ff6SJason M. Bills 
127223a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1273c4bf6374SJason M. Bills 
12744851d45dSJason M. Bills         // Fill the MessageArgs into the Message
127595820184SJason M. Bills         int i = 0;
127695820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
12774851d45dSJason M. Bills         {
127895820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
12794851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
12804851d45dSJason M. Bills             if (argPos != std::string::npos)
12814851d45dSJason M. Bills             {
128295820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
12834851d45dSJason M. Bills             }
12844851d45dSJason M. Bills         }
128515a86ff6SJason M. Bills     }
12864851d45dSJason M. Bills 
128795820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
128895820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
128995820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1290f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1291f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
129295820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1293c4bf6374SJason M. Bills     {
129495820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1295c4bf6374SJason M. Bills     }
1296c4bf6374SJason M. Bills 
1297c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
12989c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
129984afc48bSJason M. Bills     logEntryJson["@odata.id"] =
130084afc48bSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/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";
16337e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
16340fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
16357e860f15SJohn Edward Broadbent                     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"] =
17540fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1755f86bb901SAdriana Kobylak                 std::to_string(*id);
175645ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1757f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1758f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1759f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
17609017faf2SAbhishek Patel             std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
17619017faf2SAbhishek Patel             if (notifyAction)
17629017faf2SAbhishek Patel             {
17639017faf2SAbhishek Patel                 asyncResp->res.jsonValue["ServiceProviderNotified"] =
17649017faf2SAbhishek Patel                     *notifyAction;
17659017faf2SAbhishek Patel             }
17669c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
17679c11a172SVijay Lobo             {
17689c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
17699c11a172SVijay Lobo             }
1770f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1771f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1772f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1773f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
17742b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1775f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
17762b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1777f86bb901SAdriana Kobylak             if (filePath != nullptr)
1778f86bb901SAdriana Kobylak             {
1779f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1780e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1781e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1782f86bb901SAdriana Kobylak             }
1783d1bde9e5SKrzysztof Grobelny             });
17847e860f15SJohn Edward Broadbent         });
1785336e96c6SChicago Duan 
17867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
178722d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1788ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
17897e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
179045ca1b86SEd Tanous             [&app](const crow::Request& req,
17917e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
179222d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
17933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179445ca1b86SEd Tanous         {
179545ca1b86SEd Tanous             return;
179645ca1b86SEd Tanous         }
179722d268cbSEd Tanous         if (systemName != "system")
179822d268cbSEd Tanous         {
179922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
180022d268cbSEd Tanous                                        systemName);
180122d268cbSEd Tanous             return;
180222d268cbSEd Tanous         }
180375710de2SXiaochao Ma         std::optional<bool> resolved;
180475710de2SXiaochao Ma 
180515ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
18067e860f15SJohn Edward Broadbent                                       resolved))
180775710de2SXiaochao Ma         {
180875710de2SXiaochao Ma             return;
180975710de2SXiaochao Ma         }
181075710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
181175710de2SXiaochao Ma 
181275710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
18134f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
181475710de2SXiaochao Ma             if (ec)
181575710de2SXiaochao Ma             {
181675710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
181775710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
181875710de2SXiaochao Ma                 return;
181975710de2SXiaochao Ma             }
182075710de2SXiaochao Ma             },
182175710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
182275710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
182375710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
182475710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1825168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
18267e860f15SJohn Edward Broadbent         });
182775710de2SXiaochao Ma 
18287e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
182922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1830ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1831ed398213SEd Tanous 
1832002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1833002d39b4SEd Tanous             [&app](const crow::Request& req,
1834002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
183522d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1837336e96c6SChicago Duan         {
183845ca1b86SEd Tanous             return;
183945ca1b86SEd Tanous         }
184022d268cbSEd Tanous         if (systemName != "system")
184122d268cbSEd Tanous         {
184222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
184322d268cbSEd Tanous                                        systemName);
184422d268cbSEd Tanous             return;
184522d268cbSEd Tanous         }
1846336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1847336e96c6SChicago Duan 
18487e860f15SJohn Edward Broadbent         std::string entryID = param;
1849336e96c6SChicago Duan 
1850336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1851336e96c6SChicago Duan 
1852336e96c6SChicago Duan         // Process response from Logging service.
1853002d39b4SEd Tanous         auto respHandler =
1854002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1855002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1856336e96c6SChicago Duan             if (ec)
1857336e96c6SChicago Duan             {
18583de8d8baSGeorge Liu                 if (ec.value() == EBADR)
18593de8d8baSGeorge Liu                 {
186045ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
186145ca1b86SEd Tanous                                                entryID);
18623de8d8baSGeorge Liu                     return;
18633de8d8baSGeorge Liu                 }
1864336e96c6SChicago Duan                 // TODO Handle for specific error code
18650fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
18660fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1867336e96c6SChicago Duan                     << ec;
1868336e96c6SChicago Duan                 asyncResp->res.result(
1869336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1870336e96c6SChicago Duan                 return;
1871336e96c6SChicago Duan             }
1872336e96c6SChicago Duan 
1873336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1874336e96c6SChicago Duan         };
1875336e96c6SChicago Duan 
1876336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1877336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1878336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1879336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1880336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
18817e860f15SJohn Edward Broadbent         });
1882400fd1fbSAdriana Kobylak }
1883400fd1fbSAdriana Kobylak 
18847e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1885400fd1fbSAdriana Kobylak {
18860fda0f12SGeorge Liu     BMCWEB_ROUTE(
18870fda0f12SGeorge Liu         app,
188822d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1889ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
18907e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
189145ca1b86SEd Tanous             [&app](const crow::Request& req,
18927e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
189322d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18943ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18957e860f15SJohn Edward Broadbent         {
189645ca1b86SEd Tanous             return;
189745ca1b86SEd Tanous         }
189899351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
189999351cd8SEd Tanous                 req.getHeaderValue("Accept"),
19004a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1901400fd1fbSAdriana Kobylak         {
1902002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1903400fd1fbSAdriana Kobylak             return;
1904400fd1fbSAdriana Kobylak         }
190522d268cbSEd Tanous         if (systemName != "system")
190622d268cbSEd Tanous         {
190722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
190822d268cbSEd Tanous                                        systemName);
190922d268cbSEd Tanous             return;
191022d268cbSEd Tanous         }
1911400fd1fbSAdriana Kobylak 
19127e860f15SJohn Edward Broadbent         std::string entryID = param;
1913400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1914400fd1fbSAdriana Kobylak 
1915400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1916002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1917400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1918400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1919400fd1fbSAdriana Kobylak             {
1920002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1921002d39b4SEd Tanous                                            entryID);
1922400fd1fbSAdriana Kobylak                 return;
1923400fd1fbSAdriana Kobylak             }
1924400fd1fbSAdriana Kobylak             if (ec)
1925400fd1fbSAdriana Kobylak             {
1926400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1927400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1928400fd1fbSAdriana Kobylak                 return;
1929400fd1fbSAdriana Kobylak             }
1930400fd1fbSAdriana Kobylak 
1931400fd1fbSAdriana Kobylak             int fd = -1;
1932400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1933400fd1fbSAdriana Kobylak             if (fd == -1)
1934400fd1fbSAdriana Kobylak             {
1935400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1936400fd1fbSAdriana Kobylak                 return;
1937400fd1fbSAdriana Kobylak             }
1938400fd1fbSAdriana Kobylak 
1939400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1940400fd1fbSAdriana Kobylak             if (size == -1)
1941400fd1fbSAdriana Kobylak             {
1942400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1943400fd1fbSAdriana Kobylak                 return;
1944400fd1fbSAdriana Kobylak             }
1945400fd1fbSAdriana Kobylak 
1946400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1947400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1948400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1949400fd1fbSAdriana Kobylak             {
1950002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1951400fd1fbSAdriana Kobylak                                  << maxFileSize;
1952400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1953400fd1fbSAdriana Kobylak                 return;
1954400fd1fbSAdriana Kobylak             }
1955400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1956400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1957400fd1fbSAdriana Kobylak             if (rc == -1)
1958400fd1fbSAdriana Kobylak             {
1959400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1960400fd1fbSAdriana Kobylak                 return;
1961400fd1fbSAdriana Kobylak             }
1962400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1963400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1964400fd1fbSAdriana Kobylak             {
1965400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1966400fd1fbSAdriana Kobylak                 return;
1967400fd1fbSAdriana Kobylak             }
1968400fd1fbSAdriana Kobylak             close(fd);
1969400fd1fbSAdriana Kobylak 
1970400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1971002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1972400fd1fbSAdriana Kobylak 
1973d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1974400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1975d9f6c621SEd Tanous             asyncResp->res.addHeader(
1976d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
1977400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1978400fd1fbSAdriana Kobylak             },
1979400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1980400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1981400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
19827e860f15SJohn Edward Broadbent         });
19831da66f75SEd Tanous }
19841da66f75SEd Tanous 
1985b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1986b7028ebfSSpencer Ku 
1987b7028ebfSSpencer Ku inline bool
1988b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1989b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1990b7028ebfSSpencer Ku {
1991b7028ebfSSpencer Ku     std::error_code ec;
1992b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1993b7028ebfSSpencer Ku     if (ec)
1994b7028ebfSSpencer Ku     {
1995b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1996b7028ebfSSpencer Ku         return false;
1997b7028ebfSSpencer Ku     }
1998b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1999b7028ebfSSpencer Ku     {
2000b7028ebfSSpencer Ku         std::string filename = it.path().filename();
2001b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
2002b7028ebfSSpencer Ku         // path
200311ba3979SEd Tanous         if (filename.starts_with("log"))
2004b7028ebfSSpencer Ku         {
2005b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
2006b7028ebfSSpencer Ku         }
2007b7028ebfSSpencer Ku     }
2008b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
2009b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
2010b7028ebfSSpencer Ku     // descending order.
2011b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2012b7028ebfSSpencer Ku               AlphanumLess<std::string>());
2013b7028ebfSSpencer Ku 
2014b7028ebfSSpencer Ku     return true;
2015b7028ebfSSpencer Ku }
2016b7028ebfSSpencer Ku 
201702cad96eSEd Tanous inline bool getHostLoggerEntries(
201802cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
201902cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
2020b7028ebfSSpencer Ku {
2021b7028ebfSSpencer Ku     GzFileReader logFile;
2022b7028ebfSSpencer Ku 
2023b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
2024b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
2025b7028ebfSSpencer Ku     {
2026b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2027b7028ebfSSpencer Ku         {
2028b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
2029b7028ebfSSpencer Ku             return false;
2030b7028ebfSSpencer Ku         }
2031b7028ebfSSpencer Ku     }
2032b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2033b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2034b7028ebfSSpencer Ku     if (!lastMessage.empty())
2035b7028ebfSSpencer Ku     {
2036b7028ebfSSpencer Ku         logCount++;
2037b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2038b7028ebfSSpencer Ku         {
2039b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2040b7028ebfSSpencer Ku         }
2041b7028ebfSSpencer Ku     }
2042b7028ebfSSpencer Ku     return true;
2043b7028ebfSSpencer Ku }
2044b7028ebfSSpencer Ku 
2045b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2046b7028ebfSSpencer Ku                                     const std::string& msg,
20476d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2048b7028ebfSSpencer Ku {
2049b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
20509c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
20516d6574c9SJason M. Bills     logEntryJson["@odata.id"] =
2052b7028ebfSSpencer Ku         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
20536d6574c9SJason M. Bills         logEntryID;
20546d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
20556d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
20566d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
20576d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
20586d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
20596d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2060b7028ebfSSpencer Ku }
2061b7028ebfSSpencer Ku 
2062b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2063b7028ebfSSpencer Ku {
206422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2065b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
20661476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
20671476687dSEd Tanous             [&app](const crow::Request& req,
206822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
206922d268cbSEd Tanous                    const std::string& systemName) {
20703ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207145ca1b86SEd Tanous         {
207245ca1b86SEd Tanous             return;
207345ca1b86SEd Tanous         }
207422d268cbSEd Tanous         if (systemName != "system")
207522d268cbSEd Tanous         {
207622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
207722d268cbSEd Tanous                                        systemName);
207822d268cbSEd Tanous             return;
207922d268cbSEd Tanous         }
2080b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2081b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
2082b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2083b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
2084b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2085b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2086b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
20871476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
20881476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2089b7028ebfSSpencer Ku         });
2090b7028ebfSSpencer Ku }
2091b7028ebfSSpencer Ku 
2092b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2093b7028ebfSSpencer Ku {
2094b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
209522d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2096b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2097002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2098002d39b4SEd Tanous             [&app](const crow::Request& req,
209922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
210022d268cbSEd Tanous                    const std::string& systemName) {
2101c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2102c937d2bfSEd Tanous             .canDelegateTop = true,
2103c937d2bfSEd Tanous             .canDelegateSkip = true,
2104c937d2bfSEd Tanous         };
2105c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2106c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21073ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2108b7028ebfSSpencer Ku         {
2109b7028ebfSSpencer Ku             return;
2110b7028ebfSSpencer Ku         }
211122d268cbSEd Tanous         if (systemName != "system")
211222d268cbSEd Tanous         {
211322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
211422d268cbSEd Tanous                                        systemName);
211522d268cbSEd Tanous             return;
211622d268cbSEd Tanous         }
2117b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2118b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2119b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2120b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2121b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2122b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2123b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
21240fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2125b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2126b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2127b7028ebfSSpencer Ku 
2128b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2129b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2130b7028ebfSSpencer Ku         {
2131b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2132b7028ebfSSpencer Ku             return;
2133b7028ebfSSpencer Ku         }
21343648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
21353648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21365143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2137b7028ebfSSpencer Ku         size_t logCount = 0;
2138b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2139b7028ebfSSpencer Ku         // control by skip and top.
2140b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
21413648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
21423648c8beSEd Tanous                                   logCount))
2143b7028ebfSSpencer Ku         {
2144b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2145b7028ebfSSpencer Ku             return;
2146b7028ebfSSpencer Ku         }
2147b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2148b7028ebfSSpencer Ku         // log count
214926f6976fSEd Tanous         if (logEntries.empty())
2150b7028ebfSSpencer Ku         {
2151b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2152b7028ebfSSpencer Ku             return;
2153b7028ebfSSpencer Ku         }
215426f6976fSEd Tanous         if (!logEntries.empty())
2155b7028ebfSSpencer Ku         {
2156b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2157b7028ebfSSpencer Ku             {
21586d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
21593648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
21603648c8beSEd Tanous                                         hostLogEntry);
21616d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
2162b7028ebfSSpencer Ku             }
2163b7028ebfSSpencer Ku 
2164b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
21653648c8beSEd Tanous             if (skip + top < logCount)
2166b7028ebfSSpencer Ku             {
2167b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
21680fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
21693648c8beSEd Tanous                     std::to_string(skip + top);
2170b7028ebfSSpencer Ku             }
2171b7028ebfSSpencer Ku         }
2172b7028ebfSSpencer Ku         });
2173b7028ebfSSpencer Ku }
2174b7028ebfSSpencer Ku 
2175b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2176b7028ebfSSpencer Ku {
2177b7028ebfSSpencer Ku     BMCWEB_ROUTE(
217822d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2179b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2180b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
218145ca1b86SEd Tanous             [&app](const crow::Request& req,
2182b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218322d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
21843ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
218545ca1b86SEd Tanous         {
218645ca1b86SEd Tanous             return;
218745ca1b86SEd Tanous         }
218822d268cbSEd Tanous         if (systemName != "system")
218922d268cbSEd Tanous         {
219022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
219122d268cbSEd Tanous                                        systemName);
219222d268cbSEd Tanous             return;
219322d268cbSEd Tanous         }
2194b7028ebfSSpencer Ku         const std::string& targetID = param;
2195b7028ebfSSpencer Ku 
2196b7028ebfSSpencer Ku         uint64_t idInt = 0;
2197ca45aa3cSEd Tanous 
2198ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2199ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
2200ca45aa3cSEd Tanous 
2201ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
22029db4ba25SJiaqing Zhao         if (ec == std::errc::invalid_argument ||
22039db4ba25SJiaqing Zhao             ec == std::errc::result_out_of_range)
2204b7028ebfSSpencer Ku         {
22059db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2206b7028ebfSSpencer Ku             return;
2207b7028ebfSSpencer Ku         }
2208b7028ebfSSpencer Ku 
2209b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2210b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2211b7028ebfSSpencer Ku         {
2212b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2213b7028ebfSSpencer Ku             return;
2214b7028ebfSSpencer Ku         }
2215b7028ebfSSpencer Ku 
2216b7028ebfSSpencer Ku         size_t logCount = 0;
22173648c8beSEd Tanous         size_t top = 1;
2218b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2219b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2220b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2221b7028ebfSSpencer Ku         // get that entry
2222002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2223002d39b4SEd Tanous                                   logCount))
2224b7028ebfSSpencer Ku         {
2225b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2226b7028ebfSSpencer Ku             return;
2227b7028ebfSSpencer Ku         }
2228b7028ebfSSpencer Ku 
2229b7028ebfSSpencer Ku         if (!logEntries.empty())
2230b7028ebfSSpencer Ku         {
22316d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
22326d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
22336d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2234b7028ebfSSpencer Ku             return;
2235b7028ebfSSpencer Ku         }
2236b7028ebfSSpencer Ku 
2237b7028ebfSSpencer Ku         // Requested ID was not found
22389db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2239b7028ebfSSpencer Ku         });
2240b7028ebfSSpencer Ku }
2241b7028ebfSSpencer Ku 
2242dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2243fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2244fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22451da66f75SEd Tanous {
22463ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
224745ca1b86SEd Tanous     {
224845ca1b86SEd Tanous         return;
224945ca1b86SEd Tanous     }
22507e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
22517e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2252e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
22531da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2254e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2255e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2256002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2257e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
22581da66f75SEd Tanous         "Collection of LogServices for this Manager";
2259002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2260c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2261fdd26906SClaire Weinan 
2262c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2263613dabeaSEd Tanous     nlohmann::json::object_t journal;
2264613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2265613dabeaSEd Tanous     logServiceArray.push_back(std::move(journal));
2266c4bf6374SJason M. Bills #endif
2267fdd26906SClaire Weinan 
2268fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2269fdd26906SClaire Weinan 
2270fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
227115912159SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
22727a1dbc48SGeorge Liu         "xyz.openbmc_project.Collection.DeleteAll"};
22737a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
22747a1dbc48SGeorge Liu         "/xyz/openbmc_project/dump", 0, interfaces,
2275fdd26906SClaire Weinan         [asyncResp](
22767a1dbc48SGeorge Liu             const boost::system::error_code& ec,
2277fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2278fdd26906SClaire Weinan         if (ec)
2279fdd26906SClaire Weinan         {
2280fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2281dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2282fdd26906SClaire Weinan                 << ec;
2283fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2284fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2285fdd26906SClaire Weinan             return;
2286fdd26906SClaire Weinan         }
2287fdd26906SClaire Weinan 
2288fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2289fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2290fdd26906SClaire Weinan 
2291fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2292fdd26906SClaire Weinan         {
2293fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2294fdd26906SClaire Weinan             {
2295613dabeaSEd Tanous                 nlohmann::json::object_t member;
2296613dabeaSEd Tanous                 member["@odata.id"] =
2297613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2298613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2299fdd26906SClaire Weinan             }
2300fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2301fdd26906SClaire Weinan             {
2302613dabeaSEd Tanous                 nlohmann::json::object_t member;
2303613dabeaSEd Tanous                 member["@odata.id"] =
2304613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2305613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2306fdd26906SClaire Weinan             }
2307fdd26906SClaire Weinan         }
2308fdd26906SClaire Weinan 
2309e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2310fdd26906SClaire Weinan             logServiceArrayLocal.size();
23117a1dbc48SGeorge Liu         });
2312fdd26906SClaire Weinan #endif
2313fdd26906SClaire Weinan }
2314fdd26906SClaire Weinan 
2315fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2316fdd26906SClaire Weinan {
2317fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2318fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2319fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2320dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2321e1f26343SJason M. Bills }
2322e1f26343SJason M. Bills 
23237e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2324e1f26343SJason M. Bills {
23257e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2326ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23277e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
232845ca1b86SEd Tanous             [&app](const crow::Request& req,
232945ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23303ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
23317e860f15SJohn Edward Broadbent         {
233245ca1b86SEd Tanous             return;
233345ca1b86SEd Tanous         }
2334e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2335e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
23360f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
23370f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2338002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2339002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2340ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "Journal";
2341e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23427c8c4058STejas Patil 
23437c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
23442b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2345002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23467c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23477c8c4058STejas Patil             redfishDateTimeOffset.second;
23487c8c4058STejas Patil 
23491476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
23501476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
23517e860f15SJohn Edward Broadbent         });
2352e1f26343SJason M. Bills }
2353e1f26343SJason M. Bills 
23543a48b3a2SJason M. Bills static int
23553a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2356e1f26343SJason M. Bills                                sd_journal* journal,
23573a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2358e1f26343SJason M. Bills {
2359e1f26343SJason M. Bills     // Get the Log Entry contents
2360e1f26343SJason M. Bills     int ret = 0;
2361e1f26343SJason M. Bills 
2362a8fe54f0SJason M. Bills     std::string message;
2363a8fe54f0SJason M. Bills     std::string_view syslogID;
2364a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2365a8fe54f0SJason M. Bills     if (ret < 0)
2366a8fe54f0SJason M. Bills     {
2367a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2368a8fe54f0SJason M. Bills                          << strerror(-ret);
2369a8fe54f0SJason M. Bills     }
2370a8fe54f0SJason M. Bills     if (!syslogID.empty())
2371a8fe54f0SJason M. Bills     {
2372a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2373a8fe54f0SJason M. Bills     }
2374a8fe54f0SJason M. Bills 
237539e77504SEd Tanous     std::string_view msg;
237616428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2377e1f26343SJason M. Bills     if (ret < 0)
2378e1f26343SJason M. Bills     {
2379e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2380e1f26343SJason M. Bills         return 1;
2381e1f26343SJason M. Bills     }
2382a8fe54f0SJason M. Bills     message += std::string(msg);
2383e1f26343SJason M. Bills 
2384e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2385271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
238616428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2387e1f26343SJason M. Bills     if (ret < 0)
2388e1f26343SJason M. Bills     {
2389e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2390e1f26343SJason M. Bills     }
2391e1f26343SJason M. Bills 
2392e1f26343SJason M. Bills     // Get the Created time from the timestamp
239316428a1aSJason M. Bills     std::string entryTimeStr;
239416428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2395e1f26343SJason M. Bills     {
239616428a1aSJason M. Bills         return 1;
2397e1f26343SJason M. Bills     }
2398e1f26343SJason M. Bills 
2399e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
24009c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
240184afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.id"] =
240284afc48bSJason M. Bills         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
240384afc48bSJason M. Bills         bmcJournalLogEntryID;
240484afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
240584afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
240684afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
240784afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
240884afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2409738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
241084afc48bSJason M. Bills                                                          : "OK";
241184afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
241284afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2413e1f26343SJason M. Bills     return 0;
2414e1f26343SJason M. Bills }
2415e1f26343SJason M. Bills 
24167e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2417e1f26343SJason M. Bills {
24187e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2419ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2420002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2421002d39b4SEd Tanous             [&app](const crow::Request& req,
2422002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2423c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2424c937d2bfSEd Tanous             .canDelegateTop = true,
2425c937d2bfSEd Tanous             .canDelegateSkip = true,
2426c937d2bfSEd Tanous         };
2427c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2428c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
24293ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2430193ad2faSJason M. Bills         {
2431193ad2faSJason M. Bills             return;
2432193ad2faSJason M. Bills         }
24333648c8beSEd Tanous 
24343648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
24355143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
24363648c8beSEd Tanous 
24377e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
24387e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2439e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2440e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
24410f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
24420f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2443e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2444e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2445e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
24460fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2447e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2448e1f26343SJason M. Bills 
24497e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
24507e860f15SJohn Edward Broadbent         // unique ID for each entry
2451e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2452e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2453e1f26343SJason M. Bills         if (ret < 0)
2454e1f26343SJason M. Bills         {
2455002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2456f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2457e1f26343SJason M. Bills             return;
2458e1f26343SJason M. Bills         }
24590fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
24600fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2461e1f26343SJason M. Bills         journalTmp = nullptr;
2462b01bf299SEd Tanous         uint64_t entryCount = 0;
2463e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2464e85d6b16SJason M. Bills         bool firstEntry = true;
2465e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2466e1f26343SJason M. Bills         {
2467193ad2faSJason M. Bills             entryCount++;
24687e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
24697e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
24703648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2471193ad2faSJason M. Bills             {
2472193ad2faSJason M. Bills                 continue;
2473193ad2faSJason M. Bills             }
2474193ad2faSJason M. Bills 
247516428a1aSJason M. Bills             std::string idStr;
2476e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2477e1f26343SJason M. Bills             {
2478e1f26343SJason M. Bills                 continue;
2479e1f26343SJason M. Bills             }
2480e85d6b16SJason M. Bills             firstEntry = false;
2481e85d6b16SJason M. Bills 
24823a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2483c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2484c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2485e1f26343SJason M. Bills             {
2486f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2487e1f26343SJason M. Bills                 return;
2488e1f26343SJason M. Bills             }
24893a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2490e1f26343SJason M. Bills         }
2491193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
24923648c8beSEd Tanous         if (skip + top < entryCount)
2493193ad2faSJason M. Bills         {
2494193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
24950fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
24963648c8beSEd Tanous                 std::to_string(skip + top);
2497193ad2faSJason M. Bills         }
24987e860f15SJohn Edward Broadbent         });
2499e1f26343SJason M. Bills }
2500e1f26343SJason M. Bills 
25017e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2502e1f26343SJason M. Bills {
25037e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
25047e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2505ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
25067e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
250745ca1b86SEd Tanous             [&app](const crow::Request& req,
25087e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25097e860f15SJohn Edward Broadbent                    const std::string& entryID) {
25103ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
251145ca1b86SEd Tanous         {
251245ca1b86SEd Tanous             return;
251345ca1b86SEd Tanous         }
2514e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2515e1f26343SJason M. Bills         uint64_t ts = 0;
2516271584abSEd Tanous         uint64_t index = 0;
25178d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2518e1f26343SJason M. Bills         {
251916428a1aSJason M. Bills             return;
2520e1f26343SJason M. Bills         }
2521e1f26343SJason M. Bills 
2522e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2523e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2524e1f26343SJason M. Bills         if (ret < 0)
2525e1f26343SJason M. Bills         {
2526002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2527f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2528e1f26343SJason M. Bills             return;
2529e1f26343SJason M. Bills         }
2530002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2531002d39b4SEd Tanous             journalTmp, sd_journal_close);
2532e1f26343SJason M. Bills         journalTmp = nullptr;
25337e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
25347e860f15SJohn Edward Broadbent         // index tracking the unique ID
2535af07e3f5SJason M. Bills         std::string idStr;
2536af07e3f5SJason M. Bills         bool firstEntry = true;
2537e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
25382056b6d1SManojkiran Eda         if (ret < 0)
25392056b6d1SManojkiran Eda         {
25402056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
25412056b6d1SManojkiran Eda                              << strerror(-ret);
25422056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
25432056b6d1SManojkiran Eda             return;
25442056b6d1SManojkiran Eda         }
2545271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2546e1f26343SJason M. Bills         {
2547e1f26343SJason M. Bills             sd_journal_next(journal.get());
2548af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2549af07e3f5SJason M. Bills             {
2550af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2551af07e3f5SJason M. Bills                 return;
2552af07e3f5SJason M. Bills             }
2553af07e3f5SJason M. Bills             firstEntry = false;
2554af07e3f5SJason M. Bills         }
2555c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2556af07e3f5SJason M. Bills         if (idStr != entryID)
2557c4bf6374SJason M. Bills         {
25589db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
2559c4bf6374SJason M. Bills             return;
2560c4bf6374SJason M. Bills         }
2561c4bf6374SJason M. Bills 
25623a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2563c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
25643a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2565e1f26343SJason M. Bills         {
2566f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2567e1f26343SJason M. Bills             return;
2568e1f26343SJason M. Bills         }
2569d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
25707e860f15SJohn Edward Broadbent         });
2571c9bb6861Sraviteja-b }
2572c9bb6861Sraviteja-b 
2573fdd26906SClaire Weinan inline void
2574fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2575fdd26906SClaire Weinan                        const std::string& dumpType)
2576c9bb6861Sraviteja-b {
2577fdd26906SClaire Weinan     std::string dumpPath;
2578fdd26906SClaire Weinan     std::string overWritePolicy;
2579fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2580fdd26906SClaire Weinan 
2581fdd26906SClaire Weinan     if (dumpType == "BMC")
258245ca1b86SEd Tanous     {
2583fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2584fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2585fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2586fdd26906SClaire Weinan     }
2587fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2588fdd26906SClaire Weinan     {
2589fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2590fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2591fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2592fdd26906SClaire Weinan     }
2593fdd26906SClaire Weinan     else if (dumpType == "System")
2594fdd26906SClaire Weinan     {
2595fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2596fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2597fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2598fdd26906SClaire Weinan     }
2599fdd26906SClaire Weinan     else
2600fdd26906SClaire Weinan     {
2601fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2602fdd26906SClaire Weinan                          << dumpType;
2603fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
260445ca1b86SEd Tanous         return;
260545ca1b86SEd Tanous     }
2606fdd26906SClaire Weinan 
2607fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2608fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2609c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2610fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2611fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2612fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
26137c8c4058STejas Patil 
26147c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
26152b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
26160fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26177c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26187c8c4058STejas Patil         redfishDateTimeOffset.second;
26197c8c4058STejas Patil 
2620fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2621fdd26906SClaire Weinan 
2622fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2623fdd26906SClaire Weinan     {
2624002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26251476687dSEd Tanous                                 ["target"] =
2626fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2627fdd26906SClaire Weinan     }
26280d946211SClaire Weinan 
26290d946211SClaire Weinan     constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
26300d946211SClaire Weinan     dbus::utility::getSubTreePaths(
26310d946211SClaire Weinan         "/xyz/openbmc_project/dump", 0, interfaces,
26320d946211SClaire Weinan         [asyncResp, dumpType, dumpPath](
26330d946211SClaire Weinan             const boost::system::error_code& ec,
26340d946211SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
26350d946211SClaire Weinan         if (ec)
26360d946211SClaire Weinan         {
26370d946211SClaire Weinan             BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
26380d946211SClaire Weinan                              << ec;
26390d946211SClaire Weinan             // Assume that getting an error simply means there are no dump
26400d946211SClaire Weinan             // LogServices. Return without adding any error response.
26410d946211SClaire Weinan             return;
26420d946211SClaire Weinan         }
26430d946211SClaire Weinan 
26440d946211SClaire Weinan         const std::string dbusDumpPath =
26450d946211SClaire Weinan             "/xyz/openbmc_project/dump/" +
26460d946211SClaire Weinan             boost::algorithm::to_lower_copy(dumpType);
26470d946211SClaire Weinan 
26480d946211SClaire Weinan         for (const std::string& path : subTreePaths)
26490d946211SClaire Weinan         {
26500d946211SClaire Weinan             if (path == dbusDumpPath)
26510d946211SClaire Weinan             {
26520d946211SClaire Weinan                 asyncResp->res
26530d946211SClaire Weinan                     .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
26540d946211SClaire Weinan                     dumpPath + "/Actions/LogService.ClearLog";
26550d946211SClaire Weinan                 break;
26560d946211SClaire Weinan             }
26570d946211SClaire Weinan         }
26580d946211SClaire Weinan         });
2659c9bb6861Sraviteja-b }
2660c9bb6861Sraviteja-b 
2661fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2662fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2663fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26647e860f15SJohn Edward Broadbent {
26653ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266645ca1b86SEd Tanous     {
266745ca1b86SEd Tanous         return;
266845ca1b86SEd Tanous     }
2669fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2670fdd26906SClaire Weinan }
2671c9bb6861Sraviteja-b 
267222d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
267322d268cbSEd Tanous     crow::App& app, const crow::Request& req,
267422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
267522d268cbSEd Tanous     const std::string& chassisId)
267622d268cbSEd Tanous {
267722d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
267822d268cbSEd Tanous     {
267922d268cbSEd Tanous         return;
268022d268cbSEd Tanous     }
268122d268cbSEd Tanous     if (chassisId != "system")
268222d268cbSEd Tanous     {
268322d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
268422d268cbSEd Tanous         return;
268522d268cbSEd Tanous     }
268622d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
268722d268cbSEd Tanous }
268822d268cbSEd Tanous 
2689fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2690fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2691fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2692fdd26906SClaire Weinan {
2693fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2694fdd26906SClaire Weinan     {
2695fdd26906SClaire Weinan         return;
2696fdd26906SClaire Weinan     }
2697fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2698fdd26906SClaire Weinan }
2699fdd26906SClaire Weinan 
270022d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
270122d268cbSEd Tanous     crow::App& app, const crow::Request& req,
270222d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
270322d268cbSEd Tanous     const std::string& chassisId)
270422d268cbSEd Tanous {
270522d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
270622d268cbSEd Tanous     {
270722d268cbSEd Tanous         return;
270822d268cbSEd Tanous     }
270922d268cbSEd Tanous     if (chassisId != "system")
271022d268cbSEd Tanous     {
271122d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
271222d268cbSEd Tanous         return;
271322d268cbSEd Tanous     }
271422d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
271522d268cbSEd Tanous }
271622d268cbSEd Tanous 
2717fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2718fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2719fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2720fdd26906SClaire Weinan     const std::string& dumpId)
2721fdd26906SClaire Weinan {
2722fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2723fdd26906SClaire Weinan     {
2724fdd26906SClaire Weinan         return;
2725fdd26906SClaire Weinan     }
2726fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2727fdd26906SClaire Weinan }
272822d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
272922d268cbSEd Tanous     crow::App& app, const crow::Request& req,
273022d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
273122d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
273222d268cbSEd Tanous {
273322d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
273422d268cbSEd Tanous     {
273522d268cbSEd Tanous         return;
273622d268cbSEd Tanous     }
273722d268cbSEd Tanous     if (chassisId != "system")
273822d268cbSEd Tanous     {
273922d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
274022d268cbSEd Tanous         return;
274122d268cbSEd Tanous     }
274222d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
274322d268cbSEd Tanous }
2744fdd26906SClaire Weinan 
2745fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2746fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2747fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2748fdd26906SClaire Weinan     const std::string& dumpId)
2749fdd26906SClaire Weinan {
2750fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2751fdd26906SClaire Weinan     {
2752fdd26906SClaire Weinan         return;
2753fdd26906SClaire Weinan     }
2754fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2755fdd26906SClaire Weinan }
2756fdd26906SClaire Weinan 
275722d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
275822d268cbSEd Tanous     crow::App& app, const crow::Request& req,
275922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
276022d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
276122d268cbSEd Tanous {
276222d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
276322d268cbSEd Tanous     {
276422d268cbSEd Tanous         return;
276522d268cbSEd Tanous     }
276622d268cbSEd Tanous     if (chassisId != "system")
276722d268cbSEd Tanous     {
276822d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
276922d268cbSEd Tanous         return;
277022d268cbSEd Tanous     }
277122d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
277222d268cbSEd Tanous }
277322d268cbSEd Tanous 
2774fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2775fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2776fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2777fdd26906SClaire Weinan {
2778fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2779fdd26906SClaire Weinan     {
2780fdd26906SClaire Weinan         return;
2781fdd26906SClaire Weinan     }
2782fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2783fdd26906SClaire Weinan }
2784fdd26906SClaire Weinan 
278522d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
278622d268cbSEd Tanous     crow::App& app, const crow::Request& req,
278722d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
278822d268cbSEd Tanous     const std::string& chassisId)
278922d268cbSEd Tanous {
279022d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
279122d268cbSEd Tanous     {
279222d268cbSEd Tanous         return;
279322d268cbSEd Tanous     }
279422d268cbSEd Tanous     if (chassisId != "system")
279522d268cbSEd Tanous     {
279622d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
279722d268cbSEd Tanous         return;
279822d268cbSEd Tanous     }
279922d268cbSEd Tanous     createDump(asyncResp, req, "System");
280022d268cbSEd Tanous }
280122d268cbSEd Tanous 
2802fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2803fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2804fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2805fdd26906SClaire Weinan {
2806fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2807fdd26906SClaire Weinan     {
2808fdd26906SClaire Weinan         return;
2809fdd26906SClaire Weinan     }
2810fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2811fdd26906SClaire Weinan }
2812fdd26906SClaire Weinan 
281322d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
281422d268cbSEd Tanous     crow::App& app, const crow::Request& req,
281522d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
281622d268cbSEd Tanous     const std::string& chassisId)
281722d268cbSEd Tanous {
281822d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
281922d268cbSEd Tanous     {
282022d268cbSEd Tanous         return;
282122d268cbSEd Tanous     }
282222d268cbSEd Tanous     if (chassisId != "system")
282322d268cbSEd Tanous     {
282422d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
282522d268cbSEd Tanous         return;
282622d268cbSEd Tanous     }
282722d268cbSEd Tanous     clearDump(asyncResp, "System");
282822d268cbSEd Tanous }
282922d268cbSEd Tanous 
2830fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2831fdd26906SClaire Weinan {
2832fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2833fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2834fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2835fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2836fdd26906SClaire Weinan }
2837fdd26906SClaire Weinan 
2838fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2839fdd26906SClaire Weinan {
2840fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2841fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2842fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2843fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2844c9bb6861Sraviteja-b }
2845c9bb6861Sraviteja-b 
28467e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2847c9bb6861Sraviteja-b {
28487e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28497e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2850ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2851fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2852fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2853fdd26906SClaire Weinan 
28547e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28557e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2856ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2857fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2858fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2859c9bb6861Sraviteja-b }
2860c9bb6861Sraviteja-b 
28617e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2862c9bb6861Sraviteja-b {
28630fda0f12SGeorge Liu     BMCWEB_ROUTE(
28640fda0f12SGeorge Liu         app,
28650fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2866ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
28677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2868fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2869fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2870a43be80fSAsmitha Karunanithi }
2871a43be80fSAsmitha Karunanithi 
28727e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
287380319af1SAsmitha Karunanithi {
28740fda0f12SGeorge Liu     BMCWEB_ROUTE(
28750fda0f12SGeorge Liu         app,
28760fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2877ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2878fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2879fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
288045ca1b86SEd Tanous }
2881fdd26906SClaire Weinan 
2882fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2883fdd26906SClaire Weinan {
2884fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2885fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2886fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2887fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2888fdd26906SClaire Weinan }
2889fdd26906SClaire Weinan 
2890fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2891fdd26906SClaire Weinan {
2892fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2893fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2894fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2895fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2896fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2897fdd26906SClaire Weinan }
2898fdd26906SClaire Weinan 
2899fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2900fdd26906SClaire Weinan {
2901fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2902fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2903fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2904fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2905fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2906fdd26906SClaire Weinan 
2907fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2908fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2909fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2910fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2911fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2912fdd26906SClaire Weinan }
2913fdd26906SClaire Weinan 
2914fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2915fdd26906SClaire Weinan {
2916fdd26906SClaire Weinan     BMCWEB_ROUTE(
2917fdd26906SClaire Weinan         app,
2918fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2919fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2920fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2921fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
29225cb1dd27SAsmitha Karunanithi }
29235cb1dd27SAsmitha Karunanithi 
29247e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
29255cb1dd27SAsmitha Karunanithi {
292622d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2927ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
29286ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
292922d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
29305cb1dd27SAsmitha Karunanithi }
29315cb1dd27SAsmitha Karunanithi 
29327e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
29337e860f15SJohn Edward Broadbent {
293422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2935ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
293622d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
293722d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
293822d268cbSEd Tanous             std::ref(app)));
29395cb1dd27SAsmitha Karunanithi }
29405cb1dd27SAsmitha Karunanithi 
29417e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
29425cb1dd27SAsmitha Karunanithi {
29437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
294422d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2945ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29466ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
294722d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
29488d1b46d7Szhanghch05 
29497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
295022d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2951ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
29526ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
295322d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
29545cb1dd27SAsmitha Karunanithi }
2955c9bb6861Sraviteja-b 
29567e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2957c9bb6861Sraviteja-b {
29580fda0f12SGeorge Liu     BMCWEB_ROUTE(
29590fda0f12SGeorge Liu         app,
296022d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2961ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
296222d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
296322d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
296422d268cbSEd Tanous             std::ref(app)));
2965a43be80fSAsmitha Karunanithi }
2966a43be80fSAsmitha Karunanithi 
29677e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2968a43be80fSAsmitha Karunanithi {
29690fda0f12SGeorge Liu     BMCWEB_ROUTE(
29700fda0f12SGeorge Liu         app,
297122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2972ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29736ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
297422d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2975013487e5Sraviteja-b }
2976013487e5Sraviteja-b 
29777e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
29781da66f75SEd Tanous {
29793946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29803946028dSAppaRao Puli     // method for security reasons.
29811da66f75SEd Tanous     /**
29821da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
29831da66f75SEd Tanous      */
298422d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2985ed398213SEd Tanous         // This is incorrect, should be:
2986ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2987432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2988002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2989002d39b4SEd Tanous             [&app](const crow::Request& req,
299022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
299122d268cbSEd Tanous                    const std::string& systemName) {
29923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
299345ca1b86SEd Tanous         {
299445ca1b86SEd Tanous             return;
299545ca1b86SEd Tanous         }
299622d268cbSEd Tanous         if (systemName != "system")
299722d268cbSEd Tanous         {
299822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
299922d268cbSEd Tanous                                        systemName);
300022d268cbSEd Tanous             return;
300122d268cbSEd Tanous         }
300222d268cbSEd Tanous 
30037e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
30047e860f15SJohn Edward Broadbent         // SubRoute
30050f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
3006424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
3007e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
30088e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
30094f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
30104f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
30114f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3012e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3013e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
30147c8c4058STejas Patil 
30157c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
30162b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
30177c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30187c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30197c8c4058STejas Patil             redfishDateTimeOffset.second;
30207c8c4058STejas Patil 
30211476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
30221476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3023002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
30241476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3025002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
30261476687dSEd Tanous                                 ["target"] =
30271476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
30287e860f15SJohn Edward Broadbent         });
30291da66f75SEd Tanous }
30301da66f75SEd Tanous 
30317e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
30325b61b5e8SJason M. Bills {
30330fda0f12SGeorge Liu     BMCWEB_ROUTE(
30340fda0f12SGeorge Liu         app,
303522d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
3036ed398213SEd Tanous         // This is incorrect, should be:
3037ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3038432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30397e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
304045ca1b86SEd Tanous             [&app](const crow::Request& req,
304122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
304222d268cbSEd Tanous                    const std::string& systemName) {
30433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
304445ca1b86SEd Tanous         {
304545ca1b86SEd Tanous             return;
304645ca1b86SEd Tanous         }
304722d268cbSEd Tanous         if (systemName != "system")
304822d268cbSEd Tanous         {
304922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
305022d268cbSEd Tanous                                        systemName);
305122d268cbSEd Tanous             return;
305222d268cbSEd Tanous         }
30535b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
30545b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
3055cb13a392SEd Tanous                         const std::string&) {
30565b61b5e8SJason M. Bills             if (ec)
30575b61b5e8SJason M. Bills             {
30585b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
30595b61b5e8SJason M. Bills                 return;
30605b61b5e8SJason M. Bills             }
30615b61b5e8SJason M. Bills             messages::success(asyncResp->res);
30625b61b5e8SJason M. Bills             },
3063002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
30647e860f15SJohn Edward Broadbent         });
30655b61b5e8SJason M. Bills }
30665b61b5e8SJason M. Bills 
30678d1b46d7Szhanghch05 static void
30688d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30698d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3070e855dd28SJason M. Bills {
3071043a0536SJohnathan Mantey     auto getStoredLogCallback =
3072b9d36b47SEd Tanous         [asyncResp, logID,
3073b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
3074b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3075e855dd28SJason M. Bills         if (ec)
3076e855dd28SJason M. Bills         {
3077e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
30781ddcf01aSJason M. Bills             if (ec.value() ==
30791ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
30801ddcf01aSJason M. Bills             {
3081002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
30821ddcf01aSJason M. Bills             }
30831ddcf01aSJason M. Bills             else
30841ddcf01aSJason M. Bills             {
3085e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
30861ddcf01aSJason M. Bills             }
3087e855dd28SJason M. Bills             return;
3088e855dd28SJason M. Bills         }
3089043a0536SJohnathan Mantey 
3090043a0536SJohnathan Mantey         std::string timestamp{};
3091043a0536SJohnathan Mantey         std::string filename{};
3092043a0536SJohnathan Mantey         std::string logfile{};
30932c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3094043a0536SJohnathan Mantey 
3095043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3096e855dd28SJason M. Bills         {
30979db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3098e855dd28SJason M. Bills             return;
3099e855dd28SJason M. Bills         }
3100e855dd28SJason M. Bills 
3101043a0536SJohnathan Mantey         std::string crashdumpURI =
3102e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3103043a0536SJohnathan Mantey             logID + "/" + filename;
310484afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
31059c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
310684afc48bSJason M. Bills         logEntry["@odata.id"] =
310784afc48bSJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
310884afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
310984afc48bSJason M. Bills         logEntry["Id"] = logID;
311084afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
311184afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
311284afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
311384afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
311484afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
31152b20ef6eSJason M. Bills 
31162b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
31172b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
31182b20ef6eSJason M. Bills         // directly
31192b20ef6eSJason M. Bills         if (logEntryJson.is_array())
31202b20ef6eSJason M. Bills         {
31212b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
31222b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
31232b20ef6eSJason M. Bills                 logEntryJson.size();
31242b20ef6eSJason M. Bills         }
31252b20ef6eSJason M. Bills         else
31262b20ef6eSJason M. Bills         {
3127d405bb51SJason M. Bills             logEntryJson.update(logEntry);
31282b20ef6eSJason M. Bills         }
3129e855dd28SJason M. Bills     };
3130d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3131d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3132d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3133d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3134e855dd28SJason M. Bills }
3135e855dd28SJason M. Bills 
31367e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
31371da66f75SEd Tanous {
31383946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31393946028dSAppaRao Puli     // method for security reasons.
31401da66f75SEd Tanous     /**
31411da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
31421da66f75SEd Tanous      */
31437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
314422d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3145ed398213SEd Tanous         // This is incorrect, should be.
3146ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3147432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3148002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3149002d39b4SEd Tanous             [&app](const crow::Request& req,
315022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
315122d268cbSEd Tanous                    const std::string& systemName) {
31523ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
315345ca1b86SEd Tanous         {
315445ca1b86SEd Tanous             return;
315545ca1b86SEd Tanous         }
315622d268cbSEd Tanous         if (systemName != "system")
315722d268cbSEd Tanous         {
315822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
315922d268cbSEd Tanous                                        systemName);
316022d268cbSEd Tanous             return;
316122d268cbSEd Tanous         }
316222d268cbSEd Tanous 
31637a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
31647a1dbc48SGeorge Liu             crashdumpInterface};
31657a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
31667a1dbc48SGeorge Liu             "/", 0, interfaces,
31677a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
31682b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
31691da66f75SEd Tanous             if (ec)
31701da66f75SEd Tanous             {
31711da66f75SEd Tanous                 if (ec.value() !=
31721da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
31731da66f75SEd Tanous                 {
31741da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
31751da66f75SEd Tanous                                      << ec.message();
3176f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
31771da66f75SEd Tanous                     return;
31781da66f75SEd Tanous                 }
31791da66f75SEd Tanous             }
3180e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
31811da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
31820f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3183424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3184002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3185e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3186424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3187002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3188a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
31892b20ef6eSJason M. Bills 
31902b20ef6eSJason M. Bills             for (const std::string& path : resp)
31911da66f75SEd Tanous             {
31922b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3193e855dd28SJason M. Bills                 // Get the log ID
31942b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
31952b20ef6eSJason M. Bills                 if (logID.empty())
31961da66f75SEd Tanous                 {
3197e855dd28SJason M. Bills                     continue;
31981da66f75SEd Tanous                 }
3199e855dd28SJason M. Bills                 // Add the log entry to the array
32002b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
32012b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
32021da66f75SEd Tanous             }
32037a1dbc48SGeorge Liu             });
32047e860f15SJohn Edward Broadbent         });
32051da66f75SEd Tanous }
32061da66f75SEd Tanous 
32077e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
32081da66f75SEd Tanous {
32093946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32103946028dSAppaRao Puli     // method for security reasons.
32111da66f75SEd Tanous 
32127e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
321322d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3214ed398213SEd Tanous         // this is incorrect, should be
3215ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3216432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32177e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
321845ca1b86SEd Tanous             [&app](const crow::Request& req,
32197e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
322022d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
32213ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
322245ca1b86SEd Tanous         {
322345ca1b86SEd Tanous             return;
322445ca1b86SEd Tanous         }
322522d268cbSEd Tanous         if (systemName != "system")
322622d268cbSEd Tanous         {
322722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
322822d268cbSEd Tanous                                        systemName);
322922d268cbSEd Tanous             return;
323022d268cbSEd Tanous         }
32317e860f15SJohn Edward Broadbent         const std::string& logID = param;
3232e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
32337e860f15SJohn Edward Broadbent         });
3234e855dd28SJason M. Bills }
3235e855dd28SJason M. Bills 
32367e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3237e855dd28SJason M. Bills {
32383946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32393946028dSAppaRao Puli     // method for security reasons.
32407e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
32417e860f15SJohn Edward Broadbent         app,
324222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3243ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
32447e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3245a4ce114aSNan Zhou             [](const crow::Request& req,
32467e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
324722d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
324822d268cbSEd Tanous                const std::string& fileName) {
32492a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
32502a9beeedSShounak Mitra         // Redfish resource.
325122d268cbSEd Tanous 
325222d268cbSEd Tanous         if (systemName != "system")
325322d268cbSEd Tanous         {
325422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
325522d268cbSEd Tanous                                        systemName);
325622d268cbSEd Tanous             return;
325722d268cbSEd Tanous         }
325822d268cbSEd Tanous 
3259043a0536SJohnathan Mantey         auto getStoredLogCallback =
3260002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3261abf2add6SEd Tanous                 const boost::system::error_code ec,
3262002d39b4SEd Tanous                 const std::vector<
3263002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
32647e860f15SJohn Edward Broadbent                     resp) {
32651da66f75SEd Tanous             if (ec)
32661da66f75SEd Tanous             {
3267002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3268f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
32691da66f75SEd Tanous                 return;
32701da66f75SEd Tanous             }
3271e855dd28SJason M. Bills 
3272043a0536SJohnathan Mantey             std::string dbusFilename{};
3273043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3274043a0536SJohnathan Mantey             std::string dbusFilepath{};
3275043a0536SJohnathan Mantey 
3276002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3277002d39b4SEd Tanous                                      dbusFilepath);
3278043a0536SJohnathan Mantey 
3279043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3280043a0536SJohnathan Mantey                 dbusFilepath.empty())
32811da66f75SEd Tanous             {
32829db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
32831da66f75SEd Tanous                 return;
32841da66f75SEd Tanous             }
3285e855dd28SJason M. Bills 
3286043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3287043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3288043a0536SJohnathan Mantey             {
32899db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3290043a0536SJohnathan Mantey                 return;
3291043a0536SJohnathan Mantey             }
3292043a0536SJohnathan Mantey 
3293043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3294043a0536SJohnathan Mantey             {
32959db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3296043a0536SJohnathan Mantey                 return;
3297043a0536SJohnathan Mantey             }
3298002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3299002d39b4SEd Tanous             asyncResp->res.body() =
3300002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3301043a0536SJohnathan Mantey 
33027e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
33037e860f15SJohn Edward Broadbent             // from a browser
3304d9f6c621SEd Tanous             asyncResp->res.addHeader(
3305d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
33061da66f75SEd Tanous         };
3307d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3308d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3309d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3310d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
33117e860f15SJohn Edward Broadbent         });
33121da66f75SEd Tanous }
33131da66f75SEd Tanous 
3314c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3315c5a4c82aSJason M. Bills {
3316c5a4c82aSJason M. Bills     onDemand,
3317c5a4c82aSJason M. Bills     telemetry,
3318c5a4c82aSJason M. Bills     invalid,
3319c5a4c82aSJason M. Bills };
3320c5a4c82aSJason M. Bills 
3321f7725d79SEd Tanous inline OEMDiagnosticType
3322f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
3323c5a4c82aSJason M. Bills {
3324c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3325c5a4c82aSJason M. Bills     {
3326c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3327c5a4c82aSJason M. Bills     }
3328c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3329c5a4c82aSJason M. Bills     {
3330c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3331c5a4c82aSJason M. Bills     }
3332c5a4c82aSJason M. Bills 
3333c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3334c5a4c82aSJason M. Bills }
3335c5a4c82aSJason M. Bills 
33367e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
33371da66f75SEd Tanous {
33383946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
33393946028dSAppaRao Puli     // method for security reasons.
33400fda0f12SGeorge Liu     BMCWEB_ROUTE(
33410fda0f12SGeorge Liu         app,
334222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3343ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3344ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3345432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3346002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3347002d39b4SEd Tanous             [&app](const crow::Request& req,
334822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
334922d268cbSEd Tanous                    const std::string& systemName) {
33503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
335145ca1b86SEd Tanous         {
335245ca1b86SEd Tanous             return;
335345ca1b86SEd Tanous         }
335422d268cbSEd Tanous 
335522d268cbSEd Tanous         if (systemName != "system")
335622d268cbSEd Tanous         {
335722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
335822d268cbSEd Tanous                                        systemName);
335922d268cbSEd Tanous             return;
336022d268cbSEd Tanous         }
336122d268cbSEd Tanous 
33628e6c099aSJason M. Bills         std::string diagnosticDataType;
33638e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
336415ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3365002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3366002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
33678e6c099aSJason M. Bills         {
33688e6c099aSJason M. Bills             return;
33698e6c099aSJason M. Bills         }
33708e6c099aSJason M. Bills 
33718e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
33728e6c099aSJason M. Bills         {
33738e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
33748e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
33758e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
33768e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
33778e6c099aSJason M. Bills                 "CollectDiagnosticData");
33788e6c099aSJason M. Bills             return;
33798e6c099aSJason M. Bills         }
33808e6c099aSJason M. Bills 
3381c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3382c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3383c5a4c82aSJason M. Bills 
3384c5a4c82aSJason M. Bills         std::string iface;
3385c5a4c82aSJason M. Bills         std::string method;
3386c5a4c82aSJason M. Bills         std::string taskMatchStr;
3387c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3388c5a4c82aSJason M. Bills         {
3389c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3390c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3391c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3392c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3393c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3394c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3395c5a4c82aSJason M. Bills         }
3396c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3397c5a4c82aSJason M. Bills         {
3398c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3399c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3400c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3401c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3402c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3403c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3404c5a4c82aSJason M. Bills         }
3405c5a4c82aSJason M. Bills         else
3406c5a4c82aSJason M. Bills         {
3407c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3408c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3409c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3410002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3411002d39b4SEd Tanous                 "CollectDiagnosticData");
3412c5a4c82aSJason M. Bills             return;
3413c5a4c82aSJason M. Bills         }
3414c5a4c82aSJason M. Bills 
3415c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3416c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3417c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
341898be3e39SEd Tanous                            const std::string&) mutable {
34191da66f75SEd Tanous             if (ec)
34201da66f75SEd Tanous             {
3421002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
34221da66f75SEd Tanous                 {
3423f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
34241da66f75SEd Tanous                 }
34254363d3b2SJason M. Bills                 else if (ec.value() ==
34264363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
34274363d3b2SJason M. Bills                 {
3428002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3429002d39b4SEd Tanous                                                             "60");
34304363d3b2SJason M. Bills                 }
34311da66f75SEd Tanous                 else
34321da66f75SEd Tanous                 {
3433f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
34341da66f75SEd Tanous                 }
34351da66f75SEd Tanous                 return;
34361da66f75SEd Tanous             }
3437002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
343859d494eeSPatrick Williams                 [](boost::system::error_code err, sdbusplus::message_t&,
3439002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
344066afe4faSJames Feist                 if (!err)
344166afe4faSJames Feist                 {
3442002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3443e5d5006bSJames Feist                         std::to_string(taskData->index)));
3444831d6b09SJames Feist                     taskData->state = "Completed";
344566afe4faSJames Feist                 }
344632898ceaSJames Feist                 return task::completed;
344766afe4faSJames Feist                 },
3448c5a4c82aSJason M. Bills                 taskMatchStr);
3449c5a4c82aSJason M. Bills 
345046229577SJames Feist             task->startTimer(std::chrono::minutes(5));
345146229577SJames Feist             task->populateResp(asyncResp->res);
345298be3e39SEd Tanous             task->payload.emplace(std::move(payload));
34531da66f75SEd Tanous         };
34548e6c099aSJason M. Bills 
34551da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3456002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3457002d39b4SEd Tanous             iface, method);
34587e860f15SJohn Edward Broadbent         });
34596eda7685SKenny L. Ku }
34606eda7685SKenny L. Ku 
3461cb92c03bSAndrew Geissler /**
3462cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3463cb92c03bSAndrew Geissler  */
34647e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3465cb92c03bSAndrew Geissler {
3466cb92c03bSAndrew Geissler     /**
3467cb92c03bSAndrew Geissler      * Function handles POST method request.
3468cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3469cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3470cb92c03bSAndrew Geissler      */
34717e860f15SJohn Edward Broadbent 
34720fda0f12SGeorge Liu     BMCWEB_ROUTE(
34730fda0f12SGeorge Liu         app,
347422d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3475ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
34767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
347745ca1b86SEd Tanous             [&app](const crow::Request& req,
347822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
347922d268cbSEd Tanous                    const std::string& systemName) {
34803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
348145ca1b86SEd Tanous         {
348245ca1b86SEd Tanous             return;
348345ca1b86SEd Tanous         }
348422d268cbSEd Tanous         if (systemName != "system")
348522d268cbSEd Tanous         {
348622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
348722d268cbSEd Tanous                                        systemName);
348822d268cbSEd Tanous             return;
348922d268cbSEd Tanous         }
3490cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3491cb92c03bSAndrew Geissler 
3492cb92c03bSAndrew Geissler         // Process response from Logging service.
3493002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3494002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3495cb92c03bSAndrew Geissler             if (ec)
3496cb92c03bSAndrew Geissler             {
3497cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3498002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3499cb92c03bSAndrew Geissler                 asyncResp->res.result(
3500cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3501cb92c03bSAndrew Geissler                 return;
3502cb92c03bSAndrew Geissler             }
3503cb92c03bSAndrew Geissler 
3504002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3505cb92c03bSAndrew Geissler         };
3506cb92c03bSAndrew Geissler 
3507cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3508cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
35092c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3510cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3511cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
35127e860f15SJohn Edward Broadbent         });
3513cb92c03bSAndrew Geissler }
3514a3316fc6SZhikuiRen 
3515a3316fc6SZhikuiRen /****************************************************
3516a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3517a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3518a3316fc6SZhikuiRen  ******************************************************/
35197e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3520a3316fc6SZhikuiRen {
352122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3522ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3523002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3524002d39b4SEd Tanous             [&app](const crow::Request& req,
352522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
352622d268cbSEd Tanous                    const std::string& systemName) {
35273ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
352845ca1b86SEd Tanous         {
352945ca1b86SEd Tanous             return;
353045ca1b86SEd Tanous         }
353122d268cbSEd Tanous         if (systemName != "system")
353222d268cbSEd Tanous         {
353322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
353422d268cbSEd Tanous                                        systemName);
353522d268cbSEd Tanous             return;
353622d268cbSEd Tanous         }
35371476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
35381476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
35391476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
35401476687dSEd Tanous             "#LogService.v1_1_0.LogService";
35411476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
35421476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3543ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "PostCodes";
35441476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
35451476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
35461476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
35477c8c4058STejas Patil 
35487c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
35492b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
35500fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
35517c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
35527c8c4058STejas Patil             redfishDateTimeOffset.second;
35537c8c4058STejas Patil 
3554a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
35557e860f15SJohn Edward Broadbent             {"target",
35560fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
35577e860f15SJohn Edward Broadbent         });
3558a3316fc6SZhikuiRen }
3559a3316fc6SZhikuiRen 
35607e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3561a3316fc6SZhikuiRen {
35620fda0f12SGeorge Liu     BMCWEB_ROUTE(
35630fda0f12SGeorge Liu         app,
356422d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3565ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3566ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3567432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
35687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
356945ca1b86SEd Tanous             [&app](const crow::Request& req,
357022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357122d268cbSEd Tanous                    const std::string& systemName) {
35723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
357345ca1b86SEd Tanous         {
357445ca1b86SEd Tanous             return;
357545ca1b86SEd Tanous         }
357622d268cbSEd Tanous         if (systemName != "system")
357722d268cbSEd Tanous         {
357822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
357922d268cbSEd Tanous                                        systemName);
358022d268cbSEd Tanous             return;
358122d268cbSEd Tanous         }
3582a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3583a3316fc6SZhikuiRen 
3584a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3585a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3586a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3587a3316fc6SZhikuiRen             if (ec)
3588a3316fc6SZhikuiRen             {
3589a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3590002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
35917e860f15SJohn Edward Broadbent                                  << ec;
3592002d39b4SEd Tanous                 asyncResp->res.result(
3593002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3594a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3595a3316fc6SZhikuiRen                 return;
3596a3316fc6SZhikuiRen             }
3597a3316fc6SZhikuiRen             },
359815124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
359915124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3600a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
36017e860f15SJohn Edward Broadbent         });
3602a3316fc6SZhikuiRen }
3603a3316fc6SZhikuiRen 
36046f284d24SJiaqing Zhao /**
36056f284d24SJiaqing Zhao  * @brief Parse post code ID and get the current value and index value
36066f284d24SJiaqing Zhao  *        eg: postCodeID=B1-2, currentValue=1, index=2
36076f284d24SJiaqing Zhao  *
36086f284d24SJiaqing Zhao  * @param[in]  postCodeID     Post Code ID
36096f284d24SJiaqing Zhao  * @param[out] currentValue   Current value
36106f284d24SJiaqing Zhao  * @param[out] index          Index value
36116f284d24SJiaqing Zhao  *
36126f284d24SJiaqing Zhao  * @return bool true if the parsing is successful, false the parsing fails
36136f284d24SJiaqing Zhao  */
36146f284d24SJiaqing Zhao inline static bool parsePostCode(const std::string& postCodeID,
36156f284d24SJiaqing Zhao                                  uint64_t& currentValue, uint16_t& index)
36166f284d24SJiaqing Zhao {
36176f284d24SJiaqing Zhao     std::vector<std::string> split;
3618*50ebd4afSEd Tanous     bmcweb::split(split, postCodeID, '-');
36196f284d24SJiaqing Zhao     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
36206f284d24SJiaqing Zhao     {
36216f284d24SJiaqing Zhao         return false;
36226f284d24SJiaqing Zhao     }
36236f284d24SJiaqing Zhao 
36246f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36256f284d24SJiaqing Zhao     const char* start = split[0].data() + 1;
36266f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36276f284d24SJiaqing Zhao     const char* end = split[0].data() + split[0].size();
36286f284d24SJiaqing Zhao     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
36296f284d24SJiaqing Zhao 
36306f284d24SJiaqing Zhao     if (ptrIndex != end || ecIndex != std::errc())
36316f284d24SJiaqing Zhao     {
36326f284d24SJiaqing Zhao         return false;
36336f284d24SJiaqing Zhao     }
36346f284d24SJiaqing Zhao 
36356f284d24SJiaqing Zhao     start = split[1].data();
36366f284d24SJiaqing Zhao 
36376f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36386f284d24SJiaqing Zhao     end = split[1].data() + split[1].size();
36396f284d24SJiaqing Zhao     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
36406f284d24SJiaqing Zhao 
36416f284d24SJiaqing Zhao     return ptrValue == end && ecValue == std::errc();
36426f284d24SJiaqing Zhao }
36436f284d24SJiaqing Zhao 
36446f284d24SJiaqing Zhao static bool fillPostCodeEntry(
36458d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
36466c9a279eSManojkiran Eda     const boost::container::flat_map<
36476c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3648a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3649a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3650a3316fc6SZhikuiRen {
3651a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3652fffb8c1fSEd Tanous     const registries::Message* message =
3653fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3654a3316fc6SZhikuiRen 
3655a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3656a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
36576c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
36586c9a279eSManojkiran Eda              code : postcode)
3659a3316fc6SZhikuiRen     {
3660a3316fc6SZhikuiRen         currentCodeIndex++;
3661a3316fc6SZhikuiRen         std::string postcodeEntryID =
3662a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3663a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3664a3316fc6SZhikuiRen 
3665a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3666a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3667a3316fc6SZhikuiRen 
3668a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3669a3316fc6SZhikuiRen         { // already incremented
3670a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3671a3316fc6SZhikuiRen         }
3672a3316fc6SZhikuiRen         else
3673a3316fc6SZhikuiRen         {
3674a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3675a3316fc6SZhikuiRen         }
3676a3316fc6SZhikuiRen 
3677a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3678a3316fc6SZhikuiRen         // not fall between top and skip
3679a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3680a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3681a3316fc6SZhikuiRen         {
3682a3316fc6SZhikuiRen             continue;
3683a3316fc6SZhikuiRen         }
3684a3316fc6SZhikuiRen 
36854e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3686a3316fc6SZhikuiRen         // currentIndex
3687a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3688a3316fc6SZhikuiRen         {
3689a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3690a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3691a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3692a3316fc6SZhikuiRen             continue;
3693a3316fc6SZhikuiRen         }
3694a3316fc6SZhikuiRen 
3695a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3696a3316fc6SZhikuiRen         // index
3697a3316fc6SZhikuiRen 
3698a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3699a3316fc6SZhikuiRen         std::string entryTimeStr;
37001d8782e7SNan Zhou         entryTimeStr =
37012b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3702a3316fc6SZhikuiRen 
3703a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3704a3316fc6SZhikuiRen         std::ostringstream hexCode;
3705a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
37066c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3707a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3708a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3709a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3710a3316fc6SZhikuiRen         // Set precision to 4 digits
3711a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3712a3316fc6SZhikuiRen         // Add double to stream
3713a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3714a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3715a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3716a3316fc6SZhikuiRen 
3717a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3718a3316fc6SZhikuiRen         std::string msg;
3719a3316fc6SZhikuiRen         if (message != nullptr)
3720a3316fc6SZhikuiRen         {
3721a3316fc6SZhikuiRen             msg = message->message;
3722a3316fc6SZhikuiRen 
3723a3316fc6SZhikuiRen             // fill in this post code value
3724a3316fc6SZhikuiRen             int i = 0;
3725a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3726a3316fc6SZhikuiRen             {
3727a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3728a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3729a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3730a3316fc6SZhikuiRen                 {
3731a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3732a3316fc6SZhikuiRen                 }
3733a3316fc6SZhikuiRen             }
3734a3316fc6SZhikuiRen         }
3735a3316fc6SZhikuiRen 
3736d4342a92STim Lee         // Get Severity template from message registry
3737d4342a92STim Lee         std::string severity;
3738d4342a92STim Lee         if (message != nullptr)
3739d4342a92STim Lee         {
37405f2b84eeSEd Tanous             severity = message->messageSeverity;
3741d4342a92STim Lee         }
3742d4342a92STim Lee 
37436f284d24SJiaqing Zhao         // Format entry
37446f284d24SJiaqing Zhao         nlohmann::json::object_t bmcLogEntry;
37459c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
374684afc48bSJason M. Bills         bmcLogEntry["@odata.id"] =
37470fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
374884afc48bSJason M. Bills             postcodeEntryID;
374984afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
375084afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
375184afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
375284afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
375384afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
375484afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
375584afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
375684afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3757647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3758647b3cdcSGeorge Liu         {
3759647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3760647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3761647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3762647b3cdcSGeorge Liu         }
37636f284d24SJiaqing Zhao 
37646f284d24SJiaqing Zhao         // codeIndex is only specified when querying single entry, return only
37656f284d24SJiaqing Zhao         // that entry in this case
37666f284d24SJiaqing Zhao         if (codeIndex != 0)
37676f284d24SJiaqing Zhao         {
37686f284d24SJiaqing Zhao             aResp->res.jsonValue.update(bmcLogEntry);
37696f284d24SJiaqing Zhao             return true;
3770a3316fc6SZhikuiRen         }
37716f284d24SJiaqing Zhao 
37726f284d24SJiaqing Zhao         nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
37736f284d24SJiaqing Zhao         logEntryArray.push_back(std::move(bmcLogEntry));
37746f284d24SJiaqing Zhao     }
37756f284d24SJiaqing Zhao 
37766f284d24SJiaqing Zhao     // Return value is always false when querying multiple entries
37776f284d24SJiaqing Zhao     return false;
3778a3316fc6SZhikuiRen }
3779a3316fc6SZhikuiRen 
37808d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
37816f284d24SJiaqing Zhao                                 const std::string& entryId)
3782a3316fc6SZhikuiRen {
37836f284d24SJiaqing Zhao     uint16_t bootIndex = 0;
37846f284d24SJiaqing Zhao     uint64_t codeIndex = 0;
37856f284d24SJiaqing Zhao     if (!parsePostCode(entryId, codeIndex, bootIndex))
37866f284d24SJiaqing Zhao     {
37876f284d24SJiaqing Zhao         // Requested ID was not found
37886f284d24SJiaqing Zhao         messages::resourceNotFound(aResp->res, "LogEntry", entryId);
37896f284d24SJiaqing Zhao         return;
37906f284d24SJiaqing Zhao     }
37916f284d24SJiaqing Zhao 
37926f284d24SJiaqing Zhao     if (bootIndex == 0 || codeIndex == 0)
37936f284d24SJiaqing Zhao     {
37946f284d24SJiaqing Zhao         // 0 is an invalid index
37956f284d24SJiaqing Zhao         messages::resourceNotFound(aResp->res, "LogEntry", entryId);
37966f284d24SJiaqing Zhao         return;
37976f284d24SJiaqing Zhao     }
37986f284d24SJiaqing Zhao 
3799a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
38006f284d24SJiaqing Zhao         [aResp, entryId, bootIndex,
38016c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
38026c9a279eSManojkiran Eda                     const boost::container::flat_map<
38036c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38046c9a279eSManojkiran Eda                         postcode) {
3805a3316fc6SZhikuiRen         if (ec)
3806a3316fc6SZhikuiRen         {
3807a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3808a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3809a3316fc6SZhikuiRen             return;
3810a3316fc6SZhikuiRen         }
3811a3316fc6SZhikuiRen 
3812a3316fc6SZhikuiRen         if (postcode.empty())
3813a3316fc6SZhikuiRen         {
38146f284d24SJiaqing Zhao             messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3815a3316fc6SZhikuiRen             return;
3816a3316fc6SZhikuiRen         }
3817a3316fc6SZhikuiRen 
38186f284d24SJiaqing Zhao         if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
38196f284d24SJiaqing Zhao         {
38206f284d24SJiaqing Zhao             messages::resourceNotFound(aResp->res, "LogEntry", entryId);
38216f284d24SJiaqing Zhao             return;
38226f284d24SJiaqing Zhao         }
3823a3316fc6SZhikuiRen         },
382415124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
382515124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3826a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3827a3316fc6SZhikuiRen         bootIndex);
3828a3316fc6SZhikuiRen }
3829a3316fc6SZhikuiRen 
38308d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3831a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3832a3316fc6SZhikuiRen                                const uint16_t bootCount,
38333648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
38343648c8beSEd Tanous                                size_t top)
3835a3316fc6SZhikuiRen {
3836a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3837a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3838a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
38396c9a279eSManojkiran Eda               const boost::container::flat_map<
38406c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38416c9a279eSManojkiran Eda                   postcode) {
3842a3316fc6SZhikuiRen         if (ec)
3843a3316fc6SZhikuiRen         {
3844a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3845a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3846a3316fc6SZhikuiRen             return;
3847a3316fc6SZhikuiRen         }
3848a3316fc6SZhikuiRen 
3849a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3850a3316fc6SZhikuiRen         if (!postcode.empty())
3851a3316fc6SZhikuiRen         {
3852a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
38533648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3854a3316fc6SZhikuiRen             {
38553648c8beSEd Tanous                 uint64_t thisBootSkip =
38563648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
38573648c8beSEd Tanous                     entryCount;
3858a3316fc6SZhikuiRen                 uint64_t thisBootTop =
38593648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
38603648c8beSEd Tanous                     entryCount;
3861a3316fc6SZhikuiRen 
3862002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3863002d39b4SEd Tanous                                   thisBootTop);
3864a3316fc6SZhikuiRen             }
3865a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3866a3316fc6SZhikuiRen         }
3867a3316fc6SZhikuiRen 
3868a3316fc6SZhikuiRen         // continue to previous bootIndex
3869a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3870a3316fc6SZhikuiRen         {
3871a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3872a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3873a3316fc6SZhikuiRen         }
387481584abeSJiaqing Zhao         else if (skip + top < endCount)
3875a3316fc6SZhikuiRen         {
3876a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
38770fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3878a3316fc6SZhikuiRen                 std::to_string(skip + top);
3879a3316fc6SZhikuiRen         }
3880a3316fc6SZhikuiRen         },
388115124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
388215124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3883a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3884a3316fc6SZhikuiRen         bootIndex);
3885a3316fc6SZhikuiRen }
3886a3316fc6SZhikuiRen 
38878d1b46d7Szhanghch05 static void
38888d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
38893648c8beSEd Tanous                          size_t skip, size_t top)
3890a3316fc6SZhikuiRen {
3891a3316fc6SZhikuiRen     uint64_t entryCount = 0;
38921e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
38931e1e598dSJonathan Doman         *crow::connections::systemBus,
38941e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
38951e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
38961e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
38971e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
38981e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3899a3316fc6SZhikuiRen         if (ec)
3900a3316fc6SZhikuiRen         {
3901a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3902a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3903a3316fc6SZhikuiRen             return;
3904a3316fc6SZhikuiRen         }
39051e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
39061e1e598dSJonathan Doman         });
3907a3316fc6SZhikuiRen }
3908a3316fc6SZhikuiRen 
39097e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3910a3316fc6SZhikuiRen {
39117e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
391222d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3913ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
39147e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
391545ca1b86SEd Tanous             [&app](const crow::Request& req,
391622d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
391722d268cbSEd Tanous                    const std::string& systemName) {
3918c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3919c937d2bfSEd Tanous             .canDelegateTop = true,
3920c937d2bfSEd Tanous             .canDelegateSkip = true,
3921c937d2bfSEd Tanous         };
3922c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3923c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
39243ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
392545ca1b86SEd Tanous         {
392645ca1b86SEd Tanous             return;
392745ca1b86SEd Tanous         }
392822d268cbSEd Tanous 
392922d268cbSEd Tanous         if (systemName != "system")
393022d268cbSEd Tanous         {
393122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
393222d268cbSEd Tanous                                        systemName);
393322d268cbSEd Tanous             return;
393422d268cbSEd Tanous         }
3935a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3936a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3937a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3938a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3939a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3940a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3941a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3942a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3943a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
39443648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
39455143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
39463648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
39477e860f15SJohn Edward Broadbent         });
3948a3316fc6SZhikuiRen }
3949a3316fc6SZhikuiRen 
3950647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3951647b3cdcSGeorge Liu {
39520fda0f12SGeorge Liu     BMCWEB_ROUTE(
39530fda0f12SGeorge Liu         app,
395422d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
3955647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3956647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
395745ca1b86SEd Tanous             [&app](const crow::Request& req,
3958647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
395922d268cbSEd Tanous                    const std::string& systemName,
3960647b3cdcSGeorge Liu                    const std::string& postCodeID) {
39613ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
396245ca1b86SEd Tanous         {
396345ca1b86SEd Tanous             return;
396445ca1b86SEd Tanous         }
396599351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
396699351cd8SEd Tanous                 req.getHeaderValue("Accept"),
39674a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
3968647b3cdcSGeorge Liu         {
3969002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3970647b3cdcSGeorge Liu             return;
3971647b3cdcSGeorge Liu         }
397222d268cbSEd Tanous         if (systemName != "system")
397322d268cbSEd Tanous         {
397422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
397522d268cbSEd Tanous                                        systemName);
397622d268cbSEd Tanous             return;
397722d268cbSEd Tanous         }
3978647b3cdcSGeorge Liu 
3979647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3980647b3cdcSGeorge Liu         uint16_t index = 0;
3981647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3982647b3cdcSGeorge Liu         {
3983002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3984647b3cdcSGeorge Liu             return;
3985647b3cdcSGeorge Liu         }
3986647b3cdcSGeorge Liu 
3987647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3988647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3989647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3990002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3991002d39b4SEd Tanous                     postcodes) {
3992647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3993647b3cdcSGeorge Liu             {
3994002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3995002d39b4SEd Tanous                                            postCodeID);
3996647b3cdcSGeorge Liu                 return;
3997647b3cdcSGeorge Liu             }
3998647b3cdcSGeorge Liu             if (ec)
3999647b3cdcSGeorge Liu             {
4000647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
4001647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
4002647b3cdcSGeorge Liu                 return;
4003647b3cdcSGeorge Liu             }
4004647b3cdcSGeorge Liu 
4005647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
4006002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
4007647b3cdcSGeorge Liu             {
4008647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
4009002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4010002d39b4SEd Tanous                                            postCodeID);
4011647b3cdcSGeorge Liu                 return;
4012647b3cdcSGeorge Liu             }
4013647b3cdcSGeorge Liu 
40149eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
401546ff87baSEd Tanous             if (c.empty())
4016647b3cdcSGeorge Liu             {
4017647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
4018002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4019002d39b4SEd Tanous                                            postCodeID);
4020647b3cdcSGeorge Liu                 return;
4021647b3cdcSGeorge Liu             }
402246ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
402346ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
402446ff87baSEd Tanous             std::string_view strData(d, c.size());
4025647b3cdcSGeorge Liu 
4026d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
4027647b3cdcSGeorge Liu                                      "application/octet-stream");
4028d9f6c621SEd Tanous             asyncResp->res.addHeader(
4029d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
4030002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
4031647b3cdcSGeorge Liu             },
4032647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
4033647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
4034002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4035647b3cdcSGeorge Liu         });
4036647b3cdcSGeorge Liu }
4037647b3cdcSGeorge Liu 
40387e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
4039a3316fc6SZhikuiRen {
40407e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
404122d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
4042ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
40437e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
404445ca1b86SEd Tanous             [&app](const crow::Request& req,
40457e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
404622d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
40473ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
404845ca1b86SEd Tanous         {
404945ca1b86SEd Tanous             return;
405045ca1b86SEd Tanous         }
405122d268cbSEd Tanous         if (systemName != "system")
405222d268cbSEd Tanous         {
405322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
405422d268cbSEd Tanous                                        systemName);
405522d268cbSEd Tanous             return;
405622d268cbSEd Tanous         }
405722d268cbSEd Tanous 
40586f284d24SJiaqing Zhao         getPostCodeForEntry(asyncResp, targetID);
40597e860f15SJohn Edward Broadbent         });
4060a3316fc6SZhikuiRen }
4061a3316fc6SZhikuiRen 
40621da66f75SEd Tanous } // namespace redfish
4063