xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision d405bb51fb420ee9318b1cdc13c791db2f56b99c)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
18b7028ebfSSpencer Ku #include "gzfile.hpp"
19647b3cdcSGeorge Liu #include "http_utility.hpp"
20b7028ebfSSpencer Ku #include "human_sort.hpp"
214851d45dSJason M. Bills #include "registries.hpp"
224851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
234851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2446229577SJames Feist #include "task.hpp"
251da66f75SEd Tanous 
26e1f26343SJason M. Bills #include <systemd/sd-journal.h>
27400fd1fbSAdriana Kobylak #include <unistd.h>
28e1f26343SJason M. Bills 
297e860f15SJohn Edward Broadbent #include <app.hpp>
30400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
314851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
32400fd1fbSAdriana Kobylak #include <boost/beast/http.hpp>
331da66f75SEd Tanous #include <boost/container/flat_map.hpp>
341ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
35168e20c1SEd Tanous #include <dbus_utility.hpp>
36cb92c03bSAndrew Geissler #include <error_messages.hpp>
3745ca1b86SEd Tanous #include <query.hpp>
38ed398213SEd Tanous #include <registries/privilege_registry.hpp>
391214b7e7SGunnar Mills 
40647b3cdcSGeorge Liu #include <charconv>
414418c7f0SJames Feist #include <filesystem>
4275710de2SXiaochao Ma #include <optional>
4326702d01SEd Tanous #include <span>
44cd225da8SJason M. Bills #include <string_view>
45abf2add6SEd Tanous #include <variant>
461da66f75SEd Tanous 
471da66f75SEd Tanous namespace redfish
481da66f75SEd Tanous {
491da66f75SEd Tanous 
505b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
515b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
525b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
535b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
545b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
555b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
56424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
576eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
586eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
591da66f75SEd Tanous 
60fffb8c1fSEd Tanous namespace registries
614851d45dSJason M. Bills {
6226702d01SEd Tanous static const Message*
6326702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
6426702d01SEd Tanous                            const std::span<const MessageEntry> registry)
654851d45dSJason M. Bills {
66002d39b4SEd Tanous     std::span<const MessageEntry>::iterator messageIt =
67002d39b4SEd Tanous         std::find_if(registry.begin(), registry.end(),
684851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
69e662eae8SEd Tanous         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
704851d45dSJason M. Bills         });
7126702d01SEd Tanous     if (messageIt != registry.end())
724851d45dSJason M. Bills     {
734851d45dSJason M. Bills         return &messageIt->second;
744851d45dSJason M. Bills     }
754851d45dSJason M. Bills 
764851d45dSJason M. Bills     return nullptr;
774851d45dSJason M. Bills }
784851d45dSJason M. Bills 
794851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
804851d45dSJason M. Bills {
814851d45dSJason M. Bills     // Redfish MessageIds are in the form
824851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
834851d45dSJason M. Bills     // the right Message
844851d45dSJason M. Bills     std::vector<std::string> fields;
854851d45dSJason M. Bills     fields.reserve(4);
864851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
874851d45dSJason M. Bills     std::string& registryName = fields[0];
884851d45dSJason M. Bills     std::string& messageKey = fields[3];
894851d45dSJason M. Bills 
904851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
914851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
924851d45dSJason M. Bills     {
934851d45dSJason M. Bills         return getMessageFromRegistry(
9426702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
954851d45dSJason M. Bills     }
964851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
974851d45dSJason M. Bills     {
984851d45dSJason M. Bills         return getMessageFromRegistry(
9926702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1004851d45dSJason M. Bills     }
1014851d45dSJason M. Bills     return nullptr;
1024851d45dSJason M. Bills }
103fffb8c1fSEd Tanous } // namespace registries
1044851d45dSJason M. Bills 
105f6150403SJames Feist namespace fs = std::filesystem;
1061da66f75SEd Tanous 
107cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
108cb92c03bSAndrew Geissler {
109d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
110d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
111d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
112d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
113cb92c03bSAndrew Geissler     {
114cb92c03bSAndrew Geissler         return "Critical";
115cb92c03bSAndrew Geissler     }
1163174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
117d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
118d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
119cb92c03bSAndrew Geissler     {
120cb92c03bSAndrew Geissler         return "OK";
121cb92c03bSAndrew Geissler     }
1223174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
123cb92c03bSAndrew Geissler     {
124cb92c03bSAndrew Geissler         return "Warning";
125cb92c03bSAndrew Geissler     }
126cb92c03bSAndrew Geissler     return "";
127cb92c03bSAndrew Geissler }
128cb92c03bSAndrew Geissler 
1297e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13039e77504SEd Tanous                                      const std::string_view& field,
13139e77504SEd Tanous                                      std::string_view& contents)
13216428a1aSJason M. Bills {
13316428a1aSJason M. Bills     const char* data = nullptr;
13416428a1aSJason M. Bills     size_t length = 0;
13516428a1aSJason M. Bills     int ret = 0;
13616428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
13746ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
13846ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
13946ff87baSEd Tanous 
14046ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
14116428a1aSJason M. Bills     if (ret < 0)
14216428a1aSJason M. Bills     {
14316428a1aSJason M. Bills         return ret;
14416428a1aSJason M. Bills     }
14539e77504SEd Tanous     contents = std::string_view(data, length);
14616428a1aSJason M. Bills     // Only use the content after the "=" character.
14781ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
14816428a1aSJason M. Bills     return ret;
14916428a1aSJason M. Bills }
15016428a1aSJason M. Bills 
1517e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1527e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1537e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15416428a1aSJason M. Bills {
15516428a1aSJason M. Bills     int ret = 0;
15639e77504SEd Tanous     std::string_view metadata;
15716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15816428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
15916428a1aSJason M. Bills     if (ret < 0)
16016428a1aSJason M. Bills     {
16116428a1aSJason M. Bills         return ret;
16216428a1aSJason M. Bills     }
163b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16416428a1aSJason M. Bills     return ret;
16516428a1aSJason M. Bills }
16616428a1aSJason M. Bills 
1677e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1687e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
169a3316fc6SZhikuiRen {
170a3316fc6SZhikuiRen     int ret = 0;
171a3316fc6SZhikuiRen     uint64_t timestamp = 0;
172a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
173a3316fc6SZhikuiRen     if (ret < 0)
174a3316fc6SZhikuiRen     {
175a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
176a3316fc6SZhikuiRen                          << strerror(-ret);
177a3316fc6SZhikuiRen         return false;
178a3316fc6SZhikuiRen     }
1791d8782e7SNan Zhou     entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
1809c620e21SAsmitha Karunanithi     return true;
181a3316fc6SZhikuiRen }
18250b8a43aSEd Tanous 
1837e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
184e85d6b16SJason M. Bills                                     const bool firstEntry = true)
18516428a1aSJason M. Bills {
18616428a1aSJason M. Bills     int ret = 0;
18716428a1aSJason M. Bills     static uint64_t prevTs = 0;
18816428a1aSJason M. Bills     static int index = 0;
189e85d6b16SJason M. Bills     if (firstEntry)
190e85d6b16SJason M. Bills     {
191e85d6b16SJason M. Bills         prevTs = 0;
192e85d6b16SJason M. Bills     }
193e85d6b16SJason M. Bills 
19416428a1aSJason M. Bills     // Get the entry timestamp
19516428a1aSJason M. Bills     uint64_t curTs = 0;
19616428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
19716428a1aSJason M. Bills     if (ret < 0)
19816428a1aSJason M. Bills     {
19916428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
20016428a1aSJason M. Bills                          << strerror(-ret);
20116428a1aSJason M. Bills         return false;
20216428a1aSJason M. Bills     }
20316428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
20416428a1aSJason M. Bills     if (curTs == prevTs)
20516428a1aSJason M. Bills     {
20616428a1aSJason M. Bills         index++;
20716428a1aSJason M. Bills     }
20816428a1aSJason M. Bills     else
20916428a1aSJason M. Bills     {
21016428a1aSJason M. Bills         // Otherwise, reset it
21116428a1aSJason M. Bills         index = 0;
21216428a1aSJason M. Bills     }
21316428a1aSJason M. Bills     // Save the timestamp
21416428a1aSJason M. Bills     prevTs = curTs;
21516428a1aSJason M. Bills 
21616428a1aSJason M. Bills     entryID = std::to_string(curTs);
21716428a1aSJason M. Bills     if (index > 0)
21816428a1aSJason M. Bills     {
21916428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
22016428a1aSJason M. Bills     }
22116428a1aSJason M. Bills     return true;
22216428a1aSJason M. Bills }
22316428a1aSJason M. Bills 
224e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
225e85d6b16SJason M. Bills                              const bool firstEntry = true)
22695820184SJason M. Bills {
227271584abSEd Tanous     static time_t prevTs = 0;
22895820184SJason M. Bills     static int index = 0;
229e85d6b16SJason M. Bills     if (firstEntry)
230e85d6b16SJason M. Bills     {
231e85d6b16SJason M. Bills         prevTs = 0;
232e85d6b16SJason M. Bills     }
233e85d6b16SJason M. Bills 
23495820184SJason M. Bills     // Get the entry timestamp
235271584abSEd Tanous     std::time_t curTs = 0;
23695820184SJason M. Bills     std::tm timeStruct = {};
23795820184SJason M. Bills     std::istringstream entryStream(logEntry);
23895820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
23995820184SJason M. Bills     {
24095820184SJason M. Bills         curTs = std::mktime(&timeStruct);
24195820184SJason M. Bills     }
24295820184SJason M. Bills     // If the timestamp isn't unique, increment the index
24395820184SJason M. Bills     if (curTs == prevTs)
24495820184SJason M. Bills     {
24595820184SJason M. Bills         index++;
24695820184SJason M. Bills     }
24795820184SJason M. Bills     else
24895820184SJason M. Bills     {
24995820184SJason M. Bills         // Otherwise, reset it
25095820184SJason M. Bills         index = 0;
25195820184SJason M. Bills     }
25295820184SJason M. Bills     // Save the timestamp
25395820184SJason M. Bills     prevTs = curTs;
25495820184SJason M. Bills 
25595820184SJason M. Bills     entryID = std::to_string(curTs);
25695820184SJason M. Bills     if (index > 0)
25795820184SJason M. Bills     {
25895820184SJason M. Bills         entryID += "_" + std::to_string(index);
25995820184SJason M. Bills     }
26095820184SJason M. Bills     return true;
26195820184SJason M. Bills }
26295820184SJason M. Bills 
2637e860f15SJohn Edward Broadbent inline static bool
2648d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2658d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2668d1b46d7Szhanghch05                        uint64_t& index)
26716428a1aSJason M. Bills {
26816428a1aSJason M. Bills     if (entryID.empty())
26916428a1aSJason M. Bills     {
27016428a1aSJason M. Bills         return false;
27116428a1aSJason M. Bills     }
27216428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
27339e77504SEd Tanous     std::string_view tsStr(entryID);
27416428a1aSJason M. Bills 
27581ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
27671d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
27716428a1aSJason M. Bills     {
27816428a1aSJason M. Bills         // Timestamp has an index
27916428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
28039e77504SEd Tanous         std::string_view indexStr(entryID);
28116428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
282c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
283c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
284c0bd5e4bSEd Tanous         if (ec != std::errc())
28516428a1aSJason M. Bills         {
286ace85d60SEd Tanous             messages::resourceMissingAtURI(
287ace85d60SEd Tanous                 asyncResp->res, crow::utility::urlFromPieces(entryID));
28816428a1aSJason M. Bills             return false;
28916428a1aSJason M. Bills         }
29016428a1aSJason M. Bills     }
29116428a1aSJason M. Bills     // Timestamp has no index
292c0bd5e4bSEd Tanous     auto [ptr, ec] =
293c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
294c0bd5e4bSEd Tanous     if (ec != std::errc())
29516428a1aSJason M. Bills     {
296ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
297ace85d60SEd Tanous                                        crow::utility::urlFromPieces(entryID));
29816428a1aSJason M. Bills         return false;
29916428a1aSJason M. Bills     }
30016428a1aSJason M. Bills     return true;
30116428a1aSJason M. Bills }
30216428a1aSJason M. Bills 
30395820184SJason M. Bills static bool
30495820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
30595820184SJason M. Bills {
30695820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
30795820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
30895820184SJason M. Bills 
30995820184SJason M. Bills     // Loop through the directory looking for redfish log files
31095820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
31195820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
31295820184SJason M. Bills     {
31395820184SJason M. Bills         // If we find a redfish log file, save the path
31495820184SJason M. Bills         std::string filename = dirEnt.path().filename();
31595820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
31695820184SJason M. Bills         {
31795820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
31895820184SJason M. Bills         }
31995820184SJason M. Bills     }
32095820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
32195820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
32295820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
32395820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
32495820184SJason M. Bills 
32595820184SJason M. Bills     return !redfishLogFiles.empty();
32695820184SJason M. Bills }
32795820184SJason M. Bills 
32821ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
329fdd26906SClaire Weinan {
330fdd26906SClaire Weinan     std::string entriesPath;
331fdd26906SClaire Weinan 
332fdd26906SClaire Weinan     if (dumpType == "BMC")
333fdd26906SClaire Weinan     {
334fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
335fdd26906SClaire Weinan     }
336fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
337fdd26906SClaire Weinan     {
338fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
339fdd26906SClaire Weinan     }
340fdd26906SClaire Weinan     else if (dumpType == "System")
341fdd26906SClaire Weinan     {
342fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
343fdd26906SClaire Weinan     }
344fdd26906SClaire Weinan     else
345fdd26906SClaire Weinan     {
346fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
347fdd26906SClaire Weinan                          << dumpType;
348fdd26906SClaire Weinan     }
349fdd26906SClaire Weinan 
350fdd26906SClaire Weinan     // Returns empty string on error
351fdd26906SClaire Weinan     return entriesPath;
352fdd26906SClaire Weinan }
353fdd26906SClaire Weinan 
3548d1b46d7Szhanghch05 inline void
3558d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3565cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3575cb1dd27SAsmitha Karunanithi {
358fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
359fdd26906SClaire Weinan     if (entriesPath.empty())
3605cb1dd27SAsmitha Karunanithi     {
3615cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3625cb1dd27SAsmitha Karunanithi         return;
3635cb1dd27SAsmitha Karunanithi     }
3645cb1dd27SAsmitha Karunanithi 
3655cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
366fdd26906SClaire Weinan         [asyncResp, entriesPath,
367711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
368711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
3695cb1dd27SAsmitha Karunanithi         if (ec)
3705cb1dd27SAsmitha Karunanithi         {
3715cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
3725cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
3735cb1dd27SAsmitha Karunanithi             return;
3745cb1dd27SAsmitha Karunanithi         }
3755cb1dd27SAsmitha Karunanithi 
376fdd26906SClaire Weinan         // Remove ending slash
377fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
378fdd26906SClaire Weinan         if (!odataIdStr.empty())
379fdd26906SClaire Weinan         {
380fdd26906SClaire Weinan             odataIdStr.pop_back();
381fdd26906SClaire Weinan         }
382fdd26906SClaire Weinan 
383fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
384fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
385fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
386fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
387fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
388fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
389fdd26906SClaire Weinan 
3905cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
3915cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
392b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
393b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
394002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
3955cb1dd27SAsmitha Karunanithi 
396002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
397002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
398002d39b4SEd Tanous                                                r.first.filename());
399565dfb6fSClaire Weinan         });
400565dfb6fSClaire Weinan 
4015cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4025cb1dd27SAsmitha Karunanithi         {
403b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4045cb1dd27SAsmitha Karunanithi             {
4055cb1dd27SAsmitha Karunanithi                 continue;
4065cb1dd27SAsmitha Karunanithi             }
4071d8782e7SNan Zhou             uint64_t timestamp = 0;
4085cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
40935440d18SAsmitha Karunanithi             std::string dumpStatus;
41035440d18SAsmitha Karunanithi             nlohmann::json thisEntry;
4112dfd18efSEd Tanous 
4122dfd18efSEd Tanous             std::string entryID = object.first.filename();
4132dfd18efSEd Tanous             if (entryID.empty())
4145cb1dd27SAsmitha Karunanithi             {
4155cb1dd27SAsmitha Karunanithi                 continue;
4165cb1dd27SAsmitha Karunanithi             }
4175cb1dd27SAsmitha Karunanithi 
4185cb1dd27SAsmitha Karunanithi             for (auto& interfaceMap : object.second)
4195cb1dd27SAsmitha Karunanithi             {
420002d39b4SEd Tanous                 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
42135440d18SAsmitha Karunanithi                 {
4229eb808c1SEd Tanous                     for (const auto& propertyMap : interfaceMap.second)
42335440d18SAsmitha Karunanithi                     {
42435440d18SAsmitha Karunanithi                         if (propertyMap.first == "Status")
42535440d18SAsmitha Karunanithi                         {
426002d39b4SEd Tanous                             const auto* status =
427002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
42835440d18SAsmitha Karunanithi                             if (status == nullptr)
42935440d18SAsmitha Karunanithi                             {
43035440d18SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
43135440d18SAsmitha Karunanithi                                 break;
43235440d18SAsmitha Karunanithi                             }
43335440d18SAsmitha Karunanithi                             dumpStatus = *status;
43435440d18SAsmitha Karunanithi                         }
43535440d18SAsmitha Karunanithi                     }
43635440d18SAsmitha Karunanithi                 }
437002d39b4SEd Tanous                 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
4385cb1dd27SAsmitha Karunanithi                 {
4395cb1dd27SAsmitha Karunanithi 
4405cb1dd27SAsmitha Karunanithi                     for (auto& propertyMap : interfaceMap.second)
4415cb1dd27SAsmitha Karunanithi                     {
4425cb1dd27SAsmitha Karunanithi                         if (propertyMap.first == "Size")
4435cb1dd27SAsmitha Karunanithi                         {
44455f79e6fSEd Tanous                             const auto* sizePtr =
4455cb1dd27SAsmitha Karunanithi                                 std::get_if<uint64_t>(&propertyMap.second);
4465cb1dd27SAsmitha Karunanithi                             if (sizePtr == nullptr)
4475cb1dd27SAsmitha Karunanithi                             {
4485cb1dd27SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
4495cb1dd27SAsmitha Karunanithi                                 break;
4505cb1dd27SAsmitha Karunanithi                             }
4515cb1dd27SAsmitha Karunanithi                             size = *sizePtr;
4525cb1dd27SAsmitha Karunanithi                             break;
4535cb1dd27SAsmitha Karunanithi                         }
4545cb1dd27SAsmitha Karunanithi                     }
4555cb1dd27SAsmitha Karunanithi                 }
4565cb1dd27SAsmitha Karunanithi                 else if (interfaceMap.first ==
4575cb1dd27SAsmitha Karunanithi                          "xyz.openbmc_project.Time.EpochTime")
4585cb1dd27SAsmitha Karunanithi                 {
4595cb1dd27SAsmitha Karunanithi 
4609eb808c1SEd Tanous                     for (const auto& propertyMap : interfaceMap.second)
4615cb1dd27SAsmitha Karunanithi                     {
4625cb1dd27SAsmitha Karunanithi                         if (propertyMap.first == "Elapsed")
4635cb1dd27SAsmitha Karunanithi                         {
4645cb1dd27SAsmitha Karunanithi                             const uint64_t* usecsTimeStamp =
4655cb1dd27SAsmitha Karunanithi                                 std::get_if<uint64_t>(&propertyMap.second);
4665cb1dd27SAsmitha Karunanithi                             if (usecsTimeStamp == nullptr)
4675cb1dd27SAsmitha Karunanithi                             {
4685cb1dd27SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
4695cb1dd27SAsmitha Karunanithi                                 break;
4705cb1dd27SAsmitha Karunanithi                             }
4711d8782e7SNan Zhou                             timestamp = (*usecsTimeStamp / 1000 / 1000);
4725cb1dd27SAsmitha Karunanithi                             break;
4735cb1dd27SAsmitha Karunanithi                         }
4745cb1dd27SAsmitha Karunanithi                     }
4755cb1dd27SAsmitha Karunanithi                 }
4765cb1dd27SAsmitha Karunanithi             }
4775cb1dd27SAsmitha Karunanithi 
4780fda0f12SGeorge Liu             if (dumpStatus !=
4790fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
48035440d18SAsmitha Karunanithi                 !dumpStatus.empty())
48135440d18SAsmitha Karunanithi             {
48235440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
48335440d18SAsmitha Karunanithi                 continue;
48435440d18SAsmitha Karunanithi             }
48535440d18SAsmitha Karunanithi 
486647b3cdcSGeorge Liu             thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
487fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
4885cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
4895cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
490002d39b4SEd Tanous             thisEntry["Created"] = crow::utility::getDateTimeUint(timestamp);
4915cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
4925cb1dd27SAsmitha Karunanithi 
4935cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
4945cb1dd27SAsmitha Karunanithi             {
495d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
496d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
497fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
498fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
4995cb1dd27SAsmitha Karunanithi             }
5005cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5015cb1dd27SAsmitha Karunanithi             {
502d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
503d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
504d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
505fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
506fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5075cb1dd27SAsmitha Karunanithi             }
50835440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5095cb1dd27SAsmitha Karunanithi         }
510002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5115cb1dd27SAsmitha Karunanithi         },
5125cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5135cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5145cb1dd27SAsmitha Karunanithi }
5155cb1dd27SAsmitha Karunanithi 
5168d1b46d7Szhanghch05 inline void
517c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5188d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5195cb1dd27SAsmitha Karunanithi {
520fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
521fdd26906SClaire Weinan     if (entriesPath.empty())
5225cb1dd27SAsmitha Karunanithi     {
5235cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5245cb1dd27SAsmitha Karunanithi         return;
5255cb1dd27SAsmitha Karunanithi     }
5265cb1dd27SAsmitha Karunanithi 
5275cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
528fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
529fdd26906SClaire Weinan          entriesPath](const boost::system::error_code ec,
530711ac7a9SEd Tanous                       dbus::utility::ManagedObjectType& resp) {
5315cb1dd27SAsmitha Karunanithi         if (ec)
5325cb1dd27SAsmitha Karunanithi         {
5335cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5345cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5355cb1dd27SAsmitha Karunanithi             return;
5365cb1dd27SAsmitha Karunanithi         }
5375cb1dd27SAsmitha Karunanithi 
538b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
539b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
540b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
541002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
542b47452b2SAsmitha Karunanithi 
5439eb808c1SEd Tanous         for (const auto& objectPath : resp)
5445cb1dd27SAsmitha Karunanithi         {
545b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5465cb1dd27SAsmitha Karunanithi             {
5475cb1dd27SAsmitha Karunanithi                 continue;
5485cb1dd27SAsmitha Karunanithi             }
5495cb1dd27SAsmitha Karunanithi 
5505cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
5511d8782e7SNan Zhou             uint64_t timestamp = 0;
5525cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
55335440d18SAsmitha Karunanithi             std::string dumpStatus;
5545cb1dd27SAsmitha Karunanithi 
5559eb808c1SEd Tanous             for (const auto& interfaceMap : objectPath.second)
5565cb1dd27SAsmitha Karunanithi             {
557002d39b4SEd Tanous                 if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
55835440d18SAsmitha Karunanithi                 {
5599eb808c1SEd Tanous                     for (const auto& propertyMap : interfaceMap.second)
56035440d18SAsmitha Karunanithi                     {
56135440d18SAsmitha Karunanithi                         if (propertyMap.first == "Status")
56235440d18SAsmitha Karunanithi                         {
5639eb808c1SEd Tanous                             const std::string* status =
564002d39b4SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
56535440d18SAsmitha Karunanithi                             if (status == nullptr)
56635440d18SAsmitha Karunanithi                             {
56735440d18SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
56835440d18SAsmitha Karunanithi                                 break;
56935440d18SAsmitha Karunanithi                             }
57035440d18SAsmitha Karunanithi                             dumpStatus = *status;
57135440d18SAsmitha Karunanithi                         }
57235440d18SAsmitha Karunanithi                     }
57335440d18SAsmitha Karunanithi                 }
574002d39b4SEd Tanous                 else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
5755cb1dd27SAsmitha Karunanithi                 {
5769eb808c1SEd Tanous                     for (const auto& propertyMap : interfaceMap.second)
5775cb1dd27SAsmitha Karunanithi                     {
5785cb1dd27SAsmitha Karunanithi                         if (propertyMap.first == "Size")
5795cb1dd27SAsmitha Karunanithi                         {
5809eb808c1SEd Tanous                             const uint64_t* sizePtr =
5815cb1dd27SAsmitha Karunanithi                                 std::get_if<uint64_t>(&propertyMap.second);
5825cb1dd27SAsmitha Karunanithi                             if (sizePtr == nullptr)
5835cb1dd27SAsmitha Karunanithi                             {
5845cb1dd27SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
5855cb1dd27SAsmitha Karunanithi                                 break;
5865cb1dd27SAsmitha Karunanithi                             }
5875cb1dd27SAsmitha Karunanithi                             size = *sizePtr;
5885cb1dd27SAsmitha Karunanithi                             break;
5895cb1dd27SAsmitha Karunanithi                         }
5905cb1dd27SAsmitha Karunanithi                     }
5915cb1dd27SAsmitha Karunanithi                 }
5925cb1dd27SAsmitha Karunanithi                 else if (interfaceMap.first ==
5935cb1dd27SAsmitha Karunanithi                          "xyz.openbmc_project.Time.EpochTime")
5945cb1dd27SAsmitha Karunanithi                 {
5959eb808c1SEd Tanous                     for (const auto& propertyMap : interfaceMap.second)
5965cb1dd27SAsmitha Karunanithi                     {
5975cb1dd27SAsmitha Karunanithi                         if (propertyMap.first == "Elapsed")
5985cb1dd27SAsmitha Karunanithi                         {
5995cb1dd27SAsmitha Karunanithi                             const uint64_t* usecsTimeStamp =
6005cb1dd27SAsmitha Karunanithi                                 std::get_if<uint64_t>(&propertyMap.second);
6015cb1dd27SAsmitha Karunanithi                             if (usecsTimeStamp == nullptr)
6025cb1dd27SAsmitha Karunanithi                             {
6035cb1dd27SAsmitha Karunanithi                                 messages::internalError(asyncResp->res);
6045cb1dd27SAsmitha Karunanithi                                 break;
6055cb1dd27SAsmitha Karunanithi                             }
6061d8782e7SNan Zhou                             timestamp = *usecsTimeStamp / 1000 / 1000;
6075cb1dd27SAsmitha Karunanithi                             break;
6085cb1dd27SAsmitha Karunanithi                         }
6095cb1dd27SAsmitha Karunanithi                     }
6105cb1dd27SAsmitha Karunanithi                 }
6115cb1dd27SAsmitha Karunanithi             }
6125cb1dd27SAsmitha Karunanithi 
6130fda0f12SGeorge Liu             if (dumpStatus !=
6140fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
61535440d18SAsmitha Karunanithi                 !dumpStatus.empty())
61635440d18SAsmitha Karunanithi             {
61735440d18SAsmitha Karunanithi                 // Dump status is not Complete
61835440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
619002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
620002d39b4SEd Tanous                                            entryID);
62135440d18SAsmitha Karunanithi                 return;
62235440d18SAsmitha Karunanithi             }
62335440d18SAsmitha Karunanithi 
6245cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
625647b3cdcSGeorge Liu                 "#LogEntry.v1_8_0.LogEntry";
626fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
6275cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
6285cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
6295cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Created"] =
6301d8782e7SNan Zhou                 crow::utility::getDateTimeUint(timestamp);
6315cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6325cb1dd27SAsmitha Karunanithi 
6335cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
6345cb1dd27SAsmitha Karunanithi             {
635d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
636d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
637fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
638fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6395cb1dd27SAsmitha Karunanithi             }
6405cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
6415cb1dd27SAsmitha Karunanithi             {
642d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
643002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
644d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
645fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
646fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6475cb1dd27SAsmitha Karunanithi             }
6485cb1dd27SAsmitha Karunanithi         }
649e05aec50SEd Tanous         if (!foundDumpEntry)
650b47452b2SAsmitha Karunanithi         {
651b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
652b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
653b47452b2SAsmitha Karunanithi             return;
654b47452b2SAsmitha Karunanithi         }
6555cb1dd27SAsmitha Karunanithi         },
6565cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6575cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6585cb1dd27SAsmitha Karunanithi }
6595cb1dd27SAsmitha Karunanithi 
6608d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6619878256fSStanley Chu                             const std::string& entryID,
662b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6635cb1dd27SAsmitha Karunanithi {
664002d39b4SEd Tanous     auto respHandler =
665002d39b4SEd Tanous         [asyncResp, entryID](const boost::system::error_code ec) {
6665cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6675cb1dd27SAsmitha Karunanithi         if (ec)
6685cb1dd27SAsmitha Karunanithi         {
6693de8d8baSGeorge Liu             if (ec.value() == EBADR)
6703de8d8baSGeorge Liu             {
6713de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6723de8d8baSGeorge Liu                 return;
6733de8d8baSGeorge Liu             }
6745cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
675fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6765cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6775cb1dd27SAsmitha Karunanithi             return;
6785cb1dd27SAsmitha Karunanithi         }
6795cb1dd27SAsmitha Karunanithi     };
6805cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6815cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
682b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
683b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
684b47452b2SAsmitha Karunanithi             entryID,
6855cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6865cb1dd27SAsmitha Karunanithi }
6875cb1dd27SAsmitha Karunanithi 
6888d1b46d7Szhanghch05 inline void
68998be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
6908d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6918d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
692a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
693a43be80fSAsmitha Karunanithi {
694a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
6956145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
696a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
697a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
698cb13a392SEd Tanous         if (err)
699cb13a392SEd Tanous         {
7006145ed6fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Error in creating a dump";
7016145ed6fSAsmitha Karunanithi             taskData->state = "Cancelled";
7026145ed6fSAsmitha Karunanithi             return task::completed;
703cb13a392SEd Tanous         }
704b9d36b47SEd Tanous 
705b9d36b47SEd Tanous         dbus::utility::DBusInteracesMap interfacesList;
706a43be80fSAsmitha Karunanithi 
707a43be80fSAsmitha Karunanithi         sdbusplus::message::object_path objPath;
708a43be80fSAsmitha Karunanithi 
709a43be80fSAsmitha Karunanithi         m.read(objPath, interfacesList);
710a43be80fSAsmitha Karunanithi 
711b47452b2SAsmitha Karunanithi         if (objPath.str ==
712b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
713b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
714b47452b2SAsmitha Karunanithi                 "/entry/" + std::to_string(dumpId))
715a43be80fSAsmitha Karunanithi         {
716a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
717a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
718a43be80fSAsmitha Karunanithi 
719a43be80fSAsmitha Karunanithi             std::string headerLoc =
720a43be80fSAsmitha Karunanithi                 "Location: " + dumpPath + std::to_string(dumpId);
721002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
722a43be80fSAsmitha Karunanithi 
723a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
724b47452b2SAsmitha Karunanithi             return task::completed;
7256145ed6fSAsmitha Karunanithi         }
726a43be80fSAsmitha Karunanithi         return task::completed;
727a43be80fSAsmitha Karunanithi         },
7284978b63fSJason M. Bills         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
729a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
730a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
731a43be80fSAsmitha Karunanithi 
732a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
733a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
73498be3e39SEd Tanous     task->payload.emplace(std::move(payload));
735a43be80fSAsmitha Karunanithi }
736a43be80fSAsmitha Karunanithi 
7378d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7388d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
739a43be80fSAsmitha Karunanithi {
740fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
741fdd26906SClaire Weinan     if (dumpPath.empty())
742a43be80fSAsmitha Karunanithi     {
743a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
744a43be80fSAsmitha Karunanithi         return;
745a43be80fSAsmitha Karunanithi     }
746a43be80fSAsmitha Karunanithi 
747a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
748a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
749a43be80fSAsmitha Karunanithi 
75015ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
751a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
752a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
753a43be80fSAsmitha Karunanithi     {
754a43be80fSAsmitha Karunanithi         return;
755a43be80fSAsmitha Karunanithi     }
756a43be80fSAsmitha Karunanithi 
757a43be80fSAsmitha Karunanithi     if (dumpType == "System")
758a43be80fSAsmitha Karunanithi     {
759a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
760a43be80fSAsmitha Karunanithi         {
7614978b63fSJason M. Bills             BMCWEB_LOG_ERROR
7624978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
763a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
764a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
765a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
766a43be80fSAsmitha Karunanithi             return;
767a43be80fSAsmitha Karunanithi         }
7683174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
769a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
770a43be80fSAsmitha Karunanithi         {
771a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
772ace85d60SEd Tanous             messages::internalError(asyncResp->res);
773a43be80fSAsmitha Karunanithi             return;
774a43be80fSAsmitha Karunanithi         }
775a43be80fSAsmitha Karunanithi     }
776a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
777a43be80fSAsmitha Karunanithi     {
778a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
779a43be80fSAsmitha Karunanithi         {
7800fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
7810fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
782a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
783a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
784a43be80fSAsmitha Karunanithi             return;
785a43be80fSAsmitha Karunanithi         }
7863174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
787a43be80fSAsmitha Karunanithi         {
788a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
789a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
790ace85d60SEd Tanous             messages::internalError(asyncResp->res);
791a43be80fSAsmitha Karunanithi             return;
792a43be80fSAsmitha Karunanithi         }
793a43be80fSAsmitha Karunanithi     }
794a43be80fSAsmitha Karunanithi 
795a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
79698be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
79798be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
79898be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
799a43be80fSAsmitha Karunanithi         if (ec)
800a43be80fSAsmitha Karunanithi         {
801a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
802a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
803a43be80fSAsmitha Karunanithi             return;
804a43be80fSAsmitha Karunanithi         }
805a43be80fSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
806a43be80fSAsmitha Karunanithi 
807002d39b4SEd Tanous         createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath,
808002d39b4SEd Tanous                                dumpType);
809a43be80fSAsmitha Karunanithi         },
810b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
811b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
812b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
813a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
814a43be80fSAsmitha Karunanithi }
815a43be80fSAsmitha Karunanithi 
8168d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8178d1b46d7Szhanghch05                       const std::string& dumpType)
81880319af1SAsmitha Karunanithi {
819b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
820b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8218d1b46d7Szhanghch05 
82280319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
823b9d36b47SEd Tanous         [asyncResp, dumpType](
824b9d36b47SEd Tanous             const boost::system::error_code ec,
825b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
82680319af1SAsmitha Karunanithi         if (ec)
82780319af1SAsmitha Karunanithi         {
82880319af1SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
82980319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
83080319af1SAsmitha Karunanithi             return;
83180319af1SAsmitha Karunanithi         }
83280319af1SAsmitha Karunanithi 
83380319af1SAsmitha Karunanithi         for (const std::string& path : subTreePaths)
83480319af1SAsmitha Karunanithi         {
8352dfd18efSEd Tanous             sdbusplus::message::object_path objPath(path);
8362dfd18efSEd Tanous             std::string logID = objPath.filename();
8372dfd18efSEd Tanous             if (logID.empty())
83880319af1SAsmitha Karunanithi             {
8392dfd18efSEd Tanous                 continue;
84080319af1SAsmitha Karunanithi             }
8412dfd18efSEd Tanous             deleteDumpEntry(asyncResp, logID, dumpType);
84280319af1SAsmitha Karunanithi         }
84380319af1SAsmitha Karunanithi         },
84480319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
84580319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
84680319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
847b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
848b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
849b47452b2SAsmitha Karunanithi                                    dumpType});
85080319af1SAsmitha Karunanithi }
85180319af1SAsmitha Karunanithi 
852b9d36b47SEd Tanous inline static void
853b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
854b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
855b9d36b47SEd Tanous                              std::string& logfile)
856043a0536SJohnathan Mantey {
857043a0536SJohnathan Mantey     for (auto property : params)
858043a0536SJohnathan Mantey     {
859043a0536SJohnathan Mantey         if (property.first == "Timestamp")
860043a0536SJohnathan Mantey         {
861043a0536SJohnathan Mantey             const std::string* value =
8628d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
863043a0536SJohnathan Mantey             if (value != nullptr)
864043a0536SJohnathan Mantey             {
865043a0536SJohnathan Mantey                 timestamp = *value;
866043a0536SJohnathan Mantey             }
867043a0536SJohnathan Mantey         }
868043a0536SJohnathan Mantey         else if (property.first == "Filename")
869043a0536SJohnathan Mantey         {
870043a0536SJohnathan Mantey             const std::string* value =
8718d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
872043a0536SJohnathan Mantey             if (value != nullptr)
873043a0536SJohnathan Mantey             {
874043a0536SJohnathan Mantey                 filename = *value;
875043a0536SJohnathan Mantey             }
876043a0536SJohnathan Mantey         }
877043a0536SJohnathan Mantey         else if (property.first == "Log")
878043a0536SJohnathan Mantey         {
879043a0536SJohnathan Mantey             const std::string* value =
8808d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
881043a0536SJohnathan Mantey             if (value != nullptr)
882043a0536SJohnathan Mantey             {
883043a0536SJohnathan Mantey                 logfile = *value;
884043a0536SJohnathan Mantey             }
885043a0536SJohnathan Mantey         }
886043a0536SJohnathan Mantey     }
887043a0536SJohnathan Mantey }
888043a0536SJohnathan Mantey 
889a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
8907e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
8911da66f75SEd Tanous {
892c4bf6374SJason M. Bills     /**
893c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
894c4bf6374SJason M. Bills      */
8957e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
896ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
897002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
898002d39b4SEd Tanous             [&app](const crow::Request& req,
899002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9003ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
901c4bf6374SJason M. Bills         {
90245ca1b86SEd Tanous             return;
90345ca1b86SEd Tanous         }
9047e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
9057e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
906c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
907c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
908c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
909029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
91045ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
911c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
912c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
913002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
914c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
9151476687dSEd Tanous         nlohmann::json::object_t eventLog;
9161476687dSEd Tanous         eventLog["@odata.id"] =
9171476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
9181476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
9195cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
9201476687dSEd Tanous         nlohmann::json::object_t dumpLog;
921002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
9221476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
923c9bb6861Sraviteja-b #endif
924c9bb6861Sraviteja-b 
925d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
9261476687dSEd Tanous         nlohmann::json::object_t crashdump;
9271476687dSEd Tanous         crashdump["@odata.id"] =
9281476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
9291476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
930d53dd41fSJason M. Bills #endif
931b7028ebfSSpencer Ku 
932b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
9331476687dSEd Tanous         nlohmann::json::object_t hostlogger;
9341476687dSEd Tanous         hostlogger["@odata.id"] =
9351476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
9361476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
937b7028ebfSSpencer Ku #endif
938c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
939c4bf6374SJason M. Bills             logServiceArray.size();
940a3316fc6SZhikuiRen 
941a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
94245ca1b86SEd Tanous             [asyncResp](const boost::system::error_code ec,
943b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
944b9d36b47SEd Tanous                             subtreePath) {
945a3316fc6SZhikuiRen             if (ec)
946a3316fc6SZhikuiRen             {
947a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
948a3316fc6SZhikuiRen                 return;
949a3316fc6SZhikuiRen             }
950a3316fc6SZhikuiRen 
95155f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
952a3316fc6SZhikuiRen             {
953a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
954a3316fc6SZhikuiRen                 {
95523a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
956a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
95723a21a1cSEd Tanous                     logServiceArrayLocal.push_back(
9580fda0f12SGeorge Liu                         {{"@odata.id",
9590fda0f12SGeorge Liu                           "/redfish/v1/Systems/system/LogServices/PostCodes"}});
96045ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
96123a21a1cSEd Tanous                         logServiceArrayLocal.size();
962a3316fc6SZhikuiRen                     return;
963a3316fc6SZhikuiRen                 }
964a3316fc6SZhikuiRen             }
965a3316fc6SZhikuiRen             },
966a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
967a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
96845ca1b86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
96945ca1b86SEd Tanous             std::array<const char*, 1>{postCodeIface});
9707e860f15SJohn Edward Broadbent         });
971c4bf6374SJason M. Bills }
972c4bf6374SJason M. Bills 
9737e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
974c4bf6374SJason M. Bills {
9757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
976ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
977002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
978002d39b4SEd Tanous             [&app](const crow::Request& req,
979002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
98145ca1b86SEd Tanous         {
98245ca1b86SEd Tanous             return;
98345ca1b86SEd Tanous         }
984c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
985029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
986c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
987c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
988c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
989002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
990c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
991c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
9927c8c4058STejas Patil 
9937c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
9947c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
9957c8c4058STejas Patil 
9967c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
9977c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
9987c8c4058STejas Patil             redfishDateTimeOffset.second;
9997c8c4058STejas Patil 
10001476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
10011476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1002e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1003e7d6c8b2SGunnar Mills 
10040fda0f12SGeorge Liu             {"target",
10050fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10067e860f15SJohn Edward Broadbent         });
1007489640c6SJason M. Bills }
1008489640c6SJason M. Bills 
10097e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1010489640c6SJason M. Bills {
10114978b63fSJason M. Bills     BMCWEB_ROUTE(
10124978b63fSJason M. Bills         app,
10134978b63fSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
1014432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10157e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
101645ca1b86SEd Tanous             [&app](const crow::Request& req,
10177e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
10183ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
101945ca1b86SEd Tanous         {
102045ca1b86SEd Tanous             return;
102145ca1b86SEd Tanous         }
1022489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
1023489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1024489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
1025489640c6SJason M. Bills         {
1026489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
1027489640c6SJason M. Bills             {
1028489640c6SJason M. Bills                 std::error_code ec;
1029489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
1030489640c6SJason M. Bills             }
1031489640c6SJason M. Bills         }
1032489640c6SJason M. Bills 
1033489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
1034489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
1035489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
1036489640c6SJason M. Bills             if (ec)
1037489640c6SJason M. Bills             {
1038002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
1039489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
1040489640c6SJason M. Bills                 return;
1041489640c6SJason M. Bills             }
1042489640c6SJason M. Bills 
1043489640c6SJason M. Bills             messages::success(asyncResp->res);
1044489640c6SJason M. Bills             },
1045489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1046002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1047002d39b4SEd Tanous             "replace");
10487e860f15SJohn Edward Broadbent         });
1049c4bf6374SJason M. Bills }
1050c4bf6374SJason M. Bills 
105195820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1052b5a76932SEd Tanous                                  const std::string& logEntry,
1053de703c5dSJason M. Bills                                  nlohmann::json::object_t& logEntryJson)
1054c4bf6374SJason M. Bills {
105595820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1056cd225da8SJason M. Bills     // First get the Timestamp
1057f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1058cd225da8SJason M. Bills     if (space == std::string::npos)
105995820184SJason M. Bills     {
106095820184SJason M. Bills         return 1;
106195820184SJason M. Bills     }
1062cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1063cd225da8SJason M. Bills     // Then get the log contents
1064f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1065cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1066cd225da8SJason M. Bills     {
1067cd225da8SJason M. Bills         return 1;
1068cd225da8SJason M. Bills     }
1069cd225da8SJason M. Bills     std::string_view entry(logEntry);
1070cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1071cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1072cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1073cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1074cd225da8SJason M. Bills                  boost::token_compress_on);
1075cd225da8SJason M. Bills     // We need at least a MessageId to be valid
107626f6976fSEd Tanous     if (logEntryFields.empty())
1077cd225da8SJason M. Bills     {
1078cd225da8SJason M. Bills         return 1;
1079cd225da8SJason M. Bills     }
1080cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
108195820184SJason M. Bills 
10824851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1083fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1084c4bf6374SJason M. Bills 
108554417b02SSui Chen     if (message == nullptr)
1086c4bf6374SJason M. Bills     {
108754417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
108854417b02SSui Chen         return 0;
1089c4bf6374SJason M. Bills     }
1090c4bf6374SJason M. Bills 
109154417b02SSui Chen     std::string msg = message->message;
109254417b02SSui Chen 
109315a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
109426702d01SEd Tanous     std::span<std::string> messageArgs;
109515a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
109615a86ff6SJason M. Bills     {
109715a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
109815a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
109915a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
110015a86ff6SJason M. Bills         if (!messageArgsStart.empty())
110115a86ff6SJason M. Bills         {
110215a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
110315a86ff6SJason M. Bills         }
110415a86ff6SJason M. Bills 
110523a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1106c4bf6374SJason M. Bills 
11074851d45dSJason M. Bills         // Fill the MessageArgs into the Message
110895820184SJason M. Bills         int i = 0;
110995820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11104851d45dSJason M. Bills         {
111195820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11124851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11134851d45dSJason M. Bills             if (argPos != std::string::npos)
11144851d45dSJason M. Bills             {
111595820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11164851d45dSJason M. Bills             }
11174851d45dSJason M. Bills         }
111815a86ff6SJason M. Bills     }
11194851d45dSJason M. Bills 
112095820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
112195820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
112295820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1123f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1124f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
112595820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1126c4bf6374SJason M. Bills     {
112795820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1128c4bf6374SJason M. Bills     }
1129c4bf6374SJason M. Bills 
1130c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
113195820184SJason M. Bills     logEntryJson = {
1132647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1133029573d4SEd Tanous         {"@odata.id",
1134897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
113595820184SJason M. Bills              logEntryID},
1136c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
113795820184SJason M. Bills         {"Id", logEntryID},
113895820184SJason M. Bills         {"Message", std::move(msg)},
113995820184SJason M. Bills         {"MessageId", std::move(messageID)},
1140f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1141c4bf6374SJason M. Bills         {"EntryType", "Event"},
114254417b02SSui Chen         {"Severity", message->messageSeverity},
114395820184SJason M. Bills         {"Created", std::move(timestamp)}};
1144c4bf6374SJason M. Bills     return 0;
1145c4bf6374SJason M. Bills }
1146c4bf6374SJason M. Bills 
11477e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1148c4bf6374SJason M. Bills {
11497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11507e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11518b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1152002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1153002d39b4SEd Tanous             [&app](const crow::Request& req,
1154002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1155c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1156c937d2bfSEd Tanous             .canDelegateTop = true,
1157c937d2bfSEd Tanous             .canDelegateSkip = true,
1158c937d2bfSEd Tanous         };
1159c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1160c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
11613ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1162c4bf6374SJason M. Bills         {
1163c4bf6374SJason M. Bills             return;
1164c4bf6374SJason M. Bills         }
11657e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
11667e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1167c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1168c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1169c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1170029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1171c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1172c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1173c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1174cb92c03bSAndrew Geissler 
11754978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1176c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
11777e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
11787e860f15SJohn Edward Broadbent         // entry
117995820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
118095820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1181b01bf299SEd Tanous         uint64_t entryCount = 0;
1182cd225da8SJason M. Bills         std::string logEntry;
118395820184SJason M. Bills 
11847e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
11857e860f15SJohn Edward Broadbent         // backwards
1186002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1187002d39b4SEd Tanous              it++)
1188c4bf6374SJason M. Bills         {
1189cd225da8SJason M. Bills             std::ifstream logStream(*it);
119095820184SJason M. Bills             if (!logStream.is_open())
1191c4bf6374SJason M. Bills             {
1192c4bf6374SJason M. Bills                 continue;
1193c4bf6374SJason M. Bills             }
1194c4bf6374SJason M. Bills 
1195e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1196e85d6b16SJason M. Bills             bool firstEntry = true;
119795820184SJason M. Bills             while (std::getline(logStream, logEntry))
119895820184SJason M. Bills             {
1199c4bf6374SJason M. Bills                 std::string idStr;
1200e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1201c4bf6374SJason M. Bills                 {
1202c4bf6374SJason M. Bills                     continue;
1203c4bf6374SJason M. Bills                 }
1204e85d6b16SJason M. Bills                 firstEntry = false;
1205e85d6b16SJason M. Bills 
1206de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1207002d39b4SEd Tanous                 if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) != 0)
1208c4bf6374SJason M. Bills                 {
1209c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1210c4bf6374SJason M. Bills                     return;
1211c4bf6374SJason M. Bills                 }
1212de703c5dSJason M. Bills 
1213de703c5dSJason M. Bills                 if (bmcLogEntry.empty())
1214de703c5dSJason M. Bills                 {
1215de703c5dSJason M. Bills                     continue;
1216de703c5dSJason M. Bills                 }
1217de703c5dSJason M. Bills 
1218de703c5dSJason M. Bills                 entryCount++;
1219de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1220de703c5dSJason M. Bills                 // start) and top (number of entries to display)
1221de703c5dSJason M. Bills                 if (entryCount <= delegatedQuery.skip ||
1222de703c5dSJason M. Bills                     entryCount > delegatedQuery.skip + delegatedQuery.top)
1223de703c5dSJason M. Bills                 {
1224de703c5dSJason M. Bills                     continue;
1225de703c5dSJason M. Bills                 }
1226de703c5dSJason M. Bills 
1227de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1228c4bf6374SJason M. Bills             }
122995820184SJason M. Bills         }
1230c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1231c937d2bfSEd Tanous         if (delegatedQuery.skip + delegatedQuery.top < entryCount)
1232c4bf6374SJason M. Bills         {
1233c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
12344978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
1235c937d2bfSEd Tanous                 std::to_string(delegatedQuery.skip + delegatedQuery.top);
1236c4bf6374SJason M. Bills         }
12377e860f15SJohn Edward Broadbent         });
1238897967deSJason M. Bills }
1239897967deSJason M. Bills 
12407e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1241897967deSJason M. Bills {
12427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12437e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1244ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
124645ca1b86SEd Tanous             [&app](const crow::Request& req,
12477e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12487e860f15SJohn Edward Broadbent                    const std::string& param) {
12493ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
125045ca1b86SEd Tanous         {
125145ca1b86SEd Tanous             return;
125245ca1b86SEd Tanous         }
12537e860f15SJohn Edward Broadbent         const std::string& targetID = param;
12548d1b46d7Szhanghch05 
12557e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
12567e860f15SJohn Edward Broadbent         // entry to find the target entry
1257897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1258897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1259897967deSJason M. Bills         std::string logEntry;
1260897967deSJason M. Bills 
12617e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
12627e860f15SJohn Edward Broadbent         // backwards
1263002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1264002d39b4SEd Tanous              it++)
1265897967deSJason M. Bills         {
1266897967deSJason M. Bills             std::ifstream logStream(*it);
1267897967deSJason M. Bills             if (!logStream.is_open())
1268897967deSJason M. Bills             {
1269897967deSJason M. Bills                 continue;
1270897967deSJason M. Bills             }
1271897967deSJason M. Bills 
1272897967deSJason M. Bills             // Reset the unique ID on the first entry
1273897967deSJason M. Bills             bool firstEntry = true;
1274897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1275897967deSJason M. Bills             {
1276897967deSJason M. Bills                 std::string idStr;
1277897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1278897967deSJason M. Bills                 {
1279897967deSJason M. Bills                     continue;
1280897967deSJason M. Bills                 }
1281897967deSJason M. Bills                 firstEntry = false;
1282897967deSJason M. Bills 
1283897967deSJason M. Bills                 if (idStr == targetID)
1284897967deSJason M. Bills                 {
1285de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1286de703c5dSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) !=
1287de703c5dSJason M. Bills                         0)
1288897967deSJason M. Bills                     {
1289897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1290897967deSJason M. Bills                         return;
1291897967deSJason M. Bills                     }
1292*d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1293897967deSJason M. Bills                     return;
1294897967deSJason M. Bills                 }
1295897967deSJason M. Bills             }
1296897967deSJason M. Bills         }
1297897967deSJason M. Bills         // Requested ID was not found
1298002d39b4SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
1299002d39b4SEd Tanous                                        crow::utility::urlFromPieces(targetID));
13007e860f15SJohn Edward Broadbent         });
130108a4e4b5SAnthony Wilson }
130208a4e4b5SAnthony Wilson 
13037e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
130408a4e4b5SAnthony Wilson {
13057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13067e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1307ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1308002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1309002d39b4SEd Tanous             [&app](const crow::Request& req,
1310002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
13113ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
131245ca1b86SEd Tanous         {
131345ca1b86SEd Tanous             return;
131445ca1b86SEd Tanous         }
13157e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
13167e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
131708a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
131808a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
131908a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
132008a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
132108a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
132208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
132308a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
132408a4e4b5SAnthony Wilson 
1325cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1326cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1327cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1328cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1329914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1330cb92c03bSAndrew Geissler             if (ec)
1331cb92c03bSAndrew Geissler             {
1332cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1333cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1334002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1335cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1336cb92c03bSAndrew Geissler                 return;
1337cb92c03bSAndrew Geissler             }
1338002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1339cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
13409eb808c1SEd Tanous             for (const auto& objectPath : resp)
1341cb92c03bSAndrew Geissler             {
1342914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1343c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1344c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1345914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1346914e2d5dSEd Tanous                 const std::string* message = nullptr;
1347914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
134875710de2SXiaochao Ma                 bool resolved = false;
13499eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1350f86bb901SAdriana Kobylak                 {
1351f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1352f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1353f86bb901SAdriana Kobylak                     {
1354002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1355cb92c03bSAndrew Geissler                         {
1356cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1357cb92c03bSAndrew Geissler                             {
1358002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1359cb92c03bSAndrew Geissler                             }
1360cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1361cb92c03bSAndrew Geissler                             {
1362002d39b4SEd Tanous                                 timestamp =
1363002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13647e860f15SJohn Edward Broadbent                             }
1365002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
13667e860f15SJohn Edward Broadbent                             {
1367002d39b4SEd Tanous                                 updateTimestamp =
1368002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13697e860f15SJohn Edward Broadbent                             }
13707e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
13717e860f15SJohn Edward Broadbent                             {
13727e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
13737e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13747e860f15SJohn Edward Broadbent                             }
13757e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
13767e860f15SJohn Edward Broadbent                             {
13777e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
13787e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13797e860f15SJohn Edward Broadbent                             }
13807e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
13817e860f15SJohn Edward Broadbent                             {
1382914e2d5dSEd Tanous                                 const bool* resolveptr =
1383002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
13847e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
13857e860f15SJohn Edward Broadbent                                 {
1386002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
13877e860f15SJohn Edward Broadbent                                     return;
13887e860f15SJohn Edward Broadbent                                 }
13897e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
13907e860f15SJohn Edward Broadbent                             }
13917e860f15SJohn Edward Broadbent                         }
13927e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
13937e860f15SJohn Edward Broadbent                             severity == nullptr)
13947e860f15SJohn Edward Broadbent                         {
13957e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
13967e860f15SJohn Edward Broadbent                             return;
13977e860f15SJohn Edward Broadbent                         }
13987e860f15SJohn Edward Broadbent                     }
13997e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
14007e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
14017e860f15SJohn Edward Broadbent                     {
1402002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
14037e860f15SJohn Edward Broadbent                         {
14047e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
14057e860f15SJohn Edward Broadbent                             {
14067e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
14077e860f15SJohn Edward Broadbent                                     &propertyMap.second);
14087e860f15SJohn Edward Broadbent                             }
14097e860f15SJohn Edward Broadbent                         }
14107e860f15SJohn Edward Broadbent                     }
14117e860f15SJohn Edward Broadbent                 }
14127e860f15SJohn Edward Broadbent                 // Object path without the
14137e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
14147e860f15SJohn Edward Broadbent                 // and continue.
14157e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1416c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1417c419c759SEd Tanous                     updateTimestamp == nullptr)
14187e860f15SJohn Edward Broadbent                 {
14197e860f15SJohn Edward Broadbent                     continue;
14207e860f15SJohn Edward Broadbent                 }
14217e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
14227e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
14237e860f15SJohn Edward Broadbent                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14247e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
14250fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14267e860f15SJohn Edward Broadbent                     std::to_string(*id);
14277e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
14287e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
14297e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
14307e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
14317e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
14327e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
14337e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
14347e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
1435c419c759SEd Tanous                     crow::utility::getDateTimeUintMs(*timestamp);
14367e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
1437c419c759SEd Tanous                     crow::utility::getDateTimeUintMs(*updateTimestamp);
14387e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
14397e860f15SJohn Edward Broadbent                 {
14407e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
14410fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14427e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
14437e860f15SJohn Edward Broadbent                 }
14447e860f15SJohn Edward Broadbent             }
1445002d39b4SEd Tanous             std::sort(
1446002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1447002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
14487e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
14497e860f15SJohn Edward Broadbent                 });
14507e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
14517e860f15SJohn Edward Broadbent                 entriesArray.size();
14527e860f15SJohn Edward Broadbent             },
14537e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
14547e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14557e860f15SJohn Edward Broadbent         });
14567e860f15SJohn Edward Broadbent }
14577e860f15SJohn Edward Broadbent 
14587e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
14597e860f15SJohn Edward Broadbent {
14607e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
14617e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1462ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1463002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1464002d39b4SEd Tanous             [&app](const crow::Request& req,
14657e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
146645ca1b86SEd Tanous                    const std::string& param) {
14673ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
14687e860f15SJohn Edward Broadbent         {
146945ca1b86SEd Tanous             return;
147045ca1b86SEd Tanous         }
14717e860f15SJohn Edward Broadbent         std::string entryID = param;
14727e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
14737e860f15SJohn Edward Broadbent 
14747e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
14757e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
14767e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
1477002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1478b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
14797e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
14807e860f15SJohn Edward Broadbent             {
1481002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1482002d39b4SEd Tanous                                            entryID);
14837e860f15SJohn Edward Broadbent                 return;
14847e860f15SJohn Edward Broadbent             }
14857e860f15SJohn Edward Broadbent             if (ec)
14867e860f15SJohn Edward Broadbent             {
14870fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1488002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
14897e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
14907e860f15SJohn Edward Broadbent                 return;
14917e860f15SJohn Edward Broadbent             }
1492914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1493c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1494c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1495914e2d5dSEd Tanous             const std::string* severity = nullptr;
1496914e2d5dSEd Tanous             const std::string* message = nullptr;
1497914e2d5dSEd Tanous             const std::string* filePath = nullptr;
14987e860f15SJohn Edward Broadbent             bool resolved = false;
14997e860f15SJohn Edward Broadbent 
15009eb808c1SEd Tanous             for (const auto& propertyMap : resp)
15017e860f15SJohn Edward Broadbent             {
15027e860f15SJohn Edward Broadbent                 if (propertyMap.first == "Id")
15037e860f15SJohn Edward Broadbent                 {
15047e860f15SJohn Edward Broadbent                     id = std::get_if<uint32_t>(&propertyMap.second);
15057e860f15SJohn Edward Broadbent                 }
15067e860f15SJohn Edward Broadbent                 else if (propertyMap.first == "Timestamp")
15077e860f15SJohn Edward Broadbent                 {
1508002d39b4SEd Tanous                     timestamp = std::get_if<uint64_t>(&propertyMap.second);
1509ebd45906SGeorge Liu                 }
1510d139c236SGeorge Liu                 else if (propertyMap.first == "UpdateTimestamp")
1511d139c236SGeorge Liu                 {
1512ebd45906SGeorge Liu                     updateTimestamp =
1513c419c759SEd Tanous                         std::get_if<uint64_t>(&propertyMap.second);
1514ebd45906SGeorge Liu                 }
1515cb92c03bSAndrew Geissler                 else if (propertyMap.first == "Severity")
1516cb92c03bSAndrew Geissler                 {
1517002d39b4SEd Tanous                     severity = std::get_if<std::string>(&propertyMap.second);
1518cb92c03bSAndrew Geissler                 }
1519cb92c03bSAndrew Geissler                 else if (propertyMap.first == "Message")
1520cb92c03bSAndrew Geissler                 {
1521002d39b4SEd Tanous                     message = std::get_if<std::string>(&propertyMap.second);
1522ae34c8e8SAdriana Kobylak                 }
152375710de2SXiaochao Ma                 else if (propertyMap.first == "Resolved")
152475710de2SXiaochao Ma                 {
1525914e2d5dSEd Tanous                     const bool* resolveptr =
152675710de2SXiaochao Ma                         std::get_if<bool>(&propertyMap.second);
152775710de2SXiaochao Ma                     if (resolveptr == nullptr)
152875710de2SXiaochao Ma                     {
152975710de2SXiaochao Ma                         messages::internalError(asyncResp->res);
153075710de2SXiaochao Ma                         return;
153175710de2SXiaochao Ma                     }
153275710de2SXiaochao Ma                     resolved = *resolveptr;
153375710de2SXiaochao Ma                 }
15347e860f15SJohn Edward Broadbent                 else if (propertyMap.first == "Path")
1535f86bb901SAdriana Kobylak                 {
1536002d39b4SEd Tanous                     filePath = std::get_if<std::string>(&propertyMap.second);
1537f86bb901SAdriana Kobylak                 }
1538f86bb901SAdriana Kobylak             }
1539002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
1540002d39b4SEd Tanous                 timestamp == nullptr || updateTimestamp == nullptr)
1541f86bb901SAdriana Kobylak             {
1542ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1543271584abSEd Tanous                 return;
1544271584abSEd Tanous             }
1545f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
1546f86bb901SAdriana Kobylak                 "#LogEntry.v1_8_0.LogEntry";
1547f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
15480fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1549f86bb901SAdriana Kobylak                 std::to_string(*id);
155045ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1551f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1552f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1553f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
1554f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1555f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1556f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1557f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
1558c419c759SEd Tanous                 crow::utility::getDateTimeUintMs(*timestamp);
1559f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
1560c419c759SEd Tanous                 crow::utility::getDateTimeUintMs(*updateTimestamp);
1561f86bb901SAdriana Kobylak             if (filePath != nullptr)
1562f86bb901SAdriana Kobylak             {
1563f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1564e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1565e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1566f86bb901SAdriana Kobylak             }
1567cb92c03bSAndrew Geissler             },
1568cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1569cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1570f86bb901SAdriana Kobylak             "org.freedesktop.DBus.Properties", "GetAll", "");
15717e860f15SJohn Edward Broadbent         });
1572336e96c6SChicago Duan 
15737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15747e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1575ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
15767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
157745ca1b86SEd Tanous             [&app](const crow::Request& req,
15787e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15797e860f15SJohn Edward Broadbent                    const std::string& entryId) {
15803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
158145ca1b86SEd Tanous         {
158245ca1b86SEd Tanous             return;
158345ca1b86SEd Tanous         }
158475710de2SXiaochao Ma         std::optional<bool> resolved;
158575710de2SXiaochao Ma 
158615ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
15877e860f15SJohn Edward Broadbent                                       resolved))
158875710de2SXiaochao Ma         {
158975710de2SXiaochao Ma             return;
159075710de2SXiaochao Ma         }
159175710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
159275710de2SXiaochao Ma 
159375710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
15944f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
159575710de2SXiaochao Ma             if (ec)
159675710de2SXiaochao Ma             {
159775710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
159875710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
159975710de2SXiaochao Ma                 return;
160075710de2SXiaochao Ma             }
160175710de2SXiaochao Ma             },
160275710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
160375710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
160475710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
160575710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1606168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
16077e860f15SJohn Edward Broadbent         });
160875710de2SXiaochao Ma 
16097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16107e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1611ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1612ed398213SEd Tanous 
1613002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1614002d39b4SEd Tanous             [&app](const crow::Request& req,
1615002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
161645ca1b86SEd Tanous                    const std::string& param) {
16173ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1618336e96c6SChicago Duan         {
161945ca1b86SEd Tanous             return;
162045ca1b86SEd Tanous         }
1621336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1622336e96c6SChicago Duan 
16237e860f15SJohn Edward Broadbent         std::string entryID = param;
1624336e96c6SChicago Duan 
1625336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1626336e96c6SChicago Duan 
1627336e96c6SChicago Duan         // Process response from Logging service.
1628002d39b4SEd Tanous         auto respHandler =
1629002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1630002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1631336e96c6SChicago Duan             if (ec)
1632336e96c6SChicago Duan             {
16333de8d8baSGeorge Liu                 if (ec.value() == EBADR)
16343de8d8baSGeorge Liu                 {
163545ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
163645ca1b86SEd Tanous                                                entryID);
16373de8d8baSGeorge Liu                     return;
16383de8d8baSGeorge Liu                 }
1639336e96c6SChicago Duan                 // TODO Handle for specific error code
16400fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
16410fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1642336e96c6SChicago Duan                     << ec;
1643336e96c6SChicago Duan                 asyncResp->res.result(
1644336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1645336e96c6SChicago Duan                 return;
1646336e96c6SChicago Duan             }
1647336e96c6SChicago Duan 
1648336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1649336e96c6SChicago Duan         };
1650336e96c6SChicago Duan 
1651336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1652336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1653336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1654336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1655336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
16567e860f15SJohn Edward Broadbent         });
1657400fd1fbSAdriana Kobylak }
1658400fd1fbSAdriana Kobylak 
16597e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1660400fd1fbSAdriana Kobylak {
16610fda0f12SGeorge Liu     BMCWEB_ROUTE(
16620fda0f12SGeorge Liu         app,
16630fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1664ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
16657e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
166645ca1b86SEd Tanous             [&app](const crow::Request& req,
16677e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
166845ca1b86SEd Tanous                    const std::string& param) {
16693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16707e860f15SJohn Edward Broadbent         {
167145ca1b86SEd Tanous             return;
167245ca1b86SEd Tanous         }
1673002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
1674400fd1fbSAdriana Kobylak         {
1675002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1676400fd1fbSAdriana Kobylak             return;
1677400fd1fbSAdriana Kobylak         }
1678400fd1fbSAdriana Kobylak 
16797e860f15SJohn Edward Broadbent         std::string entryID = param;
1680400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1681400fd1fbSAdriana Kobylak 
1682400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1683002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1684400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1685400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1686400fd1fbSAdriana Kobylak             {
1687002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1688002d39b4SEd Tanous                                            entryID);
1689400fd1fbSAdriana Kobylak                 return;
1690400fd1fbSAdriana Kobylak             }
1691400fd1fbSAdriana Kobylak             if (ec)
1692400fd1fbSAdriana Kobylak             {
1693400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1694400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1695400fd1fbSAdriana Kobylak                 return;
1696400fd1fbSAdriana Kobylak             }
1697400fd1fbSAdriana Kobylak 
1698400fd1fbSAdriana Kobylak             int fd = -1;
1699400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1700400fd1fbSAdriana Kobylak             if (fd == -1)
1701400fd1fbSAdriana Kobylak             {
1702400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1703400fd1fbSAdriana Kobylak                 return;
1704400fd1fbSAdriana Kobylak             }
1705400fd1fbSAdriana Kobylak 
1706400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1707400fd1fbSAdriana Kobylak             if (size == -1)
1708400fd1fbSAdriana Kobylak             {
1709400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1710400fd1fbSAdriana Kobylak                 return;
1711400fd1fbSAdriana Kobylak             }
1712400fd1fbSAdriana Kobylak 
1713400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1714400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1715400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1716400fd1fbSAdriana Kobylak             {
1717002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1718400fd1fbSAdriana Kobylak                                  << maxFileSize;
1719400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1720400fd1fbSAdriana Kobylak                 return;
1721400fd1fbSAdriana Kobylak             }
1722400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1723400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1724400fd1fbSAdriana Kobylak             if (rc == -1)
1725400fd1fbSAdriana Kobylak             {
1726400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1727400fd1fbSAdriana Kobylak                 return;
1728400fd1fbSAdriana Kobylak             }
1729400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1730400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1731400fd1fbSAdriana Kobylak             {
1732400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1733400fd1fbSAdriana Kobylak                 return;
1734400fd1fbSAdriana Kobylak             }
1735400fd1fbSAdriana Kobylak             close(fd);
1736400fd1fbSAdriana Kobylak 
1737400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1738002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1739400fd1fbSAdriana Kobylak 
1740400fd1fbSAdriana Kobylak             asyncResp->res.addHeader("Content-Type",
1741400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1742002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
1743400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1744400fd1fbSAdriana Kobylak             },
1745400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1746400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1747400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
17487e860f15SJohn Edward Broadbent         });
17491da66f75SEd Tanous }
17501da66f75SEd Tanous 
1751b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1752b7028ebfSSpencer Ku 
1753b7028ebfSSpencer Ku inline bool
1754b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1755b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1756b7028ebfSSpencer Ku {
1757b7028ebfSSpencer Ku     std::error_code ec;
1758b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1759b7028ebfSSpencer Ku     if (ec)
1760b7028ebfSSpencer Ku     {
1761b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1762b7028ebfSSpencer Ku         return false;
1763b7028ebfSSpencer Ku     }
1764b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1765b7028ebfSSpencer Ku     {
1766b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1767b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1768b7028ebfSSpencer Ku         // path
1769b7028ebfSSpencer Ku         if (boost::starts_with(filename, "log"))
1770b7028ebfSSpencer Ku         {
1771b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1772b7028ebfSSpencer Ku         }
1773b7028ebfSSpencer Ku     }
1774b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1775b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1776b7028ebfSSpencer Ku     // descending order.
1777b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1778b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1779b7028ebfSSpencer Ku 
1780b7028ebfSSpencer Ku     return true;
1781b7028ebfSSpencer Ku }
1782b7028ebfSSpencer Ku 
1783b7028ebfSSpencer Ku inline bool
1784b7028ebfSSpencer Ku     getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1785c937d2bfSEd Tanous                          uint64_t skip, uint64_t top,
1786b7028ebfSSpencer Ku                          std::vector<std::string>& logEntries, size_t& logCount)
1787b7028ebfSSpencer Ku {
1788b7028ebfSSpencer Ku     GzFileReader logFile;
1789b7028ebfSSpencer Ku 
1790b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1791b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1792b7028ebfSSpencer Ku     {
1793b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1794b7028ebfSSpencer Ku         {
1795b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1796b7028ebfSSpencer Ku             return false;
1797b7028ebfSSpencer Ku         }
1798b7028ebfSSpencer Ku     }
1799b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1800b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1801b7028ebfSSpencer Ku     if (!lastMessage.empty())
1802b7028ebfSSpencer Ku     {
1803b7028ebfSSpencer Ku         logCount++;
1804b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1805b7028ebfSSpencer Ku         {
1806b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1807b7028ebfSSpencer Ku         }
1808b7028ebfSSpencer Ku     }
1809b7028ebfSSpencer Ku     return true;
1810b7028ebfSSpencer Ku }
1811b7028ebfSSpencer Ku 
1812b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1813b7028ebfSSpencer Ku                                     const std::string& msg,
1814b7028ebfSSpencer Ku                                     nlohmann::json& logEntryJson)
1815b7028ebfSSpencer Ku {
1816b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
1817b7028ebfSSpencer Ku     logEntryJson = {
1818b7028ebfSSpencer Ku         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1819b7028ebfSSpencer Ku         {"@odata.id",
1820b7028ebfSSpencer Ku          "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1821b7028ebfSSpencer Ku              logEntryID},
1822b7028ebfSSpencer Ku         {"Name", "Host Logger Entry"},
1823b7028ebfSSpencer Ku         {"Id", logEntryID},
1824b7028ebfSSpencer Ku         {"Message", msg},
1825b7028ebfSSpencer Ku         {"EntryType", "Oem"},
1826b7028ebfSSpencer Ku         {"Severity", "OK"},
1827b7028ebfSSpencer Ku         {"OemRecordFormat", "Host Logger Entry"}};
1828b7028ebfSSpencer Ku }
1829b7028ebfSSpencer Ku 
1830b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1831b7028ebfSSpencer Ku {
1832b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1833b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
18341476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
18351476687dSEd Tanous             [&app](const crow::Request& req,
18361476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
18373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
183845ca1b86SEd Tanous         {
183945ca1b86SEd Tanous             return;
184045ca1b86SEd Tanous         }
1841b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1842b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
1843b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1844b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
1845b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1846b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1847b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
18481476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
18491476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1850b7028ebfSSpencer Ku         });
1851b7028ebfSSpencer Ku }
1852b7028ebfSSpencer Ku 
1853b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1854b7028ebfSSpencer Ku {
1855b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1856b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1857b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1858002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1859002d39b4SEd Tanous             [&app](const crow::Request& req,
1860002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1861c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1862c937d2bfSEd Tanous             .canDelegateTop = true,
1863c937d2bfSEd Tanous             .canDelegateSkip = true,
1864c937d2bfSEd Tanous         };
1865c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1866c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
18673ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1868b7028ebfSSpencer Ku         {
1869b7028ebfSSpencer Ku             return;
1870b7028ebfSSpencer Ku         }
1871b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1872b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1873b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1874b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
1875b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1876b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
1877b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
18780fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1879b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
1880b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
1881b7028ebfSSpencer Ku 
1882b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1883b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1884b7028ebfSSpencer Ku         {
1885b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1886b7028ebfSSpencer Ku             return;
1887b7028ebfSSpencer Ku         }
1888b7028ebfSSpencer Ku 
1889b7028ebfSSpencer Ku         size_t logCount = 0;
1890b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
1891b7028ebfSSpencer Ku         // control by skip and top.
1892b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
1893c937d2bfSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, delegatedQuery.skip,
1894c937d2bfSEd Tanous                                   delegatedQuery.top, logEntries, logCount))
1895b7028ebfSSpencer Ku         {
1896b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1897b7028ebfSSpencer Ku             return;
1898b7028ebfSSpencer Ku         }
1899b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
1900b7028ebfSSpencer Ku         // log count
190126f6976fSEd Tanous         if (logEntries.empty())
1902b7028ebfSSpencer Ku         {
1903b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1904b7028ebfSSpencer Ku             return;
1905b7028ebfSSpencer Ku         }
190626f6976fSEd Tanous         if (!logEntries.empty())
1907b7028ebfSSpencer Ku         {
1908b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
1909b7028ebfSSpencer Ku             {
1910b7028ebfSSpencer Ku                 logEntryArray.push_back({});
1911b7028ebfSSpencer Ku                 nlohmann::json& hostLogEntry = logEntryArray.back();
1912002d39b4SEd Tanous                 fillHostLoggerEntryJson(std::to_string(delegatedQuery.skip + i),
1913002d39b4SEd Tanous                                         logEntries[i], hostLogEntry);
1914b7028ebfSSpencer Ku             }
1915b7028ebfSSpencer Ku 
1916b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1917c937d2bfSEd Tanous             if (delegatedQuery.skip + delegatedQuery.top < logCount)
1918b7028ebfSSpencer Ku             {
1919b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
19200fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1921002d39b4SEd Tanous                     std::to_string(delegatedQuery.skip + delegatedQuery.top);
1922b7028ebfSSpencer Ku             }
1923b7028ebfSSpencer Ku         }
1924b7028ebfSSpencer Ku         });
1925b7028ebfSSpencer Ku }
1926b7028ebfSSpencer Ku 
1927b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1928b7028ebfSSpencer Ku {
1929b7028ebfSSpencer Ku     BMCWEB_ROUTE(
1930b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1931b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1932b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
193345ca1b86SEd Tanous             [&app](const crow::Request& req,
1934b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1935b7028ebfSSpencer Ku                    const std::string& param) {
19363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
193745ca1b86SEd Tanous         {
193845ca1b86SEd Tanous             return;
193945ca1b86SEd Tanous         }
1940b7028ebfSSpencer Ku         const std::string& targetID = param;
1941b7028ebfSSpencer Ku 
1942b7028ebfSSpencer Ku         uint64_t idInt = 0;
1943ca45aa3cSEd Tanous 
1944ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1945ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
1946ca45aa3cSEd Tanous 
1947ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1948b7028ebfSSpencer Ku         if (ec == std::errc::invalid_argument)
1949b7028ebfSSpencer Ku         {
1950ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1951b7028ebfSSpencer Ku             return;
1952b7028ebfSSpencer Ku         }
1953b7028ebfSSpencer Ku         if (ec == std::errc::result_out_of_range)
1954b7028ebfSSpencer Ku         {
1955ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1956b7028ebfSSpencer Ku             return;
1957b7028ebfSSpencer Ku         }
1958b7028ebfSSpencer Ku 
1959b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1960b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1961b7028ebfSSpencer Ku         {
1962b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1963b7028ebfSSpencer Ku             return;
1964b7028ebfSSpencer Ku         }
1965b7028ebfSSpencer Ku 
1966b7028ebfSSpencer Ku         size_t logCount = 0;
1967b7028ebfSSpencer Ku         uint64_t top = 1;
1968b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
1969b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
1970b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
1971b7028ebfSSpencer Ku         // get that entry
1972002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
1973002d39b4SEd Tanous                                   logCount))
1974b7028ebfSSpencer Ku         {
1975b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1976b7028ebfSSpencer Ku             return;
1977b7028ebfSSpencer Ku         }
1978b7028ebfSSpencer Ku 
1979b7028ebfSSpencer Ku         if (!logEntries.empty())
1980b7028ebfSSpencer Ku         {
1981b7028ebfSSpencer Ku             fillHostLoggerEntryJson(targetID, logEntries[0],
1982b7028ebfSSpencer Ku                                     asyncResp->res.jsonValue);
1983b7028ebfSSpencer Ku             return;
1984b7028ebfSSpencer Ku         }
1985b7028ebfSSpencer Ku 
1986b7028ebfSSpencer Ku         // Requested ID was not found
1987ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1988b7028ebfSSpencer Ku         });
1989b7028ebfSSpencer Ku }
1990b7028ebfSSpencer Ku 
1991fdd26906SClaire Weinan constexpr char const* dumpManagerIface =
1992fdd26906SClaire Weinan     "xyz.openbmc_project.Collection.DeleteAll";
1993fdd26906SClaire Weinan inline void handleLogServicesCollectionGet(
1994fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
1995fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
19961da66f75SEd Tanous {
19973ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
199845ca1b86SEd Tanous     {
199945ca1b86SEd Tanous         return;
200045ca1b86SEd Tanous     }
20017e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
20027e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
2003e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
20041da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
2005e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
2006e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
2007002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
2008e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
20091da66f75SEd Tanous         "Collection of LogServices for this Manager";
2010002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
2011c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
2012fdd26906SClaire Weinan 
2013c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2014c4bf6374SJason M. Bills     logServiceArray.push_back(
2015002d39b4SEd Tanous         {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2016c4bf6374SJason M. Bills #endif
2017fdd26906SClaire Weinan 
2018fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
2019fdd26906SClaire Weinan 
2020fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
2021fdd26906SClaire Weinan     auto respHandler =
2022fdd26906SClaire Weinan         [asyncResp](
2023fdd26906SClaire Weinan             const boost::system::error_code ec,
2024fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
2025fdd26906SClaire Weinan         if (ec)
2026fdd26906SClaire Weinan         {
2027fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
2028fdd26906SClaire Weinan                 << "handleLogServicesCollectionGet respHandler got error "
2029fdd26906SClaire Weinan                 << ec;
2030fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
2031fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
2032fdd26906SClaire Weinan             return;
2033fdd26906SClaire Weinan         }
2034fdd26906SClaire Weinan 
2035fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2036fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2037fdd26906SClaire Weinan 
2038fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2039fdd26906SClaire Weinan         {
2040fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2041fdd26906SClaire Weinan             {
2042fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2043fdd26906SClaire Weinan                     {{"@odata.id",
2044fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
2045fdd26906SClaire Weinan             }
2046fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2047fdd26906SClaire Weinan             {
2048fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2049fdd26906SClaire Weinan                     {{"@odata.id",
2050fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/FaultLog"}});
2051fdd26906SClaire Weinan             }
2052fdd26906SClaire Weinan         }
2053fdd26906SClaire Weinan 
2054e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2055fdd26906SClaire Weinan             logServiceArrayLocal.size();
2056fdd26906SClaire Weinan     };
2057fdd26906SClaire Weinan 
2058fdd26906SClaire Weinan     crow::connections::systemBus->async_method_call(
2059fdd26906SClaire Weinan         respHandler, "xyz.openbmc_project.ObjectMapper",
2060fdd26906SClaire Weinan         "/xyz/openbmc_project/object_mapper",
2061fdd26906SClaire Weinan         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2062fdd26906SClaire Weinan         "/xyz/openbmc_project/dump", 0,
2063fdd26906SClaire Weinan         std::array<const char*, 1>{dumpManagerIface});
2064fdd26906SClaire Weinan #endif
2065fdd26906SClaire Weinan }
2066fdd26906SClaire Weinan 
2067fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2068fdd26906SClaire Weinan {
2069fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2070fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2071fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2072fdd26906SClaire Weinan             std::bind_front(handleLogServicesCollectionGet, std::ref(app)));
2073e1f26343SJason M. Bills }
2074e1f26343SJason M. Bills 
20757e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2076e1f26343SJason M. Bills {
20777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2078ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
20797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
208045ca1b86SEd Tanous             [&app](const crow::Request& req,
208145ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
20823ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20837e860f15SJohn Edward Broadbent         {
208445ca1b86SEd Tanous             return;
208545ca1b86SEd Tanous         }
2086e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2087e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
20880f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20890f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2090002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2091002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2092c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
2093e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
20947c8c4058STejas Patil 
20957c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20967c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
2097002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20987c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20997c8c4058STejas Patil             redfishDateTimeOffset.second;
21007c8c4058STejas Patil 
21011476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
21021476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
21037e860f15SJohn Edward Broadbent         });
2104e1f26343SJason M. Bills }
2105e1f26343SJason M. Bills 
21063a48b3a2SJason M. Bills static int
21073a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2108e1f26343SJason M. Bills                                sd_journal* journal,
21093a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2110e1f26343SJason M. Bills {
2111e1f26343SJason M. Bills     // Get the Log Entry contents
2112e1f26343SJason M. Bills     int ret = 0;
2113e1f26343SJason M. Bills 
2114a8fe54f0SJason M. Bills     std::string message;
2115a8fe54f0SJason M. Bills     std::string_view syslogID;
2116a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2117a8fe54f0SJason M. Bills     if (ret < 0)
2118a8fe54f0SJason M. Bills     {
2119a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2120a8fe54f0SJason M. Bills                          << strerror(-ret);
2121a8fe54f0SJason M. Bills     }
2122a8fe54f0SJason M. Bills     if (!syslogID.empty())
2123a8fe54f0SJason M. Bills     {
2124a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2125a8fe54f0SJason M. Bills     }
2126a8fe54f0SJason M. Bills 
212739e77504SEd Tanous     std::string_view msg;
212816428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2129e1f26343SJason M. Bills     if (ret < 0)
2130e1f26343SJason M. Bills     {
2131e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2132e1f26343SJason M. Bills         return 1;
2133e1f26343SJason M. Bills     }
2134a8fe54f0SJason M. Bills     message += std::string(msg);
2135e1f26343SJason M. Bills 
2136e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2137271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
213816428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2139e1f26343SJason M. Bills     if (ret < 0)
2140e1f26343SJason M. Bills     {
2141e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2142e1f26343SJason M. Bills     }
2143e1f26343SJason M. Bills 
2144e1f26343SJason M. Bills     // Get the Created time from the timestamp
214516428a1aSJason M. Bills     std::string entryTimeStr;
214616428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2147e1f26343SJason M. Bills     {
214816428a1aSJason M. Bills         return 1;
2149e1f26343SJason M. Bills     }
2150e1f26343SJason M. Bills 
2151e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2152c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2153647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2154c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2155c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2156e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2157c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2158a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2159e1f26343SJason M. Bills         {"EntryType", "Oem"},
2160738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2161738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2162738c1e61SPatrick Williams                                      : "OK"},
2163086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2164e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2165e1f26343SJason M. Bills     return 0;
2166e1f26343SJason M. Bills }
2167e1f26343SJason M. Bills 
21687e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2169e1f26343SJason M. Bills {
21707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2171ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2172002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2173002d39b4SEd Tanous             [&app](const crow::Request& req,
2174002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2175c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2176c937d2bfSEd Tanous             .canDelegateTop = true,
2177c937d2bfSEd Tanous             .canDelegateSkip = true,
2178c937d2bfSEd Tanous         };
2179c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2180c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21813ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2182193ad2faSJason M. Bills         {
2183193ad2faSJason M. Bills             return;
2184193ad2faSJason M. Bills         }
21857e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
21867e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2187e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2188e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
21890f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
21900f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2191e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2192e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2193e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
21940fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2195e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2196e1f26343SJason M. Bills 
21977e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
21987e860f15SJohn Edward Broadbent         // unique ID for each entry
2199e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2200e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2201e1f26343SJason M. Bills         if (ret < 0)
2202e1f26343SJason M. Bills         {
2203002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2204f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2205e1f26343SJason M. Bills             return;
2206e1f26343SJason M. Bills         }
22070fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22080fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2209e1f26343SJason M. Bills         journalTmp = nullptr;
2210b01bf299SEd Tanous         uint64_t entryCount = 0;
2211e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2212e85d6b16SJason M. Bills         bool firstEntry = true;
2213e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2214e1f26343SJason M. Bills         {
2215193ad2faSJason M. Bills             entryCount++;
22167e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
22177e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
2218c937d2bfSEd Tanous             if (entryCount <= delegatedQuery.skip ||
2219c937d2bfSEd Tanous                 entryCount > delegatedQuery.skip + delegatedQuery.top)
2220193ad2faSJason M. Bills             {
2221193ad2faSJason M. Bills                 continue;
2222193ad2faSJason M. Bills             }
2223193ad2faSJason M. Bills 
222416428a1aSJason M. Bills             std::string idStr;
2225e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2226e1f26343SJason M. Bills             {
2227e1f26343SJason M. Bills                 continue;
2228e1f26343SJason M. Bills             }
2229e85d6b16SJason M. Bills             firstEntry = false;
2230e85d6b16SJason M. Bills 
22313a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2232c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2233c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2234e1f26343SJason M. Bills             {
2235f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2236e1f26343SJason M. Bills                 return;
2237e1f26343SJason M. Bills             }
22383a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2239e1f26343SJason M. Bills         }
2240193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2241c937d2bfSEd Tanous         if (delegatedQuery.skip + delegatedQuery.top < entryCount)
2242193ad2faSJason M. Bills         {
2243193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
22440fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2245c937d2bfSEd Tanous                 std::to_string(delegatedQuery.skip + delegatedQuery.top);
2246193ad2faSJason M. Bills         }
22477e860f15SJohn Edward Broadbent         });
2248e1f26343SJason M. Bills }
2249e1f26343SJason M. Bills 
22507e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2251e1f26343SJason M. Bills {
22527e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
22537e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2254ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
22557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
225645ca1b86SEd Tanous             [&app](const crow::Request& req,
22577e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22587e860f15SJohn Edward Broadbent                    const std::string& entryID) {
22593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
226045ca1b86SEd Tanous         {
226145ca1b86SEd Tanous             return;
226245ca1b86SEd Tanous         }
2263e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2264e1f26343SJason M. Bills         uint64_t ts = 0;
2265271584abSEd Tanous         uint64_t index = 0;
22668d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2267e1f26343SJason M. Bills         {
226816428a1aSJason M. Bills             return;
2269e1f26343SJason M. Bills         }
2270e1f26343SJason M. Bills 
2271e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2272e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2273e1f26343SJason M. Bills         if (ret < 0)
2274e1f26343SJason M. Bills         {
2275002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2276f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2277e1f26343SJason M. Bills             return;
2278e1f26343SJason M. Bills         }
2279002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2280002d39b4SEd Tanous             journalTmp, sd_journal_close);
2281e1f26343SJason M. Bills         journalTmp = nullptr;
22827e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
22837e860f15SJohn Edward Broadbent         // index tracking the unique ID
2284af07e3f5SJason M. Bills         std::string idStr;
2285af07e3f5SJason M. Bills         bool firstEntry = true;
2286e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
22872056b6d1SManojkiran Eda         if (ret < 0)
22882056b6d1SManojkiran Eda         {
22892056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
22902056b6d1SManojkiran Eda                              << strerror(-ret);
22912056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
22922056b6d1SManojkiran Eda             return;
22932056b6d1SManojkiran Eda         }
2294271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2295e1f26343SJason M. Bills         {
2296e1f26343SJason M. Bills             sd_journal_next(journal.get());
2297af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2298af07e3f5SJason M. Bills             {
2299af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2300af07e3f5SJason M. Bills                 return;
2301af07e3f5SJason M. Bills             }
2302af07e3f5SJason M. Bills             firstEntry = false;
2303af07e3f5SJason M. Bills         }
2304c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2305af07e3f5SJason M. Bills         if (idStr != entryID)
2306c4bf6374SJason M. Bills         {
2307ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2308c4bf6374SJason M. Bills             return;
2309c4bf6374SJason M. Bills         }
2310c4bf6374SJason M. Bills 
23113a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2312c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
23133a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2314e1f26343SJason M. Bills         {
2315f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2316e1f26343SJason M. Bills             return;
2317e1f26343SJason M. Bills         }
2318*d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
23197e860f15SJohn Edward Broadbent         });
2320c9bb6861Sraviteja-b }
2321c9bb6861Sraviteja-b 
2322fdd26906SClaire Weinan inline void
2323fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2324fdd26906SClaire Weinan                        const std::string& dumpType)
2325c9bb6861Sraviteja-b {
2326fdd26906SClaire Weinan     std::string dumpPath;
2327fdd26906SClaire Weinan     std::string overWritePolicy;
2328fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2329fdd26906SClaire Weinan 
2330fdd26906SClaire Weinan     if (dumpType == "BMC")
233145ca1b86SEd Tanous     {
2332fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2333fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2334fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2335fdd26906SClaire Weinan     }
2336fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2337fdd26906SClaire Weinan     {
2338fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2339fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2340fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2341fdd26906SClaire Weinan     }
2342fdd26906SClaire Weinan     else if (dumpType == "System")
2343fdd26906SClaire Weinan     {
2344fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2345fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2346fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2347fdd26906SClaire Weinan     }
2348fdd26906SClaire Weinan     else
2349fdd26906SClaire Weinan     {
2350fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2351fdd26906SClaire Weinan                          << dumpType;
2352fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
235345ca1b86SEd Tanous         return;
235445ca1b86SEd Tanous     }
2355fdd26906SClaire Weinan 
2356fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2357fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2358c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2359fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2360fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2361fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
23627c8c4058STejas Patil 
23637c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
23647c8c4058STejas Patil         crow::utility::getDateTimeOffsetNow();
23650fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23667c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23677c8c4058STejas Patil         redfishDateTimeOffset.second;
23687c8c4058STejas Patil 
2369fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2370002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2371fdd26906SClaire Weinan         dumpPath + "/Actions/LogService.ClearLog";
2372fdd26906SClaire Weinan 
2373fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2374fdd26906SClaire Weinan     {
2375002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
23761476687dSEd Tanous                                 ["target"] =
2377fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2378fdd26906SClaire Weinan     }
2379c9bb6861Sraviteja-b }
2380c9bb6861Sraviteja-b 
2381fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2382fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2383fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23847e860f15SJohn Edward Broadbent {
23853ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
238645ca1b86SEd Tanous     {
238745ca1b86SEd Tanous         return;
238845ca1b86SEd Tanous     }
2389fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2390fdd26906SClaire Weinan }
2391c9bb6861Sraviteja-b 
2392fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2393fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2394fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2395fdd26906SClaire Weinan {
2396fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2397fdd26906SClaire Weinan     {
2398fdd26906SClaire Weinan         return;
2399fdd26906SClaire Weinan     }
2400fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2401fdd26906SClaire Weinan }
2402fdd26906SClaire Weinan 
2403fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2404fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2405fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2406fdd26906SClaire Weinan     const std::string& dumpId)
2407fdd26906SClaire Weinan {
2408fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2409fdd26906SClaire Weinan     {
2410fdd26906SClaire Weinan         return;
2411fdd26906SClaire Weinan     }
2412fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2413fdd26906SClaire Weinan }
2414fdd26906SClaire Weinan 
2415fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2416fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2417fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2418fdd26906SClaire Weinan     const std::string& dumpId)
2419fdd26906SClaire Weinan {
2420fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2421fdd26906SClaire Weinan     {
2422fdd26906SClaire Weinan         return;
2423fdd26906SClaire Weinan     }
2424fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2425fdd26906SClaire Weinan }
2426fdd26906SClaire Weinan 
2427fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2428fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2429fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2430fdd26906SClaire Weinan {
2431fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2432fdd26906SClaire Weinan     {
2433fdd26906SClaire Weinan         return;
2434fdd26906SClaire Weinan     }
2435fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2436fdd26906SClaire Weinan }
2437fdd26906SClaire Weinan 
2438fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2439fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2440fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2441fdd26906SClaire Weinan {
2442fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2443fdd26906SClaire Weinan     {
2444fdd26906SClaire Weinan         return;
2445fdd26906SClaire Weinan     }
2446fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2447fdd26906SClaire Weinan }
2448fdd26906SClaire Weinan 
2449fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2450fdd26906SClaire Weinan {
2451fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2452fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2453fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2454fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2455fdd26906SClaire Weinan }
2456fdd26906SClaire Weinan 
2457fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2458fdd26906SClaire Weinan {
2459fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2460fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2461fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2462fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2463c9bb6861Sraviteja-b }
2464c9bb6861Sraviteja-b 
24657e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2466c9bb6861Sraviteja-b {
24677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24687e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2469ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2470fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2471fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2472fdd26906SClaire Weinan 
24737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24747e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2475ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2476fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2477fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2478c9bb6861Sraviteja-b }
2479c9bb6861Sraviteja-b 
24807e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2481c9bb6861Sraviteja-b {
24820fda0f12SGeorge Liu     BMCWEB_ROUTE(
24830fda0f12SGeorge Liu         app,
24840fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2485ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24867e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2487fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2488fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2489a43be80fSAsmitha Karunanithi }
2490a43be80fSAsmitha Karunanithi 
24917e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
249280319af1SAsmitha Karunanithi {
24930fda0f12SGeorge Liu     BMCWEB_ROUTE(
24940fda0f12SGeorge Liu         app,
24950fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2496ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2497fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2498fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
249945ca1b86SEd Tanous }
2500fdd26906SClaire Weinan 
2501fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2502fdd26906SClaire Weinan {
2503fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2504fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2505fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2506fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2507fdd26906SClaire Weinan }
2508fdd26906SClaire Weinan 
2509fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2510fdd26906SClaire Weinan {
2511fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2512fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2513fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2514fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2515fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2516fdd26906SClaire Weinan }
2517fdd26906SClaire Weinan 
2518fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2519fdd26906SClaire Weinan {
2520fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2521fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2522fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2523fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2524fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2525fdd26906SClaire Weinan 
2526fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2527fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2528fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2529fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2530fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2531fdd26906SClaire Weinan }
2532fdd26906SClaire Weinan 
2533fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2534fdd26906SClaire Weinan {
2535fdd26906SClaire Weinan     BMCWEB_ROUTE(
2536fdd26906SClaire Weinan         app,
2537fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2538fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2539fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2540fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
25415cb1dd27SAsmitha Karunanithi }
25425cb1dd27SAsmitha Karunanithi 
25437e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
25445cb1dd27SAsmitha Karunanithi {
25457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2546ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
2547002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2548002d39b4SEd Tanous             [&app](const crow::Request& req,
2549002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
25517e860f15SJohn Edward Broadbent         {
255245ca1b86SEd Tanous             return;
255345ca1b86SEd Tanous         }
25545cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
25555cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump";
25565cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
2557d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
25585cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "Dump LogService";
255945ca1b86SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Dump LogService";
25605cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
25615cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
25627c8c4058STejas Patil 
25637c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
25647c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
256545ca1b86SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25667c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25677c8c4058STejas Patil             redfishDateTimeOffset.second;
25687c8c4058STejas Patil 
25691476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
25701476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2571002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
25721476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog";
25731476687dSEd Tanous 
2574002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
25751476687dSEd Tanous                                 ["target"] =
25761476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData";
25777e860f15SJohn Edward Broadbent         });
25785cb1dd27SAsmitha Karunanithi }
25795cb1dd27SAsmitha Karunanithi 
25807e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
25817e860f15SJohn Edward Broadbent {
25827e860f15SJohn Edward Broadbent 
25835cb1dd27SAsmitha Karunanithi     /**
25845cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25855cb1dd27SAsmitha Karunanithi      */
2586b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2587ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
258945ca1b86SEd Tanous             [&app](const crow::Request& req,
2590864d6a17SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
259245ca1b86SEd Tanous         {
259345ca1b86SEd Tanous             return;
259445ca1b86SEd Tanous         }
25955cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "System");
25967e860f15SJohn Edward Broadbent         });
25975cb1dd27SAsmitha Karunanithi }
25985cb1dd27SAsmitha Karunanithi 
25997e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
26005cb1dd27SAsmitha Karunanithi {
26017e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2602864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2603ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2604ed398213SEd Tanous 
26057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
260645ca1b86SEd Tanous             [&app](const crow::Request& req,
26077e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26087e860f15SJohn Edward Broadbent                    const std::string& param) {
26093ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2610c7a6d660SClaire Weinan         {
2611c7a6d660SClaire Weinan             return;
2612c7a6d660SClaire Weinan         }
2613c7a6d660SClaire Weinan         getDumpEntryById(asyncResp, param, "System");
26147e860f15SJohn Edward Broadbent         });
26158d1b46d7Szhanghch05 
26167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2617864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2618ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
26197e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
262045ca1b86SEd Tanous             [&app](const crow::Request& req,
26217e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26227e860f15SJohn Edward Broadbent                    const std::string& param) {
26233ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
262445ca1b86SEd Tanous         {
262545ca1b86SEd Tanous             return;
262645ca1b86SEd Tanous         }
26277e860f15SJohn Edward Broadbent         deleteDumpEntry(asyncResp, param, "system");
26287e860f15SJohn Edward Broadbent         });
26295cb1dd27SAsmitha Karunanithi }
2630c9bb6861Sraviteja-b 
26317e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2632c9bb6861Sraviteja-b {
26330fda0f12SGeorge Liu     BMCWEB_ROUTE(
26340fda0f12SGeorge Liu         app,
26350fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2636ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26377e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
263845ca1b86SEd Tanous             [&app](const crow::Request& req,
263945ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26403ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
264145ca1b86SEd Tanous         {
264245ca1b86SEd Tanous             return;
264345ca1b86SEd Tanous         }
264445ca1b86SEd Tanous         createDump(asyncResp, req, "System");
264545ca1b86SEd Tanous         });
2646a43be80fSAsmitha Karunanithi }
2647a43be80fSAsmitha Karunanithi 
26487e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2649a43be80fSAsmitha Karunanithi {
26500fda0f12SGeorge Liu     BMCWEB_ROUTE(
26510fda0f12SGeorge Liu         app,
26520fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2653ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26547e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
265545ca1b86SEd Tanous             [&app](const crow::Request& req,
26567e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26577e860f15SJohn Edward Broadbent 
265845ca1b86SEd Tanous             {
26593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
266045ca1b86SEd Tanous         {
266145ca1b86SEd Tanous             return;
266245ca1b86SEd Tanous         }
266345ca1b86SEd Tanous         clearDump(asyncResp, "System");
266445ca1b86SEd Tanous         });
2665013487e5Sraviteja-b }
2666013487e5Sraviteja-b 
26677e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
26681da66f75SEd Tanous {
26693946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26703946028dSAppaRao Puli     // method for security reasons.
26711da66f75SEd Tanous     /**
26721da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26731da66f75SEd Tanous      */
26747e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2675ed398213SEd Tanous         // This is incorrect, should be:
2676ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2677432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2678002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2679002d39b4SEd Tanous             [&app](const crow::Request& req,
2680002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26813ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
268245ca1b86SEd Tanous         {
268345ca1b86SEd Tanous             return;
268445ca1b86SEd Tanous         }
26857e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
26867e860f15SJohn Edward Broadbent         // SubRoute
26870f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2688424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2689e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
26908e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
26914f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26924f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26934f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2694e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2695e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
26967c8c4058STejas Patil 
26977c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
26987c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
26997c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
27007c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
27017c8c4058STejas Patil             redfishDateTimeOffset.second;
27027c8c4058STejas Patil 
27031476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
27041476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2705002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
27061476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2707002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
27081476687dSEd Tanous                                 ["target"] =
27091476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
27107e860f15SJohn Edward Broadbent         });
27111da66f75SEd Tanous }
27121da66f75SEd Tanous 
27137e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
27145b61b5e8SJason M. Bills {
27150fda0f12SGeorge Liu     BMCWEB_ROUTE(
27160fda0f12SGeorge Liu         app,
27170fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2718ed398213SEd Tanous         // This is incorrect, should be:
2719ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2720432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27217e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
272245ca1b86SEd Tanous             [&app](const crow::Request& req,
27237e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27243ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
272545ca1b86SEd Tanous         {
272645ca1b86SEd Tanous             return;
272745ca1b86SEd Tanous         }
27285b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
27295b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
2730cb13a392SEd Tanous                         const std::string&) {
27315b61b5e8SJason M. Bills             if (ec)
27325b61b5e8SJason M. Bills             {
27335b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
27345b61b5e8SJason M. Bills                 return;
27355b61b5e8SJason M. Bills             }
27365b61b5e8SJason M. Bills             messages::success(asyncResp->res);
27375b61b5e8SJason M. Bills             },
2738002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
27397e860f15SJohn Edward Broadbent         });
27405b61b5e8SJason M. Bills }
27415b61b5e8SJason M. Bills 
27428d1b46d7Szhanghch05 static void
27438d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27448d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2745e855dd28SJason M. Bills {
2746043a0536SJohnathan Mantey     auto getStoredLogCallback =
2747b9d36b47SEd Tanous         [asyncResp, logID,
2748b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2749b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2750e855dd28SJason M. Bills         if (ec)
2751e855dd28SJason M. Bills         {
2752e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
27531ddcf01aSJason M. Bills             if (ec.value() ==
27541ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
27551ddcf01aSJason M. Bills             {
2756002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
27571ddcf01aSJason M. Bills             }
27581ddcf01aSJason M. Bills             else
27591ddcf01aSJason M. Bills             {
2760e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
27611ddcf01aSJason M. Bills             }
2762e855dd28SJason M. Bills             return;
2763e855dd28SJason M. Bills         }
2764043a0536SJohnathan Mantey 
2765043a0536SJohnathan Mantey         std::string timestamp{};
2766043a0536SJohnathan Mantey         std::string filename{};
2767043a0536SJohnathan Mantey         std::string logfile{};
27682c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
2769043a0536SJohnathan Mantey 
2770043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
2771e855dd28SJason M. Bills         {
2772002d39b4SEd Tanous             messages::resourceMissingAtURI(asyncResp->res,
2773002d39b4SEd Tanous                                            crow::utility::urlFromPieces(logID));
2774e855dd28SJason M. Bills             return;
2775e855dd28SJason M. Bills         }
2776e855dd28SJason M. Bills 
2777043a0536SJohnathan Mantey         std::string crashdumpURI =
2778e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2779043a0536SJohnathan Mantey             logID + "/" + filename;
27803a48b3a2SJason M. Bills         nlohmann::json::object_t logEntry = {
27814978b63fSJason M. Bills             {"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
27824978b63fSJason M. Bills             {"@odata.id",
27834978b63fSJason M. Bills              "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2784e855dd28SJason M. Bills                  logID},
2785e855dd28SJason M. Bills             {"Name", "CPU Crashdump"},
2786e855dd28SJason M. Bills             {"Id", logID},
2787e855dd28SJason M. Bills             {"EntryType", "Oem"},
27888e6c099aSJason M. Bills             {"AdditionalDataURI", std::move(crashdumpURI)},
27898e6c099aSJason M. Bills             {"DiagnosticDataType", "OEM"},
27908e6c099aSJason M. Bills             {"OEMDiagnosticDataType", "PECICrashdump"},
2791043a0536SJohnathan Mantey             {"Created", std::move(timestamp)}};
27922b20ef6eSJason M. Bills 
27932b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
27942b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
27952b20ef6eSJason M. Bills         // directly
27962b20ef6eSJason M. Bills         if (logEntryJson.is_array())
27972b20ef6eSJason M. Bills         {
27982b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
27992b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
28002b20ef6eSJason M. Bills                 logEntryJson.size();
28012b20ef6eSJason M. Bills         }
28022b20ef6eSJason M. Bills         else
28032b20ef6eSJason M. Bills         {
2804*d405bb51SJason M. Bills             logEntryJson.update(logEntry);
28052b20ef6eSJason M. Bills         }
2806e855dd28SJason M. Bills     };
2807e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
28085b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
28095b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2810043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2811e855dd28SJason M. Bills }
2812e855dd28SJason M. Bills 
28137e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
28141da66f75SEd Tanous {
28153946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28163946028dSAppaRao Puli     // method for security reasons.
28171da66f75SEd Tanous     /**
28181da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
28191da66f75SEd Tanous      */
28207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28217e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2822ed398213SEd Tanous         // This is incorrect, should be.
2823ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2824432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2825002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2826002d39b4SEd Tanous             [&app](const crow::Request& req,
2827002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
28283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
282945ca1b86SEd Tanous         {
283045ca1b86SEd Tanous             return;
283145ca1b86SEd Tanous         }
28322b20ef6eSJason M. Bills         crow::connections::systemBus->async_method_call(
28332b20ef6eSJason M. Bills             [asyncResp](const boost::system::error_code ec,
28342b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
28351da66f75SEd Tanous             if (ec)
28361da66f75SEd Tanous             {
28371da66f75SEd Tanous                 if (ec.value() !=
28381da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
28391da66f75SEd Tanous                 {
28401da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
28411da66f75SEd Tanous                                      << ec.message();
2842f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
28431da66f75SEd Tanous                     return;
28441da66f75SEd Tanous                 }
28451da66f75SEd Tanous             }
2846e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
28471da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
28480f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2849424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2850002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2851e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2852424c4176SJason M. Bills                 "Collection of Crashdump Entries";
2853002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2854a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
28552b20ef6eSJason M. Bills 
28562b20ef6eSJason M. Bills             for (const std::string& path : resp)
28571da66f75SEd Tanous             {
28582b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
2859e855dd28SJason M. Bills                 // Get the log ID
28602b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
28612b20ef6eSJason M. Bills                 if (logID.empty())
28621da66f75SEd Tanous                 {
2863e855dd28SJason M. Bills                     continue;
28641da66f75SEd Tanous                 }
2865e855dd28SJason M. Bills                 // Add the log entry to the array
28662b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
28672b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
28681da66f75SEd Tanous             }
28692b20ef6eSJason M. Bills             },
28701da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
28711da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
28721da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
28735b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
28747e860f15SJohn Edward Broadbent         });
28751da66f75SEd Tanous }
28761da66f75SEd Tanous 
28777e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28781da66f75SEd Tanous {
28793946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28803946028dSAppaRao Puli     // method for security reasons.
28811da66f75SEd Tanous 
28827e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28837e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2884ed398213SEd Tanous         // this is incorrect, should be
2885ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2886432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28877e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
288845ca1b86SEd Tanous             [&app](const crow::Request& req,
28897e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28907e860f15SJohn Edward Broadbent                    const std::string& param) {
28913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
289245ca1b86SEd Tanous         {
289345ca1b86SEd Tanous             return;
289445ca1b86SEd Tanous         }
28957e860f15SJohn Edward Broadbent         const std::string& logID = param;
2896e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
28977e860f15SJohn Edward Broadbent         });
2898e855dd28SJason M. Bills }
2899e855dd28SJason M. Bills 
29007e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2901e855dd28SJason M. Bills {
29023946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29033946028dSAppaRao Puli     // method for security reasons.
29047e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
29057e860f15SJohn Edward Broadbent         app,
29067e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2907ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
29087e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
290945ca1b86SEd Tanous             [&app](const crow::Request& req,
29107e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29117e860f15SJohn Edward Broadbent                    const std::string& logID, const std::string& fileName) {
29123ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
291345ca1b86SEd Tanous         {
291445ca1b86SEd Tanous             return;
291545ca1b86SEd Tanous         }
2916043a0536SJohnathan Mantey         auto getStoredLogCallback =
2917002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
2918abf2add6SEd Tanous                 const boost::system::error_code ec,
2919002d39b4SEd Tanous                 const std::vector<
2920002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
29217e860f15SJohn Edward Broadbent                     resp) {
29221da66f75SEd Tanous             if (ec)
29231da66f75SEd Tanous             {
2924002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2925f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
29261da66f75SEd Tanous                 return;
29271da66f75SEd Tanous             }
2928e855dd28SJason M. Bills 
2929043a0536SJohnathan Mantey             std::string dbusFilename{};
2930043a0536SJohnathan Mantey             std::string dbusTimestamp{};
2931043a0536SJohnathan Mantey             std::string dbusFilepath{};
2932043a0536SJohnathan Mantey 
2933002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2934002d39b4SEd Tanous                                      dbusFilepath);
2935043a0536SJohnathan Mantey 
2936043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
2937043a0536SJohnathan Mantey                 dbusFilepath.empty())
29381da66f75SEd Tanous             {
2939ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
29401da66f75SEd Tanous                 return;
29411da66f75SEd Tanous             }
2942e855dd28SJason M. Bills 
2943043a0536SJohnathan Mantey             // Verify the file name parameter is correct
2944043a0536SJohnathan Mantey             if (fileName != dbusFilename)
2945043a0536SJohnathan Mantey             {
2946ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2947043a0536SJohnathan Mantey                 return;
2948043a0536SJohnathan Mantey             }
2949043a0536SJohnathan Mantey 
2950043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
2951043a0536SJohnathan Mantey             {
2952ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2953043a0536SJohnathan Mantey                 return;
2954043a0536SJohnathan Mantey             }
2955002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
2956002d39b4SEd Tanous             asyncResp->res.body() =
2957002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
2958043a0536SJohnathan Mantey 
29597e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
29607e860f15SJohn Edward Broadbent             // from a browser
2961002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Disposition", "attachment");
29621da66f75SEd Tanous         };
29631da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
29645b61b5e8SJason M. Bills             std::move(getStoredLogCallback), crashdumpObject,
29655b61b5e8SJason M. Bills             crashdumpPath + std::string("/") + logID,
2966002d39b4SEd Tanous             "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
29677e860f15SJohn Edward Broadbent         });
29681da66f75SEd Tanous }
29691da66f75SEd Tanous 
2970c5a4c82aSJason M. Bills enum class OEMDiagnosticType
2971c5a4c82aSJason M. Bills {
2972c5a4c82aSJason M. Bills     onDemand,
2973c5a4c82aSJason M. Bills     telemetry,
2974c5a4c82aSJason M. Bills     invalid,
2975c5a4c82aSJason M. Bills };
2976c5a4c82aSJason M. Bills 
2977f7725d79SEd Tanous inline OEMDiagnosticType
2978f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
2979c5a4c82aSJason M. Bills {
2980c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
2981c5a4c82aSJason M. Bills     {
2982c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
2983c5a4c82aSJason M. Bills     }
2984c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
2985c5a4c82aSJason M. Bills     {
2986c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
2987c5a4c82aSJason M. Bills     }
2988c5a4c82aSJason M. Bills 
2989c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
2990c5a4c82aSJason M. Bills }
2991c5a4c82aSJason M. Bills 
29927e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
29931da66f75SEd Tanous {
29943946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29953946028dSAppaRao Puli     // method for security reasons.
29960fda0f12SGeorge Liu     BMCWEB_ROUTE(
29970fda0f12SGeorge Liu         app,
29980fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2999ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
3000ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3001432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
3002002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
3003002d39b4SEd Tanous             [&app](const crow::Request& req,
30047e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
30053ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
300645ca1b86SEd Tanous         {
300745ca1b86SEd Tanous             return;
300845ca1b86SEd Tanous         }
30098e6c099aSJason M. Bills         std::string diagnosticDataType;
30108e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
301115ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
3012002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
3013002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
30148e6c099aSJason M. Bills         {
30158e6c099aSJason M. Bills             return;
30168e6c099aSJason M. Bills         }
30178e6c099aSJason M. Bills 
30188e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
30198e6c099aSJason M. Bills         {
30208e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
30218e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
30228e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
30238e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
30248e6c099aSJason M. Bills                 "CollectDiagnosticData");
30258e6c099aSJason M. Bills             return;
30268e6c099aSJason M. Bills         }
30278e6c099aSJason M. Bills 
3028c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
3029c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
3030c5a4c82aSJason M. Bills 
3031c5a4c82aSJason M. Bills         std::string iface;
3032c5a4c82aSJason M. Bills         std::string method;
3033c5a4c82aSJason M. Bills         std::string taskMatchStr;
3034c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3035c5a4c82aSJason M. Bills         {
3036c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3037c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3038c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3039c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3040c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3041c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3042c5a4c82aSJason M. Bills         }
3043c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3044c5a4c82aSJason M. Bills         {
3045c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3046c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3047c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3048c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3049c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3050c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3051c5a4c82aSJason M. Bills         }
3052c5a4c82aSJason M. Bills         else
3053c5a4c82aSJason M. Bills         {
3054c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3055c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3056c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3057002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3058002d39b4SEd Tanous                 "CollectDiagnosticData");
3059c5a4c82aSJason M. Bills             return;
3060c5a4c82aSJason M. Bills         }
3061c5a4c82aSJason M. Bills 
3062c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3063c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3064c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
306598be3e39SEd Tanous                            const std::string&) mutable {
30661da66f75SEd Tanous             if (ec)
30671da66f75SEd Tanous             {
3068002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
30691da66f75SEd Tanous                 {
3070f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
30711da66f75SEd Tanous                 }
30724363d3b2SJason M. Bills                 else if (ec.value() ==
30734363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
30744363d3b2SJason M. Bills                 {
3075002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3076002d39b4SEd Tanous                                                             "60");
30774363d3b2SJason M. Bills                 }
30781da66f75SEd Tanous                 else
30791da66f75SEd Tanous                 {
3080f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
30811da66f75SEd Tanous                 }
30821da66f75SEd Tanous                 return;
30831da66f75SEd Tanous             }
3084002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
3085002d39b4SEd Tanous                 [](boost::system::error_code err, sdbusplus::message::message&,
3086002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
308766afe4faSJames Feist                 if (!err)
308866afe4faSJames Feist                 {
3089002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3090e5d5006bSJames Feist                         std::to_string(taskData->index)));
3091831d6b09SJames Feist                     taskData->state = "Completed";
309266afe4faSJames Feist                 }
309332898ceaSJames Feist                 return task::completed;
309466afe4faSJames Feist                 },
3095c5a4c82aSJason M. Bills                 taskMatchStr);
3096c5a4c82aSJason M. Bills 
309746229577SJames Feist             task->startTimer(std::chrono::minutes(5));
309846229577SJames Feist             task->populateResp(asyncResp->res);
309998be3e39SEd Tanous             task->payload.emplace(std::move(payload));
31001da66f75SEd Tanous         };
31018e6c099aSJason M. Bills 
31021da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3103002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3104002d39b4SEd Tanous             iface, method);
31057e860f15SJohn Edward Broadbent         });
31066eda7685SKenny L. Ku }
31076eda7685SKenny L. Ku 
3108cb92c03bSAndrew Geissler /**
3109cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3110cb92c03bSAndrew Geissler  */
31117e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3112cb92c03bSAndrew Geissler {
3113cb92c03bSAndrew Geissler     /**
3114cb92c03bSAndrew Geissler      * Function handles POST method request.
3115cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3116cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3117cb92c03bSAndrew Geissler      */
31187e860f15SJohn Edward Broadbent 
31190fda0f12SGeorge Liu     BMCWEB_ROUTE(
31200fda0f12SGeorge Liu         app,
31210fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3122ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
31237e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
312445ca1b86SEd Tanous             [&app](const crow::Request& req,
31257e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
312745ca1b86SEd Tanous         {
312845ca1b86SEd Tanous             return;
312945ca1b86SEd Tanous         }
3130cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3131cb92c03bSAndrew Geissler 
3132cb92c03bSAndrew Geissler         // Process response from Logging service.
3133002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3134002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3135cb92c03bSAndrew Geissler             if (ec)
3136cb92c03bSAndrew Geissler             {
3137cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3138002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3139cb92c03bSAndrew Geissler                 asyncResp->res.result(
3140cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3141cb92c03bSAndrew Geissler                 return;
3142cb92c03bSAndrew Geissler             }
3143cb92c03bSAndrew Geissler 
3144002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3145cb92c03bSAndrew Geissler         };
3146cb92c03bSAndrew Geissler 
3147cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3148cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
31492c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3150cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3151cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31527e860f15SJohn Edward Broadbent         });
3153cb92c03bSAndrew Geissler }
3154a3316fc6SZhikuiRen 
3155a3316fc6SZhikuiRen /****************************************************
3156a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3157a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3158a3316fc6SZhikuiRen  ******************************************************/
31597e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3160a3316fc6SZhikuiRen {
31617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3162ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3163002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3164002d39b4SEd Tanous             [&app](const crow::Request& req,
3165002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31663ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
316745ca1b86SEd Tanous         {
316845ca1b86SEd Tanous             return;
316945ca1b86SEd Tanous         }
31701476687dSEd Tanous 
31711476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
31721476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
31731476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
31741476687dSEd Tanous             "#LogService.v1_1_0.LogService";
31751476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
31761476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
31771476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
31781476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
31791476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
31801476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
31817c8c4058STejas Patil 
31827c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
31837c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
31840fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
31857c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
31867c8c4058STejas Patil             redfishDateTimeOffset.second;
31877c8c4058STejas Patil 
3188a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
31897e860f15SJohn Edward Broadbent             {"target",
31900fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
31917e860f15SJohn Edward Broadbent         });
3192a3316fc6SZhikuiRen }
3193a3316fc6SZhikuiRen 
31947e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3195a3316fc6SZhikuiRen {
31960fda0f12SGeorge Liu     BMCWEB_ROUTE(
31970fda0f12SGeorge Liu         app,
31980fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3199ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3200ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3201432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32027e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
320345ca1b86SEd Tanous             [&app](const crow::Request& req,
32047e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
32053ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
320645ca1b86SEd Tanous         {
320745ca1b86SEd Tanous             return;
320845ca1b86SEd Tanous         }
3209a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3210a3316fc6SZhikuiRen 
3211a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3212a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3213a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3214a3316fc6SZhikuiRen             if (ec)
3215a3316fc6SZhikuiRen             {
3216a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3217002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
32187e860f15SJohn Edward Broadbent                                  << ec;
3219002d39b4SEd Tanous                 asyncResp->res.result(
3220002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3221a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3222a3316fc6SZhikuiRen                 return;
3223a3316fc6SZhikuiRen             }
3224a3316fc6SZhikuiRen             },
322515124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
322615124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3227a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
32287e860f15SJohn Edward Broadbent         });
3229a3316fc6SZhikuiRen }
3230a3316fc6SZhikuiRen 
3231a3316fc6SZhikuiRen static void fillPostCodeEntry(
32328d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
32336c9a279eSManojkiran Eda     const boost::container::flat_map<
32346c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3235a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3236a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3237a3316fc6SZhikuiRen {
3238a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3239fffb8c1fSEd Tanous     const registries::Message* message =
3240fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3241a3316fc6SZhikuiRen 
3242a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3243a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3244a3316fc6SZhikuiRen 
3245a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
32466c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32476c9a279eSManojkiran Eda              code : postcode)
3248a3316fc6SZhikuiRen     {
3249a3316fc6SZhikuiRen         currentCodeIndex++;
3250a3316fc6SZhikuiRen         std::string postcodeEntryID =
3251a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3252a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3253a3316fc6SZhikuiRen 
3254a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3255a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3256a3316fc6SZhikuiRen 
3257a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3258a3316fc6SZhikuiRen         { // already incremented
3259a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3260a3316fc6SZhikuiRen         }
3261a3316fc6SZhikuiRen         else
3262a3316fc6SZhikuiRen         {
3263a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3264a3316fc6SZhikuiRen         }
3265a3316fc6SZhikuiRen 
3266a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3267a3316fc6SZhikuiRen         // not fall between top and skip
3268a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3269a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3270a3316fc6SZhikuiRen         {
3271a3316fc6SZhikuiRen             continue;
3272a3316fc6SZhikuiRen         }
3273a3316fc6SZhikuiRen 
32744e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3275a3316fc6SZhikuiRen         // currentIndex
3276a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3277a3316fc6SZhikuiRen         {
3278a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3279a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3280a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3281a3316fc6SZhikuiRen             continue;
3282a3316fc6SZhikuiRen         }
3283a3316fc6SZhikuiRen 
3284a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3285a3316fc6SZhikuiRen         // index
3286a3316fc6SZhikuiRen 
3287a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3288a3316fc6SZhikuiRen         std::string entryTimeStr;
32891d8782e7SNan Zhou         entryTimeStr =
32901d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3291a3316fc6SZhikuiRen 
3292a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3293a3316fc6SZhikuiRen         std::ostringstream hexCode;
3294a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32956c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3296a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3297a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3298a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3299a3316fc6SZhikuiRen         // Set precision to 4 digits
3300a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3301a3316fc6SZhikuiRen         // Add double to stream
3302a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3303a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3304a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3305a3316fc6SZhikuiRen 
3306a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3307a3316fc6SZhikuiRen         std::string msg;
3308a3316fc6SZhikuiRen         if (message != nullptr)
3309a3316fc6SZhikuiRen         {
3310a3316fc6SZhikuiRen             msg = message->message;
3311a3316fc6SZhikuiRen 
3312a3316fc6SZhikuiRen             // fill in this post code value
3313a3316fc6SZhikuiRen             int i = 0;
3314a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3315a3316fc6SZhikuiRen             {
3316a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3317a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3318a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3319a3316fc6SZhikuiRen                 {
3320a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3321a3316fc6SZhikuiRen                 }
3322a3316fc6SZhikuiRen             }
3323a3316fc6SZhikuiRen         }
3324a3316fc6SZhikuiRen 
3325d4342a92STim Lee         // Get Severity template from message registry
3326d4342a92STim Lee         std::string severity;
3327d4342a92STim Lee         if (message != nullptr)
3328d4342a92STim Lee         {
33295f2b84eeSEd Tanous             severity = message->messageSeverity;
3330d4342a92STim Lee         }
3331d4342a92STim Lee 
3332a3316fc6SZhikuiRen         // add to AsyncResp
3333a3316fc6SZhikuiRen         logEntryArray.push_back({});
3334a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
33350fda0f12SGeorge Liu         bmcLogEntry = {
33360fda0f12SGeorge Liu             {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
33370fda0f12SGeorge Liu             {"@odata.id",
33380fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3339a3316fc6SZhikuiRen                  postcodeEntryID},
3340a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
3341a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
3342a3316fc6SZhikuiRen             {"Message", std::move(msg)},
33434a0bf539SManojkiran Eda             {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3344a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
3345a3316fc6SZhikuiRen             {"EntryType", "Event"},
3346a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
33479c620e21SAsmitha Karunanithi             {"Created", entryTimeStr}};
3348647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3349647b3cdcSGeorge Liu         {
3350647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3351647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3352647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3353647b3cdcSGeorge Liu         }
3354a3316fc6SZhikuiRen     }
3355a3316fc6SZhikuiRen }
3356a3316fc6SZhikuiRen 
33578d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3358a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3359a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3360a3316fc6SZhikuiRen {
3361a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
33626c9a279eSManojkiran Eda         [aResp, bootIndex,
33636c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
33646c9a279eSManojkiran Eda                     const boost::container::flat_map<
33656c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33666c9a279eSManojkiran Eda                         postcode) {
3367a3316fc6SZhikuiRen         if (ec)
3368a3316fc6SZhikuiRen         {
3369a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3370a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3371a3316fc6SZhikuiRen             return;
3372a3316fc6SZhikuiRen         }
3373a3316fc6SZhikuiRen 
3374a3316fc6SZhikuiRen         // skip the empty postcode boots
3375a3316fc6SZhikuiRen         if (postcode.empty())
3376a3316fc6SZhikuiRen         {
3377a3316fc6SZhikuiRen             return;
3378a3316fc6SZhikuiRen         }
3379a3316fc6SZhikuiRen 
3380a3316fc6SZhikuiRen         fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3381a3316fc6SZhikuiRen 
3382a3316fc6SZhikuiRen         aResp->res.jsonValue["Members@odata.count"] =
3383a3316fc6SZhikuiRen             aResp->res.jsonValue["Members"].size();
3384a3316fc6SZhikuiRen         },
338515124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
338615124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3387a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3388a3316fc6SZhikuiRen         bootIndex);
3389a3316fc6SZhikuiRen }
3390a3316fc6SZhikuiRen 
33918d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3392a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3393a3316fc6SZhikuiRen                                const uint16_t bootCount,
3394a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3395a3316fc6SZhikuiRen                                const uint64_t top)
3396a3316fc6SZhikuiRen {
3397a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3398a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3399a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
34006c9a279eSManojkiran Eda               const boost::container::flat_map<
34016c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
34026c9a279eSManojkiran Eda                   postcode) {
3403a3316fc6SZhikuiRen         if (ec)
3404a3316fc6SZhikuiRen         {
3405a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3406a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3407a3316fc6SZhikuiRen             return;
3408a3316fc6SZhikuiRen         }
3409a3316fc6SZhikuiRen 
3410a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3411a3316fc6SZhikuiRen         if (!postcode.empty())
3412a3316fc6SZhikuiRen         {
3413a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
3414a3316fc6SZhikuiRen 
3415a3316fc6SZhikuiRen             if ((skip < endCount) && ((top + skip) > entryCount))
3416a3316fc6SZhikuiRen             {
3417002d39b4SEd Tanous                 uint64_t thisBootSkip = std::max(skip, entryCount) - entryCount;
3418a3316fc6SZhikuiRen                 uint64_t thisBootTop =
3419a3316fc6SZhikuiRen                     std::min(top + skip, endCount) - entryCount;
3420a3316fc6SZhikuiRen 
3421002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3422002d39b4SEd Tanous                                   thisBootTop);
3423a3316fc6SZhikuiRen             }
3424a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3425a3316fc6SZhikuiRen         }
3426a3316fc6SZhikuiRen 
3427a3316fc6SZhikuiRen         // continue to previous bootIndex
3428a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3429a3316fc6SZhikuiRen         {
3430a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3431a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3432a3316fc6SZhikuiRen         }
3433a3316fc6SZhikuiRen         else
3434a3316fc6SZhikuiRen         {
3435a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
34360fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3437a3316fc6SZhikuiRen                 std::to_string(skip + top);
3438a3316fc6SZhikuiRen         }
3439a3316fc6SZhikuiRen         },
344015124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
344115124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3442a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3443a3316fc6SZhikuiRen         bootIndex);
3444a3316fc6SZhikuiRen }
3445a3316fc6SZhikuiRen 
34468d1b46d7Szhanghch05 static void
34478d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3448a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3449a3316fc6SZhikuiRen {
3450a3316fc6SZhikuiRen     uint64_t entryCount = 0;
34511e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
34521e1e598dSJonathan Doman         *crow::connections::systemBus,
34531e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
34541e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
34551e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
34561e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
34571e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3458a3316fc6SZhikuiRen         if (ec)
3459a3316fc6SZhikuiRen         {
3460a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3461a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3462a3316fc6SZhikuiRen             return;
3463a3316fc6SZhikuiRen         }
34641e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
34651e1e598dSJonathan Doman         });
3466a3316fc6SZhikuiRen }
3467a3316fc6SZhikuiRen 
34687e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3469a3316fc6SZhikuiRen {
34707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
34717e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3472ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
34737e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
347445ca1b86SEd Tanous             [&app](const crow::Request& req,
34757e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3476c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3477c937d2bfSEd Tanous             .canDelegateTop = true,
3478c937d2bfSEd Tanous             .canDelegateSkip = true,
3479c937d2bfSEd Tanous         };
3480c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3481c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
34823ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
348345ca1b86SEd Tanous         {
348445ca1b86SEd Tanous             return;
348545ca1b86SEd Tanous         }
3486a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3487a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3488a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3489a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3490a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3491a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3492a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3493a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3494a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3495a3316fc6SZhikuiRen 
3496c937d2bfSEd Tanous         getCurrentBootNumber(asyncResp, delegatedQuery.skip,
3497c937d2bfSEd Tanous                              delegatedQuery.top);
34987e860f15SJohn Edward Broadbent         });
3499a3316fc6SZhikuiRen }
3500a3316fc6SZhikuiRen 
3501647b3cdcSGeorge Liu /**
3502647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3503647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3504647b3cdcSGeorge Liu  *
3505647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3506647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3507647b3cdcSGeorge Liu  * @param[out] index          Index value
3508647b3cdcSGeorge Liu  *
3509647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3510647b3cdcSGeorge Liu  */
3511647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3512647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3513647b3cdcSGeorge Liu {
3514647b3cdcSGeorge Liu     std::vector<std::string> split;
3515647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3516647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3517647b3cdcSGeorge Liu     {
3518647b3cdcSGeorge Liu         return false;
3519647b3cdcSGeorge Liu     }
3520647b3cdcSGeorge Liu 
3521ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3522647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3523ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3524647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3525647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3526647b3cdcSGeorge Liu 
3527647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3528647b3cdcSGeorge Liu     {
3529647b3cdcSGeorge Liu         return false;
3530647b3cdcSGeorge Liu     }
3531647b3cdcSGeorge Liu 
3532647b3cdcSGeorge Liu     start = split[1].data();
3533ca45aa3cSEd Tanous 
3534ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3535647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3536647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3537647b3cdcSGeorge Liu 
3538dcf2ebc0SEd Tanous     return ptrValue == end && ecValue != std::errc();
3539647b3cdcSGeorge Liu }
3540647b3cdcSGeorge Liu 
3541647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3542647b3cdcSGeorge Liu {
35430fda0f12SGeorge Liu     BMCWEB_ROUTE(
35440fda0f12SGeorge Liu         app,
35450fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3546647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3547647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
354845ca1b86SEd Tanous             [&app](const crow::Request& req,
3549647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3550647b3cdcSGeorge Liu                    const std::string& postCodeID) {
35513ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
355245ca1b86SEd Tanous         {
355345ca1b86SEd Tanous             return;
355445ca1b86SEd Tanous         }
3555002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
3556647b3cdcSGeorge Liu         {
3557002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3558647b3cdcSGeorge Liu             return;
3559647b3cdcSGeorge Liu         }
3560647b3cdcSGeorge Liu 
3561647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3562647b3cdcSGeorge Liu         uint16_t index = 0;
3563647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3564647b3cdcSGeorge Liu         {
3565002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3566647b3cdcSGeorge Liu             return;
3567647b3cdcSGeorge Liu         }
3568647b3cdcSGeorge Liu 
3569647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3570647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3571647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3572002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3573002d39b4SEd Tanous                     postcodes) {
3574647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3575647b3cdcSGeorge Liu             {
3576002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3577002d39b4SEd Tanous                                            postCodeID);
3578647b3cdcSGeorge Liu                 return;
3579647b3cdcSGeorge Liu             }
3580647b3cdcSGeorge Liu             if (ec)
3581647b3cdcSGeorge Liu             {
3582647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3583647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
3584647b3cdcSGeorge Liu                 return;
3585647b3cdcSGeorge Liu             }
3586647b3cdcSGeorge Liu 
3587647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
3588002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
3589647b3cdcSGeorge Liu             {
3590647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3591002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3592002d39b4SEd Tanous                                            postCodeID);
3593647b3cdcSGeorge Liu                 return;
3594647b3cdcSGeorge Liu             }
3595647b3cdcSGeorge Liu 
35969eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
359746ff87baSEd Tanous             if (c.empty())
3598647b3cdcSGeorge Liu             {
3599647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
3600002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3601002d39b4SEd Tanous                                            postCodeID);
3602647b3cdcSGeorge Liu                 return;
3603647b3cdcSGeorge Liu             }
360446ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
360546ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
360646ff87baSEd Tanous             std::string_view strData(d, c.size());
3607647b3cdcSGeorge Liu 
3608647b3cdcSGeorge Liu             asyncResp->res.addHeader("Content-Type",
3609647b3cdcSGeorge Liu                                      "application/octet-stream");
3610002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
3611002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
3612647b3cdcSGeorge Liu             },
3613647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
3614647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
3615002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3616647b3cdcSGeorge Liu         });
3617647b3cdcSGeorge Liu }
3618647b3cdcSGeorge Liu 
36197e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3620a3316fc6SZhikuiRen {
36217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
36227e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3623ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
36247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
362545ca1b86SEd Tanous             [&app](const crow::Request& req,
36267e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36277e860f15SJohn Edward Broadbent                    const std::string& targetID) {
36283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
362945ca1b86SEd Tanous         {
363045ca1b86SEd Tanous             return;
363145ca1b86SEd Tanous         }
3632647b3cdcSGeorge Liu         uint16_t bootIndex = 0;
3633647b3cdcSGeorge Liu         uint64_t codeIndex = 0;
3634647b3cdcSGeorge Liu         if (!parsePostCode(targetID, codeIndex, bootIndex))
3635a3316fc6SZhikuiRen         {
3636a3316fc6SZhikuiRen             // Requested ID was not found
3637ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3638a3316fc6SZhikuiRen             return;
3639a3316fc6SZhikuiRen         }
3640a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3641a3316fc6SZhikuiRen         {
3642a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
36437e860f15SJohn Edward Broadbent                              << targetID;
3644a3316fc6SZhikuiRen         }
3645a3316fc6SZhikuiRen 
3646002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3647a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
36480fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3649a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3650a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3651a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3652a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3653a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3654a3316fc6SZhikuiRen 
3655a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
36567e860f15SJohn Edward Broadbent         });
3657a3316fc6SZhikuiRen }
3658a3316fc6SZhikuiRen 
36591da66f75SEd Tanous } // namespace redfish
3660