xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 647b3cdc4d6092d5ed77b1d7e2dac643373a5f8b)
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 
18*647b3cdcSGeorge Liu #include "http_utility.hpp"
194851d45dSJason M. Bills #include "registries.hpp"
204851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
214851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2246229577SJames Feist #include "task.hpp"
231da66f75SEd Tanous 
24e1f26343SJason M. Bills #include <systemd/sd-journal.h>
25400fd1fbSAdriana Kobylak #include <unistd.h>
26e1f26343SJason M. Bills 
277e860f15SJohn Edward Broadbent #include <app.hpp>
28400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
294851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
304851d45dSJason M. Bills #include <boost/beast/core/span.hpp>
31400fd1fbSAdriana Kobylak #include <boost/beast/http.hpp>
321da66f75SEd Tanous #include <boost/container/flat_map.hpp>
331ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
34cb92c03bSAndrew Geissler #include <error_messages.hpp>
35ed398213SEd Tanous #include <registries/privilege_registry.hpp>
361214b7e7SGunnar Mills 
37*647b3cdcSGeorge Liu #include <charconv>
384418c7f0SJames Feist #include <filesystem>
3975710de2SXiaochao Ma #include <optional>
40cd225da8SJason M. Bills #include <string_view>
41abf2add6SEd Tanous #include <variant>
421da66f75SEd Tanous 
431da66f75SEd Tanous namespace redfish
441da66f75SEd Tanous {
451da66f75SEd Tanous 
465b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
475b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
485b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
495b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
505b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
515b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
52424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
536eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
546eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
551da66f75SEd Tanous 
564851d45dSJason M. Bills namespace message_registries
574851d45dSJason M. Bills {
584851d45dSJason M. Bills static const Message* getMessageFromRegistry(
594851d45dSJason M. Bills     const std::string& messageKey,
604851d45dSJason M. Bills     const boost::beast::span<const MessageEntry> registry)
614851d45dSJason M. Bills {
624851d45dSJason M. Bills     boost::beast::span<const MessageEntry>::const_iterator messageIt =
634851d45dSJason M. Bills         std::find_if(registry.cbegin(), registry.cend(),
644851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
654851d45dSJason M. Bills                          return !std::strcmp(messageEntry.first,
664851d45dSJason M. Bills                                              messageKey.c_str());
674851d45dSJason M. Bills                      });
684851d45dSJason M. Bills     if (messageIt != registry.cend())
694851d45dSJason M. Bills     {
704851d45dSJason M. Bills         return &messageIt->second;
714851d45dSJason M. Bills     }
724851d45dSJason M. Bills 
734851d45dSJason M. Bills     return nullptr;
744851d45dSJason M. Bills }
754851d45dSJason M. Bills 
764851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
774851d45dSJason M. Bills {
784851d45dSJason M. Bills     // Redfish MessageIds are in the form
794851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
804851d45dSJason M. Bills     // the right Message
814851d45dSJason M. Bills     std::vector<std::string> fields;
824851d45dSJason M. Bills     fields.reserve(4);
834851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
844851d45dSJason M. Bills     std::string& registryName = fields[0];
854851d45dSJason M. Bills     std::string& messageKey = fields[3];
864851d45dSJason M. Bills 
874851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
884851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
894851d45dSJason M. Bills     {
904851d45dSJason M. Bills         return getMessageFromRegistry(
914851d45dSJason M. Bills             messageKey, boost::beast::span<const MessageEntry>(base::registry));
924851d45dSJason M. Bills     }
934851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
944851d45dSJason M. Bills     {
954851d45dSJason M. Bills         return getMessageFromRegistry(
964851d45dSJason M. Bills             messageKey,
974851d45dSJason M. Bills             boost::beast::span<const MessageEntry>(openbmc::registry));
984851d45dSJason M. Bills     }
994851d45dSJason M. Bills     return nullptr;
1004851d45dSJason M. Bills }
1014851d45dSJason M. Bills } // namespace message_registries
1024851d45dSJason M. Bills 
103f6150403SJames Feist namespace fs = std::filesystem;
1041da66f75SEd Tanous 
105cb92c03bSAndrew Geissler using GetManagedPropertyType = boost::container::flat_map<
10619bd78d9SPatrick Williams     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
107cb92c03bSAndrew Geissler                               int32_t, uint32_t, int64_t, uint64_t, double>>;
108cb92c03bSAndrew Geissler 
109cb92c03bSAndrew Geissler using GetManagedObjectsType = boost::container::flat_map<
110cb92c03bSAndrew Geissler     sdbusplus::message::object_path,
111cb92c03bSAndrew Geissler     boost::container::flat_map<std::string, GetManagedPropertyType>>;
112cb92c03bSAndrew Geissler 
113cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
114cb92c03bSAndrew Geissler {
115d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
116d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
117d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
118d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
119cb92c03bSAndrew Geissler     {
120cb92c03bSAndrew Geissler         return "Critical";
121cb92c03bSAndrew Geissler     }
1223174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
123d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
124d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
125cb92c03bSAndrew Geissler     {
126cb92c03bSAndrew Geissler         return "OK";
127cb92c03bSAndrew Geissler     }
1283174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
129cb92c03bSAndrew Geissler     {
130cb92c03bSAndrew Geissler         return "Warning";
131cb92c03bSAndrew Geissler     }
132cb92c03bSAndrew Geissler     return "";
133cb92c03bSAndrew Geissler }
134cb92c03bSAndrew Geissler 
1357e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13639e77504SEd Tanous                                      const std::string_view& field,
13739e77504SEd Tanous                                      std::string_view& contents)
13816428a1aSJason M. Bills {
13916428a1aSJason M. Bills     const char* data = nullptr;
14016428a1aSJason M. Bills     size_t length = 0;
14116428a1aSJason M. Bills     int ret = 0;
14216428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
143271584abSEd Tanous     ret = sd_journal_get_data(journal, field.data(),
144271584abSEd Tanous                               reinterpret_cast<const void**>(&data), &length);
14516428a1aSJason M. Bills     if (ret < 0)
14616428a1aSJason M. Bills     {
14716428a1aSJason M. Bills         return ret;
14816428a1aSJason M. Bills     }
14939e77504SEd Tanous     contents = std::string_view(data, length);
15016428a1aSJason M. Bills     // Only use the content after the "=" character.
15181ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
15216428a1aSJason M. Bills     return ret;
15316428a1aSJason M. Bills }
15416428a1aSJason M. Bills 
1557e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1567e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1577e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15816428a1aSJason M. Bills {
15916428a1aSJason M. Bills     int ret = 0;
16039e77504SEd Tanous     std::string_view metadata;
16116428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16216428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16316428a1aSJason M. Bills     if (ret < 0)
16416428a1aSJason M. Bills     {
16516428a1aSJason M. Bills         return ret;
16616428a1aSJason M. Bills     }
167b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16816428a1aSJason M. Bills     return ret;
16916428a1aSJason M. Bills }
17016428a1aSJason M. Bills 
1717e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1727e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
173a3316fc6SZhikuiRen {
174a3316fc6SZhikuiRen     int ret = 0;
175a3316fc6SZhikuiRen     uint64_t timestamp = 0;
176a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
177a3316fc6SZhikuiRen     if (ret < 0)
178a3316fc6SZhikuiRen     {
179a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
180a3316fc6SZhikuiRen                          << strerror(-ret);
181a3316fc6SZhikuiRen         return false;
182a3316fc6SZhikuiRen     }
1839c620e21SAsmitha Karunanithi     entryTimestamp = crow::utility::getDateTime(
1849c620e21SAsmitha Karunanithi         static_cast<std::time_t>(timestamp / 1000 / 1000));
1859c620e21SAsmitha Karunanithi     return true;
186a3316fc6SZhikuiRen }
187a3316fc6SZhikuiRen 
1888d1b46d7Szhanghch05 static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1898d1b46d7Szhanghch05                          const crow::Request& req, uint64_t& skip)
19016428a1aSJason M. Bills {
1915a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
1925a7e877eSJames Feist         req.urlParams.find("$skip");
1935a7e877eSJames Feist     if (it != req.urlParams.end())
19416428a1aSJason M. Bills     {
1955a7e877eSJames Feist         std::string skipParam = it->value();
19616428a1aSJason M. Bills         char* ptr = nullptr;
1975a7e877eSJames Feist         skip = std::strtoul(skipParam.c_str(), &ptr, 10);
1985a7e877eSJames Feist         if (skipParam.empty() || *ptr != '\0')
19916428a1aSJason M. Bills         {
20016428a1aSJason M. Bills 
2018d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2028d1b46d7Szhanghch05                 asyncResp->res, std::string(skipParam), "$skip");
20316428a1aSJason M. Bills             return false;
20416428a1aSJason M. Bills         }
20516428a1aSJason M. Bills     }
20616428a1aSJason M. Bills     return true;
20716428a1aSJason M. Bills }
20816428a1aSJason M. Bills 
209271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
2108d1b46d7Szhanghch05 static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2118d1b46d7Szhanghch05                         const crow::Request& req, uint64_t& top)
21216428a1aSJason M. Bills {
2135a7e877eSJames Feist     boost::urls::url_view::params_type::iterator it =
2145a7e877eSJames Feist         req.urlParams.find("$top");
2155a7e877eSJames Feist     if (it != req.urlParams.end())
21616428a1aSJason M. Bills     {
2175a7e877eSJames Feist         std::string topParam = it->value();
21816428a1aSJason M. Bills         char* ptr = nullptr;
2195a7e877eSJames Feist         top = std::strtoul(topParam.c_str(), &ptr, 10);
2205a7e877eSJames Feist         if (topParam.empty() || *ptr != '\0')
22116428a1aSJason M. Bills         {
2228d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2238d1b46d7Szhanghch05                 asyncResp->res, std::string(topParam), "$top");
22416428a1aSJason M. Bills             return false;
22516428a1aSJason M. Bills         }
226271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
22716428a1aSJason M. Bills         {
22816428a1aSJason M. Bills 
22916428a1aSJason M. Bills             messages::queryParameterOutOfRange(
2308d1b46d7Szhanghch05                 asyncResp->res, std::to_string(top), "$top",
23116428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
23216428a1aSJason M. Bills             return false;
23316428a1aSJason M. Bills         }
23416428a1aSJason M. Bills     }
23516428a1aSJason M. Bills     return true;
23616428a1aSJason M. Bills }
23716428a1aSJason M. Bills 
2387e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
239e85d6b16SJason M. Bills                                     const bool firstEntry = true)
24016428a1aSJason M. Bills {
24116428a1aSJason M. Bills     int ret = 0;
24216428a1aSJason M. Bills     static uint64_t prevTs = 0;
24316428a1aSJason M. Bills     static int index = 0;
244e85d6b16SJason M. Bills     if (firstEntry)
245e85d6b16SJason M. Bills     {
246e85d6b16SJason M. Bills         prevTs = 0;
247e85d6b16SJason M. Bills     }
248e85d6b16SJason M. Bills 
24916428a1aSJason M. Bills     // Get the entry timestamp
25016428a1aSJason M. Bills     uint64_t curTs = 0;
25116428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
25216428a1aSJason M. Bills     if (ret < 0)
25316428a1aSJason M. Bills     {
25416428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
25516428a1aSJason M. Bills                          << strerror(-ret);
25616428a1aSJason M. Bills         return false;
25716428a1aSJason M. Bills     }
25816428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
25916428a1aSJason M. Bills     if (curTs == prevTs)
26016428a1aSJason M. Bills     {
26116428a1aSJason M. Bills         index++;
26216428a1aSJason M. Bills     }
26316428a1aSJason M. Bills     else
26416428a1aSJason M. Bills     {
26516428a1aSJason M. Bills         // Otherwise, reset it
26616428a1aSJason M. Bills         index = 0;
26716428a1aSJason M. Bills     }
26816428a1aSJason M. Bills     // Save the timestamp
26916428a1aSJason M. Bills     prevTs = curTs;
27016428a1aSJason M. Bills 
27116428a1aSJason M. Bills     entryID = std::to_string(curTs);
27216428a1aSJason M. Bills     if (index > 0)
27316428a1aSJason M. Bills     {
27416428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
27516428a1aSJason M. Bills     }
27616428a1aSJason M. Bills     return true;
27716428a1aSJason M. Bills }
27816428a1aSJason M. Bills 
279e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
280e85d6b16SJason M. Bills                              const bool firstEntry = true)
28195820184SJason M. Bills {
282271584abSEd Tanous     static time_t prevTs = 0;
28395820184SJason M. Bills     static int index = 0;
284e85d6b16SJason M. Bills     if (firstEntry)
285e85d6b16SJason M. Bills     {
286e85d6b16SJason M. Bills         prevTs = 0;
287e85d6b16SJason M. Bills     }
288e85d6b16SJason M. Bills 
28995820184SJason M. Bills     // Get the entry timestamp
290271584abSEd Tanous     std::time_t curTs = 0;
29195820184SJason M. Bills     std::tm timeStruct = {};
29295820184SJason M. Bills     std::istringstream entryStream(logEntry);
29395820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
29495820184SJason M. Bills     {
29595820184SJason M. Bills         curTs = std::mktime(&timeStruct);
29695820184SJason M. Bills     }
29795820184SJason M. Bills     // If the timestamp isn't unique, increment the index
29895820184SJason M. Bills     if (curTs == prevTs)
29995820184SJason M. Bills     {
30095820184SJason M. Bills         index++;
30195820184SJason M. Bills     }
30295820184SJason M. Bills     else
30395820184SJason M. Bills     {
30495820184SJason M. Bills         // Otherwise, reset it
30595820184SJason M. Bills         index = 0;
30695820184SJason M. Bills     }
30795820184SJason M. Bills     // Save the timestamp
30895820184SJason M. Bills     prevTs = curTs;
30995820184SJason M. Bills 
31095820184SJason M. Bills     entryID = std::to_string(curTs);
31195820184SJason M. Bills     if (index > 0)
31295820184SJason M. Bills     {
31395820184SJason M. Bills         entryID += "_" + std::to_string(index);
31495820184SJason M. Bills     }
31595820184SJason M. Bills     return true;
31695820184SJason M. Bills }
31795820184SJason M. Bills 
3187e860f15SJohn Edward Broadbent inline static bool
3198d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3208d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3218d1b46d7Szhanghch05                        uint64_t& index)
32216428a1aSJason M. Bills {
32316428a1aSJason M. Bills     if (entryID.empty())
32416428a1aSJason M. Bills     {
32516428a1aSJason M. Bills         return false;
32616428a1aSJason M. Bills     }
32716428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
32839e77504SEd Tanous     std::string_view tsStr(entryID);
32916428a1aSJason M. Bills 
33081ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
33116428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
33216428a1aSJason M. Bills     {
33316428a1aSJason M. Bills         // Timestamp has an index
33416428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
33539e77504SEd Tanous         std::string_view indexStr(entryID);
33616428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
337c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
338c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
339c0bd5e4bSEd Tanous         if (ec != std::errc())
34016428a1aSJason M. Bills         {
3418d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
34216428a1aSJason M. Bills             return false;
34316428a1aSJason M. Bills         }
34416428a1aSJason M. Bills     }
34516428a1aSJason M. Bills     // Timestamp has no index
346c0bd5e4bSEd Tanous     auto [ptr, ec] =
347c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
348c0bd5e4bSEd Tanous     if (ec != std::errc())
34916428a1aSJason M. Bills     {
3508d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
35116428a1aSJason M. Bills         return false;
35216428a1aSJason M. Bills     }
35316428a1aSJason M. Bills     return true;
35416428a1aSJason M. Bills }
35516428a1aSJason M. Bills 
35695820184SJason M. Bills static bool
35795820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
35895820184SJason M. Bills {
35995820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
36095820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
36195820184SJason M. Bills 
36295820184SJason M. Bills     // Loop through the directory looking for redfish log files
36395820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
36495820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
36595820184SJason M. Bills     {
36695820184SJason M. Bills         // If we find a redfish log file, save the path
36795820184SJason M. Bills         std::string filename = dirEnt.path().filename();
36895820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
36995820184SJason M. Bills         {
37095820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
37195820184SJason M. Bills         }
37295820184SJason M. Bills     }
37395820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
37495820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
37595820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
37695820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
37795820184SJason M. Bills 
37895820184SJason M. Bills     return !redfishLogFiles.empty();
37995820184SJason M. Bills }
38095820184SJason M. Bills 
3818d1b46d7Szhanghch05 inline void
3828d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3835cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3845cb1dd27SAsmitha Karunanithi {
3855cb1dd27SAsmitha Karunanithi     std::string dumpPath;
3865cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
3875cb1dd27SAsmitha Karunanithi     {
3885cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
3895cb1dd27SAsmitha Karunanithi     }
3905cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
3915cb1dd27SAsmitha Karunanithi     {
3925cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
3935cb1dd27SAsmitha Karunanithi     }
3945cb1dd27SAsmitha Karunanithi     else
3955cb1dd27SAsmitha Karunanithi     {
3965cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
3975cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3985cb1dd27SAsmitha Karunanithi         return;
3995cb1dd27SAsmitha Karunanithi     }
4005cb1dd27SAsmitha Karunanithi 
4015cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
4025cb1dd27SAsmitha Karunanithi         [asyncResp, dumpPath, dumpType](const boost::system::error_code ec,
4035cb1dd27SAsmitha Karunanithi                                         GetManagedObjectsType& resp) {
4045cb1dd27SAsmitha Karunanithi             if (ec)
4055cb1dd27SAsmitha Karunanithi             {
4065cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4075cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
4085cb1dd27SAsmitha Karunanithi                 return;
4095cb1dd27SAsmitha Karunanithi             }
4105cb1dd27SAsmitha Karunanithi 
4115cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4125cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
413b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
414b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
415b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
416b47452b2SAsmitha Karunanithi                 "/entry/";
4175cb1dd27SAsmitha Karunanithi 
4185cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4195cb1dd27SAsmitha Karunanithi             {
420b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4215cb1dd27SAsmitha Karunanithi                 {
4225cb1dd27SAsmitha Karunanithi                     continue;
4235cb1dd27SAsmitha Karunanithi                 }
4245cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
4255cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
42635440d18SAsmitha Karunanithi                 std::string dumpStatus;
42735440d18SAsmitha Karunanithi                 nlohmann::json thisEntry;
4282dfd18efSEd Tanous 
4292dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4302dfd18efSEd Tanous                 if (entryID.empty())
4315cb1dd27SAsmitha Karunanithi                 {
4325cb1dd27SAsmitha Karunanithi                     continue;
4335cb1dd27SAsmitha Karunanithi                 }
4345cb1dd27SAsmitha Karunanithi 
4355cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4365cb1dd27SAsmitha Karunanithi                 {
43735440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
43835440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
43935440d18SAsmitha Karunanithi                     {
44035440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
44135440d18SAsmitha Karunanithi                         {
44235440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
44335440d18SAsmitha Karunanithi                             {
44435440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
44535440d18SAsmitha Karunanithi                                     &propertyMap.second);
44635440d18SAsmitha Karunanithi                                 if (status == nullptr)
44735440d18SAsmitha Karunanithi                                 {
44835440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
44935440d18SAsmitha Karunanithi                                     break;
45035440d18SAsmitha Karunanithi                                 }
45135440d18SAsmitha Karunanithi                                 dumpStatus = *status;
45235440d18SAsmitha Karunanithi                             }
45335440d18SAsmitha Karunanithi                         }
45435440d18SAsmitha Karunanithi                     }
45535440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
45635440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
4575cb1dd27SAsmitha Karunanithi                     {
4585cb1dd27SAsmitha Karunanithi 
4595cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4605cb1dd27SAsmitha Karunanithi                         {
4615cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4625cb1dd27SAsmitha Karunanithi                             {
4635cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
4645cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4655cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4665cb1dd27SAsmitha Karunanithi                                 {
4675cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4685cb1dd27SAsmitha Karunanithi                                     break;
4695cb1dd27SAsmitha Karunanithi                                 }
4705cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4715cb1dd27SAsmitha Karunanithi                                 break;
4725cb1dd27SAsmitha Karunanithi                             }
4735cb1dd27SAsmitha Karunanithi                         }
4745cb1dd27SAsmitha Karunanithi                     }
4755cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4765cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4775cb1dd27SAsmitha Karunanithi                     {
4785cb1dd27SAsmitha Karunanithi 
4795cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4805cb1dd27SAsmitha Karunanithi                         {
4815cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4825cb1dd27SAsmitha Karunanithi                             {
4835cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4845cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4855cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4865cb1dd27SAsmitha Karunanithi                                 {
4875cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4885cb1dd27SAsmitha Karunanithi                                     break;
4895cb1dd27SAsmitha Karunanithi                                 }
4905cb1dd27SAsmitha Karunanithi                                 timestamp =
4915cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
4925cb1dd27SAsmitha Karunanithi                                 break;
4935cb1dd27SAsmitha Karunanithi                             }
4945cb1dd27SAsmitha Karunanithi                         }
4955cb1dd27SAsmitha Karunanithi                     }
4965cb1dd27SAsmitha Karunanithi                 }
4975cb1dd27SAsmitha Karunanithi 
49835440d18SAsmitha Karunanithi                 if (dumpStatus != "xyz.openbmc_project.Common.Progress."
49935440d18SAsmitha Karunanithi                                   "OperationStatus.Completed" &&
50035440d18SAsmitha Karunanithi                     !dumpStatus.empty())
50135440d18SAsmitha Karunanithi                 {
50235440d18SAsmitha Karunanithi                     // Dump status is not Complete, no need to enumerate
50335440d18SAsmitha Karunanithi                     continue;
50435440d18SAsmitha Karunanithi                 }
50535440d18SAsmitha Karunanithi 
506*647b3cdcSGeorge Liu                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
5075cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
5085cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
5095cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5105cb1dd27SAsmitha Karunanithi                 thisEntry["Created"] = crow::utility::getDateTime(timestamp);
5115cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5125cb1dd27SAsmitha Karunanithi 
513d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5145cb1dd27SAsmitha Karunanithi 
5155cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5165cb1dd27SAsmitha Karunanithi                 {
517d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
518d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
519de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
520de8d94a3SAbhishek Patel                         entryID + "/attachment";
5215cb1dd27SAsmitha Karunanithi                 }
5225cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5235cb1dd27SAsmitha Karunanithi                 {
524d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
525d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
526d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
527de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
528de8d94a3SAbhishek Patel                         entryID + "/attachment";
5295cb1dd27SAsmitha Karunanithi                 }
53035440d18SAsmitha Karunanithi                 entriesArray.push_back(std::move(thisEntry));
5315cb1dd27SAsmitha Karunanithi             }
5325cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5335cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5345cb1dd27SAsmitha Karunanithi         },
5355cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5365cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5375cb1dd27SAsmitha Karunanithi }
5385cb1dd27SAsmitha Karunanithi 
5398d1b46d7Szhanghch05 inline void
5408d1b46d7Szhanghch05     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5418d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5425cb1dd27SAsmitha Karunanithi {
5435cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5445cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5455cb1dd27SAsmitha Karunanithi     {
5465cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5475cb1dd27SAsmitha Karunanithi     }
5485cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5495cb1dd27SAsmitha Karunanithi     {
5505cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5515cb1dd27SAsmitha Karunanithi     }
5525cb1dd27SAsmitha Karunanithi     else
5535cb1dd27SAsmitha Karunanithi     {
5545cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5555cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5565cb1dd27SAsmitha Karunanithi         return;
5575cb1dd27SAsmitha Karunanithi     }
5585cb1dd27SAsmitha Karunanithi 
5595cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
5605cb1dd27SAsmitha Karunanithi         [asyncResp, entryID, dumpPath, dumpType](
5615cb1dd27SAsmitha Karunanithi             const boost::system::error_code ec, GetManagedObjectsType& resp) {
5625cb1dd27SAsmitha Karunanithi             if (ec)
5635cb1dd27SAsmitha Karunanithi             {
5645cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5655cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5665cb1dd27SAsmitha Karunanithi                 return;
5675cb1dd27SAsmitha Karunanithi             }
5685cb1dd27SAsmitha Karunanithi 
569b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
570b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
571b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
572b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
573b47452b2SAsmitha Karunanithi                 "/entry/";
574b47452b2SAsmitha Karunanithi 
5755cb1dd27SAsmitha Karunanithi             for (auto& objectPath : resp)
5765cb1dd27SAsmitha Karunanithi             {
577b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5785cb1dd27SAsmitha Karunanithi                 {
5795cb1dd27SAsmitha Karunanithi                     continue;
5805cb1dd27SAsmitha Karunanithi                 }
5815cb1dd27SAsmitha Karunanithi 
5825cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5835cb1dd27SAsmitha Karunanithi                 std::time_t timestamp;
5845cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
58535440d18SAsmitha Karunanithi                 std::string dumpStatus;
5865cb1dd27SAsmitha Karunanithi 
5875cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : objectPath.second)
5885cb1dd27SAsmitha Karunanithi                 {
58935440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
59035440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
59135440d18SAsmitha Karunanithi                     {
59235440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
59335440d18SAsmitha Karunanithi                         {
59435440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
59535440d18SAsmitha Karunanithi                             {
59635440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
59735440d18SAsmitha Karunanithi                                     &propertyMap.second);
59835440d18SAsmitha Karunanithi                                 if (status == nullptr)
59935440d18SAsmitha Karunanithi                                 {
60035440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
60135440d18SAsmitha Karunanithi                                     break;
60235440d18SAsmitha Karunanithi                                 }
60335440d18SAsmitha Karunanithi                                 dumpStatus = *status;
60435440d18SAsmitha Karunanithi                             }
60535440d18SAsmitha Karunanithi                         }
60635440d18SAsmitha Karunanithi                     }
60735440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
60835440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
6095cb1dd27SAsmitha Karunanithi                     {
6105cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6115cb1dd27SAsmitha Karunanithi                         {
6125cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
6135cb1dd27SAsmitha Karunanithi                             {
6145cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
6155cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6165cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
6175cb1dd27SAsmitha Karunanithi                                 {
6185cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6195cb1dd27SAsmitha Karunanithi                                     break;
6205cb1dd27SAsmitha Karunanithi                                 }
6215cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
6225cb1dd27SAsmitha Karunanithi                                 break;
6235cb1dd27SAsmitha Karunanithi                             }
6245cb1dd27SAsmitha Karunanithi                         }
6255cb1dd27SAsmitha Karunanithi                     }
6265cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6275cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6285cb1dd27SAsmitha Karunanithi                     {
6295cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6305cb1dd27SAsmitha Karunanithi                         {
6315cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6325cb1dd27SAsmitha Karunanithi                             {
6335cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6345cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6355cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6365cb1dd27SAsmitha Karunanithi                                 {
6375cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6385cb1dd27SAsmitha Karunanithi                                     break;
6395cb1dd27SAsmitha Karunanithi                                 }
6405cb1dd27SAsmitha Karunanithi                                 timestamp =
6415cb1dd27SAsmitha Karunanithi                                     static_cast<std::time_t>(*usecsTimeStamp);
6425cb1dd27SAsmitha Karunanithi                                 break;
6435cb1dd27SAsmitha Karunanithi                             }
6445cb1dd27SAsmitha Karunanithi                         }
6455cb1dd27SAsmitha Karunanithi                     }
6465cb1dd27SAsmitha Karunanithi                 }
6475cb1dd27SAsmitha Karunanithi 
64835440d18SAsmitha Karunanithi                 if (dumpStatus != "xyz.openbmc_project.Common.Progress."
64935440d18SAsmitha Karunanithi                                   "OperationStatus.Completed" &&
65035440d18SAsmitha Karunanithi                     !dumpStatus.empty())
65135440d18SAsmitha Karunanithi                 {
65235440d18SAsmitha Karunanithi                     // Dump status is not Complete
65335440d18SAsmitha Karunanithi                     // return not found until status is changed to Completed
65435440d18SAsmitha Karunanithi                     messages::resourceNotFound(asyncResp->res,
65535440d18SAsmitha Karunanithi                                                dumpType + " dump", entryID);
65635440d18SAsmitha Karunanithi                     return;
65735440d18SAsmitha Karunanithi                 }
65835440d18SAsmitha Karunanithi 
6595cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
660*647b3cdcSGeorge Liu                     "#LogEntry.v1_8_0.LogEntry";
6615cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6625cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6635cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6645cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6655cb1dd27SAsmitha Karunanithi                     crow::utility::getDateTime(timestamp);
6665cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6675cb1dd27SAsmitha Karunanithi 
668d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6695cb1dd27SAsmitha Karunanithi 
6705cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6715cb1dd27SAsmitha Karunanithi                 {
672d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
673d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
674de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
675de8d94a3SAbhishek Patel                         entryID + "/attachment";
6765cb1dd27SAsmitha Karunanithi                 }
6775cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6785cb1dd27SAsmitha Karunanithi                 {
679d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
680d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6815cb1dd27SAsmitha Karunanithi                         "System";
682d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
683de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
684de8d94a3SAbhishek Patel                         entryID + "/attachment";
6855cb1dd27SAsmitha Karunanithi                 }
6865cb1dd27SAsmitha Karunanithi             }
687b47452b2SAsmitha Karunanithi             if (foundDumpEntry == false)
688b47452b2SAsmitha Karunanithi             {
689b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
690b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
691b47452b2SAsmitha Karunanithi                 return;
692b47452b2SAsmitha Karunanithi             }
6935cb1dd27SAsmitha Karunanithi         },
6945cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6955cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6965cb1dd27SAsmitha Karunanithi }
6975cb1dd27SAsmitha Karunanithi 
6988d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6999878256fSStanley Chu                             const std::string& entryID,
700b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
7015cb1dd27SAsmitha Karunanithi {
7023de8d8baSGeorge Liu     auto respHandler = [asyncResp,
7033de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
7045cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
7055cb1dd27SAsmitha Karunanithi         if (ec)
7065cb1dd27SAsmitha Karunanithi         {
7073de8d8baSGeorge Liu             if (ec.value() == EBADR)
7083de8d8baSGeorge Liu             {
7093de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
7103de8d8baSGeorge Liu                 return;
7113de8d8baSGeorge Liu             }
7125cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
7135cb1dd27SAsmitha Karunanithi                              << ec;
7145cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
7155cb1dd27SAsmitha Karunanithi             return;
7165cb1dd27SAsmitha Karunanithi         }
7175cb1dd27SAsmitha Karunanithi     };
7185cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7195cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
720b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
721b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
722b47452b2SAsmitha Karunanithi             entryID,
7235cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7245cb1dd27SAsmitha Karunanithi }
7255cb1dd27SAsmitha Karunanithi 
7268d1b46d7Szhanghch05 inline void
7278d1b46d7Szhanghch05     createDumpTaskCallback(const crow::Request& req,
7288d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7298d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
730a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
731a43be80fSAsmitha Karunanithi {
732a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7336145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
734a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
735a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
736cb13a392SEd Tanous             if (err)
737cb13a392SEd Tanous             {
7386145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7396145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7406145ed6fSAsmitha Karunanithi                 return task::completed;
741cb13a392SEd Tanous             }
742a43be80fSAsmitha Karunanithi             std::vector<std::pair<
743a43be80fSAsmitha Karunanithi                 std::string,
744a43be80fSAsmitha Karunanithi                 std::vector<std::pair<std::string, std::variant<std::string>>>>>
745a43be80fSAsmitha Karunanithi                 interfacesList;
746a43be80fSAsmitha Karunanithi 
747a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
748a43be80fSAsmitha Karunanithi 
749a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
750a43be80fSAsmitha Karunanithi 
751b47452b2SAsmitha Karunanithi             if (objPath.str ==
752b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
753b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
754b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
755a43be80fSAsmitha Karunanithi             {
756a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
757a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
758a43be80fSAsmitha Karunanithi 
759a43be80fSAsmitha Karunanithi                 std::string headerLoc =
760a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
761a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
762a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
763a43be80fSAsmitha Karunanithi 
764a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
765b47452b2SAsmitha Karunanithi                 return task::completed;
7666145ed6fSAsmitha Karunanithi             }
767a43be80fSAsmitha Karunanithi             return task::completed;
768a43be80fSAsmitha Karunanithi         },
769a43be80fSAsmitha Karunanithi         "type='signal',interface='org.freedesktop.DBus."
770a43be80fSAsmitha Karunanithi         "ObjectManager',"
771a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
772a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
773a43be80fSAsmitha Karunanithi 
774a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
775a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
776a43be80fSAsmitha Karunanithi     task->payload.emplace(req);
777a43be80fSAsmitha Karunanithi }
778a43be80fSAsmitha Karunanithi 
7798d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7808d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
781a43be80fSAsmitha Karunanithi {
782a43be80fSAsmitha Karunanithi 
783a43be80fSAsmitha Karunanithi     std::string dumpPath;
784a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
785a43be80fSAsmitha Karunanithi     {
786a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
787a43be80fSAsmitha Karunanithi     }
788a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
789a43be80fSAsmitha Karunanithi     {
790a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
791a43be80fSAsmitha Karunanithi     }
792a43be80fSAsmitha Karunanithi     else
793a43be80fSAsmitha Karunanithi     {
794a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
795a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
796a43be80fSAsmitha Karunanithi         return;
797a43be80fSAsmitha Karunanithi     }
798a43be80fSAsmitha Karunanithi 
799a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
800a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
801a43be80fSAsmitha Karunanithi 
802a43be80fSAsmitha Karunanithi     if (!redfish::json_util::readJson(
803a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
804a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
805a43be80fSAsmitha Karunanithi     {
806a43be80fSAsmitha Karunanithi         return;
807a43be80fSAsmitha Karunanithi     }
808a43be80fSAsmitha Karunanithi 
809a43be80fSAsmitha Karunanithi     if (dumpType == "System")
810a43be80fSAsmitha Karunanithi     {
811a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
812a43be80fSAsmitha Karunanithi         {
813a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
814a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType'/"
815a43be80fSAsmitha Karunanithi                                 "'OEMDiagnosticDataType' value not found!";
816a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
817a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
818a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
819a43be80fSAsmitha Karunanithi             return;
820a43be80fSAsmitha Karunanithi         }
8213174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
822a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
823a43be80fSAsmitha Karunanithi         {
824a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
825a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
826a43be80fSAsmitha Karunanithi                                     "System Dump creation parameters");
827a43be80fSAsmitha Karunanithi             return;
828a43be80fSAsmitha Karunanithi         }
829a43be80fSAsmitha Karunanithi     }
830a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
831a43be80fSAsmitha Karunanithi     {
832a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
833a43be80fSAsmitha Karunanithi         {
834a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
835a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType' not found!";
836a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
837a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
838a43be80fSAsmitha Karunanithi             return;
839a43be80fSAsmitha Karunanithi         }
8403174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
841a43be80fSAsmitha Karunanithi         {
842a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
843a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
844a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
845a43be80fSAsmitha Karunanithi                                     "BMC Dump creation parameters");
846a43be80fSAsmitha Karunanithi             return;
847a43be80fSAsmitha Karunanithi         }
848a43be80fSAsmitha Karunanithi     }
849a43be80fSAsmitha Karunanithi 
850a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
851a43be80fSAsmitha Karunanithi         [asyncResp, req, dumpPath, dumpType](const boost::system::error_code ec,
852a43be80fSAsmitha Karunanithi                                              const uint32_t& dumpId) {
853a43be80fSAsmitha Karunanithi             if (ec)
854a43be80fSAsmitha Karunanithi             {
855a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
856a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
857a43be80fSAsmitha Karunanithi                 return;
858a43be80fSAsmitha Karunanithi             }
859a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
860a43be80fSAsmitha Karunanithi 
861a43be80fSAsmitha Karunanithi             createDumpTaskCallback(req, asyncResp, dumpId, dumpPath, dumpType);
862a43be80fSAsmitha Karunanithi         },
863b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
864b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
865b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
866a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
867a43be80fSAsmitha Karunanithi }
868a43be80fSAsmitha Karunanithi 
8698d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8708d1b46d7Szhanghch05                       const std::string& dumpType)
87180319af1SAsmitha Karunanithi {
872b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
873b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8748d1b46d7Szhanghch05 
87580319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
876b47452b2SAsmitha Karunanithi         [asyncResp, dumpType](const boost::system::error_code ec,
87780319af1SAsmitha Karunanithi                               const std::vector<std::string>& subTreePaths) {
87880319af1SAsmitha Karunanithi             if (ec)
87980319af1SAsmitha Karunanithi             {
88080319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
88180319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
88280319af1SAsmitha Karunanithi                 return;
88380319af1SAsmitha Karunanithi             }
88480319af1SAsmitha Karunanithi 
88580319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
88680319af1SAsmitha Karunanithi             {
8872dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8882dfd18efSEd Tanous                 std::string logID = objPath.filename();
8892dfd18efSEd Tanous                 if (logID.empty())
89080319af1SAsmitha Karunanithi                 {
8912dfd18efSEd Tanous                     continue;
89280319af1SAsmitha Karunanithi                 }
8932dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
89480319af1SAsmitha Karunanithi             }
89580319af1SAsmitha Karunanithi         },
89680319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
89780319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
89880319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
899b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
900b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
901b47452b2SAsmitha Karunanithi                                    dumpType});
90280319af1SAsmitha Karunanithi }
90380319af1SAsmitha Karunanithi 
9047e860f15SJohn Edward Broadbent inline static void parseCrashdumpParameters(
905043a0536SJohnathan Mantey     const std::vector<std::pair<std::string, VariantType>>& params,
906043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
907043a0536SJohnathan Mantey {
908043a0536SJohnathan Mantey     for (auto property : params)
909043a0536SJohnathan Mantey     {
910043a0536SJohnathan Mantey         if (property.first == "Timestamp")
911043a0536SJohnathan Mantey         {
912043a0536SJohnathan Mantey             const std::string* value =
9138d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
914043a0536SJohnathan Mantey             if (value != nullptr)
915043a0536SJohnathan Mantey             {
916043a0536SJohnathan Mantey                 timestamp = *value;
917043a0536SJohnathan Mantey             }
918043a0536SJohnathan Mantey         }
919043a0536SJohnathan Mantey         else if (property.first == "Filename")
920043a0536SJohnathan Mantey         {
921043a0536SJohnathan Mantey             const std::string* value =
9228d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
923043a0536SJohnathan Mantey             if (value != nullptr)
924043a0536SJohnathan Mantey             {
925043a0536SJohnathan Mantey                 filename = *value;
926043a0536SJohnathan Mantey             }
927043a0536SJohnathan Mantey         }
928043a0536SJohnathan Mantey         else if (property.first == "Log")
929043a0536SJohnathan Mantey         {
930043a0536SJohnathan Mantey             const std::string* value =
9318d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
932043a0536SJohnathan Mantey             if (value != nullptr)
933043a0536SJohnathan Mantey             {
934043a0536SJohnathan Mantey                 logfile = *value;
935043a0536SJohnathan Mantey             }
936043a0536SJohnathan Mantey         }
937043a0536SJohnathan Mantey     }
938043a0536SJohnathan Mantey }
939043a0536SJohnathan Mantey 
940a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
9417e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9421da66f75SEd Tanous {
943c4bf6374SJason M. Bills     /**
944c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
945c4bf6374SJason M. Bills      */
9467e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
947ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
9487e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9497e860f15SJohn Edward Broadbent             [](const crow::Request&,
9507e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
9517e860f15SJohn Edward Broadbent 
952c4bf6374SJason M. Bills             {
9537e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
9547e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
955c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
956c4bf6374SJason M. Bills                     "#LogServiceCollection.LogServiceCollection";
957c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
958029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices";
9597e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
9607e860f15SJohn Edward Broadbent                     "System Log Services Collection";
961c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
962c4bf6374SJason M. Bills                     "Collection of LogServices for this Computer System";
9637e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
9647e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
965c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
966029573d4SEd Tanous                 logServiceArray.push_back(
9677e860f15SJohn Edward Broadbent                     {{"@odata.id",
9687e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/EventLog"}});
9695cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
970c9bb6861Sraviteja-b                 logServiceArray.push_back(
9717e860f15SJohn Edward Broadbent                     {{"@odata.id",
9727e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/Dump"}});
973c9bb6861Sraviteja-b #endif
974c9bb6861Sraviteja-b 
975d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
976d53dd41fSJason M. Bills                 logServiceArray.push_back(
977cb92c03bSAndrew Geissler                     {{"@odata.id",
978424c4176SJason M. Bills                       "/redfish/v1/Systems/system/LogServices/Crashdump"}});
979d53dd41fSJason M. Bills #endif
980c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
981c4bf6374SJason M. Bills                     logServiceArray.size();
982a3316fc6SZhikuiRen 
983a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
984a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec,
985a3316fc6SZhikuiRen                                 const std::vector<std::string>& subtreePath) {
986a3316fc6SZhikuiRen                         if (ec)
987a3316fc6SZhikuiRen                         {
988a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR << ec;
989a3316fc6SZhikuiRen                             return;
990a3316fc6SZhikuiRen                         }
991a3316fc6SZhikuiRen 
992a3316fc6SZhikuiRen                         for (auto& pathStr : subtreePath)
993a3316fc6SZhikuiRen                         {
994a3316fc6SZhikuiRen                             if (pathStr.find("PostCode") != std::string::npos)
995a3316fc6SZhikuiRen                             {
99623a21a1cSEd Tanous                                 nlohmann::json& logServiceArrayLocal =
997a3316fc6SZhikuiRen                                     asyncResp->res.jsonValue["Members"];
99823a21a1cSEd Tanous                                 logServiceArrayLocal.push_back(
999a3316fc6SZhikuiRen                                     {{"@odata.id", "/redfish/v1/Systems/system/"
1000a3316fc6SZhikuiRen                                                    "LogServices/PostCodes"}});
10017e860f15SJohn Edward Broadbent                                 asyncResp->res
10027e860f15SJohn Edward Broadbent                                     .jsonValue["Members@odata.count"] =
100323a21a1cSEd Tanous                                     logServiceArrayLocal.size();
1004a3316fc6SZhikuiRen                                 return;
1005a3316fc6SZhikuiRen                             }
1006a3316fc6SZhikuiRen                         }
1007a3316fc6SZhikuiRen                     },
1008a3316fc6SZhikuiRen                     "xyz.openbmc_project.ObjectMapper",
1009a3316fc6SZhikuiRen                     "/xyz/openbmc_project/object_mapper",
10107e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
10117e860f15SJohn Edward Broadbent                     0, std::array<const char*, 1>{postCodeIface});
10127e860f15SJohn Edward Broadbent             });
1013c4bf6374SJason M. Bills }
1014c4bf6374SJason M. Bills 
10157e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1016c4bf6374SJason M. Bills {
10177e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
1018ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
10197e860f15SJohn Edward Broadbent         .methods(
10207e860f15SJohn Edward Broadbent             boost::beast::http::verb::
10217e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
10227e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1023c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1024029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
1025c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1026c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
1027c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
10287e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
10297e860f15SJohn Edward Broadbent                 "System Event Log Service";
1030c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
1031c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10327c8c4058STejas Patil 
10337c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
10347c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
10357c8c4058STejas Patil 
10367c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10377c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10387c8c4058STejas Patil                 redfishDateTimeOffset.second;
10397c8c4058STejas Patil 
1040c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
1041c4bf6374SJason M. Bills                 {"@odata.id",
1042029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1043e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1044e7d6c8b2SGunnar Mills 
1045e7d6c8b2SGunnar Mills                 {"target", "/redfish/v1/Systems/system/LogServices/EventLog/"
1046e7d6c8b2SGunnar Mills                            "Actions/LogService.ClearLog"}};
10477e860f15SJohn Edward Broadbent         });
1048489640c6SJason M. Bills }
1049489640c6SJason M. Bills 
10507e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1051489640c6SJason M. Bills {
10527e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1053489640c6SJason M. Bills                       "LogService.ClearLog/")
1054432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
10567e860f15SJohn Edward Broadbent             [](const crow::Request&,
10577e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1058489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1059489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1060489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1061489640c6SJason M. Bills                 {
1062489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1063489640c6SJason M. Bills                     {
1064489640c6SJason M. Bills                         std::error_code ec;
1065489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1066489640c6SJason M. Bills                     }
1067489640c6SJason M. Bills                 }
1068489640c6SJason M. Bills 
1069489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1070489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1071489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1072489640c6SJason M. Bills                         if (ec)
1073489640c6SJason M. Bills                         {
10747e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
10757e860f15SJohn Edward Broadbent                                              << ec;
1076489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1077489640c6SJason M. Bills                             return;
1078489640c6SJason M. Bills                         }
1079489640c6SJason M. Bills 
1080489640c6SJason M. Bills                         messages::success(asyncResp->res);
1081489640c6SJason M. Bills                     },
1082489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
10837e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
10847e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
10857e860f15SJohn Edward Broadbent             });
1086c4bf6374SJason M. Bills }
1087c4bf6374SJason M. Bills 
108895820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1089b5a76932SEd Tanous                                  const std::string& logEntry,
109095820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1091c4bf6374SJason M. Bills {
109295820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1093cd225da8SJason M. Bills     // First get the Timestamp
1094f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1095cd225da8SJason M. Bills     if (space == std::string::npos)
109695820184SJason M. Bills     {
109795820184SJason M. Bills         return 1;
109895820184SJason M. Bills     }
1099cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1100cd225da8SJason M. Bills     // Then get the log contents
1101f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1102cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1103cd225da8SJason M. Bills     {
1104cd225da8SJason M. Bills         return 1;
1105cd225da8SJason M. Bills     }
1106cd225da8SJason M. Bills     std::string_view entry(logEntry);
1107cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1108cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1109cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1110cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1111cd225da8SJason M. Bills                  boost::token_compress_on);
1112cd225da8SJason M. Bills     // We need at least a MessageId to be valid
1113cd225da8SJason M. Bills     if (logEntryFields.size() < 1)
1114cd225da8SJason M. Bills     {
1115cd225da8SJason M. Bills         return 1;
1116cd225da8SJason M. Bills     }
1117cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
111895820184SJason M. Bills 
11194851d45dSJason M. Bills     // Get the Message from the MessageRegistry
11204851d45dSJason M. Bills     const message_registries::Message* message =
11214851d45dSJason M. Bills         message_registries::getMessage(messageID);
1122c4bf6374SJason M. Bills 
11234851d45dSJason M. Bills     std::string msg;
11244851d45dSJason M. Bills     std::string severity;
11254851d45dSJason M. Bills     if (message != nullptr)
1126c4bf6374SJason M. Bills     {
11274851d45dSJason M. Bills         msg = message->message;
11284851d45dSJason M. Bills         severity = message->severity;
1129c4bf6374SJason M. Bills     }
1130c4bf6374SJason M. Bills 
113115a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
113215a86ff6SJason M. Bills     boost::beast::span<std::string> messageArgs;
113315a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
113415a86ff6SJason M. Bills     {
113515a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
113615a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
113715a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
113815a86ff6SJason M. Bills         if (!messageArgsStart.empty())
113915a86ff6SJason M. Bills         {
114015a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
114115a86ff6SJason M. Bills         }
114215a86ff6SJason M. Bills 
114323a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1144c4bf6374SJason M. Bills 
11454851d45dSJason M. Bills         // Fill the MessageArgs into the Message
114695820184SJason M. Bills         int i = 0;
114795820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11484851d45dSJason M. Bills         {
114995820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11504851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11514851d45dSJason M. Bills             if (argPos != std::string::npos)
11524851d45dSJason M. Bills             {
115395820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11544851d45dSJason M. Bills             }
11554851d45dSJason M. Bills         }
115615a86ff6SJason M. Bills     }
11574851d45dSJason M. Bills 
115895820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
115995820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
116095820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1161f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1162f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
116395820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1164c4bf6374SJason M. Bills     {
116595820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1166c4bf6374SJason M. Bills     }
1167c4bf6374SJason M. Bills 
1168c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
116995820184SJason M. Bills     logEntryJson = {
1170*647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1171029573d4SEd Tanous         {"@odata.id",
1172897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
117395820184SJason M. Bills              logEntryID},
1174c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
117595820184SJason M. Bills         {"Id", logEntryID},
117695820184SJason M. Bills         {"Message", std::move(msg)},
117795820184SJason M. Bills         {"MessageId", std::move(messageID)},
1178f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1179c4bf6374SJason M. Bills         {"EntryType", "Event"},
118095820184SJason M. Bills         {"Severity", std::move(severity)},
118195820184SJason M. Bills         {"Created", std::move(timestamp)}};
1182c4bf6374SJason M. Bills     return 0;
1183c4bf6374SJason M. Bills }
1184c4bf6374SJason M. Bills 
11857e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1186c4bf6374SJason M. Bills {
11877e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11887e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11898b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
11907e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
11917e860f15SJohn Edward Broadbent             [](const crow::Request& req,
11927e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1193271584abSEd Tanous                 uint64_t skip = 0;
1194271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
11958d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1196c4bf6374SJason M. Bills                 {
1197c4bf6374SJason M. Bills                     return;
1198c4bf6374SJason M. Bills                 }
11998d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1200c4bf6374SJason M. Bills                 {
1201c4bf6374SJason M. Bills                     return;
1202c4bf6374SJason M. Bills                 }
12037e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
12047e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1205c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1206c4bf6374SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
1207c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1208029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1209c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1210c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1211c4bf6374SJason M. Bills                     "Collection of System Event Log Entries";
1212cb92c03bSAndrew Geissler 
12137e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
12147e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1215c4bf6374SJason M. Bills                 logEntryArray = nlohmann::json::array();
12167e860f15SJohn Edward Broadbent                 // Go through the log files and create a unique ID for each
12177e860f15SJohn Edward Broadbent                 // entry
121895820184SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
121995820184SJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1220b01bf299SEd Tanous                 uint64_t entryCount = 0;
1221cd225da8SJason M. Bills                 std::string logEntry;
122295820184SJason M. Bills 
12237e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12247e860f15SJohn Edward Broadbent                 // backwards
12257e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12267e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1227c4bf6374SJason M. Bills                 {
1228cd225da8SJason M. Bills                     std::ifstream logStream(*it);
122995820184SJason M. Bills                     if (!logStream.is_open())
1230c4bf6374SJason M. Bills                     {
1231c4bf6374SJason M. Bills                         continue;
1232c4bf6374SJason M. Bills                     }
1233c4bf6374SJason M. Bills 
1234e85d6b16SJason M. Bills                     // Reset the unique ID on the first entry
1235e85d6b16SJason M. Bills                     bool firstEntry = true;
123695820184SJason M. Bills                     while (std::getline(logStream, logEntry))
123795820184SJason M. Bills                     {
1238c4bf6374SJason M. Bills                         entryCount++;
12397e860f15SJohn Edward Broadbent                         // Handle paging using skip (number of entries to skip
12407e860f15SJohn Edward Broadbent                         // from the start) and top (number of entries to
12417e860f15SJohn Edward Broadbent                         // display)
1242c4bf6374SJason M. Bills                         if (entryCount <= skip || entryCount > skip + top)
1243c4bf6374SJason M. Bills                         {
1244c4bf6374SJason M. Bills                             continue;
1245c4bf6374SJason M. Bills                         }
1246c4bf6374SJason M. Bills 
1247c4bf6374SJason M. Bills                         std::string idStr;
1248e85d6b16SJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1249c4bf6374SJason M. Bills                         {
1250c4bf6374SJason M. Bills                             continue;
1251c4bf6374SJason M. Bills                         }
1252c4bf6374SJason M. Bills 
1253e85d6b16SJason M. Bills                         if (firstEntry)
1254e85d6b16SJason M. Bills                         {
1255e85d6b16SJason M. Bills                             firstEntry = false;
1256e85d6b16SJason M. Bills                         }
1257e85d6b16SJason M. Bills 
1258c4bf6374SJason M. Bills                         logEntryArray.push_back({});
1259c4bf6374SJason M. Bills                         nlohmann::json& bmcLogEntry = logEntryArray.back();
12607e860f15SJohn Edward Broadbent                         if (fillEventLogEntryJson(idStr, logEntry,
12617e860f15SJohn Edward Broadbent                                                   bmcLogEntry) != 0)
1262c4bf6374SJason M. Bills                         {
1263c4bf6374SJason M. Bills                             messages::internalError(asyncResp->res);
1264c4bf6374SJason M. Bills                             return;
1265c4bf6374SJason M. Bills                         }
1266c4bf6374SJason M. Bills                     }
126795820184SJason M. Bills                 }
1268c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1269c4bf6374SJason M. Bills                 if (skip + top < entryCount)
1270c4bf6374SJason M. Bills                 {
1271c4bf6374SJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
127295820184SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/EventLog/"
127395820184SJason M. Bills                         "Entries?$skip=" +
1274c4bf6374SJason M. Bills                         std::to_string(skip + top);
1275c4bf6374SJason M. Bills                 }
12767e860f15SJohn Edward Broadbent             });
1277897967deSJason M. Bills }
1278897967deSJason M. Bills 
12797e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1280897967deSJason M. Bills {
12817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12827e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1283ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12847e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12857e860f15SJohn Edward Broadbent             [](const crow::Request&,
12867e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12877e860f15SJohn Edward Broadbent                const std::string& param) {
12887e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
12898d1b46d7Szhanghch05 
12907e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
12917e860f15SJohn Edward Broadbent                 // entry to find the target entry
1292897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1293897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1294897967deSJason M. Bills                 std::string logEntry;
1295897967deSJason M. Bills 
12967e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12977e860f15SJohn Edward Broadbent                 // backwards
12987e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12997e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1300897967deSJason M. Bills                 {
1301897967deSJason M. Bills                     std::ifstream logStream(*it);
1302897967deSJason M. Bills                     if (!logStream.is_open())
1303897967deSJason M. Bills                     {
1304897967deSJason M. Bills                         continue;
1305897967deSJason M. Bills                     }
1306897967deSJason M. Bills 
1307897967deSJason M. Bills                     // Reset the unique ID on the first entry
1308897967deSJason M. Bills                     bool firstEntry = true;
1309897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1310897967deSJason M. Bills                     {
1311897967deSJason M. Bills                         std::string idStr;
1312897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1313897967deSJason M. Bills                         {
1314897967deSJason M. Bills                             continue;
1315897967deSJason M. Bills                         }
1316897967deSJason M. Bills 
1317897967deSJason M. Bills                         if (firstEntry)
1318897967deSJason M. Bills                         {
1319897967deSJason M. Bills                             firstEntry = false;
1320897967deSJason M. Bills                         }
1321897967deSJason M. Bills 
1322897967deSJason M. Bills                         if (idStr == targetID)
1323897967deSJason M. Bills                         {
13247e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
13257e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1326897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1327897967deSJason M. Bills                             {
1328897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1329897967deSJason M. Bills                                 return;
1330897967deSJason M. Bills                             }
1331897967deSJason M. Bills                             return;
1332897967deSJason M. Bills                         }
1333897967deSJason M. Bills                     }
1334897967deSJason M. Bills                 }
1335897967deSJason M. Bills                 // Requested ID was not found
1336897967deSJason M. Bills                 messages::resourceMissingAtURI(asyncResp->res, targetID);
13377e860f15SJohn Edward Broadbent             });
133808a4e4b5SAnthony Wilson }
133908a4e4b5SAnthony Wilson 
13407e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
134108a4e4b5SAnthony Wilson {
13427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13437e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1344ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
13457e860f15SJohn Edward Broadbent         .methods(
13467e860f15SJohn Edward Broadbent             boost::beast::http::verb::
13477e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
13487e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
13497e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
13507e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
135108a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
135208a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
135308a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
135408a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
135508a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
135608a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
135708a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
135808a4e4b5SAnthony Wilson 
1359cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1360cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1361cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1362cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1363cb92c03bSAndrew Geissler                             GetManagedObjectsType& resp) {
1364cb92c03bSAndrew Geissler                     if (ec)
1365cb92c03bSAndrew Geissler                     {
1366cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1367cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1368cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1369cb92c03bSAndrew Geissler                             << ec;
1370cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1371cb92c03bSAndrew Geissler                         return;
1372cb92c03bSAndrew Geissler                     }
1373cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1374cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1375cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
1376cb92c03bSAndrew Geissler                     for (auto& objectPath : resp)
1377cb92c03bSAndrew Geissler                     {
137866664f25SEd Tanous                         uint32_t* id = nullptr;
137966664f25SEd Tanous                         std::time_t timestamp{};
1380d139c236SGeorge Liu                         std::time_t updateTimestamp{};
138166664f25SEd Tanous                         std::string* severity = nullptr;
138266664f25SEd Tanous                         std::string* message = nullptr;
1383f86bb901SAdriana Kobylak                         std::string* filePath = nullptr;
138475710de2SXiaochao Ma                         bool resolved = false;
1385f86bb901SAdriana Kobylak                         for (auto& interfaceMap : objectPath.second)
1386f86bb901SAdriana Kobylak                         {
1387f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1388f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1389f86bb901SAdriana Kobylak                             {
1390cb92c03bSAndrew Geissler                                 for (auto& propertyMap : interfaceMap.second)
1391cb92c03bSAndrew Geissler                                 {
1392cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1393cb92c03bSAndrew Geissler                                     {
1394f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1395f86bb901SAdriana Kobylak                                             &propertyMap.second);
1396cb92c03bSAndrew Geissler                                     }
1397cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1398cb92c03bSAndrew Geissler                                     {
1399cb92c03bSAndrew Geissler                                         const uint64_t* millisTimeStamp =
1400f86bb901SAdriana Kobylak                                             std::get_if<uint64_t>(
1401f86bb901SAdriana Kobylak                                                 &propertyMap.second);
1402ae34c8e8SAdriana Kobylak                                         if (millisTimeStamp != nullptr)
1403ebd45906SGeorge Liu                                         {
14047e860f15SJohn Edward Broadbent                                             timestamp =
14057e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14067e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14077e860f15SJohn Edward Broadbent                                         }
14087e860f15SJohn Edward Broadbent                                     }
14097e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
14107e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
14117e860f15SJohn Edward Broadbent                                     {
14127e860f15SJohn Edward Broadbent                                         const uint64_t* millisTimeStamp =
14137e860f15SJohn Edward Broadbent                                             std::get_if<uint64_t>(
14147e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14157e860f15SJohn Edward Broadbent                                         if (millisTimeStamp != nullptr)
14167e860f15SJohn Edward Broadbent                                         {
14177e860f15SJohn Edward Broadbent                                             updateTimestamp =
14187e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14197e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14207e860f15SJohn Edward Broadbent                                         }
14217e860f15SJohn Edward Broadbent                                     }
14227e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
14237e860f15SJohn Edward Broadbent                                     {
14247e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
14257e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14267e860f15SJohn Edward Broadbent                                     }
14277e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
14287e860f15SJohn Edward Broadbent                                     {
14297e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
14307e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14317e860f15SJohn Edward Broadbent                                     }
14327e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
14337e860f15SJohn Edward Broadbent                                     {
14347e860f15SJohn Edward Broadbent                                         bool* resolveptr = std::get_if<bool>(
14357e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14367e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14377e860f15SJohn Edward Broadbent                                         {
14387e860f15SJohn Edward Broadbent                                             messages::internalError(
14397e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14407e860f15SJohn Edward Broadbent                                             return;
14417e860f15SJohn Edward Broadbent                                         }
14427e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14437e860f15SJohn Edward Broadbent                                     }
14447e860f15SJohn Edward Broadbent                                 }
14457e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14467e860f15SJohn Edward Broadbent                                     severity == nullptr)
14477e860f15SJohn Edward Broadbent                                 {
14487e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
14497e860f15SJohn Edward Broadbent                                     return;
14507e860f15SJohn Edward Broadbent                                 }
14517e860f15SJohn Edward Broadbent                             }
14527e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
14537e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
14547e860f15SJohn Edward Broadbent                             {
14557e860f15SJohn Edward Broadbent                                 for (auto& propertyMap : interfaceMap.second)
14567e860f15SJohn Edward Broadbent                                 {
14577e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
14587e860f15SJohn Edward Broadbent                                     {
14597e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
14607e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14617e860f15SJohn Edward Broadbent                                     }
14627e860f15SJohn Edward Broadbent                                 }
14637e860f15SJohn Edward Broadbent                             }
14647e860f15SJohn Edward Broadbent                         }
14657e860f15SJohn Edward Broadbent                         // Object path without the
14667e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
14677e860f15SJohn Edward Broadbent                         // and continue.
14687e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
14697e860f15SJohn Edward Broadbent                             severity == nullptr)
14707e860f15SJohn Edward Broadbent                         {
14717e860f15SJohn Edward Broadbent                             continue;
14727e860f15SJohn Edward Broadbent                         }
14737e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
14747e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
14757e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14767e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
14777e860f15SJohn Edward Broadbent                             "/redfish/v1/Systems/system/"
14787e860f15SJohn Edward Broadbent                             "LogServices/EventLog/Entries/" +
14797e860f15SJohn Edward Broadbent                             std::to_string(*id);
14807e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
14817e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
14827e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
14837e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
14847e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
14857e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
14867e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
14877e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
14887e860f15SJohn Edward Broadbent                             crow::utility::getDateTime(timestamp);
14897e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
14907e860f15SJohn Edward Broadbent                             crow::utility::getDateTime(updateTimestamp);
14917e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
14927e860f15SJohn Edward Broadbent                         {
14937e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
14947e860f15SJohn Edward Broadbent                                 "/redfish/v1/Systems/system/LogServices/"
14957e860f15SJohn Edward Broadbent                                 "EventLog/"
14967e860f15SJohn Edward Broadbent                                 "Entries/" +
14977e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
14987e860f15SJohn Edward Broadbent                         }
14997e860f15SJohn Edward Broadbent                     }
15007e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
15017e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
15027e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
15037e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
15047e860f15SJohn Edward Broadbent                               });
15057e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
15067e860f15SJohn Edward Broadbent                         entriesArray.size();
15077e860f15SJohn Edward Broadbent                 },
15087e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
15097e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15107e860f15SJohn Edward Broadbent         });
15117e860f15SJohn Edward Broadbent }
15127e860f15SJohn Edward Broadbent 
15137e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
15147e860f15SJohn Edward Broadbent {
15157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15167e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1517ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
15187e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
15197e860f15SJohn Edward Broadbent             [](const crow::Request&,
15207e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15217e860f15SJohn Edward Broadbent                const std::string& param)
15227e860f15SJohn Edward Broadbent 
15237e860f15SJohn Edward Broadbent             {
15247e860f15SJohn Edward Broadbent                 std::string entryID = param;
15257e860f15SJohn Edward Broadbent                 dbus::utility::escapePathForDbus(entryID);
15267e860f15SJohn Edward Broadbent 
15277e860f15SJohn Edward Broadbent                 // DBus implementation of EventLog/Entries
15287e860f15SJohn Edward Broadbent                 // Make call to Logging Service to find all log entry objects
15297e860f15SJohn Edward Broadbent                 crow::connections::systemBus->async_method_call(
15307e860f15SJohn Edward Broadbent                     [asyncResp, entryID](const boost::system::error_code ec,
15317e860f15SJohn Edward Broadbent                                          GetManagedPropertyType& resp) {
15327e860f15SJohn Edward Broadbent                         if (ec.value() == EBADR)
15337e860f15SJohn Edward Broadbent                         {
15347e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
15357e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogEntry", entryID);
15367e860f15SJohn Edward Broadbent                             return;
15377e860f15SJohn Edward Broadbent                         }
15387e860f15SJohn Edward Broadbent                         if (ec)
15397e860f15SJohn Edward Broadbent                         {
15407e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "EventLogEntry (DBus) "
15417e860f15SJohn Edward Broadbent                                                 "resp_handler got error "
15427e860f15SJohn Edward Broadbent                                              << ec;
15437e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
15447e860f15SJohn Edward Broadbent                             return;
15457e860f15SJohn Edward Broadbent                         }
15467e860f15SJohn Edward Broadbent                         uint32_t* id = nullptr;
15477e860f15SJohn Edward Broadbent                         std::time_t timestamp{};
15487e860f15SJohn Edward Broadbent                         std::time_t updateTimestamp{};
15497e860f15SJohn Edward Broadbent                         std::string* severity = nullptr;
15507e860f15SJohn Edward Broadbent                         std::string* message = nullptr;
15517e860f15SJohn Edward Broadbent                         std::string* filePath = nullptr;
15527e860f15SJohn Edward Broadbent                         bool resolved = false;
15537e860f15SJohn Edward Broadbent 
15547e860f15SJohn Edward Broadbent                         for (auto& propertyMap : resp)
15557e860f15SJohn Edward Broadbent                         {
15567e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Id")
15577e860f15SJohn Edward Broadbent                             {
15587e860f15SJohn Edward Broadbent                                 id = std::get_if<uint32_t>(&propertyMap.second);
15597e860f15SJohn Edward Broadbent                             }
15607e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Timestamp")
15617e860f15SJohn Edward Broadbent                             {
15627e860f15SJohn Edward Broadbent                                 const uint64_t* millisTimeStamp =
15637e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
15647e860f15SJohn Edward Broadbent                                 if (millisTimeStamp != nullptr)
15657e860f15SJohn Edward Broadbent                                 {
1566d139c236SGeorge Liu                                     timestamp = crow::utility::getTimestamp(
1567cb92c03bSAndrew Geissler                                         *millisTimeStamp);
1568d139c236SGeorge Liu                                 }
1569ebd45906SGeorge Liu                             }
1570d139c236SGeorge Liu                             else if (propertyMap.first == "UpdateTimestamp")
1571d139c236SGeorge Liu                             {
1572d139c236SGeorge Liu                                 const uint64_t* millisTimeStamp =
15737e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1574ae34c8e8SAdriana Kobylak                                 if (millisTimeStamp != nullptr)
1575ebd45906SGeorge Liu                                 {
1576ebd45906SGeorge Liu                                     updateTimestamp =
1577ebd45906SGeorge Liu                                         crow::utility::getTimestamp(
1578d139c236SGeorge Liu                                             *millisTimeStamp);
1579cb92c03bSAndrew Geissler                                 }
1580ebd45906SGeorge Liu                             }
1581cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1582cb92c03bSAndrew Geissler                             {
1583cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1584cb92c03bSAndrew Geissler                                     &propertyMap.second);
1585cb92c03bSAndrew Geissler                             }
1586cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1587cb92c03bSAndrew Geissler                             {
1588cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1589cb92c03bSAndrew Geissler                                     &propertyMap.second);
1590ae34c8e8SAdriana Kobylak                             }
159175710de2SXiaochao Ma                             else if (propertyMap.first == "Resolved")
159275710de2SXiaochao Ma                             {
159375710de2SXiaochao Ma                                 bool* resolveptr =
159475710de2SXiaochao Ma                                     std::get_if<bool>(&propertyMap.second);
159575710de2SXiaochao Ma                                 if (resolveptr == nullptr)
159675710de2SXiaochao Ma                                 {
159775710de2SXiaochao Ma                                     messages::internalError(asyncResp->res);
159875710de2SXiaochao Ma                                     return;
159975710de2SXiaochao Ma                                 }
160075710de2SXiaochao Ma                                 resolved = *resolveptr;
160175710de2SXiaochao Ma                             }
16027e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Path")
1603f86bb901SAdriana Kobylak                             {
1604f86bb901SAdriana Kobylak                                 filePath = std::get_if<std::string>(
1605f86bb901SAdriana Kobylak                                     &propertyMap.second);
1606f86bb901SAdriana Kobylak                             }
1607f86bb901SAdriana Kobylak                         }
1608f86bb901SAdriana Kobylak                         if (id == nullptr || message == nullptr ||
1609f86bb901SAdriana Kobylak                             severity == nullptr)
1610f86bb901SAdriana Kobylak                         {
1611ae34c8e8SAdriana Kobylak                             messages::internalError(asyncResp->res);
1612271584abSEd Tanous                             return;
1613271584abSEd Tanous                         }
1614f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.type"] =
1615f86bb901SAdriana Kobylak                             "#LogEntry.v1_8_0.LogEntry";
1616f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.id"] =
16177e860f15SJohn Edward Broadbent                             "/redfish/v1/Systems/system/LogServices/EventLog/"
16187e860f15SJohn Edward Broadbent                             "Entries/" +
1619f86bb901SAdriana Kobylak                             std::to_string(*id);
16207e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Name"] =
16217e860f15SJohn Edward Broadbent                             "System Event Log Entry";
1622f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1623f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Message"] = *message;
1624f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Resolved"] = resolved;
1625f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["EntryType"] = "Event";
1626f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Severity"] =
1627f86bb901SAdriana Kobylak                             translateSeverityDbusToRedfish(*severity);
1628f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Created"] =
1629f86bb901SAdriana Kobylak                             crow::utility::getDateTime(timestamp);
1630f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Modified"] =
1631f86bb901SAdriana Kobylak                             crow::utility::getDateTime(updateTimestamp);
1632f86bb901SAdriana Kobylak                         if (filePath != nullptr)
1633f86bb901SAdriana Kobylak                         {
1634f86bb901SAdriana Kobylak                             asyncResp->res.jsonValue["AdditionalDataURI"] =
16357e860f15SJohn Edward Broadbent                                 "/redfish/v1/Systems/system/LogServices/"
16367e860f15SJohn Edward Broadbent                                 "EventLog/"
16377e860f15SJohn Edward Broadbent                                 "attachment/" +
16387e860f15SJohn Edward Broadbent                                 std::to_string(*id);
1639f86bb901SAdriana Kobylak                         }
1640cb92c03bSAndrew Geissler                     },
1641cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Logging",
1642cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging/entry/" + entryID,
1643f86bb901SAdriana Kobylak                     "org.freedesktop.DBus.Properties", "GetAll", "");
16447e860f15SJohn Edward Broadbent             });
1645336e96c6SChicago Duan 
16467e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16477e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1648ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16497e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
16507e860f15SJohn Edward Broadbent             [](const crow::Request& req,
16517e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16527e860f15SJohn Edward Broadbent                const std::string& entryId) {
165375710de2SXiaochao Ma                 std::optional<bool> resolved;
165475710de2SXiaochao Ma 
16557e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "Resolved",
16567e860f15SJohn Edward Broadbent                                          resolved))
165775710de2SXiaochao Ma                 {
165875710de2SXiaochao Ma                     return;
165975710de2SXiaochao Ma                 }
166075710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
166175710de2SXiaochao Ma 
166275710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
16634f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
166475710de2SXiaochao Ma                         if (ec)
166575710de2SXiaochao Ma                         {
166675710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
166775710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
166875710de2SXiaochao Ma                             return;
166975710de2SXiaochao Ma                         }
167075710de2SXiaochao Ma                     },
167175710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
167275710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
167375710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
167475710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
167575710de2SXiaochao Ma                     std::variant<bool>(*resolved));
16767e860f15SJohn Edward Broadbent             });
167775710de2SXiaochao Ma 
16787e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16797e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1680ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1681ed398213SEd Tanous 
16827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
16837e860f15SJohn Edward Broadbent             [](const crow::Request&,
16847e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16857e860f15SJohn Edward Broadbent                const std::string& param)
16867e860f15SJohn Edward Broadbent 
1687336e96c6SChicago Duan             {
1688336e96c6SChicago Duan                 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1689336e96c6SChicago Duan 
16907e860f15SJohn Edward Broadbent                 std::string entryID = param;
1691336e96c6SChicago Duan 
1692336e96c6SChicago Duan                 dbus::utility::escapePathForDbus(entryID);
1693336e96c6SChicago Duan 
1694336e96c6SChicago Duan                 // Process response from Logging service.
16957e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp, entryID](
16967e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
16977e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
16987e860f15SJohn Edward Broadbent                         << "EventLogEntry (DBus) doDelete callback: Done";
1699336e96c6SChicago Duan                     if (ec)
1700336e96c6SChicago Duan                     {
17013de8d8baSGeorge Liu                         if (ec.value() == EBADR)
17023de8d8baSGeorge Liu                         {
17037e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
17047e860f15SJohn Edward Broadbent                                                        "LogEntry", entryID);
17053de8d8baSGeorge Liu                             return;
17063de8d8baSGeorge Liu                         }
1707336e96c6SChicago Duan                         // TODO Handle for specific error code
17087e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "EventLogEntry (DBus) doDelete "
17097e860f15SJohn Edward Broadbent                                             "respHandler got error "
1710336e96c6SChicago Duan                                          << ec;
1711336e96c6SChicago Duan                         asyncResp->res.result(
1712336e96c6SChicago Duan                             boost::beast::http::status::internal_server_error);
1713336e96c6SChicago Duan                         return;
1714336e96c6SChicago Duan                     }
1715336e96c6SChicago Duan 
1716336e96c6SChicago Duan                     asyncResp->res.result(boost::beast::http::status::ok);
1717336e96c6SChicago Duan                 };
1718336e96c6SChicago Duan 
1719336e96c6SChicago Duan                 // Make call to Logging service to request Delete Log
1720336e96c6SChicago Duan                 crow::connections::systemBus->async_method_call(
1721336e96c6SChicago Duan                     respHandler, "xyz.openbmc_project.Logging",
1722336e96c6SChicago Duan                     "/xyz/openbmc_project/logging/entry/" + entryID,
1723336e96c6SChicago Duan                     "xyz.openbmc_project.Object.Delete", "Delete");
17247e860f15SJohn Edward Broadbent             });
1725400fd1fbSAdriana Kobylak }
1726400fd1fbSAdriana Kobylak 
17277e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1728400fd1fbSAdriana Kobylak {
17297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/"
17307e860f15SJohn Edward Broadbent                       "<str>/attachment")
1731ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17327e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
17337e860f15SJohn Edward Broadbent             [](const crow::Request& req,
17347e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17357e860f15SJohn Edward Broadbent                const std::string& param)
1736400fd1fbSAdriana Kobylak 
17377e860f15SJohn Edward Broadbent             {
1738*647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
1739*647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
1740400fd1fbSAdriana Kobylak                 {
17417e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17427e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1743400fd1fbSAdriana Kobylak                     return;
1744400fd1fbSAdriana Kobylak                 }
1745400fd1fbSAdriana Kobylak 
17467e860f15SJohn Edward Broadbent                 std::string entryID = param;
1747400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1748400fd1fbSAdriana Kobylak 
1749400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17507e860f15SJohn Edward Broadbent                     [asyncResp,
17517e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1752400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1753400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1754400fd1fbSAdriana Kobylak                         {
17557e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
17567e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1757400fd1fbSAdriana Kobylak                             return;
1758400fd1fbSAdriana Kobylak                         }
1759400fd1fbSAdriana Kobylak                         if (ec)
1760400fd1fbSAdriana Kobylak                         {
1761400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1762400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1763400fd1fbSAdriana Kobylak                             return;
1764400fd1fbSAdriana Kobylak                         }
1765400fd1fbSAdriana Kobylak 
1766400fd1fbSAdriana Kobylak                         int fd = -1;
1767400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1768400fd1fbSAdriana Kobylak                         if (fd == -1)
1769400fd1fbSAdriana Kobylak                         {
1770400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1771400fd1fbSAdriana Kobylak                             return;
1772400fd1fbSAdriana Kobylak                         }
1773400fd1fbSAdriana Kobylak 
1774400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1775400fd1fbSAdriana Kobylak                         if (size == -1)
1776400fd1fbSAdriana Kobylak                         {
1777400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1778400fd1fbSAdriana Kobylak                             return;
1779400fd1fbSAdriana Kobylak                         }
1780400fd1fbSAdriana Kobylak 
1781400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1782400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1783400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1784400fd1fbSAdriana Kobylak                         {
1785400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1786400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1787400fd1fbSAdriana Kobylak                                 << maxFileSize;
1788400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1789400fd1fbSAdriana Kobylak                             return;
1790400fd1fbSAdriana Kobylak                         }
1791400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1792400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1793400fd1fbSAdriana Kobylak                         if (rc == -1)
1794400fd1fbSAdriana Kobylak                         {
1795400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1796400fd1fbSAdriana Kobylak                             return;
1797400fd1fbSAdriana Kobylak                         }
1798400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1799400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1800400fd1fbSAdriana Kobylak                         {
1801400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1802400fd1fbSAdriana Kobylak                             return;
1803400fd1fbSAdriana Kobylak                         }
1804400fd1fbSAdriana Kobylak                         close(fd);
1805400fd1fbSAdriana Kobylak 
1806400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
18077e860f15SJohn Edward Broadbent                         std::string output =
18087e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1809400fd1fbSAdriana Kobylak 
1810400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1811400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
18127e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
18137e860f15SJohn Edward Broadbent                                                  "Base64");
1814400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1815400fd1fbSAdriana Kobylak                     },
1816400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1817400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1818400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
18197e860f15SJohn Edward Broadbent             });
18201da66f75SEd Tanous }
18211da66f75SEd Tanous 
18227e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
18231da66f75SEd Tanous {
18247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
1825ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
18267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
18277e860f15SJohn Edward Broadbent             [](const crow::Request&,
18287e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
18297e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
18307e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1831e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
18321da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
1833e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1834e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
18357e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
18367e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
1837e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
18381da66f75SEd Tanous                     "Collection of LogServices for this Manager";
18397e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
18407e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1841c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
18425cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
18435cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
18447e860f15SJohn Edward Broadbent                     {{"@odata.id",
18457e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
18465cb1dd27SAsmitha Karunanithi #endif
1847c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1848c4bf6374SJason M. Bills                 logServiceArray.push_back(
18497e860f15SJohn Edward Broadbent                     {{"@odata.id",
18507e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1851c4bf6374SJason M. Bills #endif
1852e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
1853c4bf6374SJason M. Bills                     logServiceArray.size();
18547e860f15SJohn Edward Broadbent             });
1855e1f26343SJason M. Bills }
1856e1f26343SJason M. Bills 
18577e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
1858e1f26343SJason M. Bills {
18597e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
1860ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
18617e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
18627e860f15SJohn Edward Broadbent             [](const crow::Request&,
18637e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
18648d1b46d7Szhanghch05 
18657e860f15SJohn Edward Broadbent             {
1866e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1867e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
18680f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
18690f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
18707e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
18717e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
18727e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
18737e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
1874c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
1875e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
18767c8c4058STejas Patil 
18777c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
18787c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
18797c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
18807c8c4058STejas Patil                     redfishDateTimeOffset.first;
18817c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
18827c8c4058STejas Patil                     redfishDateTimeOffset.second;
18837c8c4058STejas Patil 
1884cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
1885cd50aa42SJason M. Bills                     {"@odata.id",
1886086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
18877e860f15SJohn Edward Broadbent             });
1888e1f26343SJason M. Bills }
1889e1f26343SJason M. Bills 
1890c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
1891e1f26343SJason M. Bills                                       sd_journal* journal,
1892c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
1893e1f26343SJason M. Bills {
1894e1f26343SJason M. Bills     // Get the Log Entry contents
1895e1f26343SJason M. Bills     int ret = 0;
1896e1f26343SJason M. Bills 
1897a8fe54f0SJason M. Bills     std::string message;
1898a8fe54f0SJason M. Bills     std::string_view syslogID;
1899a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
1900a8fe54f0SJason M. Bills     if (ret < 0)
1901a8fe54f0SJason M. Bills     {
1902a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
1903a8fe54f0SJason M. Bills                          << strerror(-ret);
1904a8fe54f0SJason M. Bills     }
1905a8fe54f0SJason M. Bills     if (!syslogID.empty())
1906a8fe54f0SJason M. Bills     {
1907a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
1908a8fe54f0SJason M. Bills     }
1909a8fe54f0SJason M. Bills 
191039e77504SEd Tanous     std::string_view msg;
191116428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
1912e1f26343SJason M. Bills     if (ret < 0)
1913e1f26343SJason M. Bills     {
1914e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
1915e1f26343SJason M. Bills         return 1;
1916e1f26343SJason M. Bills     }
1917a8fe54f0SJason M. Bills     message += std::string(msg);
1918e1f26343SJason M. Bills 
1919e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
1920271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
192116428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
1922e1f26343SJason M. Bills     if (ret < 0)
1923e1f26343SJason M. Bills     {
1924e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
1925e1f26343SJason M. Bills     }
1926e1f26343SJason M. Bills 
1927e1f26343SJason M. Bills     // Get the Created time from the timestamp
192816428a1aSJason M. Bills     std::string entryTimeStr;
192916428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
1930e1f26343SJason M. Bills     {
193116428a1aSJason M. Bills         return 1;
1932e1f26343SJason M. Bills     }
1933e1f26343SJason M. Bills 
1934e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
1935c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
1936*647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1937c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
1938c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
1939e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
1940c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
1941a8fe54f0SJason M. Bills         {"Message", std::move(message)},
1942e1f26343SJason M. Bills         {"EntryType", "Oem"},
1943738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
1944738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
1945738c1e61SPatrick Williams                                      : "OK"},
1946086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
1947e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
1948e1f26343SJason M. Bills     return 0;
1949e1f26343SJason M. Bills }
1950e1f26343SJason M. Bills 
19517e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
1952e1f26343SJason M. Bills {
19537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
1954ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
19557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
19567e860f15SJohn Edward Broadbent             [](const crow::Request& req,
19577e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1958193ad2faSJason M. Bills                 static constexpr const long maxEntriesPerPage = 1000;
1959271584abSEd Tanous                 uint64_t skip = 0;
1960271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
19618d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1962193ad2faSJason M. Bills                 {
1963193ad2faSJason M. Bills                     return;
1964193ad2faSJason M. Bills                 }
19658d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1966193ad2faSJason M. Bills                 {
1967193ad2faSJason M. Bills                     return;
1968193ad2faSJason M. Bills                 }
19697e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
19707e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1971e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1972e1f26343SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
19730f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
19740f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
1975e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
1976e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1977e1f26343SJason M. Bills                     "Collection of BMC Journal Entries";
19787e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
19797e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1980e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
1981e1f26343SJason M. Bills 
19827e860f15SJohn Edward Broadbent                 // Go through the journal and use the timestamp to create a
19837e860f15SJohn Edward Broadbent                 // unique ID for each entry
1984e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
1985e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
1986e1f26343SJason M. Bills                 if (ret < 0)
1987e1f26343SJason M. Bills                 {
19887e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
19897e860f15SJohn Edward Broadbent                                      << strerror(-ret);
1990f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
1991e1f26343SJason M. Bills                     return;
1992e1f26343SJason M. Bills                 }
19937e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
19947e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
1995e1f26343SJason M. Bills                 journalTmp = nullptr;
1996b01bf299SEd Tanous                 uint64_t entryCount = 0;
1997e85d6b16SJason M. Bills                 // Reset the unique ID on the first entry
1998e85d6b16SJason M. Bills                 bool firstEntry = true;
1999e1f26343SJason M. Bills                 SD_JOURNAL_FOREACH(journal.get())
2000e1f26343SJason M. Bills                 {
2001193ad2faSJason M. Bills                     entryCount++;
20027e860f15SJohn Edward Broadbent                     // Handle paging using skip (number of entries to skip from
20037e860f15SJohn Edward Broadbent                     // the start) and top (number of entries to display)
2004193ad2faSJason M. Bills                     if (entryCount <= skip || entryCount > skip + top)
2005193ad2faSJason M. Bills                     {
2006193ad2faSJason M. Bills                         continue;
2007193ad2faSJason M. Bills                     }
2008193ad2faSJason M. Bills 
200916428a1aSJason M. Bills                     std::string idStr;
2010e85d6b16SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2011e1f26343SJason M. Bills                     {
2012e1f26343SJason M. Bills                         continue;
2013e1f26343SJason M. Bills                     }
2014e1f26343SJason M. Bills 
2015e85d6b16SJason M. Bills                     if (firstEntry)
2016e85d6b16SJason M. Bills                     {
2017e85d6b16SJason M. Bills                         firstEntry = false;
2018e85d6b16SJason M. Bills                     }
2019e85d6b16SJason M. Bills 
2020e1f26343SJason M. Bills                     logEntryArray.push_back({});
2021c4bf6374SJason M. Bills                     nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2022c4bf6374SJason M. Bills                     if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2023c4bf6374SJason M. Bills                                                    bmcJournalLogEntry) != 0)
2024e1f26343SJason M. Bills                     {
2025f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2026e1f26343SJason M. Bills                         return;
2027e1f26343SJason M. Bills                     }
2028e1f26343SJason M. Bills                 }
2029193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2030193ad2faSJason M. Bills                 if (skip + top < entryCount)
2031193ad2faSJason M. Bills                 {
2032193ad2faSJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
20337e860f15SJohn Edward Broadbent                         "/redfish/v1/Managers/bmc/LogServices/Journal/"
20347e860f15SJohn Edward Broadbent                         "Entries?$skip=" +
2035193ad2faSJason M. Bills                         std::to_string(skip + top);
2036193ad2faSJason M. Bills                 }
20377e860f15SJohn Edward Broadbent             });
2038e1f26343SJason M. Bills }
2039e1f26343SJason M. Bills 
20407e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2041e1f26343SJason M. Bills {
20427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
20437e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2044ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
20457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
20467e860f15SJohn Edward Broadbent             [](const crow::Request&,
20477e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20487e860f15SJohn Edward Broadbent                const std::string& entryID) {
2049e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2050e1f26343SJason M. Bills                 uint64_t ts = 0;
2051271584abSEd Tanous                 uint64_t index = 0;
20528d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2053e1f26343SJason M. Bills                 {
205416428a1aSJason M. Bills                     return;
2055e1f26343SJason M. Bills                 }
2056e1f26343SJason M. Bills 
2057e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2058e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2059e1f26343SJason M. Bills                 if (ret < 0)
2060e1f26343SJason M. Bills                 {
20617e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
20627e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2063f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2064e1f26343SJason M. Bills                     return;
2065e1f26343SJason M. Bills                 }
20667e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
20677e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2068e1f26343SJason M. Bills                 journalTmp = nullptr;
20697e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
20707e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2071af07e3f5SJason M. Bills                 std::string idStr;
2072af07e3f5SJason M. Bills                 bool firstEntry = true;
2073e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
20742056b6d1SManojkiran Eda                 if (ret < 0)
20752056b6d1SManojkiran Eda                 {
20762056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
20772056b6d1SManojkiran Eda                                      << strerror(-ret);
20782056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
20792056b6d1SManojkiran Eda                     return;
20802056b6d1SManojkiran Eda                 }
2081271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2082e1f26343SJason M. Bills                 {
2083e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2084af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2085af07e3f5SJason M. Bills                     {
2086af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2087af07e3f5SJason M. Bills                         return;
2088af07e3f5SJason M. Bills                     }
2089af07e3f5SJason M. Bills                     if (firstEntry)
2090af07e3f5SJason M. Bills                     {
2091af07e3f5SJason M. Bills                         firstEntry = false;
2092af07e3f5SJason M. Bills                     }
2093e1f26343SJason M. Bills                 }
2094c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2095af07e3f5SJason M. Bills                 if (idStr != entryID)
2096c4bf6374SJason M. Bills                 {
2097c4bf6374SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, entryID);
2098c4bf6374SJason M. Bills                     return;
2099c4bf6374SJason M. Bills                 }
2100c4bf6374SJason M. Bills 
2101c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2102e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2103e1f26343SJason M. Bills                 {
2104f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2105e1f26343SJason M. Bills                     return;
2106e1f26343SJason M. Bills                 }
21077e860f15SJohn Edward Broadbent             });
2108c9bb6861Sraviteja-b }
2109c9bb6861Sraviteja-b 
21107e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2111c9bb6861Sraviteja-b {
21127e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2113ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
21147e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21157e860f15SJohn Edward Broadbent             [](const crow::Request&,
21167e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2117c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
21185cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump";
2119c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2120d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
2121c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
21225cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
21235cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
2124c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
21257c8c4058STejas Patil 
21267c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
21277c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
21287c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
21297c8c4058STejas Patil                     redfishDateTimeOffset.first;
21307c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21317c8c4058STejas Patil                     redfishDateTimeOffset.second;
21327c8c4058STejas Patil 
2133c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Entries"] = {
21347e860f15SJohn Edward Broadbent                     {"@odata.id",
21357e860f15SJohn Edward Broadbent                      "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
21365cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
21375cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
21385cb1dd27SAsmitha Karunanithi                      {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
21395cb1dd27SAsmitha Karunanithi                                  "Actions/LogService.ClearLog"}}},
2140d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
2141d337bb72SAsmitha Karunanithi                      {{"target", "/redfish/v1/Managers/bmc/LogServices/Dump/"
2142d337bb72SAsmitha Karunanithi                                  "Actions/LogService.CollectDiagnosticData"}}}};
21437e860f15SJohn Edward Broadbent             });
2144c9bb6861Sraviteja-b }
2145c9bb6861Sraviteja-b 
21467e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
21477e860f15SJohn Edward Broadbent {
21487e860f15SJohn Edward Broadbent 
2149c9bb6861Sraviteja-b     /**
2150c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2151c9bb6861Sraviteja-b      */
21527e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2153ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
21547e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21557e860f15SJohn Edward Broadbent             [](const crow::Request&,
21567e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2157c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2158c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2159c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
21605cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
21615cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2162c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
21635cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2164c9bb6861Sraviteja-b 
21655cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
21667e860f15SJohn Edward Broadbent             });
2167c9bb6861Sraviteja-b }
2168c9bb6861Sraviteja-b 
21697e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2170c9bb6861Sraviteja-b {
21717e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
21727e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2173ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
21747e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21757e860f15SJohn Edward Broadbent             [](const crow::Request&,
21767e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21777e860f15SJohn Edward Broadbent                const std::string& param) {
21787e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "BMC");
21797e860f15SJohn Edward Broadbent             });
21807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
21817e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2182ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
21837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
21847e860f15SJohn Edward Broadbent             [](const crow::Request&,
21857e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
21867e860f15SJohn Edward Broadbent                const std::string& param) {
21877e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
21887e860f15SJohn Edward Broadbent             });
2189c9bb6861Sraviteja-b }
2190c9bb6861Sraviteja-b 
21917e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2192c9bb6861Sraviteja-b {
21938d1b46d7Szhanghch05 
21947e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
2195d337bb72SAsmitha Karunanithi                       "Actions/"
2196d337bb72SAsmitha Karunanithi                       "LogService.CollectDiagnosticData/")
2197ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
21987e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
21997e860f15SJohn Edward Broadbent             [](const crow::Request& req,
22007e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22018d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
22027e860f15SJohn Edward Broadbent             });
2203a43be80fSAsmitha Karunanithi }
2204a43be80fSAsmitha Karunanithi 
22057e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
220680319af1SAsmitha Karunanithi {
22077e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/"
220880319af1SAsmitha Karunanithi                       "Actions/"
220980319af1SAsmitha Karunanithi                       "LogService.ClearLog/")
2210ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
22117e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
22127e860f15SJohn Edward Broadbent             [](const crow::Request&,
22137e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22148d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
22157e860f15SJohn Edward Broadbent             });
22165cb1dd27SAsmitha Karunanithi }
22175cb1dd27SAsmitha Karunanithi 
22187e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
22195cb1dd27SAsmitha Karunanithi {
22207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2221ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
22227e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
22237e860f15SJohn Edward Broadbent             [](const crow::Request&,
22247e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
22255cb1dd27SAsmitha Karunanithi 
22267e860f15SJohn Edward Broadbent             {
22275cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
22285cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump";
22295cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
2230d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
22315cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
22327e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
22337e860f15SJohn Edward Broadbent                     "System Dump LogService";
22345cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
22355cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
22367c8c4058STejas Patil 
22377c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
22387c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
22397c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
22407c8c4058STejas Patil                     redfishDateTimeOffset.first;
22417c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
22427c8c4058STejas Patil                     redfishDateTimeOffset.second;
22437c8c4058STejas Patil 
22445cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Entries"] = {
22455cb1dd27SAsmitha Karunanithi                     {"@odata.id",
22465cb1dd27SAsmitha Karunanithi                      "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
22475cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
22485cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
22497e860f15SJohn Edward Broadbent                      {{"target",
22507e860f15SJohn Edward Broadbent                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
22515cb1dd27SAsmitha Karunanithi                        "LogService.ClearLog"}}},
2252d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
22537e860f15SJohn Edward Broadbent                      {{"target",
22547e860f15SJohn Edward Broadbent                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/"
2255d337bb72SAsmitha Karunanithi                        "LogService.CollectDiagnosticData"}}}};
22567e860f15SJohn Edward Broadbent             });
22575cb1dd27SAsmitha Karunanithi }
22585cb1dd27SAsmitha Karunanithi 
22597e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
22607e860f15SJohn Edward Broadbent {
22617e860f15SJohn Edward Broadbent 
22625cb1dd27SAsmitha Karunanithi     /**
22635cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
22645cb1dd27SAsmitha Karunanithi      */
2265b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2266ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
22677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
22687e860f15SJohn Edward Broadbent             [](const crow::Request&,
2269864d6a17SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
22705cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
22715cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
22725cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
22735cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
22745cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
22755cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
22765cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
22775cb1dd27SAsmitha Karunanithi 
22785cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
22797e860f15SJohn Edward Broadbent             });
22805cb1dd27SAsmitha Karunanithi }
22815cb1dd27SAsmitha Karunanithi 
22827e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
22835cb1dd27SAsmitha Karunanithi {
22847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2285864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2286ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2287ed398213SEd Tanous 
22887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
22897e860f15SJohn Edward Broadbent             [](const crow::Request&,
22907e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22917e860f15SJohn Edward Broadbent                const std::string& param) {
22927e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "System");
22937e860f15SJohn Edward Broadbent             });
22948d1b46d7Szhanghch05 
22957e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2296864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2297ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
22987e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
22997e860f15SJohn Edward Broadbent             [](const crow::Request&,
23007e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23017e860f15SJohn Edward Broadbent                const std::string& param) {
23027e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
23037e860f15SJohn Edward Broadbent             });
23045cb1dd27SAsmitha Karunanithi }
2305c9bb6861Sraviteja-b 
23067e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2307c9bb6861Sraviteja-b {
23087e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2309d337bb72SAsmitha Karunanithi                       "Actions/"
2310d337bb72SAsmitha Karunanithi                       "LogService.CollectDiagnosticData/")
2311ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
23127e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
23137e860f15SJohn Edward Broadbent             [](const crow::Request& req,
23147e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23157e860f15SJohn Edward Broadbent 
23167e860f15SJohn Edward Broadbent             { createDump(asyncResp, req, "System"); });
2317a43be80fSAsmitha Karunanithi }
2318a43be80fSAsmitha Karunanithi 
23197e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2320a43be80fSAsmitha Karunanithi {
23217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/"
2322013487e5Sraviteja-b                       "Actions/"
2323013487e5Sraviteja-b                       "LogService.ClearLog/")
2324ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
23257e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
23267e860f15SJohn Edward Broadbent             [](const crow::Request&,
23277e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23287e860f15SJohn Edward Broadbent 
23297e860f15SJohn Edward Broadbent             { clearDump(asyncResp, "System"); });
2330013487e5Sraviteja-b }
2331013487e5Sraviteja-b 
23327e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
23331da66f75SEd Tanous {
23343946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
23353946028dSAppaRao Puli     // method for security reasons.
23361da66f75SEd Tanous     /**
23371da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
23381da66f75SEd Tanous      */
23397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2340ed398213SEd Tanous         // This is incorrect, should be:
2341ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2342432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
23437e860f15SJohn Edward Broadbent         .methods(
23447e860f15SJohn Edward Broadbent             boost::beast::http::verb::
23457e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
23467e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23477e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
23487e860f15SJohn Edward Broadbent             // SubRoute
23490f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2350424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2351e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
23528e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
23534f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
23544f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
23554f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2356e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2357e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
23587c8c4058STejas Patil 
23597c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
23607c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
23617c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23627c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23637c8c4058STejas Patil                 redfishDateTimeOffset.second;
23647c8c4058STejas Patil 
2365cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2366cd50aa42SJason M. Bills                 {"@odata.id",
2367424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2368e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
23695b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
23705b61b5e8SJason M. Bills                  {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
23715b61b5e8SJason M. Bills                              "Actions/LogService.ClearLog"}}},
23728e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
2373424c4176SJason M. Bills                  {{"target", "/redfish/v1/Systems/system/LogServices/Crashdump/"
23748e6c099aSJason M. Bills                              "Actions/LogService.CollectDiagnosticData"}}}};
23757e860f15SJohn Edward Broadbent         });
23761da66f75SEd Tanous }
23771da66f75SEd Tanous 
23787e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
23795b61b5e8SJason M. Bills {
23807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23817e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/"
23825b61b5e8SJason M. Bills                  "LogService.ClearLog/")
2383ed398213SEd Tanous         // This is incorrect, should be:
2384ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2385432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
23867e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
23877e860f15SJohn Edward Broadbent             [](const crow::Request&,
23887e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
23895b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
23905b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2391cb13a392SEd Tanous                                 const std::string&) {
23925b61b5e8SJason M. Bills                         if (ec)
23935b61b5e8SJason M. Bills                         {
23945b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
23955b61b5e8SJason M. Bills                             return;
23965b61b5e8SJason M. Bills                         }
23975b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
23985b61b5e8SJason M. Bills                     },
23997e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
24007e860f15SJohn Edward Broadbent                     "DeleteAll");
24017e860f15SJohn Edward Broadbent             });
24025b61b5e8SJason M. Bills }
24035b61b5e8SJason M. Bills 
24048d1b46d7Szhanghch05 static void
24058d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24068d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2407e855dd28SJason M. Bills {
2408043a0536SJohnathan Mantey     auto getStoredLogCallback =
2409043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2410e855dd28SJason M. Bills             const boost::system::error_code ec,
2411043a0536SJohnathan Mantey             const std::vector<std::pair<std::string, VariantType>>& params) {
2412e855dd28SJason M. Bills             if (ec)
2413e855dd28SJason M. Bills             {
2414e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
24151ddcf01aSJason M. Bills                 if (ec.value() ==
24161ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
24171ddcf01aSJason M. Bills                 {
2418043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2419043a0536SJohnathan Mantey                                                logID);
24201ddcf01aSJason M. Bills                 }
24211ddcf01aSJason M. Bills                 else
24221ddcf01aSJason M. Bills                 {
2423e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
24241ddcf01aSJason M. Bills                 }
2425e855dd28SJason M. Bills                 return;
2426e855dd28SJason M. Bills             }
2427043a0536SJohnathan Mantey 
2428043a0536SJohnathan Mantey             std::string timestamp{};
2429043a0536SJohnathan Mantey             std::string filename{};
2430043a0536SJohnathan Mantey             std::string logfile{};
24312c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2432043a0536SJohnathan Mantey 
2433043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2434e855dd28SJason M. Bills             {
2435043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2436e855dd28SJason M. Bills                 return;
2437e855dd28SJason M. Bills             }
2438e855dd28SJason M. Bills 
2439043a0536SJohnathan Mantey             std::string crashdumpURI =
2440e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2441043a0536SJohnathan Mantey                 logID + "/" + filename;
2442d0dbeefdSEd Tanous             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2443043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2444043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2445e855dd28SJason M. Bills                                               logID},
2446e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2447e855dd28SJason M. Bills                             {"Id", logID},
2448e855dd28SJason M. Bills                             {"EntryType", "Oem"},
24498e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
24508e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
24518e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2452043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2453e855dd28SJason M. Bills         };
2454e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
24555b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
24565b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2457043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2458e855dd28SJason M. Bills }
2459e855dd28SJason M. Bills 
24607e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
24611da66f75SEd Tanous {
24623946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
24633946028dSAppaRao Puli     // method for security reasons.
24641da66f75SEd Tanous     /**
24651da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
24661da66f75SEd Tanous      */
24677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24687e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2469ed398213SEd Tanous         // This is incorrect, should be.
2470ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2471432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
24727e860f15SJohn Edward Broadbent         .methods(
24737e860f15SJohn Edward Broadbent             boost::beast::http::verb::
24747e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
24757e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24767e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
24777e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2478e1f26343SJason M. Bills             auto getLogEntriesCallback = [asyncResp](
2479e1f26343SJason M. Bills                                              const boost::system::error_code ec,
24807e860f15SJohn Edward Broadbent                                              const std::vector<std::string>&
24817e860f15SJohn Edward Broadbent                                                  resp) {
24821da66f75SEd Tanous                 if (ec)
24831da66f75SEd Tanous                 {
24841da66f75SEd Tanous                     if (ec.value() !=
24851da66f75SEd Tanous                         boost::system::errc::no_such_file_or_directory)
24861da66f75SEd Tanous                     {
24871da66f75SEd Tanous                         BMCWEB_LOG_DEBUG << "failed to get entries ec: "
24881da66f75SEd Tanous                                          << ec.message();
2489f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
24901da66f75SEd Tanous                         return;
24911da66f75SEd Tanous                     }
24921da66f75SEd Tanous                 }
2493e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
24941da66f75SEd Tanous                     "#LogEntryCollection.LogEntryCollection";
24950f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
2496424c4176SJason M. Bills                     "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2497424c4176SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2498e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
2499424c4176SJason M. Bills                     "Collection of Crashdump Entries";
25007e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
25017e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2502e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
2503e855dd28SJason M. Bills                 std::vector<std::string> logIDs;
2504e855dd28SJason M. Bills                 // Get the list of log entries and build up an empty array big
2505e855dd28SJason M. Bills                 // enough to hold them
25061da66f75SEd Tanous                 for (const std::string& objpath : resp)
25071da66f75SEd Tanous                 {
2508e855dd28SJason M. Bills                     // Get the log ID
2509f23b7296SEd Tanous                     std::size_t lastPos = objpath.rfind('/');
2510e855dd28SJason M. Bills                     if (lastPos == std::string::npos)
25111da66f75SEd Tanous                     {
2512e855dd28SJason M. Bills                         continue;
25131da66f75SEd Tanous                     }
2514e855dd28SJason M. Bills                     logIDs.emplace_back(objpath.substr(lastPos + 1));
2515e855dd28SJason M. Bills 
2516e855dd28SJason M. Bills                     // Add a space for the log entry to the array
2517e855dd28SJason M. Bills                     logEntryArray.push_back({});
2518e855dd28SJason M. Bills                 }
2519e855dd28SJason M. Bills                 // Now go through and set up async calls to fill in the entries
2520e855dd28SJason M. Bills                 size_t index = 0;
2521e855dd28SJason M. Bills                 for (const std::string& logID : logIDs)
2522e855dd28SJason M. Bills                 {
2523e855dd28SJason M. Bills                     // Add the log entry to the array
2524e855dd28SJason M. Bills                     logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
25251da66f75SEd Tanous                 }
2526e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2527e1f26343SJason M. Bills                     logEntryArray.size();
25281da66f75SEd Tanous             };
25291da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
25301da66f75SEd Tanous                 std::move(getLogEntriesCallback),
25311da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
25321da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
25331da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
25345b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
25357e860f15SJohn Edward Broadbent         });
25361da66f75SEd Tanous }
25371da66f75SEd Tanous 
25387e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
25391da66f75SEd Tanous {
25403946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25413946028dSAppaRao Puli     // method for security reasons.
25421da66f75SEd Tanous 
25437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
25447e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2545ed398213SEd Tanous         // this is incorrect, should be
2546ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2547432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
25487e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25497e860f15SJohn Edward Broadbent             [](const crow::Request&,
25507e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25517e860f15SJohn Edward Broadbent                const std::string& param) {
25527e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2553e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
25547e860f15SJohn Edward Broadbent             });
2555e855dd28SJason M. Bills }
2556e855dd28SJason M. Bills 
25577e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2558e855dd28SJason M. Bills {
25593946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25603946028dSAppaRao Puli     // method for security reasons.
25617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
25627e860f15SJohn Edward Broadbent         app,
25637e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2564ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
25657e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25667e860f15SJohn Edward Broadbent             [](const crow::Request&,
25677e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25687e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
2569043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2570043a0536SJohnathan Mantey                     [asyncResp, logID, fileName](
2571abf2add6SEd Tanous                         const boost::system::error_code ec,
25727e860f15SJohn Edward Broadbent                         const std::vector<std::pair<std::string, VariantType>>&
25737e860f15SJohn Edward Broadbent                             resp) {
25741da66f75SEd Tanous                         if (ec)
25751da66f75SEd Tanous                         {
2576043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2577043a0536SJohnathan Mantey                                              << ec.message();
2578f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
25791da66f75SEd Tanous                             return;
25801da66f75SEd Tanous                         }
2581e855dd28SJason M. Bills 
2582043a0536SJohnathan Mantey                         std::string dbusFilename{};
2583043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2584043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2585043a0536SJohnathan Mantey 
25867e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
25877e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2588043a0536SJohnathan Mantey 
2589043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2590043a0536SJohnathan Mantey                             dbusFilepath.empty())
25911da66f75SEd Tanous                         {
25927e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
25937e860f15SJohn Edward Broadbent                                                            fileName);
25941da66f75SEd Tanous                             return;
25951da66f75SEd Tanous                         }
2596e855dd28SJason M. Bills 
2597043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2598043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2599043a0536SJohnathan Mantey                         {
26007e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
26017e860f15SJohn Edward Broadbent                                                            fileName);
2602043a0536SJohnathan Mantey                             return;
2603043a0536SJohnathan Mantey                         }
2604043a0536SJohnathan Mantey 
2605043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2606043a0536SJohnathan Mantey                         {
26077e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
26087e860f15SJohn Edward Broadbent                                                            fileName);
2609043a0536SJohnathan Mantey                             return;
2610043a0536SJohnathan Mantey                         }
2611043a0536SJohnathan Mantey                         std::ifstream ifs(dbusFilepath, std::ios::in |
2612043a0536SJohnathan Mantey                                                             std::ios::binary |
2613043a0536SJohnathan Mantey                                                             std::ios::ate);
2614043a0536SJohnathan Mantey                         std::ifstream::pos_type fileSize = ifs.tellg();
2615043a0536SJohnathan Mantey                         if (fileSize < 0)
2616043a0536SJohnathan Mantey                         {
2617043a0536SJohnathan Mantey                             messages::generalError(asyncResp->res);
2618043a0536SJohnathan Mantey                             return;
2619043a0536SJohnathan Mantey                         }
2620043a0536SJohnathan Mantey                         ifs.seekg(0, std::ios::beg);
2621043a0536SJohnathan Mantey 
2622043a0536SJohnathan Mantey                         auto crashData = std::make_unique<char[]>(
2623043a0536SJohnathan Mantey                             static_cast<unsigned int>(fileSize));
2624043a0536SJohnathan Mantey 
2625043a0536SJohnathan Mantey                         ifs.read(crashData.get(), static_cast<int>(fileSize));
2626043a0536SJohnathan Mantey 
26277e860f15SJohn Edward Broadbent                         // The cast to std::string is intentional in order to
26287e860f15SJohn Edward Broadbent                         // use the assign() that applies move mechanics
2629043a0536SJohnathan Mantey                         asyncResp->res.body().assign(
2630043a0536SJohnathan Mantey                             static_cast<std::string>(crashData.get()));
2631043a0536SJohnathan Mantey 
26327e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
26337e860f15SJohn Edward Broadbent                         // from a browser
26347e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
26357e860f15SJohn Edward Broadbent                                                  "attachment");
26361da66f75SEd Tanous                     };
26371da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
26385b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
26395b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
26407e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
26417e860f15SJohn Edward Broadbent                     crashdumpInterface);
26427e860f15SJohn Edward Broadbent             });
26431da66f75SEd Tanous }
26441da66f75SEd Tanous 
26457e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
26461da66f75SEd Tanous {
26473946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26483946028dSAppaRao Puli     // method for security reasons.
26497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/"
26507e860f15SJohn Edward Broadbent                       "Actions/LogService.CollectDiagnosticData/")
2651ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2652ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2653432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26547e860f15SJohn Edward Broadbent         .methods(
26557e860f15SJohn Edward Broadbent             boost::beast::http::verb::
26567e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
26577e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26588e6c099aSJason M. Bills             std::string diagnosticDataType;
26598e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
26608e6c099aSJason M. Bills             if (!redfish::json_util::readJson(
26617e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
26627e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
26637e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
26648e6c099aSJason M. Bills             {
26658e6c099aSJason M. Bills                 return;
26668e6c099aSJason M. Bills             }
26678e6c099aSJason M. Bills 
26688e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
26698e6c099aSJason M. Bills             {
26708e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
26718e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
26728e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
26738e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
26748e6c099aSJason M. Bills                     "CollectDiagnosticData");
26758e6c099aSJason M. Bills                 return;
26768e6c099aSJason M. Bills             }
26778e6c099aSJason M. Bills 
26788e6c099aSJason M. Bills             auto collectCrashdumpCallback = [asyncResp, req](
26797e860f15SJohn Edward Broadbent                                                 const boost::system::error_code
26807e860f15SJohn Edward Broadbent                                                     ec,
2681cb13a392SEd Tanous                                                 const std::string&) {
26821da66f75SEd Tanous                 if (ec)
26831da66f75SEd Tanous                 {
26847e860f15SJohn Edward Broadbent                     if (ec.value() ==
26857e860f15SJohn Edward Broadbent                         boost::system::errc::operation_not_supported)
26861da66f75SEd Tanous                     {
2687f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
26881da66f75SEd Tanous                     }
26894363d3b2SJason M. Bills                     else if (ec.value() ==
26904363d3b2SJason M. Bills                              boost::system::errc::device_or_resource_busy)
26914363d3b2SJason M. Bills                     {
26924363d3b2SJason M. Bills                         messages::serviceTemporarilyUnavailable(asyncResp->res,
26934363d3b2SJason M. Bills                                                                 "60");
26944363d3b2SJason M. Bills                     }
26951da66f75SEd Tanous                     else
26961da66f75SEd Tanous                     {
2697f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
26981da66f75SEd Tanous                     }
26991da66f75SEd Tanous                     return;
27001da66f75SEd Tanous                 }
27017e860f15SJohn Edward Broadbent                 std::shared_ptr<task::TaskData> task =
27027e860f15SJohn Edward Broadbent                     task::TaskData::createTask(
27037e860f15SJohn Edward Broadbent                         [](boost::system::error_code err,
27047e860f15SJohn Edward Broadbent                            sdbusplus::message::message&,
270566afe4faSJames Feist                            const std::shared_ptr<task::TaskData>& taskData) {
270666afe4faSJames Feist                             if (!err)
270766afe4faSJames Feist                             {
2708e5d5006bSJames Feist                                 taskData->messages.emplace_back(
2709e5d5006bSJames Feist                                     messages::taskCompletedOK(
2710e5d5006bSJames Feist                                         std::to_string(taskData->index)));
2711831d6b09SJames Feist                                 taskData->state = "Completed";
271266afe4faSJames Feist                             }
271332898ceaSJames Feist                             return task::completed;
271466afe4faSJames Feist                         },
27157e860f15SJohn Edward Broadbent                         "type='signal',interface='org.freedesktop.DBus."
27167e860f15SJohn Edward Broadbent                         "Properties',"
271746229577SJames Feist                         "member='PropertiesChanged',arg0namespace='com.intel."
271846229577SJames Feist                         "crashdump'");
271946229577SJames Feist                 task->startTimer(std::chrono::minutes(5));
272046229577SJames Feist                 task->populateResp(asyncResp->res);
2721fe306728SJames Feist                 task->payload.emplace(req);
27221da66f75SEd Tanous             };
27238e6c099aSJason M. Bills 
27248e6c099aSJason M. Bills             if (oemDiagnosticDataType == "OnDemand")
27258e6c099aSJason M. Bills             {
27261da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
27278e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
27288e6c099aSJason M. Bills                     crashdumpPath, crashdumpOnDemandInterface,
27298e6c099aSJason M. Bills                     "GenerateOnDemandLog");
27301da66f75SEd Tanous             }
27318e6c099aSJason M. Bills             else if (oemDiagnosticDataType == "Telemetry")
27326eda7685SKenny L. Ku             {
27338e6c099aSJason M. Bills                 crow::connections::systemBus->async_method_call(
27348e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
27358e6c099aSJason M. Bills                     crashdumpPath, crashdumpTelemetryInterface,
27368e6c099aSJason M. Bills                     "GenerateTelemetryLog");
27376eda7685SKenny L. Ku             }
27386eda7685SKenny L. Ku             else
27396eda7685SKenny L. Ku             {
27408e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
27418e6c099aSJason M. Bills                                  << oemDiagnosticDataType;
27428e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
27437e860f15SJohn Edward Broadbent                     asyncResp->res, oemDiagnosticDataType,
27447e860f15SJohn Edward Broadbent                     "OEMDiagnosticDataType", "CollectDiagnosticData");
27456eda7685SKenny L. Ku                 return;
27466eda7685SKenny L. Ku             }
27477e860f15SJohn Edward Broadbent         });
27486eda7685SKenny L. Ku }
27496eda7685SKenny L. Ku 
2750cb92c03bSAndrew Geissler /**
2751cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2752cb92c03bSAndrew Geissler  */
27537e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
2754cb92c03bSAndrew Geissler {
2755cb92c03bSAndrew Geissler     /**
2756cb92c03bSAndrew Geissler      * Function handles POST method request.
2757cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2758cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2759cb92c03bSAndrew Geissler      */
27607e860f15SJohn Edward Broadbent 
27617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
27627e860f15SJohn Edward Broadbent                       "LogService.ClearLog/")
2763ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
27647e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
27657e860f15SJohn Edward Broadbent             [](const crow::Request&,
27667e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2767cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
2768cb92c03bSAndrew Geissler 
2769cb92c03bSAndrew Geissler                 // Process response from Logging service.
27707e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
27717e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
27727e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
27737e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
2774cb92c03bSAndrew Geissler                     if (ec)
2775cb92c03bSAndrew Geissler                     {
2776cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
27777e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
27787e860f15SJohn Edward Broadbent                                          << ec;
2779cb92c03bSAndrew Geissler                         asyncResp->res.result(
2780cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
2781cb92c03bSAndrew Geissler                         return;
2782cb92c03bSAndrew Geissler                     }
2783cb92c03bSAndrew Geissler 
27847e860f15SJohn Edward Broadbent                     asyncResp->res.result(
27857e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
2786cb92c03bSAndrew Geissler                 };
2787cb92c03bSAndrew Geissler 
2788cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
2789cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
27902c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
2791cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
2792cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
27937e860f15SJohn Edward Broadbent             });
2794cb92c03bSAndrew Geissler }
2795a3316fc6SZhikuiRen 
2796a3316fc6SZhikuiRen /****************************************************
2797a3316fc6SZhikuiRen  * Redfish PostCode interfaces
2798a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
2799a3316fc6SZhikuiRen  ******************************************************/
28007e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
2801a3316fc6SZhikuiRen {
28027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
2803ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
28047e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
28057e860f15SJohn Edward Broadbent             [](const crow::Request&,
28067e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2807a3316fc6SZhikuiRen                 asyncResp->res.jsonValue = {
28087e860f15SJohn Edward Broadbent                     {"@odata.id",
28097e860f15SJohn Edward Broadbent                      "/redfish/v1/Systems/system/LogServices/PostCodes"},
2810a3316fc6SZhikuiRen                     {"@odata.type", "#LogService.v1_1_0.LogService"},
2811a3316fc6SZhikuiRen                     {"Name", "POST Code Log Service"},
2812a3316fc6SZhikuiRen                     {"Description", "POST Code Log Service"},
2813a3316fc6SZhikuiRen                     {"Id", "BIOS POST Code Log"},
2814a3316fc6SZhikuiRen                     {"OverWritePolicy", "WrapsWhenFull"},
2815a3316fc6SZhikuiRen                     {"Entries",
28167e860f15SJohn Edward Broadbent                      {{"@odata.id", "/redfish/v1/Systems/system/LogServices/"
28177e860f15SJohn Edward Broadbent                                     "PostCodes/Entries"}}}};
28187c8c4058STejas Patil 
28197c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
28207c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
28217c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
28227c8c4058STejas Patil                     redfishDateTimeOffset.first;
28237c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
28247c8c4058STejas Patil                     redfishDateTimeOffset.second;
28257c8c4058STejas Patil 
2826a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
28277e860f15SJohn Edward Broadbent                     {"target",
28287e860f15SJohn Edward Broadbent                      "/redfish/v1/Systems/system/LogServices/PostCodes/"
2829a3316fc6SZhikuiRen                      "Actions/LogService.ClearLog"}};
28307e860f15SJohn Edward Broadbent             });
2831a3316fc6SZhikuiRen }
2832a3316fc6SZhikuiRen 
28337e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
2834a3316fc6SZhikuiRen {
28357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28367e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/"
2837a3316fc6SZhikuiRen                  "LogService.ClearLog/")
2838ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
2839ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2840432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
28427e860f15SJohn Edward Broadbent             [](const crow::Request&,
28437e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2844a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
2845a3316fc6SZhikuiRen 
2846a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
2847a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
2848a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
2849a3316fc6SZhikuiRen                         if (ec)
2850a3316fc6SZhikuiRen                         {
2851a3316fc6SZhikuiRen                             // TODO Handle for specific error code
2852a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
28537e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
28547e860f15SJohn Edward Broadbent                                 << ec;
28557e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
28567e860f15SJohn Edward Broadbent                                                       internal_server_error);
2857a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
2858a3316fc6SZhikuiRen                             return;
2859a3316fc6SZhikuiRen                         }
2860a3316fc6SZhikuiRen                     },
286115124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
286215124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
2863a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
28647e860f15SJohn Edward Broadbent             });
2865a3316fc6SZhikuiRen }
2866a3316fc6SZhikuiRen 
2867a3316fc6SZhikuiRen static void fillPostCodeEntry(
28688d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
28696c9a279eSManojkiran Eda     const boost::container::flat_map<
28706c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
2871a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
2872a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
2873a3316fc6SZhikuiRen {
2874a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
2875a3316fc6SZhikuiRen     const message_registries::Message* message =
28764a0bf539SManojkiran Eda         message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
2877a3316fc6SZhikuiRen 
2878a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
2879a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
2880a3316fc6SZhikuiRen 
2881a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
28826c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
28836c9a279eSManojkiran Eda              code : postcode)
2884a3316fc6SZhikuiRen     {
2885a3316fc6SZhikuiRen         currentCodeIndex++;
2886a3316fc6SZhikuiRen         std::string postcodeEntryID =
2887a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
2888a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
2889a3316fc6SZhikuiRen 
2890a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
2891a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
2892a3316fc6SZhikuiRen 
2893a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
2894a3316fc6SZhikuiRen         { // already incremented
2895a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
2896a3316fc6SZhikuiRen         }
2897a3316fc6SZhikuiRen         else
2898a3316fc6SZhikuiRen         {
2899a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
2900a3316fc6SZhikuiRen         }
2901a3316fc6SZhikuiRen 
2902a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
2903a3316fc6SZhikuiRen         // not fall between top and skip
2904a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
2905a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
2906a3316fc6SZhikuiRen         {
2907a3316fc6SZhikuiRen             continue;
2908a3316fc6SZhikuiRen         }
2909a3316fc6SZhikuiRen 
29104e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
2911a3316fc6SZhikuiRen         // currentIndex
2912a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
2913a3316fc6SZhikuiRen         {
2914a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
2915a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
2916a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
2917a3316fc6SZhikuiRen             continue;
2918a3316fc6SZhikuiRen         }
2919a3316fc6SZhikuiRen 
2920a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
2921a3316fc6SZhikuiRen         // index
2922a3316fc6SZhikuiRen 
2923a3316fc6SZhikuiRen         // Get the Created time from the timestamp
2924a3316fc6SZhikuiRen         std::string entryTimeStr;
29259c620e21SAsmitha Karunanithi         entryTimeStr = crow::utility::getDateTime(
29269c620e21SAsmitha Karunanithi             static_cast<std::time_t>(usecSinceEpoch / 1000 / 1000));
2927a3316fc6SZhikuiRen 
2928a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
2929a3316fc6SZhikuiRen         std::ostringstream hexCode;
2930a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
29316c9a279eSManojkiran Eda                 << std::get<0>(code.second);
2932a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
2933a3316fc6SZhikuiRen         // Set Fixed -Point Notation
2934a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
2935a3316fc6SZhikuiRen         // Set precision to 4 digits
2936a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
2937a3316fc6SZhikuiRen         // Add double to stream
2938a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
2939a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
2940a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
2941a3316fc6SZhikuiRen 
2942a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
2943a3316fc6SZhikuiRen         std::string msg;
2944a3316fc6SZhikuiRen         if (message != nullptr)
2945a3316fc6SZhikuiRen         {
2946a3316fc6SZhikuiRen             msg = message->message;
2947a3316fc6SZhikuiRen 
2948a3316fc6SZhikuiRen             // fill in this post code value
2949a3316fc6SZhikuiRen             int i = 0;
2950a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
2951a3316fc6SZhikuiRen             {
2952a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
2953a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
2954a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
2955a3316fc6SZhikuiRen                 {
2956a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
2957a3316fc6SZhikuiRen                 }
2958a3316fc6SZhikuiRen             }
2959a3316fc6SZhikuiRen         }
2960a3316fc6SZhikuiRen 
2961d4342a92STim Lee         // Get Severity template from message registry
2962d4342a92STim Lee         std::string severity;
2963d4342a92STim Lee         if (message != nullptr)
2964d4342a92STim Lee         {
2965d4342a92STim Lee             severity = message->severity;
2966d4342a92STim Lee         }
2967d4342a92STim Lee 
2968a3316fc6SZhikuiRen         // add to AsyncResp
2969a3316fc6SZhikuiRen         logEntryArray.push_back({});
2970a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
2971*647b3cdcSGeorge Liu         bmcLogEntry = {{"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2972a3316fc6SZhikuiRen                        {"@odata.id", "/redfish/v1/Systems/system/LogServices/"
2973a3316fc6SZhikuiRen                                      "PostCodes/Entries/" +
2974a3316fc6SZhikuiRen                                          postcodeEntryID},
2975a3316fc6SZhikuiRen                        {"Name", "POST Code Log Entry"},
2976a3316fc6SZhikuiRen                        {"Id", postcodeEntryID},
2977a3316fc6SZhikuiRen                        {"Message", std::move(msg)},
29784a0bf539SManojkiran Eda                        {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
2979a3316fc6SZhikuiRen                        {"MessageArgs", std::move(messageArgs)},
2980a3316fc6SZhikuiRen                        {"EntryType", "Event"},
2981a3316fc6SZhikuiRen                        {"Severity", std::move(severity)},
29829c620e21SAsmitha Karunanithi                        {"Created", entryTimeStr}};
2983*647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
2984*647b3cdcSGeorge Liu         {
2985*647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
2986*647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
2987*647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
2988*647b3cdcSGeorge Liu         }
2989a3316fc6SZhikuiRen     }
2990a3316fc6SZhikuiRen }
2991a3316fc6SZhikuiRen 
29928d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
2993a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
2994a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
2995a3316fc6SZhikuiRen {
2996a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
29976c9a279eSManojkiran Eda         [aResp, bootIndex,
29986c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
29996c9a279eSManojkiran Eda                     const boost::container::flat_map<
30006c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
30016c9a279eSManojkiran Eda                         postcode) {
3002a3316fc6SZhikuiRen             if (ec)
3003a3316fc6SZhikuiRen             {
3004a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3005a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3006a3316fc6SZhikuiRen                 return;
3007a3316fc6SZhikuiRen             }
3008a3316fc6SZhikuiRen 
3009a3316fc6SZhikuiRen             // skip the empty postcode boots
3010a3316fc6SZhikuiRen             if (postcode.empty())
3011a3316fc6SZhikuiRen             {
3012a3316fc6SZhikuiRen                 return;
3013a3316fc6SZhikuiRen             }
3014a3316fc6SZhikuiRen 
3015a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3016a3316fc6SZhikuiRen 
3017a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3018a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3019a3316fc6SZhikuiRen         },
302015124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
302115124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3022a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3023a3316fc6SZhikuiRen         bootIndex);
3024a3316fc6SZhikuiRen }
3025a3316fc6SZhikuiRen 
30268d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3027a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3028a3316fc6SZhikuiRen                                const uint16_t bootCount,
3029a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3030a3316fc6SZhikuiRen                                const uint64_t top)
3031a3316fc6SZhikuiRen {
3032a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3033a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3034a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
30356c9a279eSManojkiran Eda               const boost::container::flat_map<
30366c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
30376c9a279eSManojkiran Eda                   postcode) {
3038a3316fc6SZhikuiRen             if (ec)
3039a3316fc6SZhikuiRen             {
3040a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3041a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3042a3316fc6SZhikuiRen                 return;
3043a3316fc6SZhikuiRen             }
3044a3316fc6SZhikuiRen 
3045a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3046a3316fc6SZhikuiRen             if (!postcode.empty())
3047a3316fc6SZhikuiRen             {
3048a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3049a3316fc6SZhikuiRen 
3050a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3051a3316fc6SZhikuiRen                 {
3052a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3053a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3054a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3055a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3056a3316fc6SZhikuiRen 
3057a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3058a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3059a3316fc6SZhikuiRen                 }
3060a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3061a3316fc6SZhikuiRen             }
3062a3316fc6SZhikuiRen 
3063a3316fc6SZhikuiRen             // continue to previous bootIndex
3064a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3065a3316fc6SZhikuiRen             {
3066a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3067a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3068a3316fc6SZhikuiRen             }
3069a3316fc6SZhikuiRen             else
3070a3316fc6SZhikuiRen             {
3071a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
3072a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
3073a3316fc6SZhikuiRen                     "Entries?$skip=" +
3074a3316fc6SZhikuiRen                     std::to_string(skip + top);
3075a3316fc6SZhikuiRen             }
3076a3316fc6SZhikuiRen         },
307715124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
307815124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3079a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3080a3316fc6SZhikuiRen         bootIndex);
3081a3316fc6SZhikuiRen }
3082a3316fc6SZhikuiRen 
30838d1b46d7Szhanghch05 static void
30848d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3085a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3086a3316fc6SZhikuiRen {
3087a3316fc6SZhikuiRen     uint64_t entryCount = 0;
3088a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3089a3316fc6SZhikuiRen         [aResp, entryCount, skip,
3090a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
3091a3316fc6SZhikuiRen               const std::variant<uint16_t>& bootCount) {
3092a3316fc6SZhikuiRen             if (ec)
3093a3316fc6SZhikuiRen             {
3094a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3095a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3096a3316fc6SZhikuiRen                 return;
3097a3316fc6SZhikuiRen             }
3098a3316fc6SZhikuiRen             auto pVal = std::get_if<uint16_t>(&bootCount);
3099a3316fc6SZhikuiRen             if (pVal)
3100a3316fc6SZhikuiRen             {
3101a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, 1, *pVal, entryCount, skip, top);
3102a3316fc6SZhikuiRen             }
3103a3316fc6SZhikuiRen             else
3104a3316fc6SZhikuiRen             {
3105a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Post code boot index failed.";
3106a3316fc6SZhikuiRen             }
3107a3316fc6SZhikuiRen         },
310815124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
310915124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3110a3316fc6SZhikuiRen         "org.freedesktop.DBus.Properties", "Get",
3111a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount");
3112a3316fc6SZhikuiRen }
3113a3316fc6SZhikuiRen 
31147e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3115a3316fc6SZhikuiRen {
31167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
31177e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3118ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
31197e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
31207e860f15SJohn Edward Broadbent             [](const crow::Request& req,
31217e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3122a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3123a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3124a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3125a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3126a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3127a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3128a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3129a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3130a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3131a3316fc6SZhikuiRen 
3132a3316fc6SZhikuiRen                 uint64_t skip = 0;
3133a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
31348d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3135a3316fc6SZhikuiRen                 {
3136a3316fc6SZhikuiRen                     return;
3137a3316fc6SZhikuiRen                 }
31388d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3139a3316fc6SZhikuiRen                 {
3140a3316fc6SZhikuiRen                     return;
3141a3316fc6SZhikuiRen                 }
3142a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
31437e860f15SJohn Edward Broadbent             });
3144a3316fc6SZhikuiRen }
3145a3316fc6SZhikuiRen 
3146*647b3cdcSGeorge Liu /**
3147*647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3148*647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3149*647b3cdcSGeorge Liu  *
3150*647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3151*647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3152*647b3cdcSGeorge Liu  * @param[out] index          Index value
3153*647b3cdcSGeorge Liu  *
3154*647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3155*647b3cdcSGeorge Liu  */
3156*647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3157*647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3158*647b3cdcSGeorge Liu {
3159*647b3cdcSGeorge Liu     std::vector<std::string> split;
3160*647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3161*647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3162*647b3cdcSGeorge Liu     {
3163*647b3cdcSGeorge Liu         return false;
3164*647b3cdcSGeorge Liu     }
3165*647b3cdcSGeorge Liu 
3166*647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3167*647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3168*647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3169*647b3cdcSGeorge Liu 
3170*647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3171*647b3cdcSGeorge Liu     {
3172*647b3cdcSGeorge Liu         return false;
3173*647b3cdcSGeorge Liu     }
3174*647b3cdcSGeorge Liu 
3175*647b3cdcSGeorge Liu     start = split[1].data();
3176*647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3177*647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3178*647b3cdcSGeorge Liu     if (ptrValue != end || ecValue != std::errc())
3179*647b3cdcSGeorge Liu     {
3180*647b3cdcSGeorge Liu         return false;
3181*647b3cdcSGeorge Liu     }
3182*647b3cdcSGeorge Liu 
3183*647b3cdcSGeorge Liu     return true;
3184*647b3cdcSGeorge Liu }
3185*647b3cdcSGeorge Liu 
3186*647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3187*647b3cdcSGeorge Liu {
3188*647b3cdcSGeorge Liu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/"
3189*647b3cdcSGeorge Liu                       "Entries/<str>/attachment/")
3190*647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3191*647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
3192*647b3cdcSGeorge Liu             [](const crow::Request& req,
3193*647b3cdcSGeorge Liu                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3194*647b3cdcSGeorge Liu                const std::string& postCodeID) {
3195*647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
3196*647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
3197*647b3cdcSGeorge Liu                 {
3198*647b3cdcSGeorge Liu                     asyncResp->res.result(
3199*647b3cdcSGeorge Liu                         boost::beast::http::status::bad_request);
3200*647b3cdcSGeorge Liu                     return;
3201*647b3cdcSGeorge Liu                 }
3202*647b3cdcSGeorge Liu 
3203*647b3cdcSGeorge Liu                 uint64_t currentValue = 0;
3204*647b3cdcSGeorge Liu                 uint16_t index = 0;
3205*647b3cdcSGeorge Liu                 if (!parsePostCode(postCodeID, currentValue, index))
3206*647b3cdcSGeorge Liu                 {
3207*647b3cdcSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
3208*647b3cdcSGeorge Liu                                                postCodeID);
3209*647b3cdcSGeorge Liu                     return;
3210*647b3cdcSGeorge Liu                 }
3211*647b3cdcSGeorge Liu 
3212*647b3cdcSGeorge Liu                 crow::connections::systemBus->async_method_call(
3213*647b3cdcSGeorge Liu                     [asyncResp, postCodeID, currentValue](
3214*647b3cdcSGeorge Liu                         const boost::system::error_code ec,
3215*647b3cdcSGeorge Liu                         const std::vector<std::tuple<
3216*647b3cdcSGeorge Liu                             uint64_t, std::vector<uint8_t>>>& postcodes) {
3217*647b3cdcSGeorge Liu                         if (ec.value() == EBADR)
3218*647b3cdcSGeorge Liu                         {
3219*647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3220*647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3221*647b3cdcSGeorge Liu                             return;
3222*647b3cdcSGeorge Liu                         }
3223*647b3cdcSGeorge Liu                         if (ec)
3224*647b3cdcSGeorge Liu                         {
3225*647b3cdcSGeorge Liu                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3226*647b3cdcSGeorge Liu                             messages::internalError(asyncResp->res);
3227*647b3cdcSGeorge Liu                             return;
3228*647b3cdcSGeorge Liu                         }
3229*647b3cdcSGeorge Liu 
3230*647b3cdcSGeorge Liu                         size_t value = static_cast<size_t>(currentValue) - 1;
3231*647b3cdcSGeorge Liu                         if (value == std::string::npos ||
3232*647b3cdcSGeorge Liu                             postcodes.size() < currentValue)
3233*647b3cdcSGeorge Liu                         {
3234*647b3cdcSGeorge Liu                             BMCWEB_LOG_ERROR << "Wrong currentValue value";
3235*647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3236*647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3237*647b3cdcSGeorge Liu                             return;
3238*647b3cdcSGeorge Liu                         }
3239*647b3cdcSGeorge Liu 
3240*647b3cdcSGeorge Liu                         auto& [tID, code] = postcodes[value];
3241*647b3cdcSGeorge Liu                         if (code.empty())
3242*647b3cdcSGeorge Liu                         {
3243*647b3cdcSGeorge Liu                             BMCWEB_LOG_INFO << "No found post code data";
3244*647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3245*647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3246*647b3cdcSGeorge Liu                             return;
3247*647b3cdcSGeorge Liu                         }
3248*647b3cdcSGeorge Liu 
3249*647b3cdcSGeorge Liu                         std::string_view strData(
3250*647b3cdcSGeorge Liu                             reinterpret_cast<const char*>(code.data()),
3251*647b3cdcSGeorge Liu                             code.size());
3252*647b3cdcSGeorge Liu 
3253*647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Type",
3254*647b3cdcSGeorge Liu                                                  "application/octet-stream");
3255*647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Transfer-Encoding",
3256*647b3cdcSGeorge Liu                                                  "Base64");
3257*647b3cdcSGeorge Liu                         asyncResp->res.body() =
3258*647b3cdcSGeorge Liu                             crow::utility::base64encode(strData);
3259*647b3cdcSGeorge Liu                     },
3260*647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode0",
3261*647b3cdcSGeorge Liu                     "/xyz/openbmc_project/State/Boot/PostCode0",
3262*647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3263*647b3cdcSGeorge Liu                     index);
3264*647b3cdcSGeorge Liu             });
3265*647b3cdcSGeorge Liu }
3266*647b3cdcSGeorge Liu 
32677e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3268a3316fc6SZhikuiRen {
32697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
32707e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3271ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
32727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
32737e860f15SJohn Edward Broadbent             [](const crow::Request&,
32747e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32757e860f15SJohn Edward Broadbent                const std::string& targetID) {
3276*647b3cdcSGeorge Liu                 uint16_t bootIndex = 0;
3277*647b3cdcSGeorge Liu                 uint64_t codeIndex = 0;
3278*647b3cdcSGeorge Liu                 if (!parsePostCode(targetID, codeIndex, bootIndex))
3279a3316fc6SZhikuiRen                 {
3280a3316fc6SZhikuiRen                     // Requested ID was not found
3281a3316fc6SZhikuiRen                     messages::resourceMissingAtURI(asyncResp->res, targetID);
3282a3316fc6SZhikuiRen                     return;
3283a3316fc6SZhikuiRen                 }
3284a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3285a3316fc6SZhikuiRen                 {
3286a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
32877e860f15SJohn Edward Broadbent                                      << targetID;
3288a3316fc6SZhikuiRen                 }
3289a3316fc6SZhikuiRen 
32907e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
32917e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3292a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3293a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/"
3294a3316fc6SZhikuiRen                     "Entries";
3295a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3296a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3297a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3298a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3299a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3300a3316fc6SZhikuiRen 
3301a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
33027e860f15SJohn Edward Broadbent             });
3303a3316fc6SZhikuiRen }
3304a3316fc6SZhikuiRen 
33051da66f75SEd Tanous } // namespace redfish
3306