xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision d1bde9e590f165f28d948fda93e48d51b30bb463)
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 
18b7028ebfSSpencer Ku #include "gzfile.hpp"
19647b3cdcSGeorge Liu #include "http_utility.hpp"
20b7028ebfSSpencer Ku #include "human_sort.hpp"
214851d45dSJason M. Bills #include "registries.hpp"
224851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
234851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2446229577SJames Feist #include "task.hpp"
251da66f75SEd Tanous 
26e1f26343SJason M. Bills #include <systemd/sd-journal.h>
27400fd1fbSAdriana Kobylak #include <unistd.h>
28e1f26343SJason M. Bills 
297e860f15SJohn Edward Broadbent #include <app.hpp>
309896eaedSEd Tanous #include <boost/algorithm/string/case_conv.hpp>
3111ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
32400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
334851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
3407c8c20dSEd Tanous #include <boost/beast/http/verb.hpp>
351da66f75SEd Tanous #include <boost/container/flat_map.hpp>
361ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
37168e20c1SEd Tanous #include <dbus_utility.hpp>
38cb92c03bSAndrew Geissler #include <error_messages.hpp>
3945ca1b86SEd Tanous #include <query.hpp>
40ed398213SEd Tanous #include <registries/privilege_registry.hpp>
41*d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
42*d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
43*d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
442b82937eSEd Tanous #include <utils/time_utils.hpp>
451214b7e7SGunnar Mills 
46647b3cdcSGeorge Liu #include <charconv>
474418c7f0SJames Feist #include <filesystem>
4875710de2SXiaochao Ma #include <optional>
4926702d01SEd Tanous #include <span>
50cd225da8SJason M. Bills #include <string_view>
51abf2add6SEd Tanous #include <variant>
521da66f75SEd Tanous 
531da66f75SEd Tanous namespace redfish
541da66f75SEd Tanous {
551da66f75SEd Tanous 
565b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
575b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
585b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
595b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
605b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
615b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
62424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
636eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
646eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
651da66f75SEd Tanous 
66fffb8c1fSEd Tanous namespace registries
674851d45dSJason M. Bills {
6826702d01SEd Tanous static const Message*
6926702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
7026702d01SEd Tanous                            const std::span<const MessageEntry> registry)
714851d45dSJason M. Bills {
72002d39b4SEd Tanous     std::span<const MessageEntry>::iterator messageIt =
73002d39b4SEd Tanous         std::find_if(registry.begin(), registry.end(),
744851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
75e662eae8SEd Tanous         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
764851d45dSJason M. Bills         });
7726702d01SEd Tanous     if (messageIt != registry.end())
784851d45dSJason M. Bills     {
794851d45dSJason M. Bills         return &messageIt->second;
804851d45dSJason M. Bills     }
814851d45dSJason M. Bills 
824851d45dSJason M. Bills     return nullptr;
834851d45dSJason M. Bills }
844851d45dSJason M. Bills 
854851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
864851d45dSJason M. Bills {
874851d45dSJason M. Bills     // Redfish MessageIds are in the form
884851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
894851d45dSJason M. Bills     // the right Message
904851d45dSJason M. Bills     std::vector<std::string> fields;
914851d45dSJason M. Bills     fields.reserve(4);
924851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
9302cad96eSEd Tanous     const std::string& registryName = fields[0];
9402cad96eSEd Tanous     const std::string& messageKey = fields[3];
954851d45dSJason M. Bills 
964851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
974851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
984851d45dSJason M. Bills     {
994851d45dSJason M. Bills         return getMessageFromRegistry(
10026702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
1014851d45dSJason M. Bills     }
1024851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
1034851d45dSJason M. Bills     {
1044851d45dSJason M. Bills         return getMessageFromRegistry(
10526702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1064851d45dSJason M. Bills     }
1074851d45dSJason M. Bills     return nullptr;
1084851d45dSJason M. Bills }
109fffb8c1fSEd Tanous } // namespace registries
1104851d45dSJason M. Bills 
111f6150403SJames Feist namespace fs = std::filesystem;
1121da66f75SEd Tanous 
113cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
114cb92c03bSAndrew Geissler {
115d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
116d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
117d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
118d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
119cb92c03bSAndrew Geissler     {
120cb92c03bSAndrew Geissler         return "Critical";
121cb92c03bSAndrew Geissler     }
1223174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
123d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
124d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
125cb92c03bSAndrew Geissler     {
126cb92c03bSAndrew Geissler         return "OK";
127cb92c03bSAndrew Geissler     }
1283174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
129cb92c03bSAndrew Geissler     {
130cb92c03bSAndrew Geissler         return "Warning";
131cb92c03bSAndrew Geissler     }
132cb92c03bSAndrew Geissler     return "";
133cb92c03bSAndrew Geissler }
134cb92c03bSAndrew Geissler 
1357e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13639e77504SEd Tanous                                      const std::string_view& field,
13739e77504SEd Tanous                                      std::string_view& contents)
13816428a1aSJason M. Bills {
13916428a1aSJason M. Bills     const char* data = nullptr;
14016428a1aSJason M. Bills     size_t length = 0;
14116428a1aSJason M. Bills     int ret = 0;
14216428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
14346ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
14446ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
14546ff87baSEd Tanous 
14646ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
14716428a1aSJason M. Bills     if (ret < 0)
14816428a1aSJason M. Bills     {
14916428a1aSJason M. Bills         return ret;
15016428a1aSJason M. Bills     }
15139e77504SEd Tanous     contents = std::string_view(data, length);
15216428a1aSJason M. Bills     // Only use the content after the "=" character.
15381ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
15416428a1aSJason M. Bills     return ret;
15516428a1aSJason M. Bills }
15616428a1aSJason M. Bills 
1577e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1587e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1597e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
16016428a1aSJason M. Bills {
16116428a1aSJason M. Bills     int ret = 0;
16239e77504SEd Tanous     std::string_view metadata;
16316428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16416428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16516428a1aSJason M. Bills     if (ret < 0)
16616428a1aSJason M. Bills     {
16716428a1aSJason M. Bills         return ret;
16816428a1aSJason M. Bills     }
169b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
17016428a1aSJason M. Bills     return ret;
17116428a1aSJason M. Bills }
17216428a1aSJason M. Bills 
1737e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1747e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
175a3316fc6SZhikuiRen {
176a3316fc6SZhikuiRen     int ret = 0;
177a3316fc6SZhikuiRen     uint64_t timestamp = 0;
178a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
179a3316fc6SZhikuiRen     if (ret < 0)
180a3316fc6SZhikuiRen     {
181a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
182a3316fc6SZhikuiRen                          << strerror(-ret);
183a3316fc6SZhikuiRen         return false;
184a3316fc6SZhikuiRen     }
1852b82937eSEd Tanous     entryTimestamp =
1862b82937eSEd Tanous         redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000);
1879c620e21SAsmitha Karunanithi     return true;
188a3316fc6SZhikuiRen }
18950b8a43aSEd Tanous 
1907e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
191e85d6b16SJason M. Bills                                     const bool firstEntry = true)
19216428a1aSJason M. Bills {
19316428a1aSJason M. Bills     int ret = 0;
19416428a1aSJason M. Bills     static uint64_t prevTs = 0;
19516428a1aSJason M. Bills     static int index = 0;
196e85d6b16SJason M. Bills     if (firstEntry)
197e85d6b16SJason M. Bills     {
198e85d6b16SJason M. Bills         prevTs = 0;
199e85d6b16SJason M. Bills     }
200e85d6b16SJason M. Bills 
20116428a1aSJason M. Bills     // Get the entry timestamp
20216428a1aSJason M. Bills     uint64_t curTs = 0;
20316428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
20416428a1aSJason M. Bills     if (ret < 0)
20516428a1aSJason M. Bills     {
20616428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
20716428a1aSJason M. Bills                          << strerror(-ret);
20816428a1aSJason M. Bills         return false;
20916428a1aSJason M. Bills     }
21016428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
21116428a1aSJason M. Bills     if (curTs == prevTs)
21216428a1aSJason M. Bills     {
21316428a1aSJason M. Bills         index++;
21416428a1aSJason M. Bills     }
21516428a1aSJason M. Bills     else
21616428a1aSJason M. Bills     {
21716428a1aSJason M. Bills         // Otherwise, reset it
21816428a1aSJason M. Bills         index = 0;
21916428a1aSJason M. Bills     }
22016428a1aSJason M. Bills     // Save the timestamp
22116428a1aSJason M. Bills     prevTs = curTs;
22216428a1aSJason M. Bills 
22316428a1aSJason M. Bills     entryID = std::to_string(curTs);
22416428a1aSJason M. Bills     if (index > 0)
22516428a1aSJason M. Bills     {
22616428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
22716428a1aSJason M. Bills     }
22816428a1aSJason M. Bills     return true;
22916428a1aSJason M. Bills }
23016428a1aSJason M. Bills 
231e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
232e85d6b16SJason M. Bills                              const bool firstEntry = true)
23395820184SJason M. Bills {
234271584abSEd Tanous     static time_t prevTs = 0;
23595820184SJason M. Bills     static int index = 0;
236e85d6b16SJason M. Bills     if (firstEntry)
237e85d6b16SJason M. Bills     {
238e85d6b16SJason M. Bills         prevTs = 0;
239e85d6b16SJason M. Bills     }
240e85d6b16SJason M. Bills 
24195820184SJason M. Bills     // Get the entry timestamp
242271584abSEd Tanous     std::time_t curTs = 0;
24395820184SJason M. Bills     std::tm timeStruct = {};
24495820184SJason M. Bills     std::istringstream entryStream(logEntry);
24595820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
24695820184SJason M. Bills     {
24795820184SJason M. Bills         curTs = std::mktime(&timeStruct);
24895820184SJason M. Bills     }
24995820184SJason M. Bills     // If the timestamp isn't unique, increment the index
25095820184SJason M. Bills     if (curTs == prevTs)
25195820184SJason M. Bills     {
25295820184SJason M. Bills         index++;
25395820184SJason M. Bills     }
25495820184SJason M. Bills     else
25595820184SJason M. Bills     {
25695820184SJason M. Bills         // Otherwise, reset it
25795820184SJason M. Bills         index = 0;
25895820184SJason M. Bills     }
25995820184SJason M. Bills     // Save the timestamp
26095820184SJason M. Bills     prevTs = curTs;
26195820184SJason M. Bills 
26295820184SJason M. Bills     entryID = std::to_string(curTs);
26395820184SJason M. Bills     if (index > 0)
26495820184SJason M. Bills     {
26595820184SJason M. Bills         entryID += "_" + std::to_string(index);
26695820184SJason M. Bills     }
26795820184SJason M. Bills     return true;
26895820184SJason M. Bills }
26995820184SJason M. Bills 
2707e860f15SJohn Edward Broadbent inline static bool
2718d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2728d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2738d1b46d7Szhanghch05                        uint64_t& index)
27416428a1aSJason M. Bills {
27516428a1aSJason M. Bills     if (entryID.empty())
27616428a1aSJason M. Bills     {
27716428a1aSJason M. Bills         return false;
27816428a1aSJason M. Bills     }
27916428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
28039e77504SEd Tanous     std::string_view tsStr(entryID);
28116428a1aSJason M. Bills 
28281ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
28371d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
28416428a1aSJason M. Bills     {
28516428a1aSJason M. Bills         // Timestamp has an index
28616428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
28739e77504SEd Tanous         std::string_view indexStr(entryID);
28816428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
289c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
290c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
291c0bd5e4bSEd Tanous         if (ec != std::errc())
29216428a1aSJason M. Bills         {
293ace85d60SEd Tanous             messages::resourceMissingAtURI(
294ace85d60SEd Tanous                 asyncResp->res, crow::utility::urlFromPieces(entryID));
29516428a1aSJason M. Bills             return false;
29616428a1aSJason M. Bills         }
29716428a1aSJason M. Bills     }
29816428a1aSJason M. Bills     // Timestamp has no index
299c0bd5e4bSEd Tanous     auto [ptr, ec] =
300c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
301c0bd5e4bSEd Tanous     if (ec != std::errc())
30216428a1aSJason M. Bills     {
303ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
304ace85d60SEd Tanous                                        crow::utility::urlFromPieces(entryID));
30516428a1aSJason M. Bills         return false;
30616428a1aSJason M. Bills     }
30716428a1aSJason M. Bills     return true;
30816428a1aSJason M. Bills }
30916428a1aSJason M. Bills 
31095820184SJason M. Bills static bool
31195820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
31295820184SJason M. Bills {
31395820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
31495820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
31595820184SJason M. Bills 
31695820184SJason M. Bills     // Loop through the directory looking for redfish log files
31795820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
31895820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
31995820184SJason M. Bills     {
32095820184SJason M. Bills         // If we find a redfish log file, save the path
32195820184SJason M. Bills         std::string filename = dirEnt.path().filename();
32211ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
32395820184SJason M. Bills         {
32495820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
32595820184SJason M. Bills         }
32695820184SJason M. Bills     }
32795820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
32895820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
32995820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
33095820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
33195820184SJason M. Bills 
33295820184SJason M. Bills     return !redfishLogFiles.empty();
33395820184SJason M. Bills }
33495820184SJason M. Bills 
335aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
3362d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
337c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
338aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
339aefe3786SClaire Weinan {
340aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
341aefe3786SClaire Weinan     {
342aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
343aefe3786SClaire Weinan         {
344aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
345aefe3786SClaire Weinan             {
346aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
347aefe3786SClaire Weinan                 {
348aefe3786SClaire Weinan                     const auto* status =
349aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
350aefe3786SClaire Weinan                     if (status == nullptr)
351aefe3786SClaire Weinan                     {
352aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
353aefe3786SClaire Weinan                         break;
354aefe3786SClaire Weinan                     }
355aefe3786SClaire Weinan                     dumpStatus = *status;
356aefe3786SClaire Weinan                 }
357aefe3786SClaire Weinan             }
358aefe3786SClaire Weinan         }
359aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
360aefe3786SClaire Weinan         {
361aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
362aefe3786SClaire Weinan             {
363aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
364aefe3786SClaire Weinan                 {
365aefe3786SClaire Weinan                     const auto* sizePtr =
366aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
367aefe3786SClaire Weinan                     if (sizePtr == nullptr)
368aefe3786SClaire Weinan                     {
369aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
370aefe3786SClaire Weinan                         break;
371aefe3786SClaire Weinan                     }
372aefe3786SClaire Weinan                     size = *sizePtr;
373aefe3786SClaire Weinan                     break;
374aefe3786SClaire Weinan                 }
375aefe3786SClaire Weinan             }
376aefe3786SClaire Weinan         }
377aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
378aefe3786SClaire Weinan         {
379aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
380aefe3786SClaire Weinan             {
381aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
382aefe3786SClaire Weinan                 {
383aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
384aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
385aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
386aefe3786SClaire Weinan                     {
387aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
388aefe3786SClaire Weinan                         break;
389aefe3786SClaire Weinan                     }
390c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
391aefe3786SClaire Weinan                     break;
392aefe3786SClaire Weinan                 }
393aefe3786SClaire Weinan             }
394aefe3786SClaire Weinan         }
395aefe3786SClaire Weinan     }
396aefe3786SClaire Weinan }
397aefe3786SClaire Weinan 
39821ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
399fdd26906SClaire Weinan {
400fdd26906SClaire Weinan     std::string entriesPath;
401fdd26906SClaire Weinan 
402fdd26906SClaire Weinan     if (dumpType == "BMC")
403fdd26906SClaire Weinan     {
404fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
405fdd26906SClaire Weinan     }
406fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
407fdd26906SClaire Weinan     {
408fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
409fdd26906SClaire Weinan     }
410fdd26906SClaire Weinan     else if (dumpType == "System")
411fdd26906SClaire Weinan     {
412fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
413fdd26906SClaire Weinan     }
414fdd26906SClaire Weinan     else
415fdd26906SClaire Weinan     {
416fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
417fdd26906SClaire Weinan                          << dumpType;
418fdd26906SClaire Weinan     }
419fdd26906SClaire Weinan 
420fdd26906SClaire Weinan     // Returns empty string on error
421fdd26906SClaire Weinan     return entriesPath;
422fdd26906SClaire Weinan }
423fdd26906SClaire Weinan 
4248d1b46d7Szhanghch05 inline void
4258d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4265cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4275cb1dd27SAsmitha Karunanithi {
428fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
429fdd26906SClaire Weinan     if (entriesPath.empty())
4305cb1dd27SAsmitha Karunanithi     {
4315cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4325cb1dd27SAsmitha Karunanithi         return;
4335cb1dd27SAsmitha Karunanithi     }
4345cb1dd27SAsmitha Karunanithi 
4355cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
436fdd26906SClaire Weinan         [asyncResp, entriesPath,
437711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
438711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4395cb1dd27SAsmitha Karunanithi         if (ec)
4405cb1dd27SAsmitha Karunanithi         {
4415cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4425cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4435cb1dd27SAsmitha Karunanithi             return;
4445cb1dd27SAsmitha Karunanithi         }
4455cb1dd27SAsmitha Karunanithi 
446fdd26906SClaire Weinan         // Remove ending slash
447fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
448fdd26906SClaire Weinan         if (!odataIdStr.empty())
449fdd26906SClaire Weinan         {
450fdd26906SClaire Weinan             odataIdStr.pop_back();
451fdd26906SClaire Weinan         }
452fdd26906SClaire Weinan 
453fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
454fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
455fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
456fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
457fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
458fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
459fdd26906SClaire Weinan 
4605cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4615cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
462b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
463b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
464002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
4655cb1dd27SAsmitha Karunanithi 
466002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
467002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
468002d39b4SEd Tanous                                                r.first.filename());
469565dfb6fSClaire Weinan         });
470565dfb6fSClaire Weinan 
4715cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4725cb1dd27SAsmitha Karunanithi         {
473b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4745cb1dd27SAsmitha Karunanithi             {
4755cb1dd27SAsmitha Karunanithi                 continue;
4765cb1dd27SAsmitha Karunanithi             }
477c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
4785cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
47935440d18SAsmitha Karunanithi             std::string dumpStatus;
480433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
4812dfd18efSEd Tanous 
4822dfd18efSEd Tanous             std::string entryID = object.first.filename();
4832dfd18efSEd Tanous             if (entryID.empty())
4845cb1dd27SAsmitha Karunanithi             {
4855cb1dd27SAsmitha Karunanithi                 continue;
4865cb1dd27SAsmitha Karunanithi             }
4875cb1dd27SAsmitha Karunanithi 
488c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
489aefe3786SClaire Weinan                                          asyncResp);
4905cb1dd27SAsmitha Karunanithi 
4910fda0f12SGeorge Liu             if (dumpStatus !=
4920fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
49335440d18SAsmitha Karunanithi                 !dumpStatus.empty())
49435440d18SAsmitha Karunanithi             {
49535440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
49635440d18SAsmitha Karunanithi                 continue;
49735440d18SAsmitha Karunanithi             }
49835440d18SAsmitha Karunanithi 
499647b3cdcSGeorge Liu             thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
500fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
5015cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
5025cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
5035cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
5045cb1dd27SAsmitha Karunanithi 
5055cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5065cb1dd27SAsmitha Karunanithi             {
507c6fecdabSClaire Weinan                 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
508c6fecdabSClaire Weinan                     timestampUs / 1000 / 1000);
509d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
510d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
511fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
512fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5135cb1dd27SAsmitha Karunanithi             }
514c6fecdabSClaire Weinan             else if (dumpType == "FaultLog")
515c6fecdabSClaire Weinan             {
516c6fecdabSClaire Weinan                 thisEntry["Created"] =
517c6fecdabSClaire Weinan                     redfish::time_utils::getDateTimeUintUs(timestampUs);
518c6fecdabSClaire Weinan             }
5195cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5205cb1dd27SAsmitha Karunanithi             {
521c6fecdabSClaire Weinan                 thisEntry["Created"] = redfish::time_utils::getDateTimeUint(
522c6fecdabSClaire Weinan                     timestampUs / 1000 / 1000);
523d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
524d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
525d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
526fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
527fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5285cb1dd27SAsmitha Karunanithi             }
52935440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5305cb1dd27SAsmitha Karunanithi         }
531002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5325cb1dd27SAsmitha Karunanithi         },
5335cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5345cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5355cb1dd27SAsmitha Karunanithi }
5365cb1dd27SAsmitha Karunanithi 
5378d1b46d7Szhanghch05 inline void
538c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5398d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5405cb1dd27SAsmitha Karunanithi {
541fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
542fdd26906SClaire Weinan     if (entriesPath.empty())
5435cb1dd27SAsmitha Karunanithi     {
5445cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5455cb1dd27SAsmitha Karunanithi         return;
5465cb1dd27SAsmitha Karunanithi     }
5475cb1dd27SAsmitha Karunanithi 
5485cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
549fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
550fdd26906SClaire Weinan          entriesPath](const boost::system::error_code ec,
55102cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5525cb1dd27SAsmitha Karunanithi         if (ec)
5535cb1dd27SAsmitha Karunanithi         {
5545cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5555cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5565cb1dd27SAsmitha Karunanithi             return;
5575cb1dd27SAsmitha Karunanithi         }
5585cb1dd27SAsmitha Karunanithi 
559b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
560b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
561b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
562002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
563b47452b2SAsmitha Karunanithi 
5649eb808c1SEd Tanous         for (const auto& objectPath : resp)
5655cb1dd27SAsmitha Karunanithi         {
566b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5675cb1dd27SAsmitha Karunanithi             {
5685cb1dd27SAsmitha Karunanithi                 continue;
5695cb1dd27SAsmitha Karunanithi             }
5705cb1dd27SAsmitha Karunanithi 
5715cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
572c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5735cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
57435440d18SAsmitha Karunanithi             std::string dumpStatus;
5755cb1dd27SAsmitha Karunanithi 
576aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
577c6fecdabSClaire Weinan                                          timestampUs, asyncResp);
5785cb1dd27SAsmitha Karunanithi 
5790fda0f12SGeorge Liu             if (dumpStatus !=
5800fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
58135440d18SAsmitha Karunanithi                 !dumpStatus.empty())
58235440d18SAsmitha Karunanithi             {
58335440d18SAsmitha Karunanithi                 // Dump status is not Complete
58435440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
585*d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
586*d1bde9e5SKrzysztof Grobelny                                            entryID);
58735440d18SAsmitha Karunanithi                 return;
58835440d18SAsmitha Karunanithi             }
58935440d18SAsmitha Karunanithi 
5905cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
591647b3cdcSGeorge Liu                 "#LogEntry.v1_8_0.LogEntry";
592fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
5935cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
5945cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
5955cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
5965cb1dd27SAsmitha Karunanithi 
5975cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5985cb1dd27SAsmitha Karunanithi             {
599c6fecdabSClaire Weinan                 asyncResp->res.jsonValue["Created"] =
600c6fecdabSClaire Weinan                     redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
601c6fecdabSClaire Weinan                                                          1000);
602d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
603d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
604fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
605fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6065cb1dd27SAsmitha Karunanithi             }
607c6fecdabSClaire Weinan             else if (dumpType == "FaultLog")
608c6fecdabSClaire Weinan             {
609c6fecdabSClaire Weinan                 asyncResp->res.jsonValue["Created"] =
610c6fecdabSClaire Weinan                     redfish::time_utils::getDateTimeUintUs(timestampUs);
611c6fecdabSClaire Weinan             }
6125cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6135cb1dd27SAsmitha Karunanithi             {
614c6fecdabSClaire Weinan                 asyncResp->res.jsonValue["Created"] =
615c6fecdabSClaire Weinan                     redfish::time_utils::getDateTimeUint(timestampUs / 1000 /
616c6fecdabSClaire Weinan                                                          1000);
617d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
618002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
619d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
620fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
621fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6225cb1dd27SAsmitha Karunanithi             }
6235cb1dd27SAsmitha Karunanithi         }
624e05aec50SEd Tanous         if (!foundDumpEntry)
625b47452b2SAsmitha Karunanithi         {
626b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
627b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
628b47452b2SAsmitha Karunanithi             return;
629b47452b2SAsmitha Karunanithi         }
6305cb1dd27SAsmitha Karunanithi         },
6315cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6325cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6335cb1dd27SAsmitha Karunanithi }
6345cb1dd27SAsmitha Karunanithi 
6358d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6369878256fSStanley Chu                             const std::string& entryID,
637b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6385cb1dd27SAsmitha Karunanithi {
639002d39b4SEd Tanous     auto respHandler =
640002d39b4SEd Tanous         [asyncResp, entryID](const boost::system::error_code ec) {
6415cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6425cb1dd27SAsmitha Karunanithi         if (ec)
6435cb1dd27SAsmitha Karunanithi         {
6443de8d8baSGeorge Liu             if (ec.value() == EBADR)
6453de8d8baSGeorge Liu             {
6463de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6473de8d8baSGeorge Liu                 return;
6483de8d8baSGeorge Liu             }
6495cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
650fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6515cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6525cb1dd27SAsmitha Karunanithi             return;
6535cb1dd27SAsmitha Karunanithi         }
6545cb1dd27SAsmitha Karunanithi     };
6555cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6565cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
657b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
658b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
659b47452b2SAsmitha Karunanithi             entryID,
6605cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6615cb1dd27SAsmitha Karunanithi }
6625cb1dd27SAsmitha Karunanithi 
6638d1b46d7Szhanghch05 inline void
66498be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
6658d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6668d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
667a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
668a43be80fSAsmitha Karunanithi {
669a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
67059d494eeSPatrick Williams         [dumpId, dumpPath,
67159d494eeSPatrick Williams          dumpType](boost::system::error_code err, sdbusplus::message_t& m,
672a43be80fSAsmitha Karunanithi                    const std::shared_ptr<task::TaskData>& taskData) {
673cb13a392SEd Tanous         if (err)
674cb13a392SEd Tanous         {
6756145ed6fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Error in creating a dump";
6766145ed6fSAsmitha Karunanithi             taskData->state = "Cancelled";
6776145ed6fSAsmitha Karunanithi             return task::completed;
678cb13a392SEd Tanous         }
679b9d36b47SEd Tanous 
680b9d36b47SEd Tanous         dbus::utility::DBusInteracesMap interfacesList;
681a43be80fSAsmitha Karunanithi 
682a43be80fSAsmitha Karunanithi         sdbusplus::message::object_path objPath;
683a43be80fSAsmitha Karunanithi 
684a43be80fSAsmitha Karunanithi         m.read(objPath, interfacesList);
685a43be80fSAsmitha Karunanithi 
686b47452b2SAsmitha Karunanithi         if (objPath.str ==
687b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
688b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
689b47452b2SAsmitha Karunanithi                 "/entry/" + std::to_string(dumpId))
690a43be80fSAsmitha Karunanithi         {
691a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
692a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
693a43be80fSAsmitha Karunanithi 
694a43be80fSAsmitha Karunanithi             std::string headerLoc =
695a43be80fSAsmitha Karunanithi                 "Location: " + dumpPath + std::to_string(dumpId);
696002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
697a43be80fSAsmitha Karunanithi 
698a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
699b47452b2SAsmitha Karunanithi             return task::completed;
7006145ed6fSAsmitha Karunanithi         }
701a43be80fSAsmitha Karunanithi         return task::completed;
702a43be80fSAsmitha Karunanithi         },
7034978b63fSJason M. Bills         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
704a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
705a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
706a43be80fSAsmitha Karunanithi 
707a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
708a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
70998be3e39SEd Tanous     task->payload.emplace(std::move(payload));
710a43be80fSAsmitha Karunanithi }
711a43be80fSAsmitha Karunanithi 
7128d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7138d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
714a43be80fSAsmitha Karunanithi {
715fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
716fdd26906SClaire Weinan     if (dumpPath.empty())
717a43be80fSAsmitha Karunanithi     {
718a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
719a43be80fSAsmitha Karunanithi         return;
720a43be80fSAsmitha Karunanithi     }
721a43be80fSAsmitha Karunanithi 
722a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
723a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
724a43be80fSAsmitha Karunanithi 
72515ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
726a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
727a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
728a43be80fSAsmitha Karunanithi     {
729a43be80fSAsmitha Karunanithi         return;
730a43be80fSAsmitha Karunanithi     }
731a43be80fSAsmitha Karunanithi 
732a43be80fSAsmitha Karunanithi     if (dumpType == "System")
733a43be80fSAsmitha Karunanithi     {
734a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
735a43be80fSAsmitha Karunanithi         {
7364978b63fSJason M. Bills             BMCWEB_LOG_ERROR
7374978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
738a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
739a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
740a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
741a43be80fSAsmitha Karunanithi             return;
742a43be80fSAsmitha Karunanithi         }
7433174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
744a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
745a43be80fSAsmitha Karunanithi         {
746a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
747ace85d60SEd Tanous             messages::internalError(asyncResp->res);
748a43be80fSAsmitha Karunanithi             return;
749a43be80fSAsmitha Karunanithi         }
7505907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
751a43be80fSAsmitha Karunanithi     }
752a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
753a43be80fSAsmitha Karunanithi     {
754a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
755a43be80fSAsmitha Karunanithi         {
7560fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
7570fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
758a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
759a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
760a43be80fSAsmitha Karunanithi             return;
761a43be80fSAsmitha Karunanithi         }
7623174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
763a43be80fSAsmitha Karunanithi         {
764a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
765a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
766ace85d60SEd Tanous             messages::internalError(asyncResp->res);
767a43be80fSAsmitha Karunanithi             return;
768a43be80fSAsmitha Karunanithi         }
7695907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
7705907571dSAsmitha Karunanithi     }
7715907571dSAsmitha Karunanithi     else
7725907571dSAsmitha Karunanithi     {
7735907571dSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
7745907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
7755907571dSAsmitha Karunanithi         return;
776a43be80fSAsmitha Karunanithi     }
777a43be80fSAsmitha Karunanithi 
778a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
77998be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
78098be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
7815907571dSAsmitha Karunanithi                    const sdbusplus::message::message& msg,
78298be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
783a43be80fSAsmitha Karunanithi         if (ec)
784a43be80fSAsmitha Karunanithi         {
785a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
7865907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
7875907571dSAsmitha Karunanithi             if (dbusError == nullptr)
7885907571dSAsmitha Karunanithi             {
7895907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
7905907571dSAsmitha Karunanithi                 return;
7915907571dSAsmitha Karunanithi             }
7925907571dSAsmitha Karunanithi 
7935907571dSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
7945907571dSAsmitha Karunanithi                              << " and error msg: " << dbusError->message;
7955907571dSAsmitha Karunanithi             if (std::string_view(
7965907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
7975907571dSAsmitha Karunanithi                 dbusError->name)
7985907571dSAsmitha Karunanithi             {
7995907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
8005907571dSAsmitha Karunanithi                 return;
8015907571dSAsmitha Karunanithi             }
8025907571dSAsmitha Karunanithi             if (std::string_view(
8035907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
8045907571dSAsmitha Karunanithi                 dbusError->name)
8055907571dSAsmitha Karunanithi             {
8065907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
8075907571dSAsmitha Karunanithi                 return;
8085907571dSAsmitha Karunanithi             }
8095907571dSAsmitha Karunanithi             if (std::string_view(
8105907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
8115907571dSAsmitha Karunanithi                 dbusError->name)
8125907571dSAsmitha Karunanithi             {
8135907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
8145907571dSAsmitha Karunanithi                 return;
8155907571dSAsmitha Karunanithi             }
8165907571dSAsmitha Karunanithi             // Other Dbus errors such as:
8175907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
8185907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
8195907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
8205907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
8215907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
8225907571dSAsmitha Karunanithi             // back to the client.
823a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
824a43be80fSAsmitha Karunanithi             return;
825a43be80fSAsmitha Karunanithi         }
826a43be80fSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
827a43be80fSAsmitha Karunanithi 
828002d39b4SEd Tanous         createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath,
829002d39b4SEd Tanous                                dumpType);
830a43be80fSAsmitha Karunanithi         },
831b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
832b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
833b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
834a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
835a43be80fSAsmitha Karunanithi }
836a43be80fSAsmitha Karunanithi 
8378d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8388d1b46d7Szhanghch05                       const std::string& dumpType)
83980319af1SAsmitha Karunanithi {
840b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
841b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8428d1b46d7Szhanghch05 
84380319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
844b9d36b47SEd Tanous         [asyncResp, dumpType](
845b9d36b47SEd Tanous             const boost::system::error_code ec,
846b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
84780319af1SAsmitha Karunanithi         if (ec)
84880319af1SAsmitha Karunanithi         {
84980319af1SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
85080319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
85180319af1SAsmitha Karunanithi             return;
85280319af1SAsmitha Karunanithi         }
85380319af1SAsmitha Karunanithi 
85480319af1SAsmitha Karunanithi         for (const std::string& path : subTreePaths)
85580319af1SAsmitha Karunanithi         {
8562dfd18efSEd Tanous             sdbusplus::message::object_path objPath(path);
8572dfd18efSEd Tanous             std::string logID = objPath.filename();
8582dfd18efSEd Tanous             if (logID.empty())
85980319af1SAsmitha Karunanithi             {
8602dfd18efSEd Tanous                 continue;
86180319af1SAsmitha Karunanithi             }
8622dfd18efSEd Tanous             deleteDumpEntry(asyncResp, logID, dumpType);
86380319af1SAsmitha Karunanithi         }
86480319af1SAsmitha Karunanithi         },
86580319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
86680319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
86780319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
868b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
869b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
870b47452b2SAsmitha Karunanithi                                    dumpType});
87180319af1SAsmitha Karunanithi }
87280319af1SAsmitha Karunanithi 
873b9d36b47SEd Tanous inline static void
874b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
875b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
876b9d36b47SEd Tanous                              std::string& logfile)
877043a0536SJohnathan Mantey {
878*d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
879*d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
880*d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
881*d1bde9e5SKrzysztof Grobelny 
882*d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
883*d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
884*d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
885*d1bde9e5SKrzysztof Grobelny 
886*d1bde9e5SKrzysztof Grobelny     if (!success)
887043a0536SJohnathan Mantey     {
888*d1bde9e5SKrzysztof Grobelny         return;
889043a0536SJohnathan Mantey     }
890*d1bde9e5SKrzysztof Grobelny 
891*d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
892043a0536SJohnathan Mantey     {
893*d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
894*d1bde9e5SKrzysztof Grobelny     }
895*d1bde9e5SKrzysztof Grobelny 
896*d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
897043a0536SJohnathan Mantey     {
898*d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
899043a0536SJohnathan Mantey     }
900*d1bde9e5SKrzysztof Grobelny 
901*d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
902043a0536SJohnathan Mantey     {
903*d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
904043a0536SJohnathan Mantey     }
905043a0536SJohnathan Mantey }
906043a0536SJohnathan Mantey 
907a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
9087e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9091da66f75SEd Tanous {
910c4bf6374SJason M. Bills     /**
911c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
912c4bf6374SJason M. Bills      */
9137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
914ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
915002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
916002d39b4SEd Tanous             [&app](const crow::Request& req,
917002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9183ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
919c4bf6374SJason M. Bills         {
92045ca1b86SEd Tanous             return;
92145ca1b86SEd Tanous         }
9227e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
9237e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
924c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
925c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
926c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
927029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
92845ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
929c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
930c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
931002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
932c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
9331476687dSEd Tanous         nlohmann::json::object_t eventLog;
9341476687dSEd Tanous         eventLog["@odata.id"] =
9351476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
9361476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
9375cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
9381476687dSEd Tanous         nlohmann::json::object_t dumpLog;
939002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
9401476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
941c9bb6861Sraviteja-b #endif
942c9bb6861Sraviteja-b 
943d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
9441476687dSEd Tanous         nlohmann::json::object_t crashdump;
9451476687dSEd Tanous         crashdump["@odata.id"] =
9461476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
9471476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
948d53dd41fSJason M. Bills #endif
949b7028ebfSSpencer Ku 
950b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
9511476687dSEd Tanous         nlohmann::json::object_t hostlogger;
9521476687dSEd Tanous         hostlogger["@odata.id"] =
9531476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
9541476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
955b7028ebfSSpencer Ku #endif
956c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
957c4bf6374SJason M. Bills             logServiceArray.size();
958a3316fc6SZhikuiRen 
959a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
96045ca1b86SEd Tanous             [asyncResp](const boost::system::error_code ec,
961b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
962b9d36b47SEd Tanous                             subtreePath) {
963a3316fc6SZhikuiRen             if (ec)
964a3316fc6SZhikuiRen             {
965a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
966a3316fc6SZhikuiRen                 return;
967a3316fc6SZhikuiRen             }
968a3316fc6SZhikuiRen 
96955f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
970a3316fc6SZhikuiRen             {
971a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
972a3316fc6SZhikuiRen                 {
97323a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
974a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
97523a21a1cSEd Tanous                     logServiceArrayLocal.push_back(
9760fda0f12SGeorge Liu                         {{"@odata.id",
9770fda0f12SGeorge Liu                           "/redfish/v1/Systems/system/LogServices/PostCodes"}});
97845ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
97923a21a1cSEd Tanous                         logServiceArrayLocal.size();
980a3316fc6SZhikuiRen                     return;
981a3316fc6SZhikuiRen                 }
982a3316fc6SZhikuiRen             }
983a3316fc6SZhikuiRen             },
984a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
985a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
98645ca1b86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
98745ca1b86SEd Tanous             std::array<const char*, 1>{postCodeIface});
9887e860f15SJohn Edward Broadbent         });
989c4bf6374SJason M. Bills }
990c4bf6374SJason M. Bills 
9917e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
992c4bf6374SJason M. Bills {
9937e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
994ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
995002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
996002d39b4SEd Tanous             [&app](const crow::Request& req,
997002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9983ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
99945ca1b86SEd Tanous         {
100045ca1b86SEd Tanous             return;
100145ca1b86SEd Tanous         }
1002c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1003029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1004c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1005c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1006c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1007002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1008c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1009c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10107c8c4058STejas Patil 
10117c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
10122b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
10137c8c4058STejas Patil 
10147c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10157c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10167c8c4058STejas Patil             redfishDateTimeOffset.second;
10177c8c4058STejas Patil 
10181476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
10191476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1020e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1021e7d6c8b2SGunnar Mills 
10220fda0f12SGeorge Liu             {"target",
10230fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10247e860f15SJohn Edward Broadbent         });
1025489640c6SJason M. Bills }
1026489640c6SJason M. Bills 
10277e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1028489640c6SJason M. Bills {
10294978b63fSJason M. Bills     BMCWEB_ROUTE(
10304978b63fSJason M. Bills         app,
10314978b63fSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
1032432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10337e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
103445ca1b86SEd Tanous             [&app](const crow::Request& req,
10357e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
10363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
103745ca1b86SEd Tanous         {
103845ca1b86SEd Tanous             return;
103945ca1b86SEd Tanous         }
1040489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1041489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1042489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1043489640c6SJason M. Bills         {
1044489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1045489640c6SJason M. Bills             {
1046489640c6SJason M. Bills                 std::error_code ec;
1047489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1048489640c6SJason M. Bills             }
1049489640c6SJason M. Bills         }
1050489640c6SJason M. Bills 
1051489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1052489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1053489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1054489640c6SJason M. Bills             if (ec)
1055489640c6SJason M. Bills             {
1056002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1057489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1058489640c6SJason M. Bills                 return;
1059489640c6SJason M. Bills             }
1060489640c6SJason M. Bills 
1061489640c6SJason M. Bills             messages::success(asyncResp->res);
1062489640c6SJason M. Bills             },
1063489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1064002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1065002d39b4SEd Tanous             "replace");
10667e860f15SJohn Edward Broadbent         });
1067c4bf6374SJason M. Bills }
1068c4bf6374SJason M. Bills 
1069ac992cdeSJason M. Bills enum class LogParseError
1070ac992cdeSJason M. Bills {
1071ac992cdeSJason M. Bills     success,
1072ac992cdeSJason M. Bills     parseFailed,
1073ac992cdeSJason M. Bills     messageIdNotInRegistry,
1074ac992cdeSJason M. Bills };
1075ac992cdeSJason M. Bills 
1076ac992cdeSJason M. Bills static LogParseError
1077ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1078b5a76932SEd Tanous                           const std::string& logEntry,
1079de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1080c4bf6374SJason M. Bills {
108195820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1082cd225da8SJason M. Bills     // First get the Timestamp
1083f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1084cd225da8SJason M. Bills     if (space == std::string::npos)
108595820184SJason M. Bills     {
1086ac992cdeSJason M. Bills         return LogParseError::parseFailed;
108795820184SJason M. Bills     }
1088cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1089cd225da8SJason M. Bills     // Then get the log contents
1090f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1091cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1092cd225da8SJason M. Bills     {
1093ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1094cd225da8SJason M. Bills     }
1095cd225da8SJason M. Bills     std::string_view entry(logEntry);
1096cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1097cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1098cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1099cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1100cd225da8SJason M. Bills                  boost::token_compress_on);
1101cd225da8SJason M. Bills     // We need at least a MessageId to be valid
110226f6976fSEd Tanous     if (logEntryFields.empty())
1103cd225da8SJason M. Bills     {
1104ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1105cd225da8SJason M. Bills     }
1106cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
110795820184SJason M. Bills 
11084851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1109fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1110c4bf6374SJason M. Bills 
111154417b02SSui Chen     if (message == nullptr)
1112c4bf6374SJason M. Bills     {
111354417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1114ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1115c4bf6374SJason M. Bills     }
1116c4bf6374SJason M. Bills 
111754417b02SSui Chen     std::string msg = message->message;
111854417b02SSui Chen 
111915a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
112026702d01SEd Tanous     std::span<std::string> messageArgs;
112115a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
112215a86ff6SJason M. Bills     {
112315a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
112415a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
112515a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
112615a86ff6SJason M. Bills         if (!messageArgsStart.empty())
112715a86ff6SJason M. Bills         {
112815a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
112915a86ff6SJason M. Bills         }
113015a86ff6SJason M. Bills 
113123a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1132c4bf6374SJason M. Bills 
11334851d45dSJason M. Bills         // Fill the MessageArgs into the Message
113495820184SJason M. Bills         int i = 0;
113595820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11364851d45dSJason M. Bills         {
113795820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11384851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11394851d45dSJason M. Bills             if (argPos != std::string::npos)
11404851d45dSJason M. Bills             {
114195820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11424851d45dSJason M. Bills             }
11434851d45dSJason M. Bills         }
114415a86ff6SJason M. Bills     }
11454851d45dSJason M. Bills 
114695820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
114795820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
114895820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1149f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1150f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
115195820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1152c4bf6374SJason M. Bills     {
115395820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1154c4bf6374SJason M. Bills     }
1155c4bf6374SJason M. Bills 
1156c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
115784afc48bSJason M. Bills     logEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
115884afc48bSJason M. Bills     logEntryJson["@odata.id"] =
115984afc48bSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
116084afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
116184afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
116284afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
116384afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
116484afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
116584afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
116684afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
116784afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1168ac992cdeSJason M. Bills     return LogParseError::success;
1169c4bf6374SJason M. Bills }
1170c4bf6374SJason M. Bills 
11717e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1172c4bf6374SJason M. Bills {
11737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11747e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11758b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1176002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1177002d39b4SEd Tanous             [&app](const crow::Request& req,
1178002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1179c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1180c937d2bfSEd Tanous             .canDelegateTop = true,
1181c937d2bfSEd Tanous             .canDelegateSkip = true,
1182c937d2bfSEd Tanous         };
1183c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1184c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
11853ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1186c4bf6374SJason M. Bills         {
1187c4bf6374SJason M. Bills             return;
1188c4bf6374SJason M. Bills         }
11895143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
11903648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
11913648c8beSEd Tanous 
11927e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
11937e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1194c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1195c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1196c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1197029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1198c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1199c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1200c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1201cb92c03bSAndrew Geissler 
12024978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1203c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
12047e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
12057e860f15SJohn Edward Broadbent         // entry
120695820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
120795820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1208b01bf299SEd Tanous         uint64_t entryCount = 0;
1209cd225da8SJason M. Bills         std::string logEntry;
121095820184SJason M. Bills 
12117e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
12127e860f15SJohn Edward Broadbent         // backwards
1213002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1214002d39b4SEd Tanous              it++)
1215c4bf6374SJason M. Bills         {
1216cd225da8SJason M. Bills             std::ifstream logStream(*it);
121795820184SJason M. Bills             if (!logStream.is_open())
1218c4bf6374SJason M. Bills             {
1219c4bf6374SJason M. Bills                 continue;
1220c4bf6374SJason M. Bills             }
1221c4bf6374SJason M. Bills 
1222e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1223e85d6b16SJason M. Bills             bool firstEntry = true;
122495820184SJason M. Bills             while (std::getline(logStream, logEntry))
122595820184SJason M. Bills             {
1226c4bf6374SJason M. Bills                 std::string idStr;
1227e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1228c4bf6374SJason M. Bills                 {
1229c4bf6374SJason M. Bills                     continue;
1230c4bf6374SJason M. Bills                 }
1231e85d6b16SJason M. Bills                 firstEntry = false;
1232e85d6b16SJason M. Bills 
1233de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1234ac992cdeSJason M. Bills                 LogParseError status =
1235ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1236ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1237ac992cdeSJason M. Bills                 {
1238ac992cdeSJason M. Bills                     continue;
1239ac992cdeSJason M. Bills                 }
1240ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1241c4bf6374SJason M. Bills                 {
1242c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1243c4bf6374SJason M. Bills                     return;
1244c4bf6374SJason M. Bills                 }
1245de703c5dSJason M. Bills 
1246de703c5dSJason M. Bills                 entryCount++;
1247de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1248de703c5dSJason M. Bills                 // start) and top (number of entries to display)
12493648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1250de703c5dSJason M. Bills                 {
1251de703c5dSJason M. Bills                     continue;
1252de703c5dSJason M. Bills                 }
1253de703c5dSJason M. Bills 
1254de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1255c4bf6374SJason M. Bills             }
125695820184SJason M. Bills         }
1257c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
12583648c8beSEd Tanous         if (skip + top < entryCount)
1259c4bf6374SJason M. Bills         {
1260c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
12614978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
12623648c8beSEd Tanous                 std::to_string(skip + top);
1263c4bf6374SJason M. Bills         }
12647e860f15SJohn Edward Broadbent         });
1265897967deSJason M. Bills }
1266897967deSJason M. Bills 
12677e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1268897967deSJason M. Bills {
12697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12707e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1271ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
127345ca1b86SEd Tanous             [&app](const crow::Request& req,
12747e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12757e860f15SJohn Edward Broadbent                    const std::string& param) {
12763ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
127745ca1b86SEd Tanous         {
127845ca1b86SEd Tanous             return;
127945ca1b86SEd Tanous         }
12807e860f15SJohn Edward Broadbent         const std::string& targetID = param;
12818d1b46d7Szhanghch05 
12827e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
12837e860f15SJohn Edward Broadbent         // entry to find the target entry
1284897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1285897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1286897967deSJason M. Bills         std::string logEntry;
1287897967deSJason M. Bills 
12887e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
12897e860f15SJohn Edward Broadbent         // backwards
1290002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1291002d39b4SEd Tanous              it++)
1292897967deSJason M. Bills         {
1293897967deSJason M. Bills             std::ifstream logStream(*it);
1294897967deSJason M. Bills             if (!logStream.is_open())
1295897967deSJason M. Bills             {
1296897967deSJason M. Bills                 continue;
1297897967deSJason M. Bills             }
1298897967deSJason M. Bills 
1299897967deSJason M. Bills             // Reset the unique ID on the first entry
1300897967deSJason M. Bills             bool firstEntry = true;
1301897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1302897967deSJason M. Bills             {
1303897967deSJason M. Bills                 std::string idStr;
1304897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1305897967deSJason M. Bills                 {
1306897967deSJason M. Bills                     continue;
1307897967deSJason M. Bills                 }
1308897967deSJason M. Bills                 firstEntry = false;
1309897967deSJason M. Bills 
1310897967deSJason M. Bills                 if (idStr == targetID)
1311897967deSJason M. Bills                 {
1312de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1313ac992cdeSJason M. Bills                     LogParseError status =
1314ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1315ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1316897967deSJason M. Bills                     {
1317897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1318897967deSJason M. Bills                         return;
1319897967deSJason M. Bills                     }
1320d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1321897967deSJason M. Bills                     return;
1322897967deSJason M. Bills                 }
1323897967deSJason M. Bills             }
1324897967deSJason M. Bills         }
1325897967deSJason M. Bills         // Requested ID was not found
1326002d39b4SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
1327002d39b4SEd Tanous                                        crow::utility::urlFromPieces(targetID));
13287e860f15SJohn Edward Broadbent         });
132908a4e4b5SAnthony Wilson }
133008a4e4b5SAnthony Wilson 
13317e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
133208a4e4b5SAnthony Wilson {
13337e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13347e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1335ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1336002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1337002d39b4SEd Tanous             [&app](const crow::Request& req,
1338002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
13393ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
134045ca1b86SEd Tanous         {
134145ca1b86SEd Tanous             return;
134245ca1b86SEd Tanous         }
13437e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13447e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
134508a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
134608a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
134708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
134808a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
134908a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
135008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
135108a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
135208a4e4b5SAnthony Wilson 
1353cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1354cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1355cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1356cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1357914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1358cb92c03bSAndrew Geissler             if (ec)
1359cb92c03bSAndrew Geissler             {
1360cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1361cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1362002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1363cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1364cb92c03bSAndrew Geissler                 return;
1365cb92c03bSAndrew Geissler             }
1366002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1367cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
13689eb808c1SEd Tanous             for (const auto& objectPath : resp)
1369cb92c03bSAndrew Geissler             {
1370914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1371c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1372c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1373914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1374914e2d5dSEd Tanous                 const std::string* message = nullptr;
1375914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
137675710de2SXiaochao Ma                 bool resolved = false;
13779eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1378f86bb901SAdriana Kobylak                 {
1379f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1380f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1381f86bb901SAdriana Kobylak                     {
1382002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1383cb92c03bSAndrew Geissler                         {
1384cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1385cb92c03bSAndrew Geissler                             {
1386002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1387cb92c03bSAndrew Geissler                             }
1388cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1389cb92c03bSAndrew Geissler                             {
1390002d39b4SEd Tanous                                 timestamp =
1391002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13927e860f15SJohn Edward Broadbent                             }
1393002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
13947e860f15SJohn Edward Broadbent                             {
1395002d39b4SEd Tanous                                 updateTimestamp =
1396002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13977e860f15SJohn Edward Broadbent                             }
13987e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
13997e860f15SJohn Edward Broadbent                             {
14007e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
14017e860f15SJohn Edward Broadbent                                     &propertyMap.second);
14027e860f15SJohn Edward Broadbent                             }
14037e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
14047e860f15SJohn Edward Broadbent                             {
14057e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
14067e860f15SJohn Edward Broadbent                                     &propertyMap.second);
14077e860f15SJohn Edward Broadbent                             }
14087e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
14097e860f15SJohn Edward Broadbent                             {
1410914e2d5dSEd Tanous                                 const bool* resolveptr =
1411002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
14127e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
14137e860f15SJohn Edward Broadbent                                 {
1414002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
14157e860f15SJohn Edward Broadbent                                     return;
14167e860f15SJohn Edward Broadbent                                 }
14177e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
14187e860f15SJohn Edward Broadbent                             }
14197e860f15SJohn Edward Broadbent                         }
14207e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
14217e860f15SJohn Edward Broadbent                             severity == nullptr)
14227e860f15SJohn Edward Broadbent                         {
14237e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
14247e860f15SJohn Edward Broadbent                             return;
14257e860f15SJohn Edward Broadbent                         }
14267e860f15SJohn Edward Broadbent                     }
14277e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
14287e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
14297e860f15SJohn Edward Broadbent                     {
1430002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
14317e860f15SJohn Edward Broadbent                         {
14327e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
14337e860f15SJohn Edward Broadbent                             {
14347e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
14357e860f15SJohn Edward Broadbent                                     &propertyMap.second);
14367e860f15SJohn Edward Broadbent                             }
14377e860f15SJohn Edward Broadbent                         }
14387e860f15SJohn Edward Broadbent                     }
14397e860f15SJohn Edward Broadbent                 }
14407e860f15SJohn Edward Broadbent                 // Object path without the
14417e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
14427e860f15SJohn Edward Broadbent                 // and continue.
14437e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1444c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1445c419c759SEd Tanous                     updateTimestamp == nullptr)
14467e860f15SJohn Edward Broadbent                 {
14477e860f15SJohn Edward Broadbent                     continue;
14487e860f15SJohn Edward Broadbent                 }
14497e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
14507e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
14517e860f15SJohn Edward Broadbent                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14527e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
14530fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14547e860f15SJohn Edward Broadbent                     std::to_string(*id);
14557e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
14567e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
14577e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
14587e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
14597e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
14607e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
14617e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
14627e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
14632b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
14647e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
14652b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
14667e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
14677e860f15SJohn Edward Broadbent                 {
14687e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
14690fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14707e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
14717e860f15SJohn Edward Broadbent                 }
14727e860f15SJohn Edward Broadbent             }
1473002d39b4SEd Tanous             std::sort(
1474002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1475002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
14767e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
14777e860f15SJohn Edward Broadbent                 });
14787e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
14797e860f15SJohn Edward Broadbent                 entriesArray.size();
14807e860f15SJohn Edward Broadbent             },
14817e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
14827e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14837e860f15SJohn Edward Broadbent         });
14847e860f15SJohn Edward Broadbent }
14857e860f15SJohn Edward Broadbent 
14867e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
14877e860f15SJohn Edward Broadbent {
14887e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
14897e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1490ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1491002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1492002d39b4SEd Tanous             [&app](const crow::Request& req,
14937e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
149445ca1b86SEd Tanous                    const std::string& param) {
14953ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
14967e860f15SJohn Edward Broadbent         {
149745ca1b86SEd Tanous             return;
149845ca1b86SEd Tanous         }
14997e860f15SJohn Edward Broadbent         std::string entryID = param;
15007e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
15017e860f15SJohn Edward Broadbent 
15027e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
15037e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1504*d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1505*d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1506*d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
1507002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1508b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
15097e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
15107e860f15SJohn Edward Broadbent             {
1511*d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1512*d1bde9e5SKrzysztof Grobelny                                            entryID);
15137e860f15SJohn Edward Broadbent                 return;
15147e860f15SJohn Edward Broadbent             }
15157e860f15SJohn Edward Broadbent             if (ec)
15167e860f15SJohn Edward Broadbent             {
15170fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1518002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
15197e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
15207e860f15SJohn Edward Broadbent                 return;
15217e860f15SJohn Edward Broadbent             }
1522914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1523c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1524c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1525914e2d5dSEd Tanous             const std::string* severity = nullptr;
1526914e2d5dSEd Tanous             const std::string* message = nullptr;
1527914e2d5dSEd Tanous             const std::string* filePath = nullptr;
15287e860f15SJohn Edward Broadbent             bool resolved = false;
15297e860f15SJohn Edward Broadbent 
1530*d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1531*d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1532*d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
1533*d1bde9e5SKrzysztof Grobelny                 severity, "Message", message, "Resolved", resolved, "Path",
1534*d1bde9e5SKrzysztof Grobelny                 filePath);
1535*d1bde9e5SKrzysztof Grobelny 
1536*d1bde9e5SKrzysztof Grobelny             if (!success)
153775710de2SXiaochao Ma             {
153875710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
153975710de2SXiaochao Ma                 return;
154075710de2SXiaochao Ma             }
1541*d1bde9e5SKrzysztof Grobelny 
1542002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
1543002d39b4SEd Tanous                 timestamp == nullptr || updateTimestamp == nullptr)
1544f86bb901SAdriana Kobylak             {
1545ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1546271584abSEd Tanous                 return;
1547271584abSEd Tanous             }
1548f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
1549f86bb901SAdriana Kobylak                 "#LogEntry.v1_8_0.LogEntry";
1550f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
15510fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1552f86bb901SAdriana Kobylak                 std::to_string(*id);
155345ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1554f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1555f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1556f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
1557f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1558f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1559f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1560f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
15612b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1562f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
15632b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1564f86bb901SAdriana Kobylak             if (filePath != nullptr)
1565f86bb901SAdriana Kobylak             {
1566f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1567e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1568e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1569f86bb901SAdriana Kobylak             }
1570*d1bde9e5SKrzysztof Grobelny             });
15717e860f15SJohn Edward Broadbent         });
1572336e96c6SChicago Duan 
15737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15747e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1575ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
15767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
157745ca1b86SEd Tanous             [&app](const crow::Request& req,
15787e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15797e860f15SJohn Edward Broadbent                    const std::string& entryId) {
15803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
158145ca1b86SEd Tanous         {
158245ca1b86SEd Tanous             return;
158345ca1b86SEd Tanous         }
158475710de2SXiaochao Ma         std::optional<bool> resolved;
158575710de2SXiaochao Ma 
158615ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
15877e860f15SJohn Edward Broadbent                                       resolved))
158875710de2SXiaochao Ma         {
158975710de2SXiaochao Ma             return;
159075710de2SXiaochao Ma         }
159175710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
159275710de2SXiaochao Ma 
159375710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
15944f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
159575710de2SXiaochao Ma             if (ec)
159675710de2SXiaochao Ma             {
159775710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
159875710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
159975710de2SXiaochao Ma                 return;
160075710de2SXiaochao Ma             }
160175710de2SXiaochao Ma             },
160275710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
160375710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
160475710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
160575710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1606168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
16077e860f15SJohn Edward Broadbent         });
160875710de2SXiaochao Ma 
16097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16107e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1611ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1612ed398213SEd Tanous 
1613002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1614002d39b4SEd Tanous             [&app](const crow::Request& req,
1615002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
161645ca1b86SEd Tanous                    const std::string& param) {
16173ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1618336e96c6SChicago Duan         {
161945ca1b86SEd Tanous             return;
162045ca1b86SEd Tanous         }
1621336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1622336e96c6SChicago Duan 
16237e860f15SJohn Edward Broadbent         std::string entryID = param;
1624336e96c6SChicago Duan 
1625336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1626336e96c6SChicago Duan 
1627336e96c6SChicago Duan         // Process response from Logging service.
1628002d39b4SEd Tanous         auto respHandler =
1629002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1630002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1631336e96c6SChicago Duan             if (ec)
1632336e96c6SChicago Duan             {
16333de8d8baSGeorge Liu                 if (ec.value() == EBADR)
16343de8d8baSGeorge Liu                 {
163545ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
163645ca1b86SEd Tanous                                                entryID);
16373de8d8baSGeorge Liu                     return;
16383de8d8baSGeorge Liu                 }
1639336e96c6SChicago Duan                 // TODO Handle for specific error code
16400fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
16410fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1642336e96c6SChicago Duan                     << ec;
1643336e96c6SChicago Duan                 asyncResp->res.result(
1644336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1645336e96c6SChicago Duan                 return;
1646336e96c6SChicago Duan             }
1647336e96c6SChicago Duan 
1648336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1649336e96c6SChicago Duan         };
1650336e96c6SChicago Duan 
1651336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1652336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1653336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1654336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1655336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
16567e860f15SJohn Edward Broadbent         });
1657400fd1fbSAdriana Kobylak }
1658400fd1fbSAdriana Kobylak 
16597e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1660400fd1fbSAdriana Kobylak {
16610fda0f12SGeorge Liu     BMCWEB_ROUTE(
16620fda0f12SGeorge Liu         app,
16630fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1664ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
16657e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
166645ca1b86SEd Tanous             [&app](const crow::Request& req,
16677e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
166845ca1b86SEd Tanous                    const std::string& param) {
16693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16707e860f15SJohn Edward Broadbent         {
167145ca1b86SEd Tanous             return;
167245ca1b86SEd Tanous         }
1673002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
1674400fd1fbSAdriana Kobylak         {
1675002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1676400fd1fbSAdriana Kobylak             return;
1677400fd1fbSAdriana Kobylak         }
1678400fd1fbSAdriana Kobylak 
16797e860f15SJohn Edward Broadbent         std::string entryID = param;
1680400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1681400fd1fbSAdriana Kobylak 
1682400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1683002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1684400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1685400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1686400fd1fbSAdriana Kobylak             {
1687002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1688002d39b4SEd Tanous                                            entryID);
1689400fd1fbSAdriana Kobylak                 return;
1690400fd1fbSAdriana Kobylak             }
1691400fd1fbSAdriana Kobylak             if (ec)
1692400fd1fbSAdriana Kobylak             {
1693400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1694400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1695400fd1fbSAdriana Kobylak                 return;
1696400fd1fbSAdriana Kobylak             }
1697400fd1fbSAdriana Kobylak 
1698400fd1fbSAdriana Kobylak             int fd = -1;
1699400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1700400fd1fbSAdriana Kobylak             if (fd == -1)
1701400fd1fbSAdriana Kobylak             {
1702400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1703400fd1fbSAdriana Kobylak                 return;
1704400fd1fbSAdriana Kobylak             }
1705400fd1fbSAdriana Kobylak 
1706400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1707400fd1fbSAdriana Kobylak             if (size == -1)
1708400fd1fbSAdriana Kobylak             {
1709400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1710400fd1fbSAdriana Kobylak                 return;
1711400fd1fbSAdriana Kobylak             }
1712400fd1fbSAdriana Kobylak 
1713400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1714400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1715400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1716400fd1fbSAdriana Kobylak             {
1717002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1718400fd1fbSAdriana Kobylak                                  << maxFileSize;
1719400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1720400fd1fbSAdriana Kobylak                 return;
1721400fd1fbSAdriana Kobylak             }
1722400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1723400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1724400fd1fbSAdriana Kobylak             if (rc == -1)
1725400fd1fbSAdriana Kobylak             {
1726400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1727400fd1fbSAdriana Kobylak                 return;
1728400fd1fbSAdriana Kobylak             }
1729400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1730400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1731400fd1fbSAdriana Kobylak             {
1732400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1733400fd1fbSAdriana Kobylak                 return;
1734400fd1fbSAdriana Kobylak             }
1735400fd1fbSAdriana Kobylak             close(fd);
1736400fd1fbSAdriana Kobylak 
1737400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1738002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1739400fd1fbSAdriana Kobylak 
1740d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1741400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1742d9f6c621SEd Tanous             asyncResp->res.addHeader(
1743d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
1744400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1745400fd1fbSAdriana Kobylak             },
1746400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1747400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1748400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
17497e860f15SJohn Edward Broadbent         });
17501da66f75SEd Tanous }
17511da66f75SEd Tanous 
1752b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1753b7028ebfSSpencer Ku 
1754b7028ebfSSpencer Ku inline bool
1755b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1756b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1757b7028ebfSSpencer Ku {
1758b7028ebfSSpencer Ku     std::error_code ec;
1759b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1760b7028ebfSSpencer Ku     if (ec)
1761b7028ebfSSpencer Ku     {
1762b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1763b7028ebfSSpencer Ku         return false;
1764b7028ebfSSpencer Ku     }
1765b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1766b7028ebfSSpencer Ku     {
1767b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1768b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1769b7028ebfSSpencer Ku         // path
177011ba3979SEd Tanous         if (filename.starts_with("log"))
1771b7028ebfSSpencer Ku         {
1772b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1773b7028ebfSSpencer Ku         }
1774b7028ebfSSpencer Ku     }
1775b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1776b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1777b7028ebfSSpencer Ku     // descending order.
1778b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1779b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1780b7028ebfSSpencer Ku 
1781b7028ebfSSpencer Ku     return true;
1782b7028ebfSSpencer Ku }
1783b7028ebfSSpencer Ku 
178402cad96eSEd Tanous inline bool getHostLoggerEntries(
178502cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
178602cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
1787b7028ebfSSpencer Ku {
1788b7028ebfSSpencer Ku     GzFileReader logFile;
1789b7028ebfSSpencer Ku 
1790b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1791b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1792b7028ebfSSpencer Ku     {
1793b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1794b7028ebfSSpencer Ku         {
1795b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1796b7028ebfSSpencer Ku             return false;
1797b7028ebfSSpencer Ku         }
1798b7028ebfSSpencer Ku     }
1799b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1800b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1801b7028ebfSSpencer Ku     if (!lastMessage.empty())
1802b7028ebfSSpencer Ku     {
1803b7028ebfSSpencer Ku         logCount++;
1804b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1805b7028ebfSSpencer Ku         {
1806b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1807b7028ebfSSpencer Ku         }
1808b7028ebfSSpencer Ku     }
1809b7028ebfSSpencer Ku     return true;
1810b7028ebfSSpencer Ku }
1811b7028ebfSSpencer Ku 
1812b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1813b7028ebfSSpencer Ku                                     const std::string& msg,
18146d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
1815b7028ebfSSpencer Ku {
1816b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
18176d6574c9SJason M. Bills     logEntryJson["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
18186d6574c9SJason M. Bills     logEntryJson["@odata.id"] =
1819b7028ebfSSpencer Ku         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
18206d6574c9SJason M. Bills         logEntryID;
18216d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
18226d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
18236d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
18246d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
18256d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
18266d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
1827b7028ebfSSpencer Ku }
1828b7028ebfSSpencer Ku 
1829b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1830b7028ebfSSpencer Ku {
1831b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1832b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
18331476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
18341476687dSEd Tanous             [&app](const crow::Request& req,
18351476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
18363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
183745ca1b86SEd Tanous         {
183845ca1b86SEd Tanous             return;
183945ca1b86SEd Tanous         }
1840b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1841b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
1842b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1843b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
1844b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1845b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1846b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
18471476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
18481476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1849b7028ebfSSpencer Ku         });
1850b7028ebfSSpencer Ku }
1851b7028ebfSSpencer Ku 
1852b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1853b7028ebfSSpencer Ku {
1854b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1855b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1856b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1857002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1858002d39b4SEd Tanous             [&app](const crow::Request& req,
1859002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1860c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1861c937d2bfSEd Tanous             .canDelegateTop = true,
1862c937d2bfSEd Tanous             .canDelegateSkip = true,
1863c937d2bfSEd Tanous         };
1864c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1865c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
18663ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1867b7028ebfSSpencer Ku         {
1868b7028ebfSSpencer Ku             return;
1869b7028ebfSSpencer Ku         }
1870b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1871b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1872b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1873b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
1874b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1875b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
1876b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
18770fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1878b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
1879b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
1880b7028ebfSSpencer Ku 
1881b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1882b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1883b7028ebfSSpencer Ku         {
1884b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1885b7028ebfSSpencer Ku             return;
1886b7028ebfSSpencer Ku         }
18873648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
18883648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
18895143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
1890b7028ebfSSpencer Ku         size_t logCount = 0;
1891b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
1892b7028ebfSSpencer Ku         // control by skip and top.
1893b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
18943648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
18953648c8beSEd Tanous                                   logCount))
1896b7028ebfSSpencer Ku         {
1897b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1898b7028ebfSSpencer Ku             return;
1899b7028ebfSSpencer Ku         }
1900b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
1901b7028ebfSSpencer Ku         // log count
190226f6976fSEd Tanous         if (logEntries.empty())
1903b7028ebfSSpencer Ku         {
1904b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1905b7028ebfSSpencer Ku             return;
1906b7028ebfSSpencer Ku         }
190726f6976fSEd Tanous         if (!logEntries.empty())
1908b7028ebfSSpencer Ku         {
1909b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
1910b7028ebfSSpencer Ku             {
19116d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
19123648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
19133648c8beSEd Tanous                                         hostLogEntry);
19146d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
1915b7028ebfSSpencer Ku             }
1916b7028ebfSSpencer Ku 
1917b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
19183648c8beSEd Tanous             if (skip + top < logCount)
1919b7028ebfSSpencer Ku             {
1920b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
19210fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
19223648c8beSEd Tanous                     std::to_string(skip + top);
1923b7028ebfSSpencer Ku             }
1924b7028ebfSSpencer Ku         }
1925b7028ebfSSpencer Ku         });
1926b7028ebfSSpencer Ku }
1927b7028ebfSSpencer Ku 
1928b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1929b7028ebfSSpencer Ku {
1930b7028ebfSSpencer Ku     BMCWEB_ROUTE(
1931b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1932b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1933b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
193445ca1b86SEd Tanous             [&app](const crow::Request& req,
1935b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1936b7028ebfSSpencer Ku                    const std::string& param) {
19373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
193845ca1b86SEd Tanous         {
193945ca1b86SEd Tanous             return;
194045ca1b86SEd Tanous         }
1941b7028ebfSSpencer Ku         const std::string& targetID = param;
1942b7028ebfSSpencer Ku 
1943b7028ebfSSpencer Ku         uint64_t idInt = 0;
1944ca45aa3cSEd Tanous 
1945ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1946ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
1947ca45aa3cSEd Tanous 
1948ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1949b7028ebfSSpencer Ku         if (ec == std::errc::invalid_argument)
1950b7028ebfSSpencer Ku         {
1951ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1952b7028ebfSSpencer Ku             return;
1953b7028ebfSSpencer Ku         }
1954b7028ebfSSpencer Ku         if (ec == std::errc::result_out_of_range)
1955b7028ebfSSpencer Ku         {
1956ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1957b7028ebfSSpencer Ku             return;
1958b7028ebfSSpencer Ku         }
1959b7028ebfSSpencer Ku 
1960b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1961b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1962b7028ebfSSpencer Ku         {
1963b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1964b7028ebfSSpencer Ku             return;
1965b7028ebfSSpencer Ku         }
1966b7028ebfSSpencer Ku 
1967b7028ebfSSpencer Ku         size_t logCount = 0;
19683648c8beSEd Tanous         size_t top = 1;
1969b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
1970b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
1971b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
1972b7028ebfSSpencer Ku         // get that entry
1973002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
1974002d39b4SEd Tanous                                   logCount))
1975b7028ebfSSpencer Ku         {
1976b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1977b7028ebfSSpencer Ku             return;
1978b7028ebfSSpencer Ku         }
1979b7028ebfSSpencer Ku 
1980b7028ebfSSpencer Ku         if (!logEntries.empty())
1981b7028ebfSSpencer Ku         {
19826d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
19836d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
19846d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
1985b7028ebfSSpencer Ku             return;
1986b7028ebfSSpencer Ku         }
1987b7028ebfSSpencer Ku 
1988b7028ebfSSpencer Ku         // Requested ID was not found
1989ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1990b7028ebfSSpencer Ku         });
1991b7028ebfSSpencer Ku }
1992b7028ebfSSpencer Ku 
1993fdd26906SClaire Weinan constexpr char const* dumpManagerIface =
1994fdd26906SClaire Weinan     "xyz.openbmc_project.Collection.DeleteAll";
1995dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
1996fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
1997fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
19981da66f75SEd Tanous {
19993ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
200045ca1b86SEd Tanous     {
200145ca1b86SEd Tanous         return;
200245ca1b86SEd Tanous     }
20037e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
20047e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2005e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
20061da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2007e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2008e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2009002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2010e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
20111da66f75SEd Tanous         "Collection of LogServices for this Manager";
2012002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2013c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2014fdd26906SClaire Weinan 
2015c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2016c4bf6374SJason M. Bills     logServiceArray.push_back(
2017002d39b4SEd Tanous         {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2018c4bf6374SJason M. Bills #endif
2019fdd26906SClaire Weinan 
2020fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2021fdd26906SClaire Weinan 
2022fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2023fdd26906SClaire Weinan     auto respHandler =
2024fdd26906SClaire Weinan         [asyncResp](
2025fdd26906SClaire Weinan             const boost::system::error_code ec,
2026fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2027fdd26906SClaire Weinan         if (ec)
2028fdd26906SClaire Weinan         {
2029fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2030dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2031fdd26906SClaire Weinan                 << ec;
2032fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2033fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2034fdd26906SClaire Weinan             return;
2035fdd26906SClaire Weinan         }
2036fdd26906SClaire Weinan 
2037fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2038fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2039fdd26906SClaire Weinan 
2040fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2041fdd26906SClaire Weinan         {
2042fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2043fdd26906SClaire Weinan             {
2044fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2045fdd26906SClaire Weinan                     {{"@odata.id",
2046fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
2047fdd26906SClaire Weinan             }
2048fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2049fdd26906SClaire Weinan             {
2050fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2051fdd26906SClaire Weinan                     {{"@odata.id",
2052fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/FaultLog"}});
2053fdd26906SClaire Weinan             }
2054fdd26906SClaire Weinan         }
2055fdd26906SClaire Weinan 
2056e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2057fdd26906SClaire Weinan             logServiceArrayLocal.size();
2058fdd26906SClaire Weinan     };
2059fdd26906SClaire Weinan 
2060fdd26906SClaire Weinan     crow::connections::systemBus->async_method_call(
2061fdd26906SClaire Weinan         respHandler, "xyz.openbmc_project.ObjectMapper",
2062fdd26906SClaire Weinan         "/xyz/openbmc_project/object_mapper",
2063fdd26906SClaire Weinan         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2064fdd26906SClaire Weinan         "/xyz/openbmc_project/dump", 0,
2065fdd26906SClaire Weinan         std::array<const char*, 1>{dumpManagerIface});
2066fdd26906SClaire Weinan #endif
2067fdd26906SClaire Weinan }
2068fdd26906SClaire Weinan 
2069fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2070fdd26906SClaire Weinan {
2071fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2072fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2073fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2074dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2075e1f26343SJason M. Bills }
2076e1f26343SJason M. Bills 
20777e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2078e1f26343SJason M. Bills {
20797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2080ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
20817e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
208245ca1b86SEd Tanous             [&app](const crow::Request& req,
208345ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
20843ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20857e860f15SJohn Edward Broadbent         {
208645ca1b86SEd Tanous             return;
208745ca1b86SEd Tanous         }
2088e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2089e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
20900f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20910f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2092002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2093002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2094c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
2095e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
20967c8c4058STejas Patil 
20977c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20982b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2099002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
21007c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21017c8c4058STejas Patil             redfishDateTimeOffset.second;
21027c8c4058STejas Patil 
21031476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
21041476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
21057e860f15SJohn Edward Broadbent         });
2106e1f26343SJason M. Bills }
2107e1f26343SJason M. Bills 
21083a48b3a2SJason M. Bills static int
21093a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2110e1f26343SJason M. Bills                                sd_journal* journal,
21113a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2112e1f26343SJason M. Bills {
2113e1f26343SJason M. Bills     // Get the Log Entry contents
2114e1f26343SJason M. Bills     int ret = 0;
2115e1f26343SJason M. Bills 
2116a8fe54f0SJason M. Bills     std::string message;
2117a8fe54f0SJason M. Bills     std::string_view syslogID;
2118a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2119a8fe54f0SJason M. Bills     if (ret < 0)
2120a8fe54f0SJason M. Bills     {
2121a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2122a8fe54f0SJason M. Bills                          << strerror(-ret);
2123a8fe54f0SJason M. Bills     }
2124a8fe54f0SJason M. Bills     if (!syslogID.empty())
2125a8fe54f0SJason M. Bills     {
2126a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2127a8fe54f0SJason M. Bills     }
2128a8fe54f0SJason M. Bills 
212939e77504SEd Tanous     std::string_view msg;
213016428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2131e1f26343SJason M. Bills     if (ret < 0)
2132e1f26343SJason M. Bills     {
2133e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2134e1f26343SJason M. Bills         return 1;
2135e1f26343SJason M. Bills     }
2136a8fe54f0SJason M. Bills     message += std::string(msg);
2137e1f26343SJason M. Bills 
2138e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2139271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
214016428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2141e1f26343SJason M. Bills     if (ret < 0)
2142e1f26343SJason M. Bills     {
2143e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2144e1f26343SJason M. Bills     }
2145e1f26343SJason M. Bills 
2146e1f26343SJason M. Bills     // Get the Created time from the timestamp
214716428a1aSJason M. Bills     std::string entryTimeStr;
214816428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2149e1f26343SJason M. Bills     {
215016428a1aSJason M. Bills         return 1;
2151e1f26343SJason M. Bills     }
2152e1f26343SJason M. Bills 
2153e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
215484afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
215584afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.id"] =
215684afc48bSJason M. Bills         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
215784afc48bSJason M. Bills         bmcJournalLogEntryID;
215884afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
215984afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
216084afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
216184afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
216284afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2163738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
216484afc48bSJason M. Bills                                                          : "OK";
216584afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
216684afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2167e1f26343SJason M. Bills     return 0;
2168e1f26343SJason M. Bills }
2169e1f26343SJason M. Bills 
21707e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2171e1f26343SJason M. Bills {
21727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2173ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2174002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2175002d39b4SEd Tanous             [&app](const crow::Request& req,
2176002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2177c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2178c937d2bfSEd Tanous             .canDelegateTop = true,
2179c937d2bfSEd Tanous             .canDelegateSkip = true,
2180c937d2bfSEd Tanous         };
2181c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2182c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21833ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2184193ad2faSJason M. Bills         {
2185193ad2faSJason M. Bills             return;
2186193ad2faSJason M. Bills         }
21873648c8beSEd Tanous 
21883648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21895143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
21903648c8beSEd Tanous 
21917e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
21927e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2193e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2194e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
21950f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
21960f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2197e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2198e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2199e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
22000fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2201e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2202e1f26343SJason M. Bills 
22037e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
22047e860f15SJohn Edward Broadbent         // unique ID for each entry
2205e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2206e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2207e1f26343SJason M. Bills         if (ret < 0)
2208e1f26343SJason M. Bills         {
2209002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2210f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2211e1f26343SJason M. Bills             return;
2212e1f26343SJason M. Bills         }
22130fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22140fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2215e1f26343SJason M. Bills         journalTmp = nullptr;
2216b01bf299SEd Tanous         uint64_t entryCount = 0;
2217e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2218e85d6b16SJason M. Bills         bool firstEntry = true;
2219e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2220e1f26343SJason M. Bills         {
2221193ad2faSJason M. Bills             entryCount++;
22227e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
22237e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
22243648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2225193ad2faSJason M. Bills             {
2226193ad2faSJason M. Bills                 continue;
2227193ad2faSJason M. Bills             }
2228193ad2faSJason M. Bills 
222916428a1aSJason M. Bills             std::string idStr;
2230e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2231e1f26343SJason M. Bills             {
2232e1f26343SJason M. Bills                 continue;
2233e1f26343SJason M. Bills             }
2234e85d6b16SJason M. Bills             firstEntry = false;
2235e85d6b16SJason M. Bills 
22363a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2237c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2238c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2239e1f26343SJason M. Bills             {
2240f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2241e1f26343SJason M. Bills                 return;
2242e1f26343SJason M. Bills             }
22433a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2244e1f26343SJason M. Bills         }
2245193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
22463648c8beSEd Tanous         if (skip + top < entryCount)
2247193ad2faSJason M. Bills         {
2248193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
22490fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
22503648c8beSEd Tanous                 std::to_string(skip + top);
2251193ad2faSJason M. Bills         }
22527e860f15SJohn Edward Broadbent         });
2253e1f26343SJason M. Bills }
2254e1f26343SJason M. Bills 
22557e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2256e1f26343SJason M. Bills {
22577e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
22587e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2259ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
22607e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
226145ca1b86SEd Tanous             [&app](const crow::Request& req,
22627e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22637e860f15SJohn Edward Broadbent                    const std::string& entryID) {
22643ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
226545ca1b86SEd Tanous         {
226645ca1b86SEd Tanous             return;
226745ca1b86SEd Tanous         }
2268e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2269e1f26343SJason M. Bills         uint64_t ts = 0;
2270271584abSEd Tanous         uint64_t index = 0;
22718d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2272e1f26343SJason M. Bills         {
227316428a1aSJason M. Bills             return;
2274e1f26343SJason M. Bills         }
2275e1f26343SJason M. Bills 
2276e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2277e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2278e1f26343SJason M. Bills         if (ret < 0)
2279e1f26343SJason M. Bills         {
2280002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2281f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2282e1f26343SJason M. Bills             return;
2283e1f26343SJason M. Bills         }
2284002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2285002d39b4SEd Tanous             journalTmp, sd_journal_close);
2286e1f26343SJason M. Bills         journalTmp = nullptr;
22877e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
22887e860f15SJohn Edward Broadbent         // index tracking the unique ID
2289af07e3f5SJason M. Bills         std::string idStr;
2290af07e3f5SJason M. Bills         bool firstEntry = true;
2291e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
22922056b6d1SManojkiran Eda         if (ret < 0)
22932056b6d1SManojkiran Eda         {
22942056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
22952056b6d1SManojkiran Eda                              << strerror(-ret);
22962056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
22972056b6d1SManojkiran Eda             return;
22982056b6d1SManojkiran Eda         }
2299271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2300e1f26343SJason M. Bills         {
2301e1f26343SJason M. Bills             sd_journal_next(journal.get());
2302af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2303af07e3f5SJason M. Bills             {
2304af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2305af07e3f5SJason M. Bills                 return;
2306af07e3f5SJason M. Bills             }
2307af07e3f5SJason M. Bills             firstEntry = false;
2308af07e3f5SJason M. Bills         }
2309c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2310af07e3f5SJason M. Bills         if (idStr != entryID)
2311c4bf6374SJason M. Bills         {
2312ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2313c4bf6374SJason M. Bills             return;
2314c4bf6374SJason M. Bills         }
2315c4bf6374SJason M. Bills 
23163a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2317c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
23183a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2319e1f26343SJason M. Bills         {
2320f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2321e1f26343SJason M. Bills             return;
2322e1f26343SJason M. Bills         }
2323d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
23247e860f15SJohn Edward Broadbent         });
2325c9bb6861Sraviteja-b }
2326c9bb6861Sraviteja-b 
2327fdd26906SClaire Weinan inline void
2328fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2329fdd26906SClaire Weinan                        const std::string& dumpType)
2330c9bb6861Sraviteja-b {
2331fdd26906SClaire Weinan     std::string dumpPath;
2332fdd26906SClaire Weinan     std::string overWritePolicy;
2333fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2334fdd26906SClaire Weinan 
2335fdd26906SClaire Weinan     if (dumpType == "BMC")
233645ca1b86SEd Tanous     {
2337fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2338fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2339fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2340fdd26906SClaire Weinan     }
2341fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2342fdd26906SClaire Weinan     {
2343fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2344fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2345fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2346fdd26906SClaire Weinan     }
2347fdd26906SClaire Weinan     else if (dumpType == "System")
2348fdd26906SClaire Weinan     {
2349fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2350fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2351fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2352fdd26906SClaire Weinan     }
2353fdd26906SClaire Weinan     else
2354fdd26906SClaire Weinan     {
2355fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2356fdd26906SClaire Weinan                          << dumpType;
2357fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
235845ca1b86SEd Tanous         return;
235945ca1b86SEd Tanous     }
2360fdd26906SClaire Weinan 
2361fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2362fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2363c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2364fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2365fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2366fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
23677c8c4058STejas Patil 
23687c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
23692b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
23700fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23717c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23727c8c4058STejas Patil         redfishDateTimeOffset.second;
23737c8c4058STejas Patil 
2374fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2375002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2376fdd26906SClaire Weinan         dumpPath + "/Actions/LogService.ClearLog";
2377fdd26906SClaire Weinan 
2378fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2379fdd26906SClaire Weinan     {
2380002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
23811476687dSEd Tanous                                 ["target"] =
2382fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2383fdd26906SClaire Weinan     }
2384c9bb6861Sraviteja-b }
2385c9bb6861Sraviteja-b 
2386fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2387fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2388fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23897e860f15SJohn Edward Broadbent {
23903ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
239145ca1b86SEd Tanous     {
239245ca1b86SEd Tanous         return;
239345ca1b86SEd Tanous     }
2394fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2395fdd26906SClaire Weinan }
2396c9bb6861Sraviteja-b 
2397fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2398fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2399fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2400fdd26906SClaire Weinan {
2401fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2402fdd26906SClaire Weinan     {
2403fdd26906SClaire Weinan         return;
2404fdd26906SClaire Weinan     }
2405fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2406fdd26906SClaire Weinan }
2407fdd26906SClaire Weinan 
2408fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2409fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2410fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2411fdd26906SClaire Weinan     const std::string& dumpId)
2412fdd26906SClaire Weinan {
2413fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2414fdd26906SClaire Weinan     {
2415fdd26906SClaire Weinan         return;
2416fdd26906SClaire Weinan     }
2417fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2418fdd26906SClaire Weinan }
2419fdd26906SClaire Weinan 
2420fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2421fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2422fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2423fdd26906SClaire Weinan     const std::string& dumpId)
2424fdd26906SClaire Weinan {
2425fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2426fdd26906SClaire Weinan     {
2427fdd26906SClaire Weinan         return;
2428fdd26906SClaire Weinan     }
2429fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2430fdd26906SClaire Weinan }
2431fdd26906SClaire Weinan 
2432fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2433fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2434fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2435fdd26906SClaire Weinan {
2436fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2437fdd26906SClaire Weinan     {
2438fdd26906SClaire Weinan         return;
2439fdd26906SClaire Weinan     }
2440fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2441fdd26906SClaire Weinan }
2442fdd26906SClaire Weinan 
2443fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2444fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2445fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2446fdd26906SClaire Weinan {
2447fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2448fdd26906SClaire Weinan     {
2449fdd26906SClaire Weinan         return;
2450fdd26906SClaire Weinan     }
2451fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2452fdd26906SClaire Weinan }
2453fdd26906SClaire Weinan 
2454fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2455fdd26906SClaire Weinan {
2456fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2457fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2458fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2459fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2460fdd26906SClaire Weinan }
2461fdd26906SClaire Weinan 
2462fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2463fdd26906SClaire Weinan {
2464fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2465fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2466fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2467fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2468c9bb6861Sraviteja-b }
2469c9bb6861Sraviteja-b 
24707e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2471c9bb6861Sraviteja-b {
24727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24737e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2474ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2475fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2476fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2477fdd26906SClaire Weinan 
24787e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24797e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2480ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2481fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2482fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2483c9bb6861Sraviteja-b }
2484c9bb6861Sraviteja-b 
24857e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2486c9bb6861Sraviteja-b {
24870fda0f12SGeorge Liu     BMCWEB_ROUTE(
24880fda0f12SGeorge Liu         app,
24890fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2490ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24917e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2492fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2493fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2494a43be80fSAsmitha Karunanithi }
2495a43be80fSAsmitha Karunanithi 
24967e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
249780319af1SAsmitha Karunanithi {
24980fda0f12SGeorge Liu     BMCWEB_ROUTE(
24990fda0f12SGeorge Liu         app,
25000fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2501ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2502fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2503fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
250445ca1b86SEd Tanous }
2505fdd26906SClaire Weinan 
2506fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2507fdd26906SClaire Weinan {
2508fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2509fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2510fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2511fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2512fdd26906SClaire Weinan }
2513fdd26906SClaire Weinan 
2514fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2515fdd26906SClaire Weinan {
2516fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2517fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2518fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2519fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2520fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2521fdd26906SClaire Weinan }
2522fdd26906SClaire Weinan 
2523fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2524fdd26906SClaire Weinan {
2525fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2526fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2527fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2528fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2529fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2530fdd26906SClaire Weinan 
2531fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2532fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2533fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2534fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2535fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2536fdd26906SClaire Weinan }
2537fdd26906SClaire Weinan 
2538fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2539fdd26906SClaire Weinan {
2540fdd26906SClaire Weinan     BMCWEB_ROUTE(
2541fdd26906SClaire Weinan         app,
2542fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2543fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2544fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2545fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
25465cb1dd27SAsmitha Karunanithi }
25475cb1dd27SAsmitha Karunanithi 
25487e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
25495cb1dd27SAsmitha Karunanithi {
25507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2551ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
2552002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2553002d39b4SEd Tanous             [&app](const crow::Request& req,
2554002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25553ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
25567e860f15SJohn Edward Broadbent         {
255745ca1b86SEd Tanous             return;
255845ca1b86SEd Tanous         }
25595cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
25605cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump";
25615cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
2562d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
25635cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "Dump LogService";
256445ca1b86SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Dump LogService";
25655cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
25665cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
25677c8c4058STejas Patil 
25687c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
25692b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
257045ca1b86SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25717c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25727c8c4058STejas Patil             redfishDateTimeOffset.second;
25737c8c4058STejas Patil 
25741476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
25751476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2576002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
25771476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog";
25781476687dSEd Tanous 
2579002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
25801476687dSEd Tanous                                 ["target"] =
25811476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData";
25827e860f15SJohn Edward Broadbent         });
25835cb1dd27SAsmitha Karunanithi }
25845cb1dd27SAsmitha Karunanithi 
25857e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
25867e860f15SJohn Edward Broadbent {
25877e860f15SJohn Edward Broadbent 
25885cb1dd27SAsmitha Karunanithi     /**
25895cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25905cb1dd27SAsmitha Karunanithi      */
2591b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2592ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
259445ca1b86SEd Tanous             [&app](const crow::Request& req,
2595864d6a17SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
259745ca1b86SEd Tanous         {
259845ca1b86SEd Tanous             return;
259945ca1b86SEd Tanous         }
26005cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "System");
26017e860f15SJohn Edward Broadbent         });
26025cb1dd27SAsmitha Karunanithi }
26035cb1dd27SAsmitha Karunanithi 
26047e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
26055cb1dd27SAsmitha Karunanithi {
26067e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2607864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2608ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2609ed398213SEd Tanous 
26107e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
261145ca1b86SEd Tanous             [&app](const crow::Request& req,
26127e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26137e860f15SJohn Edward Broadbent                    const std::string& param) {
26143ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2615c7a6d660SClaire Weinan         {
2616c7a6d660SClaire Weinan             return;
2617c7a6d660SClaire Weinan         }
2618c7a6d660SClaire Weinan         getDumpEntryById(asyncResp, param, "System");
26197e860f15SJohn Edward Broadbent         });
26208d1b46d7Szhanghch05 
26217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2622864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2623ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
26247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
262545ca1b86SEd Tanous             [&app](const crow::Request& req,
26267e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26277e860f15SJohn Edward Broadbent                    const std::string& param) {
26283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
262945ca1b86SEd Tanous         {
263045ca1b86SEd Tanous             return;
263145ca1b86SEd Tanous         }
26327e860f15SJohn Edward Broadbent         deleteDumpEntry(asyncResp, param, "system");
26337e860f15SJohn Edward Broadbent         });
26345cb1dd27SAsmitha Karunanithi }
2635c9bb6861Sraviteja-b 
26367e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2637c9bb6861Sraviteja-b {
26380fda0f12SGeorge Liu     BMCWEB_ROUTE(
26390fda0f12SGeorge Liu         app,
26400fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2641ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
264345ca1b86SEd Tanous             [&app](const crow::Request& req,
264445ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
264645ca1b86SEd Tanous         {
264745ca1b86SEd Tanous             return;
264845ca1b86SEd Tanous         }
264945ca1b86SEd Tanous         createDump(asyncResp, req, "System");
265045ca1b86SEd Tanous         });
2651a43be80fSAsmitha Karunanithi }
2652a43be80fSAsmitha Karunanithi 
26537e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2654a43be80fSAsmitha Karunanithi {
26550fda0f12SGeorge Liu     BMCWEB_ROUTE(
26560fda0f12SGeorge Liu         app,
26570fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2658ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
266045ca1b86SEd Tanous             [&app](const crow::Request& req,
26617e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26627e860f15SJohn Edward Broadbent 
266345ca1b86SEd Tanous             {
26643ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266545ca1b86SEd Tanous         {
266645ca1b86SEd Tanous             return;
266745ca1b86SEd Tanous         }
266845ca1b86SEd Tanous         clearDump(asyncResp, "System");
266945ca1b86SEd Tanous         });
2670013487e5Sraviteja-b }
2671013487e5Sraviteja-b 
26727e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
26731da66f75SEd Tanous {
26743946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26753946028dSAppaRao Puli     // method for security reasons.
26761da66f75SEd Tanous     /**
26771da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26781da66f75SEd Tanous      */
26797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2680ed398213SEd Tanous         // This is incorrect, should be:
2681ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2682432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2683002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2684002d39b4SEd Tanous             [&app](const crow::Request& req,
2685002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26863ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
268745ca1b86SEd Tanous         {
268845ca1b86SEd Tanous             return;
268945ca1b86SEd Tanous         }
26907e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
26917e860f15SJohn Edward Broadbent         // SubRoute
26920f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2693424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2694e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
26958e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
26964f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26974f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26984f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2699e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2700e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
27017c8c4058STejas Patil 
27027c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
27032b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
27047c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
27057c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
27067c8c4058STejas Patil             redfishDateTimeOffset.second;
27077c8c4058STejas Patil 
27081476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
27091476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2710002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
27111476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2712002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
27131476687dSEd Tanous                                 ["target"] =
27141476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
27157e860f15SJohn Edward Broadbent         });
27161da66f75SEd Tanous }
27171da66f75SEd Tanous 
27187e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
27195b61b5e8SJason M. Bills {
27200fda0f12SGeorge Liu     BMCWEB_ROUTE(
27210fda0f12SGeorge Liu         app,
27220fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2723ed398213SEd Tanous         // This is incorrect, should be:
2724ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2725432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
272745ca1b86SEd Tanous             [&app](const crow::Request& req,
27287e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
273045ca1b86SEd Tanous         {
273145ca1b86SEd Tanous             return;
273245ca1b86SEd Tanous         }
27335b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
27345b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
2735cb13a392SEd Tanous                         const std::string&) {
27365b61b5e8SJason M. Bills             if (ec)
27375b61b5e8SJason M. Bills             {
27385b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
27395b61b5e8SJason M. Bills                 return;
27405b61b5e8SJason M. Bills             }
27415b61b5e8SJason M. Bills             messages::success(asyncResp->res);
27425b61b5e8SJason M. Bills             },
2743002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
27447e860f15SJohn Edward Broadbent         });
27455b61b5e8SJason M. Bills }
27465b61b5e8SJason M. Bills 
27478d1b46d7Szhanghch05 static void
27488d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27498d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2750e855dd28SJason M. Bills {
2751043a0536SJohnathan Mantey     auto getStoredLogCallback =
2752b9d36b47SEd Tanous         [asyncResp, logID,
2753b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2754b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2755e855dd28SJason M. Bills         if (ec)
2756e855dd28SJason M. Bills         {
2757e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
27581ddcf01aSJason M. Bills             if (ec.value() ==
27591ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
27601ddcf01aSJason M. Bills             {
2761002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
27621ddcf01aSJason M. Bills             }
27631ddcf01aSJason M. Bills             else
27641ddcf01aSJason M. Bills             {
2765e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
27661ddcf01aSJason M. Bills             }
2767e855dd28SJason M. Bills             return;
2768e855dd28SJason M. Bills         }
2769043a0536SJohnathan Mantey 
2770043a0536SJohnathan Mantey         std::string timestamp{};
2771043a0536SJohnathan Mantey         std::string filename{};
2772043a0536SJohnathan Mantey         std::string logfile{};
27732c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
2774043a0536SJohnathan Mantey 
2775043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
2776e855dd28SJason M. Bills         {
2777002d39b4SEd Tanous             messages::resourceMissingAtURI(asyncResp->res,
2778002d39b4SEd Tanous                                            crow::utility::urlFromPieces(logID));
2779e855dd28SJason M. Bills             return;
2780e855dd28SJason M. Bills         }
2781e855dd28SJason M. Bills 
2782043a0536SJohnathan Mantey         std::string crashdumpURI =
2783e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2784043a0536SJohnathan Mantey             logID + "/" + filename;
278584afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
278684afc48bSJason M. Bills         logEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
278784afc48bSJason M. Bills         logEntry["@odata.id"] =
278884afc48bSJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
278984afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
279084afc48bSJason M. Bills         logEntry["Id"] = logID;
279184afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
279284afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
279384afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
279484afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
279584afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
27962b20ef6eSJason M. Bills 
27972b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
27982b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
27992b20ef6eSJason M. Bills         // directly
28002b20ef6eSJason M. Bills         if (logEntryJson.is_array())
28012b20ef6eSJason M. Bills         {
28022b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
28032b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
28042b20ef6eSJason M. Bills                 logEntryJson.size();
28052b20ef6eSJason M. Bills         }
28062b20ef6eSJason M. Bills         else
28072b20ef6eSJason M. Bills         {
2808d405bb51SJason M. Bills             logEntryJson.update(logEntry);
28092b20ef6eSJason M. Bills         }
2810e855dd28SJason M. Bills     };
2811*d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2812*d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
2813*d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
2814*d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
2815e855dd28SJason M. Bills }
2816e855dd28SJason M. Bills 
28177e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
28181da66f75SEd Tanous {
28193946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28203946028dSAppaRao Puli     // method for security reasons.
28211da66f75SEd Tanous     /**
28221da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
28231da66f75SEd Tanous      */
28247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28257e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2826ed398213SEd Tanous         // This is incorrect, should be.
2827ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2828432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2829002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2830002d39b4SEd Tanous             [&app](const crow::Request& req,
2831002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
28323ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
283345ca1b86SEd Tanous         {
283445ca1b86SEd Tanous             return;
283545ca1b86SEd Tanous         }
28362b20ef6eSJason M. Bills         crow::connections::systemBus->async_method_call(
28372b20ef6eSJason M. Bills             [asyncResp](const boost::system::error_code ec,
28382b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
28391da66f75SEd Tanous             if (ec)
28401da66f75SEd Tanous             {
28411da66f75SEd Tanous                 if (ec.value() !=
28421da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
28431da66f75SEd Tanous                 {
28441da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
28451da66f75SEd Tanous                                      << ec.message();
2846f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
28471da66f75SEd Tanous                     return;
28481da66f75SEd Tanous                 }
28491da66f75SEd Tanous             }
2850e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
28511da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
28520f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2853424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2854002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2855e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2856424c4176SJason M. Bills                 "Collection of Crashdump Entries";
2857002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2858a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
28592b20ef6eSJason M. Bills 
28602b20ef6eSJason M. Bills             for (const std::string& path : resp)
28611da66f75SEd Tanous             {
28622b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
2863e855dd28SJason M. Bills                 // Get the log ID
28642b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
28652b20ef6eSJason M. Bills                 if (logID.empty())
28661da66f75SEd Tanous                 {
2867e855dd28SJason M. Bills                     continue;
28681da66f75SEd Tanous                 }
2869e855dd28SJason M. Bills                 // Add the log entry to the array
28702b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
28712b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
28721da66f75SEd Tanous             }
28732b20ef6eSJason M. Bills             },
28741da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
28751da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
28761da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
28775b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
28787e860f15SJohn Edward Broadbent         });
28791da66f75SEd Tanous }
28801da66f75SEd Tanous 
28817e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28821da66f75SEd Tanous {
28833946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28843946028dSAppaRao Puli     // method for security reasons.
28851da66f75SEd Tanous 
28867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28877e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2888ed398213SEd Tanous         // this is incorrect, should be
2889ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2890432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28917e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
289245ca1b86SEd Tanous             [&app](const crow::Request& req,
28937e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28947e860f15SJohn Edward Broadbent                    const std::string& param) {
28953ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
289645ca1b86SEd Tanous         {
289745ca1b86SEd Tanous             return;
289845ca1b86SEd Tanous         }
28997e860f15SJohn Edward Broadbent         const std::string& logID = param;
2900e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
29017e860f15SJohn Edward Broadbent         });
2902e855dd28SJason M. Bills }
2903e855dd28SJason M. Bills 
29047e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2905e855dd28SJason M. Bills {
29063946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29073946028dSAppaRao Puli     // method for security reasons.
29087e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
29097e860f15SJohn Edward Broadbent         app,
29107e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2911ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2913a4ce114aSNan Zhou             [](const crow::Request& req,
29147e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29157e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
29162a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
29172a9beeedSShounak Mitra         // Redfish resource.
2918043a0536SJohnathan Mantey         auto getStoredLogCallback =
2919002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
2920abf2add6SEd Tanous                 const boost::system::error_code ec,
2921002d39b4SEd Tanous                 const std::vector<
2922002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
29237e860f15SJohn Edward Broadbent                     resp) {
29241da66f75SEd Tanous             if (ec)
29251da66f75SEd Tanous             {
2926002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2927f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
29281da66f75SEd Tanous                 return;
29291da66f75SEd Tanous             }
2930e855dd28SJason M. Bills 
2931043a0536SJohnathan Mantey             std::string dbusFilename{};
2932043a0536SJohnathan Mantey             std::string dbusTimestamp{};
2933043a0536SJohnathan Mantey             std::string dbusFilepath{};
2934043a0536SJohnathan Mantey 
2935002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2936002d39b4SEd Tanous                                      dbusFilepath);
2937043a0536SJohnathan Mantey 
2938043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
2939043a0536SJohnathan Mantey                 dbusFilepath.empty())
29401da66f75SEd Tanous             {
2941ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
29421da66f75SEd Tanous                 return;
29431da66f75SEd Tanous             }
2944e855dd28SJason M. Bills 
2945043a0536SJohnathan Mantey             // Verify the file name parameter is correct
2946043a0536SJohnathan Mantey             if (fileName != dbusFilename)
2947043a0536SJohnathan Mantey             {
2948ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2949043a0536SJohnathan Mantey                 return;
2950043a0536SJohnathan Mantey             }
2951043a0536SJohnathan Mantey 
2952043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
2953043a0536SJohnathan Mantey             {
2954ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2955043a0536SJohnathan Mantey                 return;
2956043a0536SJohnathan Mantey             }
2957002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
2958002d39b4SEd Tanous             asyncResp->res.body() =
2959002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
2960043a0536SJohnathan Mantey 
29617e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
29627e860f15SJohn Edward Broadbent             // from a browser
2963d9f6c621SEd Tanous             asyncResp->res.addHeader(
2964d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
29651da66f75SEd Tanous         };
2966*d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
2967*d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
2968*d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
2969*d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
29707e860f15SJohn Edward Broadbent         });
29711da66f75SEd Tanous }
29721da66f75SEd Tanous 
2973c5a4c82aSJason M. Bills enum class OEMDiagnosticType
2974c5a4c82aSJason M. Bills {
2975c5a4c82aSJason M. Bills     onDemand,
2976c5a4c82aSJason M. Bills     telemetry,
2977c5a4c82aSJason M. Bills     invalid,
2978c5a4c82aSJason M. Bills };
2979c5a4c82aSJason M. Bills 
2980f7725d79SEd Tanous inline OEMDiagnosticType
2981f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
2982c5a4c82aSJason M. Bills {
2983c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
2984c5a4c82aSJason M. Bills     {
2985c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
2986c5a4c82aSJason M. Bills     }
2987c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
2988c5a4c82aSJason M. Bills     {
2989c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
2990c5a4c82aSJason M. Bills     }
2991c5a4c82aSJason M. Bills 
2992c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
2993c5a4c82aSJason M. Bills }
2994c5a4c82aSJason M. Bills 
29957e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
29961da66f75SEd Tanous {
29973946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29983946028dSAppaRao Puli     // method for security reasons.
29990fda0f12SGeorge Liu     BMCWEB_ROUTE(
30000fda0f12SGeorge Liu         app,
30010fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3002ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3003ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3004432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3005002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3006002d39b4SEd Tanous             [&app](const crow::Request& req,
30077e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
30083ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
300945ca1b86SEd Tanous         {
301045ca1b86SEd Tanous             return;
301145ca1b86SEd Tanous         }
30128e6c099aSJason M. Bills         std::string diagnosticDataType;
30138e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
301415ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3015002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3016002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
30178e6c099aSJason M. Bills         {
30188e6c099aSJason M. Bills             return;
30198e6c099aSJason M. Bills         }
30208e6c099aSJason M. Bills 
30218e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
30228e6c099aSJason M. Bills         {
30238e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
30248e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
30258e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
30268e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
30278e6c099aSJason M. Bills                 "CollectDiagnosticData");
30288e6c099aSJason M. Bills             return;
30298e6c099aSJason M. Bills         }
30308e6c099aSJason M. Bills 
3031c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3032c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3033c5a4c82aSJason M. Bills 
3034c5a4c82aSJason M. Bills         std::string iface;
3035c5a4c82aSJason M. Bills         std::string method;
3036c5a4c82aSJason M. Bills         std::string taskMatchStr;
3037c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3038c5a4c82aSJason M. Bills         {
3039c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3040c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3041c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3042c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3043c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3044c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3045c5a4c82aSJason M. Bills         }
3046c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3047c5a4c82aSJason M. Bills         {
3048c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3049c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3050c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3051c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3052c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3053c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3054c5a4c82aSJason M. Bills         }
3055c5a4c82aSJason M. Bills         else
3056c5a4c82aSJason M. Bills         {
3057c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3058c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3059c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3060002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3061002d39b4SEd Tanous                 "CollectDiagnosticData");
3062c5a4c82aSJason M. Bills             return;
3063c5a4c82aSJason M. Bills         }
3064c5a4c82aSJason M. Bills 
3065c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3066c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3067c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
306898be3e39SEd Tanous                            const std::string&) mutable {
30691da66f75SEd Tanous             if (ec)
30701da66f75SEd Tanous             {
3071002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
30721da66f75SEd Tanous                 {
3073f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
30741da66f75SEd Tanous                 }
30754363d3b2SJason M. Bills                 else if (ec.value() ==
30764363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
30774363d3b2SJason M. Bills                 {
3078002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3079002d39b4SEd Tanous                                                             "60");
30804363d3b2SJason M. Bills                 }
30811da66f75SEd Tanous                 else
30821da66f75SEd Tanous                 {
3083f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
30841da66f75SEd Tanous                 }
30851da66f75SEd Tanous                 return;
30861da66f75SEd Tanous             }
3087002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
308859d494eeSPatrick Williams                 [](boost::system::error_code err, sdbusplus::message_t&,
3089002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
309066afe4faSJames Feist                 if (!err)
309166afe4faSJames Feist                 {
3092002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3093e5d5006bSJames Feist                         std::to_string(taskData->index)));
3094831d6b09SJames Feist                     taskData->state = "Completed";
309566afe4faSJames Feist                 }
309632898ceaSJames Feist                 return task::completed;
309766afe4faSJames Feist                 },
3098c5a4c82aSJason M. Bills                 taskMatchStr);
3099c5a4c82aSJason M. Bills 
310046229577SJames Feist             task->startTimer(std::chrono::minutes(5));
310146229577SJames Feist             task->populateResp(asyncResp->res);
310298be3e39SEd Tanous             task->payload.emplace(std::move(payload));
31031da66f75SEd Tanous         };
31048e6c099aSJason M. Bills 
31051da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3106002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3107002d39b4SEd Tanous             iface, method);
31087e860f15SJohn Edward Broadbent         });
31096eda7685SKenny L. Ku }
31106eda7685SKenny L. Ku 
3111cb92c03bSAndrew Geissler /**
3112cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3113cb92c03bSAndrew Geissler  */
31147e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3115cb92c03bSAndrew Geissler {
3116cb92c03bSAndrew Geissler     /**
3117cb92c03bSAndrew Geissler      * Function handles POST method request.
3118cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3119cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3120cb92c03bSAndrew Geissler      */
31217e860f15SJohn Edward Broadbent 
31220fda0f12SGeorge Liu     BMCWEB_ROUTE(
31230fda0f12SGeorge Liu         app,
31240fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3125ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
31267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
312745ca1b86SEd Tanous             [&app](const crow::Request& req,
31287e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
313045ca1b86SEd Tanous         {
313145ca1b86SEd Tanous             return;
313245ca1b86SEd Tanous         }
3133cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3134cb92c03bSAndrew Geissler 
3135cb92c03bSAndrew Geissler         // Process response from Logging service.
3136002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3137002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3138cb92c03bSAndrew Geissler             if (ec)
3139cb92c03bSAndrew Geissler             {
3140cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3141002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3142cb92c03bSAndrew Geissler                 asyncResp->res.result(
3143cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3144cb92c03bSAndrew Geissler                 return;
3145cb92c03bSAndrew Geissler             }
3146cb92c03bSAndrew Geissler 
3147002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3148cb92c03bSAndrew Geissler         };
3149cb92c03bSAndrew Geissler 
3150cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3151cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
31522c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3153cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3154cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31557e860f15SJohn Edward Broadbent         });
3156cb92c03bSAndrew Geissler }
3157a3316fc6SZhikuiRen 
3158a3316fc6SZhikuiRen /****************************************************
3159a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3160a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3161a3316fc6SZhikuiRen  ******************************************************/
31627e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3163a3316fc6SZhikuiRen {
31647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3165ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3166002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3167002d39b4SEd Tanous             [&app](const crow::Request& req,
3168002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
317045ca1b86SEd Tanous         {
317145ca1b86SEd Tanous             return;
317245ca1b86SEd Tanous         }
31731476687dSEd Tanous 
31741476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
31751476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
31761476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
31771476687dSEd Tanous             "#LogService.v1_1_0.LogService";
31781476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
31791476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
31801476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
31811476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
31821476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
31831476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
31847c8c4058STejas Patil 
31857c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
31862b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
31870fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
31887c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
31897c8c4058STejas Patil             redfishDateTimeOffset.second;
31907c8c4058STejas Patil 
3191a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
31927e860f15SJohn Edward Broadbent             {"target",
31930fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
31947e860f15SJohn Edward Broadbent         });
3195a3316fc6SZhikuiRen }
3196a3316fc6SZhikuiRen 
31977e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3198a3316fc6SZhikuiRen {
31990fda0f12SGeorge Liu     BMCWEB_ROUTE(
32000fda0f12SGeorge Liu         app,
32010fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3202ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3203ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3204432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
320645ca1b86SEd Tanous             [&app](const crow::Request& req,
32077e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
32083ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
320945ca1b86SEd Tanous         {
321045ca1b86SEd Tanous             return;
321145ca1b86SEd Tanous         }
3212a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3213a3316fc6SZhikuiRen 
3214a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3215a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3216a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3217a3316fc6SZhikuiRen             if (ec)
3218a3316fc6SZhikuiRen             {
3219a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3220002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
32217e860f15SJohn Edward Broadbent                                  << ec;
3222002d39b4SEd Tanous                 asyncResp->res.result(
3223002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3224a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3225a3316fc6SZhikuiRen                 return;
3226a3316fc6SZhikuiRen             }
3227a3316fc6SZhikuiRen             },
322815124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
322915124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3230a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
32317e860f15SJohn Edward Broadbent         });
3232a3316fc6SZhikuiRen }
3233a3316fc6SZhikuiRen 
3234a3316fc6SZhikuiRen static void fillPostCodeEntry(
32358d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
32366c9a279eSManojkiran Eda     const boost::container::flat_map<
32376c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3238a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3239a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3240a3316fc6SZhikuiRen {
3241a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3242fffb8c1fSEd Tanous     const registries::Message* message =
3243fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3244a3316fc6SZhikuiRen 
3245a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3246a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3247a3316fc6SZhikuiRen 
3248a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
32496c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32506c9a279eSManojkiran Eda              code : postcode)
3251a3316fc6SZhikuiRen     {
3252a3316fc6SZhikuiRen         currentCodeIndex++;
3253a3316fc6SZhikuiRen         std::string postcodeEntryID =
3254a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3255a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3256a3316fc6SZhikuiRen 
3257a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3258a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3259a3316fc6SZhikuiRen 
3260a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3261a3316fc6SZhikuiRen         { // already incremented
3262a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3263a3316fc6SZhikuiRen         }
3264a3316fc6SZhikuiRen         else
3265a3316fc6SZhikuiRen         {
3266a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3267a3316fc6SZhikuiRen         }
3268a3316fc6SZhikuiRen 
3269a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3270a3316fc6SZhikuiRen         // not fall between top and skip
3271a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3272a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3273a3316fc6SZhikuiRen         {
3274a3316fc6SZhikuiRen             continue;
3275a3316fc6SZhikuiRen         }
3276a3316fc6SZhikuiRen 
32774e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3278a3316fc6SZhikuiRen         // currentIndex
3279a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3280a3316fc6SZhikuiRen         {
3281a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3282a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3283a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3284a3316fc6SZhikuiRen             continue;
3285a3316fc6SZhikuiRen         }
3286a3316fc6SZhikuiRen 
3287a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3288a3316fc6SZhikuiRen         // index
3289a3316fc6SZhikuiRen 
3290a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3291a3316fc6SZhikuiRen         std::string entryTimeStr;
32921d8782e7SNan Zhou         entryTimeStr =
32932b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3294a3316fc6SZhikuiRen 
3295a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3296a3316fc6SZhikuiRen         std::ostringstream hexCode;
3297a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32986c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3299a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3300a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3301a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3302a3316fc6SZhikuiRen         // Set precision to 4 digits
3303a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3304a3316fc6SZhikuiRen         // Add double to stream
3305a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3306a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3307a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3308a3316fc6SZhikuiRen 
3309a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3310a3316fc6SZhikuiRen         std::string msg;
3311a3316fc6SZhikuiRen         if (message != nullptr)
3312a3316fc6SZhikuiRen         {
3313a3316fc6SZhikuiRen             msg = message->message;
3314a3316fc6SZhikuiRen 
3315a3316fc6SZhikuiRen             // fill in this post code value
3316a3316fc6SZhikuiRen             int i = 0;
3317a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3318a3316fc6SZhikuiRen             {
3319a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3320a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3321a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3322a3316fc6SZhikuiRen                 {
3323a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3324a3316fc6SZhikuiRen                 }
3325a3316fc6SZhikuiRen             }
3326a3316fc6SZhikuiRen         }
3327a3316fc6SZhikuiRen 
3328d4342a92STim Lee         // Get Severity template from message registry
3329d4342a92STim Lee         std::string severity;
3330d4342a92STim Lee         if (message != nullptr)
3331d4342a92STim Lee         {
33325f2b84eeSEd Tanous             severity = message->messageSeverity;
3333d4342a92STim Lee         }
3334d4342a92STim Lee 
3335a3316fc6SZhikuiRen         // add to AsyncResp
3336a3316fc6SZhikuiRen         logEntryArray.push_back({});
3337a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
333884afc48bSJason M. Bills         bmcLogEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
333984afc48bSJason M. Bills         bmcLogEntry["@odata.id"] =
33400fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
334184afc48bSJason M. Bills             postcodeEntryID;
334284afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
334384afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
334484afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
334584afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
334684afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
334784afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
334884afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
334984afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3350647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3351647b3cdcSGeorge Liu         {
3352647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3353647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3354647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3355647b3cdcSGeorge Liu         }
3356a3316fc6SZhikuiRen     }
3357a3316fc6SZhikuiRen }
3358a3316fc6SZhikuiRen 
33598d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3360a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3361a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3362a3316fc6SZhikuiRen {
3363a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
33646c9a279eSManojkiran Eda         [aResp, bootIndex,
33656c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
33666c9a279eSManojkiran Eda                     const boost::container::flat_map<
33676c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33686c9a279eSManojkiran Eda                         postcode) {
3369a3316fc6SZhikuiRen         if (ec)
3370a3316fc6SZhikuiRen         {
3371a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3372a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3373a3316fc6SZhikuiRen             return;
3374a3316fc6SZhikuiRen         }
3375a3316fc6SZhikuiRen 
3376a3316fc6SZhikuiRen         // skip the empty postcode boots
3377a3316fc6SZhikuiRen         if (postcode.empty())
3378a3316fc6SZhikuiRen         {
3379a3316fc6SZhikuiRen             return;
3380a3316fc6SZhikuiRen         }
3381a3316fc6SZhikuiRen 
3382a3316fc6SZhikuiRen         fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3383a3316fc6SZhikuiRen 
3384a3316fc6SZhikuiRen         aResp->res.jsonValue["Members@odata.count"] =
3385a3316fc6SZhikuiRen             aResp->res.jsonValue["Members"].size();
3386a3316fc6SZhikuiRen         },
338715124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
338815124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3389a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3390a3316fc6SZhikuiRen         bootIndex);
3391a3316fc6SZhikuiRen }
3392a3316fc6SZhikuiRen 
33938d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3394a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3395a3316fc6SZhikuiRen                                const uint16_t bootCount,
33963648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
33973648c8beSEd Tanous                                size_t top)
3398a3316fc6SZhikuiRen {
3399a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3400a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3401a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
34026c9a279eSManojkiran Eda               const boost::container::flat_map<
34036c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
34046c9a279eSManojkiran Eda                   postcode) {
3405a3316fc6SZhikuiRen         if (ec)
3406a3316fc6SZhikuiRen         {
3407a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3408a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3409a3316fc6SZhikuiRen             return;
3410a3316fc6SZhikuiRen         }
3411a3316fc6SZhikuiRen 
3412a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3413a3316fc6SZhikuiRen         if (!postcode.empty())
3414a3316fc6SZhikuiRen         {
3415a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
34163648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3417a3316fc6SZhikuiRen             {
34183648c8beSEd Tanous                 uint64_t thisBootSkip =
34193648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
34203648c8beSEd Tanous                     entryCount;
3421a3316fc6SZhikuiRen                 uint64_t thisBootTop =
34223648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
34233648c8beSEd Tanous                     entryCount;
3424a3316fc6SZhikuiRen 
3425002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3426002d39b4SEd Tanous                                   thisBootTop);
3427a3316fc6SZhikuiRen             }
3428a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3429a3316fc6SZhikuiRen         }
3430a3316fc6SZhikuiRen 
3431a3316fc6SZhikuiRen         // continue to previous bootIndex
3432a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3433a3316fc6SZhikuiRen         {
3434a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3435a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3436a3316fc6SZhikuiRen         }
343781584abeSJiaqing Zhao         else if (skip + top < endCount)
3438a3316fc6SZhikuiRen         {
3439a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
34400fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3441a3316fc6SZhikuiRen                 std::to_string(skip + top);
3442a3316fc6SZhikuiRen         }
3443a3316fc6SZhikuiRen         },
344415124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
344515124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3446a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3447a3316fc6SZhikuiRen         bootIndex);
3448a3316fc6SZhikuiRen }
3449a3316fc6SZhikuiRen 
34508d1b46d7Szhanghch05 static void
34518d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
34523648c8beSEd Tanous                          size_t skip, size_t top)
3453a3316fc6SZhikuiRen {
3454a3316fc6SZhikuiRen     uint64_t entryCount = 0;
34551e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
34561e1e598dSJonathan Doman         *crow::connections::systemBus,
34571e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
34581e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
34591e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
34601e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
34611e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3462a3316fc6SZhikuiRen         if (ec)
3463a3316fc6SZhikuiRen         {
3464a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3465a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3466a3316fc6SZhikuiRen             return;
3467a3316fc6SZhikuiRen         }
34681e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
34691e1e598dSJonathan Doman         });
3470a3316fc6SZhikuiRen }
3471a3316fc6SZhikuiRen 
34727e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3473a3316fc6SZhikuiRen {
34747e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
34757e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3476ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
34777e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
347845ca1b86SEd Tanous             [&app](const crow::Request& req,
34797e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3480c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3481c937d2bfSEd Tanous             .canDelegateTop = true,
3482c937d2bfSEd Tanous             .canDelegateSkip = true,
3483c937d2bfSEd Tanous         };
3484c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3485c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
34863ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
348745ca1b86SEd Tanous         {
348845ca1b86SEd Tanous             return;
348945ca1b86SEd Tanous         }
3490a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3491a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3492a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3493a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3494a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3495a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3496a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3497a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3498a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
34993648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
35005143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
35013648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
35027e860f15SJohn Edward Broadbent         });
3503a3316fc6SZhikuiRen }
3504a3316fc6SZhikuiRen 
3505647b3cdcSGeorge Liu /**
3506647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3507647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3508647b3cdcSGeorge Liu  *
3509647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3510647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3511647b3cdcSGeorge Liu  * @param[out] index          Index value
3512647b3cdcSGeorge Liu  *
3513647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3514647b3cdcSGeorge Liu  */
3515647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3516647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3517647b3cdcSGeorge Liu {
3518647b3cdcSGeorge Liu     std::vector<std::string> split;
3519647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3520647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3521647b3cdcSGeorge Liu     {
3522647b3cdcSGeorge Liu         return false;
3523647b3cdcSGeorge Liu     }
3524647b3cdcSGeorge Liu 
3525ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3526647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3527ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3528647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3529647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3530647b3cdcSGeorge Liu 
3531647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3532647b3cdcSGeorge Liu     {
3533647b3cdcSGeorge Liu         return false;
3534647b3cdcSGeorge Liu     }
3535647b3cdcSGeorge Liu 
3536647b3cdcSGeorge Liu     start = split[1].data();
3537ca45aa3cSEd Tanous 
3538ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3539647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3540647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3541647b3cdcSGeorge Liu 
3542517d9a58STony Lee     return ptrValue == end && ecValue == std::errc();
3543647b3cdcSGeorge Liu }
3544647b3cdcSGeorge Liu 
3545647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3546647b3cdcSGeorge Liu {
35470fda0f12SGeorge Liu     BMCWEB_ROUTE(
35480fda0f12SGeorge Liu         app,
35490fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3550647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3551647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
355245ca1b86SEd Tanous             [&app](const crow::Request& req,
3553647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3554647b3cdcSGeorge Liu                    const std::string& postCodeID) {
35553ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
355645ca1b86SEd Tanous         {
355745ca1b86SEd Tanous             return;
355845ca1b86SEd Tanous         }
3559002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
3560647b3cdcSGeorge Liu         {
3561002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3562647b3cdcSGeorge Liu             return;
3563647b3cdcSGeorge Liu         }
3564647b3cdcSGeorge Liu 
3565647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3566647b3cdcSGeorge Liu         uint16_t index = 0;
3567647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3568647b3cdcSGeorge Liu         {
3569002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3570647b3cdcSGeorge Liu             return;
3571647b3cdcSGeorge Liu         }
3572647b3cdcSGeorge Liu 
3573647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3574647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3575647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3576002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3577002d39b4SEd Tanous                     postcodes) {
3578647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3579647b3cdcSGeorge Liu             {
3580002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3581002d39b4SEd Tanous                                            postCodeID);
3582647b3cdcSGeorge Liu                 return;
3583647b3cdcSGeorge Liu             }
3584647b3cdcSGeorge Liu             if (ec)
3585647b3cdcSGeorge Liu             {
3586647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3587647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
3588647b3cdcSGeorge Liu                 return;
3589647b3cdcSGeorge Liu             }
3590647b3cdcSGeorge Liu 
3591647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
3592002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
3593647b3cdcSGeorge Liu             {
3594647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3595002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3596002d39b4SEd Tanous                                            postCodeID);
3597647b3cdcSGeorge Liu                 return;
3598647b3cdcSGeorge Liu             }
3599647b3cdcSGeorge Liu 
36009eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
360146ff87baSEd Tanous             if (c.empty())
3602647b3cdcSGeorge Liu             {
3603647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
3604002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3605002d39b4SEd Tanous                                            postCodeID);
3606647b3cdcSGeorge Liu                 return;
3607647b3cdcSGeorge Liu             }
360846ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
360946ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
361046ff87baSEd Tanous             std::string_view strData(d, c.size());
3611647b3cdcSGeorge Liu 
3612d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
3613647b3cdcSGeorge Liu                                      "application/octet-stream");
3614d9f6c621SEd Tanous             asyncResp->res.addHeader(
3615d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
3616002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
3617647b3cdcSGeorge Liu             },
3618647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
3619647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
3620002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3621647b3cdcSGeorge Liu         });
3622647b3cdcSGeorge Liu }
3623647b3cdcSGeorge Liu 
36247e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3625a3316fc6SZhikuiRen {
36267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
36277e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3628ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
36297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
363045ca1b86SEd Tanous             [&app](const crow::Request& req,
36317e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36327e860f15SJohn Edward Broadbent                    const std::string& targetID) {
36333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
363445ca1b86SEd Tanous         {
363545ca1b86SEd Tanous             return;
363645ca1b86SEd Tanous         }
3637647b3cdcSGeorge Liu         uint16_t bootIndex = 0;
3638647b3cdcSGeorge Liu         uint64_t codeIndex = 0;
3639647b3cdcSGeorge Liu         if (!parsePostCode(targetID, codeIndex, bootIndex))
3640a3316fc6SZhikuiRen         {
3641a3316fc6SZhikuiRen             // Requested ID was not found
3642ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3643a3316fc6SZhikuiRen             return;
3644a3316fc6SZhikuiRen         }
3645a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3646a3316fc6SZhikuiRen         {
3647a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
36487e860f15SJohn Edward Broadbent                              << targetID;
3649a3316fc6SZhikuiRen         }
3650a3316fc6SZhikuiRen 
3651002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3652a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
36530fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3654a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3655a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3656a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3657a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3658a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3659a3316fc6SZhikuiRen 
3660a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
36617e860f15SJohn Edward Broadbent         });
3662a3316fc6SZhikuiRen }
3663a3316fc6SZhikuiRen 
36641da66f75SEd Tanous } // namespace redfish
3665