xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision c937d2bfefa3ff91b7175d9684f99712ec9b88e5)
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 {
6626702d01SEd Tanous     std::span<const MessageEntry>::iterator messageIt = std::find_if(
6726702d01SEd Tanous         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 
3288d1b46d7Szhanghch05 inline void
3298d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3305cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3315cb1dd27SAsmitha Karunanithi {
3325cb1dd27SAsmitha Karunanithi     std::string dumpPath;
3335cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
3345cb1dd27SAsmitha Karunanithi     {
3355cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
3365cb1dd27SAsmitha Karunanithi     }
3375cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
3385cb1dd27SAsmitha Karunanithi     {
3395cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
3405cb1dd27SAsmitha Karunanithi     }
3415cb1dd27SAsmitha Karunanithi     else
3425cb1dd27SAsmitha Karunanithi     {
3435cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
3445cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3455cb1dd27SAsmitha Karunanithi         return;
3465cb1dd27SAsmitha Karunanithi     }
3475cb1dd27SAsmitha Karunanithi 
3485cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
349711ac7a9SEd Tanous         [asyncResp, dumpPath,
350711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
351711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
3525cb1dd27SAsmitha Karunanithi             if (ec)
3535cb1dd27SAsmitha Karunanithi             {
3545cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
3555cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
3565cb1dd27SAsmitha Karunanithi                 return;
3575cb1dd27SAsmitha Karunanithi             }
3585cb1dd27SAsmitha Karunanithi 
3595cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
3605cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
361b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
362b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
363b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
364b47452b2SAsmitha Karunanithi                 "/entry/";
3655cb1dd27SAsmitha Karunanithi 
3665cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
3675cb1dd27SAsmitha Karunanithi             {
368b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
3695cb1dd27SAsmitha Karunanithi                 {
3705cb1dd27SAsmitha Karunanithi                     continue;
3715cb1dd27SAsmitha Karunanithi                 }
3721d8782e7SNan Zhou                 uint64_t timestamp = 0;
3735cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
37435440d18SAsmitha Karunanithi                 std::string dumpStatus;
37535440d18SAsmitha Karunanithi                 nlohmann::json thisEntry;
3762dfd18efSEd Tanous 
3772dfd18efSEd Tanous                 std::string entryID = object.first.filename();
3782dfd18efSEd Tanous                 if (entryID.empty())
3795cb1dd27SAsmitha Karunanithi                 {
3805cb1dd27SAsmitha Karunanithi                     continue;
3815cb1dd27SAsmitha Karunanithi                 }
3825cb1dd27SAsmitha Karunanithi 
3835cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
3845cb1dd27SAsmitha Karunanithi                 {
38535440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
38635440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
38735440d18SAsmitha Karunanithi                     {
3889eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
38935440d18SAsmitha Karunanithi                         {
39035440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
39135440d18SAsmitha Karunanithi                             {
39255f79e6fSEd Tanous                                 const auto* status = std::get_if<std::string>(
39335440d18SAsmitha Karunanithi                                     &propertyMap.second);
39435440d18SAsmitha Karunanithi                                 if (status == nullptr)
39535440d18SAsmitha Karunanithi                                 {
39635440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
39735440d18SAsmitha Karunanithi                                     break;
39835440d18SAsmitha Karunanithi                                 }
39935440d18SAsmitha Karunanithi                                 dumpStatus = *status;
40035440d18SAsmitha Karunanithi                             }
40135440d18SAsmitha Karunanithi                         }
40235440d18SAsmitha Karunanithi                     }
40335440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
40435440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
4055cb1dd27SAsmitha Karunanithi                     {
4065cb1dd27SAsmitha Karunanithi 
4075cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4085cb1dd27SAsmitha Karunanithi                         {
4095cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4105cb1dd27SAsmitha Karunanithi                             {
41155f79e6fSEd Tanous                                 const auto* sizePtr =
4125cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4135cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4145cb1dd27SAsmitha Karunanithi                                 {
4155cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4165cb1dd27SAsmitha Karunanithi                                     break;
4175cb1dd27SAsmitha Karunanithi                                 }
4185cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4195cb1dd27SAsmitha Karunanithi                                 break;
4205cb1dd27SAsmitha Karunanithi                             }
4215cb1dd27SAsmitha Karunanithi                         }
4225cb1dd27SAsmitha Karunanithi                     }
4235cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4245cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4255cb1dd27SAsmitha Karunanithi                     {
4265cb1dd27SAsmitha Karunanithi 
4279eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
4285cb1dd27SAsmitha Karunanithi                         {
4295cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4305cb1dd27SAsmitha Karunanithi                             {
4315cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4325cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4335cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4345cb1dd27SAsmitha Karunanithi                                 {
4355cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4365cb1dd27SAsmitha Karunanithi                                     break;
4375cb1dd27SAsmitha Karunanithi                                 }
4381d8782e7SNan Zhou                                 timestamp = (*usecsTimeStamp / 1000 / 1000);
4395cb1dd27SAsmitha Karunanithi                                 break;
4405cb1dd27SAsmitha Karunanithi                             }
4415cb1dd27SAsmitha Karunanithi                         }
4425cb1dd27SAsmitha Karunanithi                     }
4435cb1dd27SAsmitha Karunanithi                 }
4445cb1dd27SAsmitha Karunanithi 
4450fda0f12SGeorge Liu                 if (dumpStatus !=
4460fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
44735440d18SAsmitha Karunanithi                     !dumpStatus.empty())
44835440d18SAsmitha Karunanithi                 {
44935440d18SAsmitha Karunanithi                     // Dump status is not Complete, no need to enumerate
45035440d18SAsmitha Karunanithi                     continue;
45135440d18SAsmitha Karunanithi                 }
45235440d18SAsmitha Karunanithi 
453647b3cdcSGeorge Liu                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
4545cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
4555cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
4565cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
4571d8782e7SNan Zhou                 thisEntry["Created"] =
4581d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
4595cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
4605cb1dd27SAsmitha Karunanithi 
461d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
4625cb1dd27SAsmitha Karunanithi 
4635cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
4645cb1dd27SAsmitha Karunanithi                 {
465d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
466d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
467de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
468de8d94a3SAbhishek Patel                         entryID + "/attachment";
4695cb1dd27SAsmitha Karunanithi                 }
4705cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
4715cb1dd27SAsmitha Karunanithi                 {
472d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
473d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
474d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
475de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
476de8d94a3SAbhishek Patel                         entryID + "/attachment";
4775cb1dd27SAsmitha Karunanithi                 }
47835440d18SAsmitha Karunanithi                 entriesArray.push_back(std::move(thisEntry));
4795cb1dd27SAsmitha Karunanithi             }
4805cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
4815cb1dd27SAsmitha Karunanithi                 entriesArray.size();
4825cb1dd27SAsmitha Karunanithi         },
4835cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
4845cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
4855cb1dd27SAsmitha Karunanithi }
4865cb1dd27SAsmitha Karunanithi 
4878d1b46d7Szhanghch05 inline void
48845ca1b86SEd Tanous     getDumpEntryById(crow::App& app, const crow::Request& req,
48945ca1b86SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4908d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
4915cb1dd27SAsmitha Karunanithi {
49245ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
49345ca1b86SEd Tanous     {
49445ca1b86SEd Tanous         return;
49545ca1b86SEd Tanous     }
4965cb1dd27SAsmitha Karunanithi     std::string dumpPath;
4975cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
4985cb1dd27SAsmitha Karunanithi     {
4995cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5005cb1dd27SAsmitha Karunanithi     }
5015cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5025cb1dd27SAsmitha Karunanithi     {
5035cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5045cb1dd27SAsmitha Karunanithi     }
5055cb1dd27SAsmitha Karunanithi     else
5065cb1dd27SAsmitha Karunanithi     {
5075cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5085cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5095cb1dd27SAsmitha Karunanithi         return;
5105cb1dd27SAsmitha Karunanithi     }
5115cb1dd27SAsmitha Karunanithi 
5125cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
513711ac7a9SEd Tanous         [asyncResp, entryID, dumpPath,
514711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
515711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
5165cb1dd27SAsmitha Karunanithi             if (ec)
5175cb1dd27SAsmitha Karunanithi             {
5185cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5195cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5205cb1dd27SAsmitha Karunanithi                 return;
5215cb1dd27SAsmitha Karunanithi             }
5225cb1dd27SAsmitha Karunanithi 
523b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
524b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
525b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
526b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
527b47452b2SAsmitha Karunanithi                 "/entry/";
528b47452b2SAsmitha Karunanithi 
5299eb808c1SEd Tanous             for (const auto& objectPath : resp)
5305cb1dd27SAsmitha Karunanithi             {
531b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5325cb1dd27SAsmitha Karunanithi                 {
5335cb1dd27SAsmitha Karunanithi                     continue;
5345cb1dd27SAsmitha Karunanithi                 }
5355cb1dd27SAsmitha Karunanithi 
5365cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5371d8782e7SNan Zhou                 uint64_t timestamp = 0;
5385cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
53935440d18SAsmitha Karunanithi                 std::string dumpStatus;
5405cb1dd27SAsmitha Karunanithi 
5419eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
5425cb1dd27SAsmitha Karunanithi                 {
54335440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
54435440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
54535440d18SAsmitha Karunanithi                     {
5469eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
54735440d18SAsmitha Karunanithi                         {
54835440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
54935440d18SAsmitha Karunanithi                             {
5509eb808c1SEd Tanous                                 const std::string* status =
5519eb808c1SEd Tanous                                     std::get_if<std::string>(
55235440d18SAsmitha Karunanithi                                         &propertyMap.second);
55335440d18SAsmitha Karunanithi                                 if (status == nullptr)
55435440d18SAsmitha Karunanithi                                 {
55535440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
55635440d18SAsmitha Karunanithi                                     break;
55735440d18SAsmitha Karunanithi                                 }
55835440d18SAsmitha Karunanithi                                 dumpStatus = *status;
55935440d18SAsmitha Karunanithi                             }
56035440d18SAsmitha Karunanithi                         }
56135440d18SAsmitha Karunanithi                     }
56235440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
56335440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
5645cb1dd27SAsmitha Karunanithi                     {
5659eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
5665cb1dd27SAsmitha Karunanithi                         {
5675cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
5685cb1dd27SAsmitha Karunanithi                             {
5699eb808c1SEd Tanous                                 const uint64_t* sizePtr =
5705cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
5715cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
5725cb1dd27SAsmitha Karunanithi                                 {
5735cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
5745cb1dd27SAsmitha Karunanithi                                     break;
5755cb1dd27SAsmitha Karunanithi                                 }
5765cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
5775cb1dd27SAsmitha Karunanithi                                 break;
5785cb1dd27SAsmitha Karunanithi                             }
5795cb1dd27SAsmitha Karunanithi                         }
5805cb1dd27SAsmitha Karunanithi                     }
5815cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
5825cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
5835cb1dd27SAsmitha Karunanithi                     {
5849eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
5855cb1dd27SAsmitha Karunanithi                         {
5865cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
5875cb1dd27SAsmitha Karunanithi                             {
5885cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
5895cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
5905cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
5915cb1dd27SAsmitha Karunanithi                                 {
5925cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
5935cb1dd27SAsmitha Karunanithi                                     break;
5945cb1dd27SAsmitha Karunanithi                                 }
5951d8782e7SNan Zhou                                 timestamp = *usecsTimeStamp / 1000 / 1000;
5965cb1dd27SAsmitha Karunanithi                                 break;
5975cb1dd27SAsmitha Karunanithi                             }
5985cb1dd27SAsmitha Karunanithi                         }
5995cb1dd27SAsmitha Karunanithi                     }
6005cb1dd27SAsmitha Karunanithi                 }
6015cb1dd27SAsmitha Karunanithi 
6020fda0f12SGeorge Liu                 if (dumpStatus !=
6030fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
60435440d18SAsmitha Karunanithi                     !dumpStatus.empty())
60535440d18SAsmitha Karunanithi                 {
60635440d18SAsmitha Karunanithi                     // Dump status is not Complete
60735440d18SAsmitha Karunanithi                     // return not found until status is changed to Completed
60835440d18SAsmitha Karunanithi                     messages::resourceNotFound(asyncResp->res,
60935440d18SAsmitha Karunanithi                                                dumpType + " dump", entryID);
61035440d18SAsmitha Karunanithi                     return;
61135440d18SAsmitha Karunanithi                 }
61235440d18SAsmitha Karunanithi 
6135cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
614647b3cdcSGeorge Liu                     "#LogEntry.v1_8_0.LogEntry";
6155cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6165cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6175cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6185cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6191d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
6205cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6215cb1dd27SAsmitha Karunanithi 
622d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6235cb1dd27SAsmitha Karunanithi 
6245cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6255cb1dd27SAsmitha Karunanithi                 {
626d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
627d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
628de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
629de8d94a3SAbhishek Patel                         entryID + "/attachment";
6305cb1dd27SAsmitha Karunanithi                 }
6315cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6325cb1dd27SAsmitha Karunanithi                 {
633d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
634d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6355cb1dd27SAsmitha Karunanithi                         "System";
636d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
637de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
638de8d94a3SAbhishek Patel                         entryID + "/attachment";
6395cb1dd27SAsmitha Karunanithi                 }
6405cb1dd27SAsmitha Karunanithi             }
641e05aec50SEd Tanous             if (!foundDumpEntry)
642b47452b2SAsmitha Karunanithi             {
643b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
644b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
645b47452b2SAsmitha Karunanithi                 return;
646b47452b2SAsmitha Karunanithi             }
6475cb1dd27SAsmitha Karunanithi         },
6485cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6495cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6505cb1dd27SAsmitha Karunanithi }
6515cb1dd27SAsmitha Karunanithi 
6528d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6539878256fSStanley Chu                             const std::string& entryID,
654b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6555cb1dd27SAsmitha Karunanithi {
6563de8d8baSGeorge Liu     auto respHandler = [asyncResp,
6573de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
6585cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6595cb1dd27SAsmitha Karunanithi         if (ec)
6605cb1dd27SAsmitha Karunanithi         {
6613de8d8baSGeorge Liu             if (ec.value() == EBADR)
6623de8d8baSGeorge Liu             {
6633de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6643de8d8baSGeorge Liu                 return;
6653de8d8baSGeorge Liu             }
6665cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
6675cb1dd27SAsmitha Karunanithi                              << ec;
6685cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6695cb1dd27SAsmitha Karunanithi             return;
6705cb1dd27SAsmitha Karunanithi         }
6715cb1dd27SAsmitha Karunanithi     };
6725cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6735cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
674b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
675b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
676b47452b2SAsmitha Karunanithi             entryID,
6775cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6785cb1dd27SAsmitha Karunanithi }
6795cb1dd27SAsmitha Karunanithi 
6808d1b46d7Szhanghch05 inline void
68198be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
6828d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6838d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
684a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
685a43be80fSAsmitha Karunanithi {
686a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
6876145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
688a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
689a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
690cb13a392SEd Tanous             if (err)
691cb13a392SEd Tanous             {
6926145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
6936145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
6946145ed6fSAsmitha Karunanithi                 return task::completed;
695cb13a392SEd Tanous             }
696b9d36b47SEd Tanous 
697b9d36b47SEd Tanous             dbus::utility::DBusInteracesMap interfacesList;
698a43be80fSAsmitha Karunanithi 
699a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
700a43be80fSAsmitha Karunanithi 
701a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
702a43be80fSAsmitha Karunanithi 
703b47452b2SAsmitha Karunanithi             if (objPath.str ==
704b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
705b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
706b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
707a43be80fSAsmitha Karunanithi             {
708a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
709a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
710a43be80fSAsmitha Karunanithi 
711a43be80fSAsmitha Karunanithi                 std::string headerLoc =
712a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
713a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
714a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
715a43be80fSAsmitha Karunanithi 
716a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
717b47452b2SAsmitha Karunanithi                 return task::completed;
7186145ed6fSAsmitha Karunanithi             }
719a43be80fSAsmitha Karunanithi             return task::completed;
720a43be80fSAsmitha Karunanithi         },
7214978b63fSJason M. Bills         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
722a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
723a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
724a43be80fSAsmitha Karunanithi 
725a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
726a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
72798be3e39SEd Tanous     task->payload.emplace(std::move(payload));
728a43be80fSAsmitha Karunanithi }
729a43be80fSAsmitha Karunanithi 
7308d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7318d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
732a43be80fSAsmitha Karunanithi {
733a43be80fSAsmitha Karunanithi     std::string dumpPath;
734a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
735a43be80fSAsmitha Karunanithi     {
736a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
737a43be80fSAsmitha Karunanithi     }
738a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
739a43be80fSAsmitha Karunanithi     {
740a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
741a43be80fSAsmitha Karunanithi     }
742a43be80fSAsmitha Karunanithi     else
743a43be80fSAsmitha Karunanithi     {
744a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
745a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
746a43be80fSAsmitha Karunanithi         return;
747a43be80fSAsmitha Karunanithi     }
748a43be80fSAsmitha Karunanithi 
749a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
750a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
751a43be80fSAsmitha Karunanithi 
75215ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
753a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
754a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
755a43be80fSAsmitha Karunanithi     {
756a43be80fSAsmitha Karunanithi         return;
757a43be80fSAsmitha Karunanithi     }
758a43be80fSAsmitha Karunanithi 
759a43be80fSAsmitha Karunanithi     if (dumpType == "System")
760a43be80fSAsmitha Karunanithi     {
761a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
762a43be80fSAsmitha Karunanithi         {
7634978b63fSJason M. Bills             BMCWEB_LOG_ERROR
7644978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
765a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
766a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
767a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
768a43be80fSAsmitha Karunanithi             return;
769a43be80fSAsmitha Karunanithi         }
7703174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
771a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
772a43be80fSAsmitha Karunanithi         {
773a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
774ace85d60SEd Tanous             messages::internalError(asyncResp->res);
775a43be80fSAsmitha Karunanithi             return;
776a43be80fSAsmitha Karunanithi         }
777a43be80fSAsmitha Karunanithi     }
778a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
779a43be80fSAsmitha Karunanithi     {
780a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
781a43be80fSAsmitha Karunanithi         {
7820fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
7830fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
784a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
785a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
786a43be80fSAsmitha Karunanithi             return;
787a43be80fSAsmitha Karunanithi         }
7883174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
789a43be80fSAsmitha Karunanithi         {
790a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
791a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
792ace85d60SEd Tanous             messages::internalError(asyncResp->res);
793a43be80fSAsmitha Karunanithi             return;
794a43be80fSAsmitha Karunanithi         }
795a43be80fSAsmitha Karunanithi     }
796a43be80fSAsmitha Karunanithi 
797a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
79898be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
79998be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
80098be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
801a43be80fSAsmitha Karunanithi             if (ec)
802a43be80fSAsmitha Karunanithi             {
803a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
804a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
805a43be80fSAsmitha Karunanithi                 return;
806a43be80fSAsmitha Karunanithi             }
807a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
808a43be80fSAsmitha Karunanithi 
80998be3e39SEd Tanous             createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
81098be3e39SEd Tanous                                    dumpPath, dumpType);
811a43be80fSAsmitha Karunanithi         },
812b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
813b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
814b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
815a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
816a43be80fSAsmitha Karunanithi }
817a43be80fSAsmitha Karunanithi 
8188d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8198d1b46d7Szhanghch05                       const std::string& dumpType)
82080319af1SAsmitha Karunanithi {
821b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
822b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8238d1b46d7Szhanghch05 
82480319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
825b9d36b47SEd Tanous         [asyncResp, dumpType](
826b9d36b47SEd Tanous             const boost::system::error_code ec,
827b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
82880319af1SAsmitha Karunanithi             if (ec)
82980319af1SAsmitha Karunanithi             {
83080319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
83180319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
83280319af1SAsmitha Karunanithi                 return;
83380319af1SAsmitha Karunanithi             }
83480319af1SAsmitha Karunanithi 
83580319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
83680319af1SAsmitha Karunanithi             {
8372dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8382dfd18efSEd Tanous                 std::string logID = objPath.filename();
8392dfd18efSEd Tanous                 if (logID.empty())
84080319af1SAsmitha Karunanithi                 {
8412dfd18efSEd Tanous                     continue;
84280319af1SAsmitha Karunanithi                 }
8432dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
84480319af1SAsmitha Karunanithi             }
84580319af1SAsmitha Karunanithi         },
84680319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
84780319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
84880319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
849b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
850b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
851b47452b2SAsmitha Karunanithi                                    dumpType});
85280319af1SAsmitha Karunanithi }
85380319af1SAsmitha Karunanithi 
854b9d36b47SEd Tanous inline static void
855b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
856b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
857b9d36b47SEd Tanous                              std::string& logfile)
858043a0536SJohnathan Mantey {
859043a0536SJohnathan Mantey     for (auto property : params)
860043a0536SJohnathan Mantey     {
861043a0536SJohnathan Mantey         if (property.first == "Timestamp")
862043a0536SJohnathan Mantey         {
863043a0536SJohnathan Mantey             const std::string* value =
8648d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
865043a0536SJohnathan Mantey             if (value != nullptr)
866043a0536SJohnathan Mantey             {
867043a0536SJohnathan Mantey                 timestamp = *value;
868043a0536SJohnathan Mantey             }
869043a0536SJohnathan Mantey         }
870043a0536SJohnathan Mantey         else if (property.first == "Filename")
871043a0536SJohnathan Mantey         {
872043a0536SJohnathan Mantey             const std::string* value =
8738d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
874043a0536SJohnathan Mantey             if (value != nullptr)
875043a0536SJohnathan Mantey             {
876043a0536SJohnathan Mantey                 filename = *value;
877043a0536SJohnathan Mantey             }
878043a0536SJohnathan Mantey         }
879043a0536SJohnathan Mantey         else if (property.first == "Log")
880043a0536SJohnathan Mantey         {
881043a0536SJohnathan Mantey             const std::string* value =
8828d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
883043a0536SJohnathan Mantey             if (value != nullptr)
884043a0536SJohnathan Mantey             {
885043a0536SJohnathan Mantey                 logfile = *value;
886043a0536SJohnathan Mantey             }
887043a0536SJohnathan Mantey         }
888043a0536SJohnathan Mantey     }
889043a0536SJohnathan Mantey }
890043a0536SJohnathan Mantey 
891a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
8927e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
8931da66f75SEd Tanous {
894c4bf6374SJason M. Bills     /**
895c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
896c4bf6374SJason M. Bills      */
8977e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
898ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
89945ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
90045ca1b86SEd Tanous                                                        const std::shared_ptr<
90145ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
90245ca1b86SEd Tanous                                                            asyncResp) {
90345ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
904c4bf6374SJason M. Bills             {
90545ca1b86SEd Tanous                 return;
90645ca1b86SEd Tanous             }
9077e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
9087e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
909c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
910c4bf6374SJason M. Bills                 "#LogServiceCollection.LogServiceCollection";
911c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
912029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices";
91345ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
914c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Description"] =
915c4bf6374SJason M. Bills                 "Collection of LogServices for this Computer System";
9167e860f15SJohn Edward Broadbent             nlohmann::json& logServiceArray =
9177e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members"];
918c4bf6374SJason M. Bills             logServiceArray = nlohmann::json::array();
919029573d4SEd Tanous             logServiceArray.push_back(
9207e860f15SJohn Edward Broadbent                 {{"@odata.id",
9217e860f15SJohn Edward Broadbent                   "/redfish/v1/Systems/system/LogServices/EventLog"}});
9225cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
923c9bb6861Sraviteja-b             logServiceArray.push_back(
92445ca1b86SEd Tanous                 {{"@odata.id", "/redfish/v1/Systems/system/LogServices/Dump"}});
925c9bb6861Sraviteja-b #endif
926c9bb6861Sraviteja-b 
927d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
928d53dd41fSJason M. Bills             logServiceArray.push_back(
929cb92c03bSAndrew Geissler                 {{"@odata.id",
930424c4176SJason M. Bills                   "/redfish/v1/Systems/system/LogServices/Crashdump"}});
931d53dd41fSJason M. Bills #endif
932b7028ebfSSpencer Ku 
933b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
934b7028ebfSSpencer Ku             logServiceArray.push_back(
935b7028ebfSSpencer Ku                 {{"@odata.id",
936b7028ebfSSpencer Ku                   "/redfish/v1/Systems/system/LogServices/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)
97745ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
97845ca1b86SEd Tanous                                                        const std::shared_ptr<
97945ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
98045ca1b86SEd Tanous                                                            asyncResp) {
98145ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
98245ca1b86SEd Tanous             {
98345ca1b86SEd Tanous                 return;
98445ca1b86SEd Tanous             }
985c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
986029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
987c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
988c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
989c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
9907e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
9917e860f15SJohn Edward Broadbent                 "System Event Log Service";
992c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
993c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
9947c8c4058STejas Patil 
9957c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
9967c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
9977c8c4058STejas Patil 
9987c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
9997c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10007c8c4058STejas Patil                 redfishDateTimeOffset.second;
10017c8c4058STejas Patil 
1002c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
1003c4bf6374SJason M. Bills                 {"@odata.id",
1004029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1005e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1006e7d6c8b2SGunnar Mills 
10070fda0f12SGeorge Liu                 {"target",
10080fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10097e860f15SJohn Edward Broadbent         });
1010489640c6SJason M. Bills }
1011489640c6SJason M. Bills 
10127e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1013489640c6SJason M. Bills {
10144978b63fSJason M. Bills     BMCWEB_ROUTE(
10154978b63fSJason M. Bills         app,
10164978b63fSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
1017432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
101945ca1b86SEd Tanous             [&app](const crow::Request& req,
10207e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
102145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
102245ca1b86SEd Tanous                 {
102345ca1b86SEd Tanous                     return;
102445ca1b86SEd Tanous                 }
1025489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1026489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1027489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1028489640c6SJason M. Bills                 {
1029489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1030489640c6SJason M. Bills                     {
1031489640c6SJason M. Bills                         std::error_code ec;
1032489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1033489640c6SJason M. Bills                     }
1034489640c6SJason M. Bills                 }
1035489640c6SJason M. Bills 
1036489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1037489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1038489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1039489640c6SJason M. Bills                         if (ec)
1040489640c6SJason M. Bills                         {
10417e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
10427e860f15SJohn Edward Broadbent                                              << ec;
1043489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1044489640c6SJason M. Bills                             return;
1045489640c6SJason M. Bills                         }
1046489640c6SJason M. Bills 
1047489640c6SJason M. Bills                         messages::success(asyncResp->res);
1048489640c6SJason M. Bills                     },
1049489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
10507e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
10517e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
10527e860f15SJohn Edward Broadbent             });
1053c4bf6374SJason M. Bills }
1054c4bf6374SJason M. Bills 
105595820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1056b5a76932SEd Tanous                                  const std::string& logEntry,
105795820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1058c4bf6374SJason M. Bills {
105995820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1060cd225da8SJason M. Bills     // First get the Timestamp
1061f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1062cd225da8SJason M. Bills     if (space == std::string::npos)
106395820184SJason M. Bills     {
106495820184SJason M. Bills         return 1;
106595820184SJason M. Bills     }
1066cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1067cd225da8SJason M. Bills     // Then get the log contents
1068f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1069cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1070cd225da8SJason M. Bills     {
1071cd225da8SJason M. Bills         return 1;
1072cd225da8SJason M. Bills     }
1073cd225da8SJason M. Bills     std::string_view entry(logEntry);
1074cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1075cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1076cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1077cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1078cd225da8SJason M. Bills                  boost::token_compress_on);
1079cd225da8SJason M. Bills     // We need at least a MessageId to be valid
108026f6976fSEd Tanous     if (logEntryFields.empty())
1081cd225da8SJason M. Bills     {
1082cd225da8SJason M. Bills         return 1;
1083cd225da8SJason M. Bills     }
1084cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
108595820184SJason M. Bills 
10864851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1087fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1088c4bf6374SJason M. Bills 
108954417b02SSui Chen     if (message == nullptr)
1090c4bf6374SJason M. Bills     {
109154417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
109254417b02SSui Chen         return 0;
1093c4bf6374SJason M. Bills     }
1094c4bf6374SJason M. Bills 
109554417b02SSui Chen     std::string msg = message->message;
109654417b02SSui Chen 
109715a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
109826702d01SEd Tanous     std::span<std::string> messageArgs;
109915a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
110015a86ff6SJason M. Bills     {
110115a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
110215a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
110315a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
110415a86ff6SJason M. Bills         if (!messageArgsStart.empty())
110515a86ff6SJason M. Bills         {
110615a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
110715a86ff6SJason M. Bills         }
110815a86ff6SJason M. Bills 
110923a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1110c4bf6374SJason M. Bills 
11114851d45dSJason M. Bills         // Fill the MessageArgs into the Message
111295820184SJason M. Bills         int i = 0;
111395820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11144851d45dSJason M. Bills         {
111595820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11164851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11174851d45dSJason M. Bills             if (argPos != std::string::npos)
11184851d45dSJason M. Bills             {
111995820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11204851d45dSJason M. Bills             }
11214851d45dSJason M. Bills         }
112215a86ff6SJason M. Bills     }
11234851d45dSJason M. Bills 
112495820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
112595820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
112695820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1127f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1128f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
112995820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1130c4bf6374SJason M. Bills     {
113195820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1132c4bf6374SJason M. Bills     }
1133c4bf6374SJason M. Bills 
1134c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
113595820184SJason M. Bills     logEntryJson = {
1136647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1137029573d4SEd Tanous         {"@odata.id",
1138897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
113995820184SJason M. Bills              logEntryID},
1140c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
114195820184SJason M. Bills         {"Id", logEntryID},
114295820184SJason M. Bills         {"Message", std::move(msg)},
114395820184SJason M. Bills         {"MessageId", std::move(messageID)},
1144f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1145c4bf6374SJason M. Bills         {"EntryType", "Event"},
114654417b02SSui Chen         {"Severity", message->messageSeverity},
114795820184SJason M. Bills         {"Created", std::move(timestamp)}};
1148c4bf6374SJason M. Bills     return 0;
1149c4bf6374SJason M. Bills }
1150c4bf6374SJason M. Bills 
11517e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1152c4bf6374SJason M. Bills {
11537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11547e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11558b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
115645ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
115745ca1b86SEd Tanous                                                        const std::shared_ptr<
115845ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
115945ca1b86SEd Tanous                                                            asyncResp) {
1160*c937d2bfSEd Tanous             query_param::QueryCapabilities capabilities = {
1161*c937d2bfSEd Tanous                 .canDelegateTop = true,
1162*c937d2bfSEd Tanous                 .canDelegateSkip = true,
1163*c937d2bfSEd Tanous             };
1164*c937d2bfSEd Tanous             query_param::Query delegatedQuery;
1165*c937d2bfSEd Tanous             if (!redfish::setUpRedfishRouteWithDelegation(
1166*c937d2bfSEd Tanous                     app, req, asyncResp->res, delegatedQuery, capabilities))
1167c4bf6374SJason M. Bills             {
1168c4bf6374SJason M. Bills                 return;
1169c4bf6374SJason M. Bills             }
11707e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
11717e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
1172c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1173c4bf6374SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
1174c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1175029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1176c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1177c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Description"] =
1178c4bf6374SJason M. Bills                 "Collection of System Event Log Entries";
1179cb92c03bSAndrew Geissler 
11804978b63fSJason M. Bills             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1181c4bf6374SJason M. Bills             logEntryArray = nlohmann::json::array();
11827e860f15SJohn Edward Broadbent             // Go through the log files and create a unique ID for each
11837e860f15SJohn Edward Broadbent             // entry
118495820184SJason M. Bills             std::vector<std::filesystem::path> redfishLogFiles;
118595820184SJason M. Bills             getRedfishLogFiles(redfishLogFiles);
1186b01bf299SEd Tanous             uint64_t entryCount = 0;
1187cd225da8SJason M. Bills             std::string logEntry;
118895820184SJason M. Bills 
11897e860f15SJohn Edward Broadbent             // Oldest logs are in the last file, so start there and loop
11907e860f15SJohn Edward Broadbent             // backwards
11917e860f15SJohn Edward Broadbent             for (auto it = redfishLogFiles.rbegin();
11927e860f15SJohn Edward Broadbent                  it < redfishLogFiles.rend(); it++)
1193c4bf6374SJason M. Bills             {
1194cd225da8SJason M. Bills                 std::ifstream logStream(*it);
119595820184SJason M. Bills                 if (!logStream.is_open())
1196c4bf6374SJason M. Bills                 {
1197c4bf6374SJason M. Bills                     continue;
1198c4bf6374SJason M. Bills                 }
1199c4bf6374SJason M. Bills 
1200e85d6b16SJason M. Bills                 // Reset the unique ID on the first entry
1201e85d6b16SJason M. Bills                 bool firstEntry = true;
120295820184SJason M. Bills                 while (std::getline(logStream, logEntry))
120395820184SJason M. Bills                 {
1204c4bf6374SJason M. Bills                     entryCount++;
12057e860f15SJohn Edward Broadbent                     // Handle paging using skip (number of entries to skip
12067e860f15SJohn Edward Broadbent                     // from the start) and top (number of entries to
12077e860f15SJohn Edward Broadbent                     // display)
1208*c937d2bfSEd Tanous                     if (entryCount <= delegatedQuery.skip ||
1209*c937d2bfSEd Tanous                         entryCount > delegatedQuery.skip + delegatedQuery.top)
1210c4bf6374SJason M. Bills                     {
1211c4bf6374SJason M. Bills                         continue;
1212c4bf6374SJason M. Bills                     }
1213c4bf6374SJason M. Bills 
1214c4bf6374SJason M. Bills                     std::string idStr;
1215e85d6b16SJason M. Bills                     if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1216c4bf6374SJason M. Bills                     {
1217c4bf6374SJason M. Bills                         continue;
1218c4bf6374SJason M. Bills                     }
1219c4bf6374SJason M. Bills 
1220e85d6b16SJason M. Bills                     if (firstEntry)
1221e85d6b16SJason M. Bills                     {
1222e85d6b16SJason M. Bills                         firstEntry = false;
1223e85d6b16SJason M. Bills                     }
1224e85d6b16SJason M. Bills 
1225c4bf6374SJason M. Bills                     logEntryArray.push_back({});
1226c4bf6374SJason M. Bills                     nlohmann::json& bmcLogEntry = logEntryArray.back();
12274978b63fSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) !=
12284978b63fSJason M. Bills                         0)
1229c4bf6374SJason M. Bills                     {
1230c4bf6374SJason M. Bills                         messages::internalError(asyncResp->res);
1231c4bf6374SJason M. Bills                         return;
1232c4bf6374SJason M. Bills                     }
1233c4bf6374SJason M. Bills                 }
123495820184SJason M. Bills             }
1235c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1236*c937d2bfSEd Tanous             if (delegatedQuery.skip + delegatedQuery.top < entryCount)
1237c4bf6374SJason M. Bills             {
1238c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
12394978b63fSJason M. Bills                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
1240*c937d2bfSEd Tanous                     std::to_string(delegatedQuery.skip + delegatedQuery.top);
1241c4bf6374SJason M. Bills             }
12427e860f15SJohn Edward Broadbent         });
1243897967deSJason M. Bills }
1244897967deSJason M. Bills 
12457e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1246897967deSJason M. Bills {
12477e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12487e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1249ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12507e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
125145ca1b86SEd Tanous             [&app](const crow::Request& req,
12527e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12537e860f15SJohn Edward Broadbent                    const std::string& param) {
125445ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
125545ca1b86SEd Tanous                 {
125645ca1b86SEd Tanous                     return;
125745ca1b86SEd Tanous                 }
12587e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
12598d1b46d7Szhanghch05 
12607e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
12617e860f15SJohn Edward Broadbent                 // entry to find the target entry
1262897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1263897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1264897967deSJason M. Bills                 std::string logEntry;
1265897967deSJason M. Bills 
12667e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12677e860f15SJohn Edward Broadbent                 // backwards
12687e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12697e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1270897967deSJason M. Bills                 {
1271897967deSJason M. Bills                     std::ifstream logStream(*it);
1272897967deSJason M. Bills                     if (!logStream.is_open())
1273897967deSJason M. Bills                     {
1274897967deSJason M. Bills                         continue;
1275897967deSJason M. Bills                     }
1276897967deSJason M. Bills 
1277897967deSJason M. Bills                     // Reset the unique ID on the first entry
1278897967deSJason M. Bills                     bool firstEntry = true;
1279897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1280897967deSJason M. Bills                     {
1281897967deSJason M. Bills                         std::string idStr;
1282897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1283897967deSJason M. Bills                         {
1284897967deSJason M. Bills                             continue;
1285897967deSJason M. Bills                         }
1286897967deSJason M. Bills 
1287897967deSJason M. Bills                         if (firstEntry)
1288897967deSJason M. Bills                         {
1289897967deSJason M. Bills                             firstEntry = false;
1290897967deSJason M. Bills                         }
1291897967deSJason M. Bills 
1292897967deSJason M. Bills                         if (idStr == targetID)
1293897967deSJason M. Bills                         {
12947e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
12957e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1296897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1297897967deSJason M. Bills                             {
1298897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1299897967deSJason M. Bills                                 return;
1300897967deSJason M. Bills                             }
1301897967deSJason M. Bills                             return;
1302897967deSJason M. Bills                         }
1303897967deSJason M. Bills                     }
1304897967deSJason M. Bills                 }
1305897967deSJason M. Bills                 // Requested ID was not found
1306ace85d60SEd Tanous                 messages::resourceMissingAtURI(
1307ace85d60SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(targetID));
13087e860f15SJohn Edward Broadbent             });
130908a4e4b5SAnthony Wilson }
131008a4e4b5SAnthony Wilson 
13117e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
131208a4e4b5SAnthony Wilson {
13137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13147e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1315ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
131645ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
131745ca1b86SEd Tanous                                                        const std::shared_ptr<
131845ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
131945ca1b86SEd Tanous                                                            asyncResp) {
132045ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
132145ca1b86SEd Tanous             {
132245ca1b86SEd Tanous                 return;
132345ca1b86SEd Tanous             }
13247e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
13257e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
132608a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
132708a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
132808a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
132908a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
133008a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
133108a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
133208a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
133308a4e4b5SAnthony Wilson 
1334cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1335cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1336cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1337cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1338914e2d5dSEd Tanous                             const dbus::utility::ManagedObjectType& resp) {
1339cb92c03bSAndrew Geissler                     if (ec)
1340cb92c03bSAndrew Geissler                     {
1341cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1342cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1343cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1344cb92c03bSAndrew Geissler                             << ec;
1345cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1346cb92c03bSAndrew Geissler                         return;
1347cb92c03bSAndrew Geissler                     }
1348cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1349cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1350cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
13519eb808c1SEd Tanous                     for (const auto& objectPath : resp)
1352cb92c03bSAndrew Geissler                     {
1353914e2d5dSEd Tanous                         const uint32_t* id = nullptr;
1354c419c759SEd Tanous                         const uint64_t* timestamp = nullptr;
1355c419c759SEd Tanous                         const uint64_t* updateTimestamp = nullptr;
1356914e2d5dSEd Tanous                         const std::string* severity = nullptr;
1357914e2d5dSEd Tanous                         const std::string* message = nullptr;
1358914e2d5dSEd Tanous                         const std::string* filePath = nullptr;
135975710de2SXiaochao Ma                         bool resolved = false;
13609eb808c1SEd Tanous                         for (const auto& interfaceMap : objectPath.second)
1361f86bb901SAdriana Kobylak                         {
1362f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1363f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1364f86bb901SAdriana Kobylak                             {
13659eb808c1SEd Tanous                                 for (const auto& propertyMap :
13669eb808c1SEd Tanous                                      interfaceMap.second)
1367cb92c03bSAndrew Geissler                                 {
1368cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1369cb92c03bSAndrew Geissler                                     {
1370f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1371f86bb901SAdriana Kobylak                                             &propertyMap.second);
1372cb92c03bSAndrew Geissler                                     }
1373cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1374cb92c03bSAndrew Geissler                                     {
1375c419c759SEd Tanous                                         timestamp = std::get_if<uint64_t>(
1376f86bb901SAdriana Kobylak                                             &propertyMap.second);
13777e860f15SJohn Edward Broadbent                                     }
13787e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
13797e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
13807e860f15SJohn Edward Broadbent                                     {
1381c419c759SEd Tanous                                         updateTimestamp = std::get_if<uint64_t>(
13827e860f15SJohn Edward Broadbent                                             &propertyMap.second);
13837e860f15SJohn Edward Broadbent                                     }
13847e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
13857e860f15SJohn Edward Broadbent                                     {
13867e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
13877e860f15SJohn Edward Broadbent                                             &propertyMap.second);
13887e860f15SJohn Edward Broadbent                                     }
13897e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
13907e860f15SJohn Edward Broadbent                                     {
13917e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
13927e860f15SJohn Edward Broadbent                                             &propertyMap.second);
13937e860f15SJohn Edward Broadbent                                     }
13947e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
13957e860f15SJohn Edward Broadbent                                     {
1396914e2d5dSEd Tanous                                         const bool* resolveptr =
1397914e2d5dSEd Tanous                                             std::get_if<bool>(
13987e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
13997e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14007e860f15SJohn Edward Broadbent                                         {
14017e860f15SJohn Edward Broadbent                                             messages::internalError(
14027e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14037e860f15SJohn Edward Broadbent                                             return;
14047e860f15SJohn Edward Broadbent                                         }
14057e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14067e860f15SJohn Edward Broadbent                                     }
14077e860f15SJohn Edward Broadbent                                 }
14087e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14097e860f15SJohn Edward Broadbent                                     severity == nullptr)
14107e860f15SJohn Edward Broadbent                                 {
14117e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
14127e860f15SJohn Edward Broadbent                                     return;
14137e860f15SJohn Edward Broadbent                                 }
14147e860f15SJohn Edward Broadbent                             }
14157e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
14167e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
14177e860f15SJohn Edward Broadbent                             {
14189eb808c1SEd Tanous                                 for (const auto& propertyMap :
14199eb808c1SEd Tanous                                      interfaceMap.second)
14207e860f15SJohn Edward Broadbent                                 {
14217e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
14227e860f15SJohn Edward Broadbent                                     {
14237e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
14247e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14257e860f15SJohn Edward Broadbent                                     }
14267e860f15SJohn Edward Broadbent                                 }
14277e860f15SJohn Edward Broadbent                             }
14287e860f15SJohn Edward Broadbent                         }
14297e860f15SJohn Edward Broadbent                         // Object path without the
14307e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
14317e860f15SJohn Edward Broadbent                         // and continue.
14327e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
1433c419c759SEd Tanous                             severity == nullptr || timestamp == nullptr ||
1434c419c759SEd Tanous                             updateTimestamp == nullptr)
14357e860f15SJohn Edward Broadbent                         {
14367e860f15SJohn Edward Broadbent                             continue;
14377e860f15SJohn Edward Broadbent                         }
14387e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
14397e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
14407e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14417e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
14420fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14437e860f15SJohn Edward Broadbent                             std::to_string(*id);
14447e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
14457e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
14467e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
14477e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
14487e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
14497e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
14507e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
14517e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
1452c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*timestamp);
14537e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
1454c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*updateTimestamp);
14557e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
14567e860f15SJohn Edward Broadbent                         {
14577e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
14580fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14597e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
14607e860f15SJohn Edward Broadbent                         }
14617e860f15SJohn Edward Broadbent                     }
14627e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
14637e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
14647e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
14657e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
14667e860f15SJohn Edward Broadbent                               });
14677e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
14687e860f15SJohn Edward Broadbent                         entriesArray.size();
14697e860f15SJohn Edward Broadbent                 },
14707e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
14717e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14727e860f15SJohn Edward Broadbent         });
14737e860f15SJohn Edward Broadbent }
14747e860f15SJohn Edward Broadbent 
14757e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
14767e860f15SJohn Edward Broadbent {
14777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
14787e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1479ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
148045ca1b86SEd Tanous         .methods(
148145ca1b86SEd Tanous             boost::beast::http::verb::
148245ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
14837e860f15SJohn Edward Broadbent                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
148445ca1b86SEd Tanous                             const std::string& param) {
148545ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
14867e860f15SJohn Edward Broadbent             {
148745ca1b86SEd Tanous                 return;
148845ca1b86SEd Tanous             }
14897e860f15SJohn Edward Broadbent             std::string entryID = param;
14907e860f15SJohn Edward Broadbent             dbus::utility::escapePathForDbus(entryID);
14917e860f15SJohn Edward Broadbent 
14927e860f15SJohn Edward Broadbent             // DBus implementation of EventLog/Entries
14937e860f15SJohn Edward Broadbent             // Make call to Logging Service to find all log entry objects
14947e860f15SJohn Edward Broadbent             crow::connections::systemBus->async_method_call(
1495b9d36b47SEd Tanous                 [asyncResp,
1496b9d36b47SEd Tanous                  entryID](const boost::system::error_code ec,
1497b9d36b47SEd Tanous                           const dbus::utility::DBusPropertiesMap& resp) {
14987e860f15SJohn Edward Broadbent                     if (ec.value() == EBADR)
14997e860f15SJohn Edward Broadbent                     {
150045ca1b86SEd Tanous                         messages::resourceNotFound(asyncResp->res,
150145ca1b86SEd Tanous                                                    "EventLogEntry", entryID);
15027e860f15SJohn Edward Broadbent                         return;
15037e860f15SJohn Edward Broadbent                     }
15047e860f15SJohn Edward Broadbent                     if (ec)
15057e860f15SJohn Edward Broadbent                     {
15060fda0f12SGeorge Liu                         BMCWEB_LOG_ERROR
15070fda0f12SGeorge Liu                             << "EventLogEntry (DBus) resp_handler got error "
15087e860f15SJohn Edward Broadbent                             << ec;
15097e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
15107e860f15SJohn Edward Broadbent                         return;
15117e860f15SJohn Edward Broadbent                     }
1512914e2d5dSEd Tanous                     const uint32_t* id = nullptr;
1513c419c759SEd Tanous                     const uint64_t* timestamp = nullptr;
1514c419c759SEd Tanous                     const uint64_t* updateTimestamp = nullptr;
1515914e2d5dSEd Tanous                     const std::string* severity = nullptr;
1516914e2d5dSEd Tanous                     const std::string* message = nullptr;
1517914e2d5dSEd Tanous                     const std::string* filePath = nullptr;
15187e860f15SJohn Edward Broadbent                     bool resolved = false;
15197e860f15SJohn Edward Broadbent 
15209eb808c1SEd Tanous                     for (const auto& propertyMap : resp)
15217e860f15SJohn Edward Broadbent                     {
15227e860f15SJohn Edward Broadbent                         if (propertyMap.first == "Id")
15237e860f15SJohn Edward Broadbent                         {
15247e860f15SJohn Edward Broadbent                             id = std::get_if<uint32_t>(&propertyMap.second);
15257e860f15SJohn Edward Broadbent                         }
15267e860f15SJohn Edward Broadbent                         else if (propertyMap.first == "Timestamp")
15277e860f15SJohn Edward Broadbent                         {
1528c419c759SEd Tanous                             timestamp =
15297e860f15SJohn Edward Broadbent                                 std::get_if<uint64_t>(&propertyMap.second);
1530ebd45906SGeorge Liu                         }
1531d139c236SGeorge Liu                         else if (propertyMap.first == "UpdateTimestamp")
1532d139c236SGeorge Liu                         {
1533ebd45906SGeorge Liu                             updateTimestamp =
1534c419c759SEd Tanous                                 std::get_if<uint64_t>(&propertyMap.second);
1535ebd45906SGeorge Liu                         }
1536cb92c03bSAndrew Geissler                         else if (propertyMap.first == "Severity")
1537cb92c03bSAndrew Geissler                         {
153845ca1b86SEd Tanous                             severity =
153945ca1b86SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
1540cb92c03bSAndrew Geissler                         }
1541cb92c03bSAndrew Geissler                         else if (propertyMap.first == "Message")
1542cb92c03bSAndrew Geissler                         {
154345ca1b86SEd Tanous                             message =
154445ca1b86SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
1545ae34c8e8SAdriana Kobylak                         }
154675710de2SXiaochao Ma                         else if (propertyMap.first == "Resolved")
154775710de2SXiaochao Ma                         {
1548914e2d5dSEd Tanous                             const bool* resolveptr =
154975710de2SXiaochao Ma                                 std::get_if<bool>(&propertyMap.second);
155075710de2SXiaochao Ma                             if (resolveptr == nullptr)
155175710de2SXiaochao Ma                             {
155275710de2SXiaochao Ma                                 messages::internalError(asyncResp->res);
155375710de2SXiaochao Ma                                 return;
155475710de2SXiaochao Ma                             }
155575710de2SXiaochao Ma                             resolved = *resolveptr;
155675710de2SXiaochao Ma                         }
15577e860f15SJohn Edward Broadbent                         else if (propertyMap.first == "Path")
1558f86bb901SAdriana Kobylak                         {
155945ca1b86SEd Tanous                             filePath =
156045ca1b86SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
1561f86bb901SAdriana Kobylak                         }
1562f86bb901SAdriana Kobylak                     }
1563f86bb901SAdriana Kobylak                     if (id == nullptr || message == nullptr ||
1564c419c759SEd Tanous                         severity == nullptr || timestamp == nullptr ||
1565c419c759SEd Tanous                         updateTimestamp == nullptr)
1566f86bb901SAdriana Kobylak                     {
1567ae34c8e8SAdriana Kobylak                         messages::internalError(asyncResp->res);
1568271584abSEd Tanous                         return;
1569271584abSEd Tanous                     }
1570f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["@odata.type"] =
1571f86bb901SAdriana Kobylak                         "#LogEntry.v1_8_0.LogEntry";
1572f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["@odata.id"] =
15730fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1574f86bb901SAdriana Kobylak                         std::to_string(*id);
157545ca1b86SEd Tanous                     asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1576f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1577f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["Message"] = *message;
1578f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["Resolved"] = resolved;
1579f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["EntryType"] = "Event";
1580f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["Severity"] =
1581f86bb901SAdriana Kobylak                         translateSeverityDbusToRedfish(*severity);
1582f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["Created"] =
1583c419c759SEd Tanous                         crow::utility::getDateTimeUintMs(*timestamp);
1584f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["Modified"] =
1585c419c759SEd Tanous                         crow::utility::getDateTimeUintMs(*updateTimestamp);
1586f86bb901SAdriana Kobylak                     if (filePath != nullptr)
1587f86bb901SAdriana Kobylak                     {
1588f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["AdditionalDataURI"] =
15890fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
15907e860f15SJohn Edward Broadbent                             std::to_string(*id);
1591f86bb901SAdriana Kobylak                     }
1592cb92c03bSAndrew Geissler                 },
1593cb92c03bSAndrew Geissler                 "xyz.openbmc_project.Logging",
1594cb92c03bSAndrew Geissler                 "/xyz/openbmc_project/logging/entry/" + entryID,
1595f86bb901SAdriana Kobylak                 "org.freedesktop.DBus.Properties", "GetAll", "");
15967e860f15SJohn Edward Broadbent         });
1597336e96c6SChicago Duan 
15987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15997e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1600ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16017e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
160245ca1b86SEd Tanous             [&app](const crow::Request& req,
16037e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16047e860f15SJohn Edward Broadbent                    const std::string& entryId) {
160545ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
160645ca1b86SEd Tanous                 {
160745ca1b86SEd Tanous                     return;
160845ca1b86SEd Tanous                 }
160975710de2SXiaochao Ma                 std::optional<bool> resolved;
161075710de2SXiaochao Ma 
161115ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
16127e860f15SJohn Edward Broadbent                                               resolved))
161375710de2SXiaochao Ma                 {
161475710de2SXiaochao Ma                     return;
161575710de2SXiaochao Ma                 }
161675710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
161775710de2SXiaochao Ma 
161875710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
16194f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
162075710de2SXiaochao Ma                         if (ec)
162175710de2SXiaochao Ma                         {
162275710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
162375710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
162475710de2SXiaochao Ma                             return;
162575710de2SXiaochao Ma                         }
162675710de2SXiaochao Ma                     },
162775710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
162875710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
162975710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
163075710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
1631168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*resolved));
16327e860f15SJohn Edward Broadbent             });
163375710de2SXiaochao Ma 
16347e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16357e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1636ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1637ed398213SEd Tanous 
163845ca1b86SEd Tanous         .methods(boost::beast::http::verb::
163945ca1b86SEd Tanous                      delete_)([&app](const crow::Request& req,
164045ca1b86SEd Tanous                                      const std::shared_ptr<bmcweb::AsyncResp>&
164145ca1b86SEd Tanous                                          asyncResp,
164245ca1b86SEd Tanous                                      const std::string& param) {
164345ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1644336e96c6SChicago Duan             {
164545ca1b86SEd Tanous                 return;
164645ca1b86SEd Tanous             }
1647336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1648336e96c6SChicago Duan 
16497e860f15SJohn Edward Broadbent             std::string entryID = param;
1650336e96c6SChicago Duan 
1651336e96c6SChicago Duan             dbus::utility::escapePathForDbus(entryID);
1652336e96c6SChicago Duan 
1653336e96c6SChicago Duan             // Process response from Logging service.
165445ca1b86SEd Tanous             auto respHandler = [asyncResp,
165545ca1b86SEd Tanous                                 entryID](const boost::system::error_code ec) {
16567e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG
16577e860f15SJohn Edward Broadbent                     << "EventLogEntry (DBus) doDelete callback: Done";
1658336e96c6SChicago Duan                 if (ec)
1659336e96c6SChicago Duan                 {
16603de8d8baSGeorge Liu                     if (ec.value() == EBADR)
16613de8d8baSGeorge Liu                     {
166245ca1b86SEd Tanous                         messages::resourceNotFound(asyncResp->res, "LogEntry",
166345ca1b86SEd Tanous                                                    entryID);
16643de8d8baSGeorge Liu                         return;
16653de8d8baSGeorge Liu                     }
1666336e96c6SChicago Duan                     // TODO Handle for specific error code
16670fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
16680fda0f12SGeorge Liu                         << "EventLogEntry (DBus) doDelete respHandler got error "
1669336e96c6SChicago Duan                         << ec;
1670336e96c6SChicago Duan                     asyncResp->res.result(
1671336e96c6SChicago Duan                         boost::beast::http::status::internal_server_error);
1672336e96c6SChicago Duan                     return;
1673336e96c6SChicago Duan                 }
1674336e96c6SChicago Duan 
1675336e96c6SChicago Duan                 asyncResp->res.result(boost::beast::http::status::ok);
1676336e96c6SChicago Duan             };
1677336e96c6SChicago Duan 
1678336e96c6SChicago Duan             // Make call to Logging service to request Delete Log
1679336e96c6SChicago Duan             crow::connections::systemBus->async_method_call(
1680336e96c6SChicago Duan                 respHandler, "xyz.openbmc_project.Logging",
1681336e96c6SChicago Duan                 "/xyz/openbmc_project/logging/entry/" + entryID,
1682336e96c6SChicago Duan                 "xyz.openbmc_project.Object.Delete", "Delete");
16837e860f15SJohn Edward Broadbent         });
1684400fd1fbSAdriana Kobylak }
1685400fd1fbSAdriana Kobylak 
16867e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1687400fd1fbSAdriana Kobylak {
16880fda0f12SGeorge Liu     BMCWEB_ROUTE(
16890fda0f12SGeorge Liu         app,
16900fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1691ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
16927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
169345ca1b86SEd Tanous             [&app](const crow::Request& req,
16947e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
169545ca1b86SEd Tanous                    const std::string& param) {
169645ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
16977e860f15SJohn Edward Broadbent                 {
169845ca1b86SEd Tanous                     return;
169945ca1b86SEd Tanous                 }
1700647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
1701647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
1702400fd1fbSAdriana Kobylak                 {
17037e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17047e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1705400fd1fbSAdriana Kobylak                     return;
1706400fd1fbSAdriana Kobylak                 }
1707400fd1fbSAdriana Kobylak 
17087e860f15SJohn Edward Broadbent                 std::string entryID = param;
1709400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1710400fd1fbSAdriana Kobylak 
1711400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17127e860f15SJohn Edward Broadbent                     [asyncResp,
17137e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1714400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1715400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1716400fd1fbSAdriana Kobylak                         {
17177e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
17187e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1719400fd1fbSAdriana Kobylak                             return;
1720400fd1fbSAdriana Kobylak                         }
1721400fd1fbSAdriana Kobylak                         if (ec)
1722400fd1fbSAdriana Kobylak                         {
1723400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1724400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1725400fd1fbSAdriana Kobylak                             return;
1726400fd1fbSAdriana Kobylak                         }
1727400fd1fbSAdriana Kobylak 
1728400fd1fbSAdriana Kobylak                         int fd = -1;
1729400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1730400fd1fbSAdriana Kobylak                         if (fd == -1)
1731400fd1fbSAdriana Kobylak                         {
1732400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1733400fd1fbSAdriana Kobylak                             return;
1734400fd1fbSAdriana Kobylak                         }
1735400fd1fbSAdriana Kobylak 
1736400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1737400fd1fbSAdriana Kobylak                         if (size == -1)
1738400fd1fbSAdriana Kobylak                         {
1739400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1740400fd1fbSAdriana Kobylak                             return;
1741400fd1fbSAdriana Kobylak                         }
1742400fd1fbSAdriana Kobylak 
1743400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1744400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1745400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1746400fd1fbSAdriana Kobylak                         {
1747400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1748400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1749400fd1fbSAdriana Kobylak                                 << maxFileSize;
1750400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1751400fd1fbSAdriana Kobylak                             return;
1752400fd1fbSAdriana Kobylak                         }
1753400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1754400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1755400fd1fbSAdriana Kobylak                         if (rc == -1)
1756400fd1fbSAdriana Kobylak                         {
1757400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1758400fd1fbSAdriana Kobylak                             return;
1759400fd1fbSAdriana Kobylak                         }
1760400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1761400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1762400fd1fbSAdriana Kobylak                         {
1763400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1764400fd1fbSAdriana Kobylak                             return;
1765400fd1fbSAdriana Kobylak                         }
1766400fd1fbSAdriana Kobylak                         close(fd);
1767400fd1fbSAdriana Kobylak 
1768400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
17697e860f15SJohn Edward Broadbent                         std::string output =
17707e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1771400fd1fbSAdriana Kobylak 
1772400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1773400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
17747e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
17757e860f15SJohn Edward Broadbent                                                  "Base64");
1776400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1777400fd1fbSAdriana Kobylak                     },
1778400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1779400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1780400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
17817e860f15SJohn Edward Broadbent             });
17821da66f75SEd Tanous }
17831da66f75SEd Tanous 
1784b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1785b7028ebfSSpencer Ku 
1786b7028ebfSSpencer Ku inline bool
1787b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1788b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1789b7028ebfSSpencer Ku {
1790b7028ebfSSpencer Ku     std::error_code ec;
1791b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1792b7028ebfSSpencer Ku     if (ec)
1793b7028ebfSSpencer Ku     {
1794b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1795b7028ebfSSpencer Ku         return false;
1796b7028ebfSSpencer Ku     }
1797b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1798b7028ebfSSpencer Ku     {
1799b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1800b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1801b7028ebfSSpencer Ku         // path
1802b7028ebfSSpencer Ku         if (boost::starts_with(filename, "log"))
1803b7028ebfSSpencer Ku         {
1804b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1805b7028ebfSSpencer Ku         }
1806b7028ebfSSpencer Ku     }
1807b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1808b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1809b7028ebfSSpencer Ku     // descending order.
1810b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1811b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1812b7028ebfSSpencer Ku 
1813b7028ebfSSpencer Ku     return true;
1814b7028ebfSSpencer Ku }
1815b7028ebfSSpencer Ku 
1816b7028ebfSSpencer Ku inline bool
1817b7028ebfSSpencer Ku     getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1818*c937d2bfSEd Tanous                          uint64_t skip, uint64_t top,
1819b7028ebfSSpencer Ku                          std::vector<std::string>& logEntries, size_t& logCount)
1820b7028ebfSSpencer Ku {
1821b7028ebfSSpencer Ku     GzFileReader logFile;
1822b7028ebfSSpencer Ku 
1823b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1824b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1825b7028ebfSSpencer Ku     {
1826b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1827b7028ebfSSpencer Ku         {
1828b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1829b7028ebfSSpencer Ku             return false;
1830b7028ebfSSpencer Ku         }
1831b7028ebfSSpencer Ku     }
1832b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1833b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1834b7028ebfSSpencer Ku     if (!lastMessage.empty())
1835b7028ebfSSpencer Ku     {
1836b7028ebfSSpencer Ku         logCount++;
1837b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1838b7028ebfSSpencer Ku         {
1839b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1840b7028ebfSSpencer Ku         }
1841b7028ebfSSpencer Ku     }
1842b7028ebfSSpencer Ku     return true;
1843b7028ebfSSpencer Ku }
1844b7028ebfSSpencer Ku 
1845b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1846b7028ebfSSpencer Ku                                     const std::string& msg,
1847b7028ebfSSpencer Ku                                     nlohmann::json& logEntryJson)
1848b7028ebfSSpencer Ku {
1849b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
1850b7028ebfSSpencer Ku     logEntryJson = {
1851b7028ebfSSpencer Ku         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1852b7028ebfSSpencer Ku         {"@odata.id",
1853b7028ebfSSpencer Ku          "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1854b7028ebfSSpencer Ku              logEntryID},
1855b7028ebfSSpencer Ku         {"Name", "Host Logger Entry"},
1856b7028ebfSSpencer Ku         {"Id", logEntryID},
1857b7028ebfSSpencer Ku         {"Message", msg},
1858b7028ebfSSpencer Ku         {"EntryType", "Oem"},
1859b7028ebfSSpencer Ku         {"Severity", "OK"},
1860b7028ebfSSpencer Ku         {"OemRecordFormat", "Host Logger Entry"}};
1861b7028ebfSSpencer Ku }
1862b7028ebfSSpencer Ku 
1863b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1864b7028ebfSSpencer Ku {
1865b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1866b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
186745ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
186845ca1b86SEd Tanous                                                        const std::shared_ptr<
186945ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
187045ca1b86SEd Tanous                                                            asyncResp) {
187145ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
187245ca1b86SEd Tanous             {
187345ca1b86SEd Tanous                 return;
187445ca1b86SEd Tanous             }
1875b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1876b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger";
1877b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1878b7028ebfSSpencer Ku                 "#LogService.v1_1_0.LogService";
1879b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1880b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1881b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Id"] = "HostLogger";
1882b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Entries"] = {
18830fda0f12SGeorge Liu                 {"@odata.id",
18840fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1885b7028ebfSSpencer Ku         });
1886b7028ebfSSpencer Ku }
1887b7028ebfSSpencer Ku 
1888b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1889b7028ebfSSpencer Ku {
1890b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1891b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1892b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
189345ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
189445ca1b86SEd Tanous                                                        const std::shared_ptr<
189545ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
189645ca1b86SEd Tanous                                                            asyncResp) {
1897*c937d2bfSEd Tanous             query_param::QueryCapabilities capabilities = {
1898*c937d2bfSEd Tanous                 .canDelegateTop = true,
1899*c937d2bfSEd Tanous                 .canDelegateSkip = true,
1900*c937d2bfSEd Tanous             };
1901*c937d2bfSEd Tanous             query_param::Query delegatedQuery;
1902*c937d2bfSEd Tanous             if (!redfish::setUpRedfishRouteWithDelegation(
1903*c937d2bfSEd Tanous                     app, req, asyncResp->res, delegatedQuery, capabilities))
1904b7028ebfSSpencer Ku             {
1905b7028ebfSSpencer Ku                 return;
1906b7028ebfSSpencer Ku             }
1907b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1908b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1909b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1910b7028ebfSSpencer Ku                 "#LogEntryCollection.LogEntryCollection";
1911b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1912b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] =
1913b7028ebfSSpencer Ku                 "Collection of HostLogger Entries";
19140fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1915b7028ebfSSpencer Ku             logEntryArray = nlohmann::json::array();
1916b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = 0;
1917b7028ebfSSpencer Ku 
1918b7028ebfSSpencer Ku             std::vector<std::filesystem::path> hostLoggerFiles;
1919b7028ebfSSpencer Ku             if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1920b7028ebfSSpencer Ku             {
1921b7028ebfSSpencer Ku                 BMCWEB_LOG_ERROR << "fail to get host log file path";
1922b7028ebfSSpencer Ku                 return;
1923b7028ebfSSpencer Ku             }
1924b7028ebfSSpencer Ku 
1925b7028ebfSSpencer Ku             size_t logCount = 0;
1926b7028ebfSSpencer Ku             // This vector only store the entries we want to expose that
1927b7028ebfSSpencer Ku             // control by skip and top.
1928b7028ebfSSpencer Ku             std::vector<std::string> logEntries;
1929*c937d2bfSEd Tanous             if (!getHostLoggerEntries(hostLoggerFiles, delegatedQuery.skip,
1930*c937d2bfSEd Tanous                                       delegatedQuery.top, logEntries, logCount))
1931b7028ebfSSpencer Ku             {
1932b7028ebfSSpencer Ku                 messages::internalError(asyncResp->res);
1933b7028ebfSSpencer Ku                 return;
1934b7028ebfSSpencer Ku             }
1935b7028ebfSSpencer Ku             // If vector is empty, that means skip value larger than total
1936b7028ebfSSpencer Ku             // log count
193726f6976fSEd Tanous             if (logEntries.empty())
1938b7028ebfSSpencer Ku             {
1939b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1940b7028ebfSSpencer Ku                 return;
1941b7028ebfSSpencer Ku             }
194226f6976fSEd Tanous             if (!logEntries.empty())
1943b7028ebfSSpencer Ku             {
1944b7028ebfSSpencer Ku                 for (size_t i = 0; i < logEntries.size(); i++)
1945b7028ebfSSpencer Ku                 {
1946b7028ebfSSpencer Ku                     logEntryArray.push_back({});
1947b7028ebfSSpencer Ku                     nlohmann::json& hostLogEntry = logEntryArray.back();
1948*c937d2bfSEd Tanous                     fillHostLoggerEntryJson(
1949*c937d2bfSEd Tanous                         std::to_string(delegatedQuery.skip + i), logEntries[i],
1950*c937d2bfSEd Tanous                         hostLogEntry);
1951b7028ebfSSpencer Ku                 }
1952b7028ebfSSpencer Ku 
1953b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1954*c937d2bfSEd Tanous                 if (delegatedQuery.skip + delegatedQuery.top < logCount)
1955b7028ebfSSpencer Ku                 {
1956b7028ebfSSpencer Ku                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
19570fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1958*c937d2bfSEd Tanous                         std::to_string(delegatedQuery.skip +
1959*c937d2bfSEd Tanous                                        delegatedQuery.top);
1960b7028ebfSSpencer Ku                 }
1961b7028ebfSSpencer Ku             }
1962b7028ebfSSpencer Ku         });
1963b7028ebfSSpencer Ku }
1964b7028ebfSSpencer Ku 
1965b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1966b7028ebfSSpencer Ku {
1967b7028ebfSSpencer Ku     BMCWEB_ROUTE(
1968b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1969b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1970b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
197145ca1b86SEd Tanous             [&app](const crow::Request& req,
1972b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1973b7028ebfSSpencer Ku                    const std::string& param) {
197445ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
197545ca1b86SEd Tanous                 {
197645ca1b86SEd Tanous                     return;
197745ca1b86SEd Tanous                 }
1978b7028ebfSSpencer Ku                 const std::string& targetID = param;
1979b7028ebfSSpencer Ku 
1980b7028ebfSSpencer Ku                 uint64_t idInt = 0;
1981ca45aa3cSEd Tanous 
1982ca45aa3cSEd Tanous                 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1983ca45aa3cSEd Tanous                 const char* end = targetID.data() + targetID.size();
1984ca45aa3cSEd Tanous 
1985ca45aa3cSEd Tanous                 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1986b7028ebfSSpencer Ku                 if (ec == std::errc::invalid_argument)
1987b7028ebfSSpencer Ku                 {
1988ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1989b7028ebfSSpencer Ku                     return;
1990b7028ebfSSpencer Ku                 }
1991b7028ebfSSpencer Ku                 if (ec == std::errc::result_out_of_range)
1992b7028ebfSSpencer Ku                 {
1993ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1994b7028ebfSSpencer Ku                     return;
1995b7028ebfSSpencer Ku                 }
1996b7028ebfSSpencer Ku 
1997b7028ebfSSpencer Ku                 std::vector<std::filesystem::path> hostLoggerFiles;
1998b7028ebfSSpencer Ku                 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1999b7028ebfSSpencer Ku                 {
2000b7028ebfSSpencer Ku                     BMCWEB_LOG_ERROR << "fail to get host log file path";
2001b7028ebfSSpencer Ku                     return;
2002b7028ebfSSpencer Ku                 }
2003b7028ebfSSpencer Ku 
2004b7028ebfSSpencer Ku                 size_t logCount = 0;
2005b7028ebfSSpencer Ku                 uint64_t top = 1;
2006b7028ebfSSpencer Ku                 std::vector<std::string> logEntries;
2007b7028ebfSSpencer Ku                 // We can get specific entry by skip and top. For example, if we
2008b7028ebfSSpencer Ku                 // want to get nth entry, we can set skip = n-1 and top = 1 to
2009b7028ebfSSpencer Ku                 // get that entry
2010b7028ebfSSpencer Ku                 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2011b7028ebfSSpencer Ku                                           logEntries, logCount))
2012b7028ebfSSpencer Ku                 {
2013b7028ebfSSpencer Ku                     messages::internalError(asyncResp->res);
2014b7028ebfSSpencer Ku                     return;
2015b7028ebfSSpencer Ku                 }
2016b7028ebfSSpencer Ku 
2017b7028ebfSSpencer Ku                 if (!logEntries.empty())
2018b7028ebfSSpencer Ku                 {
2019b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(targetID, logEntries[0],
2020b7028ebfSSpencer Ku                                             asyncResp->res.jsonValue);
2021b7028ebfSSpencer Ku                     return;
2022b7028ebfSSpencer Ku                 }
2023b7028ebfSSpencer Ku 
2024b7028ebfSSpencer Ku                 // Requested ID was not found
2025ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2026b7028ebfSSpencer Ku             });
2027b7028ebfSSpencer Ku }
2028b7028ebfSSpencer Ku 
20297e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
20301da66f75SEd Tanous {
20317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2032ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
20337e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
203445ca1b86SEd Tanous             [&app](const crow::Request& req,
20357e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
203645ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
203745ca1b86SEd Tanous                 {
203845ca1b86SEd Tanous                     return;
203945ca1b86SEd Tanous                 }
20407e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
20417e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
2042e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
20431da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
2044e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
2045e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
20467e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
20477e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
2048e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
20491da66f75SEd Tanous                     "Collection of LogServices for this Manager";
20507e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
20517e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2052c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
20535cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
20545cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
20557e860f15SJohn Edward Broadbent                     {{"@odata.id",
20567e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
20575cb1dd27SAsmitha Karunanithi #endif
2058c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2059c4bf6374SJason M. Bills                 logServiceArray.push_back(
20607e860f15SJohn Edward Broadbent                     {{"@odata.id",
20617e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2062c4bf6374SJason M. Bills #endif
2063e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2064c4bf6374SJason M. Bills                     logServiceArray.size();
20657e860f15SJohn Edward Broadbent             });
2066e1f26343SJason M. Bills }
2067e1f26343SJason M. Bills 
20687e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2069e1f26343SJason M. Bills {
20707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2071ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
20727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
207345ca1b86SEd Tanous             [&app](const crow::Request& req,
207445ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
207545ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
20767e860f15SJohn Edward Broadbent                 {
207745ca1b86SEd Tanous                     return;
207845ca1b86SEd Tanous                 }
2079e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
2080e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
20810f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
20820f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
20837e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
20847e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
20857e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
20867e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
2087c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2088e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
20897c8c4058STejas Patil 
20907c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
20917c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
20927c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
20937c8c4058STejas Patil                     redfishDateTimeOffset.first;
20947c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20957c8c4058STejas Patil                     redfishDateTimeOffset.second;
20967c8c4058STejas Patil 
2097cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
2098cd50aa42SJason M. Bills                     {"@odata.id",
2099086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
21007e860f15SJohn Edward Broadbent             });
2101e1f26343SJason M. Bills }
2102e1f26343SJason M. Bills 
2103c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2104e1f26343SJason M. Bills                                       sd_journal* journal,
2105c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
2106e1f26343SJason M. Bills {
2107e1f26343SJason M. Bills     // Get the Log Entry contents
2108e1f26343SJason M. Bills     int ret = 0;
2109e1f26343SJason M. Bills 
2110a8fe54f0SJason M. Bills     std::string message;
2111a8fe54f0SJason M. Bills     std::string_view syslogID;
2112a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2113a8fe54f0SJason M. Bills     if (ret < 0)
2114a8fe54f0SJason M. Bills     {
2115a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2116a8fe54f0SJason M. Bills                          << strerror(-ret);
2117a8fe54f0SJason M. Bills     }
2118a8fe54f0SJason M. Bills     if (!syslogID.empty())
2119a8fe54f0SJason M. Bills     {
2120a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2121a8fe54f0SJason M. Bills     }
2122a8fe54f0SJason M. Bills 
212339e77504SEd Tanous     std::string_view msg;
212416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2125e1f26343SJason M. Bills     if (ret < 0)
2126e1f26343SJason M. Bills     {
2127e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2128e1f26343SJason M. Bills         return 1;
2129e1f26343SJason M. Bills     }
2130a8fe54f0SJason M. Bills     message += std::string(msg);
2131e1f26343SJason M. Bills 
2132e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2133271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
213416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2135e1f26343SJason M. Bills     if (ret < 0)
2136e1f26343SJason M. Bills     {
2137e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2138e1f26343SJason M. Bills     }
2139e1f26343SJason M. Bills 
2140e1f26343SJason M. Bills     // Get the Created time from the timestamp
214116428a1aSJason M. Bills     std::string entryTimeStr;
214216428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2143e1f26343SJason M. Bills     {
214416428a1aSJason M. Bills         return 1;
2145e1f26343SJason M. Bills     }
2146e1f26343SJason M. Bills 
2147e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2148c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2149647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2150c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2151c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2152e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2153c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2154a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2155e1f26343SJason M. Bills         {"EntryType", "Oem"},
2156738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2157738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2158738c1e61SPatrick Williams                                      : "OK"},
2159086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2160e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2161e1f26343SJason M. Bills     return 0;
2162e1f26343SJason M. Bills }
2163e1f26343SJason M. Bills 
21647e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2165e1f26343SJason M. Bills {
21667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2167ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
216845ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
216945ca1b86SEd Tanous                                                        const std::shared_ptr<
217045ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
217145ca1b86SEd Tanous                                                            asyncResp) {
2172*c937d2bfSEd Tanous             query_param::QueryCapabilities capabilities = {
2173*c937d2bfSEd Tanous                 .canDelegateTop = true,
2174*c937d2bfSEd Tanous                 .canDelegateSkip = true,
2175*c937d2bfSEd Tanous             };
2176*c937d2bfSEd Tanous             query_param::Query delegatedQuery;
2177*c937d2bfSEd Tanous             if (!redfish::setUpRedfishRouteWithDelegation(
2178*c937d2bfSEd Tanous                     app, req, asyncResp->res, delegatedQuery, capabilities))
2179193ad2faSJason M. Bills             {
2180193ad2faSJason M. Bills                 return;
2181193ad2faSJason M. Bills             }
21827e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
21837e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2184e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
2185e1f26343SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
21860f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
21870f74e643SEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2188e1f26343SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2189e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2190e1f26343SJason M. Bills                 "Collection of BMC Journal Entries";
21910fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2192e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2193e1f26343SJason M. Bills 
21947e860f15SJohn Edward Broadbent             // Go through the journal and use the timestamp to create a
21957e860f15SJohn Edward Broadbent             // unique ID for each entry
2196e1f26343SJason M. Bills             sd_journal* journalTmp = nullptr;
2197e1f26343SJason M. Bills             int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2198e1f26343SJason M. Bills             if (ret < 0)
2199e1f26343SJason M. Bills             {
22007e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "failed to open journal: "
22017e860f15SJohn Edward Broadbent                                  << strerror(-ret);
2202f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2203e1f26343SJason M. Bills                 return;
2204e1f26343SJason M. Bills             }
22050fda0f12SGeorge Liu             std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22060fda0f12SGeorge Liu                 journalTmp, sd_journal_close);
2207e1f26343SJason M. Bills             journalTmp = nullptr;
2208b01bf299SEd Tanous             uint64_t entryCount = 0;
2209e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
2210e85d6b16SJason M. Bills             bool firstEntry = true;
2211e1f26343SJason M. Bills             SD_JOURNAL_FOREACH(journal.get())
2212e1f26343SJason M. Bills             {
2213193ad2faSJason M. Bills                 entryCount++;
22147e860f15SJohn Edward Broadbent                 // Handle paging using skip (number of entries to skip from
22157e860f15SJohn Edward Broadbent                 // the start) and top (number of entries to display)
2216*c937d2bfSEd Tanous                 if (entryCount <= delegatedQuery.skip ||
2217*c937d2bfSEd Tanous                     entryCount > delegatedQuery.skip + delegatedQuery.top)
2218193ad2faSJason M. Bills                 {
2219193ad2faSJason M. Bills                     continue;
2220193ad2faSJason M. Bills                 }
2221193ad2faSJason M. Bills 
222216428a1aSJason M. Bills                 std::string idStr;
2223e85d6b16SJason M. Bills                 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2224e1f26343SJason M. Bills                 {
2225e1f26343SJason M. Bills                     continue;
2226e1f26343SJason M. Bills                 }
2227e1f26343SJason M. Bills 
2228e85d6b16SJason M. Bills                 if (firstEntry)
2229e85d6b16SJason M. Bills                 {
2230e85d6b16SJason M. Bills                     firstEntry = false;
2231e85d6b16SJason M. Bills                 }
2232e85d6b16SJason M. Bills 
2233e1f26343SJason M. Bills                 logEntryArray.push_back({});
2234c4bf6374SJason M. Bills                 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2235c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2236c4bf6374SJason M. Bills                                                bmcJournalLogEntry) != 0)
2237e1f26343SJason M. Bills                 {
2238f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2239e1f26343SJason M. Bills                     return;
2240e1f26343SJason M. Bills                 }
2241e1f26343SJason M. Bills             }
2242193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2243*c937d2bfSEd Tanous             if (delegatedQuery.skip + delegatedQuery.top < entryCount)
2244193ad2faSJason M. Bills             {
2245193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
22460fda0f12SGeorge Liu                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2247*c937d2bfSEd Tanous                     std::to_string(delegatedQuery.skip + delegatedQuery.top);
2248193ad2faSJason M. Bills             }
22497e860f15SJohn Edward Broadbent         });
2250e1f26343SJason M. Bills }
2251e1f26343SJason M. Bills 
22527e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2253e1f26343SJason M. Bills {
22547e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
22557e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2256ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
22577e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
225845ca1b86SEd Tanous             [&app](const crow::Request& req,
22597e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22607e860f15SJohn Edward Broadbent                    const std::string& entryID) {
226145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
226245ca1b86SEd Tanous                 {
226345ca1b86SEd Tanous                     return;
226445ca1b86SEd Tanous                 }
2265e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2266e1f26343SJason M. Bills                 uint64_t ts = 0;
2267271584abSEd Tanous                 uint64_t index = 0;
22688d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2269e1f26343SJason M. Bills                 {
227016428a1aSJason M. Bills                     return;
2271e1f26343SJason M. Bills                 }
2272e1f26343SJason M. Bills 
2273e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2274e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2275e1f26343SJason M. Bills                 if (ret < 0)
2276e1f26343SJason M. Bills                 {
22777e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
22787e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2279f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2280e1f26343SJason M. Bills                     return;
2281e1f26343SJason M. Bills                 }
22827e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
22837e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2284e1f26343SJason M. Bills                 journalTmp = nullptr;
22857e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
22867e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2287af07e3f5SJason M. Bills                 std::string idStr;
2288af07e3f5SJason M. Bills                 bool firstEntry = true;
2289e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
22902056b6d1SManojkiran Eda                 if (ret < 0)
22912056b6d1SManojkiran Eda                 {
22922056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
22932056b6d1SManojkiran Eda                                      << strerror(-ret);
22942056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
22952056b6d1SManojkiran Eda                     return;
22962056b6d1SManojkiran Eda                 }
2297271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2298e1f26343SJason M. Bills                 {
2299e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2300af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2301af07e3f5SJason M. Bills                     {
2302af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2303af07e3f5SJason M. Bills                         return;
2304af07e3f5SJason M. Bills                     }
2305af07e3f5SJason M. Bills                     if (firstEntry)
2306af07e3f5SJason M. Bills                     {
2307af07e3f5SJason M. Bills                         firstEntry = false;
2308af07e3f5SJason M. Bills                     }
2309e1f26343SJason M. Bills                 }
2310c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2311af07e3f5SJason M. Bills                 if (idStr != entryID)
2312c4bf6374SJason M. Bills                 {
2313ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2314c4bf6374SJason M. Bills                     return;
2315c4bf6374SJason M. Bills                 }
2316c4bf6374SJason M. Bills 
2317c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2318e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2319e1f26343SJason M. Bills                 {
2320f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2321e1f26343SJason M. Bills                     return;
2322e1f26343SJason M. Bills                 }
23237e860f15SJohn Edward Broadbent             });
2324c9bb6861Sraviteja-b }
2325c9bb6861Sraviteja-b 
23267e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2327c9bb6861Sraviteja-b {
23287e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2329ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
233045ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
233145ca1b86SEd Tanous                                                        const std::shared_ptr<
233245ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
233345ca1b86SEd Tanous                                                            asyncResp) {
233445ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
233545ca1b86SEd Tanous             {
233645ca1b86SEd Tanous                 return;
233745ca1b86SEd Tanous             }
2338c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.id"] =
23395cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Managers/bmc/LogServices/Dump";
2340c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.type"] =
2341d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
2342c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Name"] = "Dump LogService";
23435cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
23445cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
2345c9bb6861Sraviteja-b             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23467c8c4058STejas Patil 
23477c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
23487c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
23490fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23507c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23517c8c4058STejas Patil                 redfishDateTimeOffset.second;
23527c8c4058STejas Patil 
2353c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Entries"] = {
23547e860f15SJohn Edward Broadbent                 {"@odata.id",
23557e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
23565cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
23575cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
23580fda0f12SGeorge Liu                  {{"target",
23590fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2360d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
23610fda0f12SGeorge Liu                  {{"target",
23620fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
23637e860f15SJohn Edward Broadbent         });
2364c9bb6861Sraviteja-b }
2365c9bb6861Sraviteja-b 
23667e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
23677e860f15SJohn Edward Broadbent {
23687e860f15SJohn Edward Broadbent 
2369c9bb6861Sraviteja-b     /**
2370c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2371c9bb6861Sraviteja-b      */
23727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2373ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
23747e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
237545ca1b86SEd Tanous             [&app](const crow::Request& req,
23767e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
237745ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
237845ca1b86SEd Tanous                 {
237945ca1b86SEd Tanous                     return;
238045ca1b86SEd Tanous                 }
2381c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2382c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2383c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
23845cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
23855cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2386c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
23875cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2388c9bb6861Sraviteja-b 
23895cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
23907e860f15SJohn Edward Broadbent             });
2391c9bb6861Sraviteja-b }
2392c9bb6861Sraviteja-b 
23937e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2394c9bb6861Sraviteja-b {
23957e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23967e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2397ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
23987e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
239945ca1b86SEd Tanous             [&app](const crow::Request& req,
24007e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24017e860f15SJohn Edward Broadbent                    const std::string& param) {
240245ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
240345ca1b86SEd Tanous                 {
240445ca1b86SEd Tanous                     return;
240545ca1b86SEd Tanous                 }
240645ca1b86SEd Tanous 
240745ca1b86SEd Tanous                 getDumpEntryById(app, req, asyncResp, param, "BMC");
24087e860f15SJohn Edward Broadbent             });
24097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24107e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2411ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
24127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
241345ca1b86SEd Tanous             [&app](const crow::Request& req,
24147e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24157e860f15SJohn Edward Broadbent                    const std::string& param) {
241645ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
241745ca1b86SEd Tanous                 {
241845ca1b86SEd Tanous                     return;
241945ca1b86SEd Tanous                 }
24207e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
24217e860f15SJohn Edward Broadbent             });
2422c9bb6861Sraviteja-b }
2423c9bb6861Sraviteja-b 
24247e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2425c9bb6861Sraviteja-b {
24260fda0f12SGeorge Liu     BMCWEB_ROUTE(
24270fda0f12SGeorge Liu         app,
24280fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2429ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24307e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
243145ca1b86SEd Tanous             [&app](const crow::Request& req,
24327e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
243345ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
243445ca1b86SEd Tanous                 {
243545ca1b86SEd Tanous                     return;
243645ca1b86SEd Tanous                 }
24378d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
24387e860f15SJohn Edward Broadbent             });
2439a43be80fSAsmitha Karunanithi }
2440a43be80fSAsmitha Karunanithi 
24417e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
244280319af1SAsmitha Karunanithi {
24430fda0f12SGeorge Liu     BMCWEB_ROUTE(
24440fda0f12SGeorge Liu         app,
24450fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2446ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
244845ca1b86SEd Tanous             [&app](const crow::Request& req,
24497e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
245045ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
245145ca1b86SEd Tanous                 {
245245ca1b86SEd Tanous                     return;
245345ca1b86SEd Tanous                 }
24548d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
24557e860f15SJohn Edward Broadbent             });
24565cb1dd27SAsmitha Karunanithi }
24575cb1dd27SAsmitha Karunanithi 
24587e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
24595cb1dd27SAsmitha Karunanithi {
24607e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2461ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
246245ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
246345ca1b86SEd Tanous                                                        const std::shared_ptr<
246445ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
246545ca1b86SEd Tanous                                                            asyncResp) {
246645ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
24677e860f15SJohn Edward Broadbent             {
246845ca1b86SEd Tanous                 return;
246945ca1b86SEd Tanous             }
24705cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.id"] =
24715cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Systems/system/LogServices/Dump";
24725cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
2473d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
24745cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = "Dump LogService";
247545ca1b86SEd Tanous             asyncResp->res.jsonValue["Description"] = "System Dump LogService";
24765cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
24775cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24787c8c4058STejas Patil 
24797c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
24807c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
248145ca1b86SEd Tanous             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
24827c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24837c8c4058STejas Patil                 redfishDateTimeOffset.second;
24847c8c4058STejas Patil 
24855cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Entries"] = {
24865cb1dd27SAsmitha Karunanithi                 {"@odata.id",
24875cb1dd27SAsmitha Karunanithi                  "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
24885cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
24895cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
24907e860f15SJohn Edward Broadbent                  {{"target",
24910fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
2492d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
24937e860f15SJohn Edward Broadbent                  {{"target",
24940fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
24957e860f15SJohn Edward Broadbent         });
24965cb1dd27SAsmitha Karunanithi }
24975cb1dd27SAsmitha Karunanithi 
24987e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
24997e860f15SJohn Edward Broadbent {
25007e860f15SJohn Edward Broadbent 
25015cb1dd27SAsmitha Karunanithi     /**
25025cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25035cb1dd27SAsmitha Karunanithi      */
2504b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2505ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25067e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
250745ca1b86SEd Tanous             [&app](const crow::Request& req,
2508864d6a17SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
250945ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
251045ca1b86SEd Tanous                 {
251145ca1b86SEd Tanous                     return;
251245ca1b86SEd Tanous                 }
25135cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
25145cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
25155cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
25165cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
25175cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
25185cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
25195cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
25205cb1dd27SAsmitha Karunanithi 
25215cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
25227e860f15SJohn Edward Broadbent             });
25235cb1dd27SAsmitha Karunanithi }
25245cb1dd27SAsmitha Karunanithi 
25257e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
25265cb1dd27SAsmitha Karunanithi {
25277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2528864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2529ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2530ed398213SEd Tanous 
25317e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
253245ca1b86SEd Tanous             [&app](const crow::Request& req,
25337e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25347e860f15SJohn Edward Broadbent                    const std::string& param) {
253545ca1b86SEd Tanous                 getDumpEntryById(app, req, asyncResp, param, "System");
25367e860f15SJohn Edward Broadbent             });
25378d1b46d7Szhanghch05 
25387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2539864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2540ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
254245ca1b86SEd Tanous             [&app](const crow::Request& req,
25437e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25447e860f15SJohn Edward Broadbent                    const std::string& param) {
254545ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
254645ca1b86SEd Tanous                 {
254745ca1b86SEd Tanous                     return;
254845ca1b86SEd Tanous                 }
25497e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
25507e860f15SJohn Edward Broadbent             });
25515cb1dd27SAsmitha Karunanithi }
2552c9bb6861Sraviteja-b 
25537e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2554c9bb6861Sraviteja-b {
25550fda0f12SGeorge Liu     BMCWEB_ROUTE(
25560fda0f12SGeorge Liu         app,
25570fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2558ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
256045ca1b86SEd Tanous             [&app](const crow::Request& req,
256145ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
256245ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
256345ca1b86SEd Tanous                 {
256445ca1b86SEd Tanous                     return;
256545ca1b86SEd Tanous                 }
256645ca1b86SEd Tanous                 createDump(asyncResp, req, "System");
256745ca1b86SEd Tanous             });
2568a43be80fSAsmitha Karunanithi }
2569a43be80fSAsmitha Karunanithi 
25707e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2571a43be80fSAsmitha Karunanithi {
25720fda0f12SGeorge Liu     BMCWEB_ROUTE(
25730fda0f12SGeorge Liu         app,
25740fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2575ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
257745ca1b86SEd Tanous             [&app](const crow::Request& req,
25787e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25797e860f15SJohn Edward Broadbent 
258045ca1b86SEd Tanous             {
258145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
258245ca1b86SEd Tanous                 {
258345ca1b86SEd Tanous                     return;
258445ca1b86SEd Tanous                 }
258545ca1b86SEd Tanous                 clearDump(asyncResp, "System");
258645ca1b86SEd Tanous             });
2587013487e5Sraviteja-b }
2588013487e5Sraviteja-b 
25897e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
25901da66f75SEd Tanous {
25913946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25923946028dSAppaRao Puli     // method for security reasons.
25931da66f75SEd Tanous     /**
25941da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
25951da66f75SEd Tanous      */
25967e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2597ed398213SEd Tanous         // This is incorrect, should be:
2598ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2599432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
260045ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
260145ca1b86SEd Tanous                                                        const std::shared_ptr<
260245ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
260345ca1b86SEd Tanous                                                            asyncResp) {
260445ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
260545ca1b86SEd Tanous             {
260645ca1b86SEd Tanous                 return;
260745ca1b86SEd Tanous             }
26087e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
26097e860f15SJohn Edward Broadbent             // SubRoute
26100f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2611424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2612e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
26138e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
26144f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26154f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26164f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2617e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2618e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
26197c8c4058STejas Patil 
26207c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
26217c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
26227c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26237c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26247c8c4058STejas Patil                 redfishDateTimeOffset.second;
26257c8c4058STejas Patil 
2626cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2627cd50aa42SJason M. Bills                 {"@odata.id",
2628424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2629e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
26305b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
26310fda0f12SGeorge Liu                  {{"target",
26320fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
26338e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
26340fda0f12SGeorge Liu                  {{"target",
26350fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
26367e860f15SJohn Edward Broadbent         });
26371da66f75SEd Tanous }
26381da66f75SEd Tanous 
26397e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
26405b61b5e8SJason M. Bills {
26410fda0f12SGeorge Liu     BMCWEB_ROUTE(
26420fda0f12SGeorge Liu         app,
26430fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2644ed398213SEd Tanous         // This is incorrect, should be:
2645ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2646432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
264845ca1b86SEd Tanous             [&app](const crow::Request& req,
26497e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
265045ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
265145ca1b86SEd Tanous                 {
265245ca1b86SEd Tanous                     return;
265345ca1b86SEd Tanous                 }
26545b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
26555b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2656cb13a392SEd Tanous                                 const std::string&) {
26575b61b5e8SJason M. Bills                         if (ec)
26585b61b5e8SJason M. Bills                         {
26595b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
26605b61b5e8SJason M. Bills                             return;
26615b61b5e8SJason M. Bills                         }
26625b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
26635b61b5e8SJason M. Bills                     },
26647e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
26657e860f15SJohn Edward Broadbent                     "DeleteAll");
26667e860f15SJohn Edward Broadbent             });
26675b61b5e8SJason M. Bills }
26685b61b5e8SJason M. Bills 
26698d1b46d7Szhanghch05 static void
26708d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26718d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2672e855dd28SJason M. Bills {
2673043a0536SJohnathan Mantey     auto getStoredLogCallback =
2674b9d36b47SEd Tanous         [asyncResp, logID,
2675b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2676b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2677e855dd28SJason M. Bills             if (ec)
2678e855dd28SJason M. Bills             {
2679e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
26801ddcf01aSJason M. Bills                 if (ec.value() ==
26811ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
26821ddcf01aSJason M. Bills                 {
2683043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2684043a0536SJohnathan Mantey                                                logID);
26851ddcf01aSJason M. Bills                 }
26861ddcf01aSJason M. Bills                 else
26871ddcf01aSJason M. Bills                 {
2688e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
26891ddcf01aSJason M. Bills                 }
2690e855dd28SJason M. Bills                 return;
2691e855dd28SJason M. Bills             }
2692043a0536SJohnathan Mantey 
2693043a0536SJohnathan Mantey             std::string timestamp{};
2694043a0536SJohnathan Mantey             std::string filename{};
2695043a0536SJohnathan Mantey             std::string logfile{};
26962c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2697043a0536SJohnathan Mantey 
2698043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2699e855dd28SJason M. Bills             {
2700ace85d60SEd Tanous                 messages::resourceMissingAtURI(
2701ace85d60SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(logID));
2702e855dd28SJason M. Bills                 return;
2703e855dd28SJason M. Bills             }
2704e855dd28SJason M. Bills 
2705043a0536SJohnathan Mantey             std::string crashdumpURI =
2706e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2707043a0536SJohnathan Mantey                 logID + "/" + filename;
27082b20ef6eSJason M. Bills             nlohmann::json logEntry = {
27094978b63fSJason M. Bills                 {"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
27104978b63fSJason M. Bills                 {"@odata.id",
27114978b63fSJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2712e855dd28SJason M. Bills                      logID},
2713e855dd28SJason M. Bills                 {"Name", "CPU Crashdump"},
2714e855dd28SJason M. Bills                 {"Id", logID},
2715e855dd28SJason M. Bills                 {"EntryType", "Oem"},
27168e6c099aSJason M. Bills                 {"AdditionalDataURI", std::move(crashdumpURI)},
27178e6c099aSJason M. Bills                 {"DiagnosticDataType", "OEM"},
27188e6c099aSJason M. Bills                 {"OEMDiagnosticDataType", "PECICrashdump"},
2719043a0536SJohnathan Mantey                 {"Created", std::move(timestamp)}};
27202b20ef6eSJason M. Bills 
27212b20ef6eSJason M. Bills             // If logEntryJson references an array of LogEntry resources
27222b20ef6eSJason M. Bills             // ('Members' list), then push this as a new entry, otherwise set it
27232b20ef6eSJason M. Bills             // directly
27242b20ef6eSJason M. Bills             if (logEntryJson.is_array())
27252b20ef6eSJason M. Bills             {
27262b20ef6eSJason M. Bills                 logEntryJson.push_back(logEntry);
27272b20ef6eSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
27282b20ef6eSJason M. Bills                     logEntryJson.size();
27292b20ef6eSJason M. Bills             }
27302b20ef6eSJason M. Bills             else
27312b20ef6eSJason M. Bills             {
27322b20ef6eSJason M. Bills                 logEntryJson = logEntry;
27332b20ef6eSJason M. Bills             }
2734e855dd28SJason M. Bills         };
2735e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
27365b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
27375b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2738043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2739e855dd28SJason M. Bills }
2740e855dd28SJason M. Bills 
27417e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
27421da66f75SEd Tanous {
27433946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27443946028dSAppaRao Puli     // method for security reasons.
27451da66f75SEd Tanous     /**
27461da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
27471da66f75SEd Tanous      */
27487e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
27497e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2750ed398213SEd Tanous         // This is incorrect, should be.
2751ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2752432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
275345ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
275445ca1b86SEd Tanous                                                        const std::shared_ptr<
275545ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
275645ca1b86SEd Tanous                                                            asyncResp) {
275745ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
275845ca1b86SEd Tanous             {
275945ca1b86SEd Tanous                 return;
276045ca1b86SEd Tanous             }
27612b20ef6eSJason M. Bills             crow::connections::systemBus->async_method_call(
27622b20ef6eSJason M. Bills                 [asyncResp](const boost::system::error_code ec,
27632b20ef6eSJason M. Bills                             const std::vector<std::string>& resp) {
27641da66f75SEd Tanous                     if (ec)
27651da66f75SEd Tanous                     {
27661da66f75SEd Tanous                         if (ec.value() !=
27671da66f75SEd Tanous                             boost::system::errc::no_such_file_or_directory)
27681da66f75SEd Tanous                         {
27691da66f75SEd Tanous                             BMCWEB_LOG_DEBUG << "failed to get entries ec: "
27701da66f75SEd Tanous                                              << ec.message();
2771f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
27721da66f75SEd Tanous                             return;
27731da66f75SEd Tanous                         }
27741da66f75SEd Tanous                     }
2775e1f26343SJason M. Bills                     asyncResp->res.jsonValue["@odata.type"] =
27761da66f75SEd Tanous                         "#LogEntryCollection.LogEntryCollection";
27770f74e643SEd Tanous                     asyncResp->res.jsonValue["@odata.id"] =
2778424c4176SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
27792b20ef6eSJason M. Bills                     asyncResp->res.jsonValue["Name"] =
27802b20ef6eSJason M. Bills                         "Open BMC Crashdump Entries";
2781e1f26343SJason M. Bills                     asyncResp->res.jsonValue["Description"] =
2782424c4176SJason M. Bills                         "Collection of Crashdump Entries";
27832b20ef6eSJason M. Bills                     asyncResp->res.jsonValue["Members"] =
27842b20ef6eSJason M. Bills                         nlohmann::json::array();
2785a2dd60a6SBrandon Kim                     asyncResp->res.jsonValue["Members@odata.count"] = 0;
27862b20ef6eSJason M. Bills 
27872b20ef6eSJason M. Bills                     for (const std::string& path : resp)
27881da66f75SEd Tanous                     {
27892b20ef6eSJason M. Bills                         const sdbusplus::message::object_path objPath(path);
2790e855dd28SJason M. Bills                         // Get the log ID
27912b20ef6eSJason M. Bills                         std::string logID = objPath.filename();
27922b20ef6eSJason M. Bills                         if (logID.empty())
27931da66f75SEd Tanous                         {
2794e855dd28SJason M. Bills                             continue;
27951da66f75SEd Tanous                         }
2796e855dd28SJason M. Bills                         // Add the log entry to the array
27972b20ef6eSJason M. Bills                         logCrashdumpEntry(asyncResp, logID,
27982b20ef6eSJason M. Bills                                           asyncResp->res.jsonValue["Members"]);
27991da66f75SEd Tanous                     }
28002b20ef6eSJason M. Bills                 },
28011da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
28021da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
28031da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
28045b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
28057e860f15SJohn Edward Broadbent         });
28061da66f75SEd Tanous }
28071da66f75SEd Tanous 
28087e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28091da66f75SEd Tanous {
28103946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28113946028dSAppaRao Puli     // method for security reasons.
28121da66f75SEd Tanous 
28137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28147e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2815ed398213SEd Tanous         // this is incorrect, should be
2816ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2817432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
281945ca1b86SEd Tanous             [&app](const crow::Request& req,
28207e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28217e860f15SJohn Edward Broadbent                    const std::string& param) {
282245ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
282345ca1b86SEd Tanous                 {
282445ca1b86SEd Tanous                     return;
282545ca1b86SEd Tanous                 }
28267e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2827e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
28287e860f15SJohn Edward Broadbent             });
2829e855dd28SJason M. Bills }
2830e855dd28SJason M. Bills 
28317e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2832e855dd28SJason M. Bills {
28333946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28343946028dSAppaRao Puli     // method for security reasons.
28357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28367e860f15SJohn Edward Broadbent         app,
28377e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2838ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
28397e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
284045ca1b86SEd Tanous             [&app](const crow::Request& req,
28417e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28427e860f15SJohn Edward Broadbent                    const std::string& logID, const std::string& fileName) {
284345ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
284445ca1b86SEd Tanous                 {
284545ca1b86SEd Tanous                     return;
284645ca1b86SEd Tanous                 }
2847043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2848ace85d60SEd Tanous                     [asyncResp, logID, fileName,
2849ace85d60SEd Tanous                      url(boost::urls::url(req.urlView))](
2850abf2add6SEd Tanous                         const boost::system::error_code ec,
2851168e20c1SEd Tanous                         const std::vector<std::pair<
2852168e20c1SEd Tanous                             std::string, dbus::utility::DbusVariantType>>&
28537e860f15SJohn Edward Broadbent                             resp) {
28541da66f75SEd Tanous                         if (ec)
28551da66f75SEd Tanous                         {
2856043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2857043a0536SJohnathan Mantey                                              << ec.message();
2858f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
28591da66f75SEd Tanous                             return;
28601da66f75SEd Tanous                         }
2861e855dd28SJason M. Bills 
2862043a0536SJohnathan Mantey                         std::string dbusFilename{};
2863043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2864043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2865043a0536SJohnathan Mantey 
28667e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
28677e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2868043a0536SJohnathan Mantey 
2869043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2870043a0536SJohnathan Mantey                             dbusFilepath.empty())
28711da66f75SEd Tanous                         {
2872ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
28731da66f75SEd Tanous                             return;
28741da66f75SEd Tanous                         }
2875e855dd28SJason M. Bills 
2876043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2877043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2878043a0536SJohnathan Mantey                         {
2879ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
2880043a0536SJohnathan Mantey                             return;
2881043a0536SJohnathan Mantey                         }
2882043a0536SJohnathan Mantey 
2883043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2884043a0536SJohnathan Mantey                         {
2885ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
2886043a0536SJohnathan Mantey                             return;
2887043a0536SJohnathan Mantey                         }
28882d31491bSJason M. Bills                         std::ifstream ifs(dbusFilepath,
28892d31491bSJason M. Bills                                           std::ios::in | std::ios::binary);
28902d31491bSJason M. Bills                         asyncResp->res.body() = std::string(
28912d31491bSJason M. Bills                             std::istreambuf_iterator<char>{ifs}, {});
2892043a0536SJohnathan Mantey 
28937e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
28947e860f15SJohn Edward Broadbent                         // from a browser
28957e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
28967e860f15SJohn Edward Broadbent                                                  "attachment");
28971da66f75SEd Tanous                     };
28981da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
28995b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
29005b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
29017e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
29027e860f15SJohn Edward Broadbent                     crashdumpInterface);
29037e860f15SJohn Edward Broadbent             });
29041da66f75SEd Tanous }
29051da66f75SEd Tanous 
2906c5a4c82aSJason M. Bills enum class OEMDiagnosticType
2907c5a4c82aSJason M. Bills {
2908c5a4c82aSJason M. Bills     onDemand,
2909c5a4c82aSJason M. Bills     telemetry,
2910c5a4c82aSJason M. Bills     invalid,
2911c5a4c82aSJason M. Bills };
2912c5a4c82aSJason M. Bills 
2913f7725d79SEd Tanous inline OEMDiagnosticType
2914f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
2915c5a4c82aSJason M. Bills {
2916c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
2917c5a4c82aSJason M. Bills     {
2918c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
2919c5a4c82aSJason M. Bills     }
2920c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
2921c5a4c82aSJason M. Bills     {
2922c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
2923c5a4c82aSJason M. Bills     }
2924c5a4c82aSJason M. Bills 
2925c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
2926c5a4c82aSJason M. Bills }
2927c5a4c82aSJason M. Bills 
29287e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
29291da66f75SEd Tanous {
29303946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29313946028dSAppaRao Puli     // method for security reasons.
29320fda0f12SGeorge Liu     BMCWEB_ROUTE(
29330fda0f12SGeorge Liu         app,
29340fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2935ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2936ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2937432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
29387e860f15SJohn Edward Broadbent         .methods(
29397e860f15SJohn Edward Broadbent             boost::beast::http::verb::
294045ca1b86SEd Tanous                 post)([&app](
294145ca1b86SEd Tanous                           const crow::Request& req,
29427e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
294345ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
294445ca1b86SEd Tanous             {
294545ca1b86SEd Tanous                 return;
294645ca1b86SEd Tanous             }
29478e6c099aSJason M. Bills             std::string diagnosticDataType;
29488e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
294915ed6780SWilly Tu             if (!redfish::json_util::readJsonAction(
29507e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
29517e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
29527e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
29538e6c099aSJason M. Bills             {
29548e6c099aSJason M. Bills                 return;
29558e6c099aSJason M. Bills             }
29568e6c099aSJason M. Bills 
29578e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
29588e6c099aSJason M. Bills             {
29598e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
29608e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
29618e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
29628e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29638e6c099aSJason M. Bills                     "CollectDiagnosticData");
29648e6c099aSJason M. Bills                 return;
29658e6c099aSJason M. Bills             }
29668e6c099aSJason M. Bills 
2967c5a4c82aSJason M. Bills             OEMDiagnosticType oemDiagType =
2968c5a4c82aSJason M. Bills                 getOEMDiagnosticType(oemDiagnosticDataType);
2969c5a4c82aSJason M. Bills 
2970c5a4c82aSJason M. Bills             std::string iface;
2971c5a4c82aSJason M. Bills             std::string method;
2972c5a4c82aSJason M. Bills             std::string taskMatchStr;
2973c5a4c82aSJason M. Bills             if (oemDiagType == OEMDiagnosticType::onDemand)
2974c5a4c82aSJason M. Bills             {
2975c5a4c82aSJason M. Bills                 iface = crashdumpOnDemandInterface;
2976c5a4c82aSJason M. Bills                 method = "GenerateOnDemandLog";
2977c5a4c82aSJason M. Bills                 taskMatchStr = "type='signal',"
2978c5a4c82aSJason M. Bills                                "interface='org.freedesktop.DBus.Properties',"
2979c5a4c82aSJason M. Bills                                "member='PropertiesChanged',"
2980c5a4c82aSJason M. Bills                                "arg0namespace='com.intel.crashdump'";
2981c5a4c82aSJason M. Bills             }
2982c5a4c82aSJason M. Bills             else if (oemDiagType == OEMDiagnosticType::telemetry)
2983c5a4c82aSJason M. Bills             {
2984c5a4c82aSJason M. Bills                 iface = crashdumpTelemetryInterface;
2985c5a4c82aSJason M. Bills                 method = "GenerateTelemetryLog";
2986c5a4c82aSJason M. Bills                 taskMatchStr = "type='signal',"
2987c5a4c82aSJason M. Bills                                "interface='org.freedesktop.DBus.Properties',"
2988c5a4c82aSJason M. Bills                                "member='PropertiesChanged',"
2989c5a4c82aSJason M. Bills                                "arg0namespace='com.intel.crashdump'";
2990c5a4c82aSJason M. Bills             }
2991c5a4c82aSJason M. Bills             else
2992c5a4c82aSJason M. Bills             {
2993c5a4c82aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2994c5a4c82aSJason M. Bills                                  << oemDiagnosticDataType;
2995c5a4c82aSJason M. Bills                 messages::actionParameterValueFormatError(
2996c5a4c82aSJason M. Bills                     asyncResp->res, oemDiagnosticDataType,
2997c5a4c82aSJason M. Bills                     "OEMDiagnosticDataType", "CollectDiagnosticData");
2998c5a4c82aSJason M. Bills                 return;
2999c5a4c82aSJason M. Bills             }
3000c5a4c82aSJason M. Bills 
3001c5a4c82aSJason M. Bills             auto collectCrashdumpCallback =
3002c5a4c82aSJason M. Bills                 [asyncResp, payload(task::Payload(req)),
3003c5a4c82aSJason M. Bills                  taskMatchStr](const boost::system::error_code ec,
300498be3e39SEd Tanous                                const std::string&) mutable {
30051da66f75SEd Tanous                     if (ec)
30061da66f75SEd Tanous                     {
30077e860f15SJohn Edward Broadbent                         if (ec.value() ==
30087e860f15SJohn Edward Broadbent                             boost::system::errc::operation_not_supported)
30091da66f75SEd Tanous                         {
3010f12894f8SJason M. Bills                             messages::resourceInStandby(asyncResp->res);
30111da66f75SEd Tanous                         }
30124363d3b2SJason M. Bills                         else if (ec.value() ==
30134363d3b2SJason M. Bills                                  boost::system::errc::device_or_resource_busy)
30144363d3b2SJason M. Bills                         {
3015c5a4c82aSJason M. Bills                             messages::serviceTemporarilyUnavailable(
3016c5a4c82aSJason M. Bills                                 asyncResp->res, "60");
30174363d3b2SJason M. Bills                         }
30181da66f75SEd Tanous                         else
30191da66f75SEd Tanous                         {
3020f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
30211da66f75SEd Tanous                         }
30221da66f75SEd Tanous                         return;
30231da66f75SEd Tanous                     }
3024c5a4c82aSJason M. Bills                     std::shared_ptr<task::TaskData> task =
3025c5a4c82aSJason M. Bills                         task::TaskData::createTask(
30267e860f15SJohn Edward Broadbent                             [](boost::system::error_code err,
30277e860f15SJohn Edward Broadbent                                sdbusplus::message::message&,
3028c5a4c82aSJason M. Bills                                const std::shared_ptr<task::TaskData>&
3029c5a4c82aSJason M. Bills                                    taskData) {
303066afe4faSJames Feist                                 if (!err)
303166afe4faSJames Feist                                 {
3032e5d5006bSJames Feist                                     taskData->messages.emplace_back(
3033e5d5006bSJames Feist                                         messages::taskCompletedOK(
3034e5d5006bSJames Feist                                             std::to_string(taskData->index)));
3035831d6b09SJames Feist                                     taskData->state = "Completed";
303666afe4faSJames Feist                                 }
303732898ceaSJames Feist                                 return task::completed;
303866afe4faSJames Feist                             },
3039c5a4c82aSJason M. Bills                             taskMatchStr);
3040c5a4c82aSJason M. Bills 
304146229577SJames Feist                     task->startTimer(std::chrono::minutes(5));
304246229577SJames Feist                     task->populateResp(asyncResp->res);
304398be3e39SEd Tanous                     task->payload.emplace(std::move(payload));
30441da66f75SEd Tanous                 };
30458e6c099aSJason M. Bills 
30461da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
30478e6c099aSJason M. Bills                 std::move(collectCrashdumpCallback), crashdumpObject,
3048c5a4c82aSJason M. Bills                 crashdumpPath, iface, method);
30497e860f15SJohn Edward Broadbent         });
30506eda7685SKenny L. Ku }
30516eda7685SKenny L. Ku 
3052cb92c03bSAndrew Geissler /**
3053cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3054cb92c03bSAndrew Geissler  */
30557e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3056cb92c03bSAndrew Geissler {
3057cb92c03bSAndrew Geissler     /**
3058cb92c03bSAndrew Geissler      * Function handles POST method request.
3059cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3060cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3061cb92c03bSAndrew Geissler      */
30627e860f15SJohn Edward Broadbent 
30630fda0f12SGeorge Liu     BMCWEB_ROUTE(
30640fda0f12SGeorge Liu         app,
30650fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3066ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
30677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
306845ca1b86SEd Tanous             [&app](const crow::Request& req,
30697e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
307045ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
307145ca1b86SEd Tanous                 {
307245ca1b86SEd Tanous                     return;
307345ca1b86SEd Tanous                 }
3074cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
3075cb92c03bSAndrew Geissler 
3076cb92c03bSAndrew Geissler                 // Process response from Logging service.
30777e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
30787e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
30797e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
30807e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
3081cb92c03bSAndrew Geissler                     if (ec)
3082cb92c03bSAndrew Geissler                     {
3083cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
30847e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
30857e860f15SJohn Edward Broadbent                                          << ec;
3086cb92c03bSAndrew Geissler                         asyncResp->res.result(
3087cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
3088cb92c03bSAndrew Geissler                         return;
3089cb92c03bSAndrew Geissler                     }
3090cb92c03bSAndrew Geissler 
30917e860f15SJohn Edward Broadbent                     asyncResp->res.result(
30927e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
3093cb92c03bSAndrew Geissler                 };
3094cb92c03bSAndrew Geissler 
3095cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
3096cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
30972c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
3098cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
3099cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31007e860f15SJohn Edward Broadbent             });
3101cb92c03bSAndrew Geissler }
3102a3316fc6SZhikuiRen 
3103a3316fc6SZhikuiRen /****************************************************
3104a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3105a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3106a3316fc6SZhikuiRen  ******************************************************/
31077e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3108a3316fc6SZhikuiRen {
31097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3110ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
311145ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
311245ca1b86SEd Tanous                                                        const std::shared_ptr<
311345ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
311445ca1b86SEd Tanous                                                            asyncResp) {
311545ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
311645ca1b86SEd Tanous             {
311745ca1b86SEd Tanous                 return;
311845ca1b86SEd Tanous             }
3119a3316fc6SZhikuiRen             asyncResp->res.jsonValue = {
31207e860f15SJohn Edward Broadbent                 {"@odata.id",
31217e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes"},
3122a3316fc6SZhikuiRen                 {"@odata.type", "#LogService.v1_1_0.LogService"},
3123a3316fc6SZhikuiRen                 {"Name", "POST Code Log Service"},
3124a3316fc6SZhikuiRen                 {"Description", "POST Code Log Service"},
3125a3316fc6SZhikuiRen                 {"Id", "BIOS POST Code Log"},
3126a3316fc6SZhikuiRen                 {"OverWritePolicy", "WrapsWhenFull"},
3127a3316fc6SZhikuiRen                 {"Entries",
31280fda0f12SGeorge Liu                  {{"@odata.id",
31290fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
31307c8c4058STejas Patil 
31317c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
31327c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
31330fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
31347c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
31357c8c4058STejas Patil                 redfishDateTimeOffset.second;
31367c8c4058STejas Patil 
3137a3316fc6SZhikuiRen             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
31387e860f15SJohn Edward Broadbent                 {"target",
31390fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
31407e860f15SJohn Edward Broadbent         });
3141a3316fc6SZhikuiRen }
3142a3316fc6SZhikuiRen 
31437e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3144a3316fc6SZhikuiRen {
31450fda0f12SGeorge Liu     BMCWEB_ROUTE(
31460fda0f12SGeorge Liu         app,
31470fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3148ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3149ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3150432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
31517e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
315245ca1b86SEd Tanous             [&app](const crow::Request& req,
31537e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
315445ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
315545ca1b86SEd Tanous                 {
315645ca1b86SEd Tanous                     return;
315745ca1b86SEd Tanous                 }
3158a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3159a3316fc6SZhikuiRen 
3160a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
3161a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
3162a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
3163a3316fc6SZhikuiRen                         if (ec)
3164a3316fc6SZhikuiRen                         {
3165a3316fc6SZhikuiRen                             // TODO Handle for specific error code
3166a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
31677e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
31687e860f15SJohn Edward Broadbent                                 << ec;
31697e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
31707e860f15SJohn Edward Broadbent                                                       internal_server_error);
3171a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
3172a3316fc6SZhikuiRen                             return;
3173a3316fc6SZhikuiRen                         }
3174a3316fc6SZhikuiRen                     },
317515124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
317615124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
3177a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31787e860f15SJohn Edward Broadbent             });
3179a3316fc6SZhikuiRen }
3180a3316fc6SZhikuiRen 
3181a3316fc6SZhikuiRen static void fillPostCodeEntry(
31828d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
31836c9a279eSManojkiran Eda     const boost::container::flat_map<
31846c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3185a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3186a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3187a3316fc6SZhikuiRen {
3188a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3189fffb8c1fSEd Tanous     const registries::Message* message =
3190fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3191a3316fc6SZhikuiRen 
3192a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3193a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3194a3316fc6SZhikuiRen 
3195a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
31966c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
31976c9a279eSManojkiran Eda              code : postcode)
3198a3316fc6SZhikuiRen     {
3199a3316fc6SZhikuiRen         currentCodeIndex++;
3200a3316fc6SZhikuiRen         std::string postcodeEntryID =
3201a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3202a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3203a3316fc6SZhikuiRen 
3204a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3205a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3206a3316fc6SZhikuiRen 
3207a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3208a3316fc6SZhikuiRen         { // already incremented
3209a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3210a3316fc6SZhikuiRen         }
3211a3316fc6SZhikuiRen         else
3212a3316fc6SZhikuiRen         {
3213a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3214a3316fc6SZhikuiRen         }
3215a3316fc6SZhikuiRen 
3216a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3217a3316fc6SZhikuiRen         // not fall between top and skip
3218a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3219a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3220a3316fc6SZhikuiRen         {
3221a3316fc6SZhikuiRen             continue;
3222a3316fc6SZhikuiRen         }
3223a3316fc6SZhikuiRen 
32244e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3225a3316fc6SZhikuiRen         // currentIndex
3226a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3227a3316fc6SZhikuiRen         {
3228a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3229a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3230a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3231a3316fc6SZhikuiRen             continue;
3232a3316fc6SZhikuiRen         }
3233a3316fc6SZhikuiRen 
3234a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3235a3316fc6SZhikuiRen         // index
3236a3316fc6SZhikuiRen 
3237a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3238a3316fc6SZhikuiRen         std::string entryTimeStr;
32391d8782e7SNan Zhou         entryTimeStr =
32401d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3241a3316fc6SZhikuiRen 
3242a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3243a3316fc6SZhikuiRen         std::ostringstream hexCode;
3244a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32456c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3246a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3247a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3248a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3249a3316fc6SZhikuiRen         // Set precision to 4 digits
3250a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3251a3316fc6SZhikuiRen         // Add double to stream
3252a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3253a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3254a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3255a3316fc6SZhikuiRen 
3256a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3257a3316fc6SZhikuiRen         std::string msg;
3258a3316fc6SZhikuiRen         if (message != nullptr)
3259a3316fc6SZhikuiRen         {
3260a3316fc6SZhikuiRen             msg = message->message;
3261a3316fc6SZhikuiRen 
3262a3316fc6SZhikuiRen             // fill in this post code value
3263a3316fc6SZhikuiRen             int i = 0;
3264a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3265a3316fc6SZhikuiRen             {
3266a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3267a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3268a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3269a3316fc6SZhikuiRen                 {
3270a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3271a3316fc6SZhikuiRen                 }
3272a3316fc6SZhikuiRen             }
3273a3316fc6SZhikuiRen         }
3274a3316fc6SZhikuiRen 
3275d4342a92STim Lee         // Get Severity template from message registry
3276d4342a92STim Lee         std::string severity;
3277d4342a92STim Lee         if (message != nullptr)
3278d4342a92STim Lee         {
32795f2b84eeSEd Tanous             severity = message->messageSeverity;
3280d4342a92STim Lee         }
3281d4342a92STim Lee 
3282a3316fc6SZhikuiRen         // add to AsyncResp
3283a3316fc6SZhikuiRen         logEntryArray.push_back({});
3284a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
32850fda0f12SGeorge Liu         bmcLogEntry = {
32860fda0f12SGeorge Liu             {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
32870fda0f12SGeorge Liu             {"@odata.id",
32880fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3289a3316fc6SZhikuiRen                  postcodeEntryID},
3290a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
3291a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
3292a3316fc6SZhikuiRen             {"Message", std::move(msg)},
32934a0bf539SManojkiran Eda             {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3294a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
3295a3316fc6SZhikuiRen             {"EntryType", "Event"},
3296a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
32979c620e21SAsmitha Karunanithi             {"Created", entryTimeStr}};
3298647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3299647b3cdcSGeorge Liu         {
3300647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3301647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3302647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3303647b3cdcSGeorge Liu         }
3304a3316fc6SZhikuiRen     }
3305a3316fc6SZhikuiRen }
3306a3316fc6SZhikuiRen 
33078d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3308a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3309a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3310a3316fc6SZhikuiRen {
3311a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
33126c9a279eSManojkiran Eda         [aResp, bootIndex,
33136c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
33146c9a279eSManojkiran Eda                     const boost::container::flat_map<
33156c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33166c9a279eSManojkiran Eda                         postcode) {
3317a3316fc6SZhikuiRen             if (ec)
3318a3316fc6SZhikuiRen             {
3319a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3320a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3321a3316fc6SZhikuiRen                 return;
3322a3316fc6SZhikuiRen             }
3323a3316fc6SZhikuiRen 
3324a3316fc6SZhikuiRen             // skip the empty postcode boots
3325a3316fc6SZhikuiRen             if (postcode.empty())
3326a3316fc6SZhikuiRen             {
3327a3316fc6SZhikuiRen                 return;
3328a3316fc6SZhikuiRen             }
3329a3316fc6SZhikuiRen 
3330a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3331a3316fc6SZhikuiRen 
3332a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3333a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3334a3316fc6SZhikuiRen         },
333515124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
333615124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3337a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3338a3316fc6SZhikuiRen         bootIndex);
3339a3316fc6SZhikuiRen }
3340a3316fc6SZhikuiRen 
33418d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3342a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3343a3316fc6SZhikuiRen                                const uint16_t bootCount,
3344a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3345a3316fc6SZhikuiRen                                const uint64_t top)
3346a3316fc6SZhikuiRen {
3347a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3348a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3349a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
33506c9a279eSManojkiran Eda               const boost::container::flat_map<
33516c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33526c9a279eSManojkiran Eda                   postcode) {
3353a3316fc6SZhikuiRen             if (ec)
3354a3316fc6SZhikuiRen             {
3355a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3356a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3357a3316fc6SZhikuiRen                 return;
3358a3316fc6SZhikuiRen             }
3359a3316fc6SZhikuiRen 
3360a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3361a3316fc6SZhikuiRen             if (!postcode.empty())
3362a3316fc6SZhikuiRen             {
3363a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3364a3316fc6SZhikuiRen 
3365a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3366a3316fc6SZhikuiRen                 {
3367a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3368a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3369a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3370a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3371a3316fc6SZhikuiRen 
3372a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3373a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3374a3316fc6SZhikuiRen                 }
3375a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3376a3316fc6SZhikuiRen             }
3377a3316fc6SZhikuiRen 
3378a3316fc6SZhikuiRen             // continue to previous bootIndex
3379a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3380a3316fc6SZhikuiRen             {
3381a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3382a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3383a3316fc6SZhikuiRen             }
3384a3316fc6SZhikuiRen             else
3385a3316fc6SZhikuiRen             {
3386a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
33870fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3388a3316fc6SZhikuiRen                     std::to_string(skip + top);
3389a3316fc6SZhikuiRen             }
3390a3316fc6SZhikuiRen         },
339115124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
339215124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3393a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3394a3316fc6SZhikuiRen         bootIndex);
3395a3316fc6SZhikuiRen }
3396a3316fc6SZhikuiRen 
33978d1b46d7Szhanghch05 static void
33988d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3399a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3400a3316fc6SZhikuiRen {
3401a3316fc6SZhikuiRen     uint64_t entryCount = 0;
34021e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
34031e1e598dSJonathan Doman         *crow::connections::systemBus,
34041e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
34051e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
34061e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
34071e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
34081e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3409a3316fc6SZhikuiRen             if (ec)
3410a3316fc6SZhikuiRen             {
3411a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3412a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3413a3316fc6SZhikuiRen                 return;
3414a3316fc6SZhikuiRen             }
34151e1e598dSJonathan Doman             getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
34161e1e598dSJonathan Doman         });
3417a3316fc6SZhikuiRen }
3418a3316fc6SZhikuiRen 
34197e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3420a3316fc6SZhikuiRen {
34217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
34227e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3423ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
34247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
342545ca1b86SEd Tanous             [&app](const crow::Request& req,
34267e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3427*c937d2bfSEd Tanous                 query_param::QueryCapabilities capabilities = {
3428*c937d2bfSEd Tanous                     .canDelegateTop = true,
3429*c937d2bfSEd Tanous                     .canDelegateSkip = true,
3430*c937d2bfSEd Tanous                 };
3431*c937d2bfSEd Tanous                 query_param::Query delegatedQuery;
3432*c937d2bfSEd Tanous                 if (!redfish::setUpRedfishRouteWithDelegation(
3433*c937d2bfSEd Tanous                         app, req, asyncResp->res, delegatedQuery, capabilities))
343445ca1b86SEd Tanous                 {
343545ca1b86SEd Tanous                     return;
343645ca1b86SEd Tanous                 }
3437a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3438a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3439a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3440a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3441a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3442a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3443a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3444a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3445a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3446a3316fc6SZhikuiRen 
3447*c937d2bfSEd Tanous                 getCurrentBootNumber(asyncResp, delegatedQuery.skip,
3448*c937d2bfSEd Tanous                                      delegatedQuery.top);
34497e860f15SJohn Edward Broadbent             });
3450a3316fc6SZhikuiRen }
3451a3316fc6SZhikuiRen 
3452647b3cdcSGeorge Liu /**
3453647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3454647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3455647b3cdcSGeorge Liu  *
3456647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3457647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3458647b3cdcSGeorge Liu  * @param[out] index          Index value
3459647b3cdcSGeorge Liu  *
3460647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3461647b3cdcSGeorge Liu  */
3462647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3463647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3464647b3cdcSGeorge Liu {
3465647b3cdcSGeorge Liu     std::vector<std::string> split;
3466647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3467647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3468647b3cdcSGeorge Liu     {
3469647b3cdcSGeorge Liu         return false;
3470647b3cdcSGeorge Liu     }
3471647b3cdcSGeorge Liu 
3472ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3473647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3474ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3475647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3476647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3477647b3cdcSGeorge Liu 
3478647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3479647b3cdcSGeorge Liu     {
3480647b3cdcSGeorge Liu         return false;
3481647b3cdcSGeorge Liu     }
3482647b3cdcSGeorge Liu 
3483647b3cdcSGeorge Liu     start = split[1].data();
3484ca45aa3cSEd Tanous 
3485ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3486647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3487647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3488647b3cdcSGeorge Liu 
3489dcf2ebc0SEd Tanous     return ptrValue == end && ecValue != std::errc();
3490647b3cdcSGeorge Liu }
3491647b3cdcSGeorge Liu 
3492647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3493647b3cdcSGeorge Liu {
34940fda0f12SGeorge Liu     BMCWEB_ROUTE(
34950fda0f12SGeorge Liu         app,
34960fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3497647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3498647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
349945ca1b86SEd Tanous             [&app](const crow::Request& req,
3500647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3501647b3cdcSGeorge Liu                    const std::string& postCodeID) {
350245ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
350345ca1b86SEd Tanous                 {
350445ca1b86SEd Tanous                     return;
350545ca1b86SEd Tanous                 }
3506647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
3507647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
3508647b3cdcSGeorge Liu                 {
3509647b3cdcSGeorge Liu                     asyncResp->res.result(
3510647b3cdcSGeorge Liu                         boost::beast::http::status::bad_request);
3511647b3cdcSGeorge Liu                     return;
3512647b3cdcSGeorge Liu                 }
3513647b3cdcSGeorge Liu 
3514647b3cdcSGeorge Liu                 uint64_t currentValue = 0;
3515647b3cdcSGeorge Liu                 uint16_t index = 0;
3516647b3cdcSGeorge Liu                 if (!parsePostCode(postCodeID, currentValue, index))
3517647b3cdcSGeorge Liu                 {
3518647b3cdcSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
3519647b3cdcSGeorge Liu                                                postCodeID);
3520647b3cdcSGeorge Liu                     return;
3521647b3cdcSGeorge Liu                 }
3522647b3cdcSGeorge Liu 
3523647b3cdcSGeorge Liu                 crow::connections::systemBus->async_method_call(
3524647b3cdcSGeorge Liu                     [asyncResp, postCodeID, currentValue](
3525647b3cdcSGeorge Liu                         const boost::system::error_code ec,
3526647b3cdcSGeorge Liu                         const std::vector<std::tuple<
3527647b3cdcSGeorge Liu                             uint64_t, std::vector<uint8_t>>>& postcodes) {
3528647b3cdcSGeorge Liu                         if (ec.value() == EBADR)
3529647b3cdcSGeorge Liu                         {
3530647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3531647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3532647b3cdcSGeorge Liu                             return;
3533647b3cdcSGeorge Liu                         }
3534647b3cdcSGeorge Liu                         if (ec)
3535647b3cdcSGeorge Liu                         {
3536647b3cdcSGeorge Liu                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3537647b3cdcSGeorge Liu                             messages::internalError(asyncResp->res);
3538647b3cdcSGeorge Liu                             return;
3539647b3cdcSGeorge Liu                         }
3540647b3cdcSGeorge Liu 
3541647b3cdcSGeorge Liu                         size_t value = static_cast<size_t>(currentValue) - 1;
3542647b3cdcSGeorge Liu                         if (value == std::string::npos ||
3543647b3cdcSGeorge Liu                             postcodes.size() < currentValue)
3544647b3cdcSGeorge Liu                         {
3545647b3cdcSGeorge Liu                             BMCWEB_LOG_ERROR << "Wrong currentValue value";
3546647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3547647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3548647b3cdcSGeorge Liu                             return;
3549647b3cdcSGeorge Liu                         }
3550647b3cdcSGeorge Liu 
35519eb808c1SEd Tanous                         const auto& [tID, c] = postcodes[value];
355246ff87baSEd Tanous                         if (c.empty())
3553647b3cdcSGeorge Liu                         {
3554647b3cdcSGeorge Liu                             BMCWEB_LOG_INFO << "No found post code data";
3555647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3556647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3557647b3cdcSGeorge Liu                             return;
3558647b3cdcSGeorge Liu                         }
355946ff87baSEd Tanous                         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
356046ff87baSEd Tanous                         const char* d = reinterpret_cast<const char*>(c.data());
356146ff87baSEd Tanous                         std::string_view strData(d, c.size());
3562647b3cdcSGeorge Liu 
3563647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Type",
3564647b3cdcSGeorge Liu                                                  "application/octet-stream");
3565647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Transfer-Encoding",
3566647b3cdcSGeorge Liu                                                  "Base64");
3567647b3cdcSGeorge Liu                         asyncResp->res.body() =
3568647b3cdcSGeorge Liu                             crow::utility::base64encode(strData);
3569647b3cdcSGeorge Liu                     },
3570647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode0",
3571647b3cdcSGeorge Liu                     "/xyz/openbmc_project/State/Boot/PostCode0",
3572647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3573647b3cdcSGeorge Liu                     index);
3574647b3cdcSGeorge Liu             });
3575647b3cdcSGeorge Liu }
3576647b3cdcSGeorge Liu 
35777e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3578a3316fc6SZhikuiRen {
35797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
35807e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3581ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
35827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
358345ca1b86SEd Tanous             [&app](const crow::Request& req,
35847e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35857e860f15SJohn Edward Broadbent                    const std::string& targetID) {
358645ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
358745ca1b86SEd Tanous                 {
358845ca1b86SEd Tanous                     return;
358945ca1b86SEd Tanous                 }
3590647b3cdcSGeorge Liu                 uint16_t bootIndex = 0;
3591647b3cdcSGeorge Liu                 uint64_t codeIndex = 0;
3592647b3cdcSGeorge Liu                 if (!parsePostCode(targetID, codeIndex, bootIndex))
3593a3316fc6SZhikuiRen                 {
3594a3316fc6SZhikuiRen                     // Requested ID was not found
3595ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3596a3316fc6SZhikuiRen                     return;
3597a3316fc6SZhikuiRen                 }
3598a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3599a3316fc6SZhikuiRen                 {
3600a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
36017e860f15SJohn Edward Broadbent                                      << targetID;
3602a3316fc6SZhikuiRen                 }
3603a3316fc6SZhikuiRen 
36047e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
36057e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3606a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
36070fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3608a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3609a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3610a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3611a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3612a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3613a3316fc6SZhikuiRen 
3614a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
36157e860f15SJohn Edward Broadbent             });
3616a3316fc6SZhikuiRen }
3617a3316fc6SZhikuiRen 
36181da66f75SEd Tanous } // namespace redfish
3619