xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 22d268cb2c0bc00676d08c79f6ab8958bee74a25)
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>
41d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
42d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
43d1bde9e5SKrzysztof 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 
4999c11a172SVijay Lobo             thisEntry["@odata.type"] = "#LogEntry.v1_9_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
585d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
586d1bde9e5SKrzysztof Grobelny                                            entryID);
58735440d18SAsmitha Karunanithi                 return;
58835440d18SAsmitha Karunanithi             }
58935440d18SAsmitha Karunanithi 
5905cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
5919c11a172SVijay Lobo                 "#LogEntry.v1_9_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 {
878d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
879d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
880d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
881d1bde9e5SKrzysztof Grobelny 
882d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
883d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
884d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
885d1bde9e5SKrzysztof Grobelny 
886d1bde9e5SKrzysztof Grobelny     if (!success)
887043a0536SJohnathan Mantey     {
888d1bde9e5SKrzysztof Grobelny         return;
889043a0536SJohnathan Mantey     }
890d1bde9e5SKrzysztof Grobelny 
891d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
892043a0536SJohnathan Mantey     {
893d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
894d1bde9e5SKrzysztof Grobelny     }
895d1bde9e5SKrzysztof Grobelny 
896d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
897043a0536SJohnathan Mantey     {
898d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
899043a0536SJohnathan Mantey     }
900d1bde9e5SKrzysztof Grobelny 
901d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
902043a0536SJohnathan Mantey     {
903d1bde9e5SKrzysztof 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      */
913*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
914ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
915002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
916002d39b4SEd Tanous             [&app](const crow::Request& req,
917*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
918*22d268cbSEd Tanous                    const std::string& systemName) {
9193ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
920c4bf6374SJason M. Bills         {
92145ca1b86SEd Tanous             return;
92245ca1b86SEd Tanous         }
923*22d268cbSEd Tanous         if (systemName != "system")
924*22d268cbSEd Tanous         {
925*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
926*22d268cbSEd Tanous                                        systemName);
927*22d268cbSEd Tanous             return;
928*22d268cbSEd Tanous         }
929*22d268cbSEd Tanous 
9307e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
9317e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
932c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
933c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
934c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
935029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
93645ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
937c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
938c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
939002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
940c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
9411476687dSEd Tanous         nlohmann::json::object_t eventLog;
9421476687dSEd Tanous         eventLog["@odata.id"] =
9431476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
9441476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
9455cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
9461476687dSEd Tanous         nlohmann::json::object_t dumpLog;
947002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
9481476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
949c9bb6861Sraviteja-b #endif
950c9bb6861Sraviteja-b 
951d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
9521476687dSEd Tanous         nlohmann::json::object_t crashdump;
9531476687dSEd Tanous         crashdump["@odata.id"] =
9541476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
9551476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
956d53dd41fSJason M. Bills #endif
957b7028ebfSSpencer Ku 
958b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
9591476687dSEd Tanous         nlohmann::json::object_t hostlogger;
9601476687dSEd Tanous         hostlogger["@odata.id"] =
9611476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
9621476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
963b7028ebfSSpencer Ku #endif
964c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
965c4bf6374SJason M. Bills             logServiceArray.size();
966a3316fc6SZhikuiRen 
967a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
96845ca1b86SEd Tanous             [asyncResp](const boost::system::error_code ec,
969b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
970b9d36b47SEd Tanous                             subtreePath) {
971a3316fc6SZhikuiRen             if (ec)
972a3316fc6SZhikuiRen             {
973a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
974a3316fc6SZhikuiRen                 return;
975a3316fc6SZhikuiRen             }
976a3316fc6SZhikuiRen 
97755f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
978a3316fc6SZhikuiRen             {
979a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
980a3316fc6SZhikuiRen                 {
98123a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
982a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
983613dabeaSEd Tanous                     nlohmann::json::object_t member;
984613dabeaSEd Tanous                     member["@odata.id"] =
985613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
986613dabeaSEd Tanous 
987613dabeaSEd Tanous                     logServiceArrayLocal.push_back(std::move(member));
988613dabeaSEd Tanous 
98945ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
99023a21a1cSEd Tanous                         logServiceArrayLocal.size();
991a3316fc6SZhikuiRen                     return;
992a3316fc6SZhikuiRen                 }
993a3316fc6SZhikuiRen             }
994a3316fc6SZhikuiRen             },
995a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
996a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
99745ca1b86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
99845ca1b86SEd Tanous             std::array<const char*, 1>{postCodeIface});
9997e860f15SJohn Edward Broadbent         });
1000c4bf6374SJason M. Bills }
1001c4bf6374SJason M. Bills 
10027e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1003c4bf6374SJason M. Bills {
1004*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1005ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1006002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1007002d39b4SEd Tanous             [&app](const crow::Request& req,
1008*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1009*22d268cbSEd Tanous                    const std::string& systemName) {
10103ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
101145ca1b86SEd Tanous         {
101245ca1b86SEd Tanous             return;
101345ca1b86SEd Tanous         }
1014*22d268cbSEd Tanous         if (systemName != "system")
1015*22d268cbSEd Tanous         {
1016*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1017*22d268cbSEd Tanous                                        systemName);
1018*22d268cbSEd Tanous             return;
1019*22d268cbSEd Tanous         }
1020c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1021029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1022c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1023c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1024c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1025002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1026c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1027c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10287c8c4058STejas Patil 
10297c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
10302b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
10317c8c4058STejas Patil 
10327c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10337c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10347c8c4058STejas Patil             redfishDateTimeOffset.second;
10357c8c4058STejas Patil 
10361476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
10371476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1038e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1039e7d6c8b2SGunnar Mills 
10400fda0f12SGeorge Liu             {"target",
10410fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10427e860f15SJohn Edward Broadbent         });
1043489640c6SJason M. Bills }
1044489640c6SJason M. Bills 
10457e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1046489640c6SJason M. Bills {
10474978b63fSJason M. Bills     BMCWEB_ROUTE(
10484978b63fSJason M. Bills         app,
1049*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1050432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10517e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
105245ca1b86SEd Tanous             [&app](const crow::Request& req,
1053*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1054*22d268cbSEd Tanous                    const std::string& systemName) {
10553ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
105645ca1b86SEd Tanous         {
105745ca1b86SEd Tanous             return;
105845ca1b86SEd Tanous         }
1059*22d268cbSEd Tanous         if (systemName != "system")
1060*22d268cbSEd Tanous         {
1061*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1062*22d268cbSEd Tanous                                        systemName);
1063*22d268cbSEd Tanous             return;
1064*22d268cbSEd Tanous         }
1065489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1066489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1067489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1068489640c6SJason M. Bills         {
1069489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1070489640c6SJason M. Bills             {
1071489640c6SJason M. Bills                 std::error_code ec;
1072489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1073489640c6SJason M. Bills             }
1074489640c6SJason M. Bills         }
1075489640c6SJason M. Bills 
1076489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1077489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1078489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1079489640c6SJason M. Bills             if (ec)
1080489640c6SJason M. Bills             {
1081002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1082489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1083489640c6SJason M. Bills                 return;
1084489640c6SJason M. Bills             }
1085489640c6SJason M. Bills 
1086489640c6SJason M. Bills             messages::success(asyncResp->res);
1087489640c6SJason M. Bills             },
1088489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1089002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1090002d39b4SEd Tanous             "replace");
10917e860f15SJohn Edward Broadbent         });
1092c4bf6374SJason M. Bills }
1093c4bf6374SJason M. Bills 
1094ac992cdeSJason M. Bills enum class LogParseError
1095ac992cdeSJason M. Bills {
1096ac992cdeSJason M. Bills     success,
1097ac992cdeSJason M. Bills     parseFailed,
1098ac992cdeSJason M. Bills     messageIdNotInRegistry,
1099ac992cdeSJason M. Bills };
1100ac992cdeSJason M. Bills 
1101ac992cdeSJason M. Bills static LogParseError
1102ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1103b5a76932SEd Tanous                           const std::string& logEntry,
1104de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1105c4bf6374SJason M. Bills {
110695820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1107cd225da8SJason M. Bills     // First get the Timestamp
1108f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1109cd225da8SJason M. Bills     if (space == std::string::npos)
111095820184SJason M. Bills     {
1111ac992cdeSJason M. Bills         return LogParseError::parseFailed;
111295820184SJason M. Bills     }
1113cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1114cd225da8SJason M. Bills     // Then get the log contents
1115f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1116cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1117cd225da8SJason M. Bills     {
1118ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1119cd225da8SJason M. Bills     }
1120cd225da8SJason M. Bills     std::string_view entry(logEntry);
1121cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1122cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1123cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1124cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1125cd225da8SJason M. Bills                  boost::token_compress_on);
1126cd225da8SJason M. Bills     // We need at least a MessageId to be valid
112726f6976fSEd Tanous     if (logEntryFields.empty())
1128cd225da8SJason M. Bills     {
1129ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1130cd225da8SJason M. Bills     }
1131cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
113295820184SJason M. Bills 
11334851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1134fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1135c4bf6374SJason M. Bills 
113654417b02SSui Chen     if (message == nullptr)
1137c4bf6374SJason M. Bills     {
113854417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1139ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1140c4bf6374SJason M. Bills     }
1141c4bf6374SJason M. Bills 
114254417b02SSui Chen     std::string msg = message->message;
114354417b02SSui Chen 
114415a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
114526702d01SEd Tanous     std::span<std::string> messageArgs;
114615a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
114715a86ff6SJason M. Bills     {
114815a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
114915a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
115015a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
115115a86ff6SJason M. Bills         if (!messageArgsStart.empty())
115215a86ff6SJason M. Bills         {
115315a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
115415a86ff6SJason M. Bills         }
115515a86ff6SJason M. Bills 
115623a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1157c4bf6374SJason M. Bills 
11584851d45dSJason M. Bills         // Fill the MessageArgs into the Message
115995820184SJason M. Bills         int i = 0;
116095820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11614851d45dSJason M. Bills         {
116295820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11634851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11644851d45dSJason M. Bills             if (argPos != std::string::npos)
11654851d45dSJason M. Bills             {
116695820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11674851d45dSJason M. Bills             }
11684851d45dSJason M. Bills         }
116915a86ff6SJason M. Bills     }
11704851d45dSJason M. Bills 
117195820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
117295820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
117395820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1174f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1175f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
117695820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1177c4bf6374SJason M. Bills     {
117895820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1179c4bf6374SJason M. Bills     }
1180c4bf6374SJason M. Bills 
1181c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
11829c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
118384afc48bSJason M. Bills     logEntryJson["@odata.id"] =
118484afc48bSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
118584afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
118684afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
118784afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
118884afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
118984afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
119084afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
119184afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
119284afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1193ac992cdeSJason M. Bills     return LogParseError::success;
1194c4bf6374SJason M. Bills }
1195c4bf6374SJason M. Bills 
11967e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1197c4bf6374SJason M. Bills {
1198*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
11998b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1200002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1201002d39b4SEd Tanous             [&app](const crow::Request& req,
1202*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1203*22d268cbSEd Tanous                    const std::string& systemName) {
1204c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1205c937d2bfSEd Tanous             .canDelegateTop = true,
1206c937d2bfSEd Tanous             .canDelegateSkip = true,
1207c937d2bfSEd Tanous         };
1208c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1209c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
12103ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1211c4bf6374SJason M. Bills         {
1212c4bf6374SJason M. Bills             return;
1213c4bf6374SJason M. Bills         }
1214*22d268cbSEd Tanous         if (systemName != "system")
1215*22d268cbSEd Tanous         {
1216*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1217*22d268cbSEd Tanous                                        systemName);
1218*22d268cbSEd Tanous             return;
1219*22d268cbSEd Tanous         }
1220*22d268cbSEd Tanous 
12215143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
12223648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
12233648c8beSEd Tanous 
12247e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
12257e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1226c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1227c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1228c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1229029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1230c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1231c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1232c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1233cb92c03bSAndrew Geissler 
12344978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1235c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
12367e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
12377e860f15SJohn Edward Broadbent         // entry
123895820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
123995820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1240b01bf299SEd Tanous         uint64_t entryCount = 0;
1241cd225da8SJason M. Bills         std::string logEntry;
124295820184SJason M. Bills 
12437e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
12447e860f15SJohn Edward Broadbent         // backwards
1245002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1246002d39b4SEd Tanous              it++)
1247c4bf6374SJason M. Bills         {
1248cd225da8SJason M. Bills             std::ifstream logStream(*it);
124995820184SJason M. Bills             if (!logStream.is_open())
1250c4bf6374SJason M. Bills             {
1251c4bf6374SJason M. Bills                 continue;
1252c4bf6374SJason M. Bills             }
1253c4bf6374SJason M. Bills 
1254e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1255e85d6b16SJason M. Bills             bool firstEntry = true;
125695820184SJason M. Bills             while (std::getline(logStream, logEntry))
125795820184SJason M. Bills             {
1258c4bf6374SJason M. Bills                 std::string idStr;
1259e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1260c4bf6374SJason M. Bills                 {
1261c4bf6374SJason M. Bills                     continue;
1262c4bf6374SJason M. Bills                 }
1263e85d6b16SJason M. Bills                 firstEntry = false;
1264e85d6b16SJason M. Bills 
1265de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1266ac992cdeSJason M. Bills                 LogParseError status =
1267ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1268ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1269ac992cdeSJason M. Bills                 {
1270ac992cdeSJason M. Bills                     continue;
1271ac992cdeSJason M. Bills                 }
1272ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1273c4bf6374SJason M. Bills                 {
1274c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1275c4bf6374SJason M. Bills                     return;
1276c4bf6374SJason M. Bills                 }
1277de703c5dSJason M. Bills 
1278de703c5dSJason M. Bills                 entryCount++;
1279de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1280de703c5dSJason M. Bills                 // start) and top (number of entries to display)
12813648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1282de703c5dSJason M. Bills                 {
1283de703c5dSJason M. Bills                     continue;
1284de703c5dSJason M. Bills                 }
1285de703c5dSJason M. Bills 
1286de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1287c4bf6374SJason M. Bills             }
128895820184SJason M. Bills         }
1289c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
12903648c8beSEd Tanous         if (skip + top < entryCount)
1291c4bf6374SJason M. Bills         {
1292c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
12934978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
12943648c8beSEd Tanous                 std::to_string(skip + top);
1295c4bf6374SJason M. Bills         }
12967e860f15SJohn Edward Broadbent         });
1297897967deSJason M. Bills }
1298897967deSJason M. Bills 
12997e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1300897967deSJason M. Bills {
13017e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1302*22d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1303ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
13047e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
130545ca1b86SEd Tanous             [&app](const crow::Request& req,
13067e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1307*22d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
13083ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
130945ca1b86SEd Tanous         {
131045ca1b86SEd Tanous             return;
131145ca1b86SEd Tanous         }
1312*22d268cbSEd Tanous 
1313*22d268cbSEd Tanous         if (systemName != "system")
1314*22d268cbSEd Tanous         {
1315*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1316*22d268cbSEd Tanous                                        systemName);
1317*22d268cbSEd Tanous             return;
1318*22d268cbSEd Tanous         }
1319*22d268cbSEd Tanous 
13207e860f15SJohn Edward Broadbent         const std::string& targetID = param;
13218d1b46d7Szhanghch05 
13227e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
13237e860f15SJohn Edward Broadbent         // entry to find the target entry
1324897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1325897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1326897967deSJason M. Bills         std::string logEntry;
1327897967deSJason M. Bills 
13287e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
13297e860f15SJohn Edward Broadbent         // backwards
1330002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1331002d39b4SEd Tanous              it++)
1332897967deSJason M. Bills         {
1333897967deSJason M. Bills             std::ifstream logStream(*it);
1334897967deSJason M. Bills             if (!logStream.is_open())
1335897967deSJason M. Bills             {
1336897967deSJason M. Bills                 continue;
1337897967deSJason M. Bills             }
1338897967deSJason M. Bills 
1339897967deSJason M. Bills             // Reset the unique ID on the first entry
1340897967deSJason M. Bills             bool firstEntry = true;
1341897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1342897967deSJason M. Bills             {
1343897967deSJason M. Bills                 std::string idStr;
1344897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1345897967deSJason M. Bills                 {
1346897967deSJason M. Bills                     continue;
1347897967deSJason M. Bills                 }
1348897967deSJason M. Bills                 firstEntry = false;
1349897967deSJason M. Bills 
1350897967deSJason M. Bills                 if (idStr == targetID)
1351897967deSJason M. Bills                 {
1352de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1353ac992cdeSJason M. Bills                     LogParseError status =
1354ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1355ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1356897967deSJason M. Bills                     {
1357897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1358897967deSJason M. Bills                         return;
1359897967deSJason M. Bills                     }
1360d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1361897967deSJason M. Bills                     return;
1362897967deSJason M. Bills                 }
1363897967deSJason M. Bills             }
1364897967deSJason M. Bills         }
1365897967deSJason M. Bills         // Requested ID was not found
1366002d39b4SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
1367002d39b4SEd Tanous                                        crow::utility::urlFromPieces(targetID));
13687e860f15SJohn Edward Broadbent         });
136908a4e4b5SAnthony Wilson }
137008a4e4b5SAnthony Wilson 
13717e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
137208a4e4b5SAnthony Wilson {
1373*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1374ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1375002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1376002d39b4SEd Tanous             [&app](const crow::Request& req,
1377*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1378*22d268cbSEd Tanous                    const std::string& systemName) {
13793ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
138045ca1b86SEd Tanous         {
138145ca1b86SEd Tanous             return;
138245ca1b86SEd Tanous         }
1383*22d268cbSEd Tanous         if (systemName != "system")
1384*22d268cbSEd Tanous         {
1385*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1386*22d268cbSEd Tanous                                        systemName);
1387*22d268cbSEd Tanous             return;
1388*22d268cbSEd Tanous         }
1389*22d268cbSEd Tanous 
13907e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13917e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
139208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
139308a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
139408a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
139508a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
139608a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
139708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
139808a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
139908a4e4b5SAnthony Wilson 
1400cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1401cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1402cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1403cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1404914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1405cb92c03bSAndrew Geissler             if (ec)
1406cb92c03bSAndrew Geissler             {
1407cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1408cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1409002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1410cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1411cb92c03bSAndrew Geissler                 return;
1412cb92c03bSAndrew Geissler             }
1413002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1414cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
14159eb808c1SEd Tanous             for (const auto& objectPath : resp)
1416cb92c03bSAndrew Geissler             {
1417914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1418c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1419c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1420914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1421914e2d5dSEd Tanous                 const std::string* message = nullptr;
1422914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
14239c11a172SVijay Lobo                 const std::string* resolution = nullptr;
142475710de2SXiaochao Ma                 bool resolved = false;
14259eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1426f86bb901SAdriana Kobylak                 {
1427f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1428f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1429f86bb901SAdriana Kobylak                     {
1430002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1431cb92c03bSAndrew Geissler                         {
1432cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1433cb92c03bSAndrew Geissler                             {
1434002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1435cb92c03bSAndrew Geissler                             }
1436cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1437cb92c03bSAndrew Geissler                             {
1438002d39b4SEd Tanous                                 timestamp =
1439002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
14407e860f15SJohn Edward Broadbent                             }
1441002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
14427e860f15SJohn Edward Broadbent                             {
1443002d39b4SEd Tanous                                 updateTimestamp =
1444002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
14457e860f15SJohn Edward Broadbent                             }
14467e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
14477e860f15SJohn Edward Broadbent                             {
14487e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
14497e860f15SJohn Edward Broadbent                                     &propertyMap.second);
14507e860f15SJohn Edward Broadbent                             }
14519c11a172SVijay Lobo                             else if (propertyMap.first == "Resolution")
14529c11a172SVijay Lobo                             {
14539c11a172SVijay Lobo                                 resolution = std::get_if<std::string>(
14549c11a172SVijay Lobo                                     &propertyMap.second);
14559c11a172SVijay Lobo                             }
14567e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
14577e860f15SJohn Edward Broadbent                             {
14587e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
14597e860f15SJohn Edward Broadbent                                     &propertyMap.second);
14607e860f15SJohn Edward Broadbent                             }
14617e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
14627e860f15SJohn Edward Broadbent                             {
1463914e2d5dSEd Tanous                                 const bool* resolveptr =
1464002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
14657e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
14667e860f15SJohn Edward Broadbent                                 {
1467002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
14687e860f15SJohn Edward Broadbent                                     return;
14697e860f15SJohn Edward Broadbent                                 }
14707e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
14717e860f15SJohn Edward Broadbent                             }
14727e860f15SJohn Edward Broadbent                         }
14737e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
14747e860f15SJohn Edward Broadbent                             severity == nullptr)
14757e860f15SJohn Edward Broadbent                         {
14767e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
14777e860f15SJohn Edward Broadbent                             return;
14787e860f15SJohn Edward Broadbent                         }
14797e860f15SJohn Edward Broadbent                     }
14807e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
14817e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
14827e860f15SJohn Edward Broadbent                     {
1483002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
14847e860f15SJohn Edward Broadbent                         {
14857e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
14867e860f15SJohn Edward Broadbent                             {
14877e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
14887e860f15SJohn Edward Broadbent                                     &propertyMap.second);
14897e860f15SJohn Edward Broadbent                             }
14907e860f15SJohn Edward Broadbent                         }
14917e860f15SJohn Edward Broadbent                     }
14927e860f15SJohn Edward Broadbent                 }
14937e860f15SJohn Edward Broadbent                 // Object path without the
14947e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
14957e860f15SJohn Edward Broadbent                 // and continue.
14967e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1497c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1498c419c759SEd Tanous                     updateTimestamp == nullptr)
14997e860f15SJohn Edward Broadbent                 {
15007e860f15SJohn Edward Broadbent                     continue;
15017e860f15SJohn Edward Broadbent                 }
15027e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
15037e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
15049c11a172SVijay Lobo                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
15057e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
15060fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
15077e860f15SJohn Edward Broadbent                     std::to_string(*id);
15087e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
15097e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
15107e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
15117e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
15129c11a172SVijay Lobo                 if ((resolution != nullptr) && (!(*resolution).empty()))
15139c11a172SVijay Lobo                 {
15149c11a172SVijay Lobo                     thisEntry["Resolution"] = *resolution;
15159c11a172SVijay Lobo                 }
15167e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
15177e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
15187e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
15197e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
15202b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
15217e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
15222b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
15237e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
15247e860f15SJohn Edward Broadbent                 {
15257e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
15260fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
15277e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
15287e860f15SJohn Edward Broadbent                 }
15297e860f15SJohn Edward Broadbent             }
1530002d39b4SEd Tanous             std::sort(
1531002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1532002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
15337e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
15347e860f15SJohn Edward Broadbent                 });
15357e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
15367e860f15SJohn Edward Broadbent                 entriesArray.size();
15377e860f15SJohn Edward Broadbent             },
15387e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
15397e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15407e860f15SJohn Edward Broadbent         });
15417e860f15SJohn Edward Broadbent }
15427e860f15SJohn Edward Broadbent 
15437e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
15447e860f15SJohn Edward Broadbent {
15457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1546*22d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1547ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1548002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1549002d39b4SEd Tanous             [&app](const crow::Request& req,
15507e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1551*22d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
15523ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
15537e860f15SJohn Edward Broadbent         {
155445ca1b86SEd Tanous             return;
155545ca1b86SEd Tanous         }
1556*22d268cbSEd Tanous         if (systemName != "system")
1557*22d268cbSEd Tanous         {
1558*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1559*22d268cbSEd Tanous                                        systemName);
1560*22d268cbSEd Tanous             return;
1561*22d268cbSEd Tanous         }
1562*22d268cbSEd Tanous 
15637e860f15SJohn Edward Broadbent         std::string entryID = param;
15647e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
15657e860f15SJohn Edward Broadbent 
15667e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
15677e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1568d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1569d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1570d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
1571002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1572b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
15737e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
15747e860f15SJohn Edward Broadbent             {
1575d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1576d1bde9e5SKrzysztof Grobelny                                            entryID);
15777e860f15SJohn Edward Broadbent                 return;
15787e860f15SJohn Edward Broadbent             }
15797e860f15SJohn Edward Broadbent             if (ec)
15807e860f15SJohn Edward Broadbent             {
15810fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1582002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
15837e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
15847e860f15SJohn Edward Broadbent                 return;
15857e860f15SJohn Edward Broadbent             }
1586914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1587c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1588c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1589914e2d5dSEd Tanous             const std::string* severity = nullptr;
1590914e2d5dSEd Tanous             const std::string* message = nullptr;
1591914e2d5dSEd Tanous             const std::string* filePath = nullptr;
15929c11a172SVijay Lobo             const std::string* resolution = nullptr;
15937e860f15SJohn Edward Broadbent             bool resolved = false;
15947e860f15SJohn Edward Broadbent 
1595d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1596d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1597d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
15989c11a172SVijay Lobo                 severity, "Message", message, "Resolved", resolved,
15999c11a172SVijay Lobo                 "Resolution", resolution, "Path", filePath);
1600d1bde9e5SKrzysztof Grobelny 
1601d1bde9e5SKrzysztof Grobelny             if (!success)
160275710de2SXiaochao Ma             {
160375710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
160475710de2SXiaochao Ma                 return;
160575710de2SXiaochao Ma             }
1606d1bde9e5SKrzysztof Grobelny 
1607002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
1608002d39b4SEd Tanous                 timestamp == nullptr || updateTimestamp == nullptr)
1609f86bb901SAdriana Kobylak             {
1610ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1611271584abSEd Tanous                 return;
1612271584abSEd Tanous             }
1613f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
16149c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
1615f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
16160fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1617f86bb901SAdriana Kobylak                 std::to_string(*id);
161845ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1619f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1620f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1621f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
16229c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
16239c11a172SVijay Lobo             {
16249c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
16259c11a172SVijay Lobo             }
1626f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1627f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1628f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1629f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
16302b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1631f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
16322b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1633f86bb901SAdriana Kobylak             if (filePath != nullptr)
1634f86bb901SAdriana Kobylak             {
1635f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1636e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1637e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1638f86bb901SAdriana Kobylak             }
1639d1bde9e5SKrzysztof Grobelny             });
16407e860f15SJohn Edward Broadbent         });
1641336e96c6SChicago Duan 
16427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1643*22d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1644ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
164645ca1b86SEd Tanous             [&app](const crow::Request& req,
16477e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1648*22d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
16493ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
165045ca1b86SEd Tanous         {
165145ca1b86SEd Tanous             return;
165245ca1b86SEd Tanous         }
1653*22d268cbSEd Tanous         if (systemName != "system")
1654*22d268cbSEd Tanous         {
1655*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1656*22d268cbSEd Tanous                                        systemName);
1657*22d268cbSEd Tanous             return;
1658*22d268cbSEd Tanous         }
165975710de2SXiaochao Ma         std::optional<bool> resolved;
166075710de2SXiaochao Ma 
166115ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
16627e860f15SJohn Edward Broadbent                                       resolved))
166375710de2SXiaochao Ma         {
166475710de2SXiaochao Ma             return;
166575710de2SXiaochao Ma         }
166675710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
166775710de2SXiaochao Ma 
166875710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
16694f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
167075710de2SXiaochao Ma             if (ec)
167175710de2SXiaochao Ma             {
167275710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
167375710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
167475710de2SXiaochao Ma                 return;
167575710de2SXiaochao Ma             }
167675710de2SXiaochao Ma             },
167775710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
167875710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
167975710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
168075710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1681168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
16827e860f15SJohn Edward Broadbent         });
168375710de2SXiaochao Ma 
16847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1685*22d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1686ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1687ed398213SEd Tanous 
1688002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1689002d39b4SEd Tanous             [&app](const crow::Request& req,
1690002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1691*22d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
16923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1693336e96c6SChicago Duan         {
169445ca1b86SEd Tanous             return;
169545ca1b86SEd Tanous         }
1696*22d268cbSEd Tanous         if (systemName != "system")
1697*22d268cbSEd Tanous         {
1698*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1699*22d268cbSEd Tanous                                        systemName);
1700*22d268cbSEd Tanous             return;
1701*22d268cbSEd Tanous         }
1702336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1703336e96c6SChicago Duan 
17047e860f15SJohn Edward Broadbent         std::string entryID = param;
1705336e96c6SChicago Duan 
1706336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1707336e96c6SChicago Duan 
1708336e96c6SChicago Duan         // Process response from Logging service.
1709002d39b4SEd Tanous         auto respHandler =
1710002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1711002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1712336e96c6SChicago Duan             if (ec)
1713336e96c6SChicago Duan             {
17143de8d8baSGeorge Liu                 if (ec.value() == EBADR)
17153de8d8baSGeorge Liu                 {
171645ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
171745ca1b86SEd Tanous                                                entryID);
17183de8d8baSGeorge Liu                     return;
17193de8d8baSGeorge Liu                 }
1720336e96c6SChicago Duan                 // TODO Handle for specific error code
17210fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
17220fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1723336e96c6SChicago Duan                     << ec;
1724336e96c6SChicago Duan                 asyncResp->res.result(
1725336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1726336e96c6SChicago Duan                 return;
1727336e96c6SChicago Duan             }
1728336e96c6SChicago Duan 
1729336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1730336e96c6SChicago Duan         };
1731336e96c6SChicago Duan 
1732336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1733336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1734336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1735336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1736336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
17377e860f15SJohn Edward Broadbent         });
1738400fd1fbSAdriana Kobylak }
1739400fd1fbSAdriana Kobylak 
17407e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1741400fd1fbSAdriana Kobylak {
17420fda0f12SGeorge Liu     BMCWEB_ROUTE(
17430fda0f12SGeorge Liu         app,
1744*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1745ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17467e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
174745ca1b86SEd Tanous             [&app](const crow::Request& req,
17487e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1749*22d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
17503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
17517e860f15SJohn Edward Broadbent         {
175245ca1b86SEd Tanous             return;
175345ca1b86SEd Tanous         }
175499351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
175599351cd8SEd Tanous                 req.getHeaderValue("Accept"),
17564a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1757400fd1fbSAdriana Kobylak         {
1758002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1759400fd1fbSAdriana Kobylak             return;
1760400fd1fbSAdriana Kobylak         }
1761*22d268cbSEd Tanous         if (systemName != "system")
1762*22d268cbSEd Tanous         {
1763*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1764*22d268cbSEd Tanous                                        systemName);
1765*22d268cbSEd Tanous             return;
1766*22d268cbSEd Tanous         }
1767400fd1fbSAdriana Kobylak 
17687e860f15SJohn Edward Broadbent         std::string entryID = param;
1769400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1770400fd1fbSAdriana Kobylak 
1771400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1772002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1773400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1774400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1775400fd1fbSAdriana Kobylak             {
1776002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1777002d39b4SEd Tanous                                            entryID);
1778400fd1fbSAdriana Kobylak                 return;
1779400fd1fbSAdriana Kobylak             }
1780400fd1fbSAdriana Kobylak             if (ec)
1781400fd1fbSAdriana Kobylak             {
1782400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1783400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1784400fd1fbSAdriana Kobylak                 return;
1785400fd1fbSAdriana Kobylak             }
1786400fd1fbSAdriana Kobylak 
1787400fd1fbSAdriana Kobylak             int fd = -1;
1788400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1789400fd1fbSAdriana Kobylak             if (fd == -1)
1790400fd1fbSAdriana Kobylak             {
1791400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1792400fd1fbSAdriana Kobylak                 return;
1793400fd1fbSAdriana Kobylak             }
1794400fd1fbSAdriana Kobylak 
1795400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1796400fd1fbSAdriana Kobylak             if (size == -1)
1797400fd1fbSAdriana Kobylak             {
1798400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1799400fd1fbSAdriana Kobylak                 return;
1800400fd1fbSAdriana Kobylak             }
1801400fd1fbSAdriana Kobylak 
1802400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1803400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1804400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1805400fd1fbSAdriana Kobylak             {
1806002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1807400fd1fbSAdriana Kobylak                                  << maxFileSize;
1808400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1809400fd1fbSAdriana Kobylak                 return;
1810400fd1fbSAdriana Kobylak             }
1811400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1812400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1813400fd1fbSAdriana Kobylak             if (rc == -1)
1814400fd1fbSAdriana Kobylak             {
1815400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1816400fd1fbSAdriana Kobylak                 return;
1817400fd1fbSAdriana Kobylak             }
1818400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1819400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1820400fd1fbSAdriana Kobylak             {
1821400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1822400fd1fbSAdriana Kobylak                 return;
1823400fd1fbSAdriana Kobylak             }
1824400fd1fbSAdriana Kobylak             close(fd);
1825400fd1fbSAdriana Kobylak 
1826400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1827002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1828400fd1fbSAdriana Kobylak 
1829d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1830400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1831d9f6c621SEd Tanous             asyncResp->res.addHeader(
1832d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
1833400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1834400fd1fbSAdriana Kobylak             },
1835400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1836400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1837400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
18387e860f15SJohn Edward Broadbent         });
18391da66f75SEd Tanous }
18401da66f75SEd Tanous 
1841b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1842b7028ebfSSpencer Ku 
1843b7028ebfSSpencer Ku inline bool
1844b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1845b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1846b7028ebfSSpencer Ku {
1847b7028ebfSSpencer Ku     std::error_code ec;
1848b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1849b7028ebfSSpencer Ku     if (ec)
1850b7028ebfSSpencer Ku     {
1851b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1852b7028ebfSSpencer Ku         return false;
1853b7028ebfSSpencer Ku     }
1854b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1855b7028ebfSSpencer Ku     {
1856b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1857b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1858b7028ebfSSpencer Ku         // path
185911ba3979SEd Tanous         if (filename.starts_with("log"))
1860b7028ebfSSpencer Ku         {
1861b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1862b7028ebfSSpencer Ku         }
1863b7028ebfSSpencer Ku     }
1864b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1865b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1866b7028ebfSSpencer Ku     // descending order.
1867b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1868b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1869b7028ebfSSpencer Ku 
1870b7028ebfSSpencer Ku     return true;
1871b7028ebfSSpencer Ku }
1872b7028ebfSSpencer Ku 
187302cad96eSEd Tanous inline bool getHostLoggerEntries(
187402cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
187502cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
1876b7028ebfSSpencer Ku {
1877b7028ebfSSpencer Ku     GzFileReader logFile;
1878b7028ebfSSpencer Ku 
1879b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1880b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1881b7028ebfSSpencer Ku     {
1882b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1883b7028ebfSSpencer Ku         {
1884b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1885b7028ebfSSpencer Ku             return false;
1886b7028ebfSSpencer Ku         }
1887b7028ebfSSpencer Ku     }
1888b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1889b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1890b7028ebfSSpencer Ku     if (!lastMessage.empty())
1891b7028ebfSSpencer Ku     {
1892b7028ebfSSpencer Ku         logCount++;
1893b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1894b7028ebfSSpencer Ku         {
1895b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1896b7028ebfSSpencer Ku         }
1897b7028ebfSSpencer Ku     }
1898b7028ebfSSpencer Ku     return true;
1899b7028ebfSSpencer Ku }
1900b7028ebfSSpencer Ku 
1901b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1902b7028ebfSSpencer Ku                                     const std::string& msg,
19036d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
1904b7028ebfSSpencer Ku {
1905b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
19069c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
19076d6574c9SJason M. Bills     logEntryJson["@odata.id"] =
1908b7028ebfSSpencer Ku         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
19096d6574c9SJason M. Bills         logEntryID;
19106d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
19116d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
19126d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
19136d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
19146d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
19156d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
1916b7028ebfSSpencer Ku }
1917b7028ebfSSpencer Ku 
1918b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1919b7028ebfSSpencer Ku {
1920*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
1921b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
19221476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
19231476687dSEd Tanous             [&app](const crow::Request& req,
1924*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1925*22d268cbSEd Tanous                    const std::string& systemName) {
19263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
192745ca1b86SEd Tanous         {
192845ca1b86SEd Tanous             return;
192945ca1b86SEd Tanous         }
1930*22d268cbSEd Tanous         if (systemName != "system")
1931*22d268cbSEd Tanous         {
1932*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1933*22d268cbSEd Tanous                                        systemName);
1934*22d268cbSEd Tanous             return;
1935*22d268cbSEd Tanous         }
1936b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1937b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
1938b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1939b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
1940b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1941b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1942b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
19431476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
19441476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1945b7028ebfSSpencer Ku         });
1946b7028ebfSSpencer Ku }
1947b7028ebfSSpencer Ku 
1948b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1949b7028ebfSSpencer Ku {
1950b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1951*22d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
1952b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1953002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1954002d39b4SEd Tanous             [&app](const crow::Request& req,
1955*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1956*22d268cbSEd Tanous                    const std::string& systemName) {
1957c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1958c937d2bfSEd Tanous             .canDelegateTop = true,
1959c937d2bfSEd Tanous             .canDelegateSkip = true,
1960c937d2bfSEd Tanous         };
1961c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1962c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
19633ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1964b7028ebfSSpencer Ku         {
1965b7028ebfSSpencer Ku             return;
1966b7028ebfSSpencer Ku         }
1967*22d268cbSEd Tanous         if (systemName != "system")
1968*22d268cbSEd Tanous         {
1969*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
1970*22d268cbSEd Tanous                                        systemName);
1971*22d268cbSEd Tanous             return;
1972*22d268cbSEd Tanous         }
1973b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1974b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1975b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1976b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
1977b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1978b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
1979b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
19800fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1981b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
1982b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
1983b7028ebfSSpencer Ku 
1984b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1985b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1986b7028ebfSSpencer Ku         {
1987b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1988b7028ebfSSpencer Ku             return;
1989b7028ebfSSpencer Ku         }
19903648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
19913648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
19925143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
1993b7028ebfSSpencer Ku         size_t logCount = 0;
1994b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
1995b7028ebfSSpencer Ku         // control by skip and top.
1996b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
19973648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
19983648c8beSEd Tanous                                   logCount))
1999b7028ebfSSpencer Ku         {
2000b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2001b7028ebfSSpencer Ku             return;
2002b7028ebfSSpencer Ku         }
2003b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2004b7028ebfSSpencer Ku         // log count
200526f6976fSEd Tanous         if (logEntries.empty())
2006b7028ebfSSpencer Ku         {
2007b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2008b7028ebfSSpencer Ku             return;
2009b7028ebfSSpencer Ku         }
201026f6976fSEd Tanous         if (!logEntries.empty())
2011b7028ebfSSpencer Ku         {
2012b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2013b7028ebfSSpencer Ku             {
20146d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
20153648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
20163648c8beSEd Tanous                                         hostLogEntry);
20176d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
2018b7028ebfSSpencer Ku             }
2019b7028ebfSSpencer Ku 
2020b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
20213648c8beSEd Tanous             if (skip + top < logCount)
2022b7028ebfSSpencer Ku             {
2023b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
20240fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
20253648c8beSEd Tanous                     std::to_string(skip + top);
2026b7028ebfSSpencer Ku             }
2027b7028ebfSSpencer Ku         }
2028b7028ebfSSpencer Ku         });
2029b7028ebfSSpencer Ku }
2030b7028ebfSSpencer Ku 
2031b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2032b7028ebfSSpencer Ku {
2033b7028ebfSSpencer Ku     BMCWEB_ROUTE(
2034*22d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2035b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2036b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
203745ca1b86SEd Tanous             [&app](const crow::Request& req,
2038b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2039*22d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
20403ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
204145ca1b86SEd Tanous         {
204245ca1b86SEd Tanous             return;
204345ca1b86SEd Tanous         }
2044*22d268cbSEd Tanous         if (systemName != "system")
2045*22d268cbSEd Tanous         {
2046*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2047*22d268cbSEd Tanous                                        systemName);
2048*22d268cbSEd Tanous             return;
2049*22d268cbSEd Tanous         }
2050b7028ebfSSpencer Ku         const std::string& targetID = param;
2051b7028ebfSSpencer Ku 
2052b7028ebfSSpencer Ku         uint64_t idInt = 0;
2053ca45aa3cSEd Tanous 
2054ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2055ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
2056ca45aa3cSEd Tanous 
2057ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
2058b7028ebfSSpencer Ku         if (ec == std::errc::invalid_argument)
2059b7028ebfSSpencer Ku         {
2060ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2061b7028ebfSSpencer Ku             return;
2062b7028ebfSSpencer Ku         }
2063b7028ebfSSpencer Ku         if (ec == std::errc::result_out_of_range)
2064b7028ebfSSpencer Ku         {
2065ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2066b7028ebfSSpencer Ku             return;
2067b7028ebfSSpencer Ku         }
2068b7028ebfSSpencer Ku 
2069b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2070b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2071b7028ebfSSpencer Ku         {
2072b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2073b7028ebfSSpencer Ku             return;
2074b7028ebfSSpencer Ku         }
2075b7028ebfSSpencer Ku 
2076b7028ebfSSpencer Ku         size_t logCount = 0;
20773648c8beSEd Tanous         size_t top = 1;
2078b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2079b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2080b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2081b7028ebfSSpencer Ku         // get that entry
2082002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2083002d39b4SEd Tanous                                   logCount))
2084b7028ebfSSpencer Ku         {
2085b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2086b7028ebfSSpencer Ku             return;
2087b7028ebfSSpencer Ku         }
2088b7028ebfSSpencer Ku 
2089b7028ebfSSpencer Ku         if (!logEntries.empty())
2090b7028ebfSSpencer Ku         {
20916d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
20926d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
20936d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2094b7028ebfSSpencer Ku             return;
2095b7028ebfSSpencer Ku         }
2096b7028ebfSSpencer Ku 
2097b7028ebfSSpencer Ku         // Requested ID was not found
2098ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2099b7028ebfSSpencer Ku         });
2100b7028ebfSSpencer Ku }
2101b7028ebfSSpencer Ku 
2102fdd26906SClaire Weinan constexpr char const* dumpManagerIface =
2103fdd26906SClaire Weinan     "xyz.openbmc_project.Collection.DeleteAll";
2104dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2105fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2106fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21071da66f75SEd Tanous {
21083ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
210945ca1b86SEd Tanous     {
211045ca1b86SEd Tanous         return;
211145ca1b86SEd Tanous     }
21127e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
21137e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2114e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
21151da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2116e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2117e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2118002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2119e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
21201da66f75SEd Tanous         "Collection of LogServices for this Manager";
2121002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2122c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2123fdd26906SClaire Weinan 
2124c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2125613dabeaSEd Tanous     nlohmann::json::object_t journal;
2126613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2127613dabeaSEd Tanous     logServiceArray.push_back(std::move(journal));
2128c4bf6374SJason M. Bills #endif
2129fdd26906SClaire Weinan 
2130fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2131fdd26906SClaire Weinan 
2132fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2133fdd26906SClaire Weinan     auto respHandler =
2134fdd26906SClaire Weinan         [asyncResp](
2135fdd26906SClaire Weinan             const boost::system::error_code ec,
2136fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2137fdd26906SClaire Weinan         if (ec)
2138fdd26906SClaire Weinan         {
2139fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2140dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2141fdd26906SClaire Weinan                 << ec;
2142fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2143fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2144fdd26906SClaire Weinan             return;
2145fdd26906SClaire Weinan         }
2146fdd26906SClaire Weinan 
2147fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2148fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2149fdd26906SClaire Weinan 
2150fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2151fdd26906SClaire Weinan         {
2152fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2153fdd26906SClaire Weinan             {
2154613dabeaSEd Tanous                 nlohmann::json::object_t member;
2155613dabeaSEd Tanous                 member["@odata.id"] =
2156613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2157613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2158fdd26906SClaire Weinan             }
2159fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2160fdd26906SClaire Weinan             {
2161613dabeaSEd Tanous                 nlohmann::json::object_t member;
2162613dabeaSEd Tanous                 member["@odata.id"] =
2163613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2164613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2165fdd26906SClaire Weinan             }
2166fdd26906SClaire Weinan         }
2167fdd26906SClaire Weinan 
2168e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2169fdd26906SClaire Weinan             logServiceArrayLocal.size();
2170fdd26906SClaire Weinan     };
2171fdd26906SClaire Weinan 
2172fdd26906SClaire Weinan     crow::connections::systemBus->async_method_call(
2173fdd26906SClaire Weinan         respHandler, "xyz.openbmc_project.ObjectMapper",
2174fdd26906SClaire Weinan         "/xyz/openbmc_project/object_mapper",
2175fdd26906SClaire Weinan         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2176fdd26906SClaire Weinan         "/xyz/openbmc_project/dump", 0,
2177fdd26906SClaire Weinan         std::array<const char*, 1>{dumpManagerIface});
2178fdd26906SClaire Weinan #endif
2179fdd26906SClaire Weinan }
2180fdd26906SClaire Weinan 
2181fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2182fdd26906SClaire Weinan {
2183fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2184fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2185fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2186dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2187e1f26343SJason M. Bills }
2188e1f26343SJason M. Bills 
21897e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2190e1f26343SJason M. Bills {
21917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2192ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
21937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
219445ca1b86SEd Tanous             [&app](const crow::Request& req,
219545ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
21963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
21977e860f15SJohn Edward Broadbent         {
219845ca1b86SEd Tanous             return;
219945ca1b86SEd Tanous         }
2200e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2201e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
22020f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
22030f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2204002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2205002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2206c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
2207e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
22087c8c4058STejas Patil 
22097c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
22102b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2211002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
22127c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
22137c8c4058STejas Patil             redfishDateTimeOffset.second;
22147c8c4058STejas Patil 
22151476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
22161476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
22177e860f15SJohn Edward Broadbent         });
2218e1f26343SJason M. Bills }
2219e1f26343SJason M. Bills 
22203a48b3a2SJason M. Bills static int
22213a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2222e1f26343SJason M. Bills                                sd_journal* journal,
22233a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2224e1f26343SJason M. Bills {
2225e1f26343SJason M. Bills     // Get the Log Entry contents
2226e1f26343SJason M. Bills     int ret = 0;
2227e1f26343SJason M. Bills 
2228a8fe54f0SJason M. Bills     std::string message;
2229a8fe54f0SJason M. Bills     std::string_view syslogID;
2230a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2231a8fe54f0SJason M. Bills     if (ret < 0)
2232a8fe54f0SJason M. Bills     {
2233a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2234a8fe54f0SJason M. Bills                          << strerror(-ret);
2235a8fe54f0SJason M. Bills     }
2236a8fe54f0SJason M. Bills     if (!syslogID.empty())
2237a8fe54f0SJason M. Bills     {
2238a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2239a8fe54f0SJason M. Bills     }
2240a8fe54f0SJason M. Bills 
224139e77504SEd Tanous     std::string_view msg;
224216428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2243e1f26343SJason M. Bills     if (ret < 0)
2244e1f26343SJason M. Bills     {
2245e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2246e1f26343SJason M. Bills         return 1;
2247e1f26343SJason M. Bills     }
2248a8fe54f0SJason M. Bills     message += std::string(msg);
2249e1f26343SJason M. Bills 
2250e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2251271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
225216428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2253e1f26343SJason M. Bills     if (ret < 0)
2254e1f26343SJason M. Bills     {
2255e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2256e1f26343SJason M. Bills     }
2257e1f26343SJason M. Bills 
2258e1f26343SJason M. Bills     // Get the Created time from the timestamp
225916428a1aSJason M. Bills     std::string entryTimeStr;
226016428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2261e1f26343SJason M. Bills     {
226216428a1aSJason M. Bills         return 1;
2263e1f26343SJason M. Bills     }
2264e1f26343SJason M. Bills 
2265e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
22669c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
226784afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.id"] =
226884afc48bSJason M. Bills         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
226984afc48bSJason M. Bills         bmcJournalLogEntryID;
227084afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
227184afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
227284afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
227384afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
227484afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2275738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
227684afc48bSJason M. Bills                                                          : "OK";
227784afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
227884afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2279e1f26343SJason M. Bills     return 0;
2280e1f26343SJason M. Bills }
2281e1f26343SJason M. Bills 
22827e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2283e1f26343SJason M. Bills {
22847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2285ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2286002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2287002d39b4SEd Tanous             [&app](const crow::Request& req,
2288002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2289c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2290c937d2bfSEd Tanous             .canDelegateTop = true,
2291c937d2bfSEd Tanous             .canDelegateSkip = true,
2292c937d2bfSEd Tanous         };
2293c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2294c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
22953ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2296193ad2faSJason M. Bills         {
2297193ad2faSJason M. Bills             return;
2298193ad2faSJason M. Bills         }
22993648c8beSEd Tanous 
23003648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
23015143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
23023648c8beSEd Tanous 
23037e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
23047e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2305e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2306e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
23070f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
23080f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2309e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2310e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2311e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
23120fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2313e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2314e1f26343SJason M. Bills 
23157e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
23167e860f15SJohn Edward Broadbent         // unique ID for each entry
2317e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2318e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2319e1f26343SJason M. Bills         if (ret < 0)
2320e1f26343SJason M. Bills         {
2321002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2322f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2323e1f26343SJason M. Bills             return;
2324e1f26343SJason M. Bills         }
23250fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
23260fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2327e1f26343SJason M. Bills         journalTmp = nullptr;
2328b01bf299SEd Tanous         uint64_t entryCount = 0;
2329e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2330e85d6b16SJason M. Bills         bool firstEntry = true;
2331e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2332e1f26343SJason M. Bills         {
2333193ad2faSJason M. Bills             entryCount++;
23347e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
23357e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
23363648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2337193ad2faSJason M. Bills             {
2338193ad2faSJason M. Bills                 continue;
2339193ad2faSJason M. Bills             }
2340193ad2faSJason M. Bills 
234116428a1aSJason M. Bills             std::string idStr;
2342e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2343e1f26343SJason M. Bills             {
2344e1f26343SJason M. Bills                 continue;
2345e1f26343SJason M. Bills             }
2346e85d6b16SJason M. Bills             firstEntry = false;
2347e85d6b16SJason M. Bills 
23483a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2349c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2350c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2351e1f26343SJason M. Bills             {
2352f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2353e1f26343SJason M. Bills                 return;
2354e1f26343SJason M. Bills             }
23553a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2356e1f26343SJason M. Bills         }
2357193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
23583648c8beSEd Tanous         if (skip + top < entryCount)
2359193ad2faSJason M. Bills         {
2360193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
23610fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
23623648c8beSEd Tanous                 std::to_string(skip + top);
2363193ad2faSJason M. Bills         }
23647e860f15SJohn Edward Broadbent         });
2365e1f26343SJason M. Bills }
2366e1f26343SJason M. Bills 
23677e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2368e1f26343SJason M. Bills {
23697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23707e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2371ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
23727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
237345ca1b86SEd Tanous             [&app](const crow::Request& req,
23747e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23757e860f15SJohn Edward Broadbent                    const std::string& entryID) {
23763ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
237745ca1b86SEd Tanous         {
237845ca1b86SEd Tanous             return;
237945ca1b86SEd Tanous         }
2380e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2381e1f26343SJason M. Bills         uint64_t ts = 0;
2382271584abSEd Tanous         uint64_t index = 0;
23838d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2384e1f26343SJason M. Bills         {
238516428a1aSJason M. Bills             return;
2386e1f26343SJason M. Bills         }
2387e1f26343SJason M. Bills 
2388e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2389e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2390e1f26343SJason M. Bills         if (ret < 0)
2391e1f26343SJason M. Bills         {
2392002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2393f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2394e1f26343SJason M. Bills             return;
2395e1f26343SJason M. Bills         }
2396002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2397002d39b4SEd Tanous             journalTmp, sd_journal_close);
2398e1f26343SJason M. Bills         journalTmp = nullptr;
23997e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
24007e860f15SJohn Edward Broadbent         // index tracking the unique ID
2401af07e3f5SJason M. Bills         std::string idStr;
2402af07e3f5SJason M. Bills         bool firstEntry = true;
2403e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
24042056b6d1SManojkiran Eda         if (ret < 0)
24052056b6d1SManojkiran Eda         {
24062056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
24072056b6d1SManojkiran Eda                              << strerror(-ret);
24082056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
24092056b6d1SManojkiran Eda             return;
24102056b6d1SManojkiran Eda         }
2411271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2412e1f26343SJason M. Bills         {
2413e1f26343SJason M. Bills             sd_journal_next(journal.get());
2414af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2415af07e3f5SJason M. Bills             {
2416af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2417af07e3f5SJason M. Bills                 return;
2418af07e3f5SJason M. Bills             }
2419af07e3f5SJason M. Bills             firstEntry = false;
2420af07e3f5SJason M. Bills         }
2421c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2422af07e3f5SJason M. Bills         if (idStr != entryID)
2423c4bf6374SJason M. Bills         {
2424ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2425c4bf6374SJason M. Bills             return;
2426c4bf6374SJason M. Bills         }
2427c4bf6374SJason M. Bills 
24283a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2429c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
24303a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2431e1f26343SJason M. Bills         {
2432f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2433e1f26343SJason M. Bills             return;
2434e1f26343SJason M. Bills         }
2435d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
24367e860f15SJohn Edward Broadbent         });
2437c9bb6861Sraviteja-b }
2438c9bb6861Sraviteja-b 
2439fdd26906SClaire Weinan inline void
2440fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2441fdd26906SClaire Weinan                        const std::string& dumpType)
2442c9bb6861Sraviteja-b {
2443fdd26906SClaire Weinan     std::string dumpPath;
2444fdd26906SClaire Weinan     std::string overWritePolicy;
2445fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2446fdd26906SClaire Weinan 
2447fdd26906SClaire Weinan     if (dumpType == "BMC")
244845ca1b86SEd Tanous     {
2449fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2450fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2451fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2452fdd26906SClaire Weinan     }
2453fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2454fdd26906SClaire Weinan     {
2455fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2456fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2457fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2458fdd26906SClaire Weinan     }
2459fdd26906SClaire Weinan     else if (dumpType == "System")
2460fdd26906SClaire Weinan     {
2461fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2462fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2463fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2464fdd26906SClaire Weinan     }
2465fdd26906SClaire Weinan     else
2466fdd26906SClaire Weinan     {
2467fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2468fdd26906SClaire Weinan                          << dumpType;
2469fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
247045ca1b86SEd Tanous         return;
247145ca1b86SEd Tanous     }
2472fdd26906SClaire Weinan 
2473fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2474fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2475c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2476fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2477fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2478fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
24797c8c4058STejas Patil 
24807c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
24812b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
24820fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
24837c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24847c8c4058STejas Patil         redfishDateTimeOffset.second;
24857c8c4058STejas Patil 
2486fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2487002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2488fdd26906SClaire Weinan         dumpPath + "/Actions/LogService.ClearLog";
2489fdd26906SClaire Weinan 
2490fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2491fdd26906SClaire Weinan     {
2492002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
24931476687dSEd Tanous                                 ["target"] =
2494fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2495fdd26906SClaire Weinan     }
2496c9bb6861Sraviteja-b }
2497c9bb6861Sraviteja-b 
2498fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2499fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2500fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25017e860f15SJohn Edward Broadbent {
25023ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
250345ca1b86SEd Tanous     {
250445ca1b86SEd Tanous         return;
250545ca1b86SEd Tanous     }
2506fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2507fdd26906SClaire Weinan }
2508c9bb6861Sraviteja-b 
2509*22d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
2510*22d268cbSEd Tanous     crow::App& app, const crow::Request& req,
2511*22d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2512*22d268cbSEd Tanous     const std::string& chassisId)
2513*22d268cbSEd Tanous {
2514*22d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2515*22d268cbSEd Tanous     {
2516*22d268cbSEd Tanous         return;
2517*22d268cbSEd Tanous     }
2518*22d268cbSEd Tanous     if (chassisId != "system")
2519*22d268cbSEd Tanous     {
2520*22d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2521*22d268cbSEd Tanous         return;
2522*22d268cbSEd Tanous     }
2523*22d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
2524*22d268cbSEd Tanous }
2525*22d268cbSEd Tanous 
2526fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2527fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2528fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2529fdd26906SClaire Weinan {
2530fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2531fdd26906SClaire Weinan     {
2532fdd26906SClaire Weinan         return;
2533fdd26906SClaire Weinan     }
2534fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2535fdd26906SClaire Weinan }
2536fdd26906SClaire Weinan 
2537*22d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
2538*22d268cbSEd Tanous     crow::App& app, const crow::Request& req,
2539*22d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2540*22d268cbSEd Tanous     const std::string& chassisId)
2541*22d268cbSEd Tanous {
2542*22d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2543*22d268cbSEd Tanous     {
2544*22d268cbSEd Tanous         return;
2545*22d268cbSEd Tanous     }
2546*22d268cbSEd Tanous     if (chassisId != "system")
2547*22d268cbSEd Tanous     {
2548*22d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2549*22d268cbSEd Tanous         return;
2550*22d268cbSEd Tanous     }
2551*22d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
2552*22d268cbSEd Tanous }
2553*22d268cbSEd Tanous 
2554fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2555fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2556fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2557fdd26906SClaire Weinan     const std::string& dumpId)
2558fdd26906SClaire Weinan {
2559fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2560fdd26906SClaire Weinan     {
2561fdd26906SClaire Weinan         return;
2562fdd26906SClaire Weinan     }
2563fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2564fdd26906SClaire Weinan }
2565*22d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
2566*22d268cbSEd Tanous     crow::App& app, const crow::Request& req,
2567*22d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2568*22d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
2569*22d268cbSEd Tanous {
2570*22d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2571*22d268cbSEd Tanous     {
2572*22d268cbSEd Tanous         return;
2573*22d268cbSEd Tanous     }
2574*22d268cbSEd Tanous     if (chassisId != "system")
2575*22d268cbSEd Tanous     {
2576*22d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2577*22d268cbSEd Tanous         return;
2578*22d268cbSEd Tanous     }
2579*22d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
2580*22d268cbSEd Tanous }
2581fdd26906SClaire Weinan 
2582fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2583fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2584fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2585fdd26906SClaire Weinan     const std::string& dumpId)
2586fdd26906SClaire Weinan {
2587fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2588fdd26906SClaire Weinan     {
2589fdd26906SClaire Weinan         return;
2590fdd26906SClaire Weinan     }
2591fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2592fdd26906SClaire Weinan }
2593fdd26906SClaire Weinan 
2594*22d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
2595*22d268cbSEd Tanous     crow::App& app, const crow::Request& req,
2596*22d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2597*22d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
2598*22d268cbSEd Tanous {
2599*22d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2600*22d268cbSEd Tanous     {
2601*22d268cbSEd Tanous         return;
2602*22d268cbSEd Tanous     }
2603*22d268cbSEd Tanous     if (chassisId != "system")
2604*22d268cbSEd Tanous     {
2605*22d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2606*22d268cbSEd Tanous         return;
2607*22d268cbSEd Tanous     }
2608*22d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
2609*22d268cbSEd Tanous }
2610*22d268cbSEd Tanous 
2611fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2612fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2613fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2614fdd26906SClaire Weinan {
2615fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2616fdd26906SClaire Weinan     {
2617fdd26906SClaire Weinan         return;
2618fdd26906SClaire Weinan     }
2619fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2620fdd26906SClaire Weinan }
2621fdd26906SClaire Weinan 
2622*22d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
2623*22d268cbSEd Tanous     crow::App& app, const crow::Request& req,
2624*22d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2625*22d268cbSEd Tanous     const std::string& chassisId)
2626*22d268cbSEd Tanous {
2627*22d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2628*22d268cbSEd Tanous     {
2629*22d268cbSEd Tanous         return;
2630*22d268cbSEd Tanous     }
2631*22d268cbSEd Tanous     if (chassisId != "system")
2632*22d268cbSEd Tanous     {
2633*22d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2634*22d268cbSEd Tanous         return;
2635*22d268cbSEd Tanous     }
2636*22d268cbSEd Tanous     createDump(asyncResp, req, "System");
2637*22d268cbSEd Tanous }
2638*22d268cbSEd Tanous 
2639fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2640fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2641fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2642fdd26906SClaire Weinan {
2643fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2644fdd26906SClaire Weinan     {
2645fdd26906SClaire Weinan         return;
2646fdd26906SClaire Weinan     }
2647fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2648fdd26906SClaire Weinan }
2649fdd26906SClaire Weinan 
2650*22d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
2651*22d268cbSEd Tanous     crow::App& app, const crow::Request& req,
2652*22d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2653*22d268cbSEd Tanous     const std::string& chassisId)
2654*22d268cbSEd Tanous {
2655*22d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2656*22d268cbSEd Tanous     {
2657*22d268cbSEd Tanous         return;
2658*22d268cbSEd Tanous     }
2659*22d268cbSEd Tanous     if (chassisId != "system")
2660*22d268cbSEd Tanous     {
2661*22d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
2662*22d268cbSEd Tanous         return;
2663*22d268cbSEd Tanous     }
2664*22d268cbSEd Tanous     clearDump(asyncResp, "System");
2665*22d268cbSEd Tanous }
2666*22d268cbSEd Tanous 
2667fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2668fdd26906SClaire Weinan {
2669fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2670fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2671fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2672fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2673fdd26906SClaire Weinan }
2674fdd26906SClaire Weinan 
2675fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2676fdd26906SClaire Weinan {
2677fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2678fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2679fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2680fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2681c9bb6861Sraviteja-b }
2682c9bb6861Sraviteja-b 
26837e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2684c9bb6861Sraviteja-b {
26857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
26867e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2687ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2688fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2689fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2690fdd26906SClaire Weinan 
26917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
26927e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2693ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2694fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2695fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2696c9bb6861Sraviteja-b }
2697c9bb6861Sraviteja-b 
26987e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2699c9bb6861Sraviteja-b {
27000fda0f12SGeorge Liu     BMCWEB_ROUTE(
27010fda0f12SGeorge Liu         app,
27020fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2703ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
27047e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2705fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2706fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2707a43be80fSAsmitha Karunanithi }
2708a43be80fSAsmitha Karunanithi 
27097e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
271080319af1SAsmitha Karunanithi {
27110fda0f12SGeorge Liu     BMCWEB_ROUTE(
27120fda0f12SGeorge Liu         app,
27130fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2714ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2715fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2716fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
271745ca1b86SEd Tanous }
2718fdd26906SClaire Weinan 
2719fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2720fdd26906SClaire Weinan {
2721fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2722fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2723fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2724fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2725fdd26906SClaire Weinan }
2726fdd26906SClaire Weinan 
2727fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2728fdd26906SClaire Weinan {
2729fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2730fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2731fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2732fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2733fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2734fdd26906SClaire Weinan }
2735fdd26906SClaire Weinan 
2736fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2737fdd26906SClaire Weinan {
2738fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2739fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2740fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2741fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2742fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2743fdd26906SClaire Weinan 
2744fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2745fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2746fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2747fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2748fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2749fdd26906SClaire Weinan }
2750fdd26906SClaire Weinan 
2751fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2752fdd26906SClaire Weinan {
2753fdd26906SClaire Weinan     BMCWEB_ROUTE(
2754fdd26906SClaire Weinan         app,
2755fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2756fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2757fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2758fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
27595cb1dd27SAsmitha Karunanithi }
27605cb1dd27SAsmitha Karunanithi 
27617e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
27625cb1dd27SAsmitha Karunanithi {
2763*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2764ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
27656ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2766*22d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
27675cb1dd27SAsmitha Karunanithi }
27685cb1dd27SAsmitha Karunanithi 
27697e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
27707e860f15SJohn Edward Broadbent {
2771*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2772ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2773*22d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
2774*22d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
2775*22d268cbSEd Tanous             std::ref(app)));
27765cb1dd27SAsmitha Karunanithi }
27775cb1dd27SAsmitha Karunanithi 
27787e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
27795cb1dd27SAsmitha Karunanithi {
27807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2781*22d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2782ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
27836ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2784*22d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
27858d1b46d7Szhanghch05 
27867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2787*22d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2788ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
27896ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2790*22d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
27915cb1dd27SAsmitha Karunanithi }
2792c9bb6861Sraviteja-b 
27937e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2794c9bb6861Sraviteja-b {
27950fda0f12SGeorge Liu     BMCWEB_ROUTE(
27960fda0f12SGeorge Liu         app,
2797*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2798ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2799*22d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
2800*22d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
2801*22d268cbSEd Tanous             std::ref(app)));
2802a43be80fSAsmitha Karunanithi }
2803a43be80fSAsmitha Karunanithi 
28047e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2805a43be80fSAsmitha Karunanithi {
28060fda0f12SGeorge Liu     BMCWEB_ROUTE(
28070fda0f12SGeorge Liu         app,
2808*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2809ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
28106ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2811*22d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2812013487e5Sraviteja-b }
2813013487e5Sraviteja-b 
28147e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
28151da66f75SEd Tanous {
28163946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28173946028dSAppaRao Puli     // method for security reasons.
28181da66f75SEd Tanous     /**
28191da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
28201da66f75SEd Tanous      */
2821*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2822ed398213SEd Tanous         // This is incorrect, should be:
2823ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2824432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2825002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2826002d39b4SEd Tanous             [&app](const crow::Request& req,
2827*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2828*22d268cbSEd Tanous                    const std::string& systemName) {
28293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
283045ca1b86SEd Tanous         {
283145ca1b86SEd Tanous             return;
283245ca1b86SEd Tanous         }
2833*22d268cbSEd Tanous         if (systemName != "system")
2834*22d268cbSEd Tanous         {
2835*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2836*22d268cbSEd Tanous                                        systemName);
2837*22d268cbSEd Tanous             return;
2838*22d268cbSEd Tanous         }
2839*22d268cbSEd Tanous 
28407e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
28417e860f15SJohn Edward Broadbent         // SubRoute
28420f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2843424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2844e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
28458e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
28464f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
28474f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
28484f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2849e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2850e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
28517c8c4058STejas Patil 
28527c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
28532b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
28547c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
28557c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
28567c8c4058STejas Patil             redfishDateTimeOffset.second;
28577c8c4058STejas Patil 
28581476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
28591476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2860002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
28611476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2862002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
28631476687dSEd Tanous                                 ["target"] =
28641476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
28657e860f15SJohn Edward Broadbent         });
28661da66f75SEd Tanous }
28671da66f75SEd Tanous 
28687e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
28695b61b5e8SJason M. Bills {
28700fda0f12SGeorge Liu     BMCWEB_ROUTE(
28710fda0f12SGeorge Liu         app,
2872*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
2873ed398213SEd Tanous         // This is incorrect, should be:
2874ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2875432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
287745ca1b86SEd Tanous             [&app](const crow::Request& req,
2878*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2879*22d268cbSEd Tanous                    const std::string& systemName) {
28803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
288145ca1b86SEd Tanous         {
288245ca1b86SEd Tanous             return;
288345ca1b86SEd Tanous         }
2884*22d268cbSEd Tanous         if (systemName != "system")
2885*22d268cbSEd Tanous         {
2886*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2887*22d268cbSEd Tanous                                        systemName);
2888*22d268cbSEd Tanous             return;
2889*22d268cbSEd Tanous         }
28905b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
28915b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
2892cb13a392SEd Tanous                         const std::string&) {
28935b61b5e8SJason M. Bills             if (ec)
28945b61b5e8SJason M. Bills             {
28955b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
28965b61b5e8SJason M. Bills                 return;
28975b61b5e8SJason M. Bills             }
28985b61b5e8SJason M. Bills             messages::success(asyncResp->res);
28995b61b5e8SJason M. Bills             },
2900002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
29017e860f15SJohn Edward Broadbent         });
29025b61b5e8SJason M. Bills }
29035b61b5e8SJason M. Bills 
29048d1b46d7Szhanghch05 static void
29058d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29068d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2907e855dd28SJason M. Bills {
2908043a0536SJohnathan Mantey     auto getStoredLogCallback =
2909b9d36b47SEd Tanous         [asyncResp, logID,
2910b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2911b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2912e855dd28SJason M. Bills         if (ec)
2913e855dd28SJason M. Bills         {
2914e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
29151ddcf01aSJason M. Bills             if (ec.value() ==
29161ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
29171ddcf01aSJason M. Bills             {
2918002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
29191ddcf01aSJason M. Bills             }
29201ddcf01aSJason M. Bills             else
29211ddcf01aSJason M. Bills             {
2922e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
29231ddcf01aSJason M. Bills             }
2924e855dd28SJason M. Bills             return;
2925e855dd28SJason M. Bills         }
2926043a0536SJohnathan Mantey 
2927043a0536SJohnathan Mantey         std::string timestamp{};
2928043a0536SJohnathan Mantey         std::string filename{};
2929043a0536SJohnathan Mantey         std::string logfile{};
29302c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
2931043a0536SJohnathan Mantey 
2932043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
2933e855dd28SJason M. Bills         {
2934002d39b4SEd Tanous             messages::resourceMissingAtURI(asyncResp->res,
2935002d39b4SEd Tanous                                            crow::utility::urlFromPieces(logID));
2936e855dd28SJason M. Bills             return;
2937e855dd28SJason M. Bills         }
2938e855dd28SJason M. Bills 
2939043a0536SJohnathan Mantey         std::string crashdumpURI =
2940e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2941043a0536SJohnathan Mantey             logID + "/" + filename;
294284afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
29439c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
294484afc48bSJason M. Bills         logEntry["@odata.id"] =
294584afc48bSJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
294684afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
294784afc48bSJason M. Bills         logEntry["Id"] = logID;
294884afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
294984afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
295084afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
295184afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
295284afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
29532b20ef6eSJason M. Bills 
29542b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
29552b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
29562b20ef6eSJason M. Bills         // directly
29572b20ef6eSJason M. Bills         if (logEntryJson.is_array())
29582b20ef6eSJason M. Bills         {
29592b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
29602b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
29612b20ef6eSJason M. Bills                 logEntryJson.size();
29622b20ef6eSJason M. Bills         }
29632b20ef6eSJason M. Bills         else
29642b20ef6eSJason M. Bills         {
2965d405bb51SJason M. Bills             logEntryJson.update(logEntry);
29662b20ef6eSJason M. Bills         }
2967e855dd28SJason M. Bills     };
2968d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
2969d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
2970d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
2971d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
2972e855dd28SJason M. Bills }
2973e855dd28SJason M. Bills 
29747e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
29751da66f75SEd Tanous {
29763946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29773946028dSAppaRao Puli     // method for security reasons.
29781da66f75SEd Tanous     /**
29791da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
29801da66f75SEd Tanous      */
29817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2982*22d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
2983ed398213SEd Tanous         // This is incorrect, should be.
2984ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2985432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2986002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2987002d39b4SEd Tanous             [&app](const crow::Request& req,
2988*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2989*22d268cbSEd Tanous                    const std::string& systemName) {
29903ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
299145ca1b86SEd Tanous         {
299245ca1b86SEd Tanous             return;
299345ca1b86SEd Tanous         }
2994*22d268cbSEd Tanous         if (systemName != "system")
2995*22d268cbSEd Tanous         {
2996*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
2997*22d268cbSEd Tanous                                        systemName);
2998*22d268cbSEd Tanous             return;
2999*22d268cbSEd Tanous         }
3000*22d268cbSEd Tanous 
30012b20ef6eSJason M. Bills         crow::connections::systemBus->async_method_call(
30022b20ef6eSJason M. Bills             [asyncResp](const boost::system::error_code ec,
30032b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
30041da66f75SEd Tanous             if (ec)
30051da66f75SEd Tanous             {
30061da66f75SEd Tanous                 if (ec.value() !=
30071da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
30081da66f75SEd Tanous                 {
30091da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
30101da66f75SEd Tanous                                      << ec.message();
3011f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
30121da66f75SEd Tanous                     return;
30131da66f75SEd Tanous                 }
30141da66f75SEd Tanous             }
3015e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
30161da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
30170f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3018424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3019002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3020e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3021424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3022002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3023a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
30242b20ef6eSJason M. Bills 
30252b20ef6eSJason M. Bills             for (const std::string& path : resp)
30261da66f75SEd Tanous             {
30272b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3028e855dd28SJason M. Bills                 // Get the log ID
30292b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
30302b20ef6eSJason M. Bills                 if (logID.empty())
30311da66f75SEd Tanous                 {
3032e855dd28SJason M. Bills                     continue;
30331da66f75SEd Tanous                 }
3034e855dd28SJason M. Bills                 // Add the log entry to the array
30352b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
30362b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
30371da66f75SEd Tanous             }
30382b20ef6eSJason M. Bills             },
30391da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
30401da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
30411da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
30425b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
30437e860f15SJohn Edward Broadbent         });
30441da66f75SEd Tanous }
30451da66f75SEd Tanous 
30467e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
30471da66f75SEd Tanous {
30483946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
30493946028dSAppaRao Puli     // method for security reasons.
30501da66f75SEd Tanous 
30517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3052*22d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3053ed398213SEd Tanous         // this is incorrect, should be
3054ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3055432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30567e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
305745ca1b86SEd Tanous             [&app](const crow::Request& req,
30587e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3059*22d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
30603ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
306145ca1b86SEd Tanous         {
306245ca1b86SEd Tanous             return;
306345ca1b86SEd Tanous         }
3064*22d268cbSEd Tanous         if (systemName != "system")
3065*22d268cbSEd Tanous         {
3066*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3067*22d268cbSEd Tanous                                        systemName);
3068*22d268cbSEd Tanous             ;
3069*22d268cbSEd Tanous             return;
3070*22d268cbSEd Tanous         }
30717e860f15SJohn Edward Broadbent         const std::string& logID = param;
3072e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
30737e860f15SJohn Edward Broadbent         });
3074e855dd28SJason M. Bills }
3075e855dd28SJason M. Bills 
30767e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3077e855dd28SJason M. Bills {
30783946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
30793946028dSAppaRao Puli     // method for security reasons.
30807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
30817e860f15SJohn Edward Broadbent         app,
3082*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3083ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
30847e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3085a4ce114aSNan Zhou             [](const crow::Request& req,
30867e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3087*22d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
3088*22d268cbSEd Tanous                const std::string& fileName) {
30892a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
30902a9beeedSShounak Mitra         // Redfish resource.
3091*22d268cbSEd Tanous 
3092*22d268cbSEd Tanous         if (systemName != "system")
3093*22d268cbSEd Tanous         {
3094*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3095*22d268cbSEd Tanous                                        systemName);
3096*22d268cbSEd Tanous             ;
3097*22d268cbSEd Tanous             return;
3098*22d268cbSEd Tanous         }
3099*22d268cbSEd Tanous 
3100043a0536SJohnathan Mantey         auto getStoredLogCallback =
3101002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3102abf2add6SEd Tanous                 const boost::system::error_code ec,
3103002d39b4SEd Tanous                 const std::vector<
3104002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
31057e860f15SJohn Edward Broadbent                     resp) {
31061da66f75SEd Tanous             if (ec)
31071da66f75SEd Tanous             {
3108002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3109f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
31101da66f75SEd Tanous                 return;
31111da66f75SEd Tanous             }
3112e855dd28SJason M. Bills 
3113043a0536SJohnathan Mantey             std::string dbusFilename{};
3114043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3115043a0536SJohnathan Mantey             std::string dbusFilepath{};
3116043a0536SJohnathan Mantey 
3117002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3118002d39b4SEd Tanous                                      dbusFilepath);
3119043a0536SJohnathan Mantey 
3120043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3121043a0536SJohnathan Mantey                 dbusFilepath.empty())
31221da66f75SEd Tanous             {
3123ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
31241da66f75SEd Tanous                 return;
31251da66f75SEd Tanous             }
3126e855dd28SJason M. Bills 
3127043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3128043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3129043a0536SJohnathan Mantey             {
3130ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
3131043a0536SJohnathan Mantey                 return;
3132043a0536SJohnathan Mantey             }
3133043a0536SJohnathan Mantey 
3134043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3135043a0536SJohnathan Mantey             {
3136ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
3137043a0536SJohnathan Mantey                 return;
3138043a0536SJohnathan Mantey             }
3139002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3140002d39b4SEd Tanous             asyncResp->res.body() =
3141002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3142043a0536SJohnathan Mantey 
31437e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
31447e860f15SJohn Edward Broadbent             // from a browser
3145d9f6c621SEd Tanous             asyncResp->res.addHeader(
3146d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
31471da66f75SEd Tanous         };
3148d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3149d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3150d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3151d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
31527e860f15SJohn Edward Broadbent         });
31531da66f75SEd Tanous }
31541da66f75SEd Tanous 
3155c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3156c5a4c82aSJason M. Bills {
3157c5a4c82aSJason M. Bills     onDemand,
3158c5a4c82aSJason M. Bills     telemetry,
3159c5a4c82aSJason M. Bills     invalid,
3160c5a4c82aSJason M. Bills };
3161c5a4c82aSJason M. Bills 
3162f7725d79SEd Tanous inline OEMDiagnosticType
3163f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
3164c5a4c82aSJason M. Bills {
3165c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3166c5a4c82aSJason M. Bills     {
3167c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3168c5a4c82aSJason M. Bills     }
3169c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3170c5a4c82aSJason M. Bills     {
3171c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3172c5a4c82aSJason M. Bills     }
3173c5a4c82aSJason M. Bills 
3174c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3175c5a4c82aSJason M. Bills }
3176c5a4c82aSJason M. Bills 
31777e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
31781da66f75SEd Tanous {
31793946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31803946028dSAppaRao Puli     // method for security reasons.
31810fda0f12SGeorge Liu     BMCWEB_ROUTE(
31820fda0f12SGeorge Liu         app,
3183*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3184ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3185ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3186432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3187002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3188002d39b4SEd Tanous             [&app](const crow::Request& req,
3189*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3190*22d268cbSEd Tanous                    const std::string& systemName) {
31913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
319245ca1b86SEd Tanous         {
319345ca1b86SEd Tanous             return;
319445ca1b86SEd Tanous         }
3195*22d268cbSEd Tanous 
3196*22d268cbSEd Tanous         if (systemName != "system")
3197*22d268cbSEd Tanous         {
3198*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3199*22d268cbSEd Tanous                                        systemName);
3200*22d268cbSEd Tanous             ;
3201*22d268cbSEd Tanous             return;
3202*22d268cbSEd Tanous         }
3203*22d268cbSEd Tanous 
32048e6c099aSJason M. Bills         std::string diagnosticDataType;
32058e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
320615ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3207002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3208002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
32098e6c099aSJason M. Bills         {
32108e6c099aSJason M. Bills             return;
32118e6c099aSJason M. Bills         }
32128e6c099aSJason M. Bills 
32138e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
32148e6c099aSJason M. Bills         {
32158e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
32168e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
32178e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
32188e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
32198e6c099aSJason M. Bills                 "CollectDiagnosticData");
32208e6c099aSJason M. Bills             return;
32218e6c099aSJason M. Bills         }
32228e6c099aSJason M. Bills 
3223c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3224c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3225c5a4c82aSJason M. Bills 
3226c5a4c82aSJason M. Bills         std::string iface;
3227c5a4c82aSJason M. Bills         std::string method;
3228c5a4c82aSJason M. Bills         std::string taskMatchStr;
3229c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3230c5a4c82aSJason M. Bills         {
3231c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3232c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3233c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3234c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3235c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3236c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3237c5a4c82aSJason M. Bills         }
3238c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3239c5a4c82aSJason M. Bills         {
3240c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3241c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3242c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3243c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3244c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3245c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3246c5a4c82aSJason M. Bills         }
3247c5a4c82aSJason M. Bills         else
3248c5a4c82aSJason M. Bills         {
3249c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3250c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3251c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3252002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3253002d39b4SEd Tanous                 "CollectDiagnosticData");
3254c5a4c82aSJason M. Bills             return;
3255c5a4c82aSJason M. Bills         }
3256c5a4c82aSJason M. Bills 
3257c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3258c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3259c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
326098be3e39SEd Tanous                            const std::string&) mutable {
32611da66f75SEd Tanous             if (ec)
32621da66f75SEd Tanous             {
3263002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
32641da66f75SEd Tanous                 {
3265f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
32661da66f75SEd Tanous                 }
32674363d3b2SJason M. Bills                 else if (ec.value() ==
32684363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
32694363d3b2SJason M. Bills                 {
3270002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3271002d39b4SEd Tanous                                                             "60");
32724363d3b2SJason M. Bills                 }
32731da66f75SEd Tanous                 else
32741da66f75SEd Tanous                 {
3275f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
32761da66f75SEd Tanous                 }
32771da66f75SEd Tanous                 return;
32781da66f75SEd Tanous             }
3279002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
328059d494eeSPatrick Williams                 [](boost::system::error_code err, sdbusplus::message_t&,
3281002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
328266afe4faSJames Feist                 if (!err)
328366afe4faSJames Feist                 {
3284002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3285e5d5006bSJames Feist                         std::to_string(taskData->index)));
3286831d6b09SJames Feist                     taskData->state = "Completed";
328766afe4faSJames Feist                 }
328832898ceaSJames Feist                 return task::completed;
328966afe4faSJames Feist                 },
3290c5a4c82aSJason M. Bills                 taskMatchStr);
3291c5a4c82aSJason M. Bills 
329246229577SJames Feist             task->startTimer(std::chrono::minutes(5));
329346229577SJames Feist             task->populateResp(asyncResp->res);
329498be3e39SEd Tanous             task->payload.emplace(std::move(payload));
32951da66f75SEd Tanous         };
32968e6c099aSJason M. Bills 
32971da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3298002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3299002d39b4SEd Tanous             iface, method);
33007e860f15SJohn Edward Broadbent         });
33016eda7685SKenny L. Ku }
33026eda7685SKenny L. Ku 
3303cb92c03bSAndrew Geissler /**
3304cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3305cb92c03bSAndrew Geissler  */
33067e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3307cb92c03bSAndrew Geissler {
3308cb92c03bSAndrew Geissler     /**
3309cb92c03bSAndrew Geissler      * Function handles POST method request.
3310cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3311cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3312cb92c03bSAndrew Geissler      */
33137e860f15SJohn Edward Broadbent 
33140fda0f12SGeorge Liu     BMCWEB_ROUTE(
33150fda0f12SGeorge Liu         app,
3316*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3317ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
33187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
331945ca1b86SEd Tanous             [&app](const crow::Request& req,
3320*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3321*22d268cbSEd Tanous                    const std::string& systemName) {
33223ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
332345ca1b86SEd Tanous         {
332445ca1b86SEd Tanous             return;
332545ca1b86SEd Tanous         }
3326*22d268cbSEd Tanous         if (systemName != "system")
3327*22d268cbSEd Tanous         {
3328*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3329*22d268cbSEd Tanous                                        systemName);
3330*22d268cbSEd Tanous             ;
3331*22d268cbSEd Tanous             return;
3332*22d268cbSEd Tanous         }
3333cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3334cb92c03bSAndrew Geissler 
3335cb92c03bSAndrew Geissler         // Process response from Logging service.
3336002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3337002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3338cb92c03bSAndrew Geissler             if (ec)
3339cb92c03bSAndrew Geissler             {
3340cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3341002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3342cb92c03bSAndrew Geissler                 asyncResp->res.result(
3343cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3344cb92c03bSAndrew Geissler                 return;
3345cb92c03bSAndrew Geissler             }
3346cb92c03bSAndrew Geissler 
3347002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3348cb92c03bSAndrew Geissler         };
3349cb92c03bSAndrew Geissler 
3350cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3351cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
33522c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3353cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3354cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
33557e860f15SJohn Edward Broadbent         });
3356cb92c03bSAndrew Geissler }
3357a3316fc6SZhikuiRen 
3358a3316fc6SZhikuiRen /****************************************************
3359a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3360a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3361a3316fc6SZhikuiRen  ******************************************************/
33627e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3363a3316fc6SZhikuiRen {
3364*22d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3365ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3366002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3367002d39b4SEd Tanous             [&app](const crow::Request& req,
3368*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3369*22d268cbSEd Tanous                    const std::string& systemName) {
33703ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
337145ca1b86SEd Tanous         {
337245ca1b86SEd Tanous             return;
337345ca1b86SEd Tanous         }
3374*22d268cbSEd Tanous         if (systemName != "system")
3375*22d268cbSEd Tanous         {
3376*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3377*22d268cbSEd Tanous                                        systemName);
3378*22d268cbSEd Tanous             ;
3379*22d268cbSEd Tanous             return;
3380*22d268cbSEd Tanous         }
33811476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
33821476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
33831476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
33841476687dSEd Tanous             "#LogService.v1_1_0.LogService";
33851476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
33861476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
33871476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
33881476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
33891476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
33901476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
33917c8c4058STejas Patil 
33927c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
33932b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
33940fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
33957c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
33967c8c4058STejas Patil             redfishDateTimeOffset.second;
33977c8c4058STejas Patil 
3398a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
33997e860f15SJohn Edward Broadbent             {"target",
34000fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
34017e860f15SJohn Edward Broadbent         });
3402a3316fc6SZhikuiRen }
3403a3316fc6SZhikuiRen 
34047e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3405a3316fc6SZhikuiRen {
34060fda0f12SGeorge Liu     BMCWEB_ROUTE(
34070fda0f12SGeorge Liu         app,
3408*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3409ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3410ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3411432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
34127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
341345ca1b86SEd Tanous             [&app](const crow::Request& req,
3414*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3415*22d268cbSEd Tanous                    const std::string& systemName) {
34163ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
341745ca1b86SEd Tanous         {
341845ca1b86SEd Tanous             return;
341945ca1b86SEd Tanous         }
3420*22d268cbSEd Tanous         if (systemName != "system")
3421*22d268cbSEd Tanous         {
3422*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3423*22d268cbSEd Tanous                                        systemName);
3424*22d268cbSEd Tanous             ;
3425*22d268cbSEd Tanous             return;
3426*22d268cbSEd Tanous         }
3427a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3428a3316fc6SZhikuiRen 
3429a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3430a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3431a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3432a3316fc6SZhikuiRen             if (ec)
3433a3316fc6SZhikuiRen             {
3434a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3435002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
34367e860f15SJohn Edward Broadbent                                  << ec;
3437002d39b4SEd Tanous                 asyncResp->res.result(
3438002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3439a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3440a3316fc6SZhikuiRen                 return;
3441a3316fc6SZhikuiRen             }
3442a3316fc6SZhikuiRen             },
344315124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
344415124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3445a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
34467e860f15SJohn Edward Broadbent         });
3447a3316fc6SZhikuiRen }
3448a3316fc6SZhikuiRen 
3449a3316fc6SZhikuiRen static void fillPostCodeEntry(
34508d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
34516c9a279eSManojkiran Eda     const boost::container::flat_map<
34526c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3453a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3454a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3455a3316fc6SZhikuiRen {
3456a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3457fffb8c1fSEd Tanous     const registries::Message* message =
3458fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3459a3316fc6SZhikuiRen 
3460a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3461a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3462a3316fc6SZhikuiRen 
3463a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
34646c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
34656c9a279eSManojkiran Eda              code : postcode)
3466a3316fc6SZhikuiRen     {
3467a3316fc6SZhikuiRen         currentCodeIndex++;
3468a3316fc6SZhikuiRen         std::string postcodeEntryID =
3469a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3470a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3471a3316fc6SZhikuiRen 
3472a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3473a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3474a3316fc6SZhikuiRen 
3475a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3476a3316fc6SZhikuiRen         { // already incremented
3477a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3478a3316fc6SZhikuiRen         }
3479a3316fc6SZhikuiRen         else
3480a3316fc6SZhikuiRen         {
3481a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3482a3316fc6SZhikuiRen         }
3483a3316fc6SZhikuiRen 
3484a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3485a3316fc6SZhikuiRen         // not fall between top and skip
3486a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3487a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3488a3316fc6SZhikuiRen         {
3489a3316fc6SZhikuiRen             continue;
3490a3316fc6SZhikuiRen         }
3491a3316fc6SZhikuiRen 
34924e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3493a3316fc6SZhikuiRen         // currentIndex
3494a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3495a3316fc6SZhikuiRen         {
3496a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3497a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3498a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3499a3316fc6SZhikuiRen             continue;
3500a3316fc6SZhikuiRen         }
3501a3316fc6SZhikuiRen 
3502a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3503a3316fc6SZhikuiRen         // index
3504a3316fc6SZhikuiRen 
3505a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3506a3316fc6SZhikuiRen         std::string entryTimeStr;
35071d8782e7SNan Zhou         entryTimeStr =
35082b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3509a3316fc6SZhikuiRen 
3510a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3511a3316fc6SZhikuiRen         std::ostringstream hexCode;
3512a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
35136c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3514a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3515a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3516a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3517a3316fc6SZhikuiRen         // Set precision to 4 digits
3518a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3519a3316fc6SZhikuiRen         // Add double to stream
3520a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3521a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3522a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3523a3316fc6SZhikuiRen 
3524a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3525a3316fc6SZhikuiRen         std::string msg;
3526a3316fc6SZhikuiRen         if (message != nullptr)
3527a3316fc6SZhikuiRen         {
3528a3316fc6SZhikuiRen             msg = message->message;
3529a3316fc6SZhikuiRen 
3530a3316fc6SZhikuiRen             // fill in this post code value
3531a3316fc6SZhikuiRen             int i = 0;
3532a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3533a3316fc6SZhikuiRen             {
3534a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3535a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3536a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3537a3316fc6SZhikuiRen                 {
3538a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3539a3316fc6SZhikuiRen                 }
3540a3316fc6SZhikuiRen             }
3541a3316fc6SZhikuiRen         }
3542a3316fc6SZhikuiRen 
3543d4342a92STim Lee         // Get Severity template from message registry
3544d4342a92STim Lee         std::string severity;
3545d4342a92STim Lee         if (message != nullptr)
3546d4342a92STim Lee         {
35475f2b84eeSEd Tanous             severity = message->messageSeverity;
3548d4342a92STim Lee         }
3549d4342a92STim Lee 
3550a3316fc6SZhikuiRen         // add to AsyncResp
3551a3316fc6SZhikuiRen         logEntryArray.push_back({});
3552a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
35539c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
355484afc48bSJason M. Bills         bmcLogEntry["@odata.id"] =
35550fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
355684afc48bSJason M. Bills             postcodeEntryID;
355784afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
355884afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
355984afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
356084afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
356184afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
356284afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
356384afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
356484afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3565647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3566647b3cdcSGeorge Liu         {
3567647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3568647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3569647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3570647b3cdcSGeorge Liu         }
3571a3316fc6SZhikuiRen     }
3572a3316fc6SZhikuiRen }
3573a3316fc6SZhikuiRen 
35748d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3575a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3576a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3577a3316fc6SZhikuiRen {
3578a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
35796c9a279eSManojkiran Eda         [aResp, bootIndex,
35806c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
35816c9a279eSManojkiran Eda                     const boost::container::flat_map<
35826c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
35836c9a279eSManojkiran Eda                         postcode) {
3584a3316fc6SZhikuiRen         if (ec)
3585a3316fc6SZhikuiRen         {
3586a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3587a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3588a3316fc6SZhikuiRen             return;
3589a3316fc6SZhikuiRen         }
3590a3316fc6SZhikuiRen 
3591a3316fc6SZhikuiRen         // skip the empty postcode boots
3592a3316fc6SZhikuiRen         if (postcode.empty())
3593a3316fc6SZhikuiRen         {
3594a3316fc6SZhikuiRen             return;
3595a3316fc6SZhikuiRen         }
3596a3316fc6SZhikuiRen 
3597a3316fc6SZhikuiRen         fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3598a3316fc6SZhikuiRen 
3599a3316fc6SZhikuiRen         aResp->res.jsonValue["Members@odata.count"] =
3600a3316fc6SZhikuiRen             aResp->res.jsonValue["Members"].size();
3601a3316fc6SZhikuiRen         },
360215124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
360315124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3604a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3605a3316fc6SZhikuiRen         bootIndex);
3606a3316fc6SZhikuiRen }
3607a3316fc6SZhikuiRen 
36088d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3609a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3610a3316fc6SZhikuiRen                                const uint16_t bootCount,
36113648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
36123648c8beSEd Tanous                                size_t top)
3613a3316fc6SZhikuiRen {
3614a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3615a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3616a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
36176c9a279eSManojkiran Eda               const boost::container::flat_map<
36186c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
36196c9a279eSManojkiran Eda                   postcode) {
3620a3316fc6SZhikuiRen         if (ec)
3621a3316fc6SZhikuiRen         {
3622a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3623a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3624a3316fc6SZhikuiRen             return;
3625a3316fc6SZhikuiRen         }
3626a3316fc6SZhikuiRen 
3627a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3628a3316fc6SZhikuiRen         if (!postcode.empty())
3629a3316fc6SZhikuiRen         {
3630a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
36313648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3632a3316fc6SZhikuiRen             {
36333648c8beSEd Tanous                 uint64_t thisBootSkip =
36343648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
36353648c8beSEd Tanous                     entryCount;
3636a3316fc6SZhikuiRen                 uint64_t thisBootTop =
36373648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
36383648c8beSEd Tanous                     entryCount;
3639a3316fc6SZhikuiRen 
3640002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3641002d39b4SEd Tanous                                   thisBootTop);
3642a3316fc6SZhikuiRen             }
3643a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3644a3316fc6SZhikuiRen         }
3645a3316fc6SZhikuiRen 
3646a3316fc6SZhikuiRen         // continue to previous bootIndex
3647a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3648a3316fc6SZhikuiRen         {
3649a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3650a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3651a3316fc6SZhikuiRen         }
365281584abeSJiaqing Zhao         else if (skip + top < endCount)
3653a3316fc6SZhikuiRen         {
3654a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
36550fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3656a3316fc6SZhikuiRen                 std::to_string(skip + top);
3657a3316fc6SZhikuiRen         }
3658a3316fc6SZhikuiRen         },
365915124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
366015124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3661a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3662a3316fc6SZhikuiRen         bootIndex);
3663a3316fc6SZhikuiRen }
3664a3316fc6SZhikuiRen 
36658d1b46d7Szhanghch05 static void
36668d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
36673648c8beSEd Tanous                          size_t skip, size_t top)
3668a3316fc6SZhikuiRen {
3669a3316fc6SZhikuiRen     uint64_t entryCount = 0;
36701e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
36711e1e598dSJonathan Doman         *crow::connections::systemBus,
36721e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
36731e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
36741e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
36751e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
36761e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3677a3316fc6SZhikuiRen         if (ec)
3678a3316fc6SZhikuiRen         {
3679a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3680a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3681a3316fc6SZhikuiRen             return;
3682a3316fc6SZhikuiRen         }
36831e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
36841e1e598dSJonathan Doman         });
3685a3316fc6SZhikuiRen }
3686a3316fc6SZhikuiRen 
36877e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3688a3316fc6SZhikuiRen {
36897e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
3690*22d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3691ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
36927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
369345ca1b86SEd Tanous             [&app](const crow::Request& req,
3694*22d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3695*22d268cbSEd Tanous                    const std::string& systemName) {
3696c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3697c937d2bfSEd Tanous             .canDelegateTop = true,
3698c937d2bfSEd Tanous             .canDelegateSkip = true,
3699c937d2bfSEd Tanous         };
3700c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3701c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
37023ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
370345ca1b86SEd Tanous         {
370445ca1b86SEd Tanous             return;
370545ca1b86SEd Tanous         }
3706*22d268cbSEd Tanous 
3707*22d268cbSEd Tanous         if (systemName != "system")
3708*22d268cbSEd Tanous         {
3709*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3710*22d268cbSEd Tanous                                        systemName);
3711*22d268cbSEd Tanous             ;
3712*22d268cbSEd Tanous             return;
3713*22d268cbSEd Tanous         }
3714a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3715a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3716a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3717a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3718a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3719a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3720a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3721a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3722a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
37233648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
37245143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
37253648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
37267e860f15SJohn Edward Broadbent         });
3727a3316fc6SZhikuiRen }
3728a3316fc6SZhikuiRen 
3729647b3cdcSGeorge Liu /**
3730647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3731647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3732647b3cdcSGeorge Liu  *
3733647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3734647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3735647b3cdcSGeorge Liu  * @param[out] index          Index value
3736647b3cdcSGeorge Liu  *
3737647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3738647b3cdcSGeorge Liu  */
3739647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3740647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3741647b3cdcSGeorge Liu {
3742647b3cdcSGeorge Liu     std::vector<std::string> split;
3743647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3744647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3745647b3cdcSGeorge Liu     {
3746647b3cdcSGeorge Liu         return false;
3747647b3cdcSGeorge Liu     }
3748647b3cdcSGeorge Liu 
3749ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3750647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3751ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3752647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3753647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3754647b3cdcSGeorge Liu 
3755647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3756647b3cdcSGeorge Liu     {
3757647b3cdcSGeorge Liu         return false;
3758647b3cdcSGeorge Liu     }
3759647b3cdcSGeorge Liu 
3760647b3cdcSGeorge Liu     start = split[1].data();
3761ca45aa3cSEd Tanous 
3762ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3763647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3764647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3765647b3cdcSGeorge Liu 
3766517d9a58STony Lee     return ptrValue == end && ecValue == std::errc();
3767647b3cdcSGeorge Liu }
3768647b3cdcSGeorge Liu 
3769647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3770647b3cdcSGeorge Liu {
37710fda0f12SGeorge Liu     BMCWEB_ROUTE(
37720fda0f12SGeorge Liu         app,
3773*22d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
3774647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3775647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
377645ca1b86SEd Tanous             [&app](const crow::Request& req,
3777647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3778*22d268cbSEd Tanous                    const std::string& systemName,
3779647b3cdcSGeorge Liu                    const std::string& postCodeID) {
37803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
378145ca1b86SEd Tanous         {
378245ca1b86SEd Tanous             return;
378345ca1b86SEd Tanous         }
378499351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
378599351cd8SEd Tanous                 req.getHeaderValue("Accept"),
37864a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
3787647b3cdcSGeorge Liu         {
3788002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3789647b3cdcSGeorge Liu             return;
3790647b3cdcSGeorge Liu         }
3791*22d268cbSEd Tanous         if (systemName != "system")
3792*22d268cbSEd Tanous         {
3793*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3794*22d268cbSEd Tanous                                        systemName);
3795*22d268cbSEd Tanous             ;
3796*22d268cbSEd Tanous             return;
3797*22d268cbSEd Tanous         }
3798647b3cdcSGeorge Liu 
3799647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3800647b3cdcSGeorge Liu         uint16_t index = 0;
3801647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3802647b3cdcSGeorge Liu         {
3803002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3804647b3cdcSGeorge Liu             return;
3805647b3cdcSGeorge Liu         }
3806647b3cdcSGeorge Liu 
3807647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3808647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3809647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3810002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3811002d39b4SEd Tanous                     postcodes) {
3812647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3813647b3cdcSGeorge Liu             {
3814002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3815002d39b4SEd Tanous                                            postCodeID);
3816647b3cdcSGeorge Liu                 return;
3817647b3cdcSGeorge Liu             }
3818647b3cdcSGeorge Liu             if (ec)
3819647b3cdcSGeorge Liu             {
3820647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3821647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
3822647b3cdcSGeorge Liu                 return;
3823647b3cdcSGeorge Liu             }
3824647b3cdcSGeorge Liu 
3825647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
3826002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
3827647b3cdcSGeorge Liu             {
3828647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3829002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3830002d39b4SEd Tanous                                            postCodeID);
3831647b3cdcSGeorge Liu                 return;
3832647b3cdcSGeorge Liu             }
3833647b3cdcSGeorge Liu 
38349eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
383546ff87baSEd Tanous             if (c.empty())
3836647b3cdcSGeorge Liu             {
3837647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
3838002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3839002d39b4SEd Tanous                                            postCodeID);
3840647b3cdcSGeorge Liu                 return;
3841647b3cdcSGeorge Liu             }
384246ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
384346ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
384446ff87baSEd Tanous             std::string_view strData(d, c.size());
3845647b3cdcSGeorge Liu 
3846d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
3847647b3cdcSGeorge Liu                                      "application/octet-stream");
3848d9f6c621SEd Tanous             asyncResp->res.addHeader(
3849d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
3850002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
3851647b3cdcSGeorge Liu             },
3852647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
3853647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
3854002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3855647b3cdcSGeorge Liu         });
3856647b3cdcSGeorge Liu }
3857647b3cdcSGeorge Liu 
38587e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3859a3316fc6SZhikuiRen {
38607e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3861*22d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
3862ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
38637e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
386445ca1b86SEd Tanous             [&app](const crow::Request& req,
38657e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3866*22d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
38673ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
386845ca1b86SEd Tanous         {
386945ca1b86SEd Tanous             return;
387045ca1b86SEd Tanous         }
3871*22d268cbSEd Tanous         if (systemName != "system")
3872*22d268cbSEd Tanous         {
3873*22d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
3874*22d268cbSEd Tanous                                        systemName);
3875*22d268cbSEd Tanous             return;
3876*22d268cbSEd Tanous         }
3877*22d268cbSEd Tanous 
3878647b3cdcSGeorge Liu         uint16_t bootIndex = 0;
3879647b3cdcSGeorge Liu         uint64_t codeIndex = 0;
3880647b3cdcSGeorge Liu         if (!parsePostCode(targetID, codeIndex, bootIndex))
3881a3316fc6SZhikuiRen         {
3882a3316fc6SZhikuiRen             // Requested ID was not found
3883ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3884a3316fc6SZhikuiRen             return;
3885a3316fc6SZhikuiRen         }
3886a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3887a3316fc6SZhikuiRen         {
3888a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
38897e860f15SJohn Edward Broadbent                              << targetID;
3890a3316fc6SZhikuiRen         }
3891a3316fc6SZhikuiRen 
38929c11a172SVijay Lobo         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
3893a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
38940fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3895a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3896a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3897a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3898a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3899a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3900a3316fc6SZhikuiRen 
3901a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
39027e860f15SJohn Edward Broadbent         });
3903a3316fc6SZhikuiRen }
3904a3316fc6SZhikuiRen 
39051da66f75SEd Tanous } // namespace redfish
3906