xref: /openbmc/bmcweb/features/redfish/lib/log_services.hpp (revision aefe3786057499a767c5627b0106f0248fa3762d)
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>
3011ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
31400fd1fbSAdriana Kobylak #include <boost/algorithm/string/replace.hpp>
324851d45dSJason M. Bills #include <boost/algorithm/string/split.hpp>
3307c8c20dSEd Tanous #include <boost/beast/http/verb.hpp>
341da66f75SEd Tanous #include <boost/container/flat_map.hpp>
351ddcf01aSJason M. Bills #include <boost/system/linux_error.hpp>
36168e20c1SEd Tanous #include <dbus_utility.hpp>
37cb92c03bSAndrew Geissler #include <error_messages.hpp>
3845ca1b86SEd Tanous #include <query.hpp>
39ed398213SEd Tanous #include <registries/privilege_registry.hpp>
401214b7e7SGunnar Mills 
41647b3cdcSGeorge Liu #include <charconv>
424418c7f0SJames Feist #include <filesystem>
4375710de2SXiaochao Ma #include <optional>
4426702d01SEd Tanous #include <span>
45cd225da8SJason M. Bills #include <string_view>
46abf2add6SEd Tanous #include <variant>
471da66f75SEd Tanous 
481da66f75SEd Tanous namespace redfish
491da66f75SEd Tanous {
501da66f75SEd Tanous 
515b61b5e8SJason M. Bills constexpr char const* crashdumpObject = "com.intel.crashdump";
525b61b5e8SJason M. Bills constexpr char const* crashdumpPath = "/com/intel/crashdump";
535b61b5e8SJason M. Bills constexpr char const* crashdumpInterface = "com.intel.crashdump";
545b61b5e8SJason M. Bills constexpr char const* deleteAllInterface =
555b61b5e8SJason M. Bills     "xyz.openbmc_project.Collection.DeleteAll";
565b61b5e8SJason M. Bills constexpr char const* crashdumpOnDemandInterface =
57424c4176SJason M. Bills     "com.intel.crashdump.OnDemand";
586eda7685SKenny L. Ku constexpr char const* crashdumpTelemetryInterface =
596eda7685SKenny L. Ku     "com.intel.crashdump.Telemetry";
601da66f75SEd Tanous 
61fffb8c1fSEd Tanous namespace registries
624851d45dSJason M. Bills {
6326702d01SEd Tanous static const Message*
6426702d01SEd Tanous     getMessageFromRegistry(const std::string& messageKey,
6526702d01SEd Tanous                            const std::span<const MessageEntry> registry)
664851d45dSJason M. Bills {
67002d39b4SEd Tanous     std::span<const MessageEntry>::iterator messageIt =
68002d39b4SEd Tanous         std::find_if(registry.begin(), registry.end(),
694851d45dSJason M. Bills                      [&messageKey](const MessageEntry& messageEntry) {
70e662eae8SEd Tanous         return std::strcmp(messageEntry.first, messageKey.c_str()) == 0;
714851d45dSJason M. Bills         });
7226702d01SEd Tanous     if (messageIt != registry.end())
734851d45dSJason M. Bills     {
744851d45dSJason M. Bills         return &messageIt->second;
754851d45dSJason M. Bills     }
764851d45dSJason M. Bills 
774851d45dSJason M. Bills     return nullptr;
784851d45dSJason M. Bills }
794851d45dSJason M. Bills 
804851d45dSJason M. Bills static const Message* getMessage(const std::string_view& messageID)
814851d45dSJason M. Bills {
824851d45dSJason M. Bills     // Redfish MessageIds are in the form
834851d45dSJason M. Bills     // RegistryName.MajorVersion.MinorVersion.MessageKey, so parse it to find
844851d45dSJason M. Bills     // the right Message
854851d45dSJason M. Bills     std::vector<std::string> fields;
864851d45dSJason M. Bills     fields.reserve(4);
874851d45dSJason M. Bills     boost::split(fields, messageID, boost::is_any_of("."));
8802cad96eSEd Tanous     const std::string& registryName = fields[0];
8902cad96eSEd Tanous     const std::string& messageKey = fields[3];
904851d45dSJason M. Bills 
914851d45dSJason M. Bills     // Find the right registry and check it for the MessageKey
924851d45dSJason M. Bills     if (std::string(base::header.registryPrefix) == registryName)
934851d45dSJason M. Bills     {
944851d45dSJason M. Bills         return getMessageFromRegistry(
9526702d01SEd Tanous             messageKey, std::span<const MessageEntry>(base::registry));
964851d45dSJason M. Bills     }
974851d45dSJason M. Bills     if (std::string(openbmc::header.registryPrefix) == registryName)
984851d45dSJason M. Bills     {
994851d45dSJason M. Bills         return getMessageFromRegistry(
10026702d01SEd Tanous             messageKey, std::span<const MessageEntry>(openbmc::registry));
1014851d45dSJason M. Bills     }
1024851d45dSJason M. Bills     return nullptr;
1034851d45dSJason M. Bills }
104fffb8c1fSEd Tanous } // namespace registries
1054851d45dSJason M. Bills 
106f6150403SJames Feist namespace fs = std::filesystem;
1071da66f75SEd Tanous 
108cb92c03bSAndrew Geissler inline std::string translateSeverityDbusToRedfish(const std::string& s)
109cb92c03bSAndrew Geissler {
110d4d25793SEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Alert") ||
111d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Critical") ||
112d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Emergency") ||
113d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Error"))
114cb92c03bSAndrew Geissler     {
115cb92c03bSAndrew Geissler         return "Critical";
116cb92c03bSAndrew Geissler     }
1173174e4dfSEd Tanous     if ((s == "xyz.openbmc_project.Logging.Entry.Level.Debug") ||
118d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Informational") ||
119d4d25793SEd Tanous         (s == "xyz.openbmc_project.Logging.Entry.Level.Notice"))
120cb92c03bSAndrew Geissler     {
121cb92c03bSAndrew Geissler         return "OK";
122cb92c03bSAndrew Geissler     }
1233174e4dfSEd Tanous     if (s == "xyz.openbmc_project.Logging.Entry.Level.Warning")
124cb92c03bSAndrew Geissler     {
125cb92c03bSAndrew Geissler         return "Warning";
126cb92c03bSAndrew Geissler     }
127cb92c03bSAndrew Geissler     return "";
128cb92c03bSAndrew Geissler }
129cb92c03bSAndrew Geissler 
1307e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
13139e77504SEd Tanous                                      const std::string_view& field,
13239e77504SEd Tanous                                      std::string_view& contents)
13316428a1aSJason M. Bills {
13416428a1aSJason M. Bills     const char* data = nullptr;
13516428a1aSJason M. Bills     size_t length = 0;
13616428a1aSJason M. Bills     int ret = 0;
13716428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
13846ff87baSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
13946ff87baSEd Tanous     const void** dataVoid = reinterpret_cast<const void**>(&data);
14046ff87baSEd Tanous 
14146ff87baSEd Tanous     ret = sd_journal_get_data(journal, field.data(), dataVoid, &length);
14216428a1aSJason M. Bills     if (ret < 0)
14316428a1aSJason M. Bills     {
14416428a1aSJason M. Bills         return ret;
14516428a1aSJason M. Bills     }
14639e77504SEd Tanous     contents = std::string_view(data, length);
14716428a1aSJason M. Bills     // Only use the content after the "=" character.
14881ce609eSEd Tanous     contents.remove_prefix(std::min(contents.find('=') + 1, contents.size()));
14916428a1aSJason M. Bills     return ret;
15016428a1aSJason M. Bills }
15116428a1aSJason M. Bills 
1527e860f15SJohn Edward Broadbent inline static int getJournalMetadata(sd_journal* journal,
1537e860f15SJohn Edward Broadbent                                      const std::string_view& field,
1547e860f15SJohn Edward Broadbent                                      const int& base, long int& contents)
15516428a1aSJason M. Bills {
15616428a1aSJason M. Bills     int ret = 0;
15739e77504SEd Tanous     std::string_view metadata;
15816428a1aSJason M. Bills     // Get the metadata from the requested field of the journal entry
15916428a1aSJason M. Bills     ret = getJournalMetadata(journal, field, metadata);
16016428a1aSJason M. Bills     if (ret < 0)
16116428a1aSJason M. Bills     {
16216428a1aSJason M. Bills         return ret;
16316428a1aSJason M. Bills     }
164b01bf299SEd Tanous     contents = strtol(metadata.data(), nullptr, base);
16516428a1aSJason M. Bills     return ret;
16616428a1aSJason M. Bills }
16716428a1aSJason M. Bills 
1687e860f15SJohn Edward Broadbent inline static bool getEntryTimestamp(sd_journal* journal,
1697e860f15SJohn Edward Broadbent                                      std::string& entryTimestamp)
170a3316fc6SZhikuiRen {
171a3316fc6SZhikuiRen     int ret = 0;
172a3316fc6SZhikuiRen     uint64_t timestamp = 0;
173a3316fc6SZhikuiRen     ret = sd_journal_get_realtime_usec(journal, &timestamp);
174a3316fc6SZhikuiRen     if (ret < 0)
175a3316fc6SZhikuiRen     {
176a3316fc6SZhikuiRen         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
177a3316fc6SZhikuiRen                          << strerror(-ret);
178a3316fc6SZhikuiRen         return false;
179a3316fc6SZhikuiRen     }
1801d8782e7SNan Zhou     entryTimestamp = crow::utility::getDateTimeUint(timestamp / 1000 / 1000);
1819c620e21SAsmitha Karunanithi     return true;
182a3316fc6SZhikuiRen }
18350b8a43aSEd Tanous 
1847e860f15SJohn Edward Broadbent inline static bool getUniqueEntryID(sd_journal* journal, std::string& entryID,
185e85d6b16SJason M. Bills                                     const bool firstEntry = true)
18616428a1aSJason M. Bills {
18716428a1aSJason M. Bills     int ret = 0;
18816428a1aSJason M. Bills     static uint64_t prevTs = 0;
18916428a1aSJason M. Bills     static int index = 0;
190e85d6b16SJason M. Bills     if (firstEntry)
191e85d6b16SJason M. Bills     {
192e85d6b16SJason M. Bills         prevTs = 0;
193e85d6b16SJason M. Bills     }
194e85d6b16SJason M. Bills 
19516428a1aSJason M. Bills     // Get the entry timestamp
19616428a1aSJason M. Bills     uint64_t curTs = 0;
19716428a1aSJason M. Bills     ret = sd_journal_get_realtime_usec(journal, &curTs);
19816428a1aSJason M. Bills     if (ret < 0)
19916428a1aSJason M. Bills     {
20016428a1aSJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read entry timestamp: "
20116428a1aSJason M. Bills                          << strerror(-ret);
20216428a1aSJason M. Bills         return false;
20316428a1aSJason M. Bills     }
20416428a1aSJason M. Bills     // If the timestamp isn't unique, increment the index
20516428a1aSJason M. Bills     if (curTs == prevTs)
20616428a1aSJason M. Bills     {
20716428a1aSJason M. Bills         index++;
20816428a1aSJason M. Bills     }
20916428a1aSJason M. Bills     else
21016428a1aSJason M. Bills     {
21116428a1aSJason M. Bills         // Otherwise, reset it
21216428a1aSJason M. Bills         index = 0;
21316428a1aSJason M. Bills     }
21416428a1aSJason M. Bills     // Save the timestamp
21516428a1aSJason M. Bills     prevTs = curTs;
21616428a1aSJason M. Bills 
21716428a1aSJason M. Bills     entryID = std::to_string(curTs);
21816428a1aSJason M. Bills     if (index > 0)
21916428a1aSJason M. Bills     {
22016428a1aSJason M. Bills         entryID += "_" + std::to_string(index);
22116428a1aSJason M. Bills     }
22216428a1aSJason M. Bills     return true;
22316428a1aSJason M. Bills }
22416428a1aSJason M. Bills 
225e85d6b16SJason M. Bills static bool getUniqueEntryID(const std::string& logEntry, std::string& entryID,
226e85d6b16SJason M. Bills                              const bool firstEntry = true)
22795820184SJason M. Bills {
228271584abSEd Tanous     static time_t prevTs = 0;
22995820184SJason M. Bills     static int index = 0;
230e85d6b16SJason M. Bills     if (firstEntry)
231e85d6b16SJason M. Bills     {
232e85d6b16SJason M. Bills         prevTs = 0;
233e85d6b16SJason M. Bills     }
234e85d6b16SJason M. Bills 
23595820184SJason M. Bills     // Get the entry timestamp
236271584abSEd Tanous     std::time_t curTs = 0;
23795820184SJason M. Bills     std::tm timeStruct = {};
23895820184SJason M. Bills     std::istringstream entryStream(logEntry);
23995820184SJason M. Bills     if (entryStream >> std::get_time(&timeStruct, "%Y-%m-%dT%H:%M:%S"))
24095820184SJason M. Bills     {
24195820184SJason M. Bills         curTs = std::mktime(&timeStruct);
24295820184SJason M. Bills     }
24395820184SJason M. Bills     // If the timestamp isn't unique, increment the index
24495820184SJason M. Bills     if (curTs == prevTs)
24595820184SJason M. Bills     {
24695820184SJason M. Bills         index++;
24795820184SJason M. Bills     }
24895820184SJason M. Bills     else
24995820184SJason M. Bills     {
25095820184SJason M. Bills         // Otherwise, reset it
25195820184SJason M. Bills         index = 0;
25295820184SJason M. Bills     }
25395820184SJason M. Bills     // Save the timestamp
25495820184SJason M. Bills     prevTs = curTs;
25595820184SJason M. Bills 
25695820184SJason M. Bills     entryID = std::to_string(curTs);
25795820184SJason M. Bills     if (index > 0)
25895820184SJason M. Bills     {
25995820184SJason M. Bills         entryID += "_" + std::to_string(index);
26095820184SJason M. Bills     }
26195820184SJason M. Bills     return true;
26295820184SJason M. Bills }
26395820184SJason M. Bills 
2647e860f15SJohn Edward Broadbent inline static bool
2658d1b46d7Szhanghch05     getTimestampFromID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2668d1b46d7Szhanghch05                        const std::string& entryID, uint64_t& timestamp,
2678d1b46d7Szhanghch05                        uint64_t& index)
26816428a1aSJason M. Bills {
26916428a1aSJason M. Bills     if (entryID.empty())
27016428a1aSJason M. Bills     {
27116428a1aSJason M. Bills         return false;
27216428a1aSJason M. Bills     }
27316428a1aSJason M. Bills     // Convert the unique ID back to a timestamp to find the entry
27439e77504SEd Tanous     std::string_view tsStr(entryID);
27516428a1aSJason M. Bills 
27681ce609eSEd Tanous     auto underscorePos = tsStr.find('_');
27771d5d8dbSEd Tanous     if (underscorePos != std::string_view::npos)
27816428a1aSJason M. Bills     {
27916428a1aSJason M. Bills         // Timestamp has an index
28016428a1aSJason M. Bills         tsStr.remove_suffix(tsStr.size() - underscorePos);
28139e77504SEd Tanous         std::string_view indexStr(entryID);
28216428a1aSJason M. Bills         indexStr.remove_prefix(underscorePos + 1);
283c0bd5e4bSEd Tanous         auto [ptr, ec] = std::from_chars(
284c0bd5e4bSEd Tanous             indexStr.data(), indexStr.data() + indexStr.size(), index);
285c0bd5e4bSEd Tanous         if (ec != std::errc())
28616428a1aSJason M. Bills         {
287ace85d60SEd Tanous             messages::resourceMissingAtURI(
288ace85d60SEd Tanous                 asyncResp->res, crow::utility::urlFromPieces(entryID));
28916428a1aSJason M. Bills             return false;
29016428a1aSJason M. Bills         }
29116428a1aSJason M. Bills     }
29216428a1aSJason M. Bills     // Timestamp has no index
293c0bd5e4bSEd Tanous     auto [ptr, ec] =
294c0bd5e4bSEd Tanous         std::from_chars(tsStr.data(), tsStr.data() + tsStr.size(), timestamp);
295c0bd5e4bSEd Tanous     if (ec != std::errc())
29616428a1aSJason M. Bills     {
297ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
298ace85d60SEd Tanous                                        crow::utility::urlFromPieces(entryID));
29916428a1aSJason M. Bills         return false;
30016428a1aSJason M. Bills     }
30116428a1aSJason M. Bills     return true;
30216428a1aSJason M. Bills }
30316428a1aSJason M. Bills 
30495820184SJason M. Bills static bool
30595820184SJason M. Bills     getRedfishLogFiles(std::vector<std::filesystem::path>& redfishLogFiles)
30695820184SJason M. Bills {
30795820184SJason M. Bills     static const std::filesystem::path redfishLogDir = "/var/log";
30895820184SJason M. Bills     static const std::string redfishLogFilename = "redfish";
30995820184SJason M. Bills 
31095820184SJason M. Bills     // Loop through the directory looking for redfish log files
31195820184SJason M. Bills     for (const std::filesystem::directory_entry& dirEnt :
31295820184SJason M. Bills          std::filesystem::directory_iterator(redfishLogDir))
31395820184SJason M. Bills     {
31495820184SJason M. Bills         // If we find a redfish log file, save the path
31595820184SJason M. Bills         std::string filename = dirEnt.path().filename();
31611ba3979SEd Tanous         if (filename.starts_with(redfishLogFilename))
31795820184SJason M. Bills         {
31895820184SJason M. Bills             redfishLogFiles.emplace_back(redfishLogDir / filename);
31995820184SJason M. Bills         }
32095820184SJason M. Bills     }
32195820184SJason M. Bills     // As the log files rotate, they are appended with a ".#" that is higher for
32295820184SJason M. Bills     // the older logs. Since we don't expect more than 10 log files, we
32395820184SJason M. Bills     // can just sort the list to get them in order from newest to oldest
32495820184SJason M. Bills     std::sort(redfishLogFiles.begin(), redfishLogFiles.end());
32595820184SJason M. Bills 
32695820184SJason M. Bills     return !redfishLogFiles.empty();
32795820184SJason M. Bills }
32895820184SJason M. Bills 
329*aefe3786SClaire Weinan inline void parseDumpEntryFromDbusObject(
330*aefe3786SClaire Weinan     const dbus::utility::ManagedItem& object, std::string& dumpStatus,
331*aefe3786SClaire Weinan     uint64_t& size, uint64_t& timestamp,
332*aefe3786SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
333*aefe3786SClaire Weinan {
334*aefe3786SClaire Weinan     for (const auto& interfaceMap : object.second)
335*aefe3786SClaire Weinan     {
336*aefe3786SClaire Weinan         if (interfaceMap.first == "xyz.openbmc_project.Common.Progress")
337*aefe3786SClaire Weinan         {
338*aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
339*aefe3786SClaire Weinan             {
340*aefe3786SClaire Weinan                 if (propertyMap.first == "Status")
341*aefe3786SClaire Weinan                 {
342*aefe3786SClaire Weinan                     const auto* status =
343*aefe3786SClaire Weinan                         std::get_if<std::string>(&propertyMap.second);
344*aefe3786SClaire Weinan                     if (status == nullptr)
345*aefe3786SClaire Weinan                     {
346*aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
347*aefe3786SClaire Weinan                         break;
348*aefe3786SClaire Weinan                     }
349*aefe3786SClaire Weinan                     dumpStatus = *status;
350*aefe3786SClaire Weinan                 }
351*aefe3786SClaire Weinan             }
352*aefe3786SClaire Weinan         }
353*aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Dump.Entry")
354*aefe3786SClaire Weinan         {
355*aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
356*aefe3786SClaire Weinan             {
357*aefe3786SClaire Weinan                 if (propertyMap.first == "Size")
358*aefe3786SClaire Weinan                 {
359*aefe3786SClaire Weinan                     const auto* sizePtr =
360*aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
361*aefe3786SClaire Weinan                     if (sizePtr == nullptr)
362*aefe3786SClaire Weinan                     {
363*aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
364*aefe3786SClaire Weinan                         break;
365*aefe3786SClaire Weinan                     }
366*aefe3786SClaire Weinan                     size = *sizePtr;
367*aefe3786SClaire Weinan                     break;
368*aefe3786SClaire Weinan                 }
369*aefe3786SClaire Weinan             }
370*aefe3786SClaire Weinan         }
371*aefe3786SClaire Weinan         else if (interfaceMap.first == "xyz.openbmc_project.Time.EpochTime")
372*aefe3786SClaire Weinan         {
373*aefe3786SClaire Weinan             for (const auto& propertyMap : interfaceMap.second)
374*aefe3786SClaire Weinan             {
375*aefe3786SClaire Weinan                 if (propertyMap.first == "Elapsed")
376*aefe3786SClaire Weinan                 {
377*aefe3786SClaire Weinan                     const uint64_t* usecsTimeStamp =
378*aefe3786SClaire Weinan                         std::get_if<uint64_t>(&propertyMap.second);
379*aefe3786SClaire Weinan                     if (usecsTimeStamp == nullptr)
380*aefe3786SClaire Weinan                     {
381*aefe3786SClaire Weinan                         messages::internalError(asyncResp->res);
382*aefe3786SClaire Weinan                         break;
383*aefe3786SClaire Weinan                     }
384*aefe3786SClaire Weinan                     timestamp = *usecsTimeStamp;
385*aefe3786SClaire Weinan                     break;
386*aefe3786SClaire Weinan                 }
387*aefe3786SClaire Weinan             }
388*aefe3786SClaire Weinan         }
389*aefe3786SClaire Weinan     }
390*aefe3786SClaire Weinan }
391*aefe3786SClaire Weinan 
39221ab404cSNan Zhou static std::string getDumpEntriesPath(const std::string& dumpType)
393fdd26906SClaire Weinan {
394fdd26906SClaire Weinan     std::string entriesPath;
395fdd26906SClaire Weinan 
396fdd26906SClaire Weinan     if (dumpType == "BMC")
397fdd26906SClaire Weinan     {
398fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/";
399fdd26906SClaire Weinan     }
400fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
401fdd26906SClaire Weinan     {
402fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/";
403fdd26906SClaire Weinan     }
404fdd26906SClaire Weinan     else if (dumpType == "System")
405fdd26906SClaire Weinan     {
406fdd26906SClaire Weinan         entriesPath = "/redfish/v1/Systems/system/LogServices/Dump/Entries/";
407fdd26906SClaire Weinan     }
408fdd26906SClaire Weinan     else
409fdd26906SClaire Weinan     {
410fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpEntriesPath() invalid dump type: "
411fdd26906SClaire Weinan                          << dumpType;
412fdd26906SClaire Weinan     }
413fdd26906SClaire Weinan 
414fdd26906SClaire Weinan     // Returns empty string on error
415fdd26906SClaire Weinan     return entriesPath;
416fdd26906SClaire Weinan }
417fdd26906SClaire Weinan 
4188d1b46d7Szhanghch05 inline void
4198d1b46d7Szhanghch05     getDumpEntryCollection(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4205cb1dd27SAsmitha Karunanithi                            const std::string& dumpType)
4215cb1dd27SAsmitha Karunanithi {
422fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
423fdd26906SClaire Weinan     if (entriesPath.empty())
4245cb1dd27SAsmitha Karunanithi     {
4255cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
4265cb1dd27SAsmitha Karunanithi         return;
4275cb1dd27SAsmitha Karunanithi     }
4285cb1dd27SAsmitha Karunanithi 
4295cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
430fdd26906SClaire Weinan         [asyncResp, entriesPath,
431711ac7a9SEd Tanous          dumpType](const boost::system::error_code ec,
432711ac7a9SEd Tanous                    dbus::utility::ManagedObjectType& resp) {
4335cb1dd27SAsmitha Karunanithi         if (ec)
4345cb1dd27SAsmitha Karunanithi         {
4355cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
4365cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
4375cb1dd27SAsmitha Karunanithi             return;
4385cb1dd27SAsmitha Karunanithi         }
4395cb1dd27SAsmitha Karunanithi 
440fdd26906SClaire Weinan         // Remove ending slash
441fdd26906SClaire Weinan         std::string odataIdStr = entriesPath;
442fdd26906SClaire Weinan         if (!odataIdStr.empty())
443fdd26906SClaire Weinan         {
444fdd26906SClaire Weinan             odataIdStr.pop_back();
445fdd26906SClaire Weinan         }
446fdd26906SClaire Weinan 
447fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.type"] =
448fdd26906SClaire Weinan             "#LogEntryCollection.LogEntryCollection";
449fdd26906SClaire Weinan         asyncResp->res.jsonValue["@odata.id"] = std::move(odataIdStr);
450fdd26906SClaire Weinan         asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entries";
451fdd26906SClaire Weinan         asyncResp->res.jsonValue["Description"] =
452fdd26906SClaire Weinan             "Collection of " + dumpType + " Dump Entries";
453fdd26906SClaire Weinan 
4545cb1dd27SAsmitha Karunanithi         nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
4555cb1dd27SAsmitha Karunanithi         entriesArray = nlohmann::json::array();
456b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
457b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
458002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
4595cb1dd27SAsmitha Karunanithi 
460002d39b4SEd Tanous         std::sort(resp.begin(), resp.end(), [](const auto& l, const auto& r) {
461002d39b4SEd Tanous             return AlphanumLess<std::string>()(l.first.filename(),
462002d39b4SEd Tanous                                                r.first.filename());
463565dfb6fSClaire Weinan         });
464565dfb6fSClaire Weinan 
4655cb1dd27SAsmitha Karunanithi         for (auto& object : resp)
4665cb1dd27SAsmitha Karunanithi         {
467b47452b2SAsmitha Karunanithi             if (object.first.str.find(dumpEntryPath) == std::string::npos)
4685cb1dd27SAsmitha Karunanithi             {
4695cb1dd27SAsmitha Karunanithi                 continue;
4705cb1dd27SAsmitha Karunanithi             }
4711d8782e7SNan Zhou             uint64_t timestamp = 0;
4725cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
47335440d18SAsmitha Karunanithi             std::string dumpStatus;
474433b68b4SJason M. Bills             nlohmann::json::object_t thisEntry;
4752dfd18efSEd Tanous 
4762dfd18efSEd Tanous             std::string entryID = object.first.filename();
4772dfd18efSEd Tanous             if (entryID.empty())
4785cb1dd27SAsmitha Karunanithi             {
4795cb1dd27SAsmitha Karunanithi                 continue;
4805cb1dd27SAsmitha Karunanithi             }
4815cb1dd27SAsmitha Karunanithi 
482*aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(object, dumpStatus, size, timestamp,
483*aefe3786SClaire Weinan                                          asyncResp);
4845cb1dd27SAsmitha Karunanithi 
4850fda0f12SGeorge Liu             if (dumpStatus !=
4860fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
48735440d18SAsmitha Karunanithi                 !dumpStatus.empty())
48835440d18SAsmitha Karunanithi             {
48935440d18SAsmitha Karunanithi                 // Dump status is not Complete, no need to enumerate
49035440d18SAsmitha Karunanithi                 continue;
49135440d18SAsmitha Karunanithi             }
49235440d18SAsmitha Karunanithi 
493647b3cdcSGeorge Liu             thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
494fdd26906SClaire Weinan             thisEntry["@odata.id"] = entriesPath + entryID;
4955cb1dd27SAsmitha Karunanithi             thisEntry["Id"] = entryID;
4965cb1dd27SAsmitha Karunanithi             thisEntry["EntryType"] = "Event";
497002d39b4SEd Tanous             thisEntry["Created"] = crow::utility::getDateTimeUint(timestamp);
4985cb1dd27SAsmitha Karunanithi             thisEntry["Name"] = dumpType + " Dump Entry";
4995cb1dd27SAsmitha Karunanithi 
5005cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5015cb1dd27SAsmitha Karunanithi             {
502d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "Manager";
503d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
504fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
505fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5065cb1dd27SAsmitha Karunanithi             }
5075cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5085cb1dd27SAsmitha Karunanithi             {
509d337bb72SAsmitha Karunanithi                 thisEntry["DiagnosticDataType"] = "OEM";
510d337bb72SAsmitha Karunanithi                 thisEntry["OEMDiagnosticDataType"] = "System";
511d337bb72SAsmitha Karunanithi                 thisEntry["AdditionalDataURI"] =
512fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
513fdd26906SClaire Weinan                 thisEntry["AdditionalDataSizeBytes"] = size;
5145cb1dd27SAsmitha Karunanithi             }
51535440d18SAsmitha Karunanithi             entriesArray.push_back(std::move(thisEntry));
5165cb1dd27SAsmitha Karunanithi         }
517002d39b4SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = entriesArray.size();
5185cb1dd27SAsmitha Karunanithi         },
5195cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
5205cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
5215cb1dd27SAsmitha Karunanithi }
5225cb1dd27SAsmitha Karunanithi 
5238d1b46d7Szhanghch05 inline void
524c7a6d660SClaire Weinan     getDumpEntryById(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5258d1b46d7Szhanghch05                      const std::string& entryID, const std::string& dumpType)
5265cb1dd27SAsmitha Karunanithi {
527fdd26906SClaire Weinan     std::string entriesPath = getDumpEntriesPath(dumpType);
528fdd26906SClaire Weinan     if (entriesPath.empty())
5295cb1dd27SAsmitha Karunanithi     {
5305cb1dd27SAsmitha Karunanithi         messages::internalError(asyncResp->res);
5315cb1dd27SAsmitha Karunanithi         return;
5325cb1dd27SAsmitha Karunanithi     }
5335cb1dd27SAsmitha Karunanithi 
5345cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
535fdd26906SClaire Weinan         [asyncResp, entryID, dumpType,
536fdd26906SClaire Weinan          entriesPath](const boost::system::error_code ec,
53702cad96eSEd Tanous                       const dbus::utility::ManagedObjectType& resp) {
5385cb1dd27SAsmitha Karunanithi         if (ec)
5395cb1dd27SAsmitha Karunanithi         {
5405cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "DumpEntry resp_handler got error " << ec;
5415cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
5425cb1dd27SAsmitha Karunanithi             return;
5435cb1dd27SAsmitha Karunanithi         }
5445cb1dd27SAsmitha Karunanithi 
545b47452b2SAsmitha Karunanithi         bool foundDumpEntry = false;
546b47452b2SAsmitha Karunanithi         std::string dumpEntryPath =
547b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
548002d39b4SEd Tanous             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/";
549b47452b2SAsmitha Karunanithi 
5509eb808c1SEd Tanous         for (const auto& objectPath : resp)
5515cb1dd27SAsmitha Karunanithi         {
552b47452b2SAsmitha Karunanithi             if (objectPath.first.str != dumpEntryPath + entryID)
5535cb1dd27SAsmitha Karunanithi             {
5545cb1dd27SAsmitha Karunanithi                 continue;
5555cb1dd27SAsmitha Karunanithi             }
5565cb1dd27SAsmitha Karunanithi 
5575cb1dd27SAsmitha Karunanithi             foundDumpEntry = true;
5581d8782e7SNan Zhou             uint64_t timestamp = 0;
5595cb1dd27SAsmitha Karunanithi             uint64_t size = 0;
56035440d18SAsmitha Karunanithi             std::string dumpStatus;
5615cb1dd27SAsmitha Karunanithi 
562*aefe3786SClaire Weinan             parseDumpEntryFromDbusObject(objectPath, dumpStatus, size,
563*aefe3786SClaire Weinan                                          timestamp, asyncResp);
5645cb1dd27SAsmitha Karunanithi 
5650fda0f12SGeorge Liu             if (dumpStatus !=
5660fda0f12SGeorge Liu                     "xyz.openbmc_project.Common.Progress.OperationStatus.Completed" &&
56735440d18SAsmitha Karunanithi                 !dumpStatus.empty())
56835440d18SAsmitha Karunanithi             {
56935440d18SAsmitha Karunanithi                 // Dump status is not Complete
57035440d18SAsmitha Karunanithi                 // return not found until status is changed to Completed
571002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, dumpType + " dump",
572002d39b4SEd Tanous                                            entryID);
57335440d18SAsmitha Karunanithi                 return;
57435440d18SAsmitha Karunanithi             }
57535440d18SAsmitha Karunanithi 
5765cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["@odata.type"] =
577647b3cdcSGeorge Liu                 "#LogEntry.v1_8_0.LogEntry";
578fdd26906SClaire Weinan             asyncResp->res.jsonValue["@odata.id"] = entriesPath + entryID;
5795cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Id"] = entryID;
5805cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["EntryType"] = "Event";
5815cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Created"] =
5821d8782e7SNan Zhou                 crow::utility::getDateTimeUint(timestamp);
5835cb1dd27SAsmitha Karunanithi             asyncResp->res.jsonValue["Name"] = dumpType + " Dump Entry";
5845cb1dd27SAsmitha Karunanithi 
5855cb1dd27SAsmitha Karunanithi             if (dumpType == "BMC")
5865cb1dd27SAsmitha Karunanithi             {
587d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "Manager";
588d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
589fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
590fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
5915cb1dd27SAsmitha Karunanithi             }
5925cb1dd27SAsmitha Karunanithi             else if (dumpType == "System")
5935cb1dd27SAsmitha Karunanithi             {
594d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["DiagnosticDataType"] = "OEM";
595002d39b4SEd Tanous                 asyncResp->res.jsonValue["OEMDiagnosticDataType"] = "System";
596d337bb72SAsmitha Karunanithi                 asyncResp->res.jsonValue["AdditionalDataURI"] =
597fdd26906SClaire Weinan                     entriesPath + entryID + "/attachment";
598fdd26906SClaire Weinan                 asyncResp->res.jsonValue["AdditionalDataSizeBytes"] = size;
5995cb1dd27SAsmitha Karunanithi             }
6005cb1dd27SAsmitha Karunanithi         }
601e05aec50SEd Tanous         if (!foundDumpEntry)
602b47452b2SAsmitha Karunanithi         {
603b47452b2SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Can't find Dump Entry";
604b47452b2SAsmitha Karunanithi             messages::internalError(asyncResp->res);
605b47452b2SAsmitha Karunanithi             return;
606b47452b2SAsmitha Karunanithi         }
6075cb1dd27SAsmitha Karunanithi         },
6085cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager", "/xyz/openbmc_project/dump",
6095cb1dd27SAsmitha Karunanithi         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
6105cb1dd27SAsmitha Karunanithi }
6115cb1dd27SAsmitha Karunanithi 
6128d1b46d7Szhanghch05 inline void deleteDumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6139878256fSStanley Chu                             const std::string& entryID,
614b47452b2SAsmitha Karunanithi                             const std::string& dumpType)
6155cb1dd27SAsmitha Karunanithi {
616002d39b4SEd Tanous     auto respHandler =
617002d39b4SEd Tanous         [asyncResp, entryID](const boost::system::error_code ec) {
6185cb1dd27SAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Entry doDelete callback: Done";
6195cb1dd27SAsmitha Karunanithi         if (ec)
6205cb1dd27SAsmitha Karunanithi         {
6213de8d8baSGeorge Liu             if (ec.value() == EBADR)
6223de8d8baSGeorge Liu             {
6233de8d8baSGeorge Liu                 messages::resourceNotFound(asyncResp->res, "LogEntry", entryID);
6243de8d8baSGeorge Liu                 return;
6253de8d8baSGeorge Liu             }
6265cb1dd27SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Dump (DBus) doDelete respHandler got error "
627fdd26906SClaire Weinan                              << ec << " entryID=" << entryID;
6285cb1dd27SAsmitha Karunanithi             messages::internalError(asyncResp->res);
6295cb1dd27SAsmitha Karunanithi             return;
6305cb1dd27SAsmitha Karunanithi         }
6315cb1dd27SAsmitha Karunanithi     };
6325cb1dd27SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
6335cb1dd27SAsmitha Karunanithi         respHandler, "xyz.openbmc_project.Dump.Manager",
634b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
635b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)) + "/entry/" +
636b47452b2SAsmitha Karunanithi             entryID,
6375cb1dd27SAsmitha Karunanithi         "xyz.openbmc_project.Object.Delete", "Delete");
6385cb1dd27SAsmitha Karunanithi }
6395cb1dd27SAsmitha Karunanithi 
6408d1b46d7Szhanghch05 inline void
64198be3e39SEd Tanous     createDumpTaskCallback(task::Payload&& payload,
6428d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6438d1b46d7Szhanghch05                            const uint32_t& dumpId, const std::string& dumpPath,
644a43be80fSAsmitha Karunanithi                            const std::string& dumpType)
645a43be80fSAsmitha Karunanithi {
646a43be80fSAsmitha Karunanithi     std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
64759d494eeSPatrick Williams         [dumpId, dumpPath,
64859d494eeSPatrick Williams          dumpType](boost::system::error_code err, sdbusplus::message_t& m,
649a43be80fSAsmitha Karunanithi                    const std::shared_ptr<task::TaskData>& taskData) {
650cb13a392SEd Tanous         if (err)
651cb13a392SEd Tanous         {
6526145ed6fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Error in creating a dump";
6536145ed6fSAsmitha Karunanithi             taskData->state = "Cancelled";
6546145ed6fSAsmitha Karunanithi             return task::completed;
655cb13a392SEd Tanous         }
656b9d36b47SEd Tanous 
657b9d36b47SEd Tanous         dbus::utility::DBusInteracesMap interfacesList;
658a43be80fSAsmitha Karunanithi 
659a43be80fSAsmitha Karunanithi         sdbusplus::message::object_path objPath;
660a43be80fSAsmitha Karunanithi 
661a43be80fSAsmitha Karunanithi         m.read(objPath, interfacesList);
662a43be80fSAsmitha Karunanithi 
663b47452b2SAsmitha Karunanithi         if (objPath.str ==
664b47452b2SAsmitha Karunanithi             "/xyz/openbmc_project/dump/" +
665b47452b2SAsmitha Karunanithi                 std::string(boost::algorithm::to_lower_copy(dumpType)) +
666b47452b2SAsmitha Karunanithi                 "/entry/" + std::to_string(dumpId))
667a43be80fSAsmitha Karunanithi         {
668a43be80fSAsmitha Karunanithi             nlohmann::json retMessage = messages::success();
669a43be80fSAsmitha Karunanithi             taskData->messages.emplace_back(retMessage);
670a43be80fSAsmitha Karunanithi 
671a43be80fSAsmitha Karunanithi             std::string headerLoc =
672a43be80fSAsmitha Karunanithi                 "Location: " + dumpPath + std::to_string(dumpId);
673002d39b4SEd Tanous             taskData->payload->httpHeaders.emplace_back(std::move(headerLoc));
674a43be80fSAsmitha Karunanithi 
675a43be80fSAsmitha Karunanithi             taskData->state = "Completed";
676b47452b2SAsmitha Karunanithi             return task::completed;
6776145ed6fSAsmitha Karunanithi         }
678a43be80fSAsmitha Karunanithi         return task::completed;
679a43be80fSAsmitha Karunanithi         },
6804978b63fSJason M. Bills         "type='signal',interface='org.freedesktop.DBus.ObjectManager',"
681a43be80fSAsmitha Karunanithi         "member='InterfacesAdded', "
682a43be80fSAsmitha Karunanithi         "path='/xyz/openbmc_project/dump'");
683a43be80fSAsmitha Karunanithi 
684a43be80fSAsmitha Karunanithi     task->startTimer(std::chrono::minutes(3));
685a43be80fSAsmitha Karunanithi     task->populateResp(asyncResp->res);
68698be3e39SEd Tanous     task->payload.emplace(std::move(payload));
687a43be80fSAsmitha Karunanithi }
688a43be80fSAsmitha Karunanithi 
6898d1b46d7Szhanghch05 inline void createDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
6908d1b46d7Szhanghch05                        const crow::Request& req, const std::string& dumpType)
691a43be80fSAsmitha Karunanithi {
692fdd26906SClaire Weinan     std::string dumpPath = getDumpEntriesPath(dumpType);
693fdd26906SClaire Weinan     if (dumpPath.empty())
694a43be80fSAsmitha Karunanithi     {
695a43be80fSAsmitha Karunanithi         messages::internalError(asyncResp->res);
696a43be80fSAsmitha Karunanithi         return;
697a43be80fSAsmitha Karunanithi     }
698a43be80fSAsmitha Karunanithi 
699a43be80fSAsmitha Karunanithi     std::optional<std::string> diagnosticDataType;
700a43be80fSAsmitha Karunanithi     std::optional<std::string> oemDiagnosticDataType;
701a43be80fSAsmitha Karunanithi 
70215ed6780SWilly Tu     if (!redfish::json_util::readJsonAction(
703a43be80fSAsmitha Karunanithi             req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
704a43be80fSAsmitha Karunanithi             "OEMDiagnosticDataType", oemDiagnosticDataType))
705a43be80fSAsmitha Karunanithi     {
706a43be80fSAsmitha Karunanithi         return;
707a43be80fSAsmitha Karunanithi     }
708a43be80fSAsmitha Karunanithi 
709a43be80fSAsmitha Karunanithi     if (dumpType == "System")
710a43be80fSAsmitha Karunanithi     {
711a43be80fSAsmitha Karunanithi         if (!oemDiagnosticDataType || !diagnosticDataType)
712a43be80fSAsmitha Karunanithi         {
7134978b63fSJason M. Bills             BMCWEB_LOG_ERROR
7144978b63fSJason M. Bills                 << "CreateDump action parameter 'DiagnosticDataType'/'OEMDiagnosticDataType' value not found!";
715a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
716a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData",
717a43be80fSAsmitha Karunanithi                 "DiagnosticDataType & OEMDiagnosticDataType");
718a43be80fSAsmitha Karunanithi             return;
719a43be80fSAsmitha Karunanithi         }
7203174e4dfSEd Tanous         if ((*oemDiagnosticDataType != "System") ||
721a43be80fSAsmitha Karunanithi             (*diagnosticDataType != "OEM"))
722a43be80fSAsmitha Karunanithi         {
723a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "Wrong parameter values passed";
724ace85d60SEd Tanous             messages::internalError(asyncResp->res);
725a43be80fSAsmitha Karunanithi             return;
726a43be80fSAsmitha Karunanithi         }
727a43be80fSAsmitha Karunanithi     }
728a43be80fSAsmitha Karunanithi     else if (dumpType == "BMC")
729a43be80fSAsmitha Karunanithi     {
730a43be80fSAsmitha Karunanithi         if (!diagnosticDataType)
731a43be80fSAsmitha Karunanithi         {
7320fda0f12SGeorge Liu             BMCWEB_LOG_ERROR
7330fda0f12SGeorge Liu                 << "CreateDump action parameter 'DiagnosticDataType' not found!";
734a43be80fSAsmitha Karunanithi             messages::actionParameterMissing(
735a43be80fSAsmitha Karunanithi                 asyncResp->res, "CollectDiagnosticData", "DiagnosticDataType");
736a43be80fSAsmitha Karunanithi             return;
737a43be80fSAsmitha Karunanithi         }
7383174e4dfSEd Tanous         if (*diagnosticDataType != "Manager")
739a43be80fSAsmitha Karunanithi         {
740a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR
741a43be80fSAsmitha Karunanithi                 << "Wrong parameter value passed for 'DiagnosticDataType'";
742ace85d60SEd Tanous             messages::internalError(asyncResp->res);
743a43be80fSAsmitha Karunanithi             return;
744a43be80fSAsmitha Karunanithi         }
745a43be80fSAsmitha Karunanithi     }
746a43be80fSAsmitha Karunanithi 
747a43be80fSAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
74898be3e39SEd Tanous         [asyncResp, payload(task::Payload(req)), dumpPath,
74998be3e39SEd Tanous          dumpType](const boost::system::error_code ec,
75098be3e39SEd Tanous                    const uint32_t& dumpId) mutable {
751a43be80fSAsmitha Karunanithi         if (ec)
752a43be80fSAsmitha Karunanithi         {
753a43be80fSAsmitha Karunanithi             BMCWEB_LOG_ERROR << "CreateDump resp_handler got error " << ec;
754a43be80fSAsmitha Karunanithi             messages::internalError(asyncResp->res);
755a43be80fSAsmitha Karunanithi             return;
756a43be80fSAsmitha Karunanithi         }
757a43be80fSAsmitha Karunanithi         BMCWEB_LOG_DEBUG << "Dump Created. Id: " << dumpId;
758a43be80fSAsmitha Karunanithi 
759002d39b4SEd Tanous         createDumpTaskCallback(std::move(payload), asyncResp, dumpId, dumpPath,
760002d39b4SEd Tanous                                dumpType);
761a43be80fSAsmitha Karunanithi         },
762b47452b2SAsmitha Karunanithi         "xyz.openbmc_project.Dump.Manager",
763b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" +
764b47452b2SAsmitha Karunanithi             std::string(boost::algorithm::to_lower_copy(dumpType)),
765a43be80fSAsmitha Karunanithi         "xyz.openbmc_project.Dump.Create", "CreateDump");
766a43be80fSAsmitha Karunanithi }
767a43be80fSAsmitha Karunanithi 
7688d1b46d7Szhanghch05 inline void clearDump(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
7698d1b46d7Szhanghch05                       const std::string& dumpType)
77080319af1SAsmitha Karunanithi {
771b47452b2SAsmitha Karunanithi     std::string dumpTypeLowerCopy =
772b47452b2SAsmitha Karunanithi         std::string(boost::algorithm::to_lower_copy(dumpType));
7738d1b46d7Szhanghch05 
77480319af1SAsmitha Karunanithi     crow::connections::systemBus->async_method_call(
775b9d36b47SEd Tanous         [asyncResp, dumpType](
776b9d36b47SEd Tanous             const boost::system::error_code ec,
777b9d36b47SEd Tanous             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
77880319af1SAsmitha Karunanithi         if (ec)
77980319af1SAsmitha Karunanithi         {
78080319af1SAsmitha Karunanithi             BMCWEB_LOG_ERROR << "resp_handler got error " << ec;
78180319af1SAsmitha Karunanithi             messages::internalError(asyncResp->res);
78280319af1SAsmitha Karunanithi             return;
78380319af1SAsmitha Karunanithi         }
78480319af1SAsmitha Karunanithi 
78580319af1SAsmitha Karunanithi         for (const std::string& path : subTreePaths)
78680319af1SAsmitha Karunanithi         {
7872dfd18efSEd Tanous             sdbusplus::message::object_path objPath(path);
7882dfd18efSEd Tanous             std::string logID = objPath.filename();
7892dfd18efSEd Tanous             if (logID.empty())
79080319af1SAsmitha Karunanithi             {
7912dfd18efSEd Tanous                 continue;
79280319af1SAsmitha Karunanithi             }
7932dfd18efSEd Tanous             deleteDumpEntry(asyncResp, logID, dumpType);
79480319af1SAsmitha Karunanithi         }
79580319af1SAsmitha Karunanithi         },
79680319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper",
79780319af1SAsmitha Karunanithi         "/xyz/openbmc_project/object_mapper",
79880319af1SAsmitha Karunanithi         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
799b47452b2SAsmitha Karunanithi         "/xyz/openbmc_project/dump/" + dumpTypeLowerCopy, 0,
800b47452b2SAsmitha Karunanithi         std::array<std::string, 1>{"xyz.openbmc_project.Dump.Entry." +
801b47452b2SAsmitha Karunanithi                                    dumpType});
80280319af1SAsmitha Karunanithi }
80380319af1SAsmitha Karunanithi 
804b9d36b47SEd Tanous inline static void
805b9d36b47SEd Tanous     parseCrashdumpParameters(const dbus::utility::DBusPropertiesMap& params,
806b9d36b47SEd Tanous                              std::string& filename, std::string& timestamp,
807b9d36b47SEd Tanous                              std::string& logfile)
808043a0536SJohnathan Mantey {
809043a0536SJohnathan Mantey     for (auto property : params)
810043a0536SJohnathan Mantey     {
811043a0536SJohnathan Mantey         if (property.first == "Timestamp")
812043a0536SJohnathan Mantey         {
813043a0536SJohnathan Mantey             const std::string* value =
8148d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
815043a0536SJohnathan Mantey             if (value != nullptr)
816043a0536SJohnathan Mantey             {
817043a0536SJohnathan Mantey                 timestamp = *value;
818043a0536SJohnathan Mantey             }
819043a0536SJohnathan Mantey         }
820043a0536SJohnathan Mantey         else if (property.first == "Filename")
821043a0536SJohnathan Mantey         {
822043a0536SJohnathan Mantey             const std::string* value =
8238d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
824043a0536SJohnathan Mantey             if (value != nullptr)
825043a0536SJohnathan Mantey             {
826043a0536SJohnathan Mantey                 filename = *value;
827043a0536SJohnathan Mantey             }
828043a0536SJohnathan Mantey         }
829043a0536SJohnathan Mantey         else if (property.first == "Log")
830043a0536SJohnathan Mantey         {
831043a0536SJohnathan Mantey             const std::string* value =
8328d78b7a9SPatrick Williams                 std::get_if<std::string>(&property.second);
833043a0536SJohnathan Mantey             if (value != nullptr)
834043a0536SJohnathan Mantey             {
835043a0536SJohnathan Mantey                 logfile = *value;
836043a0536SJohnathan Mantey             }
837043a0536SJohnathan Mantey         }
838043a0536SJohnathan Mantey     }
839043a0536SJohnathan Mantey }
840043a0536SJohnathan Mantey 
841a3316fc6SZhikuiRen constexpr char const* postCodeIface = "xyz.openbmc_project.State.Boot.PostCode";
8427e860f15SJohn Edward Broadbent inline void requestRoutesSystemLogServiceCollection(App& app)
8431da66f75SEd Tanous {
844c4bf6374SJason M. Bills     /**
845c4bf6374SJason M. Bills      * Functions triggers appropriate requests on DBus
846c4bf6374SJason M. Bills      */
8477e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/")
848ed398213SEd Tanous         .privileges(redfish::privileges::getLogServiceCollection)
849002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
850002d39b4SEd Tanous             [&app](const crow::Request& req,
851002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
8523ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
853c4bf6374SJason M. Bills         {
85445ca1b86SEd Tanous             return;
85545ca1b86SEd Tanous         }
8567e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
8577e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
858c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
859c4bf6374SJason M. Bills             "#LogServiceCollection.LogServiceCollection";
860c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
861029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices";
86245ca1b86SEd Tanous         asyncResp->res.jsonValue["Name"] = "System Log Services Collection";
863c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
864c4bf6374SJason M. Bills             "Collection of LogServices for this Computer System";
865002d39b4SEd Tanous         nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
866c4bf6374SJason M. Bills         logServiceArray = nlohmann::json::array();
8671476687dSEd Tanous         nlohmann::json::object_t eventLog;
8681476687dSEd Tanous         eventLog["@odata.id"] =
8691476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
8701476687dSEd Tanous         logServiceArray.push_back(std::move(eventLog));
8715cb1dd27SAsmitha Karunanithi #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
8721476687dSEd Tanous         nlohmann::json::object_t dumpLog;
873002d39b4SEd Tanous         dumpLog["@odata.id"] = "/redfish/v1/Systems/system/LogServices/Dump";
8741476687dSEd Tanous         logServiceArray.push_back(std::move(dumpLog));
875c9bb6861Sraviteja-b #endif
876c9bb6861Sraviteja-b 
877d53dd41fSJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_CPU_LOG
8781476687dSEd Tanous         nlohmann::json::object_t crashdump;
8791476687dSEd Tanous         crashdump["@odata.id"] =
8801476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump";
8811476687dSEd Tanous         logServiceArray.push_back(std::move(crashdump));
882d53dd41fSJason M. Bills #endif
883b7028ebfSSpencer Ku 
884b7028ebfSSpencer Ku #ifdef BMCWEB_ENABLE_REDFISH_HOST_LOGGER
8851476687dSEd Tanous         nlohmann::json::object_t hostlogger;
8861476687dSEd Tanous         hostlogger["@odata.id"] =
8871476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger";
8881476687dSEd Tanous         logServiceArray.push_back(std::move(hostlogger));
889b7028ebfSSpencer Ku #endif
890c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
891c4bf6374SJason M. Bills             logServiceArray.size();
892a3316fc6SZhikuiRen 
893a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
89445ca1b86SEd Tanous             [asyncResp](const boost::system::error_code ec,
895b9d36b47SEd Tanous                         const dbus::utility::MapperGetSubTreePathsResponse&
896b9d36b47SEd Tanous                             subtreePath) {
897a3316fc6SZhikuiRen             if (ec)
898a3316fc6SZhikuiRen             {
899a3316fc6SZhikuiRen                 BMCWEB_LOG_ERROR << ec;
900a3316fc6SZhikuiRen                 return;
901a3316fc6SZhikuiRen             }
902a3316fc6SZhikuiRen 
90355f79e6fSEd Tanous             for (const auto& pathStr : subtreePath)
904a3316fc6SZhikuiRen             {
905a3316fc6SZhikuiRen                 if (pathStr.find("PostCode") != std::string::npos)
906a3316fc6SZhikuiRen                 {
90723a21a1cSEd Tanous                     nlohmann::json& logServiceArrayLocal =
908a3316fc6SZhikuiRen                         asyncResp->res.jsonValue["Members"];
90923a21a1cSEd Tanous                     logServiceArrayLocal.push_back(
9100fda0f12SGeorge Liu                         {{"@odata.id",
9110fda0f12SGeorge Liu                           "/redfish/v1/Systems/system/LogServices/PostCodes"}});
91245ca1b86SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
91323a21a1cSEd Tanous                         logServiceArrayLocal.size();
914a3316fc6SZhikuiRen                     return;
915a3316fc6SZhikuiRen                 }
916a3316fc6SZhikuiRen             }
917a3316fc6SZhikuiRen             },
918a3316fc6SZhikuiRen             "xyz.openbmc_project.ObjectMapper",
919a3316fc6SZhikuiRen             "/xyz/openbmc_project/object_mapper",
92045ca1b86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
92145ca1b86SEd Tanous             std::array<const char*, 1>{postCodeIface});
9227e860f15SJohn Edward Broadbent         });
923c4bf6374SJason M. Bills }
924c4bf6374SJason M. Bills 
9257e860f15SJohn Edward Broadbent inline void requestRoutesEventLogService(App& app)
926c4bf6374SJason M. Bills {
9277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/EventLog/")
928ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
929002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
930002d39b4SEd Tanous             [&app](const crow::Request& req,
931002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9323ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
93345ca1b86SEd Tanous         {
93445ca1b86SEd Tanous             return;
93545ca1b86SEd Tanous         }
936c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
937029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog";
938c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
939c4bf6374SJason M. Bills             "#LogService.v1_1_0.LogService";
940c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Event Log Service";
941002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Event Log Service";
942c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "EventLog";
943c4bf6374SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
9447c8c4058STejas Patil 
9457c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
9467c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
9477c8c4058STejas Patil 
9487c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
9497c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
9507c8c4058STejas Patil             redfishDateTimeOffset.second;
9517c8c4058STejas Patil 
9521476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
9531476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
954e7d6c8b2SGunnar Mills         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
955e7d6c8b2SGunnar Mills 
9560fda0f12SGeorge Liu             {"target",
9570fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog"}};
9587e860f15SJohn Edward Broadbent         });
959489640c6SJason M. Bills }
960489640c6SJason M. Bills 
9617e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogClear(App& app)
962489640c6SJason M. Bills {
9634978b63fSJason M. Bills     BMCWEB_ROUTE(
9644978b63fSJason M. Bills         app,
9654978b63fSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
966432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
9677e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
96845ca1b86SEd Tanous             [&app](const crow::Request& req,
9697e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
9703ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
97145ca1b86SEd Tanous         {
97245ca1b86SEd Tanous             return;
97345ca1b86SEd Tanous         }
974489640c6SJason M. Bills         // Clear the EventLog by deleting the log files
975489640c6SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
976489640c6SJason M. Bills         if (getRedfishLogFiles(redfishLogFiles))
977489640c6SJason M. Bills         {
978489640c6SJason M. Bills             for (const std::filesystem::path& file : redfishLogFiles)
979489640c6SJason M. Bills             {
980489640c6SJason M. Bills                 std::error_code ec;
981489640c6SJason M. Bills                 std::filesystem::remove(file, ec);
982489640c6SJason M. Bills             }
983489640c6SJason M. Bills         }
984489640c6SJason M. Bills 
985489640c6SJason M. Bills         // Reload rsyslog so it knows to start new log files
986489640c6SJason M. Bills         crow::connections::systemBus->async_method_call(
987489640c6SJason M. Bills             [asyncResp](const boost::system::error_code ec) {
988489640c6SJason M. Bills             if (ec)
989489640c6SJason M. Bills             {
990002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Failed to reload rsyslog: " << ec;
991489640c6SJason M. Bills                 messages::internalError(asyncResp->res);
992489640c6SJason M. Bills                 return;
993489640c6SJason M. Bills             }
994489640c6SJason M. Bills 
995489640c6SJason M. Bills             messages::success(asyncResp->res);
996489640c6SJason M. Bills             },
997489640c6SJason M. Bills             "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
998002d39b4SEd Tanous             "org.freedesktop.systemd1.Manager", "ReloadUnit", "rsyslog.service",
999002d39b4SEd Tanous             "replace");
10007e860f15SJohn Edward Broadbent         });
1001c4bf6374SJason M. Bills }
1002c4bf6374SJason M. Bills 
1003ac992cdeSJason M. Bills enum class LogParseError
1004ac992cdeSJason M. Bills {
1005ac992cdeSJason M. Bills     success,
1006ac992cdeSJason M. Bills     parseFailed,
1007ac992cdeSJason M. Bills     messageIdNotInRegistry,
1008ac992cdeSJason M. Bills };
1009ac992cdeSJason M. Bills 
1010ac992cdeSJason M. Bills static LogParseError
1011ac992cdeSJason M. Bills     fillEventLogEntryJson(const std::string& logEntryID,
1012b5a76932SEd Tanous                           const std::string& logEntry,
1013de703c5dSJason M. Bills                           nlohmann::json::object_t& logEntryJson)
1014c4bf6374SJason M. Bills {
101595820184SJason M. Bills     // The redfish log format is "<Timestamp> <MessageId>,<MessageArgs>"
1016cd225da8SJason M. Bills     // First get the Timestamp
1017f23b7296SEd Tanous     size_t space = logEntry.find_first_of(' ');
1018cd225da8SJason M. Bills     if (space == std::string::npos)
101995820184SJason M. Bills     {
1020ac992cdeSJason M. Bills         return LogParseError::parseFailed;
102195820184SJason M. Bills     }
1022cd225da8SJason M. Bills     std::string timestamp = logEntry.substr(0, space);
1023cd225da8SJason M. Bills     // Then get the log contents
1024f23b7296SEd Tanous     size_t entryStart = logEntry.find_first_not_of(' ', space);
1025cd225da8SJason M. Bills     if (entryStart == std::string::npos)
1026cd225da8SJason M. Bills     {
1027ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1028cd225da8SJason M. Bills     }
1029cd225da8SJason M. Bills     std::string_view entry(logEntry);
1030cd225da8SJason M. Bills     entry.remove_prefix(entryStart);
1031cd225da8SJason M. Bills     // Use split to separate the entry into its fields
1032cd225da8SJason M. Bills     std::vector<std::string> logEntryFields;
1033cd225da8SJason M. Bills     boost::split(logEntryFields, entry, boost::is_any_of(","),
1034cd225da8SJason M. Bills                  boost::token_compress_on);
1035cd225da8SJason M. Bills     // We need at least a MessageId to be valid
103626f6976fSEd Tanous     if (logEntryFields.empty())
1037cd225da8SJason M. Bills     {
1038ac992cdeSJason M. Bills         return LogParseError::parseFailed;
1039cd225da8SJason M. Bills     }
1040cd225da8SJason M. Bills     std::string& messageID = logEntryFields[0];
104195820184SJason M. Bills 
10424851d45dSJason M. Bills     // Get the Message from the MessageRegistry
1043fffb8c1fSEd Tanous     const registries::Message* message = registries::getMessage(messageID);
1044c4bf6374SJason M. Bills 
104554417b02SSui Chen     if (message == nullptr)
1046c4bf6374SJason M. Bills     {
104754417b02SSui Chen         BMCWEB_LOG_WARNING << "Log entry not found in registry: " << logEntry;
1048ac992cdeSJason M. Bills         return LogParseError::messageIdNotInRegistry;
1049c4bf6374SJason M. Bills     }
1050c4bf6374SJason M. Bills 
105154417b02SSui Chen     std::string msg = message->message;
105254417b02SSui Chen 
105315a86ff6SJason M. Bills     // Get the MessageArgs from the log if there are any
105426702d01SEd Tanous     std::span<std::string> messageArgs;
105515a86ff6SJason M. Bills     if (logEntryFields.size() > 1)
105615a86ff6SJason M. Bills     {
105715a86ff6SJason M. Bills         std::string& messageArgsStart = logEntryFields[1];
105815a86ff6SJason M. Bills         // If the first string is empty, assume there are no MessageArgs
105915a86ff6SJason M. Bills         std::size_t messageArgsSize = 0;
106015a86ff6SJason M. Bills         if (!messageArgsStart.empty())
106115a86ff6SJason M. Bills         {
106215a86ff6SJason M. Bills             messageArgsSize = logEntryFields.size() - 1;
106315a86ff6SJason M. Bills         }
106415a86ff6SJason M. Bills 
106523a21a1cSEd Tanous         messageArgs = {&messageArgsStart, messageArgsSize};
1066c4bf6374SJason M. Bills 
10674851d45dSJason M. Bills         // Fill the MessageArgs into the Message
106895820184SJason M. Bills         int i = 0;
106995820184SJason M. Bills         for (const std::string& messageArg : messageArgs)
10704851d45dSJason M. Bills         {
107195820184SJason M. Bills             std::string argStr = "%" + std::to_string(++i);
10724851d45dSJason M. Bills             size_t argPos = msg.find(argStr);
10734851d45dSJason M. Bills             if (argPos != std::string::npos)
10744851d45dSJason M. Bills             {
107595820184SJason M. Bills                 msg.replace(argPos, argStr.length(), messageArg);
10764851d45dSJason M. Bills             }
10774851d45dSJason M. Bills         }
107815a86ff6SJason M. Bills     }
10794851d45dSJason M. Bills 
108095820184SJason M. Bills     // Get the Created time from the timestamp. The log timestamp is in RFC3339
108195820184SJason M. Bills     // format which matches the Redfish format except for the fractional seconds
108295820184SJason M. Bills     // between the '.' and the '+', so just remove them.
1083f23b7296SEd Tanous     std::size_t dot = timestamp.find_first_of('.');
1084f23b7296SEd Tanous     std::size_t plus = timestamp.find_first_of('+');
108595820184SJason M. Bills     if (dot != std::string::npos && plus != std::string::npos)
1086c4bf6374SJason M. Bills     {
108795820184SJason M. Bills         timestamp.erase(dot, plus - dot);
1088c4bf6374SJason M. Bills     }
1089c4bf6374SJason M. Bills 
1090c4bf6374SJason M. Bills     // Fill in the log entry with the gathered data
109184afc48bSJason M. Bills     logEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
109284afc48bSJason M. Bills     logEntryJson["@odata.id"] =
109384afc48bSJason M. Bills         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" + logEntryID;
109484afc48bSJason M. Bills     logEntryJson["Name"] = "System Event Log Entry";
109584afc48bSJason M. Bills     logEntryJson["Id"] = logEntryID;
109684afc48bSJason M. Bills     logEntryJson["Message"] = std::move(msg);
109784afc48bSJason M. Bills     logEntryJson["MessageId"] = std::move(messageID);
109884afc48bSJason M. Bills     logEntryJson["MessageArgs"] = messageArgs;
109984afc48bSJason M. Bills     logEntryJson["EntryType"] = "Event";
110084afc48bSJason M. Bills     logEntryJson["Severity"] = message->messageSeverity;
110184afc48bSJason M. Bills     logEntryJson["Created"] = std::move(timestamp);
1102ac992cdeSJason M. Bills     return LogParseError::success;
1103c4bf6374SJason M. Bills }
1104c4bf6374SJason M. Bills 
11057e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntryCollection(App& app)
1106c4bf6374SJason M. Bills {
11077e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
11087e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
11098b6a35f0SGunnar Mills         .privileges(redfish::privileges::getLogEntryCollection)
1110002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1111002d39b4SEd Tanous             [&app](const crow::Request& req,
1112002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1113c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1114c937d2bfSEd Tanous             .canDelegateTop = true,
1115c937d2bfSEd Tanous             .canDelegateSkip = true,
1116c937d2bfSEd Tanous         };
1117c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1118c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
11193ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1120c4bf6374SJason M. Bills         {
1121c4bf6374SJason M. Bills             return;
1122c4bf6374SJason M. Bills         }
11233648c8beSEd Tanous         size_t top =
11243648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
11253648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
11263648c8beSEd Tanous 
11277e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
11287e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
1129c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
1130c4bf6374SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
1131c4bf6374SJason M. Bills         asyncResp->res.jsonValue["@odata.id"] =
1132029573d4SEd Tanous             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
1133c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
1134c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Description"] =
1135c4bf6374SJason M. Bills             "Collection of System Event Log Entries";
1136cb92c03bSAndrew Geissler 
11374978b63fSJason M. Bills         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1138c4bf6374SJason M. Bills         logEntryArray = nlohmann::json::array();
11397e860f15SJohn Edward Broadbent         // Go through the log files and create a unique ID for each
11407e860f15SJohn Edward Broadbent         // entry
114195820184SJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
114295820184SJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1143b01bf299SEd Tanous         uint64_t entryCount = 0;
1144cd225da8SJason M. Bills         std::string logEntry;
114595820184SJason M. Bills 
11467e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
11477e860f15SJohn Edward Broadbent         // backwards
1148002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1149002d39b4SEd Tanous              it++)
1150c4bf6374SJason M. Bills         {
1151cd225da8SJason M. Bills             std::ifstream logStream(*it);
115295820184SJason M. Bills             if (!logStream.is_open())
1153c4bf6374SJason M. Bills             {
1154c4bf6374SJason M. Bills                 continue;
1155c4bf6374SJason M. Bills             }
1156c4bf6374SJason M. Bills 
1157e85d6b16SJason M. Bills             // Reset the unique ID on the first entry
1158e85d6b16SJason M. Bills             bool firstEntry = true;
115995820184SJason M. Bills             while (std::getline(logStream, logEntry))
116095820184SJason M. Bills             {
1161c4bf6374SJason M. Bills                 std::string idStr;
1162e85d6b16SJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1163c4bf6374SJason M. Bills                 {
1164c4bf6374SJason M. Bills                     continue;
1165c4bf6374SJason M. Bills                 }
1166e85d6b16SJason M. Bills                 firstEntry = false;
1167e85d6b16SJason M. Bills 
1168de703c5dSJason M. Bills                 nlohmann::json::object_t bmcLogEntry;
1169ac992cdeSJason M. Bills                 LogParseError status =
1170ac992cdeSJason M. Bills                     fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1171ac992cdeSJason M. Bills                 if (status == LogParseError::messageIdNotInRegistry)
1172ac992cdeSJason M. Bills                 {
1173ac992cdeSJason M. Bills                     continue;
1174ac992cdeSJason M. Bills                 }
1175ac992cdeSJason M. Bills                 if (status != LogParseError::success)
1176c4bf6374SJason M. Bills                 {
1177c4bf6374SJason M. Bills                     messages::internalError(asyncResp->res);
1178c4bf6374SJason M. Bills                     return;
1179c4bf6374SJason M. Bills                 }
1180de703c5dSJason M. Bills 
1181de703c5dSJason M. Bills                 entryCount++;
1182de703c5dSJason M. Bills                 // Handle paging using skip (number of entries to skip from the
1183de703c5dSJason M. Bills                 // start) and top (number of entries to display)
11843648c8beSEd Tanous                 if (entryCount <= skip || entryCount > skip + top)
1185de703c5dSJason M. Bills                 {
1186de703c5dSJason M. Bills                     continue;
1187de703c5dSJason M. Bills                 }
1188de703c5dSJason M. Bills 
1189de703c5dSJason M. Bills                 logEntryArray.push_back(std::move(bmcLogEntry));
1190c4bf6374SJason M. Bills             }
119195820184SJason M. Bills         }
1192c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
11933648c8beSEd Tanous         if (skip + top < entryCount)
1194c4bf6374SJason M. Bills         {
1195c4bf6374SJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
11964978b63fSJason M. Bills                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries?$skip=" +
11973648c8beSEd Tanous                 std::to_string(skip + top);
1198c4bf6374SJason M. Bills         }
11997e860f15SJohn Edward Broadbent         });
1200897967deSJason M. Bills }
1201897967deSJason M. Bills 
12027e860f15SJohn Edward Broadbent inline void requestRoutesJournalEventLogEntry(App& app)
1203897967deSJason M. Bills {
12047e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
12057e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1206ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
12077e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
120845ca1b86SEd Tanous             [&app](const crow::Request& req,
12097e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
12107e860f15SJohn Edward Broadbent                    const std::string& param) {
12113ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
121245ca1b86SEd Tanous         {
121345ca1b86SEd Tanous             return;
121445ca1b86SEd Tanous         }
12157e860f15SJohn Edward Broadbent         const std::string& targetID = param;
12168d1b46d7Szhanghch05 
12177e860f15SJohn Edward Broadbent         // Go through the log files and check the unique ID for each
12187e860f15SJohn Edward Broadbent         // entry to find the target entry
1219897967deSJason M. Bills         std::vector<std::filesystem::path> redfishLogFiles;
1220897967deSJason M. Bills         getRedfishLogFiles(redfishLogFiles);
1221897967deSJason M. Bills         std::string logEntry;
1222897967deSJason M. Bills 
12237e860f15SJohn Edward Broadbent         // Oldest logs are in the last file, so start there and loop
12247e860f15SJohn Edward Broadbent         // backwards
1225002d39b4SEd Tanous         for (auto it = redfishLogFiles.rbegin(); it < redfishLogFiles.rend();
1226002d39b4SEd Tanous              it++)
1227897967deSJason M. Bills         {
1228897967deSJason M. Bills             std::ifstream logStream(*it);
1229897967deSJason M. Bills             if (!logStream.is_open())
1230897967deSJason M. Bills             {
1231897967deSJason M. Bills                 continue;
1232897967deSJason M. Bills             }
1233897967deSJason M. Bills 
1234897967deSJason M. Bills             // Reset the unique ID on the first entry
1235897967deSJason M. Bills             bool firstEntry = true;
1236897967deSJason M. Bills             while (std::getline(logStream, logEntry))
1237897967deSJason M. Bills             {
1238897967deSJason M. Bills                 std::string idStr;
1239897967deSJason M. Bills                 if (!getUniqueEntryID(logEntry, idStr, firstEntry))
1240897967deSJason M. Bills                 {
1241897967deSJason M. Bills                     continue;
1242897967deSJason M. Bills                 }
1243897967deSJason M. Bills                 firstEntry = false;
1244897967deSJason M. Bills 
1245897967deSJason M. Bills                 if (idStr == targetID)
1246897967deSJason M. Bills                 {
1247de703c5dSJason M. Bills                     nlohmann::json::object_t bmcLogEntry;
1248ac992cdeSJason M. Bills                     LogParseError status =
1249ac992cdeSJason M. Bills                         fillEventLogEntryJson(idStr, logEntry, bmcLogEntry);
1250ac992cdeSJason M. Bills                     if (status != LogParseError::success)
1251897967deSJason M. Bills                     {
1252897967deSJason M. Bills                         messages::internalError(asyncResp->res);
1253897967deSJason M. Bills                         return;
1254897967deSJason M. Bills                     }
1255d405bb51SJason M. Bills                     asyncResp->res.jsonValue.update(bmcLogEntry);
1256897967deSJason M. Bills                     return;
1257897967deSJason M. Bills                 }
1258897967deSJason M. Bills             }
1259897967deSJason M. Bills         }
1260897967deSJason M. Bills         // Requested ID was not found
1261002d39b4SEd Tanous         messages::resourceMissingAtURI(asyncResp->res,
1262002d39b4SEd Tanous                                        crow::utility::urlFromPieces(targetID));
12637e860f15SJohn Edward Broadbent         });
126408a4e4b5SAnthony Wilson }
126508a4e4b5SAnthony Wilson 
12667e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryCollection(App& app)
126708a4e4b5SAnthony Wilson {
12687e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
12697e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/EventLog/Entries/")
1270ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
1271002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1272002d39b4SEd Tanous             [&app](const crow::Request& req,
1273002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
12743ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
127545ca1b86SEd Tanous         {
127645ca1b86SEd Tanous             return;
127745ca1b86SEd Tanous         }
12787e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
12797e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
128008a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.type"] =
128108a4e4b5SAnthony Wilson             "#LogEntryCollection.LogEntryCollection";
128208a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["@odata.id"] =
128308a4e4b5SAnthony Wilson             "/redfish/v1/Systems/system/LogServices/EventLog/Entries";
128408a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Name"] = "System Event Log Entries";
128508a4e4b5SAnthony Wilson         asyncResp->res.jsonValue["Description"] =
128608a4e4b5SAnthony Wilson             "Collection of System Event Log Entries";
128708a4e4b5SAnthony Wilson 
1288cb92c03bSAndrew Geissler         // DBus implementation of EventLog/Entries
1289cb92c03bSAndrew Geissler         // Make call to Logging Service to find all log entry objects
1290cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
1291cb92c03bSAndrew Geissler             [asyncResp](const boost::system::error_code ec,
1292914e2d5dSEd Tanous                         const dbus::utility::ManagedObjectType& resp) {
1293cb92c03bSAndrew Geissler             if (ec)
1294cb92c03bSAndrew Geissler             {
1295cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
1296cb92c03bSAndrew Geissler                 BMCWEB_LOG_ERROR
1297002d39b4SEd Tanous                     << "getLogEntriesIfaceData resp_handler got error " << ec;
1298cb92c03bSAndrew Geissler                 messages::internalError(asyncResp->res);
1299cb92c03bSAndrew Geissler                 return;
1300cb92c03bSAndrew Geissler             }
1301002d39b4SEd Tanous             nlohmann::json& entriesArray = asyncResp->res.jsonValue["Members"];
1302cb92c03bSAndrew Geissler             entriesArray = nlohmann::json::array();
13039eb808c1SEd Tanous             for (const auto& objectPath : resp)
1304cb92c03bSAndrew Geissler             {
1305914e2d5dSEd Tanous                 const uint32_t* id = nullptr;
1306c419c759SEd Tanous                 const uint64_t* timestamp = nullptr;
1307c419c759SEd Tanous                 const uint64_t* updateTimestamp = nullptr;
1308914e2d5dSEd Tanous                 const std::string* severity = nullptr;
1309914e2d5dSEd Tanous                 const std::string* message = nullptr;
1310914e2d5dSEd Tanous                 const std::string* filePath = nullptr;
131175710de2SXiaochao Ma                 bool resolved = false;
13129eb808c1SEd Tanous                 for (const auto& interfaceMap : objectPath.second)
1313f86bb901SAdriana Kobylak                 {
1314f86bb901SAdriana Kobylak                     if (interfaceMap.first ==
1315f86bb901SAdriana Kobylak                         "xyz.openbmc_project.Logging.Entry")
1316f86bb901SAdriana Kobylak                     {
1317002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
1318cb92c03bSAndrew Geissler                         {
1319cb92c03bSAndrew Geissler                             if (propertyMap.first == "Id")
1320cb92c03bSAndrew Geissler                             {
1321002d39b4SEd Tanous                                 id = std::get_if<uint32_t>(&propertyMap.second);
1322cb92c03bSAndrew Geissler                             }
1323cb92c03bSAndrew Geissler                             else if (propertyMap.first == "Timestamp")
1324cb92c03bSAndrew Geissler                             {
1325002d39b4SEd Tanous                                 timestamp =
1326002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13277e860f15SJohn Edward Broadbent                             }
1328002d39b4SEd Tanous                             else if (propertyMap.first == "UpdateTimestamp")
13297e860f15SJohn Edward Broadbent                             {
1330002d39b4SEd Tanous                                 updateTimestamp =
1331002d39b4SEd Tanous                                     std::get_if<uint64_t>(&propertyMap.second);
13327e860f15SJohn Edward Broadbent                             }
13337e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Severity")
13347e860f15SJohn Edward Broadbent                             {
13357e860f15SJohn Edward Broadbent                                 severity = std::get_if<std::string>(
13367e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13377e860f15SJohn Edward Broadbent                             }
13387e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Message")
13397e860f15SJohn Edward Broadbent                             {
13407e860f15SJohn Edward Broadbent                                 message = std::get_if<std::string>(
13417e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13427e860f15SJohn Edward Broadbent                             }
13437e860f15SJohn Edward Broadbent                             else if (propertyMap.first == "Resolved")
13447e860f15SJohn Edward Broadbent                             {
1345914e2d5dSEd Tanous                                 const bool* resolveptr =
1346002d39b4SEd Tanous                                     std::get_if<bool>(&propertyMap.second);
13477e860f15SJohn Edward Broadbent                                 if (resolveptr == nullptr)
13487e860f15SJohn Edward Broadbent                                 {
1349002d39b4SEd Tanous                                     messages::internalError(asyncResp->res);
13507e860f15SJohn Edward Broadbent                                     return;
13517e860f15SJohn Edward Broadbent                                 }
13527e860f15SJohn Edward Broadbent                                 resolved = *resolveptr;
13537e860f15SJohn Edward Broadbent                             }
13547e860f15SJohn Edward Broadbent                         }
13557e860f15SJohn Edward Broadbent                         if (id == nullptr || message == nullptr ||
13567e860f15SJohn Edward Broadbent                             severity == nullptr)
13577e860f15SJohn Edward Broadbent                         {
13587e860f15SJohn Edward Broadbent                             messages::internalError(asyncResp->res);
13597e860f15SJohn Edward Broadbent                             return;
13607e860f15SJohn Edward Broadbent                         }
13617e860f15SJohn Edward Broadbent                     }
13627e860f15SJohn Edward Broadbent                     else if (interfaceMap.first ==
13637e860f15SJohn Edward Broadbent                              "xyz.openbmc_project.Common.FilePath")
13647e860f15SJohn Edward Broadbent                     {
1365002d39b4SEd Tanous                         for (const auto& propertyMap : interfaceMap.second)
13667e860f15SJohn Edward Broadbent                         {
13677e860f15SJohn Edward Broadbent                             if (propertyMap.first == "Path")
13687e860f15SJohn Edward Broadbent                             {
13697e860f15SJohn Edward Broadbent                                 filePath = std::get_if<std::string>(
13707e860f15SJohn Edward Broadbent                                     &propertyMap.second);
13717e860f15SJohn Edward Broadbent                             }
13727e860f15SJohn Edward Broadbent                         }
13737e860f15SJohn Edward Broadbent                     }
13747e860f15SJohn Edward Broadbent                 }
13757e860f15SJohn Edward Broadbent                 // Object path without the
13767e860f15SJohn Edward Broadbent                 // xyz.openbmc_project.Logging.Entry interface, ignore
13777e860f15SJohn Edward Broadbent                 // and continue.
13787e860f15SJohn Edward Broadbent                 if (id == nullptr || message == nullptr ||
1379c419c759SEd Tanous                     severity == nullptr || timestamp == nullptr ||
1380c419c759SEd Tanous                     updateTimestamp == nullptr)
13817e860f15SJohn Edward Broadbent                 {
13827e860f15SJohn Edward Broadbent                     continue;
13837e860f15SJohn Edward Broadbent                 }
13847e860f15SJohn Edward Broadbent                 entriesArray.push_back({});
13857e860f15SJohn Edward Broadbent                 nlohmann::json& thisEntry = entriesArray.back();
13867e860f15SJohn Edward Broadbent                 thisEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
13877e860f15SJohn Edward Broadbent                 thisEntry["@odata.id"] =
13880fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
13897e860f15SJohn Edward Broadbent                     std::to_string(*id);
13907e860f15SJohn Edward Broadbent                 thisEntry["Name"] = "System Event Log Entry";
13917e860f15SJohn Edward Broadbent                 thisEntry["Id"] = std::to_string(*id);
13927e860f15SJohn Edward Broadbent                 thisEntry["Message"] = *message;
13937e860f15SJohn Edward Broadbent                 thisEntry["Resolved"] = resolved;
13947e860f15SJohn Edward Broadbent                 thisEntry["EntryType"] = "Event";
13957e860f15SJohn Edward Broadbent                 thisEntry["Severity"] =
13967e860f15SJohn Edward Broadbent                     translateSeverityDbusToRedfish(*severity);
13977e860f15SJohn Edward Broadbent                 thisEntry["Created"] =
1398c419c759SEd Tanous                     crow::utility::getDateTimeUintMs(*timestamp);
13997e860f15SJohn Edward Broadbent                 thisEntry["Modified"] =
1400c419c759SEd Tanous                     crow::utility::getDateTimeUintMs(*updateTimestamp);
14017e860f15SJohn Edward Broadbent                 if (filePath != nullptr)
14027e860f15SJohn Edward Broadbent                 {
14037e860f15SJohn Edward Broadbent                     thisEntry["AdditionalDataURI"] =
14040fda0f12SGeorge Liu                         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
14057e860f15SJohn Edward Broadbent                         std::to_string(*id) + "/attachment";
14067e860f15SJohn Edward Broadbent                 }
14077e860f15SJohn Edward Broadbent             }
1408002d39b4SEd Tanous             std::sort(
1409002d39b4SEd Tanous                 entriesArray.begin(), entriesArray.end(),
1410002d39b4SEd Tanous                 [](const nlohmann::json& left, const nlohmann::json& right) {
14117e860f15SJohn Edward Broadbent                 return (left["Id"] <= right["Id"]);
14127e860f15SJohn Edward Broadbent                 });
14137e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Members@odata.count"] =
14147e860f15SJohn Edward Broadbent                 entriesArray.size();
14157e860f15SJohn Edward Broadbent             },
14167e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
14177e860f15SJohn Edward Broadbent             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
14187e860f15SJohn Edward Broadbent         });
14197e860f15SJohn Edward Broadbent }
14207e860f15SJohn Edward Broadbent 
14217e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntry(App& app)
14227e860f15SJohn Edward Broadbent {
14237e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
14247e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1425ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
1426002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1427002d39b4SEd Tanous             [&app](const crow::Request& req,
14287e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
142945ca1b86SEd Tanous                    const std::string& param) {
14303ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
14317e860f15SJohn Edward Broadbent         {
143245ca1b86SEd Tanous             return;
143345ca1b86SEd Tanous         }
14347e860f15SJohn Edward Broadbent         std::string entryID = param;
14357e860f15SJohn Edward Broadbent         dbus::utility::escapePathForDbus(entryID);
14367e860f15SJohn Edward Broadbent 
14377e860f15SJohn Edward Broadbent         // DBus implementation of EventLog/Entries
14387e860f15SJohn Edward Broadbent         // Make call to Logging Service to find all log entry objects
14397e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
1440002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1441b9d36b47SEd Tanous                                  const dbus::utility::DBusPropertiesMap& resp) {
14427e860f15SJohn Edward Broadbent             if (ec.value() == EBADR)
14437e860f15SJohn Edward Broadbent             {
1444002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogEntry",
1445002d39b4SEd Tanous                                            entryID);
14467e860f15SJohn Edward Broadbent                 return;
14477e860f15SJohn Edward Broadbent             }
14487e860f15SJohn Edward Broadbent             if (ec)
14497e860f15SJohn Edward Broadbent             {
14500fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
1451002d39b4SEd Tanous                     << "EventLogEntry (DBus) resp_handler got error " << ec;
14527e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
14537e860f15SJohn Edward Broadbent                 return;
14547e860f15SJohn Edward Broadbent             }
1455914e2d5dSEd Tanous             const uint32_t* id = nullptr;
1456c419c759SEd Tanous             const uint64_t* timestamp = nullptr;
1457c419c759SEd Tanous             const uint64_t* updateTimestamp = nullptr;
1458914e2d5dSEd Tanous             const std::string* severity = nullptr;
1459914e2d5dSEd Tanous             const std::string* message = nullptr;
1460914e2d5dSEd Tanous             const std::string* filePath = nullptr;
14617e860f15SJohn Edward Broadbent             bool resolved = false;
14627e860f15SJohn Edward Broadbent 
14639eb808c1SEd Tanous             for (const auto& propertyMap : resp)
14647e860f15SJohn Edward Broadbent             {
14657e860f15SJohn Edward Broadbent                 if (propertyMap.first == "Id")
14667e860f15SJohn Edward Broadbent                 {
14677e860f15SJohn Edward Broadbent                     id = std::get_if<uint32_t>(&propertyMap.second);
14687e860f15SJohn Edward Broadbent                 }
14697e860f15SJohn Edward Broadbent                 else if (propertyMap.first == "Timestamp")
14707e860f15SJohn Edward Broadbent                 {
1471002d39b4SEd Tanous                     timestamp = std::get_if<uint64_t>(&propertyMap.second);
1472ebd45906SGeorge Liu                 }
1473d139c236SGeorge Liu                 else if (propertyMap.first == "UpdateTimestamp")
1474d139c236SGeorge Liu                 {
1475ebd45906SGeorge Liu                     updateTimestamp =
1476c419c759SEd Tanous                         std::get_if<uint64_t>(&propertyMap.second);
1477ebd45906SGeorge Liu                 }
1478cb92c03bSAndrew Geissler                 else if (propertyMap.first == "Severity")
1479cb92c03bSAndrew Geissler                 {
1480002d39b4SEd Tanous                     severity = std::get_if<std::string>(&propertyMap.second);
1481cb92c03bSAndrew Geissler                 }
1482cb92c03bSAndrew Geissler                 else if (propertyMap.first == "Message")
1483cb92c03bSAndrew Geissler                 {
1484002d39b4SEd Tanous                     message = std::get_if<std::string>(&propertyMap.second);
1485ae34c8e8SAdriana Kobylak                 }
148675710de2SXiaochao Ma                 else if (propertyMap.first == "Resolved")
148775710de2SXiaochao Ma                 {
1488914e2d5dSEd Tanous                     const bool* resolveptr =
148975710de2SXiaochao Ma                         std::get_if<bool>(&propertyMap.second);
149075710de2SXiaochao Ma                     if (resolveptr == nullptr)
149175710de2SXiaochao Ma                     {
149275710de2SXiaochao Ma                         messages::internalError(asyncResp->res);
149375710de2SXiaochao Ma                         return;
149475710de2SXiaochao Ma                     }
149575710de2SXiaochao Ma                     resolved = *resolveptr;
149675710de2SXiaochao Ma                 }
14977e860f15SJohn Edward Broadbent                 else if (propertyMap.first == "Path")
1498f86bb901SAdriana Kobylak                 {
1499002d39b4SEd Tanous                     filePath = std::get_if<std::string>(&propertyMap.second);
1500f86bb901SAdriana Kobylak                 }
1501f86bb901SAdriana Kobylak             }
1502002d39b4SEd Tanous             if (id == nullptr || message == nullptr || severity == nullptr ||
1503002d39b4SEd Tanous                 timestamp == nullptr || updateTimestamp == nullptr)
1504f86bb901SAdriana Kobylak             {
1505ae34c8e8SAdriana Kobylak                 messages::internalError(asyncResp->res);
1506271584abSEd Tanous                 return;
1507271584abSEd Tanous             }
1508f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.type"] =
1509f86bb901SAdriana Kobylak                 "#LogEntry.v1_8_0.LogEntry";
1510f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["@odata.id"] =
15110fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1512f86bb901SAdriana Kobylak                 std::to_string(*id);
151345ca1b86SEd Tanous             asyncResp->res.jsonValue["Name"] = "System Event Log Entry";
1514f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Id"] = std::to_string(*id);
1515f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Message"] = *message;
1516f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Resolved"] = resolved;
1517f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["EntryType"] = "Event";
1518f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Severity"] =
1519f86bb901SAdriana Kobylak                 translateSeverityDbusToRedfish(*severity);
1520f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Created"] =
1521c419c759SEd Tanous                 crow::utility::getDateTimeUintMs(*timestamp);
1522f86bb901SAdriana Kobylak             asyncResp->res.jsonValue["Modified"] =
1523c419c759SEd Tanous                 crow::utility::getDateTimeUintMs(*updateTimestamp);
1524f86bb901SAdriana Kobylak             if (filePath != nullptr)
1525f86bb901SAdriana Kobylak             {
1526f86bb901SAdriana Kobylak                 asyncResp->res.jsonValue["AdditionalDataURI"] =
1527e7dbd530SPotin Lai                     "/redfish/v1/Systems/system/LogServices/EventLog/Entries/" +
1528e7dbd530SPotin Lai                     std::to_string(*id) + "/attachment";
1529f86bb901SAdriana Kobylak             }
1530cb92c03bSAndrew Geissler             },
1531cb92c03bSAndrew Geissler             "xyz.openbmc_project.Logging",
1532cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging/entry/" + entryID,
1533f86bb901SAdriana Kobylak             "org.freedesktop.DBus.Properties", "GetAll", "");
15347e860f15SJohn Edward Broadbent         });
1535336e96c6SChicago Duan 
15367e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15377e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1538ed398213SEd Tanous         .privileges(redfish::privileges::patchLogEntry)
15397e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::patch)(
154045ca1b86SEd Tanous             [&app](const crow::Request& req,
15417e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15427e860f15SJohn Edward Broadbent                    const std::string& entryId) {
15433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
154445ca1b86SEd Tanous         {
154545ca1b86SEd Tanous             return;
154645ca1b86SEd Tanous         }
154775710de2SXiaochao Ma         std::optional<bool> resolved;
154875710de2SXiaochao Ma 
154915ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "Resolved",
15507e860f15SJohn Edward Broadbent                                       resolved))
155175710de2SXiaochao Ma         {
155275710de2SXiaochao Ma             return;
155375710de2SXiaochao Ma         }
155475710de2SXiaochao Ma         BMCWEB_LOG_DEBUG << "Set Resolved";
155575710de2SXiaochao Ma 
155675710de2SXiaochao Ma         crow::connections::systemBus->async_method_call(
15574f48d5f6SEd Tanous             [asyncResp, entryId](const boost::system::error_code ec) {
155875710de2SXiaochao Ma             if (ec)
155975710de2SXiaochao Ma             {
156075710de2SXiaochao Ma                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
156175710de2SXiaochao Ma                 messages::internalError(asyncResp->res);
156275710de2SXiaochao Ma                 return;
156375710de2SXiaochao Ma             }
156475710de2SXiaochao Ma             },
156575710de2SXiaochao Ma             "xyz.openbmc_project.Logging",
156675710de2SXiaochao Ma             "/xyz/openbmc_project/logging/entry/" + entryId,
156775710de2SXiaochao Ma             "org.freedesktop.DBus.Properties", "Set",
156875710de2SXiaochao Ma             "xyz.openbmc_project.Logging.Entry", "Resolved",
1569168e20c1SEd Tanous             dbus::utility::DbusVariantType(*resolved));
15707e860f15SJohn Edward Broadbent         });
157175710de2SXiaochao Ma 
15727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
15737e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/")
1574ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
1575ed398213SEd Tanous 
1576002d39b4SEd Tanous         .methods(boost::beast::http::verb::delete_)(
1577002d39b4SEd Tanous             [&app](const crow::Request& req,
1578002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
157945ca1b86SEd Tanous                    const std::string& param) {
15803ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
1581336e96c6SChicago Duan         {
158245ca1b86SEd Tanous             return;
158345ca1b86SEd Tanous         }
1584336e96c6SChicago Duan         BMCWEB_LOG_DEBUG << "Do delete single event entries.";
1585336e96c6SChicago Duan 
15867e860f15SJohn Edward Broadbent         std::string entryID = param;
1587336e96c6SChicago Duan 
1588336e96c6SChicago Duan         dbus::utility::escapePathForDbus(entryID);
1589336e96c6SChicago Duan 
1590336e96c6SChicago Duan         // Process response from Logging service.
1591002d39b4SEd Tanous         auto respHandler =
1592002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec) {
1593002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "EventLogEntry (DBus) doDelete callback: Done";
1594336e96c6SChicago Duan             if (ec)
1595336e96c6SChicago Duan             {
15963de8d8baSGeorge Liu                 if (ec.value() == EBADR)
15973de8d8baSGeorge Liu                 {
159845ca1b86SEd Tanous                     messages::resourceNotFound(asyncResp->res, "LogEntry",
159945ca1b86SEd Tanous                                                entryID);
16003de8d8baSGeorge Liu                     return;
16013de8d8baSGeorge Liu                 }
1602336e96c6SChicago Duan                 // TODO Handle for specific error code
16030fda0f12SGeorge Liu                 BMCWEB_LOG_ERROR
16040fda0f12SGeorge Liu                     << "EventLogEntry (DBus) doDelete respHandler got error "
1605336e96c6SChicago Duan                     << ec;
1606336e96c6SChicago Duan                 asyncResp->res.result(
1607336e96c6SChicago Duan                     boost::beast::http::status::internal_server_error);
1608336e96c6SChicago Duan                 return;
1609336e96c6SChicago Duan             }
1610336e96c6SChicago Duan 
1611336e96c6SChicago Duan             asyncResp->res.result(boost::beast::http::status::ok);
1612336e96c6SChicago Duan         };
1613336e96c6SChicago Duan 
1614336e96c6SChicago Duan         // Make call to Logging service to request Delete Log
1615336e96c6SChicago Duan         crow::connections::systemBus->async_method_call(
1616336e96c6SChicago Duan             respHandler, "xyz.openbmc_project.Logging",
1617336e96c6SChicago Duan             "/xyz/openbmc_project/logging/entry/" + entryID,
1618336e96c6SChicago Duan             "xyz.openbmc_project.Object.Delete", "Delete");
16197e860f15SJohn Edward Broadbent         });
1620400fd1fbSAdriana Kobylak }
1621400fd1fbSAdriana Kobylak 
16227e860f15SJohn Edward Broadbent inline void requestRoutesDBusEventLogEntryDownload(App& app)
1623400fd1fbSAdriana Kobylak {
16240fda0f12SGeorge Liu     BMCWEB_ROUTE(
16250fda0f12SGeorge Liu         app,
16260fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Entries/<str>/attachment")
1627ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
16287e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
162945ca1b86SEd Tanous             [&app](const crow::Request& req,
16307e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
163145ca1b86SEd Tanous                    const std::string& param) {
16323ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
16337e860f15SJohn Edward Broadbent         {
163445ca1b86SEd Tanous             return;
163545ca1b86SEd Tanous         }
1636002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
1637400fd1fbSAdriana Kobylak         {
1638002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
1639400fd1fbSAdriana Kobylak             return;
1640400fd1fbSAdriana Kobylak         }
1641400fd1fbSAdriana Kobylak 
16427e860f15SJohn Edward Broadbent         std::string entryID = param;
1643400fd1fbSAdriana Kobylak         dbus::utility::escapePathForDbus(entryID);
1644400fd1fbSAdriana Kobylak 
1645400fd1fbSAdriana Kobylak         crow::connections::systemBus->async_method_call(
1646002d39b4SEd Tanous             [asyncResp, entryID](const boost::system::error_code ec,
1647400fd1fbSAdriana Kobylak                                  const sdbusplus::message::unix_fd& unixfd) {
1648400fd1fbSAdriana Kobylak             if (ec.value() == EBADR)
1649400fd1fbSAdriana Kobylak             {
1650002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EventLogAttachment",
1651002d39b4SEd Tanous                                            entryID);
1652400fd1fbSAdriana Kobylak                 return;
1653400fd1fbSAdriana Kobylak             }
1654400fd1fbSAdriana Kobylak             if (ec)
1655400fd1fbSAdriana Kobylak             {
1656400fd1fbSAdriana Kobylak                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
1657400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1658400fd1fbSAdriana Kobylak                 return;
1659400fd1fbSAdriana Kobylak             }
1660400fd1fbSAdriana Kobylak 
1661400fd1fbSAdriana Kobylak             int fd = -1;
1662400fd1fbSAdriana Kobylak             fd = dup(unixfd);
1663400fd1fbSAdriana Kobylak             if (fd == -1)
1664400fd1fbSAdriana Kobylak             {
1665400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1666400fd1fbSAdriana Kobylak                 return;
1667400fd1fbSAdriana Kobylak             }
1668400fd1fbSAdriana Kobylak 
1669400fd1fbSAdriana Kobylak             long long int size = lseek(fd, 0, SEEK_END);
1670400fd1fbSAdriana Kobylak             if (size == -1)
1671400fd1fbSAdriana Kobylak             {
1672400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1673400fd1fbSAdriana Kobylak                 return;
1674400fd1fbSAdriana Kobylak             }
1675400fd1fbSAdriana Kobylak 
1676400fd1fbSAdriana Kobylak             // Arbitrary max size of 64kb
1677400fd1fbSAdriana Kobylak             constexpr int maxFileSize = 65536;
1678400fd1fbSAdriana Kobylak             if (size > maxFileSize)
1679400fd1fbSAdriana Kobylak             {
1680002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "File size exceeds maximum allowed size of "
1681400fd1fbSAdriana Kobylak                                  << maxFileSize;
1682400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1683400fd1fbSAdriana Kobylak                 return;
1684400fd1fbSAdriana Kobylak             }
1685400fd1fbSAdriana Kobylak             std::vector<char> data(static_cast<size_t>(size));
1686400fd1fbSAdriana Kobylak             long long int rc = lseek(fd, 0, SEEK_SET);
1687400fd1fbSAdriana Kobylak             if (rc == -1)
1688400fd1fbSAdriana Kobylak             {
1689400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1690400fd1fbSAdriana Kobylak                 return;
1691400fd1fbSAdriana Kobylak             }
1692400fd1fbSAdriana Kobylak             rc = read(fd, data.data(), data.size());
1693400fd1fbSAdriana Kobylak             if ((rc == -1) || (rc != size))
1694400fd1fbSAdriana Kobylak             {
1695400fd1fbSAdriana Kobylak                 messages::internalError(asyncResp->res);
1696400fd1fbSAdriana Kobylak                 return;
1697400fd1fbSAdriana Kobylak             }
1698400fd1fbSAdriana Kobylak             close(fd);
1699400fd1fbSAdriana Kobylak 
1700400fd1fbSAdriana Kobylak             std::string_view strData(data.data(), data.size());
1701002d39b4SEd Tanous             std::string output = crow::utility::base64encode(strData);
1702400fd1fbSAdriana Kobylak 
1703400fd1fbSAdriana Kobylak             asyncResp->res.addHeader("Content-Type",
1704400fd1fbSAdriana Kobylak                                      "application/octet-stream");
1705002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
1706400fd1fbSAdriana Kobylak             asyncResp->res.body() = std::move(output);
1707400fd1fbSAdriana Kobylak             },
1708400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging",
1709400fd1fbSAdriana Kobylak             "/xyz/openbmc_project/logging/entry/" + entryID,
1710400fd1fbSAdriana Kobylak             "xyz.openbmc_project.Logging.Entry", "GetEntry");
17117e860f15SJohn Edward Broadbent         });
17121da66f75SEd Tanous }
17131da66f75SEd Tanous 
1714b7028ebfSSpencer Ku constexpr const char* hostLoggerFolderPath = "/var/log/console";
1715b7028ebfSSpencer Ku 
1716b7028ebfSSpencer Ku inline bool
1717b7028ebfSSpencer Ku     getHostLoggerFiles(const std::string& hostLoggerFilePath,
1718b7028ebfSSpencer Ku                        std::vector<std::filesystem::path>& hostLoggerFiles)
1719b7028ebfSSpencer Ku {
1720b7028ebfSSpencer Ku     std::error_code ec;
1721b7028ebfSSpencer Ku     std::filesystem::directory_iterator logPath(hostLoggerFilePath, ec);
1722b7028ebfSSpencer Ku     if (ec)
1723b7028ebfSSpencer Ku     {
1724b7028ebfSSpencer Ku         BMCWEB_LOG_ERROR << ec.message();
1725b7028ebfSSpencer Ku         return false;
1726b7028ebfSSpencer Ku     }
1727b7028ebfSSpencer Ku     for (const std::filesystem::directory_entry& it : logPath)
1728b7028ebfSSpencer Ku     {
1729b7028ebfSSpencer Ku         std::string filename = it.path().filename();
1730b7028ebfSSpencer Ku         // Prefix of each log files is "log". Find the file and save the
1731b7028ebfSSpencer Ku         // path
173211ba3979SEd Tanous         if (filename.starts_with("log"))
1733b7028ebfSSpencer Ku         {
1734b7028ebfSSpencer Ku             hostLoggerFiles.emplace_back(it.path());
1735b7028ebfSSpencer Ku         }
1736b7028ebfSSpencer Ku     }
1737b7028ebfSSpencer Ku     // As the log files rotate, they are appended with a ".#" that is higher for
1738b7028ebfSSpencer Ku     // the older logs. Since we start from oldest logs, sort the name in
1739b7028ebfSSpencer Ku     // descending order.
1740b7028ebfSSpencer Ku     std::sort(hostLoggerFiles.rbegin(), hostLoggerFiles.rend(),
1741b7028ebfSSpencer Ku               AlphanumLess<std::string>());
1742b7028ebfSSpencer Ku 
1743b7028ebfSSpencer Ku     return true;
1744b7028ebfSSpencer Ku }
1745b7028ebfSSpencer Ku 
174602cad96eSEd Tanous inline bool getHostLoggerEntries(
174702cad96eSEd Tanous     const std::vector<std::filesystem::path>& hostLoggerFiles, uint64_t skip,
174802cad96eSEd Tanous     uint64_t top, std::vector<std::string>& logEntries, size_t& logCount)
1749b7028ebfSSpencer Ku {
1750b7028ebfSSpencer Ku     GzFileReader logFile;
1751b7028ebfSSpencer Ku 
1752b7028ebfSSpencer Ku     // Go though all log files and expose host logs.
1753b7028ebfSSpencer Ku     for (const std::filesystem::path& it : hostLoggerFiles)
1754b7028ebfSSpencer Ku     {
1755b7028ebfSSpencer Ku         if (!logFile.gzGetLines(it.string(), skip, top, logEntries, logCount))
1756b7028ebfSSpencer Ku         {
1757b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to expose host logs";
1758b7028ebfSSpencer Ku             return false;
1759b7028ebfSSpencer Ku         }
1760b7028ebfSSpencer Ku     }
1761b7028ebfSSpencer Ku     // Get lastMessage from constructor by getter
1762b7028ebfSSpencer Ku     std::string lastMessage = logFile.getLastMessage();
1763b7028ebfSSpencer Ku     if (!lastMessage.empty())
1764b7028ebfSSpencer Ku     {
1765b7028ebfSSpencer Ku         logCount++;
1766b7028ebfSSpencer Ku         if (logCount > skip && logCount <= (skip + top))
1767b7028ebfSSpencer Ku         {
1768b7028ebfSSpencer Ku             logEntries.push_back(lastMessage);
1769b7028ebfSSpencer Ku         }
1770b7028ebfSSpencer Ku     }
1771b7028ebfSSpencer Ku     return true;
1772b7028ebfSSpencer Ku }
1773b7028ebfSSpencer Ku 
1774b7028ebfSSpencer Ku inline void fillHostLoggerEntryJson(const std::string& logEntryID,
1775b7028ebfSSpencer Ku                                     const std::string& msg,
17766d6574c9SJason M. Bills                                     nlohmann::json::object_t& logEntryJson)
1777b7028ebfSSpencer Ku {
1778b7028ebfSSpencer Ku     // Fill in the log entry with the gathered data.
17796d6574c9SJason M. Bills     logEntryJson["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
17806d6574c9SJason M. Bills     logEntryJson["@odata.id"] =
1781b7028ebfSSpencer Ku         "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/" +
17826d6574c9SJason M. Bills         logEntryID;
17836d6574c9SJason M. Bills     logEntryJson["Name"] = "Host Logger Entry";
17846d6574c9SJason M. Bills     logEntryJson["Id"] = logEntryID;
17856d6574c9SJason M. Bills     logEntryJson["Message"] = msg;
17866d6574c9SJason M. Bills     logEntryJson["EntryType"] = "Oem";
17876d6574c9SJason M. Bills     logEntryJson["Severity"] = "OK";
17886d6574c9SJason M. Bills     logEntryJson["OemRecordFormat"] = "Host Logger Entry";
1789b7028ebfSSpencer Ku }
1790b7028ebfSSpencer Ku 
1791b7028ebfSSpencer Ku inline void requestRoutesSystemHostLogger(App& app)
1792b7028ebfSSpencer Ku {
1793b7028ebfSSpencer Ku     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/HostLogger/")
1794b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogService)
17951476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
17961476687dSEd Tanous             [&app](const crow::Request& req,
17971476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17983ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179945ca1b86SEd Tanous         {
180045ca1b86SEd Tanous             return;
180145ca1b86SEd Tanous         }
1802b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1803b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger";
1804b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1805b7028ebfSSpencer Ku             "#LogService.v1_1_0.LogService";
1806b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "Host Logger Service";
1807b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] = "Host Logger Service";
1808b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Id"] = "HostLogger";
18091476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
18101476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1811b7028ebfSSpencer Ku         });
1812b7028ebfSSpencer Ku }
1813b7028ebfSSpencer Ku 
1814b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerCollection(App& app)
1815b7028ebfSSpencer Ku {
1816b7028ebfSSpencer Ku     BMCWEB_ROUTE(app,
1817b7028ebfSSpencer Ku                  "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/")
1818b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1819002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1820002d39b4SEd Tanous             [&app](const crow::Request& req,
1821002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1822c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
1823c937d2bfSEd Tanous             .canDelegateTop = true,
1824c937d2bfSEd Tanous             .canDelegateSkip = true,
1825c937d2bfSEd Tanous         };
1826c937d2bfSEd Tanous         query_param::Query delegatedQuery;
1827c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
18283ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
1829b7028ebfSSpencer Ku         {
1830b7028ebfSSpencer Ku             return;
1831b7028ebfSSpencer Ku         }
1832b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.id"] =
1833b7028ebfSSpencer Ku             "/redfish/v1/Systems/system/LogServices/HostLogger/Entries";
1834b7028ebfSSpencer Ku         asyncResp->res.jsonValue["@odata.type"] =
1835b7028ebfSSpencer Ku             "#LogEntryCollection.LogEntryCollection";
1836b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Name"] = "HostLogger Entries";
1837b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Description"] =
1838b7028ebfSSpencer Ku             "Collection of HostLogger Entries";
18390fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
1840b7028ebfSSpencer Ku         logEntryArray = nlohmann::json::array();
1841b7028ebfSSpencer Ku         asyncResp->res.jsonValue["Members@odata.count"] = 0;
1842b7028ebfSSpencer Ku 
1843b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1844b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1845b7028ebfSSpencer Ku         {
1846b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1847b7028ebfSSpencer Ku             return;
1848b7028ebfSSpencer Ku         }
18493648c8beSEd Tanous         // If we weren't provided top and skip limits, use the defaults.
18503648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
18513648c8beSEd Tanous         size_t top =
18523648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
1853b7028ebfSSpencer Ku         size_t logCount = 0;
1854b7028ebfSSpencer Ku         // This vector only store the entries we want to expose that
1855b7028ebfSSpencer Ku         // control by skip and top.
1856b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
18573648c8beSEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, skip, top, logEntries,
18583648c8beSEd Tanous                                   logCount))
1859b7028ebfSSpencer Ku         {
1860b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1861b7028ebfSSpencer Ku             return;
1862b7028ebfSSpencer Ku         }
1863b7028ebfSSpencer Ku         // If vector is empty, that means skip value larger than total
1864b7028ebfSSpencer Ku         // log count
186526f6976fSEd Tanous         if (logEntries.empty())
1866b7028ebfSSpencer Ku         {
1867b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
1868b7028ebfSSpencer Ku             return;
1869b7028ebfSSpencer Ku         }
187026f6976fSEd Tanous         if (!logEntries.empty())
1871b7028ebfSSpencer Ku         {
1872b7028ebfSSpencer Ku             for (size_t i = 0; i < logEntries.size(); i++)
1873b7028ebfSSpencer Ku             {
18746d6574c9SJason M. Bills                 nlohmann::json::object_t hostLogEntry;
18753648c8beSEd Tanous                 fillHostLoggerEntryJson(std::to_string(skip + i), logEntries[i],
18763648c8beSEd Tanous                                         hostLogEntry);
18776d6574c9SJason M. Bills                 logEntryArray.push_back(std::move(hostLogEntry));
1878b7028ebfSSpencer Ku             }
1879b7028ebfSSpencer Ku 
1880b7028ebfSSpencer Ku             asyncResp->res.jsonValue["Members@odata.count"] = logCount;
18813648c8beSEd Tanous             if (skip + top < logCount)
1882b7028ebfSSpencer Ku             {
1883b7028ebfSSpencer Ku                 asyncResp->res.jsonValue["Members@odata.nextLink"] =
18840fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/LogServices/HostLogger/Entries?$skip=" +
18853648c8beSEd Tanous                     std::to_string(skip + top);
1886b7028ebfSSpencer Ku             }
1887b7028ebfSSpencer Ku         }
1888b7028ebfSSpencer Ku         });
1889b7028ebfSSpencer Ku }
1890b7028ebfSSpencer Ku 
1891b7028ebfSSpencer Ku inline void requestRoutesSystemHostLoggerLogEntry(App& app)
1892b7028ebfSSpencer Ku {
1893b7028ebfSSpencer Ku     BMCWEB_ROUTE(
1894b7028ebfSSpencer Ku         app, "/redfish/v1/Systems/system/LogServices/HostLogger/Entries/<str>/")
1895b7028ebfSSpencer Ku         .privileges(redfish::privileges::getLogEntry)
1896b7028ebfSSpencer Ku         .methods(boost::beast::http::verb::get)(
189745ca1b86SEd Tanous             [&app](const crow::Request& req,
1898b7028ebfSSpencer Ku                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1899b7028ebfSSpencer Ku                    const std::string& param) {
19003ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
190145ca1b86SEd Tanous         {
190245ca1b86SEd Tanous             return;
190345ca1b86SEd Tanous         }
1904b7028ebfSSpencer Ku         const std::string& targetID = param;
1905b7028ebfSSpencer Ku 
1906b7028ebfSSpencer Ku         uint64_t idInt = 0;
1907ca45aa3cSEd Tanous 
1908ca45aa3cSEd Tanous         // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1909ca45aa3cSEd Tanous         const char* end = targetID.data() + targetID.size();
1910ca45aa3cSEd Tanous 
1911ca45aa3cSEd Tanous         auto [ptr, ec] = std::from_chars(targetID.data(), end, idInt);
1912b7028ebfSSpencer Ku         if (ec == std::errc::invalid_argument)
1913b7028ebfSSpencer Ku         {
1914ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1915b7028ebfSSpencer Ku             return;
1916b7028ebfSSpencer Ku         }
1917b7028ebfSSpencer Ku         if (ec == std::errc::result_out_of_range)
1918b7028ebfSSpencer Ku         {
1919ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1920b7028ebfSSpencer Ku             return;
1921b7028ebfSSpencer Ku         }
1922b7028ebfSSpencer Ku 
1923b7028ebfSSpencer Ku         std::vector<std::filesystem::path> hostLoggerFiles;
1924b7028ebfSSpencer Ku         if (!getHostLoggerFiles(hostLoggerFolderPath, hostLoggerFiles))
1925b7028ebfSSpencer Ku         {
1926b7028ebfSSpencer Ku             BMCWEB_LOG_ERROR << "fail to get host log file path";
1927b7028ebfSSpencer Ku             return;
1928b7028ebfSSpencer Ku         }
1929b7028ebfSSpencer Ku 
1930b7028ebfSSpencer Ku         size_t logCount = 0;
19313648c8beSEd Tanous         size_t top = 1;
1932b7028ebfSSpencer Ku         std::vector<std::string> logEntries;
1933b7028ebfSSpencer Ku         // We can get specific entry by skip and top. For example, if we
1934b7028ebfSSpencer Ku         // want to get nth entry, we can set skip = n-1 and top = 1 to
1935b7028ebfSSpencer Ku         // get that entry
1936002d39b4SEd Tanous         if (!getHostLoggerEntries(hostLoggerFiles, idInt, top, logEntries,
1937002d39b4SEd Tanous                                   logCount))
1938b7028ebfSSpencer Ku         {
1939b7028ebfSSpencer Ku             messages::internalError(asyncResp->res);
1940b7028ebfSSpencer Ku             return;
1941b7028ebfSSpencer Ku         }
1942b7028ebfSSpencer Ku 
1943b7028ebfSSpencer Ku         if (!logEntries.empty())
1944b7028ebfSSpencer Ku         {
19456d6574c9SJason M. Bills             nlohmann::json::object_t hostLogEntry;
19466d6574c9SJason M. Bills             fillHostLoggerEntryJson(targetID, logEntries[0], hostLogEntry);
19476d6574c9SJason M. Bills             asyncResp->res.jsonValue.update(hostLogEntry);
1948b7028ebfSSpencer Ku             return;
1949b7028ebfSSpencer Ku         }
1950b7028ebfSSpencer Ku 
1951b7028ebfSSpencer Ku         // Requested ID was not found
1952ace85d60SEd Tanous         messages::resourceMissingAtURI(asyncResp->res, req.urlView);
1953b7028ebfSSpencer Ku         });
1954b7028ebfSSpencer Ku }
1955b7028ebfSSpencer Ku 
1956fdd26906SClaire Weinan constexpr char const* dumpManagerIface =
1957fdd26906SClaire Weinan     "xyz.openbmc_project.Collection.DeleteAll";
1958fdd26906SClaire Weinan inline void handleLogServicesCollectionGet(
1959fdd26906SClaire Weinan     crow::App& app, const crow::Request& req,
1960fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
19611da66f75SEd Tanous {
19623ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
196345ca1b86SEd Tanous     {
196445ca1b86SEd Tanous         return;
196545ca1b86SEd Tanous     }
19667e860f15SJohn Edward Broadbent     // Collections don't include the static data added by SubRoute
19677e860f15SJohn Edward Broadbent     // because it has a duplicate entry for members
1968e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.type"] =
19691da66f75SEd Tanous         "#LogServiceCollection.LogServiceCollection";
1970e1f26343SJason M. Bills     asyncResp->res.jsonValue["@odata.id"] =
1971e1f26343SJason M. Bills         "/redfish/v1/Managers/bmc/LogServices";
1972002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "Open BMC Log Services Collection";
1973e1f26343SJason M. Bills     asyncResp->res.jsonValue["Description"] =
19741da66f75SEd Tanous         "Collection of LogServices for this Manager";
1975002d39b4SEd Tanous     nlohmann::json& logServiceArray = asyncResp->res.jsonValue["Members"];
1976c4bf6374SJason M. Bills     logServiceArray = nlohmann::json::array();
1977fdd26906SClaire Weinan 
1978c4bf6374SJason M. Bills #ifdef BMCWEB_ENABLE_REDFISH_BMC_JOURNAL
1979c4bf6374SJason M. Bills     logServiceArray.push_back(
1980002d39b4SEd Tanous         {{"@odata.id", "/redfish/v1/Managers/bmc/LogServices/Journal"}});
1981c4bf6374SJason M. Bills #endif
1982fdd26906SClaire Weinan 
1983fdd26906SClaire Weinan     asyncResp->res.jsonValue["Members@odata.count"] = logServiceArray.size();
1984fdd26906SClaire Weinan 
1985fdd26906SClaire Weinan #ifdef BMCWEB_ENABLE_REDFISH_DUMP_LOG
1986fdd26906SClaire Weinan     auto respHandler =
1987fdd26906SClaire Weinan         [asyncResp](
1988fdd26906SClaire Weinan             const boost::system::error_code ec,
1989fdd26906SClaire Weinan             const dbus::utility::MapperGetSubTreePathsResponse& subTreePaths) {
1990fdd26906SClaire Weinan         if (ec)
1991fdd26906SClaire Weinan         {
1992fdd26906SClaire Weinan             BMCWEB_LOG_ERROR
1993fdd26906SClaire Weinan                 << "handleLogServicesCollectionGet respHandler got error "
1994fdd26906SClaire Weinan                 << ec;
1995fdd26906SClaire Weinan             // Assume that getting an error simply means there are no dump
1996fdd26906SClaire Weinan             // LogServices. Return without adding any error response.
1997fdd26906SClaire Weinan             return;
1998fdd26906SClaire Weinan         }
1999fdd26906SClaire Weinan 
2000fdd26906SClaire Weinan         nlohmann::json& logServiceArrayLocal =
2001fdd26906SClaire Weinan             asyncResp->res.jsonValue["Members"];
2002fdd26906SClaire Weinan 
2003fdd26906SClaire Weinan         for (const std::string& path : subTreePaths)
2004fdd26906SClaire Weinan         {
2005fdd26906SClaire Weinan             if (path == "/xyz/openbmc_project/dump/bmc")
2006fdd26906SClaire Weinan             {
2007fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2008fdd26906SClaire Weinan                     {{"@odata.id",
2009fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/Dump"}});
2010fdd26906SClaire Weinan             }
2011fdd26906SClaire Weinan             else if (path == "/xyz/openbmc_project/dump/faultlog")
2012fdd26906SClaire Weinan             {
2013fdd26906SClaire Weinan                 logServiceArrayLocal.push_back(
2014fdd26906SClaire Weinan                     {{"@odata.id",
2015fdd26906SClaire Weinan                       "/redfish/v1/Managers/bmc/LogServices/FaultLog"}});
2016fdd26906SClaire Weinan             }
2017fdd26906SClaire Weinan         }
2018fdd26906SClaire Weinan 
2019e1f26343SJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] =
2020fdd26906SClaire Weinan             logServiceArrayLocal.size();
2021fdd26906SClaire Weinan     };
2022fdd26906SClaire Weinan 
2023fdd26906SClaire Weinan     crow::connections::systemBus->async_method_call(
2024fdd26906SClaire Weinan         respHandler, "xyz.openbmc_project.ObjectMapper",
2025fdd26906SClaire Weinan         "/xyz/openbmc_project/object_mapper",
2026fdd26906SClaire Weinan         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
2027fdd26906SClaire Weinan         "/xyz/openbmc_project/dump", 0,
2028fdd26906SClaire Weinan         std::array<const char*, 1>{dumpManagerIface});
2029fdd26906SClaire Weinan #endif
2030fdd26906SClaire Weinan }
2031fdd26906SClaire Weinan 
2032fdd26906SClaire Weinan inline void requestRoutesBMCLogServiceCollection(App& app)
2033fdd26906SClaire Weinan {
2034fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/")
2035fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogServiceCollection)
2036fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2037fdd26906SClaire Weinan             std::bind_front(handleLogServicesCollectionGet, std::ref(app)));
2038e1f26343SJason M. Bills }
2039e1f26343SJason M. Bills 
20407e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogService(App& app)
2041e1f26343SJason M. Bills {
20427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/")
2043ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
20447e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
204545ca1b86SEd Tanous             [&app](const crow::Request& req,
204645ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
20473ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
20487e860f15SJohn Edward Broadbent         {
204945ca1b86SEd Tanous             return;
205045ca1b86SEd Tanous         }
2051e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2052e1f26343SJason M. Bills             "#LogService.v1_1_0.LogService";
20530f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
20540f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal";
2055002d39b4SEd Tanous         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Log Service";
2056002d39b4SEd Tanous         asyncResp->res.jsonValue["Description"] = "BMC Journal Log Service";
2057c4bf6374SJason M. Bills         asyncResp->res.jsonValue["Id"] = "BMC Journal";
2058e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
20597c8c4058STejas Patil 
20607c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
20617c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
2062002d39b4SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
20637c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
20647c8c4058STejas Patil             redfishDateTimeOffset.second;
20657c8c4058STejas Patil 
20661476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
20671476687dSEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
20687e860f15SJohn Edward Broadbent         });
2069e1f26343SJason M. Bills }
2070e1f26343SJason M. Bills 
20713a48b3a2SJason M. Bills static int
20723a48b3a2SJason M. Bills     fillBMCJournalLogEntryJson(const std::string& bmcJournalLogEntryID,
2073e1f26343SJason M. Bills                                sd_journal* journal,
20743a48b3a2SJason M. Bills                                nlohmann::json::object_t& bmcJournalLogEntryJson)
2075e1f26343SJason M. Bills {
2076e1f26343SJason M. Bills     // Get the Log Entry contents
2077e1f26343SJason M. Bills     int ret = 0;
2078e1f26343SJason M. Bills 
2079a8fe54f0SJason M. Bills     std::string message;
2080a8fe54f0SJason M. Bills     std::string_view syslogID;
2081a8fe54f0SJason M. Bills     ret = getJournalMetadata(journal, "SYSLOG_IDENTIFIER", syslogID);
2082a8fe54f0SJason M. Bills     if (ret < 0)
2083a8fe54f0SJason M. Bills     {
2084a8fe54f0SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read SYSLOG_IDENTIFIER field: "
2085a8fe54f0SJason M. Bills                          << strerror(-ret);
2086a8fe54f0SJason M. Bills     }
2087a8fe54f0SJason M. Bills     if (!syslogID.empty())
2088a8fe54f0SJason M. Bills     {
2089a8fe54f0SJason M. Bills         message += std::string(syslogID) + ": ";
2090a8fe54f0SJason M. Bills     }
2091a8fe54f0SJason M. Bills 
209239e77504SEd Tanous     std::string_view msg;
209316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "MESSAGE", msg);
2094e1f26343SJason M. Bills     if (ret < 0)
2095e1f26343SJason M. Bills     {
2096e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read MESSAGE field: " << strerror(-ret);
2097e1f26343SJason M. Bills         return 1;
2098e1f26343SJason M. Bills     }
2099a8fe54f0SJason M. Bills     message += std::string(msg);
2100e1f26343SJason M. Bills 
2101e1f26343SJason M. Bills     // Get the severity from the PRIORITY field
2102271584abSEd Tanous     long int severity = 8; // Default to an invalid priority
210316428a1aSJason M. Bills     ret = getJournalMetadata(journal, "PRIORITY", 10, severity);
2104e1f26343SJason M. Bills     if (ret < 0)
2105e1f26343SJason M. Bills     {
2106e1f26343SJason M. Bills         BMCWEB_LOG_ERROR << "Failed to read PRIORITY field: " << strerror(-ret);
2107e1f26343SJason M. Bills     }
2108e1f26343SJason M. Bills 
2109e1f26343SJason M. Bills     // Get the Created time from the timestamp
211016428a1aSJason M. Bills     std::string entryTimeStr;
211116428a1aSJason M. Bills     if (!getEntryTimestamp(journal, entryTimeStr))
2112e1f26343SJason M. Bills     {
211316428a1aSJason M. Bills         return 1;
2114e1f26343SJason M. Bills     }
2115e1f26343SJason M. Bills 
2116e1f26343SJason M. Bills     // Fill in the log entry with the gathered data
211784afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
211884afc48bSJason M. Bills     bmcJournalLogEntryJson["@odata.id"] =
211984afc48bSJason M. Bills         "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/" +
212084afc48bSJason M. Bills         bmcJournalLogEntryID;
212184afc48bSJason M. Bills     bmcJournalLogEntryJson["Name"] = "BMC Journal Entry";
212284afc48bSJason M. Bills     bmcJournalLogEntryJson["Id"] = bmcJournalLogEntryID;
212384afc48bSJason M. Bills     bmcJournalLogEntryJson["Message"] = std::move(message);
212484afc48bSJason M. Bills     bmcJournalLogEntryJson["EntryType"] = "Oem";
212584afc48bSJason M. Bills     bmcJournalLogEntryJson["Severity"] = severity <= 2   ? "Critical"
2126738c1e61SPatrick Williams                                          : severity <= 4 ? "Warning"
212784afc48bSJason M. Bills                                                          : "OK";
212884afc48bSJason M. Bills     bmcJournalLogEntryJson["OemRecordFormat"] = "BMC Journal Entry";
212984afc48bSJason M. Bills     bmcJournalLogEntryJson["Created"] = std::move(entryTimeStr);
2130e1f26343SJason M. Bills     return 0;
2131e1f26343SJason M. Bills }
2132e1f26343SJason M. Bills 
21337e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntryCollection(App& app)
2134e1f26343SJason M. Bills {
21357e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/")
2136ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
2137002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2138002d39b4SEd Tanous             [&app](const crow::Request& req,
2139002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2140c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
2141c937d2bfSEd Tanous             .canDelegateTop = true,
2142c937d2bfSEd Tanous             .canDelegateSkip = true,
2143c937d2bfSEd Tanous         };
2144c937d2bfSEd Tanous         query_param::Query delegatedQuery;
2145c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
21463ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
2147193ad2faSJason M. Bills         {
2148193ad2faSJason M. Bills             return;
2149193ad2faSJason M. Bills         }
21503648c8beSEd Tanous 
21513648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
21523648c8beSEd Tanous         size_t top =
21533648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
21543648c8beSEd Tanous 
21557e860f15SJohn Edward Broadbent         // Collections don't include the static data added by SubRoute
21567e860f15SJohn Edward Broadbent         // because it has a duplicate entry for members
2157e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
2158e1f26343SJason M. Bills             "#LogEntryCollection.LogEntryCollection";
21590f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
21600f74e643SEd Tanous             "/redfish/v1/Managers/bmc/LogServices/Journal/Entries";
2161e1f26343SJason M. Bills         asyncResp->res.jsonValue["Name"] = "Open BMC Journal Entries";
2162e1f26343SJason M. Bills         asyncResp->res.jsonValue["Description"] =
2163e1f26343SJason M. Bills             "Collection of BMC Journal Entries";
21640fda0f12SGeorge Liu         nlohmann::json& logEntryArray = asyncResp->res.jsonValue["Members"];
2165e1f26343SJason M. Bills         logEntryArray = nlohmann::json::array();
2166e1f26343SJason M. Bills 
21677e860f15SJohn Edward Broadbent         // Go through the journal and use the timestamp to create a
21687e860f15SJohn Edward Broadbent         // unique ID for each entry
2169e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2170e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2171e1f26343SJason M. Bills         if (ret < 0)
2172e1f26343SJason M. Bills         {
2173002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2174f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2175e1f26343SJason M. Bills             return;
2176e1f26343SJason M. Bills         }
21770fda0f12SGeorge Liu         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
21780fda0f12SGeorge Liu             journalTmp, sd_journal_close);
2179e1f26343SJason M. Bills         journalTmp = nullptr;
2180b01bf299SEd Tanous         uint64_t entryCount = 0;
2181e85d6b16SJason M. Bills         // Reset the unique ID on the first entry
2182e85d6b16SJason M. Bills         bool firstEntry = true;
2183e1f26343SJason M. Bills         SD_JOURNAL_FOREACH(journal.get())
2184e1f26343SJason M. Bills         {
2185193ad2faSJason M. Bills             entryCount++;
21867e860f15SJohn Edward Broadbent             // Handle paging using skip (number of entries to skip from
21877e860f15SJohn Edward Broadbent             // the start) and top (number of entries to display)
21883648c8beSEd Tanous             if (entryCount <= skip || entryCount > skip + top)
2189193ad2faSJason M. Bills             {
2190193ad2faSJason M. Bills                 continue;
2191193ad2faSJason M. Bills             }
2192193ad2faSJason M. Bills 
219316428a1aSJason M. Bills             std::string idStr;
2194e85d6b16SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2195e1f26343SJason M. Bills             {
2196e1f26343SJason M. Bills                 continue;
2197e1f26343SJason M. Bills             }
2198e85d6b16SJason M. Bills             firstEntry = false;
2199e85d6b16SJason M. Bills 
22003a48b3a2SJason M. Bills             nlohmann::json::object_t bmcJournalLogEntry;
2201c4bf6374SJason M. Bills             if (fillBMCJournalLogEntryJson(idStr, journal.get(),
2202c4bf6374SJason M. Bills                                            bmcJournalLogEntry) != 0)
2203e1f26343SJason M. Bills             {
2204f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
2205e1f26343SJason M. Bills                 return;
2206e1f26343SJason M. Bills             }
22073a48b3a2SJason M. Bills             logEntryArray.push_back(std::move(bmcJournalLogEntry));
2208e1f26343SJason M. Bills         }
2209193ad2faSJason M. Bills         asyncResp->res.jsonValue["Members@odata.count"] = entryCount;
22103648c8beSEd Tanous         if (skip + top < entryCount)
2211193ad2faSJason M. Bills         {
2212193ad2faSJason M. Bills             asyncResp->res.jsonValue["Members@odata.nextLink"] =
22130fda0f12SGeorge Liu                 "/redfish/v1/Managers/bmc/LogServices/Journal/Entries?$skip=" +
22143648c8beSEd Tanous                 std::to_string(skip + top);
2215193ad2faSJason M. Bills         }
22167e860f15SJohn Edward Broadbent         });
2217e1f26343SJason M. Bills }
2218e1f26343SJason M. Bills 
22197e860f15SJohn Edward Broadbent inline void requestRoutesBMCJournalLogEntry(App& app)
2220e1f26343SJason M. Bills {
22217e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
22227e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Journal/Entries/<str>/")
2223ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
22247e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
222545ca1b86SEd Tanous             [&app](const crow::Request& req,
22267e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
22277e860f15SJohn Edward Broadbent                    const std::string& entryID) {
22283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
222945ca1b86SEd Tanous         {
223045ca1b86SEd Tanous             return;
223145ca1b86SEd Tanous         }
2232e1f26343SJason M. Bills         // Convert the unique ID back to a timestamp to find the entry
2233e1f26343SJason M. Bills         uint64_t ts = 0;
2234271584abSEd Tanous         uint64_t index = 0;
22358d1b46d7Szhanghch05         if (!getTimestampFromID(asyncResp, entryID, ts, index))
2236e1f26343SJason M. Bills         {
223716428a1aSJason M. Bills             return;
2238e1f26343SJason M. Bills         }
2239e1f26343SJason M. Bills 
2240e1f26343SJason M. Bills         sd_journal* journalTmp = nullptr;
2241e1f26343SJason M. Bills         int ret = sd_journal_open(&journalTmp, SD_JOURNAL_LOCAL_ONLY);
2242e1f26343SJason M. Bills         if (ret < 0)
2243e1f26343SJason M. Bills         {
2244002d39b4SEd Tanous             BMCWEB_LOG_ERROR << "failed to open journal: " << strerror(-ret);
2245f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2246e1f26343SJason M. Bills             return;
2247e1f26343SJason M. Bills         }
2248002d39b4SEd Tanous         std::unique_ptr<sd_journal, decltype(&sd_journal_close)> journal(
2249002d39b4SEd Tanous             journalTmp, sd_journal_close);
2250e1f26343SJason M. Bills         journalTmp = nullptr;
22517e860f15SJohn Edward Broadbent         // Go to the timestamp in the log and move to the entry at the
22527e860f15SJohn Edward Broadbent         // index tracking the unique ID
2253af07e3f5SJason M. Bills         std::string idStr;
2254af07e3f5SJason M. Bills         bool firstEntry = true;
2255e1f26343SJason M. Bills         ret = sd_journal_seek_realtime_usec(journal.get(), ts);
22562056b6d1SManojkiran Eda         if (ret < 0)
22572056b6d1SManojkiran Eda         {
22582056b6d1SManojkiran Eda             BMCWEB_LOG_ERROR << "failed to seek to an entry in journal"
22592056b6d1SManojkiran Eda                              << strerror(-ret);
22602056b6d1SManojkiran Eda             messages::internalError(asyncResp->res);
22612056b6d1SManojkiran Eda             return;
22622056b6d1SManojkiran Eda         }
2263271584abSEd Tanous         for (uint64_t i = 0; i <= index; i++)
2264e1f26343SJason M. Bills         {
2265e1f26343SJason M. Bills             sd_journal_next(journal.get());
2266af07e3f5SJason M. Bills             if (!getUniqueEntryID(journal.get(), idStr, firstEntry))
2267af07e3f5SJason M. Bills             {
2268af07e3f5SJason M. Bills                 messages::internalError(asyncResp->res);
2269af07e3f5SJason M. Bills                 return;
2270af07e3f5SJason M. Bills             }
2271af07e3f5SJason M. Bills             firstEntry = false;
2272af07e3f5SJason M. Bills         }
2273c4bf6374SJason M. Bills         // Confirm that the entry ID matches what was requested
2274af07e3f5SJason M. Bills         if (idStr != entryID)
2275c4bf6374SJason M. Bills         {
2276ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
2277c4bf6374SJason M. Bills             return;
2278c4bf6374SJason M. Bills         }
2279c4bf6374SJason M. Bills 
22803a48b3a2SJason M. Bills         nlohmann::json::object_t bmcJournalLogEntry;
2281c4bf6374SJason M. Bills         if (fillBMCJournalLogEntryJson(entryID, journal.get(),
22823a48b3a2SJason M. Bills                                        bmcJournalLogEntry) != 0)
2283e1f26343SJason M. Bills         {
2284f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2285e1f26343SJason M. Bills             return;
2286e1f26343SJason M. Bills         }
2287d405bb51SJason M. Bills         asyncResp->res.jsonValue.update(bmcJournalLogEntry);
22887e860f15SJohn Edward Broadbent         });
2289c9bb6861Sraviteja-b }
2290c9bb6861Sraviteja-b 
2291fdd26906SClaire Weinan inline void
2292fdd26906SClaire Weinan     getDumpServiceInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2293fdd26906SClaire Weinan                        const std::string& dumpType)
2294c9bb6861Sraviteja-b {
2295fdd26906SClaire Weinan     std::string dumpPath;
2296fdd26906SClaire Weinan     std::string overWritePolicy;
2297fdd26906SClaire Weinan     bool collectDiagnosticDataSupported = false;
2298fdd26906SClaire Weinan 
2299fdd26906SClaire Weinan     if (dumpType == "BMC")
230045ca1b86SEd Tanous     {
2301fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/Dump";
2302fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2303fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2304fdd26906SClaire Weinan     }
2305fdd26906SClaire Weinan     else if (dumpType == "FaultLog")
2306fdd26906SClaire Weinan     {
2307fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Managers/bmc/LogServices/FaultLog";
2308fdd26906SClaire Weinan         overWritePolicy = "Unknown";
2309fdd26906SClaire Weinan         collectDiagnosticDataSupported = false;
2310fdd26906SClaire Weinan     }
2311fdd26906SClaire Weinan     else if (dumpType == "System")
2312fdd26906SClaire Weinan     {
2313fdd26906SClaire Weinan         dumpPath = "/redfish/v1/Systems/system/LogServices/Dump";
2314fdd26906SClaire Weinan         overWritePolicy = "WrapsWhenFull";
2315fdd26906SClaire Weinan         collectDiagnosticDataSupported = true;
2316fdd26906SClaire Weinan     }
2317fdd26906SClaire Weinan     else
2318fdd26906SClaire Weinan     {
2319fdd26906SClaire Weinan         BMCWEB_LOG_ERROR << "getDumpServiceInfo() invalid dump type: "
2320fdd26906SClaire Weinan                          << dumpType;
2321fdd26906SClaire Weinan         messages::internalError(asyncResp->res);
232245ca1b86SEd Tanous         return;
232345ca1b86SEd Tanous     }
2324fdd26906SClaire Weinan 
2325fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.id"] = dumpPath;
2326fdd26906SClaire Weinan     asyncResp->res.jsonValue["@odata.type"] = "#LogService.v1_2_0.LogService";
2327c9bb6861Sraviteja-b     asyncResp->res.jsonValue["Name"] = "Dump LogService";
2328fdd26906SClaire Weinan     asyncResp->res.jsonValue["Description"] = dumpType + " Dump LogService";
2329fdd26906SClaire Weinan     asyncResp->res.jsonValue["Id"] = std::filesystem::path(dumpPath).filename();
2330fdd26906SClaire Weinan     asyncResp->res.jsonValue["OverWritePolicy"] = std::move(overWritePolicy);
23317c8c4058STejas Patil 
23327c8c4058STejas Patil     std::pair<std::string, std::string> redfishDateTimeOffset =
23337c8c4058STejas Patil         crow::utility::getDateTimeOffsetNow();
23340fda0f12SGeorge Liu     asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
23357c8c4058STejas Patil     asyncResp->res.jsonValue["DateTimeLocalOffset"] =
23367c8c4058STejas Patil         redfishDateTimeOffset.second;
23377c8c4058STejas Patil 
2338fdd26906SClaire Weinan     asyncResp->res.jsonValue["Entries"]["@odata.id"] = dumpPath + "/Entries";
2339002d39b4SEd Tanous     asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
2340fdd26906SClaire Weinan         dumpPath + "/Actions/LogService.ClearLog";
2341fdd26906SClaire Weinan 
2342fdd26906SClaire Weinan     if (collectDiagnosticDataSupported)
2343fdd26906SClaire Weinan     {
2344002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
23451476687dSEd Tanous                                 ["target"] =
2346fdd26906SClaire Weinan             dumpPath + "/Actions/LogService.CollectDiagnosticData";
2347fdd26906SClaire Weinan     }
2348c9bb6861Sraviteja-b }
2349c9bb6861Sraviteja-b 
2350fdd26906SClaire Weinan inline void handleLogServicesDumpServiceGet(
2351fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2352fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
23537e860f15SJohn Edward Broadbent {
23543ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
235545ca1b86SEd Tanous     {
235645ca1b86SEd Tanous         return;
235745ca1b86SEd Tanous     }
2358fdd26906SClaire Weinan     getDumpServiceInfo(asyncResp, dumpType);
2359fdd26906SClaire Weinan }
2360c9bb6861Sraviteja-b 
2361fdd26906SClaire Weinan inline void handleLogServicesDumpEntriesCollectionGet(
2362fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2363fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2364fdd26906SClaire Weinan {
2365fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2366fdd26906SClaire Weinan     {
2367fdd26906SClaire Weinan         return;
2368fdd26906SClaire Weinan     }
2369fdd26906SClaire Weinan     getDumpEntryCollection(asyncResp, dumpType);
2370fdd26906SClaire Weinan }
2371fdd26906SClaire Weinan 
2372fdd26906SClaire Weinan inline void handleLogServicesDumpEntryGet(
2373fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2374fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2375fdd26906SClaire Weinan     const std::string& dumpId)
2376fdd26906SClaire Weinan {
2377fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2378fdd26906SClaire Weinan     {
2379fdd26906SClaire Weinan         return;
2380fdd26906SClaire Weinan     }
2381fdd26906SClaire Weinan     getDumpEntryById(asyncResp, dumpId, dumpType);
2382fdd26906SClaire Weinan }
2383fdd26906SClaire Weinan 
2384fdd26906SClaire Weinan inline void handleLogServicesDumpEntryDelete(
2385fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2386fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2387fdd26906SClaire Weinan     const std::string& dumpId)
2388fdd26906SClaire Weinan {
2389fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2390fdd26906SClaire Weinan     {
2391fdd26906SClaire Weinan         return;
2392fdd26906SClaire Weinan     }
2393fdd26906SClaire Weinan     deleteDumpEntry(asyncResp, dumpId, dumpType);
2394fdd26906SClaire Weinan }
2395fdd26906SClaire Weinan 
2396fdd26906SClaire Weinan inline void handleLogServicesDumpCollectDiagnosticDataPost(
2397fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2398fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2399fdd26906SClaire Weinan {
2400fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2401fdd26906SClaire Weinan     {
2402fdd26906SClaire Weinan         return;
2403fdd26906SClaire Weinan     }
2404fdd26906SClaire Weinan     createDump(asyncResp, req, dumpType);
2405fdd26906SClaire Weinan }
2406fdd26906SClaire Weinan 
2407fdd26906SClaire Weinan inline void handleLogServicesDumpClearLogPost(
2408fdd26906SClaire Weinan     crow::App& app, const std::string& dumpType, const crow::Request& req,
2409fdd26906SClaire Weinan     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
2410fdd26906SClaire Weinan {
2411fdd26906SClaire Weinan     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2412fdd26906SClaire Weinan     {
2413fdd26906SClaire Weinan         return;
2414fdd26906SClaire Weinan     }
2415fdd26906SClaire Weinan     clearDump(asyncResp, dumpType);
2416fdd26906SClaire Weinan }
2417fdd26906SClaire Weinan 
2418fdd26906SClaire Weinan inline void requestRoutesBMCDumpService(App& app)
2419fdd26906SClaire Weinan {
2420fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/")
2421fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2422fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2423fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "BMC"));
2424fdd26906SClaire Weinan }
2425fdd26906SClaire Weinan 
2426fdd26906SClaire Weinan inline void requestRoutesBMCDumpEntryCollection(App& app)
2427fdd26906SClaire Weinan {
2428fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/")
2429fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2430fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2431fdd26906SClaire Weinan             handleLogServicesDumpEntriesCollectionGet, std::ref(app), "BMC"));
2432c9bb6861Sraviteja-b }
2433c9bb6861Sraviteja-b 
24347e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpEntry(App& app)
2435c9bb6861Sraviteja-b {
24367e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24377e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2438ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2439fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2440fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "BMC"));
2441fdd26906SClaire Weinan 
24427e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
24437e860f15SJohn Edward Broadbent                  "/redfish/v1/Managers/bmc/LogServices/Dump/Entries/<str>/")
2444ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
2445fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2446fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "BMC"));
2447c9bb6861Sraviteja-b }
2448c9bb6861Sraviteja-b 
24497e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpCreate(App& app)
2450c9bb6861Sraviteja-b {
24510fda0f12SGeorge Liu     BMCWEB_ROUTE(
24520fda0f12SGeorge Liu         app,
24530fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2454ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
24557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
2456fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpCollectDiagnosticDataPost,
2457fdd26906SClaire Weinan                             std::ref(app), "BMC"));
2458a43be80fSAsmitha Karunanithi }
2459a43be80fSAsmitha Karunanithi 
24607e860f15SJohn Edward Broadbent inline void requestRoutesBMCDumpClear(App& app)
246180319af1SAsmitha Karunanithi {
24620fda0f12SGeorge Liu     BMCWEB_ROUTE(
24630fda0f12SGeorge Liu         app,
24640fda0f12SGeorge Liu         "/redfish/v1/Managers/bmc/LogServices/Dump/Actions/LogService.ClearLog/")
2465ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
2466fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2467fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "BMC"));
246845ca1b86SEd Tanous }
2469fdd26906SClaire Weinan 
2470fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpService(App& app)
2471fdd26906SClaire Weinan {
2472fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/")
2473fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogService)
2474fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2475fdd26906SClaire Weinan             handleLogServicesDumpServiceGet, std::ref(app), "FaultLog"));
2476fdd26906SClaire Weinan }
2477fdd26906SClaire Weinan 
2478fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntryCollection(App& app)
2479fdd26906SClaire Weinan {
2480fdd26906SClaire Weinan     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/")
2481fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntryCollection)
2482fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(
2483fdd26906SClaire Weinan             std::bind_front(handleLogServicesDumpEntriesCollectionGet,
2484fdd26906SClaire Weinan                             std::ref(app), "FaultLog"));
2485fdd26906SClaire Weinan }
2486fdd26906SClaire Weinan 
2487fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpEntry(App& app)
2488fdd26906SClaire Weinan {
2489fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2490fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2491fdd26906SClaire Weinan         .privileges(redfish::privileges::getLogEntry)
2492fdd26906SClaire Weinan         .methods(boost::beast::http::verb::get)(std::bind_front(
2493fdd26906SClaire Weinan             handleLogServicesDumpEntryGet, std::ref(app), "FaultLog"));
2494fdd26906SClaire Weinan 
2495fdd26906SClaire Weinan     BMCWEB_ROUTE(app,
2496fdd26906SClaire Weinan                  "/redfish/v1/Managers/bmc/LogServices/FaultLog/Entries/<str>/")
2497fdd26906SClaire Weinan         .privileges(redfish::privileges::deleteLogEntry)
2498fdd26906SClaire Weinan         .methods(boost::beast::http::verb::delete_)(std::bind_front(
2499fdd26906SClaire Weinan             handleLogServicesDumpEntryDelete, std::ref(app), "FaultLog"));
2500fdd26906SClaire Weinan }
2501fdd26906SClaire Weinan 
2502fdd26906SClaire Weinan inline void requestRoutesFaultLogDumpClear(App& app)
2503fdd26906SClaire Weinan {
2504fdd26906SClaire Weinan     BMCWEB_ROUTE(
2505fdd26906SClaire Weinan         app,
2506fdd26906SClaire Weinan         "/redfish/v1/Managers/bmc/LogServices/FaultLog/Actions/LogService.ClearLog/")
2507fdd26906SClaire Weinan         .privileges(redfish::privileges::postLogService)
2508fdd26906SClaire Weinan         .methods(boost::beast::http::verb::post)(std::bind_front(
2509fdd26906SClaire Weinan             handleLogServicesDumpClearLogPost, std::ref(app), "FaultLog"));
25105cb1dd27SAsmitha Karunanithi }
25115cb1dd27SAsmitha Karunanithi 
25127e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpService(App& app)
25135cb1dd27SAsmitha Karunanithi {
25147e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/")
2515ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
2516002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2517002d39b4SEd Tanous             [&app](const crow::Request& req,
2518002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25193ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
25207e860f15SJohn Edward Broadbent         {
252145ca1b86SEd Tanous             return;
252245ca1b86SEd Tanous         }
25235cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.id"] =
25245cb1dd27SAsmitha Karunanithi             "/redfish/v1/Systems/system/LogServices/Dump";
25255cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["@odata.type"] =
2526d337bb72SAsmitha Karunanithi             "#LogService.v1_2_0.LogService";
25275cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Name"] = "Dump LogService";
252845ca1b86SEd Tanous         asyncResp->res.jsonValue["Description"] = "System Dump LogService";
25295cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["Id"] = "Dump";
25305cb1dd27SAsmitha Karunanithi         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
25317c8c4058STejas Patil 
25327c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
25337c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
253445ca1b86SEd Tanous         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
25357c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
25367c8c4058STejas Patil             redfishDateTimeOffset.second;
25377c8c4058STejas Patil 
25381476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
25391476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Entries";
2540002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
25411476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog";
25421476687dSEd Tanous 
2543002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
25441476687dSEd Tanous                                 ["target"] =
25451476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData";
25467e860f15SJohn Edward Broadbent         });
25475cb1dd27SAsmitha Karunanithi }
25485cb1dd27SAsmitha Karunanithi 
25497e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntryCollection(App& app)
25507e860f15SJohn Edward Broadbent {
25517e860f15SJohn Edward Broadbent 
25525cb1dd27SAsmitha Karunanithi     /**
25535cb1dd27SAsmitha Karunanithi      * Functions triggers appropriate requests on DBus
25545cb1dd27SAsmitha Karunanithi      */
2555b2a3289dSAsmitha Karunanithi     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Dump/Entries/")
2556ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
25577e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
255845ca1b86SEd Tanous             [&app](const crow::Request& req,
2559864d6a17SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
25603ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
256145ca1b86SEd Tanous         {
256245ca1b86SEd Tanous             return;
256345ca1b86SEd Tanous         }
25645cb1dd27SAsmitha Karunanithi         getDumpEntryCollection(asyncResp, "System");
25657e860f15SJohn Edward Broadbent         });
25665cb1dd27SAsmitha Karunanithi }
25675cb1dd27SAsmitha Karunanithi 
25687e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpEntry(App& app)
25695cb1dd27SAsmitha Karunanithi {
25707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2571864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2572ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
2573ed398213SEd Tanous 
25747e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
257545ca1b86SEd Tanous             [&app](const crow::Request& req,
25767e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25777e860f15SJohn Edward Broadbent                    const std::string& param) {
25783ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
2579c7a6d660SClaire Weinan         {
2580c7a6d660SClaire Weinan             return;
2581c7a6d660SClaire Weinan         }
2582c7a6d660SClaire Weinan         getDumpEntryById(asyncResp, param, "System");
25837e860f15SJohn Edward Broadbent         });
25848d1b46d7Szhanghch05 
25857e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
2586864d6a17SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Dump/Entries/<str>/")
2587ed398213SEd Tanous         .privileges(redfish::privileges::deleteLogEntry)
25887e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::delete_)(
258945ca1b86SEd Tanous             [&app](const crow::Request& req,
25907e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25917e860f15SJohn Edward Broadbent                    const std::string& param) {
25923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
259345ca1b86SEd Tanous         {
259445ca1b86SEd Tanous             return;
259545ca1b86SEd Tanous         }
25967e860f15SJohn Edward Broadbent         deleteDumpEntry(asyncResp, param, "system");
25977e860f15SJohn Edward Broadbent         });
25985cb1dd27SAsmitha Karunanithi }
2599c9bb6861Sraviteja-b 
26007e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpCreate(App& app)
2601c9bb6861Sraviteja-b {
26020fda0f12SGeorge Liu     BMCWEB_ROUTE(
26030fda0f12SGeorge Liu         app,
26040fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.CollectDiagnosticData/")
2605ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26067e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
260745ca1b86SEd Tanous             [&app](const crow::Request& req,
260845ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26093ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
261045ca1b86SEd Tanous         {
261145ca1b86SEd Tanous             return;
261245ca1b86SEd Tanous         }
261345ca1b86SEd Tanous         createDump(asyncResp, req, "System");
261445ca1b86SEd Tanous         });
2615a43be80fSAsmitha Karunanithi }
2616a43be80fSAsmitha Karunanithi 
26177e860f15SJohn Edward Broadbent inline void requestRoutesSystemDumpClear(App& app)
2618a43be80fSAsmitha Karunanithi {
26190fda0f12SGeorge Liu     BMCWEB_ROUTE(
26200fda0f12SGeorge Liu         app,
26210fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Dump/Actions/LogService.ClearLog/")
2622ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
26237e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
262445ca1b86SEd Tanous             [&app](const crow::Request& req,
26257e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
26267e860f15SJohn Edward Broadbent 
262745ca1b86SEd Tanous             {
26283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
262945ca1b86SEd Tanous         {
263045ca1b86SEd Tanous             return;
263145ca1b86SEd Tanous         }
263245ca1b86SEd Tanous         clearDump(asyncResp, "System");
263345ca1b86SEd Tanous         });
2634013487e5Sraviteja-b }
2635013487e5Sraviteja-b 
26367e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpService(App& app)
26371da66f75SEd Tanous {
26383946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
26393946028dSAppaRao Puli     // method for security reasons.
26401da66f75SEd Tanous     /**
26411da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
26421da66f75SEd Tanous      */
26437e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/Crashdump/")
2644ed398213SEd Tanous         // This is incorrect, should be:
2645ed398213SEd Tanous         //.privileges(redfish::privileges::getLogService)
2646432a890cSEd Tanous         .privileges({{"ConfigureManager"}})
2647002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2648002d39b4SEd Tanous             [&app](const crow::Request& req,
2649002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
265145ca1b86SEd Tanous         {
265245ca1b86SEd Tanous             return;
265345ca1b86SEd Tanous         }
26547e860f15SJohn Edward Broadbent         // Copy over the static data to include the entries added by
26557e860f15SJohn Edward Broadbent         // SubRoute
26560f74e643SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
2657424c4176SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump";
2658e1f26343SJason M. Bills         asyncResp->res.jsonValue["@odata.type"] =
26598e6c099aSJason M. Bills             "#LogService.v1_2_0.LogService";
26604f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Open BMC Oem Crashdump Service";
26614f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Description"] = "Oem Crashdump Service";
26624f50ae4bSGunnar Mills         asyncResp->res.jsonValue["Id"] = "Oem Crashdump";
2663e1f26343SJason M. Bills         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
2664e1f26343SJason M. Bills         asyncResp->res.jsonValue["MaxNumberOfRecords"] = 3;
26657c8c4058STejas Patil 
26667c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
26677c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
26687c8c4058STejas Patil         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
26697c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
26707c8c4058STejas Patil             redfishDateTimeOffset.second;
26717c8c4058STejas Patil 
26721476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
26731476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2674002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"]["target"] =
26751476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog";
2676002d39b4SEd Tanous         asyncResp->res.jsonValue["Actions"]["#LogService.CollectDiagnosticData"]
26771476687dSEd Tanous                                 ["target"] =
26781476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData";
26797e860f15SJohn Edward Broadbent         });
26801da66f75SEd Tanous }
26811da66f75SEd Tanous 
26827e860f15SJohn Edward Broadbent void inline requestRoutesCrashdumpClear(App& app)
26835b61b5e8SJason M. Bills {
26840fda0f12SGeorge Liu     BMCWEB_ROUTE(
26850fda0f12SGeorge Liu         app,
26860fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.ClearLog/")
2687ed398213SEd Tanous         // This is incorrect, should be:
2688ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2689432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
26907e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
269145ca1b86SEd Tanous             [&app](const crow::Request& req,
26927e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
26933ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
269445ca1b86SEd Tanous         {
269545ca1b86SEd Tanous             return;
269645ca1b86SEd Tanous         }
26975b61b5e8SJason M. Bills         crow::connections::systemBus->async_method_call(
26985b61b5e8SJason M. Bills             [asyncResp](const boost::system::error_code ec,
2699cb13a392SEd Tanous                         const std::string&) {
27005b61b5e8SJason M. Bills             if (ec)
27015b61b5e8SJason M. Bills             {
27025b61b5e8SJason M. Bills                 messages::internalError(asyncResp->res);
27035b61b5e8SJason M. Bills                 return;
27045b61b5e8SJason M. Bills             }
27055b61b5e8SJason M. Bills             messages::success(asyncResp->res);
27065b61b5e8SJason M. Bills             },
2707002d39b4SEd Tanous             crashdumpObject, crashdumpPath, deleteAllInterface, "DeleteAll");
27087e860f15SJohn Edward Broadbent         });
27095b61b5e8SJason M. Bills }
27105b61b5e8SJason M. Bills 
27118d1b46d7Szhanghch05 static void
27128d1b46d7Szhanghch05     logCrashdumpEntry(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
27138d1b46d7Szhanghch05                       const std::string& logID, nlohmann::json& logEntryJson)
2714e855dd28SJason M. Bills {
2715043a0536SJohnathan Mantey     auto getStoredLogCallback =
2716b9d36b47SEd Tanous         [asyncResp, logID,
2717b9d36b47SEd Tanous          &logEntryJson](const boost::system::error_code ec,
2718b9d36b47SEd Tanous                         const dbus::utility::DBusPropertiesMap& params) {
2719e855dd28SJason M. Bills         if (ec)
2720e855dd28SJason M. Bills         {
2721e855dd28SJason M. Bills             BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
27221ddcf01aSJason M. Bills             if (ec.value() ==
27231ddcf01aSJason M. Bills                 boost::system::linux_error::bad_request_descriptor)
27241ddcf01aSJason M. Bills             {
2725002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry", logID);
27261ddcf01aSJason M. Bills             }
27271ddcf01aSJason M. Bills             else
27281ddcf01aSJason M. Bills             {
2729e855dd28SJason M. Bills                 messages::internalError(asyncResp->res);
27301ddcf01aSJason M. Bills             }
2731e855dd28SJason M. Bills             return;
2732e855dd28SJason M. Bills         }
2733043a0536SJohnathan Mantey 
2734043a0536SJohnathan Mantey         std::string timestamp{};
2735043a0536SJohnathan Mantey         std::string filename{};
2736043a0536SJohnathan Mantey         std::string logfile{};
27372c70f800SEd Tanous         parseCrashdumpParameters(params, filename, timestamp, logfile);
2738043a0536SJohnathan Mantey 
2739043a0536SJohnathan Mantey         if (filename.empty() || timestamp.empty())
2740e855dd28SJason M. Bills         {
2741002d39b4SEd Tanous             messages::resourceMissingAtURI(asyncResp->res,
2742002d39b4SEd Tanous                                            crow::utility::urlFromPieces(logID));
2743e855dd28SJason M. Bills             return;
2744e855dd28SJason M. Bills         }
2745e855dd28SJason M. Bills 
2746043a0536SJohnathan Mantey         std::string crashdumpURI =
2747e855dd28SJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" +
2748043a0536SJohnathan Mantey             logID + "/" + filename;
274984afc48bSJason M. Bills         nlohmann::json::object_t logEntry;
275084afc48bSJason M. Bills         logEntry["@odata.type"] = "#LogEntry.v1_7_0.LogEntry";
275184afc48bSJason M. Bills         logEntry["@odata.id"] =
275284afc48bSJason M. Bills             "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/" + logID;
275384afc48bSJason M. Bills         logEntry["Name"] = "CPU Crashdump";
275484afc48bSJason M. Bills         logEntry["Id"] = logID;
275584afc48bSJason M. Bills         logEntry["EntryType"] = "Oem";
275684afc48bSJason M. Bills         logEntry["AdditionalDataURI"] = std::move(crashdumpURI);
275784afc48bSJason M. Bills         logEntry["DiagnosticDataType"] = "OEM";
275884afc48bSJason M. Bills         logEntry["OEMDiagnosticDataType"] = "PECICrashdump";
275984afc48bSJason M. Bills         logEntry["Created"] = std::move(timestamp);
27602b20ef6eSJason M. Bills 
27612b20ef6eSJason M. Bills         // If logEntryJson references an array of LogEntry resources
27622b20ef6eSJason M. Bills         // ('Members' list), then push this as a new entry, otherwise set it
27632b20ef6eSJason M. Bills         // directly
27642b20ef6eSJason M. Bills         if (logEntryJson.is_array())
27652b20ef6eSJason M. Bills         {
27662b20ef6eSJason M. Bills             logEntryJson.push_back(logEntry);
27672b20ef6eSJason M. Bills             asyncResp->res.jsonValue["Members@odata.count"] =
27682b20ef6eSJason M. Bills                 logEntryJson.size();
27692b20ef6eSJason M. Bills         }
27702b20ef6eSJason M. Bills         else
27712b20ef6eSJason M. Bills         {
2772d405bb51SJason M. Bills             logEntryJson.update(logEntry);
27732b20ef6eSJason M. Bills         }
2774e855dd28SJason M. Bills     };
2775e855dd28SJason M. Bills     crow::connections::systemBus->async_method_call(
27765b61b5e8SJason M. Bills         std::move(getStoredLogCallback), crashdumpObject,
27775b61b5e8SJason M. Bills         crashdumpPath + std::string("/") + logID,
2778043a0536SJohnathan Mantey         "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
2779e855dd28SJason M. Bills }
2780e855dd28SJason M. Bills 
27817e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntryCollection(App& app)
27821da66f75SEd Tanous {
27833946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
27843946028dSAppaRao Puli     // method for security reasons.
27851da66f75SEd Tanous     /**
27861da66f75SEd Tanous      * Functions triggers appropriate requests on DBus
27871da66f75SEd Tanous      */
27887e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
27897e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/")
2790ed398213SEd Tanous         // This is incorrect, should be.
2791ed398213SEd Tanous         //.privileges(redfish::privileges::postLogEntryCollection)
2792432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2793002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
2794002d39b4SEd Tanous             [&app](const crow::Request& req,
2795002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
27963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
279745ca1b86SEd Tanous         {
279845ca1b86SEd Tanous             return;
279945ca1b86SEd Tanous         }
28002b20ef6eSJason M. Bills         crow::connections::systemBus->async_method_call(
28012b20ef6eSJason M. Bills             [asyncResp](const boost::system::error_code ec,
28022b20ef6eSJason M. Bills                         const std::vector<std::string>& resp) {
28031da66f75SEd Tanous             if (ec)
28041da66f75SEd Tanous             {
28051da66f75SEd Tanous                 if (ec.value() !=
28061da66f75SEd Tanous                     boost::system::errc::no_such_file_or_directory)
28071da66f75SEd Tanous                 {
28081da66f75SEd Tanous                     BMCWEB_LOG_DEBUG << "failed to get entries ec: "
28091da66f75SEd Tanous                                      << ec.message();
2810f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
28111da66f75SEd Tanous                     return;
28121da66f75SEd Tanous                 }
28131da66f75SEd Tanous             }
2814e1f26343SJason M. Bills             asyncResp->res.jsonValue["@odata.type"] =
28151da66f75SEd Tanous                 "#LogEntryCollection.LogEntryCollection";
28160f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2817424c4176SJason M. Bills                 "/redfish/v1/Systems/system/LogServices/Crashdump/Entries";
2818002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Open BMC Crashdump Entries";
2819e1f26343SJason M. Bills             asyncResp->res.jsonValue["Description"] =
2820424c4176SJason M. Bills                 "Collection of Crashdump Entries";
2821002d39b4SEd Tanous             asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
2822a2dd60a6SBrandon Kim             asyncResp->res.jsonValue["Members@odata.count"] = 0;
28232b20ef6eSJason M. Bills 
28242b20ef6eSJason M. Bills             for (const std::string& path : resp)
28251da66f75SEd Tanous             {
28262b20ef6eSJason M. Bills                 const sdbusplus::message::object_path objPath(path);
2827e855dd28SJason M. Bills                 // Get the log ID
28282b20ef6eSJason M. Bills                 std::string logID = objPath.filename();
28292b20ef6eSJason M. Bills                 if (logID.empty())
28301da66f75SEd Tanous                 {
2831e855dd28SJason M. Bills                     continue;
28321da66f75SEd Tanous                 }
2833e855dd28SJason M. Bills                 // Add the log entry to the array
28342b20ef6eSJason M. Bills                 logCrashdumpEntry(asyncResp, logID,
28352b20ef6eSJason M. Bills                                   asyncResp->res.jsonValue["Members"]);
28361da66f75SEd Tanous             }
28372b20ef6eSJason M. Bills             },
28381da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper",
28391da66f75SEd Tanous             "/xyz/openbmc_project/object_mapper",
28401da66f75SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "", 0,
28415b61b5e8SJason M. Bills             std::array<const char*, 1>{crashdumpInterface});
28427e860f15SJohn Edward Broadbent         });
28431da66f75SEd Tanous }
28441da66f75SEd Tanous 
28457e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpEntry(App& app)
28461da66f75SEd Tanous {
28473946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28483946028dSAppaRao Puli     // method for security reasons.
28491da66f75SEd Tanous 
28507e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28517e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/")
2852ed398213SEd Tanous         // this is incorrect, should be
2853ed398213SEd Tanous         // .privileges(redfish::privileges::getLogEntry)
2854432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
28557e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
285645ca1b86SEd Tanous             [&app](const crow::Request& req,
28577e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28587e860f15SJohn Edward Broadbent                    const std::string& param) {
28593ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
286045ca1b86SEd Tanous         {
286145ca1b86SEd Tanous             return;
286245ca1b86SEd Tanous         }
28637e860f15SJohn Edward Broadbent         const std::string& logID = param;
2864e855dd28SJason M. Bills         logCrashdumpEntry(asyncResp, logID, asyncResp->res.jsonValue);
28657e860f15SJohn Edward Broadbent         });
2866e855dd28SJason M. Bills }
2867e855dd28SJason M. Bills 
28687e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpFile(App& app)
2869e855dd28SJason M. Bills {
28703946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
28713946028dSAppaRao Puli     // method for security reasons.
28727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
28737e860f15SJohn Edward Broadbent         app,
28747e860f15SJohn Edward Broadbent         "/redfish/v1/Systems/system/LogServices/Crashdump/Entries/<str>/<str>/")
2875ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
28767e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
2877a4ce114aSNan Zhou             [](const crow::Request& req,
28787e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
28797e860f15SJohn Edward Broadbent                const std::string& logID, const std::string& fileName) {
28802a9beeedSShounak Mitra         // Do not call getRedfishRoute here since the crashdump file is not a
28812a9beeedSShounak Mitra         // Redfish resource.
2882043a0536SJohnathan Mantey         auto getStoredLogCallback =
2883002d39b4SEd Tanous             [asyncResp, logID, fileName, url(boost::urls::url(req.urlView))](
2884abf2add6SEd Tanous                 const boost::system::error_code ec,
2885002d39b4SEd Tanous                 const std::vector<
2886002d39b4SEd Tanous                     std::pair<std::string, dbus::utility::DbusVariantType>>&
28877e860f15SJohn Edward Broadbent                     resp) {
28881da66f75SEd Tanous             if (ec)
28891da66f75SEd Tanous             {
2890002d39b4SEd Tanous                 BMCWEB_LOG_DEBUG << "failed to get log ec: " << ec.message();
2891f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
28921da66f75SEd Tanous                 return;
28931da66f75SEd Tanous             }
2894e855dd28SJason M. Bills 
2895043a0536SJohnathan Mantey             std::string dbusFilename{};
2896043a0536SJohnathan Mantey             std::string dbusTimestamp{};
2897043a0536SJohnathan Mantey             std::string dbusFilepath{};
2898043a0536SJohnathan Mantey 
2899002d39b4SEd Tanous             parseCrashdumpParameters(resp, dbusFilename, dbusTimestamp,
2900002d39b4SEd Tanous                                      dbusFilepath);
2901043a0536SJohnathan Mantey 
2902043a0536SJohnathan Mantey             if (dbusFilename.empty() || dbusTimestamp.empty() ||
2903043a0536SJohnathan Mantey                 dbusFilepath.empty())
29041da66f75SEd Tanous             {
2905ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
29061da66f75SEd Tanous                 return;
29071da66f75SEd Tanous             }
2908e855dd28SJason M. Bills 
2909043a0536SJohnathan Mantey             // Verify the file name parameter is correct
2910043a0536SJohnathan Mantey             if (fileName != dbusFilename)
2911043a0536SJohnathan Mantey             {
2912ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2913043a0536SJohnathan Mantey                 return;
2914043a0536SJohnathan Mantey             }
2915043a0536SJohnathan Mantey 
2916043a0536SJohnathan Mantey             if (!std::filesystem::exists(dbusFilepath))
2917043a0536SJohnathan Mantey             {
2918ace85d60SEd Tanous                 messages::resourceMissingAtURI(asyncResp->res, url);
2919043a0536SJohnathan Mantey                 return;
2920043a0536SJohnathan Mantey             }
2921002d39b4SEd Tanous             std::ifstream ifs(dbusFilepath, std::ios::in | std::ios::binary);
2922002d39b4SEd Tanous             asyncResp->res.body() =
2923002d39b4SEd Tanous                 std::string(std::istreambuf_iterator<char>{ifs}, {});
2924043a0536SJohnathan Mantey 
29257e860f15SJohn Edward Broadbent             // Configure this to be a file download when accessed
29267e860f15SJohn Edward Broadbent             // from a browser
2927002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Disposition", "attachment");
29281da66f75SEd Tanous         };
29291da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
29305b61b5e8SJason M. Bills             std::move(getStoredLogCallback), crashdumpObject,
29315b61b5e8SJason M. Bills             crashdumpPath + std::string("/") + logID,
2932002d39b4SEd Tanous             "org.freedesktop.DBus.Properties", "GetAll", crashdumpInterface);
29337e860f15SJohn Edward Broadbent         });
29341da66f75SEd Tanous }
29351da66f75SEd Tanous 
2936c5a4c82aSJason M. Bills enum class OEMDiagnosticType
2937c5a4c82aSJason M. Bills {
2938c5a4c82aSJason M. Bills     onDemand,
2939c5a4c82aSJason M. Bills     telemetry,
2940c5a4c82aSJason M. Bills     invalid,
2941c5a4c82aSJason M. Bills };
2942c5a4c82aSJason M. Bills 
2943f7725d79SEd Tanous inline OEMDiagnosticType
2944f7725d79SEd Tanous     getOEMDiagnosticType(const std::string_view& oemDiagStr)
2945c5a4c82aSJason M. Bills {
2946c5a4c82aSJason M. Bills     if (oemDiagStr == "OnDemand")
2947c5a4c82aSJason M. Bills     {
2948c5a4c82aSJason M. Bills         return OEMDiagnosticType::onDemand;
2949c5a4c82aSJason M. Bills     }
2950c5a4c82aSJason M. Bills     if (oemDiagStr == "Telemetry")
2951c5a4c82aSJason M. Bills     {
2952c5a4c82aSJason M. Bills         return OEMDiagnosticType::telemetry;
2953c5a4c82aSJason M. Bills     }
2954c5a4c82aSJason M. Bills 
2955c5a4c82aSJason M. Bills     return OEMDiagnosticType::invalid;
2956c5a4c82aSJason M. Bills }
2957c5a4c82aSJason M. Bills 
29587e860f15SJohn Edward Broadbent inline void requestRoutesCrashdumpCollect(App& app)
29591da66f75SEd Tanous {
29603946028dSAppaRao Puli     // Note: Deviated from redfish privilege registry for GET & HEAD
29613946028dSAppaRao Puli     // method for security reasons.
29620fda0f12SGeorge Liu     BMCWEB_ROUTE(
29630fda0f12SGeorge Liu         app,
29640fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/Crashdump/Actions/LogService.CollectDiagnosticData/")
2965ed398213SEd Tanous         // The below is incorrect;  Should be ConfigureManager
2966ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
2967432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2968002d39b4SEd Tanous         .methods(boost::beast::http::verb::post)(
2969002d39b4SEd Tanous             [&app](const crow::Request& req,
29707e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
29713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
297245ca1b86SEd Tanous         {
297345ca1b86SEd Tanous             return;
297445ca1b86SEd Tanous         }
29758e6c099aSJason M. Bills         std::string diagnosticDataType;
29768e6c099aSJason M. Bills         std::string oemDiagnosticDataType;
297715ed6780SWilly Tu         if (!redfish::json_util::readJsonAction(
2978002d39b4SEd Tanous                 req, asyncResp->res, "DiagnosticDataType", diagnosticDataType,
2979002d39b4SEd Tanous                 "OEMDiagnosticDataType", oemDiagnosticDataType))
29808e6c099aSJason M. Bills         {
29818e6c099aSJason M. Bills             return;
29828e6c099aSJason M. Bills         }
29838e6c099aSJason M. Bills 
29848e6c099aSJason M. Bills         if (diagnosticDataType != "OEM")
29858e6c099aSJason M. Bills         {
29868e6c099aSJason M. Bills             BMCWEB_LOG_ERROR
29878e6c099aSJason M. Bills                 << "Only OEM DiagnosticDataType supported for Crashdump";
29888e6c099aSJason M. Bills             messages::actionParameterValueFormatError(
29898e6c099aSJason M. Bills                 asyncResp->res, diagnosticDataType, "DiagnosticDataType",
29908e6c099aSJason M. Bills                 "CollectDiagnosticData");
29918e6c099aSJason M. Bills             return;
29928e6c099aSJason M. Bills         }
29938e6c099aSJason M. Bills 
2994c5a4c82aSJason M. Bills         OEMDiagnosticType oemDiagType =
2995c5a4c82aSJason M. Bills             getOEMDiagnosticType(oemDiagnosticDataType);
2996c5a4c82aSJason M. Bills 
2997c5a4c82aSJason M. Bills         std::string iface;
2998c5a4c82aSJason M. Bills         std::string method;
2999c5a4c82aSJason M. Bills         std::string taskMatchStr;
3000c5a4c82aSJason M. Bills         if (oemDiagType == OEMDiagnosticType::onDemand)
3001c5a4c82aSJason M. Bills         {
3002c5a4c82aSJason M. Bills             iface = crashdumpOnDemandInterface;
3003c5a4c82aSJason M. Bills             method = "GenerateOnDemandLog";
3004c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3005c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3006c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3007c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3008c5a4c82aSJason M. Bills         }
3009c5a4c82aSJason M. Bills         else if (oemDiagType == OEMDiagnosticType::telemetry)
3010c5a4c82aSJason M. Bills         {
3011c5a4c82aSJason M. Bills             iface = crashdumpTelemetryInterface;
3012c5a4c82aSJason M. Bills             method = "GenerateTelemetryLog";
3013c5a4c82aSJason M. Bills             taskMatchStr = "type='signal',"
3014c5a4c82aSJason M. Bills                            "interface='org.freedesktop.DBus.Properties',"
3015c5a4c82aSJason M. Bills                            "member='PropertiesChanged',"
3016c5a4c82aSJason M. Bills                            "arg0namespace='com.intel.crashdump'";
3017c5a4c82aSJason M. Bills         }
3018c5a4c82aSJason M. Bills         else
3019c5a4c82aSJason M. Bills         {
3020c5a4c82aSJason M. Bills             BMCWEB_LOG_ERROR << "Unsupported OEMDiagnosticDataType: "
3021c5a4c82aSJason M. Bills                              << oemDiagnosticDataType;
3022c5a4c82aSJason M. Bills             messages::actionParameterValueFormatError(
3023002d39b4SEd Tanous                 asyncResp->res, oemDiagnosticDataType, "OEMDiagnosticDataType",
3024002d39b4SEd Tanous                 "CollectDiagnosticData");
3025c5a4c82aSJason M. Bills             return;
3026c5a4c82aSJason M. Bills         }
3027c5a4c82aSJason M. Bills 
3028c5a4c82aSJason M. Bills         auto collectCrashdumpCallback =
3029c5a4c82aSJason M. Bills             [asyncResp, payload(task::Payload(req)),
3030c5a4c82aSJason M. Bills              taskMatchStr](const boost::system::error_code ec,
303198be3e39SEd Tanous                            const std::string&) mutable {
30321da66f75SEd Tanous             if (ec)
30331da66f75SEd Tanous             {
3034002d39b4SEd Tanous                 if (ec.value() == boost::system::errc::operation_not_supported)
30351da66f75SEd Tanous                 {
3036f12894f8SJason M. Bills                     messages::resourceInStandby(asyncResp->res);
30371da66f75SEd Tanous                 }
30384363d3b2SJason M. Bills                 else if (ec.value() ==
30394363d3b2SJason M. Bills                          boost::system::errc::device_or_resource_busy)
30404363d3b2SJason M. Bills                 {
3041002d39b4SEd Tanous                     messages::serviceTemporarilyUnavailable(asyncResp->res,
3042002d39b4SEd Tanous                                                             "60");
30434363d3b2SJason M. Bills                 }
30441da66f75SEd Tanous                 else
30451da66f75SEd Tanous                 {
3046f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
30471da66f75SEd Tanous                 }
30481da66f75SEd Tanous                 return;
30491da66f75SEd Tanous             }
3050002d39b4SEd Tanous             std::shared_ptr<task::TaskData> task = task::TaskData::createTask(
305159d494eeSPatrick Williams                 [](boost::system::error_code err, sdbusplus::message_t&,
3052002d39b4SEd Tanous                    const std::shared_ptr<task::TaskData>& taskData) {
305366afe4faSJames Feist                 if (!err)
305466afe4faSJames Feist                 {
3055002d39b4SEd Tanous                     taskData->messages.emplace_back(messages::taskCompletedOK(
3056e5d5006bSJames Feist                         std::to_string(taskData->index)));
3057831d6b09SJames Feist                     taskData->state = "Completed";
305866afe4faSJames Feist                 }
305932898ceaSJames Feist                 return task::completed;
306066afe4faSJames Feist                 },
3061c5a4c82aSJason M. Bills                 taskMatchStr);
3062c5a4c82aSJason M. Bills 
306346229577SJames Feist             task->startTimer(std::chrono::minutes(5));
306446229577SJames Feist             task->populateResp(asyncResp->res);
306598be3e39SEd Tanous             task->payload.emplace(std::move(payload));
30661da66f75SEd Tanous         };
30678e6c099aSJason M. Bills 
30681da66f75SEd Tanous         crow::connections::systemBus->async_method_call(
3069002d39b4SEd Tanous             std::move(collectCrashdumpCallback), crashdumpObject, crashdumpPath,
3070002d39b4SEd Tanous             iface, method);
30717e860f15SJohn Edward Broadbent         });
30726eda7685SKenny L. Ku }
30736eda7685SKenny L. Ku 
3074cb92c03bSAndrew Geissler /**
3075cb92c03bSAndrew Geissler  * DBusLogServiceActionsClear class supports POST method for ClearLog action.
3076cb92c03bSAndrew Geissler  */
30777e860f15SJohn Edward Broadbent inline void requestRoutesDBusLogServiceActionsClear(App& app)
3078cb92c03bSAndrew Geissler {
3079cb92c03bSAndrew Geissler     /**
3080cb92c03bSAndrew Geissler      * Function handles POST method request.
3081cb92c03bSAndrew Geissler      * The Clear Log actions does not require any parameter.The action deletes
3082cb92c03bSAndrew Geissler      * all entries found in the Entries collection for this Log Service.
3083cb92c03bSAndrew Geissler      */
30847e860f15SJohn Edward Broadbent 
30850fda0f12SGeorge Liu     BMCWEB_ROUTE(
30860fda0f12SGeorge Liu         app,
30870fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/EventLog/Actions/LogService.ClearLog/")
3088ed398213SEd Tanous         .privileges(redfish::privileges::postLogService)
30897e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
309045ca1b86SEd Tanous             [&app](const crow::Request& req,
30917e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
30923ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
309345ca1b86SEd Tanous         {
309445ca1b86SEd Tanous             return;
309545ca1b86SEd Tanous         }
3096cb92c03bSAndrew Geissler         BMCWEB_LOG_DEBUG << "Do delete all entries.";
3097cb92c03bSAndrew Geissler 
3098cb92c03bSAndrew Geissler         // Process response from Logging service.
3099002d39b4SEd Tanous         auto respHandler = [asyncResp](const boost::system::error_code ec) {
3100002d39b4SEd Tanous             BMCWEB_LOG_DEBUG << "doClearLog resp_handler callback: Done";
3101cb92c03bSAndrew Geissler             if (ec)
3102cb92c03bSAndrew Geissler             {
3103cb92c03bSAndrew Geissler                 // TODO Handle for specific error code
3104002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearLog resp_handler got error " << ec;
3105cb92c03bSAndrew Geissler                 asyncResp->res.result(
3106cb92c03bSAndrew Geissler                     boost::beast::http::status::internal_server_error);
3107cb92c03bSAndrew Geissler                 return;
3108cb92c03bSAndrew Geissler             }
3109cb92c03bSAndrew Geissler 
3110002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::no_content);
3111cb92c03bSAndrew Geissler         };
3112cb92c03bSAndrew Geissler 
3113cb92c03bSAndrew Geissler         // Make call to Logging service to request Clear Log
3114cb92c03bSAndrew Geissler         crow::connections::systemBus->async_method_call(
31152c70f800SEd Tanous             respHandler, "xyz.openbmc_project.Logging",
3116cb92c03bSAndrew Geissler             "/xyz/openbmc_project/logging",
3117cb92c03bSAndrew Geissler             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31187e860f15SJohn Edward Broadbent         });
3119cb92c03bSAndrew Geissler }
3120a3316fc6SZhikuiRen 
3121a3316fc6SZhikuiRen /****************************************************
3122a3316fc6SZhikuiRen  * Redfish PostCode interfaces
3123a3316fc6SZhikuiRen  * using DBUS interface: getPostCodesTS
3124a3316fc6SZhikuiRen  ******************************************************/
31257e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesLogService(App& app)
3126a3316fc6SZhikuiRen {
31277e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/LogServices/PostCodes/")
3128ed398213SEd Tanous         .privileges(redfish::privileges::getLogService)
3129002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
3130002d39b4SEd Tanous             [&app](const crow::Request& req,
3131002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31323ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
313345ca1b86SEd Tanous         {
313445ca1b86SEd Tanous             return;
313545ca1b86SEd Tanous         }
31361476687dSEd Tanous 
31371476687dSEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
31381476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes";
31391476687dSEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
31401476687dSEd Tanous             "#LogService.v1_1_0.LogService";
31411476687dSEd Tanous         asyncResp->res.jsonValue["Name"] = "POST Code Log Service";
31421476687dSEd Tanous         asyncResp->res.jsonValue["Description"] = "POST Code Log Service";
31431476687dSEd Tanous         asyncResp->res.jsonValue["Id"] = "BIOS POST Code Log";
31441476687dSEd Tanous         asyncResp->res.jsonValue["OverWritePolicy"] = "WrapsWhenFull";
31451476687dSEd Tanous         asyncResp->res.jsonValue["Entries"]["@odata.id"] =
31461476687dSEd Tanous             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
31477c8c4058STejas Patil 
31487c8c4058STejas Patil         std::pair<std::string, std::string> redfishDateTimeOffset =
31497c8c4058STejas Patil             crow::utility::getDateTimeOffsetNow();
31500fda0f12SGeorge Liu         asyncResp->res.jsonValue["DateTime"] = redfishDateTimeOffset.first;
31517c8c4058STejas Patil         asyncResp->res.jsonValue["DateTimeLocalOffset"] =
31527c8c4058STejas Patil             redfishDateTimeOffset.second;
31537c8c4058STejas Patil 
3154a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Actions"]["#LogService.ClearLog"] = {
31557e860f15SJohn Edward Broadbent             {"target",
31560fda0f12SGeorge Liu              "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog"}};
31577e860f15SJohn Edward Broadbent         });
3158a3316fc6SZhikuiRen }
3159a3316fc6SZhikuiRen 
31607e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesClear(App& app)
3161a3316fc6SZhikuiRen {
31620fda0f12SGeorge Liu     BMCWEB_ROUTE(
31630fda0f12SGeorge Liu         app,
31640fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Actions/LogService.ClearLog/")
3165ed398213SEd Tanous         // The following privilege is incorrect;  It should be ConfigureManager
3166ed398213SEd Tanous         //.privileges(redfish::privileges::postLogService)
3167432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
31687e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
316945ca1b86SEd Tanous             [&app](const crow::Request& req,
31707e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
31713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
317245ca1b86SEd Tanous         {
317345ca1b86SEd Tanous             return;
317445ca1b86SEd Tanous         }
3175a3316fc6SZhikuiRen         BMCWEB_LOG_DEBUG << "Do delete all postcodes entries.";
3176a3316fc6SZhikuiRen 
3177a3316fc6SZhikuiRen         // Make call to post-code service to request clear all
3178a3316fc6SZhikuiRen         crow::connections::systemBus->async_method_call(
3179a3316fc6SZhikuiRen             [asyncResp](const boost::system::error_code ec) {
3180a3316fc6SZhikuiRen             if (ec)
3181a3316fc6SZhikuiRen             {
3182a3316fc6SZhikuiRen                 // TODO Handle for specific error code
3183002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "doClearPostCodes resp_handler got error "
31847e860f15SJohn Edward Broadbent                                  << ec;
3185002d39b4SEd Tanous                 asyncResp->res.result(
3186002d39b4SEd Tanous                     boost::beast::http::status::internal_server_error);
3187a3316fc6SZhikuiRen                 messages::internalError(asyncResp->res);
3188a3316fc6SZhikuiRen                 return;
3189a3316fc6SZhikuiRen             }
3190a3316fc6SZhikuiRen             },
319115124765SJonathan Doman             "xyz.openbmc_project.State.Boot.PostCode0",
319215124765SJonathan Doman             "/xyz/openbmc_project/State/Boot/PostCode0",
3193a3316fc6SZhikuiRen             "xyz.openbmc_project.Collection.DeleteAll", "DeleteAll");
31947e860f15SJohn Edward Broadbent         });
3195a3316fc6SZhikuiRen }
3196a3316fc6SZhikuiRen 
3197a3316fc6SZhikuiRen static void fillPostCodeEntry(
31988d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& aResp,
31996c9a279eSManojkiran Eda     const boost::container::flat_map<
32006c9a279eSManojkiran Eda         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>& postcode,
3201a3316fc6SZhikuiRen     const uint16_t bootIndex, const uint64_t codeIndex = 0,
3202a3316fc6SZhikuiRen     const uint64_t skip = 0, const uint64_t top = 0)
3203a3316fc6SZhikuiRen {
3204a3316fc6SZhikuiRen     // Get the Message from the MessageRegistry
3205fffb8c1fSEd Tanous     const registries::Message* message =
3206fffb8c1fSEd Tanous         registries::getMessage("OpenBMC.0.2.BIOSPOSTCode");
3207a3316fc6SZhikuiRen 
3208a3316fc6SZhikuiRen     uint64_t currentCodeIndex = 0;
3209a3316fc6SZhikuiRen     nlohmann::json& logEntryArray = aResp->res.jsonValue["Members"];
3210a3316fc6SZhikuiRen 
3211a3316fc6SZhikuiRen     uint64_t firstCodeTimeUs = 0;
32126c9a279eSManojkiran Eda     for (const std::pair<uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
32136c9a279eSManojkiran Eda              code : postcode)
3214a3316fc6SZhikuiRen     {
3215a3316fc6SZhikuiRen         currentCodeIndex++;
3216a3316fc6SZhikuiRen         std::string postcodeEntryID =
3217a3316fc6SZhikuiRen             "B" + std::to_string(bootIndex) + "-" +
3218a3316fc6SZhikuiRen             std::to_string(currentCodeIndex); // 1 based index in EntryID string
3219a3316fc6SZhikuiRen 
3220a3316fc6SZhikuiRen         uint64_t usecSinceEpoch = code.first;
3221a3316fc6SZhikuiRen         uint64_t usTimeOffset = 0;
3222a3316fc6SZhikuiRen 
3223a3316fc6SZhikuiRen         if (1 == currentCodeIndex)
3224a3316fc6SZhikuiRen         { // already incremented
3225a3316fc6SZhikuiRen             firstCodeTimeUs = code.first;
3226a3316fc6SZhikuiRen         }
3227a3316fc6SZhikuiRen         else
3228a3316fc6SZhikuiRen         {
3229a3316fc6SZhikuiRen             usTimeOffset = code.first - firstCodeTimeUs;
3230a3316fc6SZhikuiRen         }
3231a3316fc6SZhikuiRen 
3232a3316fc6SZhikuiRen         // skip if no specific codeIndex is specified and currentCodeIndex does
3233a3316fc6SZhikuiRen         // not fall between top and skip
3234a3316fc6SZhikuiRen         if ((codeIndex == 0) &&
3235a3316fc6SZhikuiRen             (currentCodeIndex <= skip || currentCodeIndex > top))
3236a3316fc6SZhikuiRen         {
3237a3316fc6SZhikuiRen             continue;
3238a3316fc6SZhikuiRen         }
3239a3316fc6SZhikuiRen 
32404e0453b1SGunnar Mills         // skip if a specific codeIndex is specified and does not match the
3241a3316fc6SZhikuiRen         // currentIndex
3242a3316fc6SZhikuiRen         if ((codeIndex > 0) && (currentCodeIndex != codeIndex))
3243a3316fc6SZhikuiRen         {
3244a3316fc6SZhikuiRen             // This is done for simplicity. 1st entry is needed to calculate
3245a3316fc6SZhikuiRen             // time offset. To improve efficiency, one can get to the entry
3246a3316fc6SZhikuiRen             // directly (possibly with flatmap's nth method)
3247a3316fc6SZhikuiRen             continue;
3248a3316fc6SZhikuiRen         }
3249a3316fc6SZhikuiRen 
3250a3316fc6SZhikuiRen         // currentCodeIndex is within top and skip or equal to specified code
3251a3316fc6SZhikuiRen         // index
3252a3316fc6SZhikuiRen 
3253a3316fc6SZhikuiRen         // Get the Created time from the timestamp
3254a3316fc6SZhikuiRen         std::string entryTimeStr;
32551d8782e7SNan Zhou         entryTimeStr =
32561d8782e7SNan Zhou             crow::utility::getDateTimeUint(usecSinceEpoch / 1000 / 1000);
3257a3316fc6SZhikuiRen 
3258a3316fc6SZhikuiRen         // assemble messageArgs: BootIndex, TimeOffset(100us), PostCode(hex)
3259a3316fc6SZhikuiRen         std::ostringstream hexCode;
3260a3316fc6SZhikuiRen         hexCode << "0x" << std::setfill('0') << std::setw(2) << std::hex
32616c9a279eSManojkiran Eda                 << std::get<0>(code.second);
3262a3316fc6SZhikuiRen         std::ostringstream timeOffsetStr;
3263a3316fc6SZhikuiRen         // Set Fixed -Point Notation
3264a3316fc6SZhikuiRen         timeOffsetStr << std::fixed;
3265a3316fc6SZhikuiRen         // Set precision to 4 digits
3266a3316fc6SZhikuiRen         timeOffsetStr << std::setprecision(4);
3267a3316fc6SZhikuiRen         // Add double to stream
3268a3316fc6SZhikuiRen         timeOffsetStr << static_cast<double>(usTimeOffset) / 1000 / 1000;
3269a3316fc6SZhikuiRen         std::vector<std::string> messageArgs = {
3270a3316fc6SZhikuiRen             std::to_string(bootIndex), timeOffsetStr.str(), hexCode.str()};
3271a3316fc6SZhikuiRen 
3272a3316fc6SZhikuiRen         // Get MessageArgs template from message registry
3273a3316fc6SZhikuiRen         std::string msg;
3274a3316fc6SZhikuiRen         if (message != nullptr)
3275a3316fc6SZhikuiRen         {
3276a3316fc6SZhikuiRen             msg = message->message;
3277a3316fc6SZhikuiRen 
3278a3316fc6SZhikuiRen             // fill in this post code value
3279a3316fc6SZhikuiRen             int i = 0;
3280a3316fc6SZhikuiRen             for (const std::string& messageArg : messageArgs)
3281a3316fc6SZhikuiRen             {
3282a3316fc6SZhikuiRen                 std::string argStr = "%" + std::to_string(++i);
3283a3316fc6SZhikuiRen                 size_t argPos = msg.find(argStr);
3284a3316fc6SZhikuiRen                 if (argPos != std::string::npos)
3285a3316fc6SZhikuiRen                 {
3286a3316fc6SZhikuiRen                     msg.replace(argPos, argStr.length(), messageArg);
3287a3316fc6SZhikuiRen                 }
3288a3316fc6SZhikuiRen             }
3289a3316fc6SZhikuiRen         }
3290a3316fc6SZhikuiRen 
3291d4342a92STim Lee         // Get Severity template from message registry
3292d4342a92STim Lee         std::string severity;
3293d4342a92STim Lee         if (message != nullptr)
3294d4342a92STim Lee         {
32955f2b84eeSEd Tanous             severity = message->messageSeverity;
3296d4342a92STim Lee         }
3297d4342a92STim Lee 
3298a3316fc6SZhikuiRen         // add to AsyncResp
3299a3316fc6SZhikuiRen         logEntryArray.push_back({});
3300a3316fc6SZhikuiRen         nlohmann::json& bmcLogEntry = logEntryArray.back();
330184afc48bSJason M. Bills         bmcLogEntry["@odata.type"] = "#LogEntry.v1_8_0.LogEntry";
330284afc48bSJason M. Bills         bmcLogEntry["@odata.id"] =
33030fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
330484afc48bSJason M. Bills             postcodeEntryID;
330584afc48bSJason M. Bills         bmcLogEntry["Name"] = "POST Code Log Entry";
330684afc48bSJason M. Bills         bmcLogEntry["Id"] = postcodeEntryID;
330784afc48bSJason M. Bills         bmcLogEntry["Message"] = std::move(msg);
330884afc48bSJason M. Bills         bmcLogEntry["MessageId"] = "OpenBMC.0.2.BIOSPOSTCode";
330984afc48bSJason M. Bills         bmcLogEntry["MessageArgs"] = std::move(messageArgs);
331084afc48bSJason M. Bills         bmcLogEntry["EntryType"] = "Event";
331184afc48bSJason M. Bills         bmcLogEntry["Severity"] = std::move(severity);
331284afc48bSJason M. Bills         bmcLogEntry["Created"] = entryTimeStr;
3313647b3cdcSGeorge Liu         if (!std::get<std::vector<uint8_t>>(code.second).empty())
3314647b3cdcSGeorge Liu         {
3315647b3cdcSGeorge Liu             bmcLogEntry["AdditionalDataURI"] =
3316647b3cdcSGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/" +
3317647b3cdcSGeorge Liu                 postcodeEntryID + "/attachment";
3318647b3cdcSGeorge Liu         }
3319a3316fc6SZhikuiRen     }
3320a3316fc6SZhikuiRen }
3321a3316fc6SZhikuiRen 
33228d1b46d7Szhanghch05 static void getPostCodeForEntry(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3323a3316fc6SZhikuiRen                                 const uint16_t bootIndex,
3324a3316fc6SZhikuiRen                                 const uint64_t codeIndex)
3325a3316fc6SZhikuiRen {
3326a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
33276c9a279eSManojkiran Eda         [aResp, bootIndex,
33286c9a279eSManojkiran Eda          codeIndex](const boost::system::error_code ec,
33296c9a279eSManojkiran Eda                     const boost::container::flat_map<
33306c9a279eSManojkiran Eda                         uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33316c9a279eSManojkiran Eda                         postcode) {
3332a3316fc6SZhikuiRen         if (ec)
3333a3316fc6SZhikuiRen         {
3334a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3335a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3336a3316fc6SZhikuiRen             return;
3337a3316fc6SZhikuiRen         }
3338a3316fc6SZhikuiRen 
3339a3316fc6SZhikuiRen         // skip the empty postcode boots
3340a3316fc6SZhikuiRen         if (postcode.empty())
3341a3316fc6SZhikuiRen         {
3342a3316fc6SZhikuiRen             return;
3343a3316fc6SZhikuiRen         }
3344a3316fc6SZhikuiRen 
3345a3316fc6SZhikuiRen         fillPostCodeEntry(aResp, postcode, bootIndex, codeIndex);
3346a3316fc6SZhikuiRen 
3347a3316fc6SZhikuiRen         aResp->res.jsonValue["Members@odata.count"] =
3348a3316fc6SZhikuiRen             aResp->res.jsonValue["Members"].size();
3349a3316fc6SZhikuiRen         },
335015124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
335115124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3352a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3353a3316fc6SZhikuiRen         bootIndex);
3354a3316fc6SZhikuiRen }
3355a3316fc6SZhikuiRen 
33568d1b46d7Szhanghch05 static void getPostCodeForBoot(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
3357a3316fc6SZhikuiRen                                const uint16_t bootIndex,
3358a3316fc6SZhikuiRen                                const uint16_t bootCount,
33593648c8beSEd Tanous                                const uint64_t entryCount, size_t skip,
33603648c8beSEd Tanous                                size_t top)
3361a3316fc6SZhikuiRen {
3362a3316fc6SZhikuiRen     crow::connections::systemBus->async_method_call(
3363a3316fc6SZhikuiRen         [aResp, bootIndex, bootCount, entryCount, skip,
3364a3316fc6SZhikuiRen          top](const boost::system::error_code ec,
33656c9a279eSManojkiran Eda               const boost::container::flat_map<
33666c9a279eSManojkiran Eda                   uint64_t, std::tuple<uint64_t, std::vector<uint8_t>>>&
33676c9a279eSManojkiran Eda                   postcode) {
3368a3316fc6SZhikuiRen         if (ec)
3369a3316fc6SZhikuiRen         {
3370a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS POST CODE PostCode response error";
3371a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3372a3316fc6SZhikuiRen             return;
3373a3316fc6SZhikuiRen         }
3374a3316fc6SZhikuiRen 
3375a3316fc6SZhikuiRen         uint64_t endCount = entryCount;
3376a3316fc6SZhikuiRen         if (!postcode.empty())
3377a3316fc6SZhikuiRen         {
3378a3316fc6SZhikuiRen             endCount = entryCount + postcode.size();
33793648c8beSEd Tanous             if (skip < endCount && (top + skip) > entryCount)
3380a3316fc6SZhikuiRen             {
33813648c8beSEd Tanous                 uint64_t thisBootSkip =
33823648c8beSEd Tanous                     std::max(static_cast<uint64_t>(skip), entryCount) -
33833648c8beSEd Tanous                     entryCount;
3384a3316fc6SZhikuiRen                 uint64_t thisBootTop =
33853648c8beSEd Tanous                     std::min(static_cast<uint64_t>(top + skip), endCount) -
33863648c8beSEd Tanous                     entryCount;
3387a3316fc6SZhikuiRen 
3388002d39b4SEd Tanous                 fillPostCodeEntry(aResp, postcode, bootIndex, 0, thisBootSkip,
3389002d39b4SEd Tanous                                   thisBootTop);
3390a3316fc6SZhikuiRen             }
3391a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.count"] = endCount;
3392a3316fc6SZhikuiRen         }
3393a3316fc6SZhikuiRen 
3394a3316fc6SZhikuiRen         // continue to previous bootIndex
3395a3316fc6SZhikuiRen         if (bootIndex < bootCount)
3396a3316fc6SZhikuiRen         {
3397a3316fc6SZhikuiRen             getPostCodeForBoot(aResp, static_cast<uint16_t>(bootIndex + 1),
3398a3316fc6SZhikuiRen                                bootCount, endCount, skip, top);
3399a3316fc6SZhikuiRen         }
3400a3316fc6SZhikuiRen         else
3401a3316fc6SZhikuiRen         {
3402a3316fc6SZhikuiRen             aResp->res.jsonValue["Members@odata.nextLink"] =
34030fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/LogServices/PostCodes/Entries?$skip=" +
3404a3316fc6SZhikuiRen                 std::to_string(skip + top);
3405a3316fc6SZhikuiRen         }
3406a3316fc6SZhikuiRen         },
340715124765SJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
340815124765SJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
3409a3316fc6SZhikuiRen         "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodesWithTimeStamp",
3410a3316fc6SZhikuiRen         bootIndex);
3411a3316fc6SZhikuiRen }
3412a3316fc6SZhikuiRen 
34138d1b46d7Szhanghch05 static void
34148d1b46d7Szhanghch05     getCurrentBootNumber(const std::shared_ptr<bmcweb::AsyncResp>& aResp,
34153648c8beSEd Tanous                          size_t skip, size_t top)
3416a3316fc6SZhikuiRen {
3417a3316fc6SZhikuiRen     uint64_t entryCount = 0;
34181e1e598dSJonathan Doman     sdbusplus::asio::getProperty<uint16_t>(
34191e1e598dSJonathan Doman         *crow::connections::systemBus,
34201e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode0",
34211e1e598dSJonathan Doman         "/xyz/openbmc_project/State/Boot/PostCode0",
34221e1e598dSJonathan Doman         "xyz.openbmc_project.State.Boot.PostCode", "CurrentBootCycleCount",
34231e1e598dSJonathan Doman         [aResp, entryCount, skip, top](const boost::system::error_code ec,
34241e1e598dSJonathan Doman                                        const uint16_t bootCount) {
3425a3316fc6SZhikuiRen         if (ec)
3426a3316fc6SZhikuiRen         {
3427a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3428a3316fc6SZhikuiRen             messages::internalError(aResp->res);
3429a3316fc6SZhikuiRen             return;
3430a3316fc6SZhikuiRen         }
34311e1e598dSJonathan Doman         getPostCodeForBoot(aResp, 1, bootCount, entryCount, skip, top);
34321e1e598dSJonathan Doman         });
3433a3316fc6SZhikuiRen }
3434a3316fc6SZhikuiRen 
34357e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntryCollection(App& app)
3436a3316fc6SZhikuiRen {
34377e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
34387e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/")
3439ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntryCollection)
34407e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
344145ca1b86SEd Tanous             [&app](const crow::Request& req,
34427e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3443c937d2bfSEd Tanous         query_param::QueryCapabilities capabilities = {
3444c937d2bfSEd Tanous             .canDelegateTop = true,
3445c937d2bfSEd Tanous             .canDelegateSkip = true,
3446c937d2bfSEd Tanous         };
3447c937d2bfSEd Tanous         query_param::Query delegatedQuery;
3448c937d2bfSEd Tanous         if (!redfish::setUpRedfishRouteWithDelegation(
34493ba00073SCarson Labrado                 app, req, asyncResp, delegatedQuery, capabilities))
345045ca1b86SEd Tanous         {
345145ca1b86SEd Tanous             return;
345245ca1b86SEd Tanous         }
3453a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.type"] =
3454a3316fc6SZhikuiRen             "#LogEntryCollection.LogEntryCollection";
3455a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
3456a3316fc6SZhikuiRen             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3457a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3458a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3459a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3460a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3461a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
34623648c8beSEd Tanous         size_t skip = delegatedQuery.skip.value_or(0);
34633648c8beSEd Tanous         size_t top =
34643648c8beSEd Tanous             delegatedQuery.top.value_or(query_param::maxEntriesPerPage);
34653648c8beSEd Tanous         getCurrentBootNumber(asyncResp, skip, top);
34667e860f15SJohn Edward Broadbent         });
3467a3316fc6SZhikuiRen }
3468a3316fc6SZhikuiRen 
3469647b3cdcSGeorge Liu /**
3470647b3cdcSGeorge Liu  * @brief Parse post code ID and get the current value and index value
3471647b3cdcSGeorge Liu  *        eg: postCodeID=B1-2, currentValue=1, index=2
3472647b3cdcSGeorge Liu  *
3473647b3cdcSGeorge Liu  * @param[in]  postCodeID     Post Code ID
3474647b3cdcSGeorge Liu  * @param[out] currentValue   Current value
3475647b3cdcSGeorge Liu  * @param[out] index          Index value
3476647b3cdcSGeorge Liu  *
3477647b3cdcSGeorge Liu  * @return bool true if the parsing is successful, false the parsing fails
3478647b3cdcSGeorge Liu  */
3479647b3cdcSGeorge Liu inline static bool parsePostCode(const std::string& postCodeID,
3480647b3cdcSGeorge Liu                                  uint64_t& currentValue, uint16_t& index)
3481647b3cdcSGeorge Liu {
3482647b3cdcSGeorge Liu     std::vector<std::string> split;
3483647b3cdcSGeorge Liu     boost::algorithm::split(split, postCodeID, boost::is_any_of("-"));
3484647b3cdcSGeorge Liu     if (split.size() != 2 || split[0].length() < 2 || split[0].front() != 'B')
3485647b3cdcSGeorge Liu     {
3486647b3cdcSGeorge Liu         return false;
3487647b3cdcSGeorge Liu     }
3488647b3cdcSGeorge Liu 
3489ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3490647b3cdcSGeorge Liu     const char* start = split[0].data() + 1;
3491ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3492647b3cdcSGeorge Liu     const char* end = split[0].data() + split[0].size();
3493647b3cdcSGeorge Liu     auto [ptrIndex, ecIndex] = std::from_chars(start, end, index);
3494647b3cdcSGeorge Liu 
3495647b3cdcSGeorge Liu     if (ptrIndex != end || ecIndex != std::errc())
3496647b3cdcSGeorge Liu     {
3497647b3cdcSGeorge Liu         return false;
3498647b3cdcSGeorge Liu     }
3499647b3cdcSGeorge Liu 
3500647b3cdcSGeorge Liu     start = split[1].data();
3501ca45aa3cSEd Tanous 
3502ca45aa3cSEd Tanous     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3503647b3cdcSGeorge Liu     end = split[1].data() + split[1].size();
3504647b3cdcSGeorge Liu     auto [ptrValue, ecValue] = std::from_chars(start, end, currentValue);
3505647b3cdcSGeorge Liu 
3506517d9a58STony Lee     return ptrValue == end && ecValue == std::errc();
3507647b3cdcSGeorge Liu }
3508647b3cdcSGeorge Liu 
3509647b3cdcSGeorge Liu inline void requestRoutesPostCodesEntryAdditionalData(App& app)
3510647b3cdcSGeorge Liu {
35110fda0f12SGeorge Liu     BMCWEB_ROUTE(
35120fda0f12SGeorge Liu         app,
35130fda0f12SGeorge Liu         "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/attachment/")
3514647b3cdcSGeorge Liu         .privileges(redfish::privileges::getLogEntry)
3515647b3cdcSGeorge Liu         .methods(boost::beast::http::verb::get)(
351645ca1b86SEd Tanous             [&app](const crow::Request& req,
3517647b3cdcSGeorge Liu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3518647b3cdcSGeorge Liu                    const std::string& postCodeID) {
35193ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
352045ca1b86SEd Tanous         {
352145ca1b86SEd Tanous             return;
352245ca1b86SEd Tanous         }
3523002d39b4SEd Tanous         if (!http_helpers::isOctetAccepted(req.getHeaderValue("Accept")))
3524647b3cdcSGeorge Liu         {
3525002d39b4SEd Tanous             asyncResp->res.result(boost::beast::http::status::bad_request);
3526647b3cdcSGeorge Liu             return;
3527647b3cdcSGeorge Liu         }
3528647b3cdcSGeorge Liu 
3529647b3cdcSGeorge Liu         uint64_t currentValue = 0;
3530647b3cdcSGeorge Liu         uint16_t index = 0;
3531647b3cdcSGeorge Liu         if (!parsePostCode(postCodeID, currentValue, index))
3532647b3cdcSGeorge Liu         {
3533002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "LogEntry", postCodeID);
3534647b3cdcSGeorge Liu             return;
3535647b3cdcSGeorge Liu         }
3536647b3cdcSGeorge Liu 
3537647b3cdcSGeorge Liu         crow::connections::systemBus->async_method_call(
3538647b3cdcSGeorge Liu             [asyncResp, postCodeID, currentValue](
3539647b3cdcSGeorge Liu                 const boost::system::error_code ec,
3540002d39b4SEd Tanous                 const std::vector<std::tuple<uint64_t, std::vector<uint8_t>>>&
3541002d39b4SEd Tanous                     postcodes) {
3542647b3cdcSGeorge Liu             if (ec.value() == EBADR)
3543647b3cdcSGeorge Liu             {
3544002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3545002d39b4SEd Tanous                                            postCodeID);
3546647b3cdcSGeorge Liu                 return;
3547647b3cdcSGeorge Liu             }
3548647b3cdcSGeorge Liu             if (ec)
3549647b3cdcSGeorge Liu             {
3550647b3cdcSGeorge Liu                 BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
3551647b3cdcSGeorge Liu                 messages::internalError(asyncResp->res);
3552647b3cdcSGeorge Liu                 return;
3553647b3cdcSGeorge Liu             }
3554647b3cdcSGeorge Liu 
3555647b3cdcSGeorge Liu             size_t value = static_cast<size_t>(currentValue) - 1;
3556002d39b4SEd Tanous             if (value == std::string::npos || postcodes.size() < currentValue)
3557647b3cdcSGeorge Liu             {
3558647b3cdcSGeorge Liu                 BMCWEB_LOG_ERROR << "Wrong currentValue value";
3559002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3560002d39b4SEd Tanous                                            postCodeID);
3561647b3cdcSGeorge Liu                 return;
3562647b3cdcSGeorge Liu             }
3563647b3cdcSGeorge Liu 
35649eb808c1SEd Tanous             const auto& [tID, c] = postcodes[value];
356546ff87baSEd Tanous             if (c.empty())
3566647b3cdcSGeorge Liu             {
3567647b3cdcSGeorge Liu                 BMCWEB_LOG_INFO << "No found post code data";
3568002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "LogEntry",
3569002d39b4SEd Tanous                                            postCodeID);
3570647b3cdcSGeorge Liu                 return;
3571647b3cdcSGeorge Liu             }
357246ff87baSEd Tanous             // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
357346ff87baSEd Tanous             const char* d = reinterpret_cast<const char*>(c.data());
357446ff87baSEd Tanous             std::string_view strData(d, c.size());
3575647b3cdcSGeorge Liu 
3576647b3cdcSGeorge Liu             asyncResp->res.addHeader("Content-Type",
3577647b3cdcSGeorge Liu                                      "application/octet-stream");
3578002d39b4SEd Tanous             asyncResp->res.addHeader("Content-Transfer-Encoding", "Base64");
3579002d39b4SEd Tanous             asyncResp->res.body() = crow::utility::base64encode(strData);
3580647b3cdcSGeorge Liu             },
3581647b3cdcSGeorge Liu             "xyz.openbmc_project.State.Boot.PostCode0",
3582647b3cdcSGeorge Liu             "/xyz/openbmc_project/State/Boot/PostCode0",
3583002d39b4SEd Tanous             "xyz.openbmc_project.State.Boot.PostCode", "GetPostCodes", index);
3584647b3cdcSGeorge Liu         });
3585647b3cdcSGeorge Liu }
3586647b3cdcSGeorge Liu 
35877e860f15SJohn Edward Broadbent inline void requestRoutesPostCodesEntry(App& app)
3588a3316fc6SZhikuiRen {
35897e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(
35907e860f15SJohn Edward Broadbent         app, "/redfish/v1/Systems/system/LogServices/PostCodes/Entries/<str>/")
3591ed398213SEd Tanous         .privileges(redfish::privileges::getLogEntry)
35927e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
359345ca1b86SEd Tanous             [&app](const crow::Request& req,
35947e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
35957e860f15SJohn Edward Broadbent                    const std::string& targetID) {
35963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
359745ca1b86SEd Tanous         {
359845ca1b86SEd Tanous             return;
359945ca1b86SEd Tanous         }
3600647b3cdcSGeorge Liu         uint16_t bootIndex = 0;
3601647b3cdcSGeorge Liu         uint64_t codeIndex = 0;
3602647b3cdcSGeorge Liu         if (!parsePostCode(targetID, codeIndex, bootIndex))
3603a3316fc6SZhikuiRen         {
3604a3316fc6SZhikuiRen             // Requested ID was not found
3605ace85d60SEd Tanous             messages::resourceMissingAtURI(asyncResp->res, req.urlView);
3606a3316fc6SZhikuiRen             return;
3607a3316fc6SZhikuiRen         }
3608a3316fc6SZhikuiRen         if (bootIndex == 0 || codeIndex == 0)
3609a3316fc6SZhikuiRen         {
3610a3316fc6SZhikuiRen             BMCWEB_LOG_DEBUG << "Get Post Code invalid entry string "
36117e860f15SJohn Edward Broadbent                              << targetID;
3612a3316fc6SZhikuiRen         }
3613a3316fc6SZhikuiRen 
3614002d39b4SEd Tanous         asyncResp->res.jsonValue["@odata.type"] = "#LogEntry.v1_4_0.LogEntry";
3615a3316fc6SZhikuiRen         asyncResp->res.jsonValue["@odata.id"] =
36160fda0f12SGeorge Liu             "/redfish/v1/Systems/system/LogServices/PostCodes/Entries";
3617a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Name"] = "BIOS POST Code Log Entries";
3618a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Description"] =
3619a3316fc6SZhikuiRen             "Collection of POST Code Log Entries";
3620a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
3621a3316fc6SZhikuiRen         asyncResp->res.jsonValue["Members@odata.count"] = 0;
3622a3316fc6SZhikuiRen 
3623a3316fc6SZhikuiRen         getPostCodeForEntry(asyncResp, bootIndex, codeIndex);
36247e860f15SJohn Edward Broadbent         });
3625a3316fc6SZhikuiRen }
3626a3316fc6SZhikuiRen 
36271da66f75SEd Tanous } // namespace redfish
3628