xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 5b3785464cb8a43dc6ab763789bb722bdc634ebc)
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>
278e31778eSAsmitha Karunanithi #include <tinyxml2.h>
28400fd1fbSAdriana Kobylak #include <unistd.h>
29e1f26343SJason M. Bills 
307e860f15SJohn Edward Broadbent #include <app.hpp>
319896eaedSEd Tanous #include <boost/algorithm/string/case_conv.hpp>
3211ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
33400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
344851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
3507c8c20dSEd Tanous #include <boost/beast/http/verb.hpp>
361da66f75SEd Tanous #include <boost/container/flat_map.hpp>
371ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
38168e20c1SEd Tanous #include <dbus_utility.hpp>
39cb92c03bSAndrew Geissler #include <error_messages.hpp>
4045ca1b86SEd Tanous #include <query.hpp>
41ed398213SEd Tanous #include <registries/privilege_registry.hpp>
42d1bde9e5SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
43d1bde9e5SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
44d1bde9e5SKrzysztof Grobelny #include <utils/dbus_utils.hpp>
452b82937eSEd Tanous #include <utils/time_utils.hpp>
461214b7e7SGunnar Mills 
47647b3cdcSGeorge Liu #include <charconv>
484418c7f0SJames Feist #include <filesystem>
4975710de2SXiaochao Ma #include <optional>
5026702d01SEd Tanous #include <span>
51cd225da8SJason M. Bills #include <string_view>
52abf2add6SEd Tanous #include <variant>
531da66f75SEd Tanous 
541da66f75SEd Tanous namespace redfish
551da66f75SEd Tanous {
561da66f75SEd Tanous 
575b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
585b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
595b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
605b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
615b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
625b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
63424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
646eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
656eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
661da66f75SEd Tanous 
678e31778eSAsmitha Karunanithi enum class DumpCreationProgress
688e31778eSAsmitha Karunanithi {
698e31778eSAsmitha Karunanithi     DUMP_CREATE_SUCCESS,
708e31778eSAsmitha Karunanithi     DUMP_CREATE_FAILED,
718e31778eSAsmitha Karunanithi     DUMP_CREATE_INPROGRESS
728e31778eSAsmitha Karunanithi };
738e31778eSAsmitha Karunanithi 
74fffb8c1fSEd Tanous namespace registries
754851d45dSJason M. Bills {
7626702d01SEd Tanous static const Message*
7726702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
7826702d01SEd Tanous                            const std::span<const MessageEntry> registry)
794851d45dSJason M. Bills {
80002d39b4SEd Tanous     std::span<const MessageEntry>::iterator messageIt =
81002d39b4SEd Tanous         std::find_if(registry.begin(), registry.end(),
824851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
83e662eae8SEd Tanous         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
844851d45dSJason M. Bills         });
8526702d01SEd Tanous     if (messageIt != registry.end())
864851d45dSJason M. Bills     {
874851d45dSJason M. Bills         return &messageIt->second;
884851d45dSJason M. Bills     }
894851d45dSJason M. Bills 
904851d45dSJason M. Bills     return nullptr;
914851d45dSJason M. Bills }
924851d45dSJason M. Bills 
934851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
944851d45dSJason M. Bills {
954851d45dSJason M. Bills     // Redfish MessageIds are in the form
964851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
974851d45dSJason M. Bills     // the right Message
984851d45dSJason M. Bills     std::vector<std::string> fields;
994851d45dSJason M. Bills     fields.reserve(4);
1004851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
10102cad96eSEd Tanous     const std::string& registryName = fields[0];
10202cad96eSEd Tanous     const std::string& messageKey = fields[3];
1034851d45dSJason M. Bills 
1044851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
1054851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
1064851d45dSJason M. Bills     {
1074851d45dSJason M. Bills         return getMessageFromRegistry(
10826702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
1094851d45dSJason M. Bills     }
1104851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
1114851d45dSJason M. Bills     {
1124851d45dSJason M. Bills         return getMessageFromRegistry(
11326702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1144851d45dSJason M. Bills     }
1154851d45dSJason M. Bills     return nullptr;
1164851d45dSJason M. Bills }
117fffb8c1fSEd Tanous } // namespace registries
1184851d45dSJason M. Bills 
119f6150403SJames Feist namespace fs = std::filesystem;
1201da66f75SEd Tanous 
121cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
122cb92c03bSAndrew Geissler {
123d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
124d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
125d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
126d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
127cb92c03bSAndrew Geissler     {
128cb92c03bSAndrew Geissler         return "Critical";
129cb92c03bSAndrew Geissler     }
1303174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
131d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
132d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
133cb92c03bSAndrew Geissler     {
134cb92c03bSAndrew Geissler         return "OK";
135cb92c03bSAndrew Geissler     }
1363174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
137cb92c03bSAndrew Geissler     {
138cb92c03bSAndrew Geissler         return "Warning";
139cb92c03bSAndrew Geissler     }
140cb92c03bSAndrew Geissler     return "";
141cb92c03bSAndrew Geissler }
142cb92c03bSAndrew Geissler 
1437e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
14439e77504SEd Tanous                                      const std::string_view& field,
14539e77504SEd Tanous                                      std::string_view& contents)
14616428a1aSJason M. Bills {
14716428a1aSJason M. Bills     const char* data = nullptr;
14816428a1aSJason M. Bills     size_t length = 0;
14916428a1aSJason M. Bills     int ret = 0;
15016428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15146ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
15246ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
15346ff87baSEd Tanous 
15446ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
15516428a1aSJason M. Bills     if (ret < 0)
15616428a1aSJason M. Bills     {
15716428a1aSJason M. Bills         return ret;
15816428a1aSJason M. Bills     }
15939e77504SEd Tanous     contents = std::string_view(data, length);
16016428a1aSJason M. Bills     // Only use the content after the "=" character.
16181ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
16216428a1aSJason M. Bills     return ret;
16316428a1aSJason M. Bills }
16416428a1aSJason M. Bills 
1657e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1667e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1677e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
16816428a1aSJason M. Bills {
16916428a1aSJason M. Bills     int ret = 0;
17039e77504SEd Tanous     std::string_view metadata;
17116428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
17216428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
17316428a1aSJason M. Bills     if (ret < 0)
17416428a1aSJason M. Bills     {
17516428a1aSJason M. Bills         return ret;
17616428a1aSJason M. Bills     }
177b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
17816428a1aSJason M. Bills     return ret;
17916428a1aSJason M. Bills }
18016428a1aSJason M. Bills 
1817e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1827e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
183a3316fc6SZhikuiRen {
184a3316fc6SZhikuiRen     int ret = 0;
185a3316fc6SZhikuiRen     uint64_t timestamp = 0;
186a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
187a3316fc6SZhikuiRen     if (ret < 0)
188a3316fc6SZhikuiRen     {
189a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
190a3316fc6SZhikuiRen                          << strerror(-ret);
191a3316fc6SZhikuiRen         return false;
192a3316fc6SZhikuiRen     }
1932b82937eSEd Tanous     entryTimestamp =
1942b82937eSEd Tanous         redfish::time_utils::getDateTimeUint(timestamp / 1000 / 1000);
1959c620e21SAsmitha Karunanithi     return true;
196a3316fc6SZhikuiRen }
19750b8a43aSEd Tanous 
1987e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
199e85d6b16SJason M. Bills                                     const bool firstEntry = true)
20016428a1aSJason M. Bills {
20116428a1aSJason M. Bills     int ret = 0;
20216428a1aSJason M. Bills     static uint64_t prevTs = 0;
20316428a1aSJason M. Bills     static int index = 0;
204e85d6b16SJason M. Bills     if (firstEntry)
205e85d6b16SJason M. Bills     {
206e85d6b16SJason M. Bills         prevTs = 0;
207e85d6b16SJason M. Bills     }
208e85d6b16SJason M. Bills 
20916428a1aSJason M. Bills     // Get the entry timestamp
21016428a1aSJason M. Bills     uint64_t curTs = 0;
21116428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
21216428a1aSJason M. Bills     if (ret < 0)
21316428a1aSJason M. Bills     {
21416428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
21516428a1aSJason M. Bills                          << strerror(-ret);
21616428a1aSJason M. Bills         return false;
21716428a1aSJason M. Bills     }
21816428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
21916428a1aSJason M. Bills     if (curTs == prevTs)
22016428a1aSJason M. Bills     {
22116428a1aSJason M. Bills         index++;
22216428a1aSJason M. Bills     }
22316428a1aSJason M. Bills     else
22416428a1aSJason M. Bills     {
22516428a1aSJason M. Bills         // Otherwise, reset it
22616428a1aSJason M. Bills         index = 0;
22716428a1aSJason M. Bills     }
22816428a1aSJason M. Bills     // Save the timestamp
22916428a1aSJason M. Bills     prevTs = curTs;
23016428a1aSJason M. Bills 
23116428a1aSJason M. Bills     entryID = std::to_string(curTs);
23216428a1aSJason M. Bills     if (index > 0)
23316428a1aSJason M. Bills     {
23416428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
23516428a1aSJason M. Bills     }
23616428a1aSJason M. Bills     return true;
23716428a1aSJason M. Bills }
23816428a1aSJason M. Bills 
239e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
240e85d6b16SJason M. Bills                              const bool firstEntry = true)
24195820184SJason M. Bills {
242271584abSEd Tanous     static time_t prevTs = 0;
24395820184SJason M. Bills     static int index = 0;
244e85d6b16SJason M. Bills     if (firstEntry)
245e85d6b16SJason M. Bills     {
246e85d6b16SJason M. Bills         prevTs = 0;
247e85d6b16SJason M. Bills     }
248e85d6b16SJason M. Bills 
24995820184SJason M. Bills     // Get the entry timestamp
250271584abSEd Tanous     std::time_t curTs = 0;
25195820184SJason M. Bills     std::tm timeStruct = {};
25295820184SJason M. Bills     std::istringstream entryStream(logEntry);
25395820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
25495820184SJason M. Bills     {
25595820184SJason M. Bills         curTs = std::mktime(&timeStruct);
25695820184SJason M. Bills     }
25795820184SJason M. Bills     // If the timestamp isn't unique, increment the index
25895820184SJason M. Bills     if (curTs == prevTs)
25995820184SJason M. Bills     {
26095820184SJason M. Bills         index++;
26195820184SJason M. Bills     }
26295820184SJason M. Bills     else
26395820184SJason M. Bills     {
26495820184SJason M. Bills         // Otherwise, reset it
26595820184SJason M. Bills         index = 0;
26695820184SJason M. Bills     }
26795820184SJason M. Bills     // Save the timestamp
26895820184SJason M. Bills     prevTs = curTs;
26995820184SJason M. Bills 
27095820184SJason M. Bills     entryID = std::to_string(curTs);
27195820184SJason M. Bills     if (index > 0)
27295820184SJason M. Bills     {
27395820184SJason M. Bills         entryID += "_" + std::to_string(index);
27495820184SJason M. Bills     }
27595820184SJason M. Bills     return true;
27695820184SJason M. Bills }
27795820184SJason M. Bills 
2787e860f15SJohn Edward Broadbent inline static bool
2798d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2808d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2818d1b46d7Szhanghch05                        uint64_t& index)
28216428a1aSJason M. Bills {
28316428a1aSJason M. Bills     if (entryID.empty())
28416428a1aSJason M. Bills     {
28516428a1aSJason M. Bills         return false;
28616428a1aSJason M. Bills     }
28716428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
28839e77504SEd Tanous     std::string_view tsStr(entryID);
28916428a1aSJason M. Bills 
29081ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
29171d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
29216428a1aSJason M. Bills     {
29316428a1aSJason M. Bills         // Timestamp has an index
29416428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
29539e77504SEd Tanous         std::string_view indexStr(entryID);
29616428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
297c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
298c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
299c0bd5e4bSEd Tanous         if (ec != std::errc())
30016428a1aSJason M. Bills         {
3019db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
30216428a1aSJason M. Bills             return false;
30316428a1aSJason M. Bills         }
30416428a1aSJason M. Bills     }
30516428a1aSJason M. Bills     // Timestamp has no index
306c0bd5e4bSEd Tanous     auto [ptr, ec] =
307c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
308c0bd5e4bSEd Tanous     if (ec != std::errc())
30916428a1aSJason M. Bills     {
3109db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
31116428a1aSJason M. Bills         return false;
31216428a1aSJason M. Bills     }
31316428a1aSJason M. Bills     return true;
31416428a1aSJason M. Bills }
31516428a1aSJason M. Bills 
31695820184SJason M. Bills static bool
31795820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
31895820184SJason M. Bills {
31995820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
32095820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
32195820184SJason M. Bills 
32295820184SJason M. Bills     // Loop through the directory looking for redfish log files
32395820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
32495820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
32595820184SJason M. Bills     {
32695820184SJason M. Bills         // If we find a redfish log file, save the path
32795820184SJason M. Bills         std::string filename = dirEnt.path().filename();
32811ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
32995820184SJason M. Bills         {
33095820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
33195820184SJason M. Bills         }
33295820184SJason M. Bills     }
33395820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
33495820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
33595820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
33695820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
33795820184SJason M. Bills 
33895820184SJason M. Bills     return !redfishLogFiles.empty();
33995820184SJason M. Bills }
34095820184SJason M. Bills 
341aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
3422d613eb6SJiaqing Zhao     const dbus::utility::ManagedObjectType::value_type& object,
343c6fecdabSClaire Weinan     std::string& dumpStatus, uint64_t& size, uint64_t& timestampUs,
344aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
345aefe3786SClaire Weinan {
346aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
347aefe3786SClaire Weinan     {
348aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
349aefe3786SClaire Weinan         {
350aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
351aefe3786SClaire Weinan             {
352aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
353aefe3786SClaire Weinan                 {
354aefe3786SClaire Weinan                     const auto* status =
355aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
356aefe3786SClaire Weinan                     if (status == nullptr)
357aefe3786SClaire Weinan                     {
358aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
359aefe3786SClaire Weinan                         break;
360aefe3786SClaire Weinan                     }
361aefe3786SClaire Weinan                     dumpStatus = *status;
362aefe3786SClaire Weinan                 }
363aefe3786SClaire Weinan             }
364aefe3786SClaire Weinan         }
365aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
366aefe3786SClaire Weinan         {
367aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
368aefe3786SClaire Weinan             {
369aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
370aefe3786SClaire Weinan                 {
371aefe3786SClaire Weinan                     const auto* sizePtr =
372aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
373aefe3786SClaire Weinan                     if (sizePtr == nullptr)
374aefe3786SClaire Weinan                     {
375aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
376aefe3786SClaire Weinan                         break;
377aefe3786SClaire Weinan                     }
378aefe3786SClaire Weinan                     size = *sizePtr;
379aefe3786SClaire Weinan                     break;
380aefe3786SClaire Weinan                 }
381aefe3786SClaire Weinan             }
382aefe3786SClaire Weinan         }
383aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
384aefe3786SClaire Weinan         {
385aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
386aefe3786SClaire Weinan             {
387aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
388aefe3786SClaire Weinan                 {
389aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
390aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
391aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
392aefe3786SClaire Weinan                     {
393aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
394aefe3786SClaire Weinan                         break;
395aefe3786SClaire Weinan                     }
396c6fecdabSClaire Weinan                     timestampUs = *usecsTimeStamp;
397aefe3786SClaire Weinan                     break;
398aefe3786SClaire Weinan                 }
399aefe3786SClaire Weinan             }
400aefe3786SClaire Weinan         }
401aefe3786SClaire Weinan     }
402aefe3786SClaire Weinan }
403aefe3786SClaire Weinan 
40421ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
405fdd26906SClaire Weinan {
406fdd26906SClaire Weinan     std::string entriesPath;
407fdd26906SClaire Weinan 
408fdd26906SClaire Weinan     if (dumpType == "BMC")
409fdd26906SClaire Weinan     {
410fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
411fdd26906SClaire Weinan     }
412fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
413fdd26906SClaire Weinan     {
414fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
415fdd26906SClaire Weinan     }
416fdd26906SClaire Weinan     else if (dumpType == "System")
417fdd26906SClaire Weinan     {
418fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
419fdd26906SClaire Weinan     }
420fdd26906SClaire Weinan     else
421fdd26906SClaire Weinan     {
422fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
423fdd26906SClaire Weinan                          << dumpType;
424fdd26906SClaire Weinan     }
425fdd26906SClaire Weinan 
426fdd26906SClaire Weinan     // Returns empty string on error
427fdd26906SClaire Weinan     return entriesPath;
428fdd26906SClaire Weinan }
429fdd26906SClaire Weinan 
4308d1b46d7Szhanghch05 inline void
4318d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4325cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4335cb1dd27SAsmitha Karunanithi {
434fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
435fdd26906SClaire Weinan     if (entriesPath.empty())
4365cb1dd27SAsmitha Karunanithi     {
4375cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4385cb1dd27SAsmitha Karunanithi         return;
4395cb1dd27SAsmitha Karunanithi     }
4405cb1dd27SAsmitha Karunanithi 
4415cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
442fdd26906SClaire Weinan         [asyncResp, entriesPath,
443711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
444711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4455cb1dd27SAsmitha Karunanithi         if (ec)
4465cb1dd27SAsmitha Karunanithi         {
4475cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4485cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4495cb1dd27SAsmitha Karunanithi             return;
4505cb1dd27SAsmitha Karunanithi         }
4515cb1dd27SAsmitha Karunanithi 
452fdd26906SClaire Weinan         // Remove ending slash
453fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
454fdd26906SClaire Weinan         if (!odataIdStr.empty())
455fdd26906SClaire Weinan         {
456fdd26906SClaire Weinan             odataIdStr.pop_back();
457fdd26906SClaire Weinan         }
458fdd26906SClaire Weinan 
459fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
460fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
461fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
462fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
463fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
464fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
465fdd26906SClaire Weinan 
4665cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4675cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
468b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
469b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
470002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
4715cb1dd27SAsmitha Karunanithi 
472002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
473002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
474002d39b4SEd Tanous                                                r.first.filename());
475565dfb6fSClaire Weinan         });
476565dfb6fSClaire Weinan 
4775cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4785cb1dd27SAsmitha Karunanithi         {
479b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4805cb1dd27SAsmitha Karunanithi             {
4815cb1dd27SAsmitha Karunanithi                 continue;
4825cb1dd27SAsmitha Karunanithi             }
483c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
4845cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
48535440d18SAsmitha Karunanithi             std::string dumpStatus;
486433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
4872dfd18efSEd Tanous 
4882dfd18efSEd Tanous             std::string entryID = object.first.filename();
4892dfd18efSEd Tanous             if (entryID.empty())
4905cb1dd27SAsmitha Karunanithi             {
4915cb1dd27SAsmitha Karunanithi                 continue;
4925cb1dd27SAsmitha Karunanithi             }
4935cb1dd27SAsmitha Karunanithi 
494c6fecdabSClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestampUs,
495aefe3786SClaire Weinan                                          asyncResp);
4965cb1dd27SAsmitha Karunanithi 
4970fda0f12SGeorge Liu             if (dumpStatus !=
4980fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
49935440d18SAsmitha Karunanithi                 !dumpStatus.empty())
50035440d18SAsmitha Karunanithi             {
50135440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
50235440d18SAsmitha Karunanithi                 continue;
50335440d18SAsmitha Karunanithi             }
50435440d18SAsmitha Karunanithi 
5059c11a172SVijay Lobo             thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
506fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
5075cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
5085cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
5095cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
510bbd80db8SClaire Weinan             thisEntry["Created"] =
511bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5125cb1dd27SAsmitha Karunanithi 
5135cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5145cb1dd27SAsmitha Karunanithi             {
515d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
516d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
517fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
518fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5195cb1dd27SAsmitha Karunanithi             }
5205cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5215cb1dd27SAsmitha Karunanithi             {
522d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
523d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
524d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
525fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
526fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5275cb1dd27SAsmitha Karunanithi             }
52835440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5295cb1dd27SAsmitha Karunanithi         }
530002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5315cb1dd27SAsmitha Karunanithi         },
5325cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5335cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5345cb1dd27SAsmitha Karunanithi }
5355cb1dd27SAsmitha Karunanithi 
5368d1b46d7Szhanghch05 inline void
537c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5388d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5395cb1dd27SAsmitha Karunanithi {
540fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
541fdd26906SClaire Weinan     if (entriesPath.empty())
5425cb1dd27SAsmitha Karunanithi     {
5435cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5445cb1dd27SAsmitha Karunanithi         return;
5455cb1dd27SAsmitha Karunanithi     }
5465cb1dd27SAsmitha Karunanithi 
5475cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
548fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
549fdd26906SClaire Weinan          entriesPath](const boost::system::error_code ec,
55002cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5515cb1dd27SAsmitha Karunanithi         if (ec)
5525cb1dd27SAsmitha Karunanithi         {
5535cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5545cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5555cb1dd27SAsmitha Karunanithi             return;
5565cb1dd27SAsmitha Karunanithi         }
5575cb1dd27SAsmitha Karunanithi 
558b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
559b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
560b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
561002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
562b47452b2SAsmitha Karunanithi 
5639eb808c1SEd Tanous         for (const auto& objectPath : resp)
5645cb1dd27SAsmitha Karunanithi         {
565b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5665cb1dd27SAsmitha Karunanithi             {
5675cb1dd27SAsmitha Karunanithi                 continue;
5685cb1dd27SAsmitha Karunanithi             }
5695cb1dd27SAsmitha Karunanithi 
5705cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
571c6fecdabSClaire Weinan             uint64_t timestampUs = 0;
5725cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
57335440d18SAsmitha Karunanithi             std::string dumpStatus;
5745cb1dd27SAsmitha Karunanithi 
575aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
576c6fecdabSClaire Weinan                                          timestampUs, asyncResp);
5775cb1dd27SAsmitha Karunanithi 
5780fda0f12SGeorge Liu             if (dumpStatus !=
5790fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
58035440d18SAsmitha Karunanithi                 !dumpStatus.empty())
58135440d18SAsmitha Karunanithi             {
58235440d18SAsmitha Karunanithi                 // Dump status is not Complete
58335440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
584d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
585d1bde9e5SKrzysztof Grobelny                                            entryID);
58635440d18SAsmitha Karunanithi                 return;
58735440d18SAsmitha Karunanithi             }
58835440d18SAsmitha Karunanithi 
5895cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
5909c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
591fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
5925cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
5935cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
5945cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
595bbd80db8SClaire Weinan             asyncResp->res.jsonValue["Created"] =
596bbd80db8SClaire Weinan                 redfish::time_utils::getDateTimeUintUs(timestampUs);
5975cb1dd27SAsmitha Karunanithi 
5985cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5995cb1dd27SAsmitha Karunanithi             {
600d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
601d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
602fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
603fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6045cb1dd27SAsmitha Karunanithi             }
6055cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6065cb1dd27SAsmitha Karunanithi             {
607d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
608002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
609d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
610fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
611fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6125cb1dd27SAsmitha Karunanithi             }
6135cb1dd27SAsmitha Karunanithi         }
614e05aec50SEd Tanous         if (!foundDumpEntry)
615b47452b2SAsmitha Karunanithi         {
616b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
617b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
618b47452b2SAsmitha Karunanithi             return;
619b47452b2SAsmitha Karunanithi         }
6205cb1dd27SAsmitha Karunanithi         },
6215cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6225cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6235cb1dd27SAsmitha Karunanithi }
6245cb1dd27SAsmitha Karunanithi 
6258d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6269878256fSStanley Chu                             const std::string& entryID,
627b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6285cb1dd27SAsmitha Karunanithi {
629002d39b4SEd Tanous     auto respHandler =
630002d39b4SEd Tanous         [asyncResp, entryID](const boost::system::error_code ec) {
6315cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6325cb1dd27SAsmitha Karunanithi         if (ec)
6335cb1dd27SAsmitha Karunanithi         {
6343de8d8baSGeorge Liu             if (ec.value() == EBADR)
6353de8d8baSGeorge Liu             {
6363de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6373de8d8baSGeorge Liu                 return;
6383de8d8baSGeorge Liu             }
6395cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
640fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6415cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6425cb1dd27SAsmitha Karunanithi             return;
6435cb1dd27SAsmitha Karunanithi         }
6445cb1dd27SAsmitha Karunanithi     };
6455cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6465cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
647b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
648b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
649b47452b2SAsmitha Karunanithi             entryID,
6505cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6515cb1dd27SAsmitha Karunanithi }
6525cb1dd27SAsmitha Karunanithi 
6538e31778eSAsmitha Karunanithi inline DumpCreationProgress
6548e31778eSAsmitha Karunanithi     mapDbusStatusToDumpProgress(const std::string& status)
655a43be80fSAsmitha Karunanithi {
6568e31778eSAsmitha Karunanithi     if (status ==
6578e31778eSAsmitha Karunanithi             "xyz.openbmc_project.Common.Progress.OperationStatus.Failed" ||
6588e31778eSAsmitha Karunanithi         status == "xyz.openbmc_project.Common.Progress.OperationStatus.Aborted")
6598e31778eSAsmitha Karunanithi     {
6608e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_FAILED;
6618e31778eSAsmitha Karunanithi     }
6628e31778eSAsmitha Karunanithi     if (status ==
6638e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed")
6648e31778eSAsmitha Karunanithi     {
6658e31778eSAsmitha Karunanithi         return DumpCreationProgress::DUMP_CREATE_SUCCESS;
6668e31778eSAsmitha Karunanithi     }
6678e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
6688e31778eSAsmitha Karunanithi }
6698e31778eSAsmitha Karunanithi 
6708e31778eSAsmitha Karunanithi inline DumpCreationProgress
6718e31778eSAsmitha Karunanithi     getDumpCompletionStatus(const dbus::utility::DBusPropertiesMap& values)
6728e31778eSAsmitha Karunanithi {
6738e31778eSAsmitha Karunanithi     for (const auto& [key, val] : values)
6748e31778eSAsmitha Karunanithi     {
6758e31778eSAsmitha Karunanithi         if (key == "Status")
6768e31778eSAsmitha Karunanithi         {
6778e31778eSAsmitha Karunanithi             const std::string* value = std::get_if<std::string>(&val);
6788e31778eSAsmitha Karunanithi             if (value == nullptr)
6798e31778eSAsmitha Karunanithi             {
6808e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Status property value is null";
6818e31778eSAsmitha Karunanithi                 return DumpCreationProgress::DUMP_CREATE_FAILED;
6828e31778eSAsmitha Karunanithi             }
6838e31778eSAsmitha Karunanithi             return mapDbusStatusToDumpProgress(*value);
6848e31778eSAsmitha Karunanithi         }
6858e31778eSAsmitha Karunanithi     }
6868e31778eSAsmitha Karunanithi     return DumpCreationProgress::DUMP_CREATE_INPROGRESS;
6878e31778eSAsmitha Karunanithi }
6888e31778eSAsmitha Karunanithi 
6898e31778eSAsmitha Karunanithi inline std::string getDumpEntryPath(const std::string& dumpPath)
6908e31778eSAsmitha Karunanithi {
6918e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/bmc/entry")
6928e31778eSAsmitha Karunanithi     {
6938e31778eSAsmitha Karunanithi         return "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
6948e31778eSAsmitha Karunanithi     }
6958e31778eSAsmitha Karunanithi     if (dumpPath == "/xyz/openbmc_project/dump/system/entry")
6968e31778eSAsmitha Karunanithi     {
6978e31778eSAsmitha Karunanithi         return "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
6988e31778eSAsmitha Karunanithi     }
6998e31778eSAsmitha Karunanithi     return "";
7008e31778eSAsmitha Karunanithi }
7018e31778eSAsmitha Karunanithi 
7028e31778eSAsmitha Karunanithi inline void createDumpTaskCallback(
7038e31778eSAsmitha Karunanithi     task::Payload&& payload,
7048e31778eSAsmitha Karunanithi     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7058e31778eSAsmitha Karunanithi     const sdbusplus::message::object_path& createdObjPath)
7068e31778eSAsmitha Karunanithi {
7078e31778eSAsmitha Karunanithi     const std::string dumpPath = createdObjPath.parent_path().str;
7088e31778eSAsmitha Karunanithi     const std::string dumpId = createdObjPath.filename();
7098e31778eSAsmitha Karunanithi 
7108e31778eSAsmitha Karunanithi     std::string dumpEntryPath = getDumpEntryPath(dumpPath);
7118e31778eSAsmitha Karunanithi 
7128e31778eSAsmitha Karunanithi     if (dumpEntryPath.empty())
7138e31778eSAsmitha Karunanithi     {
7148e31778eSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type received";
7158e31778eSAsmitha Karunanithi         messages::internalError(asyncResp->res);
7168e31778eSAsmitha Karunanithi         return;
7178e31778eSAsmitha Karunanithi     }
7188e31778eSAsmitha Karunanithi 
7198e31778eSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7208e31778eSAsmitha Karunanithi         [asyncResp, payload, createdObjPath,
7218e31778eSAsmitha Karunanithi          dumpEntryPath{std::move(dumpEntryPath)},
7228e31778eSAsmitha Karunanithi          dumpId](const boost::system::error_code ec,
7238e31778eSAsmitha Karunanithi                  const std::string& introspectXml) {
7248e31778eSAsmitha Karunanithi         if (ec)
7258e31778eSAsmitha Karunanithi         {
7268e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Introspect call failed with error: "
7278e31778eSAsmitha Karunanithi                              << ec.message();
7288e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7298e31778eSAsmitha Karunanithi             return;
7308e31778eSAsmitha Karunanithi         }
7318e31778eSAsmitha Karunanithi 
7328e31778eSAsmitha Karunanithi         // Check if the created dump object has implemented Progress
7338e31778eSAsmitha Karunanithi         // interface to track dump completion. If yes, fetch the "Status"
7348e31778eSAsmitha Karunanithi         // property of the interface, modify the task state accordingly.
7358e31778eSAsmitha Karunanithi         // Else, return task completed.
7368e31778eSAsmitha Karunanithi         tinyxml2::XMLDocument doc;
7378e31778eSAsmitha Karunanithi 
7388e31778eSAsmitha Karunanithi         doc.Parse(introspectXml.data(), introspectXml.size());
7398e31778eSAsmitha Karunanithi         tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node");
7408e31778eSAsmitha Karunanithi         if (pRoot == nullptr)
7418e31778eSAsmitha Karunanithi         {
7428e31778eSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "XML document failed to parse";
7438e31778eSAsmitha Karunanithi             messages::internalError(asyncResp->res);
7448e31778eSAsmitha Karunanithi             return;
7458e31778eSAsmitha Karunanithi         }
7468e31778eSAsmitha Karunanithi         tinyxml2::XMLElement* interfaceNode =
7478e31778eSAsmitha Karunanithi             pRoot->FirstChildElement("interface");
7488e31778eSAsmitha Karunanithi 
7498e31778eSAsmitha Karunanithi         bool isProgressIntfPresent = false;
7508e31778eSAsmitha Karunanithi         while (interfaceNode != nullptr)
7518e31778eSAsmitha Karunanithi         {
7528e31778eSAsmitha Karunanithi             const char* thisInterfaceName = interfaceNode->Attribute("name");
7538e31778eSAsmitha Karunanithi             if (thisInterfaceName != nullptr)
7548e31778eSAsmitha Karunanithi             {
7558e31778eSAsmitha Karunanithi                 if (thisInterfaceName ==
7568e31778eSAsmitha Karunanithi                     std::string_view("xyz.openbmc_project.Common.Progress"))
7578e31778eSAsmitha Karunanithi                 {
7588e31778eSAsmitha Karunanithi                     interfaceNode =
7598e31778eSAsmitha Karunanithi                         interfaceNode->NextSiblingElement("interface");
7608e31778eSAsmitha Karunanithi                     continue;
7618e31778eSAsmitha Karunanithi                 }
7628e31778eSAsmitha Karunanithi                 isProgressIntfPresent = true;
7638e31778eSAsmitha Karunanithi                 break;
7648e31778eSAsmitha Karunanithi             }
7658e31778eSAsmitha Karunanithi             interfaceNode = interfaceNode->NextSiblingElement("interface");
7668e31778eSAsmitha Karunanithi         }
7678e31778eSAsmitha Karunanithi 
768a43be80fSAsmitha Karunanithi         std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7698e31778eSAsmitha Karunanithi             [createdObjPath, dumpEntryPath, dumpId, isProgressIntfPresent](
770*5b378546SPatrick Williams                 boost::system::error_code err, sdbusplus::message_t& msg,
771a43be80fSAsmitha Karunanithi                 const std::shared_ptr<task::TaskData>& taskData) {
772cb13a392SEd Tanous             if (err)
773cb13a392SEd Tanous             {
7748e31778eSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << createdObjPath.str
7758e31778eSAsmitha Karunanithi                                  << ": Error in creating dump";
7768e31778eSAsmitha Karunanithi                 taskData->messages.emplace_back(messages::internalError());
7776145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7786145ed6fSAsmitha Karunanithi                 return task::completed;
779cb13a392SEd Tanous             }
780b9d36b47SEd Tanous 
7818e31778eSAsmitha Karunanithi             if (isProgressIntfPresent)
782a43be80fSAsmitha Karunanithi             {
7838e31778eSAsmitha Karunanithi                 dbus::utility::DBusPropertiesMap values;
7848e31778eSAsmitha Karunanithi                 std::string prop;
7858e31778eSAsmitha Karunanithi                 msg.read(prop, values);
7868e31778eSAsmitha Karunanithi 
7878e31778eSAsmitha Karunanithi                 DumpCreationProgress dumpStatus =
7888e31778eSAsmitha Karunanithi                     getDumpCompletionStatus(values);
7898e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_FAILED)
7908e31778eSAsmitha Karunanithi                 {
7918e31778eSAsmitha Karunanithi                     BMCWEB_LOG_ERROR << createdObjPath.str
7928e31778eSAsmitha Karunanithi                                      << ": Error in creating dump";
7938e31778eSAsmitha Karunanithi                     taskData->state = "Cancelled";
7948e31778eSAsmitha Karunanithi                     return task::completed;
7958e31778eSAsmitha Karunanithi                 }
7968e31778eSAsmitha Karunanithi 
7978e31778eSAsmitha Karunanithi                 if (dumpStatus == DumpCreationProgress::DUMP_CREATE_INPROGRESS)
7988e31778eSAsmitha Karunanithi                 {
7998e31778eSAsmitha Karunanithi                     BMCWEB_LOG_DEBUG << createdObjPath.str
8008e31778eSAsmitha Karunanithi                                      << ": Dump creation task is in progress";
8018e31778eSAsmitha Karunanithi                     return !task::completed;
8028e31778eSAsmitha Karunanithi                 }
8038e31778eSAsmitha Karunanithi             }
8048e31778eSAsmitha Karunanithi 
805a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
806a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
807a43be80fSAsmitha Karunanithi 
808a43be80fSAsmitha Karunanithi             std::string headerLoc =
8098e31778eSAsmitha Karunanithi                 "Location: " + dumpEntryPath + http_helpers::urlEncode(dumpId);
810002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
811a43be80fSAsmitha Karunanithi 
8128e31778eSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << createdObjPath.str
8138e31778eSAsmitha Karunanithi                              << ": Dump creation task completed";
814a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
815b47452b2SAsmitha Karunanithi             return task::completed;
816a43be80fSAsmitha Karunanithi             },
8178e31778eSAsmitha Karunanithi             "type='signal',interface='org.freedesktop.DBus.Properties',"
8188e31778eSAsmitha Karunanithi             "member='PropertiesChanged',path='" +
8198e31778eSAsmitha Karunanithi                 createdObjPath.str + "'");
820a43be80fSAsmitha Karunanithi 
8218e31778eSAsmitha Karunanithi         // The task timer is set to max time limit within which the
8228e31778eSAsmitha Karunanithi         // requested dump will be collected.
8238e31778eSAsmitha Karunanithi         task->startTimer(std::chrono::minutes(6));
824a43be80fSAsmitha Karunanithi         task->populateResp(asyncResp->res);
8258e31778eSAsmitha Karunanithi         task->payload.emplace(payload);
8268e31778eSAsmitha Karunanithi         },
8278e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", createdObjPath,
8288e31778eSAsmitha Karunanithi         "org.freedesktop.DBus.Introspectable", "Introspect");
829a43be80fSAsmitha Karunanithi }
830a43be80fSAsmitha Karunanithi 
8318d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8328d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
833a43be80fSAsmitha Karunanithi {
834fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
835fdd26906SClaire Weinan     if (dumpPath.empty())
836a43be80fSAsmitha Karunanithi     {
837a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
838a43be80fSAsmitha Karunanithi         return;
839a43be80fSAsmitha Karunanithi     }
840a43be80fSAsmitha Karunanithi 
841a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
842a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
843a43be80fSAsmitha Karunanithi 
84415ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
845a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
846a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
847a43be80fSAsmitha Karunanithi     {
848a43be80fSAsmitha Karunanithi         return;
849a43be80fSAsmitha Karunanithi     }
850a43be80fSAsmitha Karunanithi 
851a43be80fSAsmitha Karunanithi     if (dumpType == "System")
852a43be80fSAsmitha Karunanithi     {
853a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
854a43be80fSAsmitha Karunanithi         {
8554978b63fSJason M. Bills             BMCWEB_LOG_ERROR
8564978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
857a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
858a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
859a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
860a43be80fSAsmitha Karunanithi             return;
861a43be80fSAsmitha Karunanithi         }
8623174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
863a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
864a43be80fSAsmitha Karunanithi         {
865a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
866ace85d60SEd Tanous             messages::internalError(asyncResp->res);
867a43be80fSAsmitha Karunanithi             return;
868a43be80fSAsmitha Karunanithi         }
8695907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/";
870a43be80fSAsmitha Karunanithi     }
871a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
872a43be80fSAsmitha Karunanithi     {
873a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
874a43be80fSAsmitha Karunanithi         {
8750fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8760fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
877a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
878a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
879a43be80fSAsmitha Karunanithi             return;
880a43be80fSAsmitha Karunanithi         }
8813174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
882a43be80fSAsmitha Karunanithi         {
883a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
884a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
885ace85d60SEd Tanous             messages::internalError(asyncResp->res);
886a43be80fSAsmitha Karunanithi             return;
887a43be80fSAsmitha Karunanithi         }
8885907571dSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/";
8895907571dSAsmitha Karunanithi     }
8905907571dSAsmitha Karunanithi     else
8915907571dSAsmitha Karunanithi     {
8925907571dSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "CreateDump failed. Unknown dump type";
8935907571dSAsmitha Karunanithi         messages::internalError(asyncResp->res);
8945907571dSAsmitha Karunanithi         return;
895a43be80fSAsmitha Karunanithi     }
896a43be80fSAsmitha Karunanithi 
8978e31778eSAsmitha Karunanithi     std::vector<std::pair<std::string, std::variant<std::string, uint64_t>>>
8988e31778eSAsmitha Karunanithi         createDumpParamVec;
8998e31778eSAsmitha Karunanithi 
900a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
901*5b378546SPatrick Williams         [asyncResp, payload(task::Payload(req)), dumpPath](
902*5b378546SPatrick Williams             const boost::system::error_code ec, const sdbusplus::message_t& msg,
9038e31778eSAsmitha Karunanithi             const sdbusplus::message::object_path& objPath) mutable {
904a43be80fSAsmitha Karunanithi         if (ec)
905a43be80fSAsmitha Karunanithi         {
906a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
9075907571dSAsmitha Karunanithi             const sd_bus_error* dbusError = msg.get_error();
9085907571dSAsmitha Karunanithi             if (dbusError == nullptr)
9095907571dSAsmitha Karunanithi             {
9105907571dSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
9115907571dSAsmitha Karunanithi                 return;
9125907571dSAsmitha Karunanithi             }
9135907571dSAsmitha Karunanithi 
9145907571dSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump DBus error: " << dbusError->name
9155907571dSAsmitha Karunanithi                              << " and error msg: " << dbusError->message;
9165907571dSAsmitha Karunanithi             if (std::string_view(
9175907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.NotAllowed") ==
9185907571dSAsmitha Karunanithi                 dbusError->name)
9195907571dSAsmitha Karunanithi             {
9205907571dSAsmitha Karunanithi                 messages::resourceInStandby(asyncResp->res);
9215907571dSAsmitha Karunanithi                 return;
9225907571dSAsmitha Karunanithi             }
9235907571dSAsmitha Karunanithi             if (std::string_view(
9245907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Dump.Create.Error.Disabled") ==
9255907571dSAsmitha Karunanithi                 dbusError->name)
9265907571dSAsmitha Karunanithi             {
9275907571dSAsmitha Karunanithi                 messages::serviceDisabled(asyncResp->res, dumpPath);
9285907571dSAsmitha Karunanithi                 return;
9295907571dSAsmitha Karunanithi             }
9305907571dSAsmitha Karunanithi             if (std::string_view(
9315907571dSAsmitha Karunanithi                     "xyz.openbmc_project.Common.Error.Unavailable") ==
9325907571dSAsmitha Karunanithi                 dbusError->name)
9335907571dSAsmitha Karunanithi             {
9345907571dSAsmitha Karunanithi                 messages::resourceInUse(asyncResp->res);
9355907571dSAsmitha Karunanithi                 return;
9365907571dSAsmitha Karunanithi             }
9375907571dSAsmitha Karunanithi             // Other Dbus errors such as:
9385907571dSAsmitha Karunanithi             // xyz.openbmc_project.Common.Error.InvalidArgument &
9395907571dSAsmitha Karunanithi             // org.freedesktop.DBus.Error.InvalidArgs are all related to
9405907571dSAsmitha Karunanithi             // the dbus call that is made here in the bmcweb
9415907571dSAsmitha Karunanithi             // implementation and has nothing to do with the client's
9425907571dSAsmitha Karunanithi             // input in the request. Hence, returning internal error
9435907571dSAsmitha Karunanithi             // back to the client.
944a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
945a43be80fSAsmitha Karunanithi             return;
946a43be80fSAsmitha Karunanithi         }
9478e31778eSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Path: " << objPath.str;
9488e31778eSAsmitha Karunanithi         createDumpTaskCallback(std::move(payload), asyncResp, objPath);
949a43be80fSAsmitha Karunanithi         },
950b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
951b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
952b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
9538e31778eSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump", createDumpParamVec);
954a43be80fSAsmitha Karunanithi }
955a43be80fSAsmitha Karunanithi 
9568d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9578d1b46d7Szhanghch05                       const std::string& dumpType)
95880319af1SAsmitha Karunanithi {
959b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
960b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
9618d1b46d7Szhanghch05 
96280319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
963b9d36b47SEd Tanous         [asyncResp, dumpType](
964b9d36b47SEd Tanous             const boost::system::error_code ec,
965b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
96680319af1SAsmitha Karunanithi         if (ec)
96780319af1SAsmitha Karunanithi         {
96880319af1SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
96980319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
97080319af1SAsmitha Karunanithi             return;
97180319af1SAsmitha Karunanithi         }
97280319af1SAsmitha Karunanithi 
97380319af1SAsmitha Karunanithi         for (const std::string& path : subTreePaths)
97480319af1SAsmitha Karunanithi         {
9752dfd18efSEd Tanous             sdbusplus::message::object_path objPath(path);
9762dfd18efSEd Tanous             std::string logID = objPath.filename();
9772dfd18efSEd Tanous             if (logID.empty())
97880319af1SAsmitha Karunanithi             {
9792dfd18efSEd Tanous                 continue;
98080319af1SAsmitha Karunanithi             }
9812dfd18efSEd Tanous             deleteDumpEntry(asyncResp, logID, dumpType);
98280319af1SAsmitha Karunanithi         }
98380319af1SAsmitha Karunanithi         },
98480319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
98580319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
98680319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
987b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
988b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
989b47452b2SAsmitha Karunanithi                                    dumpType});
99080319af1SAsmitha Karunanithi }
99180319af1SAsmitha Karunanithi 
992b9d36b47SEd Tanous inline static void
993b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
994b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
995b9d36b47SEd Tanous                              std::string& logfile)
996043a0536SJohnathan Mantey {
997d1bde9e5SKrzysztof Grobelny     const std::string* filenamePtr = nullptr;
998d1bde9e5SKrzysztof Grobelny     const std::string* timestampPtr = nullptr;
999d1bde9e5SKrzysztof Grobelny     const std::string* logfilePtr = nullptr;
1000d1bde9e5SKrzysztof Grobelny 
1001d1bde9e5SKrzysztof Grobelny     const bool success = sdbusplus::unpackPropertiesNoThrow(
1002d1bde9e5SKrzysztof Grobelny         dbus_utils::UnpackErrorPrinter(), params, "Timestamp", timestampPtr,
1003d1bde9e5SKrzysztof Grobelny         "Filename", filenamePtr, "Log", logfilePtr);
1004d1bde9e5SKrzysztof Grobelny 
1005d1bde9e5SKrzysztof Grobelny     if (!success)
1006043a0536SJohnathan Mantey     {
1007d1bde9e5SKrzysztof Grobelny         return;
1008043a0536SJohnathan Mantey     }
1009d1bde9e5SKrzysztof Grobelny 
1010d1bde9e5SKrzysztof Grobelny     if (filenamePtr != nullptr)
1011043a0536SJohnathan Mantey     {
1012d1bde9e5SKrzysztof Grobelny         filename = *filenamePtr;
1013d1bde9e5SKrzysztof Grobelny     }
1014d1bde9e5SKrzysztof Grobelny 
1015d1bde9e5SKrzysztof Grobelny     if (timestampPtr != nullptr)
1016043a0536SJohnathan Mantey     {
1017d1bde9e5SKrzysztof Grobelny         timestamp = *timestampPtr;
1018043a0536SJohnathan Mantey     }
1019d1bde9e5SKrzysztof Grobelny 
1020d1bde9e5SKrzysztof Grobelny     if (logfilePtr != nullptr)
1021043a0536SJohnathan Mantey     {
1022d1bde9e5SKrzysztof Grobelny         logfile = *logfilePtr;
1023043a0536SJohnathan Mantey     }
1024043a0536SJohnathan Mantey }
1025043a0536SJohnathan Mantey 
1026a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
10277e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
10281da66f75SEd Tanous {
1029c4bf6374SJason M. Bills     /**
1030c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
1031c4bf6374SJason M. Bills      */
103222d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/")
1033ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
1034002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1035002d39b4SEd Tanous             [&app](const crow::Request& req,
103622d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
103722d268cbSEd Tanous                    const std::string& systemName) {
10383ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1039c4bf6374SJason M. Bills         {
104045ca1b86SEd Tanous             return;
104145ca1b86SEd Tanous         }
104222d268cbSEd Tanous         if (systemName != "system")
104322d268cbSEd Tanous         {
104422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
104522d268cbSEd Tanous                                        systemName);
104622d268cbSEd Tanous             return;
104722d268cbSEd Tanous         }
104822d268cbSEd Tanous 
10497e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
10507e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1051c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1052c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
1053c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1054029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
105545ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
1056c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1057c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
1058002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1059c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
10601476687dSEd Tanous         nlohmann::json::object_t eventLog;
10611476687dSEd Tanous         eventLog["@odata.id"] =
10621476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
10631476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
10645cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
10651476687dSEd Tanous         nlohmann::json::object_t dumpLog;
1066002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
10671476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
1068c9bb6861Sraviteja-b #endif
1069c9bb6861Sraviteja-b 
1070d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
10711476687dSEd Tanous         nlohmann::json::object_t crashdump;
10721476687dSEd Tanous         crashdump["@odata.id"] =
10731476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
10741476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
1075d53dd41fSJason M. Bills #endif
1076b7028ebfSSpencer Ku 
1077b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
10781476687dSEd Tanous         nlohmann::json::object_t hostlogger;
10791476687dSEd Tanous         hostlogger["@odata.id"] =
10801476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
10811476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
1082b7028ebfSSpencer Ku #endif
1083c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
1084c4bf6374SJason M. Bills             logServiceArray.size();
1085a3316fc6SZhikuiRen 
1086a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
108745ca1b86SEd Tanous             [asyncResp](const boost::system::error_code ec,
1088b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
1089b9d36b47SEd Tanous                             subtreePath) {
1090a3316fc6SZhikuiRen             if (ec)
1091a3316fc6SZhikuiRen             {
1092a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
1093a3316fc6SZhikuiRen                 return;
1094a3316fc6SZhikuiRen             }
1095a3316fc6SZhikuiRen 
109655f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
1097a3316fc6SZhikuiRen             {
1098a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
1099a3316fc6SZhikuiRen                 {
110023a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
1101a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
1102613dabeaSEd Tanous                     nlohmann::json::object_t member;
1103613dabeaSEd Tanous                     member["@odata.id"] =
1104613dabeaSEd Tanous                         "/redfish/v1/Systems/system/LogServices/PostCodes";
1105613dabeaSEd Tanous 
1106613dabeaSEd Tanous                     logServiceArrayLocal.push_back(std::move(member));
1107613dabeaSEd Tanous 
110845ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
110923a21a1cSEd Tanous                         logServiceArrayLocal.size();
1110a3316fc6SZhikuiRen                     return;
1111a3316fc6SZhikuiRen                 }
1112a3316fc6SZhikuiRen             }
1113a3316fc6SZhikuiRen             },
1114a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
1115a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
111645ca1b86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
111745ca1b86SEd Tanous             std::array<const char*, 1>{postCodeIface});
11187e860f15SJohn Edward Broadbent         });
1119c4bf6374SJason M. Bills }
1120c4bf6374SJason M. Bills 
11217e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1122c4bf6374SJason M. Bills {
112322d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/")
1124ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1125002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1126002d39b4SEd Tanous             [&app](const crow::Request& req,
112722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112822d268cbSEd Tanous                    const std::string& systemName) {
11293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
113045ca1b86SEd Tanous         {
113145ca1b86SEd Tanous             return;
113245ca1b86SEd Tanous         }
113322d268cbSEd Tanous         if (systemName != "system")
113422d268cbSEd Tanous         {
113522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
113622d268cbSEd Tanous                                        systemName);
113722d268cbSEd Tanous             return;
113822d268cbSEd Tanous         }
1139c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1140029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
1141c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1142c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
1143c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
1144002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
1145c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
1146c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
11477c8c4058STejas Patil 
11487c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
11492b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
11507c8c4058STejas Patil 
11517c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
11527c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
11537c8c4058STejas Patil             redfishDateTimeOffset.second;
11547c8c4058STejas Patil 
11551476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
11561476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1157e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1158e7d6c8b2SGunnar Mills 
11590fda0f12SGeorge Liu             {"target",
11600fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
11617e860f15SJohn Edward Broadbent         });
1162489640c6SJason M. Bills }
1163489640c6SJason M. Bills 
11647e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1165489640c6SJason M. Bills {
11664978b63fSJason M. Bills     BMCWEB_ROUTE(
11674978b63fSJason M. Bills         app,
116822d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
1169432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
11707e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
117145ca1b86SEd Tanous             [&app](const crow::Request& req,
117222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
117322d268cbSEd Tanous                    const std::string& systemName) {
11743ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
117545ca1b86SEd Tanous         {
117645ca1b86SEd Tanous             return;
117745ca1b86SEd Tanous         }
117822d268cbSEd Tanous         if (systemName != "system")
117922d268cbSEd Tanous         {
118022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
118122d268cbSEd Tanous                                        systemName);
118222d268cbSEd Tanous             return;
118322d268cbSEd Tanous         }
1184489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1185489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1186489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1187489640c6SJason M. Bills         {
1188489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1189489640c6SJason M. Bills             {
1190489640c6SJason M. Bills                 std::error_code ec;
1191489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1192489640c6SJason M. Bills             }
1193489640c6SJason M. Bills         }
1194489640c6SJason M. Bills 
1195489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1196489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1197489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1198489640c6SJason M. Bills             if (ec)
1199489640c6SJason M. Bills             {
1200002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1201489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1202489640c6SJason M. Bills                 return;
1203489640c6SJason M. Bills             }
1204489640c6SJason M. Bills 
1205489640c6SJason M. Bills             messages::success(asyncResp->res);
1206489640c6SJason M. Bills             },
1207489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1208002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1209002d39b4SEd Tanous             "replace");
12107e860f15SJohn Edward Broadbent         });
1211c4bf6374SJason M. Bills }
1212c4bf6374SJason M. Bills 
1213ac992cdeSJason M. Bills enum class LogParseError
1214ac992cdeSJason M. Bills {
1215ac992cdeSJason M. Bills     success,
1216ac992cdeSJason M. Bills     parseFailed,
1217ac992cdeSJason M. Bills     messageIdNotInRegistry,
1218ac992cdeSJason M. Bills };
1219ac992cdeSJason M. Bills 
1220ac992cdeSJason M. Bills static LogParseError
1221ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1222b5a76932SEd Tanous                           const std::string& logEntry,
1223de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1224c4bf6374SJason M. Bills {
122595820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1226cd225da8SJason M. Bills     // First get the Timestamp
1227f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1228cd225da8SJason M. Bills     if (space == std::string::npos)
122995820184SJason M. Bills     {
1230ac992cdeSJason M. Bills         return LogParseError::parseFailed;
123195820184SJason M. Bills     }
1232cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1233cd225da8SJason M. Bills     // Then get the log contents
1234f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1235cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1236cd225da8SJason M. Bills     {
1237ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1238cd225da8SJason M. Bills     }
1239cd225da8SJason M. Bills     std::string_view entry(logEntry);
1240cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1241cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1242cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1243cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1244cd225da8SJason M. Bills                  boost::token_compress_on);
1245cd225da8SJason M. Bills     // We need at least a MessageId to be valid
124626f6976fSEd Tanous     if (logEntryFields.empty())
1247cd225da8SJason M. Bills     {
1248ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1249cd225da8SJason M. Bills     }
1250cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
125195820184SJason M. Bills 
12524851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1253fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1254c4bf6374SJason M. Bills 
125554417b02SSui Chen     if (message == nullptr)
1256c4bf6374SJason M. Bills     {
125754417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1258ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1259c4bf6374SJason M. Bills     }
1260c4bf6374SJason M. Bills 
126154417b02SSui Chen     std::string msg = message->message;
126254417b02SSui Chen 
126315a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
126426702d01SEd Tanous     std::span<std::string> messageArgs;
126515a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
126615a86ff6SJason M. Bills     {
126715a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
126815a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
126915a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
127015a86ff6SJason M. Bills         if (!messageArgsStart.empty())
127115a86ff6SJason M. Bills         {
127215a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
127315a86ff6SJason M. Bills         }
127415a86ff6SJason M. Bills 
127523a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1276c4bf6374SJason M. Bills 
12774851d45dSJason M. Bills         // Fill the MessageArgs into the Message
127895820184SJason M. Bills         int i = 0;
127995820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
12804851d45dSJason M. Bills         {
128195820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
12824851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
12834851d45dSJason M. Bills             if (argPos != std::string::npos)
12844851d45dSJason M. Bills             {
128595820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
12864851d45dSJason M. Bills             }
12874851d45dSJason M. Bills         }
128815a86ff6SJason M. Bills     }
12894851d45dSJason M. Bills 
129095820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
129195820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
129295820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1293f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1294f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
129595820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1296c4bf6374SJason M. Bills     {
129795820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1298c4bf6374SJason M. Bills     }
1299c4bf6374SJason M. Bills 
1300c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
13019c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
130284afc48bSJason M. Bills     logEntryJson["@odata.id"] =
130384afc48bSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
130484afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
130584afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
130684afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
130784afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
130884afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
130984afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
131084afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
131184afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1312ac992cdeSJason M. Bills     return LogParseError::success;
1313c4bf6374SJason M. Bills }
1314c4bf6374SJason M. Bills 
13157e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1316c4bf6374SJason M. Bills {
131722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
13188b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1319002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1320002d39b4SEd Tanous             [&app](const crow::Request& req,
132122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
132222d268cbSEd Tanous                    const std::string& systemName) {
1323c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1324c937d2bfSEd Tanous             .canDelegateTop = true,
1325c937d2bfSEd Tanous             .canDelegateSkip = true,
1326c937d2bfSEd Tanous         };
1327c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1328c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
13293ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1330c4bf6374SJason M. Bills         {
1331c4bf6374SJason M. Bills             return;
1332c4bf6374SJason M. Bills         }
133322d268cbSEd Tanous         if (systemName != "system")
133422d268cbSEd Tanous         {
133522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
133622d268cbSEd Tanous                                        systemName);
133722d268cbSEd Tanous             return;
133822d268cbSEd Tanous         }
133922d268cbSEd Tanous 
13405143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
13413648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
13423648c8beSEd Tanous 
13437e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13447e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1345c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1346c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1347c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1348029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1349c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1350c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1351c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1352cb92c03bSAndrew Geissler 
13534978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1354c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
13557e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
13567e860f15SJohn Edward Broadbent         // entry
135795820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
135895820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1359b01bf299SEd Tanous         uint64_t entryCount = 0;
1360cd225da8SJason M. Bills         std::string logEntry;
136195820184SJason M. Bills 
13627e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
13637e860f15SJohn Edward Broadbent         // backwards
1364002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1365002d39b4SEd Tanous              it++)
1366c4bf6374SJason M. Bills         {
1367cd225da8SJason M. Bills             std::ifstream logStream(*it);
136895820184SJason M. Bills             if (!logStream.is_open())
1369c4bf6374SJason M. Bills             {
1370c4bf6374SJason M. Bills                 continue;
1371c4bf6374SJason M. Bills             }
1372c4bf6374SJason M. Bills 
1373e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1374e85d6b16SJason M. Bills             bool firstEntry = true;
137595820184SJason M. Bills             while (std::getline(logStream, logEntry))
137695820184SJason M. Bills             {
1377c4bf6374SJason M. Bills                 std::string idStr;
1378e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1379c4bf6374SJason M. Bills                 {
1380c4bf6374SJason M. Bills                     continue;
1381c4bf6374SJason M. Bills                 }
1382e85d6b16SJason M. Bills                 firstEntry = false;
1383e85d6b16SJason M. Bills 
1384de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1385ac992cdeSJason M. Bills                 LogParseError status =
1386ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1387ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1388ac992cdeSJason M. Bills                 {
1389ac992cdeSJason M. Bills                     continue;
1390ac992cdeSJason M. Bills                 }
1391ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1392c4bf6374SJason M. Bills                 {
1393c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1394c4bf6374SJason M. Bills                     return;
1395c4bf6374SJason M. Bills                 }
1396de703c5dSJason M. Bills 
1397de703c5dSJason M. Bills                 entryCount++;
1398de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1399de703c5dSJason M. Bills                 // start) and top (number of entries to display)
14003648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1401de703c5dSJason M. Bills                 {
1402de703c5dSJason M. Bills                     continue;
1403de703c5dSJason M. Bills                 }
1404de703c5dSJason M. Bills 
1405de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1406c4bf6374SJason M. Bills             }
140795820184SJason M. Bills         }
1408c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
14093648c8beSEd Tanous         if (skip + top < entryCount)
1410c4bf6374SJason M. Bills         {
1411c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
14124978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
14133648c8beSEd Tanous                 std::to_string(skip + top);
1414c4bf6374SJason M. Bills         }
14157e860f15SJohn Edward Broadbent         });
1416897967deSJason M. Bills }
1417897967deSJason M. Bills 
14187e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1419897967deSJason M. Bills {
14207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
142122d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1422ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
14237e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
142445ca1b86SEd Tanous             [&app](const crow::Request& req,
14257e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
142622d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
14273ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
142845ca1b86SEd Tanous         {
142945ca1b86SEd Tanous             return;
143045ca1b86SEd Tanous         }
143122d268cbSEd Tanous 
143222d268cbSEd Tanous         if (systemName != "system")
143322d268cbSEd Tanous         {
143422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
143522d268cbSEd Tanous                                        systemName);
143622d268cbSEd Tanous             return;
143722d268cbSEd Tanous         }
143822d268cbSEd Tanous 
14397e860f15SJohn Edward Broadbent         const std::string& targetID = param;
14408d1b46d7Szhanghch05 
14417e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
14427e860f15SJohn Edward Broadbent         // entry to find the target entry
1443897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1444897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1445897967deSJason M. Bills         std::string logEntry;
1446897967deSJason M. Bills 
14477e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
14487e860f15SJohn Edward Broadbent         // backwards
1449002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1450002d39b4SEd Tanous              it++)
1451897967deSJason M. Bills         {
1452897967deSJason M. Bills             std::ifstream logStream(*it);
1453897967deSJason M. Bills             if (!logStream.is_open())
1454897967deSJason M. Bills             {
1455897967deSJason M. Bills                 continue;
1456897967deSJason M. Bills             }
1457897967deSJason M. Bills 
1458897967deSJason M. Bills             // Reset the unique ID on the first entry
1459897967deSJason M. Bills             bool firstEntry = true;
1460897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1461897967deSJason M. Bills             {
1462897967deSJason M. Bills                 std::string idStr;
1463897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1464897967deSJason M. Bills                 {
1465897967deSJason M. Bills                     continue;
1466897967deSJason M. Bills                 }
1467897967deSJason M. Bills                 firstEntry = false;
1468897967deSJason M. Bills 
1469897967deSJason M. Bills                 if (idStr == targetID)
1470897967deSJason M. Bills                 {
1471de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1472ac992cdeSJason M. Bills                     LogParseError status =
1473ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1474ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1475897967deSJason M. Bills                     {
1476897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1477897967deSJason M. Bills                         return;
1478897967deSJason M. Bills                     }
1479d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1480897967deSJason M. Bills                     return;
1481897967deSJason M. Bills                 }
1482897967deSJason M. Bills             }
1483897967deSJason M. Bills         }
1484897967deSJason M. Bills         // Requested ID was not found
14859db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
14867e860f15SJohn Edward Broadbent         });
148708a4e4b5SAnthony Wilson }
148808a4e4b5SAnthony Wilson 
14897e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
149008a4e4b5SAnthony Wilson {
149122d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/")
1492ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1493002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1494002d39b4SEd Tanous             [&app](const crow::Request& req,
149522d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
149622d268cbSEd Tanous                    const std::string& systemName) {
14973ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
149845ca1b86SEd Tanous         {
149945ca1b86SEd Tanous             return;
150045ca1b86SEd Tanous         }
150122d268cbSEd Tanous         if (systemName != "system")
150222d268cbSEd Tanous         {
150322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
150422d268cbSEd Tanous                                        systemName);
150522d268cbSEd Tanous             return;
150622d268cbSEd Tanous         }
150722d268cbSEd Tanous 
15087e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
15097e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
151008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
151108a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
151208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
151308a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
151408a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
151508a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
151608a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
151708a4e4b5SAnthony Wilson 
1518cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1519cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1520cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1521cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1522914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1523cb92c03bSAndrew Geissler             if (ec)
1524cb92c03bSAndrew Geissler             {
1525cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1526cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1527002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1528cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1529cb92c03bSAndrew Geissler                 return;
1530cb92c03bSAndrew Geissler             }
1531002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1532cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
15339eb808c1SEd Tanous             for (const auto& objectPath : resp)
1534cb92c03bSAndrew Geissler             {
1535914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1536c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1537c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1538914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1539914e2d5dSEd Tanous                 const std::string* message = nullptr;
1540914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
15419c11a172SVijay Lobo                 const std::string* resolution = nullptr;
154275710de2SXiaochao Ma                 bool resolved = false;
15439eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1544f86bb901SAdriana Kobylak                 {
1545f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1546f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1547f86bb901SAdriana Kobylak                     {
1548002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1549cb92c03bSAndrew Geissler                         {
1550cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1551cb92c03bSAndrew Geissler                             {
1552002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1553cb92c03bSAndrew Geissler                             }
1554cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1555cb92c03bSAndrew Geissler                             {
1556002d39b4SEd Tanous                                 timestamp =
1557002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15587e860f15SJohn Edward Broadbent                             }
1559002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
15607e860f15SJohn Edward Broadbent                             {
1561002d39b4SEd Tanous                                 updateTimestamp =
1562002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
15637e860f15SJohn Edward Broadbent                             }
15647e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
15657e860f15SJohn Edward Broadbent                             {
15667e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
15677e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15687e860f15SJohn Edward Broadbent                             }
15699c11a172SVijay Lobo                             else if (propertyMap.first == "Resolution")
15709c11a172SVijay Lobo                             {
15719c11a172SVijay Lobo                                 resolution = std::get_if<std::string>(
15729c11a172SVijay Lobo                                     &propertyMap.second);
15739c11a172SVijay Lobo                             }
15747e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
15757e860f15SJohn Edward Broadbent                             {
15767e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
15777e860f15SJohn Edward Broadbent                                     &propertyMap.second);
15787e860f15SJohn Edward Broadbent                             }
15797e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
15807e860f15SJohn Edward Broadbent                             {
1581914e2d5dSEd Tanous                                 const bool* resolveptr =
1582002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
15837e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
15847e860f15SJohn Edward Broadbent                                 {
1585002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
15867e860f15SJohn Edward Broadbent                                     return;
15877e860f15SJohn Edward Broadbent                                 }
15887e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
15897e860f15SJohn Edward Broadbent                             }
15907e860f15SJohn Edward Broadbent                         }
15917e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
15927e860f15SJohn Edward Broadbent                             severity == nullptr)
15937e860f15SJohn Edward Broadbent                         {
15947e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
15957e860f15SJohn Edward Broadbent                             return;
15967e860f15SJohn Edward Broadbent                         }
15977e860f15SJohn Edward Broadbent                     }
15987e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
15997e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
16007e860f15SJohn Edward Broadbent                     {
1601002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
16027e860f15SJohn Edward Broadbent                         {
16037e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
16047e860f15SJohn Edward Broadbent                             {
16057e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
16067e860f15SJohn Edward Broadbent                                     &propertyMap.second);
16077e860f15SJohn Edward Broadbent                             }
16087e860f15SJohn Edward Broadbent                         }
16097e860f15SJohn Edward Broadbent                     }
16107e860f15SJohn Edward Broadbent                 }
16117e860f15SJohn Edward Broadbent                 // Object path without the
16127e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
16137e860f15SJohn Edward Broadbent                 // and continue.
16147e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1615c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1616c419c759SEd Tanous                     updateTimestamp == nullptr)
16177e860f15SJohn Edward Broadbent                 {
16187e860f15SJohn Edward Broadbent                     continue;
16197e860f15SJohn Edward Broadbent                 }
16207e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
16217e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
16229c11a172SVijay Lobo                 thisEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
16237e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
16240fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
16257e860f15SJohn Edward Broadbent                     std::to_string(*id);
16267e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
16277e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
16287e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
16297e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
16309c11a172SVijay Lobo                 if ((resolution != nullptr) && (!(*resolution).empty()))
16319c11a172SVijay Lobo                 {
16329c11a172SVijay Lobo                     thisEntry["Resolution"] = *resolution;
16339c11a172SVijay Lobo                 }
16347e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
16357e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
16367e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
16377e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
16382b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*timestamp);
16397e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
16402b82937eSEd Tanous                     redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
16417e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
16427e860f15SJohn Edward Broadbent                 {
16437e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
16440fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
16457e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
16467e860f15SJohn Edward Broadbent                 }
16477e860f15SJohn Edward Broadbent             }
1648002d39b4SEd Tanous             std::sort(
1649002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1650002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
16517e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
16527e860f15SJohn Edward Broadbent                 });
16537e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
16547e860f15SJohn Edward Broadbent                 entriesArray.size();
16557e860f15SJohn Edward Broadbent             },
16567e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
16577e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
16587e860f15SJohn Edward Broadbent         });
16597e860f15SJohn Edward Broadbent }
16607e860f15SJohn Edward Broadbent 
16617e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
16627e860f15SJohn Edward Broadbent {
16637e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
166422d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1665ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1666002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1667002d39b4SEd Tanous             [&app](const crow::Request& req,
16687e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
166922d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
16703ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16717e860f15SJohn Edward Broadbent         {
167245ca1b86SEd Tanous             return;
167345ca1b86SEd Tanous         }
167422d268cbSEd Tanous         if (systemName != "system")
167522d268cbSEd Tanous         {
167622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
167722d268cbSEd Tanous                                        systemName);
167822d268cbSEd Tanous             return;
167922d268cbSEd Tanous         }
168022d268cbSEd Tanous 
16817e860f15SJohn Edward Broadbent         std::string entryID = param;
16827e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
16837e860f15SJohn Edward Broadbent 
16847e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
16857e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
1686d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
1687d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, "xyz.openbmc_project.Logging",
1688d1bde9e5SKrzysztof Grobelny             "/xyz/openbmc_project/logging/entry/" + entryID, "",
1689002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1690b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
16917e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
16927e860f15SJohn Edward Broadbent             {
1693d1bde9e5SKrzysztof Grobelny                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1694d1bde9e5SKrzysztof Grobelny                                            entryID);
16957e860f15SJohn Edward Broadbent                 return;
16967e860f15SJohn Edward Broadbent             }
16977e860f15SJohn Edward Broadbent             if (ec)
16987e860f15SJohn Edward Broadbent             {
16990fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1700002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
17017e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
17027e860f15SJohn Edward Broadbent                 return;
17037e860f15SJohn Edward Broadbent             }
1704914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1705c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1706c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1707914e2d5dSEd Tanous             const std::string* severity = nullptr;
1708914e2d5dSEd Tanous             const std::string* message = nullptr;
1709914e2d5dSEd Tanous             const std::string* filePath = nullptr;
17109c11a172SVijay Lobo             const std::string* resolution = nullptr;
17117e860f15SJohn Edward Broadbent             bool resolved = false;
17127e860f15SJohn Edward Broadbent 
1713d1bde9e5SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
1714d1bde9e5SKrzysztof Grobelny                 dbus_utils::UnpackErrorPrinter(), resp, "Id", id, "Timestamp",
1715d1bde9e5SKrzysztof Grobelny                 timestamp, "UpdateTimestamp", updateTimestamp, "Severity",
17169c11a172SVijay Lobo                 severity, "Message", message, "Resolved", resolved,
17179c11a172SVijay Lobo                 "Resolution", resolution, "Path", filePath);
1718d1bde9e5SKrzysztof Grobelny 
1719d1bde9e5SKrzysztof Grobelny             if (!success)
172075710de2SXiaochao Ma             {
172175710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
172275710de2SXiaochao Ma                 return;
172375710de2SXiaochao Ma             }
1724d1bde9e5SKrzysztof Grobelny 
1725002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
1726002d39b4SEd Tanous                 timestamp == nullptr || updateTimestamp == nullptr)
1727f86bb901SAdriana Kobylak             {
1728ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1729271584abSEd Tanous                 return;
1730271584abSEd Tanous             }
1731f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
17329c11a172SVijay Lobo                 "#LogEntry.v1_9_0.LogEntry";
1733f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
17340fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1735f86bb901SAdriana Kobylak                 std::to_string(*id);
173645ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1737f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1738f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1739f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
17409c11a172SVijay Lobo             if ((resolution != nullptr) && (!(*resolution).empty()))
17419c11a172SVijay Lobo             {
17429c11a172SVijay Lobo                 asyncResp->res.jsonValue["Resolution"] = *resolution;
17439c11a172SVijay Lobo             }
1744f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1745f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1746f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1747f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
17482b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*timestamp);
1749f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
17502b82937eSEd Tanous                 redfish::time_utils::getDateTimeUintMs(*updateTimestamp);
1751f86bb901SAdriana Kobylak             if (filePath != nullptr)
1752f86bb901SAdriana Kobylak             {
1753f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1754e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1755e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1756f86bb901SAdriana Kobylak             }
1757d1bde9e5SKrzysztof Grobelny             });
17587e860f15SJohn Edward Broadbent         });
1759336e96c6SChicago Duan 
17607e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
176122d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1762ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
17637e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
176445ca1b86SEd Tanous             [&app](const crow::Request& req,
17657e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
176622d268cbSEd Tanous                    const std::string& systemName, const std::string& entryId) {
17673ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
176845ca1b86SEd Tanous         {
176945ca1b86SEd Tanous             return;
177045ca1b86SEd Tanous         }
177122d268cbSEd Tanous         if (systemName != "system")
177222d268cbSEd Tanous         {
177322d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
177422d268cbSEd Tanous                                        systemName);
177522d268cbSEd Tanous             return;
177622d268cbSEd Tanous         }
177775710de2SXiaochao Ma         std::optional<bool> resolved;
177875710de2SXiaochao Ma 
177915ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
17807e860f15SJohn Edward Broadbent                                       resolved))
178175710de2SXiaochao Ma         {
178275710de2SXiaochao Ma             return;
178375710de2SXiaochao Ma         }
178475710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
178575710de2SXiaochao Ma 
178675710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
17874f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
178875710de2SXiaochao Ma             if (ec)
178975710de2SXiaochao Ma             {
179075710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
179175710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
179275710de2SXiaochao Ma                 return;
179375710de2SXiaochao Ma             }
179475710de2SXiaochao Ma             },
179575710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
179675710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
179775710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
179875710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1799168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
18007e860f15SJohn Edward Broadbent         });
180175710de2SXiaochao Ma 
18027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
180322d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/")
1804ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1805ed398213SEd Tanous 
1806002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1807002d39b4SEd Tanous             [&app](const crow::Request& req,
1808002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
180922d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18103ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1811336e96c6SChicago Duan         {
181245ca1b86SEd Tanous             return;
181345ca1b86SEd Tanous         }
181422d268cbSEd Tanous         if (systemName != "system")
181522d268cbSEd Tanous         {
181622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
181722d268cbSEd Tanous                                        systemName);
181822d268cbSEd Tanous             return;
181922d268cbSEd Tanous         }
1820336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1821336e96c6SChicago Duan 
18227e860f15SJohn Edward Broadbent         std::string entryID = param;
1823336e96c6SChicago Duan 
1824336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1825336e96c6SChicago Duan 
1826336e96c6SChicago Duan         // Process response from Logging service.
1827002d39b4SEd Tanous         auto respHandler =
1828002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1829002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1830336e96c6SChicago Duan             if (ec)
1831336e96c6SChicago Duan             {
18323de8d8baSGeorge Liu                 if (ec.value() == EBADR)
18333de8d8baSGeorge Liu                 {
183445ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
183545ca1b86SEd Tanous                                                entryID);
18363de8d8baSGeorge Liu                     return;
18373de8d8baSGeorge Liu                 }
1838336e96c6SChicago Duan                 // TODO Handle for specific error code
18390fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
18400fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1841336e96c6SChicago Duan                     << ec;
1842336e96c6SChicago Duan                 asyncResp->res.result(
1843336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1844336e96c6SChicago Duan                 return;
1845336e96c6SChicago Duan             }
1846336e96c6SChicago Duan 
1847336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1848336e96c6SChicago Duan         };
1849336e96c6SChicago Duan 
1850336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1851336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1852336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1853336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1854336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
18557e860f15SJohn Edward Broadbent         });
1856400fd1fbSAdriana Kobylak }
1857400fd1fbSAdriana Kobylak 
18587e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1859400fd1fbSAdriana Kobylak {
18600fda0f12SGeorge Liu     BMCWEB_ROUTE(
18610fda0f12SGeorge Liu         app,
186222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Entries/<str>/attachment")
1863ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
18647e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
186545ca1b86SEd Tanous             [&app](const crow::Request& req,
18667e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
186722d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
18683ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
18697e860f15SJohn Edward Broadbent         {
187045ca1b86SEd Tanous             return;
187145ca1b86SEd Tanous         }
187299351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
187399351cd8SEd Tanous                 req.getHeaderValue("Accept"),
18744a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
1875400fd1fbSAdriana Kobylak         {
1876002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1877400fd1fbSAdriana Kobylak             return;
1878400fd1fbSAdriana Kobylak         }
187922d268cbSEd Tanous         if (systemName != "system")
188022d268cbSEd Tanous         {
188122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
188222d268cbSEd Tanous                                        systemName);
188322d268cbSEd Tanous             return;
188422d268cbSEd Tanous         }
1885400fd1fbSAdriana Kobylak 
18867e860f15SJohn Edward Broadbent         std::string entryID = param;
1887400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1888400fd1fbSAdriana Kobylak 
1889400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1890002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1891400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1892400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1893400fd1fbSAdriana Kobylak             {
1894002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1895002d39b4SEd Tanous                                            entryID);
1896400fd1fbSAdriana Kobylak                 return;
1897400fd1fbSAdriana Kobylak             }
1898400fd1fbSAdriana Kobylak             if (ec)
1899400fd1fbSAdriana Kobylak             {
1900400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1901400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1902400fd1fbSAdriana Kobylak                 return;
1903400fd1fbSAdriana Kobylak             }
1904400fd1fbSAdriana Kobylak 
1905400fd1fbSAdriana Kobylak             int fd = -1;
1906400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1907400fd1fbSAdriana Kobylak             if (fd == -1)
1908400fd1fbSAdriana Kobylak             {
1909400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1910400fd1fbSAdriana Kobylak                 return;
1911400fd1fbSAdriana Kobylak             }
1912400fd1fbSAdriana Kobylak 
1913400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1914400fd1fbSAdriana Kobylak             if (size == -1)
1915400fd1fbSAdriana Kobylak             {
1916400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1917400fd1fbSAdriana Kobylak                 return;
1918400fd1fbSAdriana Kobylak             }
1919400fd1fbSAdriana Kobylak 
1920400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1921400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1922400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1923400fd1fbSAdriana Kobylak             {
1924002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1925400fd1fbSAdriana Kobylak                                  << maxFileSize;
1926400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1927400fd1fbSAdriana Kobylak                 return;
1928400fd1fbSAdriana Kobylak             }
1929400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1930400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1931400fd1fbSAdriana Kobylak             if (rc == -1)
1932400fd1fbSAdriana Kobylak             {
1933400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1934400fd1fbSAdriana Kobylak                 return;
1935400fd1fbSAdriana Kobylak             }
1936400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1937400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1938400fd1fbSAdriana Kobylak             {
1939400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1940400fd1fbSAdriana Kobylak                 return;
1941400fd1fbSAdriana Kobylak             }
1942400fd1fbSAdriana Kobylak             close(fd);
1943400fd1fbSAdriana Kobylak 
1944400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1945002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1946400fd1fbSAdriana Kobylak 
1947d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
1948400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1949d9f6c621SEd Tanous             asyncResp->res.addHeader(
1950d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
1951400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1952400fd1fbSAdriana Kobylak             },
1953400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1954400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1955400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
19567e860f15SJohn Edward Broadbent         });
19571da66f75SEd Tanous }
19581da66f75SEd Tanous 
1959b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1960b7028ebfSSpencer Ku 
1961b7028ebfSSpencer Ku inline bool
1962b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1963b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1964b7028ebfSSpencer Ku {
1965b7028ebfSSpencer Ku     std::error_code ec;
1966b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1967b7028ebfSSpencer Ku     if (ec)
1968b7028ebfSSpencer Ku     {
1969b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1970b7028ebfSSpencer Ku         return false;
1971b7028ebfSSpencer Ku     }
1972b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1973b7028ebfSSpencer Ku     {
1974b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1975b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1976b7028ebfSSpencer Ku         // path
197711ba3979SEd Tanous         if (filename.starts_with("log"))
1978b7028ebfSSpencer Ku         {
1979b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1980b7028ebfSSpencer Ku         }
1981b7028ebfSSpencer Ku     }
1982b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1983b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1984b7028ebfSSpencer Ku     // descending order.
1985b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1986b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1987b7028ebfSSpencer Ku 
1988b7028ebfSSpencer Ku     return true;
1989b7028ebfSSpencer Ku }
1990b7028ebfSSpencer Ku 
199102cad96eSEd Tanous inline bool getHostLoggerEntries(
199202cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
199302cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
1994b7028ebfSSpencer Ku {
1995b7028ebfSSpencer Ku     GzFileReader logFile;
1996b7028ebfSSpencer Ku 
1997b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1998b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1999b7028ebfSSpencer Ku     {
2000b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
2001b7028ebfSSpencer Ku         {
2002b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
2003b7028ebfSSpencer Ku             return false;
2004b7028ebfSSpencer Ku         }
2005b7028ebfSSpencer Ku     }
2006b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
2007b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
2008b7028ebfSSpencer Ku     if (!lastMessage.empty())
2009b7028ebfSSpencer Ku     {
2010b7028ebfSSpencer Ku         logCount++;
2011b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
2012b7028ebfSSpencer Ku         {
2013b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
2014b7028ebfSSpencer Ku         }
2015b7028ebfSSpencer Ku     }
2016b7028ebfSSpencer Ku     return true;
2017b7028ebfSSpencer Ku }
2018b7028ebfSSpencer Ku 
2019b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
2020b7028ebfSSpencer Ku                                     const std::string& msg,
20216d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
2022b7028ebfSSpencer Ku {
2023b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
20249c11a172SVijay Lobo     logEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
20256d6574c9SJason M. Bills     logEntryJson["@odata.id"] =
2026b7028ebfSSpencer Ku         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
20276d6574c9SJason M. Bills         logEntryID;
20286d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
20296d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
20306d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
20316d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
20326d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
20336d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
2034b7028ebfSSpencer Ku }
2035b7028ebfSSpencer Ku 
2036b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
2037b7028ebfSSpencer Ku {
203822d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/")
2039b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
20401476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
20411476687dSEd Tanous             [&app](const crow::Request& req,
204222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
204322d268cbSEd Tanous                    const std::string& systemName) {
20443ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
204545ca1b86SEd Tanous         {
204645ca1b86SEd Tanous             return;
204745ca1b86SEd Tanous         }
204822d268cbSEd Tanous         if (systemName != "system")
204922d268cbSEd Tanous         {
205022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
205122d268cbSEd Tanous                                        systemName);
205222d268cbSEd Tanous             return;
205322d268cbSEd Tanous         }
2054b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2055b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
2056b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2057b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
2058b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
2059b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
2060b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
20611476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
20621476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2063b7028ebfSSpencer Ku         });
2064b7028ebfSSpencer Ku }
2065b7028ebfSSpencer Ku 
2066b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
2067b7028ebfSSpencer Ku {
2068b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
206922d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/")
2070b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2071002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2072002d39b4SEd Tanous             [&app](const crow::Request& req,
207322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
207422d268cbSEd Tanous                    const std::string& systemName) {
2075c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2076c937d2bfSEd Tanous             .canDelegateTop = true,
2077c937d2bfSEd Tanous             .canDelegateSkip = true,
2078c937d2bfSEd Tanous         };
2079c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2080c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
20813ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2082b7028ebfSSpencer Ku         {
2083b7028ebfSSpencer Ku             return;
2084b7028ebfSSpencer Ku         }
208522d268cbSEd Tanous         if (systemName != "system")
208622d268cbSEd Tanous         {
208722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
208822d268cbSEd Tanous                                        systemName);
208922d268cbSEd Tanous             return;
209022d268cbSEd Tanous         }
2091b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
2092b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
2093b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
2094b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
2095b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
2096b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
2097b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
20980fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2099b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
2100b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
2101b7028ebfSSpencer Ku 
2102b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2103b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2104b7028ebfSSpencer Ku         {
2105b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2106b7028ebfSSpencer Ku             return;
2107b7028ebfSSpencer Ku         }
21083648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
21093648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21105143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
2111b7028ebfSSpencer Ku         size_t logCount = 0;
2112b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
2113b7028ebfSSpencer Ku         // control by skip and top.
2114b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
21153648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
21163648c8beSEd Tanous                                   logCount))
2117b7028ebfSSpencer Ku         {
2118b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2119b7028ebfSSpencer Ku             return;
2120b7028ebfSSpencer Ku         }
2121b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
2122b7028ebfSSpencer Ku         // log count
212326f6976fSEd Tanous         if (logEntries.empty())
2124b7028ebfSSpencer Ku         {
2125b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2126b7028ebfSSpencer Ku             return;
2127b7028ebfSSpencer Ku         }
212826f6976fSEd Tanous         if (!logEntries.empty())
2129b7028ebfSSpencer Ku         {
2130b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
2131b7028ebfSSpencer Ku             {
21326d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
21333648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
21343648c8beSEd Tanous                                         hostLogEntry);
21356d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
2136b7028ebfSSpencer Ku             }
2137b7028ebfSSpencer Ku 
2138b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
21393648c8beSEd Tanous             if (skip + top < logCount)
2140b7028ebfSSpencer Ku             {
2141b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
21420fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
21433648c8beSEd Tanous                     std::to_string(skip + top);
2144b7028ebfSSpencer Ku             }
2145b7028ebfSSpencer Ku         }
2146b7028ebfSSpencer Ku         });
2147b7028ebfSSpencer Ku }
2148b7028ebfSSpencer Ku 
2149b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2150b7028ebfSSpencer Ku {
2151b7028ebfSSpencer Ku     BMCWEB_ROUTE(
215222d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/HostLogger/Entries/<str>/")
2153b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2154b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
215545ca1b86SEd Tanous             [&app](const crow::Request& req,
2156b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
215722d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
21583ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
215945ca1b86SEd Tanous         {
216045ca1b86SEd Tanous             return;
216145ca1b86SEd Tanous         }
216222d268cbSEd Tanous         if (systemName != "system")
216322d268cbSEd Tanous         {
216422d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
216522d268cbSEd Tanous                                        systemName);
216622d268cbSEd Tanous             return;
216722d268cbSEd Tanous         }
2168b7028ebfSSpencer Ku         const std::string& targetID = param;
2169b7028ebfSSpencer Ku 
2170b7028ebfSSpencer Ku         uint64_t idInt = 0;
2171ca45aa3cSEd Tanous 
2172ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2173ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
2174ca45aa3cSEd Tanous 
2175ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
21769db4ba25SJiaqing Zhao         if (ec == std::errc::invalid_argument ||
21779db4ba25SJiaqing Zhao             ec == std::errc::result_out_of_range)
2178b7028ebfSSpencer Ku         {
21799db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2180b7028ebfSSpencer Ku             return;
2181b7028ebfSSpencer Ku         }
2182b7028ebfSSpencer Ku 
2183b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
2184b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2185b7028ebfSSpencer Ku         {
2186b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
2187b7028ebfSSpencer Ku             return;
2188b7028ebfSSpencer Ku         }
2189b7028ebfSSpencer Ku 
2190b7028ebfSSpencer Ku         size_t logCount = 0;
21913648c8beSEd Tanous         size_t top = 1;
2192b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
2193b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
2194b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
2195b7028ebfSSpencer Ku         // get that entry
2196002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
2197002d39b4SEd Tanous                                   logCount))
2198b7028ebfSSpencer Ku         {
2199b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
2200b7028ebfSSpencer Ku             return;
2201b7028ebfSSpencer Ku         }
2202b7028ebfSSpencer Ku 
2203b7028ebfSSpencer Ku         if (!logEntries.empty())
2204b7028ebfSSpencer Ku         {
22056d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
22066d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
22076d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
2208b7028ebfSSpencer Ku             return;
2209b7028ebfSSpencer Ku         }
2210b7028ebfSSpencer Ku 
2211b7028ebfSSpencer Ku         // Requested ID was not found
22129db4ba25SJiaqing Zhao         messages::resourceNotFound(asyncResp->res, "LogEntry", param);
2213b7028ebfSSpencer Ku         });
2214b7028ebfSSpencer Ku }
2215b7028ebfSSpencer Ku 
2216fdd26906SClaire Weinan constexpr char const* dumpManagerIface =
2217fdd26906SClaire Weinan     "xyz.openbmc_project.Collection.DeleteAll";
2218dd72e87bSClaire Weinan inline void handleBMCLogServicesCollectionGet(
2219fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
2220fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22211da66f75SEd Tanous {
22223ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
222345ca1b86SEd Tanous     {
222445ca1b86SEd Tanous         return;
222545ca1b86SEd Tanous     }
22267e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
22277e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2228e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
22291da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2230e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2231e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2232002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2233e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
22341da66f75SEd Tanous         "Collection of LogServices for this Manager";
2235002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2236c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2237fdd26906SClaire Weinan 
2238c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2239613dabeaSEd Tanous     nlohmann::json::object_t journal;
2240613dabeaSEd Tanous     journal["@odata.id"] = "/redfish/v1/Managers/bmc/LogServices/Journal";
2241613dabeaSEd Tanous     logServiceArray.push_back(std::move(journal));
2242c4bf6374SJason M. Bills #endif
2243fdd26906SClaire Weinan 
2244fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2245fdd26906SClaire Weinan 
2246fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2247fdd26906SClaire Weinan     auto respHandler =
2248fdd26906SClaire Weinan         [asyncResp](
2249fdd26906SClaire Weinan             const boost::system::error_code ec,
2250fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2251fdd26906SClaire Weinan         if (ec)
2252fdd26906SClaire Weinan         {
2253fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2254dd72e87bSClaire Weinan                 << "handleBMCLogServicesCollectionGet respHandler got error "
2255fdd26906SClaire Weinan                 << ec;
2256fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2257fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2258fdd26906SClaire Weinan             return;
2259fdd26906SClaire Weinan         }
2260fdd26906SClaire Weinan 
2261fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2262fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2263fdd26906SClaire Weinan 
2264fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2265fdd26906SClaire Weinan         {
2266fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2267fdd26906SClaire Weinan             {
2268613dabeaSEd Tanous                 nlohmann::json::object_t member;
2269613dabeaSEd Tanous                 member["@odata.id"] =
2270613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2271613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2272fdd26906SClaire Weinan             }
2273fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2274fdd26906SClaire Weinan             {
2275613dabeaSEd Tanous                 nlohmann::json::object_t member;
2276613dabeaSEd Tanous                 member["@odata.id"] =
2277613dabeaSEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2278613dabeaSEd Tanous                 logServiceArrayLocal.push_back(std::move(member));
2279fdd26906SClaire Weinan             }
2280fdd26906SClaire Weinan         }
2281fdd26906SClaire Weinan 
2282e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2283fdd26906SClaire Weinan             logServiceArrayLocal.size();
2284fdd26906SClaire Weinan     };
2285fdd26906SClaire Weinan 
2286fdd26906SClaire Weinan     crow::connections::systemBus->async_method_call(
2287fdd26906SClaire Weinan         respHandler, "xyz.openbmc_project.ObjectMapper",
2288fdd26906SClaire Weinan         "/xyz/openbmc_project/object_mapper",
2289fdd26906SClaire Weinan         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2290fdd26906SClaire Weinan         "/xyz/openbmc_project/dump", 0,
2291fdd26906SClaire Weinan         std::array<const char*, 1>{dumpManagerIface});
2292fdd26906SClaire Weinan #endif
2293fdd26906SClaire Weinan }
2294fdd26906SClaire Weinan 
2295fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2296fdd26906SClaire Weinan {
2297fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2298fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2299fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2300dd72e87bSClaire Weinan             std::bind_front(handleBMCLogServicesCollectionGet, std::ref(app)));
2301e1f26343SJason M. Bills }
2302e1f26343SJason M. Bills 
23037e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2304e1f26343SJason M. Bills {
23057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2306ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
230845ca1b86SEd Tanous             [&app](const crow::Request& req,
230945ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23103ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
23117e860f15SJohn Edward Broadbent         {
231245ca1b86SEd Tanous             return;
231345ca1b86SEd Tanous         }
2314e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2315e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
23160f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
23170f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2318002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2319002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2320c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
2321e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23227c8c4058STejas Patil 
23237c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
23242b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
2325002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23267c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23277c8c4058STejas Patil             redfishDateTimeOffset.second;
23287c8c4058STejas Patil 
23291476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
23301476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
23317e860f15SJohn Edward Broadbent         });
2332e1f26343SJason M. Bills }
2333e1f26343SJason M. Bills 
23343a48b3a2SJason M. Bills static int
23353a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2336e1f26343SJason M. Bills                                sd_journal* journal,
23373a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2338e1f26343SJason M. Bills {
2339e1f26343SJason M. Bills     // Get the Log Entry contents
2340e1f26343SJason M. Bills     int ret = 0;
2341e1f26343SJason M. Bills 
2342a8fe54f0SJason M. Bills     std::string message;
2343a8fe54f0SJason M. Bills     std::string_view syslogID;
2344a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2345a8fe54f0SJason M. Bills     if (ret < 0)
2346a8fe54f0SJason M. Bills     {
2347a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2348a8fe54f0SJason M. Bills                          << strerror(-ret);
2349a8fe54f0SJason M. Bills     }
2350a8fe54f0SJason M. Bills     if (!syslogID.empty())
2351a8fe54f0SJason M. Bills     {
2352a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2353a8fe54f0SJason M. Bills     }
2354a8fe54f0SJason M. Bills 
235539e77504SEd Tanous     std::string_view msg;
235616428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2357e1f26343SJason M. Bills     if (ret < 0)
2358e1f26343SJason M. Bills     {
2359e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2360e1f26343SJason M. Bills         return 1;
2361e1f26343SJason M. Bills     }
2362a8fe54f0SJason M. Bills     message += std::string(msg);
2363e1f26343SJason M. Bills 
2364e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2365271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
236616428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2367e1f26343SJason M. Bills     if (ret < 0)
2368e1f26343SJason M. Bills     {
2369e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2370e1f26343SJason M. Bills     }
2371e1f26343SJason M. Bills 
2372e1f26343SJason M. Bills     // Get the Created time from the timestamp
237316428a1aSJason M. Bills     std::string entryTimeStr;
237416428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2375e1f26343SJason M. Bills     {
237616428a1aSJason M. Bills         return 1;
2377e1f26343SJason M. Bills     }
2378e1f26343SJason M. Bills 
2379e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
23809c11a172SVijay Lobo     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
238184afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.id"] =
238284afc48bSJason M. Bills         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
238384afc48bSJason M. Bills         bmcJournalLogEntryID;
238484afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
238584afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
238684afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
238784afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
238884afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2389738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
239084afc48bSJason M. Bills                                                          : "OK";
239184afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
239284afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2393e1f26343SJason M. Bills     return 0;
2394e1f26343SJason M. Bills }
2395e1f26343SJason M. Bills 
23967e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2397e1f26343SJason M. Bills {
23987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2399ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2400002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2401002d39b4SEd Tanous             [&app](const crow::Request& req,
2402002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2403c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2404c937d2bfSEd Tanous             .canDelegateTop = true,
2405c937d2bfSEd Tanous             .canDelegateSkip = true,
2406c937d2bfSEd Tanous         };
2407c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2408c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
24093ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2410193ad2faSJason M. Bills         {
2411193ad2faSJason M. Bills             return;
2412193ad2faSJason M. Bills         }
24133648c8beSEd Tanous 
24143648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
24155143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
24163648c8beSEd Tanous 
24177e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
24187e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2419e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2420e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
24210f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
24220f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2423e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2424e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2425e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
24260fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2427e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2428e1f26343SJason M. Bills 
24297e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
24307e860f15SJohn Edward Broadbent         // unique ID for each entry
2431e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2432e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2433e1f26343SJason M. Bills         if (ret < 0)
2434e1f26343SJason M. Bills         {
2435002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2436f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2437e1f26343SJason M. Bills             return;
2438e1f26343SJason M. Bills         }
24390fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
24400fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2441e1f26343SJason M. Bills         journalTmp = nullptr;
2442b01bf299SEd Tanous         uint64_t entryCount = 0;
2443e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2444e85d6b16SJason M. Bills         bool firstEntry = true;
2445e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2446e1f26343SJason M. Bills         {
2447193ad2faSJason M. Bills             entryCount++;
24487e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
24497e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
24503648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2451193ad2faSJason M. Bills             {
2452193ad2faSJason M. Bills                 continue;
2453193ad2faSJason M. Bills             }
2454193ad2faSJason M. Bills 
245516428a1aSJason M. Bills             std::string idStr;
2456e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2457e1f26343SJason M. Bills             {
2458e1f26343SJason M. Bills                 continue;
2459e1f26343SJason M. Bills             }
2460e85d6b16SJason M. Bills             firstEntry = false;
2461e85d6b16SJason M. Bills 
24623a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2463c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2464c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2465e1f26343SJason M. Bills             {
2466f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2467e1f26343SJason M. Bills                 return;
2468e1f26343SJason M. Bills             }
24693a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2470e1f26343SJason M. Bills         }
2471193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
24723648c8beSEd Tanous         if (skip + top < entryCount)
2473193ad2faSJason M. Bills         {
2474193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
24750fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
24763648c8beSEd Tanous                 std::to_string(skip + top);
2477193ad2faSJason M. Bills         }
24787e860f15SJohn Edward Broadbent         });
2479e1f26343SJason M. Bills }
2480e1f26343SJason M. Bills 
24817e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2482e1f26343SJason M. Bills {
24837e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24847e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2485ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
24867e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
248745ca1b86SEd Tanous             [&app](const crow::Request& req,
24887e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24897e860f15SJohn Edward Broadbent                    const std::string& entryID) {
24903ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
249145ca1b86SEd Tanous         {
249245ca1b86SEd Tanous             return;
249345ca1b86SEd Tanous         }
2494e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2495e1f26343SJason M. Bills         uint64_t ts = 0;
2496271584abSEd Tanous         uint64_t index = 0;
24978d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2498e1f26343SJason M. Bills         {
249916428a1aSJason M. Bills             return;
2500e1f26343SJason M. Bills         }
2501e1f26343SJason M. Bills 
2502e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2503e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2504e1f26343SJason M. Bills         if (ret < 0)
2505e1f26343SJason M. Bills         {
2506002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2507f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2508e1f26343SJason M. Bills             return;
2509e1f26343SJason M. Bills         }
2510002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2511002d39b4SEd Tanous             journalTmp, sd_journal_close);
2512e1f26343SJason M. Bills         journalTmp = nullptr;
25137e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
25147e860f15SJohn Edward Broadbent         // index tracking the unique ID
2515af07e3f5SJason M. Bills         std::string idStr;
2516af07e3f5SJason M. Bills         bool firstEntry = true;
2517e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
25182056b6d1SManojkiran Eda         if (ret < 0)
25192056b6d1SManojkiran Eda         {
25202056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
25212056b6d1SManojkiran Eda                              << strerror(-ret);
25222056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
25232056b6d1SManojkiran Eda             return;
25242056b6d1SManojkiran Eda         }
2525271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2526e1f26343SJason M. Bills         {
2527e1f26343SJason M. Bills             sd_journal_next(journal.get());
2528af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2529af07e3f5SJason M. Bills             {
2530af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2531af07e3f5SJason M. Bills                 return;
2532af07e3f5SJason M. Bills             }
2533af07e3f5SJason M. Bills             firstEntry = false;
2534af07e3f5SJason M. Bills         }
2535c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2536af07e3f5SJason M. Bills         if (idStr != entryID)
2537c4bf6374SJason M. Bills         {
25389db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
2539c4bf6374SJason M. Bills             return;
2540c4bf6374SJason M. Bills         }
2541c4bf6374SJason M. Bills 
25423a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2543c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
25443a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2545e1f26343SJason M. Bills         {
2546f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2547e1f26343SJason M. Bills             return;
2548e1f26343SJason M. Bills         }
2549d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
25507e860f15SJohn Edward Broadbent         });
2551c9bb6861Sraviteja-b }
2552c9bb6861Sraviteja-b 
2553fdd26906SClaire Weinan inline void
2554fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2555fdd26906SClaire Weinan                        const std::string& dumpType)
2556c9bb6861Sraviteja-b {
2557fdd26906SClaire Weinan     std::string dumpPath;
2558fdd26906SClaire Weinan     std::string overWritePolicy;
2559fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2560fdd26906SClaire Weinan 
2561fdd26906SClaire Weinan     if (dumpType == "BMC")
256245ca1b86SEd Tanous     {
2563fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2564fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2565fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2566fdd26906SClaire Weinan     }
2567fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2568fdd26906SClaire Weinan     {
2569fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2570fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2571fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2572fdd26906SClaire Weinan     }
2573fdd26906SClaire Weinan     else if (dumpType == "System")
2574fdd26906SClaire Weinan     {
2575fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2576fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2577fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2578fdd26906SClaire Weinan     }
2579fdd26906SClaire Weinan     else
2580fdd26906SClaire Weinan     {
2581fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2582fdd26906SClaire Weinan                          << dumpType;
2583fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
258445ca1b86SEd Tanous         return;
258545ca1b86SEd Tanous     }
2586fdd26906SClaire Weinan 
2587fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2588fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2589c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2590fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2591fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2592fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
25937c8c4058STejas Patil 
25947c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
25952b82937eSEd Tanous         redfish::time_utils::getDateTimeOffsetNow();
25960fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25977c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25987c8c4058STejas Patil         redfishDateTimeOffset.second;
25997c8c4058STejas Patil 
2600fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2601002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2602fdd26906SClaire Weinan         dumpPath + "/Actions/LogService.ClearLog";
2603fdd26906SClaire Weinan 
2604fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2605fdd26906SClaire Weinan     {
2606002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26071476687dSEd Tanous                                 ["target"] =
2608fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2609fdd26906SClaire Weinan     }
2610c9bb6861Sraviteja-b }
2611c9bb6861Sraviteja-b 
2612fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2613fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2614fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26157e860f15SJohn Edward Broadbent {
26163ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
261745ca1b86SEd Tanous     {
261845ca1b86SEd Tanous         return;
261945ca1b86SEd Tanous     }
2620fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2621fdd26906SClaire Weinan }
2622c9bb6861Sraviteja-b 
262322d268cbSEd Tanous inline void handleLogServicesDumpServiceComputerSystemGet(
262422d268cbSEd Tanous     crow::App& app, const crow::Request& req,
262522d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
262622d268cbSEd Tanous     const std::string& chassisId)
262722d268cbSEd Tanous {
262822d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
262922d268cbSEd Tanous     {
263022d268cbSEd Tanous         return;
263122d268cbSEd Tanous     }
263222d268cbSEd Tanous     if (chassisId != "system")
263322d268cbSEd Tanous     {
263422d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
263522d268cbSEd Tanous         return;
263622d268cbSEd Tanous     }
263722d268cbSEd Tanous     getDumpServiceInfo(asyncResp, "System");
263822d268cbSEd Tanous }
263922d268cbSEd Tanous 
2640fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2641fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2642fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2643fdd26906SClaire Weinan {
2644fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2645fdd26906SClaire Weinan     {
2646fdd26906SClaire Weinan         return;
2647fdd26906SClaire Weinan     }
2648fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2649fdd26906SClaire Weinan }
2650fdd26906SClaire Weinan 
265122d268cbSEd Tanous inline void handleLogServicesDumpEntriesCollectionComputerSystemGet(
265222d268cbSEd Tanous     crow::App& app, const crow::Request& req,
265322d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
265422d268cbSEd Tanous     const std::string& chassisId)
265522d268cbSEd Tanous {
265622d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
265722d268cbSEd Tanous     {
265822d268cbSEd Tanous         return;
265922d268cbSEd Tanous     }
266022d268cbSEd Tanous     if (chassisId != "system")
266122d268cbSEd Tanous     {
266222d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
266322d268cbSEd Tanous         return;
266422d268cbSEd Tanous     }
266522d268cbSEd Tanous     getDumpEntryCollection(asyncResp, "System");
266622d268cbSEd Tanous }
266722d268cbSEd Tanous 
2668fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2669fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2670fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2671fdd26906SClaire Weinan     const std::string& dumpId)
2672fdd26906SClaire Weinan {
2673fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2674fdd26906SClaire Weinan     {
2675fdd26906SClaire Weinan         return;
2676fdd26906SClaire Weinan     }
2677fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2678fdd26906SClaire Weinan }
267922d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemGet(
268022d268cbSEd Tanous     crow::App& app, const crow::Request& req,
268122d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
268222d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
268322d268cbSEd Tanous {
268422d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
268522d268cbSEd Tanous     {
268622d268cbSEd Tanous         return;
268722d268cbSEd Tanous     }
268822d268cbSEd Tanous     if (chassisId != "system")
268922d268cbSEd Tanous     {
269022d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
269122d268cbSEd Tanous         return;
269222d268cbSEd Tanous     }
269322d268cbSEd Tanous     getDumpEntryById(asyncResp, dumpId, "System");
269422d268cbSEd Tanous }
2695fdd26906SClaire Weinan 
2696fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2697fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2698fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2699fdd26906SClaire Weinan     const std::string& dumpId)
2700fdd26906SClaire Weinan {
2701fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2702fdd26906SClaire Weinan     {
2703fdd26906SClaire Weinan         return;
2704fdd26906SClaire Weinan     }
2705fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2706fdd26906SClaire Weinan }
2707fdd26906SClaire Weinan 
270822d268cbSEd Tanous inline void handleLogServicesDumpEntryComputerSystemDelete(
270922d268cbSEd Tanous     crow::App& app, const crow::Request& req,
271022d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
271122d268cbSEd Tanous     const std::string& chassisId, const std::string& dumpId)
271222d268cbSEd Tanous {
271322d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
271422d268cbSEd Tanous     {
271522d268cbSEd Tanous         return;
271622d268cbSEd Tanous     }
271722d268cbSEd Tanous     if (chassisId != "system")
271822d268cbSEd Tanous     {
271922d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
272022d268cbSEd Tanous         return;
272122d268cbSEd Tanous     }
272222d268cbSEd Tanous     deleteDumpEntry(asyncResp, dumpId, "System");
272322d268cbSEd Tanous }
272422d268cbSEd Tanous 
2725fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2726fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2727fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2728fdd26906SClaire Weinan {
2729fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2730fdd26906SClaire Weinan     {
2731fdd26906SClaire Weinan         return;
2732fdd26906SClaire Weinan     }
2733fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2734fdd26906SClaire Weinan }
2735fdd26906SClaire Weinan 
273622d268cbSEd Tanous inline void handleLogServicesDumpCollectDiagnosticDataComputerSystemPost(
273722d268cbSEd Tanous     crow::App& app, const crow::Request& req,
273822d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
273922d268cbSEd Tanous     const std::string& chassisId)
274022d268cbSEd Tanous {
274122d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
274222d268cbSEd Tanous     {
274322d268cbSEd Tanous         return;
274422d268cbSEd Tanous     }
274522d268cbSEd Tanous     if (chassisId != "system")
274622d268cbSEd Tanous     {
274722d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
274822d268cbSEd Tanous         return;
274922d268cbSEd Tanous     }
275022d268cbSEd Tanous     createDump(asyncResp, req, "System");
275122d268cbSEd Tanous }
275222d268cbSEd Tanous 
2753fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2754fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2755fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2756fdd26906SClaire Weinan {
2757fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2758fdd26906SClaire Weinan     {
2759fdd26906SClaire Weinan         return;
2760fdd26906SClaire Weinan     }
2761fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2762fdd26906SClaire Weinan }
2763fdd26906SClaire Weinan 
276422d268cbSEd Tanous inline void handleLogServicesDumpClearLogComputerSystemPost(
276522d268cbSEd Tanous     crow::App& app, const crow::Request& req,
276622d268cbSEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
276722d268cbSEd Tanous     const std::string& chassisId)
276822d268cbSEd Tanous {
276922d268cbSEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
277022d268cbSEd Tanous     {
277122d268cbSEd Tanous         return;
277222d268cbSEd Tanous     }
277322d268cbSEd Tanous     if (chassisId != "system")
277422d268cbSEd Tanous     {
277522d268cbSEd Tanous         messages::resourceNotFound(asyncResp->res, "ComputerSystem", chassisId);
277622d268cbSEd Tanous         return;
277722d268cbSEd Tanous     }
277822d268cbSEd Tanous     clearDump(asyncResp, "System");
277922d268cbSEd Tanous }
278022d268cbSEd Tanous 
2781fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2782fdd26906SClaire Weinan {
2783fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2784fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2785fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2786fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2787fdd26906SClaire Weinan }
2788fdd26906SClaire Weinan 
2789fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2790fdd26906SClaire Weinan {
2791fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2792fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2793fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2794fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2795c9bb6861Sraviteja-b }
2796c9bb6861Sraviteja-b 
27977e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2798c9bb6861Sraviteja-b {
27997e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28007e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2801ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2802fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2803fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2804fdd26906SClaire Weinan 
28057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28067e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2807ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2808fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2809fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2810c9bb6861Sraviteja-b }
2811c9bb6861Sraviteja-b 
28127e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2813c9bb6861Sraviteja-b {
28140fda0f12SGeorge Liu     BMCWEB_ROUTE(
28150fda0f12SGeorge Liu         app,
28160fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2817ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
28187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2819fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2820fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2821a43be80fSAsmitha Karunanithi }
2822a43be80fSAsmitha Karunanithi 
28237e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
282480319af1SAsmitha Karunanithi {
28250fda0f12SGeorge Liu     BMCWEB_ROUTE(
28260fda0f12SGeorge Liu         app,
28270fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2828ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2829fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2830fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
283145ca1b86SEd Tanous }
2832fdd26906SClaire Weinan 
2833fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2834fdd26906SClaire Weinan {
2835fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2836fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2837fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2838fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2839fdd26906SClaire Weinan }
2840fdd26906SClaire Weinan 
2841fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2842fdd26906SClaire Weinan {
2843fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2844fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2845fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2846fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2847fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2848fdd26906SClaire Weinan }
2849fdd26906SClaire Weinan 
2850fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2851fdd26906SClaire Weinan {
2852fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2853fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2854fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2855fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2856fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2857fdd26906SClaire Weinan 
2858fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2859fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2860fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2861fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2862fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2863fdd26906SClaire Weinan }
2864fdd26906SClaire Weinan 
2865fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2866fdd26906SClaire Weinan {
2867fdd26906SClaire Weinan     BMCWEB_ROUTE(
2868fdd26906SClaire Weinan         app,
2869fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2870fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2871fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2872fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
28735cb1dd27SAsmitha Karunanithi }
28745cb1dd27SAsmitha Karunanithi 
28757e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
28765cb1dd27SAsmitha Karunanithi {
287722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/")
2878ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
28796ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
288022d268cbSEd Tanous             handleLogServicesDumpServiceComputerSystemGet, std::ref(app)));
28815cb1dd27SAsmitha Karunanithi }
28825cb1dd27SAsmitha Karunanithi 
28837e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
28847e860f15SJohn Edward Broadbent {
288522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/")
2886ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
288722d268cbSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
288822d268cbSEd Tanous             handleLogServicesDumpEntriesCollectionComputerSystemGet,
288922d268cbSEd Tanous             std::ref(app)));
28905cb1dd27SAsmitha Karunanithi }
28915cb1dd27SAsmitha Karunanithi 
28927e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
28935cb1dd27SAsmitha Karunanithi {
28947e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
289522d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2896ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
28976ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
289822d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemGet, std::ref(app)));
28998d1b46d7Szhanghch05 
29007e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
290122d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Dump/Entries/<str>/")
2902ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
29036ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
290422d268cbSEd Tanous             handleLogServicesDumpEntryComputerSystemDelete, std::ref(app)));
29055cb1dd27SAsmitha Karunanithi }
2906c9bb6861Sraviteja-b 
29077e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2908c9bb6861Sraviteja-b {
29090fda0f12SGeorge Liu     BMCWEB_ROUTE(
29100fda0f12SGeorge Liu         app,
291122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2912ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
291322d268cbSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
291422d268cbSEd Tanous             handleLogServicesDumpCollectDiagnosticDataComputerSystemPost,
291522d268cbSEd Tanous             std::ref(app)));
2916a43be80fSAsmitha Karunanithi }
2917a43be80fSAsmitha Karunanithi 
29187e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2919a43be80fSAsmitha Karunanithi {
29200fda0f12SGeorge Liu     BMCWEB_ROUTE(
29210fda0f12SGeorge Liu         app,
292222d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Dump/Actions/LogService.ClearLog/")
2923ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29246ab9ad54SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
292522d268cbSEd Tanous             handleLogServicesDumpClearLogComputerSystemPost, std::ref(app)));
2926013487e5Sraviteja-b }
2927013487e5Sraviteja-b 
29287e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
29291da66f75SEd Tanous {
29303946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29313946028dSAppaRao Puli     // method for security reasons.
29321da66f75SEd Tanous     /**
29331da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
29341da66f75SEd Tanous      */
293522d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/")
2936ed398213SEd Tanous         // This is incorrect, should be:
2937ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2938432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2939002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2940002d39b4SEd Tanous             [&app](const crow::Request& req,
294122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
294222d268cbSEd Tanous                    const std::string& systemName) {
29433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
294445ca1b86SEd Tanous         {
294545ca1b86SEd Tanous             return;
294645ca1b86SEd Tanous         }
294722d268cbSEd Tanous         if (systemName != "system")
294822d268cbSEd Tanous         {
294922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
295022d268cbSEd Tanous                                        systemName);
295122d268cbSEd Tanous             return;
295222d268cbSEd Tanous         }
295322d268cbSEd Tanous 
29547e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
29557e860f15SJohn Edward Broadbent         // SubRoute
29560f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2957424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2958e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
29598e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
29604f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
29614f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
29624f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2963e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2964e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
29657c8c4058STejas Patil 
29667c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
29672b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
29687c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
29697c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
29707c8c4058STejas Patil             redfishDateTimeOffset.second;
29717c8c4058STejas Patil 
29721476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
29731476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2974002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
29751476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2976002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
29771476687dSEd Tanous                                 ["target"] =
29781476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
29797e860f15SJohn Edward Broadbent         });
29801da66f75SEd Tanous }
29811da66f75SEd Tanous 
29827e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
29835b61b5e8SJason M. Bills {
29840fda0f12SGeorge Liu     BMCWEB_ROUTE(
29850fda0f12SGeorge Liu         app,
298622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.ClearLog/")
2987ed398213SEd Tanous         // This is incorrect, should be:
2988ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2989432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
29907e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
299145ca1b86SEd Tanous             [&app](const crow::Request& req,
299222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
299322d268cbSEd Tanous                    const std::string& systemName) {
29943ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
299545ca1b86SEd Tanous         {
299645ca1b86SEd Tanous             return;
299745ca1b86SEd Tanous         }
299822d268cbSEd Tanous         if (systemName != "system")
299922d268cbSEd Tanous         {
300022d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
300122d268cbSEd Tanous                                        systemName);
300222d268cbSEd Tanous             return;
300322d268cbSEd Tanous         }
30045b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
30055b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
3006cb13a392SEd Tanous                         const std::string&) {
30075b61b5e8SJason M. Bills             if (ec)
30085b61b5e8SJason M. Bills             {
30095b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
30105b61b5e8SJason M. Bills                 return;
30115b61b5e8SJason M. Bills             }
30125b61b5e8SJason M. Bills             messages::success(asyncResp->res);
30135b61b5e8SJason M. Bills             },
3014002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
30157e860f15SJohn Edward Broadbent         });
30165b61b5e8SJason M. Bills }
30175b61b5e8SJason M. Bills 
30188d1b46d7Szhanghch05 static void
30198d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30208d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
3021e855dd28SJason M. Bills {
3022043a0536SJohnathan Mantey     auto getStoredLogCallback =
3023b9d36b47SEd Tanous         [asyncResp, logID,
3024b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
3025b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
3026e855dd28SJason M. Bills         if (ec)
3027e855dd28SJason M. Bills         {
3028e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
30291ddcf01aSJason M. Bills             if (ec.value() ==
30301ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
30311ddcf01aSJason M. Bills             {
3032002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
30331ddcf01aSJason M. Bills             }
30341ddcf01aSJason M. Bills             else
30351ddcf01aSJason M. Bills             {
3036e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
30371ddcf01aSJason M. Bills             }
3038e855dd28SJason M. Bills             return;
3039e855dd28SJason M. Bills         }
3040043a0536SJohnathan Mantey 
3041043a0536SJohnathan Mantey         std::string timestamp{};
3042043a0536SJohnathan Mantey         std::string filename{};
3043043a0536SJohnathan Mantey         std::string logfile{};
30442c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
3045043a0536SJohnathan Mantey 
3046043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
3047e855dd28SJason M. Bills         {
30489db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3049e855dd28SJason M. Bills             return;
3050e855dd28SJason M. Bills         }
3051e855dd28SJason M. Bills 
3052043a0536SJohnathan Mantey         std::string crashdumpURI =
3053e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
3054043a0536SJohnathan Mantey             logID + "/" + filename;
305584afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
30569c11a172SVijay Lobo         logEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
305784afc48bSJason M. Bills         logEntry["@odata.id"] =
305884afc48bSJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
305984afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
306084afc48bSJason M. Bills         logEntry["Id"] = logID;
306184afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
306284afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
306384afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
306484afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
306584afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
30662b20ef6eSJason M. Bills 
30672b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
30682b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
30692b20ef6eSJason M. Bills         // directly
30702b20ef6eSJason M. Bills         if (logEntryJson.is_array())
30712b20ef6eSJason M. Bills         {
30722b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
30732b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
30742b20ef6eSJason M. Bills                 logEntryJson.size();
30752b20ef6eSJason M. Bills         }
30762b20ef6eSJason M. Bills         else
30772b20ef6eSJason M. Bills         {
3078d405bb51SJason M. Bills             logEntryJson.update(logEntry);
30792b20ef6eSJason M. Bills         }
3080e855dd28SJason M. Bills     };
3081d1bde9e5SKrzysztof Grobelny     sdbusplus::asio::getAllProperties(
3082d1bde9e5SKrzysztof Grobelny         *crow::connections::systemBus, crashdumpObject,
3083d1bde9e5SKrzysztof Grobelny         crashdumpPath + std::string("/") + logID, crashdumpInterface,
3084d1bde9e5SKrzysztof Grobelny         std::move(getStoredLogCallback));
3085e855dd28SJason M. Bills }
3086e855dd28SJason M. Bills 
30877e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
30881da66f75SEd Tanous {
30893946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
30903946028dSAppaRao Puli     // method for security reasons.
30911da66f75SEd Tanous     /**
30921da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
30931da66f75SEd Tanous      */
30947e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
309522d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/")
3096ed398213SEd Tanous         // This is incorrect, should be.
3097ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
3098432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3099002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3100002d39b4SEd Tanous             [&app](const crow::Request& req,
310122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
310222d268cbSEd Tanous                    const std::string& systemName) {
31033ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
310445ca1b86SEd Tanous         {
310545ca1b86SEd Tanous             return;
310645ca1b86SEd Tanous         }
310722d268cbSEd Tanous         if (systemName != "system")
310822d268cbSEd Tanous         {
310922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
311022d268cbSEd Tanous                                        systemName);
311122d268cbSEd Tanous             return;
311222d268cbSEd Tanous         }
311322d268cbSEd Tanous 
31142b20ef6eSJason M. Bills         crow::connections::systemBus->async_method_call(
31152b20ef6eSJason M. Bills             [asyncResp](const boost::system::error_code ec,
31162b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
31171da66f75SEd Tanous             if (ec)
31181da66f75SEd Tanous             {
31191da66f75SEd Tanous                 if (ec.value() !=
31201da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
31211da66f75SEd Tanous                 {
31221da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
31231da66f75SEd Tanous                                      << ec.message();
3124f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
31251da66f75SEd Tanous                     return;
31261da66f75SEd Tanous                 }
31271da66f75SEd Tanous             }
3128e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
31291da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
31300f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
3131424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
3132002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
3133e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
3134424c4176SJason M. Bills                 "Collection of Crashdump Entries";
3135002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3136a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
31372b20ef6eSJason M. Bills 
31382b20ef6eSJason M. Bills             for (const std::string& path : resp)
31391da66f75SEd Tanous             {
31402b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
3141e855dd28SJason M. Bills                 // Get the log ID
31422b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
31432b20ef6eSJason M. Bills                 if (logID.empty())
31441da66f75SEd Tanous                 {
3145e855dd28SJason M. Bills                     continue;
31461da66f75SEd Tanous                 }
3147e855dd28SJason M. Bills                 // Add the log entry to the array
31482b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
31492b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
31501da66f75SEd Tanous             }
31512b20ef6eSJason M. Bills             },
31521da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
31531da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
31541da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
31555b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
31567e860f15SJohn Edward Broadbent         });
31571da66f75SEd Tanous }
31581da66f75SEd Tanous 
31597e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
31601da66f75SEd Tanous {
31613946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31623946028dSAppaRao Puli     // method for security reasons.
31631da66f75SEd Tanous 
31647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
316522d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/")
3166ed398213SEd Tanous         // this is incorrect, should be
3167ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
3168432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
31697e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
317045ca1b86SEd Tanous             [&app](const crow::Request& req,
31717e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
317222d268cbSEd Tanous                    const std::string& systemName, const std::string& param) {
31733ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
317445ca1b86SEd Tanous         {
317545ca1b86SEd Tanous             return;
317645ca1b86SEd Tanous         }
317722d268cbSEd Tanous         if (systemName != "system")
317822d268cbSEd Tanous         {
317922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
318022d268cbSEd Tanous                                        systemName);
318122d268cbSEd Tanous             ;
318222d268cbSEd Tanous             return;
318322d268cbSEd Tanous         }
31847e860f15SJohn Edward Broadbent         const std::string& logID = param;
3185e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
31867e860f15SJohn Edward Broadbent         });
3187e855dd28SJason M. Bills }
3188e855dd28SJason M. Bills 
31897e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
3190e855dd28SJason M. Bills {
31913946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
31923946028dSAppaRao Puli     // method for security reasons.
31937e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
31947e860f15SJohn Edward Broadbent         app,
319522d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Entries/<str>/<str>/")
3196ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
31977e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3198a4ce114aSNan Zhou             [](const crow::Request& req,
31997e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
320022d268cbSEd Tanous                const std::string& systemName, const std::string& logID,
320122d268cbSEd Tanous                const std::string& fileName) {
32022a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
32032a9beeedSShounak Mitra         // Redfish resource.
320422d268cbSEd Tanous 
320522d268cbSEd Tanous         if (systemName != "system")
320622d268cbSEd Tanous         {
320722d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
320822d268cbSEd Tanous                                        systemName);
320922d268cbSEd Tanous             ;
321022d268cbSEd Tanous             return;
321122d268cbSEd Tanous         }
321222d268cbSEd Tanous 
3213043a0536SJohnathan Mantey         auto getStoredLogCallback =
3214002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
3215abf2add6SEd Tanous                 const boost::system::error_code ec,
3216002d39b4SEd Tanous                 const std::vector<
3217002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
32187e860f15SJohn Edward Broadbent                     resp) {
32191da66f75SEd Tanous             if (ec)
32201da66f75SEd Tanous             {
3221002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
3222f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
32231da66f75SEd Tanous                 return;
32241da66f75SEd Tanous             }
3225e855dd28SJason M. Bills 
3226043a0536SJohnathan Mantey             std::string dbusFilename{};
3227043a0536SJohnathan Mantey             std::string dbusTimestamp{};
3228043a0536SJohnathan Mantey             std::string dbusFilepath{};
3229043a0536SJohnathan Mantey 
3230002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
3231002d39b4SEd Tanous                                      dbusFilepath);
3232043a0536SJohnathan Mantey 
3233043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
3234043a0536SJohnathan Mantey                 dbusFilepath.empty())
32351da66f75SEd Tanous             {
32369db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
32371da66f75SEd Tanous                 return;
32381da66f75SEd Tanous             }
3239e855dd28SJason M. Bills 
3240043a0536SJohnathan Mantey             // Verify the file name parameter is correct
3241043a0536SJohnathan Mantey             if (fileName != dbusFilename)
3242043a0536SJohnathan Mantey             {
32439db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3244043a0536SJohnathan Mantey                 return;
3245043a0536SJohnathan Mantey             }
3246043a0536SJohnathan Mantey 
3247043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
3248043a0536SJohnathan Mantey             {
32499db4ba25SJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
3250043a0536SJohnathan Mantey                 return;
3251043a0536SJohnathan Mantey             }
3252002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
3253002d39b4SEd Tanous             asyncResp->res.body() =
3254002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
3255043a0536SJohnathan Mantey 
32567e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
32577e860f15SJohn Edward Broadbent             // from a browser
3258d9f6c621SEd Tanous             asyncResp->res.addHeader(
3259d9f6c621SEd Tanous                 boost::beast::http::field::content_disposition, "attachment");
32601da66f75SEd Tanous         };
3261d1bde9e5SKrzysztof Grobelny         sdbusplus::asio::getAllProperties(
3262d1bde9e5SKrzysztof Grobelny             *crow::connections::systemBus, crashdumpObject,
3263d1bde9e5SKrzysztof Grobelny             crashdumpPath + std::string("/") + logID, crashdumpInterface,
3264d1bde9e5SKrzysztof Grobelny             std::move(getStoredLogCallback));
32657e860f15SJohn Edward Broadbent         });
32661da66f75SEd Tanous }
32671da66f75SEd Tanous 
3268c5a4c82aSJason M. Bills enum class OEMDiagnosticType
3269c5a4c82aSJason M. Bills {
3270c5a4c82aSJason M. Bills     onDemand,
3271c5a4c82aSJason M. Bills     telemetry,
3272c5a4c82aSJason M. Bills     invalid,
3273c5a4c82aSJason M. Bills };
3274c5a4c82aSJason M. Bills 
3275f7725d79SEd Tanous inline OEMDiagnosticType
3276f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
3277c5a4c82aSJason M. Bills {
3278c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
3279c5a4c82aSJason M. Bills     {
3280c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
3281c5a4c82aSJason M. Bills     }
3282c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
3283c5a4c82aSJason M. Bills     {
3284c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
3285c5a4c82aSJason M. Bills     }
3286c5a4c82aSJason M. Bills 
3287c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
3288c5a4c82aSJason M. Bills }
3289c5a4c82aSJason M. Bills 
32907e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
32911da66f75SEd Tanous {
32923946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
32933946028dSAppaRao Puli     // method for security reasons.
32940fda0f12SGeorge Liu     BMCWEB_ROUTE(
32950fda0f12SGeorge Liu         app,
329622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
3297ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3298ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3299432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3300002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3301002d39b4SEd Tanous             [&app](const crow::Request& req,
330222d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
330322d268cbSEd Tanous                    const std::string& systemName) {
33043ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
330545ca1b86SEd Tanous         {
330645ca1b86SEd Tanous             return;
330745ca1b86SEd Tanous         }
330822d268cbSEd Tanous 
330922d268cbSEd Tanous         if (systemName != "system")
331022d268cbSEd Tanous         {
331122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
331222d268cbSEd Tanous                                        systemName);
331322d268cbSEd Tanous             ;
331422d268cbSEd Tanous             return;
331522d268cbSEd Tanous         }
331622d268cbSEd Tanous 
33178e6c099aSJason M. Bills         std::string diagnosticDataType;
33188e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
331915ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3320002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3321002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
33228e6c099aSJason M. Bills         {
33238e6c099aSJason M. Bills             return;
33248e6c099aSJason M. Bills         }
33258e6c099aSJason M. Bills 
33268e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
33278e6c099aSJason M. Bills         {
33288e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
33298e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
33308e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
33318e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
33328e6c099aSJason M. Bills                 "CollectDiagnosticData");
33338e6c099aSJason M. Bills             return;
33348e6c099aSJason M. Bills         }
33358e6c099aSJason M. Bills 
3336c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3337c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3338c5a4c82aSJason M. Bills 
3339c5a4c82aSJason M. Bills         std::string iface;
3340c5a4c82aSJason M. Bills         std::string method;
3341c5a4c82aSJason M. Bills         std::string taskMatchStr;
3342c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3343c5a4c82aSJason M. Bills         {
3344c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3345c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3346c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3347c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3348c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3349c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3350c5a4c82aSJason M. Bills         }
3351c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3352c5a4c82aSJason M. Bills         {
3353c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3354c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3355c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3356c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3357c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3358c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3359c5a4c82aSJason M. Bills         }
3360c5a4c82aSJason M. Bills         else
3361c5a4c82aSJason M. Bills         {
3362c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3363c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3364c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3365002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3366002d39b4SEd Tanous                 "CollectDiagnosticData");
3367c5a4c82aSJason M. Bills             return;
3368c5a4c82aSJason M. Bills         }
3369c5a4c82aSJason M. Bills 
3370c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3371c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3372c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
337398be3e39SEd Tanous                            const std::string&) mutable {
33741da66f75SEd Tanous             if (ec)
33751da66f75SEd Tanous             {
3376002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
33771da66f75SEd Tanous                 {
3378f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
33791da66f75SEd Tanous                 }
33804363d3b2SJason M. Bills                 else if (ec.value() ==
33814363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
33824363d3b2SJason M. Bills                 {
3383002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3384002d39b4SEd Tanous                                                             "60");
33854363d3b2SJason M. Bills                 }
33861da66f75SEd Tanous                 else
33871da66f75SEd Tanous                 {
3388f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
33891da66f75SEd Tanous                 }
33901da66f75SEd Tanous                 return;
33911da66f75SEd Tanous             }
3392002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
339359d494eeSPatrick Williams                 [](boost::system::error_code err, sdbusplus::message_t&,
3394002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
339566afe4faSJames Feist                 if (!err)
339666afe4faSJames Feist                 {
3397002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3398e5d5006bSJames Feist                         std::to_string(taskData->index)));
3399831d6b09SJames Feist                     taskData->state = "Completed";
340066afe4faSJames Feist                 }
340132898ceaSJames Feist                 return task::completed;
340266afe4faSJames Feist                 },
3403c5a4c82aSJason M. Bills                 taskMatchStr);
3404c5a4c82aSJason M. Bills 
340546229577SJames Feist             task->startTimer(std::chrono::minutes(5));
340646229577SJames Feist             task->populateResp(asyncResp->res);
340798be3e39SEd Tanous             task->payload.emplace(std::move(payload));
34081da66f75SEd Tanous         };
34098e6c099aSJason M. Bills 
34101da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3411002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3412002d39b4SEd Tanous             iface, method);
34137e860f15SJohn Edward Broadbent         });
34146eda7685SKenny L. Ku }
34156eda7685SKenny L. Ku 
3416cb92c03bSAndrew Geissler /**
3417cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3418cb92c03bSAndrew Geissler  */
34197e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3420cb92c03bSAndrew Geissler {
3421cb92c03bSAndrew Geissler     /**
3422cb92c03bSAndrew Geissler      * Function handles POST method request.
3423cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3424cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3425cb92c03bSAndrew Geissler      */
34267e860f15SJohn Edward Broadbent 
34270fda0f12SGeorge Liu     BMCWEB_ROUTE(
34280fda0f12SGeorge Liu         app,
342922d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/EventLog/Actions/LogService.ClearLog/")
3430ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
34317e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
343245ca1b86SEd Tanous             [&app](const crow::Request& req,
343322d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
343422d268cbSEd Tanous                    const std::string& systemName) {
34353ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
343645ca1b86SEd Tanous         {
343745ca1b86SEd Tanous             return;
343845ca1b86SEd Tanous         }
343922d268cbSEd Tanous         if (systemName != "system")
344022d268cbSEd Tanous         {
344122d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
344222d268cbSEd Tanous                                        systemName);
344322d268cbSEd Tanous             ;
344422d268cbSEd Tanous             return;
344522d268cbSEd Tanous         }
3446cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3447cb92c03bSAndrew Geissler 
3448cb92c03bSAndrew Geissler         // Process response from Logging service.
3449002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3450002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3451cb92c03bSAndrew Geissler             if (ec)
3452cb92c03bSAndrew Geissler             {
3453cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3454002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3455cb92c03bSAndrew Geissler                 asyncResp->res.result(
3456cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3457cb92c03bSAndrew Geissler                 return;
3458cb92c03bSAndrew Geissler             }
3459cb92c03bSAndrew Geissler 
3460002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3461cb92c03bSAndrew Geissler         };
3462cb92c03bSAndrew Geissler 
3463cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3464cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
34652c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3466cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3467cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
34687e860f15SJohn Edward Broadbent         });
3469cb92c03bSAndrew Geissler }
3470a3316fc6SZhikuiRen 
3471a3316fc6SZhikuiRen /****************************************************
3472a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3473a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3474a3316fc6SZhikuiRen  ******************************************************/
34757e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3476a3316fc6SZhikuiRen {
347722d268cbSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/")
3478ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3479002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3480002d39b4SEd Tanous             [&app](const crow::Request& req,
348122d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
348222d268cbSEd Tanous                    const std::string& systemName) {
34833ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
348445ca1b86SEd Tanous         {
348545ca1b86SEd Tanous             return;
348645ca1b86SEd Tanous         }
348722d268cbSEd Tanous         if (systemName != "system")
348822d268cbSEd Tanous         {
348922d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
349022d268cbSEd Tanous                                        systemName);
349122d268cbSEd Tanous             ;
349222d268cbSEd Tanous             return;
349322d268cbSEd Tanous         }
34941476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
34951476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
34961476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
34971476687dSEd Tanous             "#LogService.v1_1_0.LogService";
34981476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
34991476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
35001476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
35011476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
35021476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
35031476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
35047c8c4058STejas Patil 
35057c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
35062b82937eSEd Tanous             redfish::time_utils::getDateTimeOffsetNow();
35070fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
35087c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
35097c8c4058STejas Patil             redfishDateTimeOffset.second;
35107c8c4058STejas Patil 
3511a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
35127e860f15SJohn Edward Broadbent             {"target",
35130fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
35147e860f15SJohn Edward Broadbent         });
3515a3316fc6SZhikuiRen }
3516a3316fc6SZhikuiRen 
35177e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3518a3316fc6SZhikuiRen {
35190fda0f12SGeorge Liu     BMCWEB_ROUTE(
35200fda0f12SGeorge Liu         app,
352122d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Actions/LogService.ClearLog/")
3522ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3523ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3524432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
35257e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
352645ca1b86SEd Tanous             [&app](const crow::Request& req,
352722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
352822d268cbSEd Tanous                    const std::string& systemName) {
35293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
353045ca1b86SEd Tanous         {
353145ca1b86SEd Tanous             return;
353245ca1b86SEd Tanous         }
353322d268cbSEd Tanous         if (systemName != "system")
353422d268cbSEd Tanous         {
353522d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
353622d268cbSEd Tanous                                        systemName);
353722d268cbSEd Tanous             ;
353822d268cbSEd Tanous             return;
353922d268cbSEd Tanous         }
3540a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3541a3316fc6SZhikuiRen 
3542a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3543a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3544a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3545a3316fc6SZhikuiRen             if (ec)
3546a3316fc6SZhikuiRen             {
3547a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3548002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
35497e860f15SJohn Edward Broadbent                                  << ec;
3550002d39b4SEd Tanous                 asyncResp->res.result(
3551002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3552a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3553a3316fc6SZhikuiRen                 return;
3554a3316fc6SZhikuiRen             }
3555a3316fc6SZhikuiRen             },
355615124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
355715124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3558a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
35597e860f15SJohn Edward Broadbent         });
3560a3316fc6SZhikuiRen }
3561a3316fc6SZhikuiRen 
3562a3316fc6SZhikuiRen static void fillPostCodeEntry(
35638d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
35646c9a279eSManojkiran Eda     const boost::container::flat_map<
35656c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3566a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3567a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3568a3316fc6SZhikuiRen {
3569a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3570fffb8c1fSEd Tanous     const registries::Message* message =
3571fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3572a3316fc6SZhikuiRen 
3573a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3574a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3575a3316fc6SZhikuiRen 
3576a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
35776c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
35786c9a279eSManojkiran Eda              code : postcode)
3579a3316fc6SZhikuiRen     {
3580a3316fc6SZhikuiRen         currentCodeIndex++;
3581a3316fc6SZhikuiRen         std::string postcodeEntryID =
3582a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3583a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3584a3316fc6SZhikuiRen 
3585a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3586a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3587a3316fc6SZhikuiRen 
3588a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3589a3316fc6SZhikuiRen         { // already incremented
3590a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3591a3316fc6SZhikuiRen         }
3592a3316fc6SZhikuiRen         else
3593a3316fc6SZhikuiRen         {
3594a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3595a3316fc6SZhikuiRen         }
3596a3316fc6SZhikuiRen 
3597a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3598a3316fc6SZhikuiRen         // not fall between top and skip
3599a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3600a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3601a3316fc6SZhikuiRen         {
3602a3316fc6SZhikuiRen             continue;
3603a3316fc6SZhikuiRen         }
3604a3316fc6SZhikuiRen 
36054e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3606a3316fc6SZhikuiRen         // currentIndex
3607a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3608a3316fc6SZhikuiRen         {
3609a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3610a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3611a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3612a3316fc6SZhikuiRen             continue;
3613a3316fc6SZhikuiRen         }
3614a3316fc6SZhikuiRen 
3615a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3616a3316fc6SZhikuiRen         // index
3617a3316fc6SZhikuiRen 
3618a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3619a3316fc6SZhikuiRen         std::string entryTimeStr;
36201d8782e7SNan Zhou         entryTimeStr =
36212b82937eSEd Tanous             redfish::time_utils::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3622a3316fc6SZhikuiRen 
3623a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3624a3316fc6SZhikuiRen         std::ostringstream hexCode;
3625a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
36266c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3627a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3628a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3629a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3630a3316fc6SZhikuiRen         // Set precision to 4 digits
3631a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3632a3316fc6SZhikuiRen         // Add double to stream
3633a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3634a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3635a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3636a3316fc6SZhikuiRen 
3637a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3638a3316fc6SZhikuiRen         std::string msg;
3639a3316fc6SZhikuiRen         if (message != nullptr)
3640a3316fc6SZhikuiRen         {
3641a3316fc6SZhikuiRen             msg = message->message;
3642a3316fc6SZhikuiRen 
3643a3316fc6SZhikuiRen             // fill in this post code value
3644a3316fc6SZhikuiRen             int i = 0;
3645a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3646a3316fc6SZhikuiRen             {
3647a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3648a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3649a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3650a3316fc6SZhikuiRen                 {
3651a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3652a3316fc6SZhikuiRen                 }
3653a3316fc6SZhikuiRen             }
3654a3316fc6SZhikuiRen         }
3655a3316fc6SZhikuiRen 
3656d4342a92STim Lee         // Get Severity template from message registry
3657d4342a92STim Lee         std::string severity;
3658d4342a92STim Lee         if (message != nullptr)
3659d4342a92STim Lee         {
36605f2b84eeSEd Tanous             severity = message->messageSeverity;
3661d4342a92STim Lee         }
3662d4342a92STim Lee 
3663a3316fc6SZhikuiRen         // add to AsyncResp
3664a3316fc6SZhikuiRen         logEntryArray.push_back({});
3665a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
36669c11a172SVijay Lobo         bmcLogEntry["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
366784afc48bSJason M. Bills         bmcLogEntry["@odata.id"] =
36680fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
366984afc48bSJason M. Bills             postcodeEntryID;
367084afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
367184afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
367284afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
367384afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
367484afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
367584afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
367684afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
367784afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3678647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3679647b3cdcSGeorge Liu         {
3680647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3681647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3682647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3683647b3cdcSGeorge Liu         }
3684a3316fc6SZhikuiRen     }
3685a3316fc6SZhikuiRen }
3686a3316fc6SZhikuiRen 
36878d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3688a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3689a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3690a3316fc6SZhikuiRen {
3691a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
36926c9a279eSManojkiran Eda         [aResp, bootIndex,
36936c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
36946c9a279eSManojkiran Eda                     const boost::container::flat_map<
36956c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
36966c9a279eSManojkiran Eda                         postcode) {
3697a3316fc6SZhikuiRen         if (ec)
3698a3316fc6SZhikuiRen         {
3699a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3700a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3701a3316fc6SZhikuiRen             return;
3702a3316fc6SZhikuiRen         }
3703a3316fc6SZhikuiRen 
3704a3316fc6SZhikuiRen         // skip the empty postcode boots
3705a3316fc6SZhikuiRen         if (postcode.empty())
3706a3316fc6SZhikuiRen         {
3707a3316fc6SZhikuiRen             return;
3708a3316fc6SZhikuiRen         }
3709a3316fc6SZhikuiRen 
3710a3316fc6SZhikuiRen         fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3711a3316fc6SZhikuiRen 
3712a3316fc6SZhikuiRen         aResp->res.jsonValue["Members@odata.count"] =
3713a3316fc6SZhikuiRen             aResp->res.jsonValue["Members"].size();
3714a3316fc6SZhikuiRen         },
371515124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
371615124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3717a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3718a3316fc6SZhikuiRen         bootIndex);
3719a3316fc6SZhikuiRen }
3720a3316fc6SZhikuiRen 
37218d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3722a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3723a3316fc6SZhikuiRen                                const uint16_t bootCount,
37243648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
37253648c8beSEd Tanous                                size_t top)
3726a3316fc6SZhikuiRen {
3727a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3728a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3729a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
37306c9a279eSManojkiran Eda               const boost::container::flat_map<
37316c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
37326c9a279eSManojkiran Eda                   postcode) {
3733a3316fc6SZhikuiRen         if (ec)
3734a3316fc6SZhikuiRen         {
3735a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3736a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3737a3316fc6SZhikuiRen             return;
3738a3316fc6SZhikuiRen         }
3739a3316fc6SZhikuiRen 
3740a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3741a3316fc6SZhikuiRen         if (!postcode.empty())
3742a3316fc6SZhikuiRen         {
3743a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
37443648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3745a3316fc6SZhikuiRen             {
37463648c8beSEd Tanous                 uint64_t thisBootSkip =
37473648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
37483648c8beSEd Tanous                     entryCount;
3749a3316fc6SZhikuiRen                 uint64_t thisBootTop =
37503648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
37513648c8beSEd Tanous                     entryCount;
3752a3316fc6SZhikuiRen 
3753002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3754002d39b4SEd Tanous                                   thisBootTop);
3755a3316fc6SZhikuiRen             }
3756a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3757a3316fc6SZhikuiRen         }
3758a3316fc6SZhikuiRen 
3759a3316fc6SZhikuiRen         // continue to previous bootIndex
3760a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3761a3316fc6SZhikuiRen         {
3762a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3763a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3764a3316fc6SZhikuiRen         }
376581584abeSJiaqing Zhao         else if (skip + top < endCount)
3766a3316fc6SZhikuiRen         {
3767a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
37680fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3769a3316fc6SZhikuiRen                 std::to_string(skip + top);
3770a3316fc6SZhikuiRen         }
3771a3316fc6SZhikuiRen         },
377215124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
377315124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3774a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3775a3316fc6SZhikuiRen         bootIndex);
3776a3316fc6SZhikuiRen }
3777a3316fc6SZhikuiRen 
37788d1b46d7Szhanghch05 static void
37798d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
37803648c8beSEd Tanous                          size_t skip, size_t top)
3781a3316fc6SZhikuiRen {
3782a3316fc6SZhikuiRen     uint64_t entryCount = 0;
37831e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
37841e1e598dSJonathan Doman         *crow::connections::systemBus,
37851e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
37861e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
37871e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
37881e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
37891e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3790a3316fc6SZhikuiRen         if (ec)
3791a3316fc6SZhikuiRen         {
3792a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3793a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3794a3316fc6SZhikuiRen             return;
3795a3316fc6SZhikuiRen         }
37961e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
37971e1e598dSJonathan Doman         });
3798a3316fc6SZhikuiRen }
3799a3316fc6SZhikuiRen 
38007e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3801a3316fc6SZhikuiRen {
38027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
380322d268cbSEd Tanous                  "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/")
3804ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
38057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
380645ca1b86SEd Tanous             [&app](const crow::Request& req,
380722d268cbSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
380822d268cbSEd Tanous                    const std::string& systemName) {
3809c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3810c937d2bfSEd Tanous             .canDelegateTop = true,
3811c937d2bfSEd Tanous             .canDelegateSkip = true,
3812c937d2bfSEd Tanous         };
3813c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3814c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
38153ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
381645ca1b86SEd Tanous         {
381745ca1b86SEd Tanous             return;
381845ca1b86SEd Tanous         }
381922d268cbSEd Tanous 
382022d268cbSEd Tanous         if (systemName != "system")
382122d268cbSEd Tanous         {
382222d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
382322d268cbSEd Tanous                                        systemName);
382422d268cbSEd Tanous             ;
382522d268cbSEd Tanous             return;
382622d268cbSEd Tanous         }
3827a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3828a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3829a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3830a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3831a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3832a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3833a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3834a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3835a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
38363648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
38375143f7a5SJiaqing Zhao         size_t top = delegatedQuery.top.value_or(query_param::Query::maxTop);
38383648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
38397e860f15SJohn Edward Broadbent         });
3840a3316fc6SZhikuiRen }
3841a3316fc6SZhikuiRen 
3842647b3cdcSGeorge Liu /**
3843647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3844647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3845647b3cdcSGeorge Liu  *
3846647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3847647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3848647b3cdcSGeorge Liu  * @param[out] index          Index value
3849647b3cdcSGeorge Liu  *
3850647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3851647b3cdcSGeorge Liu  */
3852647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3853647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3854647b3cdcSGeorge Liu {
3855647b3cdcSGeorge Liu     std::vector<std::string> split;
3856647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3857647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3858647b3cdcSGeorge Liu     {
3859647b3cdcSGeorge Liu         return false;
3860647b3cdcSGeorge Liu     }
3861647b3cdcSGeorge Liu 
3862ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3863647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3864ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3865647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3866647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3867647b3cdcSGeorge Liu 
3868647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3869647b3cdcSGeorge Liu     {
3870647b3cdcSGeorge Liu         return false;
3871647b3cdcSGeorge Liu     }
3872647b3cdcSGeorge Liu 
3873647b3cdcSGeorge Liu     start = split[1].data();
3874ca45aa3cSEd Tanous 
3875ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3876647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3877647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3878647b3cdcSGeorge Liu 
3879517d9a58STony Lee     return ptrValue == end && ecValue == std::errc();
3880647b3cdcSGeorge Liu }
3881647b3cdcSGeorge Liu 
3882647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3883647b3cdcSGeorge Liu {
38840fda0f12SGeorge Liu     BMCWEB_ROUTE(
38850fda0f12SGeorge Liu         app,
388622d268cbSEd Tanous         "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/attachment/")
3887647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3888647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
388945ca1b86SEd Tanous             [&app](const crow::Request& req,
3890647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
389122d268cbSEd Tanous                    const std::string& systemName,
3892647b3cdcSGeorge Liu                    const std::string& postCodeID) {
38933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
389445ca1b86SEd Tanous         {
389545ca1b86SEd Tanous             return;
389645ca1b86SEd Tanous         }
389799351cd8SEd Tanous         if (http_helpers::isContentTypeAllowed(
389899351cd8SEd Tanous                 req.getHeaderValue("Accept"),
38994a0e1a0cSEd Tanous                 http_helpers::ContentType::OctetStream, true))
3900647b3cdcSGeorge Liu         {
3901002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3902647b3cdcSGeorge Liu             return;
3903647b3cdcSGeorge Liu         }
390422d268cbSEd Tanous         if (systemName != "system")
390522d268cbSEd Tanous         {
390622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
390722d268cbSEd Tanous                                        systemName);
390822d268cbSEd Tanous             ;
390922d268cbSEd Tanous             return;
391022d268cbSEd Tanous         }
3911647b3cdcSGeorge Liu 
3912647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3913647b3cdcSGeorge Liu         uint16_t index = 0;
3914647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3915647b3cdcSGeorge Liu         {
3916002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3917647b3cdcSGeorge Liu             return;
3918647b3cdcSGeorge Liu         }
3919647b3cdcSGeorge Liu 
3920647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3921647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3922647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3923002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3924002d39b4SEd Tanous                     postcodes) {
3925647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3926647b3cdcSGeorge Liu             {
3927002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3928002d39b4SEd Tanous                                            postCodeID);
3929647b3cdcSGeorge Liu                 return;
3930647b3cdcSGeorge Liu             }
3931647b3cdcSGeorge Liu             if (ec)
3932647b3cdcSGeorge Liu             {
3933647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3934647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
3935647b3cdcSGeorge Liu                 return;
3936647b3cdcSGeorge Liu             }
3937647b3cdcSGeorge Liu 
3938647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
3939002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
3940647b3cdcSGeorge Liu             {
3941647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3942002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3943002d39b4SEd Tanous                                            postCodeID);
3944647b3cdcSGeorge Liu                 return;
3945647b3cdcSGeorge Liu             }
3946647b3cdcSGeorge Liu 
39479eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
394846ff87baSEd Tanous             if (c.empty())
3949647b3cdcSGeorge Liu             {
3950647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
3951002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3952002d39b4SEd Tanous                                            postCodeID);
3953647b3cdcSGeorge Liu                 return;
3954647b3cdcSGeorge Liu             }
395546ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
395646ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
395746ff87baSEd Tanous             std::string_view strData(d, c.size());
3958647b3cdcSGeorge Liu 
3959d9f6c621SEd Tanous             asyncResp->res.addHeader(boost::beast::http::field::content_type,
3960647b3cdcSGeorge Liu                                      "application/octet-stream");
3961d9f6c621SEd Tanous             asyncResp->res.addHeader(
3962d9f6c621SEd Tanous                 boost::beast::http::field::content_transfer_encoding, "Base64");
3963002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
3964647b3cdcSGeorge Liu             },
3965647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
3966647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
3967002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3968647b3cdcSGeorge Liu         });
3969647b3cdcSGeorge Liu }
3970647b3cdcSGeorge Liu 
39717e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3972a3316fc6SZhikuiRen {
39737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
397422d268cbSEd Tanous         app, "/redfish/v1/Systems/<str>/LogServices/PostCodes/Entries/<str>/")
3975ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
39767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
397745ca1b86SEd Tanous             [&app](const crow::Request& req,
39787e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
397922d268cbSEd Tanous                    const std::string& systemName, const std::string& targetID) {
39803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
398145ca1b86SEd Tanous         {
398245ca1b86SEd Tanous             return;
398345ca1b86SEd Tanous         }
398422d268cbSEd Tanous         if (systemName != "system")
398522d268cbSEd Tanous         {
398622d268cbSEd Tanous             messages::resourceNotFound(asyncResp->res, "ComputerSystem",
398722d268cbSEd Tanous                                        systemName);
398822d268cbSEd Tanous             return;
398922d268cbSEd Tanous         }
399022d268cbSEd Tanous 
3991647b3cdcSGeorge Liu         uint16_t bootIndex = 0;
3992647b3cdcSGeorge Liu         uint64_t codeIndex = 0;
3993647b3cdcSGeorge Liu         if (!parsePostCode(targetID, codeIndex, bootIndex))
3994a3316fc6SZhikuiRen         {
3995a3316fc6SZhikuiRen             // Requested ID was not found
39969db4ba25SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "LogEntry", targetID);
3997a3316fc6SZhikuiRen             return;
3998a3316fc6SZhikuiRen         }
3999a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
4000a3316fc6SZhikuiRen         {
4001a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
40027e860f15SJohn Edward Broadbent                              << targetID;
4003a3316fc6SZhikuiRen         }
4004a3316fc6SZhikuiRen 
40059c11a172SVijay Lobo         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_9_0.LogEntry";
4006a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
40070fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
4008a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
4009a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
4010a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
4011a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
4012a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
4013a3316fc6SZhikuiRen 
4014a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
40157e860f15SJohn Edward Broadbent         });
4016a3316fc6SZhikuiRen }
4017a3316fc6SZhikuiRen 
40181da66f75SEd Tanous } // namespace redfish
4019