xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 35440d18b18aecfc106baaa794c57bfd30c8ad05)
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 
184851d45dSJason M. Bills #include "registries.hpp"
194851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
204851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2146229577SJames Feist #include "task.hpp"
221da66f75SEd Tanous 
23e1f26343SJason M. Bills #include <systemd/sd-journal.h>
24400fd1fbSAdriana Kobylak #include <unistd.h>
25e1f26343SJason M. Bills 
267e860f15SJohn Edward Broadbent #include <app.hpp>
27400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
284851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
294851d45dSJason M. Bills #include <boost/beast/core/span.hpp>
30400fd1fbSAdriana Kobylak #include <boost/beast/http.hpp>
311da66f75SEd Tanous #include <boost/container/flat_map.hpp>
321ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
33cb92c03bSAndrew Geissler #include <error_messages.hpp>
34ed398213SEd Tanous #include <registries/privilege_registry.hpp>
351214b7e7SGunnar Mills 
364418c7f0SJames Feist #include <filesystem>
3775710de2SXiaochao Ma #include <optional>
38cd225da8SJason M. Bills #include <string_view>
39abf2add6SEd Tanous #include <variant>
401da66f75SEd Tanous 
411da66f75SEd Tanous namespace redfish
421da66f75SEd Tanous {
431da66f75SEd Tanous 
445b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
455b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
465b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
475b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
485b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
495b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
50424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
516eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
526eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
531da66f75SEd Tanous 
544851d45dSJason M. Bills namespace message_registries
554851d45dSJason M. Bills {
564851d45dSJason M. Bills static const Message* getMessageFromRegistry(
574851d45dSJason M. Bills     const std::string& messageKey,
584851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
594851d45dSJason M. Bills {
604851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
614851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
624851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
634851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
644851d45dSJason M. Bills                                              messageKey.c_str());
654851d45dSJason M. Bills                      });
664851d45dSJason M. Bills     if (messageIt != registry.cend())
674851d45dSJason M. Bills     {
684851d45dSJason M. Bills         return &messageIt->second;
694851d45dSJason M. Bills     }
704851d45dSJason M. Bills 
714851d45dSJason M. Bills     return nullptr;
724851d45dSJason M. Bills }
734851d45dSJason M. Bills 
744851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
754851d45dSJason M. Bills {
764851d45dSJason M. Bills     // Redfish MessageIds are in the form
774851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
784851d45dSJason M. Bills     // the right Message
794851d45dSJason M. Bills     std::vector<std::string> fields;
804851d45dSJason M. Bills     fields.reserve(4);
814851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
824851d45dSJason M. Bills     std::string& registryName = fields[0];
834851d45dSJason M. Bills     std::string& messageKey = fields[3];
844851d45dSJason M. Bills 
854851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
864851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
874851d45dSJason M. Bills     {
884851d45dSJason M. Bills         return getMessageFromRegistry(
894851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
904851d45dSJason M. Bills     }
914851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
924851d45dSJason M. Bills     {
934851d45dSJason M. Bills         return getMessageFromRegistry(
944851d45dSJason M. Bills             messageKey,
954851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
964851d45dSJason M. Bills     }
974851d45dSJason M. Bills     return nullptr;
984851d45dSJason M. Bills }
994851d45dSJason M. Bills } // namespace message_registries
1004851d45dSJason M. Bills 
101f6150403SJames Feist namespace fs = std::filesystem;
1021da66f75SEd Tanous 
103cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
10419bd78d9SPatrick Williams     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
105cb92c03bSAndrew Geissler                               int32_t, uint32_t, int64_t, uint64_t, double>>;
106cb92c03bSAndrew Geissler 
107cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
108cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
109cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
110cb92c03bSAndrew Geissler 
111cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
112cb92c03bSAndrew Geissler {
113d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
114d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
115d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
116d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
117cb92c03bSAndrew Geissler     {
118cb92c03bSAndrew Geissler         return "Critical";
119cb92c03bSAndrew Geissler     }
1203174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
121d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
122d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
123cb92c03bSAndrew Geissler     {
124cb92c03bSAndrew Geissler         return "OK";
125cb92c03bSAndrew Geissler     }
1263174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
127cb92c03bSAndrew Geissler     {
128cb92c03bSAndrew Geissler         return "Warning";
129cb92c03bSAndrew Geissler     }
130cb92c03bSAndrew Geissler     return "";
131cb92c03bSAndrew Geissler }
132cb92c03bSAndrew Geissler 
1337e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13439e77504SEd Tanous                                      const std::string_view& field,
13539e77504SEd Tanous                                      std::string_view& contents)
13616428a1aSJason M. Bills {
13716428a1aSJason M. Bills     const char* data = nullptr;
13816428a1aSJason M. Bills     size_t length = 0;
13916428a1aSJason M. Bills     int ret = 0;
14016428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
141271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
142271584abSEd Tanous                               reinterpret_cast<const void**>(&data), &length);
14316428a1aSJason M. Bills     if (ret < 0)
14416428a1aSJason M. Bills     {
14516428a1aSJason M. Bills         return ret;
14616428a1aSJason M. Bills     }
14739e77504SEd Tanous     contents = std::string_view(data, length);
14816428a1aSJason M. Bills     // Only use the content after the "=" character.
14981ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
15016428a1aSJason M. Bills     return ret;
15116428a1aSJason M. Bills }
15216428a1aSJason M. Bills 
1537e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1547e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1557e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15616428a1aSJason M. Bills {
15716428a1aSJason M. Bills     int ret = 0;
15839e77504SEd Tanous     std::string_view metadata;
15916428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16016428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16116428a1aSJason M. Bills     if (ret < 0)
16216428a1aSJason M. Bills     {
16316428a1aSJason M. Bills         return ret;
16416428a1aSJason M. Bills     }
165b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16616428a1aSJason M. Bills     return ret;
16716428a1aSJason M. Bills }
16816428a1aSJason M. Bills 
1697e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1707e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
171a3316fc6SZhikuiRen {
172a3316fc6SZhikuiRen     int ret = 0;
173a3316fc6SZhikuiRen     uint64_t timestamp = 0;
174a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
175a3316fc6SZhikuiRen     if (ret < 0)
176a3316fc6SZhikuiRen     {
177a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
178a3316fc6SZhikuiRen                          << strerror(-ret);
179a3316fc6SZhikuiRen         return false;
180a3316fc6SZhikuiRen     }
1819c620e21SAsmitha Karunanithi     entryTimestamp = crow::utility::getDateTime(
1829c620e21SAsmitha Karunanithi         static_cast<std::time_t>(timestamp / 1000 / 1000));
1839c620e21SAsmitha Karunanithi     return true;
184a3316fc6SZhikuiRen }
185a3316fc6SZhikuiRen 
1868d1b46d7Szhanghch05 static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1878d1b46d7Szhanghch05                          const crow::Request& req, uint64_t& skip)
18816428a1aSJason M. Bills {
1895a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
1905a7e877eSJames Feist         req.urlParams.find("$skip");
1915a7e877eSJames Feist     if (it != req.urlParams.end())
19216428a1aSJason M. Bills     {
1935a7e877eSJames Feist         std::string skipParam = it->value();
19416428a1aSJason M. Bills         char* ptr = nullptr;
1955a7e877eSJames Feist         skip = std::strtoul(skipParam.c_str(), &ptr, 10);
1965a7e877eSJames Feist         if (skipParam.empty() || *ptr != '\0')
19716428a1aSJason M. Bills         {
19816428a1aSJason M. Bills 
1998d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2008d1b46d7Szhanghch05                 asyncResp->res, std::string(skipParam), "$skip");
20116428a1aSJason M. Bills             return false;
20216428a1aSJason M. Bills         }
20316428a1aSJason M. Bills     }
20416428a1aSJason M. Bills     return true;
20516428a1aSJason M. Bills }
20616428a1aSJason M. Bills 
207271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
2088d1b46d7Szhanghch05 static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2098d1b46d7Szhanghch05                         const crow::Request& req, uint64_t& top)
21016428a1aSJason M. Bills {
2115a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
2125a7e877eSJames Feist         req.urlParams.find("$top");
2135a7e877eSJames Feist     if (it != req.urlParams.end())
21416428a1aSJason M. Bills     {
2155a7e877eSJames Feist         std::string topParam = it->value();
21616428a1aSJason M. Bills         char* ptr = nullptr;
2175a7e877eSJames Feist         top = std::strtoul(topParam.c_str(), &ptr, 10);
2185a7e877eSJames Feist         if (topParam.empty() || *ptr != '\0')
21916428a1aSJason M. Bills         {
2208d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2218d1b46d7Szhanghch05                 asyncResp->res, std::string(topParam), "$top");
22216428a1aSJason M. Bills             return false;
22316428a1aSJason M. Bills         }
224271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
22516428a1aSJason M. Bills         {
22616428a1aSJason M. Bills 
22716428a1aSJason M. Bills             messages::queryParameterOutOfRange(
2288d1b46d7Szhanghch05                 asyncResp->res, std::to_string(top), "$top",
22916428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
23016428a1aSJason M. Bills             return false;
23116428a1aSJason M. Bills         }
23216428a1aSJason M. Bills     }
23316428a1aSJason M. Bills     return true;
23416428a1aSJason M. Bills }
23516428a1aSJason M. Bills 
2367e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
237e85d6b16SJason M. Bills                                     const bool firstEntry = true)
23816428a1aSJason M. Bills {
23916428a1aSJason M. Bills     int ret = 0;
24016428a1aSJason M. Bills     static uint64_t prevTs = 0;
24116428a1aSJason M. Bills     static int index = 0;
242e85d6b16SJason M. Bills     if (firstEntry)
243e85d6b16SJason M. Bills     {
244e85d6b16SJason M. Bills         prevTs = 0;
245e85d6b16SJason M. Bills     }
246e85d6b16SJason M. Bills 
24716428a1aSJason M. Bills     // Get the entry timestamp
24816428a1aSJason M. Bills     uint64_t curTs = 0;
24916428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
25016428a1aSJason M. Bills     if (ret < 0)
25116428a1aSJason M. Bills     {
25216428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
25316428a1aSJason M. Bills                          << strerror(-ret);
25416428a1aSJason M. Bills         return false;
25516428a1aSJason M. Bills     }
25616428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
25716428a1aSJason M. Bills     if (curTs == prevTs)
25816428a1aSJason M. Bills     {
25916428a1aSJason M. Bills         index++;
26016428a1aSJason M. Bills     }
26116428a1aSJason M. Bills     else
26216428a1aSJason M. Bills     {
26316428a1aSJason M. Bills         // Otherwise, reset it
26416428a1aSJason M. Bills         index = 0;
26516428a1aSJason M. Bills     }
26616428a1aSJason M. Bills     // Save the timestamp
26716428a1aSJason M. Bills     prevTs = curTs;
26816428a1aSJason M. Bills 
26916428a1aSJason M. Bills     entryID = std::to_string(curTs);
27016428a1aSJason M. Bills     if (index > 0)
27116428a1aSJason M. Bills     {
27216428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
27316428a1aSJason M. Bills     }
27416428a1aSJason M. Bills     return true;
27516428a1aSJason M. Bills }
27616428a1aSJason M. Bills 
277e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
278e85d6b16SJason M. Bills                              const bool firstEntry = true)
27995820184SJason M. Bills {
280271584abSEd Tanous     static time_t prevTs = 0;
28195820184SJason M. Bills     static int index = 0;
282e85d6b16SJason M. Bills     if (firstEntry)
283e85d6b16SJason M. Bills     {
284e85d6b16SJason M. Bills         prevTs = 0;
285e85d6b16SJason M. Bills     }
286e85d6b16SJason M. Bills 
28795820184SJason M. Bills     // Get the entry timestamp
288271584abSEd Tanous     std::time_t curTs = 0;
28995820184SJason M. Bills     std::tm timeStruct = {};
29095820184SJason M. Bills     std::istringstream entryStream(logEntry);
29195820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
29295820184SJason M. Bills     {
29395820184SJason M. Bills         curTs = std::mktime(&timeStruct);
29495820184SJason M. Bills     }
29595820184SJason M. Bills     // If the timestamp isn't unique, increment the index
29695820184SJason M. Bills     if (curTs == prevTs)
29795820184SJason M. Bills     {
29895820184SJason M. Bills         index++;
29995820184SJason M. Bills     }
30095820184SJason M. Bills     else
30195820184SJason M. Bills     {
30295820184SJason M. Bills         // Otherwise, reset it
30395820184SJason M. Bills         index = 0;
30495820184SJason M. Bills     }
30595820184SJason M. Bills     // Save the timestamp
30695820184SJason M. Bills     prevTs = curTs;
30795820184SJason M. Bills 
30895820184SJason M. Bills     entryID = std::to_string(curTs);
30995820184SJason M. Bills     if (index > 0)
31095820184SJason M. Bills     {
31195820184SJason M. Bills         entryID += "_" + std::to_string(index);
31295820184SJason M. Bills     }
31395820184SJason M. Bills     return true;
31495820184SJason M. Bills }
31595820184SJason M. Bills 
3167e860f15SJohn Edward Broadbent inline static bool
3178d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3188d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3198d1b46d7Szhanghch05                        uint64_t& index)
32016428a1aSJason M. Bills {
32116428a1aSJason M. Bills     if (entryID.empty())
32216428a1aSJason M. Bills     {
32316428a1aSJason M. Bills         return false;
32416428a1aSJason M. Bills     }
32516428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
32639e77504SEd Tanous     std::string_view tsStr(entryID);
32716428a1aSJason M. Bills 
32881ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
32916428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
33016428a1aSJason M. Bills     {
33116428a1aSJason M. Bills         // Timestamp has an index
33216428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
33339e77504SEd Tanous         std::string_view indexStr(entryID);
33416428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
335c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
336c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
337c0bd5e4bSEd Tanous         if (ec != std::errc())
33816428a1aSJason M. Bills         {
3398d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
34016428a1aSJason M. Bills             return false;
34116428a1aSJason M. Bills         }
34216428a1aSJason M. Bills     }
34316428a1aSJason M. Bills     // Timestamp has no index
344c0bd5e4bSEd Tanous     auto [ptr, ec] =
345c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
346c0bd5e4bSEd Tanous     if (ec != std::errc())
34716428a1aSJason M. Bills     {
3488d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
34916428a1aSJason M. Bills         return false;
35016428a1aSJason M. Bills     }
35116428a1aSJason M. Bills     return true;
35216428a1aSJason M. Bills }
35316428a1aSJason M. Bills 
35495820184SJason M. Bills static bool
35595820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
35695820184SJason M. Bills {
35795820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
35895820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
35995820184SJason M. Bills 
36095820184SJason M. Bills     // Loop through the directory looking for redfish log files
36195820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
36295820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
36395820184SJason M. Bills     {
36495820184SJason M. Bills         // If we find a redfish log file, save the path
36595820184SJason M. Bills         std::string filename = dirEnt.path().filename();
36695820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
36795820184SJason M. Bills         {
36895820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
36995820184SJason M. Bills         }
37095820184SJason M. Bills     }
37195820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
37295820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
37395820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
37495820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
37595820184SJason M. Bills 
37695820184SJason M. Bills     return !redfishLogFiles.empty();
37795820184SJason M. Bills }
37895820184SJason M. Bills 
3798d1b46d7Szhanghch05 inline void
3808d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3815cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3825cb1dd27SAsmitha Karunanithi {
3835cb1dd27SAsmitha Karunanithi     std::string dumpPath;
3845cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
3855cb1dd27SAsmitha Karunanithi     {
3865cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
3875cb1dd27SAsmitha Karunanithi     }
3885cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
3895cb1dd27SAsmitha Karunanithi     {
3905cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
3915cb1dd27SAsmitha Karunanithi     }
3925cb1dd27SAsmitha Karunanithi     else
3935cb1dd27SAsmitha Karunanithi     {
3945cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
3955cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3965cb1dd27SAsmitha Karunanithi         return;
3975cb1dd27SAsmitha Karunanithi     }
3985cb1dd27SAsmitha Karunanithi 
3995cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
4005cb1dd27SAsmitha Karunanithi         [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
4015cb1dd27SAsmitha Karunanithi                                         GetManagedObjectsType& resp) {
4025cb1dd27SAsmitha Karunanithi             if (ec)
4035cb1dd27SAsmitha Karunanithi             {
4045cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4055cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
4065cb1dd27SAsmitha Karunanithi                 return;
4075cb1dd27SAsmitha Karunanithi             }
4085cb1dd27SAsmitha Karunanithi 
4095cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4105cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
411b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
412b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
413b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
414b47452b2SAsmitha Karunanithi                 "/entry/";
4155cb1dd27SAsmitha Karunanithi 
4165cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4175cb1dd27SAsmitha Karunanithi             {
418b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4195cb1dd27SAsmitha Karunanithi                 {
4205cb1dd27SAsmitha Karunanithi                     continue;
4215cb1dd27SAsmitha Karunanithi                 }
4225cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
4235cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
424*35440d18SAsmitha Karunanithi                 std::string dumpStatus;
425*35440d18SAsmitha Karunanithi                 nlohmann::json thisEntry;
4262dfd18efSEd Tanous 
4272dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4282dfd18efSEd Tanous                 if (entryID.empty())
4295cb1dd27SAsmitha Karunanithi                 {
4305cb1dd27SAsmitha Karunanithi                     continue;
4315cb1dd27SAsmitha Karunanithi                 }
4325cb1dd27SAsmitha Karunanithi 
4335cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4345cb1dd27SAsmitha Karunanithi                 {
435*35440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
436*35440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
437*35440d18SAsmitha Karunanithi                     {
438*35440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
439*35440d18SAsmitha Karunanithi                         {
440*35440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
441*35440d18SAsmitha Karunanithi                             {
442*35440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
443*35440d18SAsmitha Karunanithi                                     &propertyMap.second);
444*35440d18SAsmitha Karunanithi                                 if (status == nullptr)
445*35440d18SAsmitha Karunanithi                                 {
446*35440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
447*35440d18SAsmitha Karunanithi                                     break;
448*35440d18SAsmitha Karunanithi                                 }
449*35440d18SAsmitha Karunanithi                                 dumpStatus = *status;
450*35440d18SAsmitha Karunanithi                             }
451*35440d18SAsmitha Karunanithi                         }
452*35440d18SAsmitha Karunanithi                     }
453*35440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
454*35440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
4555cb1dd27SAsmitha Karunanithi                     {
4565cb1dd27SAsmitha Karunanithi 
4575cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4585cb1dd27SAsmitha Karunanithi                         {
4595cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4605cb1dd27SAsmitha Karunanithi                             {
4615cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
4625cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4635cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4645cb1dd27SAsmitha Karunanithi                                 {
4655cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4665cb1dd27SAsmitha Karunanithi                                     break;
4675cb1dd27SAsmitha Karunanithi                                 }
4685cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4695cb1dd27SAsmitha Karunanithi                                 break;
4705cb1dd27SAsmitha Karunanithi                             }
4715cb1dd27SAsmitha Karunanithi                         }
4725cb1dd27SAsmitha Karunanithi                     }
4735cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4745cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4755cb1dd27SAsmitha Karunanithi                     {
4765cb1dd27SAsmitha Karunanithi 
4775cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4785cb1dd27SAsmitha Karunanithi                         {
4795cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4805cb1dd27SAsmitha Karunanithi                             {
4815cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4825cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4835cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4845cb1dd27SAsmitha Karunanithi                                 {
4855cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4865cb1dd27SAsmitha Karunanithi                                     break;
4875cb1dd27SAsmitha Karunanithi                                 }
4885cb1dd27SAsmitha Karunanithi                                 timestamp =
4895cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
4905cb1dd27SAsmitha Karunanithi                                 break;
4915cb1dd27SAsmitha Karunanithi                             }
4925cb1dd27SAsmitha Karunanithi                         }
4935cb1dd27SAsmitha Karunanithi                     }
4945cb1dd27SAsmitha Karunanithi                 }
4955cb1dd27SAsmitha Karunanithi 
496*35440d18SAsmitha Karunanithi                 if (dumpStatus != "xyz.openbmc_project.Common.Progress."
497*35440d18SAsmitha Karunanithi                                   "OperationStatus.Completed" &&
498*35440d18SAsmitha Karunanithi                     !dumpStatus.empty())
499*35440d18SAsmitha Karunanithi                 {
500*35440d18SAsmitha Karunanithi                     // Dump status is not Complete, no need to enumerate
501*35440d18SAsmitha Karunanithi                     continue;
502*35440d18SAsmitha Karunanithi                 }
503*35440d18SAsmitha Karunanithi 
504d0dbeefdSEd Tanous                 thisEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
5055cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
5065cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
5075cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5085cb1dd27SAsmitha Karunanithi                 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
5095cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5105cb1dd27SAsmitha Karunanithi 
511d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5125cb1dd27SAsmitha Karunanithi 
5135cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5145cb1dd27SAsmitha Karunanithi                 {
515d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
516d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
517de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
518de8d94a3SAbhishek Patel                         entryID + "/attachment";
5195cb1dd27SAsmitha Karunanithi                 }
5205cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5215cb1dd27SAsmitha Karunanithi                 {
522d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
523d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
524d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
525de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
526de8d94a3SAbhishek Patel                         entryID + "/attachment";
5275cb1dd27SAsmitha Karunanithi                 }
528*35440d18SAsmitha Karunanithi                 entriesArray.push_back(std::move(thisEntry));
5295cb1dd27SAsmitha Karunanithi             }
5305cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5315cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5325cb1dd27SAsmitha Karunanithi         },
5335cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5345cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5355cb1dd27SAsmitha Karunanithi }
5365cb1dd27SAsmitha Karunanithi 
5378d1b46d7Szhanghch05 inline void
5388d1b46d7Szhanghch05     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5398d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5405cb1dd27SAsmitha Karunanithi {
5415cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5425cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5435cb1dd27SAsmitha Karunanithi     {
5445cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5455cb1dd27SAsmitha Karunanithi     }
5465cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5475cb1dd27SAsmitha Karunanithi     {
5485cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5495cb1dd27SAsmitha Karunanithi     }
5505cb1dd27SAsmitha Karunanithi     else
5515cb1dd27SAsmitha Karunanithi     {
5525cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5535cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5545cb1dd27SAsmitha Karunanithi         return;
5555cb1dd27SAsmitha Karunanithi     }
5565cb1dd27SAsmitha Karunanithi 
5575cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
5585cb1dd27SAsmitha Karunanithi         [asyncResp, entryID, dumpPath, dumpType](
5595cb1dd27SAsmitha Karunanithi             const boost::system::error_code ec, GetManagedObjectsType& resp) {
5605cb1dd27SAsmitha Karunanithi             if (ec)
5615cb1dd27SAsmitha Karunanithi             {
5625cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5635cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5645cb1dd27SAsmitha Karunanithi                 return;
5655cb1dd27SAsmitha Karunanithi             }
5665cb1dd27SAsmitha Karunanithi 
567b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
568b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
569b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
570b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
571b47452b2SAsmitha Karunanithi                 "/entry/";
572b47452b2SAsmitha Karunanithi 
5735cb1dd27SAsmitha Karunanithi             for (auto& objectPath : resp)
5745cb1dd27SAsmitha Karunanithi             {
575b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5765cb1dd27SAsmitha Karunanithi                 {
5775cb1dd27SAsmitha Karunanithi                     continue;
5785cb1dd27SAsmitha Karunanithi                 }
5795cb1dd27SAsmitha Karunanithi 
5805cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5815cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
5825cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
583*35440d18SAsmitha Karunanithi                 std::string dumpStatus;
5845cb1dd27SAsmitha Karunanithi 
5855cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : objectPath.second)
5865cb1dd27SAsmitha Karunanithi                 {
587*35440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
588*35440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
589*35440d18SAsmitha Karunanithi                     {
590*35440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
591*35440d18SAsmitha Karunanithi                         {
592*35440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
593*35440d18SAsmitha Karunanithi                             {
594*35440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
595*35440d18SAsmitha Karunanithi                                     &propertyMap.second);
596*35440d18SAsmitha Karunanithi                                 if (status == nullptr)
597*35440d18SAsmitha Karunanithi                                 {
598*35440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
599*35440d18SAsmitha Karunanithi                                     break;
600*35440d18SAsmitha Karunanithi                                 }
601*35440d18SAsmitha Karunanithi                                 dumpStatus = *status;
602*35440d18SAsmitha Karunanithi                             }
603*35440d18SAsmitha Karunanithi                         }
604*35440d18SAsmitha Karunanithi                     }
605*35440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
606*35440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
6075cb1dd27SAsmitha Karunanithi                     {
6085cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6095cb1dd27SAsmitha Karunanithi                         {
6105cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
6115cb1dd27SAsmitha Karunanithi                             {
6125cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
6135cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6145cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
6155cb1dd27SAsmitha Karunanithi                                 {
6165cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6175cb1dd27SAsmitha Karunanithi                                     break;
6185cb1dd27SAsmitha Karunanithi                                 }
6195cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
6205cb1dd27SAsmitha Karunanithi                                 break;
6215cb1dd27SAsmitha Karunanithi                             }
6225cb1dd27SAsmitha Karunanithi                         }
6235cb1dd27SAsmitha Karunanithi                     }
6245cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6255cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6265cb1dd27SAsmitha Karunanithi                     {
6275cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6285cb1dd27SAsmitha Karunanithi                         {
6295cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6305cb1dd27SAsmitha Karunanithi                             {
6315cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6325cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6335cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6345cb1dd27SAsmitha Karunanithi                                 {
6355cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6365cb1dd27SAsmitha Karunanithi                                     break;
6375cb1dd27SAsmitha Karunanithi                                 }
6385cb1dd27SAsmitha Karunanithi                                 timestamp =
6395cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
6405cb1dd27SAsmitha Karunanithi                                 break;
6415cb1dd27SAsmitha Karunanithi                             }
6425cb1dd27SAsmitha Karunanithi                         }
6435cb1dd27SAsmitha Karunanithi                     }
6445cb1dd27SAsmitha Karunanithi                 }
6455cb1dd27SAsmitha Karunanithi 
646*35440d18SAsmitha Karunanithi                 if (dumpStatus != "xyz.openbmc_project.Common.Progress."
647*35440d18SAsmitha Karunanithi                                   "OperationStatus.Completed" &&
648*35440d18SAsmitha Karunanithi                     !dumpStatus.empty())
649*35440d18SAsmitha Karunanithi                 {
650*35440d18SAsmitha Karunanithi                     // Dump status is not Complete
651*35440d18SAsmitha Karunanithi                     // return not found until status is changed to Completed
652*35440d18SAsmitha Karunanithi                     messages::resourceNotFound(asyncResp->res,
653*35440d18SAsmitha Karunanithi                                                dumpType + " dump", entryID);
654*35440d18SAsmitha Karunanithi                     return;
655*35440d18SAsmitha Karunanithi                 }
656*35440d18SAsmitha Karunanithi 
6575cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
658d0dbeefdSEd Tanous                     "#LogEntry.v1_7_0.LogEntry";
6595cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6605cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6615cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6625cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6635cb1dd27SAsmitha Karunanithi                     crow::utility::getDateTime(timestamp);
6645cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6655cb1dd27SAsmitha Karunanithi 
666d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6675cb1dd27SAsmitha Karunanithi 
6685cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6695cb1dd27SAsmitha Karunanithi                 {
670d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
671d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
672de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
673de8d94a3SAbhishek Patel                         entryID + "/attachment";
6745cb1dd27SAsmitha Karunanithi                 }
6755cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6765cb1dd27SAsmitha Karunanithi                 {
677d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
678d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6795cb1dd27SAsmitha Karunanithi                         "System";
680d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
681de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
682de8d94a3SAbhishek Patel                         entryID + "/attachment";
6835cb1dd27SAsmitha Karunanithi                 }
6845cb1dd27SAsmitha Karunanithi             }
685b47452b2SAsmitha Karunanithi             if (foundDumpEntry == false)
686b47452b2SAsmitha Karunanithi             {
687b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
688b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
689b47452b2SAsmitha Karunanithi                 return;
690b47452b2SAsmitha Karunanithi             }
6915cb1dd27SAsmitha Karunanithi         },
6925cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6935cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6945cb1dd27SAsmitha Karunanithi }
6955cb1dd27SAsmitha Karunanithi 
6968d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6979878256fSStanley Chu                             const std::string& entryID,
698b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6995cb1dd27SAsmitha Karunanithi {
7003de8d8baSGeorge Liu     auto respHandler = [asyncResp,
7013de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
7025cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
7035cb1dd27SAsmitha Karunanithi         if (ec)
7045cb1dd27SAsmitha Karunanithi         {
7053de8d8baSGeorge Liu             if (ec.value() == EBADR)
7063de8d8baSGeorge Liu             {
7073de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
7083de8d8baSGeorge Liu                 return;
7093de8d8baSGeorge Liu             }
7105cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
7115cb1dd27SAsmitha Karunanithi                              << ec;
7125cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
7135cb1dd27SAsmitha Karunanithi             return;
7145cb1dd27SAsmitha Karunanithi         }
7155cb1dd27SAsmitha Karunanithi     };
7165cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7175cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
718b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
719b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
720b47452b2SAsmitha Karunanithi             entryID,
7215cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7225cb1dd27SAsmitha Karunanithi }
7235cb1dd27SAsmitha Karunanithi 
7248d1b46d7Szhanghch05 inline void
7258d1b46d7Szhanghch05     createDumpTaskCallback(const crow::Request& req,
7268d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7278d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
728a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
729a43be80fSAsmitha Karunanithi {
730a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7316145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
732a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
733a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
734cb13a392SEd Tanous             if (err)
735cb13a392SEd Tanous             {
7366145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7376145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7386145ed6fSAsmitha Karunanithi                 return task::completed;
739cb13a392SEd Tanous             }
740a43be80fSAsmitha Karunanithi             std::vector<std::pair<
741a43be80fSAsmitha Karunanithi                 std::string,
742a43be80fSAsmitha Karunanithi                 std::vector<std::pair<std::string, std::variant<std::string>>>>>
743a43be80fSAsmitha Karunanithi                 interfacesList;
744a43be80fSAsmitha Karunanithi 
745a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
746a43be80fSAsmitha Karunanithi 
747a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
748a43be80fSAsmitha Karunanithi 
749b47452b2SAsmitha Karunanithi             if (objPath.str ==
750b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
751b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
752b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
753a43be80fSAsmitha Karunanithi             {
754a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
755a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
756a43be80fSAsmitha Karunanithi 
757a43be80fSAsmitha Karunanithi                 std::string headerLoc =
758a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
759a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
760a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
761a43be80fSAsmitha Karunanithi 
762a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
763b47452b2SAsmitha Karunanithi                 return task::completed;
7646145ed6fSAsmitha Karunanithi             }
765a43be80fSAsmitha Karunanithi             return task::completed;
766a43be80fSAsmitha Karunanithi         },
767a43be80fSAsmitha Karunanithi         "type='signal',interface='org.freedesktop.DBus."
768a43be80fSAsmitha Karunanithi         "ObjectManager',"
769a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
770a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
771a43be80fSAsmitha Karunanithi 
772a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
773a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
774a43be80fSAsmitha Karunanithi     task->payload.emplace(req);
775a43be80fSAsmitha Karunanithi }
776a43be80fSAsmitha Karunanithi 
7778d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7788d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
779a43be80fSAsmitha Karunanithi {
780a43be80fSAsmitha Karunanithi 
781a43be80fSAsmitha Karunanithi     std::string dumpPath;
782a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
783a43be80fSAsmitha Karunanithi     {
784a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
785a43be80fSAsmitha Karunanithi     }
786a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
787a43be80fSAsmitha Karunanithi     {
788a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
789a43be80fSAsmitha Karunanithi     }
790a43be80fSAsmitha Karunanithi     else
791a43be80fSAsmitha Karunanithi     {
792a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
793a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
794a43be80fSAsmitha Karunanithi         return;
795a43be80fSAsmitha Karunanithi     }
796a43be80fSAsmitha Karunanithi 
797a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
798a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
799a43be80fSAsmitha Karunanithi 
800a43be80fSAsmitha Karunanithi     if (!redfish::json_util::readJson(
801a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
802a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
803a43be80fSAsmitha Karunanithi     {
804a43be80fSAsmitha Karunanithi         return;
805a43be80fSAsmitha Karunanithi     }
806a43be80fSAsmitha Karunanithi 
807a43be80fSAsmitha Karunanithi     if (dumpType == "System")
808a43be80fSAsmitha Karunanithi     {
809a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
810a43be80fSAsmitha Karunanithi         {
811a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
812a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType'/"
813a43be80fSAsmitha Karunanithi                                 "'OEMDiagnosticDataType' value not found!";
814a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
815a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
816a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
817a43be80fSAsmitha Karunanithi             return;
818a43be80fSAsmitha Karunanithi         }
8193174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
820a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
821a43be80fSAsmitha Karunanithi         {
822a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
823a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
824a43be80fSAsmitha Karunanithi                                     "System Dump creation parameters");
825a43be80fSAsmitha Karunanithi             return;
826a43be80fSAsmitha Karunanithi         }
827a43be80fSAsmitha Karunanithi     }
828a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
829a43be80fSAsmitha Karunanithi     {
830a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
831a43be80fSAsmitha Karunanithi         {
832a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
833a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType' not found!";
834a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
835a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
836a43be80fSAsmitha Karunanithi             return;
837a43be80fSAsmitha Karunanithi         }
8383174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
839a43be80fSAsmitha Karunanithi         {
840a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
841a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
842a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
843a43be80fSAsmitha Karunanithi                                     "BMC Dump creation parameters");
844a43be80fSAsmitha Karunanithi             return;
845a43be80fSAsmitha Karunanithi         }
846a43be80fSAsmitha Karunanithi     }
847a43be80fSAsmitha Karunanithi 
848a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
849a43be80fSAsmitha Karunanithi         [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
850a43be80fSAsmitha Karunanithi                                              const uint32_t& dumpId) {
851a43be80fSAsmitha Karunanithi             if (ec)
852a43be80fSAsmitha Karunanithi             {
853a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
854a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
855a43be80fSAsmitha Karunanithi                 return;
856a43be80fSAsmitha Karunanithi             }
857a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
858a43be80fSAsmitha Karunanithi 
859a43be80fSAsmitha Karunanithi             createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
860a43be80fSAsmitha Karunanithi         },
861b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
862b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
863b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
864a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
865a43be80fSAsmitha Karunanithi }
866a43be80fSAsmitha Karunanithi 
8678d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8688d1b46d7Szhanghch05                       const std::string& dumpType)
86980319af1SAsmitha Karunanithi {
870b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
871b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8728d1b46d7Szhanghch05 
87380319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
874b47452b2SAsmitha Karunanithi         [asyncResp, dumpType](const boost::system::error_code ec,
87580319af1SAsmitha Karunanithi                               const std::vector<std::string>& subTreePaths) {
87680319af1SAsmitha Karunanithi             if (ec)
87780319af1SAsmitha Karunanithi             {
87880319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
87980319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
88080319af1SAsmitha Karunanithi                 return;
88180319af1SAsmitha Karunanithi             }
88280319af1SAsmitha Karunanithi 
88380319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
88480319af1SAsmitha Karunanithi             {
8852dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8862dfd18efSEd Tanous                 std::string logID = objPath.filename();
8872dfd18efSEd Tanous                 if (logID.empty())
88880319af1SAsmitha Karunanithi                 {
8892dfd18efSEd Tanous                     continue;
89080319af1SAsmitha Karunanithi                 }
8912dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
89280319af1SAsmitha Karunanithi             }
89380319af1SAsmitha Karunanithi         },
89480319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
89580319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
89680319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
897b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
898b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
899b47452b2SAsmitha Karunanithi                                    dumpType});
90080319af1SAsmitha Karunanithi }
90180319af1SAsmitha Karunanithi 
9027e860f15SJohn Edward Broadbent inline static void parseCrashdumpParameters(
903043a0536SJohnathan Mantey     const std::vector<std::pair<std::string, VariantType>>& params,
904043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
905043a0536SJohnathan Mantey {
906043a0536SJohnathan Mantey     for (auto property : params)
907043a0536SJohnathan Mantey     {
908043a0536SJohnathan Mantey         if (property.first == "Timestamp")
909043a0536SJohnathan Mantey         {
910043a0536SJohnathan Mantey             const std::string* value =
9118d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
912043a0536SJohnathan Mantey             if (value != nullptr)
913043a0536SJohnathan Mantey             {
914043a0536SJohnathan Mantey                 timestamp = *value;
915043a0536SJohnathan Mantey             }
916043a0536SJohnathan Mantey         }
917043a0536SJohnathan Mantey         else if (property.first == "Filename")
918043a0536SJohnathan Mantey         {
919043a0536SJohnathan Mantey             const std::string* value =
9208d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
921043a0536SJohnathan Mantey             if (value != nullptr)
922043a0536SJohnathan Mantey             {
923043a0536SJohnathan Mantey                 filename = *value;
924043a0536SJohnathan Mantey             }
925043a0536SJohnathan Mantey         }
926043a0536SJohnathan Mantey         else if (property.first == "Log")
927043a0536SJohnathan Mantey         {
928043a0536SJohnathan Mantey             const std::string* value =
9298d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
930043a0536SJohnathan Mantey             if (value != nullptr)
931043a0536SJohnathan Mantey             {
932043a0536SJohnathan Mantey                 logfile = *value;
933043a0536SJohnathan Mantey             }
934043a0536SJohnathan Mantey         }
935043a0536SJohnathan Mantey     }
936043a0536SJohnathan Mantey }
937043a0536SJohnathan Mantey 
938a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
9397e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9401da66f75SEd Tanous {
941c4bf6374SJason M. Bills     /**
942c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
943c4bf6374SJason M. Bills      */
9447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
945ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
9467e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9477e860f15SJohn Edward Broadbent             [](const crow::Request&,
9487e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
9497e860f15SJohn Edward Broadbent 
950c4bf6374SJason M. Bills             {
9517e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
9527e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
953c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
954c4bf6374SJason M. Bills                     "#LogServiceCollection.LogServiceCollection";
955c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
956029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices";
9577e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
9587e860f15SJohn Edward Broadbent                     "System Log Services Collection";
959c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
960c4bf6374SJason M. Bills                     "Collection of LogServices for this Computer System";
9617e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
9627e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
963c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
964029573d4SEd Tanous                 logServiceArray.push_back(
9657e860f15SJohn Edward Broadbent                     {{"@odata.id",
9667e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/EventLog"}});
9675cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
968c9bb6861Sraviteja-b                 logServiceArray.push_back(
9697e860f15SJohn Edward Broadbent                     {{"@odata.id",
9707e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/Dump"}});
971c9bb6861Sraviteja-b #endif
972c9bb6861Sraviteja-b 
973d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
974d53dd41fSJason M. Bills                 logServiceArray.push_back(
975cb92c03bSAndrew Geissler                     {{"@odata.id",
976424c4176SJason M. Bills                       "/redfish/v1/Systems/system/LogServices/Crashdump"}});
977d53dd41fSJason M. Bills #endif
978c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
979c4bf6374SJason M. Bills                     logServiceArray.size();
980a3316fc6SZhikuiRen 
981a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
982a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec,
983a3316fc6SZhikuiRen                                 const std::vector<std::string>& subtreePath) {
984a3316fc6SZhikuiRen                         if (ec)
985a3316fc6SZhikuiRen                         {
986a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR << ec;
987a3316fc6SZhikuiRen                             return;
988a3316fc6SZhikuiRen                         }
989a3316fc6SZhikuiRen 
990a3316fc6SZhikuiRen                         for (auto& pathStr : subtreePath)
991a3316fc6SZhikuiRen                         {
992a3316fc6SZhikuiRen                             if (pathStr.find("PostCode") != std::string::npos)
993a3316fc6SZhikuiRen                             {
99423a21a1cSEd Tanous                                 nlohmann::json& logServiceArrayLocal =
995a3316fc6SZhikuiRen                                     asyncResp->res.jsonValue["Members"];
99623a21a1cSEd Tanous                                 logServiceArrayLocal.push_back(
997a3316fc6SZhikuiRen                                     {{"@odata.id", "/redfish/v1/Systems/system/"
998a3316fc6SZhikuiRen                                                    "LogServices/PostCodes"}});
9997e860f15SJohn Edward Broadbent                                 asyncResp->res
10007e860f15SJohn Edward Broadbent                                     .jsonValue["Members@odata.count"] =
100123a21a1cSEd Tanous                                     logServiceArrayLocal.size();
1002a3316fc6SZhikuiRen                                 return;
1003a3316fc6SZhikuiRen                             }
1004a3316fc6SZhikuiRen                         }
1005a3316fc6SZhikuiRen                     },
1006a3316fc6SZhikuiRen                     "xyz.openbmc_project.ObjectMapper",
1007a3316fc6SZhikuiRen                     "/xyz/openbmc_project/object_mapper",
10087e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
10097e860f15SJohn Edward Broadbent                     0, std::array<const char*, 1>{postCodeIface});
10107e860f15SJohn Edward Broadbent             });
1011c4bf6374SJason M. Bills }
1012c4bf6374SJason M. Bills 
10137e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1014c4bf6374SJason M. Bills {
10157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
1016ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
10177e860f15SJohn Edward Broadbent         .methods(
10187e860f15SJohn Edward Broadbent             boost::beast::http::verb::
10197e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
10207e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1021c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1022029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
1023c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1024c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
1025c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
10267e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
10277e860f15SJohn Edward Broadbent                 "System Event Log Service";
1028c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
1029c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10307c8c4058STejas Patil 
10317c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
10327c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
10337c8c4058STejas Patil 
10347c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10357c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10367c8c4058STejas Patil                 redfishDateTimeOffset.second;
10377c8c4058STejas Patil 
1038c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
1039c4bf6374SJason M. Bills                 {"@odata.id",
1040029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1041e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1042e7d6c8b2SGunnar Mills 
1043e7d6c8b2SGunnar Mills                 {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1044e7d6c8b2SGunnar Mills                            "Actions/LogService.ClearLog"}};
10457e860f15SJohn Edward Broadbent         });
1046489640c6SJason M. Bills }
1047489640c6SJason M. Bills 
10487e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1049489640c6SJason M. Bills {
10507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1051489640c6SJason M. Bills                       "LogService.ClearLog/")
1052432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10537e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
10547e860f15SJohn Edward Broadbent             [](const crow::Request&,
10557e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1056489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1057489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1058489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1059489640c6SJason M. Bills                 {
1060489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1061489640c6SJason M. Bills                     {
1062489640c6SJason M. Bills                         std::error_code ec;
1063489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1064489640c6SJason M. Bills                     }
1065489640c6SJason M. Bills                 }
1066489640c6SJason M. Bills 
1067489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1068489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1069489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1070489640c6SJason M. Bills                         if (ec)
1071489640c6SJason M. Bills                         {
10727e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
10737e860f15SJohn Edward Broadbent                                              << ec;
1074489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1075489640c6SJason M. Bills                             return;
1076489640c6SJason M. Bills                         }
1077489640c6SJason M. Bills 
1078489640c6SJason M. Bills                         messages::success(asyncResp->res);
1079489640c6SJason M. Bills                     },
1080489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
10817e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
10827e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
10837e860f15SJohn Edward Broadbent             });
1084c4bf6374SJason M. Bills }
1085c4bf6374SJason M. Bills 
108695820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1087b5a76932SEd Tanous                                  const std::string& logEntry,
108895820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1089c4bf6374SJason M. Bills {
109095820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1091cd225da8SJason M. Bills     // First get the Timestamp
1092f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1093cd225da8SJason M. Bills     if (space == std::string::npos)
109495820184SJason M. Bills     {
109595820184SJason M. Bills         return 1;
109695820184SJason M. Bills     }
1097cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1098cd225da8SJason M. Bills     // Then get the log contents
1099f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1100cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1101cd225da8SJason M. Bills     {
1102cd225da8SJason M. Bills         return 1;
1103cd225da8SJason M. Bills     }
1104cd225da8SJason M. Bills     std::string_view entry(logEntry);
1105cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1106cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1107cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1108cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1109cd225da8SJason M. Bills                  boost::token_compress_on);
1110cd225da8SJason M. Bills     // We need at least a MessageId to be valid
1111cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
1112cd225da8SJason M. Bills     {
1113cd225da8SJason M. Bills         return 1;
1114cd225da8SJason M. Bills     }
1115cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
111695820184SJason M. Bills 
11174851d45dSJason M. Bills     // Get the Message from the MessageRegistry
11184851d45dSJason M. Bills     const message_registries::Message* message =
11194851d45dSJason M. Bills         message_registries::getMessage(messageID);
1120c4bf6374SJason M. Bills 
11214851d45dSJason M. Bills     std::string msg;
11224851d45dSJason M. Bills     std::string severity;
11234851d45dSJason M. Bills     if (message != nullptr)
1124c4bf6374SJason M. Bills     {
11254851d45dSJason M. Bills         msg = message->message;
11264851d45dSJason M. Bills         severity = message->severity;
1127c4bf6374SJason M. Bills     }
1128c4bf6374SJason M. Bills 
112915a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
113015a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
113115a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
113215a86ff6SJason M. Bills     {
113315a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
113415a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
113515a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
113615a86ff6SJason M. Bills         if (!messageArgsStart.empty())
113715a86ff6SJason M. Bills         {
113815a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
113915a86ff6SJason M. Bills         }
114015a86ff6SJason M. Bills 
114123a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1142c4bf6374SJason M. Bills 
11434851d45dSJason M. Bills         // Fill the MessageArgs into the Message
114495820184SJason M. Bills         int i = 0;
114595820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11464851d45dSJason M. Bills         {
114795820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11484851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11494851d45dSJason M. Bills             if (argPos != std::string::npos)
11504851d45dSJason M. Bills             {
115195820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11524851d45dSJason M. Bills             }
11534851d45dSJason M. Bills         }
115415a86ff6SJason M. Bills     }
11554851d45dSJason M. Bills 
115695820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
115795820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
115895820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1159f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1160f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
116195820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1162c4bf6374SJason M. Bills     {
116395820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1164c4bf6374SJason M. Bills     }
1165c4bf6374SJason M. Bills 
1166c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
116795820184SJason M. Bills     logEntryJson = {
1168d0dbeefdSEd Tanous         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1169029573d4SEd Tanous         {"@odata.id",
1170897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
117195820184SJason M. Bills              logEntryID},
1172c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
117395820184SJason M. Bills         {"Id", logEntryID},
117495820184SJason M. Bills         {"Message", std::move(msg)},
117595820184SJason M. Bills         {"MessageId", std::move(messageID)},
1176f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1177c4bf6374SJason M. Bills         {"EntryType", "Event"},
117895820184SJason M. Bills         {"Severity", std::move(severity)},
117995820184SJason M. Bills         {"Created", std::move(timestamp)}};
1180c4bf6374SJason M. Bills     return 0;
1181c4bf6374SJason M. Bills }
1182c4bf6374SJason M. Bills 
11837e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1184c4bf6374SJason M. Bills {
11857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11867e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11878b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
11887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
11897e860f15SJohn Edward Broadbent             [](const crow::Request& req,
11907e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1191271584abSEd Tanous                 uint64_t skip = 0;
1192271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
11938d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1194c4bf6374SJason M. Bills                 {
1195c4bf6374SJason M. Bills                     return;
1196c4bf6374SJason M. Bills                 }
11978d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1198c4bf6374SJason M. Bills                 {
1199c4bf6374SJason M. Bills                     return;
1200c4bf6374SJason M. Bills                 }
12017e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
12027e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1203c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1204c4bf6374SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
1205c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1206029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1207c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1208c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1209c4bf6374SJason M. Bills                     "Collection of System Event Log Entries";
1210cb92c03bSAndrew Geissler 
12117e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
12127e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1213c4bf6374SJason M. Bills                 logEntryArray = nlohmann::json::array();
12147e860f15SJohn Edward Broadbent                 // Go through the log files and create a unique ID for each
12157e860f15SJohn Edward Broadbent                 // entry
121695820184SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
121795820184SJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1218b01bf299SEd Tanous                 uint64_t entryCount = 0;
1219cd225da8SJason M. Bills                 std::string logEntry;
122095820184SJason M. Bills 
12217e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12227e860f15SJohn Edward Broadbent                 // backwards
12237e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12247e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1225c4bf6374SJason M. Bills                 {
1226cd225da8SJason M. Bills                     std::ifstream logStream(*it);
122795820184SJason M. Bills                     if (!logStream.is_open())
1228c4bf6374SJason M. Bills                     {
1229c4bf6374SJason M. Bills                         continue;
1230c4bf6374SJason M. Bills                     }
1231c4bf6374SJason M. Bills 
1232e85d6b16SJason M. Bills                     // Reset the unique ID on the first entry
1233e85d6b16SJason M. Bills                     bool firstEntry = true;
123495820184SJason M. Bills                     while (std::getline(logStream, logEntry))
123595820184SJason M. Bills                     {
1236c4bf6374SJason M. Bills                         entryCount++;
12377e860f15SJohn Edward Broadbent                         // Handle paging using skip (number of entries to skip
12387e860f15SJohn Edward Broadbent                         // from the start) and top (number of entries to
12397e860f15SJohn Edward Broadbent                         // display)
1240c4bf6374SJason M. Bills                         if (entryCount <= skip || entryCount > skip + top)
1241c4bf6374SJason M. Bills                         {
1242c4bf6374SJason M. Bills                             continue;
1243c4bf6374SJason M. Bills                         }
1244c4bf6374SJason M. Bills 
1245c4bf6374SJason M. Bills                         std::string idStr;
1246e85d6b16SJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1247c4bf6374SJason M. Bills                         {
1248c4bf6374SJason M. Bills                             continue;
1249c4bf6374SJason M. Bills                         }
1250c4bf6374SJason M. Bills 
1251e85d6b16SJason M. Bills                         if (firstEntry)
1252e85d6b16SJason M. Bills                         {
1253e85d6b16SJason M. Bills                             firstEntry = false;
1254e85d6b16SJason M. Bills                         }
1255e85d6b16SJason M. Bills 
1256c4bf6374SJason M. Bills                         logEntryArray.push_back({});
1257c4bf6374SJason M. Bills                         nlohmann::json& bmcLogEntry = logEntryArray.back();
12587e860f15SJohn Edward Broadbent                         if (fillEventLogEntryJson(idStr, logEntry,
12597e860f15SJohn Edward Broadbent                                                   bmcLogEntry) != 0)
1260c4bf6374SJason M. Bills                         {
1261c4bf6374SJason M. Bills                             messages::internalError(asyncResp->res);
1262c4bf6374SJason M. Bills                             return;
1263c4bf6374SJason M. Bills                         }
1264c4bf6374SJason M. Bills                     }
126595820184SJason M. Bills                 }
1266c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1267c4bf6374SJason M. Bills                 if (skip + top < entryCount)
1268c4bf6374SJason M. Bills                 {
1269c4bf6374SJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
127095820184SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/EventLog/"
127195820184SJason M. Bills                         "Entries?$skip=" +
1272c4bf6374SJason M. Bills                         std::to_string(skip + top);
1273c4bf6374SJason M. Bills                 }
12747e860f15SJohn Edward Broadbent             });
1275897967deSJason M. Bills }
1276897967deSJason M. Bills 
12777e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1278897967deSJason M. Bills {
12797e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12807e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1281ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12837e860f15SJohn Edward Broadbent             [](const crow::Request&,
12847e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12857e860f15SJohn Edward Broadbent                const std::string& param) {
12867e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
12878d1b46d7Szhanghch05 
12887e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
12897e860f15SJohn Edward Broadbent                 // entry to find the target entry
1290897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1291897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1292897967deSJason M. Bills                 std::string logEntry;
1293897967deSJason M. Bills 
12947e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12957e860f15SJohn Edward Broadbent                 // backwards
12967e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12977e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1298897967deSJason M. Bills                 {
1299897967deSJason M. Bills                     std::ifstream logStream(*it);
1300897967deSJason M. Bills                     if (!logStream.is_open())
1301897967deSJason M. Bills                     {
1302897967deSJason M. Bills                         continue;
1303897967deSJason M. Bills                     }
1304897967deSJason M. Bills 
1305897967deSJason M. Bills                     // Reset the unique ID on the first entry
1306897967deSJason M. Bills                     bool firstEntry = true;
1307897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1308897967deSJason M. Bills                     {
1309897967deSJason M. Bills                         std::string idStr;
1310897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1311897967deSJason M. Bills                         {
1312897967deSJason M. Bills                             continue;
1313897967deSJason M. Bills                         }
1314897967deSJason M. Bills 
1315897967deSJason M. Bills                         if (firstEntry)
1316897967deSJason M. Bills                         {
1317897967deSJason M. Bills                             firstEntry = false;
1318897967deSJason M. Bills                         }
1319897967deSJason M. Bills 
1320897967deSJason M. Bills                         if (idStr == targetID)
1321897967deSJason M. Bills                         {
13227e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
13237e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1324897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1325897967deSJason M. Bills                             {
1326897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1327897967deSJason M. Bills                                 return;
1328897967deSJason M. Bills                             }
1329897967deSJason M. Bills                             return;
1330897967deSJason M. Bills                         }
1331897967deSJason M. Bills                     }
1332897967deSJason M. Bills                 }
1333897967deSJason M. Bills                 // Requested ID was not found
1334897967deSJason M. Bills                 messages::resourceMissingAtURI(asyncResp->res, targetID);
13357e860f15SJohn Edward Broadbent             });
133608a4e4b5SAnthony Wilson }
133708a4e4b5SAnthony Wilson 
13387e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
133908a4e4b5SAnthony Wilson {
13407e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13417e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1342ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
13437e860f15SJohn Edward Broadbent         .methods(
13447e860f15SJohn Edward Broadbent             boost::beast::http::verb::
13457e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
13467e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
13477e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
13487e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
134908a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
135008a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
135108a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
135208a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
135308a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
135408a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
135508a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
135608a4e4b5SAnthony Wilson 
1357cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1358cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1359cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1360cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1361cb92c03bSAndrew Geissler                             GetManagedObjectsType& resp) {
1362cb92c03bSAndrew Geissler                     if (ec)
1363cb92c03bSAndrew Geissler                     {
1364cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1365cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1366cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1367cb92c03bSAndrew Geissler                             << ec;
1368cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1369cb92c03bSAndrew Geissler                         return;
1370cb92c03bSAndrew Geissler                     }
1371cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1372cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1373cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
1374cb92c03bSAndrew Geissler                     for (auto& objectPath : resp)
1375cb92c03bSAndrew Geissler                     {
137666664f25SEd Tanous                         uint32_t* id = nullptr;
137766664f25SEd Tanous                         std::time_t timestamp{};
1378d139c236SGeorge Liu                         std::time_t updateTimestamp{};
137966664f25SEd Tanous                         std::string* severity = nullptr;
138066664f25SEd Tanous                         std::string* message = nullptr;
1381f86bb901SAdriana Kobylak                         std::string* filePath = nullptr;
138275710de2SXiaochao Ma                         bool resolved = false;
1383f86bb901SAdriana Kobylak                         for (auto& interfaceMap : objectPath.second)
1384f86bb901SAdriana Kobylak                         {
1385f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1386f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1387f86bb901SAdriana Kobylak                             {
1388cb92c03bSAndrew Geissler                                 for (auto& propertyMap : interfaceMap.second)
1389cb92c03bSAndrew Geissler                                 {
1390cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1391cb92c03bSAndrew Geissler                                     {
1392f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1393f86bb901SAdriana Kobylak                                             &propertyMap.second);
1394cb92c03bSAndrew Geissler                                     }
1395cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1396cb92c03bSAndrew Geissler                                     {
1397cb92c03bSAndrew Geissler                                         const uint64_t* millisTimeStamp =
1398f86bb901SAdriana Kobylak                                             std::get_if<uint64_t>(
1399f86bb901SAdriana Kobylak                                                 &propertyMap.second);
1400ae34c8e8SAdriana Kobylak                                         if (millisTimeStamp != nullptr)
1401ebd45906SGeorge Liu                                         {
14027e860f15SJohn Edward Broadbent                                             timestamp =
14037e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14047e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14057e860f15SJohn Edward Broadbent                                         }
14067e860f15SJohn Edward Broadbent                                     }
14077e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
14087e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
14097e860f15SJohn Edward Broadbent                                     {
14107e860f15SJohn Edward Broadbent                                         const uint64_t* millisTimeStamp =
14117e860f15SJohn Edward Broadbent                                             std::get_if<uint64_t>(
14127e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14137e860f15SJohn Edward Broadbent                                         if (millisTimeStamp != nullptr)
14147e860f15SJohn Edward Broadbent                                         {
14157e860f15SJohn Edward Broadbent                                             updateTimestamp =
14167e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14177e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14187e860f15SJohn Edward Broadbent                                         }
14197e860f15SJohn Edward Broadbent                                     }
14207e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
14217e860f15SJohn Edward Broadbent                                     {
14227e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
14237e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14247e860f15SJohn Edward Broadbent                                     }
14257e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
14267e860f15SJohn Edward Broadbent                                     {
14277e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
14287e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14297e860f15SJohn Edward Broadbent                                     }
14307e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
14317e860f15SJohn Edward Broadbent                                     {
14327e860f15SJohn Edward Broadbent                                         bool* resolveptr = std::get_if<bool>(
14337e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14347e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14357e860f15SJohn Edward Broadbent                                         {
14367e860f15SJohn Edward Broadbent                                             messages::internalError(
14377e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14387e860f15SJohn Edward Broadbent                                             return;
14397e860f15SJohn Edward Broadbent                                         }
14407e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14417e860f15SJohn Edward Broadbent                                     }
14427e860f15SJohn Edward Broadbent                                 }
14437e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14447e860f15SJohn Edward Broadbent                                     severity == nullptr)
14457e860f15SJohn Edward Broadbent                                 {
14467e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
14477e860f15SJohn Edward Broadbent                                     return;
14487e860f15SJohn Edward Broadbent                                 }
14497e860f15SJohn Edward Broadbent                             }
14507e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
14517e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
14527e860f15SJohn Edward Broadbent                             {
14537e860f15SJohn Edward Broadbent                                 for (auto& propertyMap : interfaceMap.second)
14547e860f15SJohn Edward Broadbent                                 {
14557e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
14567e860f15SJohn Edward Broadbent                                     {
14577e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
14587e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14597e860f15SJohn Edward Broadbent                                     }
14607e860f15SJohn Edward Broadbent                                 }
14617e860f15SJohn Edward Broadbent                             }
14627e860f15SJohn Edward Broadbent                         }
14637e860f15SJohn Edward Broadbent                         // Object path without the
14647e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
14657e860f15SJohn Edward Broadbent                         // and continue.
14667e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
14677e860f15SJohn Edward Broadbent                             severity == nullptr)
14687e860f15SJohn Edward Broadbent                         {
14697e860f15SJohn Edward Broadbent                             continue;
14707e860f15SJohn Edward Broadbent                         }
14717e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
14727e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
14737e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14747e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
14757e860f15SJohn Edward Broadbent                             "/redfish/v1/Systems/system/"
14767e860f15SJohn Edward Broadbent                             "LogServices/EventLog/Entries/" +
14777e860f15SJohn Edward Broadbent                             std::to_string(*id);
14787e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
14797e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
14807e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
14817e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
14827e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
14837e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
14847e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
14857e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
14867e860f15SJohn Edward Broadbent                             crow::utility::getDateTime(timestamp);
14877e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
14887e860f15SJohn Edward Broadbent                             crow::utility::getDateTime(updateTimestamp);
14897e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
14907e860f15SJohn Edward Broadbent                         {
14917e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
14927e860f15SJohn Edward Broadbent                                 "/redfish/v1/Systems/system/LogServices/"
14937e860f15SJohn Edward Broadbent                                 "EventLog/"
14947e860f15SJohn Edward Broadbent                                 "Entries/" +
14957e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
14967e860f15SJohn Edward Broadbent                         }
14977e860f15SJohn Edward Broadbent                     }
14987e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
14997e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
15007e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
15017e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
15027e860f15SJohn Edward Broadbent                               });
15037e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
15047e860f15SJohn Edward Broadbent                         entriesArray.size();
15057e860f15SJohn Edward Broadbent                 },
15067e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
15077e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15087e860f15SJohn Edward Broadbent         });
15097e860f15SJohn Edward Broadbent }
15107e860f15SJohn Edward Broadbent 
15117e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
15127e860f15SJohn Edward Broadbent {
15137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15147e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1515ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
15167e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
15177e860f15SJohn Edward Broadbent             [](const crow::Request&,
15187e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15197e860f15SJohn Edward Broadbent                const std::string& param)
15207e860f15SJohn Edward Broadbent 
15217e860f15SJohn Edward Broadbent             {
15227e860f15SJohn Edward Broadbent                 std::string entryID = param;
15237e860f15SJohn Edward Broadbent                 dbus::utility::escapePathForDbus(entryID);
15247e860f15SJohn Edward Broadbent 
15257e860f15SJohn Edward Broadbent                 // DBus implementation of EventLog/Entries
15267e860f15SJohn Edward Broadbent                 // Make call to Logging Service to find all log entry objects
15277e860f15SJohn Edward Broadbent                 crow::connections::systemBus->async_method_call(
15287e860f15SJohn Edward Broadbent                     [asyncResp, entryID](const boost::system::error_code ec,
15297e860f15SJohn Edward Broadbent                                          GetManagedPropertyType& resp) {
15307e860f15SJohn Edward Broadbent                         if (ec.value() == EBADR)
15317e860f15SJohn Edward Broadbent                         {
15327e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
15337e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogEntry", entryID);
15347e860f15SJohn Edward Broadbent                             return;
15357e860f15SJohn Edward Broadbent                         }
15367e860f15SJohn Edward Broadbent                         if (ec)
15377e860f15SJohn Edward Broadbent                         {
15387e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "EventLogEntry (DBus) "
15397e860f15SJohn Edward Broadbent                                                 "resp_handler got error "
15407e860f15SJohn Edward Broadbent                                              << ec;
15417e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
15427e860f15SJohn Edward Broadbent                             return;
15437e860f15SJohn Edward Broadbent                         }
15447e860f15SJohn Edward Broadbent                         uint32_t* id = nullptr;
15457e860f15SJohn Edward Broadbent                         std::time_t timestamp{};
15467e860f15SJohn Edward Broadbent                         std::time_t updateTimestamp{};
15477e860f15SJohn Edward Broadbent                         std::string* severity = nullptr;
15487e860f15SJohn Edward Broadbent                         std::string* message = nullptr;
15497e860f15SJohn Edward Broadbent                         std::string* filePath = nullptr;
15507e860f15SJohn Edward Broadbent                         bool resolved = false;
15517e860f15SJohn Edward Broadbent 
15527e860f15SJohn Edward Broadbent                         for (auto& propertyMap : resp)
15537e860f15SJohn Edward Broadbent                         {
15547e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Id")
15557e860f15SJohn Edward Broadbent                             {
15567e860f15SJohn Edward Broadbent                                 id = std::get_if<uint32_t>(&propertyMap.second);
15577e860f15SJohn Edward Broadbent                             }
15587e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Timestamp")
15597e860f15SJohn Edward Broadbent                             {
15607e860f15SJohn Edward Broadbent                                 const uint64_t* millisTimeStamp =
15617e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
15627e860f15SJohn Edward Broadbent                                 if (millisTimeStamp != nullptr)
15637e860f15SJohn Edward Broadbent                                 {
1564d139c236SGeorge Liu                                     timestamp = crow::utility::getTimestamp(
1565cb92c03bSAndrew Geissler                                         *millisTimeStamp);
1566d139c236SGeorge Liu                                 }
1567ebd45906SGeorge Liu                             }
1568d139c236SGeorge Liu                             else if (propertyMap.first == "UpdateTimestamp")
1569d139c236SGeorge Liu                             {
1570d139c236SGeorge Liu                                 const uint64_t* millisTimeStamp =
15717e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1572ae34c8e8SAdriana Kobylak                                 if (millisTimeStamp != nullptr)
1573ebd45906SGeorge Liu                                 {
1574ebd45906SGeorge Liu                                     updateTimestamp =
1575ebd45906SGeorge Liu                                         crow::utility::getTimestamp(
1576d139c236SGeorge Liu                                             *millisTimeStamp);
1577cb92c03bSAndrew Geissler                                 }
1578ebd45906SGeorge Liu                             }
1579cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1580cb92c03bSAndrew Geissler                             {
1581cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1582cb92c03bSAndrew Geissler                                     &propertyMap.second);
1583cb92c03bSAndrew Geissler                             }
1584cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1585cb92c03bSAndrew Geissler                             {
1586cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1587cb92c03bSAndrew Geissler                                     &propertyMap.second);
1588ae34c8e8SAdriana Kobylak                             }
158975710de2SXiaochao Ma                             else if (propertyMap.first == "Resolved")
159075710de2SXiaochao Ma                             {
159175710de2SXiaochao Ma                                 bool* resolveptr =
159275710de2SXiaochao Ma                                     std::get_if<bool>(&propertyMap.second);
159375710de2SXiaochao Ma                                 if (resolveptr == nullptr)
159475710de2SXiaochao Ma                                 {
159575710de2SXiaochao Ma                                     messages::internalError(asyncResp->res);
159675710de2SXiaochao Ma                                     return;
159775710de2SXiaochao Ma                                 }
159875710de2SXiaochao Ma                                 resolved = *resolveptr;
159975710de2SXiaochao Ma                             }
16007e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Path")
1601f86bb901SAdriana Kobylak                             {
1602f86bb901SAdriana Kobylak                                 filePath = std::get_if<std::string>(
1603f86bb901SAdriana Kobylak                                     &propertyMap.second);
1604f86bb901SAdriana Kobylak                             }
1605f86bb901SAdriana Kobylak                         }
1606f86bb901SAdriana Kobylak                         if (id == nullptr || message == nullptr ||
1607f86bb901SAdriana Kobylak                             severity == nullptr)
1608f86bb901SAdriana Kobylak                         {
1609ae34c8e8SAdriana Kobylak                             messages::internalError(asyncResp->res);
1610271584abSEd Tanous                             return;
1611271584abSEd Tanous                         }
1612f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.type"] =
1613f86bb901SAdriana Kobylak                             "#LogEntry.v1_8_0.LogEntry";
1614f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.id"] =
16157e860f15SJohn Edward Broadbent                             "/redfish/v1/Systems/system/LogServices/EventLog/"
16167e860f15SJohn Edward Broadbent                             "Entries/" +
1617f86bb901SAdriana Kobylak                             std::to_string(*id);
16187e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Name"] =
16197e860f15SJohn Edward Broadbent                             "System Event Log Entry";
1620f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1621f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Message"] = *message;
1622f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Resolved"] = resolved;
1623f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["EntryType"] = "Event";
1624f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Severity"] =
1625f86bb901SAdriana Kobylak                             translateSeverityDbusToRedfish(*severity);
1626f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Created"] =
1627f86bb901SAdriana Kobylak                             crow::utility::getDateTime(timestamp);
1628f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Modified"] =
1629f86bb901SAdriana Kobylak                             crow::utility::getDateTime(updateTimestamp);
1630f86bb901SAdriana Kobylak                         if (filePath != nullptr)
1631f86bb901SAdriana Kobylak                         {
1632f86bb901SAdriana Kobylak                             asyncResp->res.jsonValue["AdditionalDataURI"] =
16337e860f15SJohn Edward Broadbent                                 "/redfish/v1/Systems/system/LogServices/"
16347e860f15SJohn Edward Broadbent                                 "EventLog/"
16357e860f15SJohn Edward Broadbent                                 "attachment/" +
16367e860f15SJohn Edward Broadbent                                 std::to_string(*id);
1637f86bb901SAdriana Kobylak                         }
1638cb92c03bSAndrew Geissler                     },
1639cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Logging",
1640cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging/entry/" + entryID,
1641f86bb901SAdriana Kobylak                     "org.freedesktop.DBus.Properties", "GetAll", "");
16427e860f15SJohn Edward Broadbent             });
1643336e96c6SChicago Duan 
16447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16457e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1646ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
16487e860f15SJohn Edward Broadbent             [](const crow::Request& req,
16497e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16507e860f15SJohn Edward Broadbent                const std::string& entryId) {
165175710de2SXiaochao Ma                 std::optional<bool> resolved;
165275710de2SXiaochao Ma 
16537e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "Resolved",
16547e860f15SJohn Edward Broadbent                                          resolved))
165575710de2SXiaochao Ma                 {
165675710de2SXiaochao Ma                     return;
165775710de2SXiaochao Ma                 }
165875710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
165975710de2SXiaochao Ma 
166075710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
16614f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
166275710de2SXiaochao Ma                         if (ec)
166375710de2SXiaochao Ma                         {
166475710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
166575710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
166675710de2SXiaochao Ma                             return;
166775710de2SXiaochao Ma                         }
166875710de2SXiaochao Ma                     },
166975710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
167075710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
167175710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
167275710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
167375710de2SXiaochao Ma                     std::variant<bool>(*resolved));
16747e860f15SJohn Edward Broadbent             });
167575710de2SXiaochao Ma 
16767e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16777e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1678ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1679ed398213SEd Tanous 
16807e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
16817e860f15SJohn Edward Broadbent             [](const crow::Request&,
16827e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16837e860f15SJohn Edward Broadbent                const std::string& param)
16847e860f15SJohn Edward Broadbent 
1685336e96c6SChicago Duan             {
1686336e96c6SChicago Duan                 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1687336e96c6SChicago Duan 
16887e860f15SJohn Edward Broadbent                 std::string entryID = param;
1689336e96c6SChicago Duan 
1690336e96c6SChicago Duan                 dbus::utility::escapePathForDbus(entryID);
1691336e96c6SChicago Duan 
1692336e96c6SChicago Duan                 // Process response from Logging service.
16937e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp, entryID](
16947e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
16957e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
16967e860f15SJohn Edward Broadbent                         << "EventLogEntry (DBus) doDelete callback: Done";
1697336e96c6SChicago Duan                     if (ec)
1698336e96c6SChicago Duan                     {
16993de8d8baSGeorge Liu                         if (ec.value() == EBADR)
17003de8d8baSGeorge Liu                         {
17017e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
17027e860f15SJohn Edward Broadbent                                                        "LogEntry", entryID);
17033de8d8baSGeorge Liu                             return;
17043de8d8baSGeorge Liu                         }
1705336e96c6SChicago Duan                         // TODO Handle for specific error code
17067e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "EventLogEntry (DBus) doDelete "
17077e860f15SJohn Edward Broadbent                                             "respHandler got error "
1708336e96c6SChicago Duan                                          << ec;
1709336e96c6SChicago Duan                         asyncResp->res.result(
1710336e96c6SChicago Duan                             boost::beast::http::status::internal_server_error);
1711336e96c6SChicago Duan                         return;
1712336e96c6SChicago Duan                     }
1713336e96c6SChicago Duan 
1714336e96c6SChicago Duan                     asyncResp->res.result(boost::beast::http::status::ok);
1715336e96c6SChicago Duan                 };
1716336e96c6SChicago Duan 
1717336e96c6SChicago Duan                 // Make call to Logging service to request Delete Log
1718336e96c6SChicago Duan                 crow::connections::systemBus->async_method_call(
1719336e96c6SChicago Duan                     respHandler, "xyz.openbmc_project.Logging",
1720336e96c6SChicago Duan                     "/xyz/openbmc_project/logging/entry/" + entryID,
1721336e96c6SChicago Duan                     "xyz.openbmc_project.Object.Delete", "Delete");
17227e860f15SJohn Edward Broadbent             });
1723400fd1fbSAdriana Kobylak }
1724400fd1fbSAdriana Kobylak 
17257e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1726400fd1fbSAdriana Kobylak {
17277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/"
17287e860f15SJohn Edward Broadbent                       "<str>/attachment")
1729ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17307e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
17317e860f15SJohn Edward Broadbent             [](const crow::Request& req,
17327e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17337e860f15SJohn Edward Broadbent                const std::string& param)
1734400fd1fbSAdriana Kobylak 
17357e860f15SJohn Edward Broadbent             {
1736753d034dSEd Tanous                 std::string_view acceptHeader = req.getHeaderValue("Accept");
1737753d034dSEd Tanous                 // The iterators in boost/http/rfc7230.hpp end the string if '/'
1738753d034dSEd Tanous                 // is found, so replace it with arbitrary character '|' which is
1739753d034dSEd Tanous                 // not part of the Accept header syntax.
1740753d034dSEd Tanous                 std::string acceptStr = boost::replace_all_copy(
1741753d034dSEd Tanous                     std::string(acceptHeader), "/", "|");
1742753d034dSEd Tanous                 boost::beast::http::ext_list acceptTypes{acceptStr};
1743753d034dSEd Tanous                 bool supported = false;
1744753d034dSEd Tanous                 for (const auto& type : acceptTypes)
1745753d034dSEd Tanous                 {
1746753d034dSEd Tanous                     if ((type.first == "*|*") ||
1747753d034dSEd Tanous                         (type.first == "application|octet-stream"))
1748753d034dSEd Tanous                     {
1749753d034dSEd Tanous                         supported = true;
1750753d034dSEd Tanous                         break;
1751753d034dSEd Tanous                     }
1752753d034dSEd Tanous                 }
1753753d034dSEd Tanous                 if (!supported)
1754400fd1fbSAdriana Kobylak                 {
17557e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17567e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1757400fd1fbSAdriana Kobylak                     return;
1758400fd1fbSAdriana Kobylak                 }
1759400fd1fbSAdriana Kobylak 
17607e860f15SJohn Edward Broadbent                 std::string entryID = param;
1761400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1762400fd1fbSAdriana Kobylak 
1763400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17647e860f15SJohn Edward Broadbent                     [asyncResp,
17657e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1766400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1767400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1768400fd1fbSAdriana Kobylak                         {
17697e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
17707e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1771400fd1fbSAdriana Kobylak                             return;
1772400fd1fbSAdriana Kobylak                         }
1773400fd1fbSAdriana Kobylak                         if (ec)
1774400fd1fbSAdriana Kobylak                         {
1775400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1776400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1777400fd1fbSAdriana Kobylak                             return;
1778400fd1fbSAdriana Kobylak                         }
1779400fd1fbSAdriana Kobylak 
1780400fd1fbSAdriana Kobylak                         int fd = -1;
1781400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1782400fd1fbSAdriana Kobylak                         if (fd == -1)
1783400fd1fbSAdriana Kobylak                         {
1784400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1785400fd1fbSAdriana Kobylak                             return;
1786400fd1fbSAdriana Kobylak                         }
1787400fd1fbSAdriana Kobylak 
1788400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1789400fd1fbSAdriana Kobylak                         if (size == -1)
1790400fd1fbSAdriana Kobylak                         {
1791400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1792400fd1fbSAdriana Kobylak                             return;
1793400fd1fbSAdriana Kobylak                         }
1794400fd1fbSAdriana Kobylak 
1795400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1796400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1797400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1798400fd1fbSAdriana Kobylak                         {
1799400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1800400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1801400fd1fbSAdriana Kobylak                                 << maxFileSize;
1802400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1803400fd1fbSAdriana Kobylak                             return;
1804400fd1fbSAdriana Kobylak                         }
1805400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1806400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1807400fd1fbSAdriana Kobylak                         if (rc == -1)
1808400fd1fbSAdriana Kobylak                         {
1809400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1810400fd1fbSAdriana Kobylak                             return;
1811400fd1fbSAdriana Kobylak                         }
1812400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1813400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1814400fd1fbSAdriana Kobylak                         {
1815400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1816400fd1fbSAdriana Kobylak                             return;
1817400fd1fbSAdriana Kobylak                         }
1818400fd1fbSAdriana Kobylak                         close(fd);
1819400fd1fbSAdriana Kobylak 
1820400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
18217e860f15SJohn Edward Broadbent                         std::string output =
18227e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1823400fd1fbSAdriana Kobylak 
1824400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1825400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
18267e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
18277e860f15SJohn Edward Broadbent                                                  "Base64");
1828400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1829400fd1fbSAdriana Kobylak                     },
1830400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1831400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1832400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
18337e860f15SJohn Edward Broadbent             });
18341da66f75SEd Tanous }
18351da66f75SEd Tanous 
18367e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
18371da66f75SEd Tanous {
18387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
1839ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
18407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
18417e860f15SJohn Edward Broadbent             [](const crow::Request&,
18427e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
18437e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
18447e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1845e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
18461da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
1847e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1848e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
18497e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
18507e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
1851e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
18521da66f75SEd Tanous                     "Collection of LogServices for this Manager";
18537e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
18547e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1855c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
18565cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
18575cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
18587e860f15SJohn Edward Broadbent                     {{"@odata.id",
18597e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
18605cb1dd27SAsmitha Karunanithi #endif
1861c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1862c4bf6374SJason M. Bills                 logServiceArray.push_back(
18637e860f15SJohn Edward Broadbent                     {{"@odata.id",
18647e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1865c4bf6374SJason M. Bills #endif
1866e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
1867c4bf6374SJason M. Bills                     logServiceArray.size();
18687e860f15SJohn Edward Broadbent             });
1869e1f26343SJason M. Bills }
1870e1f26343SJason M. Bills 
18717e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
1872e1f26343SJason M. Bills {
18737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1874ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
18757e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
18767e860f15SJohn Edward Broadbent             [](const crow::Request&,
18777e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
18788d1b46d7Szhanghch05 
18797e860f15SJohn Edward Broadbent             {
1880e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1881e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
18820f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
18830f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
18847e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
18857e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
18867e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
18877e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
1888c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
1889e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
18907c8c4058STejas Patil 
18917c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
18927c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
18937c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
18947c8c4058STejas Patil                     redfishDateTimeOffset.first;
18957c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
18967c8c4058STejas Patil                     redfishDateTimeOffset.second;
18977c8c4058STejas Patil 
1898cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
1899cd50aa42SJason M. Bills                     {"@odata.id",
1900086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
19017e860f15SJohn Edward Broadbent             });
1902e1f26343SJason M. Bills }
1903e1f26343SJason M. Bills 
1904c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1905e1f26343SJason M. Bills                                       sd_journal* journal,
1906c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
1907e1f26343SJason M. Bills {
1908e1f26343SJason M. Bills     // Get the Log Entry contents
1909e1f26343SJason M. Bills     int ret = 0;
1910e1f26343SJason M. Bills 
1911a8fe54f0SJason M. Bills     std::string message;
1912a8fe54f0SJason M. Bills     std::string_view syslogID;
1913a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1914a8fe54f0SJason M. Bills     if (ret < 0)
1915a8fe54f0SJason M. Bills     {
1916a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1917a8fe54f0SJason M. Bills                          << strerror(-ret);
1918a8fe54f0SJason M. Bills     }
1919a8fe54f0SJason M. Bills     if (!syslogID.empty())
1920a8fe54f0SJason M. Bills     {
1921a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
1922a8fe54f0SJason M. Bills     }
1923a8fe54f0SJason M. Bills 
192439e77504SEd Tanous     std::string_view msg;
192516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1926e1f26343SJason M. Bills     if (ret < 0)
1927e1f26343SJason M. Bills     {
1928e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1929e1f26343SJason M. Bills         return 1;
1930e1f26343SJason M. Bills     }
1931a8fe54f0SJason M. Bills     message += std::string(msg);
1932e1f26343SJason M. Bills 
1933e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1934271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
193516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1936e1f26343SJason M. Bills     if (ret < 0)
1937e1f26343SJason M. Bills     {
1938e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1939e1f26343SJason M. Bills     }
1940e1f26343SJason M. Bills 
1941e1f26343SJason M. Bills     // Get the Created time from the timestamp
194216428a1aSJason M. Bills     std::string entryTimeStr;
194316428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1944e1f26343SJason M. Bills     {
194516428a1aSJason M. Bills         return 1;
1946e1f26343SJason M. Bills     }
1947e1f26343SJason M. Bills 
1948e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1949c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1950d0dbeefdSEd Tanous         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1951c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1952c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1953e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1954c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
1955a8fe54f0SJason M. Bills         {"Message", std::move(message)},
1956e1f26343SJason M. Bills         {"EntryType", "Oem"},
1957738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
1958738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
1959738c1e61SPatrick Williams                                      : "OK"},
1960086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
1961e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1962e1f26343SJason M. Bills     return 0;
1963e1f26343SJason M. Bills }
1964e1f26343SJason M. Bills 
19657e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
1966e1f26343SJason M. Bills {
19677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1968ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
19697e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
19707e860f15SJohn Edward Broadbent             [](const crow::Request& req,
19717e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1972193ad2faSJason M. Bills                 static constexpr const long maxEntriesPerPage = 1000;
1973271584abSEd Tanous                 uint64_t skip = 0;
1974271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
19758d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1976193ad2faSJason M. Bills                 {
1977193ad2faSJason M. Bills                     return;
1978193ad2faSJason M. Bills                 }
19798d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1980193ad2faSJason M. Bills                 {
1981193ad2faSJason M. Bills                     return;
1982193ad2faSJason M. Bills                 }
19837e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
19847e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1985e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1986e1f26343SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
19870f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
19880f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1989e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1990e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1991e1f26343SJason M. Bills                     "Collection of BMC Journal Entries";
19927e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
19937e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1994e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
1995e1f26343SJason M. Bills 
19967e860f15SJohn Edward Broadbent                 // Go through the journal and use the timestamp to create a
19977e860f15SJohn Edward Broadbent                 // unique ID for each entry
1998e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
1999e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2000e1f26343SJason M. Bills                 if (ret < 0)
2001e1f26343SJason M. Bills                 {
20027e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
20037e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2004f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2005e1f26343SJason M. Bills                     return;
2006e1f26343SJason M. Bills                 }
20077e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
20087e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2009e1f26343SJason M. Bills                 journalTmp = nullptr;
2010b01bf299SEd Tanous                 uint64_t entryCount = 0;
2011e85d6b16SJason M. Bills                 // Reset the unique ID on the first entry
2012e85d6b16SJason M. Bills                 bool firstEntry = true;
2013e1f26343SJason M. Bills                 SD_JOURNAL_FOREACH(journal.get())
2014e1f26343SJason M. Bills                 {
2015193ad2faSJason M. Bills                     entryCount++;
20167e860f15SJohn Edward Broadbent                     // Handle paging using skip (number of entries to skip from
20177e860f15SJohn Edward Broadbent                     // the start) and top (number of entries to display)
2018193ad2faSJason M. Bills                     if (entryCount <= skip || entryCount > skip + top)
2019193ad2faSJason M. Bills                     {
2020193ad2faSJason M. Bills                         continue;
2021193ad2faSJason M. Bills                     }
2022193ad2faSJason M. Bills 
202316428a1aSJason M. Bills                     std::string idStr;
2024e85d6b16SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2025e1f26343SJason M. Bills                     {
2026e1f26343SJason M. Bills                         continue;
2027e1f26343SJason M. Bills                     }
2028e1f26343SJason M. Bills 
2029e85d6b16SJason M. Bills                     if (firstEntry)
2030e85d6b16SJason M. Bills                     {
2031e85d6b16SJason M. Bills                         firstEntry = false;
2032e85d6b16SJason M. Bills                     }
2033e85d6b16SJason M. Bills 
2034e1f26343SJason M. Bills                     logEntryArray.push_back({});
2035c4bf6374SJason M. Bills                     nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2036c4bf6374SJason M. Bills                     if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2037c4bf6374SJason M. Bills                                                    bmcJournalLogEntry) != 0)
2038e1f26343SJason M. Bills                     {
2039f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2040e1f26343SJason M. Bills                         return;
2041e1f26343SJason M. Bills                     }
2042e1f26343SJason M. Bills                 }
2043193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2044193ad2faSJason M. Bills                 if (skip + top < entryCount)
2045193ad2faSJason M. Bills                 {
2046193ad2faSJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
20477e860f15SJohn Edward Broadbent                         "/redfish/v1/Managers/bmc/LogServices/Journal/"
20487e860f15SJohn Edward Broadbent                         "Entries?$skip=" +
2049193ad2faSJason M. Bills                         std::to_string(skip + top);
2050193ad2faSJason M. Bills                 }
20517e860f15SJohn Edward Broadbent             });
2052e1f26343SJason M. Bills }
2053e1f26343SJason M. Bills 
20547e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2055e1f26343SJason M. Bills {
20567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
20577e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2058ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
20597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
20607e860f15SJohn Edward Broadbent             [](const crow::Request&,
20617e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20627e860f15SJohn Edward Broadbent                const std::string& entryID) {
2063e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2064e1f26343SJason M. Bills                 uint64_t ts = 0;
2065271584abSEd Tanous                 uint64_t index = 0;
20668d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2067e1f26343SJason M. Bills                 {
206816428a1aSJason M. Bills                     return;
2069e1f26343SJason M. Bills                 }
2070e1f26343SJason M. Bills 
2071e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2072e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2073e1f26343SJason M. Bills                 if (ret < 0)
2074e1f26343SJason M. Bills                 {
20757e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
20767e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2077f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2078e1f26343SJason M. Bills                     return;
2079e1f26343SJason M. Bills                 }
20807e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
20817e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2082e1f26343SJason M. Bills                 journalTmp = nullptr;
20837e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
20847e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2085af07e3f5SJason M. Bills                 std::string idStr;
2086af07e3f5SJason M. Bills                 bool firstEntry = true;
2087e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
20882056b6d1SManojkiran Eda                 if (ret < 0)
20892056b6d1SManojkiran Eda                 {
20902056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
20912056b6d1SManojkiran Eda                                      << strerror(-ret);
20922056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
20932056b6d1SManojkiran Eda                     return;
20942056b6d1SManojkiran Eda                 }
2095271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2096e1f26343SJason M. Bills                 {
2097e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2098af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2099af07e3f5SJason M. Bills                     {
2100af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2101af07e3f5SJason M. Bills                         return;
2102af07e3f5SJason M. Bills                     }
2103af07e3f5SJason M. Bills                     if (firstEntry)
2104af07e3f5SJason M. Bills                     {
2105af07e3f5SJason M. Bills                         firstEntry = false;
2106af07e3f5SJason M. Bills                     }
2107e1f26343SJason M. Bills                 }
2108c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2109af07e3f5SJason M. Bills                 if (idStr != entryID)
2110c4bf6374SJason M. Bills                 {
2111c4bf6374SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, entryID);
2112c4bf6374SJason M. Bills                     return;
2113c4bf6374SJason M. Bills                 }
2114c4bf6374SJason M. Bills 
2115c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2116e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2117e1f26343SJason M. Bills                 {
2118f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2119e1f26343SJason M. Bills                     return;
2120e1f26343SJason M. Bills                 }
21217e860f15SJohn Edward Broadbent             });
2122c9bb6861Sraviteja-b }
2123c9bb6861Sraviteja-b 
21247e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2125c9bb6861Sraviteja-b {
21267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2127ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
21287e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21297e860f15SJohn Edward Broadbent             [](const crow::Request&,
21307e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2131c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
21325cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2133c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2134d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
2135c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
21365cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
21375cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
2138c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
21397c8c4058STejas Patil 
21407c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
21417c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
21427c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
21437c8c4058STejas Patil                     redfishDateTimeOffset.first;
21447c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21457c8c4058STejas Patil                     redfishDateTimeOffset.second;
21467c8c4058STejas Patil 
2147c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Entries"] = {
21487e860f15SJohn Edward Broadbent                     {"@odata.id",
21497e860f15SJohn Edward Broadbent                      "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
21505cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
21515cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
21525cb1dd27SAsmitha Karunanithi                      {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
21535cb1dd27SAsmitha Karunanithi                                  "Actions/LogService.ClearLog"}}},
2154d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
2155d337bb72SAsmitha Karunanithi                      {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2156d337bb72SAsmitha Karunanithi                                  "Actions/LogService.CollectDiagnosticData"}}}};
21577e860f15SJohn Edward Broadbent             });
2158c9bb6861Sraviteja-b }
2159c9bb6861Sraviteja-b 
21607e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
21617e860f15SJohn Edward Broadbent {
21627e860f15SJohn Edward Broadbent 
2163c9bb6861Sraviteja-b     /**
2164c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2165c9bb6861Sraviteja-b      */
21667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2167ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
21687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21697e860f15SJohn Edward Broadbent             [](const crow::Request&,
21707e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2171c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2172c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2173c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
21745cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
21755cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2176c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
21775cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2178c9bb6861Sraviteja-b 
21795cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
21807e860f15SJohn Edward Broadbent             });
2181c9bb6861Sraviteja-b }
2182c9bb6861Sraviteja-b 
21837e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2184c9bb6861Sraviteja-b {
21857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
21867e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2187ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
21887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21897e860f15SJohn Edward Broadbent             [](const crow::Request&,
21907e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21917e860f15SJohn Edward Broadbent                const std::string& param) {
21927e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "BMC");
21937e860f15SJohn Edward Broadbent             });
21947e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
21957e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2196ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
21977e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
21987e860f15SJohn Edward Broadbent             [](const crow::Request&,
21997e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22007e860f15SJohn Edward Broadbent                const std::string& param) {
22017e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
22027e860f15SJohn Edward Broadbent             });
2203c9bb6861Sraviteja-b }
2204c9bb6861Sraviteja-b 
22057e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2206c9bb6861Sraviteja-b {
22078d1b46d7Szhanghch05 
22087e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2209d337bb72SAsmitha Karunanithi                       "Actions/"
2210d337bb72SAsmitha Karunanithi                       "LogService.CollectDiagnosticData/")
2211ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
22127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
22137e860f15SJohn Edward Broadbent             [](const crow::Request& req,
22147e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22158d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
22167e860f15SJohn Edward Broadbent             });
2217a43be80fSAsmitha Karunanithi }
2218a43be80fSAsmitha Karunanithi 
22197e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
222080319af1SAsmitha Karunanithi {
22217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
222280319af1SAsmitha Karunanithi                       "Actions/"
222380319af1SAsmitha Karunanithi                       "LogService.ClearLog/")
2224ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
22257e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
22267e860f15SJohn Edward Broadbent             [](const crow::Request&,
22277e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22288d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
22297e860f15SJohn Edward Broadbent             });
22305cb1dd27SAsmitha Karunanithi }
22315cb1dd27SAsmitha Karunanithi 
22327e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
22335cb1dd27SAsmitha Karunanithi {
22347e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2235ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
22367e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
22377e860f15SJohn Edward Broadbent             [](const crow::Request&,
22387e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22395cb1dd27SAsmitha Karunanithi 
22407e860f15SJohn Edward Broadbent             {
22415cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
22425cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump";
22435cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
2244d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
22455cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
22467e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
22477e860f15SJohn Edward Broadbent                     "System Dump LogService";
22485cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
22495cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
22507c8c4058STejas Patil 
22517c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
22527c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
22537c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
22547c8c4058STejas Patil                     redfishDateTimeOffset.first;
22557c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
22567c8c4058STejas Patil                     redfishDateTimeOffset.second;
22577c8c4058STejas Patil 
22585cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Entries"] = {
22595cb1dd27SAsmitha Karunanithi                     {"@odata.id",
22605cb1dd27SAsmitha Karunanithi                      "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
22615cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
22625cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
22637e860f15SJohn Edward Broadbent                      {{"target",
22647e860f15SJohn Edward Broadbent                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
22655cb1dd27SAsmitha Karunanithi                        "LogService.ClearLog"}}},
2266d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
22677e860f15SJohn Edward Broadbent                      {{"target",
22687e860f15SJohn Edward Broadbent                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2269d337bb72SAsmitha Karunanithi                        "LogService.CollectDiagnosticData"}}}};
22707e860f15SJohn Edward Broadbent             });
22715cb1dd27SAsmitha Karunanithi }
22725cb1dd27SAsmitha Karunanithi 
22737e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
22747e860f15SJohn Edward Broadbent {
22757e860f15SJohn Edward Broadbent 
22765cb1dd27SAsmitha Karunanithi     /**
22775cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
22785cb1dd27SAsmitha Karunanithi      */
2279b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2280ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
22817e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
22827e860f15SJohn Edward Broadbent             [](const crow::Request&,
2283864d6a17SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22845cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
22855cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
22865cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
22875cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
22885cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
22895cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
22905cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
22915cb1dd27SAsmitha Karunanithi 
22925cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
22937e860f15SJohn Edward Broadbent             });
22945cb1dd27SAsmitha Karunanithi }
22955cb1dd27SAsmitha Karunanithi 
22967e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
22975cb1dd27SAsmitha Karunanithi {
22987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2299864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2300ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2301ed398213SEd Tanous 
23027e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23037e860f15SJohn Edward Broadbent             [](const crow::Request&,
23047e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23057e860f15SJohn Edward Broadbent                const std::string& param) {
23067e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "System");
23077e860f15SJohn Edward Broadbent             });
23088d1b46d7Szhanghch05 
23097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2310864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2311ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
23127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
23137e860f15SJohn Edward Broadbent             [](const crow::Request&,
23147e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23157e860f15SJohn Edward Broadbent                const std::string& param) {
23167e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
23177e860f15SJohn Edward Broadbent             });
23185cb1dd27SAsmitha Karunanithi }
2319c9bb6861Sraviteja-b 
23207e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2321c9bb6861Sraviteja-b {
23227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2323d337bb72SAsmitha Karunanithi                       "Actions/"
2324d337bb72SAsmitha Karunanithi                       "LogService.CollectDiagnosticData/")
2325ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
23267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
23277e860f15SJohn Edward Broadbent             [](const crow::Request& req,
23287e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23297e860f15SJohn Edward Broadbent 
23307e860f15SJohn Edward Broadbent             { createDump(asyncResp, req, "System"); });
2331a43be80fSAsmitha Karunanithi }
2332a43be80fSAsmitha Karunanithi 
23337e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2334a43be80fSAsmitha Karunanithi {
23357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2336013487e5Sraviteja-b                       "Actions/"
2337013487e5Sraviteja-b                       "LogService.ClearLog/")
2338ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
23397e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
23407e860f15SJohn Edward Broadbent             [](const crow::Request&,
23417e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23427e860f15SJohn Edward Broadbent 
23437e860f15SJohn Edward Broadbent             { clearDump(asyncResp, "System"); });
2344013487e5Sraviteja-b }
2345013487e5Sraviteja-b 
23467e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
23471da66f75SEd Tanous {
23483946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
23493946028dSAppaRao Puli     // method for security reasons.
23501da66f75SEd Tanous     /**
23511da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
23521da66f75SEd Tanous      */
23537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2354ed398213SEd Tanous         // This is incorrect, should be:
2355ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2356432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
23577e860f15SJohn Edward Broadbent         .methods(
23587e860f15SJohn Edward Broadbent             boost::beast::http::verb::
23597e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
23607e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23617e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
23627e860f15SJohn Edward Broadbent             // SubRoute
23630f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2364424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2365e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
23668e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
23674f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
23684f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
23694f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2370e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2371e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
23727c8c4058STejas Patil 
23737c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
23747c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
23757c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23767c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23777c8c4058STejas Patil                 redfishDateTimeOffset.second;
23787c8c4058STejas Patil 
2379cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2380cd50aa42SJason M. Bills                 {"@odata.id",
2381424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2382e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
23835b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
23845b61b5e8SJason M. Bills                  {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
23855b61b5e8SJason M. Bills                              "Actions/LogService.ClearLog"}}},
23868e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
2387424c4176SJason M. Bills                  {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
23888e6c099aSJason M. Bills                              "Actions/LogService.CollectDiagnosticData"}}}};
23897e860f15SJohn Edward Broadbent         });
23901da66f75SEd Tanous }
23911da66f75SEd Tanous 
23927e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
23935b61b5e8SJason M. Bills {
23947e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23957e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
23965b61b5e8SJason M. Bills                  "LogService.ClearLog/")
2397ed398213SEd Tanous         // This is incorrect, should be:
2398ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2399432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
24007e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24017e860f15SJohn Edward Broadbent             [](const crow::Request&,
24027e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24035b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
24045b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2405cb13a392SEd Tanous                                 const std::string&) {
24065b61b5e8SJason M. Bills                         if (ec)
24075b61b5e8SJason M. Bills                         {
24085b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
24095b61b5e8SJason M. Bills                             return;
24105b61b5e8SJason M. Bills                         }
24115b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
24125b61b5e8SJason M. Bills                     },
24137e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
24147e860f15SJohn Edward Broadbent                     "DeleteAll");
24157e860f15SJohn Edward Broadbent             });
24165b61b5e8SJason M. Bills }
24175b61b5e8SJason M. Bills 
24188d1b46d7Szhanghch05 static void
24198d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24208d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2421e855dd28SJason M. Bills {
2422043a0536SJohnathan Mantey     auto getStoredLogCallback =
2423043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2424e855dd28SJason M. Bills             const boost::system::error_code ec,
2425043a0536SJohnathan Mantey             const std::vector<std::pair<std::string, VariantType>>& params) {
2426e855dd28SJason M. Bills             if (ec)
2427e855dd28SJason M. Bills             {
2428e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
24291ddcf01aSJason M. Bills                 if (ec.value() ==
24301ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
24311ddcf01aSJason M. Bills                 {
2432043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2433043a0536SJohnathan Mantey                                                logID);
24341ddcf01aSJason M. Bills                 }
24351ddcf01aSJason M. Bills                 else
24361ddcf01aSJason M. Bills                 {
2437e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
24381ddcf01aSJason M. Bills                 }
2439e855dd28SJason M. Bills                 return;
2440e855dd28SJason M. Bills             }
2441043a0536SJohnathan Mantey 
2442043a0536SJohnathan Mantey             std::string timestamp{};
2443043a0536SJohnathan Mantey             std::string filename{};
2444043a0536SJohnathan Mantey             std::string logfile{};
24452c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2446043a0536SJohnathan Mantey 
2447043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2448e855dd28SJason M. Bills             {
2449043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2450e855dd28SJason M. Bills                 return;
2451e855dd28SJason M. Bills             }
2452e855dd28SJason M. Bills 
2453043a0536SJohnathan Mantey             std::string crashdumpURI =
2454e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2455043a0536SJohnathan Mantey                 logID + "/" + filename;
2456d0dbeefdSEd Tanous             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2457043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2458043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2459e855dd28SJason M. Bills                                               logID},
2460e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2461e855dd28SJason M. Bills                             {"Id", logID},
2462e855dd28SJason M. Bills                             {"EntryType", "Oem"},
24638e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
24648e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
24658e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2466043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2467e855dd28SJason M. Bills         };
2468e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
24695b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
24705b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2471043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2472e855dd28SJason M. Bills }
2473e855dd28SJason M. Bills 
24747e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
24751da66f75SEd Tanous {
24763946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
24773946028dSAppaRao Puli     // method for security reasons.
24781da66f75SEd Tanous     /**
24791da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
24801da66f75SEd Tanous      */
24817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24827e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2483ed398213SEd Tanous         // This is incorrect, should be.
2484ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2485432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
24867e860f15SJohn Edward Broadbent         .methods(
24877e860f15SJohn Edward Broadbent             boost::beast::http::verb::
24887e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
24897e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24907e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
24917e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2492e1f26343SJason M. Bills             auto getLogEntriesCallback = [asyncResp](
2493e1f26343SJason M. Bills                                              const boost::system::error_code ec,
24947e860f15SJohn Edward Broadbent                                              const std::vector<std::string>&
24957e860f15SJohn Edward Broadbent                                                  resp) {
24961da66f75SEd Tanous                 if (ec)
24971da66f75SEd Tanous                 {
24981da66f75SEd Tanous                     if (ec.value() !=
24991da66f75SEd Tanous                         boost::system::errc::no_such_file_or_directory)
25001da66f75SEd Tanous                     {
25011da66f75SEd Tanous                         BMCWEB_LOG_DEBUG << "failed to get entries ec: "
25021da66f75SEd Tanous                                          << ec.message();
2503f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
25041da66f75SEd Tanous                         return;
25051da66f75SEd Tanous                     }
25061da66f75SEd Tanous                 }
2507e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
25081da66f75SEd Tanous                     "#LogEntryCollection.LogEntryCollection";
25090f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
2510424c4176SJason M. Bills                     "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2511424c4176SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2512e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
2513424c4176SJason M. Bills                     "Collection of Crashdump Entries";
25147e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
25157e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2516e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
2517e855dd28SJason M. Bills                 std::vector<std::string> logIDs;
2518e855dd28SJason M. Bills                 // Get the list of log entries and build up an empty array big
2519e855dd28SJason M. Bills                 // enough to hold them
25201da66f75SEd Tanous                 for (const std::string& objpath : resp)
25211da66f75SEd Tanous                 {
2522e855dd28SJason M. Bills                     // Get the log ID
2523f23b7296SEd Tanous                     std::size_t lastPos = objpath.rfind('/');
2524e855dd28SJason M. Bills                     if (lastPos == std::string::npos)
25251da66f75SEd Tanous                     {
2526e855dd28SJason M. Bills                         continue;
25271da66f75SEd Tanous                     }
2528e855dd28SJason M. Bills                     logIDs.emplace_back(objpath.substr(lastPos + 1));
2529e855dd28SJason M. Bills 
2530e855dd28SJason M. Bills                     // Add a space for the log entry to the array
2531e855dd28SJason M. Bills                     logEntryArray.push_back({});
2532e855dd28SJason M. Bills                 }
2533e855dd28SJason M. Bills                 // Now go through and set up async calls to fill in the entries
2534e855dd28SJason M. Bills                 size_t index = 0;
2535e855dd28SJason M. Bills                 for (const std::string& logID : logIDs)
2536e855dd28SJason M. Bills                 {
2537e855dd28SJason M. Bills                     // Add the log entry to the array
2538e855dd28SJason M. Bills                     logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
25391da66f75SEd Tanous                 }
2540e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2541e1f26343SJason M. Bills                     logEntryArray.size();
25421da66f75SEd Tanous             };
25431da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
25441da66f75SEd Tanous                 std::move(getLogEntriesCallback),
25451da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
25461da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
25471da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
25485b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
25497e860f15SJohn Edward Broadbent         });
25501da66f75SEd Tanous }
25511da66f75SEd Tanous 
25527e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
25531da66f75SEd Tanous {
25543946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25553946028dSAppaRao Puli     // method for security reasons.
25561da66f75SEd Tanous 
25577e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
25587e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2559ed398213SEd Tanous         // this is incorrect, should be
2560ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2561432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
25627e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25637e860f15SJohn Edward Broadbent             [](const crow::Request&,
25647e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25657e860f15SJohn Edward Broadbent                const std::string& param) {
25667e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2567e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
25687e860f15SJohn Edward Broadbent             });
2569e855dd28SJason M. Bills }
2570e855dd28SJason M. Bills 
25717e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2572e855dd28SJason M. Bills {
25733946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25743946028dSAppaRao Puli     // method for security reasons.
25757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
25767e860f15SJohn Edward Broadbent         app,
25777e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2578ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
25797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25807e860f15SJohn Edward Broadbent             [](const crow::Request&,
25817e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25827e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
2583043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2584043a0536SJohnathan Mantey                     [asyncResp, logID, fileName](
2585abf2add6SEd Tanous                         const boost::system::error_code ec,
25867e860f15SJohn Edward Broadbent                         const std::vector<std::pair<std::string, VariantType>>&
25877e860f15SJohn Edward Broadbent                             resp) {
25881da66f75SEd Tanous                         if (ec)
25891da66f75SEd Tanous                         {
2590043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2591043a0536SJohnathan Mantey                                              << ec.message();
2592f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
25931da66f75SEd Tanous                             return;
25941da66f75SEd Tanous                         }
2595e855dd28SJason M. Bills 
2596043a0536SJohnathan Mantey                         std::string dbusFilename{};
2597043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2598043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2599043a0536SJohnathan Mantey 
26007e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
26017e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2602043a0536SJohnathan Mantey 
2603043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2604043a0536SJohnathan Mantey                             dbusFilepath.empty())
26051da66f75SEd Tanous                         {
26067e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
26077e860f15SJohn Edward Broadbent                                                            fileName);
26081da66f75SEd Tanous                             return;
26091da66f75SEd Tanous                         }
2610e855dd28SJason M. Bills 
2611043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2612043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2613043a0536SJohnathan Mantey                         {
26147e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
26157e860f15SJohn Edward Broadbent                                                            fileName);
2616043a0536SJohnathan Mantey                             return;
2617043a0536SJohnathan Mantey                         }
2618043a0536SJohnathan Mantey 
2619043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2620043a0536SJohnathan Mantey                         {
26217e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
26227e860f15SJohn Edward Broadbent                                                            fileName);
2623043a0536SJohnathan Mantey                             return;
2624043a0536SJohnathan Mantey                         }
2625043a0536SJohnathan Mantey                         std::ifstream ifs(dbusFilepath, std::ios::in |
2626043a0536SJohnathan Mantey                                                             std::ios::binary |
2627043a0536SJohnathan Mantey                                                             std::ios::ate);
2628043a0536SJohnathan Mantey                         std::ifstream::pos_type fileSize = ifs.tellg();
2629043a0536SJohnathan Mantey                         if (fileSize < 0)
2630043a0536SJohnathan Mantey                         {
2631043a0536SJohnathan Mantey                             messages::generalError(asyncResp->res);
2632043a0536SJohnathan Mantey                             return;
2633043a0536SJohnathan Mantey                         }
2634043a0536SJohnathan Mantey                         ifs.seekg(0, std::ios::beg);
2635043a0536SJohnathan Mantey 
2636043a0536SJohnathan Mantey                         auto crashData = std::make_unique<char[]>(
2637043a0536SJohnathan Mantey                             static_cast<unsigned int>(fileSize));
2638043a0536SJohnathan Mantey 
2639043a0536SJohnathan Mantey                         ifs.read(crashData.get(), static_cast<int>(fileSize));
2640043a0536SJohnathan Mantey 
26417e860f15SJohn Edward Broadbent                         // The cast to std::string is intentional in order to
26427e860f15SJohn Edward Broadbent                         // use the assign() that applies move mechanics
2643043a0536SJohnathan Mantey                         asyncResp->res.body().assign(
2644043a0536SJohnathan Mantey                             static_cast<std::string>(crashData.get()));
2645043a0536SJohnathan Mantey 
26467e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
26477e860f15SJohn Edward Broadbent                         // from a browser
26487e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
26497e860f15SJohn Edward Broadbent                                                  "attachment");
26501da66f75SEd Tanous                     };
26511da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
26525b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
26535b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
26547e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
26557e860f15SJohn Edward Broadbent                     crashdumpInterface);
26567e860f15SJohn Edward Broadbent             });
26571da66f75SEd Tanous }
26581da66f75SEd Tanous 
26597e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
26601da66f75SEd Tanous {
26613946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26623946028dSAppaRao Puli     // method for security reasons.
26637e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/"
26647e860f15SJohn Edward Broadbent                       "Actions/LogService.CollectDiagnosticData/")
2665ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2666ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2667432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26687e860f15SJohn Edward Broadbent         .methods(
26697e860f15SJohn Edward Broadbent             boost::beast::http::verb::
26707e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
26717e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26728e6c099aSJason M. Bills             std::string diagnosticDataType;
26738e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
26748e6c099aSJason M. Bills             if (!redfish::json_util::readJson(
26757e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
26767e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
26777e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
26788e6c099aSJason M. Bills             {
26798e6c099aSJason M. Bills                 return;
26808e6c099aSJason M. Bills             }
26818e6c099aSJason M. Bills 
26828e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
26838e6c099aSJason M. Bills             {
26848e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
26858e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
26868e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
26878e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
26888e6c099aSJason M. Bills                     "CollectDiagnosticData");
26898e6c099aSJason M. Bills                 return;
26908e6c099aSJason M. Bills             }
26918e6c099aSJason M. Bills 
26928e6c099aSJason M. Bills             auto collectCrashdumpCallback = [asyncResp, req](
26937e860f15SJohn Edward Broadbent                                                 const boost::system::error_code
26947e860f15SJohn Edward Broadbent                                                     ec,
2695cb13a392SEd Tanous                                                 const std::string&) {
26961da66f75SEd Tanous                 if (ec)
26971da66f75SEd Tanous                 {
26987e860f15SJohn Edward Broadbent                     if (ec.value() ==
26997e860f15SJohn Edward Broadbent                         boost::system::errc::operation_not_supported)
27001da66f75SEd Tanous                     {
2701f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
27021da66f75SEd Tanous                     }
27034363d3b2SJason M. Bills                     else if (ec.value() ==
27044363d3b2SJason M. Bills                              boost::system::errc::device_or_resource_busy)
27054363d3b2SJason M. Bills                     {
27064363d3b2SJason M. Bills                         messages::serviceTemporarilyUnavailable(asyncResp->res,
27074363d3b2SJason M. Bills                                                                 "60");
27084363d3b2SJason M. Bills                     }
27091da66f75SEd Tanous                     else
27101da66f75SEd Tanous                     {
2711f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
27121da66f75SEd Tanous                     }
27131da66f75SEd Tanous                     return;
27141da66f75SEd Tanous                 }
27157e860f15SJohn Edward Broadbent                 std::shared_ptr<task::TaskData> task =
27167e860f15SJohn Edward Broadbent                     task::TaskData::createTask(
27177e860f15SJohn Edward Broadbent                         [](boost::system::error_code err,
27187e860f15SJohn Edward Broadbent                            sdbusplus::message::message&,
271966afe4faSJames Feist                            const std::shared_ptr<task::TaskData>& taskData) {
272066afe4faSJames Feist                             if (!err)
272166afe4faSJames Feist                             {
2722e5d5006bSJames Feist                                 taskData->messages.emplace_back(
2723e5d5006bSJames Feist                                     messages::taskCompletedOK(
2724e5d5006bSJames Feist                                         std::to_string(taskData->index)));
2725831d6b09SJames Feist                                 taskData->state = "Completed";
272666afe4faSJames Feist                             }
272732898ceaSJames Feist                             return task::completed;
272866afe4faSJames Feist                         },
27297e860f15SJohn Edward Broadbent                         "type='signal',interface='org.freedesktop.DBus."
27307e860f15SJohn Edward Broadbent                         "Properties',"
273146229577SJames Feist                         "member='PropertiesChanged',arg0namespace='com.intel."
273246229577SJames Feist                         "crashdump'");
273346229577SJames Feist                 task->startTimer(std::chrono::minutes(5));
273446229577SJames Feist                 task->populateResp(asyncResp->res);
2735fe306728SJames Feist                 task->payload.emplace(req);
27361da66f75SEd Tanous             };
27378e6c099aSJason M. Bills 
27388e6c099aSJason M. Bills             if (oemDiagnosticDataType == "OnDemand")
27398e6c099aSJason M. Bills             {
27401da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
27418e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
27428e6c099aSJason M. Bills                     crashdumpPath, crashdumpOnDemandInterface,
27438e6c099aSJason M. Bills                     "GenerateOnDemandLog");
27441da66f75SEd Tanous             }
27458e6c099aSJason M. Bills             else if (oemDiagnosticDataType == "Telemetry")
27466eda7685SKenny L. Ku             {
27478e6c099aSJason M. Bills                 crow::connections::systemBus->async_method_call(
27488e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
27498e6c099aSJason M. Bills                     crashdumpPath, crashdumpTelemetryInterface,
27508e6c099aSJason M. Bills                     "GenerateTelemetryLog");
27516eda7685SKenny L. Ku             }
27526eda7685SKenny L. Ku             else
27536eda7685SKenny L. Ku             {
27548e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
27558e6c099aSJason M. Bills                                  << oemDiagnosticDataType;
27568e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
27577e860f15SJohn Edward Broadbent                     asyncResp->res, oemDiagnosticDataType,
27587e860f15SJohn Edward Broadbent                     "OEMDiagnosticDataType", "CollectDiagnosticData");
27596eda7685SKenny L. Ku                 return;
27606eda7685SKenny L. Ku             }
27617e860f15SJohn Edward Broadbent         });
27626eda7685SKenny L. Ku }
27636eda7685SKenny L. Ku 
2764cb92c03bSAndrew Geissler /**
2765cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2766cb92c03bSAndrew Geissler  */
27677e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
2768cb92c03bSAndrew Geissler {
2769cb92c03bSAndrew Geissler     /**
2770cb92c03bSAndrew Geissler      * Function handles POST method request.
2771cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2772cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2773cb92c03bSAndrew Geissler      */
27747e860f15SJohn Edward Broadbent 
27757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
27767e860f15SJohn Edward Broadbent                       "LogService.ClearLog/")
2777ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
27787e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
27797e860f15SJohn Edward Broadbent             [](const crow::Request&,
27807e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2781cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
2782cb92c03bSAndrew Geissler 
2783cb92c03bSAndrew Geissler                 // Process response from Logging service.
27847e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
27857e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
27867e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
27877e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
2788cb92c03bSAndrew Geissler                     if (ec)
2789cb92c03bSAndrew Geissler                     {
2790cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
27917e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
27927e860f15SJohn Edward Broadbent                                          << ec;
2793cb92c03bSAndrew Geissler                         asyncResp->res.result(
2794cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
2795cb92c03bSAndrew Geissler                         return;
2796cb92c03bSAndrew Geissler                     }
2797cb92c03bSAndrew Geissler 
27987e860f15SJohn Edward Broadbent                     asyncResp->res.result(
27997e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
2800cb92c03bSAndrew Geissler                 };
2801cb92c03bSAndrew Geissler 
2802cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
2803cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
28042c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
2805cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
2806cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
28077e860f15SJohn Edward Broadbent             });
2808cb92c03bSAndrew Geissler }
2809a3316fc6SZhikuiRen 
2810a3316fc6SZhikuiRen /****************************************************
2811a3316fc6SZhikuiRen  * Redfish PostCode interfaces
2812a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
2813a3316fc6SZhikuiRen  ******************************************************/
28147e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
2815a3316fc6SZhikuiRen {
28167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
2817ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
28187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
28197e860f15SJohn Edward Broadbent             [](const crow::Request&,
28207e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2821a3316fc6SZhikuiRen                 asyncResp->res.jsonValue = {
28227e860f15SJohn Edward Broadbent                     {"@odata.id",
28237e860f15SJohn Edward Broadbent                      "/redfish/v1/Systems/system/LogServices/PostCodes"},
2824a3316fc6SZhikuiRen                     {"@odata.type", "#LogService.v1_1_0.LogService"},
2825a3316fc6SZhikuiRen                     {"Name", "POST Code Log Service"},
2826a3316fc6SZhikuiRen                     {"Description", "POST Code Log Service"},
2827a3316fc6SZhikuiRen                     {"Id", "BIOS POST Code Log"},
2828a3316fc6SZhikuiRen                     {"OverWritePolicy", "WrapsWhenFull"},
2829a3316fc6SZhikuiRen                     {"Entries",
28307e860f15SJohn Edward Broadbent                      {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
28317e860f15SJohn Edward Broadbent                                     "PostCodes/Entries"}}}};
28327c8c4058STejas Patil 
28337c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
28347c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
28357c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
28367c8c4058STejas Patil                     redfishDateTimeOffset.first;
28377c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
28387c8c4058STejas Patil                     redfishDateTimeOffset.second;
28397c8c4058STejas Patil 
2840a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
28417e860f15SJohn Edward Broadbent                     {"target",
28427e860f15SJohn Edward Broadbent                      "/redfish/v1/Systems/system/LogServices/PostCodes/"
2843a3316fc6SZhikuiRen                      "Actions/LogService.ClearLog"}};
28447e860f15SJohn Edward Broadbent             });
2845a3316fc6SZhikuiRen }
2846a3316fc6SZhikuiRen 
28477e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
2848a3316fc6SZhikuiRen {
28497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28507e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
2851a3316fc6SZhikuiRen                  "LogService.ClearLog/")
2852ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
2853ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2854432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
28567e860f15SJohn Edward Broadbent             [](const crow::Request&,
28577e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2858a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
2859a3316fc6SZhikuiRen 
2860a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
2861a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
2862a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
2863a3316fc6SZhikuiRen                         if (ec)
2864a3316fc6SZhikuiRen                         {
2865a3316fc6SZhikuiRen                             // TODO Handle for specific error code
2866a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
28677e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
28687e860f15SJohn Edward Broadbent                                 << ec;
28697e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
28707e860f15SJohn Edward Broadbent                                                       internal_server_error);
2871a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
2872a3316fc6SZhikuiRen                             return;
2873a3316fc6SZhikuiRen                         }
2874a3316fc6SZhikuiRen                     },
287515124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
287615124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
2877a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
28787e860f15SJohn Edward Broadbent             });
2879a3316fc6SZhikuiRen }
2880a3316fc6SZhikuiRen 
2881a3316fc6SZhikuiRen static void fillPostCodeEntry(
28828d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
28836c9a279eSManojkiran Eda     const boost::container::flat_map<
28846c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
2885a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
2886a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
2887a3316fc6SZhikuiRen {
2888a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
2889a3316fc6SZhikuiRen     const message_registries::Message* message =
28904a0bf539SManojkiran Eda         message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
2891a3316fc6SZhikuiRen 
2892a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
2893a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
2894a3316fc6SZhikuiRen 
2895a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
28966c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
28976c9a279eSManojkiran Eda              code : postcode)
2898a3316fc6SZhikuiRen     {
2899a3316fc6SZhikuiRen         currentCodeIndex++;
2900a3316fc6SZhikuiRen         std::string postcodeEntryID =
2901a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
2902a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
2903a3316fc6SZhikuiRen 
2904a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
2905a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
2906a3316fc6SZhikuiRen 
2907a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
2908a3316fc6SZhikuiRen         { // already incremented
2909a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
2910a3316fc6SZhikuiRen         }
2911a3316fc6SZhikuiRen         else
2912a3316fc6SZhikuiRen         {
2913a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
2914a3316fc6SZhikuiRen         }
2915a3316fc6SZhikuiRen 
2916a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
2917a3316fc6SZhikuiRen         // not fall between top and skip
2918a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
2919a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
2920a3316fc6SZhikuiRen         {
2921a3316fc6SZhikuiRen             continue;
2922a3316fc6SZhikuiRen         }
2923a3316fc6SZhikuiRen 
29244e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
2925a3316fc6SZhikuiRen         // currentIndex
2926a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
2927a3316fc6SZhikuiRen         {
2928a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
2929a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
2930a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
2931a3316fc6SZhikuiRen             continue;
2932a3316fc6SZhikuiRen         }
2933a3316fc6SZhikuiRen 
2934a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
2935a3316fc6SZhikuiRen         // index
2936a3316fc6SZhikuiRen 
2937a3316fc6SZhikuiRen         // Get the Created time from the timestamp
2938a3316fc6SZhikuiRen         std::string entryTimeStr;
29399c620e21SAsmitha Karunanithi         entryTimeStr = crow::utility::getDateTime(
29409c620e21SAsmitha Karunanithi             static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
2941a3316fc6SZhikuiRen 
2942a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
2943a3316fc6SZhikuiRen         std::ostringstream hexCode;
2944a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
29456c9a279eSManojkiran Eda                 << std::get<0>(code.second);
2946a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
2947a3316fc6SZhikuiRen         // Set Fixed -Point Notation
2948a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
2949a3316fc6SZhikuiRen         // Set precision to 4 digits
2950a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
2951a3316fc6SZhikuiRen         // Add double to stream
2952a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
2953a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
2954a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
2955a3316fc6SZhikuiRen 
2956a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
2957a3316fc6SZhikuiRen         std::string msg;
2958a3316fc6SZhikuiRen         if (message != nullptr)
2959a3316fc6SZhikuiRen         {
2960a3316fc6SZhikuiRen             msg = message->message;
2961a3316fc6SZhikuiRen 
2962a3316fc6SZhikuiRen             // fill in this post code value
2963a3316fc6SZhikuiRen             int i = 0;
2964a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
2965a3316fc6SZhikuiRen             {
2966a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
2967a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
2968a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
2969a3316fc6SZhikuiRen                 {
2970a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
2971a3316fc6SZhikuiRen                 }
2972a3316fc6SZhikuiRen             }
2973a3316fc6SZhikuiRen         }
2974a3316fc6SZhikuiRen 
2975d4342a92STim Lee         // Get Severity template from message registry
2976d4342a92STim Lee         std::string severity;
2977d4342a92STim Lee         if (message != nullptr)
2978d4342a92STim Lee         {
2979d4342a92STim Lee             severity = message->severity;
2980d4342a92STim Lee         }
2981d4342a92STim Lee 
2982a3316fc6SZhikuiRen         // add to AsyncResp
2983a3316fc6SZhikuiRen         logEntryArray.push_back({});
2984a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
2985d0dbeefdSEd Tanous         bmcLogEntry = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
2986a3316fc6SZhikuiRen                        {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2987a3316fc6SZhikuiRen                                      "PostCodes/Entries/" +
2988a3316fc6SZhikuiRen                                          postcodeEntryID},
2989a3316fc6SZhikuiRen                        {"Name", "POST Code Log Entry"},
2990a3316fc6SZhikuiRen                        {"Id", postcodeEntryID},
2991a3316fc6SZhikuiRen                        {"Message", std::move(msg)},
29924a0bf539SManojkiran Eda                        {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
2993a3316fc6SZhikuiRen                        {"MessageArgs", std::move(messageArgs)},
2994a3316fc6SZhikuiRen                        {"EntryType", "Event"},
2995a3316fc6SZhikuiRen                        {"Severity", std::move(severity)},
29969c620e21SAsmitha Karunanithi                        {"Created", entryTimeStr}};
2997a3316fc6SZhikuiRen     }
2998a3316fc6SZhikuiRen }
2999a3316fc6SZhikuiRen 
30008d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3001a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3002a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3003a3316fc6SZhikuiRen {
3004a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
30056c9a279eSManojkiran Eda         [aResp, bootIndex,
30066c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
30076c9a279eSManojkiran Eda                     const boost::container::flat_map<
30086c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
30096c9a279eSManojkiran Eda                         postcode) {
3010a3316fc6SZhikuiRen             if (ec)
3011a3316fc6SZhikuiRen             {
3012a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3013a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3014a3316fc6SZhikuiRen                 return;
3015a3316fc6SZhikuiRen             }
3016a3316fc6SZhikuiRen 
3017a3316fc6SZhikuiRen             // skip the empty postcode boots
3018a3316fc6SZhikuiRen             if (postcode.empty())
3019a3316fc6SZhikuiRen             {
3020a3316fc6SZhikuiRen                 return;
3021a3316fc6SZhikuiRen             }
3022a3316fc6SZhikuiRen 
3023a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3024a3316fc6SZhikuiRen 
3025a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3026a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3027a3316fc6SZhikuiRen         },
302815124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
302915124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3030a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3031a3316fc6SZhikuiRen         bootIndex);
3032a3316fc6SZhikuiRen }
3033a3316fc6SZhikuiRen 
30348d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3035a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3036a3316fc6SZhikuiRen                                const uint16_t bootCount,
3037a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3038a3316fc6SZhikuiRen                                const uint64_t top)
3039a3316fc6SZhikuiRen {
3040a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3041a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3042a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
30436c9a279eSManojkiran Eda               const boost::container::flat_map<
30446c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
30456c9a279eSManojkiran Eda                   postcode) {
3046a3316fc6SZhikuiRen             if (ec)
3047a3316fc6SZhikuiRen             {
3048a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3049a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3050a3316fc6SZhikuiRen                 return;
3051a3316fc6SZhikuiRen             }
3052a3316fc6SZhikuiRen 
3053a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3054a3316fc6SZhikuiRen             if (!postcode.empty())
3055a3316fc6SZhikuiRen             {
3056a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3057a3316fc6SZhikuiRen 
3058a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3059a3316fc6SZhikuiRen                 {
3060a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3061a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3062a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3063a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3064a3316fc6SZhikuiRen 
3065a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3066a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3067a3316fc6SZhikuiRen                 }
3068a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3069a3316fc6SZhikuiRen             }
3070a3316fc6SZhikuiRen 
3071a3316fc6SZhikuiRen             // continue to previous bootIndex
3072a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3073a3316fc6SZhikuiRen             {
3074a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3075a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3076a3316fc6SZhikuiRen             }
3077a3316fc6SZhikuiRen             else
3078a3316fc6SZhikuiRen             {
3079a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
3080a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
3081a3316fc6SZhikuiRen                     "Entries?$skip=" +
3082a3316fc6SZhikuiRen                     std::to_string(skip + top);
3083a3316fc6SZhikuiRen             }
3084a3316fc6SZhikuiRen         },
308515124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
308615124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3087a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3088a3316fc6SZhikuiRen         bootIndex);
3089a3316fc6SZhikuiRen }
3090a3316fc6SZhikuiRen 
30918d1b46d7Szhanghch05 static void
30928d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3093a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3094a3316fc6SZhikuiRen {
3095a3316fc6SZhikuiRen     uint64_t entryCount = 0;
3096a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3097a3316fc6SZhikuiRen         [aResp, entryCount, skip,
3098a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
3099a3316fc6SZhikuiRen               const std::variant<uint16_t>& bootCount) {
3100a3316fc6SZhikuiRen             if (ec)
3101a3316fc6SZhikuiRen             {
3102a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3103a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3104a3316fc6SZhikuiRen                 return;
3105a3316fc6SZhikuiRen             }
3106a3316fc6SZhikuiRen             auto pVal = std::get_if<uint16_t>(&bootCount);
3107a3316fc6SZhikuiRen             if (pVal)
3108a3316fc6SZhikuiRen             {
3109a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3110a3316fc6SZhikuiRen             }
3111a3316fc6SZhikuiRen             else
3112a3316fc6SZhikuiRen             {
3113a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3114a3316fc6SZhikuiRen             }
3115a3316fc6SZhikuiRen         },
311615124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
311715124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3118a3316fc6SZhikuiRen         "org.freedesktop.DBus.Properties", "Get",
3119a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3120a3316fc6SZhikuiRen }
3121a3316fc6SZhikuiRen 
31227e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3123a3316fc6SZhikuiRen {
31247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
31257e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3126ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
31277e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
31287e860f15SJohn Edward Broadbent             [](const crow::Request& req,
31297e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3130a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3131a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3132a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3133a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3134a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3135a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3136a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3137a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3138a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3139a3316fc6SZhikuiRen 
3140a3316fc6SZhikuiRen                 uint64_t skip = 0;
3141a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
31428d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3143a3316fc6SZhikuiRen                 {
3144a3316fc6SZhikuiRen                     return;
3145a3316fc6SZhikuiRen                 }
31468d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3147a3316fc6SZhikuiRen                 {
3148a3316fc6SZhikuiRen                     return;
3149a3316fc6SZhikuiRen                 }
3150a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
31517e860f15SJohn Edward Broadbent             });
3152a3316fc6SZhikuiRen }
3153a3316fc6SZhikuiRen 
31547e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3155a3316fc6SZhikuiRen {
31567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
31577e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3158ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
31597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
31607e860f15SJohn Edward Broadbent             [](const crow::Request&,
31617e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31627e860f15SJohn Edward Broadbent                const std::string& targetID) {
3163a3316fc6SZhikuiRen                 size_t bootPos = targetID.find('B');
3164a3316fc6SZhikuiRen                 if (bootPos == std::string::npos)
3165a3316fc6SZhikuiRen                 {
3166a3316fc6SZhikuiRen                     // Requested ID was not found
3167a3316fc6SZhikuiRen                     messages::resourceMissingAtURI(asyncResp->res, targetID);
3168a3316fc6SZhikuiRen                     return;
3169a3316fc6SZhikuiRen                 }
3170a3316fc6SZhikuiRen                 std::string_view bootIndexStr(targetID);
3171a3316fc6SZhikuiRen                 bootIndexStr.remove_prefix(bootPos + 1);
3172a3316fc6SZhikuiRen                 uint16_t bootIndex = 0;
3173a3316fc6SZhikuiRen                 uint64_t codeIndex = 0;
3174a3316fc6SZhikuiRen                 size_t dashPos = bootIndexStr.find('-');
3175a3316fc6SZhikuiRen 
3176a3316fc6SZhikuiRen                 if (dashPos == std::string::npos)
3177a3316fc6SZhikuiRen                 {
3178a3316fc6SZhikuiRen                     return;
3179a3316fc6SZhikuiRen                 }
3180a3316fc6SZhikuiRen                 std::string_view codeIndexStr(bootIndexStr);
3181a3316fc6SZhikuiRen                 bootIndexStr.remove_suffix(dashPos);
3182a3316fc6SZhikuiRen                 codeIndexStr.remove_prefix(dashPos + 1);
3183a3316fc6SZhikuiRen 
3184a3316fc6SZhikuiRen                 bootIndex = static_cast<uint16_t>(
318523a21a1cSEd Tanous                     strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
31867e860f15SJohn Edward Broadbent                 codeIndex =
31877e860f15SJohn Edward Broadbent                     strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
3188a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3189a3316fc6SZhikuiRen                 {
3190a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
31917e860f15SJohn Edward Broadbent                                      << targetID;
3192a3316fc6SZhikuiRen                 }
3193a3316fc6SZhikuiRen 
31947e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
31957e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3196a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3197a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
3198a3316fc6SZhikuiRen                     "Entries";
3199a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3200a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3201a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3202a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3203a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3204a3316fc6SZhikuiRen 
3205a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
32067e860f15SJohn Edward Broadbent             });
3207a3316fc6SZhikuiRen }
3208a3316fc6SZhikuiRen 
32091da66f75SEd Tanous } // namespace redfish
3210