xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 45ca1b868e47978a4d2e8ebb680cb384e804c97e)
11da66f75SEd Tanous /*
21da66f75SEd Tanous // Copyright (c) 2018 Intel Corporation
31da66f75SEd Tanous //
41da66f75SEd Tanous // Licensed under the Apache License, Version 2.0 (the "License");
51da66f75SEd Tanous // you may not use this file except in compliance with the License.
61da66f75SEd Tanous // You may obtain a copy of the License at
71da66f75SEd Tanous //
81da66f75SEd Tanous //      http://www.apache.org/licenses/LICENSE-2.0
91da66f75SEd Tanous //
101da66f75SEd Tanous // Unless required by applicable law or agreed to in writing, software
111da66f75SEd Tanous // distributed under the License is distributed on an "AS IS" BASIS,
121da66f75SEd Tanous // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131da66f75SEd Tanous // See the License for the specific language governing permissions and
141da66f75SEd Tanous // limitations under the License.
151da66f75SEd Tanous */
161da66f75SEd Tanous #pragma once
171da66f75SEd Tanous 
18b7028ebfSSpencer Ku #include "gzfile.hpp"
19647b3cdcSGeorge Liu #include "http_utility.hpp"
20b7028ebfSSpencer Ku #include "human_sort.hpp"
214851d45dSJason M. Bills #include "registries.hpp"
224851d45dSJason M. Bills #include "registries/base_message_registry.hpp"
234851d45dSJason M. Bills #include "registries/openbmc_message_registry.hpp"
2446229577SJames Feist #include "task.hpp"
251da66f75SEd Tanous 
26e1f26343SJason M. Bills #include <systemd/sd-journal.h>
27400fd1fbSAdriana Kobylak #include <unistd.h>
28e1f26343SJason M. Bills 
297e860f15SJohn Edward Broadbent #include <app.hpp>
30400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
314851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
32400fd1fbSAdriana Kobylak #include <boost/beast/http.hpp>
331da66f75SEd Tanous #include <boost/container/flat_map.hpp>
341ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
35168e20c1SEd Tanous #include <dbus_utility.hpp>
36cb92c03bSAndrew Geissler #include <error_messages.hpp>
37*45ca1b86SEd Tanous #include <query.hpp>
38ed398213SEd Tanous #include <registries/privilege_registry.hpp>
391214b7e7SGunnar Mills 
40647b3cdcSGeorge Liu #include <charconv>
414418c7f0SJames Feist #include <filesystem>
4275710de2SXiaochao Ma #include <optional>
4326702d01SEd Tanous #include <span>
44cd225da8SJason M. Bills #include <string_view>
45abf2add6SEd Tanous #include <variant>
461da66f75SEd Tanous 
471da66f75SEd Tanous namespace redfish
481da66f75SEd Tanous {
491da66f75SEd Tanous 
505b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
515b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
525b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
535b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
545b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
555b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
56424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
576eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
586eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
591da66f75SEd Tanous 
60fffb8c1fSEd Tanous namespace registries
614851d45dSJason M. Bills {
6226702d01SEd Tanous static const Message*
6326702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
6426702d01SEd Tanous                            const std::span<const MessageEntry> registry)
654851d45dSJason M. Bills {
6626702d01SEd Tanous     std::span<const MessageEntry>::iterator messageIt = std::find_if(
6726702d01SEd Tanous         registry.begin(), registry.end(),
684851d45dSJason M. Bills         [&messageKey](const MessageEntry& messageEntry) {
69e662eae8SEd Tanous             return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
704851d45dSJason M. Bills         });
7126702d01SEd Tanous     if (messageIt != registry.end())
724851d45dSJason M. Bills     {
734851d45dSJason M. Bills         return &messageIt->second;
744851d45dSJason M. Bills     }
754851d45dSJason M. Bills 
764851d45dSJason M. Bills     return nullptr;
774851d45dSJason M. Bills }
784851d45dSJason M. Bills 
794851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
804851d45dSJason M. Bills {
814851d45dSJason M. Bills     // Redfish MessageIds are in the form
824851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
834851d45dSJason M. Bills     // the right Message
844851d45dSJason M. Bills     std::vector<std::string> fields;
854851d45dSJason M. Bills     fields.reserve(4);
864851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
874851d45dSJason M. Bills     std::string& registryName = fields[0];
884851d45dSJason M. Bills     std::string& messageKey = fields[3];
894851d45dSJason M. Bills 
904851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
914851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
924851d45dSJason M. Bills     {
934851d45dSJason M. Bills         return getMessageFromRegistry(
9426702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
954851d45dSJason M. Bills     }
964851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
974851d45dSJason M. Bills     {
984851d45dSJason M. Bills         return getMessageFromRegistry(
9926702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1004851d45dSJason M. Bills     }
1014851d45dSJason M. Bills     return nullptr;
1024851d45dSJason M. Bills }
103fffb8c1fSEd Tanous } // namespace registries
1044851d45dSJason M. Bills 
105f6150403SJames Feist namespace fs = std::filesystem;
1061da66f75SEd Tanous 
107cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
108cb92c03bSAndrew Geissler {
109d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
110d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
111d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
112d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
113cb92c03bSAndrew Geissler     {
114cb92c03bSAndrew Geissler         return "Critical";
115cb92c03bSAndrew Geissler     }
1163174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
117d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
118d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
119cb92c03bSAndrew Geissler     {
120cb92c03bSAndrew Geissler         return "OK";
121cb92c03bSAndrew Geissler     }
1223174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
123cb92c03bSAndrew Geissler     {
124cb92c03bSAndrew Geissler         return "Warning";
125cb92c03bSAndrew Geissler     }
126cb92c03bSAndrew Geissler     return "";
127cb92c03bSAndrew Geissler }
128cb92c03bSAndrew Geissler 
1297e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13039e77504SEd Tanous                                      const std::string_view& field,
13139e77504SEd Tanous                                      std::string_view& contents)
13216428a1aSJason M. Bills {
13316428a1aSJason M. Bills     const char* data = nullptr;
13416428a1aSJason M. Bills     size_t length = 0;
13516428a1aSJason M. Bills     int ret = 0;
13616428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
13746ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
13846ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
13946ff87baSEd Tanous 
14046ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
14116428a1aSJason M. Bills     if (ret < 0)
14216428a1aSJason M. Bills     {
14316428a1aSJason M. Bills         return ret;
14416428a1aSJason M. Bills     }
14539e77504SEd Tanous     contents = std::string_view(data, length);
14616428a1aSJason M. Bills     // Only use the content after the "=" character.
14781ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
14816428a1aSJason M. Bills     return ret;
14916428a1aSJason M. Bills }
15016428a1aSJason M. Bills 
1517e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1527e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1537e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15416428a1aSJason M. Bills {
15516428a1aSJason M. Bills     int ret = 0;
15639e77504SEd Tanous     std::string_view metadata;
15716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15816428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
15916428a1aSJason M. Bills     if (ret < 0)
16016428a1aSJason M. Bills     {
16116428a1aSJason M. Bills         return ret;
16216428a1aSJason M. Bills     }
163b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16416428a1aSJason M. Bills     return ret;
16516428a1aSJason M. Bills }
16616428a1aSJason M. Bills 
1677e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1687e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
169a3316fc6SZhikuiRen {
170a3316fc6SZhikuiRen     int ret = 0;
171a3316fc6SZhikuiRen     uint64_t timestamp = 0;
172a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
173a3316fc6SZhikuiRen     if (ret < 0)
174a3316fc6SZhikuiRen     {
175a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
176a3316fc6SZhikuiRen                          << strerror(-ret);
177a3316fc6SZhikuiRen         return false;
178a3316fc6SZhikuiRen     }
1791d8782e7SNan Zhou     entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
1809c620e21SAsmitha Karunanithi     return true;
181a3316fc6SZhikuiRen }
18250b8a43aSEd Tanous 
18367df073bSEd Tanous static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18467df073bSEd Tanous                          const crow::Request& req, uint64_t& skip)
18567df073bSEd Tanous {
18667df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$skip");
18767df073bSEd Tanous     if (it != req.urlView.params().end())
18867df073bSEd Tanous     {
18967df073bSEd Tanous         std::from_chars_result r = std::from_chars(
19067df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), skip);
19167df073bSEd Tanous         if (r.ec != std::errc())
19267df073bSEd Tanous         {
19367df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$skip");
19467df073bSEd Tanous             return false;
19567df073bSEd Tanous         }
19667df073bSEd Tanous     }
19767df073bSEd Tanous     return true;
19867df073bSEd Tanous }
19967df073bSEd Tanous 
20067df073bSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
20167df073bSEd Tanous static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20267df073bSEd Tanous                         const crow::Request& req, uint64_t& top)
20367df073bSEd Tanous {
20467df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$top");
20567df073bSEd Tanous     if (it != req.urlView.params().end())
20667df073bSEd Tanous     {
20767df073bSEd Tanous         std::from_chars_result r = std::from_chars(
20867df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), top);
20967df073bSEd Tanous         if (r.ec != std::errc())
21067df073bSEd Tanous         {
21167df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$top");
21267df073bSEd Tanous             return false;
21367df073bSEd Tanous         }
21467df073bSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
21567df073bSEd Tanous         {
21667df073bSEd Tanous 
21767df073bSEd Tanous             messages::queryParameterOutOfRange(
21867df073bSEd Tanous                 asyncResp->res, std::to_string(top), "$top",
21967df073bSEd Tanous                 "1-" + std::to_string(maxEntriesPerPage));
22067df073bSEd Tanous             return false;
22167df073bSEd Tanous         }
22267df073bSEd Tanous     }
22367df073bSEd Tanous     return true;
22467df073bSEd Tanous }
22567df073bSEd Tanous 
2267e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
227e85d6b16SJason M. Bills                                     const bool firstEntry = true)
22816428a1aSJason M. Bills {
22916428a1aSJason M. Bills     int ret = 0;
23016428a1aSJason M. Bills     static uint64_t prevTs = 0;
23116428a1aSJason M. Bills     static int index = 0;
232e85d6b16SJason M. Bills     if (firstEntry)
233e85d6b16SJason M. Bills     {
234e85d6b16SJason M. Bills         prevTs = 0;
235e85d6b16SJason M. Bills     }
236e85d6b16SJason M. Bills 
23716428a1aSJason M. Bills     // Get the entry timestamp
23816428a1aSJason M. Bills     uint64_t curTs = 0;
23916428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
24016428a1aSJason M. Bills     if (ret < 0)
24116428a1aSJason M. Bills     {
24216428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
24316428a1aSJason M. Bills                          << strerror(-ret);
24416428a1aSJason M. Bills         return false;
24516428a1aSJason M. Bills     }
24616428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
24716428a1aSJason M. Bills     if (curTs == prevTs)
24816428a1aSJason M. Bills     {
24916428a1aSJason M. Bills         index++;
25016428a1aSJason M. Bills     }
25116428a1aSJason M. Bills     else
25216428a1aSJason M. Bills     {
25316428a1aSJason M. Bills         // Otherwise, reset it
25416428a1aSJason M. Bills         index = 0;
25516428a1aSJason M. Bills     }
25616428a1aSJason M. Bills     // Save the timestamp
25716428a1aSJason M. Bills     prevTs = curTs;
25816428a1aSJason M. Bills 
25916428a1aSJason M. Bills     entryID = std::to_string(curTs);
26016428a1aSJason M. Bills     if (index > 0)
26116428a1aSJason M. Bills     {
26216428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
26316428a1aSJason M. Bills     }
26416428a1aSJason M. Bills     return true;
26516428a1aSJason M. Bills }
26616428a1aSJason M. Bills 
267e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
268e85d6b16SJason M. Bills                              const bool firstEntry = true)
26995820184SJason M. Bills {
270271584abSEd Tanous     static time_t prevTs = 0;
27195820184SJason M. Bills     static int index = 0;
272e85d6b16SJason M. Bills     if (firstEntry)
273e85d6b16SJason M. Bills     {
274e85d6b16SJason M. Bills         prevTs = 0;
275e85d6b16SJason M. Bills     }
276e85d6b16SJason M. Bills 
27795820184SJason M. Bills     // Get the entry timestamp
278271584abSEd Tanous     std::time_t curTs = 0;
27995820184SJason M. Bills     std::tm timeStruct = {};
28095820184SJason M. Bills     std::istringstream entryStream(logEntry);
28195820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
28295820184SJason M. Bills     {
28395820184SJason M. Bills         curTs = std::mktime(&timeStruct);
28495820184SJason M. Bills     }
28595820184SJason M. Bills     // If the timestamp isn't unique, increment the index
28695820184SJason M. Bills     if (curTs == prevTs)
28795820184SJason M. Bills     {
28895820184SJason M. Bills         index++;
28995820184SJason M. Bills     }
29095820184SJason M. Bills     else
29195820184SJason M. Bills     {
29295820184SJason M. Bills         // Otherwise, reset it
29395820184SJason M. Bills         index = 0;
29495820184SJason M. Bills     }
29595820184SJason M. Bills     // Save the timestamp
29695820184SJason M. Bills     prevTs = curTs;
29795820184SJason M. Bills 
29895820184SJason M. Bills     entryID = std::to_string(curTs);
29995820184SJason M. Bills     if (index > 0)
30095820184SJason M. Bills     {
30195820184SJason M. Bills         entryID += "_" + std::to_string(index);
30295820184SJason M. Bills     }
30395820184SJason M. Bills     return true;
30495820184SJason M. Bills }
30595820184SJason M. Bills 
3067e860f15SJohn Edward Broadbent inline static bool
3078d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3088d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3098d1b46d7Szhanghch05                        uint64_t& index)
31016428a1aSJason M. Bills {
31116428a1aSJason M. Bills     if (entryID.empty())
31216428a1aSJason M. Bills     {
31316428a1aSJason M. Bills         return false;
31416428a1aSJason M. Bills     }
31516428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
31639e77504SEd Tanous     std::string_view tsStr(entryID);
31716428a1aSJason M. Bills 
31881ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
31971d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
32016428a1aSJason M. Bills     {
32116428a1aSJason M. Bills         // Timestamp has an index
32216428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
32339e77504SEd Tanous         std::string_view indexStr(entryID);
32416428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
325c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
326c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
327c0bd5e4bSEd Tanous         if (ec != std::errc())
32816428a1aSJason M. Bills         {
329ace85d60SEd Tanous             messages::resourceMissingAtURI(
330ace85d60SEd Tanous                 asyncResp->res, crow::utility::urlFromPieces(entryID));
33116428a1aSJason M. Bills             return false;
33216428a1aSJason M. Bills         }
33316428a1aSJason M. Bills     }
33416428a1aSJason M. Bills     // Timestamp has no index
335c0bd5e4bSEd Tanous     auto [ptr, ec] =
336c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
337c0bd5e4bSEd Tanous     if (ec != std::errc())
33816428a1aSJason M. Bills     {
339ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
340ace85d60SEd Tanous                                        crow::utility::urlFromPieces(entryID));
34116428a1aSJason M. Bills         return false;
34216428a1aSJason M. Bills     }
34316428a1aSJason M. Bills     return true;
34416428a1aSJason M. Bills }
34516428a1aSJason M. Bills 
34695820184SJason M. Bills static bool
34795820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
34895820184SJason M. Bills {
34995820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
35095820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
35195820184SJason M. Bills 
35295820184SJason M. Bills     // Loop through the directory looking for redfish log files
35395820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
35495820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
35595820184SJason M. Bills     {
35695820184SJason M. Bills         // If we find a redfish log file, save the path
35795820184SJason M. Bills         std::string filename = dirEnt.path().filename();
35895820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
35995820184SJason M. Bills         {
36095820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
36195820184SJason M. Bills         }
36295820184SJason M. Bills     }
36395820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
36495820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
36595820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
36695820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
36795820184SJason M. Bills 
36895820184SJason M. Bills     return !redfishLogFiles.empty();
36995820184SJason M. Bills }
37095820184SJason M. Bills 
3718d1b46d7Szhanghch05 inline void
3728d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3735cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3745cb1dd27SAsmitha Karunanithi {
3755cb1dd27SAsmitha Karunanithi     std::string dumpPath;
3765cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
3775cb1dd27SAsmitha Karunanithi     {
3785cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
3795cb1dd27SAsmitha Karunanithi     }
3805cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
3815cb1dd27SAsmitha Karunanithi     {
3825cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
3835cb1dd27SAsmitha Karunanithi     }
3845cb1dd27SAsmitha Karunanithi     else
3855cb1dd27SAsmitha Karunanithi     {
3865cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
3875cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3885cb1dd27SAsmitha Karunanithi         return;
3895cb1dd27SAsmitha Karunanithi     }
3905cb1dd27SAsmitha Karunanithi 
3915cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
392711ac7a9SEd Tanous         [asyncResp, dumpPath,
393711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
394711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
3955cb1dd27SAsmitha Karunanithi             if (ec)
3965cb1dd27SAsmitha Karunanithi             {
3975cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
3985cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
3995cb1dd27SAsmitha Karunanithi                 return;
4005cb1dd27SAsmitha Karunanithi             }
4015cb1dd27SAsmitha Karunanithi 
4025cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4035cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
404b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
405b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
406b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
407b47452b2SAsmitha Karunanithi                 "/entry/";
4085cb1dd27SAsmitha Karunanithi 
4095cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4105cb1dd27SAsmitha Karunanithi             {
411b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4125cb1dd27SAsmitha Karunanithi                 {
4135cb1dd27SAsmitha Karunanithi                     continue;
4145cb1dd27SAsmitha Karunanithi                 }
4151d8782e7SNan Zhou                 uint64_t timestamp = 0;
4165cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
41735440d18SAsmitha Karunanithi                 std::string dumpStatus;
41835440d18SAsmitha Karunanithi                 nlohmann::json thisEntry;
4192dfd18efSEd Tanous 
4202dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4212dfd18efSEd Tanous                 if (entryID.empty())
4225cb1dd27SAsmitha Karunanithi                 {
4235cb1dd27SAsmitha Karunanithi                     continue;
4245cb1dd27SAsmitha Karunanithi                 }
4255cb1dd27SAsmitha Karunanithi 
4265cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4275cb1dd27SAsmitha Karunanithi                 {
42835440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
42935440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
43035440d18SAsmitha Karunanithi                     {
4319eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
43235440d18SAsmitha Karunanithi                         {
43335440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
43435440d18SAsmitha Karunanithi                             {
43555f79e6fSEd Tanous                                 const auto* status = std::get_if<std::string>(
43635440d18SAsmitha Karunanithi                                     &propertyMap.second);
43735440d18SAsmitha Karunanithi                                 if (status == nullptr)
43835440d18SAsmitha Karunanithi                                 {
43935440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
44035440d18SAsmitha Karunanithi                                     break;
44135440d18SAsmitha Karunanithi                                 }
44235440d18SAsmitha Karunanithi                                 dumpStatus = *status;
44335440d18SAsmitha Karunanithi                             }
44435440d18SAsmitha Karunanithi                         }
44535440d18SAsmitha Karunanithi                     }
44635440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
44735440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
4485cb1dd27SAsmitha Karunanithi                     {
4495cb1dd27SAsmitha Karunanithi 
4505cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4515cb1dd27SAsmitha Karunanithi                         {
4525cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4535cb1dd27SAsmitha Karunanithi                             {
45455f79e6fSEd Tanous                                 const auto* sizePtr =
4555cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4565cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4575cb1dd27SAsmitha Karunanithi                                 {
4585cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4595cb1dd27SAsmitha Karunanithi                                     break;
4605cb1dd27SAsmitha Karunanithi                                 }
4615cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4625cb1dd27SAsmitha Karunanithi                                 break;
4635cb1dd27SAsmitha Karunanithi                             }
4645cb1dd27SAsmitha Karunanithi                         }
4655cb1dd27SAsmitha Karunanithi                     }
4665cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4675cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4685cb1dd27SAsmitha Karunanithi                     {
4695cb1dd27SAsmitha Karunanithi 
4709eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
4715cb1dd27SAsmitha Karunanithi                         {
4725cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4735cb1dd27SAsmitha Karunanithi                             {
4745cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4755cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4765cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4775cb1dd27SAsmitha Karunanithi                                 {
4785cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4795cb1dd27SAsmitha Karunanithi                                     break;
4805cb1dd27SAsmitha Karunanithi                                 }
4811d8782e7SNan Zhou                                 timestamp = (*usecsTimeStamp / 1000 / 1000);
4825cb1dd27SAsmitha Karunanithi                                 break;
4835cb1dd27SAsmitha Karunanithi                             }
4845cb1dd27SAsmitha Karunanithi                         }
4855cb1dd27SAsmitha Karunanithi                     }
4865cb1dd27SAsmitha Karunanithi                 }
4875cb1dd27SAsmitha Karunanithi 
4880fda0f12SGeorge Liu                 if (dumpStatus !=
4890fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
49035440d18SAsmitha Karunanithi                     !dumpStatus.empty())
49135440d18SAsmitha Karunanithi                 {
49235440d18SAsmitha Karunanithi                     // Dump status is not Complete, no need to enumerate
49335440d18SAsmitha Karunanithi                     continue;
49435440d18SAsmitha Karunanithi                 }
49535440d18SAsmitha Karunanithi 
496647b3cdcSGeorge Liu                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
4975cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
4985cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
4995cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5001d8782e7SNan Zhou                 thisEntry["Created"] =
5011d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
5025cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5035cb1dd27SAsmitha Karunanithi 
504d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5055cb1dd27SAsmitha Karunanithi 
5065cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5075cb1dd27SAsmitha Karunanithi                 {
508d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
509d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
510de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
511de8d94a3SAbhishek Patel                         entryID + "/attachment";
5125cb1dd27SAsmitha Karunanithi                 }
5135cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5145cb1dd27SAsmitha Karunanithi                 {
515d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
516d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
517d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
518de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
519de8d94a3SAbhishek Patel                         entryID + "/attachment";
5205cb1dd27SAsmitha Karunanithi                 }
52135440d18SAsmitha Karunanithi                 entriesArray.push_back(std::move(thisEntry));
5225cb1dd27SAsmitha Karunanithi             }
5235cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5245cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5255cb1dd27SAsmitha Karunanithi         },
5265cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5275cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5285cb1dd27SAsmitha Karunanithi }
5295cb1dd27SAsmitha Karunanithi 
5308d1b46d7Szhanghch05 inline void
531*45ca1b86SEd Tanous     getDumpEntryById(crow::App& app, const crow::Request& req,
532*45ca1b86SEd Tanous                      const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5338d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5345cb1dd27SAsmitha Karunanithi {
535*45ca1b86SEd Tanous     if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
536*45ca1b86SEd Tanous     {
537*45ca1b86SEd Tanous         return;
538*45ca1b86SEd Tanous     }
5395cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5405cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5415cb1dd27SAsmitha Karunanithi     {
5425cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5435cb1dd27SAsmitha Karunanithi     }
5445cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5455cb1dd27SAsmitha Karunanithi     {
5465cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5475cb1dd27SAsmitha Karunanithi     }
5485cb1dd27SAsmitha Karunanithi     else
5495cb1dd27SAsmitha Karunanithi     {
5505cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5515cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5525cb1dd27SAsmitha Karunanithi         return;
5535cb1dd27SAsmitha Karunanithi     }
5545cb1dd27SAsmitha Karunanithi 
5555cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
556711ac7a9SEd Tanous         [asyncResp, entryID, dumpPath,
557711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
558711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
5595cb1dd27SAsmitha Karunanithi             if (ec)
5605cb1dd27SAsmitha Karunanithi             {
5615cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5625cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5635cb1dd27SAsmitha Karunanithi                 return;
5645cb1dd27SAsmitha Karunanithi             }
5655cb1dd27SAsmitha Karunanithi 
566b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
567b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
568b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
569b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
570b47452b2SAsmitha Karunanithi                 "/entry/";
571b47452b2SAsmitha Karunanithi 
5729eb808c1SEd Tanous             for (const auto& objectPath : resp)
5735cb1dd27SAsmitha Karunanithi             {
574b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5755cb1dd27SAsmitha Karunanithi                 {
5765cb1dd27SAsmitha Karunanithi                     continue;
5775cb1dd27SAsmitha Karunanithi                 }
5785cb1dd27SAsmitha Karunanithi 
5795cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5801d8782e7SNan Zhou                 uint64_t timestamp = 0;
5815cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
58235440d18SAsmitha Karunanithi                 std::string dumpStatus;
5835cb1dd27SAsmitha Karunanithi 
5849eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
5855cb1dd27SAsmitha Karunanithi                 {
58635440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
58735440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
58835440d18SAsmitha Karunanithi                     {
5899eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
59035440d18SAsmitha Karunanithi                         {
59135440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
59235440d18SAsmitha Karunanithi                             {
5939eb808c1SEd Tanous                                 const std::string* status =
5949eb808c1SEd Tanous                                     std::get_if<std::string>(
59535440d18SAsmitha Karunanithi                                         &propertyMap.second);
59635440d18SAsmitha Karunanithi                                 if (status == nullptr)
59735440d18SAsmitha Karunanithi                                 {
59835440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
59935440d18SAsmitha Karunanithi                                     break;
60035440d18SAsmitha Karunanithi                                 }
60135440d18SAsmitha Karunanithi                                 dumpStatus = *status;
60235440d18SAsmitha Karunanithi                             }
60335440d18SAsmitha Karunanithi                         }
60435440d18SAsmitha Karunanithi                     }
60535440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
60635440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
6075cb1dd27SAsmitha Karunanithi                     {
6089eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
6095cb1dd27SAsmitha Karunanithi                         {
6105cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
6115cb1dd27SAsmitha Karunanithi                             {
6129eb808c1SEd Tanous                                 const uint64_t* sizePtr =
6135cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6145cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
6155cb1dd27SAsmitha Karunanithi                                 {
6165cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6175cb1dd27SAsmitha Karunanithi                                     break;
6185cb1dd27SAsmitha Karunanithi                                 }
6195cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
6205cb1dd27SAsmitha Karunanithi                                 break;
6215cb1dd27SAsmitha Karunanithi                             }
6225cb1dd27SAsmitha Karunanithi                         }
6235cb1dd27SAsmitha Karunanithi                     }
6245cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6255cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6265cb1dd27SAsmitha Karunanithi                     {
6279eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
6285cb1dd27SAsmitha Karunanithi                         {
6295cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6305cb1dd27SAsmitha Karunanithi                             {
6315cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6325cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6335cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6345cb1dd27SAsmitha Karunanithi                                 {
6355cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6365cb1dd27SAsmitha Karunanithi                                     break;
6375cb1dd27SAsmitha Karunanithi                                 }
6381d8782e7SNan Zhou                                 timestamp = *usecsTimeStamp / 1000 / 1000;
6395cb1dd27SAsmitha Karunanithi                                 break;
6405cb1dd27SAsmitha Karunanithi                             }
6415cb1dd27SAsmitha Karunanithi                         }
6425cb1dd27SAsmitha Karunanithi                     }
6435cb1dd27SAsmitha Karunanithi                 }
6445cb1dd27SAsmitha Karunanithi 
6450fda0f12SGeorge Liu                 if (dumpStatus !=
6460fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
64735440d18SAsmitha Karunanithi                     !dumpStatus.empty())
64835440d18SAsmitha Karunanithi                 {
64935440d18SAsmitha Karunanithi                     // Dump status is not Complete
65035440d18SAsmitha Karunanithi                     // return not found until status is changed to Completed
65135440d18SAsmitha Karunanithi                     messages::resourceNotFound(asyncResp->res,
65235440d18SAsmitha Karunanithi                                                dumpType + " dump", entryID);
65335440d18SAsmitha Karunanithi                     return;
65435440d18SAsmitha Karunanithi                 }
65535440d18SAsmitha Karunanithi 
6565cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
657647b3cdcSGeorge Liu                     "#LogEntry.v1_8_0.LogEntry";
6585cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6595cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6605cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6615cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6621d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
6635cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6645cb1dd27SAsmitha Karunanithi 
665d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6665cb1dd27SAsmitha Karunanithi 
6675cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6685cb1dd27SAsmitha Karunanithi                 {
669d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
670d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
671de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
672de8d94a3SAbhishek Patel                         entryID + "/attachment";
6735cb1dd27SAsmitha Karunanithi                 }
6745cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6755cb1dd27SAsmitha Karunanithi                 {
676d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
677d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6785cb1dd27SAsmitha Karunanithi                         "System";
679d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
680de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
681de8d94a3SAbhishek Patel                         entryID + "/attachment";
6825cb1dd27SAsmitha Karunanithi                 }
6835cb1dd27SAsmitha Karunanithi             }
684e05aec50SEd Tanous             if (!foundDumpEntry)
685b47452b2SAsmitha Karunanithi             {
686b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
687b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
688b47452b2SAsmitha Karunanithi                 return;
689b47452b2SAsmitha Karunanithi             }
6905cb1dd27SAsmitha Karunanithi         },
6915cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6925cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6935cb1dd27SAsmitha Karunanithi }
6945cb1dd27SAsmitha Karunanithi 
6958d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6969878256fSStanley Chu                             const std::string& entryID,
697b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6985cb1dd27SAsmitha Karunanithi {
6993de8d8baSGeorge Liu     auto respHandler = [asyncResp,
7003de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
7015cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
7025cb1dd27SAsmitha Karunanithi         if (ec)
7035cb1dd27SAsmitha Karunanithi         {
7043de8d8baSGeorge Liu             if (ec.value() == EBADR)
7053de8d8baSGeorge Liu             {
7063de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
7073de8d8baSGeorge Liu                 return;
7083de8d8baSGeorge Liu             }
7095cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
7105cb1dd27SAsmitha Karunanithi                              << ec;
7115cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
7125cb1dd27SAsmitha Karunanithi             return;
7135cb1dd27SAsmitha Karunanithi         }
7145cb1dd27SAsmitha Karunanithi     };
7155cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7165cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
717b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
718b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
719b47452b2SAsmitha Karunanithi             entryID,
7205cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7215cb1dd27SAsmitha Karunanithi }
7225cb1dd27SAsmitha Karunanithi 
7238d1b46d7Szhanghch05 inline void
72498be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
7258d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7268d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
727a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
728a43be80fSAsmitha Karunanithi {
729a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7306145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
731a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
732a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
733cb13a392SEd Tanous             if (err)
734cb13a392SEd Tanous             {
7356145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7366145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7376145ed6fSAsmitha Karunanithi                 return task::completed;
738cb13a392SEd Tanous             }
739b9d36b47SEd Tanous 
740b9d36b47SEd Tanous             dbus::utility::DBusInteracesMap interfacesList;
741a43be80fSAsmitha Karunanithi 
742a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
743a43be80fSAsmitha Karunanithi 
744a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
745a43be80fSAsmitha Karunanithi 
746b47452b2SAsmitha Karunanithi             if (objPath.str ==
747b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
748b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
749b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
750a43be80fSAsmitha Karunanithi             {
751a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
752a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
753a43be80fSAsmitha Karunanithi 
754a43be80fSAsmitha Karunanithi                 std::string headerLoc =
755a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
756a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
757a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
758a43be80fSAsmitha Karunanithi 
759a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
760b47452b2SAsmitha Karunanithi                 return task::completed;
7616145ed6fSAsmitha Karunanithi             }
762a43be80fSAsmitha Karunanithi             return task::completed;
763a43be80fSAsmitha Karunanithi         },
7644978b63fSJason M. Bills         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
765a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
766a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
767a43be80fSAsmitha Karunanithi 
768a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
769a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
77098be3e39SEd Tanous     task->payload.emplace(std::move(payload));
771a43be80fSAsmitha Karunanithi }
772a43be80fSAsmitha Karunanithi 
7738d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7748d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
775a43be80fSAsmitha Karunanithi {
776a43be80fSAsmitha Karunanithi     std::string dumpPath;
777a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
778a43be80fSAsmitha Karunanithi     {
779a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
780a43be80fSAsmitha Karunanithi     }
781a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
782a43be80fSAsmitha Karunanithi     {
783a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
784a43be80fSAsmitha Karunanithi     }
785a43be80fSAsmitha Karunanithi     else
786a43be80fSAsmitha Karunanithi     {
787a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
788a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
789a43be80fSAsmitha Karunanithi         return;
790a43be80fSAsmitha Karunanithi     }
791a43be80fSAsmitha Karunanithi 
792a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
793a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
794a43be80fSAsmitha Karunanithi 
79515ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
796a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
797a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
798a43be80fSAsmitha Karunanithi     {
799a43be80fSAsmitha Karunanithi         return;
800a43be80fSAsmitha Karunanithi     }
801a43be80fSAsmitha Karunanithi 
802a43be80fSAsmitha Karunanithi     if (dumpType == "System")
803a43be80fSAsmitha Karunanithi     {
804a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
805a43be80fSAsmitha Karunanithi         {
8064978b63fSJason M. Bills             BMCWEB_LOG_ERROR
8074978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
808a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
809a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
810a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
811a43be80fSAsmitha Karunanithi             return;
812a43be80fSAsmitha Karunanithi         }
8133174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
814a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
815a43be80fSAsmitha Karunanithi         {
816a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
817ace85d60SEd Tanous             messages::internalError(asyncResp->res);
818a43be80fSAsmitha Karunanithi             return;
819a43be80fSAsmitha Karunanithi         }
820a43be80fSAsmitha Karunanithi     }
821a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
822a43be80fSAsmitha Karunanithi     {
823a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
824a43be80fSAsmitha Karunanithi         {
8250fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8260fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
827a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
828a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
829a43be80fSAsmitha Karunanithi             return;
830a43be80fSAsmitha Karunanithi         }
8313174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
832a43be80fSAsmitha Karunanithi         {
833a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
834a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
835ace85d60SEd Tanous             messages::internalError(asyncResp->res);
836a43be80fSAsmitha Karunanithi             return;
837a43be80fSAsmitha Karunanithi         }
838a43be80fSAsmitha Karunanithi     }
839a43be80fSAsmitha Karunanithi 
840a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
84198be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
84298be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
84398be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
844a43be80fSAsmitha Karunanithi             if (ec)
845a43be80fSAsmitha Karunanithi             {
846a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
847a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
848a43be80fSAsmitha Karunanithi                 return;
849a43be80fSAsmitha Karunanithi             }
850a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
851a43be80fSAsmitha Karunanithi 
85298be3e39SEd Tanous             createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
85398be3e39SEd Tanous                                    dumpPath, dumpType);
854a43be80fSAsmitha Karunanithi         },
855b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
856b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
857b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
858a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
859a43be80fSAsmitha Karunanithi }
860a43be80fSAsmitha Karunanithi 
8618d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8628d1b46d7Szhanghch05                       const std::string& dumpType)
86380319af1SAsmitha Karunanithi {
864b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
865b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8668d1b46d7Szhanghch05 
86780319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
868b9d36b47SEd Tanous         [asyncResp, dumpType](
869b9d36b47SEd Tanous             const boost::system::error_code ec,
870b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
87180319af1SAsmitha Karunanithi             if (ec)
87280319af1SAsmitha Karunanithi             {
87380319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
87480319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
87580319af1SAsmitha Karunanithi                 return;
87680319af1SAsmitha Karunanithi             }
87780319af1SAsmitha Karunanithi 
87880319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
87980319af1SAsmitha Karunanithi             {
8802dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8812dfd18efSEd Tanous                 std::string logID = objPath.filename();
8822dfd18efSEd Tanous                 if (logID.empty())
88380319af1SAsmitha Karunanithi                 {
8842dfd18efSEd Tanous                     continue;
88580319af1SAsmitha Karunanithi                 }
8862dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
88780319af1SAsmitha Karunanithi             }
88880319af1SAsmitha Karunanithi         },
88980319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
89080319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
89180319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
892b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
893b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
894b47452b2SAsmitha Karunanithi                                    dumpType});
89580319af1SAsmitha Karunanithi }
89680319af1SAsmitha Karunanithi 
897b9d36b47SEd Tanous inline static void
898b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
899b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
900b9d36b47SEd Tanous                              std::string& logfile)
901043a0536SJohnathan Mantey {
902043a0536SJohnathan Mantey     for (auto property : params)
903043a0536SJohnathan Mantey     {
904043a0536SJohnathan Mantey         if (property.first == "Timestamp")
905043a0536SJohnathan Mantey         {
906043a0536SJohnathan Mantey             const std::string* value =
9078d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
908043a0536SJohnathan Mantey             if (value != nullptr)
909043a0536SJohnathan Mantey             {
910043a0536SJohnathan Mantey                 timestamp = *value;
911043a0536SJohnathan Mantey             }
912043a0536SJohnathan Mantey         }
913043a0536SJohnathan Mantey         else if (property.first == "Filename")
914043a0536SJohnathan Mantey         {
915043a0536SJohnathan Mantey             const std::string* value =
9168d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
917043a0536SJohnathan Mantey             if (value != nullptr)
918043a0536SJohnathan Mantey             {
919043a0536SJohnathan Mantey                 filename = *value;
920043a0536SJohnathan Mantey             }
921043a0536SJohnathan Mantey         }
922043a0536SJohnathan Mantey         else if (property.first == "Log")
923043a0536SJohnathan Mantey         {
924043a0536SJohnathan Mantey             const std::string* value =
9258d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
926043a0536SJohnathan Mantey             if (value != nullptr)
927043a0536SJohnathan Mantey             {
928043a0536SJohnathan Mantey                 logfile = *value;
929043a0536SJohnathan Mantey             }
930043a0536SJohnathan Mantey         }
931043a0536SJohnathan Mantey     }
932043a0536SJohnathan Mantey }
933043a0536SJohnathan Mantey 
934a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
9357e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9361da66f75SEd Tanous {
937c4bf6374SJason M. Bills     /**
938c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
939c4bf6374SJason M. Bills      */
9407e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
941ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
942*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
943*45ca1b86SEd Tanous                                                        const std::shared_ptr<
944*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
945*45ca1b86SEd Tanous                                                            asyncResp) {
946*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
947c4bf6374SJason M. Bills             {
948*45ca1b86SEd Tanous                 return;
949*45ca1b86SEd Tanous             }
9507e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
9517e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
952c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
953c4bf6374SJason M. Bills                 "#LogServiceCollection.LogServiceCollection";
954c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
955029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices";
956*45ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
957c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Description"] =
958c4bf6374SJason M. Bills                 "Collection of LogServices for this Computer System";
9597e860f15SJohn Edward Broadbent             nlohmann::json& logServiceArray =
9607e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members"];
961c4bf6374SJason M. Bills             logServiceArray = nlohmann::json::array();
962029573d4SEd Tanous             logServiceArray.push_back(
9637e860f15SJohn Edward Broadbent                 {{"@odata.id",
9647e860f15SJohn Edward Broadbent                   "/redfish/v1/Systems/system/LogServices/EventLog"}});
9655cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
966c9bb6861Sraviteja-b             logServiceArray.push_back(
967*45ca1b86SEd Tanous                 {{"@odata.id", "/redfish/v1/Systems/system/LogServices/Dump"}});
968c9bb6861Sraviteja-b #endif
969c9bb6861Sraviteja-b 
970d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
971d53dd41fSJason M. Bills             logServiceArray.push_back(
972cb92c03bSAndrew Geissler                 {{"@odata.id",
973424c4176SJason M. Bills                   "/redfish/v1/Systems/system/LogServices/Crashdump"}});
974d53dd41fSJason M. Bills #endif
975b7028ebfSSpencer Ku 
976b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
977b7028ebfSSpencer Ku             logServiceArray.push_back(
978b7028ebfSSpencer Ku                 {{"@odata.id",
979b7028ebfSSpencer Ku                   "/redfish/v1/Systems/system/LogServices/HostLogger"}});
980b7028ebfSSpencer Ku #endif
981c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
982c4bf6374SJason M. Bills                 logServiceArray.size();
983a3316fc6SZhikuiRen 
984a3316fc6SZhikuiRen             crow::connections::systemBus->async_method_call(
985*45ca1b86SEd Tanous                 [asyncResp](const boost::system::error_code ec,
986b9d36b47SEd Tanous                             const dbus::utility::MapperGetSubTreePathsResponse&
987b9d36b47SEd Tanous                                 subtreePath) {
988a3316fc6SZhikuiRen                     if (ec)
989a3316fc6SZhikuiRen                     {
990a3316fc6SZhikuiRen                         BMCWEB_LOG_ERROR << ec;
991a3316fc6SZhikuiRen                         return;
992a3316fc6SZhikuiRen                     }
993a3316fc6SZhikuiRen 
99455f79e6fSEd Tanous                     for (const auto& pathStr : subtreePath)
995a3316fc6SZhikuiRen                     {
996a3316fc6SZhikuiRen                         if (pathStr.find("PostCode") != std::string::npos)
997a3316fc6SZhikuiRen                         {
99823a21a1cSEd Tanous                             nlohmann::json& logServiceArrayLocal =
999a3316fc6SZhikuiRen                                 asyncResp->res.jsonValue["Members"];
100023a21a1cSEd Tanous                             logServiceArrayLocal.push_back(
10010fda0f12SGeorge Liu                                 {{"@odata.id",
10020fda0f12SGeorge Liu                                   "/redfish/v1/Systems/system/LogServices/PostCodes"}});
1003*45ca1b86SEd Tanous                             asyncResp->res.jsonValue["Members@odata.count"] =
100423a21a1cSEd Tanous                                 logServiceArrayLocal.size();
1005a3316fc6SZhikuiRen                             return;
1006a3316fc6SZhikuiRen                         }
1007a3316fc6SZhikuiRen                     }
1008a3316fc6SZhikuiRen                 },
1009a3316fc6SZhikuiRen                 "xyz.openbmc_project.ObjectMapper",
1010a3316fc6SZhikuiRen                 "/xyz/openbmc_project/object_mapper",
1011*45ca1b86SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
1012*45ca1b86SEd Tanous                 std::array<const char*, 1>{postCodeIface});
10137e860f15SJohn Edward Broadbent         });
1014c4bf6374SJason M. Bills }
1015c4bf6374SJason M. Bills 
10167e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1017c4bf6374SJason M. Bills {
10187e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
1019ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
1020*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1021*45ca1b86SEd Tanous                                                        const std::shared_ptr<
1022*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
1023*45ca1b86SEd Tanous                                                            asyncResp) {
1024*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1025*45ca1b86SEd Tanous             {
1026*45ca1b86SEd Tanous                 return;
1027*45ca1b86SEd Tanous             }
1028c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1029029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
1030c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1031c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
1032c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
10337e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
10347e860f15SJohn Edward Broadbent                 "System Event Log Service";
1035c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
1036c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10377c8c4058STejas Patil 
10387c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
10397c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
10407c8c4058STejas Patil 
10417c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10427c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10437c8c4058STejas Patil                 redfishDateTimeOffset.second;
10447c8c4058STejas Patil 
1045c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
1046c4bf6374SJason M. Bills                 {"@odata.id",
1047029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1048e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1049e7d6c8b2SGunnar Mills 
10500fda0f12SGeorge Liu                 {"target",
10510fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10527e860f15SJohn Edward Broadbent         });
1053489640c6SJason M. Bills }
1054489640c6SJason M. Bills 
10557e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1056489640c6SJason M. Bills {
10574978b63fSJason M. Bills     BMCWEB_ROUTE(
10584978b63fSJason M. Bills         app,
10594978b63fSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
1060432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10617e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
1062*45ca1b86SEd Tanous             [&app](const crow::Request& req,
10637e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1064*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1065*45ca1b86SEd Tanous                 {
1066*45ca1b86SEd Tanous                     return;
1067*45ca1b86SEd Tanous                 }
1068489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1069489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1070489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1071489640c6SJason M. Bills                 {
1072489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1073489640c6SJason M. Bills                     {
1074489640c6SJason M. Bills                         std::error_code ec;
1075489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1076489640c6SJason M. Bills                     }
1077489640c6SJason M. Bills                 }
1078489640c6SJason M. Bills 
1079489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1080489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1081489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1082489640c6SJason M. Bills                         if (ec)
1083489640c6SJason M. Bills                         {
10847e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
10857e860f15SJohn Edward Broadbent                                              << ec;
1086489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1087489640c6SJason M. Bills                             return;
1088489640c6SJason M. Bills                         }
1089489640c6SJason M. Bills 
1090489640c6SJason M. Bills                         messages::success(asyncResp->res);
1091489640c6SJason M. Bills                     },
1092489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
10937e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
10947e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
10957e860f15SJohn Edward Broadbent             });
1096c4bf6374SJason M. Bills }
1097c4bf6374SJason M. Bills 
109895820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1099b5a76932SEd Tanous                                  const std::string& logEntry,
110095820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1101c4bf6374SJason M. Bills {
110295820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1103cd225da8SJason M. Bills     // First get the Timestamp
1104f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1105cd225da8SJason M. Bills     if (space == std::string::npos)
110695820184SJason M. Bills     {
110795820184SJason M. Bills         return 1;
110895820184SJason M. Bills     }
1109cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1110cd225da8SJason M. Bills     // Then get the log contents
1111f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1112cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1113cd225da8SJason M. Bills     {
1114cd225da8SJason M. Bills         return 1;
1115cd225da8SJason M. Bills     }
1116cd225da8SJason M. Bills     std::string_view entry(logEntry);
1117cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1118cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1119cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1120cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1121cd225da8SJason M. Bills                  boost::token_compress_on);
1122cd225da8SJason M. Bills     // We need at least a MessageId to be valid
112326f6976fSEd Tanous     if (logEntryFields.empty())
1124cd225da8SJason M. Bills     {
1125cd225da8SJason M. Bills         return 1;
1126cd225da8SJason M. Bills     }
1127cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
112895820184SJason M. Bills 
11294851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1130fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1131c4bf6374SJason M. Bills 
11324851d45dSJason M. Bills     std::string msg;
11334851d45dSJason M. Bills     std::string severity;
11344851d45dSJason M. Bills     if (message != nullptr)
1135c4bf6374SJason M. Bills     {
11364851d45dSJason M. Bills         msg = message->message;
11375f2b84eeSEd Tanous         severity = message->messageSeverity;
1138c4bf6374SJason M. Bills     }
1139c4bf6374SJason M. Bills 
114015a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
114126702d01SEd Tanous     std::span<std::string> messageArgs;
114215a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
114315a86ff6SJason M. Bills     {
114415a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
114515a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
114615a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
114715a86ff6SJason M. Bills         if (!messageArgsStart.empty())
114815a86ff6SJason M. Bills         {
114915a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
115015a86ff6SJason M. Bills         }
115115a86ff6SJason M. Bills 
115223a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1153c4bf6374SJason M. Bills 
11544851d45dSJason M. Bills         // Fill the MessageArgs into the Message
115595820184SJason M. Bills         int i = 0;
115695820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11574851d45dSJason M. Bills         {
115895820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11594851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11604851d45dSJason M. Bills             if (argPos != std::string::npos)
11614851d45dSJason M. Bills             {
116295820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11634851d45dSJason M. Bills             }
11644851d45dSJason M. Bills         }
116515a86ff6SJason M. Bills     }
11664851d45dSJason M. Bills 
116795820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
116895820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
116995820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1170f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1171f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
117295820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1173c4bf6374SJason M. Bills     {
117495820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1175c4bf6374SJason M. Bills     }
1176c4bf6374SJason M. Bills 
1177c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
117895820184SJason M. Bills     logEntryJson = {
1179647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1180029573d4SEd Tanous         {"@odata.id",
1181897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
118295820184SJason M. Bills              logEntryID},
1183c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
118495820184SJason M. Bills         {"Id", logEntryID},
118595820184SJason M. Bills         {"Message", std::move(msg)},
118695820184SJason M. Bills         {"MessageId", std::move(messageID)},
1187f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1188c4bf6374SJason M. Bills         {"EntryType", "Event"},
118995820184SJason M. Bills         {"Severity", std::move(severity)},
119095820184SJason M. Bills         {"Created", std::move(timestamp)}};
1191c4bf6374SJason M. Bills     return 0;
1192c4bf6374SJason M. Bills }
1193c4bf6374SJason M. Bills 
11947e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1195c4bf6374SJason M. Bills {
11967e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11977e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11988b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1199*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1200*45ca1b86SEd Tanous                                                        const std::shared_ptr<
1201*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
1202*45ca1b86SEd Tanous                                                            asyncResp) {
1203*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1204*45ca1b86SEd Tanous             {
1205*45ca1b86SEd Tanous                 return;
1206*45ca1b86SEd Tanous             }
1207271584abSEd Tanous             uint64_t skip = 0;
1208271584abSEd Tanous             uint64_t top = maxEntriesPerPage; // Show max entries by default
12098d1b46d7Szhanghch05             if (!getSkipParam(asyncResp, req, skip))
1210c4bf6374SJason M. Bills             {
1211c4bf6374SJason M. Bills                 return;
1212c4bf6374SJason M. Bills             }
12138d1b46d7Szhanghch05             if (!getTopParam(asyncResp, req, top))
1214c4bf6374SJason M. Bills             {
1215c4bf6374SJason M. Bills                 return;
1216c4bf6374SJason M. Bills             }
12177e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
12187e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
1219c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1220c4bf6374SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
1221c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1222029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1223c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1224c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Description"] =
1225c4bf6374SJason M. Bills                 "Collection of System Event Log Entries";
1226cb92c03bSAndrew Geissler 
12274978b63fSJason M. Bills             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1228c4bf6374SJason M. Bills             logEntryArray = nlohmann::json::array();
12297e860f15SJohn Edward Broadbent             // Go through the log files and create a unique ID for each
12307e860f15SJohn Edward Broadbent             // entry
123195820184SJason M. Bills             std::vector<std::filesystem::path> redfishLogFiles;
123295820184SJason M. Bills             getRedfishLogFiles(redfishLogFiles);
1233b01bf299SEd Tanous             uint64_t entryCount = 0;
1234cd225da8SJason M. Bills             std::string logEntry;
123595820184SJason M. Bills 
12367e860f15SJohn Edward Broadbent             // Oldest logs are in the last file, so start there and loop
12377e860f15SJohn Edward Broadbent             // backwards
12387e860f15SJohn Edward Broadbent             for (auto it = redfishLogFiles.rbegin();
12397e860f15SJohn Edward Broadbent                  it < redfishLogFiles.rend(); it++)
1240c4bf6374SJason M. Bills             {
1241cd225da8SJason M. Bills                 std::ifstream logStream(*it);
124295820184SJason M. Bills                 if (!logStream.is_open())
1243c4bf6374SJason M. Bills                 {
1244c4bf6374SJason M. Bills                     continue;
1245c4bf6374SJason M. Bills                 }
1246c4bf6374SJason M. Bills 
1247e85d6b16SJason M. Bills                 // Reset the unique ID on the first entry
1248e85d6b16SJason M. Bills                 bool firstEntry = true;
124995820184SJason M. Bills                 while (std::getline(logStream, logEntry))
125095820184SJason M. Bills                 {
1251c4bf6374SJason M. Bills                     entryCount++;
12527e860f15SJohn Edward Broadbent                     // Handle paging using skip (number of entries to skip
12537e860f15SJohn Edward Broadbent                     // from the start) and top (number of entries to
12547e860f15SJohn Edward Broadbent                     // display)
1255c4bf6374SJason M. Bills                     if (entryCount <= skip || entryCount > skip + top)
1256c4bf6374SJason M. Bills                     {
1257c4bf6374SJason M. Bills                         continue;
1258c4bf6374SJason M. Bills                     }
1259c4bf6374SJason M. Bills 
1260c4bf6374SJason M. Bills                     std::string idStr;
1261e85d6b16SJason M. Bills                     if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1262c4bf6374SJason M. Bills                     {
1263c4bf6374SJason M. Bills                         continue;
1264c4bf6374SJason M. Bills                     }
1265c4bf6374SJason M. Bills 
1266e85d6b16SJason M. Bills                     if (firstEntry)
1267e85d6b16SJason M. Bills                     {
1268e85d6b16SJason M. Bills                         firstEntry = false;
1269e85d6b16SJason M. Bills                     }
1270e85d6b16SJason M. Bills 
1271c4bf6374SJason M. Bills                     logEntryArray.push_back({});
1272c4bf6374SJason M. Bills                     nlohmann::json& bmcLogEntry = logEntryArray.back();
12734978b63fSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) !=
12744978b63fSJason M. Bills                         0)
1275c4bf6374SJason M. Bills                     {
1276c4bf6374SJason M. Bills                         messages::internalError(asyncResp->res);
1277c4bf6374SJason M. Bills                         return;
1278c4bf6374SJason M. Bills                     }
1279c4bf6374SJason M. Bills                 }
128095820184SJason M. Bills             }
1281c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1282c4bf6374SJason M. Bills             if (skip + top < entryCount)
1283c4bf6374SJason M. Bills             {
1284c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
12854978b63fSJason M. Bills                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
1286c4bf6374SJason M. Bills                     std::to_string(skip + top);
1287c4bf6374SJason M. Bills             }
12887e860f15SJohn Edward Broadbent         });
1289897967deSJason M. Bills }
1290897967deSJason M. Bills 
12917e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1292897967deSJason M. Bills {
12937e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12947e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1295ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12967e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1297*45ca1b86SEd Tanous             [&app](const crow::Request& req,
12987e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12997e860f15SJohn Edward Broadbent                    const std::string& param) {
1300*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1301*45ca1b86SEd Tanous                 {
1302*45ca1b86SEd Tanous                     return;
1303*45ca1b86SEd Tanous                 }
13047e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
13058d1b46d7Szhanghch05 
13067e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
13077e860f15SJohn Edward Broadbent                 // entry to find the target entry
1308897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1309897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1310897967deSJason M. Bills                 std::string logEntry;
1311897967deSJason M. Bills 
13127e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
13137e860f15SJohn Edward Broadbent                 // backwards
13147e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
13157e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1316897967deSJason M. Bills                 {
1317897967deSJason M. Bills                     std::ifstream logStream(*it);
1318897967deSJason M. Bills                     if (!logStream.is_open())
1319897967deSJason M. Bills                     {
1320897967deSJason M. Bills                         continue;
1321897967deSJason M. Bills                     }
1322897967deSJason M. Bills 
1323897967deSJason M. Bills                     // Reset the unique ID on the first entry
1324897967deSJason M. Bills                     bool firstEntry = true;
1325897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1326897967deSJason M. Bills                     {
1327897967deSJason M. Bills                         std::string idStr;
1328897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1329897967deSJason M. Bills                         {
1330897967deSJason M. Bills                             continue;
1331897967deSJason M. Bills                         }
1332897967deSJason M. Bills 
1333897967deSJason M. Bills                         if (firstEntry)
1334897967deSJason M. Bills                         {
1335897967deSJason M. Bills                             firstEntry = false;
1336897967deSJason M. Bills                         }
1337897967deSJason M. Bills 
1338897967deSJason M. Bills                         if (idStr == targetID)
1339897967deSJason M. Bills                         {
13407e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
13417e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1342897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1343897967deSJason M. Bills                             {
1344897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1345897967deSJason M. Bills                                 return;
1346897967deSJason M. Bills                             }
1347897967deSJason M. Bills                             return;
1348897967deSJason M. Bills                         }
1349897967deSJason M. Bills                     }
1350897967deSJason M. Bills                 }
1351897967deSJason M. Bills                 // Requested ID was not found
1352ace85d60SEd Tanous                 messages::resourceMissingAtURI(
1353ace85d60SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(targetID));
13547e860f15SJohn Edward Broadbent             });
135508a4e4b5SAnthony Wilson }
135608a4e4b5SAnthony Wilson 
13577e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
135808a4e4b5SAnthony Wilson {
13597e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13607e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1361ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1362*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1363*45ca1b86SEd Tanous                                                        const std::shared_ptr<
1364*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
1365*45ca1b86SEd Tanous                                                            asyncResp) {
1366*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1367*45ca1b86SEd Tanous             {
1368*45ca1b86SEd Tanous                 return;
1369*45ca1b86SEd Tanous             }
13707e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
13717e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
137208a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
137308a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
137408a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
137508a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
137608a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
137708a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
137808a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
137908a4e4b5SAnthony Wilson 
1380cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1381cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1382cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1383cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1384914e2d5dSEd Tanous                             const dbus::utility::ManagedObjectType& resp) {
1385cb92c03bSAndrew Geissler                     if (ec)
1386cb92c03bSAndrew Geissler                     {
1387cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1388cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1389cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1390cb92c03bSAndrew Geissler                             << ec;
1391cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1392cb92c03bSAndrew Geissler                         return;
1393cb92c03bSAndrew Geissler                     }
1394cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1395cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1396cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
13979eb808c1SEd Tanous                     for (const auto& objectPath : resp)
1398cb92c03bSAndrew Geissler                     {
1399914e2d5dSEd Tanous                         const uint32_t* id = nullptr;
1400c419c759SEd Tanous                         const uint64_t* timestamp = nullptr;
1401c419c759SEd Tanous                         const uint64_t* updateTimestamp = nullptr;
1402914e2d5dSEd Tanous                         const std::string* severity = nullptr;
1403914e2d5dSEd Tanous                         const std::string* message = nullptr;
1404914e2d5dSEd Tanous                         const std::string* filePath = nullptr;
140575710de2SXiaochao Ma                         bool resolved = false;
14069eb808c1SEd Tanous                         for (const auto& interfaceMap : objectPath.second)
1407f86bb901SAdriana Kobylak                         {
1408f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1409f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1410f86bb901SAdriana Kobylak                             {
14119eb808c1SEd Tanous                                 for (const auto& propertyMap :
14129eb808c1SEd Tanous                                      interfaceMap.second)
1413cb92c03bSAndrew Geissler                                 {
1414cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1415cb92c03bSAndrew Geissler                                     {
1416f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1417f86bb901SAdriana Kobylak                                             &propertyMap.second);
1418cb92c03bSAndrew Geissler                                     }
1419cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1420cb92c03bSAndrew Geissler                                     {
1421c419c759SEd Tanous                                         timestamp = std::get_if<uint64_t>(
1422f86bb901SAdriana Kobylak                                             &propertyMap.second);
14237e860f15SJohn Edward Broadbent                                     }
14247e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
14257e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
14267e860f15SJohn Edward Broadbent                                     {
1427c419c759SEd Tanous                                         updateTimestamp = std::get_if<uint64_t>(
14287e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14297e860f15SJohn Edward Broadbent                                     }
14307e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
14317e860f15SJohn Edward Broadbent                                     {
14327e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
14337e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14347e860f15SJohn Edward Broadbent                                     }
14357e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
14367e860f15SJohn Edward Broadbent                                     {
14377e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
14387e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14397e860f15SJohn Edward Broadbent                                     }
14407e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
14417e860f15SJohn Edward Broadbent                                     {
1442914e2d5dSEd Tanous                                         const bool* resolveptr =
1443914e2d5dSEd Tanous                                             std::get_if<bool>(
14447e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14457e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14467e860f15SJohn Edward Broadbent                                         {
14477e860f15SJohn Edward Broadbent                                             messages::internalError(
14487e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14497e860f15SJohn Edward Broadbent                                             return;
14507e860f15SJohn Edward Broadbent                                         }
14517e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14527e860f15SJohn Edward Broadbent                                     }
14537e860f15SJohn Edward Broadbent                                 }
14547e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14557e860f15SJohn Edward Broadbent                                     severity == nullptr)
14567e860f15SJohn Edward Broadbent                                 {
14577e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
14587e860f15SJohn Edward Broadbent                                     return;
14597e860f15SJohn Edward Broadbent                                 }
14607e860f15SJohn Edward Broadbent                             }
14617e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
14627e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
14637e860f15SJohn Edward Broadbent                             {
14649eb808c1SEd Tanous                                 for (const auto& propertyMap :
14659eb808c1SEd Tanous                                      interfaceMap.second)
14667e860f15SJohn Edward Broadbent                                 {
14677e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
14687e860f15SJohn Edward Broadbent                                     {
14697e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
14707e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14717e860f15SJohn Edward Broadbent                                     }
14727e860f15SJohn Edward Broadbent                                 }
14737e860f15SJohn Edward Broadbent                             }
14747e860f15SJohn Edward Broadbent                         }
14757e860f15SJohn Edward Broadbent                         // Object path without the
14767e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
14777e860f15SJohn Edward Broadbent                         // and continue.
14787e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
1479c419c759SEd Tanous                             severity == nullptr || timestamp == nullptr ||
1480c419c759SEd Tanous                             updateTimestamp == nullptr)
14817e860f15SJohn Edward Broadbent                         {
14827e860f15SJohn Edward Broadbent                             continue;
14837e860f15SJohn Edward Broadbent                         }
14847e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
14857e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
14867e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14877e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
14880fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14897e860f15SJohn Edward Broadbent                             std::to_string(*id);
14907e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
14917e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
14927e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
14937e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
14947e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
14957e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
14967e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
14977e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
1498c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*timestamp);
14997e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
1500c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*updateTimestamp);
15017e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
15027e860f15SJohn Edward Broadbent                         {
15037e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
15040fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
15057e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
15067e860f15SJohn Edward Broadbent                         }
15077e860f15SJohn Edward Broadbent                     }
15087e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
15097e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
15107e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
15117e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
15127e860f15SJohn Edward Broadbent                               });
15137e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
15147e860f15SJohn Edward Broadbent                         entriesArray.size();
15157e860f15SJohn Edward Broadbent                 },
15167e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
15177e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15187e860f15SJohn Edward Broadbent         });
15197e860f15SJohn Edward Broadbent }
15207e860f15SJohn Edward Broadbent 
15217e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
15227e860f15SJohn Edward Broadbent {
15237e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15247e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1525ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1526*45ca1b86SEd Tanous         .methods(
1527*45ca1b86SEd Tanous             boost::beast::http::verb::
1528*45ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
15297e860f15SJohn Edward Broadbent                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1530*45ca1b86SEd Tanous                             const std::string& param) {
1531*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
15327e860f15SJohn Edward Broadbent             {
1533*45ca1b86SEd Tanous                 return;
1534*45ca1b86SEd Tanous             }
15357e860f15SJohn Edward Broadbent             std::string entryID = param;
15367e860f15SJohn Edward Broadbent             dbus::utility::escapePathForDbus(entryID);
15377e860f15SJohn Edward Broadbent 
15387e860f15SJohn Edward Broadbent             // DBus implementation of EventLog/Entries
15397e860f15SJohn Edward Broadbent             // Make call to Logging Service to find all log entry objects
15407e860f15SJohn Edward Broadbent             crow::connections::systemBus->async_method_call(
1541b9d36b47SEd Tanous                 [asyncResp,
1542b9d36b47SEd Tanous                  entryID](const boost::system::error_code ec,
1543b9d36b47SEd Tanous                           const dbus::utility::DBusPropertiesMap& resp) {
15447e860f15SJohn Edward Broadbent                     if (ec.value() == EBADR)
15457e860f15SJohn Edward Broadbent                     {
1546*45ca1b86SEd Tanous                         messages::resourceNotFound(asyncResp->res,
1547*45ca1b86SEd Tanous                                                    "EventLogEntry", entryID);
15487e860f15SJohn Edward Broadbent                         return;
15497e860f15SJohn Edward Broadbent                     }
15507e860f15SJohn Edward Broadbent                     if (ec)
15517e860f15SJohn Edward Broadbent                     {
15520fda0f12SGeorge Liu                         BMCWEB_LOG_ERROR
15530fda0f12SGeorge Liu                             << "EventLogEntry (DBus) resp_handler got error "
15547e860f15SJohn Edward Broadbent                             << ec;
15557e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
15567e860f15SJohn Edward Broadbent                         return;
15577e860f15SJohn Edward Broadbent                     }
1558914e2d5dSEd Tanous                     const uint32_t* id = nullptr;
1559c419c759SEd Tanous                     const uint64_t* timestamp = nullptr;
1560c419c759SEd Tanous                     const uint64_t* updateTimestamp = nullptr;
1561914e2d5dSEd Tanous                     const std::string* severity = nullptr;
1562914e2d5dSEd Tanous                     const std::string* message = nullptr;
1563914e2d5dSEd Tanous                     const std::string* filePath = nullptr;
15647e860f15SJohn Edward Broadbent                     bool resolved = false;
15657e860f15SJohn Edward Broadbent 
15669eb808c1SEd Tanous                     for (const auto& propertyMap : resp)
15677e860f15SJohn Edward Broadbent                     {
15687e860f15SJohn Edward Broadbent                         if (propertyMap.first == "Id")
15697e860f15SJohn Edward Broadbent                         {
15707e860f15SJohn Edward Broadbent                             id = std::get_if<uint32_t>(&propertyMap.second);
15717e860f15SJohn Edward Broadbent                         }
15727e860f15SJohn Edward Broadbent                         else if (propertyMap.first == "Timestamp")
15737e860f15SJohn Edward Broadbent                         {
1574c419c759SEd Tanous                             timestamp =
15757e860f15SJohn Edward Broadbent                                 std::get_if<uint64_t>(&propertyMap.second);
1576ebd45906SGeorge Liu                         }
1577d139c236SGeorge Liu                         else if (propertyMap.first == "UpdateTimestamp")
1578d139c236SGeorge Liu                         {
1579ebd45906SGeorge Liu                             updateTimestamp =
1580c419c759SEd Tanous                                 std::get_if<uint64_t>(&propertyMap.second);
1581ebd45906SGeorge Liu                         }
1582cb92c03bSAndrew Geissler                         else if (propertyMap.first == "Severity")
1583cb92c03bSAndrew Geissler                         {
1584*45ca1b86SEd Tanous                             severity =
1585*45ca1b86SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
1586cb92c03bSAndrew Geissler                         }
1587cb92c03bSAndrew Geissler                         else if (propertyMap.first == "Message")
1588cb92c03bSAndrew Geissler                         {
1589*45ca1b86SEd Tanous                             message =
1590*45ca1b86SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
1591ae34c8e8SAdriana Kobylak                         }
159275710de2SXiaochao Ma                         else if (propertyMap.first == "Resolved")
159375710de2SXiaochao Ma                         {
1594914e2d5dSEd Tanous                             const bool* resolveptr =
159575710de2SXiaochao Ma                                 std::get_if<bool>(&propertyMap.second);
159675710de2SXiaochao Ma                             if (resolveptr == nullptr)
159775710de2SXiaochao Ma                             {
159875710de2SXiaochao Ma                                 messages::internalError(asyncResp->res);
159975710de2SXiaochao Ma                                 return;
160075710de2SXiaochao Ma                             }
160175710de2SXiaochao Ma                             resolved = *resolveptr;
160275710de2SXiaochao Ma                         }
16037e860f15SJohn Edward Broadbent                         else if (propertyMap.first == "Path")
1604f86bb901SAdriana Kobylak                         {
1605*45ca1b86SEd Tanous                             filePath =
1606*45ca1b86SEd Tanous                                 std::get_if<std::string>(&propertyMap.second);
1607f86bb901SAdriana Kobylak                         }
1608f86bb901SAdriana Kobylak                     }
1609f86bb901SAdriana Kobylak                     if (id == nullptr || message == nullptr ||
1610c419c759SEd Tanous                         severity == nullptr || timestamp == nullptr ||
1611c419c759SEd Tanous                         updateTimestamp == nullptr)
1612f86bb901SAdriana Kobylak                     {
1613ae34c8e8SAdriana Kobylak                         messages::internalError(asyncResp->res);
1614271584abSEd Tanous                         return;
1615271584abSEd Tanous                     }
1616f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["@odata.type"] =
1617f86bb901SAdriana Kobylak                         "#LogEntry.v1_8_0.LogEntry";
1618f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["@odata.id"] =
16190fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1620f86bb901SAdriana Kobylak                         std::to_string(*id);
1621*45ca1b86SEd Tanous                     asyncResp->res.jsonValue["Name"] = "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"] =
1629c419c759SEd Tanous                         crow::utility::getDateTimeUintMs(*timestamp);
1630f86bb901SAdriana Kobylak                     asyncResp->res.jsonValue["Modified"] =
1631c419c759SEd Tanous                         crow::utility::getDateTimeUintMs(*updateTimestamp);
1632f86bb901SAdriana Kobylak                     if (filePath != nullptr)
1633f86bb901SAdriana Kobylak                     {
1634f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["AdditionalDataURI"] =
16350fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
16367e860f15SJohn Edward Broadbent                             std::to_string(*id);
1637f86bb901SAdriana Kobylak                     }
1638cb92c03bSAndrew Geissler                 },
1639cb92c03bSAndrew Geissler                 "xyz.openbmc_project.Logging",
1640cb92c03bSAndrew Geissler                 "/xyz/openbmc_project/logging/entry/" + entryID,
1641f86bb901SAdriana Kobylak                 "org.freedesktop.DBus.Properties", "GetAll", "");
16427e860f15SJohn Edward Broadbent         });
1643336e96c6SChicago Duan 
16447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16457e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1646ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
1648*45ca1b86SEd Tanous             [&app](const crow::Request& req,
16497e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16507e860f15SJohn Edward Broadbent                    const std::string& entryId) {
1651*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1652*45ca1b86SEd Tanous                 {
1653*45ca1b86SEd Tanous                     return;
1654*45ca1b86SEd Tanous                 }
165575710de2SXiaochao Ma                 std::optional<bool> resolved;
165675710de2SXiaochao Ma 
165715ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
16587e860f15SJohn Edward Broadbent                                               resolved))
165975710de2SXiaochao Ma                 {
166075710de2SXiaochao Ma                     return;
166175710de2SXiaochao Ma                 }
166275710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
166375710de2SXiaochao Ma 
166475710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
16654f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
166675710de2SXiaochao Ma                         if (ec)
166775710de2SXiaochao Ma                         {
166875710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
166975710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
167075710de2SXiaochao Ma                             return;
167175710de2SXiaochao Ma                         }
167275710de2SXiaochao Ma                     },
167375710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
167475710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
167575710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
167675710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
1677168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*resolved));
16787e860f15SJohn Edward Broadbent             });
167975710de2SXiaochao Ma 
16807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16817e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1682ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1683ed398213SEd Tanous 
1684*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::
1685*45ca1b86SEd Tanous                      delete_)([&app](const crow::Request& req,
1686*45ca1b86SEd Tanous                                      const std::shared_ptr<bmcweb::AsyncResp>&
1687*45ca1b86SEd Tanous                                          asyncResp,
1688*45ca1b86SEd Tanous                                      const std::string& param) {
1689*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1690336e96c6SChicago Duan             {
1691*45ca1b86SEd Tanous                 return;
1692*45ca1b86SEd Tanous             }
1693336e96c6SChicago Duan             BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1694336e96c6SChicago Duan 
16957e860f15SJohn Edward Broadbent             std::string entryID = param;
1696336e96c6SChicago Duan 
1697336e96c6SChicago Duan             dbus::utility::escapePathForDbus(entryID);
1698336e96c6SChicago Duan 
1699336e96c6SChicago Duan             // Process response from Logging service.
1700*45ca1b86SEd Tanous             auto respHandler = [asyncResp,
1701*45ca1b86SEd Tanous                                 entryID](const boost::system::error_code ec) {
17027e860f15SJohn Edward Broadbent                 BMCWEB_LOG_DEBUG
17037e860f15SJohn Edward Broadbent                     << "EventLogEntry (DBus) doDelete callback: Done";
1704336e96c6SChicago Duan                 if (ec)
1705336e96c6SChicago Duan                 {
17063de8d8baSGeorge Liu                     if (ec.value() == EBADR)
17073de8d8baSGeorge Liu                     {
1708*45ca1b86SEd Tanous                         messages::resourceNotFound(asyncResp->res, "LogEntry",
1709*45ca1b86SEd Tanous                                                    entryID);
17103de8d8baSGeorge Liu                         return;
17113de8d8baSGeorge Liu                     }
1712336e96c6SChicago Duan                     // TODO Handle for specific error code
17130fda0f12SGeorge Liu                     BMCWEB_LOG_ERROR
17140fda0f12SGeorge Liu                         << "EventLogEntry (DBus) doDelete respHandler got error "
1715336e96c6SChicago Duan                         << ec;
1716336e96c6SChicago Duan                     asyncResp->res.result(
1717336e96c6SChicago Duan                         boost::beast::http::status::internal_server_error);
1718336e96c6SChicago Duan                     return;
1719336e96c6SChicago Duan                 }
1720336e96c6SChicago Duan 
1721336e96c6SChicago Duan                 asyncResp->res.result(boost::beast::http::status::ok);
1722336e96c6SChicago Duan             };
1723336e96c6SChicago Duan 
1724336e96c6SChicago Duan             // Make call to Logging service to request Delete Log
1725336e96c6SChicago Duan             crow::connections::systemBus->async_method_call(
1726336e96c6SChicago Duan                 respHandler, "xyz.openbmc_project.Logging",
1727336e96c6SChicago Duan                 "/xyz/openbmc_project/logging/entry/" + entryID,
1728336e96c6SChicago Duan                 "xyz.openbmc_project.Object.Delete", "Delete");
17297e860f15SJohn Edward Broadbent         });
1730400fd1fbSAdriana Kobylak }
1731400fd1fbSAdriana Kobylak 
17327e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1733400fd1fbSAdriana Kobylak {
17340fda0f12SGeorge Liu     BMCWEB_ROUTE(
17350fda0f12SGeorge Liu         app,
17360fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1737ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17387e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
1739*45ca1b86SEd Tanous             [&app](const crow::Request& req,
17407e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1741*45ca1b86SEd Tanous                    const std::string& param) {
1742*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
17437e860f15SJohn Edward Broadbent                 {
1744*45ca1b86SEd Tanous                     return;
1745*45ca1b86SEd Tanous                 }
1746647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
1747647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
1748400fd1fbSAdriana Kobylak                 {
17497e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17507e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1751400fd1fbSAdriana Kobylak                     return;
1752400fd1fbSAdriana Kobylak                 }
1753400fd1fbSAdriana Kobylak 
17547e860f15SJohn Edward Broadbent                 std::string entryID = param;
1755400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1756400fd1fbSAdriana Kobylak 
1757400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17587e860f15SJohn Edward Broadbent                     [asyncResp,
17597e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1760400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1761400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1762400fd1fbSAdriana Kobylak                         {
17637e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
17647e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1765400fd1fbSAdriana Kobylak                             return;
1766400fd1fbSAdriana Kobylak                         }
1767400fd1fbSAdriana Kobylak                         if (ec)
1768400fd1fbSAdriana Kobylak                         {
1769400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1770400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1771400fd1fbSAdriana Kobylak                             return;
1772400fd1fbSAdriana Kobylak                         }
1773400fd1fbSAdriana Kobylak 
1774400fd1fbSAdriana Kobylak                         int fd = -1;
1775400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1776400fd1fbSAdriana Kobylak                         if (fd == -1)
1777400fd1fbSAdriana Kobylak                         {
1778400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1779400fd1fbSAdriana Kobylak                             return;
1780400fd1fbSAdriana Kobylak                         }
1781400fd1fbSAdriana Kobylak 
1782400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1783400fd1fbSAdriana Kobylak                         if (size == -1)
1784400fd1fbSAdriana Kobylak                         {
1785400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1786400fd1fbSAdriana Kobylak                             return;
1787400fd1fbSAdriana Kobylak                         }
1788400fd1fbSAdriana Kobylak 
1789400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1790400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1791400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1792400fd1fbSAdriana Kobylak                         {
1793400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1794400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1795400fd1fbSAdriana Kobylak                                 << maxFileSize;
1796400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1797400fd1fbSAdriana Kobylak                             return;
1798400fd1fbSAdriana Kobylak                         }
1799400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1800400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1801400fd1fbSAdriana Kobylak                         if (rc == -1)
1802400fd1fbSAdriana Kobylak                         {
1803400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1804400fd1fbSAdriana Kobylak                             return;
1805400fd1fbSAdriana Kobylak                         }
1806400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1807400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1808400fd1fbSAdriana Kobylak                         {
1809400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1810400fd1fbSAdriana Kobylak                             return;
1811400fd1fbSAdriana Kobylak                         }
1812400fd1fbSAdriana Kobylak                         close(fd);
1813400fd1fbSAdriana Kobylak 
1814400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
18157e860f15SJohn Edward Broadbent                         std::string output =
18167e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1817400fd1fbSAdriana Kobylak 
1818400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1819400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
18207e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
18217e860f15SJohn Edward Broadbent                                                  "Base64");
1822400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1823400fd1fbSAdriana Kobylak                     },
1824400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1825400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1826400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
18277e860f15SJohn Edward Broadbent             });
18281da66f75SEd Tanous }
18291da66f75SEd Tanous 
1830b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1831b7028ebfSSpencer Ku 
1832b7028ebfSSpencer Ku inline bool
1833b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1834b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1835b7028ebfSSpencer Ku {
1836b7028ebfSSpencer Ku     std::error_code ec;
1837b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1838b7028ebfSSpencer Ku     if (ec)
1839b7028ebfSSpencer Ku     {
1840b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1841b7028ebfSSpencer Ku         return false;
1842b7028ebfSSpencer Ku     }
1843b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1844b7028ebfSSpencer Ku     {
1845b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1846b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1847b7028ebfSSpencer Ku         // path
1848b7028ebfSSpencer Ku         if (boost::starts_with(filename, "log"))
1849b7028ebfSSpencer Ku         {
1850b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1851b7028ebfSSpencer Ku         }
1852b7028ebfSSpencer Ku     }
1853b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1854b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1855b7028ebfSSpencer Ku     // descending order.
1856b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1857b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1858b7028ebfSSpencer Ku 
1859b7028ebfSSpencer Ku     return true;
1860b7028ebfSSpencer Ku }
1861b7028ebfSSpencer Ku 
1862b7028ebfSSpencer Ku inline bool
1863b7028ebfSSpencer Ku     getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1864b7028ebfSSpencer Ku                          uint64_t& skip, uint64_t& top,
1865b7028ebfSSpencer Ku                          std::vector<std::string>& logEntries, size_t& logCount)
1866b7028ebfSSpencer Ku {
1867b7028ebfSSpencer Ku     GzFileReader logFile;
1868b7028ebfSSpencer Ku 
1869b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1870b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1871b7028ebfSSpencer Ku     {
1872b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1873b7028ebfSSpencer Ku         {
1874b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1875b7028ebfSSpencer Ku             return false;
1876b7028ebfSSpencer Ku         }
1877b7028ebfSSpencer Ku     }
1878b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1879b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1880b7028ebfSSpencer Ku     if (!lastMessage.empty())
1881b7028ebfSSpencer Ku     {
1882b7028ebfSSpencer Ku         logCount++;
1883b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1884b7028ebfSSpencer Ku         {
1885b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1886b7028ebfSSpencer Ku         }
1887b7028ebfSSpencer Ku     }
1888b7028ebfSSpencer Ku     return true;
1889b7028ebfSSpencer Ku }
1890b7028ebfSSpencer Ku 
1891b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1892b7028ebfSSpencer Ku                                     const std::string& msg,
1893b7028ebfSSpencer Ku                                     nlohmann::json& logEntryJson)
1894b7028ebfSSpencer Ku {
1895b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
1896b7028ebfSSpencer Ku     logEntryJson = {
1897b7028ebfSSpencer Ku         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1898b7028ebfSSpencer Ku         {"@odata.id",
1899b7028ebfSSpencer Ku          "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1900b7028ebfSSpencer Ku              logEntryID},
1901b7028ebfSSpencer Ku         {"Name", "Host Logger Entry"},
1902b7028ebfSSpencer Ku         {"Id", logEntryID},
1903b7028ebfSSpencer Ku         {"Message", msg},
1904b7028ebfSSpencer Ku         {"EntryType", "Oem"},
1905b7028ebfSSpencer Ku         {"Severity", "OK"},
1906b7028ebfSSpencer Ku         {"OemRecordFormat", "Host Logger Entry"}};
1907b7028ebfSSpencer Ku }
1908b7028ebfSSpencer Ku 
1909b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1910b7028ebfSSpencer Ku {
1911b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1912b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
1913*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1914*45ca1b86SEd Tanous                                                        const std::shared_ptr<
1915*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
1916*45ca1b86SEd Tanous                                                            asyncResp) {
1917*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1918*45ca1b86SEd Tanous             {
1919*45ca1b86SEd Tanous                 return;
1920*45ca1b86SEd Tanous             }
1921b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1922b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger";
1923b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1924b7028ebfSSpencer Ku                 "#LogService.v1_1_0.LogService";
1925b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1926b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1927b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Id"] = "HostLogger";
1928b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Entries"] = {
19290fda0f12SGeorge Liu                 {"@odata.id",
19300fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1931b7028ebfSSpencer Ku         });
1932b7028ebfSSpencer Ku }
1933b7028ebfSSpencer Ku 
1934b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1935b7028ebfSSpencer Ku {
1936b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1937b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1938b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1939*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
1940*45ca1b86SEd Tanous                                                        const std::shared_ptr<
1941*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
1942*45ca1b86SEd Tanous                                                            asyncResp) {
1943*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
1944*45ca1b86SEd Tanous             {
1945*45ca1b86SEd Tanous                 return;
1946*45ca1b86SEd Tanous             }
1947b7028ebfSSpencer Ku             uint64_t skip = 0;
1948b7028ebfSSpencer Ku             uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1949b7028ebfSSpencer Ku                                               // default, allow range 1 to
1950b7028ebfSSpencer Ku                                               // 1000 entries per page.
1951b7028ebfSSpencer Ku             if (!getSkipParam(asyncResp, req, skip))
1952b7028ebfSSpencer Ku             {
1953b7028ebfSSpencer Ku                 return;
1954b7028ebfSSpencer Ku             }
1955b7028ebfSSpencer Ku             if (!getTopParam(asyncResp, req, top))
1956b7028ebfSSpencer Ku             {
1957b7028ebfSSpencer Ku                 return;
1958b7028ebfSSpencer Ku             }
1959b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1960b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1961b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1962b7028ebfSSpencer Ku                 "#LogEntryCollection.LogEntryCollection";
1963b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1964b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] =
1965b7028ebfSSpencer Ku                 "Collection of HostLogger Entries";
19660fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1967b7028ebfSSpencer Ku             logEntryArray = nlohmann::json::array();
1968b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = 0;
1969b7028ebfSSpencer Ku 
1970b7028ebfSSpencer Ku             std::vector<std::filesystem::path> hostLoggerFiles;
1971b7028ebfSSpencer Ku             if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1972b7028ebfSSpencer Ku             {
1973b7028ebfSSpencer Ku                 BMCWEB_LOG_ERROR << "fail to get host log file path";
1974b7028ebfSSpencer Ku                 return;
1975b7028ebfSSpencer Ku             }
1976b7028ebfSSpencer Ku 
1977b7028ebfSSpencer Ku             size_t logCount = 0;
1978b7028ebfSSpencer Ku             // This vector only store the entries we want to expose that
1979b7028ebfSSpencer Ku             // control by skip and top.
1980b7028ebfSSpencer Ku             std::vector<std::string> logEntries;
19810fda0f12SGeorge Liu             if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
19820fda0f12SGeorge Liu                                       logCount))
1983b7028ebfSSpencer Ku             {
1984b7028ebfSSpencer Ku                 messages::internalError(asyncResp->res);
1985b7028ebfSSpencer Ku                 return;
1986b7028ebfSSpencer Ku             }
1987b7028ebfSSpencer Ku             // If vector is empty, that means skip value larger than total
1988b7028ebfSSpencer Ku             // log count
198926f6976fSEd Tanous             if (logEntries.empty())
1990b7028ebfSSpencer Ku             {
1991b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1992b7028ebfSSpencer Ku                 return;
1993b7028ebfSSpencer Ku             }
199426f6976fSEd Tanous             if (!logEntries.empty())
1995b7028ebfSSpencer Ku             {
1996b7028ebfSSpencer Ku                 for (size_t i = 0; i < logEntries.size(); i++)
1997b7028ebfSSpencer Ku                 {
1998b7028ebfSSpencer Ku                     logEntryArray.push_back({});
1999b7028ebfSSpencer Ku                     nlohmann::json& hostLogEntry = logEntryArray.back();
2000b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(std::to_string(skip + i),
2001b7028ebfSSpencer Ku                                             logEntries[i], hostLogEntry);
2002b7028ebfSSpencer Ku                 }
2003b7028ebfSSpencer Ku 
2004b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2005b7028ebfSSpencer Ku                 if (skip + top < logCount)
2006b7028ebfSSpencer Ku                 {
2007b7028ebfSSpencer Ku                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
20080fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
2009b7028ebfSSpencer Ku                         std::to_string(skip + top);
2010b7028ebfSSpencer Ku                 }
2011b7028ebfSSpencer Ku             }
2012b7028ebfSSpencer Ku         });
2013b7028ebfSSpencer Ku }
2014b7028ebfSSpencer Ku 
2015b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2016b7028ebfSSpencer Ku {
2017b7028ebfSSpencer Ku     BMCWEB_ROUTE(
2018b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2019b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2020b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
2021*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2022b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2023b7028ebfSSpencer Ku                    const std::string& param) {
2024*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2025*45ca1b86SEd Tanous                 {
2026*45ca1b86SEd Tanous                     return;
2027*45ca1b86SEd Tanous                 }
2028b7028ebfSSpencer Ku                 const std::string& targetID = param;
2029b7028ebfSSpencer Ku 
2030b7028ebfSSpencer Ku                 uint64_t idInt = 0;
2031ca45aa3cSEd Tanous 
2032ca45aa3cSEd Tanous                 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2033ca45aa3cSEd Tanous                 const char* end = targetID.data() + targetID.size();
2034ca45aa3cSEd Tanous 
2035ca45aa3cSEd Tanous                 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
2036b7028ebfSSpencer Ku                 if (ec == std::errc::invalid_argument)
2037b7028ebfSSpencer Ku                 {
2038ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2039b7028ebfSSpencer Ku                     return;
2040b7028ebfSSpencer Ku                 }
2041b7028ebfSSpencer Ku                 if (ec == std::errc::result_out_of_range)
2042b7028ebfSSpencer Ku                 {
2043ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2044b7028ebfSSpencer Ku                     return;
2045b7028ebfSSpencer Ku                 }
2046b7028ebfSSpencer Ku 
2047b7028ebfSSpencer Ku                 std::vector<std::filesystem::path> hostLoggerFiles;
2048b7028ebfSSpencer Ku                 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2049b7028ebfSSpencer Ku                 {
2050b7028ebfSSpencer Ku                     BMCWEB_LOG_ERROR << "fail to get host log file path";
2051b7028ebfSSpencer Ku                     return;
2052b7028ebfSSpencer Ku                 }
2053b7028ebfSSpencer Ku 
2054b7028ebfSSpencer Ku                 size_t logCount = 0;
2055b7028ebfSSpencer Ku                 uint64_t top = 1;
2056b7028ebfSSpencer Ku                 std::vector<std::string> logEntries;
2057b7028ebfSSpencer Ku                 // We can get specific entry by skip and top. For example, if we
2058b7028ebfSSpencer Ku                 // want to get nth entry, we can set skip = n-1 and top = 1 to
2059b7028ebfSSpencer Ku                 // get that entry
2060b7028ebfSSpencer Ku                 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2061b7028ebfSSpencer Ku                                           logEntries, logCount))
2062b7028ebfSSpencer Ku                 {
2063b7028ebfSSpencer Ku                     messages::internalError(asyncResp->res);
2064b7028ebfSSpencer Ku                     return;
2065b7028ebfSSpencer Ku                 }
2066b7028ebfSSpencer Ku 
2067b7028ebfSSpencer Ku                 if (!logEntries.empty())
2068b7028ebfSSpencer Ku                 {
2069b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(targetID, logEntries[0],
2070b7028ebfSSpencer Ku                                             asyncResp->res.jsonValue);
2071b7028ebfSSpencer Ku                     return;
2072b7028ebfSSpencer Ku                 }
2073b7028ebfSSpencer Ku 
2074b7028ebfSSpencer Ku                 // Requested ID was not found
2075ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2076b7028ebfSSpencer Ku             });
2077b7028ebfSSpencer Ku }
2078b7028ebfSSpencer Ku 
20797e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
20801da66f75SEd Tanous {
20817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2082ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
20837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2084*45ca1b86SEd Tanous             [&app](const crow::Request& req,
20857e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2086*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2087*45ca1b86SEd Tanous                 {
2088*45ca1b86SEd Tanous                     return;
2089*45ca1b86SEd Tanous                 }
20907e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
20917e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
2092e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
20931da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
2094e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
2095e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
20967e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
20977e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
2098e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
20991da66f75SEd Tanous                     "Collection of LogServices for this Manager";
21007e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
21017e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2102c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
21035cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
21045cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
21057e860f15SJohn Edward Broadbent                     {{"@odata.id",
21067e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
21075cb1dd27SAsmitha Karunanithi #endif
2108c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2109c4bf6374SJason M. Bills                 logServiceArray.push_back(
21107e860f15SJohn Edward Broadbent                     {{"@odata.id",
21117e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2112c4bf6374SJason M. Bills #endif
2113e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2114c4bf6374SJason M. Bills                     logServiceArray.size();
21157e860f15SJohn Edward Broadbent             });
2116e1f26343SJason M. Bills }
2117e1f26343SJason M. Bills 
21187e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2119e1f26343SJason M. Bills {
21207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2121ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
21227e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2123*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2124*45ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2125*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
21267e860f15SJohn Edward Broadbent                 {
2127*45ca1b86SEd Tanous                     return;
2128*45ca1b86SEd Tanous                 }
2129e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
2130e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
21310f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
21320f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
21337e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
21347e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
21357e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
21367e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
2137c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2138e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
21397c8c4058STejas Patil 
21407c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
21417c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
21427c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
21437c8c4058STejas Patil                     redfishDateTimeOffset.first;
21447c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21457c8c4058STejas Patil                     redfishDateTimeOffset.second;
21467c8c4058STejas Patil 
2147cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
2148cd50aa42SJason M. Bills                     {"@odata.id",
2149086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
21507e860f15SJohn Edward Broadbent             });
2151e1f26343SJason M. Bills }
2152e1f26343SJason M. Bills 
2153c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2154e1f26343SJason M. Bills                                       sd_journal* journal,
2155c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
2156e1f26343SJason M. Bills {
2157e1f26343SJason M. Bills     // Get the Log Entry contents
2158e1f26343SJason M. Bills     int ret = 0;
2159e1f26343SJason M. Bills 
2160a8fe54f0SJason M. Bills     std::string message;
2161a8fe54f0SJason M. Bills     std::string_view syslogID;
2162a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2163a8fe54f0SJason M. Bills     if (ret < 0)
2164a8fe54f0SJason M. Bills     {
2165a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2166a8fe54f0SJason M. Bills                          << strerror(-ret);
2167a8fe54f0SJason M. Bills     }
2168a8fe54f0SJason M. Bills     if (!syslogID.empty())
2169a8fe54f0SJason M. Bills     {
2170a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2171a8fe54f0SJason M. Bills     }
2172a8fe54f0SJason M. Bills 
217339e77504SEd Tanous     std::string_view msg;
217416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2175e1f26343SJason M. Bills     if (ret < 0)
2176e1f26343SJason M. Bills     {
2177e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2178e1f26343SJason M. Bills         return 1;
2179e1f26343SJason M. Bills     }
2180a8fe54f0SJason M. Bills     message += std::string(msg);
2181e1f26343SJason M. Bills 
2182e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2183271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
218416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2185e1f26343SJason M. Bills     if (ret < 0)
2186e1f26343SJason M. Bills     {
2187e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2188e1f26343SJason M. Bills     }
2189e1f26343SJason M. Bills 
2190e1f26343SJason M. Bills     // Get the Created time from the timestamp
219116428a1aSJason M. Bills     std::string entryTimeStr;
219216428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2193e1f26343SJason M. Bills     {
219416428a1aSJason M. Bills         return 1;
2195e1f26343SJason M. Bills     }
2196e1f26343SJason M. Bills 
2197e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2198c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2199647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2200c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2201c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2202e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2203c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2204a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2205e1f26343SJason M. Bills         {"EntryType", "Oem"},
2206738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2207738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2208738c1e61SPatrick Williams                                      : "OK"},
2209086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2210e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2211e1f26343SJason M. Bills     return 0;
2212e1f26343SJason M. Bills }
2213e1f26343SJason M. Bills 
22147e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2215e1f26343SJason M. Bills {
22167e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2217ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2218*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
2219*45ca1b86SEd Tanous                                                        const std::shared_ptr<
2220*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
2221*45ca1b86SEd Tanous                                                            asyncResp) {
2222*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2223*45ca1b86SEd Tanous             {
2224*45ca1b86SEd Tanous                 return;
2225*45ca1b86SEd Tanous             }
2226193ad2faSJason M. Bills             static constexpr const long maxEntriesPerPage = 1000;
2227271584abSEd Tanous             uint64_t skip = 0;
2228271584abSEd Tanous             uint64_t top = maxEntriesPerPage; // Show max entries by default
22298d1b46d7Szhanghch05             if (!getSkipParam(asyncResp, req, skip))
2230193ad2faSJason M. Bills             {
2231193ad2faSJason M. Bills                 return;
2232193ad2faSJason M. Bills             }
22338d1b46d7Szhanghch05             if (!getTopParam(asyncResp, req, top))
2234193ad2faSJason M. Bills             {
2235193ad2faSJason M. Bills                 return;
2236193ad2faSJason M. Bills             }
22377e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
22387e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2239e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
2240e1f26343SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
22410f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
22420f74e643SEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2243e1f26343SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2244e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2245e1f26343SJason M. Bills                 "Collection of BMC Journal Entries";
22460fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2247e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2248e1f26343SJason M. Bills 
22497e860f15SJohn Edward Broadbent             // Go through the journal and use the timestamp to create a
22507e860f15SJohn Edward Broadbent             // unique ID for each entry
2251e1f26343SJason M. Bills             sd_journal* journalTmp = nullptr;
2252e1f26343SJason M. Bills             int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2253e1f26343SJason M. Bills             if (ret < 0)
2254e1f26343SJason M. Bills             {
22557e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "failed to open journal: "
22567e860f15SJohn Edward Broadbent                                  << strerror(-ret);
2257f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2258e1f26343SJason M. Bills                 return;
2259e1f26343SJason M. Bills             }
22600fda0f12SGeorge Liu             std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22610fda0f12SGeorge Liu                 journalTmp, sd_journal_close);
2262e1f26343SJason M. Bills             journalTmp = nullptr;
2263b01bf299SEd Tanous             uint64_t entryCount = 0;
2264e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
2265e85d6b16SJason M. Bills             bool firstEntry = true;
2266e1f26343SJason M. Bills             SD_JOURNAL_FOREACH(journal.get())
2267e1f26343SJason M. Bills             {
2268193ad2faSJason M. Bills                 entryCount++;
22697e860f15SJohn Edward Broadbent                 // Handle paging using skip (number of entries to skip from
22707e860f15SJohn Edward Broadbent                 // the start) and top (number of entries to display)
2271193ad2faSJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
2272193ad2faSJason M. Bills                 {
2273193ad2faSJason M. Bills                     continue;
2274193ad2faSJason M. Bills                 }
2275193ad2faSJason M. Bills 
227616428a1aSJason M. Bills                 std::string idStr;
2277e85d6b16SJason M. Bills                 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2278e1f26343SJason M. Bills                 {
2279e1f26343SJason M. Bills                     continue;
2280e1f26343SJason M. Bills                 }
2281e1f26343SJason M. Bills 
2282e85d6b16SJason M. Bills                 if (firstEntry)
2283e85d6b16SJason M. Bills                 {
2284e85d6b16SJason M. Bills                     firstEntry = false;
2285e85d6b16SJason M. Bills                 }
2286e85d6b16SJason M. Bills 
2287e1f26343SJason M. Bills                 logEntryArray.push_back({});
2288c4bf6374SJason M. Bills                 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2289c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2290c4bf6374SJason M. Bills                                                bmcJournalLogEntry) != 0)
2291e1f26343SJason M. Bills                 {
2292f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2293e1f26343SJason M. Bills                     return;
2294e1f26343SJason M. Bills                 }
2295e1f26343SJason M. Bills             }
2296193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2297193ad2faSJason M. Bills             if (skip + top < entryCount)
2298193ad2faSJason M. Bills             {
2299193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
23000fda0f12SGeorge Liu                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2301193ad2faSJason M. Bills                     std::to_string(skip + top);
2302193ad2faSJason M. Bills             }
23037e860f15SJohn Edward Broadbent         });
2304e1f26343SJason M. Bills }
2305e1f26343SJason M. Bills 
23067e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2307e1f26343SJason M. Bills {
23087e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23097e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2310ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
23117e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2312*45ca1b86SEd Tanous             [&app](const crow::Request& req,
23137e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23147e860f15SJohn Edward Broadbent                    const std::string& entryID) {
2315*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2316*45ca1b86SEd Tanous                 {
2317*45ca1b86SEd Tanous                     return;
2318*45ca1b86SEd Tanous                 }
2319e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2320e1f26343SJason M. Bills                 uint64_t ts = 0;
2321271584abSEd Tanous                 uint64_t index = 0;
23228d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2323e1f26343SJason M. Bills                 {
232416428a1aSJason M. Bills                     return;
2325e1f26343SJason M. Bills                 }
2326e1f26343SJason M. Bills 
2327e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2328e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2329e1f26343SJason M. Bills                 if (ret < 0)
2330e1f26343SJason M. Bills                 {
23317e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
23327e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2333f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2334e1f26343SJason M. Bills                     return;
2335e1f26343SJason M. Bills                 }
23367e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
23377e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2338e1f26343SJason M. Bills                 journalTmp = nullptr;
23397e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
23407e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2341af07e3f5SJason M. Bills                 std::string idStr;
2342af07e3f5SJason M. Bills                 bool firstEntry = true;
2343e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
23442056b6d1SManojkiran Eda                 if (ret < 0)
23452056b6d1SManojkiran Eda                 {
23462056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
23472056b6d1SManojkiran Eda                                      << strerror(-ret);
23482056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
23492056b6d1SManojkiran Eda                     return;
23502056b6d1SManojkiran Eda                 }
2351271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2352e1f26343SJason M. Bills                 {
2353e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2354af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2355af07e3f5SJason M. Bills                     {
2356af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2357af07e3f5SJason M. Bills                         return;
2358af07e3f5SJason M. Bills                     }
2359af07e3f5SJason M. Bills                     if (firstEntry)
2360af07e3f5SJason M. Bills                     {
2361af07e3f5SJason M. Bills                         firstEntry = false;
2362af07e3f5SJason M. Bills                     }
2363e1f26343SJason M. Bills                 }
2364c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2365af07e3f5SJason M. Bills                 if (idStr != entryID)
2366c4bf6374SJason M. Bills                 {
2367ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2368c4bf6374SJason M. Bills                     return;
2369c4bf6374SJason M. Bills                 }
2370c4bf6374SJason M. Bills 
2371c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2372e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2373e1f26343SJason M. Bills                 {
2374f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2375e1f26343SJason M. Bills                     return;
2376e1f26343SJason M. Bills                 }
23777e860f15SJohn Edward Broadbent             });
2378c9bb6861Sraviteja-b }
2379c9bb6861Sraviteja-b 
23807e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2381c9bb6861Sraviteja-b {
23827e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2383ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
2384*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
2385*45ca1b86SEd Tanous                                                        const std::shared_ptr<
2386*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
2387*45ca1b86SEd Tanous                                                            asyncResp) {
2388*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2389*45ca1b86SEd Tanous             {
2390*45ca1b86SEd Tanous                 return;
2391*45ca1b86SEd Tanous             }
2392c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.id"] =
23935cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Managers/bmc/LogServices/Dump";
2394c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.type"] =
2395d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
2396c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Name"] = "Dump LogService";
23975cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
23985cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
2399c9bb6861Sraviteja-b             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24007c8c4058STejas Patil 
24017c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
24027c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
24030fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
24047c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24057c8c4058STejas Patil                 redfishDateTimeOffset.second;
24067c8c4058STejas Patil 
2407c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Entries"] = {
24087e860f15SJohn Edward Broadbent                 {"@odata.id",
24097e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
24105cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
24115cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
24120fda0f12SGeorge Liu                  {{"target",
24130fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2414d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
24150fda0f12SGeorge Liu                  {{"target",
24160fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
24177e860f15SJohn Edward Broadbent         });
2418c9bb6861Sraviteja-b }
2419c9bb6861Sraviteja-b 
24207e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
24217e860f15SJohn Edward Broadbent {
24227e860f15SJohn Edward Broadbent 
2423c9bb6861Sraviteja-b     /**
2424c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2425c9bb6861Sraviteja-b      */
24267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2427ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
24287e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2429*45ca1b86SEd Tanous             [&app](const crow::Request& req,
24307e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2431*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2432*45ca1b86SEd Tanous                 {
2433*45ca1b86SEd Tanous                     return;
2434*45ca1b86SEd Tanous                 }
2435c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2436c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2437c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
24385cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
24395cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2440c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
24415cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2442c9bb6861Sraviteja-b 
24435cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
24447e860f15SJohn Edward Broadbent             });
2445c9bb6861Sraviteja-b }
2446c9bb6861Sraviteja-b 
24477e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2448c9bb6861Sraviteja-b {
24497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24507e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2451ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
24527e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2453*45ca1b86SEd Tanous             [&app](const crow::Request& req,
24547e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24557e860f15SJohn Edward Broadbent                    const std::string& param) {
2456*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2457*45ca1b86SEd Tanous                 {
2458*45ca1b86SEd Tanous                     return;
2459*45ca1b86SEd Tanous                 }
2460*45ca1b86SEd Tanous 
2461*45ca1b86SEd Tanous                 getDumpEntryById(app, req, asyncResp, param, "BMC");
24627e860f15SJohn Edward Broadbent             });
24637e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24647e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2465ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
24667e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
2467*45ca1b86SEd Tanous             [&app](const crow::Request& req,
24687e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24697e860f15SJohn Edward Broadbent                    const std::string& param) {
2470*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2471*45ca1b86SEd Tanous                 {
2472*45ca1b86SEd Tanous                     return;
2473*45ca1b86SEd Tanous                 }
24747e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
24757e860f15SJohn Edward Broadbent             });
2476c9bb6861Sraviteja-b }
2477c9bb6861Sraviteja-b 
24787e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2479c9bb6861Sraviteja-b {
24800fda0f12SGeorge Liu     BMCWEB_ROUTE(
24810fda0f12SGeorge Liu         app,
24820fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2483ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24847e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2485*45ca1b86SEd Tanous             [&app](const crow::Request& req,
24867e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2487*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2488*45ca1b86SEd Tanous                 {
2489*45ca1b86SEd Tanous                     return;
2490*45ca1b86SEd Tanous                 }
24918d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
24927e860f15SJohn Edward Broadbent             });
2493a43be80fSAsmitha Karunanithi }
2494a43be80fSAsmitha Karunanithi 
24957e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
249680319af1SAsmitha Karunanithi {
24970fda0f12SGeorge Liu     BMCWEB_ROUTE(
24980fda0f12SGeorge Liu         app,
24990fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2500ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25017e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2502*45ca1b86SEd Tanous             [&app](const crow::Request& req,
25037e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2504*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2505*45ca1b86SEd Tanous                 {
2506*45ca1b86SEd Tanous                     return;
2507*45ca1b86SEd Tanous                 }
25088d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
25097e860f15SJohn Edward Broadbent             });
25105cb1dd27SAsmitha Karunanithi }
25115cb1dd27SAsmitha Karunanithi 
25127e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
25135cb1dd27SAsmitha Karunanithi {
25147e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2515ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
2516*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
2517*45ca1b86SEd Tanous                                                        const std::shared_ptr<
2518*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
2519*45ca1b86SEd Tanous                                                            asyncResp) {
2520*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
25217e860f15SJohn Edward Broadbent             {
2522*45ca1b86SEd Tanous                 return;
2523*45ca1b86SEd Tanous             }
25245cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.id"] =
25255cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Systems/system/LogServices/Dump";
25265cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
2527d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
25285cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = "Dump LogService";
2529*45ca1b86SEd Tanous             asyncResp->res.jsonValue["Description"] = "System Dump LogService";
25305cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
25315cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
25327c8c4058STejas Patil 
25337c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
25347c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
2535*45ca1b86SEd Tanous             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25367c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25377c8c4058STejas Patil                 redfishDateTimeOffset.second;
25387c8c4058STejas Patil 
25395cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Entries"] = {
25405cb1dd27SAsmitha Karunanithi                 {"@odata.id",
25415cb1dd27SAsmitha Karunanithi                  "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
25425cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
25435cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
25447e860f15SJohn Edward Broadbent                  {{"target",
25450fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
2546d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
25477e860f15SJohn Edward Broadbent                  {{"target",
25480fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
25497e860f15SJohn Edward Broadbent         });
25505cb1dd27SAsmitha Karunanithi }
25515cb1dd27SAsmitha Karunanithi 
25527e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
25537e860f15SJohn Edward Broadbent {
25547e860f15SJohn Edward Broadbent 
25555cb1dd27SAsmitha Karunanithi     /**
25565cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25575cb1dd27SAsmitha Karunanithi      */
2558b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2559ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25607e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2561*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2562864d6a17SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2563*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2564*45ca1b86SEd Tanous                 {
2565*45ca1b86SEd Tanous                     return;
2566*45ca1b86SEd Tanous                 }
25675cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
25685cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
25695cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
25705cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
25715cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
25725cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
25735cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
25745cb1dd27SAsmitha Karunanithi 
25755cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
25767e860f15SJohn Edward Broadbent             });
25775cb1dd27SAsmitha Karunanithi }
25785cb1dd27SAsmitha Karunanithi 
25797e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
25805cb1dd27SAsmitha Karunanithi {
25817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2582864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2583ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2584ed398213SEd Tanous 
25857e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2586*45ca1b86SEd Tanous             [&app](const crow::Request& req,
25877e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25887e860f15SJohn Edward Broadbent                    const std::string& param) {
2589*45ca1b86SEd Tanous                 getDumpEntryById(app, req, asyncResp, param, "System");
25907e860f15SJohn Edward Broadbent             });
25918d1b46d7Szhanghch05 
25927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2593864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2594ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25957e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
2596*45ca1b86SEd Tanous             [&app](const crow::Request& req,
25977e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25987e860f15SJohn Edward Broadbent                    const std::string& param) {
2599*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2600*45ca1b86SEd Tanous                 {
2601*45ca1b86SEd Tanous                     return;
2602*45ca1b86SEd Tanous                 }
26037e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
26047e860f15SJohn Edward Broadbent             });
26055cb1dd27SAsmitha Karunanithi }
2606c9bb6861Sraviteja-b 
26077e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2608c9bb6861Sraviteja-b {
26090fda0f12SGeorge Liu     BMCWEB_ROUTE(
26100fda0f12SGeorge Liu         app,
26110fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2612ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26137e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2614*45ca1b86SEd Tanous             [&app](const crow::Request& req,
2615*45ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2616*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2617*45ca1b86SEd Tanous                 {
2618*45ca1b86SEd Tanous                     return;
2619*45ca1b86SEd Tanous                 }
2620*45ca1b86SEd Tanous                 createDump(asyncResp, req, "System");
2621*45ca1b86SEd Tanous             });
2622a43be80fSAsmitha Karunanithi }
2623a43be80fSAsmitha Karunanithi 
26247e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2625a43be80fSAsmitha Karunanithi {
26260fda0f12SGeorge Liu     BMCWEB_ROUTE(
26270fda0f12SGeorge Liu         app,
26280fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2629ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26307e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2631*45ca1b86SEd Tanous             [&app](const crow::Request& req,
26327e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26337e860f15SJohn Edward Broadbent 
2634*45ca1b86SEd Tanous             {
2635*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2636*45ca1b86SEd Tanous                 {
2637*45ca1b86SEd Tanous                     return;
2638*45ca1b86SEd Tanous                 }
2639*45ca1b86SEd Tanous                 clearDump(asyncResp, "System");
2640*45ca1b86SEd Tanous             });
2641013487e5Sraviteja-b }
2642013487e5Sraviteja-b 
26437e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
26441da66f75SEd Tanous {
26453946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26463946028dSAppaRao Puli     // method for security reasons.
26471da66f75SEd Tanous     /**
26481da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26491da66f75SEd Tanous      */
26507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2651ed398213SEd Tanous         // This is incorrect, should be:
2652ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2653432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2654*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
2655*45ca1b86SEd Tanous                                                        const std::shared_ptr<
2656*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
2657*45ca1b86SEd Tanous                                                            asyncResp) {
2658*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2659*45ca1b86SEd Tanous             {
2660*45ca1b86SEd Tanous                 return;
2661*45ca1b86SEd Tanous             }
26627e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
26637e860f15SJohn Edward Broadbent             // SubRoute
26640f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2665424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2666e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
26678e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
26684f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26694f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26704f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2671e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2672e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
26737c8c4058STejas Patil 
26747c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
26757c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
26767c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26777c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26787c8c4058STejas Patil                 redfishDateTimeOffset.second;
26797c8c4058STejas Patil 
2680cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2681cd50aa42SJason M. Bills                 {"@odata.id",
2682424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2683e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
26845b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
26850fda0f12SGeorge Liu                  {{"target",
26860fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
26878e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
26880fda0f12SGeorge Liu                  {{"target",
26890fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
26907e860f15SJohn Edward Broadbent         });
26911da66f75SEd Tanous }
26921da66f75SEd Tanous 
26937e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
26945b61b5e8SJason M. Bills {
26950fda0f12SGeorge Liu     BMCWEB_ROUTE(
26960fda0f12SGeorge Liu         app,
26970fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2698ed398213SEd Tanous         // This is incorrect, should be:
2699ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2700432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27017e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2702*45ca1b86SEd Tanous             [&app](const crow::Request& req,
27037e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2704*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2705*45ca1b86SEd Tanous                 {
2706*45ca1b86SEd Tanous                     return;
2707*45ca1b86SEd Tanous                 }
27085b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
27095b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2710cb13a392SEd Tanous                                 const std::string&) {
27115b61b5e8SJason M. Bills                         if (ec)
27125b61b5e8SJason M. Bills                         {
27135b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
27145b61b5e8SJason M. Bills                             return;
27155b61b5e8SJason M. Bills                         }
27165b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
27175b61b5e8SJason M. Bills                     },
27187e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
27197e860f15SJohn Edward Broadbent                     "DeleteAll");
27207e860f15SJohn Edward Broadbent             });
27215b61b5e8SJason M. Bills }
27225b61b5e8SJason M. Bills 
27238d1b46d7Szhanghch05 static void
27248d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27258d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2726e855dd28SJason M. Bills {
2727043a0536SJohnathan Mantey     auto getStoredLogCallback =
2728b9d36b47SEd Tanous         [asyncResp, logID,
2729b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2730b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2731e855dd28SJason M. Bills             if (ec)
2732e855dd28SJason M. Bills             {
2733e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
27341ddcf01aSJason M. Bills                 if (ec.value() ==
27351ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
27361ddcf01aSJason M. Bills                 {
2737043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2738043a0536SJohnathan Mantey                                                logID);
27391ddcf01aSJason M. Bills                 }
27401ddcf01aSJason M. Bills                 else
27411ddcf01aSJason M. Bills                 {
2742e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
27431ddcf01aSJason M. Bills                 }
2744e855dd28SJason M. Bills                 return;
2745e855dd28SJason M. Bills             }
2746043a0536SJohnathan Mantey 
2747043a0536SJohnathan Mantey             std::string timestamp{};
2748043a0536SJohnathan Mantey             std::string filename{};
2749043a0536SJohnathan Mantey             std::string logfile{};
27502c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2751043a0536SJohnathan Mantey 
2752043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2753e855dd28SJason M. Bills             {
2754ace85d60SEd Tanous                 messages::resourceMissingAtURI(
2755ace85d60SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(logID));
2756e855dd28SJason M. Bills                 return;
2757e855dd28SJason M. Bills             }
2758e855dd28SJason M. Bills 
2759043a0536SJohnathan Mantey             std::string crashdumpURI =
2760e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2761043a0536SJohnathan Mantey                 logID + "/" + filename;
27622b20ef6eSJason M. Bills             nlohmann::json logEntry = {
27634978b63fSJason M. Bills                 {"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
27644978b63fSJason M. Bills                 {"@odata.id",
27654978b63fSJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2766e855dd28SJason M. Bills                      logID},
2767e855dd28SJason M. Bills                 {"Name", "CPU Crashdump"},
2768e855dd28SJason M. Bills                 {"Id", logID},
2769e855dd28SJason M. Bills                 {"EntryType", "Oem"},
27708e6c099aSJason M. Bills                 {"AdditionalDataURI", std::move(crashdumpURI)},
27718e6c099aSJason M. Bills                 {"DiagnosticDataType", "OEM"},
27728e6c099aSJason M. Bills                 {"OEMDiagnosticDataType", "PECICrashdump"},
2773043a0536SJohnathan Mantey                 {"Created", std::move(timestamp)}};
27742b20ef6eSJason M. Bills 
27752b20ef6eSJason M. Bills             // If logEntryJson references an array of LogEntry resources
27762b20ef6eSJason M. Bills             // ('Members' list), then push this as a new entry, otherwise set it
27772b20ef6eSJason M. Bills             // directly
27782b20ef6eSJason M. Bills             if (logEntryJson.is_array())
27792b20ef6eSJason M. Bills             {
27802b20ef6eSJason M. Bills                 logEntryJson.push_back(logEntry);
27812b20ef6eSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
27822b20ef6eSJason M. Bills                     logEntryJson.size();
27832b20ef6eSJason M. Bills             }
27842b20ef6eSJason M. Bills             else
27852b20ef6eSJason M. Bills             {
27862b20ef6eSJason M. Bills                 logEntryJson = logEntry;
27872b20ef6eSJason M. Bills             }
2788e855dd28SJason M. Bills         };
2789e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
27905b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
27915b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2792043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2793e855dd28SJason M. Bills }
2794e855dd28SJason M. Bills 
27957e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
27961da66f75SEd Tanous {
27973946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27983946028dSAppaRao Puli     // method for security reasons.
27991da66f75SEd Tanous     /**
28001da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
28011da66f75SEd Tanous      */
28027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
28037e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2804ed398213SEd Tanous         // This is incorrect, should be.
2805ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2806432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2807*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
2808*45ca1b86SEd Tanous                                                        const std::shared_ptr<
2809*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
2810*45ca1b86SEd Tanous                                                            asyncResp) {
2811*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2812*45ca1b86SEd Tanous             {
2813*45ca1b86SEd Tanous                 return;
2814*45ca1b86SEd Tanous             }
28152b20ef6eSJason M. Bills             crow::connections::systemBus->async_method_call(
28162b20ef6eSJason M. Bills                 [asyncResp](const boost::system::error_code ec,
28172b20ef6eSJason M. Bills                             const std::vector<std::string>& resp) {
28181da66f75SEd Tanous                     if (ec)
28191da66f75SEd Tanous                     {
28201da66f75SEd Tanous                         if (ec.value() !=
28211da66f75SEd Tanous                             boost::system::errc::no_such_file_or_directory)
28221da66f75SEd Tanous                         {
28231da66f75SEd Tanous                             BMCWEB_LOG_DEBUG << "failed to get entries ec: "
28241da66f75SEd Tanous                                              << ec.message();
2825f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
28261da66f75SEd Tanous                             return;
28271da66f75SEd Tanous                         }
28281da66f75SEd Tanous                     }
2829e1f26343SJason M. Bills                     asyncResp->res.jsonValue["@odata.type"] =
28301da66f75SEd Tanous                         "#LogEntryCollection.LogEntryCollection";
28310f74e643SEd Tanous                     asyncResp->res.jsonValue["@odata.id"] =
2832424c4176SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
28332b20ef6eSJason M. Bills                     asyncResp->res.jsonValue["Name"] =
28342b20ef6eSJason M. Bills                         "Open BMC Crashdump Entries";
2835e1f26343SJason M. Bills                     asyncResp->res.jsonValue["Description"] =
2836424c4176SJason M. Bills                         "Collection of Crashdump Entries";
28372b20ef6eSJason M. Bills                     asyncResp->res.jsonValue["Members"] =
28382b20ef6eSJason M. Bills                         nlohmann::json::array();
2839a2dd60a6SBrandon Kim                     asyncResp->res.jsonValue["Members@odata.count"] = 0;
28402b20ef6eSJason M. Bills 
28412b20ef6eSJason M. Bills                     for (const std::string& path : resp)
28421da66f75SEd Tanous                     {
28432b20ef6eSJason M. Bills                         const sdbusplus::message::object_path objPath(path);
2844e855dd28SJason M. Bills                         // Get the log ID
28452b20ef6eSJason M. Bills                         std::string logID = objPath.filename();
28462b20ef6eSJason M. Bills                         if (logID.empty())
28471da66f75SEd Tanous                         {
2848e855dd28SJason M. Bills                             continue;
28491da66f75SEd Tanous                         }
2850e855dd28SJason M. Bills                         // Add the log entry to the array
28512b20ef6eSJason M. Bills                         logCrashdumpEntry(asyncResp, logID,
28522b20ef6eSJason M. Bills                                           asyncResp->res.jsonValue["Members"]);
28531da66f75SEd Tanous                     }
28542b20ef6eSJason M. Bills                 },
28551da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
28561da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
28571da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
28585b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
28597e860f15SJohn Edward Broadbent         });
28601da66f75SEd Tanous }
28611da66f75SEd Tanous 
28627e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28631da66f75SEd Tanous {
28643946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28653946028dSAppaRao Puli     // method for security reasons.
28661da66f75SEd Tanous 
28677e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28687e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2869ed398213SEd Tanous         // this is incorrect, should be
2870ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2871432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2873*45ca1b86SEd Tanous             [&app](const crow::Request& req,
28747e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28757e860f15SJohn Edward Broadbent                    const std::string& param) {
2876*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2877*45ca1b86SEd Tanous                 {
2878*45ca1b86SEd Tanous                     return;
2879*45ca1b86SEd Tanous                 }
28807e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2881e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
28827e860f15SJohn Edward Broadbent             });
2883e855dd28SJason M. Bills }
2884e855dd28SJason M. Bills 
28857e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2886e855dd28SJason M. Bills {
28873946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28883946028dSAppaRao Puli     // method for security reasons.
28897e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28907e860f15SJohn Edward Broadbent         app,
28917e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2892ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
28937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2894*45ca1b86SEd Tanous             [&app](const crow::Request& req,
28957e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28967e860f15SJohn Edward Broadbent                    const std::string& logID, const std::string& fileName) {
2897*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2898*45ca1b86SEd Tanous                 {
2899*45ca1b86SEd Tanous                     return;
2900*45ca1b86SEd Tanous                 }
2901043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2902ace85d60SEd Tanous                     [asyncResp, logID, fileName,
2903ace85d60SEd Tanous                      url(boost::urls::url(req.urlView))](
2904abf2add6SEd Tanous                         const boost::system::error_code ec,
2905168e20c1SEd Tanous                         const std::vector<std::pair<
2906168e20c1SEd Tanous                             std::string, dbus::utility::DbusVariantType>>&
29077e860f15SJohn Edward Broadbent                             resp) {
29081da66f75SEd Tanous                         if (ec)
29091da66f75SEd Tanous                         {
2910043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2911043a0536SJohnathan Mantey                                              << ec.message();
2912f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
29131da66f75SEd Tanous                             return;
29141da66f75SEd Tanous                         }
2915e855dd28SJason M. Bills 
2916043a0536SJohnathan Mantey                         std::string dbusFilename{};
2917043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2918043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2919043a0536SJohnathan Mantey 
29207e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
29217e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2922043a0536SJohnathan Mantey 
2923043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2924043a0536SJohnathan Mantey                             dbusFilepath.empty())
29251da66f75SEd Tanous                         {
2926ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
29271da66f75SEd Tanous                             return;
29281da66f75SEd Tanous                         }
2929e855dd28SJason M. Bills 
2930043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2931043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2932043a0536SJohnathan Mantey                         {
2933ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
2934043a0536SJohnathan Mantey                             return;
2935043a0536SJohnathan Mantey                         }
2936043a0536SJohnathan Mantey 
2937043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2938043a0536SJohnathan Mantey                         {
2939ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
2940043a0536SJohnathan Mantey                             return;
2941043a0536SJohnathan Mantey                         }
29422d31491bSJason M. Bills                         std::ifstream ifs(dbusFilepath,
29432d31491bSJason M. Bills                                           std::ios::in | std::ios::binary);
29442d31491bSJason M. Bills                         asyncResp->res.body() = std::string(
29452d31491bSJason M. Bills                             std::istreambuf_iterator<char>{ifs}, {});
2946043a0536SJohnathan Mantey 
29477e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
29487e860f15SJohn Edward Broadbent                         // from a browser
29497e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
29507e860f15SJohn Edward Broadbent                                                  "attachment");
29511da66f75SEd Tanous                     };
29521da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
29535b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
29545b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
29557e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
29567e860f15SJohn Edward Broadbent                     crashdumpInterface);
29577e860f15SJohn Edward Broadbent             });
29581da66f75SEd Tanous }
29591da66f75SEd Tanous 
2960c5a4c82aSJason M. Bills enum class OEMDiagnosticType
2961c5a4c82aSJason M. Bills {
2962c5a4c82aSJason M. Bills     onDemand,
2963c5a4c82aSJason M. Bills     telemetry,
2964c5a4c82aSJason M. Bills     invalid,
2965c5a4c82aSJason M. Bills };
2966c5a4c82aSJason M. Bills 
2967f7725d79SEd Tanous inline OEMDiagnosticType
2968f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
2969c5a4c82aSJason M. Bills {
2970c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
2971c5a4c82aSJason M. Bills     {
2972c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
2973c5a4c82aSJason M. Bills     }
2974c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
2975c5a4c82aSJason M. Bills     {
2976c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
2977c5a4c82aSJason M. Bills     }
2978c5a4c82aSJason M. Bills 
2979c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
2980c5a4c82aSJason M. Bills }
2981c5a4c82aSJason M. Bills 
29827e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
29831da66f75SEd Tanous {
29843946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29853946028dSAppaRao Puli     // method for security reasons.
29860fda0f12SGeorge Liu     BMCWEB_ROUTE(
29870fda0f12SGeorge Liu         app,
29880fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2989ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2990ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2991432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
29927e860f15SJohn Edward Broadbent         .methods(
29937e860f15SJohn Edward Broadbent             boost::beast::http::verb::
2994*45ca1b86SEd Tanous                 post)([&app](
2995*45ca1b86SEd Tanous                           const crow::Request& req,
29967e860f15SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2997*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
2998*45ca1b86SEd Tanous             {
2999*45ca1b86SEd Tanous                 return;
3000*45ca1b86SEd Tanous             }
30018e6c099aSJason M. Bills             std::string diagnosticDataType;
30028e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
300315ed6780SWilly Tu             if (!redfish::json_util::readJsonAction(
30047e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
30057e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
30067e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
30078e6c099aSJason M. Bills             {
30088e6c099aSJason M. Bills                 return;
30098e6c099aSJason M. Bills             }
30108e6c099aSJason M. Bills 
30118e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
30128e6c099aSJason M. Bills             {
30138e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
30148e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
30158e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
30168e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
30178e6c099aSJason M. Bills                     "CollectDiagnosticData");
30188e6c099aSJason M. Bills                 return;
30198e6c099aSJason M. Bills             }
30208e6c099aSJason M. Bills 
3021c5a4c82aSJason M. Bills             OEMDiagnosticType oemDiagType =
3022c5a4c82aSJason M. Bills                 getOEMDiagnosticType(oemDiagnosticDataType);
3023c5a4c82aSJason M. Bills 
3024c5a4c82aSJason M. Bills             std::string iface;
3025c5a4c82aSJason M. Bills             std::string method;
3026c5a4c82aSJason M. Bills             std::string taskMatchStr;
3027c5a4c82aSJason M. Bills             if (oemDiagType == OEMDiagnosticType::onDemand)
3028c5a4c82aSJason M. Bills             {
3029c5a4c82aSJason M. Bills                 iface = crashdumpOnDemandInterface;
3030c5a4c82aSJason M. Bills                 method = "GenerateOnDemandLog";
3031c5a4c82aSJason M. Bills                 taskMatchStr = "type='signal',"
3032c5a4c82aSJason M. Bills                                "interface='org.freedesktop.DBus.Properties',"
3033c5a4c82aSJason M. Bills                                "member='PropertiesChanged',"
3034c5a4c82aSJason M. Bills                                "arg0namespace='com.intel.crashdump'";
3035c5a4c82aSJason M. Bills             }
3036c5a4c82aSJason M. Bills             else if (oemDiagType == OEMDiagnosticType::telemetry)
3037c5a4c82aSJason M. Bills             {
3038c5a4c82aSJason M. Bills                 iface = crashdumpTelemetryInterface;
3039c5a4c82aSJason M. Bills                 method = "GenerateTelemetryLog";
3040c5a4c82aSJason M. Bills                 taskMatchStr = "type='signal',"
3041c5a4c82aSJason M. Bills                                "interface='org.freedesktop.DBus.Properties',"
3042c5a4c82aSJason M. Bills                                "member='PropertiesChanged',"
3043c5a4c82aSJason M. Bills                                "arg0namespace='com.intel.crashdump'";
3044c5a4c82aSJason M. Bills             }
3045c5a4c82aSJason M. Bills             else
3046c5a4c82aSJason M. Bills             {
3047c5a4c82aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3048c5a4c82aSJason M. Bills                                  << oemDiagnosticDataType;
3049c5a4c82aSJason M. Bills                 messages::actionParameterValueFormatError(
3050c5a4c82aSJason M. Bills                     asyncResp->res, oemDiagnosticDataType,
3051c5a4c82aSJason M. Bills                     "OEMDiagnosticDataType", "CollectDiagnosticData");
3052c5a4c82aSJason M. Bills                 return;
3053c5a4c82aSJason M. Bills             }
3054c5a4c82aSJason M. Bills 
3055c5a4c82aSJason M. Bills             auto collectCrashdumpCallback =
3056c5a4c82aSJason M. Bills                 [asyncResp, payload(task::Payload(req)),
3057c5a4c82aSJason M. Bills                  taskMatchStr](const boost::system::error_code ec,
305898be3e39SEd Tanous                                const std::string&) mutable {
30591da66f75SEd Tanous                     if (ec)
30601da66f75SEd Tanous                     {
30617e860f15SJohn Edward Broadbent                         if (ec.value() ==
30627e860f15SJohn Edward Broadbent                             boost::system::errc::operation_not_supported)
30631da66f75SEd Tanous                         {
3064f12894f8SJason M. Bills                             messages::resourceInStandby(asyncResp->res);
30651da66f75SEd Tanous                         }
30664363d3b2SJason M. Bills                         else if (ec.value() ==
30674363d3b2SJason M. Bills                                  boost::system::errc::device_or_resource_busy)
30684363d3b2SJason M. Bills                         {
3069c5a4c82aSJason M. Bills                             messages::serviceTemporarilyUnavailable(
3070c5a4c82aSJason M. Bills                                 asyncResp->res, "60");
30714363d3b2SJason M. Bills                         }
30721da66f75SEd Tanous                         else
30731da66f75SEd Tanous                         {
3074f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
30751da66f75SEd Tanous                         }
30761da66f75SEd Tanous                         return;
30771da66f75SEd Tanous                     }
3078c5a4c82aSJason M. Bills                     std::shared_ptr<task::TaskData> task =
3079c5a4c82aSJason M. Bills                         task::TaskData::createTask(
30807e860f15SJohn Edward Broadbent                             [](boost::system::error_code err,
30817e860f15SJohn Edward Broadbent                                sdbusplus::message::message&,
3082c5a4c82aSJason M. Bills                                const std::shared_ptr<task::TaskData>&
3083c5a4c82aSJason M. Bills                                    taskData) {
308466afe4faSJames Feist                                 if (!err)
308566afe4faSJames Feist                                 {
3086e5d5006bSJames Feist                                     taskData->messages.emplace_back(
3087e5d5006bSJames Feist                                         messages::taskCompletedOK(
3088e5d5006bSJames Feist                                             std::to_string(taskData->index)));
3089831d6b09SJames Feist                                     taskData->state = "Completed";
309066afe4faSJames Feist                                 }
309132898ceaSJames Feist                                 return task::completed;
309266afe4faSJames Feist                             },
3093c5a4c82aSJason M. Bills                             taskMatchStr);
3094c5a4c82aSJason M. Bills 
309546229577SJames Feist                     task->startTimer(std::chrono::minutes(5));
309646229577SJames Feist                     task->populateResp(asyncResp->res);
309798be3e39SEd Tanous                     task->payload.emplace(std::move(payload));
30981da66f75SEd Tanous                 };
30998e6c099aSJason M. Bills 
31001da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
31018e6c099aSJason M. Bills                 std::move(collectCrashdumpCallback), crashdumpObject,
3102c5a4c82aSJason M. Bills                 crashdumpPath, iface, method);
31037e860f15SJohn Edward Broadbent         });
31046eda7685SKenny L. Ku }
31056eda7685SKenny L. Ku 
3106cb92c03bSAndrew Geissler /**
3107cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3108cb92c03bSAndrew Geissler  */
31097e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3110cb92c03bSAndrew Geissler {
3111cb92c03bSAndrew Geissler     /**
3112cb92c03bSAndrew Geissler      * Function handles POST method request.
3113cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3114cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3115cb92c03bSAndrew Geissler      */
31167e860f15SJohn Edward Broadbent 
31170fda0f12SGeorge Liu     BMCWEB_ROUTE(
31180fda0f12SGeorge Liu         app,
31190fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3120ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
31217e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
3122*45ca1b86SEd Tanous             [&app](const crow::Request& req,
31237e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3124*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
3125*45ca1b86SEd Tanous                 {
3126*45ca1b86SEd Tanous                     return;
3127*45ca1b86SEd Tanous                 }
3128cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
3129cb92c03bSAndrew Geissler 
3130cb92c03bSAndrew Geissler                 // Process response from Logging service.
31317e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
31327e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
31337e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
31347e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
3135cb92c03bSAndrew Geissler                     if (ec)
3136cb92c03bSAndrew Geissler                     {
3137cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
31387e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
31397e860f15SJohn Edward Broadbent                                          << ec;
3140cb92c03bSAndrew Geissler                         asyncResp->res.result(
3141cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
3142cb92c03bSAndrew Geissler                         return;
3143cb92c03bSAndrew Geissler                     }
3144cb92c03bSAndrew Geissler 
31457e860f15SJohn Edward Broadbent                     asyncResp->res.result(
31467e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
3147cb92c03bSAndrew Geissler                 };
3148cb92c03bSAndrew Geissler 
3149cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
3150cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
31512c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
3152cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
3153cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31547e860f15SJohn Edward Broadbent             });
3155cb92c03bSAndrew Geissler }
3156a3316fc6SZhikuiRen 
3157a3316fc6SZhikuiRen /****************************************************
3158a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3159a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3160a3316fc6SZhikuiRen  ******************************************************/
31617e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3162a3316fc6SZhikuiRen {
31637e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3164ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3165*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
3166*45ca1b86SEd Tanous                                                        const std::shared_ptr<
3167*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
3168*45ca1b86SEd Tanous                                                            asyncResp) {
3169*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
3170*45ca1b86SEd Tanous             {
3171*45ca1b86SEd Tanous                 return;
3172*45ca1b86SEd Tanous             }
3173a3316fc6SZhikuiRen             asyncResp->res.jsonValue = {
31747e860f15SJohn Edward Broadbent                 {"@odata.id",
31757e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes"},
3176a3316fc6SZhikuiRen                 {"@odata.type", "#LogService.v1_1_0.LogService"},
3177a3316fc6SZhikuiRen                 {"Name", "POST Code Log Service"},
3178a3316fc6SZhikuiRen                 {"Description", "POST Code Log Service"},
3179a3316fc6SZhikuiRen                 {"Id", "BIOS POST Code Log"},
3180a3316fc6SZhikuiRen                 {"OverWritePolicy", "WrapsWhenFull"},
3181a3316fc6SZhikuiRen                 {"Entries",
31820fda0f12SGeorge Liu                  {{"@odata.id",
31830fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
31847c8c4058STejas Patil 
31857c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
31867c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
31870fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
31887c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
31897c8c4058STejas Patil                 redfishDateTimeOffset.second;
31907c8c4058STejas Patil 
3191a3316fc6SZhikuiRen             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
31927e860f15SJohn Edward Broadbent                 {"target",
31930fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
31947e860f15SJohn Edward Broadbent         });
3195a3316fc6SZhikuiRen }
3196a3316fc6SZhikuiRen 
31977e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3198a3316fc6SZhikuiRen {
31990fda0f12SGeorge Liu     BMCWEB_ROUTE(
32000fda0f12SGeorge Liu         app,
32010fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3202ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3203ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3204432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
32057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
3206*45ca1b86SEd Tanous             [&app](const crow::Request& req,
32077e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3208*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
3209*45ca1b86SEd Tanous                 {
3210*45ca1b86SEd Tanous                     return;
3211*45ca1b86SEd Tanous                 }
3212a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3213a3316fc6SZhikuiRen 
3214a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
3215a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
3216a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
3217a3316fc6SZhikuiRen                         if (ec)
3218a3316fc6SZhikuiRen                         {
3219a3316fc6SZhikuiRen                             // TODO Handle for specific error code
3220a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
32217e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
32227e860f15SJohn Edward Broadbent                                 << ec;
32237e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
32247e860f15SJohn Edward Broadbent                                                       internal_server_error);
3225a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
3226a3316fc6SZhikuiRen                             return;
3227a3316fc6SZhikuiRen                         }
3228a3316fc6SZhikuiRen                     },
322915124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
323015124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
3231a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
32327e860f15SJohn Edward Broadbent             });
3233a3316fc6SZhikuiRen }
3234a3316fc6SZhikuiRen 
3235a3316fc6SZhikuiRen static void fillPostCodeEntry(
32368d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
32376c9a279eSManojkiran Eda     const boost::container::flat_map<
32386c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3239a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3240a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3241a3316fc6SZhikuiRen {
3242a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3243fffb8c1fSEd Tanous     const registries::Message* message =
3244fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3245a3316fc6SZhikuiRen 
3246a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3247a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3248a3316fc6SZhikuiRen 
3249a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
32506c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32516c9a279eSManojkiran Eda              code : postcode)
3252a3316fc6SZhikuiRen     {
3253a3316fc6SZhikuiRen         currentCodeIndex++;
3254a3316fc6SZhikuiRen         std::string postcodeEntryID =
3255a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3256a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3257a3316fc6SZhikuiRen 
3258a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3259a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3260a3316fc6SZhikuiRen 
3261a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3262a3316fc6SZhikuiRen         { // already incremented
3263a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3264a3316fc6SZhikuiRen         }
3265a3316fc6SZhikuiRen         else
3266a3316fc6SZhikuiRen         {
3267a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3268a3316fc6SZhikuiRen         }
3269a3316fc6SZhikuiRen 
3270a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3271a3316fc6SZhikuiRen         // not fall between top and skip
3272a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3273a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3274a3316fc6SZhikuiRen         {
3275a3316fc6SZhikuiRen             continue;
3276a3316fc6SZhikuiRen         }
3277a3316fc6SZhikuiRen 
32784e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3279a3316fc6SZhikuiRen         // currentIndex
3280a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3281a3316fc6SZhikuiRen         {
3282a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3283a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3284a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3285a3316fc6SZhikuiRen             continue;
3286a3316fc6SZhikuiRen         }
3287a3316fc6SZhikuiRen 
3288a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3289a3316fc6SZhikuiRen         // index
3290a3316fc6SZhikuiRen 
3291a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3292a3316fc6SZhikuiRen         std::string entryTimeStr;
32931d8782e7SNan Zhou         entryTimeStr =
32941d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3295a3316fc6SZhikuiRen 
3296a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3297a3316fc6SZhikuiRen         std::ostringstream hexCode;
3298a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32996c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3300a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3301a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3302a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3303a3316fc6SZhikuiRen         // Set precision to 4 digits
3304a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3305a3316fc6SZhikuiRen         // Add double to stream
3306a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3307a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3308a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3309a3316fc6SZhikuiRen 
3310a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3311a3316fc6SZhikuiRen         std::string msg;
3312a3316fc6SZhikuiRen         if (message != nullptr)
3313a3316fc6SZhikuiRen         {
3314a3316fc6SZhikuiRen             msg = message->message;
3315a3316fc6SZhikuiRen 
3316a3316fc6SZhikuiRen             // fill in this post code value
3317a3316fc6SZhikuiRen             int i = 0;
3318a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3319a3316fc6SZhikuiRen             {
3320a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3321a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3322a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3323a3316fc6SZhikuiRen                 {
3324a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3325a3316fc6SZhikuiRen                 }
3326a3316fc6SZhikuiRen             }
3327a3316fc6SZhikuiRen         }
3328a3316fc6SZhikuiRen 
3329d4342a92STim Lee         // Get Severity template from message registry
3330d4342a92STim Lee         std::string severity;
3331d4342a92STim Lee         if (message != nullptr)
3332d4342a92STim Lee         {
33335f2b84eeSEd Tanous             severity = message->messageSeverity;
3334d4342a92STim Lee         }
3335d4342a92STim Lee 
3336a3316fc6SZhikuiRen         // add to AsyncResp
3337a3316fc6SZhikuiRen         logEntryArray.push_back({});
3338a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
33390fda0f12SGeorge Liu         bmcLogEntry = {
33400fda0f12SGeorge Liu             {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
33410fda0f12SGeorge Liu             {"@odata.id",
33420fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3343a3316fc6SZhikuiRen                  postcodeEntryID},
3344a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
3345a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
3346a3316fc6SZhikuiRen             {"Message", std::move(msg)},
33474a0bf539SManojkiran Eda             {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3348a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
3349a3316fc6SZhikuiRen             {"EntryType", "Event"},
3350a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
33519c620e21SAsmitha Karunanithi             {"Created", entryTimeStr}};
3352647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3353647b3cdcSGeorge Liu         {
3354647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3355647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3356647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3357647b3cdcSGeorge Liu         }
3358a3316fc6SZhikuiRen     }
3359a3316fc6SZhikuiRen }
3360a3316fc6SZhikuiRen 
33618d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3362a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3363a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3364a3316fc6SZhikuiRen {
3365a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
33666c9a279eSManojkiran Eda         [aResp, bootIndex,
33676c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
33686c9a279eSManojkiran Eda                     const boost::container::flat_map<
33696c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33706c9a279eSManojkiran Eda                         postcode) {
3371a3316fc6SZhikuiRen             if (ec)
3372a3316fc6SZhikuiRen             {
3373a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3374a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3375a3316fc6SZhikuiRen                 return;
3376a3316fc6SZhikuiRen             }
3377a3316fc6SZhikuiRen 
3378a3316fc6SZhikuiRen             // skip the empty postcode boots
3379a3316fc6SZhikuiRen             if (postcode.empty())
3380a3316fc6SZhikuiRen             {
3381a3316fc6SZhikuiRen                 return;
3382a3316fc6SZhikuiRen             }
3383a3316fc6SZhikuiRen 
3384a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3385a3316fc6SZhikuiRen 
3386a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3387a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3388a3316fc6SZhikuiRen         },
338915124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
339015124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3391a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3392a3316fc6SZhikuiRen         bootIndex);
3393a3316fc6SZhikuiRen }
3394a3316fc6SZhikuiRen 
33958d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3396a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3397a3316fc6SZhikuiRen                                const uint16_t bootCount,
3398a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3399a3316fc6SZhikuiRen                                const uint64_t top)
3400a3316fc6SZhikuiRen {
3401a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3402a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3403a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
34046c9a279eSManojkiran Eda               const boost::container::flat_map<
34056c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
34066c9a279eSManojkiran Eda                   postcode) {
3407a3316fc6SZhikuiRen             if (ec)
3408a3316fc6SZhikuiRen             {
3409a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3410a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3411a3316fc6SZhikuiRen                 return;
3412a3316fc6SZhikuiRen             }
3413a3316fc6SZhikuiRen 
3414a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3415a3316fc6SZhikuiRen             if (!postcode.empty())
3416a3316fc6SZhikuiRen             {
3417a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3418a3316fc6SZhikuiRen 
3419a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3420a3316fc6SZhikuiRen                 {
3421a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3422a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3423a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3424a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3425a3316fc6SZhikuiRen 
3426a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3427a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3428a3316fc6SZhikuiRen                 }
3429a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3430a3316fc6SZhikuiRen             }
3431a3316fc6SZhikuiRen 
3432a3316fc6SZhikuiRen             // continue to previous bootIndex
3433a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3434a3316fc6SZhikuiRen             {
3435a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3436a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3437a3316fc6SZhikuiRen             }
3438a3316fc6SZhikuiRen             else
3439a3316fc6SZhikuiRen             {
3440a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
34410fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3442a3316fc6SZhikuiRen                     std::to_string(skip + top);
3443a3316fc6SZhikuiRen             }
3444a3316fc6SZhikuiRen         },
344515124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
344615124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3447a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3448a3316fc6SZhikuiRen         bootIndex);
3449a3316fc6SZhikuiRen }
3450a3316fc6SZhikuiRen 
34518d1b46d7Szhanghch05 static void
34528d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3453a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3454a3316fc6SZhikuiRen {
3455a3316fc6SZhikuiRen     uint64_t entryCount = 0;
34561e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
34571e1e598dSJonathan Doman         *crow::connections::systemBus,
34581e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
34591e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
34601e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
34611e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
34621e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3463a3316fc6SZhikuiRen             if (ec)
3464a3316fc6SZhikuiRen             {
3465a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3466a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3467a3316fc6SZhikuiRen                 return;
3468a3316fc6SZhikuiRen             }
34691e1e598dSJonathan Doman             getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
34701e1e598dSJonathan Doman         });
3471a3316fc6SZhikuiRen }
3472a3316fc6SZhikuiRen 
34737e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3474a3316fc6SZhikuiRen {
34757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
34767e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3477ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
34787e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3479*45ca1b86SEd Tanous             [&app](const crow::Request& req,
34807e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3481*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
3482*45ca1b86SEd Tanous                 {
3483*45ca1b86SEd Tanous                     return;
3484*45ca1b86SEd Tanous                 }
3485a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3486a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3487a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3488a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3489a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3490a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3491a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3492a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3493a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3494a3316fc6SZhikuiRen 
3495a3316fc6SZhikuiRen                 uint64_t skip = 0;
3496a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
34978d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3498a3316fc6SZhikuiRen                 {
3499a3316fc6SZhikuiRen                     return;
3500a3316fc6SZhikuiRen                 }
35018d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3502a3316fc6SZhikuiRen                 {
3503a3316fc6SZhikuiRen                     return;
3504a3316fc6SZhikuiRen                 }
3505a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
35067e860f15SJohn Edward Broadbent             });
3507a3316fc6SZhikuiRen }
3508a3316fc6SZhikuiRen 
3509647b3cdcSGeorge Liu /**
3510647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3511647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3512647b3cdcSGeorge Liu  *
3513647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3514647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3515647b3cdcSGeorge Liu  * @param[out] index          Index value
3516647b3cdcSGeorge Liu  *
3517647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3518647b3cdcSGeorge Liu  */
3519647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3520647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3521647b3cdcSGeorge Liu {
3522647b3cdcSGeorge Liu     std::vector<std::string> split;
3523647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3524647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3525647b3cdcSGeorge Liu     {
3526647b3cdcSGeorge Liu         return false;
3527647b3cdcSGeorge Liu     }
3528647b3cdcSGeorge Liu 
3529ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3530647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3531ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3532647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3533647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3534647b3cdcSGeorge Liu 
3535647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3536647b3cdcSGeorge Liu     {
3537647b3cdcSGeorge Liu         return false;
3538647b3cdcSGeorge Liu     }
3539647b3cdcSGeorge Liu 
3540647b3cdcSGeorge Liu     start = split[1].data();
3541ca45aa3cSEd Tanous 
3542ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3543647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3544647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3545647b3cdcSGeorge Liu 
3546dcf2ebc0SEd Tanous     return ptrValue == end && ecValue != std::errc();
3547647b3cdcSGeorge Liu }
3548647b3cdcSGeorge Liu 
3549647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3550647b3cdcSGeorge Liu {
35510fda0f12SGeorge Liu     BMCWEB_ROUTE(
35520fda0f12SGeorge Liu         app,
35530fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3554647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3555647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
3556*45ca1b86SEd Tanous             [&app](const crow::Request& req,
3557647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3558647b3cdcSGeorge Liu                    const std::string& postCodeID) {
3559*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
3560*45ca1b86SEd Tanous                 {
3561*45ca1b86SEd Tanous                     return;
3562*45ca1b86SEd Tanous                 }
3563647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
3564647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
3565647b3cdcSGeorge Liu                 {
3566647b3cdcSGeorge Liu                     asyncResp->res.result(
3567647b3cdcSGeorge Liu                         boost::beast::http::status::bad_request);
3568647b3cdcSGeorge Liu                     return;
3569647b3cdcSGeorge Liu                 }
3570647b3cdcSGeorge Liu 
3571647b3cdcSGeorge Liu                 uint64_t currentValue = 0;
3572647b3cdcSGeorge Liu                 uint16_t index = 0;
3573647b3cdcSGeorge Liu                 if (!parsePostCode(postCodeID, currentValue, index))
3574647b3cdcSGeorge Liu                 {
3575647b3cdcSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
3576647b3cdcSGeorge Liu                                                postCodeID);
3577647b3cdcSGeorge Liu                     return;
3578647b3cdcSGeorge Liu                 }
3579647b3cdcSGeorge Liu 
3580647b3cdcSGeorge Liu                 crow::connections::systemBus->async_method_call(
3581647b3cdcSGeorge Liu                     [asyncResp, postCodeID, currentValue](
3582647b3cdcSGeorge Liu                         const boost::system::error_code ec,
3583647b3cdcSGeorge Liu                         const std::vector<std::tuple<
3584647b3cdcSGeorge Liu                             uint64_t, std::vector<uint8_t>>>& postcodes) {
3585647b3cdcSGeorge Liu                         if (ec.value() == EBADR)
3586647b3cdcSGeorge Liu                         {
3587647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3588647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3589647b3cdcSGeorge Liu                             return;
3590647b3cdcSGeorge Liu                         }
3591647b3cdcSGeorge Liu                         if (ec)
3592647b3cdcSGeorge Liu                         {
3593647b3cdcSGeorge Liu                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3594647b3cdcSGeorge Liu                             messages::internalError(asyncResp->res);
3595647b3cdcSGeorge Liu                             return;
3596647b3cdcSGeorge Liu                         }
3597647b3cdcSGeorge Liu 
3598647b3cdcSGeorge Liu                         size_t value = static_cast<size_t>(currentValue) - 1;
3599647b3cdcSGeorge Liu                         if (value == std::string::npos ||
3600647b3cdcSGeorge Liu                             postcodes.size() < currentValue)
3601647b3cdcSGeorge Liu                         {
3602647b3cdcSGeorge Liu                             BMCWEB_LOG_ERROR << "Wrong currentValue value";
3603647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3604647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3605647b3cdcSGeorge Liu                             return;
3606647b3cdcSGeorge Liu                         }
3607647b3cdcSGeorge Liu 
36089eb808c1SEd Tanous                         const auto& [tID, c] = postcodes[value];
360946ff87baSEd Tanous                         if (c.empty())
3610647b3cdcSGeorge Liu                         {
3611647b3cdcSGeorge Liu                             BMCWEB_LOG_INFO << "No found post code data";
3612647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3613647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3614647b3cdcSGeorge Liu                             return;
3615647b3cdcSGeorge Liu                         }
361646ff87baSEd Tanous                         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
361746ff87baSEd Tanous                         const char* d = reinterpret_cast<const char*>(c.data());
361846ff87baSEd Tanous                         std::string_view strData(d, c.size());
3619647b3cdcSGeorge Liu 
3620647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Type",
3621647b3cdcSGeorge Liu                                                  "application/octet-stream");
3622647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Transfer-Encoding",
3623647b3cdcSGeorge Liu                                                  "Base64");
3624647b3cdcSGeorge Liu                         asyncResp->res.body() =
3625647b3cdcSGeorge Liu                             crow::utility::base64encode(strData);
3626647b3cdcSGeorge Liu                     },
3627647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode0",
3628647b3cdcSGeorge Liu                     "/xyz/openbmc_project/State/Boot/PostCode0",
3629647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3630647b3cdcSGeorge Liu                     index);
3631647b3cdcSGeorge Liu             });
3632647b3cdcSGeorge Liu }
3633647b3cdcSGeorge Liu 
36347e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3635a3316fc6SZhikuiRen {
36367e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
36377e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3638ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
36397e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3640*45ca1b86SEd Tanous             [&app](const crow::Request& req,
36417e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
36427e860f15SJohn Edward Broadbent                    const std::string& targetID) {
3643*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
3644*45ca1b86SEd Tanous                 {
3645*45ca1b86SEd Tanous                     return;
3646*45ca1b86SEd Tanous                 }
3647647b3cdcSGeorge Liu                 uint16_t bootIndex = 0;
3648647b3cdcSGeorge Liu                 uint64_t codeIndex = 0;
3649647b3cdcSGeorge Liu                 if (!parsePostCode(targetID, codeIndex, bootIndex))
3650a3316fc6SZhikuiRen                 {
3651a3316fc6SZhikuiRen                     // Requested ID was not found
3652ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3653a3316fc6SZhikuiRen                     return;
3654a3316fc6SZhikuiRen                 }
3655a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3656a3316fc6SZhikuiRen                 {
3657a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
36587e860f15SJohn Edward Broadbent                                      << targetID;
3659a3316fc6SZhikuiRen                 }
3660a3316fc6SZhikuiRen 
36617e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
36627e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3663a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
36640fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3665a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3666a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3667a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3668a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3669a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3670a3316fc6SZhikuiRen 
3671a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
36727e860f15SJohn Edward Broadbent             });
3673a3316fc6SZhikuiRen }
3674a3316fc6SZhikuiRen 
36751da66f75SEd Tanous } // namespace redfish
3676