xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 67df073b1f0950fdeafc6d30240d360bd162d0f8)
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 }
184*67df073bSEd Tanous #ifdef NEW_BOOST_URL
185*67df073bSEd Tanous static bool getSkipParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
186*67df073bSEd Tanous                          const crow::Request& req, uint64_t& skip)
187*67df073bSEd Tanous {
188*67df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$skip");
189*67df073bSEd Tanous     if (it != req.urlView.params().end())
190*67df073bSEd Tanous     {
191*67df073bSEd Tanous         std::from_chars_result r = std::from_chars(
192*67df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), skip);
193*67df073bSEd Tanous         if (r.ec != std::errc())
194*67df073bSEd Tanous         {
195*67df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$skip");
196*67df073bSEd Tanous             return false;
197*67df073bSEd Tanous         }
198*67df073bSEd Tanous     }
199*67df073bSEd Tanous     return true;
200*67df073bSEd Tanous }
201*67df073bSEd Tanous 
202*67df073bSEd Tanous static constexpr const uint64_t maxEntriesPerPage = 1000;
203*67df073bSEd Tanous static bool getTopParam(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
204*67df073bSEd Tanous                         const crow::Request& req, uint64_t& top)
205*67df073bSEd Tanous {
206*67df073bSEd Tanous     boost::urls::params_view::iterator it = req.urlView.params().find("$top");
207*67df073bSEd Tanous     if (it != req.urlView.params().end())
208*67df073bSEd Tanous     {
209*67df073bSEd Tanous         std::from_chars_result r = std::from_chars(
210*67df073bSEd Tanous             (*it).value.data(), (*it).value.data() + (*it).value.size(), top);
211*67df073bSEd Tanous         if (r.ec != std::errc())
212*67df073bSEd Tanous         {
213*67df073bSEd Tanous             messages::queryParameterValueTypeError(asyncResp->res, "", "$top");
214*67df073bSEd Tanous             return false;
215*67df073bSEd Tanous         }
216*67df073bSEd Tanous         if (top < 1U || top > maxEntriesPerPage)
217*67df073bSEd Tanous         {
218*67df073bSEd Tanous 
219*67df073bSEd Tanous             messages::queryParameterOutOfRange(
220*67df073bSEd Tanous                 asyncResp->res, std::to_string(top), "$top",
221*67df073bSEd Tanous                 "1-" + std::to_string(maxEntriesPerPage));
222*67df073bSEd Tanous             return false;
223*67df073bSEd Tanous         }
224*67df073bSEd Tanous     }
225*67df073bSEd Tanous     return true;
226*67df073bSEd Tanous }
227*67df073bSEd Tanous 
228*67df073bSEd 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 
278*67df073bSEd Tanous #endif
279*67df073bSEd 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;
143166664f25SEd Tanous                         std::time_t timestamp{};
1432d139c236SGeorge Liu                         std::time_t updateTimestamp{};
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                                     {
1451cb92c03bSAndrew Geissler                                         const uint64_t* millisTimeStamp =
1452f86bb901SAdriana Kobylak                                             std::get_if<uint64_t>(
1453f86bb901SAdriana Kobylak                                                 &propertyMap.second);
1454ae34c8e8SAdriana Kobylak                                         if (millisTimeStamp != nullptr)
1455ebd45906SGeorge Liu                                         {
14567e860f15SJohn Edward Broadbent                                             timestamp =
14577e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14587e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14597e860f15SJohn Edward Broadbent                                         }
14607e860f15SJohn Edward Broadbent                                     }
14617e860f15SJohn Edward Broadbent                                     else if (propertyMap.first ==
14627e860f15SJohn Edward Broadbent                                              "UpdateTimestamp")
14637e860f15SJohn Edward Broadbent                                     {
14647e860f15SJohn Edward Broadbent                                         const uint64_t* millisTimeStamp =
14657e860f15SJohn Edward Broadbent                                             std::get_if<uint64_t>(
14667e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14677e860f15SJohn Edward Broadbent                                         if (millisTimeStamp != nullptr)
14687e860f15SJohn Edward Broadbent                                         {
14697e860f15SJohn Edward Broadbent                                             updateTimestamp =
14707e860f15SJohn Edward Broadbent                                                 crow::utility::getTimestamp(
14717e860f15SJohn Edward Broadbent                                                     *millisTimeStamp);
14727e860f15SJohn Edward Broadbent                                         }
14737e860f15SJohn Edward Broadbent                                     }
14747e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Severity")
14757e860f15SJohn Edward Broadbent                                     {
14767e860f15SJohn Edward Broadbent                                         severity = std::get_if<std::string>(
14777e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14787e860f15SJohn Edward Broadbent                                     }
14797e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Message")
14807e860f15SJohn Edward Broadbent                                     {
14817e860f15SJohn Edward Broadbent                                         message = std::get_if<std::string>(
14827e860f15SJohn Edward Broadbent                                             &propertyMap.second);
14837e860f15SJohn Edward Broadbent                                     }
14847e860f15SJohn Edward Broadbent                                     else if (propertyMap.first == "Resolved")
14857e860f15SJohn Edward Broadbent                                     {
1486914e2d5dSEd Tanous                                         const bool* resolveptr =
1487914e2d5dSEd Tanous                                             std::get_if<bool>(
14887e860f15SJohn Edward Broadbent                                                 &propertyMap.second);
14897e860f15SJohn Edward Broadbent                                         if (resolveptr == nullptr)
14907e860f15SJohn Edward Broadbent                                         {
14917e860f15SJohn Edward Broadbent                                             messages::internalError(
14927e860f15SJohn Edward Broadbent                                                 asyncResp->res);
14937e860f15SJohn Edward Broadbent                                             return;
14947e860f15SJohn Edward Broadbent                                         }
14957e860f15SJohn Edward Broadbent                                         resolved = *resolveptr;
14967e860f15SJohn Edward Broadbent                                     }
14977e860f15SJohn Edward Broadbent                                 }
14987e860f15SJohn Edward Broadbent                                 if (id == nullptr || message == nullptr ||
14997e860f15SJohn Edward Broadbent                                     severity == nullptr)
15007e860f15SJohn Edward Broadbent                                 {
15017e860f15SJohn Edward Broadbent                                     messages::internalError(asyncResp->res);
15027e860f15SJohn Edward Broadbent                                     return;
15037e860f15SJohn Edward Broadbent                                 }
15047e860f15SJohn Edward Broadbent                             }
15057e860f15SJohn Edward Broadbent                             else if (interfaceMap.first ==
15067e860f15SJohn Edward Broadbent                                      "xyz.openbmc_project.Common.FilePath")
15077e860f15SJohn Edward Broadbent                             {
15087e860f15SJohn Edward Broadbent                                 for (auto& propertyMap : interfaceMap.second)
15097e860f15SJohn Edward Broadbent                                 {
15107e860f15SJohn Edward Broadbent                                     if (propertyMap.first == "Path")
15117e860f15SJohn Edward Broadbent                                     {
15127e860f15SJohn Edward Broadbent                                         filePath = std::get_if<std::string>(
15137e860f15SJohn Edward Broadbent                                             &propertyMap.second);
15147e860f15SJohn Edward Broadbent                                     }
15157e860f15SJohn Edward Broadbent                                 }
15167e860f15SJohn Edward Broadbent                             }
15177e860f15SJohn Edward Broadbent                         }
15187e860f15SJohn Edward Broadbent                         // Object path without the
15197e860f15SJohn Edward Broadbent                         // xyz.openbmc_project.Logging.Entry interface, ignore
15207e860f15SJohn Edward Broadbent                         // and continue.
15217e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
15227e860f15SJohn Edward Broadbent                             severity == nullptr)
15237e860f15SJohn Edward Broadbent                         {
15247e860f15SJohn Edward Broadbent                             continue;
15257e860f15SJohn Edward Broadbent                         }
15267e860f15SJohn Edward Broadbent                         entriesArray.push_back({});
15277e860f15SJohn Edward Broadbent                         nlohmann::json& thisEntry = entriesArray.back();
15287e860f15SJohn Edward Broadbent                         thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
15297e860f15SJohn Edward Broadbent                         thisEntry["@odata.id"] =
15300fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
15317e860f15SJohn Edward Broadbent                             std::to_string(*id);
15327e860f15SJohn Edward Broadbent                         thisEntry["Name"] = "System Event Log Entry";
15337e860f15SJohn Edward Broadbent                         thisEntry["Id"] = std::to_string(*id);
15347e860f15SJohn Edward Broadbent                         thisEntry["Message"] = *message;
15357e860f15SJohn Edward Broadbent                         thisEntry["Resolved"] = resolved;
15367e860f15SJohn Edward Broadbent                         thisEntry["EntryType"] = "Event";
15377e860f15SJohn Edward Broadbent                         thisEntry["Severity"] =
15387e860f15SJohn Edward Broadbent                             translateSeverityDbusToRedfish(*severity);
15397e860f15SJohn Edward Broadbent                         thisEntry["Created"] =
15401d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(timestamp);
15417e860f15SJohn Edward Broadbent                         thisEntry["Modified"] =
15421d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(updateTimestamp);
15437e860f15SJohn Edward Broadbent                         if (filePath != nullptr)
15447e860f15SJohn Edward Broadbent                         {
15457e860f15SJohn Edward Broadbent                             thisEntry["AdditionalDataURI"] =
15460fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
15477e860f15SJohn Edward Broadbent                                 std::to_string(*id) + "/attachment";
15487e860f15SJohn Edward Broadbent                         }
15497e860f15SJohn Edward Broadbent                     }
15507e860f15SJohn Edward Broadbent                     std::sort(entriesArray.begin(), entriesArray.end(),
15517e860f15SJohn Edward Broadbent                               [](const nlohmann::json& left,
15527e860f15SJohn Edward Broadbent                                  const nlohmann::json& right) {
15537e860f15SJohn Edward Broadbent                                   return (left["Id"] <= right["Id"]);
15547e860f15SJohn Edward Broadbent                               });
15557e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members@odata.count"] =
15567e860f15SJohn Edward Broadbent                         entriesArray.size();
15577e860f15SJohn Edward Broadbent                 },
15587e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
15597e860f15SJohn Edward Broadbent                 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
15607e860f15SJohn Edward Broadbent         });
15617e860f15SJohn Edward Broadbent }
15627e860f15SJohn Edward Broadbent 
15637e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
15647e860f15SJohn Edward Broadbent {
15657e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15667e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1567ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
15687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
15697e860f15SJohn Edward Broadbent             [](const crow::Request&,
15707e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15717e860f15SJohn Edward Broadbent                const std::string& param)
15727e860f15SJohn Edward Broadbent 
15737e860f15SJohn Edward Broadbent             {
15747e860f15SJohn Edward Broadbent                 std::string entryID = param;
15757e860f15SJohn Edward Broadbent                 dbus::utility::escapePathForDbus(entryID);
15767e860f15SJohn Edward Broadbent 
15777e860f15SJohn Edward Broadbent                 // DBus implementation of EventLog/Entries
15787e860f15SJohn Edward Broadbent                 // Make call to Logging Service to find all log entry objects
15797e860f15SJohn Edward Broadbent                 crow::connections::systemBus->async_method_call(
15807e860f15SJohn Edward Broadbent                     [asyncResp, entryID](const boost::system::error_code ec,
1581914e2d5dSEd Tanous                                          const GetManagedPropertyType& resp) {
15827e860f15SJohn Edward Broadbent                         if (ec.value() == EBADR)
15837e860f15SJohn Edward Broadbent                         {
15847e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
15857e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogEntry", entryID);
15867e860f15SJohn Edward Broadbent                             return;
15877e860f15SJohn Edward Broadbent                         }
15887e860f15SJohn Edward Broadbent                         if (ec)
15897e860f15SJohn Edward Broadbent                         {
15900fda0f12SGeorge Liu                             BMCWEB_LOG_ERROR
15910fda0f12SGeorge Liu                                 << "EventLogEntry (DBus) resp_handler got error "
15927e860f15SJohn Edward Broadbent                                 << ec;
15937e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
15947e860f15SJohn Edward Broadbent                             return;
15957e860f15SJohn Edward Broadbent                         }
1596914e2d5dSEd Tanous                         const uint32_t* id = nullptr;
15977e860f15SJohn Edward Broadbent                         std::time_t timestamp{};
15987e860f15SJohn Edward Broadbent                         std::time_t updateTimestamp{};
1599914e2d5dSEd Tanous                         const std::string* severity = nullptr;
1600914e2d5dSEd Tanous                         const std::string* message = nullptr;
1601914e2d5dSEd Tanous                         const std::string* filePath = nullptr;
16027e860f15SJohn Edward Broadbent                         bool resolved = false;
16037e860f15SJohn Edward Broadbent 
16047e860f15SJohn Edward Broadbent                         for (auto& propertyMap : resp)
16057e860f15SJohn Edward Broadbent                         {
16067e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Id")
16077e860f15SJohn Edward Broadbent                             {
16087e860f15SJohn Edward Broadbent                                 id = std::get_if<uint32_t>(&propertyMap.second);
16097e860f15SJohn Edward Broadbent                             }
16107e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Timestamp")
16117e860f15SJohn Edward Broadbent                             {
16127e860f15SJohn Edward Broadbent                                 const uint64_t* millisTimeStamp =
16137e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
16147e860f15SJohn Edward Broadbent                                 if (millisTimeStamp != nullptr)
16157e860f15SJohn Edward Broadbent                                 {
1616d139c236SGeorge Liu                                     timestamp = crow::utility::getTimestamp(
1617cb92c03bSAndrew Geissler                                         *millisTimeStamp);
1618d139c236SGeorge Liu                                 }
1619ebd45906SGeorge Liu                             }
1620d139c236SGeorge Liu                             else if (propertyMap.first == "UpdateTimestamp")
1621d139c236SGeorge Liu                             {
1622d139c236SGeorge Liu                                 const uint64_t* millisTimeStamp =
16237e860f15SJohn Edward Broadbent                                     std::get_if<uint64_t>(&propertyMap.second);
1624ae34c8e8SAdriana Kobylak                                 if (millisTimeStamp != nullptr)
1625ebd45906SGeorge Liu                                 {
1626ebd45906SGeorge Liu                                     updateTimestamp =
1627ebd45906SGeorge Liu                                         crow::utility::getTimestamp(
1628d139c236SGeorge Liu                                             *millisTimeStamp);
1629cb92c03bSAndrew Geissler                                 }
1630ebd45906SGeorge Liu                             }
1631cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Severity")
1632cb92c03bSAndrew Geissler                             {
1633cb92c03bSAndrew Geissler                                 severity = std::get_if<std::string>(
1634cb92c03bSAndrew Geissler                                     &propertyMap.second);
1635cb92c03bSAndrew Geissler                             }
1636cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Message")
1637cb92c03bSAndrew Geissler                             {
1638cb92c03bSAndrew Geissler                                 message = std::get_if<std::string>(
1639cb92c03bSAndrew Geissler                                     &propertyMap.second);
1640ae34c8e8SAdriana Kobylak                             }
164175710de2SXiaochao Ma                             else if (propertyMap.first == "Resolved")
164275710de2SXiaochao Ma                             {
1643914e2d5dSEd Tanous                                 const bool* resolveptr =
164475710de2SXiaochao Ma                                     std::get_if<bool>(&propertyMap.second);
164575710de2SXiaochao Ma                                 if (resolveptr == nullptr)
164675710de2SXiaochao Ma                                 {
164775710de2SXiaochao Ma                                     messages::internalError(asyncResp->res);
164875710de2SXiaochao Ma                                     return;
164975710de2SXiaochao Ma                                 }
165075710de2SXiaochao Ma                                 resolved = *resolveptr;
165175710de2SXiaochao Ma                             }
16527e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Path")
1653f86bb901SAdriana Kobylak                             {
1654f86bb901SAdriana Kobylak                                 filePath = std::get_if<std::string>(
1655f86bb901SAdriana Kobylak                                     &propertyMap.second);
1656f86bb901SAdriana Kobylak                             }
1657f86bb901SAdriana Kobylak                         }
1658f86bb901SAdriana Kobylak                         if (id == nullptr || message == nullptr ||
1659f86bb901SAdriana Kobylak                             severity == nullptr)
1660f86bb901SAdriana Kobylak                         {
1661ae34c8e8SAdriana Kobylak                             messages::internalError(asyncResp->res);
1662271584abSEd Tanous                             return;
1663271584abSEd Tanous                         }
1664f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.type"] =
1665f86bb901SAdriana Kobylak                             "#LogEntry.v1_8_0.LogEntry";
1666f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["@odata.id"] =
16670fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1668f86bb901SAdriana Kobylak                             std::to_string(*id);
16697e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Name"] =
16707e860f15SJohn Edward Broadbent                             "System Event Log Entry";
1671f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1672f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Message"] = *message;
1673f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Resolved"] = resolved;
1674f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["EntryType"] = "Event";
1675f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Severity"] =
1676f86bb901SAdriana Kobylak                             translateSeverityDbusToRedfish(*severity);
1677f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Created"] =
16781d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(timestamp);
1679f86bb901SAdriana Kobylak                         asyncResp->res.jsonValue["Modified"] =
16801d8782e7SNan Zhou                             crow::utility::getDateTimeStdtime(updateTimestamp);
1681f86bb901SAdriana Kobylak                         if (filePath != nullptr)
1682f86bb901SAdriana Kobylak                         {
1683f86bb901SAdriana Kobylak                             asyncResp->res.jsonValue["AdditionalDataURI"] =
16840fda0f12SGeorge Liu                                 "/redfish/v1/Systems/system/LogServices/EventLog/attachment/" +
16857e860f15SJohn Edward Broadbent                                 std::to_string(*id);
1686f86bb901SAdriana Kobylak                         }
1687cb92c03bSAndrew Geissler                     },
1688cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Logging",
1689cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging/entry/" + entryID,
1690f86bb901SAdriana Kobylak                     "org.freedesktop.DBus.Properties", "GetAll", "");
16917e860f15SJohn Edward Broadbent             });
1692336e96c6SChicago Duan 
16937e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
16947e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1695ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
16967e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
16977e860f15SJohn Edward Broadbent             [](const crow::Request& req,
16987e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16997e860f15SJohn Edward Broadbent                const std::string& entryId) {
170075710de2SXiaochao Ma                 std::optional<bool> resolved;
170175710de2SXiaochao Ma 
17027e860f15SJohn Edward Broadbent                 if (!json_util::readJson(req, asyncResp->res, "Resolved",
17037e860f15SJohn Edward Broadbent                                          resolved))
170475710de2SXiaochao Ma                 {
170575710de2SXiaochao Ma                     return;
170675710de2SXiaochao Ma                 }
170775710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "Set Resolved";
170875710de2SXiaochao Ma 
170975710de2SXiaochao Ma                 crow::connections::systemBus->async_method_call(
17104f48d5f6SEd Tanous                     [asyncResp, entryId](const boost::system::error_code ec) {
171175710de2SXiaochao Ma                         if (ec)
171275710de2SXiaochao Ma                         {
171375710de2SXiaochao Ma                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
171475710de2SXiaochao Ma                             messages::internalError(asyncResp->res);
171575710de2SXiaochao Ma                             return;
171675710de2SXiaochao Ma                         }
171775710de2SXiaochao Ma                     },
171875710de2SXiaochao Ma                     "xyz.openbmc_project.Logging",
171975710de2SXiaochao Ma                     "/xyz/openbmc_project/logging/entry/" + entryId,
172075710de2SXiaochao Ma                     "org.freedesktop.DBus.Properties", "Set",
172175710de2SXiaochao Ma                     "xyz.openbmc_project.Logging.Entry", "Resolved",
1722168e20c1SEd Tanous                     dbus::utility::DbusVariantType(*resolved));
17237e860f15SJohn Edward Broadbent             });
172475710de2SXiaochao Ma 
17257e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
17267e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1727ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1728ed398213SEd Tanous 
17297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
17307e860f15SJohn Edward Broadbent             [](const crow::Request&,
17317e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17327e860f15SJohn Edward Broadbent                const std::string& param)
17337e860f15SJohn Edward Broadbent 
1734336e96c6SChicago Duan             {
1735336e96c6SChicago Duan                 BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1736336e96c6SChicago Duan 
17377e860f15SJohn Edward Broadbent                 std::string entryID = param;
1738336e96c6SChicago Duan 
1739336e96c6SChicago Duan                 dbus::utility::escapePathForDbus(entryID);
1740336e96c6SChicago Duan 
1741336e96c6SChicago Duan                 // Process response from Logging service.
17427e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp, entryID](
17437e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
17447e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
17457e860f15SJohn Edward Broadbent                         << "EventLogEntry (DBus) doDelete callback: Done";
1746336e96c6SChicago Duan                     if (ec)
1747336e96c6SChicago Duan                     {
17483de8d8baSGeorge Liu                         if (ec.value() == EBADR)
17493de8d8baSGeorge Liu                         {
17507e860f15SJohn Edward Broadbent                             messages::resourceNotFound(asyncResp->res,
17517e860f15SJohn Edward Broadbent                                                        "LogEntry", entryID);
17523de8d8baSGeorge Liu                             return;
17533de8d8baSGeorge Liu                         }
1754336e96c6SChicago Duan                         // TODO Handle for specific error code
17550fda0f12SGeorge Liu                         BMCWEB_LOG_ERROR
17560fda0f12SGeorge Liu                             << "EventLogEntry (DBus) doDelete respHandler got error "
1757336e96c6SChicago Duan                             << ec;
1758336e96c6SChicago Duan                         asyncResp->res.result(
1759336e96c6SChicago Duan                             boost::beast::http::status::internal_server_error);
1760336e96c6SChicago Duan                         return;
1761336e96c6SChicago Duan                     }
1762336e96c6SChicago Duan 
1763336e96c6SChicago Duan                     asyncResp->res.result(boost::beast::http::status::ok);
1764336e96c6SChicago Duan                 };
1765336e96c6SChicago Duan 
1766336e96c6SChicago Duan                 // Make call to Logging service to request Delete Log
1767336e96c6SChicago Duan                 crow::connections::systemBus->async_method_call(
1768336e96c6SChicago Duan                     respHandler, "xyz.openbmc_project.Logging",
1769336e96c6SChicago Duan                     "/xyz/openbmc_project/logging/entry/" + entryID,
1770336e96c6SChicago Duan                     "xyz.openbmc_project.Object.Delete", "Delete");
17717e860f15SJohn Edward Broadbent             });
1772400fd1fbSAdriana Kobylak }
1773400fd1fbSAdriana Kobylak 
17747e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1775400fd1fbSAdriana Kobylak {
17760fda0f12SGeorge Liu     BMCWEB_ROUTE(
17770fda0f12SGeorge Liu         app,
17780fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1779ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
17807e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
17817e860f15SJohn Edward Broadbent             [](const crow::Request& req,
17827e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17837e860f15SJohn Edward Broadbent                const std::string& param)
1784400fd1fbSAdriana Kobylak 
17857e860f15SJohn Edward Broadbent             {
1786647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
1787647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
1788400fd1fbSAdriana Kobylak                 {
17897e860f15SJohn Edward Broadbent                     asyncResp->res.result(
17907e860f15SJohn Edward Broadbent                         boost::beast::http::status::bad_request);
1791400fd1fbSAdriana Kobylak                     return;
1792400fd1fbSAdriana Kobylak                 }
1793400fd1fbSAdriana Kobylak 
17947e860f15SJohn Edward Broadbent                 std::string entryID = param;
1795400fd1fbSAdriana Kobylak                 dbus::utility::escapePathForDbus(entryID);
1796400fd1fbSAdriana Kobylak 
1797400fd1fbSAdriana Kobylak                 crow::connections::systemBus->async_method_call(
17987e860f15SJohn Edward Broadbent                     [asyncResp,
17997e860f15SJohn Edward Broadbent                      entryID](const boost::system::error_code ec,
1800400fd1fbSAdriana Kobylak                               const sdbusplus::message::unix_fd& unixfd) {
1801400fd1fbSAdriana Kobylak                         if (ec.value() == EBADR)
1802400fd1fbSAdriana Kobylak                         {
18037e860f15SJohn Edward Broadbent                             messages::resourceNotFound(
18047e860f15SJohn Edward Broadbent                                 asyncResp->res, "EventLogAttachment", entryID);
1805400fd1fbSAdriana Kobylak                             return;
1806400fd1fbSAdriana Kobylak                         }
1807400fd1fbSAdriana Kobylak                         if (ec)
1808400fd1fbSAdriana Kobylak                         {
1809400fd1fbSAdriana Kobylak                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1810400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1811400fd1fbSAdriana Kobylak                             return;
1812400fd1fbSAdriana Kobylak                         }
1813400fd1fbSAdriana Kobylak 
1814400fd1fbSAdriana Kobylak                         int fd = -1;
1815400fd1fbSAdriana Kobylak                         fd = dup(unixfd);
1816400fd1fbSAdriana Kobylak                         if (fd == -1)
1817400fd1fbSAdriana Kobylak                         {
1818400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1819400fd1fbSAdriana Kobylak                             return;
1820400fd1fbSAdriana Kobylak                         }
1821400fd1fbSAdriana Kobylak 
1822400fd1fbSAdriana Kobylak                         long long int size = lseek(fd, 0, SEEK_END);
1823400fd1fbSAdriana Kobylak                         if (size == -1)
1824400fd1fbSAdriana Kobylak                         {
1825400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1826400fd1fbSAdriana Kobylak                             return;
1827400fd1fbSAdriana Kobylak                         }
1828400fd1fbSAdriana Kobylak 
1829400fd1fbSAdriana Kobylak                         // Arbitrary max size of 64kb
1830400fd1fbSAdriana Kobylak                         constexpr int maxFileSize = 65536;
1831400fd1fbSAdriana Kobylak                         if (size > maxFileSize)
1832400fd1fbSAdriana Kobylak                         {
1833400fd1fbSAdriana Kobylak                             BMCWEB_LOG_ERROR
1834400fd1fbSAdriana Kobylak                                 << "File size exceeds maximum allowed size of "
1835400fd1fbSAdriana Kobylak                                 << maxFileSize;
1836400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1837400fd1fbSAdriana Kobylak                             return;
1838400fd1fbSAdriana Kobylak                         }
1839400fd1fbSAdriana Kobylak                         std::vector<char> data(static_cast<size_t>(size));
1840400fd1fbSAdriana Kobylak                         long long int rc = lseek(fd, 0, SEEK_SET);
1841400fd1fbSAdriana Kobylak                         if (rc == -1)
1842400fd1fbSAdriana Kobylak                         {
1843400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1844400fd1fbSAdriana Kobylak                             return;
1845400fd1fbSAdriana Kobylak                         }
1846400fd1fbSAdriana Kobylak                         rc = read(fd, data.data(), data.size());
1847400fd1fbSAdriana Kobylak                         if ((rc == -1) || (rc != size))
1848400fd1fbSAdriana Kobylak                         {
1849400fd1fbSAdriana Kobylak                             messages::internalError(asyncResp->res);
1850400fd1fbSAdriana Kobylak                             return;
1851400fd1fbSAdriana Kobylak                         }
1852400fd1fbSAdriana Kobylak                         close(fd);
1853400fd1fbSAdriana Kobylak 
1854400fd1fbSAdriana Kobylak                         std::string_view strData(data.data(), data.size());
18557e860f15SJohn Edward Broadbent                         std::string output =
18567e860f15SJohn Edward Broadbent                             crow::utility::base64encode(strData);
1857400fd1fbSAdriana Kobylak 
1858400fd1fbSAdriana Kobylak                         asyncResp->res.addHeader("Content-Type",
1859400fd1fbSAdriana Kobylak                                                  "application/octet-stream");
18607e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Transfer-Encoding",
18617e860f15SJohn Edward Broadbent                                                  "Base64");
1862400fd1fbSAdriana Kobylak                         asyncResp->res.body() = std::move(output);
1863400fd1fbSAdriana Kobylak                     },
1864400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging",
1865400fd1fbSAdriana Kobylak                     "/xyz/openbmc_project/logging/entry/" + entryID,
1866400fd1fbSAdriana Kobylak                     "xyz.openbmc_project.Logging.Entry", "GetEntry");
18677e860f15SJohn Edward Broadbent             });
18681da66f75SEd Tanous }
18691da66f75SEd Tanous 
1870b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1871b7028ebfSSpencer Ku 
1872b7028ebfSSpencer Ku inline bool
1873b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1874b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1875b7028ebfSSpencer Ku {
1876b7028ebfSSpencer Ku     std::error_code ec;
1877b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1878b7028ebfSSpencer Ku     if (ec)
1879b7028ebfSSpencer Ku     {
1880b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1881b7028ebfSSpencer Ku         return false;
1882b7028ebfSSpencer Ku     }
1883b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1884b7028ebfSSpencer Ku     {
1885b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1886b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1887b7028ebfSSpencer Ku         // path
1888b7028ebfSSpencer Ku         if (boost::starts_with(filename, "log"))
1889b7028ebfSSpencer Ku         {
1890b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1891b7028ebfSSpencer Ku         }
1892b7028ebfSSpencer Ku     }
1893b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1894b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1895b7028ebfSSpencer Ku     // descending order.
1896b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1897b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1898b7028ebfSSpencer Ku 
1899b7028ebfSSpencer Ku     return true;
1900b7028ebfSSpencer Ku }
1901b7028ebfSSpencer Ku 
1902b7028ebfSSpencer Ku inline bool
1903b7028ebfSSpencer Ku     getHostLoggerEntries(std::vector<std::filesystem::path>& hostLoggerFiles,
1904b7028ebfSSpencer Ku                          uint64_t& skip, uint64_t& top,
1905b7028ebfSSpencer Ku                          std::vector<std::string>& logEntries, size_t& logCount)
1906b7028ebfSSpencer Ku {
1907b7028ebfSSpencer Ku     GzFileReader logFile;
1908b7028ebfSSpencer Ku 
1909b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1910b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1911b7028ebfSSpencer Ku     {
1912b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1913b7028ebfSSpencer Ku         {
1914b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1915b7028ebfSSpencer Ku             return false;
1916b7028ebfSSpencer Ku         }
1917b7028ebfSSpencer Ku     }
1918b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1919b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1920b7028ebfSSpencer Ku     if (!lastMessage.empty())
1921b7028ebfSSpencer Ku     {
1922b7028ebfSSpencer Ku         logCount++;
1923b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1924b7028ebfSSpencer Ku         {
1925b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1926b7028ebfSSpencer Ku         }
1927b7028ebfSSpencer Ku     }
1928b7028ebfSSpencer Ku     return true;
1929b7028ebfSSpencer Ku }
1930b7028ebfSSpencer Ku 
1931b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1932b7028ebfSSpencer Ku                                     const std::string& msg,
1933b7028ebfSSpencer Ku                                     nlohmann::json& logEntryJson)
1934b7028ebfSSpencer Ku {
1935b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
1936b7028ebfSSpencer Ku     logEntryJson = {
1937b7028ebfSSpencer Ku         {"@odata.type", "#LogEntry.v1_4_0.LogEntry"},
1938b7028ebfSSpencer Ku         {"@odata.id",
1939b7028ebfSSpencer Ku          "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
1940b7028ebfSSpencer Ku              logEntryID},
1941b7028ebfSSpencer Ku         {"Name", "Host Logger Entry"},
1942b7028ebfSSpencer Ku         {"Id", logEntryID},
1943b7028ebfSSpencer Ku         {"Message", msg},
1944b7028ebfSSpencer Ku         {"EntryType", "Oem"},
1945b7028ebfSSpencer Ku         {"Severity", "OK"},
1946b7028ebfSSpencer Ku         {"OemRecordFormat", "Host Logger Entry"}};
1947b7028ebfSSpencer Ku }
1948b7028ebfSSpencer Ku 
1949b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1950b7028ebfSSpencer Ku {
1951b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1952b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
19530fda0f12SGeorge Liu         .methods(
19540fda0f12SGeorge Liu             boost::beast::http::verb::
19550fda0f12SGeorge Liu                 get)([](const crow::Request&,
1956b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1957b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1958b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger";
1959b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1960b7028ebfSSpencer Ku                 "#LogService.v1_1_0.LogService";
1961b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1962b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1963b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Id"] = "HostLogger";
1964b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Entries"] = {
19650fda0f12SGeorge Liu                 {"@odata.id",
19660fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries"}};
1967b7028ebfSSpencer Ku         });
1968b7028ebfSSpencer Ku }
1969b7028ebfSSpencer Ku 
1970b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1971b7028ebfSSpencer Ku {
1972b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1973b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1974b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
19750fda0f12SGeorge Liu         .methods(
19760fda0f12SGeorge Liu             boost::beast::http::verb::
19770fda0f12SGeorge Liu                 get)([](const crow::Request& req,
1978b7028ebfSSpencer Ku                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1979b7028ebfSSpencer Ku             uint64_t skip = 0;
1980b7028ebfSSpencer Ku             uint64_t top = maxEntriesPerPage; // Show max 1000 entries by
1981b7028ebfSSpencer Ku                                               // default, allow range 1 to
1982b7028ebfSSpencer Ku                                               // 1000 entries per page.
1983b7028ebfSSpencer Ku             if (!getSkipParam(asyncResp, req, skip))
1984b7028ebfSSpencer Ku             {
1985b7028ebfSSpencer Ku                 return;
1986b7028ebfSSpencer Ku             }
1987b7028ebfSSpencer Ku             if (!getTopParam(asyncResp, req, top))
1988b7028ebfSSpencer Ku             {
1989b7028ebfSSpencer Ku                 return;
1990b7028ebfSSpencer Ku             }
1991b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.id"] =
1992b7028ebfSSpencer Ku                 "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1993b7028ebfSSpencer Ku             asyncResp->res.jsonValue["@odata.type"] =
1994b7028ebfSSpencer Ku                 "#LogEntryCollection.LogEntryCollection";
1995b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1996b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Description"] =
1997b7028ebfSSpencer Ku                 "Collection of HostLogger Entries";
19980fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1999b7028ebfSSpencer Ku             logEntryArray = nlohmann::json::array();
2000b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = 0;
2001b7028ebfSSpencer Ku 
2002b7028ebfSSpencer Ku             std::vector<std::filesystem::path> hostLoggerFiles;
2003b7028ebfSSpencer Ku             if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2004b7028ebfSSpencer Ku             {
2005b7028ebfSSpencer Ku                 BMCWEB_LOG_ERROR << "fail to get host log file path";
2006b7028ebfSSpencer Ku                 return;
2007b7028ebfSSpencer Ku             }
2008b7028ebfSSpencer Ku 
2009b7028ebfSSpencer Ku             size_t logCount = 0;
2010b7028ebfSSpencer Ku             // This vector only store the entries we want to expose that
2011b7028ebfSSpencer Ku             // control by skip and top.
2012b7028ebfSSpencer Ku             std::vector<std::string> logEntries;
20130fda0f12SGeorge Liu             if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
20140fda0f12SGeorge Liu                                       logCount))
2015b7028ebfSSpencer Ku             {
2016b7028ebfSSpencer Ku                 messages::internalError(asyncResp->res);
2017b7028ebfSSpencer Ku                 return;
2018b7028ebfSSpencer Ku             }
2019b7028ebfSSpencer Ku             // If vector is empty, that means skip value larger than total
2020b7028ebfSSpencer Ku             // log count
202126f6976fSEd Tanous             if (logEntries.empty())
2022b7028ebfSSpencer Ku             {
2023b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2024b7028ebfSSpencer Ku                 return;
2025b7028ebfSSpencer Ku             }
202626f6976fSEd Tanous             if (!logEntries.empty())
2027b7028ebfSSpencer Ku             {
2028b7028ebfSSpencer Ku                 for (size_t i = 0; i < logEntries.size(); i++)
2029b7028ebfSSpencer Ku                 {
2030b7028ebfSSpencer Ku                     logEntryArray.push_back({});
2031b7028ebfSSpencer Ku                     nlohmann::json& hostLogEntry = logEntryArray.back();
2032b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(std::to_string(skip + i),
2033b7028ebfSSpencer Ku                                             logEntries[i], hostLogEntry);
2034b7028ebfSSpencer Ku                 }
2035b7028ebfSSpencer Ku 
2036b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.count"] = logCount;
2037b7028ebfSSpencer Ku                 if (skip + top < logCount)
2038b7028ebfSSpencer Ku                 {
2039b7028ebfSSpencer Ku                     asyncResp->res.jsonValue["Members@odata.nextLink"] =
20400fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
2041b7028ebfSSpencer Ku                         std::to_string(skip + top);
2042b7028ebfSSpencer Ku                 }
2043b7028ebfSSpencer Ku             }
2044b7028ebfSSpencer Ku         });
2045b7028ebfSSpencer Ku }
2046b7028ebfSSpencer Ku 
2047b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
2048b7028ebfSSpencer Ku {
2049b7028ebfSSpencer Ku     BMCWEB_ROUTE(
2050b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
2051b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
2052b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
2053b7028ebfSSpencer Ku             [](const crow::Request&,
2054b7028ebfSSpencer Ku                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2055b7028ebfSSpencer Ku                const std::string& param) {
2056b7028ebfSSpencer Ku                 const std::string& targetID = param;
2057b7028ebfSSpencer Ku 
2058b7028ebfSSpencer Ku                 uint64_t idInt = 0;
2059ca45aa3cSEd Tanous 
2060ca45aa3cSEd Tanous                 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
2061ca45aa3cSEd Tanous                 const char* end = targetID.data() + targetID.size();
2062ca45aa3cSEd Tanous 
2063ca45aa3cSEd Tanous                 auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
2064b7028ebfSSpencer Ku                 if (ec == std::errc::invalid_argument)
2065b7028ebfSSpencer Ku                 {
2066b7028ebfSSpencer Ku                     messages::resourceMissingAtURI(asyncResp->res, targetID);
2067b7028ebfSSpencer Ku                     return;
2068b7028ebfSSpencer Ku                 }
2069b7028ebfSSpencer Ku                 if (ec == std::errc::result_out_of_range)
2070b7028ebfSSpencer Ku                 {
2071b7028ebfSSpencer Ku                     messages::resourceMissingAtURI(asyncResp->res, targetID);
2072b7028ebfSSpencer Ku                     return;
2073b7028ebfSSpencer Ku                 }
2074b7028ebfSSpencer Ku 
2075b7028ebfSSpencer Ku                 std::vector<std::filesystem::path> hostLoggerFiles;
2076b7028ebfSSpencer Ku                 if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
2077b7028ebfSSpencer Ku                 {
2078b7028ebfSSpencer Ku                     BMCWEB_LOG_ERROR << "fail to get host log file path";
2079b7028ebfSSpencer Ku                     return;
2080b7028ebfSSpencer Ku                 }
2081b7028ebfSSpencer Ku 
2082b7028ebfSSpencer Ku                 size_t logCount = 0;
2083b7028ebfSSpencer Ku                 uint64_t top = 1;
2084b7028ebfSSpencer Ku                 std::vector<std::string> logEntries;
2085b7028ebfSSpencer Ku                 // We can get specific entry by skip and top. For example, if we
2086b7028ebfSSpencer Ku                 // want to get nth entry, we can set skip = n-1 and top = 1 to
2087b7028ebfSSpencer Ku                 // get that entry
2088b7028ebfSSpencer Ku                 if (!getHostLoggerEntries(hostLoggerFiles, idInt, top,
2089b7028ebfSSpencer Ku                                           logEntries, logCount))
2090b7028ebfSSpencer Ku                 {
2091b7028ebfSSpencer Ku                     messages::internalError(asyncResp->res);
2092b7028ebfSSpencer Ku                     return;
2093b7028ebfSSpencer Ku                 }
2094b7028ebfSSpencer Ku 
2095b7028ebfSSpencer Ku                 if (!logEntries.empty())
2096b7028ebfSSpencer Ku                 {
2097b7028ebfSSpencer Ku                     fillHostLoggerEntryJson(targetID, logEntries[0],
2098b7028ebfSSpencer Ku                                             asyncResp->res.jsonValue);
2099b7028ebfSSpencer Ku                     return;
2100b7028ebfSSpencer Ku                 }
2101b7028ebfSSpencer Ku 
2102b7028ebfSSpencer Ku                 // Requested ID was not found
2103b7028ebfSSpencer Ku                 messages::resourceMissingAtURI(asyncResp->res, targetID);
2104b7028ebfSSpencer Ku             });
2105b7028ebfSSpencer Ku }
2106b7028ebfSSpencer Ku 
21077e860f15SJohn Edward Broadbent inline void requestRoutesBMCLogServiceCollection(App& app)
21081da66f75SEd Tanous {
21097e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2110ad89dcf0SGunnar Mills         .privileges(redfish::privileges::getLogServiceCollection)
21117e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21127e860f15SJohn Edward Broadbent             [](const crow::Request&,
21137e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
21147e860f15SJohn Edward Broadbent                 // Collections don't include the static data added by SubRoute
21157e860f15SJohn Edward Broadbent                 // because it has a duplicate entry for members
2116e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
21171da66f75SEd Tanous                     "#LogServiceCollection.LogServiceCollection";
2118e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.id"] =
2119e1f26343SJason M. Bills                     "/redfish/v1/Managers/bmc/LogServices";
21207e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
21217e860f15SJohn Edward Broadbent                     "Open BMC Log Services Collection";
2122e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
21231da66f75SEd Tanous                     "Collection of LogServices for this Manager";
21247e860f15SJohn Edward Broadbent                 nlohmann::json& logServiceArray =
21257e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2126c4bf6374SJason M. Bills                 logServiceArray = nlohmann::json::array();
21275cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
21285cb1dd27SAsmitha Karunanithi                 logServiceArray.push_back(
21297e860f15SJohn Edward Broadbent                     {{"@odata.id",
21307e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
21315cb1dd27SAsmitha Karunanithi #endif
2132c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
2133c4bf6374SJason M. Bills                 logServiceArray.push_back(
21347e860f15SJohn Edward Broadbent                     {{"@odata.id",
21357e860f15SJohn Edward Broadbent                       "/redfish/v1/Managers/bmc/LogServices/Journal"}});
2136c4bf6374SJason M. Bills #endif
2137e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2138c4bf6374SJason M. Bills                     logServiceArray.size();
21397e860f15SJohn Edward Broadbent             });
2140e1f26343SJason M. Bills }
2141e1f26343SJason M. Bills 
21427e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2143e1f26343SJason M. Bills {
21447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2145ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
21467e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
21477e860f15SJohn Edward Broadbent             [](const crow::Request&,
21487e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
21498d1b46d7Szhanghch05 
21507e860f15SJohn Edward Broadbent             {
2151e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
2152e1f26343SJason M. Bills                     "#LogService.v1_1_0.LogService";
21530f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
21540f74e643SEd Tanous                     "/redfish/v1/Managers/bmc/LogServices/Journal";
21557e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Name"] =
21567e860f15SJohn Edward Broadbent                     "Open BMC Journal Log Service";
21577e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
21587e860f15SJohn Edward Broadbent                     "BMC Journal Log Service";
2159c4bf6374SJason M. Bills                 asyncResp->res.jsonValue["Id"] = "BMC Journal";
2160e1f26343SJason M. Bills                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
21617c8c4058STejas Patil 
21627c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
21637c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
21647c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
21657c8c4058STejas Patil                     redfishDateTimeOffset.first;
21667c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
21677c8c4058STejas Patil                     redfishDateTimeOffset.second;
21687c8c4058STejas Patil 
2169cd50aa42SJason M. Bills                 asyncResp->res.jsonValue["Entries"] = {
2170cd50aa42SJason M. Bills                     {"@odata.id",
2171086be238SEd Tanous                      "/redfish/v1/Managers/bmc/LogServices/Journal/Entries"}};
21727e860f15SJohn Edward Broadbent             });
2173e1f26343SJason M. Bills }
2174e1f26343SJason M. Bills 
2175c4bf6374SJason M. Bills static int fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2176e1f26343SJason M. Bills                                       sd_journal* journal,
2177c4bf6374SJason M. Bills                                       nlohmann::json& bmcJournalLogEntryJson)
2178e1f26343SJason M. Bills {
2179e1f26343SJason M. Bills     // Get the Log Entry contents
2180e1f26343SJason M. Bills     int ret = 0;
2181e1f26343SJason M. Bills 
2182a8fe54f0SJason M. Bills     std::string message;
2183a8fe54f0SJason M. Bills     std::string_view syslogID;
2184a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2185a8fe54f0SJason M. Bills     if (ret < 0)
2186a8fe54f0SJason M. Bills     {
2187a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2188a8fe54f0SJason M. Bills                          << strerror(-ret);
2189a8fe54f0SJason M. Bills     }
2190a8fe54f0SJason M. Bills     if (!syslogID.empty())
2191a8fe54f0SJason M. Bills     {
2192a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2193a8fe54f0SJason M. Bills     }
2194a8fe54f0SJason M. Bills 
219539e77504SEd Tanous     std::string_view msg;
219616428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2197e1f26343SJason M. Bills     if (ret < 0)
2198e1f26343SJason M. Bills     {
2199e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2200e1f26343SJason M. Bills         return 1;
2201e1f26343SJason M. Bills     }
2202a8fe54f0SJason M. Bills     message += std::string(msg);
2203e1f26343SJason M. Bills 
2204e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2205271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
220616428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2207e1f26343SJason M. Bills     if (ret < 0)
2208e1f26343SJason M. Bills     {
2209e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2210e1f26343SJason M. Bills     }
2211e1f26343SJason M. Bills 
2212e1f26343SJason M. Bills     // Get the Created time from the timestamp
221316428a1aSJason M. Bills     std::string entryTimeStr;
221416428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2215e1f26343SJason M. Bills     {
221616428a1aSJason M. Bills         return 1;
2217e1f26343SJason M. Bills     }
2218e1f26343SJason M. Bills 
2219e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
2220c4bf6374SJason M. Bills     bmcJournalLogEntryJson = {
2221647b3cdcSGeorge Liu         {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
2222c4bf6374SJason M. Bills         {"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
2223c4bf6374SJason M. Bills                           bmcJournalLogEntryID},
2224e1f26343SJason M. Bills         {"Name", "BMC Journal Entry"},
2225c4bf6374SJason M. Bills         {"Id", bmcJournalLogEntryID},
2226a8fe54f0SJason M. Bills         {"Message", std::move(message)},
2227e1f26343SJason M. Bills         {"EntryType", "Oem"},
2228738c1e61SPatrick Williams         {"Severity", severity <= 2   ? "Critical"
2229738c1e61SPatrick Williams                      : severity <= 4 ? "Warning"
2230738c1e61SPatrick Williams                                      : "OK"},
2231086be238SEd Tanous         {"OemRecordFormat", "BMC Journal Entry"},
2232e1f26343SJason M. Bills         {"Created", std::move(entryTimeStr)}};
2233e1f26343SJason M. Bills     return 0;
2234e1f26343SJason M. Bills }
2235e1f26343SJason M. Bills 
22367e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2237e1f26343SJason M. Bills {
22387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2239ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
22400fda0f12SGeorge Liu         .methods(
22410fda0f12SGeorge Liu             boost::beast::http::verb::
22420fda0f12SGeorge Liu                 get)([](const crow::Request& req,
22437e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2244193ad2faSJason M. Bills             static constexpr const long maxEntriesPerPage = 1000;
2245271584abSEd Tanous             uint64_t skip = 0;
2246271584abSEd Tanous             uint64_t top = maxEntriesPerPage; // Show max entries by default
22478d1b46d7Szhanghch05             if (!getSkipParam(asyncResp, req, skip))
2248193ad2faSJason M. Bills             {
2249193ad2faSJason M. Bills                 return;
2250193ad2faSJason M. Bills             }
22518d1b46d7Szhanghch05             if (!getTopParam(asyncResp, req, top))
2252193ad2faSJason M. Bills             {
2253193ad2faSJason M. Bills                 return;
2254193ad2faSJason M. Bills             }
22557e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
22567e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2257e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
2258e1f26343SJason M. Bills                 "#LogEntryCollection.LogEntryCollection";
22590f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
22600f74e643SEd Tanous                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2261e1f26343SJason M. Bills             asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2262e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2263e1f26343SJason M. Bills                 "Collection of BMC Journal Entries";
22640fda0f12SGeorge Liu             nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2265e1f26343SJason M. Bills             logEntryArray = nlohmann::json::array();
2266e1f26343SJason M. Bills 
22677e860f15SJohn Edward Broadbent             // Go through the journal and use the timestamp to create a
22687e860f15SJohn Edward Broadbent             // unique ID for each entry
2269e1f26343SJason M. Bills             sd_journal* journalTmp = nullptr;
2270e1f26343SJason M. Bills             int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2271e1f26343SJason M. Bills             if (ret < 0)
2272e1f26343SJason M. Bills             {
22737e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "failed to open journal: "
22747e860f15SJohn Edward Broadbent                                  << strerror(-ret);
2275f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2276e1f26343SJason M. Bills                 return;
2277e1f26343SJason M. Bills             }
22780fda0f12SGeorge Liu             std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
22790fda0f12SGeorge Liu                 journalTmp, sd_journal_close);
2280e1f26343SJason M. Bills             journalTmp = nullptr;
2281b01bf299SEd Tanous             uint64_t entryCount = 0;
2282e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
2283e85d6b16SJason M. Bills             bool firstEntry = true;
2284e1f26343SJason M. Bills             SD_JOURNAL_FOREACH(journal.get())
2285e1f26343SJason M. Bills             {
2286193ad2faSJason M. Bills                 entryCount++;
22877e860f15SJohn Edward Broadbent                 // Handle paging using skip (number of entries to skip from
22887e860f15SJohn Edward Broadbent                 // the start) and top (number of entries to display)
2289193ad2faSJason M. Bills                 if (entryCount <= skip || entryCount > skip + top)
2290193ad2faSJason M. Bills                 {
2291193ad2faSJason M. Bills                     continue;
2292193ad2faSJason M. Bills                 }
2293193ad2faSJason M. Bills 
229416428a1aSJason M. Bills                 std::string idStr;
2295e85d6b16SJason M. Bills                 if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2296e1f26343SJason M. Bills                 {
2297e1f26343SJason M. Bills                     continue;
2298e1f26343SJason M. Bills                 }
2299e1f26343SJason M. Bills 
2300e85d6b16SJason M. Bills                 if (firstEntry)
2301e85d6b16SJason M. Bills                 {
2302e85d6b16SJason M. Bills                     firstEntry = false;
2303e85d6b16SJason M. Bills                 }
2304e85d6b16SJason M. Bills 
2305e1f26343SJason M. Bills                 logEntryArray.push_back({});
2306c4bf6374SJason M. Bills                 nlohmann::json& bmcJournalLogEntry = logEntryArray.back();
2307c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2308c4bf6374SJason M. Bills                                                bmcJournalLogEntry) != 0)
2309e1f26343SJason M. Bills                 {
2310f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2311e1f26343SJason M. Bills                     return;
2312e1f26343SJason M. Bills                 }
2313e1f26343SJason M. Bills             }
2314193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
2315193ad2faSJason M. Bills             if (skip + top < entryCount)
2316193ad2faSJason M. Bills             {
2317193ad2faSJason M. Bills                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
23180fda0f12SGeorge Liu                     "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
2319193ad2faSJason M. Bills                     std::to_string(skip + top);
2320193ad2faSJason M. Bills             }
23217e860f15SJohn Edward Broadbent         });
2322e1f26343SJason M. Bills }
2323e1f26343SJason M. Bills 
23247e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2325e1f26343SJason M. Bills {
23267e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
23277e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2328ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
23297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
23307e860f15SJohn Edward Broadbent             [](const crow::Request&,
23317e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
23327e860f15SJohn Edward Broadbent                const std::string& entryID) {
2333e1f26343SJason M. Bills                 // Convert the unique ID back to a timestamp to find the entry
2334e1f26343SJason M. Bills                 uint64_t ts = 0;
2335271584abSEd Tanous                 uint64_t index = 0;
23368d1b46d7Szhanghch05                 if (!getTimestampFromID(asyncResp, entryID, ts, index))
2337e1f26343SJason M. Bills                 {
233816428a1aSJason M. Bills                     return;
2339e1f26343SJason M. Bills                 }
2340e1f26343SJason M. Bills 
2341e1f26343SJason M. Bills                 sd_journal* journalTmp = nullptr;
2342e1f26343SJason M. Bills                 int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2343e1f26343SJason M. Bills                 if (ret < 0)
2344e1f26343SJason M. Bills                 {
23457e860f15SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "failed to open journal: "
23467e860f15SJohn Edward Broadbent                                      << strerror(-ret);
2347f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2348e1f26343SJason M. Bills                     return;
2349e1f26343SJason M. Bills                 }
23507e860f15SJohn Edward Broadbent                 std::unique_ptr<sd_journal, decltype(&sd_journal_close)>
23517e860f15SJohn Edward Broadbent                     journal(journalTmp, sd_journal_close);
2352e1f26343SJason M. Bills                 journalTmp = nullptr;
23537e860f15SJohn Edward Broadbent                 // Go to the timestamp in the log and move to the entry at the
23547e860f15SJohn Edward Broadbent                 // index tracking the unique ID
2355af07e3f5SJason M. Bills                 std::string idStr;
2356af07e3f5SJason M. Bills                 bool firstEntry = true;
2357e1f26343SJason M. Bills                 ret = sd_journal_seek_realtime_usec(journal.get(), ts);
23582056b6d1SManojkiran Eda                 if (ret < 0)
23592056b6d1SManojkiran Eda                 {
23602056b6d1SManojkiran Eda                     BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
23612056b6d1SManojkiran Eda                                      << strerror(-ret);
23622056b6d1SManojkiran Eda                     messages::internalError(asyncResp->res);
23632056b6d1SManojkiran Eda                     return;
23642056b6d1SManojkiran Eda                 }
2365271584abSEd Tanous                 for (uint64_t i = 0; i <= index; i++)
2366e1f26343SJason M. Bills                 {
2367e1f26343SJason M. Bills                     sd_journal_next(journal.get());
2368af07e3f5SJason M. Bills                     if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2369af07e3f5SJason M. Bills                     {
2370af07e3f5SJason M. Bills                         messages::internalError(asyncResp->res);
2371af07e3f5SJason M. Bills                         return;
2372af07e3f5SJason M. Bills                     }
2373af07e3f5SJason M. Bills                     if (firstEntry)
2374af07e3f5SJason M. Bills                     {
2375af07e3f5SJason M. Bills                         firstEntry = false;
2376af07e3f5SJason M. Bills                     }
2377e1f26343SJason M. Bills                 }
2378c4bf6374SJason M. Bills                 // Confirm that the entry ID matches what was requested
2379af07e3f5SJason M. Bills                 if (idStr != entryID)
2380c4bf6374SJason M. Bills                 {
2381c4bf6374SJason M. Bills                     messages::resourceMissingAtURI(asyncResp->res, entryID);
2382c4bf6374SJason M. Bills                     return;
2383c4bf6374SJason M. Bills                 }
2384c4bf6374SJason M. Bills 
2385c4bf6374SJason M. Bills                 if (fillBMCJournalLogEntryJson(entryID, journal.get(),
2386e1f26343SJason M. Bills                                                asyncResp->res.jsonValue) != 0)
2387e1f26343SJason M. Bills                 {
2388f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2389e1f26343SJason M. Bills                     return;
2390e1f26343SJason M. Bills                 }
23917e860f15SJohn Edward Broadbent             });
2392c9bb6861Sraviteja-b }
2393c9bb6861Sraviteja-b 
23947e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpService(App& app)
2395c9bb6861Sraviteja-b {
23967e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2397ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
23980fda0f12SGeorge Liu         .methods(
23990fda0f12SGeorge Liu             boost::beast::http::verb::
24000fda0f12SGeorge Liu                 get)([](const crow::Request&,
24017e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2402c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.id"] =
24035cb1dd27SAsmitha Karunanithi                 "/redfish/v1/Managers/bmc/LogServices/Dump";
2404c9bb6861Sraviteja-b             asyncResp->res.jsonValue["@odata.type"] =
2405d337bb72SAsmitha Karunanithi                 "#LogService.v1_2_0.LogService";
2406c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Name"] = "Dump LogService";
24075cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Description"] = "BMC Dump LogService";
24085cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = "Dump";
2409c9bb6861Sraviteja-b             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
24107c8c4058STejas Patil 
24117c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
24127c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
24130fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
24147c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
24157c8c4058STejas Patil                 redfishDateTimeOffset.second;
24167c8c4058STejas Patil 
2417c9bb6861Sraviteja-b             asyncResp->res.jsonValue["Entries"] = {
24187e860f15SJohn Edward Broadbent                 {"@odata.id",
24197e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries"}};
24205cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Actions"] = {
24215cb1dd27SAsmitha Karunanithi                 {"#LogService.ClearLog",
24220fda0f12SGeorge Liu                  {{"target",
24230fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog"}}},
2424d337bb72SAsmitha Karunanithi                 {"#LogService.CollectDiagnosticData",
24250fda0f12SGeorge Liu                  {{"target",
24260fda0f12SGeorge Liu                    "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
24277e860f15SJohn Edward Broadbent         });
2428c9bb6861Sraviteja-b }
2429c9bb6861Sraviteja-b 
24307e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntryCollection(App& app)
24317e860f15SJohn Edward Broadbent {
24327e860f15SJohn Edward Broadbent 
2433c9bb6861Sraviteja-b     /**
2434c9bb6861Sraviteja-b      * Functions triggers appropriate requests on DBus
2435c9bb6861Sraviteja-b      */
24367e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2437ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
24387e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24397e860f15SJohn Edward Broadbent             [](const crow::Request&,
24407e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2441c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.type"] =
2442c9bb6861Sraviteja-b                     "#LogEntryCollection.LogEntryCollection";
2443c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["@odata.id"] =
24445cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Managers/bmc/LogServices/Dump/Entries";
24455cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "BMC Dump Entries";
2446c9bb6861Sraviteja-b                 asyncResp->res.jsonValue["Description"] =
24475cb1dd27SAsmitha Karunanithi                     "Collection of BMC Dump Entries";
2448c9bb6861Sraviteja-b 
24495cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "BMC");
24507e860f15SJohn Edward Broadbent             });
2451c9bb6861Sraviteja-b }
2452c9bb6861Sraviteja-b 
24537e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2454c9bb6861Sraviteja-b {
24557e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24567e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2457ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
24587e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
24597e860f15SJohn Edward Broadbent             [](const crow::Request&,
24607e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24617e860f15SJohn Edward Broadbent                const std::string& param) {
24627e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "BMC");
24637e860f15SJohn Edward Broadbent             });
24647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24657e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2466ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
24677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
24687e860f15SJohn Edward Broadbent             [](const crow::Request&,
24697e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
24707e860f15SJohn Edward Broadbent                const std::string& param) {
24717e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "bmc");
24727e860f15SJohn Edward Broadbent             });
2473c9bb6861Sraviteja-b }
2474c9bb6861Sraviteja-b 
24757e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2476c9bb6861Sraviteja-b {
24778d1b46d7Szhanghch05 
24780fda0f12SGeorge Liu     BMCWEB_ROUTE(
24790fda0f12SGeorge Liu         app,
24800fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2481ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24827e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24837e860f15SJohn Edward Broadbent             [](const crow::Request& req,
24847e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24858d1b46d7Szhanghch05                 createDump(asyncResp, req, "BMC");
24867e860f15SJohn Edward Broadbent             });
2487a43be80fSAsmitha Karunanithi }
2488a43be80fSAsmitha Karunanithi 
24897e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
249080319af1SAsmitha Karunanithi {
24910fda0f12SGeorge Liu     BMCWEB_ROUTE(
24920fda0f12SGeorge Liu         app,
24930fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2494ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24957e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
24967e860f15SJohn Edward Broadbent             [](const crow::Request&,
24977e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
24988d1b46d7Szhanghch05                 clearDump(asyncResp, "BMC");
24997e860f15SJohn Edward Broadbent             });
25005cb1dd27SAsmitha Karunanithi }
25015cb1dd27SAsmitha Karunanithi 
25027e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
25035cb1dd27SAsmitha Karunanithi {
25047e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2505ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
25067e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25077e860f15SJohn Edward Broadbent             [](const crow::Request&,
25087e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25095cb1dd27SAsmitha Karunanithi 
25107e860f15SJohn Edward Broadbent             {
25115cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
25125cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump";
25135cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
2514d337bb72SAsmitha Karunanithi                     "#LogService.v1_2_0.LogService";
25155cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "Dump LogService";
25167e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["Description"] =
25177e860f15SJohn Edward Broadbent                     "System Dump LogService";
25185cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Id"] = "Dump";
25195cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
25207c8c4058STejas Patil 
25217c8c4058STejas Patil                 std::pair<std::string, std::string> redfishDateTimeOffset =
25227c8c4058STejas Patil                     crow::utility::getDateTimeOffsetNow();
25237c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTime"] =
25247c8c4058STejas Patil                     redfishDateTimeOffset.first;
25257c8c4058STejas Patil                 asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25267c8c4058STejas Patil                     redfishDateTimeOffset.second;
25277c8c4058STejas Patil 
25285cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Entries"] = {
25295cb1dd27SAsmitha Karunanithi                     {"@odata.id",
25305cb1dd27SAsmitha Karunanithi                      "/redfish/v1/Systems/system/LogServices/Dump/Entries"}};
25315cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Actions"] = {
25325cb1dd27SAsmitha Karunanithi                     {"#LogService.ClearLog",
25337e860f15SJohn Edward Broadbent                      {{"target",
25340fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog"}}},
2535d337bb72SAsmitha Karunanithi                     {"#LogService.CollectDiagnosticData",
25367e860f15SJohn Edward Broadbent                      {{"target",
25370fda0f12SGeorge Liu                        "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData"}}}};
25387e860f15SJohn Edward Broadbent             });
25395cb1dd27SAsmitha Karunanithi }
25405cb1dd27SAsmitha Karunanithi 
25417e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
25427e860f15SJohn Edward Broadbent {
25437e860f15SJohn Edward Broadbent 
25445cb1dd27SAsmitha Karunanithi     /**
25455cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25465cb1dd27SAsmitha Karunanithi      */
2547b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2548ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25497e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25507e860f15SJohn Edward Broadbent             [](const crow::Request&,
2551864d6a17SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25525cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.type"] =
25535cb1dd27SAsmitha Karunanithi                     "#LogEntryCollection.LogEntryCollection";
25545cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["@odata.id"] =
25555cb1dd27SAsmitha Karunanithi                     "/redfish/v1/Systems/system/LogServices/Dump/Entries";
25565cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Name"] = "System Dump Entries";
25575cb1dd27SAsmitha Karunanithi                 asyncResp->res.jsonValue["Description"] =
25585cb1dd27SAsmitha Karunanithi                     "Collection of System Dump Entries";
25595cb1dd27SAsmitha Karunanithi 
25605cb1dd27SAsmitha Karunanithi                 getDumpEntryCollection(asyncResp, "System");
25617e860f15SJohn Edward Broadbent             });
25625cb1dd27SAsmitha Karunanithi }
25635cb1dd27SAsmitha Karunanithi 
25647e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
25655cb1dd27SAsmitha Karunanithi {
25667e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2567864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2568ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2569ed398213SEd Tanous 
25707e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
25717e860f15SJohn Edward Broadbent             [](const crow::Request&,
25727e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25737e860f15SJohn Edward Broadbent                const std::string& param) {
25747e860f15SJohn Edward Broadbent                 getDumpEntryById(asyncResp, param, "System");
25757e860f15SJohn Edward Broadbent             });
25768d1b46d7Szhanghch05 
25777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2578864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2579ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25807e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
25817e860f15SJohn Edward Broadbent             [](const crow::Request&,
25827e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25837e860f15SJohn Edward Broadbent                const std::string& param) {
25847e860f15SJohn Edward Broadbent                 deleteDumpEntry(asyncResp, param, "system");
25857e860f15SJohn Edward Broadbent             });
25865cb1dd27SAsmitha Karunanithi }
2587c9bb6861Sraviteja-b 
25887e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2589c9bb6861Sraviteja-b {
25900fda0f12SGeorge Liu     BMCWEB_ROUTE(
25910fda0f12SGeorge Liu         app,
25920fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2593ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
25947e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
25957e860f15SJohn Edward Broadbent             [](const crow::Request& req,
25967e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
25977e860f15SJohn Edward Broadbent 
25987e860f15SJohn Edward Broadbent             { createDump(asyncResp, req, "System"); });
2599a43be80fSAsmitha Karunanithi }
2600a43be80fSAsmitha Karunanithi 
26017e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2602a43be80fSAsmitha Karunanithi {
26030fda0f12SGeorge Liu     BMCWEB_ROUTE(
26040fda0f12SGeorge Liu         app,
26050fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2606ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
26087e860f15SJohn Edward Broadbent             [](const crow::Request&,
26097e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26107e860f15SJohn Edward Broadbent 
26117e860f15SJohn Edward Broadbent             { clearDump(asyncResp, "System"); });
2612013487e5Sraviteja-b }
2613013487e5Sraviteja-b 
26147e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
26151da66f75SEd Tanous {
26163946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26173946028dSAppaRao Puli     // method for security reasons.
26181da66f75SEd Tanous     /**
26191da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26201da66f75SEd Tanous      */
26217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2622ed398213SEd Tanous         // This is incorrect, should be:
2623ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2624432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
26257e860f15SJohn Edward Broadbent         .methods(
26267e860f15SJohn Edward Broadbent             boost::beast::http::verb::
26277e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
26287e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26297e860f15SJohn Edward Broadbent             // Copy over the static data to include the entries added by
26307e860f15SJohn Edward Broadbent             // SubRoute
26310f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2632424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump";
2633e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
26348e6c099aSJason M. Bills                 "#LogService.v1_2_0.LogService";
26354f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26364f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26374f50ae4bSGunnar Mills             asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2638e1f26343SJason M. Bills             asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2639e1f26343SJason M. Bills             asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
26407c8c4058STejas Patil 
26417c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
26427c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
26437c8c4058STejas Patil             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26447c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26457c8c4058STejas Patil                 redfishDateTimeOffset.second;
26467c8c4058STejas Patil 
2647cd50aa42SJason M. Bills             asyncResp->res.jsonValue["Entries"] = {
2648cd50aa42SJason M. Bills                 {"@odata.id",
2649424c4176SJason M. Bills                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries"}};
2650e1f26343SJason M. Bills             asyncResp->res.jsonValue["Actions"] = {
26515b61b5e8SJason M. Bills                 {"#LogService.ClearLog",
26520fda0f12SGeorge Liu                  {{"target",
26530fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog"}}},
26548e6c099aSJason M. Bills                 {"#LogService.CollectDiagnosticData",
26550fda0f12SGeorge Liu                  {{"target",
26560fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData"}}}};
26577e860f15SJohn Edward Broadbent         });
26581da66f75SEd Tanous }
26591da66f75SEd Tanous 
26607e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
26615b61b5e8SJason M. Bills {
26620fda0f12SGeorge Liu     BMCWEB_ROUTE(
26630fda0f12SGeorge Liu         app,
26640fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2665ed398213SEd Tanous         // This is incorrect, should be:
2666ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2667432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
26697e860f15SJohn Edward Broadbent             [](const crow::Request&,
26707e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26715b61b5e8SJason M. Bills                 crow::connections::systemBus->async_method_call(
26725b61b5e8SJason M. Bills                     [asyncResp](const boost::system::error_code ec,
2673cb13a392SEd Tanous                                 const std::string&) {
26745b61b5e8SJason M. Bills                         if (ec)
26755b61b5e8SJason M. Bills                         {
26765b61b5e8SJason M. Bills                             messages::internalError(asyncResp->res);
26775b61b5e8SJason M. Bills                             return;
26785b61b5e8SJason M. Bills                         }
26795b61b5e8SJason M. Bills                         messages::success(asyncResp->res);
26805b61b5e8SJason M. Bills                     },
26817e860f15SJohn Edward Broadbent                     crashdumpObject, crashdumpPath, deleteAllInterface,
26827e860f15SJohn Edward Broadbent                     "DeleteAll");
26837e860f15SJohn Edward Broadbent             });
26845b61b5e8SJason M. Bills }
26855b61b5e8SJason M. Bills 
26868d1b46d7Szhanghch05 static void
26878d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26888d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2689e855dd28SJason M. Bills {
2690043a0536SJohnathan Mantey     auto getStoredLogCallback =
2691043a0536SJohnathan Mantey         [asyncResp, logID, &logEntryJson](
2692e855dd28SJason M. Bills             const boost::system::error_code ec,
2693168e20c1SEd Tanous             const std::vector<
2694168e20c1SEd Tanous                 std::pair<std::string, dbus::utility::DbusVariantType>>&
2695168e20c1SEd Tanous                 params) {
2696e855dd28SJason M. Bills             if (ec)
2697e855dd28SJason M. Bills             {
2698e855dd28SJason M. Bills                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
26991ddcf01aSJason M. Bills                 if (ec.value() ==
27001ddcf01aSJason M. Bills                     boost::system::linux_error::bad_request_descriptor)
27011ddcf01aSJason M. Bills                 {
2702043a0536SJohnathan Mantey                     messages::resourceNotFound(asyncResp->res, "LogEntry",
2703043a0536SJohnathan Mantey                                                logID);
27041ddcf01aSJason M. Bills                 }
27051ddcf01aSJason M. Bills                 else
27061ddcf01aSJason M. Bills                 {
2707e855dd28SJason M. Bills                     messages::internalError(asyncResp->res);
27081ddcf01aSJason M. Bills                 }
2709e855dd28SJason M. Bills                 return;
2710e855dd28SJason M. Bills             }
2711043a0536SJohnathan Mantey 
2712043a0536SJohnathan Mantey             std::string timestamp{};
2713043a0536SJohnathan Mantey             std::string filename{};
2714043a0536SJohnathan Mantey             std::string logfile{};
27152c70f800SEd Tanous             parseCrashdumpParameters(params, filename, timestamp, logfile);
2716043a0536SJohnathan Mantey 
2717043a0536SJohnathan Mantey             if (filename.empty() || timestamp.empty())
2718e855dd28SJason M. Bills             {
2719043a0536SJohnathan Mantey                 messages::resourceMissingAtURI(asyncResp->res, logID);
2720e855dd28SJason M. Bills                 return;
2721e855dd28SJason M. Bills             }
2722e855dd28SJason M. Bills 
2723043a0536SJohnathan Mantey             std::string crashdumpURI =
2724e855dd28SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2725043a0536SJohnathan Mantey                 logID + "/" + filename;
2726d0dbeefdSEd Tanous             logEntryJson = {{"@odata.type", "#LogEntry.v1_7_0.LogEntry"},
2727043a0536SJohnathan Mantey                             {"@odata.id", "/redfish/v1/Systems/system/"
2728043a0536SJohnathan Mantey                                           "LogServices/Crashdump/Entries/" +
2729e855dd28SJason M. Bills                                               logID},
2730e855dd28SJason M. Bills                             {"Name", "CPU Crashdump"},
2731e855dd28SJason M. Bills                             {"Id", logID},
2732e855dd28SJason M. Bills                             {"EntryType", "Oem"},
27338e6c099aSJason M. Bills                             {"AdditionalDataURI", std::move(crashdumpURI)},
27348e6c099aSJason M. Bills                             {"DiagnosticDataType", "OEM"},
27358e6c099aSJason M. Bills                             {"OEMDiagnosticDataType", "PECICrashdump"},
2736043a0536SJohnathan Mantey                             {"Created", std::move(timestamp)}};
2737e855dd28SJason M. Bills         };
2738e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
27395b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
27405b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2741043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2742e855dd28SJason M. Bills }
2743e855dd28SJason M. Bills 
27447e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
27451da66f75SEd Tanous {
27463946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27473946028dSAppaRao Puli     // method for security reasons.
27481da66f75SEd Tanous     /**
27491da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
27501da66f75SEd Tanous      */
27517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
27527e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2753ed398213SEd Tanous         // This is incorrect, should be.
2754ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2755432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
27567e860f15SJohn Edward Broadbent         .methods(
27577e860f15SJohn Edward Broadbent             boost::beast::http::verb::
27587e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
27597e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27607e860f15SJohn Edward Broadbent             // Collections don't include the static data added by SubRoute
27617e860f15SJohn Edward Broadbent             // because it has a duplicate entry for members
2762e1f26343SJason M. Bills             auto getLogEntriesCallback = [asyncResp](
2763e1f26343SJason M. Bills                                              const boost::system::error_code ec,
27647e860f15SJohn Edward Broadbent                                              const std::vector<std::string>&
27657e860f15SJohn Edward Broadbent                                                  resp) {
27661da66f75SEd Tanous                 if (ec)
27671da66f75SEd Tanous                 {
27681da66f75SEd Tanous                     if (ec.value() !=
27691da66f75SEd Tanous                         boost::system::errc::no_such_file_or_directory)
27701da66f75SEd Tanous                     {
27711da66f75SEd Tanous                         BMCWEB_LOG_DEBUG << "failed to get entries ec: "
27721da66f75SEd Tanous                                          << ec.message();
2773f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
27741da66f75SEd Tanous                         return;
27751da66f75SEd Tanous                     }
27761da66f75SEd Tanous                 }
2777e1f26343SJason M. Bills                 asyncResp->res.jsonValue["@odata.type"] =
27781da66f75SEd Tanous                     "#LogEntryCollection.LogEntryCollection";
27790f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
2780424c4176SJason M. Bills                     "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2781424c4176SJason M. Bills                 asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2782e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Description"] =
2783424c4176SJason M. Bills                     "Collection of Crashdump Entries";
27847e860f15SJohn Edward Broadbent                 nlohmann::json& logEntryArray =
27857e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Members"];
2786e1f26343SJason M. Bills                 logEntryArray = nlohmann::json::array();
2787e855dd28SJason M. Bills                 std::vector<std::string> logIDs;
2788e855dd28SJason M. Bills                 // Get the list of log entries and build up an empty array big
2789e855dd28SJason M. Bills                 // enough to hold them
27901da66f75SEd Tanous                 for (const std::string& objpath : resp)
27911da66f75SEd Tanous                 {
2792e855dd28SJason M. Bills                     // Get the log ID
2793f23b7296SEd Tanous                     std::size_t lastPos = objpath.rfind('/');
2794e855dd28SJason M. Bills                     if (lastPos == std::string::npos)
27951da66f75SEd Tanous                     {
2796e855dd28SJason M. Bills                         continue;
27971da66f75SEd Tanous                     }
2798e855dd28SJason M. Bills                     logIDs.emplace_back(objpath.substr(lastPos + 1));
2799e855dd28SJason M. Bills 
2800e855dd28SJason M. Bills                     // Add a space for the log entry to the array
2801e855dd28SJason M. Bills                     logEntryArray.push_back({});
2802e855dd28SJason M. Bills                 }
2803e855dd28SJason M. Bills                 // Now go through and set up async calls to fill in the entries
2804e855dd28SJason M. Bills                 size_t index = 0;
2805e855dd28SJason M. Bills                 for (const std::string& logID : logIDs)
2806e855dd28SJason M. Bills                 {
2807e855dd28SJason M. Bills                     // Add the log entry to the array
2808e855dd28SJason M. Bills                     logCrashdumpEntry(asyncResp, logID, logEntryArray[index++]);
28091da66f75SEd Tanous                 }
2810e1f26343SJason M. Bills                 asyncResp->res.jsonValue["Members@odata.count"] =
2811e1f26343SJason M. Bills                     logEntryArray.size();
28121da66f75SEd Tanous             };
28131da66f75SEd Tanous             crow::connections::systemBus->async_method_call(
28141da66f75SEd Tanous                 std::move(getLogEntriesCallback),
28151da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper",
28161da66f75SEd Tanous                 "/xyz/openbmc_project/object_mapper",
28171da66f75SEd Tanous                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
28185b61b5e8SJason M. Bills                 std::array<const char*, 1>{crashdumpInterface});
28197e860f15SJohn Edward Broadbent         });
28201da66f75SEd Tanous }
28211da66f75SEd Tanous 
28227e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28231da66f75SEd Tanous {
28243946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28253946028dSAppaRao Puli     // method for security reasons.
28261da66f75SEd Tanous 
28277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28287e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2829ed398213SEd Tanous         // this is incorrect, should be
2830ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2831432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28327e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
28337e860f15SJohn Edward Broadbent             [](const crow::Request&,
28347e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28357e860f15SJohn Edward Broadbent                const std::string& param) {
28367e860f15SJohn Edward Broadbent                 const std::string& logID = param;
2837e855dd28SJason M. Bills                 logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
28387e860f15SJohn Edward Broadbent             });
2839e855dd28SJason M. Bills }
2840e855dd28SJason M. Bills 
28417e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2842e855dd28SJason M. Bills {
28433946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28443946028dSAppaRao Puli     // method for security reasons.
28457e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28467e860f15SJohn Edward Broadbent         app,
28477e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2848ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
28497e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
28507e860f15SJohn Edward Broadbent             [](const crow::Request&,
28517e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28527e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
2853043a0536SJohnathan Mantey                 auto getStoredLogCallback =
2854043a0536SJohnathan Mantey                     [asyncResp, logID, fileName](
2855abf2add6SEd Tanous                         const boost::system::error_code ec,
2856168e20c1SEd Tanous                         const std::vector<std::pair<
2857168e20c1SEd Tanous                             std::string, dbus::utility::DbusVariantType>>&
28587e860f15SJohn Edward Broadbent                             resp) {
28591da66f75SEd Tanous                         if (ec)
28601da66f75SEd Tanous                         {
2861043a0536SJohnathan Mantey                             BMCWEB_LOG_DEBUG << "failed to get log ec: "
2862043a0536SJohnathan Mantey                                              << ec.message();
2863f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
28641da66f75SEd Tanous                             return;
28651da66f75SEd Tanous                         }
2866e855dd28SJason M. Bills 
2867043a0536SJohnathan Mantey                         std::string dbusFilename{};
2868043a0536SJohnathan Mantey                         std::string dbusTimestamp{};
2869043a0536SJohnathan Mantey                         std::string dbusFilepath{};
2870043a0536SJohnathan Mantey 
28717e860f15SJohn Edward Broadbent                         parseCrashdumpParameters(resp, dbusFilename,
28727e860f15SJohn Edward Broadbent                                                  dbusTimestamp, dbusFilepath);
2873043a0536SJohnathan Mantey 
2874043a0536SJohnathan Mantey                         if (dbusFilename.empty() || dbusTimestamp.empty() ||
2875043a0536SJohnathan Mantey                             dbusFilepath.empty())
28761da66f75SEd Tanous                         {
28777e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28787e860f15SJohn Edward Broadbent                                                            fileName);
28791da66f75SEd Tanous                             return;
28801da66f75SEd Tanous                         }
2881e855dd28SJason M. Bills 
2882043a0536SJohnathan Mantey                         // Verify the file name parameter is correct
2883043a0536SJohnathan Mantey                         if (fileName != dbusFilename)
2884043a0536SJohnathan Mantey                         {
28857e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28867e860f15SJohn Edward Broadbent                                                            fileName);
2887043a0536SJohnathan Mantey                             return;
2888043a0536SJohnathan Mantey                         }
2889043a0536SJohnathan Mantey 
2890043a0536SJohnathan Mantey                         if (!std::filesystem::exists(dbusFilepath))
2891043a0536SJohnathan Mantey                         {
28927e860f15SJohn Edward Broadbent                             messages::resourceMissingAtURI(asyncResp->res,
28937e860f15SJohn Edward Broadbent                                                            fileName);
2894043a0536SJohnathan Mantey                             return;
2895043a0536SJohnathan Mantey                         }
28962d31491bSJason M. Bills                         std::ifstream ifs(dbusFilepath,
28972d31491bSJason M. Bills                                           std::ios::in | std::ios::binary);
28982d31491bSJason M. Bills                         asyncResp->res.body() = std::string(
28992d31491bSJason M. Bills                             std::istreambuf_iterator<char>{ifs}, {});
2900043a0536SJohnathan Mantey 
29017e860f15SJohn Edward Broadbent                         // Configure this to be a file download when accessed
29027e860f15SJohn Edward Broadbent                         // from a browser
29037e860f15SJohn Edward Broadbent                         asyncResp->res.addHeader("Content-Disposition",
29047e860f15SJohn Edward Broadbent                                                  "attachment");
29051da66f75SEd Tanous                     };
29061da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
29075b61b5e8SJason M. Bills                     std::move(getStoredLogCallback), crashdumpObject,
29085b61b5e8SJason M. Bills                     crashdumpPath + std::string("/") + logID,
29097e860f15SJohn Edward Broadbent                     "org.freedesktop.DBus.Properties", "GetAll",
29107e860f15SJohn Edward Broadbent                     crashdumpInterface);
29117e860f15SJohn Edward Broadbent             });
29121da66f75SEd Tanous }
29131da66f75SEd Tanous 
29147e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
29151da66f75SEd Tanous {
29163946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29173946028dSAppaRao Puli     // method for security reasons.
29180fda0f12SGeorge Liu     BMCWEB_ROUTE(
29190fda0f12SGeorge Liu         app,
29200fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2921ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2922ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2923432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
29247e860f15SJohn Edward Broadbent         .methods(
29257e860f15SJohn Edward Broadbent             boost::beast::http::verb::
29267e860f15SJohn Edward Broadbent                 post)([](const crow::Request& req,
29277e860f15SJohn Edward Broadbent                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
29288e6c099aSJason M. Bills             std::string diagnosticDataType;
29298e6c099aSJason M. Bills             std::string oemDiagnosticDataType;
29308e6c099aSJason M. Bills             if (!redfish::json_util::readJson(
29317e860f15SJohn Edward Broadbent                     req, asyncResp->res, "DiagnosticDataType",
29327e860f15SJohn Edward Broadbent                     diagnosticDataType, "OEMDiagnosticDataType",
29337e860f15SJohn Edward Broadbent                     oemDiagnosticDataType))
29348e6c099aSJason M. Bills             {
29358e6c099aSJason M. Bills                 return;
29368e6c099aSJason M. Bills             }
29378e6c099aSJason M. Bills 
29388e6c099aSJason M. Bills             if (diagnosticDataType != "OEM")
29398e6c099aSJason M. Bills             {
29408e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR
29418e6c099aSJason M. Bills                     << "Only OEM DiagnosticDataType supported for Crashdump";
29428e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
29438e6c099aSJason M. Bills                     asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29448e6c099aSJason M. Bills                     "CollectDiagnosticData");
29458e6c099aSJason M. Bills                 return;
29468e6c099aSJason M. Bills             }
29478e6c099aSJason M. Bills 
294898be3e39SEd Tanous             auto collectCrashdumpCallback = [asyncResp,
294998be3e39SEd Tanous                                              payload(task::Payload(req))](
29507e860f15SJohn Edward Broadbent                                                 const boost::system::error_code
29517e860f15SJohn Edward Broadbent                                                     ec,
295298be3e39SEd Tanous                                                 const std::string&) mutable {
29531da66f75SEd Tanous                 if (ec)
29541da66f75SEd Tanous                 {
29557e860f15SJohn Edward Broadbent                     if (ec.value() ==
29567e860f15SJohn Edward Broadbent                         boost::system::errc::operation_not_supported)
29571da66f75SEd Tanous                     {
2958f12894f8SJason M. Bills                         messages::resourceInStandby(asyncResp->res);
29591da66f75SEd Tanous                     }
29604363d3b2SJason M. Bills                     else if (ec.value() ==
29614363d3b2SJason M. Bills                              boost::system::errc::device_or_resource_busy)
29624363d3b2SJason M. Bills                     {
29634363d3b2SJason M. Bills                         messages::serviceTemporarilyUnavailable(asyncResp->res,
29644363d3b2SJason M. Bills                                                                 "60");
29654363d3b2SJason M. Bills                     }
29661da66f75SEd Tanous                     else
29671da66f75SEd Tanous                     {
2968f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
29691da66f75SEd Tanous                     }
29701da66f75SEd Tanous                     return;
29711da66f75SEd Tanous                 }
29720fda0f12SGeorge Liu                 std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
29737e860f15SJohn Edward Broadbent                     [](boost::system::error_code err,
29747e860f15SJohn Edward Broadbent                        sdbusplus::message::message&,
297566afe4faSJames Feist                        const std::shared_ptr<task::TaskData>& taskData) {
297666afe4faSJames Feist                         if (!err)
297766afe4faSJames Feist                         {
2978e5d5006bSJames Feist                             taskData->messages.emplace_back(
2979e5d5006bSJames Feist                                 messages::taskCompletedOK(
2980e5d5006bSJames Feist                                     std::to_string(taskData->index)));
2981831d6b09SJames Feist                             taskData->state = "Completed";
298266afe4faSJames Feist                         }
298332898ceaSJames Feist                         return task::completed;
298466afe4faSJames Feist                     },
29857e860f15SJohn Edward Broadbent                     "type='signal',interface='org.freedesktop.DBus."
29867e860f15SJohn Edward Broadbent                     "Properties',"
29870fda0f12SGeorge Liu                     "member='PropertiesChanged',arg0namespace='com.intel.crashdump'");
298846229577SJames Feist                 task->startTimer(std::chrono::minutes(5));
298946229577SJames Feist                 task->populateResp(asyncResp->res);
299098be3e39SEd Tanous                 task->payload.emplace(std::move(payload));
29911da66f75SEd Tanous             };
29928e6c099aSJason M. Bills 
29938e6c099aSJason M. Bills             if (oemDiagnosticDataType == "OnDemand")
29948e6c099aSJason M. Bills             {
29951da66f75SEd Tanous                 crow::connections::systemBus->async_method_call(
29968e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
29978e6c099aSJason M. Bills                     crashdumpPath, crashdumpOnDemandInterface,
29988e6c099aSJason M. Bills                     "GenerateOnDemandLog");
29991da66f75SEd Tanous             }
30008e6c099aSJason M. Bills             else if (oemDiagnosticDataType == "Telemetry")
30016eda7685SKenny L. Ku             {
30028e6c099aSJason M. Bills                 crow::connections::systemBus->async_method_call(
30038e6c099aSJason M. Bills                     std::move(collectCrashdumpCallback), crashdumpObject,
30048e6c099aSJason M. Bills                     crashdumpPath, crashdumpTelemetryInterface,
30058e6c099aSJason M. Bills                     "GenerateTelemetryLog");
30066eda7685SKenny L. Ku             }
30076eda7685SKenny L. Ku             else
30086eda7685SKenny L. Ku             {
30098e6c099aSJason M. Bills                 BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
30108e6c099aSJason M. Bills                                  << oemDiagnosticDataType;
30118e6c099aSJason M. Bills                 messages::actionParameterValueFormatError(
30127e860f15SJohn Edward Broadbent                     asyncResp->res, oemDiagnosticDataType,
30137e860f15SJohn Edward Broadbent                     "OEMDiagnosticDataType", "CollectDiagnosticData");
30146eda7685SKenny L. Ku                 return;
30156eda7685SKenny L. Ku             }
30167e860f15SJohn Edward Broadbent         });
30176eda7685SKenny L. Ku }
30186eda7685SKenny L. Ku 
3019cb92c03bSAndrew Geissler /**
3020cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3021cb92c03bSAndrew Geissler  */
30227e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3023cb92c03bSAndrew Geissler {
3024cb92c03bSAndrew Geissler     /**
3025cb92c03bSAndrew Geissler      * Function handles POST method request.
3026cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3027cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3028cb92c03bSAndrew Geissler      */
30297e860f15SJohn Edward Broadbent 
30300fda0f12SGeorge Liu     BMCWEB_ROUTE(
30310fda0f12SGeorge Liu         app,
30320fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3033ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
30347e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
30357e860f15SJohn Edward Broadbent             [](const crow::Request&,
30367e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3037cb92c03bSAndrew Geissler                 BMCWEB_LOG_DEBUG << "Do delete all entries.";
3038cb92c03bSAndrew Geissler 
3039cb92c03bSAndrew Geissler                 // Process response from Logging service.
30407e860f15SJohn Edward Broadbent                 auto respHandler = [asyncResp](
30417e860f15SJohn Edward Broadbent                                        const boost::system::error_code ec) {
30427e860f15SJohn Edward Broadbent                     BMCWEB_LOG_DEBUG
30437e860f15SJohn Edward Broadbent                         << "doClearLog resp_handler callback: Done";
3044cb92c03bSAndrew Geissler                     if (ec)
3045cb92c03bSAndrew Geissler                     {
3046cb92c03bSAndrew Geissler                         // TODO Handle for specific error code
30477e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "doClearLog resp_handler got error "
30487e860f15SJohn Edward Broadbent                                          << ec;
3049cb92c03bSAndrew Geissler                         asyncResp->res.result(
3050cb92c03bSAndrew Geissler                             boost::beast::http::status::internal_server_error);
3051cb92c03bSAndrew Geissler                         return;
3052cb92c03bSAndrew Geissler                     }
3053cb92c03bSAndrew Geissler 
30547e860f15SJohn Edward Broadbent                     asyncResp->res.result(
30557e860f15SJohn Edward Broadbent                         boost::beast::http::status::no_content);
3056cb92c03bSAndrew Geissler                 };
3057cb92c03bSAndrew Geissler 
3058cb92c03bSAndrew Geissler                 // Make call to Logging service to request Clear Log
3059cb92c03bSAndrew Geissler                 crow::connections::systemBus->async_method_call(
30602c70f800SEd Tanous                     respHandler, "xyz.openbmc_project.Logging",
3061cb92c03bSAndrew Geissler                     "/xyz/openbmc_project/logging",
3062cb92c03bSAndrew Geissler                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
30637e860f15SJohn Edward Broadbent             });
3064cb92c03bSAndrew Geissler }
3065a3316fc6SZhikuiRen 
3066a3316fc6SZhikuiRen /****************************************************
3067a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3068a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3069a3316fc6SZhikuiRen  ******************************************************/
30707e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3071a3316fc6SZhikuiRen {
30727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3073ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
30740fda0f12SGeorge Liu         .methods(
30750fda0f12SGeorge Liu             boost::beast::http::verb::
30760fda0f12SGeorge Liu                 get)([](const crow::Request&,
30777e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3078a3316fc6SZhikuiRen             asyncResp->res.jsonValue = {
30797e860f15SJohn Edward Broadbent                 {"@odata.id",
30807e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes"},
3081a3316fc6SZhikuiRen                 {"@odata.type", "#LogService.v1_1_0.LogService"},
3082a3316fc6SZhikuiRen                 {"Name", "POST Code Log Service"},
3083a3316fc6SZhikuiRen                 {"Description", "POST Code Log Service"},
3084a3316fc6SZhikuiRen                 {"Id", "BIOS POST Code Log"},
3085a3316fc6SZhikuiRen                 {"OverWritePolicy", "WrapsWhenFull"},
3086a3316fc6SZhikuiRen                 {"Entries",
30870fda0f12SGeorge Liu                  {{"@odata.id",
30880fda0f12SGeorge Liu                    "/redfish/v1/Systems/system/LogServices/PostCodes/Entries"}}}};
30897c8c4058STejas Patil 
30907c8c4058STejas Patil             std::pair<std::string, std::string> redfishDateTimeOffset =
30917c8c4058STejas Patil                 crow::utility::getDateTimeOffsetNow();
30920fda0f12SGeorge Liu             asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
30937c8c4058STejas Patil             asyncResp->res.jsonValue["DateTimeLocalOffset"] =
30947c8c4058STejas Patil                 redfishDateTimeOffset.second;
30957c8c4058STejas Patil 
3096a3316fc6SZhikuiRen             asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
30977e860f15SJohn Edward Broadbent                 {"target",
30980fda0f12SGeorge Liu                  "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
30997e860f15SJohn Edward Broadbent         });
3100a3316fc6SZhikuiRen }
3101a3316fc6SZhikuiRen 
31027e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3103a3316fc6SZhikuiRen {
31040fda0f12SGeorge Liu     BMCWEB_ROUTE(
31050fda0f12SGeorge Liu         app,
31060fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3107ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3108ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3109432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
31107e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
31117e860f15SJohn Edward Broadbent             [](const crow::Request&,
31127e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3113a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3114a3316fc6SZhikuiRen 
3115a3316fc6SZhikuiRen                 // Make call to post-code service to request clear all
3116a3316fc6SZhikuiRen                 crow::connections::systemBus->async_method_call(
3117a3316fc6SZhikuiRen                     [asyncResp](const boost::system::error_code ec) {
3118a3316fc6SZhikuiRen                         if (ec)
3119a3316fc6SZhikuiRen                         {
3120a3316fc6SZhikuiRen                             // TODO Handle for specific error code
3121a3316fc6SZhikuiRen                             BMCWEB_LOG_ERROR
31227e860f15SJohn Edward Broadbent                                 << "doClearPostCodes resp_handler got error "
31237e860f15SJohn Edward Broadbent                                 << ec;
31247e860f15SJohn Edward Broadbent                             asyncResp->res.result(boost::beast::http::status::
31257e860f15SJohn Edward Broadbent                                                       internal_server_error);
3126a3316fc6SZhikuiRen                             messages::internalError(asyncResp->res);
3127a3316fc6SZhikuiRen                             return;
3128a3316fc6SZhikuiRen                         }
3129a3316fc6SZhikuiRen                     },
313015124765SJonathan Doman                     "xyz.openbmc_project.State.Boot.PostCode0",
313115124765SJonathan Doman                     "/xyz/openbmc_project/State/Boot/PostCode0",
3132a3316fc6SZhikuiRen                     "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31337e860f15SJohn Edward Broadbent             });
3134a3316fc6SZhikuiRen }
3135a3316fc6SZhikuiRen 
3136a3316fc6SZhikuiRen static void fillPostCodeEntry(
31378d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
31386c9a279eSManojkiran Eda     const boost::container::flat_map<
31396c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3140a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3141a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3142a3316fc6SZhikuiRen {
3143a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3144a3316fc6SZhikuiRen     const message_registries::Message* message =
31454a0bf539SManojkiran Eda         message_registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3146a3316fc6SZhikuiRen 
3147a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3148a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3149a3316fc6SZhikuiRen 
3150a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
31516c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
31526c9a279eSManojkiran Eda              code : postcode)
3153a3316fc6SZhikuiRen     {
3154a3316fc6SZhikuiRen         currentCodeIndex++;
3155a3316fc6SZhikuiRen         std::string postcodeEntryID =
3156a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3157a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3158a3316fc6SZhikuiRen 
3159a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3160a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3161a3316fc6SZhikuiRen 
3162a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3163a3316fc6SZhikuiRen         { // already incremented
3164a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3165a3316fc6SZhikuiRen         }
3166a3316fc6SZhikuiRen         else
3167a3316fc6SZhikuiRen         {
3168a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3169a3316fc6SZhikuiRen         }
3170a3316fc6SZhikuiRen 
3171a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3172a3316fc6SZhikuiRen         // not fall between top and skip
3173a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3174a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3175a3316fc6SZhikuiRen         {
3176a3316fc6SZhikuiRen             continue;
3177a3316fc6SZhikuiRen         }
3178a3316fc6SZhikuiRen 
31794e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3180a3316fc6SZhikuiRen         // currentIndex
3181a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3182a3316fc6SZhikuiRen         {
3183a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3184a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3185a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3186a3316fc6SZhikuiRen             continue;
3187a3316fc6SZhikuiRen         }
3188a3316fc6SZhikuiRen 
3189a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3190a3316fc6SZhikuiRen         // index
3191a3316fc6SZhikuiRen 
3192a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3193a3316fc6SZhikuiRen         std::string entryTimeStr;
31941d8782e7SNan Zhou         entryTimeStr =
31951d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3196a3316fc6SZhikuiRen 
3197a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3198a3316fc6SZhikuiRen         std::ostringstream hexCode;
3199a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32006c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3201a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3202a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3203a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3204a3316fc6SZhikuiRen         // Set precision to 4 digits
3205a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3206a3316fc6SZhikuiRen         // Add double to stream
3207a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3208a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3209a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3210a3316fc6SZhikuiRen 
3211a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3212a3316fc6SZhikuiRen         std::string msg;
3213a3316fc6SZhikuiRen         if (message != nullptr)
3214a3316fc6SZhikuiRen         {
3215a3316fc6SZhikuiRen             msg = message->message;
3216a3316fc6SZhikuiRen 
3217a3316fc6SZhikuiRen             // fill in this post code value
3218a3316fc6SZhikuiRen             int i = 0;
3219a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3220a3316fc6SZhikuiRen             {
3221a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3222a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3223a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3224a3316fc6SZhikuiRen                 {
3225a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3226a3316fc6SZhikuiRen                 }
3227a3316fc6SZhikuiRen             }
3228a3316fc6SZhikuiRen         }
3229a3316fc6SZhikuiRen 
3230d4342a92STim Lee         // Get Severity template from message registry
3231d4342a92STim Lee         std::string severity;
3232d4342a92STim Lee         if (message != nullptr)
3233d4342a92STim Lee         {
3234d4342a92STim Lee             severity = message->severity;
3235d4342a92STim Lee         }
3236d4342a92STim Lee 
3237a3316fc6SZhikuiRen         // add to AsyncResp
3238a3316fc6SZhikuiRen         logEntryArray.push_back({});
3239a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
32400fda0f12SGeorge Liu         bmcLogEntry = {
32410fda0f12SGeorge Liu             {"@odata.type", "#LogEntry.v1_8_0.LogEntry"},
32420fda0f12SGeorge Liu             {"@odata.id",
32430fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3244a3316fc6SZhikuiRen                  postcodeEntryID},
3245a3316fc6SZhikuiRen             {"Name", "POST Code Log Entry"},
3246a3316fc6SZhikuiRen             {"Id", postcodeEntryID},
3247a3316fc6SZhikuiRen             {"Message", std::move(msg)},
32484a0bf539SManojkiran Eda             {"MessageId", "OpenBMC.0.2.BIOSPOSTCode"},
3249a3316fc6SZhikuiRen             {"MessageArgs", std::move(messageArgs)},
3250a3316fc6SZhikuiRen             {"EntryType", "Event"},
3251a3316fc6SZhikuiRen             {"Severity", std::move(severity)},
32529c620e21SAsmitha Karunanithi             {"Created", entryTimeStr}};
3253647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3254647b3cdcSGeorge Liu         {
3255647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3256647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3257647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3258647b3cdcSGeorge Liu         }
3259a3316fc6SZhikuiRen     }
3260a3316fc6SZhikuiRen }
3261a3316fc6SZhikuiRen 
32628d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3263a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3264a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3265a3316fc6SZhikuiRen {
3266a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
32676c9a279eSManojkiran Eda         [aResp, bootIndex,
32686c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
32696c9a279eSManojkiran Eda                     const boost::container::flat_map<
32706c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32716c9a279eSManojkiran Eda                         postcode) {
3272a3316fc6SZhikuiRen             if (ec)
3273a3316fc6SZhikuiRen             {
3274a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3275a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3276a3316fc6SZhikuiRen                 return;
3277a3316fc6SZhikuiRen             }
3278a3316fc6SZhikuiRen 
3279a3316fc6SZhikuiRen             // skip the empty postcode boots
3280a3316fc6SZhikuiRen             if (postcode.empty())
3281a3316fc6SZhikuiRen             {
3282a3316fc6SZhikuiRen                 return;
3283a3316fc6SZhikuiRen             }
3284a3316fc6SZhikuiRen 
3285a3316fc6SZhikuiRen             fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3286a3316fc6SZhikuiRen 
3287a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] =
3288a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members"].size();
3289a3316fc6SZhikuiRen         },
329015124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
329115124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3292a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3293a3316fc6SZhikuiRen         bootIndex);
3294a3316fc6SZhikuiRen }
3295a3316fc6SZhikuiRen 
32968d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3297a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3298a3316fc6SZhikuiRen                                const uint16_t bootCount,
3299a3316fc6SZhikuiRen                                const uint64_t entryCount, const uint64_t skip,
3300a3316fc6SZhikuiRen                                const uint64_t top)
3301a3316fc6SZhikuiRen {
3302a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3303a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3304a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
33056c9a279eSManojkiran Eda               const boost::container::flat_map<
33066c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33076c9a279eSManojkiran Eda                   postcode) {
3308a3316fc6SZhikuiRen             if (ec)
3309a3316fc6SZhikuiRen             {
3310a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3311a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3312a3316fc6SZhikuiRen                 return;
3313a3316fc6SZhikuiRen             }
3314a3316fc6SZhikuiRen 
3315a3316fc6SZhikuiRen             uint64_t endCount = entryCount;
3316a3316fc6SZhikuiRen             if (!postcode.empty())
3317a3316fc6SZhikuiRen             {
3318a3316fc6SZhikuiRen                 endCount = entryCount + postcode.size();
3319a3316fc6SZhikuiRen 
3320a3316fc6SZhikuiRen                 if ((skip < endCount) && ((top + skip) > entryCount))
3321a3316fc6SZhikuiRen                 {
3322a3316fc6SZhikuiRen                     uint64_t thisBootSkip =
3323a3316fc6SZhikuiRen                         std::max(skip, entryCount) - entryCount;
3324a3316fc6SZhikuiRen                     uint64_t thisBootTop =
3325a3316fc6SZhikuiRen                         std::min(top + skip, endCount) - entryCount;
3326a3316fc6SZhikuiRen 
3327a3316fc6SZhikuiRen                     fillPostCodeEntry(aResp, postcode, bootIndex, 0,
3328a3316fc6SZhikuiRen                                       thisBootSkip, thisBootTop);
3329a3316fc6SZhikuiRen                 }
3330a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.count"] = endCount;
3331a3316fc6SZhikuiRen             }
3332a3316fc6SZhikuiRen 
3333a3316fc6SZhikuiRen             // continue to previous bootIndex
3334a3316fc6SZhikuiRen             if (bootIndex < bootCount)
3335a3316fc6SZhikuiRen             {
3336a3316fc6SZhikuiRen                 getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3337a3316fc6SZhikuiRen                                    bootCount, endCount, skip, top);
3338a3316fc6SZhikuiRen             }
3339a3316fc6SZhikuiRen             else
3340a3316fc6SZhikuiRen             {
3341a3316fc6SZhikuiRen                 aResp->res.jsonValue["Members@odata.nextLink"] =
33420fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3343a3316fc6SZhikuiRen                     std::to_string(skip + top);
3344a3316fc6SZhikuiRen             }
3345a3316fc6SZhikuiRen         },
334615124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
334715124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3348a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3349a3316fc6SZhikuiRen         bootIndex);
3350a3316fc6SZhikuiRen }
3351a3316fc6SZhikuiRen 
33528d1b46d7Szhanghch05 static void
33538d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3354a3316fc6SZhikuiRen                          const uint64_t skip, const uint64_t top)
3355a3316fc6SZhikuiRen {
3356a3316fc6SZhikuiRen     uint64_t entryCount = 0;
33571e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
33581e1e598dSJonathan Doman         *crow::connections::systemBus,
33591e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
33601e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
33611e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
33621e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
33631e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3364a3316fc6SZhikuiRen             if (ec)
3365a3316fc6SZhikuiRen             {
3366a3316fc6SZhikuiRen                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3367a3316fc6SZhikuiRen                 messages::internalError(aResp->res);
3368a3316fc6SZhikuiRen                 return;
3369a3316fc6SZhikuiRen             }
33701e1e598dSJonathan Doman             getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
33711e1e598dSJonathan Doman         });
3372a3316fc6SZhikuiRen }
3373a3316fc6SZhikuiRen 
33747e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3375a3316fc6SZhikuiRen {
33767e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
33777e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3378ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
33797e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
33807e860f15SJohn Edward Broadbent             [](const crow::Request& req,
33817e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3382a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.type"] =
3383a3316fc6SZhikuiRen                     "#LogEntryCollection.LogEntryCollection";
3384a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
3385a3316fc6SZhikuiRen                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3386a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3387a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3388a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3389a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3390a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3391a3316fc6SZhikuiRen 
3392a3316fc6SZhikuiRen                 uint64_t skip = 0;
3393a3316fc6SZhikuiRen                 uint64_t top = maxEntriesPerPage; // Show max entries by default
33948d1b46d7Szhanghch05                 if (!getSkipParam(asyncResp, req, skip))
3395a3316fc6SZhikuiRen                 {
3396a3316fc6SZhikuiRen                     return;
3397a3316fc6SZhikuiRen                 }
33988d1b46d7Szhanghch05                 if (!getTopParam(asyncResp, req, top))
3399a3316fc6SZhikuiRen                 {
3400a3316fc6SZhikuiRen                     return;
3401a3316fc6SZhikuiRen                 }
3402a3316fc6SZhikuiRen                 getCurrentBootNumber(asyncResp, skip, top);
34037e860f15SJohn Edward Broadbent             });
3404a3316fc6SZhikuiRen }
3405a3316fc6SZhikuiRen 
3406647b3cdcSGeorge Liu /**
3407647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3408647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3409647b3cdcSGeorge Liu  *
3410647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3411647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3412647b3cdcSGeorge Liu  * @param[out] index          Index value
3413647b3cdcSGeorge Liu  *
3414647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3415647b3cdcSGeorge Liu  */
3416647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3417647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3418647b3cdcSGeorge Liu {
3419647b3cdcSGeorge Liu     std::vector<std::string> split;
3420647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3421647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3422647b3cdcSGeorge Liu     {
3423647b3cdcSGeorge Liu         return false;
3424647b3cdcSGeorge Liu     }
3425647b3cdcSGeorge Liu 
3426ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3427647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3428ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3429647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3430647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3431647b3cdcSGeorge Liu 
3432647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3433647b3cdcSGeorge Liu     {
3434647b3cdcSGeorge Liu         return false;
3435647b3cdcSGeorge Liu     }
3436647b3cdcSGeorge Liu 
3437647b3cdcSGeorge Liu     start = split[1].data();
3438ca45aa3cSEd Tanous 
3439ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3440647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3441647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3442647b3cdcSGeorge Liu     if (ptrValue != end || ecValue != std::errc())
3443647b3cdcSGeorge Liu     {
3444647b3cdcSGeorge Liu         return false;
3445647b3cdcSGeorge Liu     }
3446647b3cdcSGeorge Liu 
3447647b3cdcSGeorge Liu     return true;
3448647b3cdcSGeorge Liu }
3449647b3cdcSGeorge Liu 
3450647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3451647b3cdcSGeorge Liu {
34520fda0f12SGeorge Liu     BMCWEB_ROUTE(
34530fda0f12SGeorge Liu         app,
34540fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3455647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3456647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
3457647b3cdcSGeorge Liu             [](const crow::Request& req,
3458647b3cdcSGeorge Liu                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3459647b3cdcSGeorge Liu                const std::string& postCodeID) {
3460647b3cdcSGeorge Liu                 if (!http_helpers::isOctetAccepted(
3461647b3cdcSGeorge Liu                         req.getHeaderValue("Accept")))
3462647b3cdcSGeorge Liu                 {
3463647b3cdcSGeorge Liu                     asyncResp->res.result(
3464647b3cdcSGeorge Liu                         boost::beast::http::status::bad_request);
3465647b3cdcSGeorge Liu                     return;
3466647b3cdcSGeorge Liu                 }
3467647b3cdcSGeorge Liu 
3468647b3cdcSGeorge Liu                 uint64_t currentValue = 0;
3469647b3cdcSGeorge Liu                 uint16_t index = 0;
3470647b3cdcSGeorge Liu                 if (!parsePostCode(postCodeID, currentValue, index))
3471647b3cdcSGeorge Liu                 {
3472647b3cdcSGeorge Liu                     messages::resourceNotFound(asyncResp->res, "LogEntry",
3473647b3cdcSGeorge Liu                                                postCodeID);
3474647b3cdcSGeorge Liu                     return;
3475647b3cdcSGeorge Liu                 }
3476647b3cdcSGeorge Liu 
3477647b3cdcSGeorge Liu                 crow::connections::systemBus->async_method_call(
3478647b3cdcSGeorge Liu                     [asyncResp, postCodeID, currentValue](
3479647b3cdcSGeorge Liu                         const boost::system::error_code ec,
3480647b3cdcSGeorge Liu                         const std::vector<std::tuple<
3481647b3cdcSGeorge Liu                             uint64_t, std::vector<uint8_t>>>& postcodes) {
3482647b3cdcSGeorge Liu                         if (ec.value() == EBADR)
3483647b3cdcSGeorge Liu                         {
3484647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3485647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3486647b3cdcSGeorge Liu                             return;
3487647b3cdcSGeorge Liu                         }
3488647b3cdcSGeorge Liu                         if (ec)
3489647b3cdcSGeorge Liu                         {
3490647b3cdcSGeorge Liu                             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3491647b3cdcSGeorge Liu                             messages::internalError(asyncResp->res);
3492647b3cdcSGeorge Liu                             return;
3493647b3cdcSGeorge Liu                         }
3494647b3cdcSGeorge Liu 
3495647b3cdcSGeorge Liu                         size_t value = static_cast<size_t>(currentValue) - 1;
3496647b3cdcSGeorge Liu                         if (value == std::string::npos ||
3497647b3cdcSGeorge Liu                             postcodes.size() < currentValue)
3498647b3cdcSGeorge Liu                         {
3499647b3cdcSGeorge Liu                             BMCWEB_LOG_ERROR << "Wrong currentValue value";
3500647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3501647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3502647b3cdcSGeorge Liu                             return;
3503647b3cdcSGeorge Liu                         }
3504647b3cdcSGeorge Liu 
350546ff87baSEd Tanous                         auto& [tID, c] = postcodes[value];
350646ff87baSEd Tanous                         if (c.empty())
3507647b3cdcSGeorge Liu                         {
3508647b3cdcSGeorge Liu                             BMCWEB_LOG_INFO << "No found post code data";
3509647b3cdcSGeorge Liu                             messages::resourceNotFound(asyncResp->res,
3510647b3cdcSGeorge Liu                                                        "LogEntry", postCodeID);
3511647b3cdcSGeorge Liu                             return;
3512647b3cdcSGeorge Liu                         }
351346ff87baSEd Tanous                         // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
351446ff87baSEd Tanous                         const char* d = reinterpret_cast<const char*>(c.data());
351546ff87baSEd Tanous                         std::string_view strData(d, c.size());
3516647b3cdcSGeorge Liu 
3517647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Type",
3518647b3cdcSGeorge Liu                                                  "application/octet-stream");
3519647b3cdcSGeorge Liu                         asyncResp->res.addHeader("Content-Transfer-Encoding",
3520647b3cdcSGeorge Liu                                                  "Base64");
3521647b3cdcSGeorge Liu                         asyncResp->res.body() =
3522647b3cdcSGeorge Liu                             crow::utility::base64encode(strData);
3523647b3cdcSGeorge Liu                     },
3524647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode0",
3525647b3cdcSGeorge Liu                     "/xyz/openbmc_project/State/Boot/PostCode0",
3526647b3cdcSGeorge Liu                     "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes",
3527647b3cdcSGeorge Liu                     index);
3528647b3cdcSGeorge Liu             });
3529647b3cdcSGeorge Liu }
3530647b3cdcSGeorge Liu 
35317e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3532a3316fc6SZhikuiRen {
35337e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
35347e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3535ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
35367e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
35377e860f15SJohn Edward Broadbent             [](const crow::Request&,
35387e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35397e860f15SJohn Edward Broadbent                const std::string& targetID) {
3540647b3cdcSGeorge Liu                 uint16_t bootIndex = 0;
3541647b3cdcSGeorge Liu                 uint64_t codeIndex = 0;
3542647b3cdcSGeorge Liu                 if (!parsePostCode(targetID, codeIndex, bootIndex))
3543a3316fc6SZhikuiRen                 {
3544a3316fc6SZhikuiRen                     // Requested ID was not found
3545a3316fc6SZhikuiRen                     messages::resourceMissingAtURI(asyncResp->res, targetID);
3546a3316fc6SZhikuiRen                     return;
3547a3316fc6SZhikuiRen                 }
3548a3316fc6SZhikuiRen                 if (bootIndex == 0 || codeIndex == 0)
3549a3316fc6SZhikuiRen                 {
3550a3316fc6SZhikuiRen                     BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
35517e860f15SJohn Edward Broadbent                                      << targetID;
3552a3316fc6SZhikuiRen                 }
3553a3316fc6SZhikuiRen 
35547e860f15SJohn Edward Broadbent                 asyncResp->res.jsonValue["@odata.type"] =
35557e860f15SJohn Edward Broadbent                     "#LogEntry.v1_4_0.LogEntry";
3556a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["@odata.id"] =
35570fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3558a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3559a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Description"] =
3560a3316fc6SZhikuiRen                     "Collection of POST Code Log Entries";
3561a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3562a3316fc6SZhikuiRen                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
3563a3316fc6SZhikuiRen 
3564a3316fc6SZhikuiRen                 getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
35657e860f15SJohn Edward Broadbent             });
3566a3316fc6SZhikuiRen }
3567a3316fc6SZhikuiRen 
35681da66f75SEd Tanous } // namespace redfish
3569