xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision e645c5e6a922817a56c3b9452799b8b4cce38a7c)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
183ccb3adbSEd Tanous #include "app.hpp"
197a1dbc48SGeorge Liu #include "dbus_utility.hpp"
203ccb3adbSEd Tanous #include "error_messages.hpp"
21b7028ebfSSpencer Ku #include "gzfile.hpp"
22647b3cdcSGeorge Liu #include "http_utility.hpp"
23b7028ebfSSpencer Ku #include "human_sort.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
254851d45dSJason M. Bills #include "registries.hpp"
264851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
274851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
283ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
2946229577SJames Feist #include "task.hpp"
303ccb3adbSEd Tanous #include "utils/dbus_utils.hpp"
313ccb3adbSEd Tanous #include "utils/time_utils.hpp"
321da66f75SEd Tanous 
33e1f26343SJason M. Bills #include <systemd/sd-journal.h>
348e31778eSAsmitha Karunanithi #include <tinyxml2.h>
35400fd1fbSAdriana Kobylak #include <unistd.h>
36e1f26343SJason M. Bills 
379896eaedSEd Tanous #include <boost/algorithm/string/case_conv.hpp>
3811ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
39400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
404851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
4107c8c20dSEd Tanous #include <boost/beast/http/verb.hpp>
421da66f75SEd Tanous #include <boost/container/flat_map.hpp>
431ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
44d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
45d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
461214b7e7SGunnar Mills 
477a1dbc48SGeorge Liu #include <array>
48647b3cdcSGeorge Liu #include <charconv>
494418c7f0SJames Feist #include <filesystem>
5075710de2SXiaochao Ma #include <optional>
5126702d01SEd Tanous #include <span>
52cd225da8SJason M. Bills #include <string_view>
53abf2add6SEd Tanous #include <variant>
541da66f75SEd Tanous 
551da66f75SEd Tanous namespace redfish
561da66f75SEd Tanous {
571da66f75SEd Tanous 
585b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
595b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
605b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
615b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
625b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
635b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
64424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
656eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
666eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
671da66f75SEd Tanous 
688e31778eSAsmitha Karunanithi enum class DumpCreationProgress
698e31778eSAsmitha Karunanithi {
708e31778eSAsmitha Karunanithi     DUMP_CREATE_SUCCESS,
718e31778eSAsmitha Karunanithi     DUMP_CREATE_FAILED,
728e31778eSAsmitha Karunanithi     DUMP_CREATE_INPROGRESS
738e31778eSAsmitha Karunanithi };
748e31778eSAsmitha Karunanithi 
75fffb8c1fSEd Tanous namespace registries
764851d45dSJason M. Bills {
7726702d01SEd Tanous static const Message*
7826702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
7926702d01SEd Tanous                            const std::span<const MessageEntry> registry)
804851d45dSJason M. Bills {
81002d39b4SEd Tanous     std::span<const MessageEntry>::iterator messageIt =
82002d39b4SEd Tanous         std::find_if(registry.begin(), registry.end(),
834851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
84e662eae8SEd Tanous         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
854851d45dSJason M. Bills         });
8626702d01SEd Tanous     if (messageIt != registry.end())
874851d45dSJason M. Bills     {
884851d45dSJason M. Bills         return &messageIt->second;
894851d45dSJason M. Bills     }
904851d45dSJason M. Bills 
914851d45dSJason M. Bills     return nullptr;
924851d45dSJason M. Bills }
934851d45dSJason M. Bills 
944851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
954851d45dSJason M. Bills {
964851d45dSJason M. Bills     // Redfish MessageIds are in the form
974851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
984851d45dSJason M. Bills     // the right Message
994851d45dSJason M. Bills     std::vector<std::string> fields;
1004851d45dSJason M. Bills     fields.reserve(4);
10150ebd4afSEd Tanous     bmcweb::split(fields, messageID, '.');
10202cad96eSEd Tanous     const std::string& registryName = fields[0];
10302cad96eSEd Tanous     const std::string& messageKey = fields[3];
1044851d45dSJason M. Bills 
1054851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
1064851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
1074851d45dSJason M. Bills     {
1084851d45dSJason M. Bills         return getMessageFromRegistry(
10926702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
1104851d45dSJason M. Bills     }
1114851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
1124851d45dSJason M. Bills     {
1134851d45dSJason M. Bills         return getMessageFromRegistry(
11426702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1154851d45dSJason M. Bills     }
1164851d45dSJason M. Bills     return nullptr;
1174851d45dSJason M. Bills }
118fffb8c1fSEd Tanous } // namespace registries
1194851d45dSJason M. Bills 
120f6150403SJames Feist namespace fs = std::filesystem;
1211da66f75SEd Tanous 
122cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
123cb92c03bSAndrew Geissler {
124d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
125d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
126d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
127d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
128cb92c03bSAndrew Geissler     {
129cb92c03bSAndrew Geissler         return "Critical";
130cb92c03bSAndrew Geissler     }
1313174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
132d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
133d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
134cb92c03bSAndrew Geissler     {
135cb92c03bSAndrew Geissler         return "OK";
136cb92c03bSAndrew Geissler     }
1373174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
138cb92c03bSAndrew Geissler     {
139cb92c03bSAndrew Geissler         return "Warning";
140cb92c03bSAndrew Geissler     }
141cb92c03bSAndrew Geissler     return "";
142cb92c03bSAndrew Geissler }
143cb92c03bSAndrew Geissler 
1449017faf2SAbhishek Patel inline std::optional<bool> getProviderNotifyAction(const std::string& notify)
1459017faf2SAbhishek Patel {
1469017faf2SAbhishek Patel     std::optional<bool> notifyAction;
1479017faf2SAbhishek Patel     if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Notify")
1489017faf2SAbhishek Patel     {
1499017faf2SAbhishek Patel         notifyAction = true;
1509017faf2SAbhishek Patel     }
1519017faf2SAbhishek Patel     else if (notify == "xyz.openbmc_project.Logging.Entry.Notify.Inhibit")
1529017faf2SAbhishek Patel     {
1539017faf2SAbhishek Patel         notifyAction = false;
1549017faf2SAbhishek Patel     }
1559017faf2SAbhishek Patel 
1569017faf2SAbhishek Patel     return notifyAction;
1579017faf2SAbhishek Patel }
1589017faf2SAbhishek Patel 
1597e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
16039e77504SEd Tanous                                      const std::string_view& field,
16139e77504SEd Tanous                                      std::string_view& contents)
16216428a1aSJason M. Bills {
16316428a1aSJason M. Bills     const char* data = nullptr;
16416428a1aSJason M. Bills     size_t length = 0;
16516428a1aSJason M. Bills     int ret = 0;
16616428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16746ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
16846ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
16946ff87baSEd Tanous 
17046ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
17116428a1aSJason M. Bills     if (ret < 0)
17216428a1aSJason M. Bills     {
17316428a1aSJason M. Bills         return ret;
17416428a1aSJason M. Bills     }
17539e77504SEd Tanous     contents = std::string_view(data, length);
17616428a1aSJason M. Bills     // Only use the content after the "=" character.
17781ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
17816428a1aSJason M. Bills     return ret;
17916428a1aSJason M. Bills }
18016428a1aSJason M. Bills 
1817e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1827e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1837e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
18416428a1aSJason M. Bills {
18516428a1aSJason M. Bills     int ret = 0;
18639e77504SEd Tanous     std::string_view metadata;
18716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
18816428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
18916428a1aSJason M. Bills     if (ret < 0)
19016428a1aSJason M. Bills     {
19116428a1aSJason M. Bills         return ret;
19216428a1aSJason M. Bills     }
193b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
19416428a1aSJason M. Bills     return ret;
19516428a1aSJason M. Bills }
19616428a1aSJason M. Bills 
1977e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1987e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
199a3316fc6SZhikuiRen {
200a3316fc6SZhikuiRen     int ret = 0;
201a3316fc6SZhikuiRen     uint64_t timestamp = 0;
202a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
203a3316fc6SZhikuiRen     if (ret < 0)
204a3316fc6SZhikuiRen     {
205a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
206a3316fc6SZhikuiRen                          << strerror(-ret);
207a3316fc6SZhikuiRen         return false;
208a3316fc6SZhikuiRen     }
209*e645c5e6SKonstantin Aladyshev     entryTimestamp = redfish::time_utils::getDateTimeUintUs(timestamp);
2109c620e21SAsmitha Karunanithi     return true;
211a3316fc6SZhikuiRen }
21250b8a43aSEd Tanous 
2137e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
214e85d6b16SJason M. Bills                                     const bool firstEntry = true)
21516428a1aSJason M. Bills {
21616428a1aSJason M. Bills     int ret = 0;
21716428a1aSJason M. Bills     static uint64_t prevTs = 0;
21816428a1aSJason M. Bills     static int index = 0;
219e85d6b16SJason M. Bills     if (firstEntry)
220e85d6b16SJason M. Bills     {
221e85d6b16SJason M. Bills         prevTs = 0;
222e85d6b16SJason M. Bills     }
223e85d6b16SJason M. Bills 
22416428a1aSJason M. Bills     // Get the entry timestamp
22516428a1aSJason M. Bills     uint64_t curTs = 0;
22616428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
22716428a1aSJason M. Bills     if (ret < 0)
22816428a1aSJason M. Bills     {
22916428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
23016428a1aSJason M. Bills                          << strerror(-ret);
23116428a1aSJason M. Bills         return false;
23216428a1aSJason M. Bills     }
23316428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
23416428a1aSJason M. Bills     if (curTs == prevTs)
23516428a1aSJason M. Bills     {
23616428a1aSJason M. Bills         index++;
23716428a1aSJason M. Bills     }
23816428a1aSJason M. Bills     else
23916428a1aSJason M. Bills     {
24016428a1aSJason M. Bills         // Otherwise, reset it
24116428a1aSJason M. Bills         index = 0;
24216428a1aSJason M. Bills     }
24316428a1aSJason M. Bills     // Save the timestamp
24416428a1aSJason M. Bills     prevTs = curTs;
24516428a1aSJason M. Bills 
24616428a1aSJason M. Bills     entryID = std::to_string(curTs);
24716428a1aSJason M. Bills     if (index > 0)
24816428a1aSJason M. Bills     {
24916428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
25016428a1aSJason M. Bills     }
25116428a1aSJason M. Bills     return true;
25216428a1aSJason M. Bills }
25316428a1aSJason M. Bills 
254e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
255e85d6b16SJason M. Bills                              const bool firstEntry = true)
25695820184SJason M. Bills {
257271584abSEd Tanous     static time_t prevTs = 0;
25895820184SJason M. Bills     static int index = 0;
259e85d6b16SJason M. Bills     if (firstEntry)
260e85d6b16SJason M. Bills     {
261e85d6b16SJason M. Bills         prevTs = 0;
262e85d6b16SJason M. Bills     }
263e85d6b16SJason M. Bills 
26495820184SJason M. Bills     // Get the entry timestamp
265271584abSEd Tanous     std::time_t curTs = 0;
26695820184SJason M. Bills     std::tm timeStruct = {};
26795820184SJason M. Bills     std::istringstream entryStream(logEntry);
26895820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
26995820184SJason M. Bills     {
27095820184SJason M. Bills         curTs = std::mktime(&timeStruct);
27195820184SJason M. Bills     }
27295820184SJason M. Bills     // If the timestamp isn't unique, increment the index
27395820184SJason M. Bills     if (curTs == prevTs)
27495820184SJason M. Bills     {
27595820184SJason M. Bills         index++;
27695820184SJason M. Bills     }
27795820184SJason M. Bills     else
27895820184SJason M. Bills     {
27995820184SJason M. Bills         // Otherwise, reset it
28095820184SJason M. Bills         index = 0;
28195820184SJason M. Bills     }
28295820184SJason M. Bills     // Save the timestamp
28395820184SJason M. Bills     prevTs = curTs;
28495820184SJason M. Bills 
28595820184SJason M. Bills     entryID = std::to_string(curTs);
28695820184SJason M. Bills     if (index > 0)
28795820184SJason M. Bills     {
28895820184SJason M. Bills         entryID += "_" + std::to_string(index);
28995820184SJason M. Bills     }
29095820184SJason M. Bills     return true;
29195820184SJason M. Bills }
29295820184SJason M. Bills 
2937e860f15SJohn Edward Broadbent inline static bool
2948d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2958d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2968d1b46d7Szhanghch05                        uint64_t& index)
29716428a1aSJason M. Bills {
29816428a1aSJason M. Bills     if (entryID.empty())
29916428a1aSJason M. Bills     {
30016428a1aSJason M. Bills         return false;
30116428a1aSJason M. Bills     }
30216428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
30339e77504SEd Tanous     std::string_view tsStr(entryID);
30416428a1aSJason M. Bills 
30581ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
30671d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
30716428a1aSJason M. Bills     {
30816428a1aSJason M. Bills         // Timestamp has an index
30916428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
31039e77504SEd Tanous         std::string_view indexStr(entryID);
31116428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
312c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
313c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
314c0bd5e4bSEd Tanous         if (ec != std::errc())
31516428a1aSJason M. Bills         {
3169db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
31716428a1aSJason M. Bills             return false;
31816428a1aSJason M. Bills         }
31916428a1aSJason M. Bills     }
32016428a1aSJason M. Bills     // Timestamp has no index
321c0bd5e4bSEd Tanous     auto [ptr, ec] =
322c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
323c0bd5e4bSEd Tanous     if (ec != std::errc())
32416428a1aSJason M. Bills     {
3259db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
32616428a1aSJason M. Bills         return false;
32716428a1aSJason M. Bills     }
32816428a1aSJason M. Bills     return true;
32916428a1aSJason M. Bills }
33016428a1aSJason M. Bills 
33195820184SJason M. Bills static bool
33295820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
33395820184SJason M. Bills {
33495820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
33595820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
33695820184SJason M. Bills 
33795820184SJason M. Bills     // Loop through the directory looking for redfish log files
33895820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
33995820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
34095820184SJason M. Bills     {
34195820184SJason M. Bills         // If we find a redfish log file, save the path
34295820184SJason M. Bills         std::string filename = dirEnt.path().filename();
34311ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
34495820184SJason M. Bills         {
34595820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
34695820184SJason M. Bills         }
34795820184SJason M. Bills     }
34895820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
34995820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
35095820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
35195820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
35295820184SJason M. Bills 
35395820184SJason M. Bills     return !redfishLogFiles.empty();
35495820184SJason M. Bills }
35595820184SJason M. Bills 
356aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
3572d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
358c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
359aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
360aefe3786SClaire Weinan {
361aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
362aefe3786SClaire Weinan     {
363aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
364aefe3786SClaire Weinan         {
365aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
366aefe3786SClaire Weinan             {
367aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
368aefe3786SClaire Weinan                 {
369aefe3786SClaire Weinan                     const auto* status =
370aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
371aefe3786SClaire Weinan                     if (status == nullptr)
372aefe3786SClaire Weinan                     {
373aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
374aefe3786SClaire Weinan                         break;
375aefe3786SClaire Weinan                     }
376aefe3786SClaire Weinan                     dumpStatus = *status;
377aefe3786SClaire Weinan                 }
378aefe3786SClaire Weinan             }
379aefe3786SClaire Weinan         }
380aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
381aefe3786SClaire Weinan         {
382aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
383aefe3786SClaire Weinan             {
384aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
385aefe3786SClaire Weinan                 {
386aefe3786SClaire Weinan                     const auto* sizePtr =
387aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
388aefe3786SClaire Weinan                     if (sizePtr == nullptr)
389aefe3786SClaire Weinan                     {
390aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
391aefe3786SClaire Weinan                         break;
392aefe3786SClaire Weinan                     }
393aefe3786SClaire Weinan                     size = *sizePtr;
394aefe3786SClaire Weinan                     break;
395aefe3786SClaire Weinan                 }
396aefe3786SClaire Weinan             }
397aefe3786SClaire Weinan         }
398aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
399aefe3786SClaire Weinan         {
400aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
401aefe3786SClaire Weinan             {
402aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
403aefe3786SClaire Weinan                 {
404aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
405aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
406aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
407aefe3786SClaire Weinan                     {
408aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
409aefe3786SClaire Weinan                         break;
410aefe3786SClaire Weinan                     }
411c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
412aefe3786SClaire Weinan                     break;
413aefe3786SClaire Weinan                 }
414aefe3786SClaire Weinan             }
415aefe3786SClaire Weinan         }
416aefe3786SClaire Weinan     }
417aefe3786SClaire Weinan }
418aefe3786SClaire Weinan 
41921ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
420fdd26906SClaire Weinan {
421fdd26906SClaire Weinan     std::string entriesPath;
422fdd26906SClaire Weinan 
423fdd26906SClaire Weinan     if (dumpType == "BMC")
424fdd26906SClaire Weinan     {
425fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
426fdd26906SClaire Weinan     }
427fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
428fdd26906SClaire Weinan     {
429fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
430fdd26906SClaire Weinan     }
431fdd26906SClaire Weinan     else if (dumpType == "System")
432fdd26906SClaire Weinan     {
433fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
434fdd26906SClaire Weinan     }
435fdd26906SClaire Weinan     else
436fdd26906SClaire Weinan     {
437fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
438fdd26906SClaire Weinan                          << dumpType;
439fdd26906SClaire Weinan     }
440fdd26906SClaire Weinan 
441fdd26906SClaire Weinan     // Returns empty string on error
442fdd26906SClaire Weinan     return entriesPath;
443fdd26906SClaire Weinan }
444fdd26906SClaire Weinan 
4458d1b46d7Szhanghch05 inline void
4468d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4475cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4485cb1dd27SAsmitha Karunanithi {
449fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
450fdd26906SClaire Weinan     if (entriesPath.empty())
4515cb1dd27SAsmitha Karunanithi     {
4525cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4535cb1dd27SAsmitha Karunanithi         return;
4545cb1dd27SAsmitha Karunanithi     }
4555cb1dd27SAsmitha Karunanithi 
4565cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
457fdd26906SClaire Weinan         [asyncResp, entriesPath,
458711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
459711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4605cb1dd27SAsmitha Karunanithi         if (ec)
4615cb1dd27SAsmitha Karunanithi         {
4625cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4635cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4645cb1dd27SAsmitha Karunanithi             return;
4655cb1dd27SAsmitha Karunanithi         }
4665cb1dd27SAsmitha Karunanithi 
467fdd26906SClaire Weinan         // Remove ending slash
468fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
469fdd26906SClaire Weinan         if (!odataIdStr.empty())
470fdd26906SClaire Weinan         {
471fdd26906SClaire Weinan             odataIdStr.pop_back();
472fdd26906SClaire Weinan         }
473fdd26906SClaire Weinan 
474fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
475fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
476fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
477fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
478fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
479fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
480fdd26906SClaire Weinan 
4815cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4825cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
483b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
484b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
485002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
4865cb1dd27SAsmitha Karunanithi 
487002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
488002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
489002d39b4SEd Tanous                                                r.first.filename());
490565dfb6fSClaire Weinan         });
491565dfb6fSClaire Weinan 
4925cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4935cb1dd27SAsmitha Karunanithi         {
494b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4955cb1dd27SAsmitha Karunanithi             {
4965cb1dd27SAsmitha Karunanithi                 continue;
4975cb1dd27SAsmitha Karunanithi             }
498c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
4995cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
50035440d18SAsmitha Karunanithi             std::string dumpStatus;
501433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
5022dfd18efSEd Tanous 
5032dfd18efSEd Tanous             std::string entryID = object.first.filename();
5042dfd18efSEd Tanous             if (entryID.empty())
5055cb1dd27SAsmitha Karunanithi             {
5065cb1dd27SAsmitha Karunanithi                 continue;
5075cb1dd27SAsmitha Karunanithi             }
5085cb1dd27SAsmitha Karunanithi 
509c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
510aefe3786SClaire Weinan                                          asyncResp);
5115cb1dd27SAsmitha Karunanithi 
5120fda0f12SGeorge Liu             if (dumpStatus !=
5130fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
51435440d18SAsmitha Karunanithi                 !dumpStatus.empty())
51535440d18SAsmitha Karunanithi             {
51635440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
51735440d18SAsmitha Karunanithi                 continue;
51835440d18SAsmitha Karunanithi             }
51935440d18SAsmitha Karunanithi 
5209c11a172SVijay Lobo             thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
521fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
5225cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
5235cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
5245cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
525bbd80db8SClaire Weinan             thisEntry["Created"] =
526bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5275cb1dd27SAsmitha Karunanithi 
5285cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5295cb1dd27SAsmitha Karunanithi             {
530d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
531d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
532fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
533fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5345cb1dd27SAsmitha Karunanithi             }
5355cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5365cb1dd27SAsmitha Karunanithi             {
537d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
538d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
539d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
540fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
541fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5425cb1dd27SAsmitha Karunanithi             }
54335440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5445cb1dd27SAsmitha Karunanithi         }
545002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5465cb1dd27SAsmitha Karunanithi         },
5475cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5485cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5495cb1dd27SAsmitha Karunanithi }
5505cb1dd27SAsmitha Karunanithi 
5518d1b46d7Szhanghch05 inline void
552c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5538d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5545cb1dd27SAsmitha Karunanithi {
555fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
556fdd26906SClaire Weinan     if (entriesPath.empty())
5575cb1dd27SAsmitha Karunanithi     {
5585cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5595cb1dd27SAsmitha Karunanithi         return;
5605cb1dd27SAsmitha Karunanithi     }
5615cb1dd27SAsmitha Karunanithi 
5625cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
563fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
564fdd26906SClaire Weinan          entriesPath](const boost::system::error_code ec,
56502cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5665cb1dd27SAsmitha Karunanithi         if (ec)
5675cb1dd27SAsmitha Karunanithi         {
5685cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5695cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5705cb1dd27SAsmitha Karunanithi             return;
5715cb1dd27SAsmitha Karunanithi         }
5725cb1dd27SAsmitha Karunanithi 
573b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
574b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
575b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
576002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
577b47452b2SAsmitha Karunanithi 
5789eb808c1SEd Tanous         for (const auto& objectPath : resp)
5795cb1dd27SAsmitha Karunanithi         {
580b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5815cb1dd27SAsmitha Karunanithi             {
5825cb1dd27SAsmitha Karunanithi                 continue;
5835cb1dd27SAsmitha Karunanithi             }
5845cb1dd27SAsmitha Karunanithi 
5855cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
586c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5875cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
58835440d18SAsmitha Karunanithi             std::string dumpStatus;
5895cb1dd27SAsmitha Karunanithi 
590aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
591c6fecdabSClaire Weinan                                          timestampUs, asyncResp);
5925cb1dd27SAsmitha Karunanithi 
5930fda0f12SGeorge Liu             if (dumpStatus !=
5940fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
59535440d18SAsmitha Karunanithi                 !dumpStatus.empty())
59635440d18SAsmitha Karunanithi             {
59735440d18SAsmitha Karunanithi                 // Dump status is not Complete
59835440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
599d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
600d1bde9e5SKrzysztof Grobelny                                            entryID);
60135440d18SAsmitha Karunanithi                 return;
60235440d18SAsmitha Karunanithi             }
60335440d18SAsmitha Karunanithi 
6045cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
6059c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
606fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
6075cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
6085cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
6095cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
610bbd80db8SClaire Weinan             asyncResp->res.jsonValue["Created"] =
611bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
6125cb1dd27SAsmitha Karunanithi 
6135cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
6145cb1dd27SAsmitha Karunanithi             {
615d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
616d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
617fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
618fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6195cb1dd27SAsmitha Karunanithi             }
6205cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6215cb1dd27SAsmitha Karunanithi             {
622d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
623002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
624d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
625fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
626fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6275cb1dd27SAsmitha Karunanithi             }
6285cb1dd27SAsmitha Karunanithi         }
629e05aec50SEd Tanous         if (!foundDumpEntry)
630b47452b2SAsmitha Karunanithi         {
631b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
632b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
633b47452b2SAsmitha Karunanithi             return;
634b47452b2SAsmitha Karunanithi         }
6355cb1dd27SAsmitha Karunanithi         },
6365cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6375cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6385cb1dd27SAsmitha Karunanithi }
6395cb1dd27SAsmitha Karunanithi 
6408d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6419878256fSStanley Chu                             const std::string& entryID,
642b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6435cb1dd27SAsmitha Karunanithi {
644002d39b4SEd Tanous     auto respHandler =
645002d39b4SEd Tanous         [asyncResp, entryID](const boost::system::error_code ec) {
6465cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6475cb1dd27SAsmitha Karunanithi         if (ec)
6485cb1dd27SAsmitha Karunanithi         {
6493de8d8baSGeorge Liu             if (ec.value() == EBADR)
6503de8d8baSGeorge Liu             {
6513de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6523de8d8baSGeorge Liu                 return;
6533de8d8baSGeorge Liu             }
6545cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
655fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6565cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6575cb1dd27SAsmitha Karunanithi             return;
6585cb1dd27SAsmitha Karunanithi         }
6595cb1dd27SAsmitha Karunanithi     };
6605cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6615cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
662b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
663b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
664b47452b2SAsmitha Karunanithi             entryID,
6655cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6665cb1dd27SAsmitha Karunanithi }
6675cb1dd27SAsmitha Karunanithi 
6688e31778eSAsmitha Karunanithi inline DumpCreationProgress
6698e31778eSAsmitha Karunanithi     mapDbusStatusToDumpProgress(const std::string& status)
670a43be80fSAsmitha Karunanithi {
6718e31778eSAsmitha Karunanithi     if (status ==
6728e31778eSAsmitha Karunanithi             "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
6738e31778eSAsmitha Karunanithi         status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
6748e31778eSAsmitha Karunanithi     {
6758e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_FAILED;
6768e31778eSAsmitha Karunanithi     }
6778e31778eSAsmitha Karunanithi     if (status ==
6788e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
6798e31778eSAsmitha Karunanithi     {
6808e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_SUCCESS;
6818e31778eSAsmitha Karunanithi     }
6828e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
6838e31778eSAsmitha Karunanithi }
6848e31778eSAsmitha Karunanithi 
6858e31778eSAsmitha Karunanithi inline DumpCreationProgress
6868e31778eSAsmitha Karunanithi     getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
6878e31778eSAsmitha Karunanithi {
6888e31778eSAsmitha Karunanithi     for (const auto& [key, val] : values)
6898e31778eSAsmitha Karunanithi     {
6908e31778eSAsmitha Karunanithi         if (key == "Status")
6918e31778eSAsmitha Karunanithi         {
6928e31778eSAsmitha Karunanithi             const std::string* value = std::get_if<std::string>(&val);
6938e31778eSAsmitha Karunanithi             if (value == nullptr)
6948e31778eSAsmitha Karunanithi             {
6958e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Status property value is null";
6968e31778eSAsmitha Karunanithi                 return DumpCreationProgress::DUMP_CREATE_FAILED;
6978e31778eSAsmitha Karunanithi             }
6988e31778eSAsmitha Karunanithi             return mapDbusStatusToDumpProgress(*value);
6998e31778eSAsmitha Karunanithi         }
7008e31778eSAsmitha Karunanithi     }
7018e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
7028e31778eSAsmitha Karunanithi }
7038e31778eSAsmitha Karunanithi 
7048e31778eSAsmitha Karunanithi inline std::string getDumpEntryPath(const std::string& dumpPath)
7058e31778eSAsmitha Karunanithi {
7068e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
7078e31778eSAsmitha Karunanithi     {
7088e31778eSAsmitha Karunanithi         return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
7098e31778eSAsmitha Karunanithi     }
7108e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
7118e31778eSAsmitha Karunanithi     {
7128e31778eSAsmitha Karunanithi         return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
7138e31778eSAsmitha Karunanithi     }
7148e31778eSAsmitha Karunanithi     return "";
7158e31778eSAsmitha Karunanithi }
7168e31778eSAsmitha Karunanithi 
7178e31778eSAsmitha Karunanithi inline void createDumpTaskCallback(
7188e31778eSAsmitha Karunanithi     task::Payload&& payload,
7198e31778eSAsmitha Karunanithi     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7208e31778eSAsmitha Karunanithi     const sdbusplus::message::object_path& createdObjPath)
7218e31778eSAsmitha Karunanithi {
7228e31778eSAsmitha Karunanithi     const std::string dumpPath = createdObjPath.parent_path().str;
7238e31778eSAsmitha Karunanithi     const std::string dumpId = createdObjPath.filename();
7248e31778eSAsmitha Karunanithi 
7258e31778eSAsmitha Karunanithi     std::string dumpEntryPath = getDumpEntryPath(dumpPath);
7268e31778eSAsmitha Karunanithi 
7278e31778eSAsmitha Karunanithi     if (dumpEntryPath.empty())
7288e31778eSAsmitha Karunanithi     {
7298e31778eSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type received";
7308e31778eSAsmitha Karunanithi         messages::internalError(asyncResp->res);
7318e31778eSAsmitha Karunanithi         return;
7328e31778eSAsmitha Karunanithi     }
7338e31778eSAsmitha Karunanithi 
7348e31778eSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7358e31778eSAsmitha Karunanithi         [asyncResp, payload, createdObjPath,
7368e31778eSAsmitha Karunanithi          dumpEntryPath{std::move(dumpEntryPath)},
7378e31778eSAsmitha Karunanithi          dumpId](const boost::system::error_code ec,
7388e31778eSAsmitha Karunanithi                  const std::string& introspectXml) {
7398e31778eSAsmitha Karunanithi         if (ec)
7408e31778eSAsmitha Karunanithi         {
7418e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Introspect call failed with error: "
7428e31778eSAsmitha Karunanithi                              << ec.message();
7438e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7448e31778eSAsmitha Karunanithi             return;
7458e31778eSAsmitha Karunanithi         }
7468e31778eSAsmitha Karunanithi 
7478e31778eSAsmitha Karunanithi         // Check if the created dump object has implemented Progress
7488e31778eSAsmitha Karunanithi         // interface to track dump completion. If yes, fetch the "Status"
7498e31778eSAsmitha Karunanithi         // property of the interface, modify the task state accordingly.
7508e31778eSAsmitha Karunanithi         // Else, return task completed.
7518e31778eSAsmitha Karunanithi         tinyxml2::XMLDocument doc;
7528e31778eSAsmitha Karunanithi 
7538e31778eSAsmitha Karunanithi         doc.Parse(introspectXml.data(), introspectXml.size());
7548e31778eSAsmitha Karunanithi         tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
7558e31778eSAsmitha Karunanithi         if (pRoot == nullptr)
7568e31778eSAsmitha Karunanithi         {
7578e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "XML document failed to parse";
7588e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7598e31778eSAsmitha Karunanithi             return;
7608e31778eSAsmitha Karunanithi         }
7618e31778eSAsmitha Karunanithi         tinyxml2::XMLElement* interfaceNode =
7628e31778eSAsmitha Karunanithi             pRoot->FirstChildElement("interface");
7638e31778eSAsmitha Karunanithi 
7648e31778eSAsmitha Karunanithi         bool isProgressIntfPresent = false;
7658e31778eSAsmitha Karunanithi         while (interfaceNode != nullptr)
7668e31778eSAsmitha Karunanithi         {
7678e31778eSAsmitha Karunanithi             const char* thisInterfaceName = interfaceNode->Attribute("name");
7688e31778eSAsmitha Karunanithi             if (thisInterfaceName != nullptr)
7698e31778eSAsmitha Karunanithi             {
7708e31778eSAsmitha Karunanithi                 if (thisInterfaceName ==
7718e31778eSAsmitha Karunanithi                     std::string_view("xyz.openbmc_project.Common.Progress"))
7728e31778eSAsmitha Karunanithi                 {
7738e31778eSAsmitha Karunanithi                     interfaceNode =
7748e31778eSAsmitha Karunanithi                         interfaceNode->NextSiblingElement("interface");
7758e31778eSAsmitha Karunanithi                     continue;
7768e31778eSAsmitha Karunanithi                 }
7778e31778eSAsmitha Karunanithi                 isProgressIntfPresent = true;
7788e31778eSAsmitha Karunanithi                 break;
7798e31778eSAsmitha Karunanithi             }
7808e31778eSAsmitha Karunanithi             interfaceNode = interfaceNode->NextSiblingElement("interface");
7818e31778eSAsmitha Karunanithi         }
7828e31778eSAsmitha Karunanithi 
783a43be80fSAsmitha Karunanithi         std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7848e31778eSAsmitha Karunanithi             [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
7855b378546SPatrick Williams                 boost::system::error_code err, sdbusplus::message_t& msg,
786a43be80fSAsmitha Karunanithi                 const std::shared_ptr<task::TaskData>& taskData) {
787cb13a392SEd Tanous             if (err)
788cb13a392SEd Tanous             {
7898e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << createdObjPath.str
7908e31778eSAsmitha Karunanithi                                  << ": Error in creating dump";
7918e31778eSAsmitha Karunanithi                 taskData->messages.emplace_back(messages::internalError());
7926145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7936145ed6fSAsmitha Karunanithi                 return task::completed;
794cb13a392SEd Tanous             }
795b9d36b47SEd Tanous 
7968e31778eSAsmitha Karunanithi             if (isProgressIntfPresent)
797a43be80fSAsmitha Karunanithi             {
7988e31778eSAsmitha Karunanithi                 dbus::utility::DBusPropertiesMap values;
7998e31778eSAsmitha Karunanithi                 std::string prop;
8008e31778eSAsmitha Karunanithi                 msg.read(prop, values);
8018e31778eSAsmitha Karunanithi 
8028e31778eSAsmitha Karunanithi                 DumpCreationProgress dumpStatus =
8038e31778eSAsmitha Karunanithi                     getDumpCompletionStatus(values);
8048e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
8058e31778eSAsmitha Karunanithi                 {
8068e31778eSAsmitha Karunanithi                     BMCWEB_LOG_ERROR << createdObjPath.str
8078e31778eSAsmitha Karunanithi                                      << ": Error in creating dump";
8088e31778eSAsmitha Karunanithi                     taskData->state = "Cancelled";
8098e31778eSAsmitha Karunanithi                     return task::completed;
8108e31778eSAsmitha Karunanithi                 }
8118e31778eSAsmitha Karunanithi 
8128e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
8138e31778eSAsmitha Karunanithi                 {
8148e31778eSAsmitha Karunanithi                     BMCWEB_LOG_DEBUG << createdObjPath.str
8158e31778eSAsmitha Karunanithi                                      << ": Dump creation task is in progress";
8168e31778eSAsmitha Karunanithi                     return !task::completed;
8178e31778eSAsmitha Karunanithi                 }
8188e31778eSAsmitha Karunanithi             }
8198e31778eSAsmitha Karunanithi 
820a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
821a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
822a43be80fSAsmitha Karunanithi 
823a43be80fSAsmitha Karunanithi             std::string headerLoc =
8248e31778eSAsmitha Karunanithi                 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
825002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
826a43be80fSAsmitha Karunanithi 
8278e31778eSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << createdObjPath.str
8288e31778eSAsmitha Karunanithi                              << ": Dump creation task completed";
829a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
830b47452b2SAsmitha Karunanithi             return task::completed;
831a43be80fSAsmitha Karunanithi             },
8328e31778eSAsmitha Karunanithi             "type='signal',interface='org.freedesktop.DBus.Properties',"
8338e31778eSAsmitha Karunanithi             "member='PropertiesChanged',path='" +
8348e31778eSAsmitha Karunanithi                 createdObjPath.str + "'");
835a43be80fSAsmitha Karunanithi 
8368e31778eSAsmitha Karunanithi         // The task timer is set to max time limit within which the
8378e31778eSAsmitha Karunanithi         // requested dump will be collected.
8388e31778eSAsmitha Karunanithi         task->startTimer(std::chrono::minutes(6));
839a43be80fSAsmitha Karunanithi         task->populateResp(asyncResp->res);
8408e31778eSAsmitha Karunanithi         task->payload.emplace(payload);
8418e31778eSAsmitha Karunanithi         },
8428e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", createdObjPath,
8438e31778eSAsmitha Karunanithi         "org.freedesktop.DBus.Introspectable", "Introspect");
844a43be80fSAsmitha Karunanithi }
845a43be80fSAsmitha Karunanithi 
8468d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8478d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
848a43be80fSAsmitha Karunanithi {
849fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
850fdd26906SClaire Weinan     if (dumpPath.empty())
851a43be80fSAsmitha Karunanithi     {
852a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
853a43be80fSAsmitha Karunanithi         return;
854a43be80fSAsmitha Karunanithi     }
855a43be80fSAsmitha Karunanithi 
856a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
857a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
858a43be80fSAsmitha Karunanithi 
85915ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
860a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
861a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
862a43be80fSAsmitha Karunanithi     {
863a43be80fSAsmitha Karunanithi         return;
864a43be80fSAsmitha Karunanithi     }
865a43be80fSAsmitha Karunanithi 
866a43be80fSAsmitha Karunanithi     if (dumpType == "System")
867a43be80fSAsmitha Karunanithi     {
868a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
869a43be80fSAsmitha Karunanithi         {
8704978b63fSJason M. Bills             BMCWEB_LOG_ERROR
8714978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
872a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
873a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
874a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
875a43be80fSAsmitha Karunanithi             return;
876a43be80fSAsmitha Karunanithi         }
8773174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
878a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
879a43be80fSAsmitha Karunanithi         {
880a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
881ace85d60SEd Tanous             messages::internalError(asyncResp->res);
882a43be80fSAsmitha Karunanithi             return;
883a43be80fSAsmitha Karunanithi         }
8845907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
885a43be80fSAsmitha Karunanithi     }
886a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
887a43be80fSAsmitha Karunanithi     {
888a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
889a43be80fSAsmitha Karunanithi         {
8900fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8910fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
892a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
893a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
894a43be80fSAsmitha Karunanithi             return;
895a43be80fSAsmitha Karunanithi         }
8963174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
897a43be80fSAsmitha Karunanithi         {
898a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
899a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
900ace85d60SEd Tanous             messages::internalError(asyncResp->res);
901a43be80fSAsmitha Karunanithi             return;
902a43be80fSAsmitha Karunanithi         }
9035907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
9045907571dSAsmitha Karunanithi     }
9055907571dSAsmitha Karunanithi     else
9065907571dSAsmitha Karunanithi     {
9075907571dSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
9085907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
9095907571dSAsmitha Karunanithi         return;
910a43be80fSAsmitha Karunanithi     }
911a43be80fSAsmitha Karunanithi 
9128e31778eSAsmitha Karunanithi     std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
9138e31778eSAsmitha Karunanithi         createDumpParamVec;
9148e31778eSAsmitha Karunanithi 
915a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
9165b378546SPatrick Williams         [asyncResp, payload(task::Payload(req)), dumpPath](
9175b378546SPatrick Williams             const boost::system::error_code ec, const sdbusplus::message_t& msg,
9188e31778eSAsmitha Karunanithi             const sdbusplus::message::object_path& objPath) mutable {
919a43be80fSAsmitha Karunanithi         if (ec)
920a43be80fSAsmitha Karunanithi         {
921a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
9225907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
9235907571dSAsmitha Karunanithi             if (dbusError == nullptr)
9245907571dSAsmitha Karunanithi             {
9255907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
9265907571dSAsmitha Karunanithi                 return;
9275907571dSAsmitha Karunanithi             }
9285907571dSAsmitha Karunanithi 
9295907571dSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
9305907571dSAsmitha Karunanithi                              << " and error msg: " << dbusError->message;
9315907571dSAsmitha Karunanithi             if (std::string_view(
9325907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
9335907571dSAsmitha Karunanithi                 dbusError->name)
9345907571dSAsmitha Karunanithi             {
9355907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
9365907571dSAsmitha Karunanithi                 return;
9375907571dSAsmitha Karunanithi             }
9385907571dSAsmitha Karunanithi             if (std::string_view(
9395907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
9405907571dSAsmitha Karunanithi                 dbusError->name)
9415907571dSAsmitha Karunanithi             {
9425907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
9435907571dSAsmitha Karunanithi                 return;
9445907571dSAsmitha Karunanithi             }
9455907571dSAsmitha Karunanithi             if (std::string_view(
9465907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
9475907571dSAsmitha Karunanithi                 dbusError->name)
9485907571dSAsmitha Karunanithi             {
9495907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
9505907571dSAsmitha Karunanithi                 return;
9515907571dSAsmitha Karunanithi             }
9525907571dSAsmitha Karunanithi             // Other Dbus errors such as:
9535907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
9545907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
9555907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
9565907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
9575907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
9585907571dSAsmitha Karunanithi             // back to the client.
959a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
960a43be80fSAsmitha Karunanithi             return;
961a43be80fSAsmitha Karunanithi         }
9628e31778eSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
9638e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
964a43be80fSAsmitha Karunanithi         },
965b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
966b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
967b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
9688e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
969a43be80fSAsmitha Karunanithi }
970a43be80fSAsmitha Karunanithi 
9718d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9728d1b46d7Szhanghch05                       const std::string& dumpType)
97380319af1SAsmitha Karunanithi {
974b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
975b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
9768d1b46d7Szhanghch05 
9770d946211SClaire Weinan     crow::connections::systemBus->async_method_call(
9780d946211SClaire Weinan         [asyncResp](const boost::system::error_code& ec) {
97980319af1SAsmitha Karunanithi         if (ec)
98080319af1SAsmitha Karunanithi         {
9810d946211SClaire Weinan             BMCWEB_LOG_ERROR << "clearDump resp_handler got error " << ec;
98280319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
98380319af1SAsmitha Karunanithi             return;
98480319af1SAsmitha Karunanithi         }
9850d946211SClaire Weinan         },
9860d946211SClaire Weinan         "xyz.openbmc_project.Dump.Manager",
9870d946211SClaire Weinan         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy,
9880d946211SClaire Weinan         "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
98980319af1SAsmitha Karunanithi }
99080319af1SAsmitha Karunanithi 
991b9d36b47SEd Tanous inline static void
992b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
993b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
994b9d36b47SEd Tanous                              std::string& logfile)
995043a0536SJohnathan Mantey {
996d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
997d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
998d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
999d1bde9e5SKrzysztof Grobelny 
1000d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1001d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1002d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1003d1bde9e5SKrzysztof Grobelny 
1004d1bde9e5SKrzysztof Grobelny     if (!success)
1005043a0536SJohnathan Mantey     {
1006d1bde9e5SKrzysztof Grobelny         return;
1007043a0536SJohnathan Mantey     }
1008d1bde9e5SKrzysztof Grobelny 
1009d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1010043a0536SJohnathan Mantey     {
1011d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1012d1bde9e5SKrzysztof Grobelny     }
1013d1bde9e5SKrzysztof Grobelny 
1014d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1015043a0536SJohnathan Mantey     {
1016d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1017043a0536SJohnathan Mantey     }
1018d1bde9e5SKrzysztof Grobelny 
1019d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1020043a0536SJohnathan Mantey     {
1021d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1022043a0536SJohnathan Mantey     }
1023043a0536SJohnathan Mantey }
1024043a0536SJohnathan Mantey 
10257e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
10261da66f75SEd Tanous {
1027c4bf6374SJason M. Bills     /**
1028c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1029c4bf6374SJason M. Bills      */
103022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1031ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1032002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1033002d39b4SEd Tanous             [&app](const crow::Request& req,
103422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
103522d268cbSEd Tanous                    const std::string& systemName) {
10363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1037c4bf6374SJason M. Bills         {
103845ca1b86SEd Tanous             return;
103945ca1b86SEd Tanous         }
104022d268cbSEd Tanous         if (systemName != "system")
104122d268cbSEd Tanous         {
104222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
104322d268cbSEd Tanous                                        systemName);
104422d268cbSEd Tanous             return;
104522d268cbSEd Tanous         }
104622d268cbSEd Tanous 
10477e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
10487e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1049c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1050c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1051c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1052029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
105345ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1054c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1055c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1056002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1057c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
10581476687dSEd Tanous         nlohmann::json::object_t eventLog;
10591476687dSEd Tanous         eventLog["@odata.id"] =
10601476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
10611476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
10625cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
10631476687dSEd Tanous         nlohmann::json::object_t dumpLog;
1064002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
10651476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
1066c9bb6861Sraviteja-b #endif
1067c9bb6861Sraviteja-b 
1068d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
10691476687dSEd Tanous         nlohmann::json::object_t crashdump;
10701476687dSEd Tanous         crashdump["@odata.id"] =
10711476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
10721476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
1073d53dd41fSJason M. Bills #endif
1074b7028ebfSSpencer Ku 
1075b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
10761476687dSEd Tanous         nlohmann::json::object_t hostlogger;
10771476687dSEd Tanous         hostlogger["@odata.id"] =
10781476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
10791476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
1080b7028ebfSSpencer Ku #endif
1081c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1082c4bf6374SJason M. Bills             logServiceArray.size();
1083a3316fc6SZhikuiRen 
10847a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
10857a1dbc48SGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode"};
10867a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
10877a1dbc48SGeorge Liu             "/", 0, interfaces,
10887a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
1089b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1090b9d36b47SEd Tanous                             subtreePath) {
1091a3316fc6SZhikuiRen             if (ec)
1092a3316fc6SZhikuiRen             {
1093a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
1094a3316fc6SZhikuiRen                 return;
1095a3316fc6SZhikuiRen             }
1096a3316fc6SZhikuiRen 
109755f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1098a3316fc6SZhikuiRen             {
1099a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1100a3316fc6SZhikuiRen                 {
110123a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1102a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1103613dabeaSEd Tanous                     nlohmann::json::object_t member;
1104613dabeaSEd Tanous                     member["@odata.id"] =
1105613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
1106613dabeaSEd Tanous 
1107613dabeaSEd Tanous                     logServiceArrayLocal.push_back(std::move(member));
1108613dabeaSEd Tanous 
110945ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
111023a21a1cSEd Tanous                         logServiceArrayLocal.size();
1111a3316fc6SZhikuiRen                     return;
1112a3316fc6SZhikuiRen                 }
1113a3316fc6SZhikuiRen             }
11147a1dbc48SGeorge Liu             });
11157e860f15SJohn Edward Broadbent         });
1116c4bf6374SJason M. Bills }
1117c4bf6374SJason M. Bills 
11187e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1119c4bf6374SJason M. Bills {
112022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1121ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1122002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1123002d39b4SEd Tanous             [&app](const crow::Request& req,
112422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112522d268cbSEd Tanous                    const std::string& systemName) {
11263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
112745ca1b86SEd Tanous         {
112845ca1b86SEd Tanous             return;
112945ca1b86SEd Tanous         }
113022d268cbSEd Tanous         if (systemName != "system")
113122d268cbSEd Tanous         {
113222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
113322d268cbSEd Tanous                                        systemName);
113422d268cbSEd Tanous             return;
113522d268cbSEd Tanous         }
1136c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1137029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1138c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1139c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1140c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1141002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1142c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1143c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
11447c8c4058STejas Patil 
11457c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
11462b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
11477c8c4058STejas Patil 
11487c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
11497c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
11507c8c4058STejas Patil             redfishDateTimeOffset.second;
11517c8c4058STejas Patil 
11521476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
11531476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1154e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1155e7d6c8b2SGunnar Mills 
11560fda0f12SGeorge Liu             {"target",
11570fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
11587e860f15SJohn Edward Broadbent         });
1159489640c6SJason M. Bills }
1160489640c6SJason M. Bills 
11617e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1162489640c6SJason M. Bills {
11634978b63fSJason M. Bills     BMCWEB_ROUTE(
11644978b63fSJason M. Bills         app,
116522d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1166432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
11677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
116845ca1b86SEd Tanous             [&app](const crow::Request& req,
116922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117022d268cbSEd Tanous                    const std::string& systemName) {
11713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117245ca1b86SEd Tanous         {
117345ca1b86SEd Tanous             return;
117445ca1b86SEd Tanous         }
117522d268cbSEd Tanous         if (systemName != "system")
117622d268cbSEd Tanous         {
117722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
117822d268cbSEd Tanous                                        systemName);
117922d268cbSEd Tanous             return;
118022d268cbSEd Tanous         }
1181489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1182489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1183489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1184489640c6SJason M. Bills         {
1185489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1186489640c6SJason M. Bills             {
1187489640c6SJason M. Bills                 std::error_code ec;
1188489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1189489640c6SJason M. Bills             }
1190489640c6SJason M. Bills         }
1191489640c6SJason M. Bills 
1192489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1193489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1194489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1195489640c6SJason M. Bills             if (ec)
1196489640c6SJason M. Bills             {
1197002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1198489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1199489640c6SJason M. Bills                 return;
1200489640c6SJason M. Bills             }
1201489640c6SJason M. Bills 
1202489640c6SJason M. Bills             messages::success(asyncResp->res);
1203489640c6SJason M. Bills             },
1204489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1205002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1206002d39b4SEd Tanous             "replace");
12077e860f15SJohn Edward Broadbent         });
1208c4bf6374SJason M. Bills }
1209c4bf6374SJason M. Bills 
1210ac992cdeSJason M. Bills enum class LogParseError
1211ac992cdeSJason M. Bills {
1212ac992cdeSJason M. Bills     success,
1213ac992cdeSJason M. Bills     parseFailed,
1214ac992cdeSJason M. Bills     messageIdNotInRegistry,
1215ac992cdeSJason M. Bills };
1216ac992cdeSJason M. Bills 
1217ac992cdeSJason M. Bills static LogParseError
1218ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1219b5a76932SEd Tanous                           const std::string& logEntry,
1220de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1221c4bf6374SJason M. Bills {
122295820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1223cd225da8SJason M. Bills     // First get the Timestamp
1224f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1225cd225da8SJason M. Bills     if (space == std::string::npos)
122695820184SJason M. Bills     {
1227ac992cdeSJason M. Bills         return LogParseError::parseFailed;
122895820184SJason M. Bills     }
1229cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1230cd225da8SJason M. Bills     // Then get the log contents
1231f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1232cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1233cd225da8SJason M. Bills     {
1234ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1235cd225da8SJason M. Bills     }
1236cd225da8SJason M. Bills     std::string_view entry(logEntry);
1237cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1238cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1239cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
124050ebd4afSEd Tanous     bmcweb::split(logEntryFields, entry, ',');
1241cd225da8SJason M. Bills     // We need at least a MessageId to be valid
124226f6976fSEd Tanous     if (logEntryFields.empty())
1243cd225da8SJason M. Bills     {
1244ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1245cd225da8SJason M. Bills     }
1246cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
124795820184SJason M. Bills 
12484851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1249fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1250c4bf6374SJason M. Bills 
125154417b02SSui Chen     if (message == nullptr)
1252c4bf6374SJason M. Bills     {
125354417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1254ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1255c4bf6374SJason M. Bills     }
1256c4bf6374SJason M. Bills 
125754417b02SSui Chen     std::string msg = message->message;
125854417b02SSui Chen 
125915a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
126026702d01SEd Tanous     std::span<std::string> messageArgs;
126115a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
126215a86ff6SJason M. Bills     {
126315a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
126415a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
126515a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
126615a86ff6SJason M. Bills         if (!messageArgsStart.empty())
126715a86ff6SJason M. Bills         {
126815a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
126915a86ff6SJason M. Bills         }
127015a86ff6SJason M. Bills 
127123a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1272c4bf6374SJason M. Bills 
12734851d45dSJason M. Bills         // Fill the MessageArgs into the Message
127495820184SJason M. Bills         int i = 0;
127595820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
12764851d45dSJason M. Bills         {
127795820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
12784851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
12794851d45dSJason M. Bills             if (argPos != std::string::npos)
12804851d45dSJason M. Bills             {
128195820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
12824851d45dSJason M. Bills             }
12834851d45dSJason M. Bills         }
128415a86ff6SJason M. Bills     }
12854851d45dSJason M. Bills 
128695820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
128795820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
128895820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1289f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1290f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
129195820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1292c4bf6374SJason M. Bills     {
129395820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1294c4bf6374SJason M. Bills     }
1295c4bf6374SJason M. Bills 
1296c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
12979c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
129884afc48bSJason M. Bills     logEntryJson["@odata.id"] =
129984afc48bSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
130084afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
130184afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
130284afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
130384afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
130484afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
130584afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
130684afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
130784afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1308ac992cdeSJason M. Bills     return LogParseError::success;
1309c4bf6374SJason M. Bills }
1310c4bf6374SJason M. Bills 
13117e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1312c4bf6374SJason M. Bills {
131322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
13148b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1315002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1316002d39b4SEd Tanous             [&app](const crow::Request& req,
131722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
131822d268cbSEd Tanous                    const std::string& systemName) {
1319c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1320c937d2bfSEd Tanous             .canDelegateTop = true,
1321c937d2bfSEd Tanous             .canDelegateSkip = true,
1322c937d2bfSEd Tanous         };
1323c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1324c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
13253ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1326c4bf6374SJason M. Bills         {
1327c4bf6374SJason M. Bills             return;
1328c4bf6374SJason M. Bills         }
132922d268cbSEd Tanous         if (systemName != "system")
133022d268cbSEd Tanous         {
133122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
133222d268cbSEd Tanous                                        systemName);
133322d268cbSEd Tanous             return;
133422d268cbSEd Tanous         }
133522d268cbSEd Tanous 
13365143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
13373648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
13383648c8beSEd Tanous 
13397e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13407e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1341c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1342c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1343c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1344029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1345c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1346c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1347c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1348cb92c03bSAndrew Geissler 
13494978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1350c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
13517e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
13527e860f15SJohn Edward Broadbent         // entry
135395820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
135495820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1355b01bf299SEd Tanous         uint64_t entryCount = 0;
1356cd225da8SJason M. Bills         std::string logEntry;
135795820184SJason M. Bills 
13587e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
13597e860f15SJohn Edward Broadbent         // backwards
1360002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1361002d39b4SEd Tanous              it++)
1362c4bf6374SJason M. Bills         {
1363cd225da8SJason M. Bills             std::ifstream logStream(*it);
136495820184SJason M. Bills             if (!logStream.is_open())
1365c4bf6374SJason M. Bills             {
1366c4bf6374SJason M. Bills                 continue;
1367c4bf6374SJason M. Bills             }
1368c4bf6374SJason M. Bills 
1369e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1370e85d6b16SJason M. Bills             bool firstEntry = true;
137195820184SJason M. Bills             while (std::getline(logStream, logEntry))
137295820184SJason M. Bills             {
1373c4bf6374SJason M. Bills                 std::string idStr;
1374e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1375c4bf6374SJason M. Bills                 {
1376c4bf6374SJason M. Bills                     continue;
1377c4bf6374SJason M. Bills                 }
1378e85d6b16SJason M. Bills                 firstEntry = false;
1379e85d6b16SJason M. Bills 
1380de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1381ac992cdeSJason M. Bills                 LogParseError status =
1382ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1383ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1384ac992cdeSJason M. Bills                 {
1385ac992cdeSJason M. Bills                     continue;
1386ac992cdeSJason M. Bills                 }
1387ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1388c4bf6374SJason M. Bills                 {
1389c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1390c4bf6374SJason M. Bills                     return;
1391c4bf6374SJason M. Bills                 }
1392de703c5dSJason M. Bills 
1393de703c5dSJason M. Bills                 entryCount++;
1394de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1395de703c5dSJason M. Bills                 // start) and top (number of entries to display)
13963648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1397de703c5dSJason M. Bills                 {
1398de703c5dSJason M. Bills                     continue;
1399de703c5dSJason M. Bills                 }
1400de703c5dSJason M. Bills 
1401de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1402c4bf6374SJason M. Bills             }
140395820184SJason M. Bills         }
1404c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
14053648c8beSEd Tanous         if (skip + top < entryCount)
1406c4bf6374SJason M. Bills         {
1407c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
14084978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
14093648c8beSEd Tanous                 std::to_string(skip + top);
1410c4bf6374SJason M. Bills         }
14117e860f15SJohn Edward Broadbent         });
1412897967deSJason M. Bills }
1413897967deSJason M. Bills 
14147e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1415897967deSJason M. Bills {
14167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
141722d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1418ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
14197e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
142045ca1b86SEd Tanous             [&app](const crow::Request& req,
14217e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
142222d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
14233ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
142445ca1b86SEd Tanous         {
142545ca1b86SEd Tanous             return;
142645ca1b86SEd Tanous         }
142722d268cbSEd Tanous 
142822d268cbSEd Tanous         if (systemName != "system")
142922d268cbSEd Tanous         {
143022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
143122d268cbSEd Tanous                                        systemName);
143222d268cbSEd Tanous             return;
143322d268cbSEd Tanous         }
143422d268cbSEd Tanous 
14357e860f15SJohn Edward Broadbent         const std::string& targetID = param;
14368d1b46d7Szhanghch05 
14377e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
14387e860f15SJohn Edward Broadbent         // entry to find the target entry
1439897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1440897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1441897967deSJason M. Bills         std::string logEntry;
1442897967deSJason M. Bills 
14437e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14447e860f15SJohn Edward Broadbent         // backwards
1445002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1446002d39b4SEd Tanous              it++)
1447897967deSJason M. Bills         {
1448897967deSJason M. Bills             std::ifstream logStream(*it);
1449897967deSJason M. Bills             if (!logStream.is_open())
1450897967deSJason M. Bills             {
1451897967deSJason M. Bills                 continue;
1452897967deSJason M. Bills             }
1453897967deSJason M. Bills 
1454897967deSJason M. Bills             // Reset the unique ID on the first entry
1455897967deSJason M. Bills             bool firstEntry = true;
1456897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1457897967deSJason M. Bills             {
1458897967deSJason M. Bills                 std::string idStr;
1459897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1460897967deSJason M. Bills                 {
1461897967deSJason M. Bills                     continue;
1462897967deSJason M. Bills                 }
1463897967deSJason M. Bills                 firstEntry = false;
1464897967deSJason M. Bills 
1465897967deSJason M. Bills                 if (idStr == targetID)
1466897967deSJason M. Bills                 {
1467de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1468ac992cdeSJason M. Bills                     LogParseError status =
1469ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1470ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1471897967deSJason M. Bills                     {
1472897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1473897967deSJason M. Bills                         return;
1474897967deSJason M. Bills                     }
1475d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1476897967deSJason M. Bills                     return;
1477897967deSJason M. Bills                 }
1478897967deSJason M. Bills             }
1479897967deSJason M. Bills         }
1480897967deSJason M. Bills         // Requested ID was not found
14819db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
14827e860f15SJohn Edward Broadbent         });
148308a4e4b5SAnthony Wilson }
148408a4e4b5SAnthony Wilson 
14857e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
148608a4e4b5SAnthony Wilson {
148722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1488ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1489002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1490002d39b4SEd Tanous             [&app](const crow::Request& req,
149122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
149222d268cbSEd Tanous                    const std::string& systemName) {
14933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
149445ca1b86SEd Tanous         {
149545ca1b86SEd Tanous             return;
149645ca1b86SEd Tanous         }
149722d268cbSEd Tanous         if (systemName != "system")
149822d268cbSEd Tanous         {
149922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
150022d268cbSEd Tanous                                        systemName);
150122d268cbSEd Tanous             return;
150222d268cbSEd Tanous         }
150322d268cbSEd Tanous 
15047e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
15057e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
150608a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
150708a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
150808a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
150908a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
151008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
151108a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
151208a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
151308a4e4b5SAnthony Wilson 
1514cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1515cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1516cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1517cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1518914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1519cb92c03bSAndrew Geissler             if (ec)
1520cb92c03bSAndrew Geissler             {
1521cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1522cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1523002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1524cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1525cb92c03bSAndrew Geissler                 return;
1526cb92c03bSAndrew Geissler             }
1527002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1528cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
15299eb808c1SEd Tanous             for (const auto& objectPath : resp)
1530cb92c03bSAndrew Geissler             {
1531914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1532c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1533c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1534914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1535914e2d5dSEd Tanous                 const std::string* message = nullptr;
1536914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
15379c11a172SVijay Lobo                 const std::string* resolution = nullptr;
153875710de2SXiaochao Ma                 bool resolved = false;
15399017faf2SAbhishek Patel                 const std::string* notify = nullptr;
15409017faf2SAbhishek Patel 
15419eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1542f86bb901SAdriana Kobylak                 {
1543f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1544f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1545f86bb901SAdriana Kobylak                     {
1546002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1547cb92c03bSAndrew Geissler                         {
1548cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1549cb92c03bSAndrew Geissler                             {
1550002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1551cb92c03bSAndrew Geissler                             }
1552cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1553cb92c03bSAndrew Geissler                             {
1554002d39b4SEd Tanous                                 timestamp =
1555002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15567e860f15SJohn Edward Broadbent                             }
1557002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
15587e860f15SJohn Edward Broadbent                             {
1559002d39b4SEd Tanous                                 updateTimestamp =
1560002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15617e860f15SJohn Edward Broadbent                             }
15627e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
15637e860f15SJohn Edward Broadbent                             {
15647e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
15657e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15667e860f15SJohn Edward Broadbent                             }
15679c11a172SVijay Lobo                             else if (propertyMap.first == "Resolution")
15689c11a172SVijay Lobo                             {
15699c11a172SVijay Lobo                                 resolution = std::get_if<std::string>(
15709c11a172SVijay Lobo                                     &propertyMap.second);
15719c11a172SVijay Lobo                             }
15727e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
15737e860f15SJohn Edward Broadbent                             {
15747e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
15757e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15767e860f15SJohn Edward Broadbent                             }
15777e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
15787e860f15SJohn Edward Broadbent                             {
1579914e2d5dSEd Tanous                                 const bool* resolveptr =
1580002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
15817e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
15827e860f15SJohn Edward Broadbent                                 {
1583002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
15847e860f15SJohn Edward Broadbent                                     return;
15857e860f15SJohn Edward Broadbent                                 }
15867e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
15877e860f15SJohn Edward Broadbent                             }
15889017faf2SAbhishek Patel                             else if (propertyMap.first ==
15899017faf2SAbhishek Patel                                      "ServiceProviderNotify")
15909017faf2SAbhishek Patel                             {
15919017faf2SAbhishek Patel                                 notify = std::get_if<std::string>(
15929017faf2SAbhishek Patel                                     &propertyMap.second);
15939017faf2SAbhishek Patel                                 if (notify == nullptr)
15949017faf2SAbhishek Patel                                 {
15959017faf2SAbhishek Patel                                     messages::internalError(asyncResp->res);
15969017faf2SAbhishek Patel                                     return;
15979017faf2SAbhishek Patel                                 }
15989017faf2SAbhishek Patel                             }
15997e860f15SJohn Edward Broadbent                         }
16007e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
16017e860f15SJohn Edward Broadbent                             severity == nullptr)
16027e860f15SJohn Edward Broadbent                         {
16037e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
16047e860f15SJohn Edward Broadbent                             return;
16057e860f15SJohn Edward Broadbent                         }
16067e860f15SJohn Edward Broadbent                     }
16077e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
16087e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
16097e860f15SJohn Edward Broadbent                     {
1610002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
16117e860f15SJohn Edward Broadbent                         {
16127e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
16137e860f15SJohn Edward Broadbent                             {
16147e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
16157e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16167e860f15SJohn Edward Broadbent                             }
16177e860f15SJohn Edward Broadbent                         }
16187e860f15SJohn Edward Broadbent                     }
16197e860f15SJohn Edward Broadbent                 }
16207e860f15SJohn Edward Broadbent                 // Object path without the
16217e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
16227e860f15SJohn Edward Broadbent                 // and continue.
16237e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1624c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1625c419c759SEd Tanous                     updateTimestamp == nullptr)
16267e860f15SJohn Edward Broadbent                 {
16277e860f15SJohn Edward Broadbent                     continue;
16287e860f15SJohn Edward Broadbent                 }
16297e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
16307e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
16319c11a172SVijay Lobo                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
16327e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
16330fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
16347e860f15SJohn Edward Broadbent                     std::to_string(*id);
16357e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
16367e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
16377e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
16387e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
16399c11a172SVijay Lobo                 if ((resolution != nullptr) && (!(*resolution).empty()))
16409c11a172SVijay Lobo                 {
16419c11a172SVijay Lobo                     thisEntry["Resolution"] = *resolution;
16429c11a172SVijay Lobo                 }
16439017faf2SAbhishek Patel                 std::optional<bool> notifyAction =
16449017faf2SAbhishek Patel                     getProviderNotifyAction(*notify);
16459017faf2SAbhishek Patel                 if (notifyAction)
16469017faf2SAbhishek Patel                 {
16479017faf2SAbhishek Patel                     thisEntry["ServiceProviderNotified"] = *notifyAction;
16489017faf2SAbhishek Patel                 }
16497e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
16507e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
16517e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
16527e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
16532b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
16547e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
16552b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
16567e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
16577e860f15SJohn Edward Broadbent                 {
16587e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
16590fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
16607e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
16617e860f15SJohn Edward Broadbent                 }
16627e860f15SJohn Edward Broadbent             }
1663002d39b4SEd Tanous             std::sort(
1664002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1665002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
16667e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
16677e860f15SJohn Edward Broadbent                 });
16687e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
16697e860f15SJohn Edward Broadbent                 entriesArray.size();
16707e860f15SJohn Edward Broadbent             },
16717e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
16727e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16737e860f15SJohn Edward Broadbent         });
16747e860f15SJohn Edward Broadbent }
16757e860f15SJohn Edward Broadbent 
16767e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
16777e860f15SJohn Edward Broadbent {
16787e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
167922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1680ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1681002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1682002d39b4SEd Tanous             [&app](const crow::Request& req,
16837e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
168422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
16853ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16867e860f15SJohn Edward Broadbent         {
168745ca1b86SEd Tanous             return;
168845ca1b86SEd Tanous         }
168922d268cbSEd Tanous         if (systemName != "system")
169022d268cbSEd Tanous         {
169122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
169222d268cbSEd Tanous                                        systemName);
169322d268cbSEd Tanous             return;
169422d268cbSEd Tanous         }
169522d268cbSEd Tanous 
16967e860f15SJohn Edward Broadbent         std::string entryID = param;
16977e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
16987e860f15SJohn Edward Broadbent 
16997e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
17007e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1701d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1702d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1703d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
1704002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1705b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
17067e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
17077e860f15SJohn Edward Broadbent             {
1708d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1709d1bde9e5SKrzysztof Grobelny                                            entryID);
17107e860f15SJohn Edward Broadbent                 return;
17117e860f15SJohn Edward Broadbent             }
17127e860f15SJohn Edward Broadbent             if (ec)
17137e860f15SJohn Edward Broadbent             {
17140fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1715002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
17167e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
17177e860f15SJohn Edward Broadbent                 return;
17187e860f15SJohn Edward Broadbent             }
1719914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1720c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1721c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1722914e2d5dSEd Tanous             const std::string* severity = nullptr;
1723914e2d5dSEd Tanous             const std::string* message = nullptr;
1724914e2d5dSEd Tanous             const std::string* filePath = nullptr;
17259c11a172SVijay Lobo             const std::string* resolution = nullptr;
17267e860f15SJohn Edward Broadbent             bool resolved = false;
17279017faf2SAbhishek Patel             const std::string* notify = nullptr;
17287e860f15SJohn Edward Broadbent 
1729d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1730d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1731d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
17329c11a172SVijay Lobo                 severity, "Message", message, "Resolved", resolved,
17339017faf2SAbhishek Patel                 "Resolution", resolution, "Path", filePath,
17349017faf2SAbhishek Patel                 "ServiceProviderNotify", notify);
1735d1bde9e5SKrzysztof Grobelny 
1736d1bde9e5SKrzysztof Grobelny             if (!success)
173775710de2SXiaochao Ma             {
173875710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
173975710de2SXiaochao Ma                 return;
174075710de2SXiaochao Ma             }
1741d1bde9e5SKrzysztof Grobelny 
1742002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
17439017faf2SAbhishek Patel                 timestamp == nullptr || updateTimestamp == nullptr ||
17449017faf2SAbhishek Patel                 notify == nullptr)
1745f86bb901SAdriana Kobylak             {
1746ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1747271584abSEd Tanous                 return;
1748271584abSEd Tanous             }
17499017faf2SAbhishek Patel 
1750f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
17519c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
1752f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
17530fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1754f86bb901SAdriana Kobylak                 std::to_string(*id);
175545ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1756f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1757f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1758f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
17599017faf2SAbhishek Patel             std::optional<bool> notifyAction = getProviderNotifyAction(*notify);
17609017faf2SAbhishek Patel             if (notifyAction)
17619017faf2SAbhishek Patel             {
17629017faf2SAbhishek Patel                 asyncResp->res.jsonValue["ServiceProviderNotified"] =
17639017faf2SAbhishek Patel                     *notifyAction;
17649017faf2SAbhishek Patel             }
17659c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
17669c11a172SVijay Lobo             {
17679c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
17689c11a172SVijay Lobo             }
1769f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1770f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1771f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1772f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
17732b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1774f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
17752b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1776f86bb901SAdriana Kobylak             if (filePath != nullptr)
1777f86bb901SAdriana Kobylak             {
1778f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1779e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1780e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1781f86bb901SAdriana Kobylak             }
1782d1bde9e5SKrzysztof Grobelny             });
17837e860f15SJohn Edward Broadbent         });
1784336e96c6SChicago Duan 
17857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
178622d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1787ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
17887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
178945ca1b86SEd Tanous             [&app](const crow::Request& req,
17907e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
179122d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
17923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179345ca1b86SEd Tanous         {
179445ca1b86SEd Tanous             return;
179545ca1b86SEd Tanous         }
179622d268cbSEd Tanous         if (systemName != "system")
179722d268cbSEd Tanous         {
179822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
179922d268cbSEd Tanous                                        systemName);
180022d268cbSEd Tanous             return;
180122d268cbSEd Tanous         }
180275710de2SXiaochao Ma         std::optional<bool> resolved;
180375710de2SXiaochao Ma 
180415ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
18057e860f15SJohn Edward Broadbent                                       resolved))
180675710de2SXiaochao Ma         {
180775710de2SXiaochao Ma             return;
180875710de2SXiaochao Ma         }
180975710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
181075710de2SXiaochao Ma 
181175710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
18124f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
181375710de2SXiaochao Ma             if (ec)
181475710de2SXiaochao Ma             {
181575710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
181675710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
181775710de2SXiaochao Ma                 return;
181875710de2SXiaochao Ma             }
181975710de2SXiaochao Ma             },
182075710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
182175710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
182275710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
182375710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1824168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
18257e860f15SJohn Edward Broadbent         });
182675710de2SXiaochao Ma 
18277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
182822d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1829ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1830ed398213SEd Tanous 
1831002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1832002d39b4SEd Tanous             [&app](const crow::Request& req,
1833002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
183422d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18353ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1836336e96c6SChicago Duan         {
183745ca1b86SEd Tanous             return;
183845ca1b86SEd Tanous         }
183922d268cbSEd Tanous         if (systemName != "system")
184022d268cbSEd Tanous         {
184122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
184222d268cbSEd Tanous                                        systemName);
184322d268cbSEd Tanous             return;
184422d268cbSEd Tanous         }
1845336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1846336e96c6SChicago Duan 
18477e860f15SJohn Edward Broadbent         std::string entryID = param;
1848336e96c6SChicago Duan 
1849336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1850336e96c6SChicago Duan 
1851336e96c6SChicago Duan         // Process response from Logging service.
1852002d39b4SEd Tanous         auto respHandler =
1853002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1854002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1855336e96c6SChicago Duan             if (ec)
1856336e96c6SChicago Duan             {
18573de8d8baSGeorge Liu                 if (ec.value() == EBADR)
18583de8d8baSGeorge Liu                 {
185945ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
186045ca1b86SEd Tanous                                                entryID);
18613de8d8baSGeorge Liu                     return;
18623de8d8baSGeorge Liu                 }
1863336e96c6SChicago Duan                 // TODO Handle for specific error code
18640fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
18650fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1866336e96c6SChicago Duan                     << ec;
1867336e96c6SChicago Duan                 asyncResp->res.result(
1868336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1869336e96c6SChicago Duan                 return;
1870336e96c6SChicago Duan             }
1871336e96c6SChicago Duan 
1872336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1873336e96c6SChicago Duan         };
1874336e96c6SChicago Duan 
1875336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1876336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1877336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1878336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1879336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
18807e860f15SJohn Edward Broadbent         });
1881400fd1fbSAdriana Kobylak }
1882400fd1fbSAdriana Kobylak 
18837e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1884400fd1fbSAdriana Kobylak {
18850fda0f12SGeorge Liu     BMCWEB_ROUTE(
18860fda0f12SGeorge Liu         app,
188722d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1888ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
18897e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
189045ca1b86SEd Tanous             [&app](const crow::Request& req,
18917e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
189222d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18947e860f15SJohn Edward Broadbent         {
189545ca1b86SEd Tanous             return;
189645ca1b86SEd Tanous         }
189799351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
189899351cd8SEd Tanous                 req.getHeaderValue("Accept"),
18994a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1900400fd1fbSAdriana Kobylak         {
1901002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1902400fd1fbSAdriana Kobylak             return;
1903400fd1fbSAdriana Kobylak         }
190422d268cbSEd Tanous         if (systemName != "system")
190522d268cbSEd Tanous         {
190622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
190722d268cbSEd Tanous                                        systemName);
190822d268cbSEd Tanous             return;
190922d268cbSEd Tanous         }
1910400fd1fbSAdriana Kobylak 
19117e860f15SJohn Edward Broadbent         std::string entryID = param;
1912400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1913400fd1fbSAdriana Kobylak 
1914400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1915002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1916400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1917400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1918400fd1fbSAdriana Kobylak             {
1919002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1920002d39b4SEd Tanous                                            entryID);
1921400fd1fbSAdriana Kobylak                 return;
1922400fd1fbSAdriana Kobylak             }
1923400fd1fbSAdriana Kobylak             if (ec)
1924400fd1fbSAdriana Kobylak             {
1925400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1926400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1927400fd1fbSAdriana Kobylak                 return;
1928400fd1fbSAdriana Kobylak             }
1929400fd1fbSAdriana Kobylak 
1930400fd1fbSAdriana Kobylak             int fd = -1;
1931400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1932400fd1fbSAdriana Kobylak             if (fd == -1)
1933400fd1fbSAdriana Kobylak             {
1934400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1935400fd1fbSAdriana Kobylak                 return;
1936400fd1fbSAdriana Kobylak             }
1937400fd1fbSAdriana Kobylak 
1938400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1939400fd1fbSAdriana Kobylak             if (size == -1)
1940400fd1fbSAdriana Kobylak             {
1941400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1942400fd1fbSAdriana Kobylak                 return;
1943400fd1fbSAdriana Kobylak             }
1944400fd1fbSAdriana Kobylak 
1945400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1946400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1947400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1948400fd1fbSAdriana Kobylak             {
1949002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1950400fd1fbSAdriana Kobylak                                  << maxFileSize;
1951400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1952400fd1fbSAdriana Kobylak                 return;
1953400fd1fbSAdriana Kobylak             }
1954400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1955400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1956400fd1fbSAdriana Kobylak             if (rc == -1)
1957400fd1fbSAdriana Kobylak             {
1958400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1959400fd1fbSAdriana Kobylak                 return;
1960400fd1fbSAdriana Kobylak             }
1961400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1962400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1963400fd1fbSAdriana Kobylak             {
1964400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1965400fd1fbSAdriana Kobylak                 return;
1966400fd1fbSAdriana Kobylak             }
1967400fd1fbSAdriana Kobylak             close(fd);
1968400fd1fbSAdriana Kobylak 
1969400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1970002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1971400fd1fbSAdriana Kobylak 
1972d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1973400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1974d9f6c621SEd Tanous             asyncResp->res.addHeader(
1975d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
1976400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1977400fd1fbSAdriana Kobylak             },
1978400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1979400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1980400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
19817e860f15SJohn Edward Broadbent         });
19821da66f75SEd Tanous }
19831da66f75SEd Tanous 
1984b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1985b7028ebfSSpencer Ku 
1986b7028ebfSSpencer Ku inline bool
1987b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1988b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1989b7028ebfSSpencer Ku {
1990b7028ebfSSpencer Ku     std::error_code ec;
1991b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1992b7028ebfSSpencer Ku     if (ec)
1993b7028ebfSSpencer Ku     {
1994b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1995b7028ebfSSpencer Ku         return false;
1996b7028ebfSSpencer Ku     }
1997b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1998b7028ebfSSpencer Ku     {
1999b7028ebfSSpencer Ku         std::string filename = it.path().filename();
2000b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
2001b7028ebfSSpencer Ku         // path
200211ba3979SEd Tanous         if (filename.starts_with("log"))
2003b7028ebfSSpencer Ku         {
2004b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
2005b7028ebfSSpencer Ku         }
2006b7028ebfSSpencer Ku     }
2007b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
2008b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
2009b7028ebfSSpencer Ku     // descending order.
2010b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
2011b7028ebfSSpencer Ku               AlphanumLess<std::string>());
2012b7028ebfSSpencer Ku 
2013b7028ebfSSpencer Ku     return true;
2014b7028ebfSSpencer Ku }
2015b7028ebfSSpencer Ku 
201602cad96eSEd Tanous inline bool getHostLoggerEntries(
201702cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
201802cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
2019b7028ebfSSpencer Ku {
2020b7028ebfSSpencer Ku     GzFileReader logFile;
2021b7028ebfSSpencer Ku 
2022b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
2023b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
2024b7028ebfSSpencer Ku     {
2025b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2026b7028ebfSSpencer Ku         {
2027b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
2028b7028ebfSSpencer Ku             return false;
2029b7028ebfSSpencer Ku         }
2030b7028ebfSSpencer Ku     }
2031b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2032b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2033b7028ebfSSpencer Ku     if (!lastMessage.empty())
2034b7028ebfSSpencer Ku     {
2035b7028ebfSSpencer Ku         logCount++;
2036b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2037b7028ebfSSpencer Ku         {
2038b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2039b7028ebfSSpencer Ku         }
2040b7028ebfSSpencer Ku     }
2041b7028ebfSSpencer Ku     return true;
2042b7028ebfSSpencer Ku }
2043b7028ebfSSpencer Ku 
2044b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2045b7028ebfSSpencer Ku                                     const std::string& msg,
20466d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2047b7028ebfSSpencer Ku {
2048b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
20499c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
20506d6574c9SJason M. Bills     logEntryJson["@odata.id"] =
2051b7028ebfSSpencer Ku         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
20526d6574c9SJason M. Bills         logEntryID;
20536d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
20546d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
20556d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
20566d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
20576d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
20586d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2059b7028ebfSSpencer Ku }
2060b7028ebfSSpencer Ku 
2061b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2062b7028ebfSSpencer Ku {
206322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2064b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
20651476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
20661476687dSEd Tanous             [&app](const crow::Request& req,
206722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
206822d268cbSEd Tanous                    const std::string& systemName) {
20693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207045ca1b86SEd Tanous         {
207145ca1b86SEd Tanous             return;
207245ca1b86SEd Tanous         }
207322d268cbSEd Tanous         if (systemName != "system")
207422d268cbSEd Tanous         {
207522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
207622d268cbSEd Tanous                                        systemName);
207722d268cbSEd Tanous             return;
207822d268cbSEd Tanous         }
2079b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2080b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
2081b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2082b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
2083b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2084b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2085b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
20861476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
20871476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2088b7028ebfSSpencer Ku         });
2089b7028ebfSSpencer Ku }
2090b7028ebfSSpencer Ku 
2091b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2092b7028ebfSSpencer Ku {
2093b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
209422d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2095b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2096002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2097002d39b4SEd Tanous             [&app](const crow::Request& req,
209822d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
209922d268cbSEd Tanous                    const std::string& systemName) {
2100c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2101c937d2bfSEd Tanous             .canDelegateTop = true,
2102c937d2bfSEd Tanous             .canDelegateSkip = true,
2103c937d2bfSEd Tanous         };
2104c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2105c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21063ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2107b7028ebfSSpencer Ku         {
2108b7028ebfSSpencer Ku             return;
2109b7028ebfSSpencer Ku         }
211022d268cbSEd Tanous         if (systemName != "system")
211122d268cbSEd Tanous         {
211222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
211322d268cbSEd Tanous                                        systemName);
211422d268cbSEd Tanous             return;
211522d268cbSEd Tanous         }
2116b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2117b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2118b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2119b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2120b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2121b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2122b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
21230fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2124b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2125b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2126b7028ebfSSpencer Ku 
2127b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2128b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2129b7028ebfSSpencer Ku         {
2130b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2131b7028ebfSSpencer Ku             return;
2132b7028ebfSSpencer Ku         }
21333648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
21343648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21355143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2136b7028ebfSSpencer Ku         size_t logCount = 0;
2137b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2138b7028ebfSSpencer Ku         // control by skip and top.
2139b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
21403648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
21413648c8beSEd Tanous                                   logCount))
2142b7028ebfSSpencer Ku         {
2143b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2144b7028ebfSSpencer Ku             return;
2145b7028ebfSSpencer Ku         }
2146b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2147b7028ebfSSpencer Ku         // log count
214826f6976fSEd Tanous         if (logEntries.empty())
2149b7028ebfSSpencer Ku         {
2150b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2151b7028ebfSSpencer Ku             return;
2152b7028ebfSSpencer Ku         }
215326f6976fSEd Tanous         if (!logEntries.empty())
2154b7028ebfSSpencer Ku         {
2155b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2156b7028ebfSSpencer Ku             {
21576d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
21583648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
21593648c8beSEd Tanous                                         hostLogEntry);
21606d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
2161b7028ebfSSpencer Ku             }
2162b7028ebfSSpencer Ku 
2163b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
21643648c8beSEd Tanous             if (skip + top < logCount)
2165b7028ebfSSpencer Ku             {
2166b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
21670fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
21683648c8beSEd Tanous                     std::to_string(skip + top);
2169b7028ebfSSpencer Ku             }
2170b7028ebfSSpencer Ku         }
2171b7028ebfSSpencer Ku         });
2172b7028ebfSSpencer Ku }
2173b7028ebfSSpencer Ku 
2174b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2175b7028ebfSSpencer Ku {
2176b7028ebfSSpencer Ku     BMCWEB_ROUTE(
217722d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2178b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2179b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
218045ca1b86SEd Tanous             [&app](const crow::Request& req,
2181b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218222d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
21833ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
218445ca1b86SEd Tanous         {
218545ca1b86SEd Tanous             return;
218645ca1b86SEd Tanous         }
218722d268cbSEd Tanous         if (systemName != "system")
218822d268cbSEd Tanous         {
218922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
219022d268cbSEd Tanous                                        systemName);
219122d268cbSEd Tanous             return;
219222d268cbSEd Tanous         }
2193b7028ebfSSpencer Ku         const std::string& targetID = param;
2194b7028ebfSSpencer Ku 
2195b7028ebfSSpencer Ku         uint64_t idInt = 0;
2196ca45aa3cSEd Tanous 
2197ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2198ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
2199ca45aa3cSEd Tanous 
2200ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
22019db4ba25SJiaqing Zhao         if (ec == std::errc::invalid_argument ||
22029db4ba25SJiaqing Zhao             ec == std::errc::result_out_of_range)
2203b7028ebfSSpencer Ku         {
22049db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2205b7028ebfSSpencer Ku             return;
2206b7028ebfSSpencer Ku         }
2207b7028ebfSSpencer Ku 
2208b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2209b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2210b7028ebfSSpencer Ku         {
2211b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2212b7028ebfSSpencer Ku             return;
2213b7028ebfSSpencer Ku         }
2214b7028ebfSSpencer Ku 
2215b7028ebfSSpencer Ku         size_t logCount = 0;
22163648c8beSEd Tanous         size_t top = 1;
2217b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2218b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2219b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2220b7028ebfSSpencer Ku         // get that entry
2221002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2222002d39b4SEd Tanous                                   logCount))
2223b7028ebfSSpencer Ku         {
2224b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2225b7028ebfSSpencer Ku             return;
2226b7028ebfSSpencer Ku         }
2227b7028ebfSSpencer Ku 
2228b7028ebfSSpencer Ku         if (!logEntries.empty())
2229b7028ebfSSpencer Ku         {
22306d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
22316d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
22326d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2233b7028ebfSSpencer Ku             return;
2234b7028ebfSSpencer Ku         }
2235b7028ebfSSpencer Ku 
2236b7028ebfSSpencer Ku         // Requested ID was not found
22379db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2238b7028ebfSSpencer Ku         });
2239b7028ebfSSpencer Ku }
2240b7028ebfSSpencer Ku 
2241dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2242fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2243fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22441da66f75SEd Tanous {
22453ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
224645ca1b86SEd Tanous     {
224745ca1b86SEd Tanous         return;
224845ca1b86SEd Tanous     }
22497e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
22507e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2251e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
22521da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2253e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2254e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2255002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2256e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
22571da66f75SEd Tanous         "Collection of LogServices for this Manager";
2258002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2259c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2260fdd26906SClaire Weinan 
2261c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2262613dabeaSEd Tanous     nlohmann::json::object_t journal;
2263613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2264613dabeaSEd Tanous     logServiceArray.push_back(std::move(journal));
2265c4bf6374SJason M. Bills #endif
2266fdd26906SClaire Weinan 
2267fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2268fdd26906SClaire Weinan 
2269fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
227015912159SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
22717a1dbc48SGeorge Liu         "xyz.openbmc_project.Collection.DeleteAll"};
22727a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
22737a1dbc48SGeorge Liu         "/xyz/openbmc_project/dump", 0, interfaces,
2274fdd26906SClaire Weinan         [asyncResp](
22757a1dbc48SGeorge Liu             const boost::system::error_code& ec,
2276fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2277fdd26906SClaire Weinan         if (ec)
2278fdd26906SClaire Weinan         {
2279fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2280dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2281fdd26906SClaire Weinan                 << ec;
2282fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2283fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2284fdd26906SClaire Weinan             return;
2285fdd26906SClaire Weinan         }
2286fdd26906SClaire Weinan 
2287fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2288fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2289fdd26906SClaire Weinan 
2290fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2291fdd26906SClaire Weinan         {
2292fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2293fdd26906SClaire Weinan             {
2294613dabeaSEd Tanous                 nlohmann::json::object_t member;
2295613dabeaSEd Tanous                 member["@odata.id"] =
2296613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2297613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2298fdd26906SClaire Weinan             }
2299fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2300fdd26906SClaire Weinan             {
2301613dabeaSEd Tanous                 nlohmann::json::object_t member;
2302613dabeaSEd Tanous                 member["@odata.id"] =
2303613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2304613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2305fdd26906SClaire Weinan             }
2306fdd26906SClaire Weinan         }
2307fdd26906SClaire Weinan 
2308e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2309fdd26906SClaire Weinan             logServiceArrayLocal.size();
23107a1dbc48SGeorge Liu         });
2311fdd26906SClaire Weinan #endif
2312fdd26906SClaire Weinan }
2313fdd26906SClaire Weinan 
2314fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2315fdd26906SClaire Weinan {
2316fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2317fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2318fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2319dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2320e1f26343SJason M. Bills }
2321e1f26343SJason M. Bills 
23227e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2323e1f26343SJason M. Bills {
23247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2325ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
232745ca1b86SEd Tanous             [&app](const crow::Request& req,
232845ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
23307e860f15SJohn Edward Broadbent         {
233145ca1b86SEd Tanous             return;
233245ca1b86SEd Tanous         }
2333e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2334e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
23350f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
23360f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2337002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2338002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2339ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "Journal";
2340e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23417c8c4058STejas Patil 
23427c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
23432b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2344002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23457c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23467c8c4058STejas Patil             redfishDateTimeOffset.second;
23477c8c4058STejas Patil 
23481476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
23491476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
23507e860f15SJohn Edward Broadbent         });
2351e1f26343SJason M. Bills }
2352e1f26343SJason M. Bills 
23533a48b3a2SJason M. Bills static int
23543a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2355e1f26343SJason M. Bills                                sd_journal* journal,
23563a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2357e1f26343SJason M. Bills {
2358e1f26343SJason M. Bills     // Get the Log Entry contents
2359e1f26343SJason M. Bills     int ret = 0;
2360e1f26343SJason M. Bills 
2361a8fe54f0SJason M. Bills     std::string message;
2362a8fe54f0SJason M. Bills     std::string_view syslogID;
2363a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2364a8fe54f0SJason M. Bills     if (ret < 0)
2365a8fe54f0SJason M. Bills     {
2366a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2367a8fe54f0SJason M. Bills                          << strerror(-ret);
2368a8fe54f0SJason M. Bills     }
2369a8fe54f0SJason M. Bills     if (!syslogID.empty())
2370a8fe54f0SJason M. Bills     {
2371a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2372a8fe54f0SJason M. Bills     }
2373a8fe54f0SJason M. Bills 
237439e77504SEd Tanous     std::string_view msg;
237516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2376e1f26343SJason M. Bills     if (ret < 0)
2377e1f26343SJason M. Bills     {
2378e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2379e1f26343SJason M. Bills         return 1;
2380e1f26343SJason M. Bills     }
2381a8fe54f0SJason M. Bills     message += std::string(msg);
2382e1f26343SJason M. Bills 
2383e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2384271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
238516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2386e1f26343SJason M. Bills     if (ret < 0)
2387e1f26343SJason M. Bills     {
2388e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2389e1f26343SJason M. Bills     }
2390e1f26343SJason M. Bills 
2391e1f26343SJason M. Bills     // Get the Created time from the timestamp
239216428a1aSJason M. Bills     std::string entryTimeStr;
239316428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2394e1f26343SJason M. Bills     {
239516428a1aSJason M. Bills         return 1;
2396e1f26343SJason M. Bills     }
2397e1f26343SJason M. Bills 
2398e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
23999c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
240084afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.id"] =
240184afc48bSJason M. Bills         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
240284afc48bSJason M. Bills         bmcJournalLogEntryID;
240384afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
240484afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
240584afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
240684afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
240784afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2408738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
240984afc48bSJason M. Bills                                                          : "OK";
241084afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
241184afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2412e1f26343SJason M. Bills     return 0;
2413e1f26343SJason M. Bills }
2414e1f26343SJason M. Bills 
24157e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2416e1f26343SJason M. Bills {
24177e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2418ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2419002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2420002d39b4SEd Tanous             [&app](const crow::Request& req,
2421002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2422c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2423c937d2bfSEd Tanous             .canDelegateTop = true,
2424c937d2bfSEd Tanous             .canDelegateSkip = true,
2425c937d2bfSEd Tanous         };
2426c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2427c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
24283ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2429193ad2faSJason M. Bills         {
2430193ad2faSJason M. Bills             return;
2431193ad2faSJason M. Bills         }
24323648c8beSEd Tanous 
24333648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
24345143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
24353648c8beSEd Tanous 
24367e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
24377e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2438e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2439e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
24400f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
24410f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2442e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2443e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2444e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
24450fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2446e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2447e1f26343SJason M. Bills 
24487e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
24497e860f15SJohn Edward Broadbent         // unique ID for each entry
2450e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2451e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2452e1f26343SJason M. Bills         if (ret < 0)
2453e1f26343SJason M. Bills         {
2454002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2455f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2456e1f26343SJason M. Bills             return;
2457e1f26343SJason M. Bills         }
24580fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
24590fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2460e1f26343SJason M. Bills         journalTmp = nullptr;
2461b01bf299SEd Tanous         uint64_t entryCount = 0;
2462e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2463e85d6b16SJason M. Bills         bool firstEntry = true;
2464e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2465e1f26343SJason M. Bills         {
2466193ad2faSJason M. Bills             entryCount++;
24677e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
24687e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
24693648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2470193ad2faSJason M. Bills             {
2471193ad2faSJason M. Bills                 continue;
2472193ad2faSJason M. Bills             }
2473193ad2faSJason M. Bills 
247416428a1aSJason M. Bills             std::string idStr;
2475e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2476e1f26343SJason M. Bills             {
2477e1f26343SJason M. Bills                 continue;
2478e1f26343SJason M. Bills             }
2479e85d6b16SJason M. Bills             firstEntry = false;
2480e85d6b16SJason M. Bills 
24813a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2482c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2483c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2484e1f26343SJason M. Bills             {
2485f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2486e1f26343SJason M. Bills                 return;
2487e1f26343SJason M. Bills             }
24883a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2489e1f26343SJason M. Bills         }
2490193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
24913648c8beSEd Tanous         if (skip + top < entryCount)
2492193ad2faSJason M. Bills         {
2493193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
24940fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
24953648c8beSEd Tanous                 std::to_string(skip + top);
2496193ad2faSJason M. Bills         }
24977e860f15SJohn Edward Broadbent         });
2498e1f26343SJason M. Bills }
2499e1f26343SJason M. Bills 
25007e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2501e1f26343SJason M. Bills {
25027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
25037e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2504ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
25057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
250645ca1b86SEd Tanous             [&app](const crow::Request& req,
25077e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25087e860f15SJohn Edward Broadbent                    const std::string& entryID) {
25093ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
251045ca1b86SEd Tanous         {
251145ca1b86SEd Tanous             return;
251245ca1b86SEd Tanous         }
2513e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2514e1f26343SJason M. Bills         uint64_t ts = 0;
2515271584abSEd Tanous         uint64_t index = 0;
25168d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2517e1f26343SJason M. Bills         {
251816428a1aSJason M. Bills             return;
2519e1f26343SJason M. Bills         }
2520e1f26343SJason M. Bills 
2521e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2522e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2523e1f26343SJason M. Bills         if (ret < 0)
2524e1f26343SJason M. Bills         {
2525002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2526f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2527e1f26343SJason M. Bills             return;
2528e1f26343SJason M. Bills         }
2529002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2530002d39b4SEd Tanous             journalTmp, sd_journal_close);
2531e1f26343SJason M. Bills         journalTmp = nullptr;
25327e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
25337e860f15SJohn Edward Broadbent         // index tracking the unique ID
2534af07e3f5SJason M. Bills         std::string idStr;
2535af07e3f5SJason M. Bills         bool firstEntry = true;
2536e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
25372056b6d1SManojkiran Eda         if (ret < 0)
25382056b6d1SManojkiran Eda         {
25392056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
25402056b6d1SManojkiran Eda                              << strerror(-ret);
25412056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
25422056b6d1SManojkiran Eda             return;
25432056b6d1SManojkiran Eda         }
2544271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2545e1f26343SJason M. Bills         {
2546e1f26343SJason M. Bills             sd_journal_next(journal.get());
2547af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2548af07e3f5SJason M. Bills             {
2549af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2550af07e3f5SJason M. Bills                 return;
2551af07e3f5SJason M. Bills             }
2552af07e3f5SJason M. Bills             firstEntry = false;
2553af07e3f5SJason M. Bills         }
2554c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2555af07e3f5SJason M. Bills         if (idStr != entryID)
2556c4bf6374SJason M. Bills         {
25579db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
2558c4bf6374SJason M. Bills             return;
2559c4bf6374SJason M. Bills         }
2560c4bf6374SJason M. Bills 
25613a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2562c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
25633a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2564e1f26343SJason M. Bills         {
2565f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2566e1f26343SJason M. Bills             return;
2567e1f26343SJason M. Bills         }
2568d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
25697e860f15SJohn Edward Broadbent         });
2570c9bb6861Sraviteja-b }
2571c9bb6861Sraviteja-b 
2572fdd26906SClaire Weinan inline void
2573fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2574fdd26906SClaire Weinan                        const std::string& dumpType)
2575c9bb6861Sraviteja-b {
2576fdd26906SClaire Weinan     std::string dumpPath;
2577fdd26906SClaire Weinan     std::string overWritePolicy;
2578fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2579fdd26906SClaire Weinan 
2580fdd26906SClaire Weinan     if (dumpType == "BMC")
258145ca1b86SEd Tanous     {
2582fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2583fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2584fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2585fdd26906SClaire Weinan     }
2586fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2587fdd26906SClaire Weinan     {
2588fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2589fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2590fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2591fdd26906SClaire Weinan     }
2592fdd26906SClaire Weinan     else if (dumpType == "System")
2593fdd26906SClaire Weinan     {
2594fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2595fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2596fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2597fdd26906SClaire Weinan     }
2598fdd26906SClaire Weinan     else
2599fdd26906SClaire Weinan     {
2600fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2601fdd26906SClaire Weinan                          << dumpType;
2602fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
260345ca1b86SEd Tanous         return;
260445ca1b86SEd Tanous     }
2605fdd26906SClaire Weinan 
2606fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2607fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2608c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2609fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2610fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2611fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
26127c8c4058STejas Patil 
26137c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
26142b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
26150fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26167c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26177c8c4058STejas Patil         redfishDateTimeOffset.second;
26187c8c4058STejas Patil 
2619fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2620fdd26906SClaire Weinan 
2621fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2622fdd26906SClaire Weinan     {
2623002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26241476687dSEd Tanous                                 ["target"] =
2625fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2626fdd26906SClaire Weinan     }
26270d946211SClaire Weinan 
26280d946211SClaire Weinan     constexpr std::array<std::string_view, 1> interfaces = {deleteAllInterface};
26290d946211SClaire Weinan     dbus::utility::getSubTreePaths(
26300d946211SClaire Weinan         "/xyz/openbmc_project/dump", 0, interfaces,
26310d946211SClaire Weinan         [asyncResp, dumpType, dumpPath](
26320d946211SClaire Weinan             const boost::system::error_code& ec,
26330d946211SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
26340d946211SClaire Weinan         if (ec)
26350d946211SClaire Weinan         {
26360d946211SClaire Weinan             BMCWEB_LOG_ERROR << "getDumpServiceInfo respHandler got error "
26370d946211SClaire Weinan                              << ec;
26380d946211SClaire Weinan             // Assume that getting an error simply means there are no dump
26390d946211SClaire Weinan             // LogServices. Return without adding any error response.
26400d946211SClaire Weinan             return;
26410d946211SClaire Weinan         }
26420d946211SClaire Weinan 
26430d946211SClaire Weinan         const std::string dbusDumpPath =
26440d946211SClaire Weinan             "/xyz/openbmc_project/dump/" +
26450d946211SClaire Weinan             boost::algorithm::to_lower_copy(dumpType);
26460d946211SClaire Weinan 
26470d946211SClaire Weinan         for (const std::string& path : subTreePaths)
26480d946211SClaire Weinan         {
26490d946211SClaire Weinan             if (path == dbusDumpPath)
26500d946211SClaire Weinan             {
26510d946211SClaire Weinan                 asyncResp->res
26520d946211SClaire Weinan                     .jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
26530d946211SClaire Weinan                     dumpPath + "/Actions/LogService.ClearLog";
26540d946211SClaire Weinan                 break;
26550d946211SClaire Weinan             }
26560d946211SClaire Weinan         }
26570d946211SClaire Weinan         });
2658c9bb6861Sraviteja-b }
2659c9bb6861Sraviteja-b 
2660fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2661fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2662fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26637e860f15SJohn Edward Broadbent {
26643ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266545ca1b86SEd Tanous     {
266645ca1b86SEd Tanous         return;
266745ca1b86SEd Tanous     }
2668fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2669fdd26906SClaire Weinan }
2670c9bb6861Sraviteja-b 
267122d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
267222d268cbSEd Tanous     crow::App& app, const crow::Request& req,
267322d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
267422d268cbSEd Tanous     const std::string& chassisId)
267522d268cbSEd Tanous {
267622d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
267722d268cbSEd Tanous     {
267822d268cbSEd Tanous         return;
267922d268cbSEd Tanous     }
268022d268cbSEd Tanous     if (chassisId != "system")
268122d268cbSEd Tanous     {
268222d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
268322d268cbSEd Tanous         return;
268422d268cbSEd Tanous     }
268522d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
268622d268cbSEd Tanous }
268722d268cbSEd Tanous 
2688fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2689fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2690fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2691fdd26906SClaire Weinan {
2692fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2693fdd26906SClaire Weinan     {
2694fdd26906SClaire Weinan         return;
2695fdd26906SClaire Weinan     }
2696fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2697fdd26906SClaire Weinan }
2698fdd26906SClaire Weinan 
269922d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
270022d268cbSEd Tanous     crow::App& app, const crow::Request& req,
270122d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
270222d268cbSEd Tanous     const std::string& chassisId)
270322d268cbSEd Tanous {
270422d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
270522d268cbSEd Tanous     {
270622d268cbSEd Tanous         return;
270722d268cbSEd Tanous     }
270822d268cbSEd Tanous     if (chassisId != "system")
270922d268cbSEd Tanous     {
271022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
271122d268cbSEd Tanous         return;
271222d268cbSEd Tanous     }
271322d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
271422d268cbSEd Tanous }
271522d268cbSEd Tanous 
2716fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2717fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2718fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2719fdd26906SClaire Weinan     const std::string& dumpId)
2720fdd26906SClaire Weinan {
2721fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2722fdd26906SClaire Weinan     {
2723fdd26906SClaire Weinan         return;
2724fdd26906SClaire Weinan     }
2725fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2726fdd26906SClaire Weinan }
272722d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
272822d268cbSEd Tanous     crow::App& app, const crow::Request& req,
272922d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
273022d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
273122d268cbSEd Tanous {
273222d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
273322d268cbSEd Tanous     {
273422d268cbSEd Tanous         return;
273522d268cbSEd Tanous     }
273622d268cbSEd Tanous     if (chassisId != "system")
273722d268cbSEd Tanous     {
273822d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
273922d268cbSEd Tanous         return;
274022d268cbSEd Tanous     }
274122d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
274222d268cbSEd Tanous }
2743fdd26906SClaire Weinan 
2744fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2745fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2746fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2747fdd26906SClaire Weinan     const std::string& dumpId)
2748fdd26906SClaire Weinan {
2749fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2750fdd26906SClaire Weinan     {
2751fdd26906SClaire Weinan         return;
2752fdd26906SClaire Weinan     }
2753fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2754fdd26906SClaire Weinan }
2755fdd26906SClaire Weinan 
275622d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
275722d268cbSEd Tanous     crow::App& app, const crow::Request& req,
275822d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
275922d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
276022d268cbSEd Tanous {
276122d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
276222d268cbSEd Tanous     {
276322d268cbSEd Tanous         return;
276422d268cbSEd Tanous     }
276522d268cbSEd Tanous     if (chassisId != "system")
276622d268cbSEd Tanous     {
276722d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
276822d268cbSEd Tanous         return;
276922d268cbSEd Tanous     }
277022d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
277122d268cbSEd Tanous }
277222d268cbSEd Tanous 
2773fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2774fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2775fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2776fdd26906SClaire Weinan {
2777fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2778fdd26906SClaire Weinan     {
2779fdd26906SClaire Weinan         return;
2780fdd26906SClaire Weinan     }
2781fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2782fdd26906SClaire Weinan }
2783fdd26906SClaire Weinan 
278422d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
278522d268cbSEd Tanous     crow::App& app, const crow::Request& req,
278622d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
278722d268cbSEd Tanous     const std::string& chassisId)
278822d268cbSEd Tanous {
278922d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
279022d268cbSEd Tanous     {
279122d268cbSEd Tanous         return;
279222d268cbSEd Tanous     }
279322d268cbSEd Tanous     if (chassisId != "system")
279422d268cbSEd Tanous     {
279522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
279622d268cbSEd Tanous         return;
279722d268cbSEd Tanous     }
279822d268cbSEd Tanous     createDump(asyncResp, req, "System");
279922d268cbSEd Tanous }
280022d268cbSEd Tanous 
2801fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2802fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2803fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2804fdd26906SClaire Weinan {
2805fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2806fdd26906SClaire Weinan     {
2807fdd26906SClaire Weinan         return;
2808fdd26906SClaire Weinan     }
2809fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2810fdd26906SClaire Weinan }
2811fdd26906SClaire Weinan 
281222d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
281322d268cbSEd Tanous     crow::App& app, const crow::Request& req,
281422d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
281522d268cbSEd Tanous     const std::string& chassisId)
281622d268cbSEd Tanous {
281722d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
281822d268cbSEd Tanous     {
281922d268cbSEd Tanous         return;
282022d268cbSEd Tanous     }
282122d268cbSEd Tanous     if (chassisId != "system")
282222d268cbSEd Tanous     {
282322d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
282422d268cbSEd Tanous         return;
282522d268cbSEd Tanous     }
282622d268cbSEd Tanous     clearDump(asyncResp, "System");
282722d268cbSEd Tanous }
282822d268cbSEd Tanous 
2829fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2830fdd26906SClaire Weinan {
2831fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2832fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2833fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2834fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2835fdd26906SClaire Weinan }
2836fdd26906SClaire Weinan 
2837fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2838fdd26906SClaire Weinan {
2839fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2840fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2841fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2842fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2843c9bb6861Sraviteja-b }
2844c9bb6861Sraviteja-b 
28457e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2846c9bb6861Sraviteja-b {
28477e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28487e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2849ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2850fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2851fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2852fdd26906SClaire Weinan 
28537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28547e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2855ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2856fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2857fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2858c9bb6861Sraviteja-b }
2859c9bb6861Sraviteja-b 
28607e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2861c9bb6861Sraviteja-b {
28620fda0f12SGeorge Liu     BMCWEB_ROUTE(
28630fda0f12SGeorge Liu         app,
28640fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2865ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
28667e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2867fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2868fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2869a43be80fSAsmitha Karunanithi }
2870a43be80fSAsmitha Karunanithi 
28717e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
287280319af1SAsmitha Karunanithi {
28730fda0f12SGeorge Liu     BMCWEB_ROUTE(
28740fda0f12SGeorge Liu         app,
28750fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2876ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2877fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2878fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
287945ca1b86SEd Tanous }
2880fdd26906SClaire Weinan 
2881fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2882fdd26906SClaire Weinan {
2883fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2884fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2885fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2886fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2887fdd26906SClaire Weinan }
2888fdd26906SClaire Weinan 
2889fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2890fdd26906SClaire Weinan {
2891fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2892fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2893fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2894fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2895fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2896fdd26906SClaire Weinan }
2897fdd26906SClaire Weinan 
2898fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2899fdd26906SClaire Weinan {
2900fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2901fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2902fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2903fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2904fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2905fdd26906SClaire Weinan 
2906fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2907fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2908fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2909fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2910fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2911fdd26906SClaire Weinan }
2912fdd26906SClaire Weinan 
2913fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2914fdd26906SClaire Weinan {
2915fdd26906SClaire Weinan     BMCWEB_ROUTE(
2916fdd26906SClaire Weinan         app,
2917fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2918fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2919fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2920fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
29215cb1dd27SAsmitha Karunanithi }
29225cb1dd27SAsmitha Karunanithi 
29237e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
29245cb1dd27SAsmitha Karunanithi {
292522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2926ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
29276ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
292822d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
29295cb1dd27SAsmitha Karunanithi }
29305cb1dd27SAsmitha Karunanithi 
29317e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
29327e860f15SJohn Edward Broadbent {
293322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2934ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
293522d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
293622d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
293722d268cbSEd Tanous             std::ref(app)));
29385cb1dd27SAsmitha Karunanithi }
29395cb1dd27SAsmitha Karunanithi 
29407e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
29415cb1dd27SAsmitha Karunanithi {
29427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
294322d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2944ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29456ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
294622d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
29478d1b46d7Szhanghch05 
29487e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
294922d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2950ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
29516ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
295222d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
29535cb1dd27SAsmitha Karunanithi }
2954c9bb6861Sraviteja-b 
29557e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2956c9bb6861Sraviteja-b {
29570fda0f12SGeorge Liu     BMCWEB_ROUTE(
29580fda0f12SGeorge Liu         app,
295922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2960ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
296122d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
296222d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
296322d268cbSEd Tanous             std::ref(app)));
2964a43be80fSAsmitha Karunanithi }
2965a43be80fSAsmitha Karunanithi 
29667e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2967a43be80fSAsmitha Karunanithi {
29680fda0f12SGeorge Liu     BMCWEB_ROUTE(
29690fda0f12SGeorge Liu         app,
297022d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2971ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29726ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
297322d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2974013487e5Sraviteja-b }
2975013487e5Sraviteja-b 
29767e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
29771da66f75SEd Tanous {
29783946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29793946028dSAppaRao Puli     // method for security reasons.
29801da66f75SEd Tanous     /**
29811da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
29821da66f75SEd Tanous      */
298322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2984ed398213SEd Tanous         // This is incorrect, should be:
2985ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2986432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2987002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2988002d39b4SEd Tanous             [&app](const crow::Request& req,
298922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
299022d268cbSEd Tanous                    const std::string& systemName) {
29913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
299245ca1b86SEd Tanous         {
299345ca1b86SEd Tanous             return;
299445ca1b86SEd Tanous         }
299522d268cbSEd Tanous         if (systemName != "system")
299622d268cbSEd Tanous         {
299722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
299822d268cbSEd Tanous                                        systemName);
299922d268cbSEd Tanous             return;
300022d268cbSEd Tanous         }
300122d268cbSEd Tanous 
30027e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
30037e860f15SJohn Edward Broadbent         // SubRoute
30040f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
3005424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
3006e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
30078e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
30084f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
30094f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
30104f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
3011e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
3012e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
30137c8c4058STejas Patil 
30147c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
30152b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
30167c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30177c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30187c8c4058STejas Patil             redfishDateTimeOffset.second;
30197c8c4058STejas Patil 
30201476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
30211476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3022002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
30231476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
3024002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
30251476687dSEd Tanous                                 ["target"] =
30261476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
30277e860f15SJohn Edward Broadbent         });
30281da66f75SEd Tanous }
30291da66f75SEd Tanous 
30307e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
30315b61b5e8SJason M. Bills {
30320fda0f12SGeorge Liu     BMCWEB_ROUTE(
30330fda0f12SGeorge Liu         app,
303422d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
3035ed398213SEd Tanous         // This is incorrect, should be:
3036ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3037432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30387e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
303945ca1b86SEd Tanous             [&app](const crow::Request& req,
304022d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
304122d268cbSEd Tanous                    const std::string& systemName) {
30423ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
304345ca1b86SEd Tanous         {
304445ca1b86SEd Tanous             return;
304545ca1b86SEd Tanous         }
304622d268cbSEd Tanous         if (systemName != "system")
304722d268cbSEd Tanous         {
304822d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
304922d268cbSEd Tanous                                        systemName);
305022d268cbSEd Tanous             return;
305122d268cbSEd Tanous         }
30525b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
30535b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
3054cb13a392SEd Tanous                         const std::string&) {
30555b61b5e8SJason M. Bills             if (ec)
30565b61b5e8SJason M. Bills             {
30575b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
30585b61b5e8SJason M. Bills                 return;
30595b61b5e8SJason M. Bills             }
30605b61b5e8SJason M. Bills             messages::success(asyncResp->res);
30615b61b5e8SJason M. Bills             },
3062002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
30637e860f15SJohn Edward Broadbent         });
30645b61b5e8SJason M. Bills }
30655b61b5e8SJason M. Bills 
30668d1b46d7Szhanghch05 static void
30678d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30688d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3069e855dd28SJason M. Bills {
3070043a0536SJohnathan Mantey     auto getStoredLogCallback =
3071b9d36b47SEd Tanous         [asyncResp, logID,
3072b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
3073b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3074e855dd28SJason M. Bills         if (ec)
3075e855dd28SJason M. Bills         {
3076e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
30771ddcf01aSJason M. Bills             if (ec.value() ==
30781ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
30791ddcf01aSJason M. Bills             {
3080002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
30811ddcf01aSJason M. Bills             }
30821ddcf01aSJason M. Bills             else
30831ddcf01aSJason M. Bills             {
3084e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
30851ddcf01aSJason M. Bills             }
3086e855dd28SJason M. Bills             return;
3087e855dd28SJason M. Bills         }
3088043a0536SJohnathan Mantey 
3089043a0536SJohnathan Mantey         std::string timestamp{};
3090043a0536SJohnathan Mantey         std::string filename{};
3091043a0536SJohnathan Mantey         std::string logfile{};
30922c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3093043a0536SJohnathan Mantey 
3094043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3095e855dd28SJason M. Bills         {
30969db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3097e855dd28SJason M. Bills             return;
3098e855dd28SJason M. Bills         }
3099e855dd28SJason M. Bills 
3100043a0536SJohnathan Mantey         std::string crashdumpURI =
3101e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3102043a0536SJohnathan Mantey             logID + "/" + filename;
310384afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
31049c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
310584afc48bSJason M. Bills         logEntry["@odata.id"] =
310684afc48bSJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
310784afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
310884afc48bSJason M. Bills         logEntry["Id"] = logID;
310984afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
311084afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
311184afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
311284afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
311384afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
31142b20ef6eSJason M. Bills 
31152b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
31162b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
31172b20ef6eSJason M. Bills         // directly
31182b20ef6eSJason M. Bills         if (logEntryJson.is_array())
31192b20ef6eSJason M. Bills         {
31202b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
31212b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
31222b20ef6eSJason M. Bills                 logEntryJson.size();
31232b20ef6eSJason M. Bills         }
31242b20ef6eSJason M. Bills         else
31252b20ef6eSJason M. Bills         {
3126d405bb51SJason M. Bills             logEntryJson.update(logEntry);
31272b20ef6eSJason M. Bills         }
3128e855dd28SJason M. Bills     };
3129d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3130d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3131d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3132d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3133e855dd28SJason M. Bills }
3134e855dd28SJason M. Bills 
31357e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
31361da66f75SEd Tanous {
31373946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31383946028dSAppaRao Puli     // method for security reasons.
31391da66f75SEd Tanous     /**
31401da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
31411da66f75SEd Tanous      */
31427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
314322d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3144ed398213SEd Tanous         // This is incorrect, should be.
3145ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3146432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3147002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3148002d39b4SEd Tanous             [&app](const crow::Request& req,
314922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
315022d268cbSEd Tanous                    const std::string& systemName) {
31513ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
315245ca1b86SEd Tanous         {
315345ca1b86SEd Tanous             return;
315445ca1b86SEd Tanous         }
315522d268cbSEd Tanous         if (systemName != "system")
315622d268cbSEd Tanous         {
315722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
315822d268cbSEd Tanous                                        systemName);
315922d268cbSEd Tanous             return;
316022d268cbSEd Tanous         }
316122d268cbSEd Tanous 
31627a1dbc48SGeorge Liu         constexpr std::array<std::string_view, 1> interfaces = {
31637a1dbc48SGeorge Liu             crashdumpInterface};
31647a1dbc48SGeorge Liu         dbus::utility::getSubTreePaths(
31657a1dbc48SGeorge Liu             "/", 0, interfaces,
31667a1dbc48SGeorge Liu             [asyncResp](const boost::system::error_code& ec,
31672b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
31681da66f75SEd Tanous             if (ec)
31691da66f75SEd Tanous             {
31701da66f75SEd Tanous                 if (ec.value() !=
31711da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
31721da66f75SEd Tanous                 {
31731da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
31741da66f75SEd Tanous                                      << ec.message();
3175f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
31761da66f75SEd Tanous                     return;
31771da66f75SEd Tanous                 }
31781da66f75SEd Tanous             }
3179e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
31801da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
31810f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3182424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3183002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3184e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3185424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3186002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3187a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
31882b20ef6eSJason M. Bills 
31892b20ef6eSJason M. Bills             for (const std::string& path : resp)
31901da66f75SEd Tanous             {
31912b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3192e855dd28SJason M. Bills                 // Get the log ID
31932b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
31942b20ef6eSJason M. Bills                 if (logID.empty())
31951da66f75SEd Tanous                 {
3196e855dd28SJason M. Bills                     continue;
31971da66f75SEd Tanous                 }
3198e855dd28SJason M. Bills                 // Add the log entry to the array
31992b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
32002b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
32011da66f75SEd Tanous             }
32027a1dbc48SGeorge Liu             });
32037e860f15SJohn Edward Broadbent         });
32041da66f75SEd Tanous }
32051da66f75SEd Tanous 
32067e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
32071da66f75SEd Tanous {
32083946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32093946028dSAppaRao Puli     // method for security reasons.
32101da66f75SEd Tanous 
32117e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
321222d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3213ed398213SEd Tanous         // this is incorrect, should be
3214ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3215432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32167e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
321745ca1b86SEd Tanous             [&app](const crow::Request& req,
32187e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
321922d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
32203ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
322145ca1b86SEd Tanous         {
322245ca1b86SEd Tanous             return;
322345ca1b86SEd Tanous         }
322422d268cbSEd Tanous         if (systemName != "system")
322522d268cbSEd Tanous         {
322622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
322722d268cbSEd Tanous                                        systemName);
322822d268cbSEd Tanous             return;
322922d268cbSEd Tanous         }
32307e860f15SJohn Edward Broadbent         const std::string& logID = param;
3231e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
32327e860f15SJohn Edward Broadbent         });
3233e855dd28SJason M. Bills }
3234e855dd28SJason M. Bills 
32357e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3236e855dd28SJason M. Bills {
32373946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32383946028dSAppaRao Puli     // method for security reasons.
32397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
32407e860f15SJohn Edward Broadbent         app,
324122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3242ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
32437e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3244a4ce114aSNan Zhou             [](const crow::Request& req,
32457e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
324622d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
324722d268cbSEd Tanous                const std::string& fileName) {
32482a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
32492a9beeedSShounak Mitra         // Redfish resource.
325022d268cbSEd Tanous 
325122d268cbSEd Tanous         if (systemName != "system")
325222d268cbSEd Tanous         {
325322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
325422d268cbSEd Tanous                                        systemName);
325522d268cbSEd Tanous             return;
325622d268cbSEd Tanous         }
325722d268cbSEd Tanous 
3258043a0536SJohnathan Mantey         auto getStoredLogCallback =
3259002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3260abf2add6SEd Tanous                 const boost::system::error_code ec,
3261002d39b4SEd Tanous                 const std::vector<
3262002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
32637e860f15SJohn Edward Broadbent                     resp) {
32641da66f75SEd Tanous             if (ec)
32651da66f75SEd Tanous             {
3266002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3267f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
32681da66f75SEd Tanous                 return;
32691da66f75SEd Tanous             }
3270e855dd28SJason M. Bills 
3271043a0536SJohnathan Mantey             std::string dbusFilename{};
3272043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3273043a0536SJohnathan Mantey             std::string dbusFilepath{};
3274043a0536SJohnathan Mantey 
3275002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3276002d39b4SEd Tanous                                      dbusFilepath);
3277043a0536SJohnathan Mantey 
3278043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3279043a0536SJohnathan Mantey                 dbusFilepath.empty())
32801da66f75SEd Tanous             {
32819db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
32821da66f75SEd Tanous                 return;
32831da66f75SEd Tanous             }
3284e855dd28SJason M. Bills 
3285043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3286043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3287043a0536SJohnathan Mantey             {
32889db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3289043a0536SJohnathan Mantey                 return;
3290043a0536SJohnathan Mantey             }
3291043a0536SJohnathan Mantey 
3292043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3293043a0536SJohnathan Mantey             {
32949db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3295043a0536SJohnathan Mantey                 return;
3296043a0536SJohnathan Mantey             }
3297002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3298002d39b4SEd Tanous             asyncResp->res.body() =
3299002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3300043a0536SJohnathan Mantey 
33017e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
33027e860f15SJohn Edward Broadbent             // from a browser
3303d9f6c621SEd Tanous             asyncResp->res.addHeader(
3304d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
33051da66f75SEd Tanous         };
3306d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3307d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3308d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3309d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
33107e860f15SJohn Edward Broadbent         });
33111da66f75SEd Tanous }
33121da66f75SEd Tanous 
3313c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3314c5a4c82aSJason M. Bills {
3315c5a4c82aSJason M. Bills     onDemand,
3316c5a4c82aSJason M. Bills     telemetry,
3317c5a4c82aSJason M. Bills     invalid,
3318c5a4c82aSJason M. Bills };
3319c5a4c82aSJason M. Bills 
3320f7725d79SEd Tanous inline OEMDiagnosticType
3321f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
3322c5a4c82aSJason M. Bills {
3323c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3324c5a4c82aSJason M. Bills     {
3325c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3326c5a4c82aSJason M. Bills     }
3327c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3328c5a4c82aSJason M. Bills     {
3329c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3330c5a4c82aSJason M. Bills     }
3331c5a4c82aSJason M. Bills 
3332c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3333c5a4c82aSJason M. Bills }
3334c5a4c82aSJason M. Bills 
33357e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
33361da66f75SEd Tanous {
33373946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
33383946028dSAppaRao Puli     // method for security reasons.
33390fda0f12SGeorge Liu     BMCWEB_ROUTE(
33400fda0f12SGeorge Liu         app,
334122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3342ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3343ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3344432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3345002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3346002d39b4SEd Tanous             [&app](const crow::Request& req,
334722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
334822d268cbSEd Tanous                    const std::string& systemName) {
33493ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
335045ca1b86SEd Tanous         {
335145ca1b86SEd Tanous             return;
335245ca1b86SEd Tanous         }
335322d268cbSEd Tanous 
335422d268cbSEd Tanous         if (systemName != "system")
335522d268cbSEd Tanous         {
335622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
335722d268cbSEd Tanous                                        systemName);
335822d268cbSEd Tanous             return;
335922d268cbSEd Tanous         }
336022d268cbSEd Tanous 
33618e6c099aSJason M. Bills         std::string diagnosticDataType;
33628e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
336315ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3364002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3365002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
33668e6c099aSJason M. Bills         {
33678e6c099aSJason M. Bills             return;
33688e6c099aSJason M. Bills         }
33698e6c099aSJason M. Bills 
33708e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
33718e6c099aSJason M. Bills         {
33728e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
33738e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
33748e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
33758e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
33768e6c099aSJason M. Bills                 "CollectDiagnosticData");
33778e6c099aSJason M. Bills             return;
33788e6c099aSJason M. Bills         }
33798e6c099aSJason M. Bills 
3380c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3381c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3382c5a4c82aSJason M. Bills 
3383c5a4c82aSJason M. Bills         std::string iface;
3384c5a4c82aSJason M. Bills         std::string method;
3385c5a4c82aSJason M. Bills         std::string taskMatchStr;
3386c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3387c5a4c82aSJason M. Bills         {
3388c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3389c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3390c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3391c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3392c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3393c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3394c5a4c82aSJason M. Bills         }
3395c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3396c5a4c82aSJason M. Bills         {
3397c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3398c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3399c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3400c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3401c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3402c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3403c5a4c82aSJason M. Bills         }
3404c5a4c82aSJason M. Bills         else
3405c5a4c82aSJason M. Bills         {
3406c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3407c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3408c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3409002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3410002d39b4SEd Tanous                 "CollectDiagnosticData");
3411c5a4c82aSJason M. Bills             return;
3412c5a4c82aSJason M. Bills         }
3413c5a4c82aSJason M. Bills 
3414c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3415c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3416c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
341798be3e39SEd Tanous                            const std::string&) mutable {
34181da66f75SEd Tanous             if (ec)
34191da66f75SEd Tanous             {
3420002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
34211da66f75SEd Tanous                 {
3422f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
34231da66f75SEd Tanous                 }
34244363d3b2SJason M. Bills                 else if (ec.value() ==
34254363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
34264363d3b2SJason M. Bills                 {
3427002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3428002d39b4SEd Tanous                                                             "60");
34294363d3b2SJason M. Bills                 }
34301da66f75SEd Tanous                 else
34311da66f75SEd Tanous                 {
3432f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
34331da66f75SEd Tanous                 }
34341da66f75SEd Tanous                 return;
34351da66f75SEd Tanous             }
3436002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
343759d494eeSPatrick Williams                 [](boost::system::error_code err, sdbusplus::message_t&,
3438002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
343966afe4faSJames Feist                 if (!err)
344066afe4faSJames Feist                 {
3441002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3442e5d5006bSJames Feist                         std::to_string(taskData->index)));
3443831d6b09SJames Feist                     taskData->state = "Completed";
344466afe4faSJames Feist                 }
344532898ceaSJames Feist                 return task::completed;
344666afe4faSJames Feist                 },
3447c5a4c82aSJason M. Bills                 taskMatchStr);
3448c5a4c82aSJason M. Bills 
344946229577SJames Feist             task->startTimer(std::chrono::minutes(5));
345046229577SJames Feist             task->populateResp(asyncResp->res);
345198be3e39SEd Tanous             task->payload.emplace(std::move(payload));
34521da66f75SEd Tanous         };
34538e6c099aSJason M. Bills 
34541da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3455002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3456002d39b4SEd Tanous             iface, method);
34577e860f15SJohn Edward Broadbent         });
34586eda7685SKenny L. Ku }
34596eda7685SKenny L. Ku 
3460cb92c03bSAndrew Geissler /**
3461cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3462cb92c03bSAndrew Geissler  */
34637e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3464cb92c03bSAndrew Geissler {
3465cb92c03bSAndrew Geissler     /**
3466cb92c03bSAndrew Geissler      * Function handles POST method request.
3467cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3468cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3469cb92c03bSAndrew Geissler      */
34707e860f15SJohn Edward Broadbent 
34710fda0f12SGeorge Liu     BMCWEB_ROUTE(
34720fda0f12SGeorge Liu         app,
347322d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3474ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
34757e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
347645ca1b86SEd Tanous             [&app](const crow::Request& req,
347722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
347822d268cbSEd Tanous                    const std::string& systemName) {
34793ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
348045ca1b86SEd Tanous         {
348145ca1b86SEd Tanous             return;
348245ca1b86SEd Tanous         }
348322d268cbSEd Tanous         if (systemName != "system")
348422d268cbSEd Tanous         {
348522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
348622d268cbSEd Tanous                                        systemName);
348722d268cbSEd Tanous             return;
348822d268cbSEd Tanous         }
3489cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3490cb92c03bSAndrew Geissler 
3491cb92c03bSAndrew Geissler         // Process response from Logging service.
3492002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3493002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3494cb92c03bSAndrew Geissler             if (ec)
3495cb92c03bSAndrew Geissler             {
3496cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3497002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3498cb92c03bSAndrew Geissler                 asyncResp->res.result(
3499cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3500cb92c03bSAndrew Geissler                 return;
3501cb92c03bSAndrew Geissler             }
3502cb92c03bSAndrew Geissler 
3503002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3504cb92c03bSAndrew Geissler         };
3505cb92c03bSAndrew Geissler 
3506cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3507cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
35082c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3509cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3510cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
35117e860f15SJohn Edward Broadbent         });
3512cb92c03bSAndrew Geissler }
3513a3316fc6SZhikuiRen 
3514a3316fc6SZhikuiRen /****************************************************
3515a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3516a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3517a3316fc6SZhikuiRen  ******************************************************/
35187e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3519a3316fc6SZhikuiRen {
352022d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3521ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3522002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3523002d39b4SEd Tanous             [&app](const crow::Request& req,
352422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
352522d268cbSEd Tanous                    const std::string& systemName) {
35263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
352745ca1b86SEd Tanous         {
352845ca1b86SEd Tanous             return;
352945ca1b86SEd Tanous         }
353022d268cbSEd Tanous         if (systemName != "system")
353122d268cbSEd Tanous         {
353222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
353322d268cbSEd Tanous                                        systemName);
353422d268cbSEd Tanous             return;
353522d268cbSEd Tanous         }
35361476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
35371476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
35381476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
35391476687dSEd Tanous             "#LogService.v1_1_0.LogService";
35401476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
35411476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
3542ed34a4adSEd Tanous         asyncResp->res.jsonValue["Id"] = "PostCodes";
35431476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
35441476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
35451476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
35467c8c4058STejas Patil 
35477c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
35482b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
35490fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
35507c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
35517c8c4058STejas Patil             redfishDateTimeOffset.second;
35527c8c4058STejas Patil 
3553a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
35547e860f15SJohn Edward Broadbent             {"target",
35550fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
35567e860f15SJohn Edward Broadbent         });
3557a3316fc6SZhikuiRen }
3558a3316fc6SZhikuiRen 
35597e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3560a3316fc6SZhikuiRen {
35610fda0f12SGeorge Liu     BMCWEB_ROUTE(
35620fda0f12SGeorge Liu         app,
356322d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3564ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3565ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3566432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
35677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
356845ca1b86SEd Tanous             [&app](const crow::Request& req,
356922d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
357022d268cbSEd Tanous                    const std::string& systemName) {
35713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
357245ca1b86SEd Tanous         {
357345ca1b86SEd Tanous             return;
357445ca1b86SEd Tanous         }
357522d268cbSEd Tanous         if (systemName != "system")
357622d268cbSEd Tanous         {
357722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
357822d268cbSEd Tanous                                        systemName);
357922d268cbSEd Tanous             return;
358022d268cbSEd Tanous         }
3581a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3582a3316fc6SZhikuiRen 
3583a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3584a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3585a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3586a3316fc6SZhikuiRen             if (ec)
3587a3316fc6SZhikuiRen             {
3588a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3589002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
35907e860f15SJohn Edward Broadbent                                  << ec;
3591002d39b4SEd Tanous                 asyncResp->res.result(
3592002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3593a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3594a3316fc6SZhikuiRen                 return;
3595a3316fc6SZhikuiRen             }
3596a3316fc6SZhikuiRen             },
359715124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
359815124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3599a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
36007e860f15SJohn Edward Broadbent         });
3601a3316fc6SZhikuiRen }
3602a3316fc6SZhikuiRen 
36036f284d24SJiaqing Zhao /**
36046f284d24SJiaqing Zhao  * @brief Parse post code ID and get the current value and index value
36056f284d24SJiaqing Zhao  *        eg: postCodeID=B1-2, currentValue=1, index=2
36066f284d24SJiaqing Zhao  *
36076f284d24SJiaqing Zhao  * @param[in]  postCodeID     Post Code ID
36086f284d24SJiaqing Zhao  * @param[out] currentValue   Current value
36096f284d24SJiaqing Zhao  * @param[out] index          Index value
36106f284d24SJiaqing Zhao  *
36116f284d24SJiaqing Zhao  * @return bool true if the parsing is successful, false the parsing fails
36126f284d24SJiaqing Zhao  */
36136f284d24SJiaqing Zhao inline static bool parsePostCode(const std::string& postCodeID,
36146f284d24SJiaqing Zhao                                  uint64_t& currentValue, uint16_t& index)
36156f284d24SJiaqing Zhao {
36166f284d24SJiaqing Zhao     std::vector<std::string> split;
361750ebd4afSEd Tanous     bmcweb::split(split, postCodeID, '-');
36186f284d24SJiaqing Zhao     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
36196f284d24SJiaqing Zhao     {
36206f284d24SJiaqing Zhao         return false;
36216f284d24SJiaqing Zhao     }
36226f284d24SJiaqing Zhao 
36236f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36246f284d24SJiaqing Zhao     const char* start = split[0].data() + 1;
36256f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36266f284d24SJiaqing Zhao     const char* end = split[0].data() + split[0].size();
36276f284d24SJiaqing Zhao     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
36286f284d24SJiaqing Zhao 
36296f284d24SJiaqing Zhao     if (ptrIndex != end || ecIndex != std::errc())
36306f284d24SJiaqing Zhao     {
36316f284d24SJiaqing Zhao         return false;
36326f284d24SJiaqing Zhao     }
36336f284d24SJiaqing Zhao 
36346f284d24SJiaqing Zhao     start = split[1].data();
36356f284d24SJiaqing Zhao 
36366f284d24SJiaqing Zhao     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
36376f284d24SJiaqing Zhao     end = split[1].data() + split[1].size();
36386f284d24SJiaqing Zhao     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
36396f284d24SJiaqing Zhao 
36406f284d24SJiaqing Zhao     return ptrValue == end && ecValue == std::errc();
36416f284d24SJiaqing Zhao }
36426f284d24SJiaqing Zhao 
36436f284d24SJiaqing Zhao static bool fillPostCodeEntry(
36448d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
36456c9a279eSManojkiran Eda     const boost::container::flat_map<
36466c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3647a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3648a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3649a3316fc6SZhikuiRen {
3650a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3651fffb8c1fSEd Tanous     const registries::Message* message =
3652fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3653a3316fc6SZhikuiRen 
3654a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3655a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
36566c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
36576c9a279eSManojkiran Eda              code : postcode)
3658a3316fc6SZhikuiRen     {
3659a3316fc6SZhikuiRen         currentCodeIndex++;
3660a3316fc6SZhikuiRen         std::string postcodeEntryID =
3661a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3662a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3663a3316fc6SZhikuiRen 
3664a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3665a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3666a3316fc6SZhikuiRen 
3667a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3668a3316fc6SZhikuiRen         { // already incremented
3669a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3670a3316fc6SZhikuiRen         }
3671a3316fc6SZhikuiRen         else
3672a3316fc6SZhikuiRen         {
3673a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3674a3316fc6SZhikuiRen         }
3675a3316fc6SZhikuiRen 
3676a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3677a3316fc6SZhikuiRen         // not fall between top and skip
3678a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3679a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3680a3316fc6SZhikuiRen         {
3681a3316fc6SZhikuiRen             continue;
3682a3316fc6SZhikuiRen         }
3683a3316fc6SZhikuiRen 
36844e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3685a3316fc6SZhikuiRen         // currentIndex
3686a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3687a3316fc6SZhikuiRen         {
3688a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3689a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3690a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3691a3316fc6SZhikuiRen             continue;
3692a3316fc6SZhikuiRen         }
3693a3316fc6SZhikuiRen 
3694a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3695a3316fc6SZhikuiRen         // index
3696a3316fc6SZhikuiRen 
3697a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3698a3316fc6SZhikuiRen         std::string entryTimeStr;
36992a025611SKonstantin Aladyshev         entryTimeStr = redfish::time_utils::getDateTimeUintUs(usecSinceEpoch);
3700a3316fc6SZhikuiRen 
3701a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3702a3316fc6SZhikuiRen         std::ostringstream hexCode;
3703a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
37046c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3705a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3706a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3707a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3708a3316fc6SZhikuiRen         // Set precision to 4 digits
3709a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3710a3316fc6SZhikuiRen         // Add double to stream
3711a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3712a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3713a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3714a3316fc6SZhikuiRen 
3715a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3716a3316fc6SZhikuiRen         std::string msg;
3717a3316fc6SZhikuiRen         if (message != nullptr)
3718a3316fc6SZhikuiRen         {
3719a3316fc6SZhikuiRen             msg = message->message;
3720a3316fc6SZhikuiRen 
3721a3316fc6SZhikuiRen             // fill in this post code value
3722a3316fc6SZhikuiRen             int i = 0;
3723a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3724a3316fc6SZhikuiRen             {
3725a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3726a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3727a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3728a3316fc6SZhikuiRen                 {
3729a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3730a3316fc6SZhikuiRen                 }
3731a3316fc6SZhikuiRen             }
3732a3316fc6SZhikuiRen         }
3733a3316fc6SZhikuiRen 
3734d4342a92STim Lee         // Get Severity template from message registry
3735d4342a92STim Lee         std::string severity;
3736d4342a92STim Lee         if (message != nullptr)
3737d4342a92STim Lee         {
37385f2b84eeSEd Tanous             severity = message->messageSeverity;
3739d4342a92STim Lee         }
3740d4342a92STim Lee 
37416f284d24SJiaqing Zhao         // Format entry
37426f284d24SJiaqing Zhao         nlohmann::json::object_t bmcLogEntry;
37439c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
374484afc48bSJason M. Bills         bmcLogEntry["@odata.id"] =
37450fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
374684afc48bSJason M. Bills             postcodeEntryID;
374784afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
374884afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
374984afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
375084afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
375184afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
375284afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
375384afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
375484afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3755647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3756647b3cdcSGeorge Liu         {
3757647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3758647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3759647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3760647b3cdcSGeorge Liu         }
37616f284d24SJiaqing Zhao 
37626f284d24SJiaqing Zhao         // codeIndex is only specified when querying single entry, return only
37636f284d24SJiaqing Zhao         // that entry in this case
37646f284d24SJiaqing Zhao         if (codeIndex != 0)
37656f284d24SJiaqing Zhao         {
37666f284d24SJiaqing Zhao             aResp->res.jsonValue.update(bmcLogEntry);
37676f284d24SJiaqing Zhao             return true;
3768a3316fc6SZhikuiRen         }
37696f284d24SJiaqing Zhao 
37706f284d24SJiaqing Zhao         nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
37716f284d24SJiaqing Zhao         logEntryArray.push_back(std::move(bmcLogEntry));
37726f284d24SJiaqing Zhao     }
37736f284d24SJiaqing Zhao 
37746f284d24SJiaqing Zhao     // Return value is always false when querying multiple entries
37756f284d24SJiaqing Zhao     return false;
3776a3316fc6SZhikuiRen }
3777a3316fc6SZhikuiRen 
37788d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
37796f284d24SJiaqing Zhao                                 const std::string& entryId)
3780a3316fc6SZhikuiRen {
37816f284d24SJiaqing Zhao     uint16_t bootIndex = 0;
37826f284d24SJiaqing Zhao     uint64_t codeIndex = 0;
37836f284d24SJiaqing Zhao     if (!parsePostCode(entryId, codeIndex, bootIndex))
37846f284d24SJiaqing Zhao     {
37856f284d24SJiaqing Zhao         // Requested ID was not found
37866f284d24SJiaqing Zhao         messages::resourceNotFound(aResp->res, "LogEntry", entryId);
37876f284d24SJiaqing Zhao         return;
37886f284d24SJiaqing Zhao     }
37896f284d24SJiaqing Zhao 
37906f284d24SJiaqing Zhao     if (bootIndex == 0 || codeIndex == 0)
37916f284d24SJiaqing Zhao     {
37926f284d24SJiaqing Zhao         // 0 is an invalid index
37936f284d24SJiaqing Zhao         messages::resourceNotFound(aResp->res, "LogEntry", entryId);
37946f284d24SJiaqing Zhao         return;
37956f284d24SJiaqing Zhao     }
37966f284d24SJiaqing Zhao 
3797a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
37986f284d24SJiaqing Zhao         [aResp, entryId, bootIndex,
37996c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
38006c9a279eSManojkiran Eda                     const boost::container::flat_map<
38016c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38026c9a279eSManojkiran Eda                         postcode) {
3803a3316fc6SZhikuiRen         if (ec)
3804a3316fc6SZhikuiRen         {
3805a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3806a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3807a3316fc6SZhikuiRen             return;
3808a3316fc6SZhikuiRen         }
3809a3316fc6SZhikuiRen 
3810a3316fc6SZhikuiRen         if (postcode.empty())
3811a3316fc6SZhikuiRen         {
38126f284d24SJiaqing Zhao             messages::resourceNotFound(aResp->res, "LogEntry", entryId);
3813a3316fc6SZhikuiRen             return;
3814a3316fc6SZhikuiRen         }
3815a3316fc6SZhikuiRen 
38166f284d24SJiaqing Zhao         if (!fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex))
38176f284d24SJiaqing Zhao         {
38186f284d24SJiaqing Zhao             messages::resourceNotFound(aResp->res, "LogEntry", entryId);
38196f284d24SJiaqing Zhao             return;
38206f284d24SJiaqing Zhao         }
3821a3316fc6SZhikuiRen         },
382215124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
382315124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3824a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3825a3316fc6SZhikuiRen         bootIndex);
3826a3316fc6SZhikuiRen }
3827a3316fc6SZhikuiRen 
38288d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3829a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3830a3316fc6SZhikuiRen                                const uint16_t bootCount,
38313648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
38323648c8beSEd Tanous                                size_t top)
3833a3316fc6SZhikuiRen {
3834a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3835a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3836a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
38376c9a279eSManojkiran Eda               const boost::container::flat_map<
38386c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
38396c9a279eSManojkiran Eda                   postcode) {
3840a3316fc6SZhikuiRen         if (ec)
3841a3316fc6SZhikuiRen         {
3842a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3843a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3844a3316fc6SZhikuiRen             return;
3845a3316fc6SZhikuiRen         }
3846a3316fc6SZhikuiRen 
3847a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3848a3316fc6SZhikuiRen         if (!postcode.empty())
3849a3316fc6SZhikuiRen         {
3850a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
38513648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3852a3316fc6SZhikuiRen             {
38533648c8beSEd Tanous                 uint64_t thisBootSkip =
38543648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
38553648c8beSEd Tanous                     entryCount;
3856a3316fc6SZhikuiRen                 uint64_t thisBootTop =
38573648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
38583648c8beSEd Tanous                     entryCount;
3859a3316fc6SZhikuiRen 
3860002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3861002d39b4SEd Tanous                                   thisBootTop);
3862a3316fc6SZhikuiRen             }
3863a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3864a3316fc6SZhikuiRen         }
3865a3316fc6SZhikuiRen 
3866a3316fc6SZhikuiRen         // continue to previous bootIndex
3867a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3868a3316fc6SZhikuiRen         {
3869a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3870a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3871a3316fc6SZhikuiRen         }
387281584abeSJiaqing Zhao         else if (skip + top < endCount)
3873a3316fc6SZhikuiRen         {
3874a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
38750fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3876a3316fc6SZhikuiRen                 std::to_string(skip + top);
3877a3316fc6SZhikuiRen         }
3878a3316fc6SZhikuiRen         },
387915124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
388015124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3881a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3882a3316fc6SZhikuiRen         bootIndex);
3883a3316fc6SZhikuiRen }
3884a3316fc6SZhikuiRen 
38858d1b46d7Szhanghch05 static void
38868d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
38873648c8beSEd Tanous                          size_t skip, size_t top)
3888a3316fc6SZhikuiRen {
3889a3316fc6SZhikuiRen     uint64_t entryCount = 0;
38901e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
38911e1e598dSJonathan Doman         *crow::connections::systemBus,
38921e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
38931e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
38941e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
38951e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
38961e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3897a3316fc6SZhikuiRen         if (ec)
3898a3316fc6SZhikuiRen         {
3899a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3900a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3901a3316fc6SZhikuiRen             return;
3902a3316fc6SZhikuiRen         }
39031e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
39041e1e598dSJonathan Doman         });
3905a3316fc6SZhikuiRen }
3906a3316fc6SZhikuiRen 
39077e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3908a3316fc6SZhikuiRen {
39097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
391022d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3911ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
39127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
391345ca1b86SEd Tanous             [&app](const crow::Request& req,
391422d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
391522d268cbSEd Tanous                    const std::string& systemName) {
3916c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3917c937d2bfSEd Tanous             .canDelegateTop = true,
3918c937d2bfSEd Tanous             .canDelegateSkip = true,
3919c937d2bfSEd Tanous         };
3920c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3921c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
39223ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
392345ca1b86SEd Tanous         {
392445ca1b86SEd Tanous             return;
392545ca1b86SEd Tanous         }
392622d268cbSEd Tanous 
392722d268cbSEd Tanous         if (systemName != "system")
392822d268cbSEd Tanous         {
392922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
393022d268cbSEd Tanous                                        systemName);
393122d268cbSEd Tanous             return;
393222d268cbSEd Tanous         }
3933a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3934a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3935a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3936a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3937a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3938a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3939a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3940a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3941a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
39423648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
39435143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
39443648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
39457e860f15SJohn Edward Broadbent         });
3946a3316fc6SZhikuiRen }
3947a3316fc6SZhikuiRen 
3948647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3949647b3cdcSGeorge Liu {
39500fda0f12SGeorge Liu     BMCWEB_ROUTE(
39510fda0f12SGeorge Liu         app,
395222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
3953647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3954647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
395545ca1b86SEd Tanous             [&app](const crow::Request& req,
3956647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
395722d268cbSEd Tanous                    const std::string& systemName,
3958647b3cdcSGeorge Liu                    const std::string& postCodeID) {
39593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
396045ca1b86SEd Tanous         {
396145ca1b86SEd Tanous             return;
396245ca1b86SEd Tanous         }
396399351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
396499351cd8SEd Tanous                 req.getHeaderValue("Accept"),
39654a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
3966647b3cdcSGeorge Liu         {
3967002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3968647b3cdcSGeorge Liu             return;
3969647b3cdcSGeorge Liu         }
397022d268cbSEd Tanous         if (systemName != "system")
397122d268cbSEd Tanous         {
397222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
397322d268cbSEd Tanous                                        systemName);
397422d268cbSEd Tanous             return;
397522d268cbSEd Tanous         }
3976647b3cdcSGeorge Liu 
3977647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3978647b3cdcSGeorge Liu         uint16_t index = 0;
3979647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3980647b3cdcSGeorge Liu         {
3981002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3982647b3cdcSGeorge Liu             return;
3983647b3cdcSGeorge Liu         }
3984647b3cdcSGeorge Liu 
3985647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3986647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3987647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3988002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3989002d39b4SEd Tanous                     postcodes) {
3990647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3991647b3cdcSGeorge Liu             {
3992002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3993002d39b4SEd Tanous                                            postCodeID);
3994647b3cdcSGeorge Liu                 return;
3995647b3cdcSGeorge Liu             }
3996647b3cdcSGeorge Liu             if (ec)
3997647b3cdcSGeorge Liu             {
3998647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3999647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
4000647b3cdcSGeorge Liu                 return;
4001647b3cdcSGeorge Liu             }
4002647b3cdcSGeorge Liu 
4003647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
4004002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
4005647b3cdcSGeorge Liu             {
4006647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
4007002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4008002d39b4SEd Tanous                                            postCodeID);
4009647b3cdcSGeorge Liu                 return;
4010647b3cdcSGeorge Liu             }
4011647b3cdcSGeorge Liu 
40129eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
401346ff87baSEd Tanous             if (c.empty())
4014647b3cdcSGeorge Liu             {
4015647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
4016002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
4017002d39b4SEd Tanous                                            postCodeID);
4018647b3cdcSGeorge Liu                 return;
4019647b3cdcSGeorge Liu             }
402046ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
402146ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
402246ff87baSEd Tanous             std::string_view strData(d, c.size());
4023647b3cdcSGeorge Liu 
4024d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
4025647b3cdcSGeorge Liu                                      "application/octet-stream");
4026d9f6c621SEd Tanous             asyncResp->res.addHeader(
4027d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
4028002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
4029647b3cdcSGeorge Liu             },
4030647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
4031647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
4032002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
4033647b3cdcSGeorge Liu         });
4034647b3cdcSGeorge Liu }
4035647b3cdcSGeorge Liu 
40367e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
4037a3316fc6SZhikuiRen {
40387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
403922d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
4040ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
40417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
404245ca1b86SEd Tanous             [&app](const crow::Request& req,
40437e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
404422d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
40453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
404645ca1b86SEd Tanous         {
404745ca1b86SEd Tanous             return;
404845ca1b86SEd Tanous         }
404922d268cbSEd Tanous         if (systemName != "system")
405022d268cbSEd Tanous         {
405122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
405222d268cbSEd Tanous                                        systemName);
405322d268cbSEd Tanous             return;
405422d268cbSEd Tanous         }
405522d268cbSEd Tanous 
40566f284d24SJiaqing Zhao         getPostCodeForEntry(asyncResp, targetID);
40577e860f15SJohn Edward Broadbent         });
4058a3316fc6SZhikuiRen }
4059a3316fc6SZhikuiRen 
40601da66f75SEd Tanous } // namespace redfish
4061