xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision 9896eaed88332eca7334a564a2ea2e0baf135639)
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>
30*9896eaedSEd Tanous #include <boost/algorithm/string/case_conv.hpp>
3111ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
32400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
334851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
3407c8c20dSEd Tanous #include <boost/beast/http/verb.hpp>
351da66f75SEd Tanous #include <boost/container/flat_map.hpp>
361ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
37168e20c1SEd Tanous #include <dbus_utility.hpp>
38cb92c03bSAndrew Geissler #include <error_messages.hpp>
3945ca1b86SEd Tanous #include <query.hpp>
40ed398213SEd Tanous #include <registries/privilege_registry.hpp>
411214b7e7SGunnar Mills 
42647b3cdcSGeorge Liu #include <charconv>
434418c7f0SJames Feist #include <filesystem>
4475710de2SXiaochao Ma #include <optional>
4526702d01SEd Tanous #include <span>
46cd225da8SJason M. Bills #include <string_view>
47abf2add6SEd Tanous #include <variant>
481da66f75SEd Tanous 
491da66f75SEd Tanous namespace redfish
501da66f75SEd Tanous {
511da66f75SEd Tanous 
525b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
535b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
545b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
555b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
565b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
575b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
58424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
596eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
606eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
611da66f75SEd Tanous 
62fffb8c1fSEd Tanous namespace registries
634851d45dSJason M. Bills {
6426702d01SEd Tanous static const Message*
6526702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
6626702d01SEd Tanous                            const std::span<const MessageEntry> registry)
674851d45dSJason M. Bills {
68002d39b4SEd Tanous     std::span<const MessageEntry>::iterator messageIt =
69002d39b4SEd Tanous         std::find_if(registry.begin(), registry.end(),
704851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
71e662eae8SEd Tanous         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
724851d45dSJason M. Bills         });
7326702d01SEd Tanous     if (messageIt != registry.end())
744851d45dSJason M. Bills     {
754851d45dSJason M. Bills         return &messageIt->second;
764851d45dSJason M. Bills     }
774851d45dSJason M. Bills 
784851d45dSJason M. Bills     return nullptr;
794851d45dSJason M. Bills }
804851d45dSJason M. Bills 
814851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
824851d45dSJason M. Bills {
834851d45dSJason M. Bills     // Redfish MessageIds are in the form
844851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
854851d45dSJason M. Bills     // the right Message
864851d45dSJason M. Bills     std::vector<std::string> fields;
874851d45dSJason M. Bills     fields.reserve(4);
884851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
8902cad96eSEd Tanous     const std::string& registryName = fields[0];
9002cad96eSEd Tanous     const std::string& messageKey = fields[3];
914851d45dSJason M. Bills 
924851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
934851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
944851d45dSJason M. Bills     {
954851d45dSJason M. Bills         return getMessageFromRegistry(
9626702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
974851d45dSJason M. Bills     }
984851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
994851d45dSJason M. Bills     {
1004851d45dSJason M. Bills         return getMessageFromRegistry(
10126702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1024851d45dSJason M. Bills     }
1034851d45dSJason M. Bills     return nullptr;
1044851d45dSJason M. Bills }
105fffb8c1fSEd Tanous } // namespace registries
1064851d45dSJason M. Bills 
107f6150403SJames Feist namespace fs = std::filesystem;
1081da66f75SEd Tanous 
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 }
18450b8a43aSEd Tanous 
1857e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
186e85d6b16SJason M. Bills                                     const bool firstEntry = true)
18716428a1aSJason M. Bills {
18816428a1aSJason M. Bills     int ret = 0;
18916428a1aSJason M. Bills     static uint64_t prevTs = 0;
19016428a1aSJason M. Bills     static int index = 0;
191e85d6b16SJason M. Bills     if (firstEntry)
192e85d6b16SJason M. Bills     {
193e85d6b16SJason M. Bills         prevTs = 0;
194e85d6b16SJason M. Bills     }
195e85d6b16SJason M. Bills 
19616428a1aSJason M. Bills     // Get the entry timestamp
19716428a1aSJason M. Bills     uint64_t curTs = 0;
19816428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
19916428a1aSJason M. Bills     if (ret < 0)
20016428a1aSJason M. Bills     {
20116428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
20216428a1aSJason M. Bills                          << strerror(-ret);
20316428a1aSJason M. Bills         return false;
20416428a1aSJason M. Bills     }
20516428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
20616428a1aSJason M. Bills     if (curTs == prevTs)
20716428a1aSJason M. Bills     {
20816428a1aSJason M. Bills         index++;
20916428a1aSJason M. Bills     }
21016428a1aSJason M. Bills     else
21116428a1aSJason M. Bills     {
21216428a1aSJason M. Bills         // Otherwise, reset it
21316428a1aSJason M. Bills         index = 0;
21416428a1aSJason M. Bills     }
21516428a1aSJason M. Bills     // Save the timestamp
21616428a1aSJason M. Bills     prevTs = curTs;
21716428a1aSJason M. Bills 
21816428a1aSJason M. Bills     entryID = std::to_string(curTs);
21916428a1aSJason M. Bills     if (index > 0)
22016428a1aSJason M. Bills     {
22116428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
22216428a1aSJason M. Bills     }
22316428a1aSJason M. Bills     return true;
22416428a1aSJason M. Bills }
22516428a1aSJason M. Bills 
226e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
227e85d6b16SJason M. Bills                              const bool firstEntry = true)
22895820184SJason M. Bills {
229271584abSEd Tanous     static time_t prevTs = 0;
23095820184SJason M. Bills     static int index = 0;
231e85d6b16SJason M. Bills     if (firstEntry)
232e85d6b16SJason M. Bills     {
233e85d6b16SJason M. Bills         prevTs = 0;
234e85d6b16SJason M. Bills     }
235e85d6b16SJason M. Bills 
23695820184SJason M. Bills     // Get the entry timestamp
237271584abSEd Tanous     std::time_t curTs = 0;
23895820184SJason M. Bills     std::tm timeStruct = {};
23995820184SJason M. Bills     std::istringstream entryStream(logEntry);
24095820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
24195820184SJason M. Bills     {
24295820184SJason M. Bills         curTs = std::mktime(&timeStruct);
24395820184SJason M. Bills     }
24495820184SJason M. Bills     // If the timestamp isn't unique, increment the index
24595820184SJason M. Bills     if (curTs == prevTs)
24695820184SJason M. Bills     {
24795820184SJason M. Bills         index++;
24895820184SJason M. Bills     }
24995820184SJason M. Bills     else
25095820184SJason M. Bills     {
25195820184SJason M. Bills         // Otherwise, reset it
25295820184SJason M. Bills         index = 0;
25395820184SJason M. Bills     }
25495820184SJason M. Bills     // Save the timestamp
25595820184SJason M. Bills     prevTs = curTs;
25695820184SJason M. Bills 
25795820184SJason M. Bills     entryID = std::to_string(curTs);
25895820184SJason M. Bills     if (index > 0)
25995820184SJason M. Bills     {
26095820184SJason M. Bills         entryID += "_" + std::to_string(index);
26195820184SJason M. Bills     }
26295820184SJason M. Bills     return true;
26395820184SJason M. Bills }
26495820184SJason M. Bills 
2657e860f15SJohn Edward Broadbent inline static bool
2668d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2678d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2688d1b46d7Szhanghch05                        uint64_t& index)
26916428a1aSJason M. Bills {
27016428a1aSJason M. Bills     if (entryID.empty())
27116428a1aSJason M. Bills     {
27216428a1aSJason M. Bills         return false;
27316428a1aSJason M. Bills     }
27416428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
27539e77504SEd Tanous     std::string_view tsStr(entryID);
27616428a1aSJason M. Bills 
27781ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
27871d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
27916428a1aSJason M. Bills     {
28016428a1aSJason M. Bills         // Timestamp has an index
28116428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
28239e77504SEd Tanous         std::string_view indexStr(entryID);
28316428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
284c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
285c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
286c0bd5e4bSEd Tanous         if (ec != std::errc())
28716428a1aSJason M. Bills         {
288ace85d60SEd Tanous             messages::resourceMissingAtURI(
289ace85d60SEd Tanous                 asyncResp->res, crow::utility::urlFromPieces(entryID));
29016428a1aSJason M. Bills             return false;
29116428a1aSJason M. Bills         }
29216428a1aSJason M. Bills     }
29316428a1aSJason M. Bills     // Timestamp has no index
294c0bd5e4bSEd Tanous     auto [ptr, ec] =
295c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
296c0bd5e4bSEd Tanous     if (ec != std::errc())
29716428a1aSJason M. Bills     {
298ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
299ace85d60SEd Tanous                                        crow::utility::urlFromPieces(entryID));
30016428a1aSJason M. Bills         return false;
30116428a1aSJason M. Bills     }
30216428a1aSJason M. Bills     return true;
30316428a1aSJason M. Bills }
30416428a1aSJason M. Bills 
30595820184SJason M. Bills static bool
30695820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
30795820184SJason M. Bills {
30895820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
30995820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
31095820184SJason M. Bills 
31195820184SJason M. Bills     // Loop through the directory looking for redfish log files
31295820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
31395820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
31495820184SJason M. Bills     {
31595820184SJason M. Bills         // If we find a redfish log file, save the path
31695820184SJason M. Bills         std::string filename = dirEnt.path().filename();
31711ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
31895820184SJason M. Bills         {
31995820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
32095820184SJason M. Bills         }
32195820184SJason M. Bills     }
32295820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
32395820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
32495820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
32595820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
32695820184SJason M. Bills 
32795820184SJason M. Bills     return !redfishLogFiles.empty();
32895820184SJason M. Bills }
32995820184SJason M. Bills 
330aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
331aefe3786SClaire Weinan     const dbus::utility::ManagedItem& object, std::string& dumpStatus,
332aefe3786SClaire Weinan     uint64_t& size, uint64_t& timestamp,
333aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
334aefe3786SClaire Weinan {
335aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
336aefe3786SClaire Weinan     {
337aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
338aefe3786SClaire Weinan         {
339aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
340aefe3786SClaire Weinan             {
341aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
342aefe3786SClaire Weinan                 {
343aefe3786SClaire Weinan                     const auto* status =
344aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
345aefe3786SClaire Weinan                     if (status == nullptr)
346aefe3786SClaire Weinan                     {
347aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
348aefe3786SClaire Weinan                         break;
349aefe3786SClaire Weinan                     }
350aefe3786SClaire Weinan                     dumpStatus = *status;
351aefe3786SClaire Weinan                 }
352aefe3786SClaire Weinan             }
353aefe3786SClaire Weinan         }
354aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
355aefe3786SClaire Weinan         {
356aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
357aefe3786SClaire Weinan             {
358aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
359aefe3786SClaire Weinan                 {
360aefe3786SClaire Weinan                     const auto* sizePtr =
361aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
362aefe3786SClaire Weinan                     if (sizePtr == nullptr)
363aefe3786SClaire Weinan                     {
364aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
365aefe3786SClaire Weinan                         break;
366aefe3786SClaire Weinan                     }
367aefe3786SClaire Weinan                     size = *sizePtr;
368aefe3786SClaire Weinan                     break;
369aefe3786SClaire Weinan                 }
370aefe3786SClaire Weinan             }
371aefe3786SClaire Weinan         }
372aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
373aefe3786SClaire Weinan         {
374aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
375aefe3786SClaire Weinan             {
376aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
377aefe3786SClaire Weinan                 {
378aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
379aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
380aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
381aefe3786SClaire Weinan                     {
382aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
383aefe3786SClaire Weinan                         break;
384aefe3786SClaire Weinan                     }
385aefe3786SClaire Weinan                     timestamp = *usecsTimeStamp;
386aefe3786SClaire Weinan                     break;
387aefe3786SClaire Weinan                 }
388aefe3786SClaire Weinan             }
389aefe3786SClaire Weinan         }
390aefe3786SClaire Weinan     }
391aefe3786SClaire Weinan }
392aefe3786SClaire Weinan 
39321ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
394fdd26906SClaire Weinan {
395fdd26906SClaire Weinan     std::string entriesPath;
396fdd26906SClaire Weinan 
397fdd26906SClaire Weinan     if (dumpType == "BMC")
398fdd26906SClaire Weinan     {
399fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
400fdd26906SClaire Weinan     }
401fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
402fdd26906SClaire Weinan     {
403fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
404fdd26906SClaire Weinan     }
405fdd26906SClaire Weinan     else if (dumpType == "System")
406fdd26906SClaire Weinan     {
407fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
408fdd26906SClaire Weinan     }
409fdd26906SClaire Weinan     else
410fdd26906SClaire Weinan     {
411fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
412fdd26906SClaire Weinan                          << dumpType;
413fdd26906SClaire Weinan     }
414fdd26906SClaire Weinan 
415fdd26906SClaire Weinan     // Returns empty string on error
416fdd26906SClaire Weinan     return entriesPath;
417fdd26906SClaire Weinan }
418fdd26906SClaire Weinan 
4198d1b46d7Szhanghch05 inline void
4208d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4215cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4225cb1dd27SAsmitha Karunanithi {
423fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
424fdd26906SClaire Weinan     if (entriesPath.empty())
4255cb1dd27SAsmitha Karunanithi     {
4265cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4275cb1dd27SAsmitha Karunanithi         return;
4285cb1dd27SAsmitha Karunanithi     }
4295cb1dd27SAsmitha Karunanithi 
4305cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
431fdd26906SClaire Weinan         [asyncResp, entriesPath,
432711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
433711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4345cb1dd27SAsmitha Karunanithi         if (ec)
4355cb1dd27SAsmitha Karunanithi         {
4365cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4375cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4385cb1dd27SAsmitha Karunanithi             return;
4395cb1dd27SAsmitha Karunanithi         }
4405cb1dd27SAsmitha Karunanithi 
441fdd26906SClaire Weinan         // Remove ending slash
442fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
443fdd26906SClaire Weinan         if (!odataIdStr.empty())
444fdd26906SClaire Weinan         {
445fdd26906SClaire Weinan             odataIdStr.pop_back();
446fdd26906SClaire Weinan         }
447fdd26906SClaire Weinan 
448fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
449fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
450fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
451fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
452fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
453fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
454fdd26906SClaire Weinan 
4555cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4565cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
457b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
458b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
459002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
4605cb1dd27SAsmitha Karunanithi 
461002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
462002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
463002d39b4SEd Tanous                                                r.first.filename());
464565dfb6fSClaire Weinan         });
465565dfb6fSClaire Weinan 
4665cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4675cb1dd27SAsmitha Karunanithi         {
468b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4695cb1dd27SAsmitha Karunanithi             {
4705cb1dd27SAsmitha Karunanithi                 continue;
4715cb1dd27SAsmitha Karunanithi             }
4721d8782e7SNan Zhou             uint64_t timestamp = 0;
4735cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
47435440d18SAsmitha Karunanithi             std::string dumpStatus;
475433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
4762dfd18efSEd Tanous 
4772dfd18efSEd Tanous             std::string entryID = object.first.filename();
4782dfd18efSEd Tanous             if (entryID.empty())
4795cb1dd27SAsmitha Karunanithi             {
4805cb1dd27SAsmitha Karunanithi                 continue;
4815cb1dd27SAsmitha Karunanithi             }
4825cb1dd27SAsmitha Karunanithi 
483aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestamp,
484aefe3786SClaire Weinan                                          asyncResp);
4855cb1dd27SAsmitha Karunanithi 
4860fda0f12SGeorge Liu             if (dumpStatus !=
4870fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
48835440d18SAsmitha Karunanithi                 !dumpStatus.empty())
48935440d18SAsmitha Karunanithi             {
49035440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
49135440d18SAsmitha Karunanithi                 continue;
49235440d18SAsmitha Karunanithi             }
49335440d18SAsmitha Karunanithi 
494647b3cdcSGeorge Liu             thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
495fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
4965cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
4975cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
498002d39b4SEd Tanous             thisEntry["Created"] = crow::utility::getDateTimeUint(timestamp);
4995cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
5005cb1dd27SAsmitha Karunanithi 
5015cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5025cb1dd27SAsmitha Karunanithi             {
503d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
504d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
505fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
506fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5075cb1dd27SAsmitha Karunanithi             }
5085cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5095cb1dd27SAsmitha Karunanithi             {
510d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
511d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
512d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
513fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
514fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5155cb1dd27SAsmitha Karunanithi             }
51635440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5175cb1dd27SAsmitha Karunanithi         }
518002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5195cb1dd27SAsmitha Karunanithi         },
5205cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5215cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5225cb1dd27SAsmitha Karunanithi }
5235cb1dd27SAsmitha Karunanithi 
5248d1b46d7Szhanghch05 inline void
525c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5268d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5275cb1dd27SAsmitha Karunanithi {
528fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
529fdd26906SClaire Weinan     if (entriesPath.empty())
5305cb1dd27SAsmitha Karunanithi     {
5315cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5325cb1dd27SAsmitha Karunanithi         return;
5335cb1dd27SAsmitha Karunanithi     }
5345cb1dd27SAsmitha Karunanithi 
5355cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
536fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
537fdd26906SClaire Weinan          entriesPath](const boost::system::error_code ec,
53802cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5395cb1dd27SAsmitha Karunanithi         if (ec)
5405cb1dd27SAsmitha Karunanithi         {
5415cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5425cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5435cb1dd27SAsmitha Karunanithi             return;
5445cb1dd27SAsmitha Karunanithi         }
5455cb1dd27SAsmitha Karunanithi 
546b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
547b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
548b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
549002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
550b47452b2SAsmitha Karunanithi 
5519eb808c1SEd Tanous         for (const auto& objectPath : resp)
5525cb1dd27SAsmitha Karunanithi         {
553b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5545cb1dd27SAsmitha Karunanithi             {
5555cb1dd27SAsmitha Karunanithi                 continue;
5565cb1dd27SAsmitha Karunanithi             }
5575cb1dd27SAsmitha Karunanithi 
5585cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
5591d8782e7SNan Zhou             uint64_t timestamp = 0;
5605cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
56135440d18SAsmitha Karunanithi             std::string dumpStatus;
5625cb1dd27SAsmitha Karunanithi 
563aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
564aefe3786SClaire Weinan                                          timestamp, asyncResp);
5655cb1dd27SAsmitha Karunanithi 
5660fda0f12SGeorge Liu             if (dumpStatus !=
5670fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
56835440d18SAsmitha Karunanithi                 !dumpStatus.empty())
56935440d18SAsmitha Karunanithi             {
57035440d18SAsmitha Karunanithi                 // Dump status is not Complete
57135440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
572002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
573002d39b4SEd Tanous                                            entryID);
57435440d18SAsmitha Karunanithi                 return;
57535440d18SAsmitha Karunanithi             }
57635440d18SAsmitha Karunanithi 
5775cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
578647b3cdcSGeorge Liu                 "#LogEntry.v1_8_0.LogEntry";
579fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
5805cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
5815cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
5825cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Created"] =
5831d8782e7SNan Zhou                 crow::utility::getDateTimeUint(timestamp);
5845cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
5855cb1dd27SAsmitha Karunanithi 
5865cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5875cb1dd27SAsmitha Karunanithi             {
588d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
589d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
590fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
591fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
5925cb1dd27SAsmitha Karunanithi             }
5935cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5945cb1dd27SAsmitha Karunanithi             {
595d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
596002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
597d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
598fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
599fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
6005cb1dd27SAsmitha Karunanithi             }
6015cb1dd27SAsmitha Karunanithi         }
602e05aec50SEd Tanous         if (!foundDumpEntry)
603b47452b2SAsmitha Karunanithi         {
604b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
605b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
606b47452b2SAsmitha Karunanithi             return;
607b47452b2SAsmitha Karunanithi         }
6085cb1dd27SAsmitha Karunanithi         },
6095cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6105cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6115cb1dd27SAsmitha Karunanithi }
6125cb1dd27SAsmitha Karunanithi 
6138d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6149878256fSStanley Chu                             const std::string& entryID,
615b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6165cb1dd27SAsmitha Karunanithi {
617002d39b4SEd Tanous     auto respHandler =
618002d39b4SEd Tanous         [asyncResp, entryID](const boost::system::error_code ec) {
6195cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6205cb1dd27SAsmitha Karunanithi         if (ec)
6215cb1dd27SAsmitha Karunanithi         {
6223de8d8baSGeorge Liu             if (ec.value() == EBADR)
6233de8d8baSGeorge Liu             {
6243de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6253de8d8baSGeorge Liu                 return;
6263de8d8baSGeorge Liu             }
6275cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
628fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6295cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6305cb1dd27SAsmitha Karunanithi             return;
6315cb1dd27SAsmitha Karunanithi         }
6325cb1dd27SAsmitha Karunanithi     };
6335cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6345cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
635b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
636b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
637b47452b2SAsmitha Karunanithi             entryID,
6385cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6395cb1dd27SAsmitha Karunanithi }
6405cb1dd27SAsmitha Karunanithi 
6418d1b46d7Szhanghch05 inline void
64298be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
6438d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6448d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
645a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
646a43be80fSAsmitha Karunanithi {
647a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
64859d494eeSPatrick Williams         [dumpId, dumpPath,
64959d494eeSPatrick Williams          dumpType](boost::system::error_code err, sdbusplus::message_t& m,
650a43be80fSAsmitha Karunanithi                    const std::shared_ptr<task::TaskData>& taskData) {
651cb13a392SEd Tanous         if (err)
652cb13a392SEd Tanous         {
6536145ed6fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Error in creating a dump";
6546145ed6fSAsmitha Karunanithi             taskData->state = "Cancelled";
6556145ed6fSAsmitha Karunanithi             return task::completed;
656cb13a392SEd Tanous         }
657b9d36b47SEd Tanous 
658b9d36b47SEd Tanous         dbus::utility::DBusInteracesMap interfacesList;
659a43be80fSAsmitha Karunanithi 
660a43be80fSAsmitha Karunanithi         sdbusplus::message::object_path objPath;
661a43be80fSAsmitha Karunanithi 
662a43be80fSAsmitha Karunanithi         m.read(objPath, interfacesList);
663a43be80fSAsmitha Karunanithi 
664b47452b2SAsmitha Karunanithi         if (objPath.str ==
665b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
666b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
667b47452b2SAsmitha Karunanithi                 "/entry/" + std::to_string(dumpId))
668a43be80fSAsmitha Karunanithi         {
669a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
670a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
671a43be80fSAsmitha Karunanithi 
672a43be80fSAsmitha Karunanithi             std::string headerLoc =
673a43be80fSAsmitha Karunanithi                 "Location: " + dumpPath + std::to_string(dumpId);
674002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
675a43be80fSAsmitha Karunanithi 
676a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
677b47452b2SAsmitha Karunanithi             return task::completed;
6786145ed6fSAsmitha Karunanithi         }
679a43be80fSAsmitha Karunanithi         return task::completed;
680a43be80fSAsmitha Karunanithi         },
6814978b63fSJason M. Bills         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
682a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
683a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
684a43be80fSAsmitha Karunanithi 
685a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
686a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
68798be3e39SEd Tanous     task->payload.emplace(std::move(payload));
688a43be80fSAsmitha Karunanithi }
689a43be80fSAsmitha Karunanithi 
6908d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6918d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
692a43be80fSAsmitha Karunanithi {
693fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
694fdd26906SClaire Weinan     if (dumpPath.empty())
695a43be80fSAsmitha Karunanithi     {
696a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
697a43be80fSAsmitha Karunanithi         return;
698a43be80fSAsmitha Karunanithi     }
699a43be80fSAsmitha Karunanithi 
700a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
701a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
702a43be80fSAsmitha Karunanithi 
70315ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
704a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
705a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
706a43be80fSAsmitha Karunanithi     {
707a43be80fSAsmitha Karunanithi         return;
708a43be80fSAsmitha Karunanithi     }
709a43be80fSAsmitha Karunanithi 
710a43be80fSAsmitha Karunanithi     if (dumpType == "System")
711a43be80fSAsmitha Karunanithi     {
712a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
713a43be80fSAsmitha Karunanithi         {
7144978b63fSJason M. Bills             BMCWEB_LOG_ERROR
7154978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
716a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
717a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
718a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
719a43be80fSAsmitha Karunanithi             return;
720a43be80fSAsmitha Karunanithi         }
7213174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
722a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
723a43be80fSAsmitha Karunanithi         {
724a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
725ace85d60SEd Tanous             messages::internalError(asyncResp->res);
726a43be80fSAsmitha Karunanithi             return;
727a43be80fSAsmitha Karunanithi         }
728a43be80fSAsmitha Karunanithi     }
729a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
730a43be80fSAsmitha Karunanithi     {
731a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
732a43be80fSAsmitha Karunanithi         {
7330fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
7340fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
735a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
736a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
737a43be80fSAsmitha Karunanithi             return;
738a43be80fSAsmitha Karunanithi         }
7393174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
740a43be80fSAsmitha Karunanithi         {
741a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
742a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
743ace85d60SEd Tanous             messages::internalError(asyncResp->res);
744a43be80fSAsmitha Karunanithi             return;
745a43be80fSAsmitha Karunanithi         }
746a43be80fSAsmitha Karunanithi     }
747a43be80fSAsmitha Karunanithi 
748a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
74998be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
75098be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
75198be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
752a43be80fSAsmitha Karunanithi         if (ec)
753a43be80fSAsmitha Karunanithi         {
754a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
755a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
756a43be80fSAsmitha Karunanithi             return;
757a43be80fSAsmitha Karunanithi         }
758a43be80fSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
759a43be80fSAsmitha Karunanithi 
760002d39b4SEd Tanous         createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath,
761002d39b4SEd Tanous                                dumpType);
762a43be80fSAsmitha Karunanithi         },
763b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
764b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
765b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
766a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
767a43be80fSAsmitha Karunanithi }
768a43be80fSAsmitha Karunanithi 
7698d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7708d1b46d7Szhanghch05                       const std::string& dumpType)
77180319af1SAsmitha Karunanithi {
772b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
773b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
7748d1b46d7Szhanghch05 
77580319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
776b9d36b47SEd Tanous         [asyncResp, dumpType](
777b9d36b47SEd Tanous             const boost::system::error_code ec,
778b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
77980319af1SAsmitha Karunanithi         if (ec)
78080319af1SAsmitha Karunanithi         {
78180319af1SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
78280319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
78380319af1SAsmitha Karunanithi             return;
78480319af1SAsmitha Karunanithi         }
78580319af1SAsmitha Karunanithi 
78680319af1SAsmitha Karunanithi         for (const std::string& path : subTreePaths)
78780319af1SAsmitha Karunanithi         {
7882dfd18efSEd Tanous             sdbusplus::message::object_path objPath(path);
7892dfd18efSEd Tanous             std::string logID = objPath.filename();
7902dfd18efSEd Tanous             if (logID.empty())
79180319af1SAsmitha Karunanithi             {
7922dfd18efSEd Tanous                 continue;
79380319af1SAsmitha Karunanithi             }
7942dfd18efSEd Tanous             deleteDumpEntry(asyncResp, logID, dumpType);
79580319af1SAsmitha Karunanithi         }
79680319af1SAsmitha Karunanithi         },
79780319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
79880319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
79980319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
800b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
801b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
802b47452b2SAsmitha Karunanithi                                    dumpType});
80380319af1SAsmitha Karunanithi }
80480319af1SAsmitha Karunanithi 
805b9d36b47SEd Tanous inline static void
806b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
807b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
808b9d36b47SEd Tanous                              std::string& logfile)
809043a0536SJohnathan Mantey {
810043a0536SJohnathan Mantey     for (auto property : params)
811043a0536SJohnathan Mantey     {
812043a0536SJohnathan Mantey         if (property.first == "Timestamp")
813043a0536SJohnathan Mantey         {
814043a0536SJohnathan Mantey             const std::string* value =
8158d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
816043a0536SJohnathan Mantey             if (value != nullptr)
817043a0536SJohnathan Mantey             {
818043a0536SJohnathan Mantey                 timestamp = *value;
819043a0536SJohnathan Mantey             }
820043a0536SJohnathan Mantey         }
821043a0536SJohnathan Mantey         else if (property.first == "Filename")
822043a0536SJohnathan Mantey         {
823043a0536SJohnathan Mantey             const std::string* value =
8248d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
825043a0536SJohnathan Mantey             if (value != nullptr)
826043a0536SJohnathan Mantey             {
827043a0536SJohnathan Mantey                 filename = *value;
828043a0536SJohnathan Mantey             }
829043a0536SJohnathan Mantey         }
830043a0536SJohnathan Mantey         else if (property.first == "Log")
831043a0536SJohnathan Mantey         {
832043a0536SJohnathan Mantey             const std::string* value =
8338d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
834043a0536SJohnathan Mantey             if (value != nullptr)
835043a0536SJohnathan Mantey             {
836043a0536SJohnathan Mantey                 logfile = *value;
837043a0536SJohnathan Mantey             }
838043a0536SJohnathan Mantey         }
839043a0536SJohnathan Mantey     }
840043a0536SJohnathan Mantey }
841043a0536SJohnathan Mantey 
842a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
8437e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
8441da66f75SEd Tanous {
845c4bf6374SJason M. Bills     /**
846c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
847c4bf6374SJason M. Bills      */
8487e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
849ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
850002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
851002d39b4SEd Tanous             [&app](const crow::Request& req,
852002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
8533ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
854c4bf6374SJason M. Bills         {
85545ca1b86SEd Tanous             return;
85645ca1b86SEd Tanous         }
8577e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
8587e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
859c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
860c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
861c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
862029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
86345ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
864c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
865c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
866002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
867c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
8681476687dSEd Tanous         nlohmann::json::object_t eventLog;
8691476687dSEd Tanous         eventLog["@odata.id"] =
8701476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
8711476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
8725cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
8731476687dSEd Tanous         nlohmann::json::object_t dumpLog;
874002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
8751476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
876c9bb6861Sraviteja-b #endif
877c9bb6861Sraviteja-b 
878d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
8791476687dSEd Tanous         nlohmann::json::object_t crashdump;
8801476687dSEd Tanous         crashdump["@odata.id"] =
8811476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
8821476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
883d53dd41fSJason M. Bills #endif
884b7028ebfSSpencer Ku 
885b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
8861476687dSEd Tanous         nlohmann::json::object_t hostlogger;
8871476687dSEd Tanous         hostlogger["@odata.id"] =
8881476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
8891476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
890b7028ebfSSpencer Ku #endif
891c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
892c4bf6374SJason M. Bills             logServiceArray.size();
893a3316fc6SZhikuiRen 
894a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
89545ca1b86SEd Tanous             [asyncResp](const boost::system::error_code ec,
896b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
897b9d36b47SEd Tanous                             subtreePath) {
898a3316fc6SZhikuiRen             if (ec)
899a3316fc6SZhikuiRen             {
900a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
901a3316fc6SZhikuiRen                 return;
902a3316fc6SZhikuiRen             }
903a3316fc6SZhikuiRen 
90455f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
905a3316fc6SZhikuiRen             {
906a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
907a3316fc6SZhikuiRen                 {
90823a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
909a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
91023a21a1cSEd Tanous                     logServiceArrayLocal.push_back(
9110fda0f12SGeorge Liu                         {{"@odata.id",
9120fda0f12SGeorge Liu                           "/redfish/v1/Systems/system/LogServices/PostCodes"}});
91345ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
91423a21a1cSEd Tanous                         logServiceArrayLocal.size();
915a3316fc6SZhikuiRen                     return;
916a3316fc6SZhikuiRen                 }
917a3316fc6SZhikuiRen             }
918a3316fc6SZhikuiRen             },
919a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
920a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
92145ca1b86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
92245ca1b86SEd Tanous             std::array<const char*, 1>{postCodeIface});
9237e860f15SJohn Edward Broadbent         });
924c4bf6374SJason M. Bills }
925c4bf6374SJason M. Bills 
9267e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
927c4bf6374SJason M. Bills {
9287e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
929ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
930002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
931002d39b4SEd Tanous             [&app](const crow::Request& req,
932002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
93445ca1b86SEd Tanous         {
93545ca1b86SEd Tanous             return;
93645ca1b86SEd Tanous         }
937c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
938029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
939c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
940c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
941c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
942002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
943c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
944c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
9457c8c4058STejas Patil 
9467c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
9477c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
9487c8c4058STejas Patil 
9497c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
9507c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
9517c8c4058STejas Patil             redfishDateTimeOffset.second;
9527c8c4058STejas Patil 
9531476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
9541476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
955e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
956e7d6c8b2SGunnar Mills 
9570fda0f12SGeorge Liu             {"target",
9580fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
9597e860f15SJohn Edward Broadbent         });
960489640c6SJason M. Bills }
961489640c6SJason M. Bills 
9627e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
963489640c6SJason M. Bills {
9644978b63fSJason M. Bills     BMCWEB_ROUTE(
9654978b63fSJason M. Bills         app,
9664978b63fSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
967432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
9687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
96945ca1b86SEd Tanous             [&app](const crow::Request& req,
9707e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
97245ca1b86SEd Tanous         {
97345ca1b86SEd Tanous             return;
97445ca1b86SEd Tanous         }
975489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
976489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
977489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
978489640c6SJason M. Bills         {
979489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
980489640c6SJason M. Bills             {
981489640c6SJason M. Bills                 std::error_code ec;
982489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
983489640c6SJason M. Bills             }
984489640c6SJason M. Bills         }
985489640c6SJason M. Bills 
986489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
987489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
988489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
989489640c6SJason M. Bills             if (ec)
990489640c6SJason M. Bills             {
991002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
992489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
993489640c6SJason M. Bills                 return;
994489640c6SJason M. Bills             }
995489640c6SJason M. Bills 
996489640c6SJason M. Bills             messages::success(asyncResp->res);
997489640c6SJason M. Bills             },
998489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
999002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
1000002d39b4SEd Tanous             "replace");
10017e860f15SJohn Edward Broadbent         });
1002c4bf6374SJason M. Bills }
1003c4bf6374SJason M. Bills 
1004ac992cdeSJason M. Bills enum class LogParseError
1005ac992cdeSJason M. Bills {
1006ac992cdeSJason M. Bills     success,
1007ac992cdeSJason M. Bills     parseFailed,
1008ac992cdeSJason M. Bills     messageIdNotInRegistry,
1009ac992cdeSJason M. Bills };
1010ac992cdeSJason M. Bills 
1011ac992cdeSJason M. Bills static LogParseError
1012ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1013b5a76932SEd Tanous                           const std::string& logEntry,
1014de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1015c4bf6374SJason M. Bills {
101695820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1017cd225da8SJason M. Bills     // First get the Timestamp
1018f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1019cd225da8SJason M. Bills     if (space == std::string::npos)
102095820184SJason M. Bills     {
1021ac992cdeSJason M. Bills         return LogParseError::parseFailed;
102295820184SJason M. Bills     }
1023cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1024cd225da8SJason M. Bills     // Then get the log contents
1025f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1026cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1027cd225da8SJason M. Bills     {
1028ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1029cd225da8SJason M. Bills     }
1030cd225da8SJason M. Bills     std::string_view entry(logEntry);
1031cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1032cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1033cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1034cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1035cd225da8SJason M. Bills                  boost::token_compress_on);
1036cd225da8SJason M. Bills     // We need at least a MessageId to be valid
103726f6976fSEd Tanous     if (logEntryFields.empty())
1038cd225da8SJason M. Bills     {
1039ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1040cd225da8SJason M. Bills     }
1041cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
104295820184SJason M. Bills 
10434851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1044fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1045c4bf6374SJason M. Bills 
104654417b02SSui Chen     if (message == nullptr)
1047c4bf6374SJason M. Bills     {
104854417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1049ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1050c4bf6374SJason M. Bills     }
1051c4bf6374SJason M. Bills 
105254417b02SSui Chen     std::string msg = message->message;
105354417b02SSui Chen 
105415a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
105526702d01SEd Tanous     std::span<std::string> messageArgs;
105615a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
105715a86ff6SJason M. Bills     {
105815a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
105915a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
106015a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
106115a86ff6SJason M. Bills         if (!messageArgsStart.empty())
106215a86ff6SJason M. Bills         {
106315a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
106415a86ff6SJason M. Bills         }
106515a86ff6SJason M. Bills 
106623a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1067c4bf6374SJason M. Bills 
10684851d45dSJason M. Bills         // Fill the MessageArgs into the Message
106995820184SJason M. Bills         int i = 0;
107095820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
10714851d45dSJason M. Bills         {
107295820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
10734851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
10744851d45dSJason M. Bills             if (argPos != std::string::npos)
10754851d45dSJason M. Bills             {
107695820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
10774851d45dSJason M. Bills             }
10784851d45dSJason M. Bills         }
107915a86ff6SJason M. Bills     }
10804851d45dSJason M. Bills 
108195820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
108295820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
108395820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1084f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1085f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
108695820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1087c4bf6374SJason M. Bills     {
108895820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1089c4bf6374SJason M. Bills     }
1090c4bf6374SJason M. Bills 
1091c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
109284afc48bSJason M. Bills     logEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
109384afc48bSJason M. Bills     logEntryJson["@odata.id"] =
109484afc48bSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
109584afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
109684afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
109784afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
109884afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
109984afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
110084afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
110184afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
110284afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1103ac992cdeSJason M. Bills     return LogParseError::success;
1104c4bf6374SJason M. Bills }
1105c4bf6374SJason M. Bills 
11067e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1107c4bf6374SJason M. Bills {
11087e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11097e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11108b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1111002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1112002d39b4SEd Tanous             [&app](const crow::Request& req,
1113002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1114c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1115c937d2bfSEd Tanous             .canDelegateTop = true,
1116c937d2bfSEd Tanous             .canDelegateSkip = true,
1117c937d2bfSEd Tanous         };
1118c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1119c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
11203ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1121c4bf6374SJason M. Bills         {
1122c4bf6374SJason M. Bills             return;
1123c4bf6374SJason M. Bills         }
11243648c8beSEd Tanous         size_t top =
11253648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
11263648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
11273648c8beSEd Tanous 
11287e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
11297e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1130c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1131c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1132c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1133029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1134c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1135c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1136c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1137cb92c03bSAndrew Geissler 
11384978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1139c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
11407e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
11417e860f15SJohn Edward Broadbent         // entry
114295820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
114395820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1144b01bf299SEd Tanous         uint64_t entryCount = 0;
1145cd225da8SJason M. Bills         std::string logEntry;
114695820184SJason M. Bills 
11477e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
11487e860f15SJohn Edward Broadbent         // backwards
1149002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1150002d39b4SEd Tanous              it++)
1151c4bf6374SJason M. Bills         {
1152cd225da8SJason M. Bills             std::ifstream logStream(*it);
115395820184SJason M. Bills             if (!logStream.is_open())
1154c4bf6374SJason M. Bills             {
1155c4bf6374SJason M. Bills                 continue;
1156c4bf6374SJason M. Bills             }
1157c4bf6374SJason M. Bills 
1158e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1159e85d6b16SJason M. Bills             bool firstEntry = true;
116095820184SJason M. Bills             while (std::getline(logStream, logEntry))
116195820184SJason M. Bills             {
1162c4bf6374SJason M. Bills                 std::string idStr;
1163e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1164c4bf6374SJason M. Bills                 {
1165c4bf6374SJason M. Bills                     continue;
1166c4bf6374SJason M. Bills                 }
1167e85d6b16SJason M. Bills                 firstEntry = false;
1168e85d6b16SJason M. Bills 
1169de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1170ac992cdeSJason M. Bills                 LogParseError status =
1171ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1172ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1173ac992cdeSJason M. Bills                 {
1174ac992cdeSJason M. Bills                     continue;
1175ac992cdeSJason M. Bills                 }
1176ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1177c4bf6374SJason M. Bills                 {
1178c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1179c4bf6374SJason M. Bills                     return;
1180c4bf6374SJason M. Bills                 }
1181de703c5dSJason M. Bills 
1182de703c5dSJason M. Bills                 entryCount++;
1183de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1184de703c5dSJason M. Bills                 // start) and top (number of entries to display)
11853648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1186de703c5dSJason M. Bills                 {
1187de703c5dSJason M. Bills                     continue;
1188de703c5dSJason M. Bills                 }
1189de703c5dSJason M. Bills 
1190de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1191c4bf6374SJason M. Bills             }
119295820184SJason M. Bills         }
1193c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
11943648c8beSEd Tanous         if (skip + top < entryCount)
1195c4bf6374SJason M. Bills         {
1196c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
11974978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
11983648c8beSEd Tanous                 std::to_string(skip + top);
1199c4bf6374SJason M. Bills         }
12007e860f15SJohn Edward Broadbent         });
1201897967deSJason M. Bills }
1202897967deSJason M. Bills 
12037e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1204897967deSJason M. Bills {
12057e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12067e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1207ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12087e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
120945ca1b86SEd Tanous             [&app](const crow::Request& req,
12107e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12117e860f15SJohn Edward Broadbent                    const std::string& param) {
12123ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
121345ca1b86SEd Tanous         {
121445ca1b86SEd Tanous             return;
121545ca1b86SEd Tanous         }
12167e860f15SJohn Edward Broadbent         const std::string& targetID = param;
12178d1b46d7Szhanghch05 
12187e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
12197e860f15SJohn Edward Broadbent         // entry to find the target entry
1220897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1221897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1222897967deSJason M. Bills         std::string logEntry;
1223897967deSJason M. Bills 
12247e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
12257e860f15SJohn Edward Broadbent         // backwards
1226002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1227002d39b4SEd Tanous              it++)
1228897967deSJason M. Bills         {
1229897967deSJason M. Bills             std::ifstream logStream(*it);
1230897967deSJason M. Bills             if (!logStream.is_open())
1231897967deSJason M. Bills             {
1232897967deSJason M. Bills                 continue;
1233897967deSJason M. Bills             }
1234897967deSJason M. Bills 
1235897967deSJason M. Bills             // Reset the unique ID on the first entry
1236897967deSJason M. Bills             bool firstEntry = true;
1237897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1238897967deSJason M. Bills             {
1239897967deSJason M. Bills                 std::string idStr;
1240897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1241897967deSJason M. Bills                 {
1242897967deSJason M. Bills                     continue;
1243897967deSJason M. Bills                 }
1244897967deSJason M. Bills                 firstEntry = false;
1245897967deSJason M. Bills 
1246897967deSJason M. Bills                 if (idStr == targetID)
1247897967deSJason M. Bills                 {
1248de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1249ac992cdeSJason M. Bills                     LogParseError status =
1250ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1251ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1252897967deSJason M. Bills                     {
1253897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1254897967deSJason M. Bills                         return;
1255897967deSJason M. Bills                     }
1256d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1257897967deSJason M. Bills                     return;
1258897967deSJason M. Bills                 }
1259897967deSJason M. Bills             }
1260897967deSJason M. Bills         }
1261897967deSJason M. Bills         // Requested ID was not found
1262002d39b4SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
1263002d39b4SEd Tanous                                        crow::utility::urlFromPieces(targetID));
12647e860f15SJohn Edward Broadbent         });
126508a4e4b5SAnthony Wilson }
126608a4e4b5SAnthony Wilson 
12677e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
126808a4e4b5SAnthony Wilson {
12697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
12707e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1271ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1272002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1273002d39b4SEd Tanous             [&app](const crow::Request& req,
1274002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
12753ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
127645ca1b86SEd Tanous         {
127745ca1b86SEd Tanous             return;
127845ca1b86SEd Tanous         }
12797e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
12807e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
128108a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
128208a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
128308a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
128408a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
128508a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
128608a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
128708a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
128808a4e4b5SAnthony Wilson 
1289cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1290cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1291cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1292cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1293914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1294cb92c03bSAndrew Geissler             if (ec)
1295cb92c03bSAndrew Geissler             {
1296cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1297cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1298002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1299cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1300cb92c03bSAndrew Geissler                 return;
1301cb92c03bSAndrew Geissler             }
1302002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1303cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
13049eb808c1SEd Tanous             for (const auto& objectPath : resp)
1305cb92c03bSAndrew Geissler             {
1306914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1307c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1308c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1309914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1310914e2d5dSEd Tanous                 const std::string* message = nullptr;
1311914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
131275710de2SXiaochao Ma                 bool resolved = false;
13139eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1314f86bb901SAdriana Kobylak                 {
1315f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1316f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1317f86bb901SAdriana Kobylak                     {
1318002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1319cb92c03bSAndrew Geissler                         {
1320cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1321cb92c03bSAndrew Geissler                             {
1322002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1323cb92c03bSAndrew Geissler                             }
1324cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1325cb92c03bSAndrew Geissler                             {
1326002d39b4SEd Tanous                                 timestamp =
1327002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13287e860f15SJohn Edward Broadbent                             }
1329002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
13307e860f15SJohn Edward Broadbent                             {
1331002d39b4SEd Tanous                                 updateTimestamp =
1332002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13337e860f15SJohn Edward Broadbent                             }
13347e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
13357e860f15SJohn Edward Broadbent                             {
13367e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
13377e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13387e860f15SJohn Edward Broadbent                             }
13397e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
13407e860f15SJohn Edward Broadbent                             {
13417e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
13427e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13437e860f15SJohn Edward Broadbent                             }
13447e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
13457e860f15SJohn Edward Broadbent                             {
1346914e2d5dSEd Tanous                                 const bool* resolveptr =
1347002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
13487e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
13497e860f15SJohn Edward Broadbent                                 {
1350002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
13517e860f15SJohn Edward Broadbent                                     return;
13527e860f15SJohn Edward Broadbent                                 }
13537e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
13547e860f15SJohn Edward Broadbent                             }
13557e860f15SJohn Edward Broadbent                         }
13567e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
13577e860f15SJohn Edward Broadbent                             severity == nullptr)
13587e860f15SJohn Edward Broadbent                         {
13597e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
13607e860f15SJohn Edward Broadbent                             return;
13617e860f15SJohn Edward Broadbent                         }
13627e860f15SJohn Edward Broadbent                     }
13637e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
13647e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
13657e860f15SJohn Edward Broadbent                     {
1366002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
13677e860f15SJohn Edward Broadbent                         {
13687e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
13697e860f15SJohn Edward Broadbent                             {
13707e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
13717e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13727e860f15SJohn Edward Broadbent                             }
13737e860f15SJohn Edward Broadbent                         }
13747e860f15SJohn Edward Broadbent                     }
13757e860f15SJohn Edward Broadbent                 }
13767e860f15SJohn Edward Broadbent                 // Object path without the
13777e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
13787e860f15SJohn Edward Broadbent                 // and continue.
13797e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1380c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1381c419c759SEd Tanous                     updateTimestamp == nullptr)
13827e860f15SJohn Edward Broadbent                 {
13837e860f15SJohn Edward Broadbent                     continue;
13847e860f15SJohn Edward Broadbent                 }
13857e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
13867e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
13877e860f15SJohn Edward Broadbent                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
13887e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
13890fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
13907e860f15SJohn Edward Broadbent                     std::to_string(*id);
13917e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
13927e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
13937e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
13947e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
13957e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
13967e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
13977e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
13987e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
1399c419c759SEd Tanous                     crow::utility::getDateTimeUintMs(*timestamp);
14007e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
1401c419c759SEd Tanous                     crow::utility::getDateTimeUintMs(*updateTimestamp);
14027e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
14037e860f15SJohn Edward Broadbent                 {
14047e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
14050fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14067e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
14077e860f15SJohn Edward Broadbent                 }
14087e860f15SJohn Edward Broadbent             }
1409002d39b4SEd Tanous             std::sort(
1410002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1411002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
14127e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
14137e860f15SJohn Edward Broadbent                 });
14147e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
14157e860f15SJohn Edward Broadbent                 entriesArray.size();
14167e860f15SJohn Edward Broadbent             },
14177e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
14187e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14197e860f15SJohn Edward Broadbent         });
14207e860f15SJohn Edward Broadbent }
14217e860f15SJohn Edward Broadbent 
14227e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
14237e860f15SJohn Edward Broadbent {
14247e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
14257e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1426ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1427002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1428002d39b4SEd Tanous             [&app](const crow::Request& req,
14297e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
143045ca1b86SEd Tanous                    const std::string& param) {
14313ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
14327e860f15SJohn Edward Broadbent         {
143345ca1b86SEd Tanous             return;
143445ca1b86SEd Tanous         }
14357e860f15SJohn Edward Broadbent         std::string entryID = param;
14367e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
14377e860f15SJohn Edward Broadbent 
14387e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
14397e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
14407e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
1441002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1442b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
14437e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
14447e860f15SJohn Edward Broadbent             {
1445002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1446002d39b4SEd Tanous                                            entryID);
14477e860f15SJohn Edward Broadbent                 return;
14487e860f15SJohn Edward Broadbent             }
14497e860f15SJohn Edward Broadbent             if (ec)
14507e860f15SJohn Edward Broadbent             {
14510fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1452002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
14537e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
14547e860f15SJohn Edward Broadbent                 return;
14557e860f15SJohn Edward Broadbent             }
1456914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1457c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1458c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1459914e2d5dSEd Tanous             const std::string* severity = nullptr;
1460914e2d5dSEd Tanous             const std::string* message = nullptr;
1461914e2d5dSEd Tanous             const std::string* filePath = nullptr;
14627e860f15SJohn Edward Broadbent             bool resolved = false;
14637e860f15SJohn Edward Broadbent 
14649eb808c1SEd Tanous             for (const auto& propertyMap : resp)
14657e860f15SJohn Edward Broadbent             {
14667e860f15SJohn Edward Broadbent                 if (propertyMap.first == "Id")
14677e860f15SJohn Edward Broadbent                 {
14687e860f15SJohn Edward Broadbent                     id = std::get_if<uint32_t>(&propertyMap.second);
14697e860f15SJohn Edward Broadbent                 }
14707e860f15SJohn Edward Broadbent                 else if (propertyMap.first == "Timestamp")
14717e860f15SJohn Edward Broadbent                 {
1472002d39b4SEd Tanous                     timestamp = std::get_if<uint64_t>(&propertyMap.second);
1473ebd45906SGeorge Liu                 }
1474d139c236SGeorge Liu                 else if (propertyMap.first == "UpdateTimestamp")
1475d139c236SGeorge Liu                 {
1476ebd45906SGeorge Liu                     updateTimestamp =
1477c419c759SEd Tanous                         std::get_if<uint64_t>(&propertyMap.second);
1478ebd45906SGeorge Liu                 }
1479cb92c03bSAndrew Geissler                 else if (propertyMap.first == "Severity")
1480cb92c03bSAndrew Geissler                 {
1481002d39b4SEd Tanous                     severity = std::get_if<std::string>(&propertyMap.second);
1482cb92c03bSAndrew Geissler                 }
1483cb92c03bSAndrew Geissler                 else if (propertyMap.first == "Message")
1484cb92c03bSAndrew Geissler                 {
1485002d39b4SEd Tanous                     message = std::get_if<std::string>(&propertyMap.second);
1486ae34c8e8SAdriana Kobylak                 }
148775710de2SXiaochao Ma                 else if (propertyMap.first == "Resolved")
148875710de2SXiaochao Ma                 {
1489914e2d5dSEd Tanous                     const bool* resolveptr =
149075710de2SXiaochao Ma                         std::get_if<bool>(&propertyMap.second);
149175710de2SXiaochao Ma                     if (resolveptr == nullptr)
149275710de2SXiaochao Ma                     {
149375710de2SXiaochao Ma                         messages::internalError(asyncResp->res);
149475710de2SXiaochao Ma                         return;
149575710de2SXiaochao Ma                     }
149675710de2SXiaochao Ma                     resolved = *resolveptr;
149775710de2SXiaochao Ma                 }
14987e860f15SJohn Edward Broadbent                 else if (propertyMap.first == "Path")
1499f86bb901SAdriana Kobylak                 {
1500002d39b4SEd Tanous                     filePath = std::get_if<std::string>(&propertyMap.second);
1501f86bb901SAdriana Kobylak                 }
1502f86bb901SAdriana Kobylak             }
1503002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
1504002d39b4SEd Tanous                 timestamp == nullptr || updateTimestamp == nullptr)
1505f86bb901SAdriana Kobylak             {
1506ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1507271584abSEd Tanous                 return;
1508271584abSEd Tanous             }
1509f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
1510f86bb901SAdriana Kobylak                 "#LogEntry.v1_8_0.LogEntry";
1511f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
15120fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1513f86bb901SAdriana Kobylak                 std::to_string(*id);
151445ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1515f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1516f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1517f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
1518f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1519f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1520f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1521f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
1522c419c759SEd Tanous                 crow::utility::getDateTimeUintMs(*timestamp);
1523f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
1524c419c759SEd Tanous                 crow::utility::getDateTimeUintMs(*updateTimestamp);
1525f86bb901SAdriana Kobylak             if (filePath != nullptr)
1526f86bb901SAdriana Kobylak             {
1527f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1528e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1529e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1530f86bb901SAdriana Kobylak             }
1531cb92c03bSAndrew Geissler             },
1532cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1533cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1534f86bb901SAdriana Kobylak             "org.freedesktop.DBus.Properties", "GetAll", "");
15357e860f15SJohn Edward Broadbent         });
1536336e96c6SChicago Duan 
15377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15387e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1539ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
15407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
154145ca1b86SEd Tanous             [&app](const crow::Request& req,
15427e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15437e860f15SJohn Edward Broadbent                    const std::string& entryId) {
15443ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
154545ca1b86SEd Tanous         {
154645ca1b86SEd Tanous             return;
154745ca1b86SEd Tanous         }
154875710de2SXiaochao Ma         std::optional<bool> resolved;
154975710de2SXiaochao Ma 
155015ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
15517e860f15SJohn Edward Broadbent                                       resolved))
155275710de2SXiaochao Ma         {
155375710de2SXiaochao Ma             return;
155475710de2SXiaochao Ma         }
155575710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
155675710de2SXiaochao Ma 
155775710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
15584f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
155975710de2SXiaochao Ma             if (ec)
156075710de2SXiaochao Ma             {
156175710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
156275710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
156375710de2SXiaochao Ma                 return;
156475710de2SXiaochao Ma             }
156575710de2SXiaochao Ma             },
156675710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
156775710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
156875710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
156975710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1570168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
15717e860f15SJohn Edward Broadbent         });
157275710de2SXiaochao Ma 
15737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15747e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1575ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1576ed398213SEd Tanous 
1577002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1578002d39b4SEd Tanous             [&app](const crow::Request& req,
1579002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
158045ca1b86SEd Tanous                    const std::string& param) {
15813ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1582336e96c6SChicago Duan         {
158345ca1b86SEd Tanous             return;
158445ca1b86SEd Tanous         }
1585336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1586336e96c6SChicago Duan 
15877e860f15SJohn Edward Broadbent         std::string entryID = param;
1588336e96c6SChicago Duan 
1589336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1590336e96c6SChicago Duan 
1591336e96c6SChicago Duan         // Process response from Logging service.
1592002d39b4SEd Tanous         auto respHandler =
1593002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1594002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1595336e96c6SChicago Duan             if (ec)
1596336e96c6SChicago Duan             {
15973de8d8baSGeorge Liu                 if (ec.value() == EBADR)
15983de8d8baSGeorge Liu                 {
159945ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
160045ca1b86SEd Tanous                                                entryID);
16013de8d8baSGeorge Liu                     return;
16023de8d8baSGeorge Liu                 }
1603336e96c6SChicago Duan                 // TODO Handle for specific error code
16040fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
16050fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1606336e96c6SChicago Duan                     << ec;
1607336e96c6SChicago Duan                 asyncResp->res.result(
1608336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1609336e96c6SChicago Duan                 return;
1610336e96c6SChicago Duan             }
1611336e96c6SChicago Duan 
1612336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1613336e96c6SChicago Duan         };
1614336e96c6SChicago Duan 
1615336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1616336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1617336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1618336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1619336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
16207e860f15SJohn Edward Broadbent         });
1621400fd1fbSAdriana Kobylak }
1622400fd1fbSAdriana Kobylak 
16237e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1624400fd1fbSAdriana Kobylak {
16250fda0f12SGeorge Liu     BMCWEB_ROUTE(
16260fda0f12SGeorge Liu         app,
16270fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1628ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
16297e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
163045ca1b86SEd Tanous             [&app](const crow::Request& req,
16317e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
163245ca1b86SEd Tanous                    const std::string& param) {
16333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16347e860f15SJohn Edward Broadbent         {
163545ca1b86SEd Tanous             return;
163645ca1b86SEd Tanous         }
1637002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
1638400fd1fbSAdriana Kobylak         {
1639002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1640400fd1fbSAdriana Kobylak             return;
1641400fd1fbSAdriana Kobylak         }
1642400fd1fbSAdriana Kobylak 
16437e860f15SJohn Edward Broadbent         std::string entryID = param;
1644400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1645400fd1fbSAdriana Kobylak 
1646400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1647002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1648400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1649400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1650400fd1fbSAdriana Kobylak             {
1651002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1652002d39b4SEd Tanous                                            entryID);
1653400fd1fbSAdriana Kobylak                 return;
1654400fd1fbSAdriana Kobylak             }
1655400fd1fbSAdriana Kobylak             if (ec)
1656400fd1fbSAdriana Kobylak             {
1657400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1658400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1659400fd1fbSAdriana Kobylak                 return;
1660400fd1fbSAdriana Kobylak             }
1661400fd1fbSAdriana Kobylak 
1662400fd1fbSAdriana Kobylak             int fd = -1;
1663400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1664400fd1fbSAdriana Kobylak             if (fd == -1)
1665400fd1fbSAdriana Kobylak             {
1666400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1667400fd1fbSAdriana Kobylak                 return;
1668400fd1fbSAdriana Kobylak             }
1669400fd1fbSAdriana Kobylak 
1670400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1671400fd1fbSAdriana Kobylak             if (size == -1)
1672400fd1fbSAdriana Kobylak             {
1673400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1674400fd1fbSAdriana Kobylak                 return;
1675400fd1fbSAdriana Kobylak             }
1676400fd1fbSAdriana Kobylak 
1677400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1678400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1679400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1680400fd1fbSAdriana Kobylak             {
1681002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1682400fd1fbSAdriana Kobylak                                  << maxFileSize;
1683400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1684400fd1fbSAdriana Kobylak                 return;
1685400fd1fbSAdriana Kobylak             }
1686400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1687400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1688400fd1fbSAdriana Kobylak             if (rc == -1)
1689400fd1fbSAdriana Kobylak             {
1690400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1691400fd1fbSAdriana Kobylak                 return;
1692400fd1fbSAdriana Kobylak             }
1693400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1694400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1695400fd1fbSAdriana Kobylak             {
1696400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1697400fd1fbSAdriana Kobylak                 return;
1698400fd1fbSAdriana Kobylak             }
1699400fd1fbSAdriana Kobylak             close(fd);
1700400fd1fbSAdriana Kobylak 
1701400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1702002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1703400fd1fbSAdriana Kobylak 
1704400fd1fbSAdriana Kobylak             asyncResp->res.addHeader("Content-Type",
1705400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1706002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
1707400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1708400fd1fbSAdriana Kobylak             },
1709400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1710400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1711400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
17127e860f15SJohn Edward Broadbent         });
17131da66f75SEd Tanous }
17141da66f75SEd Tanous 
1715b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1716b7028ebfSSpencer Ku 
1717b7028ebfSSpencer Ku inline bool
1718b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1719b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1720b7028ebfSSpencer Ku {
1721b7028ebfSSpencer Ku     std::error_code ec;
1722b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1723b7028ebfSSpencer Ku     if (ec)
1724b7028ebfSSpencer Ku     {
1725b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1726b7028ebfSSpencer Ku         return false;
1727b7028ebfSSpencer Ku     }
1728b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1729b7028ebfSSpencer Ku     {
1730b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1731b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1732b7028ebfSSpencer Ku         // path
173311ba3979SEd Tanous         if (filename.starts_with("log"))
1734b7028ebfSSpencer Ku         {
1735b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1736b7028ebfSSpencer Ku         }
1737b7028ebfSSpencer Ku     }
1738b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1739b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1740b7028ebfSSpencer Ku     // descending order.
1741b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1742b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1743b7028ebfSSpencer Ku 
1744b7028ebfSSpencer Ku     return true;
1745b7028ebfSSpencer Ku }
1746b7028ebfSSpencer Ku 
174702cad96eSEd Tanous inline bool getHostLoggerEntries(
174802cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
174902cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
1750b7028ebfSSpencer Ku {
1751b7028ebfSSpencer Ku     GzFileReader logFile;
1752b7028ebfSSpencer Ku 
1753b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1754b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1755b7028ebfSSpencer Ku     {
1756b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1757b7028ebfSSpencer Ku         {
1758b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1759b7028ebfSSpencer Ku             return false;
1760b7028ebfSSpencer Ku         }
1761b7028ebfSSpencer Ku     }
1762b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1763b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1764b7028ebfSSpencer Ku     if (!lastMessage.empty())
1765b7028ebfSSpencer Ku     {
1766b7028ebfSSpencer Ku         logCount++;
1767b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1768b7028ebfSSpencer Ku         {
1769b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1770b7028ebfSSpencer Ku         }
1771b7028ebfSSpencer Ku     }
1772b7028ebfSSpencer Ku     return true;
1773b7028ebfSSpencer Ku }
1774b7028ebfSSpencer Ku 
1775b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1776b7028ebfSSpencer Ku                                     const std::string& msg,
17776d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
1778b7028ebfSSpencer Ku {
1779b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
17806d6574c9SJason M. Bills     logEntryJson["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
17816d6574c9SJason M. Bills     logEntryJson["@odata.id"] =
1782b7028ebfSSpencer Ku         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
17836d6574c9SJason M. Bills         logEntryID;
17846d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
17856d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
17866d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
17876d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
17886d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
17896d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
1790b7028ebfSSpencer Ku }
1791b7028ebfSSpencer Ku 
1792b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1793b7028ebfSSpencer Ku {
1794b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1795b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
17961476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
17971476687dSEd Tanous             [&app](const crow::Request& req,
17981476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17993ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
180045ca1b86SEd Tanous         {
180145ca1b86SEd Tanous             return;
180245ca1b86SEd Tanous         }
1803b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1804b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
1805b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1806b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
1807b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1808b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1809b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
18101476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
18111476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1812b7028ebfSSpencer Ku         });
1813b7028ebfSSpencer Ku }
1814b7028ebfSSpencer Ku 
1815b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1816b7028ebfSSpencer Ku {
1817b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1818b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1819b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1820002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1821002d39b4SEd Tanous             [&app](const crow::Request& req,
1822002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1823c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1824c937d2bfSEd Tanous             .canDelegateTop = true,
1825c937d2bfSEd Tanous             .canDelegateSkip = true,
1826c937d2bfSEd Tanous         };
1827c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1828c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
18293ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1830b7028ebfSSpencer Ku         {
1831b7028ebfSSpencer Ku             return;
1832b7028ebfSSpencer Ku         }
1833b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1834b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1835b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1836b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
1837b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1838b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
1839b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
18400fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1841b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
1842b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
1843b7028ebfSSpencer Ku 
1844b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1845b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1846b7028ebfSSpencer Ku         {
1847b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1848b7028ebfSSpencer Ku             return;
1849b7028ebfSSpencer Ku         }
18503648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
18513648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
18523648c8beSEd Tanous         size_t top =
18533648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
1854b7028ebfSSpencer Ku         size_t logCount = 0;
1855b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
1856b7028ebfSSpencer Ku         // control by skip and top.
1857b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
18583648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
18593648c8beSEd Tanous                                   logCount))
1860b7028ebfSSpencer Ku         {
1861b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1862b7028ebfSSpencer Ku             return;
1863b7028ebfSSpencer Ku         }
1864b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
1865b7028ebfSSpencer Ku         // log count
186626f6976fSEd Tanous         if (logEntries.empty())
1867b7028ebfSSpencer Ku         {
1868b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1869b7028ebfSSpencer Ku             return;
1870b7028ebfSSpencer Ku         }
187126f6976fSEd Tanous         if (!logEntries.empty())
1872b7028ebfSSpencer Ku         {
1873b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
1874b7028ebfSSpencer Ku             {
18756d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
18763648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
18773648c8beSEd Tanous                                         hostLogEntry);
18786d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
1879b7028ebfSSpencer Ku             }
1880b7028ebfSSpencer Ku 
1881b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
18823648c8beSEd Tanous             if (skip + top < logCount)
1883b7028ebfSSpencer Ku             {
1884b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
18850fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
18863648c8beSEd Tanous                     std::to_string(skip + top);
1887b7028ebfSSpencer Ku             }
1888b7028ebfSSpencer Ku         }
1889b7028ebfSSpencer Ku         });
1890b7028ebfSSpencer Ku }
1891b7028ebfSSpencer Ku 
1892b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1893b7028ebfSSpencer Ku {
1894b7028ebfSSpencer Ku     BMCWEB_ROUTE(
1895b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1896b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1897b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
189845ca1b86SEd Tanous             [&app](const crow::Request& req,
1899b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1900b7028ebfSSpencer Ku                    const std::string& param) {
19013ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
190245ca1b86SEd Tanous         {
190345ca1b86SEd Tanous             return;
190445ca1b86SEd Tanous         }
1905b7028ebfSSpencer Ku         const std::string& targetID = param;
1906b7028ebfSSpencer Ku 
1907b7028ebfSSpencer Ku         uint64_t idInt = 0;
1908ca45aa3cSEd Tanous 
1909ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1910ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
1911ca45aa3cSEd Tanous 
1912ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1913b7028ebfSSpencer Ku         if (ec == std::errc::invalid_argument)
1914b7028ebfSSpencer Ku         {
1915ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1916b7028ebfSSpencer Ku             return;
1917b7028ebfSSpencer Ku         }
1918b7028ebfSSpencer Ku         if (ec == std::errc::result_out_of_range)
1919b7028ebfSSpencer Ku         {
1920ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1921b7028ebfSSpencer Ku             return;
1922b7028ebfSSpencer Ku         }
1923b7028ebfSSpencer Ku 
1924b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1925b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1926b7028ebfSSpencer Ku         {
1927b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1928b7028ebfSSpencer Ku             return;
1929b7028ebfSSpencer Ku         }
1930b7028ebfSSpencer Ku 
1931b7028ebfSSpencer Ku         size_t logCount = 0;
19323648c8beSEd Tanous         size_t top = 1;
1933b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
1934b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
1935b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
1936b7028ebfSSpencer Ku         // get that entry
1937002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
1938002d39b4SEd Tanous                                   logCount))
1939b7028ebfSSpencer Ku         {
1940b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1941b7028ebfSSpencer Ku             return;
1942b7028ebfSSpencer Ku         }
1943b7028ebfSSpencer Ku 
1944b7028ebfSSpencer Ku         if (!logEntries.empty())
1945b7028ebfSSpencer Ku         {
19466d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
19476d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
19486d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
1949b7028ebfSSpencer Ku             return;
1950b7028ebfSSpencer Ku         }
1951b7028ebfSSpencer Ku 
1952b7028ebfSSpencer Ku         // Requested ID was not found
1953ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1954b7028ebfSSpencer Ku         });
1955b7028ebfSSpencer Ku }
1956b7028ebfSSpencer Ku 
1957fdd26906SClaire Weinan constexpr char const* dumpManagerIface =
1958fdd26906SClaire Weinan     "xyz.openbmc_project.Collection.DeleteAll";
1959fdd26906SClaire Weinan inline void handleLogServicesCollectionGet(
1960fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
1961fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
19621da66f75SEd Tanous {
19633ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
196445ca1b86SEd Tanous     {
196545ca1b86SEd Tanous         return;
196645ca1b86SEd Tanous     }
19677e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
19687e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
1969e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
19701da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
1971e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
1972e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
1973002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1974e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
19751da66f75SEd Tanous         "Collection of LogServices for this Manager";
1976002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1977c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
1978fdd26906SClaire Weinan 
1979c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1980c4bf6374SJason M. Bills     logServiceArray.push_back(
1981002d39b4SEd Tanous         {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1982c4bf6374SJason M. Bills #endif
1983fdd26906SClaire Weinan 
1984fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
1985fdd26906SClaire Weinan 
1986fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
1987fdd26906SClaire Weinan     auto respHandler =
1988fdd26906SClaire Weinan         [asyncResp](
1989fdd26906SClaire Weinan             const boost::system::error_code ec,
1990fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
1991fdd26906SClaire Weinan         if (ec)
1992fdd26906SClaire Weinan         {
1993fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
1994fdd26906SClaire Weinan                 << "handleLogServicesCollectionGet respHandler got error "
1995fdd26906SClaire Weinan                 << ec;
1996fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
1997fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
1998fdd26906SClaire Weinan             return;
1999fdd26906SClaire Weinan         }
2000fdd26906SClaire Weinan 
2001fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2002fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2003fdd26906SClaire Weinan 
2004fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2005fdd26906SClaire Weinan         {
2006fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2007fdd26906SClaire Weinan             {
2008fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2009fdd26906SClaire Weinan                     {{"@odata.id",
2010fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
2011fdd26906SClaire Weinan             }
2012fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2013fdd26906SClaire Weinan             {
2014fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2015fdd26906SClaire Weinan                     {{"@odata.id",
2016fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/FaultLog"}});
2017fdd26906SClaire Weinan             }
2018fdd26906SClaire Weinan         }
2019fdd26906SClaire Weinan 
2020e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2021fdd26906SClaire Weinan             logServiceArrayLocal.size();
2022fdd26906SClaire Weinan     };
2023fdd26906SClaire Weinan 
2024fdd26906SClaire Weinan     crow::connections::systemBus->async_method_call(
2025fdd26906SClaire Weinan         respHandler, "xyz.openbmc_project.ObjectMapper",
2026fdd26906SClaire Weinan         "/xyz/openbmc_project/object_mapper",
2027fdd26906SClaire Weinan         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2028fdd26906SClaire Weinan         "/xyz/openbmc_project/dump", 0,
2029fdd26906SClaire Weinan         std::array<const char*, 1>{dumpManagerIface});
2030fdd26906SClaire Weinan #endif
2031fdd26906SClaire Weinan }
2032fdd26906SClaire Weinan 
2033fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2034fdd26906SClaire Weinan {
2035fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2036fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2037fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2038fdd26906SClaire Weinan             std::bind_front(handleLogServicesCollectionGet, std::ref(app)));
2039e1f26343SJason M. Bills }
2040e1f26343SJason M. Bills 
20417e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2042e1f26343SJason M. Bills {
20437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2044ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
20457e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
204645ca1b86SEd Tanous             [&app](const crow::Request& req,
204745ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
20483ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20497e860f15SJohn Edward Broadbent         {
205045ca1b86SEd Tanous             return;
205145ca1b86SEd Tanous         }
2052e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2053e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
20540f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20550f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2056002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2057002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2058c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
2059e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
20607c8c4058STejas Patil 
20617c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20627c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
2063002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20647c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20657c8c4058STejas Patil             redfishDateTimeOffset.second;
20667c8c4058STejas Patil 
20671476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
20681476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
20697e860f15SJohn Edward Broadbent         });
2070e1f26343SJason M. Bills }
2071e1f26343SJason M. Bills 
20723a48b3a2SJason M. Bills static int
20733a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2074e1f26343SJason M. Bills                                sd_journal* journal,
20753a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2076e1f26343SJason M. Bills {
2077e1f26343SJason M. Bills     // Get the Log Entry contents
2078e1f26343SJason M. Bills     int ret = 0;
2079e1f26343SJason M. Bills 
2080a8fe54f0SJason M. Bills     std::string message;
2081a8fe54f0SJason M. Bills     std::string_view syslogID;
2082a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2083a8fe54f0SJason M. Bills     if (ret < 0)
2084a8fe54f0SJason M. Bills     {
2085a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2086a8fe54f0SJason M. Bills                          << strerror(-ret);
2087a8fe54f0SJason M. Bills     }
2088a8fe54f0SJason M. Bills     if (!syslogID.empty())
2089a8fe54f0SJason M. Bills     {
2090a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2091a8fe54f0SJason M. Bills     }
2092a8fe54f0SJason M. Bills 
209339e77504SEd Tanous     std::string_view msg;
209416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2095e1f26343SJason M. Bills     if (ret < 0)
2096e1f26343SJason M. Bills     {
2097e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2098e1f26343SJason M. Bills         return 1;
2099e1f26343SJason M. Bills     }
2100a8fe54f0SJason M. Bills     message += std::string(msg);
2101e1f26343SJason M. Bills 
2102e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2103271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
210416428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2105e1f26343SJason M. Bills     if (ret < 0)
2106e1f26343SJason M. Bills     {
2107e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2108e1f26343SJason M. Bills     }
2109e1f26343SJason M. Bills 
2110e1f26343SJason M. Bills     // Get the Created time from the timestamp
211116428a1aSJason M. Bills     std::string entryTimeStr;
211216428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2113e1f26343SJason M. Bills     {
211416428a1aSJason M. Bills         return 1;
2115e1f26343SJason M. Bills     }
2116e1f26343SJason M. Bills 
2117e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
211884afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
211984afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.id"] =
212084afc48bSJason M. Bills         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
212184afc48bSJason M. Bills         bmcJournalLogEntryID;
212284afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
212384afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
212484afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
212584afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
212684afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2127738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
212884afc48bSJason M. Bills                                                          : "OK";
212984afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
213084afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2131e1f26343SJason M. Bills     return 0;
2132e1f26343SJason M. Bills }
2133e1f26343SJason M. Bills 
21347e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2135e1f26343SJason M. Bills {
21367e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2137ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2138002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2139002d39b4SEd Tanous             [&app](const crow::Request& req,
2140002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2141c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2142c937d2bfSEd Tanous             .canDelegateTop = true,
2143c937d2bfSEd Tanous             .canDelegateSkip = true,
2144c937d2bfSEd Tanous         };
2145c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2146c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21473ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2148193ad2faSJason M. Bills         {
2149193ad2faSJason M. Bills             return;
2150193ad2faSJason M. Bills         }
21513648c8beSEd Tanous 
21523648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21533648c8beSEd Tanous         size_t top =
21543648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
21553648c8beSEd Tanous 
21567e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
21577e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2158e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2159e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
21600f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
21610f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2162e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2163e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2164e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
21650fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2166e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2167e1f26343SJason M. Bills 
21687e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
21697e860f15SJohn Edward Broadbent         // unique ID for each entry
2170e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2171e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2172e1f26343SJason M. Bills         if (ret < 0)
2173e1f26343SJason M. Bills         {
2174002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2175f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2176e1f26343SJason M. Bills             return;
2177e1f26343SJason M. Bills         }
21780fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
21790fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2180e1f26343SJason M. Bills         journalTmp = nullptr;
2181b01bf299SEd Tanous         uint64_t entryCount = 0;
2182e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2183e85d6b16SJason M. Bills         bool firstEntry = true;
2184e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2185e1f26343SJason M. Bills         {
2186193ad2faSJason M. Bills             entryCount++;
21877e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
21887e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
21893648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2190193ad2faSJason M. Bills             {
2191193ad2faSJason M. Bills                 continue;
2192193ad2faSJason M. Bills             }
2193193ad2faSJason M. Bills 
219416428a1aSJason M. Bills             std::string idStr;
2195e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2196e1f26343SJason M. Bills             {
2197e1f26343SJason M. Bills                 continue;
2198e1f26343SJason M. Bills             }
2199e85d6b16SJason M. Bills             firstEntry = false;
2200e85d6b16SJason M. Bills 
22013a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2202c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2203c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2204e1f26343SJason M. Bills             {
2205f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2206e1f26343SJason M. Bills                 return;
2207e1f26343SJason M. Bills             }
22083a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2209e1f26343SJason M. Bills         }
2210193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
22113648c8beSEd Tanous         if (skip + top < entryCount)
2212193ad2faSJason M. Bills         {
2213193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
22140fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
22153648c8beSEd Tanous                 std::to_string(skip + top);
2216193ad2faSJason M. Bills         }
22177e860f15SJohn Edward Broadbent         });
2218e1f26343SJason M. Bills }
2219e1f26343SJason M. Bills 
22207e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2221e1f26343SJason M. Bills {
22227e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
22237e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2224ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
22257e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
222645ca1b86SEd Tanous             [&app](const crow::Request& req,
22277e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22287e860f15SJohn Edward Broadbent                    const std::string& entryID) {
22293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
223045ca1b86SEd Tanous         {
223145ca1b86SEd Tanous             return;
223245ca1b86SEd Tanous         }
2233e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2234e1f26343SJason M. Bills         uint64_t ts = 0;
2235271584abSEd Tanous         uint64_t index = 0;
22368d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2237e1f26343SJason M. Bills         {
223816428a1aSJason M. Bills             return;
2239e1f26343SJason M. Bills         }
2240e1f26343SJason M. Bills 
2241e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2242e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2243e1f26343SJason M. Bills         if (ret < 0)
2244e1f26343SJason M. Bills         {
2245002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2246f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2247e1f26343SJason M. Bills             return;
2248e1f26343SJason M. Bills         }
2249002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2250002d39b4SEd Tanous             journalTmp, sd_journal_close);
2251e1f26343SJason M. Bills         journalTmp = nullptr;
22527e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
22537e860f15SJohn Edward Broadbent         // index tracking the unique ID
2254af07e3f5SJason M. Bills         std::string idStr;
2255af07e3f5SJason M. Bills         bool firstEntry = true;
2256e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
22572056b6d1SManojkiran Eda         if (ret < 0)
22582056b6d1SManojkiran Eda         {
22592056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
22602056b6d1SManojkiran Eda                              << strerror(-ret);
22612056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
22622056b6d1SManojkiran Eda             return;
22632056b6d1SManojkiran Eda         }
2264271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2265e1f26343SJason M. Bills         {
2266e1f26343SJason M. Bills             sd_journal_next(journal.get());
2267af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2268af07e3f5SJason M. Bills             {
2269af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2270af07e3f5SJason M. Bills                 return;
2271af07e3f5SJason M. Bills             }
2272af07e3f5SJason M. Bills             firstEntry = false;
2273af07e3f5SJason M. Bills         }
2274c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2275af07e3f5SJason M. Bills         if (idStr != entryID)
2276c4bf6374SJason M. Bills         {
2277ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2278c4bf6374SJason M. Bills             return;
2279c4bf6374SJason M. Bills         }
2280c4bf6374SJason M. Bills 
22813a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2282c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
22833a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2284e1f26343SJason M. Bills         {
2285f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2286e1f26343SJason M. Bills             return;
2287e1f26343SJason M. Bills         }
2288d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
22897e860f15SJohn Edward Broadbent         });
2290c9bb6861Sraviteja-b }
2291c9bb6861Sraviteja-b 
2292fdd26906SClaire Weinan inline void
2293fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2294fdd26906SClaire Weinan                        const std::string& dumpType)
2295c9bb6861Sraviteja-b {
2296fdd26906SClaire Weinan     std::string dumpPath;
2297fdd26906SClaire Weinan     std::string overWritePolicy;
2298fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2299fdd26906SClaire Weinan 
2300fdd26906SClaire Weinan     if (dumpType == "BMC")
230145ca1b86SEd Tanous     {
2302fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2303fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2304fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2305fdd26906SClaire Weinan     }
2306fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2307fdd26906SClaire Weinan     {
2308fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2309fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2310fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2311fdd26906SClaire Weinan     }
2312fdd26906SClaire Weinan     else if (dumpType == "System")
2313fdd26906SClaire Weinan     {
2314fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2315fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2316fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2317fdd26906SClaire Weinan     }
2318fdd26906SClaire Weinan     else
2319fdd26906SClaire Weinan     {
2320fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2321fdd26906SClaire Weinan                          << dumpType;
2322fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
232345ca1b86SEd Tanous         return;
232445ca1b86SEd Tanous     }
2325fdd26906SClaire Weinan 
2326fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2327fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2328c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2329fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2330fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2331fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
23327c8c4058STejas Patil 
23337c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
23347c8c4058STejas Patil         crow::utility::getDateTimeOffsetNow();
23350fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23367c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23377c8c4058STejas Patil         redfishDateTimeOffset.second;
23387c8c4058STejas Patil 
2339fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2340002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2341fdd26906SClaire Weinan         dumpPath + "/Actions/LogService.ClearLog";
2342fdd26906SClaire Weinan 
2343fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2344fdd26906SClaire Weinan     {
2345002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
23461476687dSEd Tanous                                 ["target"] =
2347fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2348fdd26906SClaire Weinan     }
2349c9bb6861Sraviteja-b }
2350c9bb6861Sraviteja-b 
2351fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2352fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2353fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23547e860f15SJohn Edward Broadbent {
23553ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
235645ca1b86SEd Tanous     {
235745ca1b86SEd Tanous         return;
235845ca1b86SEd Tanous     }
2359fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2360fdd26906SClaire Weinan }
2361c9bb6861Sraviteja-b 
2362fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2363fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2364fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2365fdd26906SClaire Weinan {
2366fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2367fdd26906SClaire Weinan     {
2368fdd26906SClaire Weinan         return;
2369fdd26906SClaire Weinan     }
2370fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2371fdd26906SClaire Weinan }
2372fdd26906SClaire Weinan 
2373fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2374fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2375fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2376fdd26906SClaire Weinan     const std::string& dumpId)
2377fdd26906SClaire Weinan {
2378fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2379fdd26906SClaire Weinan     {
2380fdd26906SClaire Weinan         return;
2381fdd26906SClaire Weinan     }
2382fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2383fdd26906SClaire Weinan }
2384fdd26906SClaire Weinan 
2385fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2386fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2387fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2388fdd26906SClaire Weinan     const std::string& dumpId)
2389fdd26906SClaire Weinan {
2390fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2391fdd26906SClaire Weinan     {
2392fdd26906SClaire Weinan         return;
2393fdd26906SClaire Weinan     }
2394fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2395fdd26906SClaire Weinan }
2396fdd26906SClaire Weinan 
2397fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2398fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2399fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2400fdd26906SClaire Weinan {
2401fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2402fdd26906SClaire Weinan     {
2403fdd26906SClaire Weinan         return;
2404fdd26906SClaire Weinan     }
2405fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2406fdd26906SClaire Weinan }
2407fdd26906SClaire Weinan 
2408fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2409fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2410fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2411fdd26906SClaire Weinan {
2412fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2413fdd26906SClaire Weinan     {
2414fdd26906SClaire Weinan         return;
2415fdd26906SClaire Weinan     }
2416fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2417fdd26906SClaire Weinan }
2418fdd26906SClaire Weinan 
2419fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2420fdd26906SClaire Weinan {
2421fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2422fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2423fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2424fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2425fdd26906SClaire Weinan }
2426fdd26906SClaire Weinan 
2427fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2428fdd26906SClaire Weinan {
2429fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2430fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2431fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2432fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2433c9bb6861Sraviteja-b }
2434c9bb6861Sraviteja-b 
24357e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2436c9bb6861Sraviteja-b {
24377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24387e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2439ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2440fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2441fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2442fdd26906SClaire Weinan 
24437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24447e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2445ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2446fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2447fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2448c9bb6861Sraviteja-b }
2449c9bb6861Sraviteja-b 
24507e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2451c9bb6861Sraviteja-b {
24520fda0f12SGeorge Liu     BMCWEB_ROUTE(
24530fda0f12SGeorge Liu         app,
24540fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2455ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24567e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2457fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2458fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2459a43be80fSAsmitha Karunanithi }
2460a43be80fSAsmitha Karunanithi 
24617e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
246280319af1SAsmitha Karunanithi {
24630fda0f12SGeorge Liu     BMCWEB_ROUTE(
24640fda0f12SGeorge Liu         app,
24650fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2466ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2467fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2468fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
246945ca1b86SEd Tanous }
2470fdd26906SClaire Weinan 
2471fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2472fdd26906SClaire Weinan {
2473fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2474fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2475fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2476fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2477fdd26906SClaire Weinan }
2478fdd26906SClaire Weinan 
2479fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2480fdd26906SClaire Weinan {
2481fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2482fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2483fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2484fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2485fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2486fdd26906SClaire Weinan }
2487fdd26906SClaire Weinan 
2488fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2489fdd26906SClaire Weinan {
2490fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2491fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2492fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2493fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2494fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2495fdd26906SClaire Weinan 
2496fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2497fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2498fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2499fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2500fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2501fdd26906SClaire Weinan }
2502fdd26906SClaire Weinan 
2503fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2504fdd26906SClaire Weinan {
2505fdd26906SClaire Weinan     BMCWEB_ROUTE(
2506fdd26906SClaire Weinan         app,
2507fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2508fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2509fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2510fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
25115cb1dd27SAsmitha Karunanithi }
25125cb1dd27SAsmitha Karunanithi 
25137e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
25145cb1dd27SAsmitha Karunanithi {
25157e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2516ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
2517002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2518002d39b4SEd Tanous             [&app](const crow::Request& req,
2519002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25203ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
25217e860f15SJohn Edward Broadbent         {
252245ca1b86SEd Tanous             return;
252345ca1b86SEd Tanous         }
25245cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
25255cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump";
25265cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
2527d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
25285cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "Dump LogService";
252945ca1b86SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Dump LogService";
25305cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
25315cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
25327c8c4058STejas Patil 
25337c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
25347c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
253545ca1b86SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25367c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25377c8c4058STejas Patil             redfishDateTimeOffset.second;
25387c8c4058STejas Patil 
25391476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
25401476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2541002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
25421476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog";
25431476687dSEd Tanous 
2544002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
25451476687dSEd Tanous                                 ["target"] =
25461476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData";
25477e860f15SJohn Edward Broadbent         });
25485cb1dd27SAsmitha Karunanithi }
25495cb1dd27SAsmitha Karunanithi 
25507e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
25517e860f15SJohn Edward Broadbent {
25527e860f15SJohn Edward Broadbent 
25535cb1dd27SAsmitha Karunanithi     /**
25545cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25555cb1dd27SAsmitha Karunanithi      */
2556b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2557ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25587e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
255945ca1b86SEd Tanous             [&app](const crow::Request& req,
2560864d6a17SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25613ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
256245ca1b86SEd Tanous         {
256345ca1b86SEd Tanous             return;
256445ca1b86SEd Tanous         }
25655cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "System");
25667e860f15SJohn Edward Broadbent         });
25675cb1dd27SAsmitha Karunanithi }
25685cb1dd27SAsmitha Karunanithi 
25697e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
25705cb1dd27SAsmitha Karunanithi {
25717e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2572864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2573ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2574ed398213SEd Tanous 
25757e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
257645ca1b86SEd Tanous             [&app](const crow::Request& req,
25777e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25787e860f15SJohn Edward Broadbent                    const std::string& param) {
25793ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2580c7a6d660SClaire Weinan         {
2581c7a6d660SClaire Weinan             return;
2582c7a6d660SClaire Weinan         }
2583c7a6d660SClaire Weinan         getDumpEntryById(asyncResp, param, "System");
25847e860f15SJohn Edward Broadbent         });
25858d1b46d7Szhanghch05 
25867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2587864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2588ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25897e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
259045ca1b86SEd Tanous             [&app](const crow::Request& req,
25917e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25927e860f15SJohn Edward Broadbent                    const std::string& param) {
25933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
259445ca1b86SEd Tanous         {
259545ca1b86SEd Tanous             return;
259645ca1b86SEd Tanous         }
25977e860f15SJohn Edward Broadbent         deleteDumpEntry(asyncResp, param, "system");
25987e860f15SJohn Edward Broadbent         });
25995cb1dd27SAsmitha Karunanithi }
2600c9bb6861Sraviteja-b 
26017e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2602c9bb6861Sraviteja-b {
26030fda0f12SGeorge Liu     BMCWEB_ROUTE(
26040fda0f12SGeorge Liu         app,
26050fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2606ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
260845ca1b86SEd Tanous             [&app](const crow::Request& req,
260945ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26103ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
261145ca1b86SEd Tanous         {
261245ca1b86SEd Tanous             return;
261345ca1b86SEd Tanous         }
261445ca1b86SEd Tanous         createDump(asyncResp, req, "System");
261545ca1b86SEd Tanous         });
2616a43be80fSAsmitha Karunanithi }
2617a43be80fSAsmitha Karunanithi 
26187e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2619a43be80fSAsmitha Karunanithi {
26200fda0f12SGeorge Liu     BMCWEB_ROUTE(
26210fda0f12SGeorge Liu         app,
26220fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2623ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
262545ca1b86SEd Tanous             [&app](const crow::Request& req,
26267e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26277e860f15SJohn Edward Broadbent 
262845ca1b86SEd Tanous             {
26293ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
263045ca1b86SEd Tanous         {
263145ca1b86SEd Tanous             return;
263245ca1b86SEd Tanous         }
263345ca1b86SEd Tanous         clearDump(asyncResp, "System");
263445ca1b86SEd Tanous         });
2635013487e5Sraviteja-b }
2636013487e5Sraviteja-b 
26377e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
26381da66f75SEd Tanous {
26393946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26403946028dSAppaRao Puli     // method for security reasons.
26411da66f75SEd Tanous     /**
26421da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26431da66f75SEd Tanous      */
26447e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2645ed398213SEd Tanous         // This is incorrect, should be:
2646ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2647432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2648002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2649002d39b4SEd Tanous             [&app](const crow::Request& req,
2650002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26513ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
265245ca1b86SEd Tanous         {
265345ca1b86SEd Tanous             return;
265445ca1b86SEd Tanous         }
26557e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
26567e860f15SJohn Edward Broadbent         // SubRoute
26570f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2658424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2659e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
26608e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
26614f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26624f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26634f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2664e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2665e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
26667c8c4058STejas Patil 
26677c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
26687c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
26697c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26707c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26717c8c4058STejas Patil             redfishDateTimeOffset.second;
26727c8c4058STejas Patil 
26731476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
26741476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2675002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
26761476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2677002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26781476687dSEd Tanous                                 ["target"] =
26791476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
26807e860f15SJohn Edward Broadbent         });
26811da66f75SEd Tanous }
26821da66f75SEd Tanous 
26837e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
26845b61b5e8SJason M. Bills {
26850fda0f12SGeorge Liu     BMCWEB_ROUTE(
26860fda0f12SGeorge Liu         app,
26870fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2688ed398213SEd Tanous         // This is incorrect, should be:
2689ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2690432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26917e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
269245ca1b86SEd Tanous             [&app](const crow::Request& req,
26937e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26943ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
269545ca1b86SEd Tanous         {
269645ca1b86SEd Tanous             return;
269745ca1b86SEd Tanous         }
26985b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
26995b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
2700cb13a392SEd Tanous                         const std::string&) {
27015b61b5e8SJason M. Bills             if (ec)
27025b61b5e8SJason M. Bills             {
27035b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
27045b61b5e8SJason M. Bills                 return;
27055b61b5e8SJason M. Bills             }
27065b61b5e8SJason M. Bills             messages::success(asyncResp->res);
27075b61b5e8SJason M. Bills             },
2708002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
27097e860f15SJohn Edward Broadbent         });
27105b61b5e8SJason M. Bills }
27115b61b5e8SJason M. Bills 
27128d1b46d7Szhanghch05 static void
27138d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27148d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2715e855dd28SJason M. Bills {
2716043a0536SJohnathan Mantey     auto getStoredLogCallback =
2717b9d36b47SEd Tanous         [asyncResp, logID,
2718b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2719b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2720e855dd28SJason M. Bills         if (ec)
2721e855dd28SJason M. Bills         {
2722e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
27231ddcf01aSJason M. Bills             if (ec.value() ==
27241ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
27251ddcf01aSJason M. Bills             {
2726002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
27271ddcf01aSJason M. Bills             }
27281ddcf01aSJason M. Bills             else
27291ddcf01aSJason M. Bills             {
2730e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
27311ddcf01aSJason M. Bills             }
2732e855dd28SJason M. Bills             return;
2733e855dd28SJason M. Bills         }
2734043a0536SJohnathan Mantey 
2735043a0536SJohnathan Mantey         std::string timestamp{};
2736043a0536SJohnathan Mantey         std::string filename{};
2737043a0536SJohnathan Mantey         std::string logfile{};
27382c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
2739043a0536SJohnathan Mantey 
2740043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
2741e855dd28SJason M. Bills         {
2742002d39b4SEd Tanous             messages::resourceMissingAtURI(asyncResp->res,
2743002d39b4SEd Tanous                                            crow::utility::urlFromPieces(logID));
2744e855dd28SJason M. Bills             return;
2745e855dd28SJason M. Bills         }
2746e855dd28SJason M. Bills 
2747043a0536SJohnathan Mantey         std::string crashdumpURI =
2748e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2749043a0536SJohnathan Mantey             logID + "/" + filename;
275084afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
275184afc48bSJason M. Bills         logEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
275284afc48bSJason M. Bills         logEntry["@odata.id"] =
275384afc48bSJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
275484afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
275584afc48bSJason M. Bills         logEntry["Id"] = logID;
275684afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
275784afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
275884afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
275984afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
276084afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
27612b20ef6eSJason M. Bills 
27622b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
27632b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
27642b20ef6eSJason M. Bills         // directly
27652b20ef6eSJason M. Bills         if (logEntryJson.is_array())
27662b20ef6eSJason M. Bills         {
27672b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
27682b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
27692b20ef6eSJason M. Bills                 logEntryJson.size();
27702b20ef6eSJason M. Bills         }
27712b20ef6eSJason M. Bills         else
27722b20ef6eSJason M. Bills         {
2773d405bb51SJason M. Bills             logEntryJson.update(logEntry);
27742b20ef6eSJason M. Bills         }
2775e855dd28SJason M. Bills     };
2776e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
27775b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
27785b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2779043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2780e855dd28SJason M. Bills }
2781e855dd28SJason M. Bills 
27827e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
27831da66f75SEd Tanous {
27843946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27853946028dSAppaRao Puli     // method for security reasons.
27861da66f75SEd Tanous     /**
27871da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
27881da66f75SEd Tanous      */
27897e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
27907e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2791ed398213SEd Tanous         // This is incorrect, should be.
2792ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2793432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2794002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2795002d39b4SEd Tanous             [&app](const crow::Request& req,
2796002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27973ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
279845ca1b86SEd Tanous         {
279945ca1b86SEd Tanous             return;
280045ca1b86SEd Tanous         }
28012b20ef6eSJason M. Bills         crow::connections::systemBus->async_method_call(
28022b20ef6eSJason M. Bills             [asyncResp](const boost::system::error_code ec,
28032b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
28041da66f75SEd Tanous             if (ec)
28051da66f75SEd Tanous             {
28061da66f75SEd Tanous                 if (ec.value() !=
28071da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
28081da66f75SEd Tanous                 {
28091da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
28101da66f75SEd Tanous                                      << ec.message();
2811f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
28121da66f75SEd Tanous                     return;
28131da66f75SEd Tanous                 }
28141da66f75SEd Tanous             }
2815e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
28161da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
28170f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2818424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2819002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2820e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2821424c4176SJason M. Bills                 "Collection of Crashdump Entries";
2822002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2823a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
28242b20ef6eSJason M. Bills 
28252b20ef6eSJason M. Bills             for (const std::string& path : resp)
28261da66f75SEd Tanous             {
28272b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
2828e855dd28SJason M. Bills                 // Get the log ID
28292b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
28302b20ef6eSJason M. Bills                 if (logID.empty())
28311da66f75SEd Tanous                 {
2832e855dd28SJason M. Bills                     continue;
28331da66f75SEd Tanous                 }
2834e855dd28SJason M. Bills                 // Add the log entry to the array
28352b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
28362b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
28371da66f75SEd Tanous             }
28382b20ef6eSJason M. Bills             },
28391da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
28401da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
28411da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
28425b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
28437e860f15SJohn Edward Broadbent         });
28441da66f75SEd Tanous }
28451da66f75SEd Tanous 
28467e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28471da66f75SEd Tanous {
28483946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28493946028dSAppaRao Puli     // method for security reasons.
28501da66f75SEd Tanous 
28517e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28527e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2853ed398213SEd Tanous         // this is incorrect, should be
2854ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2855432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28567e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
285745ca1b86SEd Tanous             [&app](const crow::Request& req,
28587e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28597e860f15SJohn Edward Broadbent                    const std::string& param) {
28603ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
286145ca1b86SEd Tanous         {
286245ca1b86SEd Tanous             return;
286345ca1b86SEd Tanous         }
28647e860f15SJohn Edward Broadbent         const std::string& logID = param;
2865e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
28667e860f15SJohn Edward Broadbent         });
2867e855dd28SJason M. Bills }
2868e855dd28SJason M. Bills 
28697e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2870e855dd28SJason M. Bills {
28713946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28723946028dSAppaRao Puli     // method for security reasons.
28737e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28747e860f15SJohn Edward Broadbent         app,
28757e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2876ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
28777e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2878a4ce114aSNan Zhou             [](const crow::Request& req,
28797e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28807e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
28812a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
28822a9beeedSShounak Mitra         // Redfish resource.
2883043a0536SJohnathan Mantey         auto getStoredLogCallback =
2884002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
2885abf2add6SEd Tanous                 const boost::system::error_code ec,
2886002d39b4SEd Tanous                 const std::vector<
2887002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
28887e860f15SJohn Edward Broadbent                     resp) {
28891da66f75SEd Tanous             if (ec)
28901da66f75SEd Tanous             {
2891002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2892f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
28931da66f75SEd Tanous                 return;
28941da66f75SEd Tanous             }
2895e855dd28SJason M. Bills 
2896043a0536SJohnathan Mantey             std::string dbusFilename{};
2897043a0536SJohnathan Mantey             std::string dbusTimestamp{};
2898043a0536SJohnathan Mantey             std::string dbusFilepath{};
2899043a0536SJohnathan Mantey 
2900002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2901002d39b4SEd Tanous                                      dbusFilepath);
2902043a0536SJohnathan Mantey 
2903043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
2904043a0536SJohnathan Mantey                 dbusFilepath.empty())
29051da66f75SEd Tanous             {
2906ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
29071da66f75SEd Tanous                 return;
29081da66f75SEd Tanous             }
2909e855dd28SJason M. Bills 
2910043a0536SJohnathan Mantey             // Verify the file name parameter is correct
2911043a0536SJohnathan Mantey             if (fileName != dbusFilename)
2912043a0536SJohnathan Mantey             {
2913ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2914043a0536SJohnathan Mantey                 return;
2915043a0536SJohnathan Mantey             }
2916043a0536SJohnathan Mantey 
2917043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
2918043a0536SJohnathan Mantey             {
2919ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2920043a0536SJohnathan Mantey                 return;
2921043a0536SJohnathan Mantey             }
2922002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
2923002d39b4SEd Tanous             asyncResp->res.body() =
2924002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
2925043a0536SJohnathan Mantey 
29267e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
29277e860f15SJohn Edward Broadbent             // from a browser
2928002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Disposition", "attachment");
29291da66f75SEd Tanous         };
29301da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
29315b61b5e8SJason M. Bills             std::move(getStoredLogCallback), crashdumpObject,
29325b61b5e8SJason M. Bills             crashdumpPath + std::string("/") + logID,
2933002d39b4SEd Tanous             "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
29347e860f15SJohn Edward Broadbent         });
29351da66f75SEd Tanous }
29361da66f75SEd Tanous 
2937c5a4c82aSJason M. Bills enum class OEMDiagnosticType
2938c5a4c82aSJason M. Bills {
2939c5a4c82aSJason M. Bills     onDemand,
2940c5a4c82aSJason M. Bills     telemetry,
2941c5a4c82aSJason M. Bills     invalid,
2942c5a4c82aSJason M. Bills };
2943c5a4c82aSJason M. Bills 
2944f7725d79SEd Tanous inline OEMDiagnosticType
2945f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
2946c5a4c82aSJason M. Bills {
2947c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
2948c5a4c82aSJason M. Bills     {
2949c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
2950c5a4c82aSJason M. Bills     }
2951c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
2952c5a4c82aSJason M. Bills     {
2953c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
2954c5a4c82aSJason M. Bills     }
2955c5a4c82aSJason M. Bills 
2956c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
2957c5a4c82aSJason M. Bills }
2958c5a4c82aSJason M. Bills 
29597e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
29601da66f75SEd Tanous {
29613946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29623946028dSAppaRao Puli     // method for security reasons.
29630fda0f12SGeorge Liu     BMCWEB_ROUTE(
29640fda0f12SGeorge Liu         app,
29650fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2966ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2967ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2968432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2969002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
2970002d39b4SEd Tanous             [&app](const crow::Request& req,
29717e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
29723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
297345ca1b86SEd Tanous         {
297445ca1b86SEd Tanous             return;
297545ca1b86SEd Tanous         }
29768e6c099aSJason M. Bills         std::string diagnosticDataType;
29778e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
297815ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
2979002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
2980002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
29818e6c099aSJason M. Bills         {
29828e6c099aSJason M. Bills             return;
29838e6c099aSJason M. Bills         }
29848e6c099aSJason M. Bills 
29858e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
29868e6c099aSJason M. Bills         {
29878e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
29888e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
29898e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
29908e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29918e6c099aSJason M. Bills                 "CollectDiagnosticData");
29928e6c099aSJason M. Bills             return;
29938e6c099aSJason M. Bills         }
29948e6c099aSJason M. Bills 
2995c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
2996c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
2997c5a4c82aSJason M. Bills 
2998c5a4c82aSJason M. Bills         std::string iface;
2999c5a4c82aSJason M. Bills         std::string method;
3000c5a4c82aSJason M. Bills         std::string taskMatchStr;
3001c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3002c5a4c82aSJason M. Bills         {
3003c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3004c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3005c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3006c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3007c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3008c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3009c5a4c82aSJason M. Bills         }
3010c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3011c5a4c82aSJason M. Bills         {
3012c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3013c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3014c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3015c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3016c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3017c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3018c5a4c82aSJason M. Bills         }
3019c5a4c82aSJason M. Bills         else
3020c5a4c82aSJason M. Bills         {
3021c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3022c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3023c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3024002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3025002d39b4SEd Tanous                 "CollectDiagnosticData");
3026c5a4c82aSJason M. Bills             return;
3027c5a4c82aSJason M. Bills         }
3028c5a4c82aSJason M. Bills 
3029c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3030c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3031c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
303298be3e39SEd Tanous                            const std::string&) mutable {
30331da66f75SEd Tanous             if (ec)
30341da66f75SEd Tanous             {
3035002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
30361da66f75SEd Tanous                 {
3037f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
30381da66f75SEd Tanous                 }
30394363d3b2SJason M. Bills                 else if (ec.value() ==
30404363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
30414363d3b2SJason M. Bills                 {
3042002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3043002d39b4SEd Tanous                                                             "60");
30444363d3b2SJason M. Bills                 }
30451da66f75SEd Tanous                 else
30461da66f75SEd Tanous                 {
3047f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
30481da66f75SEd Tanous                 }
30491da66f75SEd Tanous                 return;
30501da66f75SEd Tanous             }
3051002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
305259d494eeSPatrick Williams                 [](boost::system::error_code err, sdbusplus::message_t&,
3053002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
305466afe4faSJames Feist                 if (!err)
305566afe4faSJames Feist                 {
3056002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3057e5d5006bSJames Feist                         std::to_string(taskData->index)));
3058831d6b09SJames Feist                     taskData->state = "Completed";
305966afe4faSJames Feist                 }
306032898ceaSJames Feist                 return task::completed;
306166afe4faSJames Feist                 },
3062c5a4c82aSJason M. Bills                 taskMatchStr);
3063c5a4c82aSJason M. Bills 
306446229577SJames Feist             task->startTimer(std::chrono::minutes(5));
306546229577SJames Feist             task->populateResp(asyncResp->res);
306698be3e39SEd Tanous             task->payload.emplace(std::move(payload));
30671da66f75SEd Tanous         };
30688e6c099aSJason M. Bills 
30691da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3070002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3071002d39b4SEd Tanous             iface, method);
30727e860f15SJohn Edward Broadbent         });
30736eda7685SKenny L. Ku }
30746eda7685SKenny L. Ku 
3075cb92c03bSAndrew Geissler /**
3076cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3077cb92c03bSAndrew Geissler  */
30787e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3079cb92c03bSAndrew Geissler {
3080cb92c03bSAndrew Geissler     /**
3081cb92c03bSAndrew Geissler      * Function handles POST method request.
3082cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3083cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3084cb92c03bSAndrew Geissler      */
30857e860f15SJohn Edward Broadbent 
30860fda0f12SGeorge Liu     BMCWEB_ROUTE(
30870fda0f12SGeorge Liu         app,
30880fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3089ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
30907e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
309145ca1b86SEd Tanous             [&app](const crow::Request& req,
30927e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
30933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
309445ca1b86SEd Tanous         {
309545ca1b86SEd Tanous             return;
309645ca1b86SEd Tanous         }
3097cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3098cb92c03bSAndrew Geissler 
3099cb92c03bSAndrew Geissler         // Process response from Logging service.
3100002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3101002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3102cb92c03bSAndrew Geissler             if (ec)
3103cb92c03bSAndrew Geissler             {
3104cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3105002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3106cb92c03bSAndrew Geissler                 asyncResp->res.result(
3107cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3108cb92c03bSAndrew Geissler                 return;
3109cb92c03bSAndrew Geissler             }
3110cb92c03bSAndrew Geissler 
3111002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3112cb92c03bSAndrew Geissler         };
3113cb92c03bSAndrew Geissler 
3114cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3115cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
31162c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3117cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3118cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31197e860f15SJohn Edward Broadbent         });
3120cb92c03bSAndrew Geissler }
3121a3316fc6SZhikuiRen 
3122a3316fc6SZhikuiRen /****************************************************
3123a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3124a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3125a3316fc6SZhikuiRen  ******************************************************/
31267e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3127a3316fc6SZhikuiRen {
31287e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3129ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3130002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3131002d39b4SEd Tanous             [&app](const crow::Request& req,
3132002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
313445ca1b86SEd Tanous         {
313545ca1b86SEd Tanous             return;
313645ca1b86SEd Tanous         }
31371476687dSEd Tanous 
31381476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
31391476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
31401476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
31411476687dSEd Tanous             "#LogService.v1_1_0.LogService";
31421476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
31431476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
31441476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
31451476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
31461476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
31471476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
31487c8c4058STejas Patil 
31497c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
31507c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
31510fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
31527c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
31537c8c4058STejas Patil             redfishDateTimeOffset.second;
31547c8c4058STejas Patil 
3155a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
31567e860f15SJohn Edward Broadbent             {"target",
31570fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
31587e860f15SJohn Edward Broadbent         });
3159a3316fc6SZhikuiRen }
3160a3316fc6SZhikuiRen 
31617e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3162a3316fc6SZhikuiRen {
31630fda0f12SGeorge Liu     BMCWEB_ROUTE(
31640fda0f12SGeorge Liu         app,
31650fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3166ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3167ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3168432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
31697e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
317045ca1b86SEd Tanous             [&app](const crow::Request& req,
31717e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
317345ca1b86SEd Tanous         {
317445ca1b86SEd Tanous             return;
317545ca1b86SEd Tanous         }
3176a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3177a3316fc6SZhikuiRen 
3178a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3179a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3180a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3181a3316fc6SZhikuiRen             if (ec)
3182a3316fc6SZhikuiRen             {
3183a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3184002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
31857e860f15SJohn Edward Broadbent                                  << ec;
3186002d39b4SEd Tanous                 asyncResp->res.result(
3187002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3188a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3189a3316fc6SZhikuiRen                 return;
3190a3316fc6SZhikuiRen             }
3191a3316fc6SZhikuiRen             },
319215124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
319315124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3194a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31957e860f15SJohn Edward Broadbent         });
3196a3316fc6SZhikuiRen }
3197a3316fc6SZhikuiRen 
3198a3316fc6SZhikuiRen static void fillPostCodeEntry(
31998d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
32006c9a279eSManojkiran Eda     const boost::container::flat_map<
32016c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3202a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3203a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3204a3316fc6SZhikuiRen {
3205a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3206fffb8c1fSEd Tanous     const registries::Message* message =
3207fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3208a3316fc6SZhikuiRen 
3209a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3210a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3211a3316fc6SZhikuiRen 
3212a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
32136c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32146c9a279eSManojkiran Eda              code : postcode)
3215a3316fc6SZhikuiRen     {
3216a3316fc6SZhikuiRen         currentCodeIndex++;
3217a3316fc6SZhikuiRen         std::string postcodeEntryID =
3218a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3219a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3220a3316fc6SZhikuiRen 
3221a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3222a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3223a3316fc6SZhikuiRen 
3224a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3225a3316fc6SZhikuiRen         { // already incremented
3226a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3227a3316fc6SZhikuiRen         }
3228a3316fc6SZhikuiRen         else
3229a3316fc6SZhikuiRen         {
3230a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3231a3316fc6SZhikuiRen         }
3232a3316fc6SZhikuiRen 
3233a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3234a3316fc6SZhikuiRen         // not fall between top and skip
3235a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3236a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3237a3316fc6SZhikuiRen         {
3238a3316fc6SZhikuiRen             continue;
3239a3316fc6SZhikuiRen         }
3240a3316fc6SZhikuiRen 
32414e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3242a3316fc6SZhikuiRen         // currentIndex
3243a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3244a3316fc6SZhikuiRen         {
3245a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3246a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3247a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3248a3316fc6SZhikuiRen             continue;
3249a3316fc6SZhikuiRen         }
3250a3316fc6SZhikuiRen 
3251a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3252a3316fc6SZhikuiRen         // index
3253a3316fc6SZhikuiRen 
3254a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3255a3316fc6SZhikuiRen         std::string entryTimeStr;
32561d8782e7SNan Zhou         entryTimeStr =
32571d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3258a3316fc6SZhikuiRen 
3259a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3260a3316fc6SZhikuiRen         std::ostringstream hexCode;
3261a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32626c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3263a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3264a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3265a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3266a3316fc6SZhikuiRen         // Set precision to 4 digits
3267a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3268a3316fc6SZhikuiRen         // Add double to stream
3269a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3270a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3271a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3272a3316fc6SZhikuiRen 
3273a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3274a3316fc6SZhikuiRen         std::string msg;
3275a3316fc6SZhikuiRen         if (message != nullptr)
3276a3316fc6SZhikuiRen         {
3277a3316fc6SZhikuiRen             msg = message->message;
3278a3316fc6SZhikuiRen 
3279a3316fc6SZhikuiRen             // fill in this post code value
3280a3316fc6SZhikuiRen             int i = 0;
3281a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3282a3316fc6SZhikuiRen             {
3283a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3284a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3285a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3286a3316fc6SZhikuiRen                 {
3287a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3288a3316fc6SZhikuiRen                 }
3289a3316fc6SZhikuiRen             }
3290a3316fc6SZhikuiRen         }
3291a3316fc6SZhikuiRen 
3292d4342a92STim Lee         // Get Severity template from message registry
3293d4342a92STim Lee         std::string severity;
3294d4342a92STim Lee         if (message != nullptr)
3295d4342a92STim Lee         {
32965f2b84eeSEd Tanous             severity = message->messageSeverity;
3297d4342a92STim Lee         }
3298d4342a92STim Lee 
3299a3316fc6SZhikuiRen         // add to AsyncResp
3300a3316fc6SZhikuiRen         logEntryArray.push_back({});
3301a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
330284afc48bSJason M. Bills         bmcLogEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
330384afc48bSJason M. Bills         bmcLogEntry["@odata.id"] =
33040fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
330584afc48bSJason M. Bills             postcodeEntryID;
330684afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
330784afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
330884afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
330984afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
331084afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
331184afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
331284afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
331384afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3314647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3315647b3cdcSGeorge Liu         {
3316647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3317647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3318647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3319647b3cdcSGeorge Liu         }
3320a3316fc6SZhikuiRen     }
3321a3316fc6SZhikuiRen }
3322a3316fc6SZhikuiRen 
33238d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3324a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3325a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3326a3316fc6SZhikuiRen {
3327a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
33286c9a279eSManojkiran Eda         [aResp, bootIndex,
33296c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
33306c9a279eSManojkiran Eda                     const boost::container::flat_map<
33316c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33326c9a279eSManojkiran Eda                         postcode) {
3333a3316fc6SZhikuiRen         if (ec)
3334a3316fc6SZhikuiRen         {
3335a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3336a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3337a3316fc6SZhikuiRen             return;
3338a3316fc6SZhikuiRen         }
3339a3316fc6SZhikuiRen 
3340a3316fc6SZhikuiRen         // skip the empty postcode boots
3341a3316fc6SZhikuiRen         if (postcode.empty())
3342a3316fc6SZhikuiRen         {
3343a3316fc6SZhikuiRen             return;
3344a3316fc6SZhikuiRen         }
3345a3316fc6SZhikuiRen 
3346a3316fc6SZhikuiRen         fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3347a3316fc6SZhikuiRen 
3348a3316fc6SZhikuiRen         aResp->res.jsonValue["Members@odata.count"] =
3349a3316fc6SZhikuiRen             aResp->res.jsonValue["Members"].size();
3350a3316fc6SZhikuiRen         },
335115124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
335215124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3353a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3354a3316fc6SZhikuiRen         bootIndex);
3355a3316fc6SZhikuiRen }
3356a3316fc6SZhikuiRen 
33578d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3358a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3359a3316fc6SZhikuiRen                                const uint16_t bootCount,
33603648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
33613648c8beSEd Tanous                                size_t top)
3362a3316fc6SZhikuiRen {
3363a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3364a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3365a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
33666c9a279eSManojkiran Eda               const boost::container::flat_map<
33676c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33686c9a279eSManojkiran Eda                   postcode) {
3369a3316fc6SZhikuiRen         if (ec)
3370a3316fc6SZhikuiRen         {
3371a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3372a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3373a3316fc6SZhikuiRen             return;
3374a3316fc6SZhikuiRen         }
3375a3316fc6SZhikuiRen 
3376a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3377a3316fc6SZhikuiRen         if (!postcode.empty())
3378a3316fc6SZhikuiRen         {
3379a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
33803648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3381a3316fc6SZhikuiRen             {
33823648c8beSEd Tanous                 uint64_t thisBootSkip =
33833648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
33843648c8beSEd Tanous                     entryCount;
3385a3316fc6SZhikuiRen                 uint64_t thisBootTop =
33863648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
33873648c8beSEd Tanous                     entryCount;
3388a3316fc6SZhikuiRen 
3389002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3390002d39b4SEd Tanous                                   thisBootTop);
3391a3316fc6SZhikuiRen             }
3392a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3393a3316fc6SZhikuiRen         }
3394a3316fc6SZhikuiRen 
3395a3316fc6SZhikuiRen         // continue to previous bootIndex
3396a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3397a3316fc6SZhikuiRen         {
3398a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3399a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3400a3316fc6SZhikuiRen         }
340181584abeSJiaqing Zhao         else if (skip + top < endCount)
3402a3316fc6SZhikuiRen         {
3403a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
34040fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3405a3316fc6SZhikuiRen                 std::to_string(skip + top);
3406a3316fc6SZhikuiRen         }
3407a3316fc6SZhikuiRen         },
340815124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
340915124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3410a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3411a3316fc6SZhikuiRen         bootIndex);
3412a3316fc6SZhikuiRen }
3413a3316fc6SZhikuiRen 
34148d1b46d7Szhanghch05 static void
34158d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
34163648c8beSEd Tanous                          size_t skip, size_t top)
3417a3316fc6SZhikuiRen {
3418a3316fc6SZhikuiRen     uint64_t entryCount = 0;
34191e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
34201e1e598dSJonathan Doman         *crow::connections::systemBus,
34211e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
34221e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
34231e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
34241e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
34251e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3426a3316fc6SZhikuiRen         if (ec)
3427a3316fc6SZhikuiRen         {
3428a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3429a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3430a3316fc6SZhikuiRen             return;
3431a3316fc6SZhikuiRen         }
34321e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
34331e1e598dSJonathan Doman         });
3434a3316fc6SZhikuiRen }
3435a3316fc6SZhikuiRen 
34367e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3437a3316fc6SZhikuiRen {
34387e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
34397e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3440ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
34417e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
344245ca1b86SEd Tanous             [&app](const crow::Request& req,
34437e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3444c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3445c937d2bfSEd Tanous             .canDelegateTop = true,
3446c937d2bfSEd Tanous             .canDelegateSkip = true,
3447c937d2bfSEd Tanous         };
3448c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3449c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
34503ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
345145ca1b86SEd Tanous         {
345245ca1b86SEd Tanous             return;
345345ca1b86SEd Tanous         }
3454a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3455a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3456a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3457a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3458a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3459a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3460a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3461a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3462a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
34633648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
34643648c8beSEd Tanous         size_t top =
34653648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
34663648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
34677e860f15SJohn Edward Broadbent         });
3468a3316fc6SZhikuiRen }
3469a3316fc6SZhikuiRen 
3470647b3cdcSGeorge Liu /**
3471647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3472647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3473647b3cdcSGeorge Liu  *
3474647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3475647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3476647b3cdcSGeorge Liu  * @param[out] index          Index value
3477647b3cdcSGeorge Liu  *
3478647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3479647b3cdcSGeorge Liu  */
3480647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3481647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3482647b3cdcSGeorge Liu {
3483647b3cdcSGeorge Liu     std::vector<std::string> split;
3484647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3485647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3486647b3cdcSGeorge Liu     {
3487647b3cdcSGeorge Liu         return false;
3488647b3cdcSGeorge Liu     }
3489647b3cdcSGeorge Liu 
3490ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3491647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3492ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3493647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3494647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3495647b3cdcSGeorge Liu 
3496647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3497647b3cdcSGeorge Liu     {
3498647b3cdcSGeorge Liu         return false;
3499647b3cdcSGeorge Liu     }
3500647b3cdcSGeorge Liu 
3501647b3cdcSGeorge Liu     start = split[1].data();
3502ca45aa3cSEd Tanous 
3503ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3504647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3505647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3506647b3cdcSGeorge Liu 
3507517d9a58STony Lee     return ptrValue == end && ecValue == std::errc();
3508647b3cdcSGeorge Liu }
3509647b3cdcSGeorge Liu 
3510647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3511647b3cdcSGeorge Liu {
35120fda0f12SGeorge Liu     BMCWEB_ROUTE(
35130fda0f12SGeorge Liu         app,
35140fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3515647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3516647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
351745ca1b86SEd Tanous             [&app](const crow::Request& req,
3518647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3519647b3cdcSGeorge Liu                    const std::string& postCodeID) {
35203ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
352145ca1b86SEd Tanous         {
352245ca1b86SEd Tanous             return;
352345ca1b86SEd Tanous         }
3524002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
3525647b3cdcSGeorge Liu         {
3526002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3527647b3cdcSGeorge Liu             return;
3528647b3cdcSGeorge Liu         }
3529647b3cdcSGeorge Liu 
3530647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3531647b3cdcSGeorge Liu         uint16_t index = 0;
3532647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3533647b3cdcSGeorge Liu         {
3534002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3535647b3cdcSGeorge Liu             return;
3536647b3cdcSGeorge Liu         }
3537647b3cdcSGeorge Liu 
3538647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3539647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3540647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3541002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3542002d39b4SEd Tanous                     postcodes) {
3543647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3544647b3cdcSGeorge Liu             {
3545002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3546002d39b4SEd Tanous                                            postCodeID);
3547647b3cdcSGeorge Liu                 return;
3548647b3cdcSGeorge Liu             }
3549647b3cdcSGeorge Liu             if (ec)
3550647b3cdcSGeorge Liu             {
3551647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3552647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
3553647b3cdcSGeorge Liu                 return;
3554647b3cdcSGeorge Liu             }
3555647b3cdcSGeorge Liu 
3556647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
3557002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
3558647b3cdcSGeorge Liu             {
3559647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3560002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3561002d39b4SEd Tanous                                            postCodeID);
3562647b3cdcSGeorge Liu                 return;
3563647b3cdcSGeorge Liu             }
3564647b3cdcSGeorge Liu 
35659eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
356646ff87baSEd Tanous             if (c.empty())
3567647b3cdcSGeorge Liu             {
3568647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
3569002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3570002d39b4SEd Tanous                                            postCodeID);
3571647b3cdcSGeorge Liu                 return;
3572647b3cdcSGeorge Liu             }
357346ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
357446ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
357546ff87baSEd Tanous             std::string_view strData(d, c.size());
3576647b3cdcSGeorge Liu 
3577647b3cdcSGeorge Liu             asyncResp->res.addHeader("Content-Type",
3578647b3cdcSGeorge Liu                                      "application/octet-stream");
3579002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
3580002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
3581647b3cdcSGeorge Liu             },
3582647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
3583647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
3584002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3585647b3cdcSGeorge Liu         });
3586647b3cdcSGeorge Liu }
3587647b3cdcSGeorge Liu 
35887e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3589a3316fc6SZhikuiRen {
35907e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
35917e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3592ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
35937e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
359445ca1b86SEd Tanous             [&app](const crow::Request& req,
35957e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35967e860f15SJohn Edward Broadbent                    const std::string& targetID) {
35973ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
359845ca1b86SEd Tanous         {
359945ca1b86SEd Tanous             return;
360045ca1b86SEd Tanous         }
3601647b3cdcSGeorge Liu         uint16_t bootIndex = 0;
3602647b3cdcSGeorge Liu         uint64_t codeIndex = 0;
3603647b3cdcSGeorge Liu         if (!parsePostCode(targetID, codeIndex, bootIndex))
3604a3316fc6SZhikuiRen         {
3605a3316fc6SZhikuiRen             // Requested ID was not found
3606ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3607a3316fc6SZhikuiRen             return;
3608a3316fc6SZhikuiRen         }
3609a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3610a3316fc6SZhikuiRen         {
3611a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
36127e860f15SJohn Edward Broadbent                              << targetID;
3613a3316fc6SZhikuiRen         }
3614a3316fc6SZhikuiRen 
3615002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3616a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
36170fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3618a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3619a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3620a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3621a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3622a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3623a3316fc6SZhikuiRen 
3624a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
36257e860f15SJohn Edward Broadbent         });
3626a3316fc6SZhikuiRen }
3627a3316fc6SZhikuiRen 
36281da66f75SEd Tanous } // namespace redfish
3629