xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 7e860f1550c8686eec42f7a75bc5f2ef51e756ad)
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 
26*7e860f15SJohn 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>
341214b7e7SGunnar Mills 
354418c7f0SJames Feist #include <filesystem>
3675710de2SXiaochao Ma #include <optional>
37cd225da8SJason M. Bills #include <string_view>
38abf2add6SEd Tanous #include <variant>
391da66f75SEd Tanous 
401da66f75SEd Tanous namespace redfish
411da66f75SEd Tanous {
421da66f75SEd Tanous 
435b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
445b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
455b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
465b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
475b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
485b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
49424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
506eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
516eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
521da66f75SEd Tanous 
534851d45dSJason M. Bills namespace message_registries
544851d45dSJason M. Bills {
554851d45dSJason M. Bills static const Message* getMessageFromRegistry(
564851d45dSJason M. Bills     const std::string& messageKey,
574851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
584851d45dSJason M. Bills {
594851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
604851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
614851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
624851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
634851d45dSJason M. Bills                                              messageKey.c_str());
644851d45dSJason M. Bills                      });
654851d45dSJason M. Bills     if (messageIt != registry.cend())
664851d45dSJason M. Bills     {
674851d45dSJason M. Bills         return &messageIt->second;
684851d45dSJason M. Bills     }
694851d45dSJason M. Bills 
704851d45dSJason M. Bills     return nullptr;
714851d45dSJason M. Bills }
724851d45dSJason M. Bills 
734851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
744851d45dSJason M. Bills {
754851d45dSJason M. Bills     // Redfish MessageIds are in the form
764851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
774851d45dSJason M. Bills     // the right Message
784851d45dSJason M. Bills     std::vector<std::string> fields;
794851d45dSJason M. Bills     fields.reserve(4);
804851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
814851d45dSJason M. Bills     std::string& registryName = fields[0];
824851d45dSJason M. Bills     std::string& messageKey = fields[3];
834851d45dSJason M. Bills 
844851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
854851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
864851d45dSJason M. Bills     {
874851d45dSJason M. Bills         return getMessageFromRegistry(
884851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
894851d45dSJason M. Bills     }
904851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
914851d45dSJason M. Bills     {
924851d45dSJason M. Bills         return getMessageFromRegistry(
934851d45dSJason M. Bills             messageKey,
944851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
954851d45dSJason M. Bills     }
964851d45dSJason M. Bills     return nullptr;
974851d45dSJason M. Bills }
984851d45dSJason M. Bills } // namespace message_registries
994851d45dSJason M. Bills 
100f6150403SJames Feist namespace fs = std::filesystem;
1011da66f75SEd Tanous 
102cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
10319bd78d9SPatrick Williams     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
104cb92c03bSAndrew Geissler                               int32_t, uint32_t, int64_t, uint64_t, double>>;
105cb92c03bSAndrew Geissler 
106cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
107cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
108cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
109cb92c03bSAndrew Geissler 
110cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
111cb92c03bSAndrew Geissler {
112d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
113d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
114d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
115d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
116cb92c03bSAndrew Geissler     {
117cb92c03bSAndrew Geissler         return "Critical";
118cb92c03bSAndrew Geissler     }
1193174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
120d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
121d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
122cb92c03bSAndrew Geissler     {
123cb92c03bSAndrew Geissler         return "OK";
124cb92c03bSAndrew Geissler     }
1253174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
126cb92c03bSAndrew Geissler     {
127cb92c03bSAndrew Geissler         return "Warning";
128cb92c03bSAndrew Geissler     }
129cb92c03bSAndrew Geissler     return "";
130cb92c03bSAndrew Geissler }
131cb92c03bSAndrew Geissler 
132*7e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13339e77504SEd Tanous                                      const std::string_view& field,
13439e77504SEd Tanous                                      std::string_view& contents)
13516428a1aSJason M. Bills {
13616428a1aSJason M. Bills     const char* data = nullptr;
13716428a1aSJason M. Bills     size_t length = 0;
13816428a1aSJason M. Bills     int ret = 0;
13916428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
140271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
141271584abSEd Tanous                               reinterpret_cast<const void**>(&data), &length);
14216428a1aSJason M. Bills     if (ret < 0)
14316428a1aSJason M. Bills     {
14416428a1aSJason M. Bills         return ret;
14516428a1aSJason M. Bills     }
14639e77504SEd Tanous     contents = std::string_view(data, length);
14716428a1aSJason M. Bills     // Only use the content after the "=" character.
14881ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
14916428a1aSJason M. Bills     return ret;
15016428a1aSJason M. Bills }
15116428a1aSJason M. Bills 
152*7e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
153*7e860f15SJohn Edward Broadbent                                      const std::string_view& field,
154*7e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15516428a1aSJason M. Bills {
15616428a1aSJason M. Bills     int ret = 0;
15739e77504SEd Tanous     std::string_view metadata;
15816428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15916428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16016428a1aSJason M. Bills     if (ret < 0)
16116428a1aSJason M. Bills     {
16216428a1aSJason M. Bills         return ret;
16316428a1aSJason M. Bills     }
164b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16516428a1aSJason M. Bills     return ret;
16616428a1aSJason M. Bills }
16716428a1aSJason M. Bills 
168*7e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
169*7e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
170a3316fc6SZhikuiRen {
171a3316fc6SZhikuiRen     int ret = 0;
172a3316fc6SZhikuiRen     uint64_t timestamp = 0;
173a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
174a3316fc6SZhikuiRen     if (ret < 0)
175a3316fc6SZhikuiRen     {
176a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
177a3316fc6SZhikuiRen                          << strerror(-ret);
178a3316fc6SZhikuiRen         return false;
179a3316fc6SZhikuiRen     }
1809c620e21SAsmitha Karunanithi     entryTimestamp = crow::utility::getDateTime(
1819c620e21SAsmitha Karunanithi         static_cast<std::time_t>(timestamp / 1000 / 1000));
1829c620e21SAsmitha Karunanithi     return true;
183a3316fc6SZhikuiRen }
184a3316fc6SZhikuiRen 
1858d1b46d7Szhanghch05 static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1868d1b46d7Szhanghch05                          const crow::Request& req, uint64_t& skip)
18716428a1aSJason M. Bills {
1885a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
1895a7e877eSJames Feist         req.urlParams.find("$skip");
1905a7e877eSJames Feist     if (it != req.urlParams.end())
19116428a1aSJason M. Bills     {
1925a7e877eSJames Feist         std::string skipParam = it->value();
19316428a1aSJason M. Bills         char* ptr = nullptr;
1945a7e877eSJames Feist         skip = std::strtoul(skipParam.c_str(), &ptr, 10);
1955a7e877eSJames Feist         if (skipParam.empty() || *ptr != '\0')
19616428a1aSJason M. Bills         {
19716428a1aSJason M. Bills 
1988d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
1998d1b46d7Szhanghch05                 asyncResp->res, std::string(skipParam), "$skip");
20016428a1aSJason M. Bills             return false;
20116428a1aSJason M. Bills         }
20216428a1aSJason M. Bills     }
20316428a1aSJason M. Bills     return true;
20416428a1aSJason M. Bills }
20516428a1aSJason M. Bills 
206271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
2078d1b46d7Szhanghch05 static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2088d1b46d7Szhanghch05                         const crow::Request& req, uint64_t& top)
20916428a1aSJason M. Bills {
2105a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
2115a7e877eSJames Feist         req.urlParams.find("$top");
2125a7e877eSJames Feist     if (it != req.urlParams.end())
21316428a1aSJason M. Bills     {
2145a7e877eSJames Feist         std::string topParam = it->value();
21516428a1aSJason M. Bills         char* ptr = nullptr;
2165a7e877eSJames Feist         top = std::strtoul(topParam.c_str(), &ptr, 10);
2175a7e877eSJames Feist         if (topParam.empty() || *ptr != '\0')
21816428a1aSJason M. Bills         {
2198d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2208d1b46d7Szhanghch05                 asyncResp->res, std::string(topParam), "$top");
22116428a1aSJason M. Bills             return false;
22216428a1aSJason M. Bills         }
223271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
22416428a1aSJason M. Bills         {
22516428a1aSJason M. Bills 
22616428a1aSJason M. Bills             messages::queryParameterOutOfRange(
2278d1b46d7Szhanghch05                 asyncResp->res, std::to_string(top), "$top",
22816428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
22916428a1aSJason M. Bills             return false;
23016428a1aSJason M. Bills         }
23116428a1aSJason M. Bills     }
23216428a1aSJason M. Bills     return true;
23316428a1aSJason M. Bills }
23416428a1aSJason M. Bills 
235*7e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
236e85d6b16SJason M. Bills                                     const bool firstEntry = true)
23716428a1aSJason M. Bills {
23816428a1aSJason M. Bills     int ret = 0;
23916428a1aSJason M. Bills     static uint64_t prevTs = 0;
24016428a1aSJason M. Bills     static int index = 0;
241e85d6b16SJason M. Bills     if (firstEntry)
242e85d6b16SJason M. Bills     {
243e85d6b16SJason M. Bills         prevTs = 0;
244e85d6b16SJason M. Bills     }
245e85d6b16SJason M. Bills 
24616428a1aSJason M. Bills     // Get the entry timestamp
24716428a1aSJason M. Bills     uint64_t curTs = 0;
24816428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
24916428a1aSJason M. Bills     if (ret < 0)
25016428a1aSJason M. Bills     {
25116428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
25216428a1aSJason M. Bills                          << strerror(-ret);
25316428a1aSJason M. Bills         return false;
25416428a1aSJason M. Bills     }
25516428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
25616428a1aSJason M. Bills     if (curTs == prevTs)
25716428a1aSJason M. Bills     {
25816428a1aSJason M. Bills         index++;
25916428a1aSJason M. Bills     }
26016428a1aSJason M. Bills     else
26116428a1aSJason M. Bills     {
26216428a1aSJason M. Bills         // Otherwise, reset it
26316428a1aSJason M. Bills         index = 0;
26416428a1aSJason M. Bills     }
26516428a1aSJason M. Bills     // Save the timestamp
26616428a1aSJason M. Bills     prevTs = curTs;
26716428a1aSJason M. Bills 
26816428a1aSJason M. Bills     entryID = std::to_string(curTs);
26916428a1aSJason M. Bills     if (index > 0)
27016428a1aSJason M. Bills     {
27116428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
27216428a1aSJason M. Bills     }
27316428a1aSJason M. Bills     return true;
27416428a1aSJason M. Bills }
27516428a1aSJason M. Bills 
276e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
277e85d6b16SJason M. Bills                              const bool firstEntry = true)
27895820184SJason M. Bills {
279271584abSEd Tanous     static time_t prevTs = 0;
28095820184SJason M. Bills     static int index = 0;
281e85d6b16SJason M. Bills     if (firstEntry)
282e85d6b16SJason M. Bills     {
283e85d6b16SJason M. Bills         prevTs = 0;
284e85d6b16SJason M. Bills     }
285e85d6b16SJason M. Bills 
28695820184SJason M. Bills     // Get the entry timestamp
287271584abSEd Tanous     std::time_t curTs = 0;
28895820184SJason M. Bills     std::tm timeStruct = {};
28995820184SJason M. Bills     std::istringstream entryStream(logEntry);
29095820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
29195820184SJason M. Bills     {
29295820184SJason M. Bills         curTs = std::mktime(&timeStruct);
29395820184SJason M. Bills     }
29495820184SJason M. Bills     // If the timestamp isn't unique, increment the index
29595820184SJason M. Bills     if (curTs == prevTs)
29695820184SJason M. Bills     {
29795820184SJason M. Bills         index++;
29895820184SJason M. Bills     }
29995820184SJason M. Bills     else
30095820184SJason M. Bills     {
30195820184SJason M. Bills         // Otherwise, reset it
30295820184SJason M. Bills         index = 0;
30395820184SJason M. Bills     }
30495820184SJason M. Bills     // Save the timestamp
30595820184SJason M. Bills     prevTs = curTs;
30695820184SJason M. Bills 
30795820184SJason M. Bills     entryID = std::to_string(curTs);
30895820184SJason M. Bills     if (index > 0)
30995820184SJason M. Bills     {
31095820184SJason M. Bills         entryID += "_" + std::to_string(index);
31195820184SJason M. Bills     }
31295820184SJason M. Bills     return true;
31395820184SJason M. Bills }
31495820184SJason M. Bills 
315*7e860f15SJohn Edward Broadbent inline static bool
3168d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3178d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3188d1b46d7Szhanghch05                        uint64_t& index)
31916428a1aSJason M. Bills {
32016428a1aSJason M. Bills     if (entryID.empty())
32116428a1aSJason M. Bills     {
32216428a1aSJason M. Bills         return false;
32316428a1aSJason M. Bills     }
32416428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
32539e77504SEd Tanous     std::string_view tsStr(entryID);
32616428a1aSJason M. Bills 
32781ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
32816428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
32916428a1aSJason M. Bills     {
33016428a1aSJason M. Bills         // Timestamp has an index
33116428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
33239e77504SEd Tanous         std::string_view indexStr(entryID);
33316428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
33416428a1aSJason M. Bills         std::size_t pos;
33516428a1aSJason M. Bills         try
33616428a1aSJason M. Bills         {
33739e77504SEd Tanous             index = std::stoul(std::string(indexStr), &pos);
33816428a1aSJason M. Bills         }
339271584abSEd Tanous         catch (std::invalid_argument&)
34016428a1aSJason M. Bills         {
3418d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
34216428a1aSJason M. Bills             return false;
34316428a1aSJason M. Bills         }
344271584abSEd Tanous         catch (std::out_of_range&)
34516428a1aSJason M. Bills         {
3468d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
34716428a1aSJason M. Bills             return false;
34816428a1aSJason M. Bills         }
34916428a1aSJason M. Bills         if (pos != indexStr.size())
35016428a1aSJason M. Bills         {
3518d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
35216428a1aSJason M. Bills             return false;
35316428a1aSJason M. Bills         }
35416428a1aSJason M. Bills     }
35516428a1aSJason M. Bills     // Timestamp has no index
35616428a1aSJason M. Bills     std::size_t pos;
35716428a1aSJason M. Bills     try
35816428a1aSJason M. Bills     {
35939e77504SEd Tanous         timestamp = std::stoull(std::string(tsStr), &pos);
36016428a1aSJason M. Bills     }
361271584abSEd Tanous     catch (std::invalid_argument&)
36216428a1aSJason M. Bills     {
3638d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
36416428a1aSJason M. Bills         return false;
36516428a1aSJason M. Bills     }
366271584abSEd Tanous     catch (std::out_of_range&)
36716428a1aSJason M. Bills     {
3688d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
36916428a1aSJason M. Bills         return false;
37016428a1aSJason M. Bills     }
37116428a1aSJason M. Bills     if (pos != tsStr.size())
37216428a1aSJason M. Bills     {
3738d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
37416428a1aSJason M. Bills         return false;
37516428a1aSJason M. Bills     }
37616428a1aSJason M. Bills     return true;
37716428a1aSJason M. Bills }
37816428a1aSJason M. Bills 
37995820184SJason M. Bills static bool
38095820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
38195820184SJason M. Bills {
38295820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
38395820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
38495820184SJason M. Bills 
38595820184SJason M. Bills     // Loop through the directory looking for redfish log files
38695820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
38795820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
38895820184SJason M. Bills     {
38995820184SJason M. Bills         // If we find a redfish log file, save the path
39095820184SJason M. Bills         std::string filename = dirEnt.path().filename();
39195820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
39295820184SJason M. Bills         {
39395820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
39495820184SJason M. Bills         }
39595820184SJason M. Bills     }
39695820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
39795820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
39895820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
39995820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
40095820184SJason M. Bills 
40195820184SJason M. Bills     return !redfishLogFiles.empty();
40295820184SJason M. Bills }
40395820184SJason M. Bills 
4048d1b46d7Szhanghch05 inline void
4058d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4065cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4075cb1dd27SAsmitha Karunanithi {
4085cb1dd27SAsmitha Karunanithi     std::string dumpPath;
4095cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
4105cb1dd27SAsmitha Karunanithi     {
4115cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
4125cb1dd27SAsmitha Karunanithi     }
4135cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
4145cb1dd27SAsmitha Karunanithi     {
4155cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
4165cb1dd27SAsmitha Karunanithi     }
4175cb1dd27SAsmitha Karunanithi     else
4185cb1dd27SAsmitha Karunanithi     {
4195cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
4205cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4215cb1dd27SAsmitha Karunanithi         return;
4225cb1dd27SAsmitha Karunanithi     }
4235cb1dd27SAsmitha Karunanithi 
4245cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
4255cb1dd27SAsmitha Karunanithi         [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
4265cb1dd27SAsmitha Karunanithi                                         GetManagedObjectsType& resp) {
4275cb1dd27SAsmitha Karunanithi             if (ec)
4285cb1dd27SAsmitha Karunanithi             {
4295cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4305cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
4315cb1dd27SAsmitha Karunanithi                 return;
4325cb1dd27SAsmitha Karunanithi             }
4335cb1dd27SAsmitha Karunanithi 
4345cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4355cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
436b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
437b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
438b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
439b47452b2SAsmitha Karunanithi                 "/entry/";
4405cb1dd27SAsmitha Karunanithi 
4415cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4425cb1dd27SAsmitha Karunanithi             {
443b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4445cb1dd27SAsmitha Karunanithi                 {
4455cb1dd27SAsmitha Karunanithi                     continue;
4465cb1dd27SAsmitha Karunanithi                 }
4475cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
4485cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
4495cb1dd27SAsmitha Karunanithi                 entriesArray.push_back({});
4505cb1dd27SAsmitha Karunanithi                 nlohmann::json& thisEntry = entriesArray.back();
4512dfd18efSEd Tanous 
4522dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4532dfd18efSEd Tanous                 if (entryID.empty())
4545cb1dd27SAsmitha Karunanithi                 {
4555cb1dd27SAsmitha Karunanithi                     continue;
4565cb1dd27SAsmitha Karunanithi                 }
4575cb1dd27SAsmitha Karunanithi 
4585cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4595cb1dd27SAsmitha Karunanithi                 {
4605cb1dd27SAsmitha Karunanithi                     if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
4615cb1dd27SAsmitha Karunanithi                     {
4625cb1dd27SAsmitha Karunanithi 
4635cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4645cb1dd27SAsmitha Karunanithi                         {
4655cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4665cb1dd27SAsmitha Karunanithi                             {
4675cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
4685cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4695cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4705cb1dd27SAsmitha Karunanithi                                 {
4715cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4725cb1dd27SAsmitha Karunanithi                                     break;
4735cb1dd27SAsmitha Karunanithi                                 }
4745cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4755cb1dd27SAsmitha Karunanithi                                 break;
4765cb1dd27SAsmitha Karunanithi                             }
4775cb1dd27SAsmitha Karunanithi                         }
4785cb1dd27SAsmitha Karunanithi                     }
4795cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4805cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4815cb1dd27SAsmitha Karunanithi                     {
4825cb1dd27SAsmitha Karunanithi 
4835cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4845cb1dd27SAsmitha Karunanithi                         {
4855cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4865cb1dd27SAsmitha Karunanithi                             {
4875cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4885cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4895cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4905cb1dd27SAsmitha Karunanithi                                 {
4915cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4925cb1dd27SAsmitha Karunanithi                                     break;
4935cb1dd27SAsmitha Karunanithi                                 }
4945cb1dd27SAsmitha Karunanithi                                 timestamp =
4955cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
4965cb1dd27SAsmitha Karunanithi                                 break;
4975cb1dd27SAsmitha Karunanithi                             }
4985cb1dd27SAsmitha Karunanithi                         }
4995cb1dd27SAsmitha Karunanithi                     }
5005cb1dd27SAsmitha Karunanithi                 }
5015cb1dd27SAsmitha Karunanithi 
502d337bb72SAsmitha Karunanithi                 thisEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
5035cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
5045cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
5055cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5065cb1dd27SAsmitha Karunanithi                 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
5075cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5085cb1dd27SAsmitha Karunanithi 
509d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5105cb1dd27SAsmitha Karunanithi 
5115cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5125cb1dd27SAsmitha Karunanithi                 {
513d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
514d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
515de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
516de8d94a3SAbhishek Patel                         entryID + "/attachment";
5175cb1dd27SAsmitha Karunanithi                 }
5185cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5195cb1dd27SAsmitha Karunanithi                 {
520d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
521d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
522d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
523de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
524de8d94a3SAbhishek Patel                         entryID + "/attachment";
5255cb1dd27SAsmitha Karunanithi                 }
5265cb1dd27SAsmitha Karunanithi             }
5275cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5285cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5295cb1dd27SAsmitha Karunanithi         },
5305cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5315cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5325cb1dd27SAsmitha Karunanithi }
5335cb1dd27SAsmitha Karunanithi 
5348d1b46d7Szhanghch05 inline void
5358d1b46d7Szhanghch05     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5368d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5375cb1dd27SAsmitha Karunanithi {
5385cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5395cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5405cb1dd27SAsmitha Karunanithi     {
5415cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5425cb1dd27SAsmitha Karunanithi     }
5435cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5445cb1dd27SAsmitha Karunanithi     {
5455cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5465cb1dd27SAsmitha Karunanithi     }
5475cb1dd27SAsmitha Karunanithi     else
5485cb1dd27SAsmitha Karunanithi     {
5495cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5505cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5515cb1dd27SAsmitha Karunanithi         return;
5525cb1dd27SAsmitha Karunanithi     }
5535cb1dd27SAsmitha Karunanithi 
5545cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
5555cb1dd27SAsmitha Karunanithi         [asyncResp, entryID, dumpPath, dumpType](
5565cb1dd27SAsmitha Karunanithi             const boost::system::error_code ec, GetManagedObjectsType& resp) {
5575cb1dd27SAsmitha Karunanithi             if (ec)
5585cb1dd27SAsmitha Karunanithi             {
5595cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5605cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5615cb1dd27SAsmitha Karunanithi                 return;
5625cb1dd27SAsmitha Karunanithi             }
5635cb1dd27SAsmitha Karunanithi 
564b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
565b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
566b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
567b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
568b47452b2SAsmitha Karunanithi                 "/entry/";
569b47452b2SAsmitha Karunanithi 
5705cb1dd27SAsmitha Karunanithi             for (auto& objectPath : resp)
5715cb1dd27SAsmitha Karunanithi             {
572b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5735cb1dd27SAsmitha Karunanithi                 {
5745cb1dd27SAsmitha Karunanithi                     continue;
5755cb1dd27SAsmitha Karunanithi                 }
5765cb1dd27SAsmitha Karunanithi 
5775cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5785cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
5795cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
5805cb1dd27SAsmitha Karunanithi 
5815cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : objectPath.second)
5825cb1dd27SAsmitha Karunanithi                 {
5835cb1dd27SAsmitha Karunanithi                     if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
5845cb1dd27SAsmitha Karunanithi                     {
5855cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
5865cb1dd27SAsmitha Karunanithi                         {
5875cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
5885cb1dd27SAsmitha Karunanithi                             {
5895cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
5905cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
5915cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
5925cb1dd27SAsmitha Karunanithi                                 {
5935cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
5945cb1dd27SAsmitha Karunanithi                                     break;
5955cb1dd27SAsmitha Karunanithi                                 }
5965cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
5975cb1dd27SAsmitha Karunanithi                                 break;
5985cb1dd27SAsmitha Karunanithi                             }
5995cb1dd27SAsmitha Karunanithi                         }
6005cb1dd27SAsmitha Karunanithi                     }
6015cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6025cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6035cb1dd27SAsmitha Karunanithi                     {
6045cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6055cb1dd27SAsmitha Karunanithi                         {
6065cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6075cb1dd27SAsmitha Karunanithi                             {
6085cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6095cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6105cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6115cb1dd27SAsmitha Karunanithi                                 {
6125cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6135cb1dd27SAsmitha Karunanithi                                     break;
6145cb1dd27SAsmitha Karunanithi                                 }
6155cb1dd27SAsmitha Karunanithi                                 timestamp =
6165cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
6175cb1dd27SAsmitha Karunanithi                                 break;
6185cb1dd27SAsmitha Karunanithi                             }
6195cb1dd27SAsmitha Karunanithi                         }
6205cb1dd27SAsmitha Karunanithi                     }
6215cb1dd27SAsmitha Karunanithi                 }
6225cb1dd27SAsmitha Karunanithi 
6235cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
624d337bb72SAsmitha Karunanithi                     "#LogEntry.v1_7_0.LogEntry";
6255cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6265cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6275cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6285cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6295cb1dd27SAsmitha Karunanithi                     crow::utility::getDateTime(timestamp);
6305cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6315cb1dd27SAsmitha Karunanithi 
632d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6335cb1dd27SAsmitha Karunanithi 
6345cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6355cb1dd27SAsmitha Karunanithi                 {
636d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
637d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
638de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
639de8d94a3SAbhishek Patel                         entryID + "/attachment";
6405cb1dd27SAsmitha Karunanithi                 }
6415cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6425cb1dd27SAsmitha Karunanithi                 {
643d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
644d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6455cb1dd27SAsmitha Karunanithi                         "System";
646d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
647de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
648de8d94a3SAbhishek Patel                         entryID + "/attachment";
6495cb1dd27SAsmitha Karunanithi                 }
6505cb1dd27SAsmitha Karunanithi             }
651b47452b2SAsmitha Karunanithi             if (foundDumpEntry == false)
652b47452b2SAsmitha Karunanithi             {
653b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
654b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
655b47452b2SAsmitha Karunanithi                 return;
656b47452b2SAsmitha Karunanithi             }
6575cb1dd27SAsmitha Karunanithi         },
6585cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6595cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6605cb1dd27SAsmitha Karunanithi }
6615cb1dd27SAsmitha Karunanithi 
6628d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6639878256fSStanley Chu                             const std::string& entryID,
664b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6655cb1dd27SAsmitha Karunanithi {
6663de8d8baSGeorge Liu     auto respHandler = [asyncResp,
6673de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
6685cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6695cb1dd27SAsmitha Karunanithi         if (ec)
6705cb1dd27SAsmitha Karunanithi         {
6713de8d8baSGeorge Liu             if (ec.value() == EBADR)
6723de8d8baSGeorge Liu             {
6733de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6743de8d8baSGeorge Liu                 return;
6753de8d8baSGeorge Liu             }
6765cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
6775cb1dd27SAsmitha Karunanithi                              << ec;
6785cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6795cb1dd27SAsmitha Karunanithi             return;
6805cb1dd27SAsmitha Karunanithi         }
6815cb1dd27SAsmitha Karunanithi     };
6825cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6835cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
684b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
685b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
686b47452b2SAsmitha Karunanithi             entryID,
6875cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6885cb1dd27SAsmitha Karunanithi }
6895cb1dd27SAsmitha Karunanithi 
6908d1b46d7Szhanghch05 inline void
6918d1b46d7Szhanghch05     createDumpTaskCallback(const crow::Request& req,
6928d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6938d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
694a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
695a43be80fSAsmitha Karunanithi {
696a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
6976145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
698a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
699a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
700cb13a392SEd Tanous             if (err)
701cb13a392SEd Tanous             {
7026145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7036145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7046145ed6fSAsmitha Karunanithi                 return task::completed;
705cb13a392SEd Tanous             }
706a43be80fSAsmitha Karunanithi             std::vector<std::pair<
707a43be80fSAsmitha Karunanithi                 std::string,
708a43be80fSAsmitha Karunanithi                 std::vector<std::pair<std::string, std::variant<std::string>>>>>
709a43be80fSAsmitha Karunanithi                 interfacesList;
710a43be80fSAsmitha Karunanithi 
711a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
712a43be80fSAsmitha Karunanithi 
713a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
714a43be80fSAsmitha Karunanithi 
715b47452b2SAsmitha Karunanithi             if (objPath.str ==
716b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
717b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
718b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
719a43be80fSAsmitha Karunanithi             {
720a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
721a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
722a43be80fSAsmitha Karunanithi 
723a43be80fSAsmitha Karunanithi                 std::string headerLoc =
724a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
725a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
726a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
727a43be80fSAsmitha Karunanithi 
728a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
729b47452b2SAsmitha Karunanithi                 return task::completed;
7306145ed6fSAsmitha Karunanithi             }
731a43be80fSAsmitha Karunanithi             return task::completed;
732a43be80fSAsmitha Karunanithi         },
733a43be80fSAsmitha Karunanithi         "type='signal',interface='org.freedesktop.DBus."
734a43be80fSAsmitha Karunanithi         "ObjectManager',"
735a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
736a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
737a43be80fSAsmitha Karunanithi 
738a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
739a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
740a43be80fSAsmitha Karunanithi     task->payload.emplace(req);
741a43be80fSAsmitha Karunanithi }
742a43be80fSAsmitha Karunanithi 
7438d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7448d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
745a43be80fSAsmitha Karunanithi {
746a43be80fSAsmitha Karunanithi 
747a43be80fSAsmitha Karunanithi     std::string dumpPath;
748a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
749a43be80fSAsmitha Karunanithi     {
750a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
751a43be80fSAsmitha Karunanithi     }
752a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
753a43be80fSAsmitha Karunanithi     {
754a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
755a43be80fSAsmitha Karunanithi     }
756a43be80fSAsmitha Karunanithi     else
757a43be80fSAsmitha Karunanithi     {
758a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
759a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
760a43be80fSAsmitha Karunanithi         return;
761a43be80fSAsmitha Karunanithi     }
762a43be80fSAsmitha Karunanithi 
763a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
764a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
765a43be80fSAsmitha Karunanithi 
766a43be80fSAsmitha Karunanithi     if (!redfish::json_util::readJson(
767a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
768a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
769a43be80fSAsmitha Karunanithi     {
770a43be80fSAsmitha Karunanithi         return;
771a43be80fSAsmitha Karunanithi     }
772a43be80fSAsmitha Karunanithi 
773a43be80fSAsmitha Karunanithi     if (dumpType == "System")
774a43be80fSAsmitha Karunanithi     {
775a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
776a43be80fSAsmitha Karunanithi         {
777a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
778a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType'/"
779a43be80fSAsmitha Karunanithi                                 "'OEMDiagnosticDataType' value not found!";
780a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
781a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
782a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
783a43be80fSAsmitha Karunanithi             return;
784a43be80fSAsmitha Karunanithi         }
7853174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
786a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
787a43be80fSAsmitha Karunanithi         {
788a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
789a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
790a43be80fSAsmitha Karunanithi                                     "System Dump creation parameters");
791a43be80fSAsmitha Karunanithi             return;
792a43be80fSAsmitha Karunanithi         }
793a43be80fSAsmitha Karunanithi     }
794a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
795a43be80fSAsmitha Karunanithi     {
796a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
797a43be80fSAsmitha Karunanithi         {
798a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
799a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType' not found!";
800a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
801a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
802a43be80fSAsmitha Karunanithi             return;
803a43be80fSAsmitha Karunanithi         }
8043174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
805a43be80fSAsmitha Karunanithi         {
806a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
807a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
808a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
809a43be80fSAsmitha Karunanithi                                     "BMC Dump creation parameters");
810a43be80fSAsmitha Karunanithi             return;
811a43be80fSAsmitha Karunanithi         }
812a43be80fSAsmitha Karunanithi     }
813a43be80fSAsmitha Karunanithi 
814a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
815a43be80fSAsmitha Karunanithi         [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
816a43be80fSAsmitha Karunanithi                                              const uint32_t& dumpId) {
817a43be80fSAsmitha Karunanithi             if (ec)
818a43be80fSAsmitha Karunanithi             {
819a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
820a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
821a43be80fSAsmitha Karunanithi                 return;
822a43be80fSAsmitha Karunanithi             }
823a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
824a43be80fSAsmitha Karunanithi 
825a43be80fSAsmitha Karunanithi             createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
826a43be80fSAsmitha Karunanithi         },
827b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
828b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
829b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
830a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
831a43be80fSAsmitha Karunanithi }
832a43be80fSAsmitha Karunanithi 
8338d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8348d1b46d7Szhanghch05                       const std::string& dumpType)
83580319af1SAsmitha Karunanithi {
836b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
837b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8388d1b46d7Szhanghch05 
83980319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
840b47452b2SAsmitha Karunanithi         [asyncResp, dumpType](const boost::system::error_code ec,
84180319af1SAsmitha Karunanithi                               const std::vector<std::string>& subTreePaths) {
84280319af1SAsmitha Karunanithi             if (ec)
84380319af1SAsmitha Karunanithi             {
84480319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
84580319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
84680319af1SAsmitha Karunanithi                 return;
84780319af1SAsmitha Karunanithi             }
84880319af1SAsmitha Karunanithi 
84980319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
85080319af1SAsmitha Karunanithi             {
8512dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8522dfd18efSEd Tanous                 std::string logID = objPath.filename();
8532dfd18efSEd Tanous                 if (logID.empty())
85480319af1SAsmitha Karunanithi                 {
8552dfd18efSEd Tanous                     continue;
85680319af1SAsmitha Karunanithi                 }
8572dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
85880319af1SAsmitha Karunanithi             }
85980319af1SAsmitha Karunanithi         },
86080319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
86180319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
86280319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
863b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
864b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
865b47452b2SAsmitha Karunanithi                                    dumpType});
86680319af1SAsmitha Karunanithi }
86780319af1SAsmitha Karunanithi 
868*7e860f15SJohn Edward Broadbent inline static void parseCrashdumpParameters(
869043a0536SJohnathan Mantey     const std::vector<std::pair<std::string, VariantType>>& params,
870043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
871043a0536SJohnathan Mantey {
872043a0536SJohnathan Mantey     for (auto property : params)
873043a0536SJohnathan Mantey     {
874043a0536SJohnathan Mantey         if (property.first == "Timestamp")
875043a0536SJohnathan Mantey         {
876043a0536SJohnathan Mantey             const std::string* value =
8778d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
878043a0536SJohnathan Mantey             if (value != nullptr)
879043a0536SJohnathan Mantey             {
880043a0536SJohnathan Mantey                 timestamp = *value;
881043a0536SJohnathan Mantey             }
882043a0536SJohnathan Mantey         }
883043a0536SJohnathan Mantey         else if (property.first == "Filename")
884043a0536SJohnathan Mantey         {
885043a0536SJohnathan Mantey             const std::string* value =
8868d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
887043a0536SJohnathan Mantey             if (value != nullptr)
888043a0536SJohnathan Mantey             {
889043a0536SJohnathan Mantey                 filename = *value;
890043a0536SJohnathan Mantey             }
891043a0536SJohnathan Mantey         }
892043a0536SJohnathan Mantey         else if (property.first == "Log")
893043a0536SJohnathan Mantey         {
894043a0536SJohnathan Mantey             const std::string* value =
8958d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
896043a0536SJohnathan Mantey             if (value != nullptr)
897043a0536SJohnathan Mantey             {
898043a0536SJohnathan Mantey                 logfile = *value;
899043a0536SJohnathan Mantey             }
900043a0536SJohnathan Mantey         }
901043a0536SJohnathan Mantey     }
902043a0536SJohnathan Mantey }
903043a0536SJohnathan Mantey 
904a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
905*7e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9061da66f75SEd Tanous {
907c4bf6374SJason M. Bills     /**
908c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
909c4bf6374SJason M. Bills      */
910*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
911*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
912*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
913*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
914*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
915*7e860f15SJohn Edward Broadbent 
916c4bf6374SJason M. Bills             {
917*7e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
918*7e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
919c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
920c4bf6374SJason M. Bills                     "#LogServiceCollection.LogServiceCollection";
921c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
922029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices";
923*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
924*7e860f15SJohn Edward Broadbent                     "System Log Services Collection";
925c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
926c4bf6374SJason M. Bills                     "Collection of LogServices for this Computer System";
927*7e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
928*7e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
929c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
930029573d4SEd Tanous                 logServiceArray.push_back(
931*7e860f15SJohn Edward Broadbent                     {{"@odata.id",
932*7e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/EventLog"}});
9335cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
934c9bb6861Sraviteja-b                 logServiceArray.push_back(
935*7e860f15SJohn Edward Broadbent                     {{"@odata.id",
936*7e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/Dump"}});
937c9bb6861Sraviteja-b #endif
938c9bb6861Sraviteja-b 
939d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
940d53dd41fSJason M. Bills                 logServiceArray.push_back(
941cb92c03bSAndrew Geissler                     {{"@odata.id",
942424c4176SJason M. Bills                       "/redfish/v1/Systems/system/LogServices/Crashdump"}});
943d53dd41fSJason M. Bills #endif
944c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
945c4bf6374SJason M. Bills                     logServiceArray.size();
946a3316fc6SZhikuiRen 
947a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
948a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec,
949a3316fc6SZhikuiRen                                 const std::vector<std::string>& subtreePath) {
950a3316fc6SZhikuiRen                         if (ec)
951a3316fc6SZhikuiRen                         {
952a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR << ec;
953a3316fc6SZhikuiRen                             return;
954a3316fc6SZhikuiRen                         }
955a3316fc6SZhikuiRen 
956a3316fc6SZhikuiRen                         for (auto& pathStr : subtreePath)
957a3316fc6SZhikuiRen                         {
958a3316fc6SZhikuiRen                             if (pathStr.find("PostCode") != std::string::npos)
959a3316fc6SZhikuiRen                             {
96023a21a1cSEd Tanous                                 nlohmann::json& logServiceArrayLocal =
961a3316fc6SZhikuiRen                                     asyncResp->res.jsonValue["Members"];
96223a21a1cSEd Tanous                                 logServiceArrayLocal.push_back(
963a3316fc6SZhikuiRen                                     {{"@odata.id", "/redfish/v1/Systems/system/"
964a3316fc6SZhikuiRen                                                    "LogServices/PostCodes"}});
965*7e860f15SJohn Edward Broadbent                                 asyncResp->res
966*7e860f15SJohn Edward Broadbent                                     .jsonValue["Members@odata.count"] =
96723a21a1cSEd Tanous                                     logServiceArrayLocal.size();
968a3316fc6SZhikuiRen                                 return;
969a3316fc6SZhikuiRen                             }
970a3316fc6SZhikuiRen                         }
971a3316fc6SZhikuiRen                     },
972a3316fc6SZhikuiRen                     "xyz.openbmc_project.ObjectMapper",
973a3316fc6SZhikuiRen                     "/xyz/openbmc_project/object_mapper",
974*7e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
975*7e860f15SJohn Edward Broadbent                     0, std::array<const char*, 1>{postCodeIface});
976*7e860f15SJohn Edward Broadbent             });
977c4bf6374SJason M. Bills }
978c4bf6374SJason M. Bills 
979*7e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
980c4bf6374SJason M. Bills {
981*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
982*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
983*7e860f15SJohn Edward Broadbent         .methods(
984*7e860f15SJohn Edward Broadbent             boost::beast::http::verb::
985*7e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
986*7e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
987c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
988029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
989c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
990c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
991c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
992*7e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
993*7e860f15SJohn Edward Broadbent                 "System Event Log Service";
994c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
995c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
996c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
997c4bf6374SJason M. Bills                 {"@odata.id",
998029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
999e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1000e7d6c8b2SGunnar Mills 
1001e7d6c8b2SGunnar Mills                 {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1002e7d6c8b2SGunnar Mills                            "Actions/LogService.ClearLog"}};
1003*7e860f15SJohn Edward Broadbent         });
1004489640c6SJason M. Bills }
1005489640c6SJason M. Bills 
1006*7e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1007489640c6SJason M. Bills {
1008*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1009489640c6SJason M. Bills                       "LogService.ClearLog/")
1010*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureComponents"})
1011*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
1012*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
1013*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1014489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1015489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1016489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1017489640c6SJason M. Bills                 {
1018489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1019489640c6SJason M. Bills                     {
1020489640c6SJason M. Bills                         std::error_code ec;
1021489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1022489640c6SJason M. Bills                     }
1023489640c6SJason M. Bills                 }
1024489640c6SJason M. Bills 
1025489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1026489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1027489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1028489640c6SJason M. Bills                         if (ec)
1029489640c6SJason M. Bills                         {
1030*7e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
1031*7e860f15SJohn Edward Broadbent                                              << ec;
1032489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1033489640c6SJason M. Bills                             return;
1034489640c6SJason M. Bills                         }
1035489640c6SJason M. Bills 
1036489640c6SJason M. Bills                         messages::success(asyncResp->res);
1037489640c6SJason M. Bills                     },
1038489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
1039*7e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
1040*7e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
1041*7e860f15SJohn Edward Broadbent             });
1042c4bf6374SJason M. Bills }
1043c4bf6374SJason M. Bills 
104495820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1045b5a76932SEd Tanous                                  const std::string& logEntry,
104695820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1047c4bf6374SJason M. Bills {
104895820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1049cd225da8SJason M. Bills     // First get the Timestamp
1050f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1051cd225da8SJason M. Bills     if (space == std::string::npos)
105295820184SJason M. Bills     {
105395820184SJason M. Bills         return 1;
105495820184SJason M. Bills     }
1055cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1056cd225da8SJason M. Bills     // Then get the log contents
1057f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1058cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1059cd225da8SJason M. Bills     {
1060cd225da8SJason M. Bills         return 1;
1061cd225da8SJason M. Bills     }
1062cd225da8SJason M. Bills     std::string_view entry(logEntry);
1063cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1064cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1065cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1066cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1067cd225da8SJason M. Bills                  boost::token_compress_on);
1068cd225da8SJason M. Bills     // We need at least a MessageId to be valid
1069cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
1070cd225da8SJason M. Bills     {
1071cd225da8SJason M. Bills         return 1;
1072cd225da8SJason M. Bills     }
1073cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
107495820184SJason M. Bills 
10754851d45dSJason M. Bills     // Get the Message from the MessageRegistry
10764851d45dSJason M. Bills     const message_registries::Message* message =
10774851d45dSJason M. Bills         message_registries::getMessage(messageID);
1078c4bf6374SJason M. Bills 
10794851d45dSJason M. Bills     std::string msg;
10804851d45dSJason M. Bills     std::string severity;
10814851d45dSJason M. Bills     if (message != nullptr)
1082c4bf6374SJason M. Bills     {
10834851d45dSJason M. Bills         msg = message->message;
10844851d45dSJason M. Bills         severity = message->severity;
1085c4bf6374SJason M. Bills     }
1086c4bf6374SJason M. Bills 
108715a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
108815a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
108915a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
109015a86ff6SJason M. Bills     {
109115a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
109215a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
109315a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
109415a86ff6SJason M. Bills         if (!messageArgsStart.empty())
109515a86ff6SJason M. Bills         {
109615a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
109715a86ff6SJason M. Bills         }
109815a86ff6SJason M. Bills 
109923a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1100c4bf6374SJason M. Bills 
11014851d45dSJason M. Bills         // Fill the MessageArgs into the Message
110295820184SJason M. Bills         int i = 0;
110395820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11044851d45dSJason M. Bills         {
110595820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11064851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11074851d45dSJason M. Bills             if (argPos != std::string::npos)
11084851d45dSJason M. Bills             {
110995820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11104851d45dSJason M. Bills             }
11114851d45dSJason M. Bills         }
111215a86ff6SJason M. Bills     }
11134851d45dSJason M. Bills 
111495820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
111595820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
111695820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1117f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1118f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
111995820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1120c4bf6374SJason M. Bills     {
112195820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1122c4bf6374SJason M. Bills     }
1123c4bf6374SJason M. Bills 
1124c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
112595820184SJason M. Bills     logEntryJson = {
1126cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1127029573d4SEd Tanous         {"@odata.id",
1128897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
112995820184SJason M. Bills              logEntryID},
1130c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
113195820184SJason M. Bills         {"Id", logEntryID},
113295820184SJason M. Bills         {"Message", std::move(msg)},
113395820184SJason M. Bills         {"MessageId", std::move(messageID)},
1134f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1135c4bf6374SJason M. Bills         {"EntryType", "Event"},
113695820184SJason M. Bills         {"Severity", std::move(severity)},
113795820184SJason M. Bills         {"Created", std::move(timestamp)}};
1138c4bf6374SJason M. Bills     return 0;
1139c4bf6374SJason M. Bills }
1140c4bf6374SJason M. Bills 
1141*7e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1142c4bf6374SJason M. Bills {
1143*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1144*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1145*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1146*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1147*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
1148*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1149271584abSEd Tanous                 uint64_t skip = 0;
1150271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
11518d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1152c4bf6374SJason M. Bills                 {
1153c4bf6374SJason M. Bills                     return;
1154c4bf6374SJason M. Bills                 }
11558d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1156c4bf6374SJason M. Bills                 {
1157c4bf6374SJason M. Bills                     return;
1158c4bf6374SJason M. Bills                 }
1159*7e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
1160*7e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1161c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1162c4bf6374SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
1163c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1164029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1165c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1166c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1167c4bf6374SJason M. Bills                     "Collection of System Event Log Entries";
1168cb92c03bSAndrew Geissler 
1169*7e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
1170*7e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1171c4bf6374SJason M. Bills                 logEntryArray = nlohmann::json::array();
1172*7e860f15SJohn Edward Broadbent                 // Go through the log files and create a unique ID for each
1173*7e860f15SJohn Edward Broadbent                 // entry
117495820184SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
117595820184SJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1176b01bf299SEd Tanous                 uint64_t entryCount = 0;
1177cd225da8SJason M. Bills                 std::string logEntry;
117895820184SJason M. Bills 
1179*7e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
1180*7e860f15SJohn Edward Broadbent                 // backwards
1181*7e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
1182*7e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1183c4bf6374SJason M. Bills                 {
1184cd225da8SJason M. Bills                     std::ifstream logStream(*it);
118595820184SJason M. Bills                     if (!logStream.is_open())
1186c4bf6374SJason M. Bills                     {
1187c4bf6374SJason M. Bills                         continue;
1188c4bf6374SJason M. Bills                     }
1189c4bf6374SJason M. Bills 
1190e85d6b16SJason M. Bills                     // Reset the unique ID on the first entry
1191e85d6b16SJason M. Bills                     bool firstEntry = true;
119295820184SJason M. Bills                     while (std::getline(logStream, logEntry))
119395820184SJason M. Bills                     {
1194c4bf6374SJason M. Bills                         entryCount++;
1195*7e860f15SJohn Edward Broadbent                         // Handle paging using skip (number of entries to skip
1196*7e860f15SJohn Edward Broadbent                         // from the start) and top (number of entries to
1197*7e860f15SJohn Edward Broadbent                         // display)
1198c4bf6374SJason M. Bills                         if (entryCount <= skip || entryCount > skip + top)
1199c4bf6374SJason M. Bills                         {
1200c4bf6374SJason M. Bills                             continue;
1201c4bf6374SJason M. Bills                         }
1202c4bf6374SJason M. Bills 
1203c4bf6374SJason M. Bills                         std::string idStr;
1204e85d6b16SJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1205c4bf6374SJason M. Bills                         {
1206c4bf6374SJason M. Bills                             continue;
1207c4bf6374SJason M. Bills                         }
1208c4bf6374SJason M. Bills 
1209e85d6b16SJason M. Bills                         if (firstEntry)
1210e85d6b16SJason M. Bills                         {
1211e85d6b16SJason M. Bills                             firstEntry = false;
1212e85d6b16SJason M. Bills                         }
1213e85d6b16SJason M. Bills 
1214c4bf6374SJason M. Bills                         logEntryArray.push_back({});
1215c4bf6374SJason M. Bills                         nlohmann::json& bmcLogEntry = logEntryArray.back();
1216*7e860f15SJohn Edward Broadbent                         if (fillEventLogEntryJson(idStr, logEntry,
1217*7e860f15SJohn Edward Broadbent                                                   bmcLogEntry) != 0)
1218c4bf6374SJason M. Bills                         {
1219c4bf6374SJason M. Bills                             messages::internalError(asyncResp->res);
1220c4bf6374SJason M. Bills                             return;
1221c4bf6374SJason M. Bills                         }
1222c4bf6374SJason M. Bills                     }
122395820184SJason M. Bills                 }
1224c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1225c4bf6374SJason M. Bills                 if (skip + top < entryCount)
1226c4bf6374SJason M. Bills                 {
1227c4bf6374SJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
122895820184SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/EventLog/"
122995820184SJason M. Bills                         "Entries?$skip=" +
1230c4bf6374SJason M. Bills                         std::to_string(skip + top);
1231c4bf6374SJason M. Bills                 }
1232*7e860f15SJohn Edward Broadbent             });
1233897967deSJason M. Bills }
1234897967deSJason M. Bills 
1235*7e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1236897967deSJason M. Bills {
1237*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1238*7e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1239*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1240*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1241*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
1242*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1243*7e860f15SJohn Edward Broadbent                const std::string& param) {
1244*7e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
12458d1b46d7Szhanghch05 
1246*7e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
1247*7e860f15SJohn Edward Broadbent                 // entry to find the target entry
1248897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1249897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1250897967deSJason M. Bills                 std::string logEntry;
1251897967deSJason M. Bills 
1252*7e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
1253*7e860f15SJohn Edward Broadbent                 // backwards
1254*7e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
1255*7e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1256897967deSJason M. Bills                 {
1257897967deSJason M. Bills                     std::ifstream logStream(*it);
1258897967deSJason M. Bills                     if (!logStream.is_open())
1259897967deSJason M. Bills                     {
1260897967deSJason M. Bills                         continue;
1261897967deSJason M. Bills                     }
1262897967deSJason M. Bills 
1263897967deSJason M. Bills                     // Reset the unique ID on the first entry
1264897967deSJason M. Bills                     bool firstEntry = true;
1265897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1266897967deSJason M. Bills                     {
1267897967deSJason M. Bills                         std::string idStr;
1268897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1269897967deSJason M. Bills                         {
1270897967deSJason M. Bills                             continue;
1271897967deSJason M. Bills                         }
1272897967deSJason M. Bills 
1273897967deSJason M. Bills                         if (firstEntry)
1274897967deSJason M. Bills                         {
1275897967deSJason M. Bills                             firstEntry = false;
1276897967deSJason M. Bills                         }
1277897967deSJason M. Bills 
1278897967deSJason M. Bills                         if (idStr == targetID)
1279897967deSJason M. Bills                         {
1280*7e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
1281*7e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1282897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1283897967deSJason M. Bills                             {
1284897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1285897967deSJason M. Bills                                 return;
1286897967deSJason M. Bills                             }
1287897967deSJason M. Bills                             return;
1288897967deSJason M. Bills                         }
1289897967deSJason M. Bills                     }
1290897967deSJason M. Bills                 }
1291897967deSJason M. Bills                 // Requested ID was not found
1292897967deSJason M. Bills                 messages::resourceMissingAtURI(asyncResp->res, targetID);
1293*7e860f15SJohn Edward Broadbent             });
129408a4e4b5SAnthony Wilson }
129508a4e4b5SAnthony Wilson 
1296*7e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
129708a4e4b5SAnthony Wilson {
1298*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
1299*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1300*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1301*7e860f15SJohn Edward Broadbent         .methods(
1302*7e860f15SJohn Edward Broadbent             boost::beast::http::verb::
1303*7e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
1304*7e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1305*7e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
1306*7e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
130708a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
130808a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
130908a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
131008a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
131108a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
131208a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
131308a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
131408a4e4b5SAnthony Wilson 
1315cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1316cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1317cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1318cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1319cb92c03bSAndrew Geissler                             GetManagedObjectsType& resp) {
1320cb92c03bSAndrew Geissler                     if (ec)
1321cb92c03bSAndrew Geissler                     {
1322cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1323cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1324cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1325cb92c03bSAndrew Geissler                             << ec;
1326cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1327cb92c03bSAndrew Geissler                         return;
1328cb92c03bSAndrew Geissler                     }
1329cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1330cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1331cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
1332cb92c03bSAndrew Geissler                     for (auto& objectPath : resp)
1333cb92c03bSAndrew Geissler                     {
133466664f25SEd Tanous                         uint32_t* id = nullptr;
133566664f25SEd Tanous                         std::time_t timestamp{};
1336d139c236SGeorge Liu                         std::time_t updateTimestamp{};
133766664f25SEd Tanous                         std::string* severity = nullptr;
133866664f25SEd Tanous                         std::string* message = nullptr;
1339f86bb901SAdriana Kobylak                         std::string* filePath = nullptr;
134075710de2SXiaochao Ma                         bool resolved = false;
1341f86bb901SAdriana Kobylak                         for (auto& interfaceMap : objectPath.second)
1342f86bb901SAdriana Kobylak                         {
1343f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1344f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1345f86bb901SAdriana Kobylak                             {
1346cb92c03bSAndrew Geissler                                 for (auto& propertyMap : interfaceMap.second)
1347cb92c03bSAndrew Geissler                                 {
1348cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1349cb92c03bSAndrew Geissler                                     {
1350f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1351f86bb901SAdriana Kobylak                                             &propertyMap.second);
1352cb92c03bSAndrew Geissler                                     }
1353cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1354cb92c03bSAndrew Geissler                                     {
1355cb92c03bSAndrew Geissler                                         const uint64_t* millisTimeStamp =
1356f86bb901SAdriana Kobylak                                             std::get_if<uint64_t>(
1357f86bb901SAdriana Kobylak                                                 &propertyMap.second);
1358ae34c8e8SAdriana Kobylak                                         if (millisTimeStamp != nullptr)
1359ebd45906SGeorge Liu                                         {
1360*7e860f15SJohn Edward Broadbent                                             timestamp =
1361*7e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
1362*7e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
1363*7e860f15SJohn Edward Broadbent                                         }
1364*7e860f15SJohn Edward Broadbent                                     }
1365*7e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
1366*7e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
1367*7e860f15SJohn Edward Broadbent                                     {
1368*7e860f15SJohn Edward Broadbent                                         const uint64_t* millisTimeStamp =
1369*7e860f15SJohn Edward Broadbent                                             std::get_if<uint64_t>(
1370*7e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
1371*7e860f15SJohn Edward Broadbent                                         if (millisTimeStamp != nullptr)
1372*7e860f15SJohn Edward Broadbent                                         {
1373*7e860f15SJohn Edward Broadbent                                             updateTimestamp =
1374*7e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
1375*7e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
1376*7e860f15SJohn Edward Broadbent                                         }
1377*7e860f15SJohn Edward Broadbent                                     }
1378*7e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
1379*7e860f15SJohn Edward Broadbent                                     {
1380*7e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
1381*7e860f15SJohn Edward Broadbent                                             &propertyMap.second);
1382*7e860f15SJohn Edward Broadbent                                     }
1383*7e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
1384*7e860f15SJohn Edward Broadbent                                     {
1385*7e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
1386*7e860f15SJohn Edward Broadbent                                             &propertyMap.second);
1387*7e860f15SJohn Edward Broadbent                                     }
1388*7e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
1389*7e860f15SJohn Edward Broadbent                                     {
1390*7e860f15SJohn Edward Broadbent                                         bool* resolveptr = std::get_if<bool>(
1391*7e860f15SJohn Edward Broadbent                                             &propertyMap.second);
1392*7e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
1393*7e860f15SJohn Edward Broadbent                                         {
1394*7e860f15SJohn Edward Broadbent                                             messages::internalError(
1395*7e860f15SJohn Edward Broadbent                                                 asyncResp->res);
1396*7e860f15SJohn Edward Broadbent                                             return;
1397*7e860f15SJohn Edward Broadbent                                         }
1398*7e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
1399*7e860f15SJohn Edward Broadbent                                     }
1400*7e860f15SJohn Edward Broadbent                                 }
1401*7e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
1402*7e860f15SJohn Edward Broadbent                                     severity == nullptr)
1403*7e860f15SJohn Edward Broadbent                                 {
1404*7e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
1405*7e860f15SJohn Edward Broadbent                                     return;
1406*7e860f15SJohn Edward Broadbent                                 }
1407*7e860f15SJohn Edward Broadbent                             }
1408*7e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
1409*7e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
1410*7e860f15SJohn Edward Broadbent                             {
1411*7e860f15SJohn Edward Broadbent                                 for (auto& propertyMap : interfaceMap.second)
1412*7e860f15SJohn Edward Broadbent                                 {
1413*7e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
1414*7e860f15SJohn Edward Broadbent                                     {
1415*7e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
1416*7e860f15SJohn Edward Broadbent                                             &propertyMap.second);
1417*7e860f15SJohn Edward Broadbent                                     }
1418*7e860f15SJohn Edward Broadbent                                 }
1419*7e860f15SJohn Edward Broadbent                             }
1420*7e860f15SJohn Edward Broadbent                         }
1421*7e860f15SJohn Edward Broadbent                         // Object path without the
1422*7e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
1423*7e860f15SJohn Edward Broadbent                         // and continue.
1424*7e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
1425*7e860f15SJohn Edward Broadbent                             severity == nullptr)
1426*7e860f15SJohn Edward Broadbent                         {
1427*7e860f15SJohn Edward Broadbent                             continue;
1428*7e860f15SJohn Edward Broadbent                         }
1429*7e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
1430*7e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
1431*7e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
1432*7e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
1433*7e860f15SJohn Edward Broadbent                             "/redfish/v1/Systems/system/"
1434*7e860f15SJohn Edward Broadbent                             "LogServices/EventLog/Entries/" +
1435*7e860f15SJohn Edward Broadbent                             std::to_string(*id);
1436*7e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
1437*7e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
1438*7e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
1439*7e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
1440*7e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
1441*7e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
1442*7e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
1443*7e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
1444*7e860f15SJohn Edward Broadbent                             crow::utility::getDateTime(timestamp);
1445*7e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
1446*7e860f15SJohn Edward Broadbent                             crow::utility::getDateTime(updateTimestamp);
1447*7e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
1448*7e860f15SJohn Edward Broadbent                         {
1449*7e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
1450*7e860f15SJohn Edward Broadbent                                 "/redfish/v1/Systems/system/LogServices/"
1451*7e860f15SJohn Edward Broadbent                                 "EventLog/"
1452*7e860f15SJohn Edward Broadbent                                 "Entries/" +
1453*7e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
1454*7e860f15SJohn Edward Broadbent                         }
1455*7e860f15SJohn Edward Broadbent                     }
1456*7e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
1457*7e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
1458*7e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
1459*7e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
1460*7e860f15SJohn Edward Broadbent                               });
1461*7e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
1462*7e860f15SJohn Edward Broadbent                         entriesArray.size();
1463*7e860f15SJohn Edward Broadbent                 },
1464*7e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
1465*7e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1466*7e860f15SJohn Edward Broadbent         });
1467*7e860f15SJohn Edward Broadbent }
1468*7e860f15SJohn Edward Broadbent 
1469*7e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
1470*7e860f15SJohn Edward Broadbent {
1471*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1472*7e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1473*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1474*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1475*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
1476*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1477*7e860f15SJohn Edward Broadbent                const std::string& param)
1478*7e860f15SJohn Edward Broadbent 
1479*7e860f15SJohn Edward Broadbent             {
1480*7e860f15SJohn Edward Broadbent                 std::string entryID = param;
1481*7e860f15SJohn Edward Broadbent                 dbus::utility::escapePathForDbus(entryID);
1482*7e860f15SJohn Edward Broadbent 
1483*7e860f15SJohn Edward Broadbent                 // DBus implementation of EventLog/Entries
1484*7e860f15SJohn Edward Broadbent                 // Make call to Logging Service to find all log entry objects
1485*7e860f15SJohn Edward Broadbent                 crow::connections::systemBus->async_method_call(
1486*7e860f15SJohn Edward Broadbent                     [asyncResp, entryID](const boost::system::error_code ec,
1487*7e860f15SJohn Edward Broadbent                                          GetManagedPropertyType& resp) {
1488*7e860f15SJohn Edward Broadbent                         if (ec.value() == EBADR)
1489*7e860f15SJohn Edward Broadbent                         {
1490*7e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
1491*7e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogEntry", entryID);
1492*7e860f15SJohn Edward Broadbent                             return;
1493*7e860f15SJohn Edward Broadbent                         }
1494*7e860f15SJohn Edward Broadbent                         if (ec)
1495*7e860f15SJohn Edward Broadbent                         {
1496*7e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "EventLogEntry (DBus) "
1497*7e860f15SJohn Edward Broadbent                                                 "resp_handler got error "
1498*7e860f15SJohn Edward Broadbent                                              << ec;
1499*7e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
1500*7e860f15SJohn Edward Broadbent                             return;
1501*7e860f15SJohn Edward Broadbent                         }
1502*7e860f15SJohn Edward Broadbent                         uint32_t* id = nullptr;
1503*7e860f15SJohn Edward Broadbent                         std::time_t timestamp{};
1504*7e860f15SJohn Edward Broadbent                         std::time_t updateTimestamp{};
1505*7e860f15SJohn Edward Broadbent                         std::string* severity = nullptr;
1506*7e860f15SJohn Edward Broadbent                         std::string* message = nullptr;
1507*7e860f15SJohn Edward Broadbent                         std::string* filePath = nullptr;
1508*7e860f15SJohn Edward Broadbent                         bool resolved = false;
1509*7e860f15SJohn Edward Broadbent 
1510*7e860f15SJohn Edward Broadbent                         for (auto& propertyMap : resp)
1511*7e860f15SJohn Edward Broadbent                         {
1512*7e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Id")
1513*7e860f15SJohn Edward Broadbent                             {
1514*7e860f15SJohn Edward Broadbent                                 id = std::get_if<uint32_t>(&propertyMap.second);
1515*7e860f15SJohn Edward Broadbent                             }
1516*7e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Timestamp")
1517*7e860f15SJohn Edward Broadbent                             {
1518*7e860f15SJohn Edward Broadbent                                 const uint64_t* millisTimeStamp =
1519*7e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1520*7e860f15SJohn Edward Broadbent                                 if (millisTimeStamp != nullptr)
1521*7e860f15SJohn Edward Broadbent                                 {
1522d139c236SGeorge Liu                                     timestamp = crow::utility::getTimestamp(
1523cb92c03bSAndrew Geissler                                         *millisTimeStamp);
1524d139c236SGeorge Liu                                 }
1525ebd45906SGeorge Liu                             }
1526d139c236SGeorge Liu                             else if (propertyMap.first == "UpdateTimestamp")
1527d139c236SGeorge Liu                             {
1528d139c236SGeorge Liu                                 const uint64_t* millisTimeStamp =
1529*7e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1530ae34c8e8SAdriana Kobylak                                 if (millisTimeStamp != nullptr)
1531ebd45906SGeorge Liu                                 {
1532ebd45906SGeorge Liu                                     updateTimestamp =
1533ebd45906SGeorge Liu                                         crow::utility::getTimestamp(
1534d139c236SGeorge Liu                                             *millisTimeStamp);
1535cb92c03bSAndrew Geissler                                 }
1536ebd45906SGeorge Liu                             }
1537cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1538cb92c03bSAndrew Geissler                             {
1539cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1540cb92c03bSAndrew Geissler                                     &propertyMap.second);
1541cb92c03bSAndrew Geissler                             }
1542cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1543cb92c03bSAndrew Geissler                             {
1544cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1545cb92c03bSAndrew Geissler                                     &propertyMap.second);
1546ae34c8e8SAdriana Kobylak                             }
154775710de2SXiaochao Ma                             else if (propertyMap.first == "Resolved")
154875710de2SXiaochao Ma                             {
154975710de2SXiaochao Ma                                 bool* resolveptr =
155075710de2SXiaochao Ma                                     std::get_if<bool>(&propertyMap.second);
155175710de2SXiaochao Ma                                 if (resolveptr == nullptr)
155275710de2SXiaochao Ma                                 {
155375710de2SXiaochao Ma                                     messages::internalError(asyncResp->res);
155475710de2SXiaochao Ma                                     return;
155575710de2SXiaochao Ma                                 }
155675710de2SXiaochao Ma                                 resolved = *resolveptr;
155775710de2SXiaochao Ma                             }
1558*7e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Path")
1559f86bb901SAdriana Kobylak                             {
1560f86bb901SAdriana Kobylak                                 filePath = std::get_if<std::string>(
1561f86bb901SAdriana Kobylak                                     &propertyMap.second);
1562f86bb901SAdriana Kobylak                             }
1563f86bb901SAdriana Kobylak                         }
1564f86bb901SAdriana Kobylak                         if (id == nullptr || message == nullptr ||
1565f86bb901SAdriana Kobylak                             severity == nullptr)
1566f86bb901SAdriana Kobylak                         {
1567ae34c8e8SAdriana Kobylak                             messages::internalError(asyncResp->res);
1568271584abSEd Tanous                             return;
1569271584abSEd Tanous                         }
1570f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.type"] =
1571f86bb901SAdriana Kobylak                             "#LogEntry.v1_8_0.LogEntry";
1572f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.id"] =
1573*7e860f15SJohn Edward Broadbent                             "/redfish/v1/Systems/system/LogServices/EventLog/"
1574*7e860f15SJohn Edward Broadbent                             "Entries/" +
1575f86bb901SAdriana Kobylak                             std::to_string(*id);
1576*7e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Name"] =
1577*7e860f15SJohn Edward Broadbent                             "System Event Log Entry";
1578f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1579f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Message"] = *message;
1580f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Resolved"] = resolved;
1581f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["EntryType"] = "Event";
1582f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Severity"] =
1583f86bb901SAdriana Kobylak                             translateSeverityDbusToRedfish(*severity);
1584f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Created"] =
1585f86bb901SAdriana Kobylak                             crow::utility::getDateTime(timestamp);
1586f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Modified"] =
1587f86bb901SAdriana Kobylak                             crow::utility::getDateTime(updateTimestamp);
1588f86bb901SAdriana Kobylak                         if (filePath != nullptr)
1589f86bb901SAdriana Kobylak                         {
1590f86bb901SAdriana Kobylak                             asyncResp->res.jsonValue["AdditionalDataURI"] =
1591*7e860f15SJohn Edward Broadbent                                 "/redfish/v1/Systems/system/LogServices/"
1592*7e860f15SJohn Edward Broadbent                                 "EventLog/"
1593*7e860f15SJohn Edward Broadbent                                 "attachment/" +
1594*7e860f15SJohn Edward Broadbent                                 std::to_string(*id);
1595f86bb901SAdriana Kobylak                         }
1596cb92c03bSAndrew Geissler                     },
1597cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Logging",
1598cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging/entry/" + entryID,
1599f86bb901SAdriana Kobylak                     "org.freedesktop.DBus.Properties", "GetAll", "");
1600*7e860f15SJohn Edward Broadbent             });
1601336e96c6SChicago Duan 
1602*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1603*7e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1604*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
1605*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
1606*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
1607*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1608*7e860f15SJohn Edward Broadbent                const std::string& entryId) {
160975710de2SXiaochao Ma                 std::optional<bool> resolved;
161075710de2SXiaochao Ma 
1611*7e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "Resolved",
1612*7e860f15SJohn Edward Broadbent                                          resolved))
161375710de2SXiaochao Ma                 {
161475710de2SXiaochao Ma                     return;
161575710de2SXiaochao Ma                 }
161675710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
161775710de2SXiaochao Ma 
161875710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
161975710de2SXiaochao Ma                     [asyncResp, resolved,
162075710de2SXiaochao Ma                      entryId](const boost::system::error_code ec) {
162175710de2SXiaochao Ma                         if (ec)
162275710de2SXiaochao Ma                         {
162375710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
162475710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
162575710de2SXiaochao Ma                             return;
162675710de2SXiaochao Ma                         }
162775710de2SXiaochao Ma                     },
162875710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
162975710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
163075710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
163175710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
163275710de2SXiaochao Ma                     std::variant<bool>(*resolved));
1633*7e860f15SJohn Edward Broadbent             });
163475710de2SXiaochao Ma 
1635*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
1636*7e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1637*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
1638*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
1639*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
1640*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1641*7e860f15SJohn Edward Broadbent                const std::string& param)
1642*7e860f15SJohn Edward Broadbent 
1643336e96c6SChicago Duan             {
1644336e96c6SChicago Duan                 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1645336e96c6SChicago Duan 
1646*7e860f15SJohn Edward Broadbent                 std::string entryID = param;
1647336e96c6SChicago Duan 
1648336e96c6SChicago Duan                 dbus::utility::escapePathForDbus(entryID);
1649336e96c6SChicago Duan 
1650336e96c6SChicago Duan                 // Process response from Logging service.
1651*7e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp, entryID](
1652*7e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
1653*7e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
1654*7e860f15SJohn Edward Broadbent                         << "EventLogEntry (DBus) doDelete callback: Done";
1655336e96c6SChicago Duan                     if (ec)
1656336e96c6SChicago Duan                     {
16573de8d8baSGeorge Liu                         if (ec.value() == EBADR)
16583de8d8baSGeorge Liu                         {
1659*7e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
1660*7e860f15SJohn Edward Broadbent                                                        "LogEntry", entryID);
16613de8d8baSGeorge Liu                             return;
16623de8d8baSGeorge Liu                         }
1663336e96c6SChicago Duan                         // TODO Handle for specific error code
1664*7e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "EventLogEntry (DBus) doDelete "
1665*7e860f15SJohn Edward Broadbent                                             "respHandler got error "
1666336e96c6SChicago Duan                                          << ec;
1667336e96c6SChicago Duan                         asyncResp->res.result(
1668336e96c6SChicago Duan                             boost::beast::http::status::internal_server_error);
1669336e96c6SChicago Duan                         return;
1670336e96c6SChicago Duan                     }
1671336e96c6SChicago Duan 
1672336e96c6SChicago Duan                     asyncResp->res.result(boost::beast::http::status::ok);
1673336e96c6SChicago Duan                 };
1674336e96c6SChicago Duan 
1675336e96c6SChicago Duan                 // Make call to Logging service to request Delete Log
1676336e96c6SChicago Duan                 crow::connections::systemBus->async_method_call(
1677336e96c6SChicago Duan                     respHandler, "xyz.openbmc_project.Logging",
1678336e96c6SChicago Duan                     "/xyz/openbmc_project/logging/entry/" + entryID,
1679336e96c6SChicago Duan                     "xyz.openbmc_project.Object.Delete", "Delete");
1680*7e860f15SJohn Edward Broadbent             });
1681400fd1fbSAdriana Kobylak }
1682400fd1fbSAdriana Kobylak 
1683*7e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1684400fd1fbSAdriana Kobylak {
1685*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/"
1686*7e860f15SJohn Edward Broadbent                       "<str>/attachment")
1687*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1688*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1689*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
1690*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1691*7e860f15SJohn Edward Broadbent                const std::string& param)
1692400fd1fbSAdriana Kobylak 
1693*7e860f15SJohn Edward Broadbent             {
1694400fd1fbSAdriana Kobylak                 std::string_view acceptHeader = req.getHeaderValue("Accept");
1695*7e860f15SJohn Edward Broadbent                 // The iterators in boost/http/rfc7230.hpp end the string if '/'
1696*7e860f15SJohn Edward Broadbent                 // is found, so replace it with arbitrary character '|' which is
1697*7e860f15SJohn Edward Broadbent                 // not part of the Accept header syntax.
1698*7e860f15SJohn Edward Broadbent                 std::string acceptStr = boost::replace_all_copy(
1699*7e860f15SJohn Edward Broadbent                     std::string(acceptHeader), "/", "|");
1700400fd1fbSAdriana Kobylak                 boost::beast::http::ext_list acceptTypes{acceptStr};
1701400fd1fbSAdriana Kobylak                 bool supported = false;
1702400fd1fbSAdriana Kobylak                 for (const auto& type : acceptTypes)
1703400fd1fbSAdriana Kobylak                 {
1704400fd1fbSAdriana Kobylak                     if ((type.first == "*|*") ||
1705400fd1fbSAdriana Kobylak                         (type.first == "application|octet-stream"))
1706400fd1fbSAdriana Kobylak                     {
1707400fd1fbSAdriana Kobylak                         supported = true;
1708400fd1fbSAdriana Kobylak                         break;
1709400fd1fbSAdriana Kobylak                     }
1710400fd1fbSAdriana Kobylak                 }
1711400fd1fbSAdriana Kobylak                 if (!supported)
1712400fd1fbSAdriana Kobylak                 {
1713*7e860f15SJohn Edward Broadbent                     asyncResp->res.result(
1714*7e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1715400fd1fbSAdriana Kobylak                     return;
1716400fd1fbSAdriana Kobylak                 }
1717400fd1fbSAdriana Kobylak 
1718*7e860f15SJohn Edward Broadbent                 std::string entryID = param;
1719400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1720400fd1fbSAdriana Kobylak 
1721400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
1722*7e860f15SJohn Edward Broadbent                     [asyncResp,
1723*7e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1724400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1725400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1726400fd1fbSAdriana Kobylak                         {
1727*7e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
1728*7e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1729400fd1fbSAdriana Kobylak                             return;
1730400fd1fbSAdriana Kobylak                         }
1731400fd1fbSAdriana Kobylak                         if (ec)
1732400fd1fbSAdriana Kobylak                         {
1733400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1734400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1735400fd1fbSAdriana Kobylak                             return;
1736400fd1fbSAdriana Kobylak                         }
1737400fd1fbSAdriana Kobylak 
1738400fd1fbSAdriana Kobylak                         int fd = -1;
1739400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1740400fd1fbSAdriana Kobylak                         if (fd == -1)
1741400fd1fbSAdriana Kobylak                         {
1742400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1743400fd1fbSAdriana Kobylak                             return;
1744400fd1fbSAdriana Kobylak                         }
1745400fd1fbSAdriana Kobylak 
1746400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1747400fd1fbSAdriana Kobylak                         if (size == -1)
1748400fd1fbSAdriana Kobylak                         {
1749400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1750400fd1fbSAdriana Kobylak                             return;
1751400fd1fbSAdriana Kobylak                         }
1752400fd1fbSAdriana Kobylak 
1753400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1754400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1755400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1756400fd1fbSAdriana Kobylak                         {
1757400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1758400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1759400fd1fbSAdriana Kobylak                                 << maxFileSize;
1760400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1761400fd1fbSAdriana Kobylak                             return;
1762400fd1fbSAdriana Kobylak                         }
1763400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1764400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1765400fd1fbSAdriana Kobylak                         if (rc == -1)
1766400fd1fbSAdriana Kobylak                         {
1767400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1768400fd1fbSAdriana Kobylak                             return;
1769400fd1fbSAdriana Kobylak                         }
1770400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1771400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1772400fd1fbSAdriana Kobylak                         {
1773400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1774400fd1fbSAdriana Kobylak                             return;
1775400fd1fbSAdriana Kobylak                         }
1776400fd1fbSAdriana Kobylak                         close(fd);
1777400fd1fbSAdriana Kobylak 
1778400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
1779*7e860f15SJohn Edward Broadbent                         std::string output =
1780*7e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1781400fd1fbSAdriana Kobylak 
1782400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1783400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
1784*7e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
1785*7e860f15SJohn Edward Broadbent                                                  "Base64");
1786400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1787400fd1fbSAdriana Kobylak                     },
1788400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1789400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1790400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
1791*7e860f15SJohn Edward Broadbent             });
17921da66f75SEd Tanous }
17931da66f75SEd Tanous 
1794*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
17951da66f75SEd Tanous {
1796*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
1797*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1798*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1799*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
1800*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1801*7e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
1802*7e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1803e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
18041da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
1805e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1806e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
1807*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
1808*7e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
1809e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
18101da66f75SEd Tanous                     "Collection of LogServices for this Manager";
1811*7e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
1812*7e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1813c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
18145cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
18155cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
1816*7e860f15SJohn Edward Broadbent                     {{"@odata.id",
1817*7e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
18185cb1dd27SAsmitha Karunanithi #endif
1819c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1820c4bf6374SJason M. Bills                 logServiceArray.push_back(
1821*7e860f15SJohn Edward Broadbent                     {{"@odata.id",
1822*7e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1823c4bf6374SJason M. Bills #endif
1824e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
1825c4bf6374SJason M. Bills                     logServiceArray.size();
1826*7e860f15SJohn Edward Broadbent             });
1827e1f26343SJason M. Bills }
1828e1f26343SJason M. Bills 
1829*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
1830e1f26343SJason M. Bills {
1831*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1832*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1833*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1834*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
1835*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
18368d1b46d7Szhanghch05 
1837*7e860f15SJohn Edward Broadbent             {
1838e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1839e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
18400f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
18410f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
1842*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
1843*7e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
1844*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
1845*7e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
1846c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
1847e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
1848cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
1849cd50aa42SJason M. Bills                     {"@odata.id",
1850086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
1851*7e860f15SJohn Edward Broadbent             });
1852e1f26343SJason M. Bills }
1853e1f26343SJason M. Bills 
1854c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1855e1f26343SJason M. Bills                                       sd_journal* journal,
1856c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
1857e1f26343SJason M. Bills {
1858e1f26343SJason M. Bills     // Get the Log Entry contents
1859e1f26343SJason M. Bills     int ret = 0;
1860e1f26343SJason M. Bills 
1861a8fe54f0SJason M. Bills     std::string message;
1862a8fe54f0SJason M. Bills     std::string_view syslogID;
1863a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1864a8fe54f0SJason M. Bills     if (ret < 0)
1865a8fe54f0SJason M. Bills     {
1866a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1867a8fe54f0SJason M. Bills                          << strerror(-ret);
1868a8fe54f0SJason M. Bills     }
1869a8fe54f0SJason M. Bills     if (!syslogID.empty())
1870a8fe54f0SJason M. Bills     {
1871a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
1872a8fe54f0SJason M. Bills     }
1873a8fe54f0SJason M. Bills 
187439e77504SEd Tanous     std::string_view msg;
187516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1876e1f26343SJason M. Bills     if (ret < 0)
1877e1f26343SJason M. Bills     {
1878e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1879e1f26343SJason M. Bills         return 1;
1880e1f26343SJason M. Bills     }
1881a8fe54f0SJason M. Bills     message += std::string(msg);
1882e1f26343SJason M. Bills 
1883e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1884271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
188516428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1886e1f26343SJason M. Bills     if (ret < 0)
1887e1f26343SJason M. Bills     {
1888e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1889e1f26343SJason M. Bills     }
1890e1f26343SJason M. Bills 
1891e1f26343SJason M. Bills     // Get the Created time from the timestamp
189216428a1aSJason M. Bills     std::string entryTimeStr;
189316428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1894e1f26343SJason M. Bills     {
189516428a1aSJason M. Bills         return 1;
1896e1f26343SJason M. Bills     }
1897e1f26343SJason M. Bills 
1898e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1899c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1900cb92c03bSAndrew Geissler         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1901c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1902c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1903e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1904c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
1905a8fe54f0SJason M. Bills         {"Message", std::move(message)},
1906e1f26343SJason M. Bills         {"EntryType", "Oem"},
1907738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
1908738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
1909738c1e61SPatrick Williams                                      : "OK"},
1910086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
1911e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1912e1f26343SJason M. Bills     return 0;
1913e1f26343SJason M. Bills }
1914e1f26343SJason M. Bills 
1915*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
1916e1f26343SJason M. Bills {
1917*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1918*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
1919*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1920*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
1921*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1922193ad2faSJason M. Bills                 static constexpr const long maxEntriesPerPage = 1000;
1923271584abSEd Tanous                 uint64_t skip = 0;
1924271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
19258d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1926193ad2faSJason M. Bills                 {
1927193ad2faSJason M. Bills                     return;
1928193ad2faSJason M. Bills                 }
19298d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1930193ad2faSJason M. Bills                 {
1931193ad2faSJason M. Bills                     return;
1932193ad2faSJason M. Bills                 }
1933*7e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
1934*7e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1935e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1936e1f26343SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
19370f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
19380f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1939e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1940e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1941e1f26343SJason M. Bills                     "Collection of BMC Journal Entries";
1942*7e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
1943*7e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1944e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
1945e1f26343SJason M. Bills 
1946*7e860f15SJohn Edward Broadbent                 // Go through the journal and use the timestamp to create a
1947*7e860f15SJohn Edward Broadbent                 // unique ID for each entry
1948e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
1949e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1950e1f26343SJason M. Bills                 if (ret < 0)
1951e1f26343SJason M. Bills                 {
1952*7e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
1953*7e860f15SJohn Edward Broadbent                                      << strerror(-ret);
1954f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1955e1f26343SJason M. Bills                     return;
1956e1f26343SJason M. Bills                 }
1957*7e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
1958*7e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
1959e1f26343SJason M. Bills                 journalTmp = nullptr;
1960b01bf299SEd Tanous                 uint64_t entryCount = 0;
1961e85d6b16SJason M. Bills                 // Reset the unique ID on the first entry
1962e85d6b16SJason M. Bills                 bool firstEntry = true;
1963e1f26343SJason M. Bills                 SD_JOURNAL_FOREACH(journal.get())
1964e1f26343SJason M. Bills                 {
1965193ad2faSJason M. Bills                     entryCount++;
1966*7e860f15SJohn Edward Broadbent                     // Handle paging using skip (number of entries to skip from
1967*7e860f15SJohn Edward Broadbent                     // the start) and top (number of entries to display)
1968193ad2faSJason M. Bills                     if (entryCount <= skip || entryCount > skip + top)
1969193ad2faSJason M. Bills                     {
1970193ad2faSJason M. Bills                         continue;
1971193ad2faSJason M. Bills                     }
1972193ad2faSJason M. Bills 
197316428a1aSJason M. Bills                     std::string idStr;
1974e85d6b16SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
1975e1f26343SJason M. Bills                     {
1976e1f26343SJason M. Bills                         continue;
1977e1f26343SJason M. Bills                     }
1978e1f26343SJason M. Bills 
1979e85d6b16SJason M. Bills                     if (firstEntry)
1980e85d6b16SJason M. Bills                     {
1981e85d6b16SJason M. Bills                         firstEntry = false;
1982e85d6b16SJason M. Bills                     }
1983e85d6b16SJason M. Bills 
1984e1f26343SJason M. Bills                     logEntryArray.push_back({});
1985c4bf6374SJason M. Bills                     nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
1986c4bf6374SJason M. Bills                     if (fillBMCJournalLogEntryJson(idStr, journal.get(),
1987c4bf6374SJason M. Bills                                                    bmcJournalLogEntry) != 0)
1988e1f26343SJason M. Bills                     {
1989f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
1990e1f26343SJason M. Bills                         return;
1991e1f26343SJason M. Bills                     }
1992e1f26343SJason M. Bills                 }
1993193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1994193ad2faSJason M. Bills                 if (skip + top < entryCount)
1995193ad2faSJason M. Bills                 {
1996193ad2faSJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
1997*7e860f15SJohn Edward Broadbent                         "/redfish/v1/Managers/bmc/LogServices/Journal/"
1998*7e860f15SJohn Edward Broadbent                         "Entries?$skip=" +
1999193ad2faSJason M. Bills                         std::to_string(skip + top);
2000193ad2faSJason M. Bills                 }
2001*7e860f15SJohn Edward Broadbent             });
2002e1f26343SJason M. Bills }
2003e1f26343SJason M. Bills 
2004*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2005e1f26343SJason M. Bills {
2006*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2007*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2008*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2009*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2010*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2011*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2012*7e860f15SJohn Edward Broadbent                const std::string& entryID) {
2013e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2014e1f26343SJason M. Bills                 uint64_t ts = 0;
2015271584abSEd Tanous                 uint64_t index = 0;
20168d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2017e1f26343SJason M. Bills                 {
201816428a1aSJason M. Bills                     return;
2019e1f26343SJason M. Bills                 }
2020e1f26343SJason M. Bills 
2021e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2022e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2023e1f26343SJason M. Bills                 if (ret < 0)
2024e1f26343SJason M. Bills                 {
2025*7e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
2026*7e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2027f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2028e1f26343SJason M. Bills                     return;
2029e1f26343SJason M. Bills                 }
2030*7e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
2031*7e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2032e1f26343SJason M. Bills                 journalTmp = nullptr;
2033*7e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
2034*7e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2035af07e3f5SJason M. Bills                 std::string idStr;
2036af07e3f5SJason M. Bills                 bool firstEntry = true;
2037e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
20382056b6d1SManojkiran Eda                 if (ret < 0)
20392056b6d1SManojkiran Eda                 {
20402056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
20412056b6d1SManojkiran Eda                                      << strerror(-ret);
20422056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
20432056b6d1SManojkiran Eda                     return;
20442056b6d1SManojkiran Eda                 }
2045271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2046e1f26343SJason M. Bills                 {
2047e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2048af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2049af07e3f5SJason M. Bills                     {
2050af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2051af07e3f5SJason M. Bills                         return;
2052af07e3f5SJason M. Bills                     }
2053af07e3f5SJason M. Bills                     if (firstEntry)
2054af07e3f5SJason M. Bills                     {
2055af07e3f5SJason M. Bills                         firstEntry = false;
2056af07e3f5SJason M. Bills                     }
2057e1f26343SJason M. Bills                 }
2058c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2059af07e3f5SJason M. Bills                 if (idStr != entryID)
2060c4bf6374SJason M. Bills                 {
2061c4bf6374SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, entryID);
2062c4bf6374SJason M. Bills                     return;
2063c4bf6374SJason M. Bills                 }
2064c4bf6374SJason M. Bills 
2065c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2066e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2067e1f26343SJason M. Bills                 {
2068f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2069e1f26343SJason M. Bills                     return;
2070e1f26343SJason M. Bills                 }
2071*7e860f15SJohn Edward Broadbent             });
2072c9bb6861Sraviteja-b }
2073c9bb6861Sraviteja-b 
2074*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2075c9bb6861Sraviteja-b {
2076*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2077*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2078*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2079*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2080*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2081c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
20825cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2083c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2084d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
2085c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
20865cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
20875cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
2088c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2089c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Entries"] = {
2090*7e860f15SJohn Edward Broadbent                     {"@odata.id",
2091*7e860f15SJohn Edward Broadbent                      "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
20925cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
20935cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
20945cb1dd27SAsmitha Karunanithi                      {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
20955cb1dd27SAsmitha Karunanithi                                  "Actions/LogService.ClearLog"}}},
2096d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
2097d337bb72SAsmitha Karunanithi                      {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2098d337bb72SAsmitha Karunanithi                                  "Actions/LogService.CollectDiagnosticData"}}}};
2099*7e860f15SJohn Edward Broadbent             });
2100c9bb6861Sraviteja-b }
2101c9bb6861Sraviteja-b 
2102*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
2103*7e860f15SJohn Edward Broadbent {
2104*7e860f15SJohn Edward Broadbent 
2105c9bb6861Sraviteja-b     /**
2106c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2107c9bb6861Sraviteja-b      */
2108*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2109*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2110*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2111*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2112*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2113c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2114c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2115c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
21165cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
21175cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2118c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
21195cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2120c9bb6861Sraviteja-b 
21215cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
2122*7e860f15SJohn Edward Broadbent             });
2123c9bb6861Sraviteja-b }
2124c9bb6861Sraviteja-b 
2125*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2126c9bb6861Sraviteja-b {
2127*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2128*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2129*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2130*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2131*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2132*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2133*7e860f15SJohn Edward Broadbent                const std::string& param) {
2134*7e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "BMC");
2135*7e860f15SJohn Edward Broadbent             });
2136*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2137*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2138*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2139*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
2140*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2141*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2142*7e860f15SJohn Edward Broadbent                const std::string& param) {
2143*7e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
2144*7e860f15SJohn Edward Broadbent             });
2145c9bb6861Sraviteja-b }
2146c9bb6861Sraviteja-b 
2147*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2148c9bb6861Sraviteja-b {
21498d1b46d7Szhanghch05 
2150*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2151d337bb72SAsmitha Karunanithi                       "Actions/"
2152d337bb72SAsmitha Karunanithi                       "LogService.CollectDiagnosticData/")
2153*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2154*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2155*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
2156*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
21578d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
2158*7e860f15SJohn Edward Broadbent             });
2159a43be80fSAsmitha Karunanithi }
2160a43be80fSAsmitha Karunanithi 
2161*7e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
216280319af1SAsmitha Karunanithi {
2163*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
216480319af1SAsmitha Karunanithi                       "Actions/"
216580319af1SAsmitha Karunanithi                       "LogService.ClearLog/")
2166*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2167*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2168*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2169*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
21708d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
2171*7e860f15SJohn Edward Broadbent             });
21725cb1dd27SAsmitha Karunanithi }
21735cb1dd27SAsmitha Karunanithi 
2174*7e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
21755cb1dd27SAsmitha Karunanithi {
2176*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2177*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2178*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2179*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2180*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21815cb1dd27SAsmitha Karunanithi 
2182*7e860f15SJohn Edward Broadbent             {
21835cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
21845cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump";
21855cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
2186d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
21875cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
2188*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
2189*7e860f15SJohn Edward Broadbent                     "System Dump LogService";
21905cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
21915cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
21925cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Entries"] = {
21935cb1dd27SAsmitha Karunanithi                     {"@odata.id",
21945cb1dd27SAsmitha Karunanithi                      "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
21955cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
21965cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
2197*7e860f15SJohn Edward Broadbent                      {{"target",
2198*7e860f15SJohn Edward Broadbent                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
21995cb1dd27SAsmitha Karunanithi                        "LogService.ClearLog"}}},
2200d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
2201*7e860f15SJohn Edward Broadbent                      {{"target",
2202*7e860f15SJohn Edward Broadbent                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2203d337bb72SAsmitha Karunanithi                        "LogService.CollectDiagnosticData"}}}};
2204*7e860f15SJohn Edward Broadbent             });
22055cb1dd27SAsmitha Karunanithi }
22065cb1dd27SAsmitha Karunanithi 
2207*7e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
2208*7e860f15SJohn Edward Broadbent {
2209*7e860f15SJohn Edward Broadbent 
22105cb1dd27SAsmitha Karunanithi     /**
22115cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
22125cb1dd27SAsmitha Karunanithi      */
2213*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2214*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2215*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2216*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2217*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2218*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2219*7e860f15SJohn Edward Broadbent                const std::string&) {
22205cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
22215cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
22225cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
22235cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
22245cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
22255cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
22265cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
22275cb1dd27SAsmitha Karunanithi 
22285cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
2229*7e860f15SJohn Edward Broadbent             });
22305cb1dd27SAsmitha Karunanithi }
22315cb1dd27SAsmitha Karunanithi 
2232*7e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
22335cb1dd27SAsmitha Karunanithi {
2234*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2235*7e860f15SJohn Edward Broadbent                  "redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2236*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2237*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2238*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2239*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2240*7e860f15SJohn Edward Broadbent                const std::string& param) {
2241*7e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "System");
2242*7e860f15SJohn Edward Broadbent             });
22438d1b46d7Szhanghch05 
2244*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2245*7e860f15SJohn Edward Broadbent                  "redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2246*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2247*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
2248*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2249*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2250*7e860f15SJohn Edward Broadbent                const std::string& param) {
2251*7e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
2252*7e860f15SJohn Edward Broadbent             });
22535cb1dd27SAsmitha Karunanithi }
2254c9bb6861Sraviteja-b 
2255*7e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2256c9bb6861Sraviteja-b {
2257*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2258d337bb72SAsmitha Karunanithi                       "Actions/"
2259d337bb72SAsmitha Karunanithi                       "LogService.CollectDiagnosticData/")
2260*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2261*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2262*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
2263*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2264*7e860f15SJohn Edward Broadbent 
2265*7e860f15SJohn Edward Broadbent             { createDump(asyncResp, req, "System"); });
2266a43be80fSAsmitha Karunanithi }
2267a43be80fSAsmitha Karunanithi 
2268*7e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2269a43be80fSAsmitha Karunanithi {
2270*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2271013487e5Sraviteja-b                       "Actions/"
2272013487e5Sraviteja-b                       "LogService.ClearLog/")
2273*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2274*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2275*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2276*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2277*7e860f15SJohn Edward Broadbent 
2278*7e860f15SJohn Edward Broadbent             { clearDump(asyncResp, "System"); });
2279013487e5Sraviteja-b }
2280013487e5Sraviteja-b 
2281*7e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
22821da66f75SEd Tanous {
22833946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
22843946028dSAppaRao Puli     // method for security reasons.
22851da66f75SEd Tanous     /**
22861da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
22871da66f75SEd Tanous      */
2288*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2289*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2290*7e860f15SJohn Edward Broadbent         .methods(
2291*7e860f15SJohn Edward Broadbent             boost::beast::http::verb::
2292*7e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
2293*7e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2294*7e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
2295*7e860f15SJohn Edward Broadbent             // SubRoute
22960f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2297424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2298e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
22998e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
23004f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
23014f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
23024f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2303e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2304e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
2305cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2306cd50aa42SJason M. Bills                 {"@odata.id",
2307424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2308e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
23095b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
23105b61b5e8SJason M. Bills                  {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
23115b61b5e8SJason M. Bills                              "Actions/LogService.ClearLog"}}},
23128e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
2313424c4176SJason M. Bills                  {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
23148e6c099aSJason M. Bills                              "Actions/LogService.CollectDiagnosticData"}}}};
2315*7e860f15SJohn Edward Broadbent         });
23161da66f75SEd Tanous }
23171da66f75SEd Tanous 
2318*7e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
23195b61b5e8SJason M. Bills {
2320*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2321*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
23225b61b5e8SJason M. Bills                  "LogService.ClearLog/")
2323*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureComponents"})
2324*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2325*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2326*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23275b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
23285b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2329cb13a392SEd Tanous                                 const std::string&) {
23305b61b5e8SJason M. Bills                         if (ec)
23315b61b5e8SJason M. Bills                         {
23325b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
23335b61b5e8SJason M. Bills                             return;
23345b61b5e8SJason M. Bills                         }
23355b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
23365b61b5e8SJason M. Bills                     },
2337*7e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
2338*7e860f15SJohn Edward Broadbent                     "DeleteAll");
2339*7e860f15SJohn Edward Broadbent             });
23405b61b5e8SJason M. Bills }
23415b61b5e8SJason M. Bills 
23428d1b46d7Szhanghch05 static void
23438d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23448d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2345e855dd28SJason M. Bills {
2346043a0536SJohnathan Mantey     auto getStoredLogCallback =
2347043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2348e855dd28SJason M. Bills             const boost::system::error_code ec,
2349043a0536SJohnathan Mantey             const std::vector<std::pair<std::string, VariantType>>& params) {
2350e855dd28SJason M. Bills             if (ec)
2351e855dd28SJason M. Bills             {
2352e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
23531ddcf01aSJason M. Bills                 if (ec.value() ==
23541ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
23551ddcf01aSJason M. Bills                 {
2356043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2357043a0536SJohnathan Mantey                                                logID);
23581ddcf01aSJason M. Bills                 }
23591ddcf01aSJason M. Bills                 else
23601ddcf01aSJason M. Bills                 {
2361e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
23621ddcf01aSJason M. Bills                 }
2363e855dd28SJason M. Bills                 return;
2364e855dd28SJason M. Bills             }
2365043a0536SJohnathan Mantey 
2366043a0536SJohnathan Mantey             std::string timestamp{};
2367043a0536SJohnathan Mantey             std::string filename{};
2368043a0536SJohnathan Mantey             std::string logfile{};
23692c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2370043a0536SJohnathan Mantey 
2371043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2372e855dd28SJason M. Bills             {
2373043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2374e855dd28SJason M. Bills                 return;
2375e855dd28SJason M. Bills             }
2376e855dd28SJason M. Bills 
2377043a0536SJohnathan Mantey             std::string crashdumpURI =
2378e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2379043a0536SJohnathan Mantey                 logID + "/" + filename;
23808e6c099aSJason M. Bills             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2381043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2382043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2383e855dd28SJason M. Bills                                               logID},
2384e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2385e855dd28SJason M. Bills                             {"Id", logID},
2386e855dd28SJason M. Bills                             {"EntryType", "Oem"},
23878e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
23888e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
23898e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2390043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2391e855dd28SJason M. Bills         };
2392e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
23935b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
23945b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2395043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2396e855dd28SJason M. Bills }
2397e855dd28SJason M. Bills 
2398*7e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
23991da66f75SEd Tanous {
24003946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
24013946028dSAppaRao Puli     // method for security reasons.
24021da66f75SEd Tanous     /**
24031da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
24041da66f75SEd Tanous      */
2405*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2406*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2407*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureComponents"})
2408*7e860f15SJohn Edward Broadbent         .methods(
2409*7e860f15SJohn Edward Broadbent             boost::beast::http::verb::
2410*7e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
2411*7e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2412*7e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
2413*7e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2414e1f26343SJason M. Bills             auto getLogEntriesCallback = [asyncResp](
2415e1f26343SJason M. Bills                                              const boost::system::error_code ec,
2416*7e860f15SJohn Edward Broadbent                                              const std::vector<std::string>&
2417*7e860f15SJohn Edward Broadbent                                                  resp) {
24181da66f75SEd Tanous                 if (ec)
24191da66f75SEd Tanous                 {
24201da66f75SEd Tanous                     if (ec.value() !=
24211da66f75SEd Tanous                         boost::system::errc::no_such_file_or_directory)
24221da66f75SEd Tanous                     {
24231da66f75SEd Tanous                         BMCWEB_LOG_DEBUG << "failed to get entries ec: "
24241da66f75SEd Tanous                                          << ec.message();
2425f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
24261da66f75SEd Tanous                         return;
24271da66f75SEd Tanous                     }
24281da66f75SEd Tanous                 }
2429e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
24301da66f75SEd Tanous                     "#LogEntryCollection.LogEntryCollection";
24310f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
2432424c4176SJason M. Bills                     "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2433424c4176SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2434e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
2435424c4176SJason M. Bills                     "Collection of Crashdump Entries";
2436*7e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
2437*7e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2438e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
2439e855dd28SJason M. Bills                 std::vector<std::string> logIDs;
2440e855dd28SJason M. Bills                 // Get the list of log entries and build up an empty array big
2441e855dd28SJason M. Bills                 // enough to hold them
24421da66f75SEd Tanous                 for (const std::string& objpath : resp)
24431da66f75SEd Tanous                 {
2444e855dd28SJason M. Bills                     // Get the log ID
2445f23b7296SEd Tanous                     std::size_t lastPos = objpath.rfind('/');
2446e855dd28SJason M. Bills                     if (lastPos == std::string::npos)
24471da66f75SEd Tanous                     {
2448e855dd28SJason M. Bills                         continue;
24491da66f75SEd Tanous                     }
2450e855dd28SJason M. Bills                     logIDs.emplace_back(objpath.substr(lastPos + 1));
2451e855dd28SJason M. Bills 
2452e855dd28SJason M. Bills                     // Add a space for the log entry to the array
2453e855dd28SJason M. Bills                     logEntryArray.push_back({});
2454e855dd28SJason M. Bills                 }
2455e855dd28SJason M. Bills                 // Now go through and set up async calls to fill in the entries
2456e855dd28SJason M. Bills                 size_t index = 0;
2457e855dd28SJason M. Bills                 for (const std::string& logID : logIDs)
2458e855dd28SJason M. Bills                 {
2459e855dd28SJason M. Bills                     // Add the log entry to the array
2460e855dd28SJason M. Bills                     logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
24611da66f75SEd Tanous                 }
2462e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2463e1f26343SJason M. Bills                     logEntryArray.size();
24641da66f75SEd Tanous             };
24651da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
24661da66f75SEd Tanous                 std::move(getLogEntriesCallback),
24671da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
24681da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
24691da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
24705b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
2471*7e860f15SJohn Edward Broadbent         });
24721da66f75SEd Tanous }
24731da66f75SEd Tanous 
2474*7e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
24751da66f75SEd Tanous {
24763946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
24773946028dSAppaRao Puli     // method for security reasons.
24781da66f75SEd Tanous 
2479*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
2480*7e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2481*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureComponents"})
2482*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2483*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2484*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2485*7e860f15SJohn Edward Broadbent                const std::string& param) {
2486*7e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2487e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
2488*7e860f15SJohn Edward Broadbent             });
2489e855dd28SJason M. Bills }
2490e855dd28SJason M. Bills 
2491*7e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2492e855dd28SJason M. Bills {
24933946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
24943946028dSAppaRao Puli     // method for security reasons.
2495*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
2496*7e860f15SJohn Edward Broadbent         app,
2497*7e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2498*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureComponents"})
2499*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2500*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2501*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2502*7e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
2503043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2504043a0536SJohnathan Mantey                     [asyncResp, logID, fileName](
2505abf2add6SEd Tanous                         const boost::system::error_code ec,
2506*7e860f15SJohn Edward Broadbent                         const std::vector<std::pair<std::string, VariantType>>&
2507*7e860f15SJohn Edward Broadbent                             resp) {
25081da66f75SEd Tanous                         if (ec)
25091da66f75SEd Tanous                         {
2510043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2511043a0536SJohnathan Mantey                                              << ec.message();
2512f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
25131da66f75SEd Tanous                             return;
25141da66f75SEd Tanous                         }
2515e855dd28SJason M. Bills 
2516043a0536SJohnathan Mantey                         std::string dbusFilename{};
2517043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2518043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2519043a0536SJohnathan Mantey 
2520*7e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
2521*7e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2522043a0536SJohnathan Mantey 
2523043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2524043a0536SJohnathan Mantey                             dbusFilepath.empty())
25251da66f75SEd Tanous                         {
2526*7e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
2527*7e860f15SJohn Edward Broadbent                                                            fileName);
25281da66f75SEd Tanous                             return;
25291da66f75SEd Tanous                         }
2530e855dd28SJason M. Bills 
2531043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2532043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2533043a0536SJohnathan Mantey                         {
2534*7e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
2535*7e860f15SJohn Edward Broadbent                                                            fileName);
2536043a0536SJohnathan Mantey                             return;
2537043a0536SJohnathan Mantey                         }
2538043a0536SJohnathan Mantey 
2539043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2540043a0536SJohnathan Mantey                         {
2541*7e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
2542*7e860f15SJohn Edward Broadbent                                                            fileName);
2543043a0536SJohnathan Mantey                             return;
2544043a0536SJohnathan Mantey                         }
2545043a0536SJohnathan Mantey                         std::ifstream ifs(dbusFilepath, std::ios::in |
2546043a0536SJohnathan Mantey                                                             std::ios::binary |
2547043a0536SJohnathan Mantey                                                             std::ios::ate);
2548043a0536SJohnathan Mantey                         std::ifstream::pos_type fileSize = ifs.tellg();
2549043a0536SJohnathan Mantey                         if (fileSize < 0)
2550043a0536SJohnathan Mantey                         {
2551043a0536SJohnathan Mantey                             messages::generalError(asyncResp->res);
2552043a0536SJohnathan Mantey                             return;
2553043a0536SJohnathan Mantey                         }
2554043a0536SJohnathan Mantey                         ifs.seekg(0, std::ios::beg);
2555043a0536SJohnathan Mantey 
2556043a0536SJohnathan Mantey                         auto crashData = std::make_unique<char[]>(
2557043a0536SJohnathan Mantey                             static_cast<unsigned int>(fileSize));
2558043a0536SJohnathan Mantey 
2559043a0536SJohnathan Mantey                         ifs.read(crashData.get(), static_cast<int>(fileSize));
2560043a0536SJohnathan Mantey 
2561*7e860f15SJohn Edward Broadbent                         // The cast to std::string is intentional in order to
2562*7e860f15SJohn Edward Broadbent                         // use the assign() that applies move mechanics
2563043a0536SJohnathan Mantey                         asyncResp->res.body().assign(
2564043a0536SJohnathan Mantey                             static_cast<std::string>(crashData.get()));
2565043a0536SJohnathan Mantey 
2566*7e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
2567*7e860f15SJohn Edward Broadbent                         // from a browser
2568*7e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
2569*7e860f15SJohn Edward Broadbent                                                  "attachment");
25701da66f75SEd Tanous                     };
25711da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
25725b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
25735b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
2574*7e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
2575*7e860f15SJohn Edward Broadbent                     crashdumpInterface);
2576*7e860f15SJohn Edward Broadbent             });
25771da66f75SEd Tanous }
25781da66f75SEd Tanous 
2579*7e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
25801da66f75SEd Tanous {
25813946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25823946028dSAppaRao Puli     // method for security reasons.
2583*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/"
2584*7e860f15SJohn Edward Broadbent                       "Actions/LogService.CollectDiagnosticData/")
2585*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureComponents"})
2586*7e860f15SJohn Edward Broadbent         .methods(
2587*7e860f15SJohn Edward Broadbent             boost::beast::http::verb::
2588*7e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
2589*7e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25908e6c099aSJason M. Bills             std::string diagnosticDataType;
25918e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
25928e6c099aSJason M. Bills             if (!redfish::json_util::readJson(
2593*7e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
2594*7e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
2595*7e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
25968e6c099aSJason M. Bills             {
25978e6c099aSJason M. Bills                 return;
25988e6c099aSJason M. Bills             }
25998e6c099aSJason M. Bills 
26008e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
26018e6c099aSJason M. Bills             {
26028e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
26038e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
26048e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
26058e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
26068e6c099aSJason M. Bills                     "CollectDiagnosticData");
26078e6c099aSJason M. Bills                 return;
26088e6c099aSJason M. Bills             }
26098e6c099aSJason M. Bills 
26108e6c099aSJason M. Bills             auto collectCrashdumpCallback = [asyncResp, req](
2611*7e860f15SJohn Edward Broadbent                                                 const boost::system::error_code
2612*7e860f15SJohn Edward Broadbent                                                     ec,
2613cb13a392SEd Tanous                                                 const std::string&) {
26141da66f75SEd Tanous                 if (ec)
26151da66f75SEd Tanous                 {
2616*7e860f15SJohn Edward Broadbent                     if (ec.value() ==
2617*7e860f15SJohn Edward Broadbent                         boost::system::errc::operation_not_supported)
26181da66f75SEd Tanous                     {
2619f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
26201da66f75SEd Tanous                     }
26214363d3b2SJason M. Bills                     else if (ec.value() ==
26224363d3b2SJason M. Bills                              boost::system::errc::device_or_resource_busy)
26234363d3b2SJason M. Bills                     {
26244363d3b2SJason M. Bills                         messages::serviceTemporarilyUnavailable(asyncResp->res,
26254363d3b2SJason M. Bills                                                                 "60");
26264363d3b2SJason M. Bills                     }
26271da66f75SEd Tanous                     else
26281da66f75SEd Tanous                     {
2629f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
26301da66f75SEd Tanous                     }
26311da66f75SEd Tanous                     return;
26321da66f75SEd Tanous                 }
2633*7e860f15SJohn Edward Broadbent                 std::shared_ptr<task::TaskData> task =
2634*7e860f15SJohn Edward Broadbent                     task::TaskData::createTask(
2635*7e860f15SJohn Edward Broadbent                         [](boost::system::error_code err,
2636*7e860f15SJohn Edward Broadbent                            sdbusplus::message::message&,
263766afe4faSJames Feist                            const std::shared_ptr<task::TaskData>& taskData) {
263866afe4faSJames Feist                             if (!err)
263966afe4faSJames Feist                             {
2640e5d5006bSJames Feist                                 taskData->messages.emplace_back(
2641e5d5006bSJames Feist                                     messages::taskCompletedOK(
2642e5d5006bSJames Feist                                         std::to_string(taskData->index)));
2643831d6b09SJames Feist                                 taskData->state = "Completed";
264466afe4faSJames Feist                             }
264532898ceaSJames Feist                             return task::completed;
264666afe4faSJames Feist                         },
2647*7e860f15SJohn Edward Broadbent                         "type='signal',interface='org.freedesktop.DBus."
2648*7e860f15SJohn Edward Broadbent                         "Properties',"
264946229577SJames Feist                         "member='PropertiesChanged',arg0namespace='com.intel."
265046229577SJames Feist                         "crashdump'");
265146229577SJames Feist                 task->startTimer(std::chrono::minutes(5));
265246229577SJames Feist                 task->populateResp(asyncResp->res);
2653fe306728SJames Feist                 task->payload.emplace(req);
26541da66f75SEd Tanous             };
26558e6c099aSJason M. Bills 
26568e6c099aSJason M. Bills             if (oemDiagnosticDataType == "OnDemand")
26578e6c099aSJason M. Bills             {
26581da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
26598e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
26608e6c099aSJason M. Bills                     crashdumpPath, crashdumpOnDemandInterface,
26618e6c099aSJason M. Bills                     "GenerateOnDemandLog");
26621da66f75SEd Tanous             }
26638e6c099aSJason M. Bills             else if (oemDiagnosticDataType == "Telemetry")
26646eda7685SKenny L. Ku             {
26658e6c099aSJason M. Bills                 crow::connections::systemBus->async_method_call(
26668e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
26678e6c099aSJason M. Bills                     crashdumpPath, crashdumpTelemetryInterface,
26688e6c099aSJason M. Bills                     "GenerateTelemetryLog");
26696eda7685SKenny L. Ku             }
26706eda7685SKenny L. Ku             else
26716eda7685SKenny L. Ku             {
26728e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
26738e6c099aSJason M. Bills                                  << oemDiagnosticDataType;
26748e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
2675*7e860f15SJohn Edward Broadbent                     asyncResp->res, oemDiagnosticDataType,
2676*7e860f15SJohn Edward Broadbent                     "OEMDiagnosticDataType", "CollectDiagnosticData");
26776eda7685SKenny L. Ku                 return;
26786eda7685SKenny L. Ku             }
2679*7e860f15SJohn Edward Broadbent         });
26806eda7685SKenny L. Ku }
26816eda7685SKenny L. Ku 
2682cb92c03bSAndrew Geissler /**
2683cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2684cb92c03bSAndrew Geissler  */
2685*7e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
2686cb92c03bSAndrew Geissler {
2687cb92c03bSAndrew Geissler     /**
2688cb92c03bSAndrew Geissler      * Function handles POST method request.
2689cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2690cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2691cb92c03bSAndrew Geissler      */
2692*7e860f15SJohn Edward Broadbent 
2693*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
2694*7e860f15SJohn Edward Broadbent                       "LogService.ClearLog/")
2695*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureManager"})
2696*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2697*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2698*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2699cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
2700cb92c03bSAndrew Geissler 
2701cb92c03bSAndrew Geissler                 // Process response from Logging service.
2702*7e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
2703*7e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
2704*7e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
2705*7e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
2706cb92c03bSAndrew Geissler                     if (ec)
2707cb92c03bSAndrew Geissler                     {
2708cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
2709*7e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
2710*7e860f15SJohn Edward Broadbent                                          << ec;
2711cb92c03bSAndrew Geissler                         asyncResp->res.result(
2712cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
2713cb92c03bSAndrew Geissler                         return;
2714cb92c03bSAndrew Geissler                     }
2715cb92c03bSAndrew Geissler 
2716*7e860f15SJohn Edward Broadbent                     asyncResp->res.result(
2717*7e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
2718cb92c03bSAndrew Geissler                 };
2719cb92c03bSAndrew Geissler 
2720cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
2721cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
27222c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
2723cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
2724cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2725*7e860f15SJohn Edward Broadbent             });
2726cb92c03bSAndrew Geissler }
2727a3316fc6SZhikuiRen 
2728a3316fc6SZhikuiRen /****************************************************
2729a3316fc6SZhikuiRen  * Redfish PostCode interfaces
2730a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
2731a3316fc6SZhikuiRen  ******************************************************/
2732*7e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
2733a3316fc6SZhikuiRen {
2734*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
2735*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
2736*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2737*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2738*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2739a3316fc6SZhikuiRen                 asyncResp->res.jsonValue = {
2740*7e860f15SJohn Edward Broadbent                     {"@odata.id",
2741*7e860f15SJohn Edward Broadbent                      "/redfish/v1/Systems/system/LogServices/PostCodes"},
2742a3316fc6SZhikuiRen                     {"@odata.type", "#LogService.v1_1_0.LogService"},
2743a3316fc6SZhikuiRen                     {"Name", "POST Code Log Service"},
2744a3316fc6SZhikuiRen                     {"Description", "POST Code Log Service"},
2745a3316fc6SZhikuiRen                     {"Id", "BIOS POST Code Log"},
2746a3316fc6SZhikuiRen                     {"OverWritePolicy", "WrapsWhenFull"},
2747a3316fc6SZhikuiRen                     {"Entries",
2748*7e860f15SJohn Edward Broadbent                      {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2749*7e860f15SJohn Edward Broadbent                                     "PostCodes/Entries"}}}};
2750a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
2751*7e860f15SJohn Edward Broadbent                     {"target",
2752*7e860f15SJohn Edward Broadbent                      "/redfish/v1/Systems/system/LogServices/PostCodes/"
2753a3316fc6SZhikuiRen                      "Actions/LogService.ClearLog"}};
2754*7e860f15SJohn Edward Broadbent             });
2755a3316fc6SZhikuiRen }
2756a3316fc6SZhikuiRen 
2757*7e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
2758a3316fc6SZhikuiRen {
2759*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2760*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
2761a3316fc6SZhikuiRen                  "LogService.ClearLog/")
2762*7e860f15SJohn Edward Broadbent         .privileges({"ConfigureComponents"})
2763*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2764*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
2765*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2766a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
2767a3316fc6SZhikuiRen 
2768a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
2769a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
2770a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
2771a3316fc6SZhikuiRen                         if (ec)
2772a3316fc6SZhikuiRen                         {
2773a3316fc6SZhikuiRen                             // TODO Handle for specific error code
2774a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
2775*7e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
2776*7e860f15SJohn Edward Broadbent                                 << ec;
2777*7e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
2778*7e860f15SJohn Edward Broadbent                                                       internal_server_error);
2779a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
2780a3316fc6SZhikuiRen                             return;
2781a3316fc6SZhikuiRen                         }
2782a3316fc6SZhikuiRen                     },
278315124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
278415124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
2785a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
2786*7e860f15SJohn Edward Broadbent             });
2787a3316fc6SZhikuiRen }
2788a3316fc6SZhikuiRen 
2789a3316fc6SZhikuiRen static void fillPostCodeEntry(
27908d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
27916c9a279eSManojkiran Eda     const boost::container::flat_map<
27926c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
2793a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
2794a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
2795a3316fc6SZhikuiRen {
2796a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
2797a3316fc6SZhikuiRen     const message_registries::Message* message =
27984a0bf539SManojkiran Eda         message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
2799a3316fc6SZhikuiRen 
2800a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
2801a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
2802a3316fc6SZhikuiRen 
2803a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
28046c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
28056c9a279eSManojkiran Eda              code : postcode)
2806a3316fc6SZhikuiRen     {
2807a3316fc6SZhikuiRen         currentCodeIndex++;
2808a3316fc6SZhikuiRen         std::string postcodeEntryID =
2809a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
2810a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
2811a3316fc6SZhikuiRen 
2812a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
2813a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
2814a3316fc6SZhikuiRen 
2815a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
2816a3316fc6SZhikuiRen         { // already incremented
2817a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
2818a3316fc6SZhikuiRen         }
2819a3316fc6SZhikuiRen         else
2820a3316fc6SZhikuiRen         {
2821a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
2822a3316fc6SZhikuiRen         }
2823a3316fc6SZhikuiRen 
2824a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
2825a3316fc6SZhikuiRen         // not fall between top and skip
2826a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
2827a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
2828a3316fc6SZhikuiRen         {
2829a3316fc6SZhikuiRen             continue;
2830a3316fc6SZhikuiRen         }
2831a3316fc6SZhikuiRen 
28324e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
2833a3316fc6SZhikuiRen         // currentIndex
2834a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
2835a3316fc6SZhikuiRen         {
2836a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
2837a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
2838a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
2839a3316fc6SZhikuiRen             continue;
2840a3316fc6SZhikuiRen         }
2841a3316fc6SZhikuiRen 
2842a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
2843a3316fc6SZhikuiRen         // index
2844a3316fc6SZhikuiRen 
2845a3316fc6SZhikuiRen         // Get the Created time from the timestamp
2846a3316fc6SZhikuiRen         std::string entryTimeStr;
28479c620e21SAsmitha Karunanithi         entryTimeStr = crow::utility::getDateTime(
28489c620e21SAsmitha Karunanithi             static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
2849a3316fc6SZhikuiRen 
2850a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
2851a3316fc6SZhikuiRen         std::ostringstream hexCode;
2852a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
28536c9a279eSManojkiran Eda                 << std::get<0>(code.second);
2854a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
2855a3316fc6SZhikuiRen         // Set Fixed -Point Notation
2856a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
2857a3316fc6SZhikuiRen         // Set precision to 4 digits
2858a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
2859a3316fc6SZhikuiRen         // Add double to stream
2860a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
2861a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
2862a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
2863a3316fc6SZhikuiRen 
2864a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
2865a3316fc6SZhikuiRen         std::string msg;
2866a3316fc6SZhikuiRen         if (message != nullptr)
2867a3316fc6SZhikuiRen         {
2868a3316fc6SZhikuiRen             msg = message->message;
2869a3316fc6SZhikuiRen 
2870a3316fc6SZhikuiRen             // fill in this post code value
2871a3316fc6SZhikuiRen             int i = 0;
2872a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
2873a3316fc6SZhikuiRen             {
2874a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
2875a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
2876a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
2877a3316fc6SZhikuiRen                 {
2878a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
2879a3316fc6SZhikuiRen                 }
2880a3316fc6SZhikuiRen             }
2881a3316fc6SZhikuiRen         }
2882a3316fc6SZhikuiRen 
2883d4342a92STim Lee         // Get Severity template from message registry
2884d4342a92STim Lee         std::string severity;
2885d4342a92STim Lee         if (message != nullptr)
2886d4342a92STim Lee         {
2887d4342a92STim Lee             severity = message->severity;
2888d4342a92STim Lee         }
2889d4342a92STim Lee 
2890a3316fc6SZhikuiRen         // add to AsyncResp
2891a3316fc6SZhikuiRen         logEntryArray.push_back({});
2892a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
2893743e9a1fSGunnar Mills         bmcLogEntry = {{"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
2894a3316fc6SZhikuiRen                        {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2895a3316fc6SZhikuiRen                                      "PostCodes/Entries/" +
2896a3316fc6SZhikuiRen                                          postcodeEntryID},
2897a3316fc6SZhikuiRen                        {"Name", "POST Code Log Entry"},
2898a3316fc6SZhikuiRen                        {"Id", postcodeEntryID},
2899a3316fc6SZhikuiRen                        {"Message", std::move(msg)},
29004a0bf539SManojkiran Eda                        {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
2901a3316fc6SZhikuiRen                        {"MessageArgs", std::move(messageArgs)},
2902a3316fc6SZhikuiRen                        {"EntryType", "Event"},
2903a3316fc6SZhikuiRen                        {"Severity", std::move(severity)},
29049c620e21SAsmitha Karunanithi                        {"Created", entryTimeStr}};
2905a3316fc6SZhikuiRen     }
2906a3316fc6SZhikuiRen }
2907a3316fc6SZhikuiRen 
29088d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2909a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
2910a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
2911a3316fc6SZhikuiRen {
2912a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
29136c9a279eSManojkiran Eda         [aResp, bootIndex,
29146c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
29156c9a279eSManojkiran Eda                     const boost::container::flat_map<
29166c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
29176c9a279eSManojkiran Eda                         postcode) {
2918a3316fc6SZhikuiRen             if (ec)
2919a3316fc6SZhikuiRen             {
2920a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
2921a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
2922a3316fc6SZhikuiRen                 return;
2923a3316fc6SZhikuiRen             }
2924a3316fc6SZhikuiRen 
2925a3316fc6SZhikuiRen             // skip the empty postcode boots
2926a3316fc6SZhikuiRen             if (postcode.empty())
2927a3316fc6SZhikuiRen             {
2928a3316fc6SZhikuiRen                 return;
2929a3316fc6SZhikuiRen             }
2930a3316fc6SZhikuiRen 
2931a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
2932a3316fc6SZhikuiRen 
2933a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
2934a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
2935a3316fc6SZhikuiRen         },
293615124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
293715124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
2938a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
2939a3316fc6SZhikuiRen         bootIndex);
2940a3316fc6SZhikuiRen }
2941a3316fc6SZhikuiRen 
29428d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2943a3316fc6SZhikuiRen                                const uint16_t bootIndex,
2944a3316fc6SZhikuiRen                                const uint16_t bootCount,
2945a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
2946a3316fc6SZhikuiRen                                const uint64_t top)
2947a3316fc6SZhikuiRen {
2948a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
2949a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
2950a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
29516c9a279eSManojkiran Eda               const boost::container::flat_map<
29526c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
29536c9a279eSManojkiran Eda                   postcode) {
2954a3316fc6SZhikuiRen             if (ec)
2955a3316fc6SZhikuiRen             {
2956a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
2957a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
2958a3316fc6SZhikuiRen                 return;
2959a3316fc6SZhikuiRen             }
2960a3316fc6SZhikuiRen 
2961a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
2962a3316fc6SZhikuiRen             if (!postcode.empty())
2963a3316fc6SZhikuiRen             {
2964a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
2965a3316fc6SZhikuiRen 
2966a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
2967a3316fc6SZhikuiRen                 {
2968a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
2969a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
2970a3316fc6SZhikuiRen                     uint64_t thisBootTop =
2971a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
2972a3316fc6SZhikuiRen 
2973a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
2974a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
2975a3316fc6SZhikuiRen                 }
2976a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
2977a3316fc6SZhikuiRen             }
2978a3316fc6SZhikuiRen 
2979a3316fc6SZhikuiRen             // continue to previous bootIndex
2980a3316fc6SZhikuiRen             if (bootIndex < bootCount)
2981a3316fc6SZhikuiRen             {
2982a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
2983a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
2984a3316fc6SZhikuiRen             }
2985a3316fc6SZhikuiRen             else
2986a3316fc6SZhikuiRen             {
2987a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
2988a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
2989a3316fc6SZhikuiRen                     "Entries?$skip=" +
2990a3316fc6SZhikuiRen                     std::to_string(skip + top);
2991a3316fc6SZhikuiRen             }
2992a3316fc6SZhikuiRen         },
299315124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
299415124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
2995a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
2996a3316fc6SZhikuiRen         bootIndex);
2997a3316fc6SZhikuiRen }
2998a3316fc6SZhikuiRen 
29998d1b46d7Szhanghch05 static void
30008d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3001a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3002a3316fc6SZhikuiRen {
3003a3316fc6SZhikuiRen     uint64_t entryCount = 0;
3004a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3005a3316fc6SZhikuiRen         [aResp, entryCount, skip,
3006a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
3007a3316fc6SZhikuiRen               const std::variant<uint16_t>& bootCount) {
3008a3316fc6SZhikuiRen             if (ec)
3009a3316fc6SZhikuiRen             {
3010a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3011a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3012a3316fc6SZhikuiRen                 return;
3013a3316fc6SZhikuiRen             }
3014a3316fc6SZhikuiRen             auto pVal = std::get_if<uint16_t>(&bootCount);
3015a3316fc6SZhikuiRen             if (pVal)
3016a3316fc6SZhikuiRen             {
3017a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3018a3316fc6SZhikuiRen             }
3019a3316fc6SZhikuiRen             else
3020a3316fc6SZhikuiRen             {
3021a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3022a3316fc6SZhikuiRen             }
3023a3316fc6SZhikuiRen         },
302415124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
302515124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3026a3316fc6SZhikuiRen         "org.freedesktop.DBus.Properties", "Get",
3027a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3028a3316fc6SZhikuiRen }
3029a3316fc6SZhikuiRen 
3030*7e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3031a3316fc6SZhikuiRen {
3032*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
3033*7e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3034*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
3035*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3036*7e860f15SJohn Edward Broadbent             [](const crow::Request& req,
3037*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3038a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3039a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3040a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3041a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3042a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3043a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3044a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3045a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3046a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3047a3316fc6SZhikuiRen 
3048a3316fc6SZhikuiRen                 uint64_t skip = 0;
3049a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
30508d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3051a3316fc6SZhikuiRen                 {
3052a3316fc6SZhikuiRen                     return;
3053a3316fc6SZhikuiRen                 }
30548d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3055a3316fc6SZhikuiRen                 {
3056a3316fc6SZhikuiRen                     return;
3057a3316fc6SZhikuiRen                 }
3058a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
3059*7e860f15SJohn Edward Broadbent             });
3060a3316fc6SZhikuiRen }
3061a3316fc6SZhikuiRen 
3062*7e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3063a3316fc6SZhikuiRen {
3064*7e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
3065*7e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3066*7e860f15SJohn Edward Broadbent         .privileges({"Login"})
3067*7e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3068*7e860f15SJohn Edward Broadbent             [](const crow::Request&,
3069*7e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3070*7e860f15SJohn Edward Broadbent                const std::string& targetID) {
3071a3316fc6SZhikuiRen                 size_t bootPos = targetID.find('B');
3072a3316fc6SZhikuiRen                 if (bootPos == std::string::npos)
3073a3316fc6SZhikuiRen                 {
3074a3316fc6SZhikuiRen                     // Requested ID was not found
3075a3316fc6SZhikuiRen                     messages::resourceMissingAtURI(asyncResp->res, targetID);
3076a3316fc6SZhikuiRen                     return;
3077a3316fc6SZhikuiRen                 }
3078a3316fc6SZhikuiRen                 std::string_view bootIndexStr(targetID);
3079a3316fc6SZhikuiRen                 bootIndexStr.remove_prefix(bootPos + 1);
3080a3316fc6SZhikuiRen                 uint16_t bootIndex = 0;
3081a3316fc6SZhikuiRen                 uint64_t codeIndex = 0;
3082a3316fc6SZhikuiRen                 size_t dashPos = bootIndexStr.find('-');
3083a3316fc6SZhikuiRen 
3084a3316fc6SZhikuiRen                 if (dashPos == std::string::npos)
3085a3316fc6SZhikuiRen                 {
3086a3316fc6SZhikuiRen                     return;
3087a3316fc6SZhikuiRen                 }
3088a3316fc6SZhikuiRen                 std::string_view codeIndexStr(bootIndexStr);
3089a3316fc6SZhikuiRen                 bootIndexStr.remove_suffix(dashPos);
3090a3316fc6SZhikuiRen                 codeIndexStr.remove_prefix(dashPos + 1);
3091a3316fc6SZhikuiRen 
3092a3316fc6SZhikuiRen                 bootIndex = static_cast<uint16_t>(
309323a21a1cSEd Tanous                     strtoul(std::string(bootIndexStr).c_str(), nullptr, 0));
3094*7e860f15SJohn Edward Broadbent                 codeIndex =
3095*7e860f15SJohn Edward Broadbent                     strtoul(std::string(codeIndexStr).c_str(), nullptr, 0);
3096a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3097a3316fc6SZhikuiRen                 {
3098a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
3099*7e860f15SJohn Edward Broadbent                                      << targetID;
3100a3316fc6SZhikuiRen                 }
3101a3316fc6SZhikuiRen 
3102*7e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
3103*7e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3104a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3105a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
3106a3316fc6SZhikuiRen                     "Entries";
3107a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3108a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3109a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3110a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3111a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3112a3316fc6SZhikuiRen 
3113a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
3114*7e860f15SJohn Edward Broadbent             });
3115a3316fc6SZhikuiRen }
3116a3316fc6SZhikuiRen 
31171da66f75SEd Tanous } // namespace redfish
3118