xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision b9d36b4791d77a47e1f3c5c4564fcdf7cc68c115)
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>
37ed398213SEd Tanous #include <registries/privilege_registry.hpp>
381214b7e7SGunnar Mills 
39647b3cdcSGeorge Liu #include <charconv>
404418c7f0SJames Feist #include <filesystem>
4175710de2SXiaochao Ma #include <optional>
4226702d01SEd Tanous #include <span>
43cd225da8SJason M. Bills #include <string_view>
44abf2add6SEd Tanous #include <variant>
451da66f75SEd Tanous 
461da66f75SEd Tanous namespace redfish
471da66f75SEd Tanous {
481da66f75SEd Tanous 
495b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
505b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
515b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
525b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
535b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
545b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
55424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
566eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
576eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
581da66f75SEd Tanous 
59fffb8c1fSEd Tanous namespace registries
604851d45dSJason M. Bills {
6126702d01SEd Tanous static const Message*
6226702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
6326702d01SEd Tanous                            const std::span<const MessageEntry> registry)
644851d45dSJason M. Bills {
6526702d01SEd Tanous     std::span<const MessageEntry>::iterator messageIt = std::find_if(
6626702d01SEd Tanous         registry.begin(), registry.end(),
674851d45dSJason M. Bills         [&messageKey](const MessageEntry& messageEntry) {
68e662eae8SEd Tanous             return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
694851d45dSJason M. Bills         });
7026702d01SEd Tanous     if (messageIt != registry.end())
714851d45dSJason M. Bills     {
724851d45dSJason M. Bills         return &messageIt->second;
734851d45dSJason M. Bills     }
744851d45dSJason M. Bills 
754851d45dSJason M. Bills     return nullptr;
764851d45dSJason M. Bills }
774851d45dSJason M. Bills 
784851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
794851d45dSJason M. Bills {
804851d45dSJason M. Bills     // Redfish MessageIds are in the form
814851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
824851d45dSJason M. Bills     // the right Message
834851d45dSJason M. Bills     std::vector<std::string> fields;
844851d45dSJason M. Bills     fields.reserve(4);
854851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
864851d45dSJason M. Bills     std::string& registryName = fields[0];
874851d45dSJason M. Bills     std::string& messageKey = fields[3];
884851d45dSJason M. Bills 
894851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
904851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
914851d45dSJason M. Bills     {
924851d45dSJason M. Bills         return getMessageFromRegistry(
9326702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
944851d45dSJason M. Bills     }
954851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
964851d45dSJason M. Bills     {
974851d45dSJason M. Bills         return getMessageFromRegistry(
9826702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
994851d45dSJason M. Bills     }
1004851d45dSJason M. Bills     return nullptr;
1014851d45dSJason M. Bills }
102fffb8c1fSEd Tanous } // namespace registries
1034851d45dSJason M. Bills 
104f6150403SJames Feist namespace fs = std::filesystem;
1051da66f75SEd Tanous 
106cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
107cb92c03bSAndrew Geissler {
108d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
109d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
110d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
111d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
112cb92c03bSAndrew Geissler     {
113cb92c03bSAndrew Geissler         return "Critical";
114cb92c03bSAndrew Geissler     }
1153174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
116d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
117d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
118cb92c03bSAndrew Geissler     {
119cb92c03bSAndrew Geissler         return "OK";
120cb92c03bSAndrew Geissler     }
1213174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
122cb92c03bSAndrew Geissler     {
123cb92c03bSAndrew Geissler         return "Warning";
124cb92c03bSAndrew Geissler     }
125cb92c03bSAndrew Geissler     return "";
126cb92c03bSAndrew Geissler }
127cb92c03bSAndrew Geissler 
1287e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
12939e77504SEd Tanous                                      const std::string_view& field,
13039e77504SEd Tanous                                      std::string_view& contents)
13116428a1aSJason M. Bills {
13216428a1aSJason M. Bills     const char* data = nullptr;
13316428a1aSJason M. Bills     size_t length = 0;
13416428a1aSJason M. Bills     int ret = 0;
13516428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
13646ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
13746ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
13846ff87baSEd Tanous 
13946ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
14016428a1aSJason M. Bills     if (ret < 0)
14116428a1aSJason M. Bills     {
14216428a1aSJason M. Bills         return ret;
14316428a1aSJason M. Bills     }
14439e77504SEd Tanous     contents = std::string_view(data, length);
14516428a1aSJason M. Bills     // Only use the content after the "=" character.
14681ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
14716428a1aSJason M. Bills     return ret;
14816428a1aSJason M. Bills }
14916428a1aSJason M. Bills 
1507e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1517e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1527e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15316428a1aSJason M. Bills {
15416428a1aSJason M. Bills     int ret = 0;
15539e77504SEd Tanous     std::string_view metadata;
15616428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15716428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
15816428a1aSJason M. Bills     if (ret < 0)
15916428a1aSJason M. Bills     {
16016428a1aSJason M. Bills         return ret;
16116428a1aSJason M. Bills     }
162b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16316428a1aSJason M. Bills     return ret;
16416428a1aSJason M. Bills }
16516428a1aSJason M. Bills 
1667e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1677e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
168a3316fc6SZhikuiRen {
169a3316fc6SZhikuiRen     int ret = 0;
170a3316fc6SZhikuiRen     uint64_t timestamp = 0;
171a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
172a3316fc6SZhikuiRen     if (ret < 0)
173a3316fc6SZhikuiRen     {
174a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
175a3316fc6SZhikuiRen                          << strerror(-ret);
176a3316fc6SZhikuiRen         return false;
177a3316fc6SZhikuiRen     }
1781d8782e7SNan Zhou     entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
1799c620e21SAsmitha Karunanithi     return true;
180a3316fc6SZhikuiRen }
18150b8a43aSEd Tanous 
18267df073bSEd Tanous static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18367df073bSEd Tanous                          const crow::Request& req, uint64_t& skip)
18467df073bSEd Tanous {
18567df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$skip");
18667df073bSEd Tanous     if (it != req.urlView.params().end())
18767df073bSEd Tanous     {
18867df073bSEd Tanous         std::from_chars_result r = std::from_chars(
18967df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), skip);
19067df073bSEd Tanous         if (r.ec != std::errc())
19167df073bSEd Tanous         {
19267df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$skip");
19367df073bSEd Tanous             return false;
19467df073bSEd Tanous         }
19567df073bSEd Tanous     }
19667df073bSEd Tanous     return true;
19767df073bSEd Tanous }
19867df073bSEd Tanous 
19967df073bSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
20067df073bSEd Tanous static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20167df073bSEd Tanous                         const crow::Request& req, uint64_t& top)
20267df073bSEd Tanous {
20367df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$top");
20467df073bSEd Tanous     if (it != req.urlView.params().end())
20567df073bSEd Tanous     {
20667df073bSEd Tanous         std::from_chars_result r = std::from_chars(
20767df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), top);
20867df073bSEd Tanous         if (r.ec != std::errc())
20967df073bSEd Tanous         {
21067df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$top");
21167df073bSEd Tanous             return false;
21267df073bSEd Tanous         }
21367df073bSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
21467df073bSEd Tanous         {
21567df073bSEd Tanous 
21667df073bSEd Tanous             messages::queryParameterOutOfRange(
21767df073bSEd Tanous                 asyncResp->res, std::to_string(top), "$top",
21867df073bSEd Tanous                 "1-" + std::to_string(maxEntriesPerPage));
21967df073bSEd Tanous             return false;
22067df073bSEd Tanous         }
22167df073bSEd Tanous     }
22267df073bSEd Tanous     return true;
22367df073bSEd Tanous }
22467df073bSEd Tanous 
2257e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
226e85d6b16SJason M. Bills                                     const bool firstEntry = true)
22716428a1aSJason M. Bills {
22816428a1aSJason M. Bills     int ret = 0;
22916428a1aSJason M. Bills     static uint64_t prevTs = 0;
23016428a1aSJason M. Bills     static int index = 0;
231e85d6b16SJason M. Bills     if (firstEntry)
232e85d6b16SJason M. Bills     {
233e85d6b16SJason M. Bills         prevTs = 0;
234e85d6b16SJason M. Bills     }
235e85d6b16SJason M. Bills 
23616428a1aSJason M. Bills     // Get the entry timestamp
23716428a1aSJason M. Bills     uint64_t curTs = 0;
23816428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
23916428a1aSJason M. Bills     if (ret < 0)
24016428a1aSJason M. Bills     {
24116428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
24216428a1aSJason M. Bills                          << strerror(-ret);
24316428a1aSJason M. Bills         return false;
24416428a1aSJason M. Bills     }
24516428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
24616428a1aSJason M. Bills     if (curTs == prevTs)
24716428a1aSJason M. Bills     {
24816428a1aSJason M. Bills         index++;
24916428a1aSJason M. Bills     }
25016428a1aSJason M. Bills     else
25116428a1aSJason M. Bills     {
25216428a1aSJason M. Bills         // Otherwise, reset it
25316428a1aSJason M. Bills         index = 0;
25416428a1aSJason M. Bills     }
25516428a1aSJason M. Bills     // Save the timestamp
25616428a1aSJason M. Bills     prevTs = curTs;
25716428a1aSJason M. Bills 
25816428a1aSJason M. Bills     entryID = std::to_string(curTs);
25916428a1aSJason M. Bills     if (index > 0)
26016428a1aSJason M. Bills     {
26116428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
26216428a1aSJason M. Bills     }
26316428a1aSJason M. Bills     return true;
26416428a1aSJason M. Bills }
26516428a1aSJason M. Bills 
266e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
267e85d6b16SJason M. Bills                              const bool firstEntry = true)
26895820184SJason M. Bills {
269271584abSEd Tanous     static time_t prevTs = 0;
27095820184SJason M. Bills     static int index = 0;
271e85d6b16SJason M. Bills     if (firstEntry)
272e85d6b16SJason M. Bills     {
273e85d6b16SJason M. Bills         prevTs = 0;
274e85d6b16SJason M. Bills     }
275e85d6b16SJason M. Bills 
27695820184SJason M. Bills     // Get the entry timestamp
277271584abSEd Tanous     std::time_t curTs = 0;
27895820184SJason M. Bills     std::tm timeStruct = {};
27995820184SJason M. Bills     std::istringstream entryStream(logEntry);
28095820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
28195820184SJason M. Bills     {
28295820184SJason M. Bills         curTs = std::mktime(&timeStruct);
28395820184SJason M. Bills     }
28495820184SJason M. Bills     // If the timestamp isn't unique, increment the index
28595820184SJason M. Bills     if (curTs == prevTs)
28695820184SJason M. Bills     {
28795820184SJason M. Bills         index++;
28895820184SJason M. Bills     }
28995820184SJason M. Bills     else
29095820184SJason M. Bills     {
29195820184SJason M. Bills         // Otherwise, reset it
29295820184SJason M. Bills         index = 0;
29395820184SJason M. Bills     }
29495820184SJason M. Bills     // Save the timestamp
29595820184SJason M. Bills     prevTs = curTs;
29695820184SJason M. Bills 
29795820184SJason M. Bills     entryID = std::to_string(curTs);
29895820184SJason M. Bills     if (index > 0)
29995820184SJason M. Bills     {
30095820184SJason M. Bills         entryID += "_" + std::to_string(index);
30195820184SJason M. Bills     }
30295820184SJason M. Bills     return true;
30395820184SJason M. Bills }
30495820184SJason M. Bills 
3057e860f15SJohn Edward Broadbent inline static bool
3068d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3078d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3088d1b46d7Szhanghch05                        uint64_t& index)
30916428a1aSJason M. Bills {
31016428a1aSJason M. Bills     if (entryID.empty())
31116428a1aSJason M. Bills     {
31216428a1aSJason M. Bills         return false;
31316428a1aSJason M. Bills     }
31416428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
31539e77504SEd Tanous     std::string_view tsStr(entryID);
31616428a1aSJason M. Bills 
31781ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
31871d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
31916428a1aSJason M. Bills     {
32016428a1aSJason M. Bills         // Timestamp has an index
32116428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
32239e77504SEd Tanous         std::string_view indexStr(entryID);
32316428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
324c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
325c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
326c0bd5e4bSEd Tanous         if (ec != std::errc())
32716428a1aSJason M. Bills         {
328ace85d60SEd Tanous             messages::resourceMissingAtURI(
329ace85d60SEd Tanous                 asyncResp->res, crow::utility::urlFromPieces(entryID));
33016428a1aSJason M. Bills             return false;
33116428a1aSJason M. Bills         }
33216428a1aSJason M. Bills     }
33316428a1aSJason M. Bills     // Timestamp has no index
334c0bd5e4bSEd Tanous     auto [ptr, ec] =
335c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
336c0bd5e4bSEd Tanous     if (ec != std::errc())
33716428a1aSJason M. Bills     {
338ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
339ace85d60SEd Tanous                                        crow::utility::urlFromPieces(entryID));
34016428a1aSJason M. Bills         return false;
34116428a1aSJason M. Bills     }
34216428a1aSJason M. Bills     return true;
34316428a1aSJason M. Bills }
34416428a1aSJason M. Bills 
34595820184SJason M. Bills static bool
34695820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
34795820184SJason M. Bills {
34895820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
34995820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
35095820184SJason M. Bills 
35195820184SJason M. Bills     // Loop through the directory looking for redfish log files
35295820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
35395820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
35495820184SJason M. Bills     {
35595820184SJason M. Bills         // If we find a redfish log file, save the path
35695820184SJason M. Bills         std::string filename = dirEnt.path().filename();
35795820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
35895820184SJason M. Bills         {
35995820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
36095820184SJason M. Bills         }
36195820184SJason M. Bills     }
36295820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
36395820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
36495820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
36595820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
36695820184SJason M. Bills 
36795820184SJason M. Bills     return !redfishLogFiles.empty();
36895820184SJason M. Bills }
36995820184SJason M. Bills 
3708d1b46d7Szhanghch05 inline void
3718d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3725cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
3735cb1dd27SAsmitha Karunanithi {
3745cb1dd27SAsmitha Karunanithi     std::string dumpPath;
3755cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
3765cb1dd27SAsmitha Karunanithi     {
3775cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
3785cb1dd27SAsmitha Karunanithi     }
3795cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
3805cb1dd27SAsmitha Karunanithi     {
3815cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
3825cb1dd27SAsmitha Karunanithi     }
3835cb1dd27SAsmitha Karunanithi     else
3845cb1dd27SAsmitha Karunanithi     {
3855cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
3865cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
3875cb1dd27SAsmitha Karunanithi         return;
3885cb1dd27SAsmitha Karunanithi     }
3895cb1dd27SAsmitha Karunanithi 
3905cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
391711ac7a9SEd Tanous         [asyncResp, dumpPath,
392711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
393711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
3945cb1dd27SAsmitha Karunanithi             if (ec)
3955cb1dd27SAsmitha Karunanithi             {
3965cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
3975cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
3985cb1dd27SAsmitha Karunanithi                 return;
3995cb1dd27SAsmitha Karunanithi             }
4005cb1dd27SAsmitha Karunanithi 
4015cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4025cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
403b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
404b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
405b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
406b47452b2SAsmitha Karunanithi                 "/entry/";
4075cb1dd27SAsmitha Karunanithi 
4085cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4095cb1dd27SAsmitha Karunanithi             {
410b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4115cb1dd27SAsmitha Karunanithi                 {
4125cb1dd27SAsmitha Karunanithi                     continue;
4135cb1dd27SAsmitha Karunanithi                 }
4141d8782e7SNan Zhou                 uint64_t timestamp = 0;
4155cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
41635440d18SAsmitha Karunanithi                 std::string dumpStatus;
41735440d18SAsmitha Karunanithi                 nlohmann::json thisEntry;
4182dfd18efSEd Tanous 
4192dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4202dfd18efSEd Tanous                 if (entryID.empty())
4215cb1dd27SAsmitha Karunanithi                 {
4225cb1dd27SAsmitha Karunanithi                     continue;
4235cb1dd27SAsmitha Karunanithi                 }
4245cb1dd27SAsmitha Karunanithi 
4255cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4265cb1dd27SAsmitha Karunanithi                 {
42735440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
42835440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
42935440d18SAsmitha Karunanithi                     {
4309eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
43135440d18SAsmitha Karunanithi                         {
43235440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
43335440d18SAsmitha Karunanithi                             {
43455f79e6fSEd Tanous                                 const auto* status = std::get_if<std::string>(
43535440d18SAsmitha Karunanithi                                     &propertyMap.second);
43635440d18SAsmitha Karunanithi                                 if (status == nullptr)
43735440d18SAsmitha Karunanithi                                 {
43835440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
43935440d18SAsmitha Karunanithi                                     break;
44035440d18SAsmitha Karunanithi                                 }
44135440d18SAsmitha Karunanithi                                 dumpStatus = *status;
44235440d18SAsmitha Karunanithi                             }
44335440d18SAsmitha Karunanithi                         }
44435440d18SAsmitha Karunanithi                     }
44535440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
44635440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
4475cb1dd27SAsmitha Karunanithi                     {
4485cb1dd27SAsmitha Karunanithi 
4495cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
4505cb1dd27SAsmitha Karunanithi                         {
4515cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
4525cb1dd27SAsmitha Karunanithi                             {
45355f79e6fSEd Tanous                                 const auto* sizePtr =
4545cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4555cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
4565cb1dd27SAsmitha Karunanithi                                 {
4575cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4585cb1dd27SAsmitha Karunanithi                                     break;
4595cb1dd27SAsmitha Karunanithi                                 }
4605cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
4615cb1dd27SAsmitha Karunanithi                                 break;
4625cb1dd27SAsmitha Karunanithi                             }
4635cb1dd27SAsmitha Karunanithi                         }
4645cb1dd27SAsmitha Karunanithi                     }
4655cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
4665cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
4675cb1dd27SAsmitha Karunanithi                     {
4685cb1dd27SAsmitha Karunanithi 
4699eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
4705cb1dd27SAsmitha Karunanithi                         {
4715cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
4725cb1dd27SAsmitha Karunanithi                             {
4735cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
4745cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
4755cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
4765cb1dd27SAsmitha Karunanithi                                 {
4775cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
4785cb1dd27SAsmitha Karunanithi                                     break;
4795cb1dd27SAsmitha Karunanithi                                 }
4801d8782e7SNan Zhou                                 timestamp = (*usecsTimeStamp / 1000 / 1000);
4815cb1dd27SAsmitha Karunanithi                                 break;
4825cb1dd27SAsmitha Karunanithi                             }
4835cb1dd27SAsmitha Karunanithi                         }
4845cb1dd27SAsmitha Karunanithi                     }
4855cb1dd27SAsmitha Karunanithi                 }
4865cb1dd27SAsmitha Karunanithi 
4870fda0f12SGeorge Liu                 if (dumpStatus !=
4880fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
48935440d18SAsmitha Karunanithi                     !dumpStatus.empty())
49035440d18SAsmitha Karunanithi                 {
49135440d18SAsmitha Karunanithi                     // Dump status is not Complete, no need to enumerate
49235440d18SAsmitha Karunanithi                     continue;
49335440d18SAsmitha Karunanithi                 }
49435440d18SAsmitha Karunanithi 
495647b3cdcSGeorge Liu                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
4965cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
4975cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
4985cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
4991d8782e7SNan Zhou                 thisEntry["Created"] =
5001d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
5015cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5025cb1dd27SAsmitha Karunanithi 
503d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5045cb1dd27SAsmitha Karunanithi 
5055cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5065cb1dd27SAsmitha Karunanithi                 {
507d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
508d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
509de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
510de8d94a3SAbhishek Patel                         entryID + "/attachment";
5115cb1dd27SAsmitha Karunanithi                 }
5125cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5135cb1dd27SAsmitha Karunanithi                 {
514d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
515d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
516d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
517de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
518de8d94a3SAbhishek Patel                         entryID + "/attachment";
5195cb1dd27SAsmitha Karunanithi                 }
52035440d18SAsmitha Karunanithi                 entriesArray.push_back(std::move(thisEntry));
5215cb1dd27SAsmitha Karunanithi             }
5225cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5235cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5245cb1dd27SAsmitha Karunanithi         },
5255cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5265cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5275cb1dd27SAsmitha Karunanithi }
5285cb1dd27SAsmitha Karunanithi 
5298d1b46d7Szhanghch05 inline void
5308d1b46d7Szhanghch05     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5318d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5325cb1dd27SAsmitha Karunanithi {
5335cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5345cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5355cb1dd27SAsmitha Karunanithi     {
5365cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5375cb1dd27SAsmitha Karunanithi     }
5385cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5395cb1dd27SAsmitha Karunanithi     {
5405cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5415cb1dd27SAsmitha Karunanithi     }
5425cb1dd27SAsmitha Karunanithi     else
5435cb1dd27SAsmitha Karunanithi     {
5445cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5455cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5465cb1dd27SAsmitha Karunanithi         return;
5475cb1dd27SAsmitha Karunanithi     }
5485cb1dd27SAsmitha Karunanithi 
5495cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
550711ac7a9SEd Tanous         [asyncResp, entryID, dumpPath,
551711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
552711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
5535cb1dd27SAsmitha Karunanithi             if (ec)
5545cb1dd27SAsmitha Karunanithi             {
5555cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5565cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
5575cb1dd27SAsmitha Karunanithi                 return;
5585cb1dd27SAsmitha Karunanithi             }
5595cb1dd27SAsmitha Karunanithi 
560b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
561b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
562b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
563b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
564b47452b2SAsmitha Karunanithi                 "/entry/";
565b47452b2SAsmitha Karunanithi 
5669eb808c1SEd Tanous             for (const auto& objectPath : resp)
5675cb1dd27SAsmitha Karunanithi             {
568b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
5695cb1dd27SAsmitha Karunanithi                 {
5705cb1dd27SAsmitha Karunanithi                     continue;
5715cb1dd27SAsmitha Karunanithi                 }
5725cb1dd27SAsmitha Karunanithi 
5735cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
5741d8782e7SNan Zhou                 uint64_t timestamp = 0;
5755cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
57635440d18SAsmitha Karunanithi                 std::string dumpStatus;
5775cb1dd27SAsmitha Karunanithi 
5789eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
5795cb1dd27SAsmitha Karunanithi                 {
58035440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
58135440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
58235440d18SAsmitha Karunanithi                     {
5839eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
58435440d18SAsmitha Karunanithi                         {
58535440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
58635440d18SAsmitha Karunanithi                             {
5879eb808c1SEd Tanous                                 const std::string* status =
5889eb808c1SEd Tanous                                     std::get_if<std::string>(
58935440d18SAsmitha Karunanithi                                         &propertyMap.second);
59035440d18SAsmitha Karunanithi                                 if (status == nullptr)
59135440d18SAsmitha Karunanithi                                 {
59235440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
59335440d18SAsmitha Karunanithi                                     break;
59435440d18SAsmitha Karunanithi                                 }
59535440d18SAsmitha Karunanithi                                 dumpStatus = *status;
59635440d18SAsmitha Karunanithi                             }
59735440d18SAsmitha Karunanithi                         }
59835440d18SAsmitha Karunanithi                     }
59935440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
60035440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
6015cb1dd27SAsmitha Karunanithi                     {
6029eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
6035cb1dd27SAsmitha Karunanithi                         {
6045cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
6055cb1dd27SAsmitha Karunanithi                             {
6069eb808c1SEd Tanous                                 const uint64_t* sizePtr =
6075cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6085cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
6095cb1dd27SAsmitha Karunanithi                                 {
6105cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6115cb1dd27SAsmitha Karunanithi                                     break;
6125cb1dd27SAsmitha Karunanithi                                 }
6135cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
6145cb1dd27SAsmitha Karunanithi                                 break;
6155cb1dd27SAsmitha Karunanithi                             }
6165cb1dd27SAsmitha Karunanithi                         }
6175cb1dd27SAsmitha Karunanithi                     }
6185cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6195cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6205cb1dd27SAsmitha Karunanithi                     {
6219eb808c1SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
6225cb1dd27SAsmitha Karunanithi                         {
6235cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6245cb1dd27SAsmitha Karunanithi                             {
6255cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6265cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6275cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6285cb1dd27SAsmitha Karunanithi                                 {
6295cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6305cb1dd27SAsmitha Karunanithi                                     break;
6315cb1dd27SAsmitha Karunanithi                                 }
6321d8782e7SNan Zhou                                 timestamp = *usecsTimeStamp / 1000 / 1000;
6335cb1dd27SAsmitha Karunanithi                                 break;
6345cb1dd27SAsmitha Karunanithi                             }
6355cb1dd27SAsmitha Karunanithi                         }
6365cb1dd27SAsmitha Karunanithi                     }
6375cb1dd27SAsmitha Karunanithi                 }
6385cb1dd27SAsmitha Karunanithi 
6390fda0f12SGeorge Liu                 if (dumpStatus !=
6400fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
64135440d18SAsmitha Karunanithi                     !dumpStatus.empty())
64235440d18SAsmitha Karunanithi                 {
64335440d18SAsmitha Karunanithi                     // Dump status is not Complete
64435440d18SAsmitha Karunanithi                     // return not found until status is changed to Completed
64535440d18SAsmitha Karunanithi                     messages::resourceNotFound(asyncResp->res,
64635440d18SAsmitha Karunanithi                                                dumpType + " dump", entryID);
64735440d18SAsmitha Karunanithi                     return;
64835440d18SAsmitha Karunanithi                 }
64935440d18SAsmitha Karunanithi 
6505cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
651647b3cdcSGeorge Liu                     "#LogEntry.v1_8_0.LogEntry";
6525cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
6535cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
6545cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
6555cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
6561d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
6575cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
6585cb1dd27SAsmitha Karunanithi 
659d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6605cb1dd27SAsmitha Karunanithi 
6615cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
6625cb1dd27SAsmitha Karunanithi                 {
663d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
664d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
665de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
666de8d94a3SAbhishek Patel                         entryID + "/attachment";
6675cb1dd27SAsmitha Karunanithi                 }
6685cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
6695cb1dd27SAsmitha Karunanithi                 {
670d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
671d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
6725cb1dd27SAsmitha Karunanithi                         "System";
673d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
674de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
675de8d94a3SAbhishek Patel                         entryID + "/attachment";
6765cb1dd27SAsmitha Karunanithi                 }
6775cb1dd27SAsmitha Karunanithi             }
678e05aec50SEd Tanous             if (!foundDumpEntry)
679b47452b2SAsmitha Karunanithi             {
680b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
681b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
682b47452b2SAsmitha Karunanithi                 return;
683b47452b2SAsmitha Karunanithi             }
6845cb1dd27SAsmitha Karunanithi         },
6855cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6865cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6875cb1dd27SAsmitha Karunanithi }
6885cb1dd27SAsmitha Karunanithi 
6898d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6909878256fSStanley Chu                             const std::string& entryID,
691b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6925cb1dd27SAsmitha Karunanithi {
6933de8d8baSGeorge Liu     auto respHandler = [asyncResp,
6943de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
6955cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6965cb1dd27SAsmitha Karunanithi         if (ec)
6975cb1dd27SAsmitha Karunanithi         {
6983de8d8baSGeorge Liu             if (ec.value() == EBADR)
6993de8d8baSGeorge Liu             {
7003de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
7013de8d8baSGeorge Liu                 return;
7023de8d8baSGeorge Liu             }
7035cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
7045cb1dd27SAsmitha Karunanithi                              << ec;
7055cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
7065cb1dd27SAsmitha Karunanithi             return;
7075cb1dd27SAsmitha Karunanithi         }
7085cb1dd27SAsmitha Karunanithi     };
7095cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7105cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
711b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
712b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
713b47452b2SAsmitha Karunanithi             entryID,
7145cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7155cb1dd27SAsmitha Karunanithi }
7165cb1dd27SAsmitha Karunanithi 
7178d1b46d7Szhanghch05 inline void
71898be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
7198d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7208d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
721a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
722a43be80fSAsmitha Karunanithi {
723a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7246145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
725a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
726a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
727cb13a392SEd Tanous             if (err)
728cb13a392SEd Tanous             {
7296145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7306145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7316145ed6fSAsmitha Karunanithi                 return task::completed;
732cb13a392SEd Tanous             }
733*b9d36b47SEd Tanous 
734*b9d36b47SEd Tanous             dbus::utility::DBusInteracesMap interfacesList;
735a43be80fSAsmitha Karunanithi 
736a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
737a43be80fSAsmitha Karunanithi 
738a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
739a43be80fSAsmitha Karunanithi 
740b47452b2SAsmitha Karunanithi             if (objPath.str ==
741b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
742b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
743b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
744a43be80fSAsmitha Karunanithi             {
745a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
746a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
747a43be80fSAsmitha Karunanithi 
748a43be80fSAsmitha Karunanithi                 std::string headerLoc =
749a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
750a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
751a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
752a43be80fSAsmitha Karunanithi 
753a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
754b47452b2SAsmitha Karunanithi                 return task::completed;
7556145ed6fSAsmitha Karunanithi             }
756a43be80fSAsmitha Karunanithi             return task::completed;
757a43be80fSAsmitha Karunanithi         },
7584978b63fSJason M. Bills         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
759a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
760a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
761a43be80fSAsmitha Karunanithi 
762a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
763a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
76498be3e39SEd Tanous     task->payload.emplace(std::move(payload));
765a43be80fSAsmitha Karunanithi }
766a43be80fSAsmitha Karunanithi 
7678d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7688d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
769a43be80fSAsmitha Karunanithi {
770a43be80fSAsmitha Karunanithi 
771a43be80fSAsmitha Karunanithi     std::string dumpPath;
772a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
773a43be80fSAsmitha Karunanithi     {
774a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
775a43be80fSAsmitha Karunanithi     }
776a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
777a43be80fSAsmitha Karunanithi     {
778a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
779a43be80fSAsmitha Karunanithi     }
780a43be80fSAsmitha Karunanithi     else
781a43be80fSAsmitha Karunanithi     {
782a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
783a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
784a43be80fSAsmitha Karunanithi         return;
785a43be80fSAsmitha Karunanithi     }
786a43be80fSAsmitha Karunanithi 
787a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
788a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
789a43be80fSAsmitha Karunanithi 
79015ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
791a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
792a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
793a43be80fSAsmitha Karunanithi     {
794a43be80fSAsmitha Karunanithi         return;
795a43be80fSAsmitha Karunanithi     }
796a43be80fSAsmitha Karunanithi 
797a43be80fSAsmitha Karunanithi     if (dumpType == "System")
798a43be80fSAsmitha Karunanithi     {
799a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
800a43be80fSAsmitha Karunanithi         {
8014978b63fSJason M. Bills             BMCWEB_LOG_ERROR
8024978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
803a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
804a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
805a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
806a43be80fSAsmitha Karunanithi             return;
807a43be80fSAsmitha Karunanithi         }
8083174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
809a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
810a43be80fSAsmitha Karunanithi         {
811a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
812ace85d60SEd Tanous             messages::internalError(asyncResp->res);
813a43be80fSAsmitha Karunanithi             return;
814a43be80fSAsmitha Karunanithi         }
815a43be80fSAsmitha Karunanithi     }
816a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
817a43be80fSAsmitha Karunanithi     {
818a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
819a43be80fSAsmitha Karunanithi         {
8200fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8210fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
822a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
823a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
824a43be80fSAsmitha Karunanithi             return;
825a43be80fSAsmitha Karunanithi         }
8263174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
827a43be80fSAsmitha Karunanithi         {
828a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
829a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
830ace85d60SEd Tanous             messages::internalError(asyncResp->res);
831a43be80fSAsmitha Karunanithi             return;
832a43be80fSAsmitha Karunanithi         }
833a43be80fSAsmitha Karunanithi     }
834a43be80fSAsmitha Karunanithi 
835a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
83698be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
83798be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
83898be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
839a43be80fSAsmitha Karunanithi             if (ec)
840a43be80fSAsmitha Karunanithi             {
841a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
842a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
843a43be80fSAsmitha Karunanithi                 return;
844a43be80fSAsmitha Karunanithi             }
845a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
846a43be80fSAsmitha Karunanithi 
84798be3e39SEd Tanous             createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
84898be3e39SEd Tanous                                    dumpPath, dumpType);
849a43be80fSAsmitha Karunanithi         },
850b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
851b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
852b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
853a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
854a43be80fSAsmitha Karunanithi }
855a43be80fSAsmitha Karunanithi 
8568d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8578d1b46d7Szhanghch05                       const std::string& dumpType)
85880319af1SAsmitha Karunanithi {
859b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
860b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
8618d1b46d7Szhanghch05 
86280319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
863*b9d36b47SEd Tanous         [asyncResp, dumpType](
864*b9d36b47SEd Tanous             const boost::system::error_code ec,
865*b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
86680319af1SAsmitha Karunanithi             if (ec)
86780319af1SAsmitha Karunanithi             {
86880319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
86980319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
87080319af1SAsmitha Karunanithi                 return;
87180319af1SAsmitha Karunanithi             }
87280319af1SAsmitha Karunanithi 
87380319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
87480319af1SAsmitha Karunanithi             {
8752dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
8762dfd18efSEd Tanous                 std::string logID = objPath.filename();
8772dfd18efSEd Tanous                 if (logID.empty())
87880319af1SAsmitha Karunanithi                 {
8792dfd18efSEd Tanous                     continue;
88080319af1SAsmitha Karunanithi                 }
8812dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
88280319af1SAsmitha Karunanithi             }
88380319af1SAsmitha Karunanithi         },
88480319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
88580319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
88680319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
887b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
888b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
889b47452b2SAsmitha Karunanithi                                    dumpType});
89080319af1SAsmitha Karunanithi }
89180319af1SAsmitha Karunanithi 
892*b9d36b47SEd Tanous inline static void
893*b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
894*b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
895*b9d36b47SEd Tanous                              std::string& logfile)
896043a0536SJohnathan Mantey {
897043a0536SJohnathan Mantey     for (auto property : params)
898043a0536SJohnathan Mantey     {
899043a0536SJohnathan Mantey         if (property.first == "Timestamp")
900043a0536SJohnathan Mantey         {
901043a0536SJohnathan Mantey             const std::string* value =
9028d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
903043a0536SJohnathan Mantey             if (value != nullptr)
904043a0536SJohnathan Mantey             {
905043a0536SJohnathan Mantey                 timestamp = *value;
906043a0536SJohnathan Mantey             }
907043a0536SJohnathan Mantey         }
908043a0536SJohnathan Mantey         else if (property.first == "Filename")
909043a0536SJohnathan Mantey         {
910043a0536SJohnathan Mantey             const std::string* value =
9118d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
912043a0536SJohnathan Mantey             if (value != nullptr)
913043a0536SJohnathan Mantey             {
914043a0536SJohnathan Mantey                 filename = *value;
915043a0536SJohnathan Mantey             }
916043a0536SJohnathan Mantey         }
917043a0536SJohnathan Mantey         else if (property.first == "Log")
918043a0536SJohnathan Mantey         {
919043a0536SJohnathan Mantey             const std::string* value =
9208d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
921043a0536SJohnathan Mantey             if (value != nullptr)
922043a0536SJohnathan Mantey             {
923043a0536SJohnathan Mantey                 logfile = *value;
924043a0536SJohnathan Mantey             }
925043a0536SJohnathan Mantey         }
926043a0536SJohnathan Mantey     }
927043a0536SJohnathan Mantey }
928043a0536SJohnathan Mantey 
929a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
9307e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9311da66f75SEd Tanous {
932c4bf6374SJason M. Bills     /**
933c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
934c4bf6374SJason M. Bills      */
9357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
936ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
9377e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9387e860f15SJohn Edward Broadbent             [](const crow::Request&,
9397e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
9407e860f15SJohn Edward Broadbent 
941c4bf6374SJason M. Bills             {
9427e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
9437e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
944c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
945c4bf6374SJason M. Bills                     "#LogServiceCollection.LogServiceCollection";
946c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
947029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices";
9487e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
9497e860f15SJohn Edward Broadbent                     "System Log Services Collection";
950c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
951c4bf6374SJason M. Bills                     "Collection of LogServices for this Computer System";
9527e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
9537e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
954c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
955029573d4SEd Tanous                 logServiceArray.push_back(
9567e860f15SJohn Edward Broadbent                     {{"@odata.id",
9577e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/EventLog"}});
9585cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
959c9bb6861Sraviteja-b                 logServiceArray.push_back(
9607e860f15SJohn Edward Broadbent                     {{"@odata.id",
9617e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/Dump"}});
962c9bb6861Sraviteja-b #endif
963c9bb6861Sraviteja-b 
964d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
965d53dd41fSJason M. Bills                 logServiceArray.push_back(
966cb92c03bSAndrew Geissler                     {{"@odata.id",
967424c4176SJason M. Bills                       "/redfish/v1/Systems/system/LogServices/Crashdump"}});
968d53dd41fSJason M. Bills #endif
969b7028ebfSSpencer Ku 
970b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
971b7028ebfSSpencer Ku                 logServiceArray.push_back(
972b7028ebfSSpencer Ku                     {{"@odata.id",
973b7028ebfSSpencer Ku                       "/redfish/v1/Systems/system/LogServices/HostLogger"}});
974b7028ebfSSpencer Ku #endif
975c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
976c4bf6374SJason M. Bills                     logServiceArray.size();
977a3316fc6SZhikuiRen 
978a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
979*b9d36b47SEd Tanous                     [asyncResp](
980*b9d36b47SEd Tanous                         const boost::system::error_code ec,
981*b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
982*b9d36b47SEd Tanous                             subtreePath) {
983a3316fc6SZhikuiRen                         if (ec)
984a3316fc6SZhikuiRen                         {
985a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR << ec;
986a3316fc6SZhikuiRen                             return;
987a3316fc6SZhikuiRen                         }
988a3316fc6SZhikuiRen 
98955f79e6fSEd Tanous                         for (const auto& pathStr : subtreePath)
990a3316fc6SZhikuiRen                         {
991a3316fc6SZhikuiRen                             if (pathStr.find("PostCode") != std::string::npos)
992a3316fc6SZhikuiRen                             {
99323a21a1cSEd Tanous                                 nlohmann::json& logServiceArrayLocal =
994a3316fc6SZhikuiRen                                     asyncResp->res.jsonValue["Members"];
99523a21a1cSEd Tanous                                 logServiceArrayLocal.push_back(
9960fda0f12SGeorge Liu                                     {{"@odata.id",
9970fda0f12SGeorge Liu                                       "/redfish/v1/Systems/system/LogServices/PostCodes"}});
9987e860f15SJohn Edward Broadbent                                 asyncResp->res
9997e860f15SJohn Edward Broadbent                                     .jsonValue["Members@odata.count"] =
100023a21a1cSEd Tanous                                     logServiceArrayLocal.size();
1001a3316fc6SZhikuiRen                                 return;
1002a3316fc6SZhikuiRen                             }
1003a3316fc6SZhikuiRen                         }
1004a3316fc6SZhikuiRen                     },
1005a3316fc6SZhikuiRen                     "xyz.openbmc_project.ObjectMapper",
1006a3316fc6SZhikuiRen                     "/xyz/openbmc_project/object_mapper",
10077e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
10087e860f15SJohn Edward Broadbent                     0, std::array<const char*, 1>{postCodeIface});
10097e860f15SJohn Edward Broadbent             });
1010c4bf6374SJason M. Bills }
1011c4bf6374SJason M. Bills 
10127e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1013c4bf6374SJason M. Bills {
10147e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
1015ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
10167e860f15SJohn Edward Broadbent         .methods(
10177e860f15SJohn Edward Broadbent             boost::beast::http::verb::
10187e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
10197e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1020c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1021029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
1022c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1023c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
1024c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
10257e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
10267e860f15SJohn Edward Broadbent                 "System Event Log Service";
1027c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
1028c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10297c8c4058STejas Patil 
10307c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
10317c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
10327c8c4058STejas Patil 
10337c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10347c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10357c8c4058STejas Patil                 redfishDateTimeOffset.second;
10367c8c4058STejas Patil 
1037c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
1038c4bf6374SJason M. Bills                 {"@odata.id",
1039029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1040e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1041e7d6c8b2SGunnar Mills 
10420fda0f12SGeorge Liu                 {"target",
10430fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10447e860f15SJohn Edward Broadbent         });
1045489640c6SJason M. Bills }
1046489640c6SJason M. Bills 
10477e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1048489640c6SJason M. Bills {
10494978b63fSJason M. Bills     BMCWEB_ROUTE(
10504978b63fSJason M. Bills         app,
10514978b63fSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
1052432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
10537e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
10547e860f15SJohn Edward Broadbent             [](const crow::Request&,
10557e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1056489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1057489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1058489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1059489640c6SJason M. Bills                 {
1060489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1061489640c6SJason M. Bills                     {
1062489640c6SJason M. Bills                         std::error_code ec;
1063489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1064489640c6SJason M. Bills                     }
1065489640c6SJason M. Bills                 }
1066489640c6SJason M. Bills 
1067489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1068489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1069489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1070489640c6SJason M. Bills                         if (ec)
1071489640c6SJason M. Bills                         {
10727e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
10737e860f15SJohn Edward Broadbent                                              << ec;
1074489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1075489640c6SJason M. Bills                             return;
1076489640c6SJason M. Bills                         }
1077489640c6SJason M. Bills 
1078489640c6SJason M. Bills                         messages::success(asyncResp->res);
1079489640c6SJason M. Bills                     },
1080489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
10817e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
10827e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
10837e860f15SJohn Edward Broadbent             });
1084c4bf6374SJason M. Bills }
1085c4bf6374SJason M. Bills 
108695820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1087b5a76932SEd Tanous                                  const std::string& logEntry,
108895820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1089c4bf6374SJason M. Bills {
109095820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1091cd225da8SJason M. Bills     // First get the Timestamp
1092f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1093cd225da8SJason M. Bills     if (space == std::string::npos)
109495820184SJason M. Bills     {
109595820184SJason M. Bills         return 1;
109695820184SJason M. Bills     }
1097cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1098cd225da8SJason M. Bills     // Then get the log contents
1099f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1100cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1101cd225da8SJason M. Bills     {
1102cd225da8SJason M. Bills         return 1;
1103cd225da8SJason M. Bills     }
1104cd225da8SJason M. Bills     std::string_view entry(logEntry);
1105cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1106cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1107cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1108cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1109cd225da8SJason M. Bills                  boost::token_compress_on);
1110cd225da8SJason M. Bills     // We need at least a MessageId to be valid
111126f6976fSEd Tanous     if (logEntryFields.empty())
1112cd225da8SJason M. Bills     {
1113cd225da8SJason M. Bills         return 1;
1114cd225da8SJason M. Bills     }
1115cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
111695820184SJason M. Bills 
11174851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1118fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1119c4bf6374SJason M. Bills 
11204851d45dSJason M. Bills     std::string msg;
11214851d45dSJason M. Bills     std::string severity;
11224851d45dSJason M. Bills     if (message != nullptr)
1123c4bf6374SJason M. Bills     {
11244851d45dSJason M. Bills         msg = message->message;
11255f2b84eeSEd Tanous         severity = message->messageSeverity;
1126c4bf6374SJason M. Bills     }
1127c4bf6374SJason M. Bills 
112815a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
112926702d01SEd Tanous     std::span<std::string> messageArgs;
113015a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
113115a86ff6SJason M. Bills     {
113215a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
113315a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
113415a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
113515a86ff6SJason M. Bills         if (!messageArgsStart.empty())
113615a86ff6SJason M. Bills         {
113715a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
113815a86ff6SJason M. Bills         }
113915a86ff6SJason M. Bills 
114023a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1141c4bf6374SJason M. Bills 
11424851d45dSJason M. Bills         // Fill the MessageArgs into the Message
114395820184SJason M. Bills         int i = 0;
114495820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
11454851d45dSJason M. Bills         {
114695820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
11474851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
11484851d45dSJason M. Bills             if (argPos != std::string::npos)
11494851d45dSJason M. Bills             {
115095820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
11514851d45dSJason M. Bills             }
11524851d45dSJason M. Bills         }
115315a86ff6SJason M. Bills     }
11544851d45dSJason M. Bills 
115595820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
115695820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
115795820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1158f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1159f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
116095820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1161c4bf6374SJason M. Bills     {
116295820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1163c4bf6374SJason M. Bills     }
1164c4bf6374SJason M. Bills 
1165c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
116695820184SJason M. Bills     logEntryJson = {
1167647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1168029573d4SEd Tanous         {"@odata.id",
1169897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
117095820184SJason M. Bills              logEntryID},
1171c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
117295820184SJason M. Bills         {"Id", logEntryID},
117395820184SJason M. Bills         {"Message", std::move(msg)},
117495820184SJason M. Bills         {"MessageId", std::move(messageID)},
1175f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1176c4bf6374SJason M. Bills         {"EntryType", "Event"},
117795820184SJason M. Bills         {"Severity", std::move(severity)},
117895820184SJason M. Bills         {"Created", std::move(timestamp)}};
1179c4bf6374SJason M. Bills     return 0;
1180c4bf6374SJason M. Bills }
1181c4bf6374SJason M. Bills 
11827e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1183c4bf6374SJason M. Bills {
11847e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11857e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11868b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
11874978b63fSJason M. Bills         .methods(
11884978b63fSJason M. Bills             boost::beast::http::verb::
11894978b63fSJason M. Bills                 get)([](const crow::Request& req,
11907e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1191271584abSEd Tanous             uint64_t skip = 0;
1192271584abSEd Tanous             uint64_t top = maxEntriesPerPage; // Show max entries by default
11938d1b46d7Szhanghch05             if (!getSkipParam(asyncResp, req, skip))
1194c4bf6374SJason M. Bills             {
1195c4bf6374SJason M. Bills                 return;
1196c4bf6374SJason M. Bills             }
11978d1b46d7Szhanghch05             if (!getTopParam(asyncResp, req, top))
1198c4bf6374SJason M. Bills             {
1199c4bf6374SJason M. Bills                 return;
1200c4bf6374SJason M. Bills             }
12017e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
12027e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
1203c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1204c4bf6374SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
1205c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1206029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1207c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1208c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Description"] =
1209c4bf6374SJason M. Bills                 "Collection of System Event Log Entries";
1210cb92c03bSAndrew Geissler 
12114978b63fSJason M. Bills             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1212c4bf6374SJason M. Bills             logEntryArray = nlohmann::json::array();
12137e860f15SJohn Edward Broadbent             // Go through the log files and create a unique ID for each
12147e860f15SJohn Edward Broadbent             // entry
121595820184SJason M. Bills             std::vector<std::filesystem::path> redfishLogFiles;
121695820184SJason M. Bills             getRedfishLogFiles(redfishLogFiles);
1217b01bf299SEd Tanous             uint64_t entryCount = 0;
1218cd225da8SJason M. Bills             std::string logEntry;
121995820184SJason M. Bills 
12207e860f15SJohn Edward Broadbent             // Oldest logs are in the last file, so start there and loop
12217e860f15SJohn Edward Broadbent             // backwards
12227e860f15SJohn Edward Broadbent             for (auto it = redfishLogFiles.rbegin();
12237e860f15SJohn Edward Broadbent                  it < redfishLogFiles.rend(); it++)
1224c4bf6374SJason M. Bills             {
1225cd225da8SJason M. Bills                 std::ifstream logStream(*it);
122695820184SJason M. Bills                 if (!logStream.is_open())
1227c4bf6374SJason M. Bills                 {
1228c4bf6374SJason M. Bills                     continue;
1229c4bf6374SJason M. Bills                 }
1230c4bf6374SJason M. Bills 
1231e85d6b16SJason M. Bills                 // Reset the unique ID on the first entry
1232e85d6b16SJason M. Bills                 bool firstEntry = true;
123395820184SJason M. Bills                 while (std::getline(logStream, logEntry))
123495820184SJason M. Bills                 {
1235c4bf6374SJason M. Bills                     entryCount++;
12367e860f15SJohn Edward Broadbent                     // Handle paging using skip (number of entries to skip
12377e860f15SJohn Edward Broadbent                     // from the start) and top (number of entries to
12387e860f15SJohn Edward Broadbent                     // display)
1239c4bf6374SJason M. Bills                     if (entryCount <= skip || entryCount > skip + top)
1240c4bf6374SJason M. Bills                     {
1241c4bf6374SJason M. Bills                         continue;
1242c4bf6374SJason M. Bills                     }
1243c4bf6374SJason M. Bills 
1244c4bf6374SJason M. Bills                     std::string idStr;
1245e85d6b16SJason M. Bills                     if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1246c4bf6374SJason M. Bills                     {
1247c4bf6374SJason M. Bills                         continue;
1248c4bf6374SJason M. Bills                     }
1249c4bf6374SJason M. Bills 
1250e85d6b16SJason M. Bills                     if (firstEntry)
1251e85d6b16SJason M. Bills                     {
1252e85d6b16SJason M. Bills                         firstEntry = false;
1253e85d6b16SJason M. Bills                     }
1254e85d6b16SJason M. Bills 
1255c4bf6374SJason M. Bills                     logEntryArray.push_back({});
1256c4bf6374SJason M. Bills                     nlohmann::json& bmcLogEntry = logEntryArray.back();
12574978b63fSJason M. Bills                     if (fillEventLogEntryJson(idStr, logEntry, bmcLogEntry) !=
12584978b63fSJason M. Bills                         0)
1259c4bf6374SJason M. Bills                     {
1260c4bf6374SJason M. Bills                         messages::internalError(asyncResp->res);
1261c4bf6374SJason M. Bills                         return;
1262c4bf6374SJason M. Bills                     }
1263c4bf6374SJason M. Bills                 }
126495820184SJason M. Bills             }
1265c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1266c4bf6374SJason M. Bills             if (skip + top < entryCount)
1267c4bf6374SJason M. Bills             {
1268c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
12694978b63fSJason M. Bills                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
1270c4bf6374SJason M. Bills                     std::to_string(skip + top);
1271c4bf6374SJason M. Bills             }
12727e860f15SJohn Edward Broadbent         });
1273897967deSJason M. Bills }
1274897967deSJason M. Bills 
12757e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1276897967deSJason M. Bills {
12777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12787e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1279ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12807e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12817e860f15SJohn Edward Broadbent             [](const crow::Request&,
12827e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12837e860f15SJohn Edward Broadbent                const std::string& param) {
12847e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
12858d1b46d7Szhanghch05 
12867e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
12877e860f15SJohn Edward Broadbent                 // entry to find the target entry
1288897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1289897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1290897967deSJason M. Bills                 std::string logEntry;
1291897967deSJason M. Bills 
12927e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12937e860f15SJohn Edward Broadbent                 // backwards
12947e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12957e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1296897967deSJason M. Bills                 {
1297897967deSJason M. Bills                     std::ifstream logStream(*it);
1298897967deSJason M. Bills                     if (!logStream.is_open())
1299897967deSJason M. Bills                     {
1300897967deSJason M. Bills                         continue;
1301897967deSJason M. Bills                     }
1302897967deSJason M. Bills 
1303897967deSJason M. Bills                     // Reset the unique ID on the first entry
1304897967deSJason M. Bills                     bool firstEntry = true;
1305897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1306897967deSJason M. Bills                     {
1307897967deSJason M. Bills                         std::string idStr;
1308897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1309897967deSJason M. Bills                         {
1310897967deSJason M. Bills                             continue;
1311897967deSJason M. Bills                         }
1312897967deSJason M. Bills 
1313897967deSJason M. Bills                         if (firstEntry)
1314897967deSJason M. Bills                         {
1315897967deSJason M. Bills                             firstEntry = false;
1316897967deSJason M. Bills                         }
1317897967deSJason M. Bills 
1318897967deSJason M. Bills                         if (idStr == targetID)
1319897967deSJason M. Bills                         {
13207e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
13217e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1322897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1323897967deSJason M. Bills                             {
1324897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1325897967deSJason M. Bills                                 return;
1326897967deSJason M. Bills                             }
1327897967deSJason M. Bills                             return;
1328897967deSJason M. Bills                         }
1329897967deSJason M. Bills                     }
1330897967deSJason M. Bills                 }
1331897967deSJason M. Bills                 // Requested ID was not found
1332ace85d60SEd Tanous                 messages::resourceMissingAtURI(
1333ace85d60SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(targetID));
13347e860f15SJohn Edward Broadbent             });
133508a4e4b5SAnthony Wilson }
133608a4e4b5SAnthony Wilson 
13377e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
133808a4e4b5SAnthony Wilson {
13397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13407e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1341ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
13427e860f15SJohn Edward Broadbent         .methods(
13437e860f15SJohn Edward Broadbent             boost::beast::http::verb::
13447e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
13457e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
13467e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
13477e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
134808a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
134908a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
135008a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
135108a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
135208a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
135308a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
135408a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
135508a4e4b5SAnthony Wilson 
1356cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1357cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1358cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1359cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1360914e2d5dSEd Tanous                             const dbus::utility::ManagedObjectType& resp) {
1361cb92c03bSAndrew Geissler                     if (ec)
1362cb92c03bSAndrew Geissler                     {
1363cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1364cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1365cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1366cb92c03bSAndrew Geissler                             << ec;
1367cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1368cb92c03bSAndrew Geissler                         return;
1369cb92c03bSAndrew Geissler                     }
1370cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1371cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1372cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
13739eb808c1SEd Tanous                     for (const auto& objectPath : resp)
1374cb92c03bSAndrew Geissler                     {
1375914e2d5dSEd Tanous                         const uint32_t* id = nullptr;
1376c419c759SEd Tanous                         const uint64_t* timestamp = nullptr;
1377c419c759SEd Tanous                         const uint64_t* updateTimestamp = nullptr;
1378914e2d5dSEd Tanous                         const std::string* severity = nullptr;
1379914e2d5dSEd Tanous                         const std::string* message = nullptr;
1380914e2d5dSEd Tanous                         const std::string* filePath = nullptr;
138175710de2SXiaochao Ma                         bool resolved = false;
13829eb808c1SEd Tanous                         for (const auto& interfaceMap : objectPath.second)
1383f86bb901SAdriana Kobylak                         {
1384f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1385f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1386f86bb901SAdriana Kobylak                             {
13879eb808c1SEd Tanous                                 for (const auto& propertyMap :
13889eb808c1SEd Tanous                                      interfaceMap.second)
1389cb92c03bSAndrew Geissler                                 {
1390cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1391cb92c03bSAndrew Geissler                                     {
1392f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1393f86bb901SAdriana Kobylak                                             &propertyMap.second);
1394cb92c03bSAndrew Geissler                                     }
1395cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1396cb92c03bSAndrew Geissler                                     {
1397c419c759SEd Tanous                                         timestamp = std::get_if<uint64_t>(
1398f86bb901SAdriana Kobylak                                             &propertyMap.second);
13997e860f15SJohn Edward Broadbent                                     }
14007e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
14017e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
14027e860f15SJohn Edward Broadbent                                     {
1403c419c759SEd Tanous                                         updateTimestamp = std::get_if<uint64_t>(
14047e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14057e860f15SJohn Edward Broadbent                                     }
14067e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
14077e860f15SJohn Edward Broadbent                                     {
14087e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
14097e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14107e860f15SJohn Edward Broadbent                                     }
14117e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
14127e860f15SJohn Edward Broadbent                                     {
14137e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
14147e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14157e860f15SJohn Edward Broadbent                                     }
14167e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
14177e860f15SJohn Edward Broadbent                                     {
1418914e2d5dSEd Tanous                                         const bool* resolveptr =
1419914e2d5dSEd Tanous                                             std::get_if<bool>(
14207e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14217e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14227e860f15SJohn Edward Broadbent                                         {
14237e860f15SJohn Edward Broadbent                                             messages::internalError(
14247e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14257e860f15SJohn Edward Broadbent                                             return;
14267e860f15SJohn Edward Broadbent                                         }
14277e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14287e860f15SJohn Edward Broadbent                                     }
14297e860f15SJohn Edward Broadbent                                 }
14307e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14317e860f15SJohn Edward Broadbent                                     severity == nullptr)
14327e860f15SJohn Edward Broadbent                                 {
14337e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
14347e860f15SJohn Edward Broadbent                                     return;
14357e860f15SJohn Edward Broadbent                                 }
14367e860f15SJohn Edward Broadbent                             }
14377e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
14387e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
14397e860f15SJohn Edward Broadbent                             {
14409eb808c1SEd Tanous                                 for (const auto& propertyMap :
14419eb808c1SEd Tanous                                      interfaceMap.second)
14427e860f15SJohn Edward Broadbent                                 {
14437e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
14447e860f15SJohn Edward Broadbent                                     {
14457e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
14467e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14477e860f15SJohn Edward Broadbent                                     }
14487e860f15SJohn Edward Broadbent                                 }
14497e860f15SJohn Edward Broadbent                             }
14507e860f15SJohn Edward Broadbent                         }
14517e860f15SJohn Edward Broadbent                         // Object path without the
14527e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
14537e860f15SJohn Edward Broadbent                         // and continue.
14547e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
1455c419c759SEd Tanous                             severity == nullptr || timestamp == nullptr ||
1456c419c759SEd Tanous                             updateTimestamp == nullptr)
14577e860f15SJohn Edward Broadbent                         {
14587e860f15SJohn Edward Broadbent                             continue;
14597e860f15SJohn Edward Broadbent                         }
14607e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
14617e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
14627e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
14637e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
14640fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14657e860f15SJohn Edward Broadbent                             std::to_string(*id);
14667e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
14677e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
14687e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
14697e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
14707e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
14717e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
14727e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
14737e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
1474c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*timestamp);
14757e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
1476c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*updateTimestamp);
14777e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
14787e860f15SJohn Edward Broadbent                         {
14797e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
14800fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14817e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
14827e860f15SJohn Edward Broadbent                         }
14837e860f15SJohn Edward Broadbent                     }
14847e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
14857e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
14867e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
14877e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
14887e860f15SJohn Edward Broadbent                               });
14897e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
14907e860f15SJohn Edward Broadbent                         entriesArray.size();
14917e860f15SJohn Edward Broadbent                 },
14927e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
14937e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14947e860f15SJohn Edward Broadbent         });
14957e860f15SJohn Edward Broadbent }
14967e860f15SJohn Edward Broadbent 
14977e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
14987e860f15SJohn Edward Broadbent {
14997e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15007e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1501ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
15027e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
15037e860f15SJohn Edward Broadbent             [](const crow::Request&,
15047e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15057e860f15SJohn Edward Broadbent                const std::string& param)
15067e860f15SJohn Edward Broadbent 
15077e860f15SJohn Edward Broadbent             {
15087e860f15SJohn Edward Broadbent                 std::string entryID = param;
15097e860f15SJohn Edward Broadbent                 dbus::utility::escapePathForDbus(entryID);
15107e860f15SJohn Edward Broadbent 
15117e860f15SJohn Edward Broadbent                 // DBus implementation of EventLog/Entries
15127e860f15SJohn Edward Broadbent                 // Make call to Logging Service to find all log entry objects
15137e860f15SJohn Edward Broadbent                 crow::connections::systemBus->async_method_call(
1514*b9d36b47SEd Tanous                     [asyncResp,
1515*b9d36b47SEd Tanous                      entryID](const boost::system::error_code ec,
1516*b9d36b47SEd Tanous                               const dbus::utility::DBusPropertiesMap& resp) {
15177e860f15SJohn Edward Broadbent                         if (ec.value() == EBADR)
15187e860f15SJohn Edward Broadbent                         {
15197e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
15207e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogEntry", entryID);
15217e860f15SJohn Edward Broadbent                             return;
15227e860f15SJohn Edward Broadbent                         }
15237e860f15SJohn Edward Broadbent                         if (ec)
15247e860f15SJohn Edward Broadbent                         {
15250fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
15260fda0f12SGeorge Liu                                 << "EventLogEntry (DBus) resp_handler got error "
15277e860f15SJohn Edward Broadbent                                 << ec;
15287e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
15297e860f15SJohn Edward Broadbent                             return;
15307e860f15SJohn Edward Broadbent                         }
1531914e2d5dSEd Tanous                         const uint32_t* id = nullptr;
1532c419c759SEd Tanous                         const uint64_t* timestamp = nullptr;
1533c419c759SEd Tanous                         const uint64_t* updateTimestamp = nullptr;
1534914e2d5dSEd Tanous                         const std::string* severity = nullptr;
1535914e2d5dSEd Tanous                         const std::string* message = nullptr;
1536914e2d5dSEd Tanous                         const std::string* filePath = nullptr;
15377e860f15SJohn Edward Broadbent                         bool resolved = false;
15387e860f15SJohn Edward Broadbent 
15399eb808c1SEd Tanous                         for (const auto& propertyMap : resp)
15407e860f15SJohn Edward Broadbent                         {
15417e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Id")
15427e860f15SJohn Edward Broadbent                             {
15437e860f15SJohn Edward Broadbent                                 id = std::get_if<uint32_t>(&propertyMap.second);
15447e860f15SJohn Edward Broadbent                             }
15457e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Timestamp")
15467e860f15SJohn Edward Broadbent                             {
1547c419c759SEd Tanous                                 timestamp =
15487e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1549ebd45906SGeorge Liu                             }
1550d139c236SGeorge Liu                             else if (propertyMap.first == "UpdateTimestamp")
1551d139c236SGeorge Liu                             {
1552ebd45906SGeorge Liu                                 updateTimestamp =
1553c419c759SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
1554ebd45906SGeorge Liu                             }
1555cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1556cb92c03bSAndrew Geissler                             {
1557cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1558cb92c03bSAndrew Geissler                                     &propertyMap.second);
1559cb92c03bSAndrew Geissler                             }
1560cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1561cb92c03bSAndrew Geissler                             {
1562cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1563cb92c03bSAndrew Geissler                                     &propertyMap.second);
1564ae34c8e8SAdriana Kobylak                             }
156575710de2SXiaochao Ma                             else if (propertyMap.first == "Resolved")
156675710de2SXiaochao Ma                             {
1567914e2d5dSEd Tanous                                 const bool* resolveptr =
156875710de2SXiaochao Ma                                     std::get_if<bool>(&propertyMap.second);
156975710de2SXiaochao Ma                                 if (resolveptr == nullptr)
157075710de2SXiaochao Ma                                 {
157175710de2SXiaochao Ma                                     messages::internalError(asyncResp->res);
157275710de2SXiaochao Ma                                     return;
157375710de2SXiaochao Ma                                 }
157475710de2SXiaochao Ma                                 resolved = *resolveptr;
157575710de2SXiaochao Ma                             }
15767e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Path")
1577f86bb901SAdriana Kobylak                             {
1578f86bb901SAdriana Kobylak                                 filePath = std::get_if<std::string>(
1579f86bb901SAdriana Kobylak                                     &propertyMap.second);
1580f86bb901SAdriana Kobylak                             }
1581f86bb901SAdriana Kobylak                         }
1582f86bb901SAdriana Kobylak                         if (id == nullptr || message == nullptr ||
1583c419c759SEd Tanous                             severity == nullptr || timestamp == nullptr ||
1584c419c759SEd Tanous                             updateTimestamp == nullptr)
1585f86bb901SAdriana Kobylak                         {
1586ae34c8e8SAdriana Kobylak                             messages::internalError(asyncResp->res);
1587271584abSEd Tanous                             return;
1588271584abSEd Tanous                         }
1589f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.type"] =
1590f86bb901SAdriana Kobylak                             "#LogEntry.v1_8_0.LogEntry";
1591f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.id"] =
15920fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1593f86bb901SAdriana Kobylak                             std::to_string(*id);
15947e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Name"] =
15957e860f15SJohn Edward Broadbent                             "System Event Log Entry";
1596f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1597f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Message"] = *message;
1598f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Resolved"] = resolved;
1599f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["EntryType"] = "Event";
1600f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Severity"] =
1601f86bb901SAdriana Kobylak                             translateSeverityDbusToRedfish(*severity);
1602f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Created"] =
1603c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*timestamp);
1604f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Modified"] =
1605c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*updateTimestamp);
1606f86bb901SAdriana Kobylak                         if (filePath != nullptr)
1607f86bb901SAdriana Kobylak                         {
1608f86bb901SAdriana Kobylak                             asyncResp->res.jsonValue["AdditionalDataURI"] =
16090fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
16107e860f15SJohn Edward Broadbent                                 std::to_string(*id);
1611f86bb901SAdriana Kobylak                         }
1612cb92c03bSAndrew Geissler                     },
1613cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Logging",
1614cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging/entry/" + entryID,
1615f86bb901SAdriana Kobylak                     "org.freedesktop.DBus.Properties", "GetAll", "");
16167e860f15SJohn Edward Broadbent             });
1617336e96c6SChicago Duan 
16187e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16197e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1620ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16217e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
16227e860f15SJohn Edward Broadbent             [](const crow::Request& req,
16237e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16247e860f15SJohn Edward Broadbent                const std::string& entryId) {
162575710de2SXiaochao Ma                 std::optional<bool> resolved;
162675710de2SXiaochao Ma 
162715ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
16287e860f15SJohn Edward Broadbent                                               resolved))
162975710de2SXiaochao Ma                 {
163075710de2SXiaochao Ma                     return;
163175710de2SXiaochao Ma                 }
163275710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
163375710de2SXiaochao Ma 
163475710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
16354f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
163675710de2SXiaochao Ma                         if (ec)
163775710de2SXiaochao Ma                         {
163875710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
163975710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
164075710de2SXiaochao Ma                             return;
164175710de2SXiaochao Ma                         }
164275710de2SXiaochao Ma                     },
164375710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
164475710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
164575710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
164675710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
1647168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*resolved));
16487e860f15SJohn Edward Broadbent             });
164975710de2SXiaochao Ma 
16507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16517e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1652ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1653ed398213SEd Tanous 
16547e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
16557e860f15SJohn Edward Broadbent             [](const crow::Request&,
16567e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16577e860f15SJohn Edward Broadbent                const std::string& param)
16587e860f15SJohn Edward Broadbent 
1659336e96c6SChicago Duan             {
1660336e96c6SChicago Duan                 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1661336e96c6SChicago Duan 
16627e860f15SJohn Edward Broadbent                 std::string entryID = param;
1663336e96c6SChicago Duan 
1664336e96c6SChicago Duan                 dbus::utility::escapePathForDbus(entryID);
1665336e96c6SChicago Duan 
1666336e96c6SChicago Duan                 // Process response from Logging service.
16677e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp, entryID](
16687e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
16697e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
16707e860f15SJohn Edward Broadbent                         << "EventLogEntry (DBus) doDelete callback: Done";
1671336e96c6SChicago Duan                     if (ec)
1672336e96c6SChicago Duan                     {
16733de8d8baSGeorge Liu                         if (ec.value() == EBADR)
16743de8d8baSGeorge Liu                         {
16757e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
16767e860f15SJohn Edward Broadbent                                                        "LogEntry", entryID);
16773de8d8baSGeorge Liu                             return;
16783de8d8baSGeorge Liu                         }
1679336e96c6SChicago Duan                         // TODO Handle for specific error code
16800fda0f12SGeorge Liu                         BMCWEB_LOG_ERROR
16810fda0f12SGeorge Liu                             << "EventLogEntry (DBus) doDelete respHandler got error "
1682336e96c6SChicago Duan                             << ec;
1683336e96c6SChicago Duan                         asyncResp->res.result(
1684336e96c6SChicago Duan                             boost::beast::http::status::internal_server_error);
1685336e96c6SChicago Duan                         return;
1686336e96c6SChicago Duan                     }
1687336e96c6SChicago Duan 
1688336e96c6SChicago Duan                     asyncResp->res.result(boost::beast::http::status::ok);
1689336e96c6SChicago Duan                 };
1690336e96c6SChicago Duan 
1691336e96c6SChicago Duan                 // Make call to Logging service to request Delete Log
1692336e96c6SChicago Duan                 crow::connections::systemBus->async_method_call(
1693336e96c6SChicago Duan                     respHandler, "xyz.openbmc_project.Logging",
1694336e96c6SChicago Duan                     "/xyz/openbmc_project/logging/entry/" + entryID,
1695336e96c6SChicago Duan                     "xyz.openbmc_project.Object.Delete", "Delete");
16967e860f15SJohn Edward Broadbent             });
1697400fd1fbSAdriana Kobylak }
1698400fd1fbSAdriana Kobylak 
16997e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1700400fd1fbSAdriana Kobylak {
17010fda0f12SGeorge Liu     BMCWEB_ROUTE(
17020fda0f12SGeorge Liu         app,
17030fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1704ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
17067e860f15SJohn Edward Broadbent             [](const crow::Request& req,
17077e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17087e860f15SJohn Edward Broadbent                const std::string& param)
1709400fd1fbSAdriana Kobylak 
17107e860f15SJohn Edward Broadbent             {
1711647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
1712647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
1713400fd1fbSAdriana Kobylak                 {
17147e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17157e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1716400fd1fbSAdriana Kobylak                     return;
1717400fd1fbSAdriana Kobylak                 }
1718400fd1fbSAdriana Kobylak 
17197e860f15SJohn Edward Broadbent                 std::string entryID = param;
1720400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1721400fd1fbSAdriana Kobylak 
1722400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17237e860f15SJohn Edward Broadbent                     [asyncResp,
17247e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1725400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1726400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1727400fd1fbSAdriana Kobylak                         {
17287e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
17297e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1730400fd1fbSAdriana Kobylak                             return;
1731400fd1fbSAdriana Kobylak                         }
1732400fd1fbSAdriana Kobylak                         if (ec)
1733400fd1fbSAdriana Kobylak                         {
1734400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1735400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1736400fd1fbSAdriana Kobylak                             return;
1737400fd1fbSAdriana Kobylak                         }
1738400fd1fbSAdriana Kobylak 
1739400fd1fbSAdriana Kobylak                         int fd = -1;
1740400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1741400fd1fbSAdriana Kobylak                         if (fd == -1)
1742400fd1fbSAdriana Kobylak                         {
1743400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1744400fd1fbSAdriana Kobylak                             return;
1745400fd1fbSAdriana Kobylak                         }
1746400fd1fbSAdriana Kobylak 
1747400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1748400fd1fbSAdriana Kobylak                         if (size == -1)
1749400fd1fbSAdriana Kobylak                         {
1750400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1751400fd1fbSAdriana Kobylak                             return;
1752400fd1fbSAdriana Kobylak                         }
1753400fd1fbSAdriana Kobylak 
1754400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1755400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1756400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1757400fd1fbSAdriana Kobylak                         {
1758400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1759400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1760400fd1fbSAdriana Kobylak                                 << maxFileSize;
1761400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1762400fd1fbSAdriana Kobylak                             return;
1763400fd1fbSAdriana Kobylak                         }
1764400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1765400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1766400fd1fbSAdriana Kobylak                         if (rc == -1)
1767400fd1fbSAdriana Kobylak                         {
1768400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1769400fd1fbSAdriana Kobylak                             return;
1770400fd1fbSAdriana Kobylak                         }
1771400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1772400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1773400fd1fbSAdriana Kobylak                         {
1774400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1775400fd1fbSAdriana Kobylak                             return;
1776400fd1fbSAdriana Kobylak                         }
1777400fd1fbSAdriana Kobylak                         close(fd);
1778400fd1fbSAdriana Kobylak 
1779400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
17807e860f15SJohn Edward Broadbent                         std::string output =
17817e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1782400fd1fbSAdriana Kobylak 
1783400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1784400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
17857e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
17867e860f15SJohn Edward Broadbent                                                  "Base64");
1787400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1788400fd1fbSAdriana Kobylak                     },
1789400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1790400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1791400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
17927e860f15SJohn Edward Broadbent             });
17931da66f75SEd Tanous }
17941da66f75SEd Tanous 
1795b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1796b7028ebfSSpencer Ku 
1797b7028ebfSSpencer Ku inline bool
1798b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1799b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1800b7028ebfSSpencer Ku {
1801b7028ebfSSpencer Ku     std::error_code ec;
1802b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1803b7028ebfSSpencer Ku     if (ec)
1804b7028ebfSSpencer Ku     {
1805b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1806b7028ebfSSpencer Ku         return false;
1807b7028ebfSSpencer Ku     }
1808b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1809b7028ebfSSpencer Ku     {
1810b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1811b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1812b7028ebfSSpencer Ku         // path
1813b7028ebfSSpencer Ku         if (boost::starts_with(filename, "log"))
1814b7028ebfSSpencer Ku         {
1815b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1816b7028ebfSSpencer Ku         }
1817b7028ebfSSpencer Ku     }
1818b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1819b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1820b7028ebfSSpencer Ku     // descending order.
1821b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1822b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1823b7028ebfSSpencer Ku 
1824b7028ebfSSpencer Ku     return true;
1825b7028ebfSSpencer Ku }
1826b7028ebfSSpencer Ku 
1827b7028ebfSSpencer Ku inline bool
1828b7028ebfSSpencer Ku     getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1829b7028ebfSSpencer Ku                          uint64_t& skip, uint64_t& top,
1830b7028ebfSSpencer Ku                          std::vector<std::string>& logEntries, size_t& logCount)
1831b7028ebfSSpencer Ku {
1832b7028ebfSSpencer Ku     GzFileReader logFile;
1833b7028ebfSSpencer Ku 
1834b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1835b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1836b7028ebfSSpencer Ku     {
1837b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1838b7028ebfSSpencer Ku         {
1839b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1840b7028ebfSSpencer Ku             return false;
1841b7028ebfSSpencer Ku         }
1842b7028ebfSSpencer Ku     }
1843b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1844b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1845b7028ebfSSpencer Ku     if (!lastMessage.empty())
1846b7028ebfSSpencer Ku     {
1847b7028ebfSSpencer Ku         logCount++;
1848b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1849b7028ebfSSpencer Ku         {
1850b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1851b7028ebfSSpencer Ku         }
1852b7028ebfSSpencer Ku     }
1853b7028ebfSSpencer Ku     return true;
1854b7028ebfSSpencer Ku }
1855b7028ebfSSpencer Ku 
1856b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1857b7028ebfSSpencer Ku                                     const std::string& msg,
1858b7028ebfSSpencer Ku                                     nlohmann::json& logEntryJson)
1859b7028ebfSSpencer Ku {
1860b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
1861b7028ebfSSpencer Ku     logEntryJson = {
1862b7028ebfSSpencer Ku         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1863b7028ebfSSpencer Ku         {"@odata.id",
1864b7028ebfSSpencer Ku          "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1865b7028ebfSSpencer Ku              logEntryID},
1866b7028ebfSSpencer Ku         {"Name", "Host Logger Entry"},
1867b7028ebfSSpencer Ku         {"Id", logEntryID},
1868b7028ebfSSpencer Ku         {"Message", msg},
1869b7028ebfSSpencer Ku         {"EntryType", "Oem"},
1870b7028ebfSSpencer Ku         {"Severity", "OK"},
1871b7028ebfSSpencer Ku         {"OemRecordFormat", "Host Logger Entry"}};
1872b7028ebfSSpencer Ku }
1873b7028ebfSSpencer Ku 
1874b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1875b7028ebfSSpencer Ku {
1876b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1877b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
18780fda0f12SGeorge Liu         .methods(
18790fda0f12SGeorge Liu             boost::beast::http::verb::
18800fda0f12SGeorge Liu                 get)([](const crow::Request&,
1881b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1882b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1883b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger";
1884b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1885b7028ebfSSpencer Ku                 "#LogService.v1_1_0.LogService";
1886b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1887b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1888b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Id"] = "HostLogger";
1889b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Entries"] = {
18900fda0f12SGeorge Liu                 {"@odata.id",
18910fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1892b7028ebfSSpencer Ku         });
1893b7028ebfSSpencer Ku }
1894b7028ebfSSpencer Ku 
1895b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1896b7028ebfSSpencer Ku {
1897b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1898b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1899b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
19000fda0f12SGeorge Liu         .methods(
19010fda0f12SGeorge Liu             boost::beast::http::verb::
19020fda0f12SGeorge Liu                 get)([](const crow::Request& req,
1903b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1904b7028ebfSSpencer Ku             uint64_t skip = 0;
1905b7028ebfSSpencer Ku             uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1906b7028ebfSSpencer Ku                                               // default, allow range 1 to
1907b7028ebfSSpencer Ku                                               // 1000 entries per page.
1908b7028ebfSSpencer Ku             if (!getSkipParam(asyncResp, req, skip))
1909b7028ebfSSpencer Ku             {
1910b7028ebfSSpencer Ku                 return;
1911b7028ebfSSpencer Ku             }
1912b7028ebfSSpencer Ku             if (!getTopParam(asyncResp, req, top))
1913b7028ebfSSpencer Ku             {
1914b7028ebfSSpencer Ku                 return;
1915b7028ebfSSpencer Ku             }
1916b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1917b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1918b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1919b7028ebfSSpencer Ku                 "#LogEntryCollection.LogEntryCollection";
1920b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1921b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] =
1922b7028ebfSSpencer Ku                 "Collection of HostLogger Entries";
19230fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1924b7028ebfSSpencer Ku             logEntryArray = nlohmann::json::array();
1925b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = 0;
1926b7028ebfSSpencer Ku 
1927b7028ebfSSpencer Ku             std::vector<std::filesystem::path> hostLoggerFiles;
1928b7028ebfSSpencer Ku             if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1929b7028ebfSSpencer Ku             {
1930b7028ebfSSpencer Ku                 BMCWEB_LOG_ERROR << "fail to get host log file path";
1931b7028ebfSSpencer Ku                 return;
1932b7028ebfSSpencer Ku             }
1933b7028ebfSSpencer Ku 
1934b7028ebfSSpencer Ku             size_t logCount = 0;
1935b7028ebfSSpencer Ku             // This vector only store the entries we want to expose that
1936b7028ebfSSpencer Ku             // control by skip and top.
1937b7028ebfSSpencer Ku             std::vector<std::string> logEntries;
19380fda0f12SGeorge Liu             if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
19390fda0f12SGeorge Liu                                       logCount))
1940b7028ebfSSpencer Ku             {
1941b7028ebfSSpencer Ku                 messages::internalError(asyncResp->res);
1942b7028ebfSSpencer Ku                 return;
1943b7028ebfSSpencer Ku             }
1944b7028ebfSSpencer Ku             // If vector is empty, that means skip value larger than total
1945b7028ebfSSpencer Ku             // log count
194626f6976fSEd Tanous             if (logEntries.empty())
1947b7028ebfSSpencer Ku             {
1948b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1949b7028ebfSSpencer Ku                 return;
1950b7028ebfSSpencer Ku             }
195126f6976fSEd Tanous             if (!logEntries.empty())
1952b7028ebfSSpencer Ku             {
1953b7028ebfSSpencer Ku                 for (size_t i = 0; i < logEntries.size(); i++)
1954b7028ebfSSpencer Ku                 {
1955b7028ebfSSpencer Ku                     logEntryArray.push_back({});
1956b7028ebfSSpencer Ku                     nlohmann::json& hostLogEntry = logEntryArray.back();
1957b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(std::to_string(skip + i),
1958b7028ebfSSpencer Ku                                             logEntries[i], hostLogEntry);
1959b7028ebfSSpencer Ku                 }
1960b7028ebfSSpencer Ku 
1961b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1962b7028ebfSSpencer Ku                 if (skip + top < logCount)
1963b7028ebfSSpencer Ku                 {
1964b7028ebfSSpencer Ku                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
19650fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
1966b7028ebfSSpencer Ku                         std::to_string(skip + top);
1967b7028ebfSSpencer Ku                 }
1968b7028ebfSSpencer Ku             }
1969b7028ebfSSpencer Ku         });
1970b7028ebfSSpencer Ku }
1971b7028ebfSSpencer Ku 
1972b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1973b7028ebfSSpencer Ku {
1974b7028ebfSSpencer Ku     BMCWEB_ROUTE(
1975b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1976b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1977b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
1978ace85d60SEd Tanous             [](const crow::Request& req,
1979b7028ebfSSpencer Ku                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1980b7028ebfSSpencer Ku                const std::string& param) {
1981b7028ebfSSpencer Ku                 const std::string& targetID = param;
1982b7028ebfSSpencer Ku 
1983b7028ebfSSpencer Ku                 uint64_t idInt = 0;
1984ca45aa3cSEd Tanous 
1985ca45aa3cSEd Tanous                 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1986ca45aa3cSEd Tanous                 const char* end = targetID.data() + targetID.size();
1987ca45aa3cSEd Tanous 
1988ca45aa3cSEd Tanous                 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1989b7028ebfSSpencer Ku                 if (ec == std::errc::invalid_argument)
1990b7028ebfSSpencer Ku                 {
1991ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1992b7028ebfSSpencer Ku                     return;
1993b7028ebfSSpencer Ku                 }
1994b7028ebfSSpencer Ku                 if (ec == std::errc::result_out_of_range)
1995b7028ebfSSpencer Ku                 {
1996ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1997b7028ebfSSpencer Ku                     return;
1998b7028ebfSSpencer Ku                 }
1999b7028ebfSSpencer Ku 
2000b7028ebfSSpencer Ku                 std::vector<std::filesystem::path> hostLoggerFiles;
2001b7028ebfSSpencer Ku                 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2002b7028ebfSSpencer Ku                 {
2003b7028ebfSSpencer Ku                     BMCWEB_LOG_ERROR << "fail to get host log file path";
2004b7028ebfSSpencer Ku                     return;
2005b7028ebfSSpencer Ku                 }
2006b7028ebfSSpencer Ku 
2007b7028ebfSSpencer Ku                 size_t logCount = 0;
2008b7028ebfSSpencer Ku                 uint64_t top = 1;
2009b7028ebfSSpencer Ku                 std::vector<std::string> logEntries;
2010b7028ebfSSpencer Ku                 // We can get specific entry by skip and top. For example, if we
2011b7028ebfSSpencer Ku                 // want to get nth entry, we can set skip = n-1 and top = 1 to
2012b7028ebfSSpencer Ku                 // get that entry
2013b7028ebfSSpencer Ku                 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2014b7028ebfSSpencer Ku                                           logEntries, logCount))
2015b7028ebfSSpencer Ku                 {
2016b7028ebfSSpencer Ku                     messages::internalError(asyncResp->res);
2017b7028ebfSSpencer Ku                     return;
2018b7028ebfSSpencer Ku                 }
2019b7028ebfSSpencer Ku 
2020b7028ebfSSpencer Ku                 if (!logEntries.empty())
2021b7028ebfSSpencer Ku                 {
2022b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(targetID, logEntries[0],
2023b7028ebfSSpencer Ku                                             asyncResp->res.jsonValue);
2024b7028ebfSSpencer Ku                     return;
2025b7028ebfSSpencer Ku                 }
2026b7028ebfSSpencer Ku 
2027b7028ebfSSpencer Ku                 // Requested ID was not found
2028ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2029b7028ebfSSpencer Ku             });
2030b7028ebfSSpencer Ku }
2031b7028ebfSSpencer Ku 
20327e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
20331da66f75SEd Tanous {
20347e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2035ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
20367e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
20377e860f15SJohn Edward Broadbent             [](const crow::Request&,
20387e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
20397e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
20407e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
2041e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
20421da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
2043e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
2044e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
20457e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
20467e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
2047e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
20481da66f75SEd Tanous                     "Collection of LogServices for this Manager";
20497e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
20507e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2051c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
20525cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
20535cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
20547e860f15SJohn Edward Broadbent                     {{"@odata.id",
20557e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
20565cb1dd27SAsmitha Karunanithi #endif
2057c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2058c4bf6374SJason M. Bills                 logServiceArray.push_back(
20597e860f15SJohn Edward Broadbent                     {{"@odata.id",
20607e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2061c4bf6374SJason M. Bills #endif
2062e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2063c4bf6374SJason M. Bills                     logServiceArray.size();
20647e860f15SJohn Edward Broadbent             });
2065e1f26343SJason M. Bills }
2066e1f26343SJason M. Bills 
20677e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2068e1f26343SJason M. Bills {
20697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2070ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
20717e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
20727e860f15SJohn Edward Broadbent             [](const crow::Request&,
20737e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
20748d1b46d7Szhanghch05 
20757e860f15SJohn Edward Broadbent             {
2076e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
2077e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
20780f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
20790f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
20807e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
20817e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
20827e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
20837e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
2084c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2085e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
20867c8c4058STejas Patil 
20877c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
20887c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
20897c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
20907c8c4058STejas Patil                     redfishDateTimeOffset.first;
20917c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20927c8c4058STejas Patil                     redfishDateTimeOffset.second;
20937c8c4058STejas Patil 
2094cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
2095cd50aa42SJason M. Bills                     {"@odata.id",
2096086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
20977e860f15SJohn Edward Broadbent             });
2098e1f26343SJason M. Bills }
2099e1f26343SJason M. Bills 
2100c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2101e1f26343SJason M. Bills                                       sd_journal* journal,
2102c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
2103e1f26343SJason M. Bills {
2104e1f26343SJason M. Bills     // Get the Log Entry contents
2105e1f26343SJason M. Bills     int ret = 0;
2106e1f26343SJason M. Bills 
2107a8fe54f0SJason M. Bills     std::string message;
2108a8fe54f0SJason M. Bills     std::string_view syslogID;
2109a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2110a8fe54f0SJason M. Bills     if (ret < 0)
2111a8fe54f0SJason M. Bills     {
2112a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2113a8fe54f0SJason M. Bills                          << strerror(-ret);
2114a8fe54f0SJason M. Bills     }
2115a8fe54f0SJason M. Bills     if (!syslogID.empty())
2116a8fe54f0SJason M. Bills     {
2117a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2118a8fe54f0SJason M. Bills     }
2119a8fe54f0SJason M. Bills 
212039e77504SEd Tanous     std::string_view msg;
212116428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2122e1f26343SJason M. Bills     if (ret < 0)
2123e1f26343SJason M. Bills     {
2124e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2125e1f26343SJason M. Bills         return 1;
2126e1f26343SJason M. Bills     }
2127a8fe54f0SJason M. Bills     message += std::string(msg);
2128e1f26343SJason M. Bills 
2129e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2130271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
213116428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2132e1f26343SJason M. Bills     if (ret < 0)
2133e1f26343SJason M. Bills     {
2134e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2135e1f26343SJason M. Bills     }
2136e1f26343SJason M. Bills 
2137e1f26343SJason M. Bills     // Get the Created time from the timestamp
213816428a1aSJason M. Bills     std::string entryTimeStr;
213916428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2140e1f26343SJason M. Bills     {
214116428a1aSJason M. Bills         return 1;
2142e1f26343SJason M. Bills     }
2143e1f26343SJason M. Bills 
2144e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2145c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2146647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2147c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2148c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2149e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2150c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2151a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2152e1f26343SJason M. Bills         {"EntryType", "Oem"},
2153738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2154738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2155738c1e61SPatrick Williams                                      : "OK"},
2156086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2157e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2158e1f26343SJason M. Bills     return 0;
2159e1f26343SJason M. Bills }
2160e1f26343SJason M. Bills 
21617e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2162e1f26343SJason M. Bills {
21637e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2164ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
21650fda0f12SGeorge Liu         .methods(
21660fda0f12SGeorge Liu             boost::beast::http::verb::
21670fda0f12SGeorge Liu                 get)([](const crow::Request& req,
21687e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2169193ad2faSJason M. Bills             static constexpr const long maxEntriesPerPage = 1000;
2170271584abSEd Tanous             uint64_t skip = 0;
2171271584abSEd Tanous             uint64_t top = maxEntriesPerPage; // Show max entries by default
21728d1b46d7Szhanghch05             if (!getSkipParam(asyncResp, req, skip))
2173193ad2faSJason M. Bills             {
2174193ad2faSJason M. Bills                 return;
2175193ad2faSJason M. Bills             }
21768d1b46d7Szhanghch05             if (!getTopParam(asyncResp, req, top))
2177193ad2faSJason M. Bills             {
2178193ad2faSJason M. Bills                 return;
2179193ad2faSJason M. Bills             }
21807e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
21817e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2182e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
2183e1f26343SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
21840f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
21850f74e643SEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2186e1f26343SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2187e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2188e1f26343SJason M. Bills                 "Collection of BMC Journal Entries";
21890fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2190e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2191e1f26343SJason M. Bills 
21927e860f15SJohn Edward Broadbent             // Go through the journal and use the timestamp to create a
21937e860f15SJohn Edward Broadbent             // unique ID for each entry
2194e1f26343SJason M. Bills             sd_journal* journalTmp = nullptr;
2195e1f26343SJason M. Bills             int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2196e1f26343SJason M. Bills             if (ret < 0)
2197e1f26343SJason M. Bills             {
21987e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "failed to open journal: "
21997e860f15SJohn Edward Broadbent                                  << strerror(-ret);
2200f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2201e1f26343SJason M. Bills                 return;
2202e1f26343SJason M. Bills             }
22030fda0f12SGeorge Liu             std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22040fda0f12SGeorge Liu                 journalTmp, sd_journal_close);
2205e1f26343SJason M. Bills             journalTmp = nullptr;
2206b01bf299SEd Tanous             uint64_t entryCount = 0;
2207e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
2208e85d6b16SJason M. Bills             bool firstEntry = true;
2209e1f26343SJason M. Bills             SD_JOURNAL_FOREACH(journal.get())
2210e1f26343SJason M. Bills             {
2211193ad2faSJason M. Bills                 entryCount++;
22127e860f15SJohn Edward Broadbent                 // Handle paging using skip (number of entries to skip from
22137e860f15SJohn Edward Broadbent                 // the start) and top (number of entries to display)
2214193ad2faSJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
2215193ad2faSJason M. Bills                 {
2216193ad2faSJason M. Bills                     continue;
2217193ad2faSJason M. Bills                 }
2218193ad2faSJason M. Bills 
221916428a1aSJason M. Bills                 std::string idStr;
2220e85d6b16SJason M. Bills                 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2221e1f26343SJason M. Bills                 {
2222e1f26343SJason M. Bills                     continue;
2223e1f26343SJason M. Bills                 }
2224e1f26343SJason M. Bills 
2225e85d6b16SJason M. Bills                 if (firstEntry)
2226e85d6b16SJason M. Bills                 {
2227e85d6b16SJason M. Bills                     firstEntry = false;
2228e85d6b16SJason M. Bills                 }
2229e85d6b16SJason M. Bills 
2230e1f26343SJason M. Bills                 logEntryArray.push_back({});
2231c4bf6374SJason M. Bills                 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2232c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2233c4bf6374SJason M. Bills                                                bmcJournalLogEntry) != 0)
2234e1f26343SJason M. Bills                 {
2235f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2236e1f26343SJason M. Bills                     return;
2237e1f26343SJason M. Bills                 }
2238e1f26343SJason M. Bills             }
2239193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2240193ad2faSJason M. Bills             if (skip + top < entryCount)
2241193ad2faSJason M. Bills             {
2242193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
22430fda0f12SGeorge Liu                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2244193ad2faSJason M. Bills                     std::to_string(skip + top);
2245193ad2faSJason M. Bills             }
22467e860f15SJohn Edward Broadbent         });
2247e1f26343SJason M. Bills }
2248e1f26343SJason M. Bills 
22497e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2250e1f26343SJason M. Bills {
22517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
22527e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2253ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
22547e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2255ace85d60SEd Tanous             [](const crow::Request& req,
22567e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22577e860f15SJohn Edward Broadbent                const std::string& entryID) {
2258e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2259e1f26343SJason M. Bills                 uint64_t ts = 0;
2260271584abSEd Tanous                 uint64_t index = 0;
22618d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2262e1f26343SJason M. Bills                 {
226316428a1aSJason M. Bills                     return;
2264e1f26343SJason M. Bills                 }
2265e1f26343SJason M. Bills 
2266e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2267e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2268e1f26343SJason M. Bills                 if (ret < 0)
2269e1f26343SJason M. Bills                 {
22707e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
22717e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2272f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2273e1f26343SJason M. Bills                     return;
2274e1f26343SJason M. Bills                 }
22757e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
22767e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2277e1f26343SJason M. Bills                 journalTmp = nullptr;
22787e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
22797e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2280af07e3f5SJason M. Bills                 std::string idStr;
2281af07e3f5SJason M. Bills                 bool firstEntry = true;
2282e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
22832056b6d1SManojkiran Eda                 if (ret < 0)
22842056b6d1SManojkiran Eda                 {
22852056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
22862056b6d1SManojkiran Eda                                      << strerror(-ret);
22872056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
22882056b6d1SManojkiran Eda                     return;
22892056b6d1SManojkiran Eda                 }
2290271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2291e1f26343SJason M. Bills                 {
2292e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2293af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2294af07e3f5SJason M. Bills                     {
2295af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2296af07e3f5SJason M. Bills                         return;
2297af07e3f5SJason M. Bills                     }
2298af07e3f5SJason M. Bills                     if (firstEntry)
2299af07e3f5SJason M. Bills                     {
2300af07e3f5SJason M. Bills                         firstEntry = false;
2301af07e3f5SJason M. Bills                     }
2302e1f26343SJason M. Bills                 }
2303c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2304af07e3f5SJason M. Bills                 if (idStr != entryID)
2305c4bf6374SJason M. Bills                 {
2306ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2307c4bf6374SJason M. Bills                     return;
2308c4bf6374SJason M. Bills                 }
2309c4bf6374SJason M. Bills 
2310c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2311e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2312e1f26343SJason M. Bills                 {
2313f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2314e1f26343SJason M. Bills                     return;
2315e1f26343SJason M. Bills                 }
23167e860f15SJohn Edward Broadbent             });
2317c9bb6861Sraviteja-b }
2318c9bb6861Sraviteja-b 
23197e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2320c9bb6861Sraviteja-b {
23217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2322ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23230fda0f12SGeorge Liu         .methods(
23240fda0f12SGeorge Liu             boost::beast::http::verb::
23250fda0f12SGeorge Liu                 get)([](const crow::Request&,
23267e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2327c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.id"] =
23285cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Managers/bmc/LogServices/Dump";
2329c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.type"] =
2330d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
2331c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Name"] = "Dump LogService";
23325cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
23335cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
2334c9bb6861Sraviteja-b             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23357c8c4058STejas Patil 
23367c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
23377c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
23380fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23397c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23407c8c4058STejas Patil                 redfishDateTimeOffset.second;
23417c8c4058STejas Patil 
2342c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Entries"] = {
23437e860f15SJohn Edward Broadbent                 {"@odata.id",
23447e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
23455cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
23465cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
23470fda0f12SGeorge Liu                  {{"target",
23480fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2349d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
23500fda0f12SGeorge Liu                  {{"target",
23510fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
23527e860f15SJohn Edward Broadbent         });
2353c9bb6861Sraviteja-b }
2354c9bb6861Sraviteja-b 
23557e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
23567e860f15SJohn Edward Broadbent {
23577e860f15SJohn Edward Broadbent 
2358c9bb6861Sraviteja-b     /**
2359c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2360c9bb6861Sraviteja-b      */
23617e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2362ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
23637e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23647e860f15SJohn Edward Broadbent             [](const crow::Request&,
23657e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2366c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2367c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2368c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
23695cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
23705cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2371c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
23725cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2373c9bb6861Sraviteja-b 
23745cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
23757e860f15SJohn Edward Broadbent             });
2376c9bb6861Sraviteja-b }
2377c9bb6861Sraviteja-b 
23787e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2379c9bb6861Sraviteja-b {
23807e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23817e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2382ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
23837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23847e860f15SJohn Edward Broadbent             [](const crow::Request&,
23857e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23867e860f15SJohn Edward Broadbent                const std::string& param) {
23877e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "BMC");
23887e860f15SJohn Edward Broadbent             });
23897e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23907e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2391ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
23927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
23937e860f15SJohn Edward Broadbent             [](const crow::Request&,
23947e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23957e860f15SJohn Edward Broadbent                const std::string& param) {
23967e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
23977e860f15SJohn Edward Broadbent             });
2398c9bb6861Sraviteja-b }
2399c9bb6861Sraviteja-b 
24007e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2401c9bb6861Sraviteja-b {
24028d1b46d7Szhanghch05 
24030fda0f12SGeorge Liu     BMCWEB_ROUTE(
24040fda0f12SGeorge Liu         app,
24050fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2406ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24087e860f15SJohn Edward Broadbent             [](const crow::Request& req,
24097e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24108d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
24117e860f15SJohn Edward Broadbent             });
2412a43be80fSAsmitha Karunanithi }
2413a43be80fSAsmitha Karunanithi 
24147e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
241580319af1SAsmitha Karunanithi {
24160fda0f12SGeorge Liu     BMCWEB_ROUTE(
24170fda0f12SGeorge Liu         app,
24180fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2419ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24207e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24217e860f15SJohn Edward Broadbent             [](const crow::Request&,
24227e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24238d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
24247e860f15SJohn Edward Broadbent             });
24255cb1dd27SAsmitha Karunanithi }
24265cb1dd27SAsmitha Karunanithi 
24277e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
24285cb1dd27SAsmitha Karunanithi {
24297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2430ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
24317e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24327e860f15SJohn Edward Broadbent             [](const crow::Request&,
24337e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
24345cb1dd27SAsmitha Karunanithi 
24357e860f15SJohn Edward Broadbent             {
24365cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
24375cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump";
24385cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
2439d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
24405cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
24417e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
24427e860f15SJohn Edward Broadbent                     "System Dump LogService";
24435cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
24445cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24457c8c4058STejas Patil 
24467c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
24477c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
24487c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
24497c8c4058STejas Patil                     redfishDateTimeOffset.first;
24507c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24517c8c4058STejas Patil                     redfishDateTimeOffset.second;
24527c8c4058STejas Patil 
24535cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Entries"] = {
24545cb1dd27SAsmitha Karunanithi                     {"@odata.id",
24555cb1dd27SAsmitha Karunanithi                      "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
24565cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
24575cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
24587e860f15SJohn Edward Broadbent                      {{"target",
24590fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
2460d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
24617e860f15SJohn Edward Broadbent                      {{"target",
24620fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
24637e860f15SJohn Edward Broadbent             });
24645cb1dd27SAsmitha Karunanithi }
24655cb1dd27SAsmitha Karunanithi 
24667e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
24677e860f15SJohn Edward Broadbent {
24687e860f15SJohn Edward Broadbent 
24695cb1dd27SAsmitha Karunanithi     /**
24705cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
24715cb1dd27SAsmitha Karunanithi      */
2472b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2473ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
24747e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24757e860f15SJohn Edward Broadbent             [](const crow::Request&,
2476864d6a17SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24775cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
24785cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
24795cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
24805cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
24815cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
24825cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
24835cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
24845cb1dd27SAsmitha Karunanithi 
24855cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
24867e860f15SJohn Edward Broadbent             });
24875cb1dd27SAsmitha Karunanithi }
24885cb1dd27SAsmitha Karunanithi 
24897e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
24905cb1dd27SAsmitha Karunanithi {
24917e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2492864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2493ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2494ed398213SEd Tanous 
24957e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24967e860f15SJohn Edward Broadbent             [](const crow::Request&,
24977e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24987e860f15SJohn Edward Broadbent                const std::string& param) {
24997e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "System");
25007e860f15SJohn Edward Broadbent             });
25018d1b46d7Szhanghch05 
25027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2503864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2504ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25057e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
25067e860f15SJohn Edward Broadbent             [](const crow::Request&,
25077e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25087e860f15SJohn Edward Broadbent                const std::string& param) {
25097e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
25107e860f15SJohn Edward Broadbent             });
25115cb1dd27SAsmitha Karunanithi }
2512c9bb6861Sraviteja-b 
25137e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2514c9bb6861Sraviteja-b {
25150fda0f12SGeorge Liu     BMCWEB_ROUTE(
25160fda0f12SGeorge Liu         app,
25170fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2518ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25197e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25207e860f15SJohn Edward Broadbent             [](const crow::Request& req,
25217e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25227e860f15SJohn Edward Broadbent 
25237e860f15SJohn Edward Broadbent             { createDump(asyncResp, req, "System"); });
2524a43be80fSAsmitha Karunanithi }
2525a43be80fSAsmitha Karunanithi 
25267e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2527a43be80fSAsmitha Karunanithi {
25280fda0f12SGeorge Liu     BMCWEB_ROUTE(
25290fda0f12SGeorge Liu         app,
25300fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2531ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25327e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25337e860f15SJohn Edward Broadbent             [](const crow::Request&,
25347e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25357e860f15SJohn Edward Broadbent 
25367e860f15SJohn Edward Broadbent             { clearDump(asyncResp, "System"); });
2537013487e5Sraviteja-b }
2538013487e5Sraviteja-b 
25397e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
25401da66f75SEd Tanous {
25413946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25423946028dSAppaRao Puli     // method for security reasons.
25431da66f75SEd Tanous     /**
25441da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
25451da66f75SEd Tanous      */
25467e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2547ed398213SEd Tanous         // This is incorrect, should be:
2548ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2549432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
25507e860f15SJohn Edward Broadbent         .methods(
25517e860f15SJohn Edward Broadbent             boost::beast::http::verb::
25527e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
25537e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25547e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
25557e860f15SJohn Edward Broadbent             // SubRoute
25560f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2557424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2558e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
25598e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
25604f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
25614f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
25624f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2563e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2564e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
25657c8c4058STejas Patil 
25667c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
25677c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
25687c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25697c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25707c8c4058STejas Patil                 redfishDateTimeOffset.second;
25717c8c4058STejas Patil 
2572cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2573cd50aa42SJason M. Bills                 {"@odata.id",
2574424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2575e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
25765b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
25770fda0f12SGeorge Liu                  {{"target",
25780fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
25798e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
25800fda0f12SGeorge Liu                  {{"target",
25810fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
25827e860f15SJohn Edward Broadbent         });
25831da66f75SEd Tanous }
25841da66f75SEd Tanous 
25857e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
25865b61b5e8SJason M. Bills {
25870fda0f12SGeorge Liu     BMCWEB_ROUTE(
25880fda0f12SGeorge Liu         app,
25890fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2590ed398213SEd Tanous         // This is incorrect, should be:
2591ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2592432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
25937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25947e860f15SJohn Edward Broadbent             [](const crow::Request&,
25957e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25965b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
25975b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2598cb13a392SEd Tanous                                 const std::string&) {
25995b61b5e8SJason M. Bills                         if (ec)
26005b61b5e8SJason M. Bills                         {
26015b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
26025b61b5e8SJason M. Bills                             return;
26035b61b5e8SJason M. Bills                         }
26045b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
26055b61b5e8SJason M. Bills                     },
26067e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
26077e860f15SJohn Edward Broadbent                     "DeleteAll");
26087e860f15SJohn Edward Broadbent             });
26095b61b5e8SJason M. Bills }
26105b61b5e8SJason M. Bills 
26118d1b46d7Szhanghch05 static void
26128d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26138d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2614e855dd28SJason M. Bills {
2615043a0536SJohnathan Mantey     auto getStoredLogCallback =
2616*b9d36b47SEd Tanous         [asyncResp, logID,
2617*b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2618*b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2619e855dd28SJason M. Bills             if (ec)
2620e855dd28SJason M. Bills             {
2621e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
26221ddcf01aSJason M. Bills                 if (ec.value() ==
26231ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
26241ddcf01aSJason M. Bills                 {
2625043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2626043a0536SJohnathan Mantey                                                logID);
26271ddcf01aSJason M. Bills                 }
26281ddcf01aSJason M. Bills                 else
26291ddcf01aSJason M. Bills                 {
2630e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
26311ddcf01aSJason M. Bills                 }
2632e855dd28SJason M. Bills                 return;
2633e855dd28SJason M. Bills             }
2634043a0536SJohnathan Mantey 
2635043a0536SJohnathan Mantey             std::string timestamp{};
2636043a0536SJohnathan Mantey             std::string filename{};
2637043a0536SJohnathan Mantey             std::string logfile{};
26382c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2639043a0536SJohnathan Mantey 
2640043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2641e855dd28SJason M. Bills             {
2642ace85d60SEd Tanous                 messages::resourceMissingAtURI(
2643ace85d60SEd Tanous                     asyncResp->res, crow::utility::urlFromPieces(logID));
2644e855dd28SJason M. Bills                 return;
2645e855dd28SJason M. Bills             }
2646e855dd28SJason M. Bills 
2647043a0536SJohnathan Mantey             std::string crashdumpURI =
2648e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2649043a0536SJohnathan Mantey                 logID + "/" + filename;
26502b20ef6eSJason M. Bills             nlohmann::json logEntry = {
26514978b63fSJason M. Bills                 {"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
26524978b63fSJason M. Bills                 {"@odata.id",
26534978b63fSJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2654e855dd28SJason M. Bills                      logID},
2655e855dd28SJason M. Bills                 {"Name", "CPU Crashdump"},
2656e855dd28SJason M. Bills                 {"Id", logID},
2657e855dd28SJason M. Bills                 {"EntryType", "Oem"},
26588e6c099aSJason M. Bills                 {"AdditionalDataURI", std::move(crashdumpURI)},
26598e6c099aSJason M. Bills                 {"DiagnosticDataType", "OEM"},
26608e6c099aSJason M. Bills                 {"OEMDiagnosticDataType", "PECICrashdump"},
2661043a0536SJohnathan Mantey                 {"Created", std::move(timestamp)}};
26622b20ef6eSJason M. Bills 
26632b20ef6eSJason M. Bills             // If logEntryJson references an array of LogEntry resources
26642b20ef6eSJason M. Bills             // ('Members' list), then push this as a new entry, otherwise set it
26652b20ef6eSJason M. Bills             // directly
26662b20ef6eSJason M. Bills             if (logEntryJson.is_array())
26672b20ef6eSJason M. Bills             {
26682b20ef6eSJason M. Bills                 logEntryJson.push_back(logEntry);
26692b20ef6eSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
26702b20ef6eSJason M. Bills                     logEntryJson.size();
26712b20ef6eSJason M. Bills             }
26722b20ef6eSJason M. Bills             else
26732b20ef6eSJason M. Bills             {
26742b20ef6eSJason M. Bills                 logEntryJson = logEntry;
26752b20ef6eSJason M. Bills             }
2676e855dd28SJason M. Bills         };
2677e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
26785b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
26795b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2680043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2681e855dd28SJason M. Bills }
2682e855dd28SJason M. Bills 
26837e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
26841da66f75SEd Tanous {
26853946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26863946028dSAppaRao Puli     // method for security reasons.
26871da66f75SEd Tanous     /**
26881da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26891da66f75SEd Tanous      */
26907e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
26917e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2692ed398213SEd Tanous         // This is incorrect, should be.
2693ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2694432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26957e860f15SJohn Edward Broadbent         .methods(
26967e860f15SJohn Edward Broadbent             boost::beast::http::verb::
26977e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
26987e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26992b20ef6eSJason M. Bills             crow::connections::systemBus->async_method_call(
27002b20ef6eSJason M. Bills                 [asyncResp](const boost::system::error_code ec,
27012b20ef6eSJason M. Bills                             const std::vector<std::string>& resp) {
27021da66f75SEd Tanous                     if (ec)
27031da66f75SEd Tanous                     {
27041da66f75SEd Tanous                         if (ec.value() !=
27051da66f75SEd Tanous                             boost::system::errc::no_such_file_or_directory)
27061da66f75SEd Tanous                         {
27071da66f75SEd Tanous                             BMCWEB_LOG_DEBUG << "failed to get entries ec: "
27081da66f75SEd Tanous                                              << ec.message();
2709f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
27101da66f75SEd Tanous                             return;
27111da66f75SEd Tanous                         }
27121da66f75SEd Tanous                     }
2713e1f26343SJason M. Bills                     asyncResp->res.jsonValue["@odata.type"] =
27141da66f75SEd Tanous                         "#LogEntryCollection.LogEntryCollection";
27150f74e643SEd Tanous                     asyncResp->res.jsonValue["@odata.id"] =
2716424c4176SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
27172b20ef6eSJason M. Bills                     asyncResp->res.jsonValue["Name"] =
27182b20ef6eSJason M. Bills                         "Open BMC Crashdump Entries";
2719e1f26343SJason M. Bills                     asyncResp->res.jsonValue["Description"] =
2720424c4176SJason M. Bills                         "Collection of Crashdump Entries";
27212b20ef6eSJason M. Bills                     asyncResp->res.jsonValue["Members"] =
27222b20ef6eSJason M. Bills                         nlohmann::json::array();
2723a2dd60a6SBrandon Kim                     asyncResp->res.jsonValue["Members@odata.count"] = 0;
27242b20ef6eSJason M. Bills 
27252b20ef6eSJason M. Bills                     for (const std::string& path : resp)
27261da66f75SEd Tanous                     {
27272b20ef6eSJason M. Bills                         const sdbusplus::message::object_path objPath(path);
2728e855dd28SJason M. Bills                         // Get the log ID
27292b20ef6eSJason M. Bills                         std::string logID = objPath.filename();
27302b20ef6eSJason M. Bills                         if (logID.empty())
27311da66f75SEd Tanous                         {
2732e855dd28SJason M. Bills                             continue;
27331da66f75SEd Tanous                         }
2734e855dd28SJason M. Bills                         // Add the log entry to the array
27352b20ef6eSJason M. Bills                         logCrashdumpEntry(asyncResp, logID,
27362b20ef6eSJason M. Bills                                           asyncResp->res.jsonValue["Members"]);
27371da66f75SEd Tanous                     }
27382b20ef6eSJason M. Bills                 },
27391da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
27401da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
27411da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
27425b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
27437e860f15SJohn Edward Broadbent         });
27441da66f75SEd Tanous }
27451da66f75SEd Tanous 
27467e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
27471da66f75SEd Tanous {
27483946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27493946028dSAppaRao Puli     // method for security reasons.
27501da66f75SEd Tanous 
27517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
27527e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2753ed398213SEd Tanous         // this is incorrect, should be
2754ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2755432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27567e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
27577e860f15SJohn Edward Broadbent             [](const crow::Request&,
27587e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27597e860f15SJohn Edward Broadbent                const std::string& param) {
27607e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2761e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
27627e860f15SJohn Edward Broadbent             });
2763e855dd28SJason M. Bills }
2764e855dd28SJason M. Bills 
27657e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2766e855dd28SJason M. Bills {
27673946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27683946028dSAppaRao Puli     // method for security reasons.
27697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
27707e860f15SJohn Edward Broadbent         app,
27717e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2772ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
27737e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2774ace85d60SEd Tanous             [](const crow::Request& req,
27757e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27767e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
2777043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2778ace85d60SEd Tanous                     [asyncResp, logID, fileName,
2779ace85d60SEd Tanous                      url(boost::urls::url(req.urlView))](
2780abf2add6SEd Tanous                         const boost::system::error_code ec,
2781168e20c1SEd Tanous                         const std::vector<std::pair<
2782168e20c1SEd Tanous                             std::string, dbus::utility::DbusVariantType>>&
27837e860f15SJohn Edward Broadbent                             resp) {
27841da66f75SEd Tanous                         if (ec)
27851da66f75SEd Tanous                         {
2786043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2787043a0536SJohnathan Mantey                                              << ec.message();
2788f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
27891da66f75SEd Tanous                             return;
27901da66f75SEd Tanous                         }
2791e855dd28SJason M. Bills 
2792043a0536SJohnathan Mantey                         std::string dbusFilename{};
2793043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2794043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2795043a0536SJohnathan Mantey 
27967e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
27977e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2798043a0536SJohnathan Mantey 
2799043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2800043a0536SJohnathan Mantey                             dbusFilepath.empty())
28011da66f75SEd Tanous                         {
2802ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
28031da66f75SEd Tanous                             return;
28041da66f75SEd Tanous                         }
2805e855dd28SJason M. Bills 
2806043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2807043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2808043a0536SJohnathan Mantey                         {
2809ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
2810043a0536SJohnathan Mantey                             return;
2811043a0536SJohnathan Mantey                         }
2812043a0536SJohnathan Mantey 
2813043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2814043a0536SJohnathan Mantey                         {
2815ace85d60SEd Tanous                             messages::resourceMissingAtURI(asyncResp->res, url);
2816043a0536SJohnathan Mantey                             return;
2817043a0536SJohnathan Mantey                         }
28182d31491bSJason M. Bills                         std::ifstream ifs(dbusFilepath,
28192d31491bSJason M. Bills                                           std::ios::in | std::ios::binary);
28202d31491bSJason M. Bills                         asyncResp->res.body() = std::string(
28212d31491bSJason M. Bills                             std::istreambuf_iterator<char>{ifs}, {});
2822043a0536SJohnathan Mantey 
28237e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
28247e860f15SJohn Edward Broadbent                         // from a browser
28257e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
28267e860f15SJohn Edward Broadbent                                                  "attachment");
28271da66f75SEd Tanous                     };
28281da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
28295b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
28305b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
28317e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
28327e860f15SJohn Edward Broadbent                     crashdumpInterface);
28337e860f15SJohn Edward Broadbent             });
28341da66f75SEd Tanous }
28351da66f75SEd Tanous 
2836c5a4c82aSJason M. Bills enum class OEMDiagnosticType
2837c5a4c82aSJason M. Bills {
2838c5a4c82aSJason M. Bills     onDemand,
2839c5a4c82aSJason M. Bills     telemetry,
2840c5a4c82aSJason M. Bills     invalid,
2841c5a4c82aSJason M. Bills };
2842c5a4c82aSJason M. Bills 
2843f7725d79SEd Tanous inline OEMDiagnosticType
2844f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
2845c5a4c82aSJason M. Bills {
2846c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
2847c5a4c82aSJason M. Bills     {
2848c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
2849c5a4c82aSJason M. Bills     }
2850c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
2851c5a4c82aSJason M. Bills     {
2852c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
2853c5a4c82aSJason M. Bills     }
2854c5a4c82aSJason M. Bills 
2855c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
2856c5a4c82aSJason M. Bills }
2857c5a4c82aSJason M. Bills 
28587e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
28591da66f75SEd Tanous {
28603946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28613946028dSAppaRao Puli     // method for security reasons.
28620fda0f12SGeorge Liu     BMCWEB_ROUTE(
28630fda0f12SGeorge Liu         app,
28640fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2865ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2866ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2867432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28687e860f15SJohn Edward Broadbent         .methods(
28697e860f15SJohn Edward Broadbent             boost::beast::http::verb::
28707e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
28717e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
28728e6c099aSJason M. Bills             std::string diagnosticDataType;
28738e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
287415ed6780SWilly Tu             if (!redfish::json_util::readJsonAction(
28757e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
28767e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
28777e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
28788e6c099aSJason M. Bills             {
28798e6c099aSJason M. Bills                 return;
28808e6c099aSJason M. Bills             }
28818e6c099aSJason M. Bills 
28828e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
28838e6c099aSJason M. Bills             {
28848e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
28858e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
28868e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
28878e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
28888e6c099aSJason M. Bills                     "CollectDiagnosticData");
28898e6c099aSJason M. Bills                 return;
28908e6c099aSJason M. Bills             }
28918e6c099aSJason M. Bills 
2892c5a4c82aSJason M. Bills             OEMDiagnosticType oemDiagType =
2893c5a4c82aSJason M. Bills                 getOEMDiagnosticType(oemDiagnosticDataType);
2894c5a4c82aSJason M. Bills 
2895c5a4c82aSJason M. Bills             std::string iface;
2896c5a4c82aSJason M. Bills             std::string method;
2897c5a4c82aSJason M. Bills             std::string taskMatchStr;
2898c5a4c82aSJason M. Bills             if (oemDiagType == OEMDiagnosticType::onDemand)
2899c5a4c82aSJason M. Bills             {
2900c5a4c82aSJason M. Bills                 iface = crashdumpOnDemandInterface;
2901c5a4c82aSJason M. Bills                 method = "GenerateOnDemandLog";
2902c5a4c82aSJason M. Bills                 taskMatchStr = "type='signal',"
2903c5a4c82aSJason M. Bills                                "interface='org.freedesktop.DBus.Properties',"
2904c5a4c82aSJason M. Bills                                "member='PropertiesChanged',"
2905c5a4c82aSJason M. Bills                                "arg0namespace='com.intel.crashdump'";
2906c5a4c82aSJason M. Bills             }
2907c5a4c82aSJason M. Bills             else if (oemDiagType == OEMDiagnosticType::telemetry)
2908c5a4c82aSJason M. Bills             {
2909c5a4c82aSJason M. Bills                 iface = crashdumpTelemetryInterface;
2910c5a4c82aSJason M. Bills                 method = "GenerateTelemetryLog";
2911c5a4c82aSJason M. Bills                 taskMatchStr = "type='signal',"
2912c5a4c82aSJason M. Bills                                "interface='org.freedesktop.DBus.Properties',"
2913c5a4c82aSJason M. Bills                                "member='PropertiesChanged',"
2914c5a4c82aSJason M. Bills                                "arg0namespace='com.intel.crashdump'";
2915c5a4c82aSJason M. Bills             }
2916c5a4c82aSJason M. Bills             else
2917c5a4c82aSJason M. Bills             {
2918c5a4c82aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
2919c5a4c82aSJason M. Bills                                  << oemDiagnosticDataType;
2920c5a4c82aSJason M. Bills                 messages::actionParameterValueFormatError(
2921c5a4c82aSJason M. Bills                     asyncResp->res, oemDiagnosticDataType,
2922c5a4c82aSJason M. Bills                     "OEMDiagnosticDataType", "CollectDiagnosticData");
2923c5a4c82aSJason M. Bills                 return;
2924c5a4c82aSJason M. Bills             }
2925c5a4c82aSJason M. Bills 
2926c5a4c82aSJason M. Bills             auto collectCrashdumpCallback =
2927c5a4c82aSJason M. Bills                 [asyncResp, payload(task::Payload(req)),
2928c5a4c82aSJason M. Bills                  taskMatchStr](const boost::system::error_code ec,
292998be3e39SEd Tanous                                const std::string&) mutable {
29301da66f75SEd Tanous                     if (ec)
29311da66f75SEd Tanous                     {
29327e860f15SJohn Edward Broadbent                         if (ec.value() ==
29337e860f15SJohn Edward Broadbent                             boost::system::errc::operation_not_supported)
29341da66f75SEd Tanous                         {
2935f12894f8SJason M. Bills                             messages::resourceInStandby(asyncResp->res);
29361da66f75SEd Tanous                         }
29374363d3b2SJason M. Bills                         else if (ec.value() ==
29384363d3b2SJason M. Bills                                  boost::system::errc::device_or_resource_busy)
29394363d3b2SJason M. Bills                         {
2940c5a4c82aSJason M. Bills                             messages::serviceTemporarilyUnavailable(
2941c5a4c82aSJason M. Bills                                 asyncResp->res, "60");
29424363d3b2SJason M. Bills                         }
29431da66f75SEd Tanous                         else
29441da66f75SEd Tanous                         {
2945f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
29461da66f75SEd Tanous                         }
29471da66f75SEd Tanous                         return;
29481da66f75SEd Tanous                     }
2949c5a4c82aSJason M. Bills                     std::shared_ptr<task::TaskData> task =
2950c5a4c82aSJason M. Bills                         task::TaskData::createTask(
29517e860f15SJohn Edward Broadbent                             [](boost::system::error_code err,
29527e860f15SJohn Edward Broadbent                                sdbusplus::message::message&,
2953c5a4c82aSJason M. Bills                                const std::shared_ptr<task::TaskData>&
2954c5a4c82aSJason M. Bills                                    taskData) {
295566afe4faSJames Feist                                 if (!err)
295666afe4faSJames Feist                                 {
2957e5d5006bSJames Feist                                     taskData->messages.emplace_back(
2958e5d5006bSJames Feist                                         messages::taskCompletedOK(
2959e5d5006bSJames Feist                                             std::to_string(taskData->index)));
2960831d6b09SJames Feist                                     taskData->state = "Completed";
296166afe4faSJames Feist                                 }
296232898ceaSJames Feist                                 return task::completed;
296366afe4faSJames Feist                             },
2964c5a4c82aSJason M. Bills                             taskMatchStr);
2965c5a4c82aSJason M. Bills 
296646229577SJames Feist                     task->startTimer(std::chrono::minutes(5));
296746229577SJames Feist                     task->populateResp(asyncResp->res);
296898be3e39SEd Tanous                     task->payload.emplace(std::move(payload));
29691da66f75SEd Tanous                 };
29708e6c099aSJason M. Bills 
29711da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
29728e6c099aSJason M. Bills                 std::move(collectCrashdumpCallback), crashdumpObject,
2973c5a4c82aSJason M. Bills                 crashdumpPath, iface, method);
29747e860f15SJohn Edward Broadbent         });
29756eda7685SKenny L. Ku }
29766eda7685SKenny L. Ku 
2977cb92c03bSAndrew Geissler /**
2978cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2979cb92c03bSAndrew Geissler  */
29807e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
2981cb92c03bSAndrew Geissler {
2982cb92c03bSAndrew Geissler     /**
2983cb92c03bSAndrew Geissler      * Function handles POST method request.
2984cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
2985cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
2986cb92c03bSAndrew Geissler      */
29877e860f15SJohn Edward Broadbent 
29880fda0f12SGeorge Liu     BMCWEB_ROUTE(
29890fda0f12SGeorge Liu         app,
29900fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
2991ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
29927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
29937e860f15SJohn Edward Broadbent             [](const crow::Request&,
29947e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2995cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
2996cb92c03bSAndrew Geissler 
2997cb92c03bSAndrew Geissler                 // Process response from Logging service.
29987e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
29997e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
30007e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
30017e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
3002cb92c03bSAndrew Geissler                     if (ec)
3003cb92c03bSAndrew Geissler                     {
3004cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
30057e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
30067e860f15SJohn Edward Broadbent                                          << ec;
3007cb92c03bSAndrew Geissler                         asyncResp->res.result(
3008cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
3009cb92c03bSAndrew Geissler                         return;
3010cb92c03bSAndrew Geissler                     }
3011cb92c03bSAndrew Geissler 
30127e860f15SJohn Edward Broadbent                     asyncResp->res.result(
30137e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
3014cb92c03bSAndrew Geissler                 };
3015cb92c03bSAndrew Geissler 
3016cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
3017cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
30182c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
3019cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
3020cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
30217e860f15SJohn Edward Broadbent             });
3022cb92c03bSAndrew Geissler }
3023a3316fc6SZhikuiRen 
3024a3316fc6SZhikuiRen /****************************************************
3025a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3026a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3027a3316fc6SZhikuiRen  ******************************************************/
30287e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3029a3316fc6SZhikuiRen {
30307e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3031ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
30320fda0f12SGeorge Liu         .methods(
30330fda0f12SGeorge Liu             boost::beast::http::verb::
30340fda0f12SGeorge Liu                 get)([](const crow::Request&,
30357e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3036a3316fc6SZhikuiRen             asyncResp->res.jsonValue = {
30377e860f15SJohn Edward Broadbent                 {"@odata.id",
30387e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes"},
3039a3316fc6SZhikuiRen                 {"@odata.type", "#LogService.v1_1_0.LogService"},
3040a3316fc6SZhikuiRen                 {"Name", "POST Code Log Service"},
3041a3316fc6SZhikuiRen                 {"Description", "POST Code Log Service"},
3042a3316fc6SZhikuiRen                 {"Id", "BIOS POST Code Log"},
3043a3316fc6SZhikuiRen                 {"OverWritePolicy", "WrapsWhenFull"},
3044a3316fc6SZhikuiRen                 {"Entries",
30450fda0f12SGeorge Liu                  {{"@odata.id",
30460fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
30477c8c4058STejas Patil 
30487c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
30497c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
30500fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30517c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30527c8c4058STejas Patil                 redfishDateTimeOffset.second;
30537c8c4058STejas Patil 
3054a3316fc6SZhikuiRen             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
30557e860f15SJohn Edward Broadbent                 {"target",
30560fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
30577e860f15SJohn Edward Broadbent         });
3058a3316fc6SZhikuiRen }
3059a3316fc6SZhikuiRen 
30607e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3061a3316fc6SZhikuiRen {
30620fda0f12SGeorge Liu     BMCWEB_ROUTE(
30630fda0f12SGeorge Liu         app,
30640fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3065ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3066ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3067432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
30697e860f15SJohn Edward Broadbent             [](const crow::Request&,
30707e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3071a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3072a3316fc6SZhikuiRen 
3073a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
3074a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
3075a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
3076a3316fc6SZhikuiRen                         if (ec)
3077a3316fc6SZhikuiRen                         {
3078a3316fc6SZhikuiRen                             // TODO Handle for specific error code
3079a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
30807e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
30817e860f15SJohn Edward Broadbent                                 << ec;
30827e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
30837e860f15SJohn Edward Broadbent                                                       internal_server_error);
3084a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
3085a3316fc6SZhikuiRen                             return;
3086a3316fc6SZhikuiRen                         }
3087a3316fc6SZhikuiRen                     },
308815124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
308915124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
3090a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
30917e860f15SJohn Edward Broadbent             });
3092a3316fc6SZhikuiRen }
3093a3316fc6SZhikuiRen 
3094a3316fc6SZhikuiRen static void fillPostCodeEntry(
30958d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
30966c9a279eSManojkiran Eda     const boost::container::flat_map<
30976c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3098a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3099a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3100a3316fc6SZhikuiRen {
3101a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3102fffb8c1fSEd Tanous     const registries::Message* message =
3103fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3104a3316fc6SZhikuiRen 
3105a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3106a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3107a3316fc6SZhikuiRen 
3108a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
31096c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
31106c9a279eSManojkiran Eda              code : postcode)
3111a3316fc6SZhikuiRen     {
3112a3316fc6SZhikuiRen         currentCodeIndex++;
3113a3316fc6SZhikuiRen         std::string postcodeEntryID =
3114a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3115a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3116a3316fc6SZhikuiRen 
3117a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3118a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3119a3316fc6SZhikuiRen 
3120a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3121a3316fc6SZhikuiRen         { // already incremented
3122a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3123a3316fc6SZhikuiRen         }
3124a3316fc6SZhikuiRen         else
3125a3316fc6SZhikuiRen         {
3126a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3127a3316fc6SZhikuiRen         }
3128a3316fc6SZhikuiRen 
3129a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3130a3316fc6SZhikuiRen         // not fall between top and skip
3131a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3132a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3133a3316fc6SZhikuiRen         {
3134a3316fc6SZhikuiRen             continue;
3135a3316fc6SZhikuiRen         }
3136a3316fc6SZhikuiRen 
31374e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3138a3316fc6SZhikuiRen         // currentIndex
3139a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3140a3316fc6SZhikuiRen         {
3141a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3142a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3143a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3144a3316fc6SZhikuiRen             continue;
3145a3316fc6SZhikuiRen         }
3146a3316fc6SZhikuiRen 
3147a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3148a3316fc6SZhikuiRen         // index
3149a3316fc6SZhikuiRen 
3150a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3151a3316fc6SZhikuiRen         std::string entryTimeStr;
31521d8782e7SNan Zhou         entryTimeStr =
31531d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3154a3316fc6SZhikuiRen 
3155a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3156a3316fc6SZhikuiRen         std::ostringstream hexCode;
3157a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
31586c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3159a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3160a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3161a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3162a3316fc6SZhikuiRen         // Set precision to 4 digits
3163a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3164a3316fc6SZhikuiRen         // Add double to stream
3165a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3166a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3167a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3168a3316fc6SZhikuiRen 
3169a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3170a3316fc6SZhikuiRen         std::string msg;
3171a3316fc6SZhikuiRen         if (message != nullptr)
3172a3316fc6SZhikuiRen         {
3173a3316fc6SZhikuiRen             msg = message->message;
3174a3316fc6SZhikuiRen 
3175a3316fc6SZhikuiRen             // fill in this post code value
3176a3316fc6SZhikuiRen             int i = 0;
3177a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3178a3316fc6SZhikuiRen             {
3179a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3180a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3181a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3182a3316fc6SZhikuiRen                 {
3183a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3184a3316fc6SZhikuiRen                 }
3185a3316fc6SZhikuiRen             }
3186a3316fc6SZhikuiRen         }
3187a3316fc6SZhikuiRen 
3188d4342a92STim Lee         // Get Severity template from message registry
3189d4342a92STim Lee         std::string severity;
3190d4342a92STim Lee         if (message != nullptr)
3191d4342a92STim Lee         {
31925f2b84eeSEd Tanous             severity = message->messageSeverity;
3193d4342a92STim Lee         }
3194d4342a92STim Lee 
3195a3316fc6SZhikuiRen         // add to AsyncResp
3196a3316fc6SZhikuiRen         logEntryArray.push_back({});
3197a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
31980fda0f12SGeorge Liu         bmcLogEntry = {
31990fda0f12SGeorge Liu             {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
32000fda0f12SGeorge Liu             {"@odata.id",
32010fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3202a3316fc6SZhikuiRen                  postcodeEntryID},
3203a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
3204a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
3205a3316fc6SZhikuiRen             {"Message", std::move(msg)},
32064a0bf539SManojkiran Eda             {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3207a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
3208a3316fc6SZhikuiRen             {"EntryType", "Event"},
3209a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
32109c620e21SAsmitha Karunanithi             {"Created", entryTimeStr}};
3211647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3212647b3cdcSGeorge Liu         {
3213647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3214647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3215647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3216647b3cdcSGeorge Liu         }
3217a3316fc6SZhikuiRen     }
3218a3316fc6SZhikuiRen }
3219a3316fc6SZhikuiRen 
32208d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3221a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3222a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3223a3316fc6SZhikuiRen {
3224a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
32256c9a279eSManojkiran Eda         [aResp, bootIndex,
32266c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
32276c9a279eSManojkiran Eda                     const boost::container::flat_map<
32286c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32296c9a279eSManojkiran Eda                         postcode) {
3230a3316fc6SZhikuiRen             if (ec)
3231a3316fc6SZhikuiRen             {
3232a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3233a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3234a3316fc6SZhikuiRen                 return;
3235a3316fc6SZhikuiRen             }
3236a3316fc6SZhikuiRen 
3237a3316fc6SZhikuiRen             // skip the empty postcode boots
3238a3316fc6SZhikuiRen             if (postcode.empty())
3239a3316fc6SZhikuiRen             {
3240a3316fc6SZhikuiRen                 return;
3241a3316fc6SZhikuiRen             }
3242a3316fc6SZhikuiRen 
3243a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3244a3316fc6SZhikuiRen 
3245a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3246a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3247a3316fc6SZhikuiRen         },
324815124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
324915124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3250a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3251a3316fc6SZhikuiRen         bootIndex);
3252a3316fc6SZhikuiRen }
3253a3316fc6SZhikuiRen 
32548d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3255a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3256a3316fc6SZhikuiRen                                const uint16_t bootCount,
3257a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3258a3316fc6SZhikuiRen                                const uint64_t top)
3259a3316fc6SZhikuiRen {
3260a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3261a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3262a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
32636c9a279eSManojkiran Eda               const boost::container::flat_map<
32646c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32656c9a279eSManojkiran Eda                   postcode) {
3266a3316fc6SZhikuiRen             if (ec)
3267a3316fc6SZhikuiRen             {
3268a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3269a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3270a3316fc6SZhikuiRen                 return;
3271a3316fc6SZhikuiRen             }
3272a3316fc6SZhikuiRen 
3273a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3274a3316fc6SZhikuiRen             if (!postcode.empty())
3275a3316fc6SZhikuiRen             {
3276a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3277a3316fc6SZhikuiRen 
3278a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3279a3316fc6SZhikuiRen                 {
3280a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3281a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3282a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3283a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3284a3316fc6SZhikuiRen 
3285a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3286a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3287a3316fc6SZhikuiRen                 }
3288a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3289a3316fc6SZhikuiRen             }
3290a3316fc6SZhikuiRen 
3291a3316fc6SZhikuiRen             // continue to previous bootIndex
3292a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3293a3316fc6SZhikuiRen             {
3294a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3295a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3296a3316fc6SZhikuiRen             }
3297a3316fc6SZhikuiRen             else
3298a3316fc6SZhikuiRen             {
3299a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
33000fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3301a3316fc6SZhikuiRen                     std::to_string(skip + top);
3302a3316fc6SZhikuiRen             }
3303a3316fc6SZhikuiRen         },
330415124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
330515124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3306a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3307a3316fc6SZhikuiRen         bootIndex);
3308a3316fc6SZhikuiRen }
3309a3316fc6SZhikuiRen 
33108d1b46d7Szhanghch05 static void
33118d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3312a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3313a3316fc6SZhikuiRen {
3314a3316fc6SZhikuiRen     uint64_t entryCount = 0;
33151e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
33161e1e598dSJonathan Doman         *crow::connections::systemBus,
33171e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
33181e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
33191e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
33201e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
33211e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3322a3316fc6SZhikuiRen             if (ec)
3323a3316fc6SZhikuiRen             {
3324a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3325a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3326a3316fc6SZhikuiRen                 return;
3327a3316fc6SZhikuiRen             }
33281e1e598dSJonathan Doman             getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
33291e1e598dSJonathan Doman         });
3330a3316fc6SZhikuiRen }
3331a3316fc6SZhikuiRen 
33327e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3333a3316fc6SZhikuiRen {
33347e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
33357e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3336ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
33377e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
33387e860f15SJohn Edward Broadbent             [](const crow::Request& req,
33397e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3340a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3341a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3342a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3343a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3344a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3345a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3346a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3347a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3348a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3349a3316fc6SZhikuiRen 
3350a3316fc6SZhikuiRen                 uint64_t skip = 0;
3351a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
33528d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3353a3316fc6SZhikuiRen                 {
3354a3316fc6SZhikuiRen                     return;
3355a3316fc6SZhikuiRen                 }
33568d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3357a3316fc6SZhikuiRen                 {
3358a3316fc6SZhikuiRen                     return;
3359a3316fc6SZhikuiRen                 }
3360a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
33617e860f15SJohn Edward Broadbent             });
3362a3316fc6SZhikuiRen }
3363a3316fc6SZhikuiRen 
3364647b3cdcSGeorge Liu /**
3365647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3366647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3367647b3cdcSGeorge Liu  *
3368647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3369647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3370647b3cdcSGeorge Liu  * @param[out] index          Index value
3371647b3cdcSGeorge Liu  *
3372647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3373647b3cdcSGeorge Liu  */
3374647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3375647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3376647b3cdcSGeorge Liu {
3377647b3cdcSGeorge Liu     std::vector<std::string> split;
3378647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3379647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3380647b3cdcSGeorge Liu     {
3381647b3cdcSGeorge Liu         return false;
3382647b3cdcSGeorge Liu     }
3383647b3cdcSGeorge Liu 
3384ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3385647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3386ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3387647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3388647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3389647b3cdcSGeorge Liu 
3390647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3391647b3cdcSGeorge Liu     {
3392647b3cdcSGeorge Liu         return false;
3393647b3cdcSGeorge Liu     }
3394647b3cdcSGeorge Liu 
3395647b3cdcSGeorge Liu     start = split[1].data();
3396ca45aa3cSEd Tanous 
3397ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3398647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3399647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3400647b3cdcSGeorge Liu 
3401dcf2ebc0SEd Tanous     return ptrValue == end && ecValue != std::errc();
3402647b3cdcSGeorge Liu }
3403647b3cdcSGeorge Liu 
3404647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3405647b3cdcSGeorge Liu {
34060fda0f12SGeorge Liu     BMCWEB_ROUTE(
34070fda0f12SGeorge Liu         app,
34080fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3409647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3410647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
3411647b3cdcSGeorge Liu             [](const crow::Request& req,
3412647b3cdcSGeorge Liu                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3413647b3cdcSGeorge Liu                const std::string& postCodeID) {
3414647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
3415647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
3416647b3cdcSGeorge Liu                 {
3417647b3cdcSGeorge Liu                     asyncResp->res.result(
3418647b3cdcSGeorge Liu                         boost::beast::http::status::bad_request);
3419647b3cdcSGeorge Liu                     return;
3420647b3cdcSGeorge Liu                 }
3421647b3cdcSGeorge Liu 
3422647b3cdcSGeorge Liu                 uint64_t currentValue = 0;
3423647b3cdcSGeorge Liu                 uint16_t index = 0;
3424647b3cdcSGeorge Liu                 if (!parsePostCode(postCodeID, currentValue, index))
3425647b3cdcSGeorge Liu                 {
3426647b3cdcSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
3427647b3cdcSGeorge Liu                                                postCodeID);
3428647b3cdcSGeorge Liu                     return;
3429647b3cdcSGeorge Liu                 }
3430647b3cdcSGeorge Liu 
3431647b3cdcSGeorge Liu                 crow::connections::systemBus->async_method_call(
3432647b3cdcSGeorge Liu                     [asyncResp, postCodeID, currentValue](
3433647b3cdcSGeorge Liu                         const boost::system::error_code ec,
3434647b3cdcSGeorge Liu                         const std::vector<std::tuple<
3435647b3cdcSGeorge Liu                             uint64_t, std::vector<uint8_t>>>& postcodes) {
3436647b3cdcSGeorge Liu                         if (ec.value() == EBADR)
3437647b3cdcSGeorge Liu                         {
3438647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3439647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3440647b3cdcSGeorge Liu                             return;
3441647b3cdcSGeorge Liu                         }
3442647b3cdcSGeorge Liu                         if (ec)
3443647b3cdcSGeorge Liu                         {
3444647b3cdcSGeorge Liu                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3445647b3cdcSGeorge Liu                             messages::internalError(asyncResp->res);
3446647b3cdcSGeorge Liu                             return;
3447647b3cdcSGeorge Liu                         }
3448647b3cdcSGeorge Liu 
3449647b3cdcSGeorge Liu                         size_t value = static_cast<size_t>(currentValue) - 1;
3450647b3cdcSGeorge Liu                         if (value == std::string::npos ||
3451647b3cdcSGeorge Liu                             postcodes.size() < currentValue)
3452647b3cdcSGeorge Liu                         {
3453647b3cdcSGeorge Liu                             BMCWEB_LOG_ERROR << "Wrong currentValue value";
3454647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3455647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3456647b3cdcSGeorge Liu                             return;
3457647b3cdcSGeorge Liu                         }
3458647b3cdcSGeorge Liu 
34599eb808c1SEd Tanous                         const auto& [tID, c] = postcodes[value];
346046ff87baSEd Tanous                         if (c.empty())
3461647b3cdcSGeorge Liu                         {
3462647b3cdcSGeorge Liu                             BMCWEB_LOG_INFO << "No found post code data";
3463647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3464647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3465647b3cdcSGeorge Liu                             return;
3466647b3cdcSGeorge Liu                         }
346746ff87baSEd Tanous                         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
346846ff87baSEd Tanous                         const char* d = reinterpret_cast<const char*>(c.data());
346946ff87baSEd Tanous                         std::string_view strData(d, c.size());
3470647b3cdcSGeorge Liu 
3471647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Type",
3472647b3cdcSGeorge Liu                                                  "application/octet-stream");
3473647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Transfer-Encoding",
3474647b3cdcSGeorge Liu                                                  "Base64");
3475647b3cdcSGeorge Liu                         asyncResp->res.body() =
3476647b3cdcSGeorge Liu                             crow::utility::base64encode(strData);
3477647b3cdcSGeorge Liu                     },
3478647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode0",
3479647b3cdcSGeorge Liu                     "/xyz/openbmc_project/State/Boot/PostCode0",
3480647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3481647b3cdcSGeorge Liu                     index);
3482647b3cdcSGeorge Liu             });
3483647b3cdcSGeorge Liu }
3484647b3cdcSGeorge Liu 
34857e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3486a3316fc6SZhikuiRen {
34877e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
34887e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3489ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
34907e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3491ace85d60SEd Tanous             [](const crow::Request& req,
34927e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
34937e860f15SJohn Edward Broadbent                const std::string& targetID) {
3494647b3cdcSGeorge Liu                 uint16_t bootIndex = 0;
3495647b3cdcSGeorge Liu                 uint64_t codeIndex = 0;
3496647b3cdcSGeorge Liu                 if (!parsePostCode(targetID, codeIndex, bootIndex))
3497a3316fc6SZhikuiRen                 {
3498a3316fc6SZhikuiRen                     // Requested ID was not found
3499ace85d60SEd Tanous                     messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3500a3316fc6SZhikuiRen                     return;
3501a3316fc6SZhikuiRen                 }
3502a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3503a3316fc6SZhikuiRen                 {
3504a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
35057e860f15SJohn Edward Broadbent                                      << targetID;
3506a3316fc6SZhikuiRen                 }
3507a3316fc6SZhikuiRen 
35087e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
35097e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3510a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
35110fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3512a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3513a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3514a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3515a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3516a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3517a3316fc6SZhikuiRen 
3518a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
35197e860f15SJohn Edward Broadbent             });
3520a3316fc6SZhikuiRen }
3521a3316fc6SZhikuiRen 
35221da66f75SEd Tanous } // namespace redfish
3523