xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision c419c759177f7d42af4848bc9d16bca3c1644387)
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 
594851d45dSJason M. Bills namespace message_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) {
6826702d01SEd Tanous             return !std::strcmp(messageEntry.first, messageKey.c_str());
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 }
1024851d45dSJason M. Bills } // namespace message_registries
1034851d45dSJason M. Bills 
104f6150403SJames Feist namespace fs = std::filesystem;
1051da66f75SEd Tanous 
106168e20c1SEd Tanous using GetManagedPropertyType =
107168e20c1SEd Tanous     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>;
108cb92c03bSAndrew Geissler 
109cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
110cb92c03bSAndrew Geissler {
111d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
112d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
113d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
114d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
115cb92c03bSAndrew Geissler     {
116cb92c03bSAndrew Geissler         return "Critical";
117cb92c03bSAndrew Geissler     }
1183174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
119d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
120d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
121cb92c03bSAndrew Geissler     {
122cb92c03bSAndrew Geissler         return "OK";
123cb92c03bSAndrew Geissler     }
1243174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
125cb92c03bSAndrew Geissler     {
126cb92c03bSAndrew Geissler         return "Warning";
127cb92c03bSAndrew Geissler     }
128cb92c03bSAndrew Geissler     return "";
129cb92c03bSAndrew Geissler }
130cb92c03bSAndrew Geissler 
1317e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13239e77504SEd Tanous                                      const std::string_view& field,
13339e77504SEd Tanous                                      std::string_view& contents)
13416428a1aSJason M. Bills {
13516428a1aSJason M. Bills     const char* data = nullptr;
13616428a1aSJason M. Bills     size_t length = 0;
13716428a1aSJason M. Bills     int ret = 0;
13816428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
13946ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
14046ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
14146ff87baSEd Tanous 
14246ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
14316428a1aSJason M. Bills     if (ret < 0)
14416428a1aSJason M. Bills     {
14516428a1aSJason M. Bills         return ret;
14616428a1aSJason M. Bills     }
14739e77504SEd Tanous     contents = std::string_view(data, length);
14816428a1aSJason M. Bills     // Only use the content after the "=" character.
14981ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
15016428a1aSJason M. Bills     return ret;
15116428a1aSJason M. Bills }
15216428a1aSJason M. Bills 
1537e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1547e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1557e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15616428a1aSJason M. Bills {
15716428a1aSJason M. Bills     int ret = 0;
15839e77504SEd Tanous     std::string_view metadata;
15916428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
16016428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16116428a1aSJason M. Bills     if (ret < 0)
16216428a1aSJason M. Bills     {
16316428a1aSJason M. Bills         return ret;
16416428a1aSJason M. Bills     }
165b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16616428a1aSJason M. Bills     return ret;
16716428a1aSJason M. Bills }
16816428a1aSJason M. Bills 
1697e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1707e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
171a3316fc6SZhikuiRen {
172a3316fc6SZhikuiRen     int ret = 0;
173a3316fc6SZhikuiRen     uint64_t timestamp = 0;
174a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
175a3316fc6SZhikuiRen     if (ret < 0)
176a3316fc6SZhikuiRen     {
177a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
178a3316fc6SZhikuiRen                          << strerror(-ret);
179a3316fc6SZhikuiRen         return false;
180a3316fc6SZhikuiRen     }
1811d8782e7SNan Zhou     entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
1829c620e21SAsmitha Karunanithi     return true;
183a3316fc6SZhikuiRen }
18467df073bSEd Tanous #ifdef NEW_BOOST_URL
18567df073bSEd Tanous static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
18667df073bSEd Tanous                          const crow::Request& req, uint64_t& skip)
18767df073bSEd Tanous {
18867df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$skip");
18967df073bSEd Tanous     if (it != req.urlView.params().end())
19067df073bSEd Tanous     {
19167df073bSEd Tanous         std::from_chars_result r = std::from_chars(
19267df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), skip);
19367df073bSEd Tanous         if (r.ec != std::errc())
19467df073bSEd Tanous         {
19567df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$skip");
19667df073bSEd Tanous             return false;
19767df073bSEd Tanous         }
19867df073bSEd Tanous     }
19967df073bSEd Tanous     return true;
20067df073bSEd Tanous }
20167df073bSEd Tanous 
20267df073bSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
20367df073bSEd Tanous static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
20467df073bSEd Tanous                         const crow::Request& req, uint64_t& top)
20567df073bSEd Tanous {
20667df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$top");
20767df073bSEd Tanous     if (it != req.urlView.params().end())
20867df073bSEd Tanous     {
20967df073bSEd Tanous         std::from_chars_result r = std::from_chars(
21067df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), top);
21167df073bSEd Tanous         if (r.ec != std::errc())
21267df073bSEd Tanous         {
21367df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$top");
21467df073bSEd Tanous             return false;
21567df073bSEd Tanous         }
21667df073bSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
21767df073bSEd Tanous         {
21867df073bSEd Tanous 
21967df073bSEd Tanous             messages::queryParameterOutOfRange(
22067df073bSEd Tanous                 asyncResp->res, std::to_string(top), "$top",
22167df073bSEd Tanous                 "1-" + std::to_string(maxEntriesPerPage));
22267df073bSEd Tanous             return false;
22367df073bSEd Tanous         }
22467df073bSEd Tanous     }
22567df073bSEd Tanous     return true;
22667df073bSEd Tanous }
22767df073bSEd Tanous 
22867df073bSEd Tanous #else
229a3316fc6SZhikuiRen 
2308d1b46d7Szhanghch05 static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2318d1b46d7Szhanghch05                          const crow::Request& req, uint64_t& skip)
23216428a1aSJason M. Bills {
233d32c4fa9SEd Tanous     boost::urls::query_params_view::iterator it = req.urlParams.find("$skip");
2345a7e877eSJames Feist     if (it != req.urlParams.end())
23516428a1aSJason M. Bills     {
2365a7e877eSJames Feist         std::string skipParam = it->value();
23716428a1aSJason M. Bills         char* ptr = nullptr;
2385a7e877eSJames Feist         skip = std::strtoul(skipParam.c_str(), &ptr, 10);
2395a7e877eSJames Feist         if (skipParam.empty() || *ptr != '\0')
24016428a1aSJason M. Bills         {
24116428a1aSJason M. Bills 
2428d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2438d1b46d7Szhanghch05                 asyncResp->res, std::string(skipParam), "$skip");
24416428a1aSJason M. Bills             return false;
24516428a1aSJason M. Bills         }
24616428a1aSJason M. Bills     }
24716428a1aSJason M. Bills     return true;
24816428a1aSJason M. Bills }
24916428a1aSJason M. Bills 
250271584abSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
2518d1b46d7Szhanghch05 static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2528d1b46d7Szhanghch05                         const crow::Request& req, uint64_t& top)
25316428a1aSJason M. Bills {
254d32c4fa9SEd Tanous     boost::urls::query_params_view::iterator it = req.urlParams.find("$top");
2555a7e877eSJames Feist     if (it != req.urlParams.end())
25616428a1aSJason M. Bills     {
2575a7e877eSJames Feist         std::string topParam = it->value();
25816428a1aSJason M. Bills         char* ptr = nullptr;
2595a7e877eSJames Feist         top = std::strtoul(topParam.c_str(), &ptr, 10);
2605a7e877eSJames Feist         if (topParam.empty() || *ptr != '\0')
26116428a1aSJason M. Bills         {
2628d1b46d7Szhanghch05             messages::queryParameterValueTypeError(
2638d1b46d7Szhanghch05                 asyncResp->res, std::string(topParam), "$top");
26416428a1aSJason M. Bills             return false;
26516428a1aSJason M. Bills         }
266271584abSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
26716428a1aSJason M. Bills         {
26816428a1aSJason M. Bills 
26916428a1aSJason M. Bills             messages::queryParameterOutOfRange(
2708d1b46d7Szhanghch05                 asyncResp->res, std::to_string(top), "$top",
27116428a1aSJason M. Bills                 "1-" + std::to_string(maxEntriesPerPage));
27216428a1aSJason M. Bills             return false;
27316428a1aSJason M. Bills         }
27416428a1aSJason M. Bills     }
27516428a1aSJason M. Bills     return true;
27616428a1aSJason M. Bills }
27716428a1aSJason M. Bills 
27867df073bSEd Tanous #endif
27967df073bSEd Tanous 
2807e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
281e85d6b16SJason M. Bills                                     const bool firstEntry = true)
28216428a1aSJason M. Bills {
28316428a1aSJason M. Bills     int ret = 0;
28416428a1aSJason M. Bills     static uint64_t prevTs = 0;
28516428a1aSJason M. Bills     static int index = 0;
286e85d6b16SJason M. Bills     if (firstEntry)
287e85d6b16SJason M. Bills     {
288e85d6b16SJason M. Bills         prevTs = 0;
289e85d6b16SJason M. Bills     }
290e85d6b16SJason M. Bills 
29116428a1aSJason M. Bills     // Get the entry timestamp
29216428a1aSJason M. Bills     uint64_t curTs = 0;
29316428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
29416428a1aSJason M. Bills     if (ret < 0)
29516428a1aSJason M. Bills     {
29616428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
29716428a1aSJason M. Bills                          << strerror(-ret);
29816428a1aSJason M. Bills         return false;
29916428a1aSJason M. Bills     }
30016428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
30116428a1aSJason M. Bills     if (curTs == prevTs)
30216428a1aSJason M. Bills     {
30316428a1aSJason M. Bills         index++;
30416428a1aSJason M. Bills     }
30516428a1aSJason M. Bills     else
30616428a1aSJason M. Bills     {
30716428a1aSJason M. Bills         // Otherwise, reset it
30816428a1aSJason M. Bills         index = 0;
30916428a1aSJason M. Bills     }
31016428a1aSJason M. Bills     // Save the timestamp
31116428a1aSJason M. Bills     prevTs = curTs;
31216428a1aSJason M. Bills 
31316428a1aSJason M. Bills     entryID = std::to_string(curTs);
31416428a1aSJason M. Bills     if (index > 0)
31516428a1aSJason M. Bills     {
31616428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
31716428a1aSJason M. Bills     }
31816428a1aSJason M. Bills     return true;
31916428a1aSJason M. Bills }
32016428a1aSJason M. Bills 
321e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
322e85d6b16SJason M. Bills                              const bool firstEntry = true)
32395820184SJason M. Bills {
324271584abSEd Tanous     static time_t prevTs = 0;
32595820184SJason M. Bills     static int index = 0;
326e85d6b16SJason M. Bills     if (firstEntry)
327e85d6b16SJason M. Bills     {
328e85d6b16SJason M. Bills         prevTs = 0;
329e85d6b16SJason M. Bills     }
330e85d6b16SJason M. Bills 
33195820184SJason M. Bills     // Get the entry timestamp
332271584abSEd Tanous     std::time_t curTs = 0;
33395820184SJason M. Bills     std::tm timeStruct = {};
33495820184SJason M. Bills     std::istringstream entryStream(logEntry);
33595820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
33695820184SJason M. Bills     {
33795820184SJason M. Bills         curTs = std::mktime(&timeStruct);
33895820184SJason M. Bills     }
33995820184SJason M. Bills     // If the timestamp isn't unique, increment the index
34095820184SJason M. Bills     if (curTs == prevTs)
34195820184SJason M. Bills     {
34295820184SJason M. Bills         index++;
34395820184SJason M. Bills     }
34495820184SJason M. Bills     else
34595820184SJason M. Bills     {
34695820184SJason M. Bills         // Otherwise, reset it
34795820184SJason M. Bills         index = 0;
34895820184SJason M. Bills     }
34995820184SJason M. Bills     // Save the timestamp
35095820184SJason M. Bills     prevTs = curTs;
35195820184SJason M. Bills 
35295820184SJason M. Bills     entryID = std::to_string(curTs);
35395820184SJason M. Bills     if (index > 0)
35495820184SJason M. Bills     {
35595820184SJason M. Bills         entryID += "_" + std::to_string(index);
35695820184SJason M. Bills     }
35795820184SJason M. Bills     return true;
35895820184SJason M. Bills }
35995820184SJason M. Bills 
3607e860f15SJohn Edward Broadbent inline static bool
3618d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3628d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
3638d1b46d7Szhanghch05                        uint64_t& index)
36416428a1aSJason M. Bills {
36516428a1aSJason M. Bills     if (entryID.empty())
36616428a1aSJason M. Bills     {
36716428a1aSJason M. Bills         return false;
36816428a1aSJason M. Bills     }
36916428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
37039e77504SEd Tanous     std::string_view tsStr(entryID);
37116428a1aSJason M. Bills 
37281ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
37316428a1aSJason M. Bills     if (underscorePos != tsStr.npos)
37416428a1aSJason M. Bills     {
37516428a1aSJason M. Bills         // Timestamp has an index
37616428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
37739e77504SEd Tanous         std::string_view indexStr(entryID);
37816428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
379c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
380c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
381c0bd5e4bSEd Tanous         if (ec != std::errc())
38216428a1aSJason M. Bills         {
3838d1b46d7Szhanghch05             messages::resourceMissingAtURI(asyncResp->res, entryID);
38416428a1aSJason M. Bills             return false;
38516428a1aSJason M. Bills         }
38616428a1aSJason M. Bills     }
38716428a1aSJason M. Bills     // Timestamp has no index
388c0bd5e4bSEd Tanous     auto [ptr, ec] =
389c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
390c0bd5e4bSEd Tanous     if (ec != std::errc())
39116428a1aSJason M. Bills     {
3928d1b46d7Szhanghch05         messages::resourceMissingAtURI(asyncResp->res, entryID);
39316428a1aSJason M. Bills         return false;
39416428a1aSJason M. Bills     }
39516428a1aSJason M. Bills     return true;
39616428a1aSJason M. Bills }
39716428a1aSJason M. Bills 
39895820184SJason M. Bills static bool
39995820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
40095820184SJason M. Bills {
40195820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
40295820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
40395820184SJason M. Bills 
40495820184SJason M. Bills     // Loop through the directory looking for redfish log files
40595820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
40695820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
40795820184SJason M. Bills     {
40895820184SJason M. Bills         // If we find a redfish log file, save the path
40995820184SJason M. Bills         std::string filename = dirEnt.path().filename();
41095820184SJason M. Bills         if (boost::starts_with(filename, redfishLogFilename))
41195820184SJason M. Bills         {
41295820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
41395820184SJason M. Bills         }
41495820184SJason M. Bills     }
41595820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
41695820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
41795820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
41895820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
41995820184SJason M. Bills 
42095820184SJason M. Bills     return !redfishLogFiles.empty();
42195820184SJason M. Bills }
42295820184SJason M. Bills 
4238d1b46d7Szhanghch05 inline void
4248d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4255cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4265cb1dd27SAsmitha Karunanithi {
4275cb1dd27SAsmitha Karunanithi     std::string dumpPath;
4285cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
4295cb1dd27SAsmitha Karunanithi     {
4305cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
4315cb1dd27SAsmitha Karunanithi     }
4325cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
4335cb1dd27SAsmitha Karunanithi     {
4345cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
4355cb1dd27SAsmitha Karunanithi     }
4365cb1dd27SAsmitha Karunanithi     else
4375cb1dd27SAsmitha Karunanithi     {
4385cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
4395cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4405cb1dd27SAsmitha Karunanithi         return;
4415cb1dd27SAsmitha Karunanithi     }
4425cb1dd27SAsmitha Karunanithi 
4435cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
444711ac7a9SEd Tanous         [asyncResp, dumpPath,
445711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
446711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4475cb1dd27SAsmitha Karunanithi             if (ec)
4485cb1dd27SAsmitha Karunanithi             {
4495cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4505cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
4515cb1dd27SAsmitha Karunanithi                 return;
4525cb1dd27SAsmitha Karunanithi             }
4535cb1dd27SAsmitha Karunanithi 
4545cb1dd27SAsmitha Karunanithi             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4555cb1dd27SAsmitha Karunanithi             entriesArray = nlohmann::json::array();
456b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
457b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
458b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
459b47452b2SAsmitha Karunanithi                 "/entry/";
4605cb1dd27SAsmitha Karunanithi 
4615cb1dd27SAsmitha Karunanithi             for (auto& object : resp)
4625cb1dd27SAsmitha Karunanithi             {
463b47452b2SAsmitha Karunanithi                 if (object.first.str.find(dumpEntryPath) == std::string::npos)
4645cb1dd27SAsmitha Karunanithi                 {
4655cb1dd27SAsmitha Karunanithi                     continue;
4665cb1dd27SAsmitha Karunanithi                 }
4671d8782e7SNan Zhou                 uint64_t timestamp = 0;
4685cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
46935440d18SAsmitha Karunanithi                 std::string dumpStatus;
47035440d18SAsmitha Karunanithi                 nlohmann::json thisEntry;
4712dfd18efSEd Tanous 
4722dfd18efSEd Tanous                 std::string entryID = object.first.filename();
4732dfd18efSEd Tanous                 if (entryID.empty())
4745cb1dd27SAsmitha Karunanithi                 {
4755cb1dd27SAsmitha Karunanithi                     continue;
4765cb1dd27SAsmitha Karunanithi                 }
4775cb1dd27SAsmitha Karunanithi 
4785cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : object.second)
4795cb1dd27SAsmitha Karunanithi                 {
48035440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
48135440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
48235440d18SAsmitha Karunanithi                     {
48335440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
48435440d18SAsmitha Karunanithi                         {
48535440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
48635440d18SAsmitha Karunanithi                             {
48735440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
48835440d18SAsmitha Karunanithi                                     &propertyMap.second);
48935440d18SAsmitha Karunanithi                                 if (status == nullptr)
49035440d18SAsmitha Karunanithi                                 {
49135440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
49235440d18SAsmitha Karunanithi                                     break;
49335440d18SAsmitha Karunanithi                                 }
49435440d18SAsmitha Karunanithi                                 dumpStatus = *status;
49535440d18SAsmitha Karunanithi                             }
49635440d18SAsmitha Karunanithi                         }
49735440d18SAsmitha Karunanithi                     }
49835440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
49935440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
5005cb1dd27SAsmitha Karunanithi                     {
5015cb1dd27SAsmitha Karunanithi 
5025cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
5035cb1dd27SAsmitha Karunanithi                         {
5045cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
5055cb1dd27SAsmitha Karunanithi                             {
5065cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
5075cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
5085cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
5095cb1dd27SAsmitha Karunanithi                                 {
5105cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
5115cb1dd27SAsmitha Karunanithi                                     break;
5125cb1dd27SAsmitha Karunanithi                                 }
5135cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
5145cb1dd27SAsmitha Karunanithi                                 break;
5155cb1dd27SAsmitha Karunanithi                             }
5165cb1dd27SAsmitha Karunanithi                         }
5175cb1dd27SAsmitha Karunanithi                     }
5185cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
5195cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
5205cb1dd27SAsmitha Karunanithi                     {
5215cb1dd27SAsmitha Karunanithi 
5225cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
5235cb1dd27SAsmitha Karunanithi                         {
5245cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
5255cb1dd27SAsmitha Karunanithi                             {
5265cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
5275cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
5285cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
5295cb1dd27SAsmitha Karunanithi                                 {
5305cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
5315cb1dd27SAsmitha Karunanithi                                     break;
5325cb1dd27SAsmitha Karunanithi                                 }
5331d8782e7SNan Zhou                                 timestamp = (*usecsTimeStamp / 1000 / 1000);
5345cb1dd27SAsmitha Karunanithi                                 break;
5355cb1dd27SAsmitha Karunanithi                             }
5365cb1dd27SAsmitha Karunanithi                         }
5375cb1dd27SAsmitha Karunanithi                     }
5385cb1dd27SAsmitha Karunanithi                 }
5395cb1dd27SAsmitha Karunanithi 
5400fda0f12SGeorge Liu                 if (dumpStatus !=
5410fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
54235440d18SAsmitha Karunanithi                     !dumpStatus.empty())
54335440d18SAsmitha Karunanithi                 {
54435440d18SAsmitha Karunanithi                     // Dump status is not Complete, no need to enumerate
54535440d18SAsmitha Karunanithi                     continue;
54635440d18SAsmitha Karunanithi                 }
54735440d18SAsmitha Karunanithi 
548647b3cdcSGeorge Liu                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
5495cb1dd27SAsmitha Karunanithi                 thisEntry["@odata.id"] = dumpPath + entryID;
5505cb1dd27SAsmitha Karunanithi                 thisEntry["Id"] = entryID;
5515cb1dd27SAsmitha Karunanithi                 thisEntry["EntryType"] = "Event";
5521d8782e7SNan Zhou                 thisEntry["Created"] =
5531d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
5545cb1dd27SAsmitha Karunanithi                 thisEntry["Name"] = dumpType + " Dump Entry";
5555cb1dd27SAsmitha Karunanithi 
556d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataSizeBytes"] = size;
5575cb1dd27SAsmitha Karunanithi 
5585cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
5595cb1dd27SAsmitha Karunanithi                 {
560d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "Manager";
561d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
562de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
563de8d94a3SAbhishek Patel                         entryID + "/attachment";
5645cb1dd27SAsmitha Karunanithi                 }
5655cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
5665cb1dd27SAsmitha Karunanithi                 {
567d337bb72SAsmitha Karunanithi                     thisEntry["DiagnosticDataType"] = "OEM";
568d337bb72SAsmitha Karunanithi                     thisEntry["OEMDiagnosticDataType"] = "System";
569d337bb72SAsmitha Karunanithi                     thisEntry["AdditionalDataURI"] =
570de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
571de8d94a3SAbhishek Patel                         entryID + "/attachment";
5725cb1dd27SAsmitha Karunanithi                 }
57335440d18SAsmitha Karunanithi                 entriesArray.push_back(std::move(thisEntry));
5745cb1dd27SAsmitha Karunanithi             }
5755cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Members@odata.count"] =
5765cb1dd27SAsmitha Karunanithi                 entriesArray.size();
5775cb1dd27SAsmitha Karunanithi         },
5785cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5795cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5805cb1dd27SAsmitha Karunanithi }
5815cb1dd27SAsmitha Karunanithi 
5828d1b46d7Szhanghch05 inline void
5838d1b46d7Szhanghch05     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5848d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5855cb1dd27SAsmitha Karunanithi {
5865cb1dd27SAsmitha Karunanithi     std::string dumpPath;
5875cb1dd27SAsmitha Karunanithi     if (dumpType == "BMC")
5885cb1dd27SAsmitha Karunanithi     {
5895cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
5905cb1dd27SAsmitha Karunanithi     }
5915cb1dd27SAsmitha Karunanithi     else if (dumpType == "System")
5925cb1dd27SAsmitha Karunanithi     {
5935cb1dd27SAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
5945cb1dd27SAsmitha Karunanithi     }
5955cb1dd27SAsmitha Karunanithi     else
5965cb1dd27SAsmitha Karunanithi     {
5975cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type" << dumpType;
5985cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5995cb1dd27SAsmitha Karunanithi         return;
6005cb1dd27SAsmitha Karunanithi     }
6015cb1dd27SAsmitha Karunanithi 
6025cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
603711ac7a9SEd Tanous         [asyncResp, entryID, dumpPath,
604711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
605711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
6065cb1dd27SAsmitha Karunanithi             if (ec)
6075cb1dd27SAsmitha Karunanithi             {
6085cb1dd27SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
6095cb1dd27SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
6105cb1dd27SAsmitha Karunanithi                 return;
6115cb1dd27SAsmitha Karunanithi             }
6125cb1dd27SAsmitha Karunanithi 
613b47452b2SAsmitha Karunanithi             bool foundDumpEntry = false;
614b47452b2SAsmitha Karunanithi             std::string dumpEntryPath =
615b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
616b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
617b47452b2SAsmitha Karunanithi                 "/entry/";
618b47452b2SAsmitha Karunanithi 
6195cb1dd27SAsmitha Karunanithi             for (auto& objectPath : resp)
6205cb1dd27SAsmitha Karunanithi             {
621b47452b2SAsmitha Karunanithi                 if (objectPath.first.str != dumpEntryPath + entryID)
6225cb1dd27SAsmitha Karunanithi                 {
6235cb1dd27SAsmitha Karunanithi                     continue;
6245cb1dd27SAsmitha Karunanithi                 }
6255cb1dd27SAsmitha Karunanithi 
6265cb1dd27SAsmitha Karunanithi                 foundDumpEntry = true;
6271d8782e7SNan Zhou                 uint64_t timestamp = 0;
6285cb1dd27SAsmitha Karunanithi                 uint64_t size = 0;
62935440d18SAsmitha Karunanithi                 std::string dumpStatus;
6305cb1dd27SAsmitha Karunanithi 
6315cb1dd27SAsmitha Karunanithi                 for (auto& interfaceMap : objectPath.second)
6325cb1dd27SAsmitha Karunanithi                 {
63335440d18SAsmitha Karunanithi                     if (interfaceMap.first ==
63435440d18SAsmitha Karunanithi                         "xyz.openbmc_project.Common.Progress")
63535440d18SAsmitha Karunanithi                     {
63635440d18SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
63735440d18SAsmitha Karunanithi                         {
63835440d18SAsmitha Karunanithi                             if (propertyMap.first == "Status")
63935440d18SAsmitha Karunanithi                             {
64035440d18SAsmitha Karunanithi                                 auto status = std::get_if<std::string>(
64135440d18SAsmitha Karunanithi                                     &propertyMap.second);
64235440d18SAsmitha Karunanithi                                 if (status == nullptr)
64335440d18SAsmitha Karunanithi                                 {
64435440d18SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
64535440d18SAsmitha Karunanithi                                     break;
64635440d18SAsmitha Karunanithi                                 }
64735440d18SAsmitha Karunanithi                                 dumpStatus = *status;
64835440d18SAsmitha Karunanithi                             }
64935440d18SAsmitha Karunanithi                         }
65035440d18SAsmitha Karunanithi                     }
65135440d18SAsmitha Karunanithi                     else if (interfaceMap.first ==
65235440d18SAsmitha Karunanithi                              "xyz.openbmc_project.Dump.Entry")
6535cb1dd27SAsmitha Karunanithi                     {
6545cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6555cb1dd27SAsmitha Karunanithi                         {
6565cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Size")
6575cb1dd27SAsmitha Karunanithi                             {
6585cb1dd27SAsmitha Karunanithi                                 auto sizePtr =
6595cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6605cb1dd27SAsmitha Karunanithi                                 if (sizePtr == nullptr)
6615cb1dd27SAsmitha Karunanithi                                 {
6625cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6635cb1dd27SAsmitha Karunanithi                                     break;
6645cb1dd27SAsmitha Karunanithi                                 }
6655cb1dd27SAsmitha Karunanithi                                 size = *sizePtr;
6665cb1dd27SAsmitha Karunanithi                                 break;
6675cb1dd27SAsmitha Karunanithi                             }
6685cb1dd27SAsmitha Karunanithi                         }
6695cb1dd27SAsmitha Karunanithi                     }
6705cb1dd27SAsmitha Karunanithi                     else if (interfaceMap.first ==
6715cb1dd27SAsmitha Karunanithi                              "xyz.openbmc_project.Time.EpochTime")
6725cb1dd27SAsmitha Karunanithi                     {
6735cb1dd27SAsmitha Karunanithi                         for (auto& propertyMap : interfaceMap.second)
6745cb1dd27SAsmitha Karunanithi                         {
6755cb1dd27SAsmitha Karunanithi                             if (propertyMap.first == "Elapsed")
6765cb1dd27SAsmitha Karunanithi                             {
6775cb1dd27SAsmitha Karunanithi                                 const uint64_t* usecsTimeStamp =
6785cb1dd27SAsmitha Karunanithi                                     std::get_if<uint64_t>(&propertyMap.second);
6795cb1dd27SAsmitha Karunanithi                                 if (usecsTimeStamp == nullptr)
6805cb1dd27SAsmitha Karunanithi                                 {
6815cb1dd27SAsmitha Karunanithi                                     messages::internalError(asyncResp->res);
6825cb1dd27SAsmitha Karunanithi                                     break;
6835cb1dd27SAsmitha Karunanithi                                 }
6841d8782e7SNan Zhou                                 timestamp = *usecsTimeStamp / 1000 / 1000;
6855cb1dd27SAsmitha Karunanithi                                 break;
6865cb1dd27SAsmitha Karunanithi                             }
6875cb1dd27SAsmitha Karunanithi                         }
6885cb1dd27SAsmitha Karunanithi                     }
6895cb1dd27SAsmitha Karunanithi                 }
6905cb1dd27SAsmitha Karunanithi 
6910fda0f12SGeorge Liu                 if (dumpStatus !=
6920fda0f12SGeorge Liu                         "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
69335440d18SAsmitha Karunanithi                     !dumpStatus.empty())
69435440d18SAsmitha Karunanithi                 {
69535440d18SAsmitha Karunanithi                     // Dump status is not Complete
69635440d18SAsmitha Karunanithi                     // return not found until status is changed to Completed
69735440d18SAsmitha Karunanithi                     messages::resourceNotFound(asyncResp->res,
69835440d18SAsmitha Karunanithi                                                dumpType + " dump", entryID);
69935440d18SAsmitha Karunanithi                     return;
70035440d18SAsmitha Karunanithi                 }
70135440d18SAsmitha Karunanithi 
7025cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
703647b3cdcSGeorge Liu                     "#LogEntry.v1_8_0.LogEntry";
7045cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] = dumpPath + entryID;
7055cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = entryID;
7065cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["EntryType"] = "Event";
7075cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Created"] =
7081d8782e7SNan Zhou                     crow::utility::getDateTimeUint(timestamp);
7095cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
7105cb1dd27SAsmitha Karunanithi 
711d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
7125cb1dd27SAsmitha Karunanithi 
7135cb1dd27SAsmitha Karunanithi                 if (dumpType == "BMC")
7145cb1dd27SAsmitha Karunanithi                 {
715d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
716d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
717de8d94a3SAbhishek Patel                         "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/" +
718de8d94a3SAbhishek Patel                         entryID + "/attachment";
7195cb1dd27SAsmitha Karunanithi                 }
7205cb1dd27SAsmitha Karunanithi                 else if (dumpType == "System")
7215cb1dd27SAsmitha Karunanithi                 {
722d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
723d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["OEMDiagnosticDataType"] =
7245cb1dd27SAsmitha Karunanithi                         "System";
725d337bb72SAsmitha Karunanithi                     asyncResp->res.jsonValue["AdditionalDataURI"] =
726de8d94a3SAbhishek Patel                         "/redfish/v1/Systems/system/LogServices/Dump/Entries/" +
727de8d94a3SAbhishek Patel                         entryID + "/attachment";
7285cb1dd27SAsmitha Karunanithi                 }
7295cb1dd27SAsmitha Karunanithi             }
730b47452b2SAsmitha Karunanithi             if (foundDumpEntry == false)
731b47452b2SAsmitha Karunanithi             {
732b47452b2SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Can't find Dump Entry";
733b47452b2SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
734b47452b2SAsmitha Karunanithi                 return;
735b47452b2SAsmitha Karunanithi             }
7365cb1dd27SAsmitha Karunanithi         },
7375cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
7385cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
7395cb1dd27SAsmitha Karunanithi }
7405cb1dd27SAsmitha Karunanithi 
7418d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7429878256fSStanley Chu                             const std::string& entryID,
743b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
7445cb1dd27SAsmitha Karunanithi {
7453de8d8baSGeorge Liu     auto respHandler = [asyncResp,
7463de8d8baSGeorge Liu                         entryID](const boost::system::error_code ec) {
7475cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
7485cb1dd27SAsmitha Karunanithi         if (ec)
7495cb1dd27SAsmitha Karunanithi         {
7503de8d8baSGeorge Liu             if (ec.value() == EBADR)
7513de8d8baSGeorge Liu             {
7523de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
7533de8d8baSGeorge Liu                 return;
7543de8d8baSGeorge Liu             }
7555cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
7565cb1dd27SAsmitha Karunanithi                              << ec;
7575cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
7585cb1dd27SAsmitha Karunanithi             return;
7595cb1dd27SAsmitha Karunanithi         }
7605cb1dd27SAsmitha Karunanithi     };
7615cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
7625cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
763b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
764b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
765b47452b2SAsmitha Karunanithi             entryID,
7665cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
7675cb1dd27SAsmitha Karunanithi }
7685cb1dd27SAsmitha Karunanithi 
7698d1b46d7Szhanghch05 inline void
77098be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
7718d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7728d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
773a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
774a43be80fSAsmitha Karunanithi {
775a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
7766145ed6fSAsmitha Karunanithi         [dumpId, dumpPath, dumpType](
777a43be80fSAsmitha Karunanithi             boost::system::error_code err, sdbusplus::message::message& m,
778a43be80fSAsmitha Karunanithi             const std::shared_ptr<task::TaskData>& taskData) {
779cb13a392SEd Tanous             if (err)
780cb13a392SEd Tanous             {
7816145ed6fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "Error in creating a dump";
7826145ed6fSAsmitha Karunanithi                 taskData->state = "Cancelled";
7836145ed6fSAsmitha Karunanithi                 return task::completed;
784cb13a392SEd Tanous             }
785a43be80fSAsmitha Karunanithi             std::vector<std::pair<
786168e20c1SEd Tanous                 std::string, std::vector<std::pair<
787168e20c1SEd Tanous                                  std::string, dbus::utility::DbusVariantType>>>>
788a43be80fSAsmitha Karunanithi                 interfacesList;
789a43be80fSAsmitha Karunanithi 
790a43be80fSAsmitha Karunanithi             sdbusplus::message::object_path objPath;
791a43be80fSAsmitha Karunanithi 
792a43be80fSAsmitha Karunanithi             m.read(objPath, interfacesList);
793a43be80fSAsmitha Karunanithi 
794b47452b2SAsmitha Karunanithi             if (objPath.str ==
795b47452b2SAsmitha Karunanithi                 "/xyz/openbmc_project/dump/" +
796b47452b2SAsmitha Karunanithi                     std::string(boost::algorithm::to_lower_copy(dumpType)) +
797b47452b2SAsmitha Karunanithi                     "/entry/" + std::to_string(dumpId))
798a43be80fSAsmitha Karunanithi             {
799a43be80fSAsmitha Karunanithi                 nlohmann::json retMessage = messages::success();
800a43be80fSAsmitha Karunanithi                 taskData->messages.emplace_back(retMessage);
801a43be80fSAsmitha Karunanithi 
802a43be80fSAsmitha Karunanithi                 std::string headerLoc =
803a43be80fSAsmitha Karunanithi                     "Location: " + dumpPath + std::to_string(dumpId);
804a43be80fSAsmitha Karunanithi                 taskData->payload->httpHeaders.emplace_back(
805a43be80fSAsmitha Karunanithi                     std::move(headerLoc));
806a43be80fSAsmitha Karunanithi 
807a43be80fSAsmitha Karunanithi                 taskData->state = "Completed";
808b47452b2SAsmitha Karunanithi                 return task::completed;
8096145ed6fSAsmitha Karunanithi             }
810a43be80fSAsmitha Karunanithi             return task::completed;
811a43be80fSAsmitha Karunanithi         },
812a43be80fSAsmitha Karunanithi         "type='signal',interface='org.freedesktop.DBus."
813a43be80fSAsmitha Karunanithi         "ObjectManager',"
814a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
815a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
816a43be80fSAsmitha Karunanithi 
817a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
818a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
81998be3e39SEd Tanous     task->payload.emplace(std::move(payload));
820a43be80fSAsmitha Karunanithi }
821a43be80fSAsmitha Karunanithi 
8228d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
8238d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
824a43be80fSAsmitha Karunanithi {
825a43be80fSAsmitha Karunanithi 
826a43be80fSAsmitha Karunanithi     std::string dumpPath;
827a43be80fSAsmitha Karunanithi     if (dumpType == "BMC")
828a43be80fSAsmitha Karunanithi     {
829a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
830a43be80fSAsmitha Karunanithi     }
831a43be80fSAsmitha Karunanithi     else if (dumpType == "System")
832a43be80fSAsmitha Karunanithi     {
833a43be80fSAsmitha Karunanithi         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
834a43be80fSAsmitha Karunanithi     }
835a43be80fSAsmitha Karunanithi     else
836a43be80fSAsmitha Karunanithi     {
837a43be80fSAsmitha Karunanithi         BMCWEB_LOG_ERROR << "Invalid dump type: " << dumpType;
838a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
839a43be80fSAsmitha Karunanithi         return;
840a43be80fSAsmitha Karunanithi     }
841a43be80fSAsmitha Karunanithi 
842a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
843a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
844a43be80fSAsmitha Karunanithi 
845a43be80fSAsmitha Karunanithi     if (!redfish::json_util::readJson(
846a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
847a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
848a43be80fSAsmitha Karunanithi     {
849a43be80fSAsmitha Karunanithi         return;
850a43be80fSAsmitha Karunanithi     }
851a43be80fSAsmitha Karunanithi 
852a43be80fSAsmitha Karunanithi     if (dumpType == "System")
853a43be80fSAsmitha Karunanithi     {
854a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
855a43be80fSAsmitha Karunanithi         {
856a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump action parameter "
857a43be80fSAsmitha Karunanithi                                 "'DiagnosticDataType'/"
858a43be80fSAsmitha Karunanithi                                 "'OEMDiagnosticDataType' value not found!";
859a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
860a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
861a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
862a43be80fSAsmitha Karunanithi             return;
863a43be80fSAsmitha Karunanithi         }
8643174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
865a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
866a43be80fSAsmitha Karunanithi         {
867a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
868a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
869a43be80fSAsmitha Karunanithi                                     "System Dump creation parameters");
870a43be80fSAsmitha Karunanithi             return;
871a43be80fSAsmitha Karunanithi         }
872a43be80fSAsmitha Karunanithi     }
873a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
874a43be80fSAsmitha Karunanithi     {
875a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
876a43be80fSAsmitha Karunanithi         {
8770fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
8780fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
879a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
880a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
881a43be80fSAsmitha Karunanithi             return;
882a43be80fSAsmitha Karunanithi         }
8833174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
884a43be80fSAsmitha Karunanithi         {
885a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
886a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
887a43be80fSAsmitha Karunanithi             messages::invalidObject(asyncResp->res,
888a43be80fSAsmitha Karunanithi                                     "BMC Dump creation parameters");
889a43be80fSAsmitha Karunanithi             return;
890a43be80fSAsmitha Karunanithi         }
891a43be80fSAsmitha Karunanithi     }
892a43be80fSAsmitha Karunanithi 
893a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
89498be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
89598be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
89698be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
897a43be80fSAsmitha Karunanithi             if (ec)
898a43be80fSAsmitha Karunanithi             {
899a43be80fSAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
900a43be80fSAsmitha Karunanithi                 messages::internalError(asyncResp->res);
901a43be80fSAsmitha Karunanithi                 return;
902a43be80fSAsmitha Karunanithi             }
903a43be80fSAsmitha Karunanithi             BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
904a43be80fSAsmitha Karunanithi 
90598be3e39SEd Tanous             createDumpTaskCallback(std::move(payload), asyncResp, dumpId,
90698be3e39SEd Tanous                                    dumpPath, dumpType);
907a43be80fSAsmitha Karunanithi         },
908b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
909b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
910b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
911a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
912a43be80fSAsmitha Karunanithi }
913a43be80fSAsmitha Karunanithi 
9148d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9158d1b46d7Szhanghch05                       const std::string& dumpType)
91680319af1SAsmitha Karunanithi {
917b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
918b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
9198d1b46d7Szhanghch05 
92080319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
921b47452b2SAsmitha Karunanithi         [asyncResp, dumpType](const boost::system::error_code ec,
92280319af1SAsmitha Karunanithi                               const std::vector<std::string>& subTreePaths) {
92380319af1SAsmitha Karunanithi             if (ec)
92480319af1SAsmitha Karunanithi             {
92580319af1SAsmitha Karunanithi                 BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
92680319af1SAsmitha Karunanithi                 messages::internalError(asyncResp->res);
92780319af1SAsmitha Karunanithi                 return;
92880319af1SAsmitha Karunanithi             }
92980319af1SAsmitha Karunanithi 
93080319af1SAsmitha Karunanithi             for (const std::string& path : subTreePaths)
93180319af1SAsmitha Karunanithi             {
9322dfd18efSEd Tanous                 sdbusplus::message::object_path objPath(path);
9332dfd18efSEd Tanous                 std::string logID = objPath.filename();
9342dfd18efSEd Tanous                 if (logID.empty())
93580319af1SAsmitha Karunanithi                 {
9362dfd18efSEd Tanous                     continue;
93780319af1SAsmitha Karunanithi                 }
9382dfd18efSEd Tanous                 deleteDumpEntry(asyncResp, logID, dumpType);
93980319af1SAsmitha Karunanithi             }
94080319af1SAsmitha Karunanithi         },
94180319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
94280319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
94380319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
944b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
945b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
946b47452b2SAsmitha Karunanithi                                    dumpType});
94780319af1SAsmitha Karunanithi }
94880319af1SAsmitha Karunanithi 
9497e860f15SJohn Edward Broadbent inline static void parseCrashdumpParameters(
950168e20c1SEd Tanous     const std::vector<std::pair<std::string, dbus::utility::DbusVariantType>>&
951168e20c1SEd Tanous         params,
952043a0536SJohnathan Mantey     std::string& filename, std::string& timestamp, std::string& logfile)
953043a0536SJohnathan Mantey {
954043a0536SJohnathan Mantey     for (auto property : params)
955043a0536SJohnathan Mantey     {
956043a0536SJohnathan Mantey         if (property.first == "Timestamp")
957043a0536SJohnathan Mantey         {
958043a0536SJohnathan Mantey             const std::string* value =
9598d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
960043a0536SJohnathan Mantey             if (value != nullptr)
961043a0536SJohnathan Mantey             {
962043a0536SJohnathan Mantey                 timestamp = *value;
963043a0536SJohnathan Mantey             }
964043a0536SJohnathan Mantey         }
965043a0536SJohnathan Mantey         else if (property.first == "Filename")
966043a0536SJohnathan Mantey         {
967043a0536SJohnathan Mantey             const std::string* value =
9688d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
969043a0536SJohnathan Mantey             if (value != nullptr)
970043a0536SJohnathan Mantey             {
971043a0536SJohnathan Mantey                 filename = *value;
972043a0536SJohnathan Mantey             }
973043a0536SJohnathan Mantey         }
974043a0536SJohnathan Mantey         else if (property.first == "Log")
975043a0536SJohnathan Mantey         {
976043a0536SJohnathan Mantey             const std::string* value =
9778d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
978043a0536SJohnathan Mantey             if (value != nullptr)
979043a0536SJohnathan Mantey             {
980043a0536SJohnathan Mantey                 logfile = *value;
981043a0536SJohnathan Mantey             }
982043a0536SJohnathan Mantey         }
983043a0536SJohnathan Mantey     }
984043a0536SJohnathan Mantey }
985043a0536SJohnathan Mantey 
986a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
9877e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
9881da66f75SEd Tanous {
989c4bf6374SJason M. Bills     /**
990c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
991c4bf6374SJason M. Bills      */
9927e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
993ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
9947e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
9957e860f15SJohn Edward Broadbent             [](const crow::Request&,
9967e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
9977e860f15SJohn Edward Broadbent 
998c4bf6374SJason M. Bills             {
9997e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
10007e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1001c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1002c4bf6374SJason M. Bills                     "#LogServiceCollection.LogServiceCollection";
1003c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1004029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices";
10057e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
10067e860f15SJohn Edward Broadbent                     "System Log Services Collection";
1007c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1008c4bf6374SJason M. Bills                     "Collection of LogServices for this Computer System";
10097e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
10107e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1011c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
1012029573d4SEd Tanous                 logServiceArray.push_back(
10137e860f15SJohn Edward Broadbent                     {{"@odata.id",
10147e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/EventLog"}});
10155cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
1016c9bb6861Sraviteja-b                 logServiceArray.push_back(
10177e860f15SJohn Edward Broadbent                     {{"@odata.id",
10187e860f15SJohn Edward Broadbent                       "/redfish/v1/Systems/system/LogServices/Dump"}});
1019c9bb6861Sraviteja-b #endif
1020c9bb6861Sraviteja-b 
1021d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
1022d53dd41fSJason M. Bills                 logServiceArray.push_back(
1023cb92c03bSAndrew Geissler                     {{"@odata.id",
1024424c4176SJason M. Bills                       "/redfish/v1/Systems/system/LogServices/Crashdump"}});
1025d53dd41fSJason M. Bills #endif
1026b7028ebfSSpencer Ku 
1027b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
1028b7028ebfSSpencer Ku                 logServiceArray.push_back(
1029b7028ebfSSpencer Ku                     {{"@odata.id",
1030b7028ebfSSpencer Ku                       "/redfish/v1/Systems/system/LogServices/HostLogger"}});
1031b7028ebfSSpencer Ku #endif
1032c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
1033c4bf6374SJason M. Bills                     logServiceArray.size();
1034a3316fc6SZhikuiRen 
1035a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
1036a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec,
1037a3316fc6SZhikuiRen                                 const std::vector<std::string>& subtreePath) {
1038a3316fc6SZhikuiRen                         if (ec)
1039a3316fc6SZhikuiRen                         {
1040a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR << ec;
1041a3316fc6SZhikuiRen                             return;
1042a3316fc6SZhikuiRen                         }
1043a3316fc6SZhikuiRen 
1044a3316fc6SZhikuiRen                         for (auto& pathStr : subtreePath)
1045a3316fc6SZhikuiRen                         {
1046a3316fc6SZhikuiRen                             if (pathStr.find("PostCode") != std::string::npos)
1047a3316fc6SZhikuiRen                             {
104823a21a1cSEd Tanous                                 nlohmann::json& logServiceArrayLocal =
1049a3316fc6SZhikuiRen                                     asyncResp->res.jsonValue["Members"];
105023a21a1cSEd Tanous                                 logServiceArrayLocal.push_back(
10510fda0f12SGeorge Liu                                     {{"@odata.id",
10520fda0f12SGeorge Liu                                       "/redfish/v1/Systems/system/LogServices/PostCodes"}});
10537e860f15SJohn Edward Broadbent                                 asyncResp->res
10547e860f15SJohn Edward Broadbent                                     .jsonValue["Members@odata.count"] =
105523a21a1cSEd Tanous                                     logServiceArrayLocal.size();
1056a3316fc6SZhikuiRen                                 return;
1057a3316fc6SZhikuiRen                             }
1058a3316fc6SZhikuiRen                         }
1059a3316fc6SZhikuiRen                     },
1060a3316fc6SZhikuiRen                     "xyz.openbmc_project.ObjectMapper",
1061a3316fc6SZhikuiRen                     "/xyz/openbmc_project/object_mapper",
10627e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
10637e860f15SJohn Edward Broadbent                     0, std::array<const char*, 1>{postCodeIface});
10647e860f15SJohn Edward Broadbent             });
1065c4bf6374SJason M. Bills }
1066c4bf6374SJason M. Bills 
10677e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
1068c4bf6374SJason M. Bills {
10697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
1070ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
10717e860f15SJohn Edward Broadbent         .methods(
10727e860f15SJohn Edward Broadbent             boost::beast::http::verb::
10737e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
10747e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1075c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.id"] =
1076029573d4SEd Tanous                 "/redfish/v1/Systems/system/LogServices/EventLog";
1077c4bf6374SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
1078c4bf6374SJason M. Bills                 "#LogService.v1_1_0.LogService";
1079c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Event Log Service";
10807e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Description"] =
10817e860f15SJohn Edward Broadbent                 "System Event Log Service";
1082c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Id"] = "EventLog";
1083c4bf6374SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
10847c8c4058STejas Patil 
10857c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
10867c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
10877c8c4058STejas Patil 
10887c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
10897c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
10907c8c4058STejas Patil                 redfishDateTimeOffset.second;
10917c8c4058STejas Patil 
1092c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
1093c4bf6374SJason M. Bills                 {"@odata.id",
1094029573d4SEd Tanous                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries"}};
1095e7d6c8b2SGunnar Mills             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
1096e7d6c8b2SGunnar Mills 
10970fda0f12SGeorge Liu                 {"target",
10980fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
10997e860f15SJohn Edward Broadbent         });
1100489640c6SJason M. Bills }
1101489640c6SJason M. Bills 
11027e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
1103489640c6SJason M. Bills {
11047e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/Actions/"
1105489640c6SJason M. Bills                       "LogService.ClearLog/")
1106432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
11077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
11087e860f15SJohn Edward Broadbent             [](const crow::Request&,
11097e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1110489640c6SJason M. Bills                 // Clear the EventLog by deleting the log files
1111489640c6SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1112489640c6SJason M. Bills                 if (getRedfishLogFiles(redfishLogFiles))
1113489640c6SJason M. Bills                 {
1114489640c6SJason M. Bills                     for (const std::filesystem::path& file : redfishLogFiles)
1115489640c6SJason M. Bills                     {
1116489640c6SJason M. Bills                         std::error_code ec;
1117489640c6SJason M. Bills                         std::filesystem::remove(file, ec);
1118489640c6SJason M. Bills                     }
1119489640c6SJason M. Bills                 }
1120489640c6SJason M. Bills 
1121489640c6SJason M. Bills                 // Reload rsyslog so it knows to start new log files
1122489640c6SJason M. Bills                 crow::connections::systemBus->async_method_call(
1123489640c6SJason M. Bills                     [asyncResp](const boost::system::error_code ec) {
1124489640c6SJason M. Bills                         if (ec)
1125489640c6SJason M. Bills                         {
11267e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to reload rsyslog: "
11277e860f15SJohn Edward Broadbent                                              << ec;
1128489640c6SJason M. Bills                             messages::internalError(asyncResp->res);
1129489640c6SJason M. Bills                             return;
1130489640c6SJason M. Bills                         }
1131489640c6SJason M. Bills 
1132489640c6SJason M. Bills                         messages::success(asyncResp->res);
1133489640c6SJason M. Bills                     },
1134489640c6SJason M. Bills                     "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
11357e860f15SJohn Edward Broadbent                     "org.freedesktop.systemd1.Manager", "ReloadUnit",
11367e860f15SJohn Edward Broadbent                     "rsyslog.service", "replace");
11377e860f15SJohn Edward Broadbent             });
1138c4bf6374SJason M. Bills }
1139c4bf6374SJason M. Bills 
114095820184SJason M. Bills static int fillEventLogEntryJson(const std::string& logEntryID,
1141b5a76932SEd Tanous                                  const std::string& logEntry,
114295820184SJason M. Bills                                  nlohmann::json& logEntryJson)
1143c4bf6374SJason M. Bills {
114495820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1145cd225da8SJason M. Bills     // First get the Timestamp
1146f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1147cd225da8SJason M. Bills     if (space == std::string::npos)
114895820184SJason M. Bills     {
114995820184SJason M. Bills         return 1;
115095820184SJason M. Bills     }
1151cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1152cd225da8SJason M. Bills     // Then get the log contents
1153f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1154cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1155cd225da8SJason M. Bills     {
1156cd225da8SJason M. Bills         return 1;
1157cd225da8SJason M. Bills     }
1158cd225da8SJason M. Bills     std::string_view entry(logEntry);
1159cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1160cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1161cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1162cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1163cd225da8SJason M. Bills                  boost::token_compress_on);
1164cd225da8SJason M. Bills     // We need at least a MessageId to be valid
116526f6976fSEd Tanous     if (logEntryFields.empty())
1166cd225da8SJason M. Bills     {
1167cd225da8SJason M. Bills         return 1;
1168cd225da8SJason M. Bills     }
1169cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
117095820184SJason M. Bills 
11714851d45dSJason M. Bills     // Get the Message from the MessageRegistry
11724851d45dSJason M. Bills     const message_registries::Message* message =
11734851d45dSJason M. Bills         message_registries::getMessage(messageID);
1174c4bf6374SJason M. Bills 
11754851d45dSJason M. Bills     std::string msg;
11764851d45dSJason M. Bills     std::string severity;
11774851d45dSJason M. Bills     if (message != nullptr)
1178c4bf6374SJason M. Bills     {
11794851d45dSJason M. Bills         msg = message->message;
11804851d45dSJason M. Bills         severity = message->severity;
1181c4bf6374SJason M. Bills     }
1182c4bf6374SJason M. Bills 
118315a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
118426702d01SEd Tanous     std::span<std::string> messageArgs;
118515a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
118615a86ff6SJason M. Bills     {
118715a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
118815a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
118915a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
119015a86ff6SJason M. Bills         if (!messageArgsStart.empty())
119115a86ff6SJason M. Bills         {
119215a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
119315a86ff6SJason M. Bills         }
119415a86ff6SJason M. Bills 
119523a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1196c4bf6374SJason M. Bills 
11974851d45dSJason M. Bills         // Fill the MessageArgs into the Message
119895820184SJason M. Bills         int i = 0;
119995820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
12004851d45dSJason M. Bills         {
120195820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
12024851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
12034851d45dSJason M. Bills             if (argPos != std::string::npos)
12044851d45dSJason M. Bills             {
120595820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
12064851d45dSJason M. Bills             }
12074851d45dSJason M. Bills         }
120815a86ff6SJason M. Bills     }
12094851d45dSJason M. Bills 
121095820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
121195820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
121295820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1213f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1214f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
121595820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1216c4bf6374SJason M. Bills     {
121795820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1218c4bf6374SJason M. Bills     }
1219c4bf6374SJason M. Bills 
1220c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
122195820184SJason M. Bills     logEntryJson = {
1222647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
1223029573d4SEd Tanous         {"@odata.id",
1224897967deSJason M. Bills          "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
122595820184SJason M. Bills              logEntryID},
1226c4bf6374SJason M. Bills         {"Name", "System Event Log Entry"},
122795820184SJason M. Bills         {"Id", logEntryID},
122895820184SJason M. Bills         {"Message", std::move(msg)},
122995820184SJason M. Bills         {"MessageId", std::move(messageID)},
1230f23b7296SEd Tanous         {"MessageArgs", messageArgs},
1231c4bf6374SJason M. Bills         {"EntryType", "Event"},
123295820184SJason M. Bills         {"Severity", std::move(severity)},
123395820184SJason M. Bills         {"Created", std::move(timestamp)}};
1234c4bf6374SJason M. Bills     return 0;
1235c4bf6374SJason M. Bills }
1236c4bf6374SJason M. Bills 
12377e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1238c4bf6374SJason M. Bills {
12397e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
12407e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
12418b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
12427e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
12437e860f15SJohn Edward Broadbent             [](const crow::Request& req,
12447e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1245271584abSEd Tanous                 uint64_t skip = 0;
1246271584abSEd Tanous                 uint64_t top = maxEntriesPerPage; // Show max entries by default
12478d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
1248c4bf6374SJason M. Bills                 {
1249c4bf6374SJason M. Bills                     return;
1250c4bf6374SJason M. Bills                 }
12518d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
1252c4bf6374SJason M. Bills                 {
1253c4bf6374SJason M. Bills                     return;
1254c4bf6374SJason M. Bills                 }
12557e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
12567e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
1257c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
1258c4bf6374SJason M. Bills                     "#LogEntryCollection.LogEntryCollection";
1259c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
1260029573d4SEd Tanous                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1261c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1262c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
1263c4bf6374SJason M. Bills                     "Collection of System Event Log Entries";
1264cb92c03bSAndrew Geissler 
12657e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
12667e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
1267c4bf6374SJason M. Bills                 logEntryArray = nlohmann::json::array();
12687e860f15SJohn Edward Broadbent                 // Go through the log files and create a unique ID for each
12697e860f15SJohn Edward Broadbent                 // entry
127095820184SJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
127195820184SJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1272b01bf299SEd Tanous                 uint64_t entryCount = 0;
1273cd225da8SJason M. Bills                 std::string logEntry;
127495820184SJason M. Bills 
12757e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
12767e860f15SJohn Edward Broadbent                 // backwards
12777e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
12787e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1279c4bf6374SJason M. Bills                 {
1280cd225da8SJason M. Bills                     std::ifstream logStream(*it);
128195820184SJason M. Bills                     if (!logStream.is_open())
1282c4bf6374SJason M. Bills                     {
1283c4bf6374SJason M. Bills                         continue;
1284c4bf6374SJason M. Bills                     }
1285c4bf6374SJason M. Bills 
1286e85d6b16SJason M. Bills                     // Reset the unique ID on the first entry
1287e85d6b16SJason M. Bills                     bool firstEntry = true;
128895820184SJason M. Bills                     while (std::getline(logStream, logEntry))
128995820184SJason M. Bills                     {
1290c4bf6374SJason M. Bills                         entryCount++;
12917e860f15SJohn Edward Broadbent                         // Handle paging using skip (number of entries to skip
12927e860f15SJohn Edward Broadbent                         // from the start) and top (number of entries to
12937e860f15SJohn Edward Broadbent                         // display)
1294c4bf6374SJason M. Bills                         if (entryCount <= skip || entryCount > skip + top)
1295c4bf6374SJason M. Bills                         {
1296c4bf6374SJason M. Bills                             continue;
1297c4bf6374SJason M. Bills                         }
1298c4bf6374SJason M. Bills 
1299c4bf6374SJason M. Bills                         std::string idStr;
1300e85d6b16SJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1301c4bf6374SJason M. Bills                         {
1302c4bf6374SJason M. Bills                             continue;
1303c4bf6374SJason M. Bills                         }
1304c4bf6374SJason M. Bills 
1305e85d6b16SJason M. Bills                         if (firstEntry)
1306e85d6b16SJason M. Bills                         {
1307e85d6b16SJason M. Bills                             firstEntry = false;
1308e85d6b16SJason M. Bills                         }
1309e85d6b16SJason M. Bills 
1310c4bf6374SJason M. Bills                         logEntryArray.push_back({});
1311c4bf6374SJason M. Bills                         nlohmann::json& bmcLogEntry = logEntryArray.back();
13127e860f15SJohn Edward Broadbent                         if (fillEventLogEntryJson(idStr, logEntry,
13137e860f15SJohn Edward Broadbent                                                   bmcLogEntry) != 0)
1314c4bf6374SJason M. Bills                         {
1315c4bf6374SJason M. Bills                             messages::internalError(asyncResp->res);
1316c4bf6374SJason M. Bills                             return;
1317c4bf6374SJason M. Bills                         }
1318c4bf6374SJason M. Bills                     }
131995820184SJason M. Bills                 }
1320c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
1321c4bf6374SJason M. Bills                 if (skip + top < entryCount)
1322c4bf6374SJason M. Bills                 {
1323c4bf6374SJason M. Bills                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
132495820184SJason M. Bills                         "/redfish/v1/Systems/system/LogServices/EventLog/"
132595820184SJason M. Bills                         "Entries?$skip=" +
1326c4bf6374SJason M. Bills                         std::to_string(skip + top);
1327c4bf6374SJason M. Bills                 }
13287e860f15SJohn Edward Broadbent             });
1329897967deSJason M. Bills }
1330897967deSJason M. Bills 
13317e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1332897967deSJason M. Bills {
13337e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
13347e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1335ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
13367e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
13377e860f15SJohn Edward Broadbent             [](const crow::Request&,
13387e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
13397e860f15SJohn Edward Broadbent                const std::string& param) {
13407e860f15SJohn Edward Broadbent                 const std::string& targetID = param;
13418d1b46d7Szhanghch05 
13427e860f15SJohn Edward Broadbent                 // Go through the log files and check the unique ID for each
13437e860f15SJohn Edward Broadbent                 // entry to find the target entry
1344897967deSJason M. Bills                 std::vector<std::filesystem::path> redfishLogFiles;
1345897967deSJason M. Bills                 getRedfishLogFiles(redfishLogFiles);
1346897967deSJason M. Bills                 std::string logEntry;
1347897967deSJason M. Bills 
13487e860f15SJohn Edward Broadbent                 // Oldest logs are in the last file, so start there and loop
13497e860f15SJohn Edward Broadbent                 // backwards
13507e860f15SJohn Edward Broadbent                 for (auto it = redfishLogFiles.rbegin();
13517e860f15SJohn Edward Broadbent                      it < redfishLogFiles.rend(); it++)
1352897967deSJason M. Bills                 {
1353897967deSJason M. Bills                     std::ifstream logStream(*it);
1354897967deSJason M. Bills                     if (!logStream.is_open())
1355897967deSJason M. Bills                     {
1356897967deSJason M. Bills                         continue;
1357897967deSJason M. Bills                     }
1358897967deSJason M. Bills 
1359897967deSJason M. Bills                     // Reset the unique ID on the first entry
1360897967deSJason M. Bills                     bool firstEntry = true;
1361897967deSJason M. Bills                     while (std::getline(logStream, logEntry))
1362897967deSJason M. Bills                     {
1363897967deSJason M. Bills                         std::string idStr;
1364897967deSJason M. Bills                         if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1365897967deSJason M. Bills                         {
1366897967deSJason M. Bills                             continue;
1367897967deSJason M. Bills                         }
1368897967deSJason M. Bills 
1369897967deSJason M. Bills                         if (firstEntry)
1370897967deSJason M. Bills                         {
1371897967deSJason M. Bills                             firstEntry = false;
1372897967deSJason M. Bills                         }
1373897967deSJason M. Bills 
1374897967deSJason M. Bills                         if (idStr == targetID)
1375897967deSJason M. Bills                         {
13767e860f15SJohn Edward Broadbent                             if (fillEventLogEntryJson(
13777e860f15SJohn Edward Broadbent                                     idStr, logEntry,
1378897967deSJason M. Bills                                     asyncResp->res.jsonValue) != 0)
1379897967deSJason M. Bills                             {
1380897967deSJason M. Bills                                 messages::internalError(asyncResp->res);
1381897967deSJason M. Bills                                 return;
1382897967deSJason M. Bills                             }
1383897967deSJason M. Bills                             return;
1384897967deSJason M. Bills                         }
1385897967deSJason M. Bills                     }
1386897967deSJason M. Bills                 }
1387897967deSJason M. Bills                 // Requested ID was not found
1388897967deSJason M. Bills                 messages::resourceMissingAtURI(asyncResp->res, targetID);
13897e860f15SJohn Edward Broadbent             });
139008a4e4b5SAnthony Wilson }
139108a4e4b5SAnthony Wilson 
13927e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
139308a4e4b5SAnthony Wilson {
13947e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
13957e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1396ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
13977e860f15SJohn Edward Broadbent         .methods(
13987e860f15SJohn Edward Broadbent             boost::beast::http::verb::
13997e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
14007e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
14017e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
14027e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
140308a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.type"] =
140408a4e4b5SAnthony Wilson                 "#LogEntryCollection.LogEntryCollection";
140508a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["@odata.id"] =
140608a4e4b5SAnthony Wilson                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
140708a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
140808a4e4b5SAnthony Wilson             asyncResp->res.jsonValue["Description"] =
140908a4e4b5SAnthony Wilson                 "Collection of System Event Log Entries";
141008a4e4b5SAnthony Wilson 
1411cb92c03bSAndrew Geissler             // DBus implementation of EventLog/Entries
1412cb92c03bSAndrew Geissler             // Make call to Logging Service to find all log entry objects
1413cb92c03bSAndrew Geissler             crow::connections::systemBus->async_method_call(
1414cb92c03bSAndrew Geissler                 [asyncResp](const boost::system::error_code ec,
1415914e2d5dSEd Tanous                             const dbus::utility::ManagedObjectType& resp) {
1416cb92c03bSAndrew Geissler                     if (ec)
1417cb92c03bSAndrew Geissler                     {
1418cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
1419cb92c03bSAndrew Geissler                         BMCWEB_LOG_ERROR
1420cb92c03bSAndrew Geissler                             << "getLogEntriesIfaceData resp_handler got error "
1421cb92c03bSAndrew Geissler                             << ec;
1422cb92c03bSAndrew Geissler                         messages::internalError(asyncResp->res);
1423cb92c03bSAndrew Geissler                         return;
1424cb92c03bSAndrew Geissler                     }
1425cb92c03bSAndrew Geissler                     nlohmann::json& entriesArray =
1426cb92c03bSAndrew Geissler                         asyncResp->res.jsonValue["Members"];
1427cb92c03bSAndrew Geissler                     entriesArray = nlohmann::json::array();
1428cb92c03bSAndrew Geissler                     for (auto& objectPath : resp)
1429cb92c03bSAndrew Geissler                     {
1430914e2d5dSEd Tanous                         const uint32_t* id = nullptr;
1431*c419c759SEd Tanous                         const uint64_t* timestamp = nullptr;
1432*c419c759SEd Tanous                         const uint64_t* updateTimestamp = nullptr;
1433914e2d5dSEd Tanous                         const std::string* severity = nullptr;
1434914e2d5dSEd Tanous                         const std::string* message = nullptr;
1435914e2d5dSEd Tanous                         const std::string* filePath = nullptr;
143675710de2SXiaochao Ma                         bool resolved = false;
1437f86bb901SAdriana Kobylak                         for (auto& interfaceMap : objectPath.second)
1438f86bb901SAdriana Kobylak                         {
1439f86bb901SAdriana Kobylak                             if (interfaceMap.first ==
1440f86bb901SAdriana Kobylak                                 "xyz.openbmc_project.Logging.Entry")
1441f86bb901SAdriana Kobylak                             {
1442cb92c03bSAndrew Geissler                                 for (auto& propertyMap : interfaceMap.second)
1443cb92c03bSAndrew Geissler                                 {
1444cb92c03bSAndrew Geissler                                     if (propertyMap.first == "Id")
1445cb92c03bSAndrew Geissler                                     {
1446f86bb901SAdriana Kobylak                                         id = std::get_if<uint32_t>(
1447f86bb901SAdriana Kobylak                                             &propertyMap.second);
1448cb92c03bSAndrew Geissler                                     }
1449cb92c03bSAndrew Geissler                                     else if (propertyMap.first == "Timestamp")
1450cb92c03bSAndrew Geissler                                     {
1451*c419c759SEd Tanous                                         timestamp = std::get_if<uint64_t>(
1452f86bb901SAdriana Kobylak                                             &propertyMap.second);
14537e860f15SJohn Edward Broadbent                                     }
14547e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
14557e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
14567e860f15SJohn Edward Broadbent                                     {
1457*c419c759SEd Tanous                                         updateTimestamp = std::get_if<uint64_t>(
14587e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14597e860f15SJohn Edward Broadbent                                     }
14607e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
14617e860f15SJohn Edward Broadbent                                     {
14627e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
14637e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14647e860f15SJohn Edward Broadbent                                     }
14657e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
14667e860f15SJohn Edward Broadbent                                     {
14677e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
14687e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14697e860f15SJohn Edward Broadbent                                     }
14707e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
14717e860f15SJohn Edward Broadbent                                     {
1472914e2d5dSEd Tanous                                         const bool* resolveptr =
1473914e2d5dSEd Tanous                                             std::get_if<bool>(
14747e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14757e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14767e860f15SJohn Edward Broadbent                                         {
14777e860f15SJohn Edward Broadbent                                             messages::internalError(
14787e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14797e860f15SJohn Edward Broadbent                                             return;
14807e860f15SJohn Edward Broadbent                                         }
14817e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14827e860f15SJohn Edward Broadbent                                     }
14837e860f15SJohn Edward Broadbent                                 }
14847e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14857e860f15SJohn Edward Broadbent                                     severity == nullptr)
14867e860f15SJohn Edward Broadbent                                 {
14877e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
14887e860f15SJohn Edward Broadbent                                     return;
14897e860f15SJohn Edward Broadbent                                 }
14907e860f15SJohn Edward Broadbent                             }
14917e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
14927e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
14937e860f15SJohn Edward Broadbent                             {
14947e860f15SJohn Edward Broadbent                                 for (auto& propertyMap : interfaceMap.second)
14957e860f15SJohn Edward Broadbent                                 {
14967e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
14977e860f15SJohn Edward Broadbent                                     {
14987e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
14997e860f15SJohn Edward Broadbent                                             &propertyMap.second);
15007e860f15SJohn Edward Broadbent                                     }
15017e860f15SJohn Edward Broadbent                                 }
15027e860f15SJohn Edward Broadbent                             }
15037e860f15SJohn Edward Broadbent                         }
15047e860f15SJohn Edward Broadbent                         // Object path without the
15057e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
15067e860f15SJohn Edward Broadbent                         // and continue.
15077e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
1508*c419c759SEd Tanous                             severity == nullptr || timestamp == nullptr ||
1509*c419c759SEd Tanous                             updateTimestamp == nullptr)
15107e860f15SJohn Edward Broadbent                         {
15117e860f15SJohn Edward Broadbent                             continue;
15127e860f15SJohn Edward Broadbent                         }
15137e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
15147e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
15157e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
15167e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
15170fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
15187e860f15SJohn Edward Broadbent                             std::to_string(*id);
15197e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
15207e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
15217e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
15227e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
15237e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
15247e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
15257e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
15267e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
1527*c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*timestamp);
15287e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
1529*c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*updateTimestamp);
15307e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
15317e860f15SJohn Edward Broadbent                         {
15327e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
15330fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
15347e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
15357e860f15SJohn Edward Broadbent                         }
15367e860f15SJohn Edward Broadbent                     }
15377e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
15387e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
15397e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
15407e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
15417e860f15SJohn Edward Broadbent                               });
15427e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
15437e860f15SJohn Edward Broadbent                         entriesArray.size();
15447e860f15SJohn Edward Broadbent                 },
15457e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
15467e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15477e860f15SJohn Edward Broadbent         });
15487e860f15SJohn Edward Broadbent }
15497e860f15SJohn Edward Broadbent 
15507e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
15517e860f15SJohn Edward Broadbent {
15527e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15537e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1554ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
15557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
15567e860f15SJohn Edward Broadbent             [](const crow::Request&,
15577e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15587e860f15SJohn Edward Broadbent                const std::string& param)
15597e860f15SJohn Edward Broadbent 
15607e860f15SJohn Edward Broadbent             {
15617e860f15SJohn Edward Broadbent                 std::string entryID = param;
15627e860f15SJohn Edward Broadbent                 dbus::utility::escapePathForDbus(entryID);
15637e860f15SJohn Edward Broadbent 
15647e860f15SJohn Edward Broadbent                 // DBus implementation of EventLog/Entries
15657e860f15SJohn Edward Broadbent                 // Make call to Logging Service to find all log entry objects
15667e860f15SJohn Edward Broadbent                 crow::connections::systemBus->async_method_call(
15677e860f15SJohn Edward Broadbent                     [asyncResp, entryID](const boost::system::error_code ec,
1568914e2d5dSEd Tanous                                          const GetManagedPropertyType& resp) {
15697e860f15SJohn Edward Broadbent                         if (ec.value() == EBADR)
15707e860f15SJohn Edward Broadbent                         {
15717e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
15727e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogEntry", entryID);
15737e860f15SJohn Edward Broadbent                             return;
15747e860f15SJohn Edward Broadbent                         }
15757e860f15SJohn Edward Broadbent                         if (ec)
15767e860f15SJohn Edward Broadbent                         {
15770fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
15780fda0f12SGeorge Liu                                 << "EventLogEntry (DBus) resp_handler got error "
15797e860f15SJohn Edward Broadbent                                 << ec;
15807e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
15817e860f15SJohn Edward Broadbent                             return;
15827e860f15SJohn Edward Broadbent                         }
1583914e2d5dSEd Tanous                         const uint32_t* id = nullptr;
1584*c419c759SEd Tanous                         const uint64_t* timestamp = nullptr;
1585*c419c759SEd Tanous                         const uint64_t* updateTimestamp = nullptr;
1586914e2d5dSEd Tanous                         const std::string* severity = nullptr;
1587914e2d5dSEd Tanous                         const std::string* message = nullptr;
1588914e2d5dSEd Tanous                         const std::string* filePath = nullptr;
15897e860f15SJohn Edward Broadbent                         bool resolved = false;
15907e860f15SJohn Edward Broadbent 
15917e860f15SJohn Edward Broadbent                         for (auto& propertyMap : resp)
15927e860f15SJohn Edward Broadbent                         {
15937e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Id")
15947e860f15SJohn Edward Broadbent                             {
15957e860f15SJohn Edward Broadbent                                 id = std::get_if<uint32_t>(&propertyMap.second);
15967e860f15SJohn Edward Broadbent                             }
15977e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Timestamp")
15987e860f15SJohn Edward Broadbent                             {
1599*c419c759SEd Tanous                                 timestamp =
16007e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1601ebd45906SGeorge Liu                             }
1602d139c236SGeorge Liu                             else if (propertyMap.first == "UpdateTimestamp")
1603d139c236SGeorge Liu                             {
1604ebd45906SGeorge Liu                                 updateTimestamp =
1605*c419c759SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
1606ebd45906SGeorge Liu                             }
1607cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1608cb92c03bSAndrew Geissler                             {
1609cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1610cb92c03bSAndrew Geissler                                     &propertyMap.second);
1611cb92c03bSAndrew Geissler                             }
1612cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1613cb92c03bSAndrew Geissler                             {
1614cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1615cb92c03bSAndrew Geissler                                     &propertyMap.second);
1616ae34c8e8SAdriana Kobylak                             }
161775710de2SXiaochao Ma                             else if (propertyMap.first == "Resolved")
161875710de2SXiaochao Ma                             {
1619914e2d5dSEd Tanous                                 const bool* resolveptr =
162075710de2SXiaochao Ma                                     std::get_if<bool>(&propertyMap.second);
162175710de2SXiaochao Ma                                 if (resolveptr == nullptr)
162275710de2SXiaochao Ma                                 {
162375710de2SXiaochao Ma                                     messages::internalError(asyncResp->res);
162475710de2SXiaochao Ma                                     return;
162575710de2SXiaochao Ma                                 }
162675710de2SXiaochao Ma                                 resolved = *resolveptr;
162775710de2SXiaochao Ma                             }
16287e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Path")
1629f86bb901SAdriana Kobylak                             {
1630f86bb901SAdriana Kobylak                                 filePath = std::get_if<std::string>(
1631f86bb901SAdriana Kobylak                                     &propertyMap.second);
1632f86bb901SAdriana Kobylak                             }
1633f86bb901SAdriana Kobylak                         }
1634f86bb901SAdriana Kobylak                         if (id == nullptr || message == nullptr ||
1635*c419c759SEd Tanous                             severity == nullptr || timestamp == nullptr ||
1636*c419c759SEd Tanous                             updateTimestamp == nullptr)
1637f86bb901SAdriana Kobylak                         {
1638ae34c8e8SAdriana Kobylak                             messages::internalError(asyncResp->res);
1639271584abSEd Tanous                             return;
1640271584abSEd Tanous                         }
1641f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.type"] =
1642f86bb901SAdriana Kobylak                             "#LogEntry.v1_8_0.LogEntry";
1643f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.id"] =
16440fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1645f86bb901SAdriana Kobylak                             std::to_string(*id);
16467e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Name"] =
16477e860f15SJohn Edward Broadbent                             "System Event Log Entry";
1648f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1649f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Message"] = *message;
1650f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Resolved"] = resolved;
1651f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["EntryType"] = "Event";
1652f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Severity"] =
1653f86bb901SAdriana Kobylak                             translateSeverityDbusToRedfish(*severity);
1654f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Created"] =
1655*c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*timestamp);
1656f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Modified"] =
1657*c419c759SEd Tanous                             crow::utility::getDateTimeUintMs(*updateTimestamp);
1658f86bb901SAdriana Kobylak                         if (filePath != nullptr)
1659f86bb901SAdriana Kobylak                         {
1660f86bb901SAdriana Kobylak                             asyncResp->res.jsonValue["AdditionalDataURI"] =
16610fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
16627e860f15SJohn Edward Broadbent                                 std::to_string(*id);
1663f86bb901SAdriana Kobylak                         }
1664cb92c03bSAndrew Geissler                     },
1665cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Logging",
1666cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging/entry/" + entryID,
1667f86bb901SAdriana Kobylak                     "org.freedesktop.DBus.Properties", "GetAll", "");
16687e860f15SJohn Edward Broadbent             });
1669336e96c6SChicago Duan 
16707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16717e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1672ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16737e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
16747e860f15SJohn Edward Broadbent             [](const crow::Request& req,
16757e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16767e860f15SJohn Edward Broadbent                const std::string& entryId) {
167775710de2SXiaochao Ma                 std::optional<bool> resolved;
167875710de2SXiaochao Ma 
16797e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "Resolved",
16807e860f15SJohn Edward Broadbent                                          resolved))
168175710de2SXiaochao Ma                 {
168275710de2SXiaochao Ma                     return;
168375710de2SXiaochao Ma                 }
168475710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
168575710de2SXiaochao Ma 
168675710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
16874f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
168875710de2SXiaochao Ma                         if (ec)
168975710de2SXiaochao Ma                         {
169075710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
169175710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
169275710de2SXiaochao Ma                             return;
169375710de2SXiaochao Ma                         }
169475710de2SXiaochao Ma                     },
169575710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
169675710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
169775710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
169875710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
1699168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*resolved));
17007e860f15SJohn Edward Broadbent             });
170175710de2SXiaochao Ma 
17027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
17037e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1704ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1705ed398213SEd Tanous 
17067e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
17077e860f15SJohn Edward Broadbent             [](const crow::Request&,
17087e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17097e860f15SJohn Edward Broadbent                const std::string& param)
17107e860f15SJohn Edward Broadbent 
1711336e96c6SChicago Duan             {
1712336e96c6SChicago Duan                 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1713336e96c6SChicago Duan 
17147e860f15SJohn Edward Broadbent                 std::string entryID = param;
1715336e96c6SChicago Duan 
1716336e96c6SChicago Duan                 dbus::utility::escapePathForDbus(entryID);
1717336e96c6SChicago Duan 
1718336e96c6SChicago Duan                 // Process response from Logging service.
17197e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp, entryID](
17207e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
17217e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
17227e860f15SJohn Edward Broadbent                         << "EventLogEntry (DBus) doDelete callback: Done";
1723336e96c6SChicago Duan                     if (ec)
1724336e96c6SChicago Duan                     {
17253de8d8baSGeorge Liu                         if (ec.value() == EBADR)
17263de8d8baSGeorge Liu                         {
17277e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
17287e860f15SJohn Edward Broadbent                                                        "LogEntry", entryID);
17293de8d8baSGeorge Liu                             return;
17303de8d8baSGeorge Liu                         }
1731336e96c6SChicago Duan                         // TODO Handle for specific error code
17320fda0f12SGeorge Liu                         BMCWEB_LOG_ERROR
17330fda0f12SGeorge Liu                             << "EventLogEntry (DBus) doDelete respHandler got error "
1734336e96c6SChicago Duan                             << ec;
1735336e96c6SChicago Duan                         asyncResp->res.result(
1736336e96c6SChicago Duan                             boost::beast::http::status::internal_server_error);
1737336e96c6SChicago Duan                         return;
1738336e96c6SChicago Duan                     }
1739336e96c6SChicago Duan 
1740336e96c6SChicago Duan                     asyncResp->res.result(boost::beast::http::status::ok);
1741336e96c6SChicago Duan                 };
1742336e96c6SChicago Duan 
1743336e96c6SChicago Duan                 // Make call to Logging service to request Delete Log
1744336e96c6SChicago Duan                 crow::connections::systemBus->async_method_call(
1745336e96c6SChicago Duan                     respHandler, "xyz.openbmc_project.Logging",
1746336e96c6SChicago Duan                     "/xyz/openbmc_project/logging/entry/" + entryID,
1747336e96c6SChicago Duan                     "xyz.openbmc_project.Object.Delete", "Delete");
17487e860f15SJohn Edward Broadbent             });
1749400fd1fbSAdriana Kobylak }
1750400fd1fbSAdriana Kobylak 
17517e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1752400fd1fbSAdriana Kobylak {
17530fda0f12SGeorge Liu     BMCWEB_ROUTE(
17540fda0f12SGeorge Liu         app,
17550fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1756ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17577e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
17587e860f15SJohn Edward Broadbent             [](const crow::Request& req,
17597e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17607e860f15SJohn Edward Broadbent                const std::string& param)
1761400fd1fbSAdriana Kobylak 
17627e860f15SJohn Edward Broadbent             {
1763647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
1764647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
1765400fd1fbSAdriana Kobylak                 {
17667e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17677e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1768400fd1fbSAdriana Kobylak                     return;
1769400fd1fbSAdriana Kobylak                 }
1770400fd1fbSAdriana Kobylak 
17717e860f15SJohn Edward Broadbent                 std::string entryID = param;
1772400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1773400fd1fbSAdriana Kobylak 
1774400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17757e860f15SJohn Edward Broadbent                     [asyncResp,
17767e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1777400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1778400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1779400fd1fbSAdriana Kobylak                         {
17807e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
17817e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1782400fd1fbSAdriana Kobylak                             return;
1783400fd1fbSAdriana Kobylak                         }
1784400fd1fbSAdriana Kobylak                         if (ec)
1785400fd1fbSAdriana Kobylak                         {
1786400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1787400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1788400fd1fbSAdriana Kobylak                             return;
1789400fd1fbSAdriana Kobylak                         }
1790400fd1fbSAdriana Kobylak 
1791400fd1fbSAdriana Kobylak                         int fd = -1;
1792400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1793400fd1fbSAdriana Kobylak                         if (fd == -1)
1794400fd1fbSAdriana Kobylak                         {
1795400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1796400fd1fbSAdriana Kobylak                             return;
1797400fd1fbSAdriana Kobylak                         }
1798400fd1fbSAdriana Kobylak 
1799400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1800400fd1fbSAdriana Kobylak                         if (size == -1)
1801400fd1fbSAdriana Kobylak                         {
1802400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1803400fd1fbSAdriana Kobylak                             return;
1804400fd1fbSAdriana Kobylak                         }
1805400fd1fbSAdriana Kobylak 
1806400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1807400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1808400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1809400fd1fbSAdriana Kobylak                         {
1810400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1811400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1812400fd1fbSAdriana Kobylak                                 << maxFileSize;
1813400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1814400fd1fbSAdriana Kobylak                             return;
1815400fd1fbSAdriana Kobylak                         }
1816400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1817400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1818400fd1fbSAdriana Kobylak                         if (rc == -1)
1819400fd1fbSAdriana Kobylak                         {
1820400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1821400fd1fbSAdriana Kobylak                             return;
1822400fd1fbSAdriana Kobylak                         }
1823400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1824400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1825400fd1fbSAdriana Kobylak                         {
1826400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1827400fd1fbSAdriana Kobylak                             return;
1828400fd1fbSAdriana Kobylak                         }
1829400fd1fbSAdriana Kobylak                         close(fd);
1830400fd1fbSAdriana Kobylak 
1831400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
18327e860f15SJohn Edward Broadbent                         std::string output =
18337e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1834400fd1fbSAdriana Kobylak 
1835400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1836400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
18377e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
18387e860f15SJohn Edward Broadbent                                                  "Base64");
1839400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1840400fd1fbSAdriana Kobylak                     },
1841400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1842400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1843400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
18447e860f15SJohn Edward Broadbent             });
18451da66f75SEd Tanous }
18461da66f75SEd Tanous 
1847b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1848b7028ebfSSpencer Ku 
1849b7028ebfSSpencer Ku inline bool
1850b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1851b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1852b7028ebfSSpencer Ku {
1853b7028ebfSSpencer Ku     std::error_code ec;
1854b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1855b7028ebfSSpencer Ku     if (ec)
1856b7028ebfSSpencer Ku     {
1857b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1858b7028ebfSSpencer Ku         return false;
1859b7028ebfSSpencer Ku     }
1860b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1861b7028ebfSSpencer Ku     {
1862b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1863b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1864b7028ebfSSpencer Ku         // path
1865b7028ebfSSpencer Ku         if (boost::starts_with(filename, "log"))
1866b7028ebfSSpencer Ku         {
1867b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1868b7028ebfSSpencer Ku         }
1869b7028ebfSSpencer Ku     }
1870b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1871b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1872b7028ebfSSpencer Ku     // descending order.
1873b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1874b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1875b7028ebfSSpencer Ku 
1876b7028ebfSSpencer Ku     return true;
1877b7028ebfSSpencer Ku }
1878b7028ebfSSpencer Ku 
1879b7028ebfSSpencer Ku inline bool
1880b7028ebfSSpencer Ku     getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1881b7028ebfSSpencer Ku                          uint64_t& skip, uint64_t& top,
1882b7028ebfSSpencer Ku                          std::vector<std::string>& logEntries, size_t& logCount)
1883b7028ebfSSpencer Ku {
1884b7028ebfSSpencer Ku     GzFileReader logFile;
1885b7028ebfSSpencer Ku 
1886b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1887b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1888b7028ebfSSpencer Ku     {
1889b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1890b7028ebfSSpencer Ku         {
1891b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1892b7028ebfSSpencer Ku             return false;
1893b7028ebfSSpencer Ku         }
1894b7028ebfSSpencer Ku     }
1895b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1896b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1897b7028ebfSSpencer Ku     if (!lastMessage.empty())
1898b7028ebfSSpencer Ku     {
1899b7028ebfSSpencer Ku         logCount++;
1900b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1901b7028ebfSSpencer Ku         {
1902b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1903b7028ebfSSpencer Ku         }
1904b7028ebfSSpencer Ku     }
1905b7028ebfSSpencer Ku     return true;
1906b7028ebfSSpencer Ku }
1907b7028ebfSSpencer Ku 
1908b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1909b7028ebfSSpencer Ku                                     const std::string& msg,
1910b7028ebfSSpencer Ku                                     nlohmann::json& logEntryJson)
1911b7028ebfSSpencer Ku {
1912b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
1913b7028ebfSSpencer Ku     logEntryJson = {
1914b7028ebfSSpencer Ku         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1915b7028ebfSSpencer Ku         {"@odata.id",
1916b7028ebfSSpencer Ku          "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1917b7028ebfSSpencer Ku              logEntryID},
1918b7028ebfSSpencer Ku         {"Name", "Host Logger Entry"},
1919b7028ebfSSpencer Ku         {"Id", logEntryID},
1920b7028ebfSSpencer Ku         {"Message", msg},
1921b7028ebfSSpencer Ku         {"EntryType", "Oem"},
1922b7028ebfSSpencer Ku         {"Severity", "OK"},
1923b7028ebfSSpencer Ku         {"OemRecordFormat", "Host Logger Entry"}};
1924b7028ebfSSpencer Ku }
1925b7028ebfSSpencer Ku 
1926b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1927b7028ebfSSpencer Ku {
1928b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1929b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
19300fda0f12SGeorge Liu         .methods(
19310fda0f12SGeorge Liu             boost::beast::http::verb::
19320fda0f12SGeorge Liu                 get)([](const crow::Request&,
1933b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1934b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1935b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger";
1936b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1937b7028ebfSSpencer Ku                 "#LogService.v1_1_0.LogService";
1938b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1939b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1940b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Id"] = "HostLogger";
1941b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Entries"] = {
19420fda0f12SGeorge Liu                 {"@odata.id",
19430fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1944b7028ebfSSpencer Ku         });
1945b7028ebfSSpencer Ku }
1946b7028ebfSSpencer Ku 
1947b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1948b7028ebfSSpencer Ku {
1949b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1950b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1951b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
19520fda0f12SGeorge Liu         .methods(
19530fda0f12SGeorge Liu             boost::beast::http::verb::
19540fda0f12SGeorge Liu                 get)([](const crow::Request& req,
1955b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1956b7028ebfSSpencer Ku             uint64_t skip = 0;
1957b7028ebfSSpencer Ku             uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1958b7028ebfSSpencer Ku                                               // default, allow range 1 to
1959b7028ebfSSpencer Ku                                               // 1000 entries per page.
1960b7028ebfSSpencer Ku             if (!getSkipParam(asyncResp, req, skip))
1961b7028ebfSSpencer Ku             {
1962b7028ebfSSpencer Ku                 return;
1963b7028ebfSSpencer Ku             }
1964b7028ebfSSpencer Ku             if (!getTopParam(asyncResp, req, top))
1965b7028ebfSSpencer Ku             {
1966b7028ebfSSpencer Ku                 return;
1967b7028ebfSSpencer Ku             }
1968b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1969b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1970b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1971b7028ebfSSpencer Ku                 "#LogEntryCollection.LogEntryCollection";
1972b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1973b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] =
1974b7028ebfSSpencer Ku                 "Collection of HostLogger Entries";
19750fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1976b7028ebfSSpencer Ku             logEntryArray = nlohmann::json::array();
1977b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = 0;
1978b7028ebfSSpencer Ku 
1979b7028ebfSSpencer Ku             std::vector<std::filesystem::path> hostLoggerFiles;
1980b7028ebfSSpencer Ku             if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1981b7028ebfSSpencer Ku             {
1982b7028ebfSSpencer Ku                 BMCWEB_LOG_ERROR << "fail to get host log file path";
1983b7028ebfSSpencer Ku                 return;
1984b7028ebfSSpencer Ku             }
1985b7028ebfSSpencer Ku 
1986b7028ebfSSpencer Ku             size_t logCount = 0;
1987b7028ebfSSpencer Ku             // This vector only store the entries we want to expose that
1988b7028ebfSSpencer Ku             // control by skip and top.
1989b7028ebfSSpencer Ku             std::vector<std::string> logEntries;
19900fda0f12SGeorge Liu             if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
19910fda0f12SGeorge Liu                                       logCount))
1992b7028ebfSSpencer Ku             {
1993b7028ebfSSpencer Ku                 messages::internalError(asyncResp->res);
1994b7028ebfSSpencer Ku                 return;
1995b7028ebfSSpencer Ku             }
1996b7028ebfSSpencer Ku             // If vector is empty, that means skip value larger than total
1997b7028ebfSSpencer Ku             // log count
199826f6976fSEd Tanous             if (logEntries.empty())
1999b7028ebfSSpencer Ku             {
2000b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2001b7028ebfSSpencer Ku                 return;
2002b7028ebfSSpencer Ku             }
200326f6976fSEd Tanous             if (!logEntries.empty())
2004b7028ebfSSpencer Ku             {
2005b7028ebfSSpencer Ku                 for (size_t i = 0; i < logEntries.size(); i++)
2006b7028ebfSSpencer Ku                 {
2007b7028ebfSSpencer Ku                     logEntryArray.push_back({});
2008b7028ebfSSpencer Ku                     nlohmann::json& hostLogEntry = logEntryArray.back();
2009b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(std::to_string(skip + i),
2010b7028ebfSSpencer Ku                                             logEntries[i], hostLogEntry);
2011b7028ebfSSpencer Ku                 }
2012b7028ebfSSpencer Ku 
2013b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2014b7028ebfSSpencer Ku                 if (skip + top < logCount)
2015b7028ebfSSpencer Ku                 {
2016b7028ebfSSpencer Ku                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
20170fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
2018b7028ebfSSpencer Ku                         std::to_string(skip + top);
2019b7028ebfSSpencer Ku                 }
2020b7028ebfSSpencer Ku             }
2021b7028ebfSSpencer Ku         });
2022b7028ebfSSpencer Ku }
2023b7028ebfSSpencer Ku 
2024b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2025b7028ebfSSpencer Ku {
2026b7028ebfSSpencer Ku     BMCWEB_ROUTE(
2027b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2028b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2029b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
2030b7028ebfSSpencer Ku             [](const crow::Request&,
2031b7028ebfSSpencer Ku                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2032b7028ebfSSpencer Ku                const std::string& param) {
2033b7028ebfSSpencer Ku                 const std::string& targetID = param;
2034b7028ebfSSpencer Ku 
2035b7028ebfSSpencer Ku                 uint64_t idInt = 0;
2036ca45aa3cSEd Tanous 
2037ca45aa3cSEd Tanous                 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2038ca45aa3cSEd Tanous                 const char* end = targetID.data() + targetID.size();
2039ca45aa3cSEd Tanous 
2040ca45aa3cSEd Tanous                 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
2041b7028ebfSSpencer Ku                 if (ec == std::errc::invalid_argument)
2042b7028ebfSSpencer Ku                 {
2043b7028ebfSSpencer Ku                     messages::resourceMissingAtURI(asyncResp->res, targetID);
2044b7028ebfSSpencer Ku                     return;
2045b7028ebfSSpencer Ku                 }
2046b7028ebfSSpencer Ku                 if (ec == std::errc::result_out_of_range)
2047b7028ebfSSpencer Ku                 {
2048b7028ebfSSpencer Ku                     messages::resourceMissingAtURI(asyncResp->res, targetID);
2049b7028ebfSSpencer Ku                     return;
2050b7028ebfSSpencer Ku                 }
2051b7028ebfSSpencer Ku 
2052b7028ebfSSpencer Ku                 std::vector<std::filesystem::path> hostLoggerFiles;
2053b7028ebfSSpencer Ku                 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2054b7028ebfSSpencer Ku                 {
2055b7028ebfSSpencer Ku                     BMCWEB_LOG_ERROR << "fail to get host log file path";
2056b7028ebfSSpencer Ku                     return;
2057b7028ebfSSpencer Ku                 }
2058b7028ebfSSpencer Ku 
2059b7028ebfSSpencer Ku                 size_t logCount = 0;
2060b7028ebfSSpencer Ku                 uint64_t top = 1;
2061b7028ebfSSpencer Ku                 std::vector<std::string> logEntries;
2062b7028ebfSSpencer Ku                 // We can get specific entry by skip and top. For example, if we
2063b7028ebfSSpencer Ku                 // want to get nth entry, we can set skip = n-1 and top = 1 to
2064b7028ebfSSpencer Ku                 // get that entry
2065b7028ebfSSpencer Ku                 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2066b7028ebfSSpencer Ku                                           logEntries, logCount))
2067b7028ebfSSpencer Ku                 {
2068b7028ebfSSpencer Ku                     messages::internalError(asyncResp->res);
2069b7028ebfSSpencer Ku                     return;
2070b7028ebfSSpencer Ku                 }
2071b7028ebfSSpencer Ku 
2072b7028ebfSSpencer Ku                 if (!logEntries.empty())
2073b7028ebfSSpencer Ku                 {
2074b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(targetID, logEntries[0],
2075b7028ebfSSpencer Ku                                             asyncResp->res.jsonValue);
2076b7028ebfSSpencer Ku                     return;
2077b7028ebfSSpencer Ku                 }
2078b7028ebfSSpencer Ku 
2079b7028ebfSSpencer Ku                 // Requested ID was not found
2080b7028ebfSSpencer Ku                 messages::resourceMissingAtURI(asyncResp->res, targetID);
2081b7028ebfSSpencer Ku             });
2082b7028ebfSSpencer Ku }
2083b7028ebfSSpencer Ku 
20847e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
20851da66f75SEd Tanous {
20867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2087ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
20887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
20897e860f15SJohn Edward Broadbent             [](const crow::Request&,
20907e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
20917e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
20927e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
2093e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
20941da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
2095e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
2096e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
20977e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
20987e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
2099e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
21001da66f75SEd Tanous                     "Collection of LogServices for this Manager";
21017e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
21027e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2103c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
21045cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
21055cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
21067e860f15SJohn Edward Broadbent                     {{"@odata.id",
21077e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
21085cb1dd27SAsmitha Karunanithi #endif
2109c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2110c4bf6374SJason M. Bills                 logServiceArray.push_back(
21117e860f15SJohn Edward Broadbent                     {{"@odata.id",
21127e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2113c4bf6374SJason M. Bills #endif
2114e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2115c4bf6374SJason M. Bills                     logServiceArray.size();
21167e860f15SJohn Edward Broadbent             });
2117e1f26343SJason M. Bills }
2118e1f26343SJason M. Bills 
21197e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2120e1f26343SJason M. Bills {
21217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2122ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
21237e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21247e860f15SJohn Edward Broadbent             [](const crow::Request&,
21257e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21268d1b46d7Szhanghch05 
21277e860f15SJohn Edward Broadbent             {
2128e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
2129e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
21300f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
21310f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
21327e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
21337e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
21347e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
21357e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
2136c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2137e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
21387c8c4058STejas Patil 
21397c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
21407c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
21417c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
21427c8c4058STejas Patil                     redfishDateTimeOffset.first;
21437c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21447c8c4058STejas Patil                     redfishDateTimeOffset.second;
21457c8c4058STejas Patil 
2146cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
2147cd50aa42SJason M. Bills                     {"@odata.id",
2148086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
21497e860f15SJohn Edward Broadbent             });
2150e1f26343SJason M. Bills }
2151e1f26343SJason M. Bills 
2152c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2153e1f26343SJason M. Bills                                       sd_journal* journal,
2154c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
2155e1f26343SJason M. Bills {
2156e1f26343SJason M. Bills     // Get the Log Entry contents
2157e1f26343SJason M. Bills     int ret = 0;
2158e1f26343SJason M. Bills 
2159a8fe54f0SJason M. Bills     std::string message;
2160a8fe54f0SJason M. Bills     std::string_view syslogID;
2161a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2162a8fe54f0SJason M. Bills     if (ret < 0)
2163a8fe54f0SJason M. Bills     {
2164a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2165a8fe54f0SJason M. Bills                          << strerror(-ret);
2166a8fe54f0SJason M. Bills     }
2167a8fe54f0SJason M. Bills     if (!syslogID.empty())
2168a8fe54f0SJason M. Bills     {
2169a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2170a8fe54f0SJason M. Bills     }
2171a8fe54f0SJason M. Bills 
217239e77504SEd Tanous     std::string_view msg;
217316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2174e1f26343SJason M. Bills     if (ret < 0)
2175e1f26343SJason M. Bills     {
2176e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2177e1f26343SJason M. Bills         return 1;
2178e1f26343SJason M. Bills     }
2179a8fe54f0SJason M. Bills     message += std::string(msg);
2180e1f26343SJason M. Bills 
2181e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2182271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
218316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2184e1f26343SJason M. Bills     if (ret < 0)
2185e1f26343SJason M. Bills     {
2186e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2187e1f26343SJason M. Bills     }
2188e1f26343SJason M. Bills 
2189e1f26343SJason M. Bills     // Get the Created time from the timestamp
219016428a1aSJason M. Bills     std::string entryTimeStr;
219116428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2192e1f26343SJason M. Bills     {
219316428a1aSJason M. Bills         return 1;
2194e1f26343SJason M. Bills     }
2195e1f26343SJason M. Bills 
2196e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2197c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2198647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2199c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2200c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2201e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2202c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2203a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2204e1f26343SJason M. Bills         {"EntryType", "Oem"},
2205738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2206738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2207738c1e61SPatrick Williams                                      : "OK"},
2208086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2209e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2210e1f26343SJason M. Bills     return 0;
2211e1f26343SJason M. Bills }
2212e1f26343SJason M. Bills 
22137e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2214e1f26343SJason M. Bills {
22157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2216ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
22170fda0f12SGeorge Liu         .methods(
22180fda0f12SGeorge Liu             boost::beast::http::verb::
22190fda0f12SGeorge Liu                 get)([](const crow::Request& req,
22207e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2221193ad2faSJason M. Bills             static constexpr const long maxEntriesPerPage = 1000;
2222271584abSEd Tanous             uint64_t skip = 0;
2223271584abSEd Tanous             uint64_t top = maxEntriesPerPage; // Show max entries by default
22248d1b46d7Szhanghch05             if (!getSkipParam(asyncResp, req, skip))
2225193ad2faSJason M. Bills             {
2226193ad2faSJason M. Bills                 return;
2227193ad2faSJason M. Bills             }
22288d1b46d7Szhanghch05             if (!getTopParam(asyncResp, req, top))
2229193ad2faSJason M. Bills             {
2230193ad2faSJason M. Bills                 return;
2231193ad2faSJason M. Bills             }
22327e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
22337e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2234e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
2235e1f26343SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
22360f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
22370f74e643SEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2238e1f26343SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2239e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2240e1f26343SJason M. Bills                 "Collection of BMC Journal Entries";
22410fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2242e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2243e1f26343SJason M. Bills 
22447e860f15SJohn Edward Broadbent             // Go through the journal and use the timestamp to create a
22457e860f15SJohn Edward Broadbent             // unique ID for each entry
2246e1f26343SJason M. Bills             sd_journal* journalTmp = nullptr;
2247e1f26343SJason M. Bills             int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2248e1f26343SJason M. Bills             if (ret < 0)
2249e1f26343SJason M. Bills             {
22507e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "failed to open journal: "
22517e860f15SJohn Edward Broadbent                                  << strerror(-ret);
2252f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2253e1f26343SJason M. Bills                 return;
2254e1f26343SJason M. Bills             }
22550fda0f12SGeorge Liu             std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22560fda0f12SGeorge Liu                 journalTmp, sd_journal_close);
2257e1f26343SJason M. Bills             journalTmp = nullptr;
2258b01bf299SEd Tanous             uint64_t entryCount = 0;
2259e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
2260e85d6b16SJason M. Bills             bool firstEntry = true;
2261e1f26343SJason M. Bills             SD_JOURNAL_FOREACH(journal.get())
2262e1f26343SJason M. Bills             {
2263193ad2faSJason M. Bills                 entryCount++;
22647e860f15SJohn Edward Broadbent                 // Handle paging using skip (number of entries to skip from
22657e860f15SJohn Edward Broadbent                 // the start) and top (number of entries to display)
2266193ad2faSJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
2267193ad2faSJason M. Bills                 {
2268193ad2faSJason M. Bills                     continue;
2269193ad2faSJason M. Bills                 }
2270193ad2faSJason M. Bills 
227116428a1aSJason M. Bills                 std::string idStr;
2272e85d6b16SJason M. Bills                 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2273e1f26343SJason M. Bills                 {
2274e1f26343SJason M. Bills                     continue;
2275e1f26343SJason M. Bills                 }
2276e1f26343SJason M. Bills 
2277e85d6b16SJason M. Bills                 if (firstEntry)
2278e85d6b16SJason M. Bills                 {
2279e85d6b16SJason M. Bills                     firstEntry = false;
2280e85d6b16SJason M. Bills                 }
2281e85d6b16SJason M. Bills 
2282e1f26343SJason M. Bills                 logEntryArray.push_back({});
2283c4bf6374SJason M. Bills                 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2284c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2285c4bf6374SJason M. Bills                                                bmcJournalLogEntry) != 0)
2286e1f26343SJason M. Bills                 {
2287f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2288e1f26343SJason M. Bills                     return;
2289e1f26343SJason M. Bills                 }
2290e1f26343SJason M. Bills             }
2291193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2292193ad2faSJason M. Bills             if (skip + top < entryCount)
2293193ad2faSJason M. Bills             {
2294193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
22950fda0f12SGeorge Liu                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2296193ad2faSJason M. Bills                     std::to_string(skip + top);
2297193ad2faSJason M. Bills             }
22987e860f15SJohn Edward Broadbent         });
2299e1f26343SJason M. Bills }
2300e1f26343SJason M. Bills 
23017e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2302e1f26343SJason M. Bills {
23037e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23047e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2305ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
23067e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23077e860f15SJohn Edward Broadbent             [](const crow::Request&,
23087e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23097e860f15SJohn Edward Broadbent                const std::string& entryID) {
2310e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2311e1f26343SJason M. Bills                 uint64_t ts = 0;
2312271584abSEd Tanous                 uint64_t index = 0;
23138d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2314e1f26343SJason M. Bills                 {
231516428a1aSJason M. Bills                     return;
2316e1f26343SJason M. Bills                 }
2317e1f26343SJason M. Bills 
2318e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2319e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2320e1f26343SJason M. Bills                 if (ret < 0)
2321e1f26343SJason M. Bills                 {
23227e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
23237e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2324f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2325e1f26343SJason M. Bills                     return;
2326e1f26343SJason M. Bills                 }
23277e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
23287e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2329e1f26343SJason M. Bills                 journalTmp = nullptr;
23307e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
23317e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2332af07e3f5SJason M. Bills                 std::string idStr;
2333af07e3f5SJason M. Bills                 bool firstEntry = true;
2334e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
23352056b6d1SManojkiran Eda                 if (ret < 0)
23362056b6d1SManojkiran Eda                 {
23372056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
23382056b6d1SManojkiran Eda                                      << strerror(-ret);
23392056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
23402056b6d1SManojkiran Eda                     return;
23412056b6d1SManojkiran Eda                 }
2342271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2343e1f26343SJason M. Bills                 {
2344e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2345af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2346af07e3f5SJason M. Bills                     {
2347af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2348af07e3f5SJason M. Bills                         return;
2349af07e3f5SJason M. Bills                     }
2350af07e3f5SJason M. Bills                     if (firstEntry)
2351af07e3f5SJason M. Bills                     {
2352af07e3f5SJason M. Bills                         firstEntry = false;
2353af07e3f5SJason M. Bills                     }
2354e1f26343SJason M. Bills                 }
2355c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2356af07e3f5SJason M. Bills                 if (idStr != entryID)
2357c4bf6374SJason M. Bills                 {
2358c4bf6374SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, entryID);
2359c4bf6374SJason M. Bills                     return;
2360c4bf6374SJason M. Bills                 }
2361c4bf6374SJason M. Bills 
2362c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2363e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2364e1f26343SJason M. Bills                 {
2365f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2366e1f26343SJason M. Bills                     return;
2367e1f26343SJason M. Bills                 }
23687e860f15SJohn Edward Broadbent             });
2369c9bb6861Sraviteja-b }
2370c9bb6861Sraviteja-b 
23717e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2372c9bb6861Sraviteja-b {
23737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2374ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23750fda0f12SGeorge Liu         .methods(
23760fda0f12SGeorge Liu             boost::beast::http::verb::
23770fda0f12SGeorge Liu                 get)([](const crow::Request&,
23787e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2379c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.id"] =
23805cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Managers/bmc/LogServices/Dump";
2381c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.type"] =
2382d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
2383c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Name"] = "Dump LogService";
23845cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
23855cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
2386c9bb6861Sraviteja-b             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
23877c8c4058STejas Patil 
23887c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
23897c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
23900fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23917c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23927c8c4058STejas Patil                 redfishDateTimeOffset.second;
23937c8c4058STejas Patil 
2394c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Entries"] = {
23957e860f15SJohn Edward Broadbent                 {"@odata.id",
23967e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
23975cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
23985cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
23990fda0f12SGeorge Liu                  {{"target",
24000fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2401d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
24020fda0f12SGeorge Liu                  {{"target",
24030fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
24047e860f15SJohn Edward Broadbent         });
2405c9bb6861Sraviteja-b }
2406c9bb6861Sraviteja-b 
24077e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
24087e860f15SJohn Edward Broadbent {
24097e860f15SJohn Edward Broadbent 
2410c9bb6861Sraviteja-b     /**
2411c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2412c9bb6861Sraviteja-b      */
24137e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2414ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
24157e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24167e860f15SJohn Edward Broadbent             [](const crow::Request&,
24177e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2418c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2419c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2420c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
24215cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
24225cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2423c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
24245cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2425c9bb6861Sraviteja-b 
24265cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
24277e860f15SJohn Edward Broadbent             });
2428c9bb6861Sraviteja-b }
2429c9bb6861Sraviteja-b 
24307e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2431c9bb6861Sraviteja-b {
24327e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24337e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2434ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
24357e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24367e860f15SJohn Edward Broadbent             [](const crow::Request&,
24377e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24387e860f15SJohn Edward Broadbent                const std::string& param) {
24397e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "BMC");
24407e860f15SJohn Edward Broadbent             });
24417e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24427e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2443ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
24447e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
24457e860f15SJohn Edward Broadbent             [](const crow::Request&,
24467e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24477e860f15SJohn Edward Broadbent                const std::string& param) {
24487e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
24497e860f15SJohn Edward Broadbent             });
2450c9bb6861Sraviteja-b }
2451c9bb6861Sraviteja-b 
24527e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2453c9bb6861Sraviteja-b {
24548d1b46d7Szhanghch05 
24550fda0f12SGeorge Liu     BMCWEB_ROUTE(
24560fda0f12SGeorge Liu         app,
24570fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2458ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24597e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24607e860f15SJohn Edward Broadbent             [](const crow::Request& req,
24617e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24628d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
24637e860f15SJohn Edward Broadbent             });
2464a43be80fSAsmitha Karunanithi }
2465a43be80fSAsmitha Karunanithi 
24667e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
246780319af1SAsmitha Karunanithi {
24680fda0f12SGeorge Liu     BMCWEB_ROUTE(
24690fda0f12SGeorge Liu         app,
24700fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2471ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24727e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24737e860f15SJohn Edward Broadbent             [](const crow::Request&,
24747e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24758d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
24767e860f15SJohn Edward Broadbent             });
24775cb1dd27SAsmitha Karunanithi }
24785cb1dd27SAsmitha Karunanithi 
24797e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
24805cb1dd27SAsmitha Karunanithi {
24817e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2482ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
24837e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24847e860f15SJohn Edward Broadbent             [](const crow::Request&,
24857e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
24865cb1dd27SAsmitha Karunanithi 
24877e860f15SJohn Edward Broadbent             {
24885cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
24895cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump";
24905cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
2491d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
24925cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
24937e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
24947e860f15SJohn Edward Broadbent                     "System Dump LogService";
24955cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
24965cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24977c8c4058STejas Patil 
24987c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
24997c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
25007c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
25017c8c4058STejas Patil                     redfishDateTimeOffset.first;
25027c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25037c8c4058STejas Patil                     redfishDateTimeOffset.second;
25047c8c4058STejas Patil 
25055cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Entries"] = {
25065cb1dd27SAsmitha Karunanithi                     {"@odata.id",
25075cb1dd27SAsmitha Karunanithi                      "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
25085cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
25095cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
25107e860f15SJohn Edward Broadbent                      {{"target",
25110fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
2512d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
25137e860f15SJohn Edward Broadbent                      {{"target",
25140fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
25157e860f15SJohn Edward Broadbent             });
25165cb1dd27SAsmitha Karunanithi }
25175cb1dd27SAsmitha Karunanithi 
25187e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
25197e860f15SJohn Edward Broadbent {
25207e860f15SJohn Edward Broadbent 
25215cb1dd27SAsmitha Karunanithi     /**
25225cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25235cb1dd27SAsmitha Karunanithi      */
2524b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2525ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25277e860f15SJohn Edward Broadbent             [](const crow::Request&,
2528864d6a17SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25295cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
25305cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
25315cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
25325cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
25335cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
25345cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
25355cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
25365cb1dd27SAsmitha Karunanithi 
25375cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
25387e860f15SJohn Edward Broadbent             });
25395cb1dd27SAsmitha Karunanithi }
25405cb1dd27SAsmitha Karunanithi 
25417e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
25425cb1dd27SAsmitha Karunanithi {
25437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2544864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2545ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2546ed398213SEd Tanous 
25477e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25487e860f15SJohn Edward Broadbent             [](const crow::Request&,
25497e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25507e860f15SJohn Edward Broadbent                const std::string& param) {
25517e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "System");
25527e860f15SJohn Edward Broadbent             });
25538d1b46d7Szhanghch05 
25547e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2555864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2556ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25577e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
25587e860f15SJohn Edward Broadbent             [](const crow::Request&,
25597e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25607e860f15SJohn Edward Broadbent                const std::string& param) {
25617e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
25627e860f15SJohn Edward Broadbent             });
25635cb1dd27SAsmitha Karunanithi }
2564c9bb6861Sraviteja-b 
25657e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2566c9bb6861Sraviteja-b {
25670fda0f12SGeorge Liu     BMCWEB_ROUTE(
25680fda0f12SGeorge Liu         app,
25690fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2570ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25717e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25727e860f15SJohn Edward Broadbent             [](const crow::Request& req,
25737e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25747e860f15SJohn Edward Broadbent 
25757e860f15SJohn Edward Broadbent             { createDump(asyncResp, req, "System"); });
2576a43be80fSAsmitha Karunanithi }
2577a43be80fSAsmitha Karunanithi 
25787e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2579a43be80fSAsmitha Karunanithi {
25800fda0f12SGeorge Liu     BMCWEB_ROUTE(
25810fda0f12SGeorge Liu         app,
25820fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2583ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25847e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25857e860f15SJohn Edward Broadbent             [](const crow::Request&,
25867e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25877e860f15SJohn Edward Broadbent 
25887e860f15SJohn Edward Broadbent             { clearDump(asyncResp, "System"); });
2589013487e5Sraviteja-b }
2590013487e5Sraviteja-b 
25917e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
25921da66f75SEd Tanous {
25933946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
25943946028dSAppaRao Puli     // method for security reasons.
25951da66f75SEd Tanous     /**
25961da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
25971da66f75SEd Tanous      */
25987e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2599ed398213SEd Tanous         // This is incorrect, should be:
2600ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2601432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
26027e860f15SJohn Edward Broadbent         .methods(
26037e860f15SJohn Edward Broadbent             boost::beast::http::verb::
26047e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
26057e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26067e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
26077e860f15SJohn Edward Broadbent             // SubRoute
26080f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2609424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2610e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
26118e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
26124f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26134f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26144f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2615e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2616e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
26177c8c4058STejas Patil 
26187c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
26197c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
26207c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26217c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26227c8c4058STejas Patil                 redfishDateTimeOffset.second;
26237c8c4058STejas Patil 
2624cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2625cd50aa42SJason M. Bills                 {"@odata.id",
2626424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2627e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
26285b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
26290fda0f12SGeorge Liu                  {{"target",
26300fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
26318e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
26320fda0f12SGeorge Liu                  {{"target",
26330fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
26347e860f15SJohn Edward Broadbent         });
26351da66f75SEd Tanous }
26361da66f75SEd Tanous 
26377e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
26385b61b5e8SJason M. Bills {
26390fda0f12SGeorge Liu     BMCWEB_ROUTE(
26400fda0f12SGeorge Liu         app,
26410fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2642ed398213SEd Tanous         // This is incorrect, should be:
2643ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2644432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
26467e860f15SJohn Edward Broadbent             [](const crow::Request&,
26477e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26485b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
26495b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2650cb13a392SEd Tanous                                 const std::string&) {
26515b61b5e8SJason M. Bills                         if (ec)
26525b61b5e8SJason M. Bills                         {
26535b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
26545b61b5e8SJason M. Bills                             return;
26555b61b5e8SJason M. Bills                         }
26565b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
26575b61b5e8SJason M. Bills                     },
26587e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
26597e860f15SJohn Edward Broadbent                     "DeleteAll");
26607e860f15SJohn Edward Broadbent             });
26615b61b5e8SJason M. Bills }
26625b61b5e8SJason M. Bills 
26638d1b46d7Szhanghch05 static void
26648d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26658d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2666e855dd28SJason M. Bills {
2667043a0536SJohnathan Mantey     auto getStoredLogCallback =
2668043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2669e855dd28SJason M. Bills             const boost::system::error_code ec,
2670168e20c1SEd Tanous             const std::vector<
2671168e20c1SEd Tanous                 std::pair<std::string, dbus::utility::DbusVariantType>>&
2672168e20c1SEd Tanous                 params) {
2673e855dd28SJason M. Bills             if (ec)
2674e855dd28SJason M. Bills             {
2675e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
26761ddcf01aSJason M. Bills                 if (ec.value() ==
26771ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
26781ddcf01aSJason M. Bills                 {
2679043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2680043a0536SJohnathan Mantey                                                logID);
26811ddcf01aSJason M. Bills                 }
26821ddcf01aSJason M. Bills                 else
26831ddcf01aSJason M. Bills                 {
2684e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
26851ddcf01aSJason M. Bills                 }
2686e855dd28SJason M. Bills                 return;
2687e855dd28SJason M. Bills             }
2688043a0536SJohnathan Mantey 
2689043a0536SJohnathan Mantey             std::string timestamp{};
2690043a0536SJohnathan Mantey             std::string filename{};
2691043a0536SJohnathan Mantey             std::string logfile{};
26922c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2693043a0536SJohnathan Mantey 
2694043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2695e855dd28SJason M. Bills             {
2696043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2697e855dd28SJason M. Bills                 return;
2698e855dd28SJason M. Bills             }
2699e855dd28SJason M. Bills 
2700043a0536SJohnathan Mantey             std::string crashdumpURI =
2701e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2702043a0536SJohnathan Mantey                 logID + "/" + filename;
2703d0dbeefdSEd Tanous             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2704043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2705043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2706e855dd28SJason M. Bills                                               logID},
2707e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2708e855dd28SJason M. Bills                             {"Id", logID},
2709e855dd28SJason M. Bills                             {"EntryType", "Oem"},
27108e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
27118e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
27128e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2713043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2714e855dd28SJason M. Bills         };
2715e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
27165b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
27175b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2718043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2719e855dd28SJason M. Bills }
2720e855dd28SJason M. Bills 
27217e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
27221da66f75SEd Tanous {
27233946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27243946028dSAppaRao Puli     // method for security reasons.
27251da66f75SEd Tanous     /**
27261da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
27271da66f75SEd Tanous      */
27287e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
27297e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2730ed398213SEd Tanous         // This is incorrect, should be.
2731ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2732432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27337e860f15SJohn Edward Broadbent         .methods(
27347e860f15SJohn Edward Broadbent             boost::beast::http::verb::
27357e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
27367e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27377e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
27387e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2739e1f26343SJason M. Bills             auto getLogEntriesCallback = [asyncResp](
2740e1f26343SJason M. Bills                                              const boost::system::error_code ec,
27417e860f15SJohn Edward Broadbent                                              const std::vector<std::string>&
27427e860f15SJohn Edward Broadbent                                                  resp) {
27431da66f75SEd Tanous                 if (ec)
27441da66f75SEd Tanous                 {
27451da66f75SEd Tanous                     if (ec.value() !=
27461da66f75SEd Tanous                         boost::system::errc::no_such_file_or_directory)
27471da66f75SEd Tanous                     {
27481da66f75SEd Tanous                         BMCWEB_LOG_DEBUG << "failed to get entries ec: "
27491da66f75SEd Tanous                                          << ec.message();
2750f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
27511da66f75SEd Tanous                         return;
27521da66f75SEd Tanous                     }
27531da66f75SEd Tanous                 }
2754e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
27551da66f75SEd Tanous                     "#LogEntryCollection.LogEntryCollection";
27560f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
2757424c4176SJason M. Bills                     "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2758424c4176SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2759e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
2760424c4176SJason M. Bills                     "Collection of Crashdump Entries";
27617e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
27627e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2763e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
2764e855dd28SJason M. Bills                 std::vector<std::string> logIDs;
2765e855dd28SJason M. Bills                 // Get the list of log entries and build up an empty array big
2766e855dd28SJason M. Bills                 // enough to hold them
27671da66f75SEd Tanous                 for (const std::string& objpath : resp)
27681da66f75SEd Tanous                 {
2769e855dd28SJason M. Bills                     // Get the log ID
2770f23b7296SEd Tanous                     std::size_t lastPos = objpath.rfind('/');
2771e855dd28SJason M. Bills                     if (lastPos == std::string::npos)
27721da66f75SEd Tanous                     {
2773e855dd28SJason M. Bills                         continue;
27741da66f75SEd Tanous                     }
2775e855dd28SJason M. Bills                     logIDs.emplace_back(objpath.substr(lastPos + 1));
2776e855dd28SJason M. Bills 
2777e855dd28SJason M. Bills                     // Add a space for the log entry to the array
2778e855dd28SJason M. Bills                     logEntryArray.push_back({});
2779e855dd28SJason M. Bills                 }
2780e855dd28SJason M. Bills                 // Now go through and set up async calls to fill in the entries
2781e855dd28SJason M. Bills                 size_t index = 0;
2782e855dd28SJason M. Bills                 for (const std::string& logID : logIDs)
2783e855dd28SJason M. Bills                 {
2784e855dd28SJason M. Bills                     // Add the log entry to the array
2785e855dd28SJason M. Bills                     logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
27861da66f75SEd Tanous                 }
2787e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2788e1f26343SJason M. Bills                     logEntryArray.size();
27891da66f75SEd Tanous             };
27901da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
27911da66f75SEd Tanous                 std::move(getLogEntriesCallback),
27921da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
27931da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
27941da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
27955b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
27967e860f15SJohn Edward Broadbent         });
27971da66f75SEd Tanous }
27981da66f75SEd Tanous 
27997e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28001da66f75SEd Tanous {
28013946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28023946028dSAppaRao Puli     // method for security reasons.
28031da66f75SEd Tanous 
28047e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28057e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2806ed398213SEd Tanous         // this is incorrect, should be
2807ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2808432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28097e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
28107e860f15SJohn Edward Broadbent             [](const crow::Request&,
28117e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28127e860f15SJohn Edward Broadbent                const std::string& param) {
28137e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2814e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
28157e860f15SJohn Edward Broadbent             });
2816e855dd28SJason M. Bills }
2817e855dd28SJason M. Bills 
28187e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2819e855dd28SJason M. Bills {
28203946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28213946028dSAppaRao Puli     // method for security reasons.
28227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28237e860f15SJohn Edward Broadbent         app,
28247e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2825ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
28267e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
28277e860f15SJohn Edward Broadbent             [](const crow::Request&,
28287e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28297e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
2830043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2831043a0536SJohnathan Mantey                     [asyncResp, logID, fileName](
2832abf2add6SEd Tanous                         const boost::system::error_code ec,
2833168e20c1SEd Tanous                         const std::vector<std::pair<
2834168e20c1SEd Tanous                             std::string, dbus::utility::DbusVariantType>>&
28357e860f15SJohn Edward Broadbent                             resp) {
28361da66f75SEd Tanous                         if (ec)
28371da66f75SEd Tanous                         {
2838043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2839043a0536SJohnathan Mantey                                              << ec.message();
2840f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
28411da66f75SEd Tanous                             return;
28421da66f75SEd Tanous                         }
2843e855dd28SJason M. Bills 
2844043a0536SJohnathan Mantey                         std::string dbusFilename{};
2845043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2846043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2847043a0536SJohnathan Mantey 
28487e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
28497e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2850043a0536SJohnathan Mantey 
2851043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2852043a0536SJohnathan Mantey                             dbusFilepath.empty())
28531da66f75SEd Tanous                         {
28547e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28557e860f15SJohn Edward Broadbent                                                            fileName);
28561da66f75SEd Tanous                             return;
28571da66f75SEd Tanous                         }
2858e855dd28SJason M. Bills 
2859043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2860043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2861043a0536SJohnathan Mantey                         {
28627e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28637e860f15SJohn Edward Broadbent                                                            fileName);
2864043a0536SJohnathan Mantey                             return;
2865043a0536SJohnathan Mantey                         }
2866043a0536SJohnathan Mantey 
2867043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2868043a0536SJohnathan Mantey                         {
28697e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28707e860f15SJohn Edward Broadbent                                                            fileName);
2871043a0536SJohnathan Mantey                             return;
2872043a0536SJohnathan Mantey                         }
28732d31491bSJason M. Bills                         std::ifstream ifs(dbusFilepath,
28742d31491bSJason M. Bills                                           std::ios::in | std::ios::binary);
28752d31491bSJason M. Bills                         asyncResp->res.body() = std::string(
28762d31491bSJason M. Bills                             std::istreambuf_iterator<char>{ifs}, {});
2877043a0536SJohnathan Mantey 
28787e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
28797e860f15SJohn Edward Broadbent                         // from a browser
28807e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
28817e860f15SJohn Edward Broadbent                                                  "attachment");
28821da66f75SEd Tanous                     };
28831da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
28845b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
28855b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
28867e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
28877e860f15SJohn Edward Broadbent                     crashdumpInterface);
28887e860f15SJohn Edward Broadbent             });
28891da66f75SEd Tanous }
28901da66f75SEd Tanous 
28917e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
28921da66f75SEd Tanous {
28933946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28943946028dSAppaRao Puli     // method for security reasons.
28950fda0f12SGeorge Liu     BMCWEB_ROUTE(
28960fda0f12SGeorge Liu         app,
28970fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2898ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2899ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2900432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
29017e860f15SJohn Edward Broadbent         .methods(
29027e860f15SJohn Edward Broadbent             boost::beast::http::verb::
29037e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
29047e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
29058e6c099aSJason M. Bills             std::string diagnosticDataType;
29068e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
29078e6c099aSJason M. Bills             if (!redfish::json_util::readJson(
29087e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
29097e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
29107e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
29118e6c099aSJason M. Bills             {
29128e6c099aSJason M. Bills                 return;
29138e6c099aSJason M. Bills             }
29148e6c099aSJason M. Bills 
29158e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
29168e6c099aSJason M. Bills             {
29178e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
29188e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
29198e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
29208e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29218e6c099aSJason M. Bills                     "CollectDiagnosticData");
29228e6c099aSJason M. Bills                 return;
29238e6c099aSJason M. Bills             }
29248e6c099aSJason M. Bills 
292598be3e39SEd Tanous             auto collectCrashdumpCallback = [asyncResp,
292698be3e39SEd Tanous                                              payload(task::Payload(req))](
29277e860f15SJohn Edward Broadbent                                                 const boost::system::error_code
29287e860f15SJohn Edward Broadbent                                                     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                     {
29404363d3b2SJason M. Bills                         messages::serviceTemporarilyUnavailable(asyncResp->res,
29414363d3b2SJason M. Bills                                                                 "60");
29424363d3b2SJason M. Bills                     }
29431da66f75SEd Tanous                     else
29441da66f75SEd Tanous                     {
2945f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
29461da66f75SEd Tanous                     }
29471da66f75SEd Tanous                     return;
29481da66f75SEd Tanous                 }
29490fda0f12SGeorge Liu                 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
29507e860f15SJohn Edward Broadbent                     [](boost::system::error_code err,
29517e860f15SJohn Edward Broadbent                        sdbusplus::message::message&,
295266afe4faSJames Feist                        const std::shared_ptr<task::TaskData>& taskData) {
295366afe4faSJames Feist                         if (!err)
295466afe4faSJames Feist                         {
2955e5d5006bSJames Feist                             taskData->messages.emplace_back(
2956e5d5006bSJames Feist                                 messages::taskCompletedOK(
2957e5d5006bSJames Feist                                     std::to_string(taskData->index)));
2958831d6b09SJames Feist                             taskData->state = "Completed";
295966afe4faSJames Feist                         }
296032898ceaSJames Feist                         return task::completed;
296166afe4faSJames Feist                     },
29627e860f15SJohn Edward Broadbent                     "type='signal',interface='org.freedesktop.DBus."
29637e860f15SJohn Edward Broadbent                     "Properties',"
29640fda0f12SGeorge Liu                     "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
296546229577SJames Feist                 task->startTimer(std::chrono::minutes(5));
296646229577SJames Feist                 task->populateResp(asyncResp->res);
296798be3e39SEd Tanous                 task->payload.emplace(std::move(payload));
29681da66f75SEd Tanous             };
29698e6c099aSJason M. Bills 
29708e6c099aSJason M. Bills             if (oemDiagnosticDataType == "OnDemand")
29718e6c099aSJason M. Bills             {
29721da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
29738e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
29748e6c099aSJason M. Bills                     crashdumpPath, crashdumpOnDemandInterface,
29758e6c099aSJason M. Bills                     "GenerateOnDemandLog");
29761da66f75SEd Tanous             }
29778e6c099aSJason M. Bills             else if (oemDiagnosticDataType == "Telemetry")
29786eda7685SKenny L. Ku             {
29798e6c099aSJason M. Bills                 crow::connections::systemBus->async_method_call(
29808e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
29818e6c099aSJason M. Bills                     crashdumpPath, crashdumpTelemetryInterface,
29828e6c099aSJason M. Bills                     "GenerateTelemetryLog");
29836eda7685SKenny L. Ku             }
29846eda7685SKenny L. Ku             else
29856eda7685SKenny L. Ku             {
29868e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
29878e6c099aSJason M. Bills                                  << oemDiagnosticDataType;
29888e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
29897e860f15SJohn Edward Broadbent                     asyncResp->res, oemDiagnosticDataType,
29907e860f15SJohn Edward Broadbent                     "OEMDiagnosticDataType", "CollectDiagnosticData");
29916eda7685SKenny L. Ku                 return;
29926eda7685SKenny L. Ku             }
29937e860f15SJohn Edward Broadbent         });
29946eda7685SKenny L. Ku }
29956eda7685SKenny L. Ku 
2996cb92c03bSAndrew Geissler /**
2997cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
2998cb92c03bSAndrew Geissler  */
29997e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3000cb92c03bSAndrew Geissler {
3001cb92c03bSAndrew Geissler     /**
3002cb92c03bSAndrew Geissler      * Function handles POST method request.
3003cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3004cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3005cb92c03bSAndrew Geissler      */
30067e860f15SJohn Edward Broadbent 
30070fda0f12SGeorge Liu     BMCWEB_ROUTE(
30080fda0f12SGeorge Liu         app,
30090fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3010ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
30117e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
30127e860f15SJohn Edward Broadbent             [](const crow::Request&,
30137e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3014cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
3015cb92c03bSAndrew Geissler 
3016cb92c03bSAndrew Geissler                 // Process response from Logging service.
30177e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
30187e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
30197e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
30207e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
3021cb92c03bSAndrew Geissler                     if (ec)
3022cb92c03bSAndrew Geissler                     {
3023cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
30247e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
30257e860f15SJohn Edward Broadbent                                          << ec;
3026cb92c03bSAndrew Geissler                         asyncResp->res.result(
3027cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
3028cb92c03bSAndrew Geissler                         return;
3029cb92c03bSAndrew Geissler                     }
3030cb92c03bSAndrew Geissler 
30317e860f15SJohn Edward Broadbent                     asyncResp->res.result(
30327e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
3033cb92c03bSAndrew Geissler                 };
3034cb92c03bSAndrew Geissler 
3035cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
3036cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
30372c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
3038cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
3039cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
30407e860f15SJohn Edward Broadbent             });
3041cb92c03bSAndrew Geissler }
3042a3316fc6SZhikuiRen 
3043a3316fc6SZhikuiRen /****************************************************
3044a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3045a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3046a3316fc6SZhikuiRen  ******************************************************/
30477e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3048a3316fc6SZhikuiRen {
30497e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3050ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
30510fda0f12SGeorge Liu         .methods(
30520fda0f12SGeorge Liu             boost::beast::http::verb::
30530fda0f12SGeorge Liu                 get)([](const crow::Request&,
30547e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3055a3316fc6SZhikuiRen             asyncResp->res.jsonValue = {
30567e860f15SJohn Edward Broadbent                 {"@odata.id",
30577e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes"},
3058a3316fc6SZhikuiRen                 {"@odata.type", "#LogService.v1_1_0.LogService"},
3059a3316fc6SZhikuiRen                 {"Name", "POST Code Log Service"},
3060a3316fc6SZhikuiRen                 {"Description", "POST Code Log Service"},
3061a3316fc6SZhikuiRen                 {"Id", "BIOS POST Code Log"},
3062a3316fc6SZhikuiRen                 {"OverWritePolicy", "WrapsWhenFull"},
3063a3316fc6SZhikuiRen                 {"Entries",
30640fda0f12SGeorge Liu                  {{"@odata.id",
30650fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
30667c8c4058STejas Patil 
30677c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
30687c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
30690fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30707c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30717c8c4058STejas Patil                 redfishDateTimeOffset.second;
30727c8c4058STejas Patil 
3073a3316fc6SZhikuiRen             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
30747e860f15SJohn Edward Broadbent                 {"target",
30750fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
30767e860f15SJohn Edward Broadbent         });
3077a3316fc6SZhikuiRen }
3078a3316fc6SZhikuiRen 
30797e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3080a3316fc6SZhikuiRen {
30810fda0f12SGeorge Liu     BMCWEB_ROUTE(
30820fda0f12SGeorge Liu         app,
30830fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3084ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3085ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3086432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
30877e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
30887e860f15SJohn Edward Broadbent             [](const crow::Request&,
30897e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3090a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3091a3316fc6SZhikuiRen 
3092a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
3093a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
3094a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
3095a3316fc6SZhikuiRen                         if (ec)
3096a3316fc6SZhikuiRen                         {
3097a3316fc6SZhikuiRen                             // TODO Handle for specific error code
3098a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
30997e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
31007e860f15SJohn Edward Broadbent                                 << ec;
31017e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
31027e860f15SJohn Edward Broadbent                                                       internal_server_error);
3103a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
3104a3316fc6SZhikuiRen                             return;
3105a3316fc6SZhikuiRen                         }
3106a3316fc6SZhikuiRen                     },
310715124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
310815124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
3109a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31107e860f15SJohn Edward Broadbent             });
3111a3316fc6SZhikuiRen }
3112a3316fc6SZhikuiRen 
3113a3316fc6SZhikuiRen static void fillPostCodeEntry(
31148d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
31156c9a279eSManojkiran Eda     const boost::container::flat_map<
31166c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3117a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3118a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3119a3316fc6SZhikuiRen {
3120a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3121a3316fc6SZhikuiRen     const message_registries::Message* message =
31224a0bf539SManojkiran Eda         message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3123a3316fc6SZhikuiRen 
3124a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3125a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3126a3316fc6SZhikuiRen 
3127a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
31286c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
31296c9a279eSManojkiran Eda              code : postcode)
3130a3316fc6SZhikuiRen     {
3131a3316fc6SZhikuiRen         currentCodeIndex++;
3132a3316fc6SZhikuiRen         std::string postcodeEntryID =
3133a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3134a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3135a3316fc6SZhikuiRen 
3136a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3137a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3138a3316fc6SZhikuiRen 
3139a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3140a3316fc6SZhikuiRen         { // already incremented
3141a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3142a3316fc6SZhikuiRen         }
3143a3316fc6SZhikuiRen         else
3144a3316fc6SZhikuiRen         {
3145a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3146a3316fc6SZhikuiRen         }
3147a3316fc6SZhikuiRen 
3148a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3149a3316fc6SZhikuiRen         // not fall between top and skip
3150a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3151a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3152a3316fc6SZhikuiRen         {
3153a3316fc6SZhikuiRen             continue;
3154a3316fc6SZhikuiRen         }
3155a3316fc6SZhikuiRen 
31564e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3157a3316fc6SZhikuiRen         // currentIndex
3158a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3159a3316fc6SZhikuiRen         {
3160a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3161a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3162a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3163a3316fc6SZhikuiRen             continue;
3164a3316fc6SZhikuiRen         }
3165a3316fc6SZhikuiRen 
3166a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3167a3316fc6SZhikuiRen         // index
3168a3316fc6SZhikuiRen 
3169a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3170a3316fc6SZhikuiRen         std::string entryTimeStr;
31711d8782e7SNan Zhou         entryTimeStr =
31721d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3173a3316fc6SZhikuiRen 
3174a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3175a3316fc6SZhikuiRen         std::ostringstream hexCode;
3176a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
31776c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3178a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3179a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3180a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3181a3316fc6SZhikuiRen         // Set precision to 4 digits
3182a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3183a3316fc6SZhikuiRen         // Add double to stream
3184a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3185a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3186a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3187a3316fc6SZhikuiRen 
3188a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3189a3316fc6SZhikuiRen         std::string msg;
3190a3316fc6SZhikuiRen         if (message != nullptr)
3191a3316fc6SZhikuiRen         {
3192a3316fc6SZhikuiRen             msg = message->message;
3193a3316fc6SZhikuiRen 
3194a3316fc6SZhikuiRen             // fill in this post code value
3195a3316fc6SZhikuiRen             int i = 0;
3196a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3197a3316fc6SZhikuiRen             {
3198a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3199a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3200a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3201a3316fc6SZhikuiRen                 {
3202a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3203a3316fc6SZhikuiRen                 }
3204a3316fc6SZhikuiRen             }
3205a3316fc6SZhikuiRen         }
3206a3316fc6SZhikuiRen 
3207d4342a92STim Lee         // Get Severity template from message registry
3208d4342a92STim Lee         std::string severity;
3209d4342a92STim Lee         if (message != nullptr)
3210d4342a92STim Lee         {
3211d4342a92STim Lee             severity = message->severity;
3212d4342a92STim Lee         }
3213d4342a92STim Lee 
3214a3316fc6SZhikuiRen         // add to AsyncResp
3215a3316fc6SZhikuiRen         logEntryArray.push_back({});
3216a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
32170fda0f12SGeorge Liu         bmcLogEntry = {
32180fda0f12SGeorge Liu             {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
32190fda0f12SGeorge Liu             {"@odata.id",
32200fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3221a3316fc6SZhikuiRen                  postcodeEntryID},
3222a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
3223a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
3224a3316fc6SZhikuiRen             {"Message", std::move(msg)},
32254a0bf539SManojkiran Eda             {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3226a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
3227a3316fc6SZhikuiRen             {"EntryType", "Event"},
3228a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
32299c620e21SAsmitha Karunanithi             {"Created", entryTimeStr}};
3230647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3231647b3cdcSGeorge Liu         {
3232647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3233647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3234647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3235647b3cdcSGeorge Liu         }
3236a3316fc6SZhikuiRen     }
3237a3316fc6SZhikuiRen }
3238a3316fc6SZhikuiRen 
32398d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3240a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3241a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3242a3316fc6SZhikuiRen {
3243a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
32446c9a279eSManojkiran Eda         [aResp, bootIndex,
32456c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
32466c9a279eSManojkiran Eda                     const boost::container::flat_map<
32476c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32486c9a279eSManojkiran Eda                         postcode) {
3249a3316fc6SZhikuiRen             if (ec)
3250a3316fc6SZhikuiRen             {
3251a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3252a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3253a3316fc6SZhikuiRen                 return;
3254a3316fc6SZhikuiRen             }
3255a3316fc6SZhikuiRen 
3256a3316fc6SZhikuiRen             // skip the empty postcode boots
3257a3316fc6SZhikuiRen             if (postcode.empty())
3258a3316fc6SZhikuiRen             {
3259a3316fc6SZhikuiRen                 return;
3260a3316fc6SZhikuiRen             }
3261a3316fc6SZhikuiRen 
3262a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3263a3316fc6SZhikuiRen 
3264a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3265a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3266a3316fc6SZhikuiRen         },
326715124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
326815124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3269a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3270a3316fc6SZhikuiRen         bootIndex);
3271a3316fc6SZhikuiRen }
3272a3316fc6SZhikuiRen 
32738d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3274a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3275a3316fc6SZhikuiRen                                const uint16_t bootCount,
3276a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3277a3316fc6SZhikuiRen                                const uint64_t top)
3278a3316fc6SZhikuiRen {
3279a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3280a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3281a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
32826c9a279eSManojkiran Eda               const boost::container::flat_map<
32836c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32846c9a279eSManojkiran Eda                   postcode) {
3285a3316fc6SZhikuiRen             if (ec)
3286a3316fc6SZhikuiRen             {
3287a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3288a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3289a3316fc6SZhikuiRen                 return;
3290a3316fc6SZhikuiRen             }
3291a3316fc6SZhikuiRen 
3292a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3293a3316fc6SZhikuiRen             if (!postcode.empty())
3294a3316fc6SZhikuiRen             {
3295a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3296a3316fc6SZhikuiRen 
3297a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3298a3316fc6SZhikuiRen                 {
3299a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3300a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3301a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3302a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3303a3316fc6SZhikuiRen 
3304a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3305a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3306a3316fc6SZhikuiRen                 }
3307a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3308a3316fc6SZhikuiRen             }
3309a3316fc6SZhikuiRen 
3310a3316fc6SZhikuiRen             // continue to previous bootIndex
3311a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3312a3316fc6SZhikuiRen             {
3313a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3314a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3315a3316fc6SZhikuiRen             }
3316a3316fc6SZhikuiRen             else
3317a3316fc6SZhikuiRen             {
3318a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
33190fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3320a3316fc6SZhikuiRen                     std::to_string(skip + top);
3321a3316fc6SZhikuiRen             }
3322a3316fc6SZhikuiRen         },
332315124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
332415124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3325a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3326a3316fc6SZhikuiRen         bootIndex);
3327a3316fc6SZhikuiRen }
3328a3316fc6SZhikuiRen 
33298d1b46d7Szhanghch05 static void
33308d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3331a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3332a3316fc6SZhikuiRen {
3333a3316fc6SZhikuiRen     uint64_t entryCount = 0;
33341e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
33351e1e598dSJonathan Doman         *crow::connections::systemBus,
33361e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
33371e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
33381e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
33391e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
33401e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3341a3316fc6SZhikuiRen             if (ec)
3342a3316fc6SZhikuiRen             {
3343a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3344a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3345a3316fc6SZhikuiRen                 return;
3346a3316fc6SZhikuiRen             }
33471e1e598dSJonathan Doman             getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
33481e1e598dSJonathan Doman         });
3349a3316fc6SZhikuiRen }
3350a3316fc6SZhikuiRen 
33517e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3352a3316fc6SZhikuiRen {
33537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
33547e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3355ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
33567e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
33577e860f15SJohn Edward Broadbent             [](const crow::Request& req,
33587e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3359a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3360a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3361a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3362a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3363a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3364a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3365a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3366a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3367a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3368a3316fc6SZhikuiRen 
3369a3316fc6SZhikuiRen                 uint64_t skip = 0;
3370a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
33718d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3372a3316fc6SZhikuiRen                 {
3373a3316fc6SZhikuiRen                     return;
3374a3316fc6SZhikuiRen                 }
33758d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3376a3316fc6SZhikuiRen                 {
3377a3316fc6SZhikuiRen                     return;
3378a3316fc6SZhikuiRen                 }
3379a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
33807e860f15SJohn Edward Broadbent             });
3381a3316fc6SZhikuiRen }
3382a3316fc6SZhikuiRen 
3383647b3cdcSGeorge Liu /**
3384647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3385647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3386647b3cdcSGeorge Liu  *
3387647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3388647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3389647b3cdcSGeorge Liu  * @param[out] index          Index value
3390647b3cdcSGeorge Liu  *
3391647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3392647b3cdcSGeorge Liu  */
3393647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3394647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3395647b3cdcSGeorge Liu {
3396647b3cdcSGeorge Liu     std::vector<std::string> split;
3397647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3398647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3399647b3cdcSGeorge Liu     {
3400647b3cdcSGeorge Liu         return false;
3401647b3cdcSGeorge Liu     }
3402647b3cdcSGeorge Liu 
3403ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3404647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3405ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3406647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3407647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3408647b3cdcSGeorge Liu 
3409647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3410647b3cdcSGeorge Liu     {
3411647b3cdcSGeorge Liu         return false;
3412647b3cdcSGeorge Liu     }
3413647b3cdcSGeorge Liu 
3414647b3cdcSGeorge Liu     start = split[1].data();
3415ca45aa3cSEd Tanous 
3416ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3417647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3418647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3419647b3cdcSGeorge Liu     if (ptrValue != end || ecValue != std::errc())
3420647b3cdcSGeorge Liu     {
3421647b3cdcSGeorge Liu         return false;
3422647b3cdcSGeorge Liu     }
3423647b3cdcSGeorge Liu 
3424647b3cdcSGeorge Liu     return true;
3425647b3cdcSGeorge Liu }
3426647b3cdcSGeorge Liu 
3427647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3428647b3cdcSGeorge Liu {
34290fda0f12SGeorge Liu     BMCWEB_ROUTE(
34300fda0f12SGeorge Liu         app,
34310fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3432647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3433647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
3434647b3cdcSGeorge Liu             [](const crow::Request& req,
3435647b3cdcSGeorge Liu                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3436647b3cdcSGeorge Liu                const std::string& postCodeID) {
3437647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
3438647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
3439647b3cdcSGeorge Liu                 {
3440647b3cdcSGeorge Liu                     asyncResp->res.result(
3441647b3cdcSGeorge Liu                         boost::beast::http::status::bad_request);
3442647b3cdcSGeorge Liu                     return;
3443647b3cdcSGeorge Liu                 }
3444647b3cdcSGeorge Liu 
3445647b3cdcSGeorge Liu                 uint64_t currentValue = 0;
3446647b3cdcSGeorge Liu                 uint16_t index = 0;
3447647b3cdcSGeorge Liu                 if (!parsePostCode(postCodeID, currentValue, index))
3448647b3cdcSGeorge Liu                 {
3449647b3cdcSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
3450647b3cdcSGeorge Liu                                                postCodeID);
3451647b3cdcSGeorge Liu                     return;
3452647b3cdcSGeorge Liu                 }
3453647b3cdcSGeorge Liu 
3454647b3cdcSGeorge Liu                 crow::connections::systemBus->async_method_call(
3455647b3cdcSGeorge Liu                     [asyncResp, postCodeID, currentValue](
3456647b3cdcSGeorge Liu                         const boost::system::error_code ec,
3457647b3cdcSGeorge Liu                         const std::vector<std::tuple<
3458647b3cdcSGeorge Liu                             uint64_t, std::vector<uint8_t>>>& postcodes) {
3459647b3cdcSGeorge Liu                         if (ec.value() == EBADR)
3460647b3cdcSGeorge Liu                         {
3461647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3462647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3463647b3cdcSGeorge Liu                             return;
3464647b3cdcSGeorge Liu                         }
3465647b3cdcSGeorge Liu                         if (ec)
3466647b3cdcSGeorge Liu                         {
3467647b3cdcSGeorge Liu                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3468647b3cdcSGeorge Liu                             messages::internalError(asyncResp->res);
3469647b3cdcSGeorge Liu                             return;
3470647b3cdcSGeorge Liu                         }
3471647b3cdcSGeorge Liu 
3472647b3cdcSGeorge Liu                         size_t value = static_cast<size_t>(currentValue) - 1;
3473647b3cdcSGeorge Liu                         if (value == std::string::npos ||
3474647b3cdcSGeorge Liu                             postcodes.size() < currentValue)
3475647b3cdcSGeorge Liu                         {
3476647b3cdcSGeorge Liu                             BMCWEB_LOG_ERROR << "Wrong currentValue value";
3477647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3478647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3479647b3cdcSGeorge Liu                             return;
3480647b3cdcSGeorge Liu                         }
3481647b3cdcSGeorge Liu 
348246ff87baSEd Tanous                         auto& [tID, c] = postcodes[value];
348346ff87baSEd Tanous                         if (c.empty())
3484647b3cdcSGeorge Liu                         {
3485647b3cdcSGeorge Liu                             BMCWEB_LOG_INFO << "No found post code data";
3486647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3487647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3488647b3cdcSGeorge Liu                             return;
3489647b3cdcSGeorge Liu                         }
349046ff87baSEd Tanous                         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
349146ff87baSEd Tanous                         const char* d = reinterpret_cast<const char*>(c.data());
349246ff87baSEd Tanous                         std::string_view strData(d, c.size());
3493647b3cdcSGeorge Liu 
3494647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Type",
3495647b3cdcSGeorge Liu                                                  "application/octet-stream");
3496647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Transfer-Encoding",
3497647b3cdcSGeorge Liu                                                  "Base64");
3498647b3cdcSGeorge Liu                         asyncResp->res.body() =
3499647b3cdcSGeorge Liu                             crow::utility::base64encode(strData);
3500647b3cdcSGeorge Liu                     },
3501647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode0",
3502647b3cdcSGeorge Liu                     "/xyz/openbmc_project/State/Boot/PostCode0",
3503647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3504647b3cdcSGeorge Liu                     index);
3505647b3cdcSGeorge Liu             });
3506647b3cdcSGeorge Liu }
3507647b3cdcSGeorge Liu 
35087e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3509a3316fc6SZhikuiRen {
35107e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
35117e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3512ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
35137e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
35147e860f15SJohn Edward Broadbent             [](const crow::Request&,
35157e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35167e860f15SJohn Edward Broadbent                const std::string& targetID) {
3517647b3cdcSGeorge Liu                 uint16_t bootIndex = 0;
3518647b3cdcSGeorge Liu                 uint64_t codeIndex = 0;
3519647b3cdcSGeorge Liu                 if (!parsePostCode(targetID, codeIndex, bootIndex))
3520a3316fc6SZhikuiRen                 {
3521a3316fc6SZhikuiRen                     // Requested ID was not found
3522a3316fc6SZhikuiRen                     messages::resourceMissingAtURI(asyncResp->res, targetID);
3523a3316fc6SZhikuiRen                     return;
3524a3316fc6SZhikuiRen                 }
3525a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3526a3316fc6SZhikuiRen                 {
3527a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
35287e860f15SJohn Edward Broadbent                                      << targetID;
3529a3316fc6SZhikuiRen                 }
3530a3316fc6SZhikuiRen 
35317e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
35327e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3533a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
35340fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3535a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3536a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3537a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3538a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3539a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3540a3316fc6SZhikuiRen 
3541a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
35427e860f15SJohn Edward Broadbent             });
3543a3316fc6SZhikuiRen }
3544a3316fc6SZhikuiRen 
35451da66f75SEd Tanous } // namespace redfish
3546